-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.hs
339 lines (325 loc) · 18 KB
/
Main.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
-- | Main program.
--
-- Source:
--
-- * If a source file name is not given in the command line, the source FL
-- program is assumed to come from standard input as a Haskell expression
-- (use @mhparse@ to generate it from a given file).
--
-- * If a source file name is given, it is passed through the Haskell
-- lexer/parser and transformed internally to FL.
--
{-# LANGUAGE CPP, MagicHash #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Main (main) where
import SLIC.AuxFun (ierr, pathOf, showNum)
import SLIC.CompManager
import SLIC.Constants
import SLIC.DFI (parseDFI)
import SLIC.Front.HStoHF (fromHStoHF)
import SLIC.LAR.LARLinker (linkLAR)
import SLIC.State
import SLIC.SyntaxAux (Mod(..))
import SLIC.Types (FileName, FPath, MName, PPrint(..), TEnv)
#ifdef USE_GHC
import GHC (DynFlags(..), GhcLink(..), GhcMode(..), TypecheckedModule(..),
defaultErrorHandler, getSessionDynFlags, runGhc, setSessionDynFlags)
import GHC.Paths (libdir)
import DynFlags (defaultFatalMessager, defaultFlushOut, xopt_set)
import qualified GHC.LanguageExtensions.Type as GHCExtType
import Outputable (Outputable, ppr)
import SLIC.Front.GHCFrontEnd (getVTypes, tcGHC)
-- import SLIC.Front.GHCBackEnd (coreGHC, transfCore)
#endif /* USE_GHC */
import Data.List (isPrefixOf, map)
import Language.Haskell.Exts.Parser as Parser
import Language.Haskell.Exts.Extension
import Language.Haskell.Exts.SrcLoc
import System.Environment
-- | Prints a usage message.
usage :: IO ()
usage = do putStrLn "Usage: gic <options> <file.hs>"
putStrLn "* General options:"
putStrLn " -help : display this help and exit"
putStrLn " -s : check the validity of the FL program"
putStrLn " -p0 : print the FL program"
putStrLn " -p0pre : print the FL program (after preprocessing)"
putStrLn " -p1 : preprocess and print the initial HIL program"
putStrLn " -p2 : preprocess and print the transformed HIL program"
putStrLn " -pz : print the transformed 0-order intensional program"
putStrLn " -plar : print the transformed 0-order LAR program"
putStrLn " -penv : preprocess and print the typing environment"
putStrLn " -df : defunctionalize and print resulting HIL program"
putStrLn " -v : print defunctionalized types"
putStrLn " -fl : evaluate the FL program with the non-strict FL interpreter"
putStrLn " -enum : optimize enumeration datatypes"
putStrLn " -null-df: do nullary defunctionalization (can do FL->FOFL without new data types)"
#ifdef USE_GHC
putStrLn " -ghc-core : use GHC as a front-end until Core"
putStrLn "* Type inference:"
putStrLn " -ghc-tc : use GHC to parse/typecheck source code"
putStrLn " -gic-tc : use the built-in type inference engine"
putStrLn " -gic-tc-nsig: same as -gic-tc, ignore type signatures"
#endif
putStrLn "* LAR back-end:"
putStrLn " -cl : transform and compile the 0-order program to C (using lazy activation records)"
putStrLn " -debug : keep extra debugging information"
putStrLn " -v : produce a graph trace file after program execution"
putStrLn " -semigc : enable semispace garbage collection (EXPERIMENTAL)"
putStrLn(" -estack S : use an explicit stack of at most S words (default="++(showNum defaultEStackSize)++")")
putStrLn " -libgc : use Boehm GC [libgc] (default)"
putStrLn " -compact : use the compact x86-64 mode"
putStrLn " -fop : use the fast integer operators (EXPERIMENTAL)"
putStrLn(" -mem M : use M bytes of memory (default="++(showNum defaultMemSize)++")")
putStrLn " -strict : insert strictness annotations for all function"
putStrLn " formals and constructor args"
putStrLn " -tag : embed tags in constructors"
putStrLn(" -pdfi : print information about a "++dfiSuffix++" file")
putStrLn " Optimizations:"
putStrLn " -heap : allocate all lazy activation records on the heap (no escape analysis)"
putStrLn " -no-sharing : skip sharing analysis"
putStrLn " -tco : do tail-call optimization"
putStrLn "* Eduction back-end:"
putStrLn " -e : transform and evaluate the 0-order program (lazy eduction)"
putStrLn(" -maxwh N : maximum warehouse entries before GC (default="++(showNum defaultMaxWHSize)++")")
putStrLn " -v : show debugging trace during evaluation"
putStrLn "* Erlang back-end:"
putStrLn " -erl : transform and pretty print the 0-order program for the Erlang intepreter"
putStrLn(" -ctxts N : each warehouse can hold at most N contexts (default="++(showNum defaultMaxCtxts)++")")
putStrLn " -redis : use the Redis-based warehouse"
putStrLn " -v : show debugging trace during evaluation"
putStrLn(" -wh N : use N warehouses (default="++(show defaultWhs)++")")
putStrLn "* Maude back-end:"
putStrLn " -cm : transform and compile the 0-order program to Maude"
putStrLn(" -wh N : use N warehouses (default="++(show defaultWhs)++")")
putStrLn "* Built-in testing interpreters:"
putStrLn " -ecbn : transform and evaluate the 0-order program (call-by-name)"
putStrLn " -v : trace the evaluation"
putStrLn "* Tagged-Token Dataflow back-end: (EXPERIMENTAL)"
putStrLn " -dfg : generate tagged-token dataflow graph for Graphviz"
putStrLn " -ettd : transform and evaluate the 0-order program for tagged-token dataflow"
putStrLn(" -workers W : use W workers (default="++(showNum defaultWorkers)++")")
putStrLn " -pttd : transform and print the TTD program"
putStrLn "* Compilation mode:"
putStrLn " -whole : whole program defunctionalization and compilation"
putStrLn " -cmod : separately compile a single module"
putStrLn " -v : verbose compilation mode"
putStrLn " -link : link a set of compiled modules and module interfaces to generate an executable"
putStrLn " -v : verbose linking mode"
-- | Processes the command line args given.
processArgs :: [String] -> IO Options
processArgs cmdArgs =
let aux [] opts = return opts
aux ("-p0" : args) opts = aux args opts{optAction=APrintFL}
aux ("-p0pre" : args) opts = aux args opts{optAction=APrintFLPre}
aux ("-p1" : args) opts = aux args opts{optAction=APrintHIL1}
aux ("-p2" : args) opts = aux args opts{optAction=APrintHIL2}
aux ("-s" : args) opts = aux args opts{optAction=ACheck}
aux ("-penv" : args) opts = aux args opts{optAction=APrintEnv}
aux ("-pz" : args) opts = aux args opts{optAction=APrintZOIL}
aux ("-plar" : args) opts = aux args opts{optAction=APrintLAR}
aux ("-pttd" : args) opts = aux args opts{optAction=APrintTTD}
aux ("-pdfi" : args) opts = aux args opts{optAction=APrintDFI}
aux ("-cl" : args) opts = aux args opts{optAction=ACompileLAR}
aux ("-ecbn" : args) opts = aux args opts{optAction=AEvalZOILCBN}
aux ("-e" : args) opts = aux args opts{optAction=AEvalZOILLazy}
aux ("-fl" : args) opts = aux args opts{optAction=AEvalFL}
aux ("-cm" : args) opts = aux args opts{optAction=ACompileMaude}
aux ("-ettd" : args) opts = aux args opts{optAction=AEvalTTD}
aux ("-erl" : args) opts = aux args opts{optAction=AEvalErl}
aux ("-df" : args) opts = aux args opts{optAction=APrintDF}
aux ("-debug" : args) opts = aux args opts{optDebug=True}
aux ("-v" : args) opts = aux args opts{optVerbose=True}
aux ("-enum" : args) opts = aux args opts{optOptEnums=True}
aux ("-null-df":args) opts = aux args opts{optNullDf=True}
aux ("-strict": args) opts = aux args opts{optStrict=True}
aux ("-semigc": args) opts = aux args opts{optLARStyle=LAR}{optScrut = True}
aux ("-libgc" : args) opts = aux args opts{optLARStyle=LAROPT}
aux ("-compact":args) opts = aux args opts{optLARStyle=LAR64}{optScrut = True}
aux ("-fop" : args) opts = aux args opts{optFastOp=True}
aux ("-tag" : args) opts = aux args opts{optTag=True}
aux ("-no-sharing":args) opts = aux args opts{optSharing=False}
aux ("-tco" : args) opts = aux args opts{optTCO=True}
aux ("-ghc-tc": args) opts = aux args opts{optGHC=GHCTc}{optTC=GHCTypeInf}
aux ("-gic-tc": args) opts = aux args opts{optGHC=NoGHC}{optTC=GICTypeInf True}
aux ("-gic-tc-nsig": args) opts = aux args opts{optGHC=NoGHC}{optTC=GICTypeInf False}
aux ("-ghc-core": args) opts = aux args opts{optGHC=GHCCore}{optTC=GHCTypeInf}
aux ("-heap" : args) opts = aux args opts{optHeap=True}
aux ("-redis" : args) opts = aux args opts{optWhRedis=True}
aux ("-whole" : args) opts = aux args opts{optCMode=Whole}
aux ("-cmod" : args) opts = aux args opts{optCMode=CompileModule}
aux ("-link" : args) opts = aux args opts{optLink=True}
aux ("-dfg" : args) opts = aux args opts{optAction=AGenerateDFG}
aux ("-wh" : arg : args) opts =
let nml :: [(Int, String)]
nml = reads arg
in case nml of
[(n, "")] -> aux args opts{optWhNum=n}
_ -> usage >> return opts{optAction=ANone}
aux ("-maxwh" : arg : args) opts =
let nml :: [(Int, String)]
nml = reads arg
in case nml of
[(n, "")] -> aux args opts{optWhSize=n}
_ -> usage >> return opts{optAction=ANone}
aux ("-mem" : arg : args) opts =
let mem :: [(Int, String)]
mem = reads arg
in case mem of
[(memSz, "")] -> aux args opts{optMaxMem=memSz}
_ -> usage >> return opts{optAction=ANone}
aux ("-estack": arg : args) opts =
let estack :: [(Int, String)]
estack = reads arg
in case estack of
[(estackSz, "")] -> aux args opts{optEStackSz=estackSz}
_ -> usage >> return opts{optAction=ANone}
aux ("-ctxts" : arg : args) opts =
let ctxts :: [(Int, String)]
ctxts = reads arg
in case ctxts of
[(n, "")] -> aux args opts{optMaxCtxts=n}
_ -> usage >> return opts{optAction=ANone}
aux ("-workers":arg : args) opts =
let nmw :: [(Int, String)]
nmw = reads arg
in case nmw of
[(n, "")] -> aux args opts{optNWorkers=n}
_ -> usage >> return opts{optAction=ANone}
aux ("-help" : _) opts = usage >> return opts{optAction=ANone}
aux (arg : args) opts =
if "-" `isPrefixOf` arg then
error ("Unknown flag " ++ arg ++ ", use -help to see all available options.")
else
case optInput opts of
Nothing -> aux args opts{optInput = Just [arg]}
Just files -> aux args opts{optInput = Just (arg:files)}
sanitizeOpts opts
| optTCO opts && optSharing opts =
printW "disabling sharing analysis, not compatible with TCO" >>
sanitizeOpts (opts{optSharing=False})
| optNullDf opts && ((optCMode opts == CompileModule)||(optLink opts)) =
printW "disabling nullary defunctionalization, not compatible with separate compilation" >>
sanitizeOpts (opts{optNullDf=False})
| otherwise = return opts
in do opts <- aux cmdArgs defaultOptions
sanitizeOpts opts
-- | Prints a warning.
printW :: String -> IO ()
printW w = putStrLn $ "warning: "++w
-- | Entry point, reads from a file (or stdin if no file given) and calls
-- the main part of the compiler.
main :: IO ()
main =
do args <- getArgs
opts <- processArgs args
case optAction opts of
ANone -> return ()
APrintDFI ->
case optInput opts of
Nothing ->
error $ "You must give one or more "++dfiSuffix++" files."
Just files ->
do dfis <- mapM parseDFI files
_ <- mapM putStrLn $ Data.List.map (\dfi->pprint dfi "") dfis
return ()
_ -> case optInput opts of
Nothing -> error "No source file was given."
Just files -> parseAndProcessFL files opts
-- | Processes a source file containg FL.
parseAndProcessFL :: [FileName] -> Options -> IO ()
parseAndProcessFL files opts =
let parseFiles :: [FileName] -> IO [ModFPre]
parseFiles [] = return []
parseFiles (f:fs) = do text <- readFile f
modftc <- parseFL opts f text
progs <- parseFiles fs
return (modftc : progs)
useGHC (modsFL, mg) =
do let mNames = map (fst.modNameF.fst) modsFL
let fPath = (snd.modNameF.fst) (modsFL !! 0) -- 1st directory is used
tEnv <- runThroughGHC mNames fPath mg opts
let (mFL, tcis) : msFL = modsFL
let mFL' = mFL{modTAnnot=tEnv}
case msFL of
[] -> contProcFL opts path ((mFL', tcis) : msFL) []
_ -> error "The GHC modes only support a single module."
path = pathOf (files !! 0)
in if optLink opts then
case optAction opts of
APrintDF ->
error "The -df option cannot be combined with -link."
ACompileLAR -> linkLAR files opts
AEvalZOILLazy ->
let files' = map (++".hs") files
in parseAndProcessFL files' opts
_ -> error "The selected back-end does not support linking."
else
do modsFL <- parseFiles files
modsFLGraph <- rearrangeMods (optDebug opts) modsFL
case optGHC opts of
GHCCore -> useGHC modsFLGraph
GHCTc -> useGHC modsFLGraph
NoGHC -> contProcFL opts path (fst modsFLGraph) []
-- | Runs source modules through the GHC front-end.
runThroughGHC :: [MName] -> FPath -> [MName] -> Options -> IO TEnv
#ifdef USE_GHC
runThroughGHC mNames fPath mg opts =
let wrapper func =
defaultErrorHandler defaultFatalMessager defaultFlushOut $ do
runGhc (Just libdir) $ do
dflags <- getSessionDynFlags
let dflags1 =
case optCMode opts of
CompileModule -> dflags{ghcMode=OneShot}{ghcLink=NoLink}
Whole -> dflags -- {ghcMode=OneShot}{hscTarget=HscNothing}{ghcLink=NoLink}{verbosity=4}{outputHi=Nothing}
let dflags2 = foldl xopt_set dflags1
[GHCExtType.Cpp, GHCExtType.ImplicitPrelude,
GHCExtType.MagicHash, GHCExtType.GADTs,
GHCExtType.GADTSyntax]
_ <- setSessionDynFlags dflags2
func dflags2 fPath mNames mg
in case optGHC opts of
NoGHC -> ierr "runThroughGHC: the GHC API is not selected"
GHCTc ->
do (dflags, tMod) <- wrapper tcGHC
let tEnv = getVTypes dflags (tm_typechecked_source tMod)
-- putStrLn "---------------------------"
-- putStr (pprintE tEnv "")
-- putStrLn "---------------------------"
-- force the result (and therefore type checking)
return tEnv
GHCCore ->
error "the GHC back-end is currently disabled"
-- do (dflags', cMod) <- wrapper coreGHC
-- binds <- cMod
-- transfCore opts dflags' binds []
-- ierr "TODO: No typing reader integrated yet for GHCCore."
instance Outputable TypecheckedModule where
ppr tmod = ppr $ tm_typechecked_source tmod
#else
runThroughGHC _ _ = error "GHC preprocessing not compiled in."
#endif
-- | Parses FL module source code. If the code imports names from other modules,
-- it reads the corresponding DFIs to find their types. The DFIs are to be
-- found in the same directory as the input module. Returns the module and the
-- external signatures needed to run it through the intensional transformation.
parseFL :: Options -> FPath -> String -> IO ModFPre
parseFL opts f text =
let pMode = ParseMode { parseFilename = f
, baseLanguage = Haskell98
, Parser.extensions =
[ EnableExtension GADTs
, EnableExtension BangPatterns
]
, ignoreLanguagePragmas = False
, ignoreLinePragmas = False
, ignoreFunctionArity = True
, fixities = Nothing
}
in case Parser.parseModuleWithMode pMode text of
ParseFailed (SrcLoc _ srcLine0 srcColumn0) message ->
error ("Parse error: "++(show srcLine0)++","++
(show srcColumn0)++": "++message)
ParseOk hsMod ->
return $ fromHStoHF opts f hsMod -- translate Haskell source to FL