This commit is contained in:
Mylloon 2022-12-22 16:57:24 +01:00
parent abf30e8bda
commit a8dfaa4fec
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
5 changed files with 15 additions and 3 deletions

View file

@ -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) ]

View file

@ -34,6 +34,7 @@ rule token = parse
| '-' { Lsub }
| '*' { Lmul }
| '/' { Ldiv }
| '%' { Lrem }
| "==" { Lseq }
| ">=" { Lsge }
| ">" { Lsgt }

View file

@ -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)

View file

@ -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) }

View file

@ -1,4 +1,5 @@
void main () {
int main () {
int res = 13 * -100 - 20 + 8;
res = abs(res * 2) / 2;
return res % 2;
}