1{-# LANGUAGE PatternGuards #-}
3module IRTS.Compiler where
6import IRTS.Defunctionalise
13import Idris.UnusedArgs
19import Control.Monad.State
23import System.Directory
24import System.Environment
28compile :: Target -> FilePath -> Term -> Idris ()
31 let tmnames = namesUsed (STerm tm)
32 used <- mapM (allNames []) tmnames
33 defsIn <- mkDecls tm (concat used)
34 findUnusedArgs (concat used)
36 objs <- getObjectFiles
39 let defs = defsIn ++ [(MN 0 "runMain", maindef)]
40 -- iputStrLn $ showSep "\n" (map show defs)
41 let (nexttag, tagged) = addTags 65536 (liftAll defs)
42 let ctxtIn = addAlist tagged emptyContext
43 iLOG "Defunctionalising"
44 let defuns_in = defunctionalise nexttag ctxtIn
45 logLvl 5 $ show defuns_in
47 let defuns = inline defuns_in
48 logLvl 5 $ show defuns
49 iLOG "Resolving variables for CG"
50 -- iputStrLn $ showSep "\n" (map show (toAlist defuns))
51 let checked = checkDefs defuns (toAlist defuns)
53 dumpCases <- getDumpCases
56 Just f -> do cs <- getCaseTrees (concat used)
57 liftIO $ writeFile f (showCaseTrees cs)
58 iLOG "Building output"
60 OK c -> case target of
61 ViaC -> liftIO $ codegenC dumpC c f True hdrs
62 (concatMap mkObj objs)
63 (concatMap mkLib libs) NONE
64 ViaJava -> liftIO $ codegenJava c f
65 Error e -> fail $ show e
66 where checkMVs = do i <- get
67 case idris_metavars i \\ primDefs of
69 ms -> fail $ "There are undefined metavariables: " ++ show ms
70 inDir d h = do let f = d ++ "/" ++ h
72 if ex then return f else return h
74 mkLib l = "-l" ++ l ++ " "
78irMain :: TT Name -> Idris LDecl
79irMain tm = do i <- ir tm
80 return $ LFun (MN 0 "runMain") [] (LForce i)
82allNames :: [Name] -> Name -> Idris [Name]
83allNames ns n | n `elem` ns = return []
84allNames ns n = do i <- get
85 case lookupCtxt Nothing n (idris_callgraph i) of
86 [ns'] -> do more <- mapM (allNames (n:ns)) (map fst (calls ns'))
87 return (nub (n : concat more))
90mkDecls :: Term -> [Name] -> Idris [(Name, LDecl)]
93 let ds = filter (\ (n, d) -> n `elem` used || isCon d) $ ctxtAlist (tt_ctxt i)
94 decls <- mapM build ds
97getCaseTrees :: [Name] -> Idris [(Name, Def)]
100 let ds = filter (\ (n, d) -> n `elem` used || isCon d) $ ctxtAlist (tt_ctxt i)
103showCaseTrees :: [(Name, Def)] -> String
104showCaseTrees ds = showSep "\n\n" (map showCT ds)
106 showCT (n, CaseOp _ _ _ _ _ args sc)
107 = show n ++ " " ++ showSep " " (map show args) ++ " = " ++ show sc
110isCon (TyDecl _ _) = True
114 ir :: a -> Idris LExp
116build :: (Name, Def) -> Idris (Name, LDecl)
119 case lookup n (idris_scprims i) of
121 let args = map (\x -> MN x "op") [0..] in
122 return (n, (LFun n (take ar args)
123 (LOp op (map (LV . Glob) (take ar args)))))
124 _ -> do def <- mkLDecl n d
125 logLvl 3 $ "Compiled " ++ show n ++ " =\n\t" ++ show def
128declArgs args n (LLam xs x) = declArgs (args ++ xs) n x
129declArgs args n x = LFun n args x
131mkLDecl n (Function tm _) = do e <- ir tm
132 return (declArgs [] n e)
133mkLDecl n (CaseOp _ _ pats _ _ args sc) = do e <- ir (args, sc)
134 return (declArgs [] n e)
135mkLDecl n (TyDecl (DCon t a) _) = return $ LConstructor n t a
136mkLDecl n (TyDecl (TCon t a) _) = return $ LConstructor n (-1) a
137mkLDecl n _ = return (LFun n [] (LError ("Impossible declaration " ++ show n)))
139instance ToIR (TT Name) where
140 ir tm = ir' [] tm where
142 | (P _ (UN "mkForeign") _, args) <- unApply tm
144 | (P _ (UN "unsafePerformIO") _, [_, arg]) <- unApply tm
146 | (P _ (UN "lazy") _, [_, arg]) <- unApply tm
147 = do arg' <- ir' env arg
148 return $ LLazyExp arg'
149 | (P _ (UN "fork") _, [arg]) <- unApply tm
150 = do arg' <- ir' env arg
151 return $ LOp LFork [LLazyExp arg']
152 | (P _ (UN "prim__IO") _, [v]) <- unApply tm
155 | (P _ (UN "io_bind") _, [_,_,v,k]) <- unApply tm
158 return (LApp False k' [LForce v'])
159 | (P _ (UN "malloc") _, [_,size,t]) <- unApply tm
160 = do size' <- ir' env size
162 return t' -- TODO $ malloc_ size' t'
163 | (P _ (UN "trace_malloc") _, [_,t]) <- unApply tm
166 | (P (DCon t a) n _, args) <- unApply tm
167 = irCon env t a n args
168 | (f, args) <- unApply tm
170 args' <- mapM (ir' env) args
171 return (LApp False f' args')
172 ir' env (P _ n _) = return $ LV (Glob n)
173 ir' env (V i) | i < length env = return $ LV (Glob (env!!i))
174 | otherwise = error $ "IR fail " ++ show i ++ " " ++ show tm
175 ir' env (Bind n (Lam _) sc)
176 = do let n' = uniqueName n env
177 sc' <- ir' (n' : env) sc
178 return $ LLam [n'] sc'
179 ir' env (Bind n (Let _ v) sc)
180 = do sc' <- ir' (n : env) sc
182 return $ LLet n v' sc'
183 ir' env (Bind _ _ _) = return $ LConst (I 424242)
184 ir' env (Proj t i) = do t' <- ir' env t
186 ir' env (Constant c) = return $ LConst c
187 ir' env _ = return $ LError "Impossible"
189 irCon env t arity n args
190 | length args == arity = buildApp env (LV (Glob n)) args
191 | otherwise = let extra = satArgs (arity - length args) in
192 do sc' <- irCon env t arity n
193 (args ++ map (\n -> P Bound n undefined) extra)
194 return $ LLam extra sc'
196 satArgs n = map (\i -> MN i "sat") [1..n]
198 buildApp env e [] = return e
199 buildApp env e xs = do xs' <- mapM (ir' env) xs
200 return $ LApp False e xs'
202 doForeign :: [Name] -> [TT Name] -> Idris LExp
203 doForeign env (_ : fgn : args)
204 | (_, (Constant (Str fgnName) : fgnArgTys : ret : [])) <- unApply fgn
205 = let tys = getFTypes fgnArgTys
207 do args' <- mapM (ir' env) args
208 -- wrap it in a prim__IO
209 -- return $ con_ 0 @@ impossible @@
210 return $ LLazyExp $ LForeign LANG_C rty fgnName (zip tys args')
211 | otherwise = fail "Badly formed foreign function call"
213getFTypes :: TT Name -> [FType]
214getFTypes tm = case unApply tm of
217 let rest = getFTypes xs in
220mkIty' (P _ (UN ty) _) = mkIty ty
224mkIty "FFloat" = FDouble
226mkIty "FString" = FString
230instance ToIR ([Name], SC) where
231 ir (args, tree) = do logLvl 3 $ "Compiling " ++ show args ++ "\n" ++ show tree
233 return $ LLam args tree'
235instance ToIR SC where
239 ir' (UnmatchedCase str) = return $ LError str
240 ir' (ProjCase tm alts) = do alts' <- mapM mkIRAlt alts
242 return $ LCase tm' alts'
243 ir' (Case n alts) = do alts' <- mapM mkIRAlt alts
244 return $ LCase (LV (Glob n)) alts'
246 mkIRAlt (ConCase n t args rhs)
248 return $ LConCase (-1) n args rhs'
249 mkIRAlt (ConstCase (I i) rhs)
251 return $ LConstCase (I i) rhs'
252 mkIRAlt (ConstCase IType rhs)
254 return $ LDefaultCase rhs'
255 mkIRAlt (ConstCase c rhs)
256 = fail $ "Can only pattern match on integer constants (" ++ show c ++ ")"
257 mkIRAlt (DefaultCase rhs)
259 return $ LDefaultCase rhs'
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