add substraction and division
This commit is contained in:
parent
c98231152a
commit
5ddc80ada2
5 changed files with 21 additions and 2 deletions
|
@ -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) ]
|
||||
]
|
||||
;;
|
||||
|
|
|
@ -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) }
|
||||
|
|
6
mips.ml
6
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
|
||||
|
|
10
parser.mly
10
parser.mly
|
@ -8,9 +8,9 @@
|
|||
%token <Ast.type_t> Ltype
|
||||
%token <string> 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) }
|
||||
}
|
||||
;
|
||||
|
|
|
@ -1 +1,2 @@
|
|||
int res = 13 * 100 + 12;
|
||||
res = res * 2 / 2;
|
Reference in a new issue