From 1b8fa63e3145a3778e065c42e9fc189344985c99 Mon Sep 17 00:00:00 2001 From: pacien Date: Sat, 21 Apr 2018 15:01:17 +0200 Subject: Add EURM interface --- common.mli | 27 +++++++++++++++++++++++++-- eurml.mli | 14 ++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 eurml.mli diff --git a/common.mli b/common.mli index a62b083..4fa838d 100644 --- a/common.mli +++ b/common.mli @@ -4,15 +4,35 @@ *) type line = int +type label = string type regidx = int type regval = int type reg = Reg of regidx * regval type urmcmd = + | URMCopy of regidx * regidx + | URMJump of regidx * regidx * line + | URMSucc of regidx + | URMZero of regidx + +type eurmcmd = + | Add of regidx * regidx + | Comment of string | Copy of regidx * regidx - | Jump of regidx * regidx * line - | Succ of regidx + | Dec of regidx + | EqPredicate of regidx * regidx * label + | GEqPredicate of regidx * regidx * label + | GTPredicate of regidx * regidx * label + | Goto of label + | Inc of regidx + | Label of label + | LEqPredicate of regidx * regidx * label + | LTPredicate of regidx * regidx * label + | Mult of regidx * regidx + | Quit + | Sub of regidx * regidx | Zero of regidx + | ZeroPredicate of regidx * label type instptr = InstPtr of (line * urmcmd) list * (line * urmcmd) list @@ -21,4 +41,7 @@ type urm = { regs : reg list } +type state = { todo : int } + exception Syntax_error + diff --git a/eurml.mli b/eurml.mli new file mode 100644 index 0000000..1b13dca --- /dev/null +++ b/eurml.mli @@ -0,0 +1,14 @@ +(* + * UPEM / L3 / Functional programming / Project: URM + * Pacien TRAN-GIRARD, Adam NAILI + *) + +open Common + +val compile_preprocess : eurmcmd list -> eurmcmd list +val compile_stage1 : eurmcmd list -> state -> eurmcmd list * state +val compile_stage2 : eurmcmd list -> state -> eurmcmd list * state +val compile_stage3 : eurmcmd list -> state -> eurmcmd list * state +val compile_stage4 : eurmcmd list -> state -> urmcmd list * state +val urm_from_eurm : eurmcmd list -> urmcmd list + -- cgit v1.2.3 From 153df26a972405c11968f7833190346bc69e07ec Mon Sep 17 00:00:00 2001 From: pacien Date: Sat, 21 Apr 2018 15:01:36 +0200 Subject: Add EURM conv test --- eurml_test.ml | 75 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 eurml_test.ml diff --git a/eurml_test.ml b/eurml_test.ml new file mode 100644 index 0000000..7dc6e5e --- /dev/null +++ b/eurml_test.ml @@ -0,0 +1,75 @@ +(* + * UPEM / L3 / Functional programming / Project: URM + * Pacien TRAN-GIRARD, Adam NAILI + *) + +open Common +open Urm +open Eurm +open Kaputt.Abbreviations + +let () = + Test.add_simple_test + ~title:"example_eurm_factorial_conversion" + (fun () -> + let input_eurm = [ + Comment "Compute r1! and place the result in r1"; + ZeroPredicate (1, "r1=0"); + Goto "r1>0"; + Comment "r1 holds 0"; + Label "r1=0"; + Inc 1; + Goto "done"; + Comment "r1 holds a positive integer"; + Label "r1>0"; + Copy (2, 1); + Zero 1; + Inc 1; + Zero 3; + Inc 3; + Comment "main loop"; + Label "loop"; + Mult (1, 3); + EqPredicate (2, 3, "done"); + Inc 3; + Goto "loop"; + Label "done"; + Quit] + and expected_urm = [ + URMZero 4; + URMJump (1, 4, 4); + URMZero 8; + URMJump (8, 8, 7); + URMSucc 1; + URMZero 9; + URMJump (9, 9, 29); + URMCopy (2, 1); + URMZero 1; + URMSucc 1; + URMZero 3; + URMSucc 3; + URMCopy (5, 1); + URMZero 1; + URMZero 6; + URMJump (3, 6, 25); + URMZero 7; + URMJump (5, 7, 22); + URMSucc 1; + URMSucc 7; + URMZero 10; + URMJump (10, 10, 17); + URMSucc 6; + URMZero 11; + URMJump (11, 11, 15); + URMJump (2, 3, 29); + URMSucc 3; + URMZero 12; + URMJump (12, 12, 12); + URMZero 13; + URMJump (13, 13, 38)] + in let output_urm = urm_from_eurm input_eurm + in + Assert.is_true (output_urm = expected_urm)) + +let () = if Array.mem "run-tests" Sys.argv then Test.launch_tests () + -- cgit v1.2.3 From 659e5fd404f454def89aacaf02b2d067e640a85f Mon Sep 17 00:00:00 2001 From: pacien Date: Sat, 21 Apr 2018 15:38:11 +0200 Subject: Document eurm transformation functions --- eurml.mli | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/eurml.mli b/eurml.mli index 1b13dca..8fd8ef1 100644 --- a/eurml.mli +++ b/eurml.mli @@ -5,10 +5,21 @@ open Common +(* Strips out comments and rewrite/enumerate labels *) val compile_preprocess : eurmcmd list -> eurmcmd list + +(* Rewrites Dec, GEqPredicate, LEqPredicate, LTPredicate, Mult and ZeroPredicate *) val compile_stage1 : eurmcmd list -> state -> eurmcmd list * state + +(* Rewrites Add, GTPredicate and Sub *) val compile_stage2 : eurmcmd list -> state -> eurmcmd list * state + +(* Rewrites Goto *) val compile_stage3 : eurmcmd list -> state -> eurmcmd list * state + +(* Rewrites Inc, EqPredicate, Label and Zero *) val compile_stage4 : eurmcmd list -> state -> urmcmd list * state + +(* Transcompiles an EURM instruction sequence into URM *) val urm_from_eurm : eurmcmd list -> urmcmd list -- cgit v1.2.3 From c198a4818eb00bbfa038b4029569fcff84096588 Mon Sep 17 00:00:00 2001 From: pacien Date: Sat, 21 Apr 2018 15:38:26 +0200 Subject: Impl urm_from_eurm --- eurm.ml | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 eurm.ml diff --git a/eurm.ml b/eurm.ml new file mode 100644 index 0000000..c571384 --- /dev/null +++ b/eurm.ml @@ -0,0 +1,22 @@ +(* + * UPEM / L3 / Functional programming / Project: URM + * Pacien TRAN-GIRARD, Adam NAILI + *) + +open Common + +let compile_preprocess eurmcmds = eurmcmds +let compile_stage1 eurmcmds state = eurmcmds, state +let compile_stage2 eurmcmds state = eurmcmds, state +let compile_stage3 eurmcmds state = eurmcmds, state +let compile_stage4 eurmcmds state = [URMZero(0)], state + +let urm_from_eurm = + let chain transform (eurmcmds, compile_state) = transform eurmcmds compile_state + and initial_state = 0 + in (compile_preprocess, initial_state) + |> chain compile_stage1 + |> chain compile_stage2 + |> chain compile_stage3 + |> chain compile_stage4 + -- cgit v1.2.3 From e828fbc45672ed2b6d9e78e6b667905d3c68267c Mon Sep 17 00:00:00 2001 From: pacien Date: Sat, 21 Apr 2018 15:41:48 +0200 Subject: Add new files to makefile --- makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/makefile b/makefile index 2ccd250..4179bf2 100644 --- a/makefile +++ b/makefile @@ -6,7 +6,8 @@ SOURCES = \ parser.mli parser.ml \ instptr.mli instptr.ml \ reg.mli reg.ml \ - urm.mli urm.ml urm_test.ml + urm.mli urm.ml urm_test.ml \ + eurm.mli eurm.ml eurm_test.ml OCAMLMAKEFILE = /usr/share/ocamlmakefile/OCamlMakefile include $(OCAMLMAKEFILE) -- cgit v1.2.3