on 4162113 help sign in
Idris-dev/src/IRTS/Compiler.hs
689 lines 26 kB history raw
1{-|
2Module : IRTS.Compiler
3Description : Coordinates the compilation process.
5License : BSD3
6Maintainer : The Idris Community.
7-}
8{-# LANGUAGE CPP, FlexibleContexts, PatternGuards, TypeSynonymInstances #-}
10module IRTS.Compiler(compile, generate) where
12import Idris.AbsSyntax
13import Idris.ASTUtils
14import Idris.Core.CaseTree
15import Idris.Core.Evaluate
16import Idris.Core.TT
17import Idris.Erasure
18import Idris.Error
19import Idris.Options
20import Idris.Output
21import IRTS.CodegenC
22import IRTS.CodegenCommon
23import IRTS.Defunctionalise
24import IRTS.DumpBC
25import IRTS.Exports
26import IRTS.Inliner
27import IRTS.LangOpts
28import IRTS.Portable
29import IRTS.Simplified
31import Prelude hiding (id, (.))
33import Control.Category
34import Control.Monad.State
35import Data.List
36import qualified Data.Map as M
37import Data.Ord
38import qualified Data.Set as S
39import System.Directory
40import System.Exit
41import System.IO
42import System.Process
44-- | Compile to simplified forms and return CodegenInfo
45compile :: Codegen -> FilePath -> Maybe Term -> Idris CodegenInfo
46compile codegen f mtm = do
47 logCodeGen 1 "Compiling Output."
48 iReport 2 "Compiling Output."
49 checkMVs -- check for undefined metavariables
50 checkTotality -- refuse to compile if there are totality problems
51 exports <- findExports
52 let rootNames = case mtm of
53 Nothing -> []
54 Just t -> freeNames t
56 logCodeGen 1 "Running Erasure Analysis"
57 iReport 3 "Running Erasure Analysis"
58 reachableNames <- performUsageAnalysis
59 (rootNames ++ getExpNames exports)
60 maindef <- case mtm of
61 Nothing -> return []
62 Just tm -> do md <- irMain tm
63 logCodeGen 1 $ "MAIN: " ++ show md
64 return [(sMN 0 "runMain", md)]
65 objs <- getObjectFiles codegen
66 libs <- getLibs codegen
67 flags <- getFlags codegen
68 hdrs <- getHdrs codegen
69 impdirs <- rankedImportDirs f
70 ttDeclarations <- getDeclarations reachableNames
71 defsIn <- mkDecls reachableNames
72 -- if no 'main term' given, generate interface files
73 let iface = case mtm of
74 Nothing -> True
75 Just _ -> False
77 let defs = defsIn ++ maindef
79 -- Inlined top level LDecl made here
80 let defsInlined = inlineAll defs
81 let defsUniq = map (allocUnique (addAlist defsInlined emptyContext))
82 defsInlined
84 logCodeGen 1 "Inlining"
86 dumpCases <- getDumpCases
87 case dumpCases of
88 Nothing -> return ()
89 Just f -> runIO $ writeFile f (showCaseTrees defsUniq)
91 let (nexttag, tagged) = addTags 65536 (liftAll defsInlined)
92 let ctxtIn = addAlist tagged emptyContext
94 logCodeGen 1 "Defunctionalising"
95 iReport 3 "Defunctionalising"
96 let defuns_in = defunctionalise nexttag ctxtIn
97 logCodeGen 5 $ show defuns_in
98 logCodeGen 1 "Inlining"
99 iReport 3 "Inlining"
100 let defuns = inline defuns_in
101 logCodeGen 5 $ show defuns
102 logCodeGen 1 "Resolving variables for CG"
104 let checked = simplifyDefs defuns (toAlist defuns)
105 outty <- outputTy
106 dumpDefun <- getDumpDefun
107 case dumpDefun of
108 Nothing -> return ()
109 Just f -> runIO $ writeFile f (dumpDefuns defuns)
110 triple <- Idris.AbsSyntax.targetTriple
111 cpu <- Idris.AbsSyntax.targetCPU
112 logCodeGen 1 "Generating Code."
113 iReport 2 "Generating Code."
114 case checked of
115 OK c -> do return $ CodegenInfo f outty triple cpu
116 hdrs impdirs objs libs flags
117 NONE c (toAlist defuns)
118 tagged iface exports
119 ttDeclarations
120 Error e -> ierror e
121 where checkMVs = do i <- getIState
122 case map fst (idris_metavars i) \\ primDefs of
123 [] -> return ()
124 ms -> do iputStrLn $ "WARNING: There are incomplete holes:\n " ++ show ms
125 iputStrLn "\nEvaluation of any of these will crash at run time."
126 return ()
127 checkTotality = do i <- getIState
128 case idris_totcheckfail i of
129 [] -> return ()
130 ((fc, msg):fs) -> ierror . At fc . Msg $ "Cannot compile:\n " ++ msg
132generate :: Codegen -> FilePath -> CodegenInfo -> IO ()
133generate codegen mainmod ir
134 = case codegen of
135 -- Built-in code generators (FIXME: lift these out!)
136 Via _ "c" -> codegenC ir
137 -- Any external code generator
138 Via fm cg -> do input <- case fm of
139 IBCFormat -> return mainmod
140 JSONFormat -> do
141 tempdir <- getTemporaryDirectory
142 (fn, h) <- openTempFile tempdir "idris-cg.json"
143 writePortable h ir
144 hClose h
145 return fn
146 let cmd = "idris-codegen-" ++ cg
147 args = [input, "-o", outputFile ir] ++ compilerFlags ir
148 exit <- rawSystem cmd (if interfaces ir then "--interface" : args else args)
149 when (exit /= ExitSuccess) $
150 putStrLn ("FAILURE: " ++ show cmd ++ " " ++ show args)
151 Bytecode -> dumpBC (simpleDecls ir) (outputFile ir)
153irMain :: TT Name -> Idris LDecl
154irMain tm = do
155 i <- irTerm (sMN 0 "runMain") M.empty [] tm
156 return $ LFun [] (sMN 0 "runMain") [] (LForce i)
158mkDecls :: [Name] -> Idris [(Name, LDecl)]
159mkDecls used
160 = do i <- getIState
161 let ds = filter (\(n, d) -> n `elem` used || isCon d) $ ctxtAlist (tt_ctxt i)
162 decls <- mapM build ds
163 return decls
165getDeclarations :: [Name] -> Idris ([(Name, TTDecl)])
166getDeclarations used
167 = do i <- getIState
168 let ds = filter (\(n, (d,_,_,_,_,_)) -> n `elem` used || isCon d) $ ((toAlist . definitions . tt_ctxt) i)
169 return ds
171showCaseTrees :: [(Name, LDecl)] -> String
172showCaseTrees = showSep "\n\n" . map showCT . sortBy (comparing defnRank)
173 where
174 showCT (n, LFun opts f args lexp)
175 = showOpts ++
176 show n ++ " " ++ showSep " " (map show args) ++ " =\n\t"
177 ++ show lexp
178 where
179 showOpts | Inline `elem` opts = "%inline "
180 | otherwise = ""
181 showCT (n, LConstructor c t a) = "data " ++ show n ++ " " ++ show a
183 defnRank :: (Name, LDecl) -> String
184 defnRank (n, LFun _ _ _ _) = "1" ++ nameRank n
185 defnRank (n, LConstructor _ _ _) = "2" ++ nameRank n
187 nameRank :: Name -> String
188 nameRank (UN s) = "1" ++ show s
189 nameRank (MN i s) = "2" ++ show s ++ show i
190 nameRank (NS n ns) = "3" ++ concatMap show (reverse ns) ++ nameRank n
191 nameRank (SN sn) = "4" ++ snRank sn
192 nameRank n = "5" ++ show n
194 snRank :: SpecialName -> String
195 snRank (WhereN i n n') = "1" ++ nameRank n' ++ nameRank n ++ show i
196 snRank (ImplementationN n args) = "2" ++ nameRank n ++ concatMap show args
197 snRank (ParentN n s) = "3" ++ nameRank n ++ show s
198 snRank (MethodN n) = "4" ++ nameRank n
199 snRank (CaseN _ n) = "5" ++ nameRank n
200 snRank (ElimN n) = "6" ++ nameRank n
201 snRank (ImplementationCtorN n) = "7" ++ nameRank n
202 snRank (WithN i n) = "8" ++ nameRank n ++ show i
204isCon (TyDecl _ _) = True
205isCon _ = False
207build :: (Name, Def) -> Idris (Name, LDecl)
208build (n, d)
209 = do i <- getIState
210 case getPrim n i of
211 Just (ar, op) ->
212 let args = map (\x -> sMN x "op") [0..] in
213 return (n, (LFun [] n (take ar args)
214 (LOp op (map LV (take ar args)))))
215 _ -> do def <- mkLDecl n d
216 logCodeGen 3 $ "Compiled " ++ show n ++ " =\n\t" ++ show def
217 return (n, def)
218 where getPrim n i
219 | Just (ar, op) <- lookup n (idris_scprims i)
220 = Just (ar, op)
221 | Just ar <- lookup n (S.toList (idris_externs i))
222 = Just (ar, LExternal n)
223 getPrim n i = Nothing
225declArgs args inl n (LLam xs x) = declArgs (args ++ xs) inl n x
226declArgs args inl n x = LFun (if inl then [Inline] else []) n args x
228mkLDecl n (Function tm _)
229 = declArgs [] True n <$> irTerm n M.empty [] tm
231mkLDecl n (CaseOp ci _ _ _ pats cd)
232 = declArgs [] (case_inlinable ci || caseName n) n <$> irTree n args sc
233 where
234 (args, sc) = cases_runtime cd
236 -- Always attempt to inline functions arising from 'case' expressions
237 caseName (SN (CaseN _ _)) = True
238 caseName (SN (WithN _ _)) = True
239 caseName (NS n _) = caseName n
240 caseName _ = False
242mkLDecl n (TyDecl (DCon tag arity _) _) =
243 LConstructor n tag . length <$> fgetState (cg_usedpos . ist_callgraph n)
245mkLDecl n (TyDecl (TCon t a) _) = return $ LConstructor n (-1) a
246mkLDecl n _ = return $ (declArgs [] True n LNothing) -- postulate, never run
248data VarInfo = VI
249 { viMethod :: Maybe Name
250 }
251 deriving Show
253type Vars = M.Map Name VarInfo
255irTerm :: Name -> Vars -> [Name] -> Term -> Idris LExp
256irTerm top vs env tm@(App _ f a) = do
257 ist <- getIState
258 case unApply tm of
259 (P _ n _, args)
260 | n `elem` map fst (idris_metavars ist) \\ primDefs
261 -> return $ LError $ "ABORT: Attempt to evaluate hole " ++ show n
262 (P _ (UN m) _, args)
263 | m == txt "mkForeignPrim"
264 -> doForeign vs env (reverse (drop 4 args)) -- drop implicits
266 (P _ (UN u) _, [_, arg])
267 | u == txt "unsafePerformPrimIO"
268 -> irTerm top vs env arg
270 (P _ (UN u) _, _)
271 | u == txt "assert_unreachable"
272 -> return $ LError $ "ABORT: Reached an unreachable case in " ++ show top
274 (P _ (UN u) _, [_, msg])
275 | u == txt "idris_crash"
276 -> do msg' <- irTerm top vs env msg
277 return $ LOp LCrash [msg']
279 -- TMP HACK - until we get inlining.
280 (P _ (UN r) _, [_, _, _, _, _, arg])
281 | r == txt "replace"
282 -> irTerm top vs env arg
284 -- 'void' doesn't have any pattern clauses and only gets called on
285 -- erased things in higher order contexts (also a TMP HACK...)
286 (P _ (UN r) _, _)
287 | r == txt "void"
288 -> return LNothing
290 -- Laziness, the old way
291 (P _ (UN l) _, [_, arg])
292 | l == txt "lazy"
293 -> error "lazy has crept in somehow"
295 (P _ (UN l) _, [_, arg])
296 | l == txt "force"
297 -> LForce <$> irTerm top vs env arg
299 -- Laziness, the new way
300 (P _ (UN l) _, [_, _, arg])
301 | l == txt "Delay"
302 -> LLazyExp <$> irTerm top vs env arg
304 (P _ (UN l) _, [_, _, arg])
305 | l == txt "Force"
306 -> LForce <$> irTerm top vs env arg
308 (P _ (UN a) _, [_, _, _, arg])
309 | a == txt "assert_smaller"
310 -> irTerm top vs env arg
312 (P _ (UN a) _, [_, arg])
313 | a == txt "assert_total"
314 -> irTerm top vs env arg
316 (P _ (UN p) _, [_, arg])
317 | p == txt "par"
318 -> do arg' <- irTerm top vs env arg
319 return $ LOp LPar [LLazyExp arg']
321 (P _ (UN pf) _, [arg])
322 | pf == txt "prim_fork"
323 -> do arg' <- irTerm top vs env arg
324 return $ LOp LFork [LLazyExp arg']
326 (P _ (UN m) _, [_,size,t])
327 | m == txt "malloc"
328 -> irTerm top vs env t
330 (P _ (UN tm) _, [_,t])
331 | tm == txt "trace_malloc"
332 -> irTerm top vs env t -- TODO
334 -- This case is here until we get more general inlining. It's just
335 -- a really common case, and the laziness hurts...
336 (P _ (NS (UN be) [b,p]) _, [_,x,(App _ (App _ (App _ (P _ (UN d) _) _) _) t),
337 (App _ (App _ (App _ (P _ (UN d') _) _) _) e)])
338 | be == txt "ifThenElse"
339 , d == txt "Delay"
340 , d' == txt "Delay"
341 , b == txt "Bool"
342 , p == txt "Prelude"
343 -> do
344 x' <- irTerm top vs env x
345 t' <- irTerm top vs env t
346 e' <- irTerm top vs env e
347 return (LCase Shared x'
348 [LConCase 0 (sNS (sUN "False") ["Bool","Prelude"]) [] e'
349 ,LConCase 1 (sNS (sUN "True" ) ["Bool","Prelude"]) [] t'
350 ])
352 -- data constructor
353 (P (DCon t arity _) n _, args) -> do
354 detag <- fgetState (opt_detaggable . ist_optimisation n)
355 used <- map fst <$> fgetState (cg_usedpos . ist_callgraph n)
357 let isNewtype = length used == 1 && detag
358 let argsPruned = [a | (i,a) <- zip [0..] args, i `elem` used]
360 -- The following code removes fields from data constructors
361 -- and performs the newtype optimisation.
362 --
363 -- The general rule here is:
364 -- Everything we get as input is not touched by erasure,
365 -- so it conforms to the official arities and types
366 -- and we can reason about it like it's plain TT.
367 --
368 -- It's only the data that leaves this point that's erased
369 -- and possibly no longer typed as the original TT version.
370 --
371 -- Especially, underapplied constructors must yield functions
372 -- even if all the remaining arguments are erased
373 -- (the resulting function *will* be applied, to NULLs).
374 --
375 -- This will probably need rethinking when we get erasure from functions.
377 -- "padLams" will wrap our term in LLam-bdas and give us
378 -- the "list of future unerased args" coming from these lambdas.
379 --
380 -- We can do whatever we like with the list of unerased args,
381 -- hence it takes a lambda: \unerased_argname_list -> resulting_LExp.
382 let padLams = padLambdas used (length args) arity
384 case compare (length args) arity of
386 -- overapplied
387 GT -> ifail ("overapplied data constructor: " ++ show tm ++
388 "\nDEBUG INFO:\n" ++
389 "Arity: " ++ show arity ++ "\n" ++
390 "Arguments: " ++ show args ++ "\n" ++
391 "Pruned arguments: " ++ show argsPruned)
393 -- exactly saturated
394 EQ | isNewtype
395 -> irTerm top vs env (head argsPruned)
397 | otherwise -- not newtype, plain data ctor
398 -> buildApp (LV n) argsPruned
400 -- not saturated, underapplied
401 LT | isNewtype -- newtype
402 , length argsPruned == 1 -- and we already have the value
403 -> padLams . (\tm [] -> tm) -- the [] asserts there are no unerased args
404 <$> irTerm top vs env (head argsPruned)
406 | isNewtype -- newtype but the value is not among args yet
407 -> return . padLams $ \[vn] -> LApp False (LV n) [LV vn]
409 -- not a newtype, just apply to a constructor
410 | otherwise
411 -> padLams . applyToNames <$> buildApp (LV n) argsPruned
413 -- type constructor
414 (P (TCon t a) n _, args) -> return LNothing
416 -- an external name applied to arguments
417 (P _ n _, args) | S.member (n, length args) (idris_externs ist) -> do
418 LOp (LExternal n) <$> mapM (irTerm top vs env) args
420 -- a name applied to arguments
421 (P _ n _, args) -> do
422 case lookup n (idris_scprims ist) of
423 -- if it's a primitive that is already saturated,
424 -- compile to the corresponding op here already to save work
425 Just (arity, op) | length args == arity
426 -> LOp op <$> mapM (irTerm top vs env) args
428 -- otherwise, just apply the name
429 _ -> applyName n ist args
431 -- turn de bruijn vars into regular named references and try again
432 (V i, args) -> irTerm top vs env $ mkApp (P Bound (env !! i) Erased) args
434 (f, args)
435 -> LApp False
436 <$> irTerm top vs env f
437 <*> mapM (irTerm top vs env) args
439 where
440 buildApp :: LExp -> [Term] -> Idris LExp
441 buildApp e [] = return e
442 buildApp e xs = LApp False e <$> mapM (irTerm top vs env) xs
444 applyToNames :: LExp -> [Name] -> LExp
445 applyToNames tm [] = tm
446 applyToNames tm ns = LApp False tm $ map LV ns
448 padLambdas :: [Int] -> Int -> Int -> ([Name] -> LExp) -> LExp
449 padLambdas used startIdx endSIdx mkTerm
450 = LLam allNames $ mkTerm nonerasedNames
451 where
452 allNames = [sMN i "sat" | i <- [startIdx .. endSIdx-1]]
453 nonerasedNames = [sMN i "sat" | i <- [startIdx .. endSIdx-1], i `elem` used]
455 applyName :: Name -> IState -> [Term] -> Idris LExp
456 applyName n ist args =
457 LApp False (LV n) <$> mapM (irTerm top vs env . erase) (zip [0..] args)
458 where
459 erase (i, x)
460 | i >= arity || i `elem` used = x
461 | otherwise = Erased
463 arity = case fst4 <$> lookupCtxtExact n (definitions . tt_ctxt $ ist) of
464 Just (CaseOp ci ty tys def tot cdefs) -> length tys
465 Just (TyDecl (DCon tag ar _) _) -> ar
466 Just (TyDecl Ref ty) -> length $ getArgTys ty
467 Just (Operator ty ar op) -> ar
468 Just def -> error $ "unknown arity: " ++ show (n, def)
469 Nothing -> 0 -- no definition, probably local name => can't erase anything
471 -- name for purposes of usage info lookup
472 uName
473 | Just n' <- viMethod =<< M.lookup n vs = n'
474 | otherwise = n
476 used = maybe [] (map fst . usedpos) $ lookupCtxtExact uName (idris_callgraph ist)
477 fst4 (x,_,_,_,_,_) = x
479irTerm top vs env (P _ n _) = return $ LV n
480irTerm top vs env (V i)
481 | i >= 0 && i < length env = return $ LV (env!!i)
482 | otherwise = ifail $ "bad de bruijn index: " ++ show i
484irTerm top vs env (Bind n (Lam _ _) sc) = LLam [n'] <$> irTerm top vs (n':env) sc
485 where
486 n' = uniqueName n env
488irTerm top vs env (Bind n (Let _ _ v) sc)
489 = LLet n <$> irTerm top vs env v <*> irTerm top vs (n : env) sc
491irTerm top vs env (Bind _ _ _) = return $ LNothing
493irTerm top vs env (Proj t (-1)) = do
494 t' <- irTerm top vs env t
495 return $ LOp (LMinus (ATInt ITBig))
496 [t', LConst (BI 1)]
498irTerm top vs env (Proj t i) = LProj <$> irTerm top vs env t <*> pure i
499irTerm top vs env (Constant TheWorld) = return LNothing
500irTerm top vs env (Constant c) = return (LConst c)
501irTerm top vs env (TType _) = return LNothing
502irTerm top vs env Erased = return LNothing
503irTerm top vs env Impossible = return LNothing
505doForeign :: Vars -> [Name] -> [Term] -> Idris LExp
506doForeign vs env (ret : fname : world : args)
507 = do args' <- mapM splitArg args
508 let fname' = toFDesc fname
509 let ret' = toFDesc ret
510 return $ LForeign ret' fname' args'
511 where
512 splitArg tm | (_, [_,_,l,r]) <- unApply tm -- pair, two implicits
513 = do let l' = toFDesc l
514 r' <- irTerm (sMN 0 "__foreignCall") vs env r
515 return (l', r')
516 splitArg _ = ifail $ "Badly formed foreign function call: " ++
517 show (ret : fname : world : args)
520 toFDesc (Constant (Str str)) = FStr str
521 toFDesc tm
522 | (P _ n _, []) <- unApply tm = FCon (deNS n)
523 | (P _ n _, as) <- unApply tm = FApp (deNS n) (map toFDesc as)
524 toFDesc _ = FUnknown
526 deNS (NS n _) = n
527 deNS n = n
528doForeign vs env xs = ifail "Badly formed foreign function call"
530irTree :: Name -> [Name] -> SC -> Idris LExp
531irTree top args tree = do
532 logCodeGen 3 $ "Compiling " ++ show args ++ "\n" ++ show tree
533 LLam args <$> irSC top M.empty tree
535irSC :: Name -> Vars -> SC -> Idris LExp
536irSC top vs (STerm t) = irTerm top vs [] t
537irSC top vs (UnmatchedCase str) = return $ LLazyExp $ LError str
539irSC top vs (ProjCase tm alts) = do
540 tm' <- irTerm top vs [] tm
541 alts' <- mapM (irAlt top vs tm') alts
542 return $ LCase Shared tm' alts'
544-- Transform matching on Delay to applications of Force.
545irSC top vs (Case up n [ConCase (UN delay) i [_, _, n'] sc])
546 | delay == txt "Delay"
547 = do sc' <- irSC top vs sc -- mkForce n' n sc
548 return $ lsubst n' (LForce (LV n)) sc'
550-- There are two transformations in this case:
551--
552-- 1. Newtype-case elimination:
553-- case {e0} of
554-- wrap({e1}) -> P({e1}) ==> P({e0})
555--
556-- This is important because newtyped constructors are compiled away entirely
557-- and we need to do that everywhere.
558--
559-- 2. Unused-case elimination (only valid for singleton branches):
560-- case {e0} of ==> P
561-- C(x,y) -> P[... x,y not used ...]
562--
563-- This is important for runtime because sometimes we case on irrelevant data:
564--
565-- In the example above, {e0} will most probably have been erased
566-- so this vain projection would make the resulting program segfault
567-- because the code generator still emits a PROJECT(...) G-machine instruction.
568--
569-- Hence, we check whether the variables are used at all
570-- and erase the casesplit if they are not.
571--
572irSC top vs (Case up n [alt]) = do
573 replacement <- case alt of
574 ConCase cn a ns sc -> do
575 detag <- fgetState (opt_detaggable . ist_optimisation cn)
576 used <- map fst <$> fgetState (cg_usedpos . ist_callgraph cn)
577 if detag && length used == 1
578 then return . Just $ substSC (ns !! head used) n sc
579 else return Nothing
580 _ -> return Nothing
582 case replacement of
583 Just sc -> irSC top vs sc
584 _ -> do
585 alt' <- irAlt top vs (LV n) alt
586 return $ case namesBoundIn alt' `usedIn` subexpr alt' of
587 [] -> subexpr alt' -- strip the unused top-most case
588 _ -> LCase up (LV n) [alt']
589 where
590 namesBoundIn :: LAlt -> [Name]
591 namesBoundIn (LConCase cn i ns sc) = ns
592 namesBoundIn (LConstCase c sc) = []
593 namesBoundIn (LDefaultCase sc) = []
595 subexpr :: LAlt -> LExp
596 subexpr (LConCase _ _ _ e) = e
597 subexpr (LConstCase _ e) = e
598 subexpr (LDefaultCase e) = e
600-- FIXME: When we have a non-singleton case-tree of the form
601--
602-- case {e0} of
603-- C(x) => ...
604-- ... => ...
605--
606-- and C is detaggable (the only constructor of the family), we can be sure
607-- that the first branch will be always taken -- so we add special handling
608-- to remove the dead default branch.
609--
610-- If we don't do so and C is newtype-optimisable, we will miss this newtype
611-- transformation and the resulting code will probably segfault.
612--
613-- This work-around is not entirely optimal; the best approach would be
614-- to ensure that such case trees don't arise in the first place.
615--
616irSC top vs (Case up n alts@[ConCase cn a ns sc, DefaultCase sc']) = do
617 detag <- fgetState (opt_detaggable . ist_optimisation cn)
618 if detag
619 then irSC top vs (Case up n [ConCase cn a ns sc])
620 else LCase up (LV n) <$> mapM (irAlt top vs (LV n)) alts
622irSC top vs sc@(Case up n alts) = do
623 -- check that neither alternative needs the newtype optimisation,
624 -- see comment above
625 goneWrong <- or <$> mapM isDetaggable alts
626 when goneWrong
627 $ ifail ("irSC: non-trivial case-match on detaggable data: " ++ show sc)
629 -- everything okay
630 LCase up (LV n) <$> mapM (irAlt top vs (LV n)) alts
631 where
632 isDetaggable (ConCase cn _ _ _) = fgetState $ opt_detaggable . ist_optimisation cn
633 isDetaggable _ = return False
635irSC top vs ImpossibleCase = return LNothing
637irAlt :: Name -> Vars -> LExp -> CaseAlt -> Idris LAlt
639-- this leaves out all unused arguments of the constructor
640irAlt top vs _ (ConCase n t args sc) = do
641 used <- map fst <$> fgetState (cg_usedpos . ist_callgraph n)
642 let usedArgs = [a | (i,a) <- zip [0..] args, i `elem` used]
643 LConCase (-1) n usedArgs <$> irSC top (methodVars `M.union` vs) sc
644 where
645 methodVars = case n of
646 SN (ImplementationCtorN interfaceName)
647 -> M.fromList [(v, VI
648 { viMethod = Just $ mkFieldName n i
649 }) | (v,i) <- zip args [0..]]
650 _
651 -> M.empty -- not an implementation constructor
653irAlt top vs _ (ConstCase x rhs)
654 | matchable x = LConstCase x <$> irSC top vs rhs
655 | matchableTy x = LDefaultCase <$> irSC top vs rhs
656 where
657 matchable (I _) = True
658 matchable (BI _) = True
659 matchable (Ch _) = True
660 matchable (Str _) = True
661 matchable (B8 _) = True
662 matchable (B16 _) = True
663 matchable (B32 _) = True
664 matchable (B64 _) = True
665 matchable _ = False
667 matchableTy (AType (ATInt ITNative)) = True
668 matchableTy (AType (ATInt ITBig)) = True
669 matchableTy (AType (ATInt ITChar)) = True
670 matchableTy StrType = True
672 matchableTy (AType (ATInt (ITFixed IT8))) = True
673 matchableTy (AType (ATInt (ITFixed IT16))) = True
674 matchableTy (AType (ATInt (ITFixed IT32))) = True
675 matchableTy (AType (ATInt (ITFixed IT64))) = True
677 matchableTy _ = False
679irAlt top vs tm (SucCase n rhs) = do
680 rhs' <- irSC top vs rhs
681 return $ LDefaultCase (LLet n (LOp (LMinus (ATInt ITBig))
682 [tm,
683 LConst (BI 1)]) rhs')
685irAlt top vs _ (ConstCase c rhs)
686 = ifail $ "Can't match on (" ++ show c ++ ")"
688irAlt top vs _ (DefaultCase rhs)
689 = LDefaultCase <$> irSC top vs rhs