1{-# LANGUAGE PatternGuards, DeriveFunctor #-}
5import Control.Monad.State hiding (lift)
6import Control.Applicative hiding (Const)
9import Idris.Core.CaseTree
14data Endianness = Native | BE | LE deriving (Show, Eq)
16data LVar = Loc Int | Glob Name
19-- ASSUMPTION: All variable bindings have unique names here
21 | LApp Bool LExp [LExp] -- True = tail call
22 | LLazyApp Name [LExp] -- True = tail call
24 | LForce LExp -- make sure Exp is evaluted
25 | LLet Name LExp LExp -- name just for pretty printing
26 | LLam [Name] LExp -- lambda, lifted out before compiling
27 | LProj LExp Int -- projection
28 | LCon (Maybe LVar) -- Location to reallocate, if available
30 | LCase CaseType LExp [LAlt]
32 | LForeign FDesc -- Function descriptor (usually name as string)
33 FDesc -- Return type descriptor
34 [(FDesc, LExp)] -- first LExp is the FFI type description
47data Export = ExportData FDesc -- Exported data descriptor (usually string)
48 | ExportFun Name -- Idris name
49 FDesc -- Exported function descriptor
50 FDesc -- Return type descriptor
51 [FDesc] -- Argument types
54data ExportIFace = Export Name -- FFI descriptor
55 String -- interface file
59-- Primitive operators. Backends are not *required* to implement all
60-- of these, but should report an error if they are unable
62data PrimFn = LPlus ArithTy | LMinus ArithTy | LTimes ArithTy
63 | LUDiv IntTy | LSDiv ArithTy | LURem IntTy | LSRem ArithTy
64 | LAnd IntTy | LOr IntTy | LXOr IntTy | LCompl IntTy
65 | LSHL IntTy | LLSHR IntTy | LASHR IntTy
66 | LEq ArithTy | LLt IntTy | LLe IntTy | LGt IntTy | LGe IntTy
67 | LSLt ArithTy | LSLe ArithTy | LSGt ArithTy | LSGe ArithTy
68 | LSExt IntTy IntTy | LZExt IntTy IntTy | LTrunc IntTy IntTy
69 | LStrConcat | LStrLt | LStrEq | LStrLen
70 | LIntFloat IntTy | LFloatInt IntTy | LIntStr IntTy | LStrInt IntTy
71 | LFloatStr | LStrFloat | LChInt IntTy | LIntCh IntTy
72 | LBitCast ArithTy ArithTy -- Only for values of equal width
74 | LFExp | LFLog | LFSin | LFCos | LFTan | LFASin | LFACos | LFATan
75 | LFSqrt | LFFloor | LFCeil | LFNegate
77 | LStrHead | LStrTail | LStrCons | LStrIndex | LStrRev
78 | LReadStr | LWriteStr
84 | LPar -- evaluate argument anywhere, possibly on another
85 -- core or another machine. 'id' is a valid implementation
90-- Supported target languages for foreign calls
92data FCallType = FStatic | FObject | FConstructor
95data FType = FArith ArithTy
105-- FIXME: Why not use this for all the IRs now?
106data LAlt' e = LConCase Int Name [Name] e
109 deriving (Show, Eq, Functor)
111type LAlt = LAlt' LExp
113data LDecl = LFun [LOpt] Name [Name] LExp -- options, name, arg names, def
114 | LConstructor Name Int Int -- constructor name, tag, arity
117type LDefs = Ctxt LDecl
119data LOpt = Inline | NoInline
122addTags :: Int -> [(Name, LDecl)] -> (Int, [(Name, LDecl)])
123addTags i ds = tag i ds []
124 where tag i ((n, LConstructor n' (-1) a) : as) acc
125 = tag (i + 1) as ((n, LConstructor n' i a) : acc)
126 tag i ((n, LConstructor n' t a) : as) acc
127 = tag i as ((n, LConstructor n' t a) : acc)
128 tag i (x : as) acc = tag i as (x : acc)
129 tag i [] acc = (i, reverse acc)
131data LiftState = LS Name Int [(Name, LDecl)]
133lname (NS n x) i = NS (lname n i) x
134lname (UN n) i = MN i n
135lname x i = sMN i (show x ++ "_lam")
137liftAll :: [(Name, LDecl)] -> [(Name, LDecl)]
138liftAll xs = concatMap (\ (x, d) -> lambdaLift x d) xs
140lambdaLift :: Name -> LDecl -> [(Name, LDecl)]
141lambdaLift n (LFun opts _ args e)
142 = let (e', (LS _ _ decls)) = runState (lift args e) (LS n 0 []) in
143 (n, LFun opts n args e') : decls
144lambdaLift n x = [(n, x)]
146getNextName :: State LiftState Name
147getNextName = do LS n i ds <- get
148 put (LS n (i + 1) ds)
151addFn :: Name -> LDecl -> State LiftState ()
152addFn fn d = do LS n i ds <- get
153 put (LS n i ((fn, d) : ds))
155lift :: [Name] -> LExp -> State LiftState LExp
156lift env (LV v) = return (LV v) -- Lifting happens before these can exist...
157lift env (LApp tc (LV (Glob n)) args) = do args' <- mapM (lift env) args
158 return (LApp tc (LV (Glob n)) args')
159lift env (LApp tc f args) = do f' <- lift env f
161 addFn fn (LFun [Inline] fn env f')
162 args' <- mapM (lift env) args
163 return (LApp tc (LV (Glob fn)) (map (LV . Glob) env ++ args'))
164lift env (LLazyApp n args) = do args' <- mapM (lift env) args
165 return (LLazyApp n args')
166lift env (LLazyExp (LConst c)) = return (LConst c)
167-- lift env (LLazyExp (LApp tc (LV (Glob f)) args))
168-- = lift env (LLazyApp f args)
169lift env (LLazyExp e) = do e' <- lift env e
170 let usedArgs = nub $ usedIn env e'
172 addFn fn (LFun [NoInline] fn usedArgs e')
173 return (LLazyApp fn (map (LV . Glob) usedArgs))
174lift env (LForce e) = do e' <- lift env e
176lift env (LLet n v e) = do v' <- lift env v
177 e' <- lift (env ++ [n]) e
178 return (LLet n v' e')
179lift env (LLam args e) = do e' <- lift (env ++ args) e
180 let usedArgs = nub $ usedIn env e'
182 addFn fn (LFun [Inline] fn (usedArgs ++ args) e')
183 return (LApp False (LV (Glob fn)) (map (LV . Glob) usedArgs))
184lift env (LProj t i) = do t' <- lift env t
186lift env (LCon loc i n args) = do args' <- mapM (lift env) args
187 return (LCon loc i n args')
188lift env (LCase up e alts) = do alts' <- mapM liftA alts
190 return (LCase up e' alts')
192 liftA (LConCase i n args e) = do e' <- lift (env ++ args) e
193 return (LConCase i n args e')
194 liftA (LConstCase c e) = do e' <- lift env e
195 return (LConstCase c e')
196 liftA (LDefaultCase e) = do e' <- lift env e
197 return (LDefaultCase e')
198lift env (LConst c) = return (LConst c)
199lift env (LForeign t s args) = do args' <- mapM (liftF env) args
200 return (LForeign t s args')
202 liftF env (t, e) = do e' <- lift env e
204lift env (LOp f args) = do args' <- mapM (lift env) args
206lift env (LError str) = return $ LError str
207lift env LNothing = return $ LNothing
209allocUnique :: LDefs -> (Name, LDecl) -> (Name, LDecl)
210allocUnique defs p@(n, LConstructor _ _ _) = p
211allocUnique defs (n, LFun opts fn args e)
212 = let e' = evalState (findUp e) [] in
213 (n, LFun opts fn args e')
215 -- Keep track of 'updatable' names in the state, i.e. names whose heap
216 -- entry may be reused, along with the arity which was there
217 findUp :: LExp -> State [(Name, Int)] LExp
218 findUp (LApp t (LV (Glob n)) as)
219 | Just (LConstructor _ i ar) <- lookupCtxtExact n defs,
221 = findUp (LCon Nothing i n as)
223 | Just (LConstructor _ i 0) <- lookupCtxtExact n defs
224 = return $ LCon Nothing i n [] -- nullary cons are global, no need to update
225 findUp (LApp t f as) = LApp t <$> findUp f <*> mapM findUp as
226 findUp (LLazyApp n as) = LLazyApp n <$> mapM findUp as
227 findUp (LLazyExp e) = LLazyExp <$> findUp e
228 findUp (LForce e) = LForce <$> findUp e
229 -- use assumption that names are unique!
230 findUp (LLet n val sc) = LLet n <$> findUp val <*> findUp sc
231 findUp (LLam ns sc) = LLam ns <$> findUp sc
232 findUp (LProj e i) = LProj <$> findUp e <*> return i
233 findUp (LCon (Just l) i n es) = LCon (Just l) i n <$> mapM findUp es
234 findUp (LCon Nothing i n es)
236 v <- findVar [] avail (length es)
237 LCon v i n <$> mapM findUp es
238 findUp (LForeign t s es)
239 = LForeign t s <$> mapM (\ (t, e) -> do e' <- findUp e
241 findUp (LOp o es) = LOp o <$> mapM findUp es
242 findUp (LCase Updatable e@(LV (Glob n)) as)
243 = LCase Updatable e <$> mapM (doUpAlt n) as
244 findUp (LCase t e as)
245 = LCase t <$> findUp e <*> mapM findUpAlt as
248 findUpAlt (LConCase i t args rhs) = do avail <- get
251 return $ LConCase i t args rhs'
252 findUpAlt (LConstCase i rhs) = LConstCase i <$> findUp rhs
253 findUpAlt (LDefaultCase rhs) = LDefaultCase <$> findUp rhs
255 doUpAlt n (LConCase i t args rhs)
257 put ((n, length args) : avail)
260 return $ LConCase i t args rhs'
261 doUpAlt n (LConstCase i rhs) = LConstCase i <$> findUp rhs
262 doUpAlt n (LDefaultCase rhs) = LDefaultCase <$> findUp rhs
264 findVar _ [] i = return Nothing
265 findVar acc ((n, l) : ns) i | l == i = do put (reverse acc ++ ns)
266 return (Just (Glob n))
267 findVar acc (n : ns) i = findVar (n : acc) ns i
270-- Return variables in list which are used in the expression
272usedArg env n | n `elem` env = [n]
275usedIn :: [Name] -> LExp -> [Name]
276usedIn env (LV (Glob n)) = usedArg env n
277usedIn env (LApp _ e args) = usedIn env e ++ concatMap (usedIn env) args
278usedIn env (LLazyApp n args) = concatMap (usedIn env) args ++ usedArg env n
279usedIn env (LLazyExp e) = usedIn env e
280usedIn env (LForce e) = usedIn env e
281usedIn env (LLet n v e) = usedIn env v ++ usedIn (env \\ [n]) e
282usedIn env (LLam ns e) = usedIn (env \\ ns) e
283usedIn env (LCon v i n args) = let rest = concatMap (usedIn env) args in
286 Just (Glob n) -> usedArg env n ++ rest
287usedIn env (LProj t i) = usedIn env t
288usedIn env (LCase up e alts) = usedIn env e ++ concatMap (usedInA env) alts
289 where usedInA env (LConCase i n ns e) = usedIn env e
290 usedInA env (LConstCase c e) = usedIn env e
291 usedInA env (LDefaultCase e) = usedIn env e
292usedIn env (LForeign _ _ args) = concatMap (usedIn env) (map snd args)
293usedIn env (LOp f args) = concatMap (usedIn env) args
296instance Show LExp where
297 show e = show' [] "" e where
298 show' env ind (LV (Loc i)) = env!!i
299 show' env ind (LV (Glob n)) = show n
301 show' env ind (LLazyApp e args)
302 = show e ++ "|(" ++ showSep ", " (map (show' env ind) args) ++")"
304 show' env ind (LApp _ e args)
305 = show' env ind e ++ "(" ++ showSep ", " (map (show' env ind) args) ++")"
307 show' env ind (LLazyExp e) = "lazy{ " ++ show' env ind e ++ " }"
308 show' env ind (LForce e) = "force{ " ++ show' env ind e ++ " }"
309 show' env ind (LLet n v e)
310 = "let " ++ show n ++ " = " ++ show' env ind v
311 ++ " in " ++ show' (env ++ [show n]) ind e
313 show' env ind (LLam args e)
314 = "\\ " ++ showSep "," (map show args)
315 ++ " => " ++ show' (env ++ (map show args)) ind e
317 show' env ind (LProj t i) = show t ++ "!" ++ show i
319 show' env ind (LCon loc i n args)
320 = atloc loc ++ show n ++ "(" ++ showSep ", " (map (show' env ind) args) ++ ")"
321 where atloc Nothing = ""
322 atloc (Just l) = "@" ++ show (LV l) ++ ":"
324 show' env ind (LCase up e alts)
325 = "case" ++ update ++ show' env ind e ++ " of \n" ++ fmt alts
332 = "\t" ++ ind ++ "| " ++ showAlt env (ind ++ " ") alt
334 = "\t" ++ ind ++ "| " ++ showAlt env (ind ++ ". ") alt
337 show' env ind (LConst c) = show c
339 show' env ind (LForeign ty n args) = concat
342 , showSep ", " (map (\(ty,x) -> show' env ind x ++ " : " ++ show ty) args)
348 show' env ind (LOp f args)
349 = show f ++ "(" ++ showSep ", " (map (show' env ind) args) ++ ")"
351 show' env ind (LError str) = "error " ++ show str
352 show' env ind LNothing = "____"
354 showAlt env ind (LConCase _ n args e)
355 = show n ++ "(" ++ showSep ", " (map show args) ++ ") => "
358 showAlt env ind (LConstCase c e) = show c ++ " => " ++ show' env ind e
359 showAlt env ind (LDefaultCase e) = "_ => " ++ show' env ind e
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