add rem
This commit is contained in:
parent
abf30e8bda
commit
a8dfaa4fec
5 changed files with 15 additions and 3 deletions
|
@ -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) ]
|
||||
|
|
|
@ -34,6 +34,7 @@ rule token = parse
|
|||
| '-' { Lsub }
|
||||
| '*' { Lmul }
|
||||
| '/' { Ldiv }
|
||||
| '%' { Lrem }
|
||||
| "==" { Lseq }
|
||||
| ">=" { Lsge }
|
||||
| ">" { Lsgt }
|
||||
|
|
3
mips.ml
3
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)
|
||||
|
|
|
@ -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) }
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
void main () {
|
||||
int main () {
|
||||
int res = 13 * -100 - 20 + 8;
|
||||
res = abs(res * 2) / 2;
|
||||
return res % 2;
|
||||
}
|
||||
|
|
Reference in a new issue