aboutsummaryrefslogtreecommitdiff
path: root/snug-clean/src/Snug/Compile/Simulate.icl
diff options
context:
space:
mode:
Diffstat (limited to 'snug-clean/src/Snug/Compile/Simulate.icl')
-rw-r--r--snug-clean/src/Snug/Compile/Simulate.icl27
1 files changed, 19 insertions, 8 deletions
diff --git a/snug-clean/src/Snug/Compile/Simulate.icl b/snug-clean/src/Snug/Compile/Simulate.icl
index b383936..672b47e 100644
--- a/snug-clean/src/Snug/Compile/Simulate.icl
+++ b/snug-clean/src/Snug/Compile/Simulate.icl
@@ -17,22 +17,20 @@ import Snug.Syntax
, stack :: ![StackValue]
}
-:: StackValue
- = SVImmediate !Immediate
- | SVRegOffset !Register !Offset /* value is reg + offset */
-
-simulate :: !(Simulator a) -> [Instruction]
-simulate sim = flatten (reverse (execState sim initial).instrs)
+simulate :: ![StackValue] !(Simulator a) -> [Instruction]
+simulate stack sim = flatten (reverse (execState sim initial).instrs)
where
// TODO: when finishing:
// - check that the stack is empty
- // - update heap pointer
initial =
{ instrs = []
, hp_offset = 0
- , stack = []
+ , stack = stack
}
+stackSize :: Simulator Int
+stackSize = gets \s -> length s.stack
+
add :: ![Instruction] -> Simulator ()
add is = modify \s -> {s & instrs=[is:s.instrs]}
@@ -80,6 +78,10 @@ storeStackValue (SVRegOffset reg offset) doffset dreg = add
[ AddImmediate Signed TempImm reg (Immediate offset)
, StoreWord TempImm doffset dreg
]
+storeStackValue (SVIndirect offset reg) doffset dreg = add
+ [ LoadWord TempImm offset reg
+ , StoreWord TempImm doffset dreg
+ ]
buildCons :: !Label !Int -> Simulator ()
buildCons cons nargs =
@@ -104,6 +106,15 @@ where
BVInt i -> i
BVChar c -> toInt c
+pushArg :: !Int !Int -> Simulator ()
+pushArg i j =
+ getState >>= \{stack} ->
+ case stack !! i of
+ SVRegOffset reg offset ->
+ push (SVIndirect (offset + (j+1)*4) reg)
+ _ ->
+ abort "unexpected reference in pushArg\n"
+
indirectAndEval :: Simulator ()
indirectAndEval =
pop >>= \sv ->