diff --git a/regression/Makefile b/regression/Makefile index 0f824f097..43c3c6376 100644 --- a/regression/Makefile +++ b/regression/Makefile @@ -9,7 +9,7 @@ check: $(TESTS) $(TESTS): %: %.lama @echo $@ cat $@.input | LAMA=../runtime $(LAMAC) -i $< > $@.log && diff $@.log orig/$@.log - cat $@.input | LAMA=../runtime $(LAMAC) -s $< > $@.log && diff $@.log orig/$@.log + cat $@.input | LAMA=../runtime $(LAMAC) -ds -s $< > $@.log && diff $@.log orig/$@.log LAMA=../runtime $(LAMAC) $< && cat $@.input | ./$@ > $@.log && diff $@.log orig/$@.log clean: diff --git a/src/SM.ml b/src/SM.ml index 690530551..90e7f70f4 100644 --- a/src/SM.ml +++ b/src/SM.ml @@ -23,9 +23,9 @@ open Language (* create a closure *) | CLOSURE of string * Value.designation list (* proto closure *) | PROTO of string * string (* proto closure to a possible constant *) | PPROTO of string * string -(* proto call *) | PCALLC of int -(* calls a closure *) | CALLC of int -(* calls a function/procedure *) | CALL of string * int +(* proto call *) | PCALLC of int * bool +(* calls a closure *) | CALLC of int * bool +(* calls a function/procedure *) | CALL of string * int * bool (* returns from a function *) | RET (* drops the top element off *) | DROP (* duplicates the top element *) | DUP @@ -154,12 +154,12 @@ let rec eval env (((cstack, stack, glob, loc, i, o) as conf) : config) = functio in eval env (cstack, (Value.Closure ([], name, closure)) :: stack, glob, loc, i, o) prg' - | CALL (f, n) -> let args, stack' = split n stack in + | CALL (f, n, _) -> let args, stack' = split n stack in if env#is_label f then eval env ((prg', loc)::cstack, stack', glob, {args = Array.of_list (List.rev args); locals = [||]; closure = [||]}, i, o) (env#labeled f) else eval env (env#builtin f args ((cstack, stack', glob, loc, i, o) : config)) prg' - | CALLC n -> let vs, stack' = split (n+1) stack in + | CALLC (n, _) -> let vs, stack' = split (n+1) stack in let f::args = List.rev vs in (match f with | Value.Builtin f -> @@ -662,7 +662,7 @@ let compile cmd ((imports, infixes), p) = List.fold_left (fun (i, env, code) p -> let env, _, pcode = pattern env ldrop p in - i+1, env, ([DUP; CONST i; CALL (".elem", 2)] @ pcode) :: code + i+1, env, ([DUP; CONST i; CALL (".elem", 2, false)] @ pcode) :: code ) (0, env, []) ps @@ -696,7 +696,7 @@ let compile cmd ((imports, infixes), p) = let env, dsg = env#lookup name in env, ([DUP] @ - List.concat (List.map (fun i -> [CONST i; CALL (".elem", 2)]) path) @ + List.concat (List.map (fun i -> [CONST i; CALL (".elem", 2, false)]) path) @ [ST dsg; DROP]) :: acc ) (env, []) @@ -704,15 +704,15 @@ let compile cmd ((imports, infixes), p) = in env, (List.flatten code) @ [DROP] and add_code (env, flag, s) l f s' = env, f, s @ (if flag then [LABEL l] else []) @ s' - and compile_list l env = function + and compile_list tail l env = function | [] -> env, false, [] - | [e] -> compile_expr l env e + | [e] -> compile_expr tail l env e | e::es -> let les, env = env#get_label in - let env, flag1, s1 = compile_expr les env e in - let env, flag2, s2 = compile_list l env es in + let env, flag1, s1 = compile_expr false les env e in + let env, flag2, s2 = compile_list tail l env es in add_code (env, flag1, s1) les flag2 s2 - and compile_expr l env = function + and compile_expr tail l env = function | Expr.Lambda (args, b) -> let env, name = env#add_lambda args b in env#register_call name, false, [PROTO (name, env#current_function)] @@ -731,21 +731,21 @@ let compile cmd ((imports, infixes), p) = (List.rev ds) in let env = List.fold_left (fun env (name, args, m, b) -> env#add_fun name args m b) env funs in - let env, flag, code = compile_expr l env e in + let env, flag, code = compile_expr tail l env e in env#pop_scope, flag, code | Expr.Unit -> env, false, [CONST 0] | Expr.Ignore s -> let ls, env = env#get_label in - add_code (compile_expr ls env s) ls false [DROP] + add_code (compile_expr tail ls env s) ls false [DROP] - | Expr.ElemRef (x, i) -> compile_list l env [x; i] + | Expr.ElemRef (x, i) -> compile_list tail l env [x; i] | Expr.Var x -> let env, acc = env#lookup x in (match acc with Value.Fun name -> env#register_call name, false, [PROTO (name, env#current_function)] | _ -> env, false, [LD acc]) | Expr.Ref x -> let env, acc = env#lookup x in env, false, [LDA acc] | Expr.Const n -> env, false, [CONST n] | Expr.String s -> env, false, [STRING s] | Expr.Binop (op, x, y) -> let lop, env = env#get_label in - add_code (compile_list lop env [x; y]) lop false [BINOP op] + add_code (compile_list false lop env [x; y]) lop false [BINOP op] | Expr.Call (f, args) -> let lcall, env = env#get_label in (match f with @@ -754,60 +754,60 @@ let compile cmd ((imports, infixes), p) = (match acc with | Value.Fun name -> let env = env#register_call name in - let env, f, code = add_code (compile_list lcall env args) lcall false [PCALLC (List.length args)] in + let env, f, code = add_code (compile_list false lcall env args) lcall false [PCALLC (List.length args, tail)] in env, f, PPROTO (name, env#current_function) :: code | _ -> - add_code (compile_list lcall env (f :: args)) lcall false [CALLC (List.length args)] + add_code (compile_list false lcall env (f :: args)) lcall false [CALLC (List.length args, tail)] ) - | _ -> add_code (compile_list lcall env (f :: args)) lcall false [CALLC (List.length args)] + | _ -> add_code (compile_list false lcall env (f :: args)) lcall false [CALLC (List.length args, tail)] ) | Expr.Array xs -> let lar, env = env#get_label in - add_code (compile_list lar env xs) lar false [CALL (".array", List.length xs)] + add_code (compile_list false lar env xs) lar false [CALL (".array", List.length xs, tail)] | Expr.Sexp (t, xs) -> let lsexp, env = env#get_label in - add_code (compile_list lsexp env xs) lsexp false [SEXP (t, List.length xs)] + add_code (compile_list false lsexp env xs) lsexp false [SEXP (t, List.length xs)] | Expr.Elem (a, i) -> let lelem, env = env#get_label in - add_code (compile_list lelem env [a; i]) lelem false [CALL (".elem", 2)] + add_code (compile_list false lelem env [a; i]) lelem false [CALL (".elem", 2, tail)] | Expr.Length e -> let llen, env = env#get_label in - add_code (compile_expr llen env e) llen false [CALL (".length", 1)] + add_code (compile_expr false llen env e) llen false [CALL (".length", 1, tail)] | Expr.StringVal e -> let lsv, env = env#get_label in - add_code (compile_expr lsv env e) lsv false [CALL (".stringval", 1)] + add_code (compile_expr false lsv env e) lsv false [CALL (".stringval", 1, tail)] | Expr.Assign (x, e) -> let lassn, env = env#get_label in - add_code (compile_list lassn env [x; e]) lassn false [match x with Expr.ElemRef _ -> STA | _ -> STI] + add_code (compile_list false lassn env [x; e]) lassn false [match x with Expr.ElemRef _ -> STA | _ -> STI] | Expr.Skip -> env, false, [] - | Expr.Seq (s1, s2) -> compile_list l env [s1; s2] + | Expr.Seq (s1, s2) -> compile_list tail l env [s1; s2] | Expr.If (c, s1, s2) -> let le, env = env#get_label in let l2, env = env#get_label in - let env, fe , se = compile_expr le env c in - let env, flag1, s1 = compile_expr l env s1 in - let env, flag2, s2 = compile_expr l env s2 in + let env, fe , se = compile_expr false le env c in + let env, flag1, s1 = compile_expr tail l env s1 in + let env, flag2, s2 = compile_expr tail l env s2 in env, true, se @ (if fe then [LABEL le] else []) @ [CJMP ("z", l2)] @ s1 @ (if flag1 then [] else [JMP l]) @ [LABEL l2] @ s2 @ (if flag2 then [] else [JMP l]) | Expr.While (c, s) -> let lexp, env = env#get_label in let loop, env = env#get_label in let cond, env = env#get_label in - let env, fe, se = compile_expr lexp env c in - let env, _ , s = compile_expr cond env s in + let env, fe, se = compile_expr false lexp env c in + let env, _ , s = compile_expr false cond env s in env, false, [JMP cond; LABEL loop] @ s @ [LABEL cond] @ se @ (if fe then [LABEL lexp] else []) @ [CJMP ("nz", loop)] | Expr.Repeat (s, c) -> let lexp , env = env#get_label in let loop , env = env#get_label in let check, env = env#get_label in - let env, fe , se = compile_expr lexp env c in - let env, flag, body = compile_expr check env s in + let env, fe , se = compile_expr false lexp env c in + let env, flag, body = compile_expr false check env s in env, false, [LABEL loop] @ body @ (if flag then [LABEL check] else []) @ se @ (if fe then [LABEL lexp] else []) @ [CJMP ("z", loop)] | Expr.Return (Some e) -> let lret, env = env#get_label in - add_code (compile_expr lret env e) lret false [RET] + add_code (compile_expr true lret env e) lret false [RET] | Expr.Return None -> env, false, [CONST 0; RET] @@ -817,7 +817,7 @@ let compile cmd ((imports, infixes), p) = let n = List.length brs - 1 in let lfail, env = env#get_label in let lexp , env = env#get_label in - let env , fe , se = compile_expr lexp env e in + let env , fe , se = compile_expr false lexp env e in let env , _, _, code, fail = List.fold_left (fun ((env, lab, i, code, continue) as acc) (p, s) -> @@ -831,7 +831,7 @@ let compile cmd ((imports, infixes), p) = let env, lfalse', pcode = pattern env lfalse p in let env = env#push_scope in let env, bindcode = bindings env p in - let env, l' , scode = compile_expr l env s in + let env, l' , scode = compile_expr tail l env s in let env = env#pop_scope in (env, Some lfalse, i+1, ((match lab with None -> [] | Some l -> [LABEL l; DUP]) @ pcode @ bindcode @ scode @ jmp) :: code, lfalse') else acc @@ -847,7 +847,7 @@ let compile cmd ((imports, infixes), p) = (*Printf.eprintf "Lookup: %s\n%!" (try show(Value.designation) @@ snd (env#lookup "inner") with _ -> "no inner..."); *) let env = List.fold_left (fun env arg -> env#add_arg arg) env args in let lend, env = env#get_label in - let env, flag, code = compile_expr lend env stmt in + let env, flag, code = compile_expr true lend env stmt in let env, funcode = compile_fundefs [] env in (*Printf.eprintf "Function: %s, closure: %s\n%!" name (show(list) (show(Value.designation)) env#closure);*) let env = env#register_closure name in @@ -875,10 +875,10 @@ let compile cmd ((imports, infixes), p) = | [] -> inner (Some f :: state) tl | closure -> CLOSURE (f, closure) :: inner (None :: state) tl ) - | PCALLC n :: tl -> + | PCALLC (n, tail) :: tl -> (match state with - | None :: state' -> CALLC n :: inner state' tl - | Some f :: state' -> CALL (f, n) :: inner state' tl + | None :: state' -> CALLC (n, tail) :: inner state' tl + | Some f :: state' -> CALL (f, n, tail) :: inner state' tl ) | insn :: tl -> insn :: inner state tl in @@ -886,10 +886,10 @@ let compile cmd ((imports, infixes), p) = in let env = new env cmd imports in let lend, env = env#get_label in - let env, flag, code = compile_expr lend env p in + let env, flag, code = compile_expr false lend env p in let code = if flag then code @ [LABEL lend] else code in let topname = cmd#topname in - let env, prg = compile_fundefs [[LABEL topname; BEGIN (topname, 0, env#nlocals, [])] @ code @ [END]] env in + let env, prg = compile_fundefs [[LABEL topname; BEGIN (topname, (if topname = "main" then 2 else 0), env#nlocals, [])] @ code @ [END]] env in let prg = [PUBLIC topname] @ env#get_decls @ List.flatten prg in (*Printf.eprintf "Before propagating closures:\n"; Printf.eprintf "%s\n%!" env#show_funinfo; diff --git a/src/X86.ml b/src/X86.ml index 979e03a62..4a20d1676 100644 --- a/src/X86.ml +++ b/src/X86.ml @@ -133,54 +133,92 @@ let compile cmd env imports code = let rec compile' env scode = let on_stack = function S _ -> true | _ -> false in let mov x s = if on_stack x && on_stack s then [Mov (x, eax); Mov (eax, s)] else [Mov (x, s)] in - let callc env n = - let pushr, popr = - List.split @@ List.map (fun r -> (Push r, Pop r)) (env#live_registers n) - in - let pushr, popr = env#save_closure @ pushr, env#rest_closure @ popr in - let env, code = + let callc env n tail = + let tail = tail && env#nargs = n in + if tail + then ( let rec push_args env acc = function | 0 -> env, acc | n -> let x, env = env#pop in - push_args env ((Push x)::acc) (n-1) + if x = env#loc (Value.Arg (n-1)) + then push_args env acc (n-1) + else push_args env ((mov x (env#loc (Value.Arg (n-1)))) @ acc) (n-1) in - let env, pushs = push_args env [] n in - let pushs = List.rev pushs in - let closure, env = env#pop in - let call_closure = - if on_stack closure - then [Mov (closure, edx); Mov (edx, eax); CallI eax] - else [Mov (closure, edx); CallI closure] + let env , pushs = push_args env [] n in + let closure, env = env#pop in + let y , env = env#allocate in + env, pushs @ [Mov (closure, edx); + Mov (I(0, edx), eax); + Mov (ebp, esp); + Pop (ebp)] @ + (if env#has_closure then [Pop ebx] else []) @ + [Jmp "*%eax"] (* UGLY!!! *) + ) + else ( + let pushr, popr = + List.split @@ List.map (fun r -> (Push r, Pop r)) (env#live_registers n) in - env, pushr @ pushs @ call_closure @ [Binop ("+", L (word_size * List.length pushs), esp)] @ (List.rev popr) - in - let y, env = env#allocate in env, code @ [Mov (eax, y)] - in - let call env f n = - let f = - match f.[0] with '.' -> "B" ^ String.sub f 1 (String.length f - 1) | _ -> f - in - let pushr, popr = - List.split @@ List.map (fun r -> (Push r, Pop r)) (env#live_registers n) - in - let pushr, popr = env#save_closure @ pushr, env#rest_closure @ popr in - let env, code = - let rec push_args env acc = function + let pushr, popr = env#save_closure @ pushr, env#rest_closure @ popr in + let env, code = + let rec push_args env acc = function | 0 -> env, acc | n -> let x, env = env#pop in push_args env ((Push x)::acc) (n-1) + in + let env, pushs = push_args env [] n in + let pushs = List.rev pushs in + let closure, env = env#pop in + let call_closure = + if on_stack closure + then [Mov (closure, edx); Mov (edx, eax); CallI eax] + else [Mov (closure, edx); CallI closure] + in + env, pushr @ pushs @ call_closure @ [Binop ("+", L (word_size * List.length pushs), esp)] @ (List.rev popr) + in + let y, env = env#allocate in env, code @ [Mov (eax, y)] + ) + in + let call env f n tail = + let tail = tail && env#nargs = n && f.[0] <> '.' in + let f = + match f.[0] with '.' -> "B" ^ String.sub f 1 (String.length f - 1) | _ -> f + in + if tail + then ( + let rec push_args env acc = function + | 0 -> env, acc + | n -> let x, env = env#pop in + if x = env#loc (Value.Arg (n-1)) + then push_args env acc (n-1) + else push_args env ((mov x (env#loc (Value.Arg (n-1)))) @ acc) (n-1) in let env, pushs = push_args env [] n in - let pushs = - match f with - | "Barray" -> List.rev @@ (Push (L (box n))) :: pushs - | "Bsexp" -> List.rev @@ (Push (L (box n))) :: pushs - | "Bsta" -> pushs - | _ -> List.rev pushs + let y, env = env#allocate in + env, pushs @ [Mov (ebp, esp); Pop (ebp); Jmp f] + ) + else ( + let pushr, popr = + List.split @@ List.map (fun r -> (Push r, Pop r)) (env#live_registers n) + in + let pushr, popr = env#save_closure @ pushr, env#rest_closure @ popr in + let env, code = + let rec push_args env acc = function + | 0 -> env, acc + | n -> let x, env = env#pop in + push_args env ((Push x)::acc) (n-1) + in + let env, pushs = push_args env [] n in + let pushs = + match f with + | "Barray" -> List.rev @@ (Push (L (box n))) :: pushs + | "Bsexp" -> List.rev @@ (Push (L (box n))) :: pushs + | "Bsta" -> pushs + | _ -> List.rev pushs + in + env, pushr @ pushs @ [Call f; Binop ("+", L (word_size * List.length pushs), esp)] @ (List.rev popr) in - env, pushr @ pushs @ [Call f; Binop ("+", L (word_size * List.length pushs), esp)] @ (List.rev popr) - in - let y, env = env#allocate in env, code @ [Mov (eax, y)] + let y, env = env#allocate in env, code @ [Mov (eax, y)] + ) in match scode with | [] -> env, [] @@ -217,7 +255,7 @@ let compile cmd env imports code = | STRING s -> let s, env = env#string s in let l, env = env#allocate in - let env, call = call env ".string" 1 in + let env, call = call env ".string" 1 false in (env, Mov (M ("$" ^ s), l) :: call) | LDA x -> @@ -246,7 +284,7 @@ let compile cmd env imports code = ) | STA -> - call env ".sta" 3 + call env ".sta" 3 false | STI -> let v, x, env' = env#pop2 in @@ -356,7 +394,7 @@ let compile cmd env imports code = | BEGIN (f, nargs, nlocals, closure) -> env#assert_empty_stack; let has_closure = closure <> [] in - let env = env#enter f nlocals has_closure in + let env = env#enter f nargs nlocals has_closure in env, (if has_closure then [Push edx] else []) @ (if f = cmd#topname then @@ -407,13 +445,13 @@ let compile cmd env imports code = let x = env#peek in env, [Mov (x, eax); Jmp env#epilogue] - | CALL (f, n) -> call env f n + | CALL (f, n, tail) -> call env f n tail - | CALLC n -> callc env n + | CALLC (n, tail) -> callc env n tail | SEXP (t, n) -> let s, env = env#allocate in - let env, code = call env ".sexp" (n+1) in + let env, code = call env ".sexp" (n+1) false in env, [Mov (L env#hash t, s)] @ code | DROP -> @@ -431,15 +469,15 @@ let compile cmd env imports code = | TAG (t, n) -> let s1, env = env#allocate in let s2, env = env#allocate in - let env, code = call env ".tag" 3 in + let env, code = call env ".tag" 3 false in env, [Mov (L (box (env#hash t)), s1); Mov (L (box n), s2)] @ code | ARRAY n -> let s, env = env#allocate in - let env, code = call env ".array_patt" 2 in + let env, code = call env ".array_patt" 2 false in env, [Mov (L (box n), s)] @ code - | PATT StrCmp -> call env ".string_patt" 2 + | PATT StrCmp -> call env ".string_patt" 2 false | PATT patt -> call env @@ -450,7 +488,7 @@ let compile cmd env imports code = | String -> ".string_tag_patt" | Sexp -> ".sexp_tag_patt" | Closure -> ".closure_tag_patt" - ) 1 + ) 1 false | FAIL ((line, col), value) -> let v, env = if value then env#peek, env else env#pop in @@ -482,9 +520,11 @@ class env prg = val stringm = M.empty (* a string map *) val scount = 0 (* string count *) val stack_slots = 0 (* maximal number of stack positions *) + val static_size = 0 (* static data size *) val stack = [] (* symbolic stack *) - val args = [] (* function arguments *) + val nargs = 0 (* number of function arguments *) + (* val args = [] (* function arguments *) *) val locals = [] (* function local variables *) val fname = "" (* function name *) val stackmap = M.empty (* labels to stack map *) @@ -629,6 +669,9 @@ class env prg = let m = M.add x y stringm in y, {< scount = scount + 1; stringm = m>} + (* gets number of arguments in the current function *) + method nargs = nargs + (* gets all global variables *) method globals = S.elements (S.diff globals externs) @@ -641,8 +684,8 @@ class env prg = method allocated_size = Printf.sprintf "LS%s_SIZE" fname (* enters a function *) - method enter f nlocals has_closure = - {< static_size = nlocals; stack_slots = nlocals; stack = []; fname = f; has_closure = has_closure >} + method enter f nargs nlocals has_closure = + {< nargs = nargs; static_size = nlocals; stack_slots = nlocals; stack = []; fname = f; has_closure = has_closure >} (* returns a label for the epilogue *) method epilogue = Printf.sprintf "L%s_epilogue" fname diff --git a/src/version.ml b/src/version.ml index 95b3a817c..62f566655 100644 --- a/src/version.ml +++ b/src/version.ml @@ -1 +1 @@ -let version = "Version 1.00, e4b34a3ec, Sat Mar 21 13:05:14 2020 +0300" +let version = "Version 1.00, eeab48ed3, Sun Mar 22 21:58:11 2020 +0300"