@@ -8,10 +8,30 @@ import CaseLift
8
8
import Fresh
9
9
import Data.List
10
10
11
- compileToGCode = labelProgram . compile . caseLift . parse
11
+ {- --------------------------------------------------
12
+ ------------- Compiler Options --------------------
13
+ ---------------------------------------------------}
12
14
13
- writeGCodeFile name =
14
- writeFile name . iDisplay . printInstructions . compileToGCode
15
+ data Option = TCO
16
+ | OZ
17
+ | File String
18
+ deriving (Eq , Show )
19
+
20
+ isFile :: Option -> Bool
21
+ isFile (File _) = True
22
+ isFile _ = False
23
+
24
+ noTC :: [Option ] -> Bool
25
+ noTC = not . elem TCO
26
+
27
+ {- --------------------------------------------------
28
+ ------------- Compiler Output ---------------------
29
+ ---------------------------------------------------}
30
+
31
+ compileToGCode opts = labelProgram . compile opts . caseLift . parse
32
+
33
+ writeGCodeFile opts name =
34
+ writeFile name . iDisplay . printInstructions . compileToGCode opts
15
35
16
36
{-
17
37
- Code for testing the compilation and labelling of G-Code:
@@ -170,11 +190,13 @@ type GMCompiledSC = (Name, Int, GMCode)
170
190
-- This function compiles to a list of tuples (one for each supercombinator).
171
191
-- Each tuple consists of the name of the SC, its arity, and the Code for the
172
192
-- supercombinator
173
- compile :: CoreProgram -> [GMCompiledSC ]
174
- compile prog = map compileSC (prog ++ preludeDefs)
175
- ++ compiledPrimitives
193
+ compile :: [Option ] -> CoreProgram -> [GMCompiledSC ]
194
+ compile opts prog
195
+ | noTC opts = map (compileSC False ) (prog ++ preludeDefs) ++ compiledPrimitives
196
+ | otherwise = map (compileSC True ) (prog ++ preludeDefs) ++ compiledPrimitives
176
197
177
198
199
+ {- Things below not needed?!
178
200
--This is the top-level compile function, it creates a heap with all of the
179
201
--global function instances
180
202
compileToHeap :: CoreProgram -> (GMHeap, GMGlobals)
@@ -196,18 +218,22 @@ allocateSC :: GMHeap -> GMCompiledSC -> (GMHeap, (Name, Addr))
196
218
allocateSC heap (name, numArgs, gmCode) = (newHeap, (name, addr))
197
219
where
198
220
(newHeap, addr) = hAlloc heap (NGlobal numArgs gmCode)
199
-
221
+ -}
200
222
201
223
{- Below is the section that compiles the coreExpr to GCode
202
224
- ---------------------------------------------------------------------
203
225
- ---------------------------------------------------------------------
204
226
- -}
205
- compileSC :: (Name , [Name ], CoreExpr ) -> GMCompiledSC
206
- compileSC (name, env, body)
207
- = (name, d, compileR body (zip env [0 .. ]) d)
227
+ compileSC :: Bool -> (Name , [Name ], CoreExpr ) -> GMCompiledSC
228
+ compileSC b (name, env, body)
229
+ | b = (name, d, compileR body (zip env [0 .. ]) d)
230
+ | otherwise = (name, d, compileRO body (zip env [0 .. ]) d)
208
231
where
209
232
d = length env
210
233
234
+ compileRO :: GMCompiler
235
+ compileRO exp env d = compileE exp env d ++ [Update d, Pop d, Unwind ]
236
+
211
237
compileR :: GMCompiler
212
238
compileR (ELet recursive defs e) env d
213
239
| recursive = compileLetRec True compileR defs e env d
0 commit comments