-
Notifications
You must be signed in to change notification settings - Fork 12
/
Gm2.hs
247 lines (243 loc) · 9.36 KB
/
Gm2.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
module GM2 where
import Language
import Utils
runProg :: [Char] -> [Char]
runProg = showResults . eval . compile . parse
type GmState
= (GmCode, -- Current instruction stream
GmStack, -- Current stack
GmHeap, -- Heap of nodes
GmGlobals, -- Global addresses in heap
GmStats) -- Statistics
type GmCode = [Instruction]
getCode :: GmState -> GmCode
getCode (i, stack, heap, globals, stats) = i
putCode :: GmCode -> GmState -> GmState
putCode i' (i, stack, heap, globals, stats)
= (i', stack, heap, globals, stats)
type GmStack = [Addr]
getStack :: GmState -> GmStack
getStack (i, stack, heap, globals, stats) = stack
putStack :: GmStack -> GmState -> GmState
putStack stack' (i, stack, heap, globals, stats)
= (i, stack', heap, globals, stats)
type GmHeap = Heap Node
getHeap :: GmState -> GmHeap
getHeap (i, stack, heap, globals, stats) = heap
putHeap :: GmHeap -> GmState -> GmState
putHeap heap' (i, stack, heap, globals, stats)
= (i, stack, heap', globals, stats)
type GmGlobals = ASSOC Name Addr
getGlobals :: GmState -> GmGlobals
getGlobals (i, stack, heap, globals, stats) = globals
statInitial :: GmStats
statIncSteps :: GmStats -> GmStats
statGetSteps :: GmStats -> Int
type GmStats = Int
statInitial = 0
statIncSteps s = s+1
statGetSteps s = s
getStats :: GmState -> GmStats
getStats (i, stack, heap, globals, stats) = stats
putStats :: GmStats -> GmState -> GmState
putStats stats' (i, stack, heap, globals, stats)
= (i, stack, heap, globals, stats')
eval :: GmState -> [GmState]
eval state = state: restStates
where
restStates | gmFinal state = []
| otherwise = eval nextState
nextState = doAdmin (step state)
doAdmin :: GmState -> GmState
doAdmin s = putStats (statIncSteps (getStats s)) s
gmFinal :: GmState -> Bool
gmFinal s = case (getCode s) of
[] -> True
otherwise -> False
step :: GmState -> GmState
step state = dispatch i (putCode is state)
where (i:is) = getCode state
pushglobal :: Name -> GmState -> GmState
pushglobal f state
= putStack (a: getStack state) state
where a = aLookup (getGlobals state) f (error ("Undeclared global " ++ f))
pushint :: Int -> GmState -> GmState
pushint n state
= putHeap heap' (putStack (a: getStack state) state)
where (heap', a) = hAlloc (getHeap state) (NNum n)
mkap :: GmState -> GmState
mkap state
= putHeap heap' (putStack (a:as') state)
where (heap', a) = hAlloc (getHeap state) (NAp a1 a2)
(a1:a2:as') = getStack state
push :: Int -> GmState -> GmState
push n state
= putStack (a:as) state
where as = getStack state
a = getArg (hLookup (getHeap state) (as !! (n+1)))
getArg :: Node -> Addr
getArg (NAp a1 a2) = a2
slide :: Int -> GmState -> GmState
slide n state
= putStack (a: drop n as) state
where (a:as) = getStack state
compile :: CoreProgram -> GmState
compile program
= (initialCode, [], heap, globals, statInitial)
where (heap, globals) = buildInitialHeap program
buildInitialHeap :: CoreProgram -> (GmHeap, GmGlobals)
buildInitialHeap program
= mapAccuml allocateSc hInitial compiled
where compiled = map compileSc (preludeDefs ++ program) ++
compiledPrimitives
type GmCompiledSC = (Name, Int, GmCode)
allocateSc :: GmHeap -> GmCompiledSC -> (GmHeap, (Name, Addr))
allocateSc heap (name, nargs, instns)
= (heap', (name, addr))
where (heap', addr) = hAlloc heap (NGlobal nargs instns)
initialCode :: GmCode
initialCode = [Pushglobal "main", Unwind]
compileSc :: (Name, [Name], CoreExpr) -> GmCompiledSC
compileSc (name, env, body)
= (name, length env, compileR body (zip2 env [0..]))
compileR :: GmCompiler
type GmCompiler = CoreExpr -> GmEnvironment -> GmCode
type GmEnvironment = ASSOC Name Int
compileC :: GmCompiler
compileC (EVar v) env
| elem v (aDomain env) = [Push n]
| otherwise = [Pushglobal v]
where n = aLookup env v (error "Can't happen")
compileC (ENum n) env = [Pushint n]
compileC (EAp e1 e2) env = compileC e2 env ++
compileC e1 (argOffset 1 env) ++
[Mkap]
argOffset :: Int -> GmEnvironment -> GmEnvironment
argOffset n env = [(v, n+m) | (v,m) <- env]
compiledPrimitives :: [GmCompiledSC]
compiledPrimitives = []
showResults :: [GmState] -> [Char]
showResults states
= iDisplay (iConcat [
iStr "Supercombinator definitions", iNewline,
iInterleave iNewline (map (showSC s) (getGlobals s)),
iNewline, iNewline, iStr "State transitions", iNewline, iNewline,
iLayn (map showState states),
iNewline, iNewline,
showStats (last states)])
where (s:ss) = states
showSC :: GmState -> (Name, Addr) -> Iseq
showSC s (name, addr)
= iConcat [ iStr "Code for ", iStr name, iNewline,
showInstructions code, iNewline, iNewline]
where (NGlobal arity code) = (hLookup (getHeap s) addr)
showInstructions :: GmCode -> Iseq
showInstructions is
= iConcat [iStr " Code:{",
iIndent (iInterleave iNewline (map showInstruction is)),
iStr "}", iNewline]
showState :: GmState -> Iseq
showState s
= iConcat [showStack s, iNewline,
showInstructions (getCode s), iNewline]
showStack :: GmState -> Iseq
showStack s
= iConcat [iStr " Stack:[",
iIndent (iInterleave iNewline
(map (showStackItem s) (reverse (getStack s)))),
iStr "]"]
showStackItem :: GmState -> Addr -> Iseq
showStackItem s a
= iConcat [iStr (showaddr a), iStr ": ",
showNode s a (hLookup (getHeap s) a)]
showStats :: GmState -> Iseq
showStats s
= iConcat [ iStr "Steps taken = ", iNum (statGetSteps (getStats s))]
data Instruction = Unwind
| Pushglobal Name
| Pushint Int
| Push Int
| Mkap
| Update Int
| Pop Int
instance Eq Instruction
where
Unwind == Unwind = True
Pushglobal a == Pushglobal b = a == b
Pushint a == Pushint b = a == b
Push a == Push b = a == b
Mkap == Mkap = True
Update a == Update b = a == b
_ == _ = False
data Node
= NNum Int -- Numbers
| NAp Addr Addr -- Applications
| NGlobal Int GmCode -- Globals
| NInd Addr -- Indirections
instance Eq Node
where
NNum a == NNum b = a == b -- needed to check conditions
NAp a b == NAp c d = False -- not needed
NGlobal a b == NGlobal c d = False -- not needed
NInd a == NInd b = False -- not needed
rearrange :: Int -> GmHeap -> GmStack -> GmStack
rearrange n heap as
= take n as' ++ drop n as
where as' = map (getArg . hLookup heap) (tl as)
compileArgs :: [(Name, CoreExpr)] -> GmEnvironment -> GmEnvironment
compileArgs defs env
= zip (map first defs) [n-1, n-2 .. 0] ++ argOffset n env
where n = length defs
boxInteger :: Int -> GmState -> GmState
boxInteger n state
= putStack (a: getStack state) (putHeap h' state)
where (h', a) = hAlloc (getHeap state) (NNum n)
unboxInteger :: Addr -> GmState -> Int
unboxInteger a state
= ub (hLookup (getHeap state) a)
where ub (NNum i) = i
ub n = error "Unboxing a non-integer"
showInstruction Unwind = iStr "Unwind"
showInstruction (Pushglobal f) = (iStr "Pushglobal ") `iAppend` (iStr f)
showInstruction (Push n) = (iStr "Push ") `iAppend` (iNum n)
showInstruction (Pushint n) = (iStr "Pushint ") `iAppend` (iNum n)
showInstruction Mkap = iStr "Mkap"
showInstruction (Update n) = (iStr "Update ") `iAppend` (iNum n)
showInstruction (Pop n) = (iStr "Pop ") `iAppend` (iNum n)
showNode s a (NNum n) = iNum n
showNode s a (NGlobal n g) = iConcat [iStr "Global ", iStr v]
where v = hd [n | (n,b) <- globals, a==b]
globals = getGlobals s
showNode s a (NAp a1 a2) = iConcat [iStr "Ap ", iStr (showaddr a1),
iStr " ", iStr (showaddr a2)]
showNode s a (NInd a1) = iConcat [iStr "Ind ", iStr (showaddr a1)]
dispatch :: Instruction -> GmState -> GmState
dispatch Unwind = unwind
dispatch (Pushglobal f) = pushglobal f
dispatch (Push n) = push n
dispatch (Pushint n) = pushint n
dispatch Mkap = mkap
dispatch (Update n) = update n
dispatch (Pop n) = pop n
update :: Int -> GmState -> GmState
update n state
= putHeap heap' (putStack as state)
where heap' = hUpdate (getHeap state) (as !! n) (NInd a)
(a:as) = getStack state
pop :: Int -> GmState -> GmState
pop n state
= putStack (drop n (getStack state)) state
unwind :: GmState -> GmState
unwind state
= newState (hLookup heap a)
where
(a:as) = getStack state
heap = getHeap state
newState (NNum n) = state
newState (NAp a1 a2) = putCode [Unwind] (putStack (a1:a:as) state)
newState (NGlobal n c)
| length as < n = error "Unwinding with too few arguments"
| otherwise = putCode c state
newState (NInd a1) = putCode [Unwind] (putStack (a1:as) state)
compileR e args = compileC e args ++ [Update n, Pop n, Unwind]
where n = length args