diff options
author | Camil Staps | 2023-02-01 21:24:05 +0100 |
---|---|---|
committer | Camil Staps | 2023-02-01 21:31:07 +0100 |
commit | 5d2f367ea59f5813b95800fcaab1e4d5e16d4a88 (patch) | |
tree | 97069353db5f6b8e2ed8f8020e64166b2d137287 /snug-clean/src/Snug/Compile/Simulate.icl | |
parent | Fix argument order in function calls (diff) |
Implement basic uses of locals
Diffstat (limited to 'snug-clean/src/Snug/Compile/Simulate.icl')
-rw-r--r-- | snug-clean/src/Snug/Compile/Simulate.icl | 27 |
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 -> |