From efb2a79edfbe3ef07b2da1be6bca86cfa363507d Mon Sep 17 00:00:00 2001 From: Mylloon Date: Sat, 10 Dec 2022 15:17:40 +0100 Subject: [PATCH] compile functions --- compiler.ml | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/compiler.ml b/compiler.ml index 9fb4514..2c7bb95 100644 --- a/compiler.ml +++ b/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)) ]) args 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 @@ -48,7 +52,11 @@ let compile_def (Func (name, args, body)) counter = let compiled = compile_block { 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 ; cnt = counter + 1 ; ret = "ret" ^ string_of_int counter