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
27import Control.Monad.State
31import System.Directory
32import System.Environment
33import System.FilePath ((</>), addTrailingPathSeparator)
37compile :: Codegen -> FilePath -> Term -> Idris ()
40 let tmnames = namesUsed (STerm tm)
41 usedIn <- mapM (allNames []) tmnames
42 let used = [UN "prim__subBigInt", UN "prim__addBigInt"] : usedIn
43 defsIn <- mkDecls tm (concat used)
44 findUnusedArgs (concat used)
46 objs <- getObjectFiles codegen
47 libs <- getLibs codegen
48 hdrs <- getHdrs codegen
49 let defs = defsIn ++ [(MN 0 "runMain", maindef)]
50 -- iputStrLn $ showSep "\n" (map show defs)
51 let (nexttag, tagged) = addTags 65536 (liftAll defs)
52 let ctxtIn = addAlist tagged emptyContext
53 iLOG "Defunctionalising"
54 let defuns_in = defunctionalise nexttag ctxtIn
55 logLvl 5 $ show defuns_in
57 let defuns = inline defuns_in
58 logLvl 5 $ show defuns
59 iLOG "Resolving variables for CG"
60 -- iputStrLn $ showSep "\n" (map show (toAlist defuns))
61 let checked = checkDefs defuns (toAlist defuns)
63 dumpCases <- getDumpCases
64 dumpDefun <- getDumpDefun
67 Just f -> liftIO $ writeFile f (showCaseTrees tagged)
70 Just f -> liftIO $ writeFile f (dumpDefuns defuns)
71 triple <- targetTriple
74 iLOG "Building output"
76 OK c -> liftIO $ case codegen of
78 codegenC c f outty hdrs
79 (concatMap mkObj objs)
80 (concatMap mkLib libs) NONE
82 codegenJava [] c f hdrs libs outty
84 codegenJavaScript JavaScript c f outty
86 codegenJavaScript Node c f outty
87 ViaLLVM -> codegenLLVM c triple cpu optimize f outty
89 Bytecode -> dumpBC c f
90 Error e -> fail $ show e
91 where checkMVs = do i <- getIState
92 case idris_metavars i \\ primDefs of
94 ms -> fail $ "There are undefined metavariables: " ++ show ms
95 inDir d h = do let f = d </> h
97 if ex then return f else return h
99 mkLib l = "-l" ++ l ++ " "
103irMain :: TT Name -> Idris LDecl
104irMain tm = do i <- ir tm
105 return $ LFun [] (MN 0 "runMain") [] (LForce i)
107allNames :: [Name] -> Name -> Idris [Name]
108allNames ns n | n `elem` ns = return []
109allNames ns n = do i <- getIState
110 case lookupCtxt n (idris_callgraph i) of
111 [ns'] -> do more <- mapM (allNames (n:ns)) (map fst (calls ns'))
112 return (nub (n : concat more))
115mkDecls :: Term -> [Name] -> Idris [(Name, LDecl)]
118 let ds = filter (\ (n, d) -> n `elem` used || isCon d) $ ctxtAlist (tt_ctxt i)
119 mapM traceUnused used
120 decls <- mapM build ds
123showCaseTrees :: [(Name, LDecl)] -> String
124showCaseTrees ds = showSep "\n\n" (map showCT ds)
126 showCT (n, LFun _ f args lexp)
127 = show n ++ " " ++ showSep " " (map show args) ++ " =\n\t "
129 showCT (n, LConstructor c t a) = "data " ++ show n ++ " " ++ show a
131isCon (TyDecl _ _) = True
135 ir :: a -> Idris LExp
137build :: (Name, Def) -> Idris (Name, LDecl)
140 case lookup n (idris_scprims i) of
142 let args = map (\x -> MN x "op") [0..] in
143 return (n, (LFun [] n (take ar args)
144 (LOp op (map (LV . Glob) (take ar args)))))
145 _ -> do def <- mkLDecl n d
146 logLvl 3 $ "Compiled " ++ show n ++ " =\n\t" ++ show def
149getPrim :: IState -> Name -> [LExp] -> Maybe LExp
150getPrim i n args = case lookup n (idris_scprims i) of
152 if (ar == length args)
153 then return (LOp op args)
157declArgs args inl n (LLam xs x) = declArgs (args ++ xs) inl n x
158declArgs args inl n x = LFun (if inl then [Inline] else []) n args x
160mkLDecl n (Function tm _) = do e <- ir tm
161 return (declArgs [] True n e)
162mkLDecl n (CaseOp _ inl _ _ pats _ _ args sc) = do e <- ir (args, sc)
163 return (declArgs [] inl 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 (LFun [] n [] (LError ("Impossible declaration " ++ show n)))
168instance ToIR (TT Name) where
169 ir tm = ir' [] tm where
171 | (P _ (UN "mkForeign") _, args) <- unApply tm
173 | (P _ (UN "unsafePerformIO") _, [_, arg]) <- unApply tm
175 -- TMP HACK - until we get inlining.
176 | (P _ (UN "replace") _, [_, _, _, _, _, arg]) <- unApply tm
178 | (P _ (UN "lazy") _, [_, arg]) <- unApply tm
179 = do arg' <- ir' env arg
180 return $ LLazyExp arg'
181 | (P _ (UN "assert_smaller") _, [_, _, _, arg]) <- unApply tm
183 | (P _ (UN "par") _, [_, arg]) <- unApply tm
184 = do arg' <- ir' env arg
185 return $ LOp LPar [LLazyExp arg']
186 | (P _ (UN "fork") _, [arg]) <- unApply tm
187 = do arg' <- ir' env arg
188 return $ LOp LFork [LLazyExp arg']
189 | (P _ (UN "prim__IO") _, [v]) <- unApply tm
192 | (P _ (UN "io_bind") _, [_,_,v,Bind n (Lam _) sc]) <- unApply tm
194 sc' <- ir' (n:env) sc
195 return (LLet n (LForce v') sc')
196 | (P _ (UN "io_bind") _, [_,_,v,k]) <- unApply tm
199 return (LApp False k' [LForce v'])
200 | (P _ (UN "malloc") _, [_,size,t]) <- unApply tm
201 = do size' <- ir' env size
203 return t' -- TODO $ malloc_ size' t'
204 | (P _ (UN "trace_malloc") _, [_,t]) <- unApply tm
207-- | (P _ (NS (UN "S") ["Nat", "Prelude"]) _, [k]) <- unApply tm
208-- = do k' <- ir' env k
209-- return (LOp LBPlus [k', LConst (BI 1)])
210 | (P (DCon t a) n _, args) <- unApply tm
211 = irCon env t a n args
212 | (P _ n _, args) <- unApply tm
214 args' <- mapM (ir' env) args
215 case getPrim i n args' of
220 (idris_optimisation i) of
221 [oi] -> collapsible oi
225 (idris_callgraph i) of
226 [CGInfo _ _ _ _ unusedpos] ->
231 else return (LApp False (LV (Glob n))
232 (mkUnused unused 0 args'))
233 | (f, args) <- unApply tm
235 args' <- mapM (ir' env) args
236 return (LApp False f' args')
237 where mkUnused u i [] = []
238 mkUnused u i (x : xs) | i `elem` u = LNothing : mkUnused u (i + 1) xs
239 | otherwise = x : mkUnused u (i + 1) xs
240-- ir' env (P _ (NS (UN "O") ["Nat", "Prelude"]) _)
241-- = return $ LConst (BI 0)
242 ir' env (P _ n _) = return $ LV (Glob n)
243 ir' env (V i) | i >= 0 && i < length env = return $ LV (Glob (env!!i))
244 | otherwise = error $ "IR fail " ++ show i ++ " " ++ show tm
245 ir' env (Bind n (Lam _) sc)
246 = do let n' = uniqueName n env
247 sc' <- ir' (n' : env) sc
248 return $ LLam [n'] sc'
249 ir' env (Bind n (Let _ v) sc)
250 = do sc' <- ir' (n : env) sc
252 return $ LLet n v' sc'
253 ir' env (Bind _ _ _) = return $ LNothing
254 ir' env (Proj t i) = do t' <- ir' env t
256 ir' env (Constant c) = return $ LConst c
257 ir' env (TType _) = return $ LNothing
258 ir' env Erased = return $ LNothing
259 ir' env Impossible = return $ LNothing
260-- ir' env _ = return $ LError "Impossible"
262 irCon env t arity n args
263 | length args == arity = buildApp env (LV (Glob n)) args
264 | otherwise = let extra = satArgs (arity - length args) in
265 do sc' <- irCon env t arity n
266 (args ++ map (\n -> P Bound n undefined) extra)
267 return $ LLam extra sc'
269 satArgs n = map (\i -> MN i "sat") [1..n]
271 buildApp env e [] = return e
272 buildApp env e xs = do xs' <- mapM (ir' env) xs
273 return $ LApp False e xs'
275 doForeign :: [Name] -> [TT Name] -> Idris LExp
276 doForeign env (_ : fgn : args)
277 | (_, (Constant (Str fgnName) : fgnArgTys : ret : [])) <- unApply fgn
278 = let maybeTys = getFTypes fgnArgTys
281 Nothing -> fail $ "Foreign type specification is not a constant list: " ++ show (fgn:args)
283 args' <- mapM (ir' env) args
284 -- wrap it in a prim__IO
285 -- return $ con_ 0 @@ impossible @@
286 return $ -- LLazyExp $
287 LForeign LANG_C rty fgnName (zip tys args')
288 | otherwise = fail "Badly formed foreign function call"
290getFTypes :: TT Name -> Maybe [FType]
291getFTypes tm = case unApply tm of
294 fmap (mkIty' ty :) (getFTypes xs)
297mkIty' (P _ (UN ty) _) = mkIty ty
298mkIty' (App (P _ (UN "FIntT") _) (P _ (UN intTy) _)) = mkIntIty intTy
299mkIty' (App (App (P _ (UN "FFunction") _) _) (App (P _ (UN "FAny") _) (App (P _ (UN "IO") _) _))) = FFunctionIO
300mkIty' (App (App (P _ (UN "FFunction") _) _) _) = FFunction
303-- would be better if these FInt types were evaluated at compile time
304-- TODO: add %eval directive for such things
306mkIty "FFloat" = FArith ATFloat
307mkIty "FInt" = mkIntIty "ITNative"
308mkIty "FChar" = mkIntIty "ITChar"
309mkIty "FByte" = mkIntIty "IT8"
310mkIty "FShort" = mkIntIty "IT16"
311mkIty "FLong" = mkIntIty "IT64"
312mkIty "FBits8" = mkIntIty "IT8"
313mkIty "FBits16" = mkIntIty "IT16"
314mkIty "FBits32" = mkIntIty "IT32"
315mkIty "FBits64" = mkIntIty "IT64"
316mkIty "FString" = FString
319mkIty "FFunction" = FFunction
320mkIty "FFunctionIO" = FFunctionIO
321mkIty "FBits8x16" = FArith (ATInt (ITVec IT8 16))
322mkIty "FBits16x8" = FArith (ATInt (ITVec IT16 8))
323mkIty "FBits32x4" = FArith (ATInt (ITVec IT32 4))
324mkIty "FBits64x2" = FArith (ATInt (ITVec IT64 2))
325mkIty x = error $ "Unknown type " ++ x
327mkIntIty "ITNative" = FArith (ATInt ITNative)
328mkIntIty "ITChar" = FArith (ATInt ITChar)
329mkIntIty "IT8" = FArith (ATInt (ITFixed IT8))
330mkIntIty "IT16" = FArith (ATInt (ITFixed IT16))
331mkIntIty "IT32" = FArith (ATInt (ITFixed IT32))
332mkIntIty "IT64" = FArith (ATInt (ITFixed IT64))
334zname = NS (UN "O") ["Nat","Prelude"]
335sname = NS (UN "S") ["Nat","Prelude"]
337instance ToIR ([Name], SC) where
338 ir (args, tree) = do logLvl 3 $ "Compiling " ++ show args ++ "\n" ++ show tree
340 return $ LLam args tree'
342instance ToIR SC where
346 ir' (UnmatchedCase str) = return $ LError str
347 ir' (ProjCase tm alts) = do alts' <- mapM (mkIRAlt tm) alts
349 return $ LCase tm' alts'
350 ir' (Case n alts) = do alts' <- mapM (mkIRAlt (P Bound n Erased)) alts
351 return $ LCase (LV (Glob n)) alts'
352 ir' ImpossibleCase = return LNothing
354 -- special cases for O and S
355 -- Needs rethink: projections make this fail
356-- mkIRAlt n (ConCase z _ [] rhs) | z == zname
357-- = mkIRAlt n (ConstCase (BI 0) rhs)
358-- mkIRAlt n (ConCase s _ [arg] rhs) | s == sname
361-- return $ LDefaultCase
362-- (LLet arg (LOp LBMinus [n', LConst (BI 1)])
364 mkIRAlt _ (ConCase n t args rhs)
366 return $ LConCase (-1) n args rhs'
367 mkIRAlt _ (ConstCase x rhs)
370 return $ LConstCase x rhs'
373 return $ LDefaultCase rhs'
374 mkIRAlt _ (ConstCase c rhs)
375 = fail $ "Can't match on (" ++ show c ++ ")"
376 mkIRAlt _ (DefaultCase rhs)
378 return $ LDefaultCase rhs'
380 matchable (I _) = True
381 matchable (BI _) = True
382 matchable (Ch _) = True
383 matchable (Str _) = True
386 matchableTy (AType (ATInt ITNative)) = True
387 matchableTy (AType (ATInt ITBig)) = True
388 matchableTy (AType (ATInt ITChar)) = True
389 matchableTy StrType = True
391 matchableTy (AType (ATInt (ITFixed IT8))) = True
392 matchableTy (AType (ATInt (ITFixed IT16))) = True
393 matchableTy (AType (ATInt (ITFixed IT32))) = True
394 matchableTy (AType (ATInt (ITFixed IT64))) = True
396 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