-
Notifications
You must be signed in to change notification settings - Fork 0
/
GM.hs
706 lines (543 loc) · 26 KB
/
GM.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
{-# OPTIONS_GHC -Wall #-}
module GM where
import Control.Monad (foldM)
import Control.Monad.Trans.State hiding (state)
import Data.List (find, isPrefixOf)
import AST
import Heap
import Iseq
import Utils
import Lexer (clex)
import Parser (parse, pPack)
import Main (preludeDefs, extraPreludeDefs, casePreludeDefs)
type GmStats = Int
type GmStack = [Addr]
type GmOutput = [Int]
type GmHeap = Heap Node
type GmDump = [GmDumpItem]
type GmCode = [Instruction]
type GmGlobals = ASSOC Name Addr
type GmDumpItem = (GmCode, GmStack)
type GmEnvironment = ASSOC Name Int
type GmCompiledSC = (Name, Int, GmCode)
type GmCompiler = CoreExpr -> GmEnvironment -> State GmGlobals GmCode
data Node = NNum Int -- Numbers
| NAp Addr Addr -- Applications
| NGlobal Int GmCode -- Globals
| NInd Addr -- Indirection
| NConstr Int [Addr] -- Tag, list of components
data Instruction = Unwind
| PushGlobal Name
| PushInt Int
| Push Int
| MkAp
| Update Int
| Pop Int
| Slide Int
| Alloc Int
| Eval
| Pack Int Int
| CaseJump [(Int, GmCode)]
| Split Int
| Print
| Abort
| Add | Sub | Mul | Div | Neg
| Eq | Ne | Lt | Le | Gt | Ge
deriving (Eq, Show)
data GmState = GmState { gmCode :: GmCode, -- Current instruction stream
gmStack :: GmStack, -- Current stack
gmDump :: GmDump, -- Stack of Code/Stack pairs
gmHeap :: GmHeap, -- Heap of nodes
gmGlobals :: GmGlobals, -- Global addresses in heap
gmOutput :: GmOutput, -- Execution output
gmStats :: GmStats -- Statistics
}
data ShowStateOptions = ShowStateOptions { ssHeap :: Bool
, ssEnv :: Bool
, ssDump :: Bool
, ssLastOnly :: Bool
, ssSCCode :: Bool
}
dbgOpts :: ShowStateOptions
dbgOpts = ShowStateOptions True True True False True
compactOpts :: ShowStateOptions
compactOpts = ShowStateOptions False False False True False
statInitial :: GmStats
statInitial = 0
statIncSteps :: GmStats -> GmStats
statIncSteps s = s + 1
statGetSteps :: GmStats -> Int
statGetSteps s = s
runProg :: String -> String
runProg = showResults compactOpts . eval . compile . parse
runDebugProg :: String -> String
runDebugProg = showResults dbgOpts . eval . compile . parse
eval :: GmState -> [GmState]
eval state = state : restStates
where restStates | gmFinal state = []
| otherwise = eval nextState
nextState = doAdmin (step state)
doAdmin :: GmState -> GmState
doAdmin s = s { gmStats = statIncSteps (gmStats s) }
gmFinal :: GmState -> Bool
gmFinal s | [] <- gmCode s = True
| otherwise = False
step :: GmState -> GmState
step state = dispatch i (state { gmCode = is })
where (i:is) = gmCode state
dispatch (PushGlobal f) = pushGlobal f
dispatch (PushInt n) = pushInt n
dispatch (Pop n) = pop n
dispatch (Push n) = push n
dispatch (Update n) = update n
dispatch (Slide n) = slide n
dispatch (Alloc n) = alloc n
dispatch MkAp = mkAp
dispatch Unwind = unwind
dispatch Eval = forceEval
dispatch Print = primPrint
dispatch Abort = error "Core: abort"
dispatch Neg = aritmetic1 negate
dispatch Add = aritmetic2 (+)
dispatch Sub = aritmetic2 (-)
dispatch Mul = aritmetic2 (*)
dispatch Div = aritmetic2 div
dispatch Eq = comparison (==)
dispatch Ne = comparison (/=)
dispatch Lt = comparison (<)
dispatch Le = comparison (<=)
dispatch Gt = comparison (>)
dispatch Ge = comparison (>=)
dispatch (Pack tag arity) = pack tag arity
dispatch (CaseJump jl) = caseJump jl
dispatch (Split n) = constrSplit n
pushGlobal :: Name -> GmState -> GmState
pushGlobal name state = state { gmStack = addr : (gmStack state) }
where addr = aLookup (gmGlobals state) name (error ("Undeclared global " ++ name))
pushInt :: Int -> GmState -> GmState
pushInt n state = state { gmStack = addr : (gmStack state), gmHeap = heap' }
where (heap', addr) = hAlloc (gmHeap state) (NNum n)
pushIntMemo :: Int -> GmState -> GmState
pushIntMemo n state = state {
gmStack = addr : (gmStack state),
gmHeap = heap',
gmGlobals = globals'
}
where heap = gmHeap state
globals = gmGlobals state
found = aLookup globals (show n) hNull
(globals', (heap', addr))
| hIsNull found = ((show n, addr) : globals, hAlloc heap (NNum n))
| otherwise = (globals, (heap, found))
mkAp :: GmState -> GmState
mkAp state = state { gmStack = addr : rest, gmHeap = heap' }
where (f:arg:rest) = gmStack state
(heap', addr) = hAlloc (gmHeap state) (NAp f arg)
push :: Int -> GmState -> GmState
push n state = state { gmStack = addr : stack }
where stack = gmStack state
addr = stack !! n
pop :: Int -> GmState -> GmState
pop n state = state { gmStack = drop n (gmStack state) }
-- update should create NInd node according to the book.
-- the current implementation directly updates the node
-- saving clutter and.. <indirections>
-- are there any negative consequences I have missed?
update :: Int -> GmState -> GmState
update n state = state { gmStack = rest, gmHeap = heap' }
where (addr:rest) = gmStack state
heap = gmHeap state
-- node = NInd addr
node = hLookup heap addr
heap' = hUpdate heap (rest !! n) node
slide :: Int -> GmState -> GmState
slide n state = state { gmStack = addr : drop n rest }
where (addr:rest) = gmStack state
alloc :: Int -> GmState -> GmState
alloc n state = state { gmStack = addrs ++ gmStack state, gmHeap = heap' }
where allocNode = (\h _ -> hAlloc h $ NInd hNull)
(heap', addrs) = mapAccuml allocNode (gmHeap state) [1..n]
isDataNode :: Node -> Bool
isDataNode (NNum _) = True
isDataNode (NConstr _ _) = True
isDataNode _ = False
forceEval :: GmState -> GmState
forceEval state
| isDataNode node = state
| otherwise = state { gmCode = [ Unwind ],
gmStack = [ addr ],
gmDump = (code, rest) : dump }
where stack = gmStack state
code = gmCode state
dump = gmDump state
(addr:rest) = stack
node = hLookup (gmHeap state) addr
primPrint :: GmState -> GmState
primPrint state = state { gmStack = rest, gmOutput = out : gmOutput state }
where (addr:rest) = gmStack state
node = hLookup (gmHeap state) addr
out | (NNum n) <- node = n
| otherwise = error "Print: number expected"
pack :: Int -> Int -> GmState -> GmState
pack tag arity state
| length parts /= arity = error "NConstr: not enough arguments"
| otherwise = state { gmStack = addr : rest, gmHeap = heap' }
where stack = gmStack state
parts = take arity stack
rest = drop arity stack
(heap', addr) = hAlloc (gmHeap state) (NConstr tag parts)
caseJump :: [(Int, GmCode)] -> GmState -> GmState
caseJump jumpLocs state = state { gmCode = code' ++ gmCode state }
where (addr:_) = gmStack state
node = hLookup (gmHeap state) addr
code' | (NConstr tag _) <- node
= aLookup jumpLocs tag (error "Case: no handler matched")
| otherwise = error "Case: not a constructor node"
constrSplit :: Int -> GmState -> GmState
constrSplit n state = state { gmStack = parts ++ rest }
where (addr:rest) = gmStack state
node = hLookup (gmHeap state) addr
parts | (NConstr _ as) <- node, length as == n
= as
| otherwise = error "Split: not a constructor node or wrong arity"
unwind :: GmState -> GmState
unwind state = newState (hLookup heap addr)
where heap = gmHeap state
stack = gmStack state
dump = gmDump state
(addr:rest) = stack
unwindData
| ((code, stack') : dump') <- dump
= state { gmCode = code, gmStack = addr : stack', gmDump = dump' }
| otherwise = state
newState (NNum _) = unwindData
newState (NConstr _ _) = unwindData
newState (NInd a1) = state { gmCode = [ Unwind ], gmStack = a1:rest }
newState (NAp a1 _) = state { gmCode = [ Unwind ], gmStack = a1:stack }
newState (NGlobal n code)
| nArgs < n = case dump of
((code', stack') : dump') -> state { gmCode = code',
gmStack = last stack : stack',
gmDump = dump' }
_ -> error "Unwinding with too few arguments"
| otherwise = state { gmCode = code, gmStack = rearrange n heap stack }
where nArgs = length rest
rearrange :: Int -> GmHeap -> GmStack -> GmStack
rearrange n heap stack = take n (map getArg (tail stack)) ++ drop n stack
where getArg addr | (NAp _ arg) <- hLookup heap addr = arg
| otherwise = error "Broken spine: non NAp node encountered"
compile :: CoreProgram -> GmState
compile program = GmState initialCode [] [] heap globals [] statInitial
where scDefs = program ++ preludeDefs ++ extraPreludeDefs ++ casePreludeDefs
(heap, globals) = buildInitialHeap scDefs
initialCode :: GmCode
initialCode = [ PushGlobal "main", Unwind ] -- no idea why we need Eval,
-- Unwind works just fine..
buildInitialHeap :: CoreProgram -> (GmHeap, GmGlobals)
buildInitialHeap prog = mapAccuml allocateSc hInitial compiled
where globalsMap = foldr (\(name, _, _) acc -> (name, hNull) : acc) [] prog
(cSc, globalsMap') = runState (mapM compileSc prog) globalsMap
synth = filter (isPrefixOf "$") (map fst globalsMap')
cSynth = map compileS synth
compiled = cSc ++ cSynth ++ compiledPrimitives
allocateSc :: GmHeap -> GmCompiledSC -> (GmHeap, (Name, Addr))
allocateSc heap (name, nargs, instns) = (heap', (name, addr))
where (heap', addr) = hAlloc heap (NGlobal nargs instns)
compileSc :: (Name, [Name], CoreExpr) -> State GmGlobals GmCompiledSC
compileSc (name, env, body) = do code <- compileR body (zip env [0..])
return (name, length env, code)
compileR :: GmCompiler
compileR e env = do code <- compileE e env
return $ code ++ [ Update n, Pop n, Unwind ]
where n = length env
argOffset :: Int -> GmEnvironment -> GmEnvironment
argOffset n env = [(v, n+m) | (v,m) <- env]
compileS :: String -> GmCompiledSC
compileS name = (name, arity, [ Pack tag arity, Update 0, Unwind ])
where (EConstr tag arity) = fst . head . pPack . clex 1 $ tail name
compileC :: GmCompiler
compileC (EVar v) env
| aHasKey v env = return [Push n]
| otherwise = return [PushGlobal v]
where n = aLookup env v (error "Can’t happen")
compileC (ENum n) _ = return [PushInt n]
compileC node@(EAp _ _) env
-- saturated ctor
| (EConstr tag arity : aps) <- spine,
n - 1 == arity
= do (_, code) <- foldM (compileAp env compileC) (n - 2, []) aps
return $ code ++ [Pack tag arity]
-- not-saturated ctor
| (EConstr tag arity : aps) <- spine,
n - 1 /= arity
= do ctorName <- getCtorName tag arity
(_, code) <- foldM (compileAp env compileC) (n - 2, []) aps
return $ code ++ (PushGlobal ctorName : replicate (n - 1) MkAp)
| otherwise
= do (_, code) <- foldM (compileAp env compileC) (n - 1, []) spine
return $ code ++ replicate (n - 1) MkAp
where spine = unfoldAp node
n = length spine
compileC (ELet rec defs expr) env = compileLet compileC rec defs expr env
compileC (EConstr tag arity) _
| arity == 0 = return [Pack tag arity]
| otherwise = do ctorName <- getCtorName tag arity
return [PushGlobal ctorName]
compileC (ECase _ _) _ = error "compileC: ECase: not yet implemented"
compileC (ELam _ _) _ = error "compileC: ELam: not yet implemented"
compileE :: GmCompiler
compileE (ENum n) _ = return [PushInt n]
compileE (ELet rec defs expr) env = compileLet compileE rec defs expr env
compileE (ECase expr alts) env = do cExpr <- compileE expr env
cAlts <- mapM compileA alts
return $ cExpr ++ [CaseJump cAlts]
where compileA (tag, vars, body) = do let n = length vars
env' = zip vars [0..] ++ argOffset n env
cBody <- compileE body env'
return (tag, Split n : cBody ++ [Slide n])
compileE expr env
= do globals <- get
case () of
_ | (EVar op : aps) <- unfoldAp expr,
Just (_, arity, instr) <- findBuiltIn op, -- it is a built-in
not (aHasKey op env), -- and not a local variable
not (aHasKey op globals) -- and not a global variable
-> do let strictAps = take arity aps
lazyAps = drop arity aps
strictRes <- foldM (compileAp env compileE) (length aps - 1, instr) strictAps
(_, code) <- foldM (compileAp env compileC) strictRes lazyAps
return (code ++ replicate (length lazyAps) MkAp)
| otherwise
-> do cExpr <- compileC expr env
return (cExpr ++ [ Eval ])
compileAp :: GmEnvironment
-> GmCompiler
-> (Int, GmCode)
-> CoreExpr
-> State GmGlobals (Int, GmCode)
compileAp env cc (offset, code) expr = do cExpr <- cc expr env'
return (offset - 1, cExpr ++ code)
where env' = argOffset offset env
findBuiltIn :: String -> Maybe (Name, Int, GmCode)
findBuiltIn op = find (\(name, _, _) -> name == op) buildIns
unfoldAp :: CoreExpr -> [CoreExpr]
unfoldAp expr = unfold expr []
where unfold (EAp e1 e2) acc = unfold e1 (e2 : acc)
unfold n acc = n : acc
getCtorName :: Int -> Int -> State GmGlobals String
getCtorName tag arity = do globals <- get
if aHasKey name globals
then return ()
else put ((name, hNull) : globals)
return name
where name = "$Pack{" ++ show tag ++ "," ++ show arity ++ "}"
compileLet :: GmCompiler -> Bool -> [(Name, CoreExpr)] -> GmCompiler
compileLet compiler rec defs expr env
| rec = do cDefs <- compileDefs True defs env'
cExpr <- compiler expr env'
return $ [ Alloc n ]
++ cDefs
++ replicate n (Update $ n - 1)
++ cExpr
++ [ Slide n ]
| otherwise = do cDefs <- compileDefs False defs env
cExpr <- compiler expr env'
return $ cDefs ++ cExpr ++ [ Slide n ]
where n = length defs
env' = zip (map fst defs) [n-1, n-2 .. 0] ++ argOffset n env
compileDefs :: Bool -> [(Name, CoreExpr)] -> GmEnvironment -> State GmGlobals GmCode
compileDefs _ [] _ = return []
compileDefs rec ((name, expr):defs) env = do cExpr <- compileC expr env
cDefs <- compileDefs rec defs env'
return $ cExpr ++ cDefs
where env' | rec = argOffset 1 env
| otherwise = (name, 0) : argOffset 1 env
boxInteger :: Int -> GmState -> GmState
boxInteger n state = state { gmStack = addr : gmStack state, gmHeap = heap' }
where (heap', addr) = hAlloc (gmHeap state) (NNum n)
unboxInteger :: Addr -> GmState -> Int
unboxInteger addr state = case hLookup (gmHeap state) addr of
(NNum n) -> n
_ -> error "Unboxing a non-integer"
boxBoolean :: Bool -> GmState -> GmState
boxBoolean b state = state { gmStack = addr : gmStack state, gmHeap = heap' }
where (heap', addr) = hAlloc (gmHeap state) (findPrimDef key)
findPrimDef prim = NInd (aLookup (gmGlobals state) prim err)
where err = error $ "Primitive definition `" ++ prim ++ "` not found!"
key | b = "True"
| otherwise = "False"
unboxBoolean :: Addr -> GmState -> Bool
unboxBoolean addr state = case hLookup (gmHeap state) addr of
(NNum 1) -> True
(NNum 0) -> False
_ -> error "Unboxing a non-boolean"
primitive1 :: (a -> GmState -> GmState) -- boxing function
-> (Addr -> GmState -> b) -- unboxing function
-> (b -> a) -- operator
-> GmState -> GmState -- in state & retval
primitive1 box unbox op state = box (op (unbox addr state)) (state { gmStack = stack' })
where (addr:stack') = gmStack state
primitive2 :: (a -> GmState -> GmState) -- boxing function
-> (Addr -> GmState -> b) -- unboxing function
-> (b -> b -> a) -- operator
-> GmState -> GmState -- in state & retval
primitive2 box unbox op state = box result (state { gmStack = stack' })
where (a1:a2:stack') = gmStack state
result = unbox a1 state `op` unbox a2 state
aritmetic1 :: (Int -> Int) -> GmState -> GmState
aritmetic1 = primitive1 boxInteger unboxInteger
aritmetic2 :: (Int -> Int -> Int) -> GmState -> GmState
aritmetic2 = primitive2 boxInteger unboxInteger
comparison :: (Int -> Int -> Bool) -> GmState -> GmState
comparison = primitive2 boxBoolean unboxInteger
buildIns :: [(Name, Int, GmCode)] -- (name, arity, instruction)
buildIns = [ ("negate", 1, [Neg]),
("+", 2, [Add]), ("-", 2, [Sub]),
("*", 2, [Mul]), ("/", 2, [Div]),
( ">", 2, [Gt, Eval]), (">=", 2, [Ge, Eval]),
( "<", 2, [Lt, Eval]), ("<=", 2, [Le, Eval]),
("==", 2, [Eq, Eval]), ("/=", 2, [Ne, Eval]) ]
compiledPrimitives :: [GmCompiledSC]
compiledPrimitives = map builtInCC buildIns ++ [
("abort", 0, [ Abort ]),
("print", 2, [ Eval, Print, Update 0, Unwind ])
-- naive:
-- ("print", 2, [ Push 0, Eval, Print, Push 1, Update 2, Pop 2, Unwind ])
]
builtInCC :: (Name, Int, GmCode) -> GmCompiledSC
builtInCC (name, arity, ins) = (name, arity, force ++ ins ++ clean)
where force = concat $ replicate arity [ Push (arity - 1), Eval ]
clean = [ Update arity, Pop arity, Unwind ]
showResults :: ShowStateOptions -> [GmState] -> String
showResults opts states = iDisplay $ iConcat [
scDefOutp,
iStr "State transitions",
iNewline, iNewline,
stateOutp,
iNewline,
showOutput lastState,
iNewline,
showStats lastState
]
where (s:_) = states
lastState = last states
nl2 = iNewline `iAppend` iNewline
scDefOutp | ssSCCode opts = iInterleave iNewline [
iStr "Supercombinator definitions:\n",
iInterleave nl2 (map (showSC s) (gmGlobals s)),
iNewline
]
| otherwise = iNil
stateOutp | ssLastOnly opts = showState dbgOpts lastState `iAppend` iNewline
| otherwise = iLayn (map (showState opts) states)
showFWAddr :: Addr -> Iseq -- Show address in field of width 4
showFWAddr addr = iStr (space (4 - length str) ++ str)
where str = showaddr addr
showSC :: GmState -> (Name, Addr) -> Iseq
showSC s (name, addr) = iConcat [ iStr "Code for ",
iStr name, iNewline,
showInstructions code ]
where (NGlobal _ code) = (hLookup (gmHeap s) addr)
showInstructions :: GmCode -> Iseq
showInstructions [] = iStr "Empty"
showInstructions is = iInterleave iNewline (map showInstruction is)
showInstruction :: Instruction -> Iseq
showInstruction Unwind = iStr "Unwind"
showInstruction MkAp = iStr "MkAp"
showInstruction Abort = iStr "Abort"
showInstruction (PushInt n) = (iStr "PushInt ") `iAppend` (iNum n)
showInstruction (PushGlobal f) = (iStr "PushGlobal ") `iAppend` (iStr f)
showInstruction (Pop n) = (iStr "Pop ") `iAppend` (iNum n)
showInstruction (Push n) = (iStr "Push ") `iAppend` (iNum n)
showInstruction (Update n) = (iStr "Update ") `iAppend` (iNum n)
showInstruction (Slide n) = (iStr "Slide ") `iAppend` (iNum n)
showInstruction (Alloc n) = (iStr "Alloc ") `iAppend` (iNum n)
showInstruction Eval = iStr "Eval"
showInstruction Print = iStr "Print"
showInstruction (Pack tag arity) = iConcat [ iStr "Pack{",
iNum tag, iStr ", ",
iNum arity, iStr "}" ]
showInstruction (CaseJump _) = iStr "CaseJump"
showInstruction (Split n) = iStr "Split " `iAppend` iNum n
showInstruction Add = iStr "Add"
showInstruction Sub = iStr "Sub"
showInstruction Mul = iStr "Mul"
showInstruction Div = iStr "Div"
showInstruction Neg = iStr "Neg"
showInstruction Eq = iStr "Eq"
showInstruction Ne = iStr "Ne"
showInstruction Lt = iStr "Lt"
showInstruction Le = iStr "Le"
showInstruction Gt = iStr "Gt"
showInstruction Ge = iStr "Ge"
showState :: ShowStateOptions -> GmState -> Iseq
showState opts s | null views = iNil
| otherwise = iSplitView views `iAppend` iNewline
where stackLines = showStack s
dumpLines = showDump s
codeLines = showInstructions (gmCode s)
heapLines | ssHeap opts = showHeap s
| otherwise = iNil
envLines | ssEnv opts = showEnv (gmGlobals s)
| otherwise = iNil
views = filter (not . iIsNil) [ heapLines,
envLines,
codeLines,
stackLines,
dumpLines ]
showStack :: GmState -> Iseq
showStack s = iInterleave iNewline stackItems
where stackItems = (map (showStackItem s) (reverse (gmStack s)))
showStackItem :: GmState -> Addr -> Iseq
showStackItem s a = iConcat [ showFWAddr a,
iStr ": ",
showNode s a (hLookup (gmHeap s) a) ]
showNode :: GmState -> Addr -> Node -> Iseq
showNode _ _ (NNum n) = iNum n
showNode _ _ (NInd addr) = iStr "NInd " `iAppend` showFWAddr addr
showNode s a (NGlobal _ _) = iConcat [ iStr "Global ", iStr v ]
where v | (name:_) <- [n | (n,b) <- gmGlobals s, a == b] = name
-- happens with lambda prelude and direct updates instead
-- of indirection
| otherwise = "Unknown"
showNode _ _ (NAp a1 a2) = iConcat [ iStr "Ap ", showFWAddr a1,
iStr " ", showFWAddr a2 ]
showNode _ _ (NConstr tag dta) = iConcat [ iStr "NConstr ", iNum tag, iStr " [",
iIndent $ iInterleave (iStr ",\n") (map showFWAddr dta),
iStr "]" ]
showDump :: GmState -> Iseq
showDump s = iConcat [ iStr " Dump: [",
iIndent (iInterleave iNewline dumpItems),
iStr "]" ]
where dumpItems = map showDumpItem (reverse (gmDump s))
showDumpItem :: GmDumpItem -> Iseq
showDumpItem (code, stack) = iConcat [ iStr "<",
shortShowInstructions 3 code,
iStr ", ",
shortShowStack stack, iStr ">" ]
shortShowInstructions :: Int -> GmCode -> Iseq
shortShowInstructions n code = iConcat [ iStr "{",
iInterleave (iStr "; ") dotcodes,
iStr "}" ]
where codes = map showInstruction (take n code)
dotcodes | length code > n = codes ++ [ iStr "..." ]
| otherwise = codes
showHeap :: GmState -> Iseq
showHeap state = iInterleave iNewline (map formatter tuples)
where formatter (addr, node) = iConcat [ showFWAddr addr,
iStr " -> ",
showNode state addr node ]
heap = gmHeap state
tuples = [ (addr, hLookup heap addr) | addr <- hAddresses heap ]
shortShowStack :: GmStack -> Iseq
shortShowStack stack = iConcat [ iStr "[",
iInterleave (iStr ", ") (map showFWAddr stack),
iStr "]" ]
showEnv :: GmGlobals -> Iseq
showEnv = iInterleave iNewline . map formatter
where formatter (name, addr) = iConcat [ iStr name, iStr " -> ", showFWAddr addr ]
showOutput :: GmState -> Iseq
showOutput s = iConcat [ iStr "Output: ",
iInterleave (iStr ", ") (reverse $ map iNum (gmOutput s)) ]
showStats :: GmState -> Iseq
showStats s = iConcat [ iStr "Steps taken = ", iNum (statGetSteps (gmStats s)) ]