diff options
Diffstat (limited to 'Sjit')
-rw-r--r-- | Sjit/Compile.dcl | 8 | ||||
-rw-r--r-- | Sjit/Compile.icl | 19 | ||||
-rw-r--r-- | Sjit/Run.icl | 16 |
3 files changed, 22 insertions, 21 deletions
diff --git a/Sjit/Compile.dcl b/Sjit/Compile.dcl index fd4b2ee..d373e42 100644 --- a/Sjit/Compile.dcl +++ b/Sjit/Compile.dcl @@ -17,10 +17,10 @@ from Sjit.Syntax import :: Function | Ret | Halt - | IAddRet - | IMulRet - | ISubRet - | IDivRet + | IAdd + | IMul + | ISub + | IDiv | PlaceHolder !Int !Int // only used during compilation diff --git a/Sjit/Compile.icl b/Sjit/Compile.icl index 8fd81a2..527676f 100644 --- a/Sjit/Compile.icl +++ b/Sjit/Compile.icl @@ -64,10 +64,6 @@ where header :: [(!String, ![Instr])] header = [ ("_", [PushI 0,Call 0 /* main address */,Halt]) - , ("+", [IAddRet]) - , ("*", [IMulRet]) - , ("-", [ISubRet]) - , ("/", [IDivRet]) ] initJITState :: !Int -> JITState @@ -105,10 +101,10 @@ where JmpTrue _ -> 0 Ret -> -1 Halt -> -2 - IAddRet -> -1 - IMulRet -> -1 - ISubRet -> -1 - IDivRet -> -1 + IAdd -> -1 + IMul -> -1 + ISub -> -1 + IDiv -> -1 PlaceHolder _ n -> n reserve :: !Int !CompileState -> m (!Int, !CompileState) | Monad m @@ -155,7 +151,12 @@ where expr (App f args) cs # args = if (args=:[]) [Int 0] args = foldM (flip expr) cs (reverse args) >>= \cs -> case get f cs.funs of - Nothing -> Left ("undefined function '" +++ toString f +++ "'") + Nothing -> case f of + "+" -> gen [IAdd] cs + "-" -> gen [ISub] cs + "*" -> gen [IMul] cs + "/" -> gen [IDiv] cs + _ -> Left ("undefined function '" +++ toString f +++ "'") Just f -> case length args of 1 -> gen [Call f] cs n -> gen [Pop (n-1),Call f] cs diff --git a/Sjit/Run.icl b/Sjit/Run.icl index e3e623f..ad03e02 100644 --- a/Sjit/Run.icl +++ b/Sjit/Run.icl @@ -33,14 +33,14 @@ where [r] -> r _ -> abort (toString (length stack) +++ " values left on stack\n") - IAddRet -> case stack of - [ret:a:b:stack] -> exec ret [a:a+b:stack] - IMulRet -> case stack of - [ret:a:b:stack] -> exec ret [a:a*b:stack] - ISubRet -> case stack of - [ret:a:b:stack] -> exec ret [a:a-b:stack] - IDivRet -> case stack of - [ret:a:b:stack] -> exec ret [a:a/b:stack] + IAdd -> case stack of + [a:b:stack] -> exec (i+1) [a+b:stack] + IMul -> case stack of + [a:b:stack] -> exec (i+1) [a*b:stack] + ISub -> case stack of + [a:b:stack] -> exec (i+1) [a-b:stack] + IDiv -> case stack of + [a:b:stack] -> exec (i+1) [a/b:stack] get_program :: !CompileState -> Program get_program cs |