From 720ffc02dc4d8614b1f8c3c31dd161984b1c6642 Mon Sep 17 00:00:00 2001 From: pacien Date: Mon, 30 Apr 2018 22:38:43 +0200 Subject: Refactor reg and label creation --- src/eurm.ml | 61 +++++++++++++++++++++++++++++++------------------------------ 1 file changed, 31 insertions(+), 30 deletions(-) diff --git a/src/eurm.ml b/src/eurm.ml index cc81aef..5cb487b 100644 --- a/src/eurm.ml +++ b/src/eurm.ml @@ -34,15 +34,16 @@ let build_initial_state eurmcmds = | EqPredicate(r1, r2, _) | GEqPredicate(r1, r2, _) | GTPredicate(r1, r2, _) | LEqPredicate(r1, r2, _) | LTPredicate(r1, r2, _) -> max r1 r2 | _ -> 0 - in { - max_reg = List.fold_left (fun acc instr -> max acc (max_reg_of_instr instr)) 0 eurmcmds; - label_count = List.fold_left (fun acc instr -> acc + (match instr with | Label(_) -> 1 | _ -> 0)) 0 eurmcmds - } + in + { max_reg = List.fold_left (fun acc instr -> max acc (max_reg_of_instr instr)) 0 eurmcmds; + label_count = List.fold_left (fun acc instr -> acc + (match instr with | Label(_) -> 1 | _ -> 0)) 0 eurmcmds } -let add_reg_label state new_regs new_labels = { - max_reg = state.max_reg + new_regs; - label_count = state.label_count + new_labels -} +let add_reg_label state new_regs new_labels = + { max_reg = state.max_reg + new_regs; + label_count = state.label_count + new_labels } + +let make_reg state offset = state.max_reg + offset +let make_label state offset = string_of_int (state.label_count + offset) let rec apply_transform transform_func state = function | [] -> [], state @@ -54,29 +55,29 @@ let rec apply_transform transform_func state = function let compile_stage1 eurmcmds state = let transform state = function | Dec(r) -> - let new_reg = state.max_reg + 1 + let new_reg = make_reg state 1 in [ Zero(new_reg); Inc(new_reg); Sub(r, new_reg) ], add_reg_label state 1 0 | GEqPredicate(r1, r2, l) -> - let new_reg = state.max_reg + 1 + let new_reg = make_reg state 1 in [ Copy(new_reg, r1); Inc(new_reg); GTPredicate(new_reg, r2, l) ], add_reg_label state 1 0 | LEqPredicate(r1, r2, l) -> - let new_reg = state.max_reg + 1 + let new_reg = make_reg state 1 in [ Copy(new_reg, r2); Inc(new_reg); GTPredicate(new_reg, r1, l) ], add_reg_label state 1 0 | Mult(r1, r2) -> - let ctr_reg = state.max_reg + 1 and res_reg = state.max_reg + 2 - and start_label = string_of_int (state.label_count + 1) and end_label = string_of_int (state.label_count + 2) + let ctr_reg = make_reg state 1 and res_reg = make_reg state 2 + and start_label = make_label state 1 and end_label = make_label state 2 in [ Zero(ctr_reg); Zero(res_reg); Label(start_label); EqPredicate(ctr_reg, r2, end_label); Add(res_reg, r1); Inc(ctr_reg); Goto(start_label); Label(end_label); Copy(r1, res_reg) ], add_reg_label state 2 2 | ZeroPredicate(r, l) -> - let new_reg = state.max_reg + 1 + let new_reg = make_reg state 1 in [ Zero(new_reg); EqPredicate(r, new_reg, l) ], add_reg_label state 1 0 @@ -88,23 +89,23 @@ let compile_stage1 eurmcmds state = let compile_stage2 eurmcmds state = let transform state = function | Add(r1, r2) -> - let ctr_reg = state.max_reg + 1 - and start_label = string_of_int (state.label_count + 1) and end_label = string_of_int (state.label_count + 2) + let ctr_reg = make_reg state 1 + and start_label = make_label state 1 and end_label = make_label state 2 in [ Zero(ctr_reg); Label(start_label); EqPredicate(ctr_reg, r2, end_label); Inc(r1); Inc(ctr_reg); Goto(start_label); Label(end_label) ], add_reg_label state 1 2 | GTPredicate(r1, r2, l) -> - let aux_reg = state.max_reg + 1 - and start_label = string_of_int (state.label_count + 1) and end_label = string_of_int (state.label_count + 2) + let aux_reg = make_reg state 1 + and start_label = make_label state 1 and end_label = make_label state 2 in [ Zero(aux_reg); Label(start_label); EqPredicate(aux_reg, r1, end_label); EqPredicate(aux_reg, r2, l); Inc(aux_reg); Goto(start_label); Label(end_label) ], add_reg_label state 1 2 | Sub(r1, r2) -> - let diff_reg = state.max_reg + 1 and aux1_reg = state.max_reg + 2 and aux2_reg = state.max_reg + 3 - and start_label = string_of_int (state.label_count + 1) and end_label = string_of_int (state.label_count + 2) - and error_label = string_of_int (state.label_count + 3) + let diff_reg = make_reg state 1 and aux1_reg = make_reg state 2 and aux2_reg = make_reg state 3 + and start_label = make_label state 1 and end_label = make_label state 2 + and error_label = make_label state 3 in [ Zero(diff_reg); Copy(aux1_reg, r1); Copy(aux2_reg, r2); Label(start_label); EqPredicate(aux1_reg, r2, error_label); EqPredicate(aux2_reg, r1, end_label); Inc(diff_reg); Inc(aux1_reg); Inc(aux2_reg); Goto(start_label); @@ -119,7 +120,7 @@ let compile_stage2 eurmcmds state = let compile_stage3 eurmcmds state = let transform state = function | Goto(lbl) -> - let dummy_reg = state.max_reg + 1 + let dummy_reg = make_reg state 1 in [ Zero(dummy_reg); EqPredicate(dummy_reg, dummy_reg, lbl) ], add_reg_label state 1 0 | any -> [ any ], state @@ -131,14 +132,14 @@ let compile_stage4 eurmcmds state = in let build_label_table = List.iteri (fun lineo cmd -> match cmd with | Label(lbl) -> Hashtbl.add label_table lbl lineo | _ -> ()) in let transform state = function - | Inc(r) -> [ URMSucc(r) ], state - | Zero(r) -> [ URMZero(r) ], state - | Copy(r1, r2) -> [ URMCopy(r1, r2) ], state - | EqPredicate(r1, r2, lbl) -> [ URMJump(r1, r2, Hashtbl.find label_table lbl) ], state - | Label(_) -> - let dummy_reg = state.max_reg + 1 - in [ URMZero(dummy_reg) ], add_reg_label state 1 0 - | _ -> failwith "Invalid_argument" + | Inc(r) -> [ URMSucc(r) ], state + | Zero(r) -> [ URMZero(r) ], state + | Copy(r1, r2) -> [ URMCopy(r1, r2) ], state + | EqPredicate(r1, r2, lbl) -> [ URMJump(r1, r2, Hashtbl.find label_table lbl) ], state + | Label(_) -> + let dummy_reg = make_reg state 1 + in [ URMZero(dummy_reg) ], add_reg_label state 1 0 + | _ -> failwith "Invalid_argument" in build_label_table eurmcmds; apply_transform (transform) state eurmcmds let urm_from_eurm eurmcmds = -- cgit v1.2.3