| 1 | module List = ListLabels |
| 2 | |
| 3 | module Frame = Tiger_frame |
| 4 | module Temp = Tiger_temp |
| 5 | |
| 6 | module Level = struct |
| 7 | type t = |
| 8 | { parent : t option |
| 9 | ; name : Temp.Label.t |
| 10 | ; formals : bool list |
| 11 | ; frame : Frame.t |
| 12 | } |
| 13 | |
| 14 | let init = |
| 15 | let name = Temp.Label.gen () in |
| 16 | let formals = [] in |
| 17 | { parent = None |
| 18 | ; name |
| 19 | ; formals |
| 20 | ; frame = Frame.make ~name ~formals |
| 21 | } |
| 22 | |
| 23 | let next t ~name ~formals = |
| 24 | (* Adding the extra parameter for the static link. See p. 142 *) |
| 25 | let formals = true :: formals in |
| 26 | { parent = Some t |
| 27 | ; name |
| 28 | ; formals |
| 29 | ; frame = Frame.make ~name ~formals |
| 30 | } |
| 31 | |
| 32 | let formals = function {formals; _} -> |
| 33 | formals |
| 34 | |
| 35 | let frame = function {frame; _} -> |
| 36 | frame |
| 37 | end |
| 38 | |
| 39 | type exp = unit |
| 40 | |
| 41 | type access = |
| 42 | (* must know about static links *) |
| 43 | { level : Level.t |
| 44 | ; frame_access : Frame.access |
| 45 | } |
| 46 | |
| 47 | let alloc_local ~level ~escapes = |
| 48 | { level |
| 49 | ; frame_access = Frame.alloc_local (Level.frame level) ~escapes |
| 50 | } |
| 51 | |
| 52 | let formals ~level = |
| 53 | (* FIXME: This seems wrong. Should we call Frame.formals? *) |
| 54 | List.map (Level.formals level) ~f:(fun escapes -> |
| 55 | alloc_local ~level ~escapes |
| 56 | ) |