Added X86 codegeneration interface and tests

This commit is contained in:
Dmitry Boulytchev 2018-03-07 10:18:30 +03:00
parent 77ec064c5c
commit de018e76aa
12 changed files with 320 additions and 34 deletions

View file

@ -24,15 +24,26 @@ type config = int list * Stmt.config
Takes a configuration and a program, and returns a configuration as a result
*)
let eval _ = failwith "Not yet implemented"
let rec eval ((stack, ((st, i, o) as c)) as conf) = function
| [] -> conf
| insn :: prg' ->
eval
(match insn with
| BINOP op -> let y::x::stack' = stack in (Expr.to_func op x y :: stack', c)
| READ -> let z::i' = i in (z::stack, (st, i', o))
| WRITE -> let z::stack' = stack in (stack', (st, i, o @ [z]))
| CONST i -> (i::stack, c)
| LD x -> (st x :: stack, c)
| ST x -> let z::stack' = stack in (stack', (Expr.update x z st, i, o))
) prg'
(* Top-level evaluation
val run : prg -> int list -> int list
Takes a program, an input stream, and returns an output stream this program calculates
Takes an input stream, a program, and returns an output stream this program calculates
*)
let run p i = let (_, (_, _, o)) = eval ([], (Language.Expr.empty, i, [])) p in o
let run p i = let (_, (_, _, o)) = eval ([], (Expr.empty, i, [])) p in o
(* Stack machine compiler
@ -40,5 +51,15 @@ let run p i = let (_, (_, _, o)) = eval ([], (Language.Expr.empty, i, [])) p in
Takes a program in the source language and returns an equivalent program for the
stack machine
*)
let compile _ = failwith "Not yet implemented"
*)
let rec compile =
let rec expr = function
| Expr.Var x -> [LD x]
| Expr.Const n -> [CONST n]
| Expr.Binop (op, x, y) -> expr x @ expr y @ [BINOP op]
in
function
| Stmt.Seq (s1, s2) -> compile s1 @ compile s2
| Stmt.Read x -> [READ; ST x]
| Stmt.Write e -> expr e @ [WRITE]
| Stmt.Assign (x, e) -> expr e @ [ST x]