0 | module Compiler.Common
4 | import Compiler.CompileExpr
5 | import Compiler.Inline
6 | import Compiler.LambdaLift
7 | import Compiler.Opts.Constructor
8 | import Compiler.Opts.CSE
9 | import Compiler.VMCode
11 | import Core.Binary.Prims
12 | import Core.Directory
16 | import Data.String as String
17 | import Data.SortedSet
18 | import Libraries.Data.NameMap
19 | import Libraries.Data.NatSet
20 | import Libraries.Data.WithDefault
21 | import Libraries.Utils.Scheme
32 | data ControlFlow = Continue | Abort
36 | record Codegen where
39 | compileExpr : Ref Ctxt Defs -> Ref Syn SyntaxInfo ->
40 | (tmpDir : String) -> (outputDir : String) ->
41 | ClosedTerm -> (outfile : String) -> Core (Maybe String)
43 | executeExpr : Ref Ctxt Defs -> Ref Syn SyntaxInfo ->
44 | (tmpDir : String) -> ClosedTerm -> Core ()
51 | incCompileFile : Maybe (Ref Ctxt Defs -> Ref Syn SyntaxInfo ->
52 | (sourcefile : String) ->
53 | Core (Maybe (String, List String)))
55 | incExt : Maybe String
60 | data UsePhase = Cases | Lifted | ANF | VMCode
63 | (==) Cases Cases = True
64 | (==) Lifted Lifted = True
66 | (==) VMCode VMCode = True
70 | compare x y = compare (tag x) (tag y)
72 | tag : UsePhase -> Int
79 | record CompileData where
80 | constructor MkCompileData
81 | mainExpr : ClosedCExp
85 | exported : List (Name, String)
87 | namedDefs : List (Name, FC, NamedDef)
88 | lambdaLifted : List (Name, LiftedDef)
92 | anf : List (Name, ANFDef)
95 | vmcode : List (Name, VMDef)
103 | compile : {auto c : Ref Ctxt Defs} ->
104 | {auto s : Ref Syn SyntaxInfo} ->
106 | ClosedTerm -> (outfile : String) -> Core (Maybe String)
107 | compile {c} {s} cg tm out
109 | let tmpDir = execBuildDir d
110 | let outputDir = outputDirWithDefault d
111 | ensureDirectoryExists tmpDir
112 | ensureDirectoryExists outputDir
113 | logTime 1 "Code generation overall" $
114 | compileExpr cg c s tmpDir outputDir tm out
120 | execute : {auto c : Ref Ctxt Defs} ->
121 | {auto s : Ref Syn SyntaxInfo} ->
122 | Codegen -> ClosedTerm -> Core ()
123 | execute {c} {s} cg tm
125 | let tmpDir = execBuildDir d
126 | ensureDirectoryExists tmpDir
127 | executeExpr cg c s tmpDir tm
130 | incCompile : {auto c : Ref Ctxt Defs} ->
131 | {auto s : Ref Syn SyntaxInfo} ->
132 | Codegen -> String -> Core (Maybe (String, List String))
133 | incCompile {c} {s} cg src
134 | = do let Just inc = incCompileFile cg
135 | | Nothing => pure Nothing
141 | getMinimalDef : ContextEntry -> Core (GlobalDef, Maybe (Namespace, Binary))
142 | getMinimalDef (Decoded def) = pure (def, Nothing)
143 | getMinimalDef (Coded ns bin)
144 | = do b <- newRef Bin bin
146 | refsRList <- fromBuf
147 | let refsR = map fromList refsRList
152 | = MkGlobalDef fc name (Erased fc Placeholder) NatSet.empty NatSet.empty NatSet.empty NatSet.empty mul
153 | Scope.empty (specified Public) (MkTotality Unchecked IsCovering) False
154 | [] Nothing refsR False False True
155 | None cdef Nothing [] Nothing
156 | pure (def, Just (ns, bin))
160 | getAllDesc : {auto c : Ref Ctxt Defs} ->
162 | IOArray (Int, Maybe (Namespace, Binary)) ->
170 | getAllDesc [] arr defs = pure ()
171 | getAllDesc (n@(Resolved i) :: rest) arr defs
172 | = do Nothing <- coreLift $
readArray arr i
173 | | Just _ => getAllDesc rest arr defs
174 | case !(lookupContextEntry n (gamma defs)) of
175 | Nothing => do log "compile.execute" 20 $
"Couldn't find " ++ show n
176 | getAllDesc rest arr defs
178 | do (def, bin) <- getMinimalDef entry
179 | ignore $
addDef n def
180 | let refs = refersToRuntime def
181 | if multiplicity def /= erased
182 | then do coreLift_ $
writeArray arr i (i, bin)
183 | let refs = refersToRuntime def
184 | refs' <- traverse toResolvedNames (keys refs)
185 | getAllDesc (refs' ++ rest) arr defs
186 | else do log "compile.execute" 20
187 | $
"Dropping " ++ show n ++ " because it's erased"
188 | getAllDesc rest arr defs
189 | getAllDesc (n :: rest) arr defs
190 | = do log "compile.execute" 20 $
191 | "Ignoring " ++ show n ++ " because it's not a Resolved name"
192 | getAllDesc rest arr defs
194 | warnIfHole : Name -> NamedDef -> Core ()
195 | warnIfHole n (MkNmError _)
196 | = coreLift $
putStrLn $
"Warning: compiling hole " ++ show n
197 | warnIfHole n _ = pure ()
199 | getNamedDef : {auto c : Ref Ctxt Defs}
201 | -> Core (Name, FC, NamedDef)
202 | getNamedDef (n,fc,cdef) =
203 | let ndef = forgetDef cdef
204 | in warnIfHole n ndef >> pure (n,fc,ndef)
206 | replaceEntry : {auto c : Ref Ctxt Defs} ->
207 | (Int, Maybe (Namespace, Binary)) -> Core ()
208 | replaceEntry (i, Nothing) = pure ()
209 | replaceEntry (i, Just (ns, b))
210 | = ignore $
addContextEntry ns (Resolved i) b
212 | natHackNames : List Name
214 | [ UN (Basic "prim__sub_Integer")
215 | , NS typesNS (UN $
Basic "prim__integerToNat")
216 | , NS eqOrdNS (UN $
Basic "compareInteger")
219 | dumpIR : Show def => String -> List (Name, def) -> Core ()
221 | = do let cstrs = map dumpDef lns
222 | Right () <- coreLift $
writeFile fn (fastConcat cstrs)
223 | | Left err => throw (FileErr fn err)
226 | fullShow : Name -> String
227 | fullShow (DN _ n) = show n
228 | fullShow n = show n
230 | dumpDef : (Name, def) -> String
231 | dumpDef (n, d) = fullShow n ++ " = " ++ show d ++ "\n"
235 | nonErased : {auto c : Ref Ctxt Defs} ->
238 | = do defs <- get Ctxt
239 | Just gdef <- lookupCtxtExact n (gamma defs)
240 | | Nothing => pure True
241 | pure (multiplicity gdef /= erased)
244 | addForeignImpl : {auto c : Ref Ctxt Defs} ->
247 | = do defs <- get Ctxt
248 | Just def <- lookupCtxtExact n (gamma defs) | Nothing => pure ()
249 | let Just (MkForeign cs atys retty) = compexpr def | _ => pure ()
250 | let xs = map snd $
filter (\x => fst x == n) $
Prelude.toList defs.options.foreignImpl
251 | setCompiled n (MkForeign (xs ++ cs) atys retty)
255 | getExported : String -> NameMap (List (String, String)) -> List (Name, String)
256 | getExported backend all
257 | = mapMaybe isExp (toList all)
260 | isExp : (Name, List (String, String)) -> Maybe (Name, String)
262 | = do fn <- lookup backend cs
269 | getCompileDataWith : {auto c : Ref Ctxt Defs} ->
271 | (doLazyAnnots : Bool) ->
272 | UsePhase -> ClosedTerm -> Core CompileData
273 | getCompileDataWith exports doLazyAnnots phase_in tm_in
274 | = do log "compile.execute" 10 $
"Getting compiled data for: " ++ show tm_in
275 | sopts <- getSession
276 | let phase = foldl {t=List} (flip $
maybe id max) phase_in $
277 | [ Cases <$ dumpcases sopts
278 | , Lifted <$ dumplifted sopts
279 | , ANF <$ dumpanf sopts
280 | , VMCode <$ dumpvmcode sopts
285 | let metas = addMetas True empty tm_in
286 | for_ (keys metas) $
\ metanm =>
287 | do defs <- get Ctxt
288 | Just gdef <- lookupCtxtExact metanm (gamma defs)
289 | | Nothing => log "compile.execute" 50 $
unwords
292 | , "(probably impossible)"]
293 | let Hole _ _ = definition gdef
295 | let fulln = fullname gdef
296 | let cexp = MkError $
CCrash emptyFC
297 | $
"Encountered unimplemented hole " ++ show fulln
298 | ignore $
addDef metanm ({ compexpr := Just cexp
299 | , namedcompexpr := Just (forgetDef cexp)
303 | let refs = getRefs (Resolved (-
1)) tm_in
304 | exported <- if isNil exports
306 | else getExports defs
307 | log "compile.export" 25 "exporting: \{show $ map fst exported}"
308 | let ns = keys (mergeWith const metas refs) ++ map fst exported
309 | log "compile.execute" 70 $
310 | "Found names: " ++ concat (intersperse ", " $
map show $
ns)
311 | tm <- toFullNames tm_in
312 | natHackNames' <- traverse toResolvedNames natHackNames
315 | asize <- getNextEntry
316 | arr <- coreLift $
newArray asize
319 | logTime 2 "Get names" $
getAllDesc (natHackNames' ++ ns) arr defs
321 | let entries = catMaybes !(coreLift (toList arr))
322 | let allNs = map (Resolved . fst) entries
323 | cns <- traverse toFullNames allNs
324 | log "compile.execute" 30 $
325 | "All names: " ++ concat (intersperse ", " $
map show $
zip allNs cns)
330 | rcns <- filterM nonErased cns
331 | log "compile.execute" 40 $
332 | "Kept: " ++ concat (intersperse ", " $
map show rcns)
334 | logTime 2 "Merge lambda" $
traverse_ mergeLamDef rcns
335 | logTime 2 "Fix arity" $
traverse_ fixArityDef rcns
336 | logTime 2 "Fix foreign bindings" $
traverse_ addForeignImpl rcns
337 | compiledtm <- fixArityExp !(compileExp tm)
339 | (cseDefs, csetm) <- logTime 2 "CSE" $
cse rcns compiledtm
342 | let cseDefs = intrinsicCons ++ cseDefs
344 | namedDefs <- logTime 2 "Forget names" $
345 | traverse getNamedDef cseDefs
347 | let mainname = MN "__mainExpression" 0
348 | (liftedtm, ldefs) <- liftBody {doLazyAnnots} mainname csetm
350 | lifted_in <- if phase >= Lifted
351 | then logTime 2 "Lambda lift" $
352 | traverse (lambdaLift doLazyAnnots) cseDefs
355 | let lifted = (mainname, MkLFun Scope.empty Scope.empty liftedtm) ::
356 | (ldefs ++ concat lifted_in)
358 | anf <- if phase >= ANF
359 | then logTime 2 "Get ANF" $
traverse (\ (n, d) => pure (n, !(toANF d))) lifted
361 | vmcode <- if phase >= VMCode
362 | then logTime 2 "Get VM Code" $
pure (allDefs anf)
366 | whenJust (dumpcases sopts) $
\ f =>
367 | do coreLift $
putStrLn $
"Dumping case trees to " ++ f
368 | dumpIR f (map (\(n, _, def) => (n, def)) namedDefs)
370 | whenJust (dumplifted sopts) $
\ f =>
371 | do coreLift $
putStrLn $
"Dumping lambda lifted defs to " ++ f
374 | whenJust (dumpanf sopts) $
\ f =>
375 | do coreLift $
putStrLn $
"Dumping ANF defs to " ++ f
378 | whenJust (dumpvmcode sopts) $
\ f =>
379 | do coreLift $
putStrLn $
"Dumping VM defs to " ++ f
385 | traverse_ replaceEntry entries
386 | pure (MkCompileData csetm exported namedDefs lifted anf vmcode)
390 | (Name, List (String, String)) ->
391 | Maybe (Name, String)
392 | lookupBackend [] _ = Nothing
393 | lookupBackend (b :: bs) (n, exps) = case find (\(b', _) => b == b') exps of
394 | Just (_, exp) => Just (n, exp)
395 | Nothing => lookupBackend bs (n, exps)
397 | getExports : Defs -> Core (List (Name, String))
398 | getExports defs = traverse (\(n, exp) => pure (!(resolved defs.gamma n), exp)) $
399 | mapMaybe (lookupBackend exports) (toList defs.foreignExports)
405 | getCompileData : {auto c : Ref Ctxt Defs} ->
406 | (doLazyAnnots : Bool) ->
407 | UsePhase -> ClosedTerm -> Core CompileData
408 | getCompileData = getCompileDataWith []
411 | compileTerm : {auto c : Ref Ctxt Defs} ->
412 | ClosedTerm -> Core ClosedCExp
414 | = do tm <- toFullNames tm_in
415 | fixArityExp !(compileExp tm)
417 | compDef : {auto c : Ref Ctxt Defs} -> Name -> Core (Maybe (Name, FC, CDef))
420 | Just def <- lookupCtxtExact n (gamma defs) | Nothing => pure Nothing
421 | let Just cexpr = compexpr def | Nothing => pure Nothing
422 | pure $
Just (n, location def, cexpr)
425 | getIncCompileData : {auto c : Ref Ctxt Defs} ->
426 | (doLazyAnnots : Bool) ->
427 | UsePhase -> Core CompileData
428 | getIncCompileData doLazyAnnots phase
429 | = do defs <- get Ctxt
432 | let ns = keys (toIR defs)
433 | cns <- traverse toFullNames ns
434 | rcns <- filterM nonErased cns
435 | cseDefs <- catMaybes <$> traverse compDef rcns
437 | namedDefs <- traverse getNamedDef cseDefs
439 | lifted_in <- if phase >= Lifted
440 | then logTime 2 "Lambda lift" $
441 | traverse (lambdaLift doLazyAnnots) cseDefs
443 | let lifted = concat lifted_in
444 | anf <- if phase >= ANF
445 | then logTime 2 "Get ANF" $
traverse (\ (n, d) => pure (n, !(toANF d))) lifted
447 | vmcode <- if phase >= VMCode
448 | then logTime 2 "Get VM Code" $
pure (allDefs anf)
450 | pure (MkCompileData (CErased emptyFC) [] namedDefs lifted anf vmcode)
457 | exists : String -> IO Bool
459 | = do Right ok <- openFile f Read
460 | | Left err => pure False
474 | parseCC : List String -> List String -> Maybe (String, List String)
475 | parseCC [] _ = Nothing
476 | parseCC (target::ts) strs = findTarget target strs <|> parseCC ts strs
478 | getOpts : String -> List String
481 | = case span (/= ',') str of
483 | (opt, rest) => opt :: getOpts (assert_total (strTail rest))
484 | hasTarget : String -> String -> Bool
485 | hasTarget target str = case span (/= ':') str of
486 | (targetSpec, _) => targetSpec == target
487 | findTarget : String -> List String -> Maybe (String, List String)
488 | findTarget target [] = Nothing
489 | findTarget target (s::xs) = if hasTarget target s
490 | then case span (/= ':') s of
491 | (t, "") => Just (trim t, [])
492 | (t, opts) => Just (trim t, map trim (getOpts
493 | (assert_total (strTail opts))))
494 | else findTarget target xs
497 | dylib_suffix : String
499 | = cond [(elem os $
the (List String) ["windows", "mingw32", "cygwin32"], "dll"),
500 | (os == "darwin", "dylib")]
504 | locate : {auto c : Ref Ctxt Defs} ->
505 | String -> Core (String, String)
509 | = case words libspec of
511 | [fn] => if '.' `elem` unpack fn
514 | fn ++ "." ++ dylib_suffix
515 | (fn :: ver :: _) =>
518 | cond [(dylib_suffix == "dll",
519 | fn ++ "-" ++ ver ++ ".dll"),
520 | (dylib_suffix == "dylib",
521 | fn ++ "." ++ ver ++ ".dylib")]
522 | (fn ++ "." ++ dylib_suffix ++ "." ++ ver)
524 | fullname <- catch (findLibraryFile fname)
528 | pure (fname, fullname)
531 | copyLib : (String, String) -> Core ()
532 | copyLib (lib, fullname)
533 | = if lib == fullname
535 | else do Right bin <- coreLift $
readFromFile fullname
536 | | Left err => pure ()
537 | Right _ <- coreLift $
writeToFile lib bin
538 | | Left err => throw (FileErr lib err)
545 | getExtraRuntime : List String -> Core String
546 | getExtraRuntime directives
547 | = do fileContents <- traverse Core.readFile paths
548 | pure $
concat $
intersperse "\n" fileContents
550 | getArg : String -> Maybe String
552 | let (k,v) = break (== '=') directive
554 | if (trim k) == "extraRuntime"
555 | then Just $
trim $
substr 1 (length v) v
558 | paths : List String
559 | paths = nub $
mapMaybe getArg $
reverse directives
566 | getWeakMemoLazy : List String -> Bool
567 | getWeakMemoLazy = elem "lazy=weakMemo"
574 | record ConstantPrimitives' str where
575 | constructor MkConstantPrimitives
576 | charToInt : IntKind -> str -> Core str
577 | intToChar : IntKind -> str -> Core str
578 | stringToInt : IntKind -> str -> Core str
579 | intToString : IntKind -> str -> Core str
580 | doubleToInt : IntKind -> str -> Core str
581 | intToDouble : IntKind -> str -> Core str
582 | intToInt : IntKind -> IntKind -> str -> Core str
585 | ConstantPrimitives : Type
586 | ConstantPrimitives = ConstantPrimitives' String
591 | castInt : ConstantPrimitives' str
596 | castInt p from to x =
597 | case ((from, intKind from), (to, intKind to)) of
598 | ((CharType, _) , (_, Just k)) => p.charToInt k x
599 | ((StringType, _), (_, Just k)) => p.stringToInt k x
600 | ((DoubleType, _), (_, Just k)) => p.doubleToInt k x
601 | ((_, Just k), (CharType, _)) => p.intToChar k x
602 | ((_, Just k), (StringType, _)) => p.intToString k x
603 | ((_, Just k), (DoubleType, _)) => p.intToDouble k x
604 | ((_, Just k1), (_, Just k2)) => p.intToInt k1 k2 x
605 | _ => throw $
InternalError $
"invalid cast: + " ++ show from ++ " + ' -> ' + " ++ show to