@@ -154,7 +154,7 @@ generate codegen mainmod ir
154154
155155irMain :: TT Name -> Idris LDecl
156156irMain tm = do
157− i <- irTerm M.empty [] tm
157+ i <- irTerm (sMN 0 "runMain") M.empty [] tm
158158 return $ LFun [] (sMN 0 "runMain") [] (LForce i)
159159
160160mkDecls :: [Name] -> Idris [(Name, LDecl)]
@@ -218,10 +218,10 @@ declArgs args inl n (LLam xs x) = declArgs (args ++ xs) inl n x
218218declArgs args inl n x = LFun (if inl then [Inline] else []) n args x
219219
220220mkLDecl n (Function tm _)
221− = declArgs [] True n <$> irTerm M.empty [] tm
221+ = declArgs [] True n <$> irTerm n M.empty [] tm
222222
223223mkLDecl n (CaseOp ci _ _ _ pats cd)
224− = declArgs [] (case_inlinable ci || caseName n) n <$> irTree args sc
224+ = declArgs [] (case_inlinable ci || caseName n) n <$> irTree n args sc
225225 where
226226 (args, sc) = cases_runtime cd
227227
@@ -244,8 +244,8 @@ data VarInfo = VI
244244
245245type Vars = M.Map Name VarInfo
246246
247−irTerm :: Vars -> [Name] -> Term -> Idris LExp
248−irTerm vs env tm@(App _ f a) = do
247+irTerm :: Name -> Vars -> [Name] -> Term -> Idris LExp
248+irTerm top vs env tm@(App _ f a) = do
249249 ist <- getIState
250250 case unApply tm of
251251 (P _ n _, args)
@@ -257,12 +257,16 @@ irTerm vs env tm@(App _ f a) = do
257257
258258 (P _ (UN u) _, [_, arg])
259259 | u == txt "unsafePerformPrimIO"
260− -> irTerm vs env arg
260+ -> irTerm top vs env arg
261+
262+ (P _ (UN u) _, _)
263+ | u == txt "assert_unreachable"
264+ -> return $ LError $ "ABORT: Reached an unreachable case in " ++ show top
261265
262266 -- TMP HACK - until we get inlining.
263267 (P _ (UN r) _, [_, _, _, _, _, arg])
264268 | r == txt "replace"
265− -> irTerm vs env arg
269+ -> irTerm top vs env arg
266270
267271 -- 'void' doesn't have any pattern clauses and only gets called on
268272 -- erased things in higher order contexts (also a TMP HACK...)
@@ -277,42 +281,42 @@ irTerm vs env tm@(App _ f a) = do
277281
278282 (P _ (UN l) _, [_, arg])
279283 | l == txt "force"
280− -> LForce <$> irTerm vs env arg
284+ -> LForce <$> irTerm top vs env arg
281285
282286 -- Laziness, the new way
283287 (P _ (UN l) _, [_, _, arg])
284288 | l == txt "Delay"
285− -> LLazyExp <$> irTerm vs env arg
289+ -> LLazyExp <$> irTerm top vs env arg
286290
287291 (P _ (UN l) _, [_, _, arg])
288292 | l == txt "Force"
289− -> LForce <$> irTerm vs env arg
293+ -> LForce <$> irTerm top vs env arg
290294
291295 (P _ (UN a) _, [_, _, _, arg])
292296 | a == txt "assert_smaller"
293− -> irTerm vs env arg
297+ -> irTerm top vs env arg
294298
295299 (P _ (UN a) _, [_, arg])
296300 | a == txt "assert_total"
297− -> irTerm vs env arg
301+ -> irTerm top vs env arg
298302
299303 (P _ (UN p) _, [_, arg])
300304 | p == txt "par"
301− -> do arg' <- irTerm vs env arg
305+ -> do arg' <- irTerm top vs env arg
302306 return $ LOp LPar [LLazyExp arg']
303307
304308 (P _ (UN pf) _, [arg])
305309 | pf == txt "prim_fork"
306− -> do arg' <- irTerm vs env arg
310+ -> do arg' <- irTerm top vs env arg
307311 return $ LOp LFork [LLazyExp arg']
308312
309313 (P _ (UN m) _, [_,size,t])
310314 | m == txt "malloc"
311− -> irTerm vs env t
315+ -> irTerm top vs env t
312316
313317 (P _ (UN tm) _, [_,t])
314318 | tm == txt "trace_malloc"
315− -> irTerm vs env t -- TODO
319+ -> irTerm top vs env t -- TODO
316320
317321 -- This case is here until we get more general inlining. It's just
318322 -- a really common case, and the laziness hurts...
@@ -324,9 +328,9 @@ irTerm vs env tm@(App _ f a) = do
324328 , b == txt "Bool"
325329 , p == txt "Prelude"
326330 -> do
327− x' <- irTerm vs env x
328− t' <- irTerm vs env t
329− e' <- irTerm vs env e
331+ x' <- irTerm top vs env x
332+ t' <- irTerm top vs env t
333+ e' <- irTerm top vs env e
330334 return (LCase Shared x'
331335 [LConCase 0 (sNS (sUN "False") ["Bool","Prelude"]) [] e'
332336 ,LConCase 1 (sNS (sUN "True" ) ["Bool","Prelude"]) [] t'
@@ -375,7 +379,7 @@ irTerm vs env tm@(App _ f a) = do
375379
376380 -- exactly saturated
377381 EQ | isNewtype
378− -> irTerm vs env (head argsPruned)
382+ -> irTerm top vs env (head argsPruned)
379383
380384 | otherwise -- not newtype, plain data ctor
381385 -> buildApp (LV $ Glob n) argsPruned
@@ -384,7 +388,7 @@ irTerm vs env tm@(App _ f a) = do
384388 LT | isNewtype -- newtype
385389 , length argsPruned == 1 -- and we already have the value
386390 -> padLams . (\tm [] -> tm) -- the [] asserts there are no unerased args
387− <$> irTerm vs env (head argsPruned)
391+ <$> irTerm top vs env (head argsPruned)
388392
389393 | isNewtype -- newtype but the value is not among args yet
390394 -> return . padLams $ \[vn] -> LApp False (LV $ Glob n) [LV $ Glob vn]
@@ -398,7 +402,7 @@ irTerm vs env tm@(App _ f a) = do
398402
399403 -- an external name applied to arguments
400404 (P _ n _, args) | S.member (n, length args) (idris_externs ist) -> do
401− LOp (LExternal n) <$> mapM (irTerm vs env) args
405+ LOp (LExternal n) <$> mapM (irTerm top vs env) args
402406
403407 -- a name applied to arguments
404408 (P _ n _, args) -> do
@@ -406,23 +410,23 @@ irTerm vs env tm@(App _ f a) = do
406410 -- if it's a primitive that is already saturated,
407411 -- compile to the corresponding op here already to save work
408412 Just (arity, op) | length args == arity
409− -> LOp op <$> mapM (irTerm vs env) args
413+ -> LOp op <$> mapM (irTerm top vs env) args
410414
411415 -- otherwise, just apply the name
412416 _ -> applyName n ist args
413417
414418 -- turn de bruijn vars into regular named references and try again
415− (V i, args) -> irTerm vs env $ mkApp (P Bound (env !! i) Erased) args
419+ (V i, args) -> irTerm top vs env $ mkApp (P Bound (env !! i) Erased) args
416420
417421 (f, args)
418422 -> LApp False
419− <$> irTerm vs env f
420− <*> mapM (irTerm vs env) args
423+ <$> irTerm top vs env f
424+ <*> mapM (irTerm top vs env) args
421425
422426 where
423427 buildApp :: LExp -> [Term] -> Idris LExp
424428 buildApp e [] = return e
425− buildApp e xs = LApp False e <$> mapM (irTerm vs env) xs
429+ buildApp e xs = LApp False e <$> mapM (irTerm top vs env) xs
426430
427431 applyToNames :: LExp -> [Name] -> LExp
428432 applyToNames tm [] = tm
@@ -437,7 +441,7 @@ irTerm vs env tm@(App _ f a) = do
437441
438442 applyName :: Name -> IState -> [Term] -> Idris LExp
439443 applyName n ist args =
440− LApp False (LV $ Glob n) <$> mapM (irTerm vs env . erase) (zip [0..] args)
444+ LApp False (LV $ Glob n) <$> mapM (irTerm top vs env . erase) (zip [0..] args)
441445 where
442446 erase (i, x)
443447 | i >= arity || i `elem` used = x
@@ -459,31 +463,31 @@ irTerm vs env tm@(App _ f a) = do
459463 used = maybe [] (map fst . usedpos) $ lookupCtxtExact uName (idris_callgraph ist)
460464 fst4 (x,_,_,_,_) = x
461465
462−irTerm vs env (P _ n _) = return $ LV (Glob n)
463−irTerm vs env (V i)
466+irTerm top vs env (P _ n _) = return $ LV (Glob n)
467+irTerm top vs env (V i)
464468 | i >= 0 && i < length env = return $ LV (Glob (env!!i))
465469 | otherwise = ifail $ "bad de bruijn index: " ++ show i
466470
467−irTerm vs env (Bind n (Lam _) sc) = LLam [n'] <$> irTerm vs (n':env) sc
471+irTerm top vs env (Bind n (Lam _) sc) = LLam [n'] <$> irTerm top vs (n':env) sc
468472 where
469473 n' = uniqueName n env
470474
471−irTerm vs env (Bind n (Let _ v) sc)
472− = LLet n <$> irTerm vs env v <*> irTerm vs (n : env) sc
475+irTerm top vs env (Bind n (Let _ v) sc)
476+ = LLet n <$> irTerm top vs env v <*> irTerm top vs (n : env) sc
473477
474−irTerm vs env (Bind _ _ _) = return $ LNothing
478+irTerm top vs env (Bind _ _ _) = return $ LNothing
475479
476−irTerm vs env (Proj t (-1)) = do
477− t' <- irTerm vs env t
480+irTerm top vs env (Proj t (-1)) = do
481+ t' <- irTerm top vs env t
478482 return $ LOp (LMinus (ATInt ITBig))
479483 [t', LConst (BI 1)]
480484
481−irTerm vs env (Proj t i) = LProj <$> irTerm vs env t <*> pure i
482−irTerm vs env (Constant TheWorld) = return $ LNothing
483−irTerm vs env (Constant c) = return $ LConst c
484−irTerm vs env (TType _) = return $ LNothing
485−irTerm vs env Erased = return $ LNothing
486−irTerm vs env Impossible = return $ LNothing
485+irTerm top vs env (Proj t i) = LProj <$> irTerm top vs env t <*> pure i
486+irTerm top vs env (Constant TheWorld) = return $ LNothing
487+irTerm top vs env (Constant c) = return $ LConst c
488+irTerm top vs env (TType _) = return $ LNothing
489+irTerm top vs env Erased = return $ LNothing
490+irTerm top vs env Impossible = return $ LNothing
487491
488492doForeign :: Vars -> [Name] -> [Term] -> Idris LExp
489493doForeign vs env (ret : fname : world : args)
@@ -494,7 +498,7 @@ doForeign vs env (ret : fname : world : args)
494498 where
495499 splitArg tm | (_, [_,_,l,r]) <- unApply tm -- pair, two implicits
496500 = do let l' = toFDesc l
497− r' <- irTerm vs env r
501+ r' <- irTerm (sMN 0 "__foreignCall") vs env r
498502 return (l', r')
499503 splitArg _ = ifail $ "Badly formed foreign function call"
500504
@@ -508,24 +512,24 @@ doForeign vs env (ret : fname : world : args)
508512 deNS n = n
509513doForeign vs env xs = ifail "Badly formed foreign function call"
510514
511−irTree :: [Name] -> SC -> Idris LExp
512−irTree args tree = do
515+irTree :: Name -> [Name] -> SC -> Idris LExp
516+irTree top args tree = do
513517 logCodeGen 3 $ "Compiling " ++ show args ++ "\n" ++ show tree
514− LLam args <$> irSC M.empty tree
518+ LLam args <$> irSC top M.empty tree
515519
516−irSC :: Vars -> SC -> Idris LExp
517−irSC vs (STerm t) = irTerm vs [] t
518−irSC vs (UnmatchedCase str) = return $ LError str
520+irSC :: Name -> Vars -> SC -> Idris LExp
521+irSC top vs (STerm t) = irTerm top vs [] t
522+irSC top vs (UnmatchedCase str) = return $ LError str
519523
520−irSC vs (ProjCase tm alts) = do
521− tm' <- irTerm vs [] tm
522− alts' <- mapM (irAlt vs tm') alts
524+irSC top vs (ProjCase tm alts) = do
525+ tm' <- irTerm top vs [] tm
526+ alts' <- mapM (irAlt top vs tm') alts
523527 return $ LCase Shared tm' alts'
524528
525529-- Transform matching on Delay to applications of Force.
526−irSC vs (Case up n [ConCase (UN delay) i [_, _, n'] sc])
530+irSC top vs (Case up n [ConCase (UN delay) i [_, _, n'] sc])
527531 | delay == txt "Delay"
528− = do sc' <- irSC vs $ mkForce n' n sc
532+ = do sc' <- irSC top vs $ mkForce n' n sc
529533 return $ LLet n' (LForce (LV (Glob n))) sc'
530534
531535-- There are two transformations in this case:
@@ -550,7 +554,7 @@ irSC vs (Case up n [ConCase (UN delay) i [_, _, n'] sc])
550554-- Hence, we check whether the variables are used at all
551555-- and erase the casesplit if they are not.
552556--
553−irSC vs (Case up n [alt]) = do
557+irSC top vs (Case up n [alt]) = do
554558 replacement <- case alt of
555559 ConCase cn a ns sc -> do
556560 detag <- fgetState (opt_detaggable . ist_optimisation cn)
@@ -561,9 +565,9 @@ irSC vs (Case up n [alt]) = do
561565 _ -> return Nothing
562566
563567 case replacement of
564− Just sc -> irSC vs sc
568+ Just sc -> irSC top vs sc
565569 _ -> do
566− alt' <- irAlt vs (LV (Glob n)) alt
570+ alt' <- irAlt top vs (LV (Glob n)) alt
567571 return $ case namesBoundIn alt' `usedIn` subexpr alt' of
568572 [] -> subexpr alt' -- strip the unused top-most case
569573 _ -> LCase up (LV (Glob n)) [alt']
@@ -594,13 +598,13 @@ irSC vs (Case up n [alt]) = do
594598-- This work-around is not entirely optimal; the best approach would be
595599-- to ensure that such case trees don't arise in the first place.
596600--
597−irSC vs (Case up n alts@[ConCase cn a ns sc, DefaultCase sc']) = do
601+irSC top vs (Case up n alts@[ConCase cn a ns sc, DefaultCase sc']) = do
598602 detag <- fgetState (opt_detaggable . ist_optimisation cn)
599603 if detag
600− then irSC vs (Case up n [ConCase cn a ns sc])
601− else LCase up (LV (Glob n)) <$> mapM (irAlt vs (LV (Glob n))) alts
604+ then irSC top vs (Case up n [ConCase cn a ns sc])
605+ else LCase up (LV (Glob n)) <$> mapM (irAlt top vs (LV (Glob n))) alts
602606
603−irSC vs sc@(Case up n alts) = do
607+irSC top vs sc@(Case up n alts) = do
604608 -- check that neither alternative needs the newtype optimisation,
605609 -- see comment above
606610 goneWrong <- or <$> mapM isDetaggable alts
@@ -608,20 +612,20 @@ irSC vs sc@(Case up n alts) = do
608612 $ ifail ("irSC: non-trivial case-match on detaggable data: " ++ show sc)
609613
610614 -- everything okay
611− LCase up (LV (Glob n)) <$> mapM (irAlt vs (LV (Glob n))) alts
615+ LCase up (LV (Glob n)) <$> mapM (irAlt top vs (LV (Glob n))) alts
612616 where
613617 isDetaggable (ConCase cn _ _ _) = fgetState $ opt_detaggable . ist_optimisation cn
614618 isDetaggable _ = return False
615619
616−irSC vs ImpossibleCase = return LNothing
620+irSC top vs ImpossibleCase = return LNothing
617621
618−irAlt :: Vars -> LExp -> CaseAlt -> Idris LAlt
622+irAlt :: Name -> Vars -> LExp -> CaseAlt -> Idris LAlt
619623
620624-- this leaves out all unused arguments of the constructor
621−irAlt vs _ (ConCase n t args sc) = do
625+irAlt top vs _ (ConCase n t args sc) = do
622626 used <- map fst <$> fgetState (cg_usedpos . ist_callgraph n)
623627 let usedArgs = [a | (i,a) <- zip [0..] args, i `elem` used]
624− LConCase (-1) n usedArgs <$> irSC (methodVars `M.union` vs) sc
628+ LConCase (-1) n usedArgs <$> irSC top (methodVars `M.union` vs) sc
625629 where
626630 methodVars = case n of
627631 SN (InstanceCtorN className)
@@ -631,9 +635,9 @@ irAlt vs _ (ConCase n t args sc) = do
631635 _
632636 -> M.empty -- not an instance constructor
633637
634−irAlt vs _ (ConstCase x rhs)
635− | matchable x = LConstCase x <$> irSC vs rhs
636− | matchableTy x = LDefaultCase <$> irSC vs rhs
638+irAlt top vs _ (ConstCase x rhs)
639+ | matchable x = LConstCase x <$> irSC top vs rhs
640+ | matchableTy x = LDefaultCase <$> irSC top vs rhs
637641 where
638642 matchable (I _) = True
639643 matchable (BI _) = True
@@ -657,14 +661,14 @@ irAlt vs _ (ConstCase x rhs)
657661
658662 matchableTy _ = False
659663
660−irAlt vs tm (SucCase n rhs) = do
661− rhs' <- irSC vs rhs
664+irAlt top vs tm (SucCase n rhs) = do
665+ rhs' <- irSC top vs rhs
662666 return $ LDefaultCase (LLet n (LOp (LMinus (ATInt ITBig))
663667 [tm,
664668 LConst (BI 1)]) rhs')
665669
666−irAlt vs _ (ConstCase c rhs)
670+irAlt top vs _ (ConstCase c rhs)
667671 = ifail $ "Can't match on (" ++ show c ++ ")"
668672
669−irAlt vs _ (DefaultCase rhs)
670− = LDefaultCase <$> irSC vs rhs
673+irAlt top vs _ (DefaultCase rhs)
674+ = LDefaultCase <$> irSC top vs rhs