1{-# LANGUAGE PatternGuards #-}
3module IRTS.Compiler where
6import IRTS.Defunctionalise
8import IRTS.CodegenCommon
10import IRTS.CodegenJava
14import Idris.UnusedArgs
20import Control.Monad.State
24import System.Directory
25import System.Environment
29compile :: Target -> FilePath -> Term -> Idris ()
32 let tmnames = namesUsed (STerm tm)
33 usedIn <- mapM (allNames []) tmnames
34 let used = [UN "prim__subBigInt", UN "prim__addBigInt"] : usedIn
35 defsIn <- mkDecls tm (concat used)
36 findUnusedArgs (concat used)
38 objs <- getObjectFiles
41 let defs = defsIn ++ [(MN 0 "runMain", maindef)]
42 -- iputStrLn $ showSep "\n" (map show defs)
43 let (nexttag, tagged) = addTags 65536 (liftAll defs)
44 let ctxtIn = addAlist tagged emptyContext
45 iLOG "Defunctionalising"
46 let defuns_in = defunctionalise nexttag ctxtIn
47 logLvl 5 $ show defuns_in
49 let defuns = inline defuns_in
50 logLvl 5 $ show defuns
51 iLOG "Resolving variables for CG"
52 -- iputStrLn $ showSep "\n" (map show (toAlist defuns))
53 let checked = checkDefs defuns (toAlist defuns)
55 dumpCases <- getDumpCases
56 dumpDefun <- getDumpDefun
59 Just f -> liftIO $ writeFile f (showCaseTrees tagged)
62 Just f -> liftIO $ writeFile f (dumpDefuns defuns)
63 iLOG "Building output"
65 OK c -> case target of
66 ViaC -> liftIO $ codegenC c f outty hdrs
67 (concatMap mkObj objs)
68 (concatMap mkLib libs) NONE
69 ViaJava -> liftIO $ codegenJava c f outty
70 Error e -> fail $ show e
71 where checkMVs = do i <- get
72 case idris_metavars i \\ primDefs of
74 ms -> fail $ "There are undefined metavariables: " ++ show ms
75 inDir d h = do let f = d ++ "/" ++ h
77 if ex then return f else return h
79 mkLib l = "-l" ++ l ++ " "
83irMain :: TT Name -> Idris LDecl
84irMain tm = do i <- ir tm
85 return $ LFun (MN 0 "runMain") [] (LForce i)
87allNames :: [Name] -> Name -> Idris [Name]
88allNames ns n | n `elem` ns = return []
89allNames ns n = do i <- get
90 case lookupCtxt Nothing n (idris_callgraph i) of
91 [ns'] -> do more <- mapM (allNames (n:ns)) (map fst (calls ns'))
92 return (nub (n : concat more))
95mkDecls :: Term -> [Name] -> Idris [(Name, LDecl)]
98 let ds = filter (\ (n, d) -> n `elem` used || isCon d) $ ctxtAlist (tt_ctxt i)
100 decls <- mapM build ds
103showCaseTrees :: [(Name, LDecl)] -> String
104showCaseTrees ds = showSep "\n\n" (map showCT ds)
106 showCT (n, LFun f args lexp)
107 = show n ++ " " ++ showSep " " (map show args) ++ " =\n\t "
109 showCT (n, LConstructor c t a) = "data " ++ show n ++ " " ++ show a
111isCon (TyDecl _ _) = True
115 ir :: a -> Idris LExp
117build :: (Name, Def) -> Idris (Name, LDecl)
120 case lookup n (idris_scprims i) of
122 let args = map (\x -> MN x "op") [0..] in
123 return (n, (LFun n (take ar args)
124 (LOp op (map (LV . Glob) (take ar args)))))
125 _ -> do def <- mkLDecl n d
126 logLvl 3 $ "Compiled " ++ show n ++ " =\n\t" ++ show def
129declArgs args n (LLam xs x) = declArgs (args ++ xs) n x
130declArgs args n x = LFun n args x
132mkLDecl n (Function tm _) = do e <- ir tm
133 return (declArgs [] n e)
134mkLDecl n (CaseOp _ _ _ _ pats _ _ args sc) = do e <- ir (args, sc)
135 return (declArgs [] n e)
136mkLDecl n (TyDecl (DCon t a) _) = return $ LConstructor n t a
137mkLDecl n (TyDecl (TCon t a) _) = return $ LConstructor n (-1) a
138mkLDecl n _ = return (LFun n [] (LError ("Impossible declaration " ++ show n)))
140instance ToIR (TT Name) where
141 ir tm = ir' [] tm where
143 | (P _ (UN "mkForeign") _, args) <- unApply tm
145 | (P _ (UN "unsafePerformIO") _, [_, arg]) <- unApply tm
147 -- TMP HACK - until we get inlining.
148 | (P _ (UN "replace") _, [_, _, _, _, _, arg]) <- unApply tm
150 | (P _ (UN "lazy") _, [_, arg]) <- unApply tm
151 = do arg' <- ir' env arg
152 return $ LLazyExp arg'
153 | (P _ (UN "par") _, [_, arg]) <- unApply tm
154 = do arg' <- ir' env arg
155 return $ LOp LPar [LLazyExp arg']
156 | (P _ (UN "fork") _, [arg]) <- unApply tm
157 = do arg' <- ir' env arg
158 return $ LOp LFork [LLazyExp arg']
159 | (P _ (UN "prim__IO") _, [v]) <- unApply tm
162 | (P _ (UN "io_bind") _, [_,_,v,k]) <- unApply tm
165 return (LApp False k' [LForce v'])
166 | (P _ (UN "malloc") _, [_,size,t]) <- unApply tm
167 = do size' <- ir' env size
169 return t' -- TODO $ malloc_ size' t'
170 | (P _ (UN "trace_malloc") _, [_,t]) <- unApply tm
173-- | (P _ (NS (UN "S") ["Nat", "Prelude"]) _, [k]) <- unApply tm
174-- = do k' <- ir' env k
175-- return (LOp LBPlus [k', LConst (BI 1)])
176 | (P (DCon t a) n _, args) <- unApply tm
177 = irCon env t a n args
178 | (P _ n _, args) <- unApply tm
180 let collapse = case lookupCtxt Nothing n (idris_optimisation i) of
181 [oi] -> collapsible oi
183 let unused = case lookupCtxt Nothing n (idris_callgraph i) of
184 [CGInfo _ _ _ _ unusedpos] -> unusedpos
186 args' <- mapM (ir' env) args
187 if collapse then return LNothing
188 else return (LApp False (LV (Glob n))
189 (mkUnused unused 0 args'))
190 | (f, args) <- unApply tm
192 args' <- mapM (ir' env) args
193 return (LApp False f' args')
194 where mkUnused u i [] = []
195 mkUnused u i (x : xs) | i `elem` u = LNothing : mkUnused u (i + 1) xs
196 | otherwise = x : mkUnused u (i + 1) xs
197-- ir' env (P _ (NS (UN "O") ["Nat", "Prelude"]) _)
198-- = return $ LConst (BI 0)
199 ir' env (P _ n _) = return $ LV (Glob n)
200 ir' env (V i) | i < length env = return $ LV (Glob (env!!i))
201 | otherwise = error $ "IR fail " ++ show i ++ " " ++ show tm
202 ir' env (Bind n (Lam _) sc)
203 = do let n' = uniqueName n env
204 sc' <- ir' (n' : env) sc
205 return $ LLam [n'] sc'
206 ir' env (Bind n (Let _ v) sc)
207 = do sc' <- ir' (n : env) sc
209 return $ LLet n v' sc'
210 ir' env (Bind _ _ _) = return $ LNothing
211 ir' env (Proj t i) = do t' <- ir' env t
213 ir' env (Constant c) = return $ LConst c
214 ir' env (Set _) = return $ LNothing
215 ir' env Erased = return $ LNothing
216 ir' env Impossible = return $ LNothing
217-- ir' env _ = return $ LError "Impossible"
219 irCon env t arity n args
220 | length args == arity = buildApp env (LV (Glob n)) args
221 | otherwise = let extra = satArgs (arity - length args) in
222 do sc' <- irCon env t arity n
223 (args ++ map (\n -> P Bound n undefined) extra)
224 return $ LLam extra sc'
226 satArgs n = map (\i -> MN i "sat") [1..n]
228 buildApp env e [] = return e
229 buildApp env e xs = do xs' <- mapM (ir' env) xs
230 return $ LApp False e xs'
232 doForeign :: [Name] -> [TT Name] -> Idris LExp
233 doForeign env (_ : fgn : args)
234 | (_, (Constant (Str fgnName) : fgnArgTys : ret : [])) <- unApply fgn
235 = let tys = getFTypes fgnArgTys
237 do args' <- mapM (ir' env) args
238 -- wrap it in a prim__IO
239 -- return $ con_ 0 @@ impossible @@
241 LForeign LANG_C rty fgnName (zip tys args')
242 | otherwise = fail "Badly formed foreign function call"
244getFTypes :: TT Name -> [FType]
245getFTypes tm = case unApply tm of
248 let rest = getFTypes xs in
251mkIty' (P _ (UN ty) _) = mkIty ty
255mkIty "FFloat" = FDouble
257mkIty "FString" = FString
261zname = NS (UN "O") ["Nat","Prelude"]
262sname = NS (UN "S") ["Nat","Prelude"]
264instance ToIR ([Name], SC) where
265 ir (args, tree) = do logLvl 3 $ "Compiling " ++ show args ++ "\n" ++ show tree
267 return $ LLam args tree'
269instance ToIR SC where
273 ir' (UnmatchedCase str) = return $ LError str
274 ir' (ProjCase tm alts) = do alts' <- mapM (mkIRAlt tm) alts
276 return $ LCase tm' alts'
277 ir' (Case n alts) = do alts' <- mapM (mkIRAlt (P Bound n Erased)) alts
278 return $ LCase (LV (Glob n)) alts'
279 ir' ImpossibleCase = return LNothing
281 -- special cases for O and S
282 -- Needs rethink: projections make this fail
283-- mkIRAlt n (ConCase z _ [] rhs) | z == zname
284-- = mkIRAlt n (ConstCase (BI 0) rhs)
285-- mkIRAlt n (ConCase s _ [arg] rhs) | s == sname
288-- return $ LDefaultCase
289-- (LLet arg (LOp LBMinus [n', LConst (BI 1)])
291 mkIRAlt _ (ConCase n t args rhs)
293 return $ LConCase (-1) n args rhs'
294 mkIRAlt _ (ConstCase x rhs)
297 return $ LConstCase x rhs'
300 return $ LDefaultCase rhs'
301 mkIRAlt _ (ConstCase c rhs)
302 = fail $ "Can't match on (" ++ show c ++ ")"
303 mkIRAlt _ (DefaultCase rhs)
305 return $ LDefaultCase rhs'
307 matchable (I _) = True
308 matchable (BI _) = True
309 matchable (Ch _) = True
310 matchable (Str _) = True
313 matchableTy IType = True
314 matchableTy BIType = True
315 matchableTy ChType = True
316 matchableTy StrType = True
317 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