diff --git a/baselib.ml b/baselib.ml index 0c899b4..c252b9f 100644 --- a/baselib.ml +++ b/baselib.ml @@ -9,6 +9,7 @@ let _types_ = ; "%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 ]) + ; "%rem", Func_t (Int_t, [ Int_t; Int_t ]) ; "%seq", Func_t (Bool_t, [ Int_t; Int_t ]) ; "%sge", Func_t (Bool_t, [ Int_t; Int_t ]) ; "%sgt", Func_t (Bool_t, [ Int_t; Int_t ]) @@ -30,6 +31,7 @@ let builtins = ; "%sub", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); Sub (V0, T0, T1) ] ; "%mul", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); Mul (V0, T0, T1) ] ; "%div", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); Div (V0, T0, T1) ] + ; "%rem", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); Rem (V0, T0, T1) ] ; "%seq", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); Seq (V0, T0, T1) ] ; "%sge", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); Sge (V0, T0, T1) ] ; "%sgt", [ Lw (T0, Mem (SP, 4)); Lw (T1, Mem (SP, 0)); Sgt (V0, T0, T1) ] diff --git a/lexer.mll b/lexer.mll index 83d6aba..ba048f8 100644 --- a/lexer.mll +++ b/lexer.mll @@ -34,6 +34,7 @@ rule token = parse | '-' { Lsub } | '*' { Lmul } | '/' { Ldiv } + | '%' { Lrem } | "==" { Lseq } | ">=" { Lsge } | ">" { Lsgt } diff --git a/mips.ml b/mips.ml index 67b47d5..1e4fa35 100644 --- a/mips.ml +++ b/mips.ml @@ -47,6 +47,7 @@ type instr = | Mul of reg * reg * reg | Sub of reg * reg * reg | Div of reg * reg * reg + | Rem of reg * reg * reg | Abs of reg * reg | Seq of reg * reg * reg | Sge of reg * reg * reg @@ -132,6 +133,8 @@ let fmt_instr ?(indent = " ") = function Printf.sprintf "%ssub %s, %s, %s" indent (fmt_reg rd) (fmt_reg rs) (fmt_reg rt) | Div (rd, rs, rt) -> Printf.sprintf "%sdiv %s, %s, %s" indent (fmt_reg rd) (fmt_reg rs) (fmt_reg rt) + | Rem (rd, rs, rt) -> + Printf.sprintf "%srem %s, %s, %s" indent (fmt_reg rd) (fmt_reg rs) (fmt_reg rt) | Abs (rd, rs) -> Printf.sprintf "%sabs %s, %s" indent (fmt_reg rd) (fmt_reg rs) | Seq (rd, rs, rt) -> Printf.sprintf "%sseq %s, %s, %s" indent (fmt_reg rd) (fmt_reg rs) (fmt_reg rt) diff --git a/parser.mly b/parser.mly index 8cc6858..88d30a0 100644 --- a/parser.mly +++ b/parser.mly @@ -11,10 +11,10 @@ %token Lend Lassign Lsc Lreturn %token Lbracedeb Lbracefin %token Lpardeb Lparfin Lcomma -%token Ladd Lsub Lmul Ldiv Lseq Lsge Lsgt Lsle Lslt Lsne +%token Ladd Lsub Lmul Ldiv Lrem Lseq Lsge Lsgt Lsle Lslt Lsne %token Lif Lelse Lwhile -%left Ladd Lsub Lmul Ldiv Lseq Lsge Lsgt Lsle Lslt Lsne +%left Ladd Lsub Lmul Ldiv Lrem Lseq Lsge Lsgt Lsle Lslt Lsne %left Lbracedeb Lparfin Lbracefin Lreturn %left Ltype Lbool Lint Lvar Lstr @@ -176,6 +176,11 @@ expr: Call { func = "%div" ; args = [ a ; b ] ; pos = $startpos($2) } } + /* e % e */ + | a = expr ; Lrem ; b = expr { + Call { func = "%rem" ; args = [ a ; b ] ; pos = $startpos($2) } + } + /* e == e */ | a = expr ; Lseq ; b = expr { Call { func = "%seq" ; args = [ a ; b ] ; pos = $startpos($2) } diff --git a/tests/10_math.test b/tests/10_math.test index 211cd95..6f82f73 100644 --- a/tests/10_math.test +++ b/tests/10_math.test @@ -1,4 +1,5 @@ -void main () { +int main () { int res = 13 * -100 - 20 + 8; res = abs(res * 2) / 2; + return res % 2; }