diff options
-rw-r--r-- | src/common.mli | 2 | ||||
-rw-r--r-- | src/eurm.ml | 16 |
2 files changed, 13 insertions, 5 deletions
diff --git a/src/common.mli b/src/common.mli index 250a804..2199254 100644 --- a/src/common.mli +++ b/src/common.mli | |||
@@ -44,7 +44,7 @@ type urm = { | |||
44 | type state = { | 44 | type state = { |
45 | max_reg : int; | 45 | max_reg : int; |
46 | label_count : int; | 46 | label_count : int; |
47 | label_table : (string, int) Hashtbl.t | 47 | label_table : (label * int) list |
48 | } | 48 | } |
49 | 49 | ||
50 | exception Syntax_error | 50 | exception Syntax_error |
diff --git a/src/eurm.ml b/src/eurm.ml index 43d3791..68605e7 100644 --- a/src/eurm.ml +++ b/src/eurm.ml | |||
@@ -35,13 +35,18 @@ let build_initial_state eurmcmds = | |||
35 | in | 35 | in |
36 | { max_reg = List.fold_left (fun acc instr -> max acc (max_reg_of_instr instr)) 0 eurmcmds; | 36 | { max_reg = List.fold_left (fun acc instr -> max acc (max_reg_of_instr instr)) 0 eurmcmds; |
37 | label_count = List.fold_left (fun acc instr -> acc + (match instr with | Label(_) -> 1 | _ -> 0)) 0 eurmcmds; | 37 | label_count = List.fold_left (fun acc instr -> acc + (match instr with | Label(_) -> 1 | _ -> 0)) 0 eurmcmds; |
38 | label_table = Hashtbl.create 100 } | 38 | label_table = [] } |
39 | 39 | ||
40 | let add_reg_label state new_regs new_labels = | 40 | let add_reg_label state new_regs new_labels = |
41 | { max_reg = state.max_reg + new_regs; | 41 | { max_reg = state.max_reg + new_regs; |
42 | label_count = state.label_count + new_labels; | 42 | label_count = state.label_count + new_labels; |
43 | label_table = state.label_table } | 43 | label_table = state.label_table } |
44 | 44 | ||
45 | let put_labels state tbl = | ||
46 | { max_reg = state.max_reg; | ||
47 | label_count = state.label_count; | ||
48 | label_table = tbl } | ||
49 | |||
45 | let make_reg state offset = state.max_reg + offset | 50 | let make_reg state offset = state.max_reg + offset |
46 | let make_label state offset = string_of_int (state.label_count + offset) | 51 | let make_label state offset = string_of_int (state.label_count + offset) |
47 | 52 | ||
@@ -128,13 +133,16 @@ let compile_stage3 eurmcmds state = | |||
128 | in apply_transform (transform) state eurmcmds | 133 | in apply_transform (transform) state eurmcmds |
129 | 134 | ||
130 | let compile_stage4 eurmcmds state = | 135 | let compile_stage4 eurmcmds state = |
131 | let build_label_table state eurmcmds= | 136 | let build_label_table state eurmcmds = |
132 | List.iteri (fun lineo cmd -> match cmd with | Label(lbl) -> Hashtbl.add state.label_table lbl lineo | _ -> ()) eurmcmds; state | 137 | List.mapi (fun lineno cmd -> (cmd, lineno)) eurmcmds |
138 | |> List.filter (fun (cmd, _) -> match cmd with | Label(_) -> true | _ -> false) | ||
139 | |> List.map (fun (cmd, lineno) -> match cmd with | Label(lbl) -> (lbl, lineno) | _ -> failwith "Unexpected state") | ||
140 | |> put_labels state | ||
133 | in let transform state = function | 141 | in let transform state = function |
134 | | Inc(r) -> [ URMSucc(r) ], state | 142 | | Inc(r) -> [ URMSucc(r) ], state |
135 | | Zero(r) -> [ URMZero(r) ], state | 143 | | Zero(r) -> [ URMZero(r) ], state |
136 | | Copy(r1, r2) -> [ URMCopy(r1, r2) ], state | 144 | | Copy(r1, r2) -> [ URMCopy(r1, r2) ], state |
137 | | EqPredicate(r1, r2, lbl) -> [ URMJump(r1, r2, Hashtbl.find state.label_table lbl) ], state | 145 | | EqPredicate(r1, r2, lbl) -> [ URMJump(r1, r2, List.assoc lbl state.label_table) ], state |
138 | | Label(_) -> | 146 | | Label(_) -> |
139 | let dummy_reg = make_reg state 1 | 147 | let dummy_reg = make_reg state 1 |
140 | in [ URMZero(dummy_reg) ], add_reg_label state 1 0 | 148 | in [ URMZero(dummy_reg) ], add_reg_label state 1 0 |