aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCamil Staps2018-12-24 14:25:46 +0100
committerCamil Staps2018-12-24 14:25:46 +0100
commit8cf914defa8030c3beab450e00430959de845fc1 (patch)
tree6c4c5300c9f01b4b0948b7a8c2dd8a5af8c706e0
parentIterative development (diff)
Remove Abstr; add fun_args
-rw-r--r--README.md3
-rw-r--r--sjit.icl23
2 files changed, 11 insertions, 15 deletions
diff --git a/README.md b/README.md
index 0bdef99..00eafdd 100644
--- a/README.md
+++ b/README.md
@@ -5,8 +5,7 @@ It does almost nothing, and what it does, it does badly and is not useful.
- There is no parser, you have to write your program in the internal Clean
representation (see `Start` in [`sjit.icl`](/sjit.icl)).
-- There is no type checker, you have to guess the implicit rules (such as that
- a `fun_expr` must always be an `Abstr`).
+- There is no type checker, you have to guess the implicit rules.
- There is no register allocation, everything is done on the stack.
- There is no code optimisation, not even to eliminate `push rbx` followed by
`pop rbx`.
diff --git a/sjit.icl b/sjit.icl
index c24ea97..7a01d2e 100644
--- a/sjit.icl
+++ b/sjit.icl
@@ -14,11 +14,11 @@ import code from "sjit_c."
:: Expr
= Int !Int
| Var !String
- | Abstr ![String] !Expr
| App !String ![Expr]
:: Function =
{ fun_name :: !String
+ , fun_args :: ![String]
, fun_expr :: !Expr
}
@@ -128,12 +128,13 @@ compile :: !Function !CompileState -> CompileState
compile f cs
# cs & funs = put f.fun_name cs.pc cs.funs
# vars = cs.vars
-# (is,cs) = expr f.fun_expr {cs & vars=newMap}
-# is = {i \\ i <- reverse [Ret:is]}
+# cs & vars = foldr (uncurry put) cs.vars [(v,sp) \\ v <- f.fun_args & sp <- [cs.sp+1..]]
+# (is,cs) = expr f.fun_expr cs
+# is = {i \\ i <- reverse [Ret:Put (max 1 (length f.fun_args)+1):is]}
=
{ cs
& vars = vars
- , pc = cs.pc+1
+ , pc = cs.pc+2
, blocks = cs.blocks ++| [!is!]
, jitst = appendProgram (f.fun_name == "main") is cs.jitst
}
@@ -144,14 +145,10 @@ where
Just i -> ([PushRef (i-cs.sp)], {cs & sp=cs.sp+1, pc=cs.pc+1})
Nothing -> abort "undefined variable\n"
expr (App f args) cs
- # (iss,cs) = mapSt expr args cs
+ # (iss,cs) = mapSt expr args {cs & sp=cs.sp+1}
= case get f cs.funs of
- Just f -> ([Pop (length args-1):Call f:flatten iss], {cs & sp=cs.sp+1, pc=cs.pc+2})
+ Just f -> ([Pop (length args-1):Call f:flatten iss], {cs & sp=cs.sp+2-length args, pc=cs.pc+2})
Nothing -> abort "undefined function\n"
- expr (Abstr vs e) cs
- # cs & vars = foldr (uncurry put) cs.vars [(v,sp) \\ v <- vs & sp <- [cs.sp+1..]]
- # (is,cs) = expr e cs
- = ([Put (max 1 (length vs)+1):is], {cs & sp=cs.sp-1, pc=cs.pc+1})
compile_all :: !(Maybe CompileState) ![Function] -> CompileState
compile_all mcs funs
@@ -268,7 +265,7 @@ where
jit_compiled_result = exec comp_state
comp_state =: compile_all Nothing
- [ {fun_name="id", fun_expr=Abstr ["x"] (Var "x")}
- , {fun_name="const", fun_expr=Abstr ["x","y"] (Var "x")}
- , {fun_name="main", fun_expr=Abstr [] (App "+" [App "const" [Int 37, Int 10], App "const" [Int 5, Int 10]])}
+ [ {fun_name="id", fun_args=["x"], fun_expr=Var "x"}
+ , {fun_name="const", fun_args=["x","y"], fun_expr=Var "x"}
+ , {fun_name="main", fun_args=[], fun_expr=App "+" [App "const" [Int 37, Int 10], App "const" [Int 5, Int 10]]}
]