| | 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 word_size_bits = 32 |
| | 23 | let word_size_bytes = word_size_bits / 8 |
| | 24 | let word_size = word_size_bytes |
| | 25 | |
| | 26 | let name {name; _} = |
| | 27 | name |
| | 28 | |
| | 29 | let formals {formals; _} = |
| | 30 | formals |
| | 31 | |
| | 32 | let alloc offset_from_frame_pointer ~escapes = |
| | 33 | if escapes then |
| | 34 | InFrame {offset_from_frame_pointer} |
| | 35 | else |
| | 36 | InReg {register = Temp.Temp.gen ()} |
| | 37 | |
| | 38 | let alloc_local _ ~escapes = |
| | 39 | (* FIXME: offset_from_frame_pointer. With neither mutation nor new frame? *) |
| | 40 | let offset_from_frame_pointer = 0 in |
| | 41 | alloc offset_from_frame_pointer ~escapes |
| | 42 | |
| | 43 | let make ~name ~formals = |
| | 44 | (* p.136: For each formal parameter, "newFrame" must calculate two things: |
| | 45 | * - How the parameter will be seen from inside the function |
| | 46 | * (in a register, or in a frame location); |
| | 47 | * - What instructions must be produced to implement the "view shift" |
| | 48 | * *) |
| | 49 | let formals, locals_count = |
| | 50 | (* TODO: What should offset increment be? Word? *) |
| | 51 | List.fold_left formals ~init:([], 0) ~f:(fun (formals, offset) escapes -> |
| | 52 | ((alloc offset ~escapes) :: formals, succ offset) |
| | 53 | ) |
| | 54 | in |
| | 55 | { name |
| | 56 | ; formals |
| | 57 | ; locals_count |
| | 58 | ; instructions = () (* TODO: instructions for view shift *) |
| | 59 | } |