1{-# LANGUAGE PatternGuards, TypeSynonymInstances #-}
3module IRTS.Compiler where
6import IRTS.Defunctionalise
8import IRTS.CodegenCommon
10import IRTS.CodegenJava
12import IRTS.CodegenJavaScript
16import Idris.UnusedArgs
22import Control.Monad.State
26import System.Directory
27import System.Environment
28import System.FilePath ((</>), addTrailingPathSeparator)
32compile :: Target -> FilePath -> Term -> Idris ()
35 let tmnames = namesUsed (STerm tm)
36 usedIn <- mapM (allNames []) tmnames
37 let used = [UN "prim__subBigInt", UN "prim__addBigInt"] : usedIn
38 defsIn <- mkDecls tm (concat used)
39 findUnusedArgs (concat used)
41 objs <- getObjectFiles
44 let defs = defsIn ++ [(MN 0 "runMain", maindef)]
45 -- iputStrLn $ showSep "\n" (map show defs)
46 let (nexttag, tagged) = addTags 65536 (liftAll defs)
47 let ctxtIn = addAlist tagged emptyContext
48 iLOG "Defunctionalising"
49 let defuns_in = defunctionalise nexttag ctxtIn
50 logLvl 5 $ show defuns_in
52 let defuns = inline defuns_in
53 logLvl 5 $ show defuns
54 iLOG "Resolving variables for CG"
55 -- iputStrLn $ showSep "\n" (map show (toAlist defuns))
56 let checked = checkDefs defuns (toAlist defuns)
58 dumpCases <- getDumpCases
59 dumpDefun <- getDumpDefun
62 Just f -> liftIO $ writeFile f (showCaseTrees tagged)
65 Just f -> liftIO $ writeFile f (dumpDefuns defuns)
66 iLOG "Building output"
68 OK c -> liftIO $ case target of
70 codegenC c f outty hdrs
71 (concatMap mkObj objs)
72 (concatMap mkLib libs) NONE
74 codegenJava [] c f hdrs libs outty
76 codegenJavaScript JavaScript c f outty
78 codegenJavaScript Node c f outty
80 Bytecode -> dumpBC c f
81 Error e -> fail $ show e
82 where checkMVs = do i <- getIState
83 case idris_metavars i \\ primDefs of
85 ms -> fail $ "There are undefined metavariables: " ++ show ms
86 inDir d h = do let f = d </> h
88 if ex then return f else return h
90 mkLib l = "-l" ++ l ++ " "
94irMain :: TT Name -> Idris LDecl
95irMain tm = do i <- ir tm
96 return $ LFun [] (MN 0 "runMain") [] (LForce i)
98allNames :: [Name] -> Name -> Idris [Name]
99allNames ns n | n `elem` ns = return []
100allNames ns n = do i <- getIState
101 case lookupCtxt n (idris_callgraph i) of
102 [ns'] -> do more <- mapM (allNames (n:ns)) (map fst (calls ns'))
103 return (nub (n : concat more))
106mkDecls :: Term -> [Name] -> Idris [(Name, LDecl)]
109 let ds = filter (\ (n, d) -> n `elem` used || isCon d) $ ctxtAlist (tt_ctxt i)
110 mapM traceUnused used
111 decls <- mapM build ds
114showCaseTrees :: [(Name, LDecl)] -> String
115showCaseTrees ds = showSep "\n\n" (map showCT ds)
117 showCT (n, LFun _ f args lexp)
118 = show n ++ " " ++ showSep " " (map show args) ++ " =\n\t "
120 showCT (n, LConstructor c t a) = "data " ++ show n ++ " " ++ show a
122isCon (TyDecl _ _) = True
126 ir :: a -> Idris LExp
128build :: (Name, Def) -> Idris (Name, LDecl)
131 case lookup n (idris_scprims i) of
133 let args = map (\x -> MN x "op") [0..] in
134 return (n, (LFun [] n (take ar args)
135 (LOp op (map (LV . Glob) (take ar args)))))
136 _ -> do def <- mkLDecl n d
137 logLvl 3 $ "Compiled " ++ show n ++ " =\n\t" ++ show def
140getPrim :: IState -> Name -> [LExp] -> Maybe LExp
141getPrim i n args = case lookup n (idris_scprims i) of
143 if (ar == length args)
144 then return (LOp op args)
148declArgs args inl n (LLam xs x) = declArgs (args ++ xs) inl n x
149declArgs args inl n x = LFun (if inl then [Inline] else []) n args x
151mkLDecl n (Function tm _) = do e <- ir tm
152 return (declArgs [] True n e)
153mkLDecl n (CaseOp _ inl _ _ pats _ _ args sc) = do e <- ir (args, sc)
154 return (declArgs [] inl n e)
155mkLDecl n (TyDecl (DCon t a) _) = return $ LConstructor n t a
156mkLDecl n (TyDecl (TCon t a) _) = return $ LConstructor n (-1) a
157mkLDecl n _ = return (LFun [] n [] (LError ("Impossible declaration " ++ show n)))
159instance ToIR (TT Name) where
160 ir tm = ir' [] tm where
162 | (P _ (UN "mkForeign") _, args) <- unApply tm
164 | (P _ (UN "unsafePerformIO") _, [_, arg]) <- unApply tm
166 -- TMP HACK - until we get inlining.
167 | (P _ (UN "replace") _, [_, _, _, _, _, arg]) <- unApply tm
169 | (P _ (UN "lazy") _, [_, arg]) <- unApply tm
170 = do arg' <- ir' env arg
171 return $ LLazyExp arg'
172 | (P _ (UN "assert_smaller") _, [_, _, _, arg]) <- unApply tm
174 | (P _ (UN "par") _, [_, arg]) <- unApply tm
175 = do arg' <- ir' env arg
176 return $ LOp LPar [LLazyExp arg']
177 | (P _ (UN "fork") _, [arg]) <- unApply tm
178 = do arg' <- ir' env arg
179 return $ LOp LFork [LLazyExp arg']
180 | (P _ (UN "prim__IO") _, [v]) <- unApply tm
183 | (P _ (UN "io_bind") _, [_,_,v,Bind n (Lam _) sc]) <- unApply tm
185 sc' <- ir' (n:env) sc
186 return (LLet n (LForce v') sc')
187 | (P _ (UN "io_bind") _, [_,_,v,k]) <- unApply tm
190 return (LApp False k' [LForce v'])
191 | (P _ (UN "malloc") _, [_,size,t]) <- unApply tm
192 = do size' <- ir' env size
194 return t' -- TODO $ malloc_ size' t'
195 | (P _ (UN "trace_malloc") _, [_,t]) <- unApply tm
198-- | (P _ (NS (UN "S") ["Nat", "Prelude"]) _, [k]) <- unApply tm
199-- = do k' <- ir' env k
200-- return (LOp LBPlus [k', LConst (BI 1)])
201 | (P (DCon t a) n _, args) <- unApply tm
202 = irCon env t a n args
203 | (P _ n _, args) <- unApply tm
205 args' <- mapM (ir' env) args
206 case getPrim i n args' of
211 (idris_optimisation i) of
212 [oi] -> collapsible oi
216 (idris_callgraph i) of
217 [CGInfo _ _ _ _ unusedpos] ->
222 else return (LApp False (LV (Glob n))
223 (mkUnused unused 0 args'))
224 | (f, args) <- unApply tm
226 args' <- mapM (ir' env) args
227 return (LApp False f' args')
228 where mkUnused u i [] = []
229 mkUnused u i (x : xs) | i `elem` u = LNothing : mkUnused u (i + 1) xs
230 | otherwise = x : mkUnused u (i + 1) xs
231-- ir' env (P _ (NS (UN "O") ["Nat", "Prelude"]) _)
232-- = return $ LConst (BI 0)
233 ir' env (P _ n _) = return $ LV (Glob n)
234 ir' env (V i) | i < length env = return $ LV (Glob (env!!i))
235 | otherwise = error $ "IR fail " ++ show i ++ " " ++ show tm
236 ir' env (Bind n (Lam _) sc)
237 = do let n' = uniqueName n env
238 sc' <- ir' (n' : env) sc
239 return $ LLam [n'] sc'
240 ir' env (Bind n (Let _ v) sc)
241 = do sc' <- ir' (n : env) sc
243 return $ LLet n v' sc'
244 ir' env (Bind _ _ _) = return $ LNothing
245 ir' env (Proj t i) = do t' <- ir' env t
247 ir' env (Constant c) = return $ LConst c
248 ir' env (TType _) = return $ LNothing
249 ir' env Erased = return $ LNothing
250 ir' env Impossible = return $ LNothing
251-- ir' env _ = return $ LError "Impossible"
253 irCon env t arity n args
254 | length args == arity = buildApp env (LV (Glob n)) args
255 | otherwise = let extra = satArgs (arity - length args) in
256 do sc' <- irCon env t arity n
257 (args ++ map (\n -> P Bound n undefined) extra)
258 return $ LLam extra sc'
260 satArgs n = map (\i -> MN i "sat") [1..n]
262 buildApp env e [] = return e
263 buildApp env e xs = do xs' <- mapM (ir' env) xs
264 return $ LApp False e xs'
266 doForeign :: [Name] -> [TT Name] -> Idris LExp
267 doForeign env (_ : fgn : args)
268 | (_, (Constant (Str fgnName) : fgnArgTys : ret : [])) <- unApply fgn
269 = let maybeTys = getFTypes fgnArgTys
272 Nothing -> fail $ "Foreign type specification is not a constant list: " ++ show (fgn:args)
274 args' <- mapM (ir' env) args
275 -- wrap it in a prim__IO
276 -- return $ con_ 0 @@ impossible @@
277 return $ -- LLazyExp $
278 LForeign LANG_C rty fgnName (zip tys args')
279 | otherwise = fail "Badly formed foreign function call"
281getFTypes :: TT Name -> Maybe [FType]
282getFTypes tm = case unApply tm of
285 fmap (mkIty' ty :) (getFTypes xs)
288mkIty' (P _ (UN ty) _) = mkIty ty
289mkIty' (App (P _ (UN "FIntT") _) (P _ (UN intTy) _)) = mkIntIty intTy
290mkIty' (App (App (P _ (UN "FFunction") _) _) (App (P _ (UN "FAny") _) (App (P _ (UN "IO") _) _))) = FFunctionIO
291mkIty' (App (App (P _ (UN "FFunction") _) _) _) = FFunction
294mkIty "FFloat" = FDouble
296mkIty "FString" = FString
299mkIty "FFunction" = FFunction
300mkIty "FFunctionIO" = FFunctionIO
302mkIntIty "ITNative" = FInt ITNative
303mkIntIty "IT8" = FInt IT8
304mkIntIty "IT16" = FInt IT16
305mkIntIty "IT32" = FInt IT32
306mkIntIty "IT64" = FInt IT64
308zname = NS (UN "O") ["Nat","Prelude"]
309sname = NS (UN "S") ["Nat","Prelude"]
311instance ToIR ([Name], SC) where
312 ir (args, tree) = do logLvl 3 $ "Compiling " ++ show args ++ "\n" ++ show tree
314 return $ LLam args tree'
316instance ToIR SC where
320 ir' (UnmatchedCase str) = return $ LError str
321 ir' (ProjCase tm alts) = do alts' <- mapM (mkIRAlt tm) alts
323 return $ LCase tm' alts'
324 ir' (Case n alts) = do alts' <- mapM (mkIRAlt (P Bound n Erased)) alts
325 return $ LCase (LV (Glob n)) alts'
326 ir' ImpossibleCase = return LNothing
328 -- special cases for O and S
329 -- Needs rethink: projections make this fail
330-- mkIRAlt n (ConCase z _ [] rhs) | z == zname
331-- = mkIRAlt n (ConstCase (BI 0) rhs)
332-- mkIRAlt n (ConCase s _ [arg] rhs) | s == sname
335-- return $ LDefaultCase
336-- (LLet arg (LOp LBMinus [n', LConst (BI 1)])
338 mkIRAlt _ (ConCase n t args rhs)
340 return $ LConCase (-1) n args rhs'
341 mkIRAlt _ (ConstCase x rhs)
344 return $ LConstCase x rhs'
347 return $ LDefaultCase rhs'
348 mkIRAlt _ (ConstCase c rhs)
349 = fail $ "Can't match on (" ++ show c ++ ")"
350 mkIRAlt _ (DefaultCase rhs)
352 return $ LDefaultCase rhs'
354 matchable (I _) = True
355 matchable (BI _) = True
356 matchable (Ch _) = True
357 matchable (Str _) = True
360 matchableTy IType = True
361 matchableTy BIType = True
362 matchableTy ChType = True
363 matchableTy StrType = True
365 matchableTy B8Type = True
366 matchableTy B16Type = True
367 matchableTy B32Type = True
368 matchableTy B64Type = True
370 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