compile functions
This commit is contained in:
parent
1c1e816457
commit
efb2a79edf
1 changed files with 10 additions and 2 deletions
12
compiler.ml
12
compiler.ml
|
@ -25,7 +25,11 @@ let rec compile_expr env = function
|
||||||
(fun a -> compile_expr env a @ [ Addi (SP, SP, -4); Sw (V0, Mem (SP, 0)) ])
|
(fun a -> compile_expr env a @ [ Addi (SP, SP, -4); Sw (V0, Mem (SP, 0)) ])
|
||||||
args
|
args
|
||||||
in
|
in
|
||||||
List.flatten ca @ Env.find f Baselib.builtins
|
List.flatten ca
|
||||||
|
@
|
||||||
|
if Env.mem f Baselib.builtins
|
||||||
|
then Env.find f Baselib.builtins
|
||||||
|
else [ Jal f; Addi (SP, SP, 4 * List.length args) ]
|
||||||
;;
|
;;
|
||||||
|
|
||||||
let compile_instr info = function
|
let compile_instr info = function
|
||||||
|
@ -48,7 +52,11 @@ let compile_def (Func (name, args, body)) counter =
|
||||||
let compiled =
|
let compiled =
|
||||||
compile_block
|
compile_block
|
||||||
{ asm = []
|
{ asm = []
|
||||||
; env = Env.empty
|
; env =
|
||||||
|
List.fold_left
|
||||||
|
(fun env (arg, addr) -> Env.add arg addr env)
|
||||||
|
Env.empty
|
||||||
|
(List.mapi (fun i a -> a, Mem (FP, 4 * (i + 1))) (List.rev args))
|
||||||
; fpo = 8
|
; fpo = 8
|
||||||
; cnt = counter + 1
|
; cnt = counter + 1
|
||||||
; ret = "ret" ^ string_of_int counter
|
; ret = "ret" ^ string_of_int counter
|
||||||
|
|
Reference in a new issue