| 1 | module List = ListLabels |
| 2 | |
| 3 | module Temp = Tiger_temp |
| 4 | |
| 5 | type access = |
| 6 | | InFrame of {offset_from_frame_pointer : int} |
| 7 | | InReg of {register : Temp.Temp.t} |
| 8 | |
| 9 | type t = |
| 10 | (* p.136 Frame.t is a data structure holding: |
| 11 | * - the locations of all the formals |
| 12 | * - instructions required to implement the "view shift" |
| 13 | * - the number of locals allocated so far |
| 14 | * - the `label` at which the function's machine code is to begin (see p.140) |
| 15 | * *) |
| 16 | { name : Temp.Label.t |
| 17 | ; formals : access list |
| 18 | ; locals_count : int |
| 19 | ; instructions : unit (* TODO: instructions for view shift *) |
| 20 | } |
| 21 | |
| 22 | let name {name; _} = |
| 23 | name |
| 24 | |
| 25 | let formals {formals; _} = |
| 26 | formals |
| 27 | |
| 28 | let alloc offset_from_frame_pointer ~escapes = |
| 29 | if escapes then |
| 30 | InFrame {offset_from_frame_pointer} |
| 31 | else |
| 32 | InReg {register = Temp.Temp.gen ()} |
| 33 | |
| 34 | let alloc_local _ ~escapes = |
| 35 | (* FIXME: offset_from_frame_pointer. With neither mutation nor new frame? *) |
| 36 | let offset_from_frame_pointer = 0 in |
| 37 | alloc offset_from_frame_pointer ~escapes |
| 38 | |
| 39 | let make ~name ~formals = |
| 40 | (* p.136: For each formal parameter, "newFrame" must calculate two things: |
| 41 | * - How the parameter will be seen from inside the function |
| 42 | * (in a register, or in a frame location); |
| 43 | * - What instructions must be produced to implement the "view shift" |
| 44 | * *) |
| 45 | let formals, locals_count = |
| 46 | (* TODO: What should offset increment be? Word? *) |
| 47 | List.fold_left formals ~init:([], 0) ~f:(fun (formals, offset) escapes -> |
| 48 | ((alloc offset ~escapes) :: formals, succ offset) |
| 49 | ) |
| 50 | in |
| 51 | { name |
| 52 | ; formals |
| 53 | ; locals_count |
| 54 | ; instructions = () (* TODO: instructions for view shift *) |
| 55 | } |