diff --git a/baselib.ml b/baselib.ml index b27515b..0c899b4 100644 --- a/baselib.ml +++ b/baselib.ml @@ -18,6 +18,7 @@ let _types_ = ; "puti", Func_t (Void_t, [ Int_t ]) ; "puts", Func_t (Void_t, [ Str_t ]) ; "geti", Func_t (Int_t, []) + ; "abs", Func_t (Int_t, [ Int_t ]) ]) ;; @@ -38,5 +39,6 @@ let builtins = ; "puti", [ Lw (A0, Mem (SP, 0)); Li (V0, Syscall.print_int); Syscall ] ; "puts", [ Lw (A0, Mem (SP, 0)); Li (V0, Syscall.print_str); Syscall ] ; "geti", [ Lw (A0, Mem (SP, 0)); Li (V0, Syscall.read_int); Syscall ] + ; "abs", [ Lw (T0, Mem (SP, 0)); Abs (V0, T0) ] ] ;; diff --git a/mips.ml b/mips.ml index 7b9f14d..67b47d5 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 + | Abs of reg * reg | Seq of reg * reg * reg | Sge of reg * reg * reg | Sgt of reg * reg * reg @@ -131,6 +132,7 @@ 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) + | 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) | Sge (rd, rs, rt) -> diff --git a/tests/10_math.test b/tests/10_math.test index 956c93e..211cd95 100644 --- a/tests/10_math.test +++ b/tests/10_math.test @@ -1,4 +1,4 @@ void main () { - int res = 13 * 100 + 20 - 8; - res = res * 2 / 2; + int res = 13 * -100 - 20 + 8; + res = abs(res * 2) / 2; }