From 5ddc80ada2890e3ed4af90fa0f0883e0e46fbbfa Mon Sep 17 00:00:00 2001 From: Mylloon Date: Fri, 9 Dec 2022 16:39:44 +0100 Subject: [PATCH] add substraction and division --- baselib.ml | 4 ++++ lexer.mll | 2 ++ mips.ml | 6 ++++++ parser.mly | 10 ++++++++-- tests/{10_add-mul.test => 10_math.test} | 1 + 5 files changed, 21 insertions(+), 2 deletions(-) rename tests/{10_add-mul.test => 10_math.test} (56%) diff --git a/baselib.ml b/baselib.ml index 610e304..c01dfa8 100644 --- a/baselib.ml +++ b/baselib.ml @@ -6,7 +6,9 @@ let _types_ = Env.of_seq (List.to_seq [ "%add", Func_t (Int_t, [ Int_t; Int_t ]) + ; "%sub", Func_t (Int_t, [ Int_t; Int_t ]) ; "%mul", Func_t (Int_t, [ Int_t; Int_t ]) + ; "%div", Func_t (Int_t, [ Int_t; Int_t ]) ]) ;; @@ -15,6 +17,8 @@ let builtins = (fun env (fn, impl) -> Env.add fn impl env) Env.empty [ "%add", [ Lw (T0, Mem (SP, 0)); Lw (T1, Mem (SP, 4)); Add (V0, T0, T1) ] + ; "%sub", [ Lw (T0, Mem (SP, 0)); Lw (T1, Mem (SP, 4)); Sub (V0, T0, T1) ] ; "%mul", [ Lw (T0, Mem (SP, 0)); Lw (T1, Mem (SP, 4)); Mul (V0, T0, T1) ] + ; "%div", [ Lw (T0, Mem (SP, 0)); Lw (T1, Mem (SP, 4)); Div (V0, T1, T0) ] ] ;; diff --git a/lexer.mll b/lexer.mll index 5294e76..514561a 100644 --- a/lexer.mll +++ b/lexer.mll @@ -22,7 +22,9 @@ rule token = parse | '=' { Lassign } | ';' { Lsc } | '+' { Ladd } +| '-' { Lsub } | '*' { Lmul } +| '/' { Ldiv } | ident as i { Lvar i } | '#' { comment lexbuf } | _ as c { raise (Error c) } diff --git a/mips.ml b/mips.ml index 8069440..c2991e8 100644 --- a/mips.ml +++ b/mips.ml @@ -48,6 +48,8 @@ type instr = | Addi of reg * reg * int | Add of reg * reg * reg | Mul of reg * reg * reg + | Sub of reg * reg * reg + | Div of reg * reg * reg | Syscall | B of label | Beqz of reg * label @@ -127,6 +129,10 @@ let fmt_instr = function Printf.sprintf " add %s, %s, %s" (fmt_reg rd) (fmt_reg rs) (fmt_reg rt) | Mul (rd, rs, rt) -> Printf.sprintf " mul %s, %s, %s" (fmt_reg rd) (fmt_reg rs) (fmt_reg rt) + | Sub (rd, rs, rt) -> + Printf.sprintf " sub %s, %s, %s" (fmt_reg rd) (fmt_reg rs) (fmt_reg rt) + | Div (rd, rs, rt) -> + Printf.sprintf " div %s, %s, %s" (fmt_reg rd) (fmt_reg rs) (fmt_reg rt) | Syscall -> Printf.sprintf " syscall" | B l -> Printf.sprintf " b %s" l | Beqz (r, l) -> Printf.sprintf " beqz %s, %s" (fmt_reg r) l diff --git a/parser.mly b/parser.mly index 74df4af..49554ec 100644 --- a/parser.mly +++ b/parser.mly @@ -8,9 +8,9 @@ %token Ltype %token Lvar %token Lend Lassign Lsc Lreturn -%token Ladd Lmul +%token Ladd Lsub Lmul Ldiv -%left Ladd Lmul +%left Ladd Lsub Lmul Ldiv %start prog @@ -49,7 +49,13 @@ expr: | a = expr ; Ladd ; b = expr { Call { func = "%add" ; args = [ a ; b ] ; pos = $startpos($2) } } + | a = expr ; Lsub ; b = expr { + Call { func = "%sub" ; args = [ a ; b ] ; pos = $startpos($2) } + } | a = expr ; Lmul ; b = expr { Call { func = "%mul" ; args = [ a ; b ] ; pos = $startpos($2) } } + | a = expr ; Ldiv ; b = expr { + Call { func = "%div" ; args = [ a ; b ] ; pos = $startpos($2) } + } ; diff --git a/tests/10_add-mul.test b/tests/10_math.test similarity index 56% rename from tests/10_add-mul.test rename to tests/10_math.test index 1a7bbe7..782899a 100644 --- a/tests/10_add-mul.test +++ b/tests/10_math.test @@ -1 +1,2 @@ int res = 13 * 100 + 12; +res = res * 2 / 2;