From 8e34ad7563ec54b3606fd530da0b4268d662740a Mon Sep 17 00:00:00 2001 From: pacien Date: Mon, 30 Apr 2018 18:48:42 +0200 Subject: Implement eurm compilation stage 3 and 4 --- src/eurm.ml | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/src/eurm.ml b/src/eurm.ml index 0caabf0..2b8cc5b 100644 --- a/src/eurm.ml +++ b/src/eurm.ml @@ -37,6 +37,11 @@ let build_initial_state 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 rec apply_transform transform_func state = function | [] -> [], state | cmd :: tail -> @@ -49,29 +54,29 @@ let compile_stage1 eurmcmds state = | Dec(r) -> let new_reg = state.max_reg + 1 in [ Zero(new_reg); Inc(new_reg); Sub(r, new_reg) ], - { max_reg = new_reg; label_count = state.label_count } + add_reg_label state 1 0 | GEqPredicate(r1, r2, l) -> let new_reg = state.max_reg + 1 in [ Copy(new_reg, r1); Inc(new_reg); GTPredicate(new_reg, r2, l) ], - { max_reg = new_reg; label_count = state.label_count } + add_reg_label state 1 0 | LEqPredicate(r1, r2, l) -> let new_reg = state.max_reg + 1 in [ Copy(new_reg, r2); Inc(new_reg); GTPredicate(new_reg, r1, l) ], - { max_reg = new_reg; label_count = state.label_count } + 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) 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) ], - { max_reg = state.max_reg + 2; label_count = state.label_count + 2} + add_reg_label state 2 2 | ZeroPredicate(r, l) -> let new_reg = state.max_reg + 1 in [ Zero(new_reg); EqPredicate(r, new_reg, l) ], - { max_reg = new_reg; label_count = state.label_count } + add_reg_label state 1 0 | LTPredicate(r1, r2, l) -> [ GTPredicate(r2, r1, l) ], state | any -> [ any ], state @@ -85,14 +90,14 @@ let compile_stage2 eurmcmds state = and start_label = string_of_int (state.label_count + 1) and end_label = string_of_int (state.label_count + 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) ], - { max_reg = state.label_count + 1; label_count = state.label_count + 2 } + 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) 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) ], - { max_reg = state.label_count + 1; label_count = state.label_count + 2 } + 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 @@ -102,14 +107,35 @@ let compile_stage2 eurmcmds state = EqPredicate(aux1_reg, r2, error_label); EqPredicate(aux2_reg, r1, end_label); Inc(diff_reg); Inc(aux1_reg); Inc(aux2_reg); Goto(start_label); Label(error_label); Quit; Label(end_label); Copy(r1, diff_reg) ], - { max_reg = state.label_count + 3; label_count = state.label_count + 3 } + add_reg_label state 3 3 + + | any -> [ any ], state + in apply_transform (transform) state eurmcmds + +let compile_stage3 eurmcmds state = + let transform = function + | Goto(lbl) -> + let dummy_reg = state.max_reg + 1 + in [ Zero(dummy_reg); EqPredicate(dummy_reg, dummy_reg, lbl) ], + add_reg_label state 1 0 | any -> [ any ], state in apply_transform (transform) state eurmcmds -let compile_stage3 eurmcmds state = eurmcmds, state -let compile_stage4 eurmcmds state = [URMZero(0)], state +let compile_stage4 eurmcmds state = + let label_table = Hashtbl.create 100 + in let build_label_table = + List.iteri (fun lineo cmd -> match cmd with | Label(lbl) -> Hashtbl.add label_table lbl lineo | _ -> ()) + in let transform = function + | Inc(r) -> [ URMSucc(r) ], state + | Zero(r) -> [ URMZero(r) ], 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" + in build_label_table eurmcmds; apply_transform (transform) state eurmcmds let urm_from_eurm eurmcmds = let chain transform (eurmcmds, compile_state) = transform eurmcmds compile_state -- cgit v1.2.3