1{-# LANGUAGE PatternGuards, TypeSynonymInstances, CPP #-}
3module IRTS.Compiler where
6import IRTS.Defunctionalise
8import IRTS.CodegenCommon
10import IRTS.CodegenJava
12import IRTS.CodegenJavaScript
14import IRTS.CodegenLLVM
21import Idris.UnusedArgs
25import Idris.Core.Evaluate
26import Idris.Core.CaseTree
28import Control.Monad.State
32import System.Directory
33import System.Environment
34import System.FilePath ((</>), addTrailingPathSeparator)
38compile :: Codegen -> FilePath -> Term -> Idris ()
41 let tmnames = namesUsed (STerm tm)
42 usedIn <- mapM (allNames []) tmnames
43 let used = [sUN "prim__subBigInt", sUN "prim__addBigInt"] : usedIn
44 defsIn <- mkDecls tm (concat used)
45 findUnusedArgs (concat used)
47 objs <- getObjectFiles codegen
48 libs <- getLibs codegen
49 flags <- getFlags codegen
50 hdrs <- getHdrs codegen
51 impdirs <- allImportDirs
52 let defs = defsIn ++ [(sMN 0 "runMain", maindef)]
53 -- iputStrLn $ showSep "\n" (map show defs)
54 let (nexttag, tagged) = addTags 65536 (liftAll defs)
55 let ctxtIn = addAlist tagged emptyContext
56 iLOG "Defunctionalising"
57 let defuns_in = defunctionalise nexttag ctxtIn
58 logLvl 5 $ show defuns_in
60 let defuns = inline defuns_in
61 logLvl 5 $ show defuns
62 iLOG "Resolving variables for CG"
63 -- iputStrLn $ showSep "\n" (map show (toAlist defuns))
64 let checked = checkDefs defuns (toAlist defuns)
66 dumpCases <- getDumpCases
67 dumpDefun <- getDumpDefun
70 Just f -> runIO $ writeFile f (showCaseTrees defs)
73 Just f -> runIO $ writeFile f (dumpDefuns defuns)
74 triple <- targetTriple
77 iLOG "Building output"
79 OK c -> runIO $ case codegen of
81 codegenC c f outty hdrs
82 (concatMap mkObj objs)
83 (concatMap mkLib libs)
84 (concatMap mkFlag flags ++
85 concatMap incdir impdirs) NONE
87 codegenJava [] c f hdrs libs outty
89 codegenJavaScript JavaScript c hdrs f outty
91 codegenJavaScript Node c hdrs f outty
92 ViaLLVM -> codegenLLVM c triple cpu optimize f outty
93 Bytecode -> dumpBC c f
95 where checkMVs = do i <- getIState
96 case map fst (idris_metavars i) \\ primDefs of
98 ms -> ifail $ "There are undefined metavariables: " ++ show ms
99 inDir d h = do let f = d </> h
100 ex <- doesFileExist f
101 if ex then return f else return h
103 mkLib l = "-l" ++ l ++ " "
106 incdir i = "-I" ++ i ++ " "
109irMain :: TT Name -> Idris LDecl
110irMain tm = do i <- ir tm
111 return $ LFun [] (sMN 0 "runMain") [] (LForce i)
113mkDecls :: Term -> [Name] -> Idris [(Name, LDecl)]
116 let ds = filter (\ (n, d) -> n `elem` used || isCon d) $ ctxtAlist (tt_ctxt i)
117 mapM traceUnused used
118 decls <- mapM build ds
121showCaseTrees :: [(Name, LDecl)] -> String
122showCaseTrees ds = showSep "\n\n" (map showCT ds)
124 showCT (n, LFun _ f args lexp)
125 = show n ++ " " ++ showSep " " (map show args) ++ " =\n\t "
127 showCT (n, LConstructor c t a) = "data " ++ show n ++ " " ++ show a
129isCon (TyDecl _ _) = True
133 ir :: a -> Idris LExp
135build :: (Name, Def) -> Idris (Name, LDecl)
138 case lookup n (idris_scprims i) of
140 let args = map (\x -> sMN x "op") [0..] in
141 return (n, (LFun [] n (take ar args)
142 (LOp op (map (LV . Glob) (take ar args)))))
143 _ -> do def <- mkLDecl n d
144 logLvl 3 $ "Compiled " ++ show n ++ " =\n\t" ++ show def
147getPrim :: IState -> Name -> [LExp] -> Maybe LExp
148getPrim i n args = case lookup n (idris_scprims i) of
150 if (ar == length args)
151 then return (LOp op args)
155declArgs args inl n (LLam xs x) = declArgs (args ++ xs) inl n x
156declArgs args inl n x = LFun (if inl then [Inline] else []) n args x
158mkLDecl n (Function tm _) = do e <- ir tm
159 return (declArgs [] True n e)
160mkLDecl n (CaseOp ci _ _ _ pats cd)
161 = let (args, sc) = cases_runtime cd in
162 do e <- ir (args, sc)
163 return (declArgs [] (case_inlinable ci) n e)
164mkLDecl n (TyDecl (DCon t a) _) = return $ LConstructor n t a
165mkLDecl n (TyDecl (TCon t a) _) = return $ LConstructor n (-1) a
166mkLDecl n _ = return $ (declArgs [] True n LNothing) -- postulate, never run
168instance ToIR (TT Name) where
169 ir tm = ir' [] tm where
171 | (P _ (UN m) _, args) <- unApply tm,
172 m == txt "mkForeignPrim"
174 | (P _ (UN u) _, [_, arg]) <- unApply tm,
175 u == txt "unsafePerformPrimIO"
177 -- TMP HACK - until we get inlining.
178 | (P _ (UN r) _, [_, _, _, _, _, arg]) <- unApply tm,
181 -- Laziness, the old way
182 | (P _ (UN l) _, [_, arg]) <- unApply tm,
184 = do arg' <- ir' env arg
185 error "lazy has crept in somehow"
186-- return $ LLazyExp arg'
187 | (P _ (UN l) _, [_, arg]) <- unApply tm,
189 = do arg' <- ir' env arg
191 -- Laziness, the new way
192 | (P _ (UN l) _, [arg]) <- unApply tm,
194 = do arg' <- ir' env arg
195 return $ LLazyExp arg'
196 | (P _ (UN l) _, [_, _, arg]) <- unApply tm,
198 = do arg' <- ir' env arg
200 | (P _ (UN a) _, [_, _, arg]) <- unApply tm,
201 a == txt "assert_smaller"
203 | (P _ (UN a) _, [_, arg]) <- unApply tm,
204 a == txt "assert_total"
206 | (P _ (UN p) _, [_, arg]) <- unApply tm,
208 = do arg' <- ir' env arg
209 return $ LOp LPar [LLazyExp arg']
210 | (P _ (UN pf) _, [arg]) <- unApply tm,
211 pf == txt "prim_fork"
212 = do arg' <- ir' env arg
213 return $ LOp LFork [LLazyExp arg']
214 | (P _ (UN m) _, [_,size,t]) <- unApply tm,
216 = do size' <- ir' env size
218 return t' -- TODO $ malloc_ size' t'
219 | (P _ (UN tm) _, [_,t]) <- unApply tm,
220 tm == txt "trace_malloc"
223 -- This case is here until we get more general inlining. It's just
224 -- a really common case, and the laziness hurts...
225 | (P _ (NS (UN be) [b,p]) _, [_,x,(App (P _ (UN d) _) t),
226 (App (P _ (UN d') _) e)]) <- unApply tm,
227 be == txt "boolElim" && d == txt "Delay" && d' == txt "Delay"
231 return (LCase x' [LConCase 0 (sNS (sUN "False") ["Bool","Prelude"]) [] e',
232 LConCase 1 (sNS (sUN "True") ["Bool","Prelude"]) [] t'])
233 | (P (DCon t a) n _, args) <- unApply tm
234 = irCon env t a n args
235 | (P (TCon t a) n _, args) <- unApply tm
237 | (P _ n _, args) <- unApply tm
239 args' <- mapM (ir' env) args
240 case getPrim i n args' of
244 = case lookupCtxtExact n
245 (idris_optimisation i) of
246 Just oi -> collapsible oi
249 = case lookupCtxtExact n
250 (idris_callgraph i) of
251 Just (CGInfo _ _ _ _ unusedpos) ->
256 else return (LApp False (LV (Glob n))
257 (mkUnused unused 0 args'))
258 | (f, args) <- unApply tm
260 args' <- mapM (ir' env) args
261 return (LApp False f' args')
262 where mkUnused u i [] = []
263 mkUnused u i (x : xs) | i `elem` u = LNothing : mkUnused u (i + 1) xs
264 | otherwise = x : mkUnused u (i + 1) xs
265-- ir' env (P _ (NS (UN "Z") ["Nat", "Prelude"]) _)
266-- = return $ LConst (BI 0)
267 ir' env (P _ n _) = return $ LV (Glob n)
268 ir' env (V i) | i >= 0 && i < length env = return $ LV (Glob (env!!i))
269 | otherwise = ifail $ "IR fail " ++ show i ++ " " ++ show tm
270 ir' env (Bind n (Lam _) sc)
271 = do let n' = uniqueName n env
272 sc' <- ir' (n' : env) sc
273 return $ LLam [n'] sc'
274 ir' env (Bind n (Let _ v) sc)
275 = do sc' <- ir' (n : env) sc
277 return $ LLet n v' sc'
278 ir' env (Bind _ _ _) = return $ LNothing
279 ir' env (Proj t i) | i == -1
281 return $ LOp (LMinus (ATInt ITBig))
283 ir' env (Proj t i) = do t' <- ir' env t
285 ir' env (Constant c) = return $ LConst c
286 ir' env (TType _) = return $ LNothing
287 ir' env Erased = return $ LNothing
288 ir' env Impossible = return $ LNothing
289-- ir' env _ = return $ LError "Impossible"
291 irCon env t arity n args
292 | length args == arity = buildApp env (LV (Glob n)) args
293 | otherwise = let extra = satArgs (arity - length args) in
294 do sc' <- irCon env t arity n
295 (args ++ map (\n -> P Bound n undefined) extra)
296 return $ LLam extra sc'
298 satArgs n = map (\i -> sMN i "sat") [1..n]
300 buildApp env e [] = return e
301 buildApp env e xs = do xs' <- mapM (ir' env) xs
302 return $ LApp False e xs'
304 doForeign :: [Name] -> [TT Name] -> Idris LExp
305 doForeign env (_ : fgn : args)
306 | (_, (Constant (Str fgnName) : fgnArgTys : ret : [])) <- unApply fgn
307 = let maybeTys = getFTypes fgnArgTys
310 Nothing -> ifail $ "Foreign type specification is not a constant list: " ++ show (fgn:args)
312 args' <- mapM (ir' env) (init args)
313 -- wrap it in a prim__IO
314 -- return $ con_ 0 @@ impossible @@
315 return $ -- LLazyExp $
316 LForeign LANG_C rty fgnName (zip tys args')
317 | otherwise = ifail "Badly formed foreign function call"
319getFTypes :: TT Name -> Maybe [FType]
320getFTypes tm = case unApply tm of
323 fmap (mkIty' ty :) (getFTypes xs)
326mkIty' (P _ (UN ty) _) = mkIty (str ty)
327mkIty' (App (P _ (UN fi) _) (P _ (UN intTy) _))
328 | fi == txt "FIntT" = mkIntIty (str intTy)
329mkIty' (App (App (P _ (UN ff) _) _) (App (P _ (UN fa) _) (App (P _ (UN io) _) _)))
330 | ff == txt "FFunction" && fa == txt "FAny" &&
333mkIty' (App (App (P _ (UN ff) _) _) _)
334 | ff == txt "FFunction" = FFunction
337-- would be better if these FInt types were evaluated at compile time
338-- TODO: add %eval directive for such things
340mkIty "FFloat" = FArith ATFloat
341mkIty "FInt" = mkIntIty "ITNative"
342mkIty "FChar" = mkIntIty "ITChar"
343mkIty "FByte" = mkIntIty "IT8"
344mkIty "FShort" = mkIntIty "IT16"
345mkIty "FLong" = mkIntIty "IT64"
346mkIty "FBits8" = mkIntIty "IT8"
347mkIty "FBits16" = mkIntIty "IT16"
348mkIty "FBits32" = mkIntIty "IT32"
349mkIty "FBits64" = mkIntIty "IT64"
350mkIty "FString" = FString
352mkIty "FManagedPtr" = FManagedPtr
354mkIty "FFunction" = FFunction
355mkIty "FFunctionIO" = FFunctionIO
356mkIty "FBits8x16" = FArith (ATInt (ITVec IT8 16))
357mkIty "FBits16x8" = FArith (ATInt (ITVec IT16 8))
358mkIty "FBits32x4" = FArith (ATInt (ITVec IT32 4))
359mkIty "FBits64x2" = FArith (ATInt (ITVec IT64 2))
360mkIty x = error $ "Unknown type " ++ x
362mkIntIty "ITNative" = FArith (ATInt ITNative)
363mkIntIty "ITChar" = FArith (ATInt ITChar)
364mkIntIty "IT8" = FArith (ATInt (ITFixed IT8))
365mkIntIty "IT16" = FArith (ATInt (ITFixed IT16))
366mkIntIty "IT32" = FArith (ATInt (ITFixed IT32))
367mkIntIty "IT64" = FArith (ATInt (ITFixed IT64))
369zname = sNS (sUN "Z") ["Nat","Prelude"]
370sname = sNS (sUN "S") ["Nat","Prelude"]
372instance ToIR ([Name], SC) where
373 ir (args, tree) = do logLvl 3 $ "Compiling " ++ show args ++ "\n" ++ show tree
375 return $ LLam args tree'
377instance ToIR SC where
381 ir' (UnmatchedCase str) = return $ LError str
382 ir' (ProjCase tm alts) = do tm' <- ir tm
383 alts' <- mapM (mkIRAlt tm') alts
384 return $ LCase tm' alts'
385 ir' (Case n alts) = do alts' <- mapM (mkIRAlt (LV (Glob n))) alts
386 return $ LCase (LV (Glob n)) alts'
387 ir' ImpossibleCase = return LNothing
389 -- special cases for Z and S
390 -- Needs rethink: projections make this fail
391-- mkIRAlt n (ConCase z _ [] rhs) | z == zname
392-- = mkIRAlt n (ConstCase (BI 0) rhs)
393-- mkIRAlt n (ConCase s _ [arg] rhs) | s == sname
396-- return $ LDefaultCase
397-- (LLet arg (LOp LBMinus [n', LConst (BI 1)])
399 mkIRAlt _ (ConCase n t args rhs)
401 return $ LConCase (-1) n args rhs'
402 mkIRAlt _ (ConstCase x rhs)
405 return $ LConstCase x rhs'
408 return $ LDefaultCase rhs'
409 mkIRAlt tm (SucCase n rhs)
411 return $ LDefaultCase (LLet n (LOp (LMinus (ATInt ITBig))
413 LConst (BI 1)]) rhs')
414-- return $ LSucCase n rhs'
415 mkIRAlt _ (ConstCase c rhs)
416 = ifail $ "Can't match on (" ++ show c ++ ")"
417 mkIRAlt _ (DefaultCase rhs)
419 return $ LDefaultCase rhs'
421 matchable (I _) = True
422 matchable (BI _) = True
423 matchable (Ch _) = True
424 matchable (Str _) = True
427 matchableTy (AType (ATInt ITNative)) = True
428 matchableTy (AType (ATInt ITBig)) = True
429 matchableTy (AType (ATInt ITChar)) = True
430 matchableTy StrType = True
432 matchableTy (AType (ATInt (ITFixed IT8))) = True
433 matchableTy (AType (ATInt (ITFixed IT16))) = True
434 matchableTy (AType (ATInt (ITFixed IT32))) = True
435 matchableTy (AType (ATInt (ITFixed IT64))) = True
437 matchableTy _ = False
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list