3Description : Internal representation of Idris' constructs.
6Maintainer : The Idris Community.
8{-# LANGUAGE DeriveDataTypeable, DeriveFunctor, DeriveGeneric, FlexibleContexts,
13import Idris.Core.CaseTree
16import Control.Monad.State hiding (lift)
17import Data.Data (Data)
19import Data.Typeable (Typeable)
20import GHC.Generics (Generic)
22data Endianness = Native | BE | LE deriving (Show, Eq)
24data LVar = Loc Int | Glob Name
27-- ASSUMPTION: All variable bindings have unique names here
28-- Constructors commented as lifted are not present in the LIR provided to the different backends.
30 | LApp Bool LExp [LExp] -- True = tail call
31 | LLazyApp Name [LExp] -- True = tail call
32 | LLazyExp LExp -- lifted out before compiling
33 | LForce LExp -- make sure Exp is evaluted
34 | LLet Name LExp LExp -- name just for pretty printing
35 | LLam [Name] LExp -- lambda, lifted out before compiling
36 | LProj LExp Int -- projection
37 | LCon (Maybe Name) -- Location to reallocate, if available
39 | LCase CaseType LExp [LAlt]
41 | LForeign FDesc -- Function descriptor (usually name as string)
42 FDesc -- Return type descriptor
43 [(FDesc, LExp)] -- first LExp is the FFI type description
56data Export = ExportData FDesc -- Exported data descriptor (usually string)
57 | ExportFun Name -- Idris name
58 FDesc -- Exported function descriptor
59 FDesc -- Return type descriptor
60 [FDesc] -- Argument types
63data ExportIFace = Export Name -- FFI descriptor
64 String -- interface file
68-- Primitive operators. Backends are not *required* to implement all
69-- of these, but should report an error if they are unable
71data PrimFn = LPlus ArithTy | LMinus ArithTy | LTimes ArithTy
72 | LUDiv IntTy | LSDiv ArithTy | LURem IntTy | LSRem ArithTy
73 | LAnd IntTy | LOr IntTy | LXOr IntTy | LCompl IntTy
74 | LSHL IntTy | LLSHR IntTy | LASHR IntTy
75 | LEq ArithTy | LLt IntTy | LLe IntTy | LGt IntTy | LGe IntTy
76 | LSLt ArithTy | LSLe ArithTy | LSGt ArithTy | LSGe ArithTy
77 | LSExt IntTy IntTy | LZExt IntTy IntTy | LTrunc IntTy IntTy
78 | LStrConcat | LStrLt | LStrEq | LStrLen
79 | LIntFloat IntTy | LFloatInt IntTy | LIntStr IntTy | LStrInt IntTy
80 | LFloatStr | LStrFloat | LChInt IntTy | LIntCh IntTy
81 | LBitCast ArithTy ArithTy -- Only for values of equal width
83 | LFExp | LFLog | LFSin | LFCos | LFTan | LFASin | LFACos | LFATan
84 | LFSqrt | LFFloor | LFCeil | LFNegate
86 | LStrHead | LStrTail | LStrCons | LStrIndex | LStrRev | LStrSubstr
87 | LReadStr | LWriteStr
93 | LPar -- evaluate argument anywhere, possibly on another
94 -- core or another machine. 'id' is a valid implementation
99 deriving (Show, Eq, Generic)
101-- Supported target languages for foreign calls
103data FCallType = FStatic | FObject | FConstructor
106data FType = FArith ArithTy
117-- FIXME: Why not use this for all the IRs now?
118data LAlt' e = LConCase Int Name [Name] e
121 deriving (Show, Eq, Functor, Data, Typeable)
123type LAlt = LAlt' LExp
125data LDecl = LFun [LOpt] Name [Name] LExp -- options, name, arg names, def
126 | LConstructor Name Int Int -- constructor name, tag, arity
129type LDefs = Ctxt LDecl
131data LOpt = Inline | NoInline
134addTags :: Int -> [(Name, LDecl)] -> (Int, [(Name, LDecl)])
135addTags i ds = tag i ds []
136 where tag i ((n, LConstructor n' (-1) a) : as) acc
137 = tag (i + 1) as ((n, LConstructor n' i a) : acc)
138 tag i ((n, LConstructor n' t a) : as) acc
139 = tag i as ((n, LConstructor n' t a) : acc)
140 tag i (x : as) acc = tag i as (x : acc)
141 tag i [] acc = (i, reverse acc)
143data LiftState = LS Name Int [(Name, LDecl)]
145lname (NS n x) i = NS (lname n i) x
146lname (UN n) i = MN i n
147lname x i = sMN i (showCG x ++ "_lam")
149liftAll :: [(Name, LDecl)] -> [(Name, LDecl)]
150liftAll xs = concatMap (\ (x, d) -> lambdaLift x d) xs
152lambdaLift :: Name -> LDecl -> [(Name, LDecl)]
153lambdaLift n (LFun opts _ args e)
154 = let (e', (LS _ _ decls)) = runState (lift args e) (LS n 0 []) in
155 (n, LFun opts n args e') : decls
156lambdaLift n x = [(n, x)]
158getNextName :: State LiftState Name
159getNextName = do LS n i ds <- get
160 put (LS n (i + 1) ds)
163addFn :: Name -> LDecl -> State LiftState ()
164addFn fn d = do LS n i ds <- get
165 put (LS n i ((fn, d) : ds))
167lift :: [Name] -> LExp -> State LiftState LExp
168lift env (LV v) = return (LV v) -- Lifting happens before these can exist...
169lift env (LApp tc (LV n) args) = do args' <- mapM (lift env) args
170 return (LApp tc (LV n) args')
171lift env (LApp tc f args) = do f' <- lift env f
173 addFn fn (LFun [Inline] fn env f')
174 args' <- mapM (lift env) args
175 return (LApp tc (LV fn) (map LV env ++ args'))
176lift env (LLazyApp n args) = do args' <- mapM (lift env) args
177 return (LLazyApp n args')
178lift env (LLazyExp (LConst c)) = return (LConst c)
179-- lift env (LLazyExp (LApp tc (LV (Glob f)) args))
180-- = lift env (LLazyApp f args)
181lift env (LLazyExp e) = do e' <- lift env e
182 let usedArgs = nub $ usedIn env e'
184 addFn fn (LFun [NoInline] fn usedArgs e')
185 return (LLazyApp fn (map LV usedArgs))
186lift env (LForce e) = do e' <- lift env e
188lift env (LLet n v e) = do v' <- lift env v
189 e' <- lift (env ++ [n]) e
190 return (LLet n v' e')
191lift env (LLam args e) = do e' <- lift (env ++ args) e
192 let usedArgs = nub $ usedIn env e'
194 addFn fn (LFun [Inline] fn (usedArgs ++ args) e')
195 return (LApp False (LV fn) (map LV usedArgs))
196lift env (LProj t i) = do t' <- lift env t
198lift env (LCon loc i n args) = do args' <- mapM (lift env) args
199 return (LCon loc i n args')
200lift env (LCase up e alts) = do alts' <- mapM liftA alts
202 return (LCase up e' alts')
204 liftA (LConCase i n args e) = do e' <- lift (env ++ args) e
205 return (LConCase i n args e')
206 liftA (LConstCase c e) = do e' <- lift env e
207 return (LConstCase c e')
208 liftA (LDefaultCase e) = do e' <- lift env e
209 return (LDefaultCase e')
210lift env (LConst c) = return (LConst c)
211lift env (LForeign t s args) = do args' <- mapM (liftF env) args
212 return (LForeign t s args')
214 liftF env (t, e) = do e' <- lift env e
216lift env (LOp f args) = do args' <- mapM (lift env) args
218lift env (LError str) = return $ LError str
219lift env LNothing = return LNothing
221allocUnique :: LDefs -> (Name, LDecl) -> (Name, LDecl)
222allocUnique defs p@(n, LConstructor _ _ _) = p
223allocUnique defs (n, LFun opts fn args e)
224 = let e' = evalState (findUp e) [] in
225 (n, LFun opts fn args e')
227 -- Keep track of 'updatable' names in the state, i.e. names whose heap
228 -- entry may be reused, along with the arity which was there
229 findUp :: LExp -> State [(Name, Int)] LExp
230 findUp (LApp t (LV n) as)
231 | Just (LConstructor _ i ar) <- lookupCtxtExact n defs,
233 = findUp (LCon Nothing i n as)
235 | Just (LConstructor _ i 0) <- lookupCtxtExact n defs
236 = return $ LCon Nothing i n [] -- nullary cons are global, no need to update
237 findUp (LApp t f as) = LApp t <$> findUp f <*> mapM findUp as
238 findUp (LLazyApp n as) = LLazyApp n <$> mapM findUp as
239 findUp (LLazyExp e) = LLazyExp <$> findUp e
240 findUp (LForce e) = LForce <$> findUp e
241 -- use assumption that names are unique!
242 findUp (LLet n val sc) = LLet n <$> findUp val <*> findUp sc
243 findUp (LLam ns sc) = LLam ns <$> findUp sc
244 findUp (LProj e i) = LProj <$> findUp e <*> return i
245 findUp (LCon (Just l) i n es) = LCon (Just l) i n <$> mapM findUp es
246 findUp (LCon Nothing i n es)
248 v <- findVar [] avail (length es)
249 LCon v i n <$> mapM findUp es
250 findUp (LForeign t s es)
251 = LForeign t s <$> mapM (\ (t, e) -> do e' <- findUp e
253 findUp (LOp o es) = LOp o <$> mapM findUp es
254 findUp (LCase Updatable e@(LV n) as)
255 = LCase Updatable e <$> mapM (doUpAlt n) as
256 findUp (LCase t e as)
257 = LCase t <$> findUp e <*> mapM findUpAlt as
260 findUpAlt (LConCase i t args rhs) = do avail <- get
263 return $ LConCase i t args rhs'
264 findUpAlt (LConstCase i rhs) = LConstCase i <$> findUp rhs
265 findUpAlt (LDefaultCase rhs) = LDefaultCase <$> findUp rhs
267 doUpAlt n (LConCase i t args rhs)
269 put ((n, length args) : avail)
272 return $ LConCase i t args rhs'
273 doUpAlt n (LConstCase i rhs) = LConstCase i <$> findUp rhs
274 doUpAlt n (LDefaultCase rhs) = LDefaultCase <$> findUp rhs
276 findVar _ [] i = return Nothing
277 findVar acc ((n, l) : ns) i | l == i = do put (reverse acc ++ ns)
279 findVar acc (n : ns) i = findVar (n : acc) ns i
282-- Return variables in list which are used in the expression
284usedArg env n | n `elem` env = [n]
287usedIn :: [Name] -> LExp -> [Name]
288usedIn env (LV n) = usedArg env n
289usedIn env (LApp _ e args) = usedIn env e ++ concatMap (usedIn env) args
290usedIn env (LLazyApp n args) = concatMap (usedIn env) args ++ usedArg env n
291usedIn env (LLazyExp e) = usedIn env e
292usedIn env (LForce e) = usedIn env e
293usedIn env (LLet n v e) = usedIn env v ++ usedIn (env \\ [n]) e
294usedIn env (LLam ns e) = usedIn (env \\ ns) e
295usedIn env (LCon v i n args) = let rest = concatMap (usedIn env) args in
298 Just n -> usedArg env n ++ rest
299usedIn env (LProj t i) = usedIn env t
300usedIn env (LCase up e alts) = usedIn env e ++ concatMap (usedInA env) alts
301 where usedInA env (LConCase i n ns e) = usedIn env e
302 usedInA env (LConstCase c e) = usedIn env e
303 usedInA env (LDefaultCase e) = usedIn env e
304usedIn env (LForeign _ _ args) = concatMap (usedIn env) (map snd args)
305usedIn env (LOp f args) = concatMap (usedIn env) args
308lsubst :: Name -> LExp -> LExp -> LExp
309lsubst n new (LV x) | n == x = new
310lsubst n new (LApp t e args) = let e' = lsubst n new e
311 args' = map (lsubst n new) args in
313lsubst n new (LLazyApp fn args) = let args' = map (lsubst n new) args in
315lsubst n new (LLazyExp e) = LLazyExp (lsubst n new e)
316lsubst n new (LForce e) = LForce (lsubst n new e)
317lsubst n new (LLet v val sc) = LLet v (lsubst n new val) (lsubst n new sc)
318lsubst n new (LLam ns sc) = LLam ns (lsubst n new sc)
319lsubst n new (LProj e i) = LProj (lsubst n new e) i
320lsubst n new (LCon lv t cn args) = let args' = map (lsubst n new) args in
322lsubst n new (LOp op args) = let args' = map (lsubst n new) args in
324lsubst n new (LForeign fd rd args)
325 = let args' = map (\(d, a) -> (d, lsubst n new a)) args in
327lsubst n new (LCase t e alts) = let e' = lsubst n new e
328 alts' = map (fmap (lsubst n new)) alts in
332instance Show LExp where
333 show e = show' [] "" e where
334 show' env ind (LV n) = show n
336 show' env ind (LLazyApp e args)
337 = show e ++ "|(" ++ showSep ", " (map (show' env ind) args) ++")"
339 show' env ind (LApp _ e args)
340 = show' env ind e ++ "(" ++ showSep ", " (map (show' env ind) args) ++")"
342 show' env ind (LLazyExp e) = "lazy{ " ++ show' env ind e ++ " }"
343 show' env ind (LForce e) = "force{ " ++ show' env ind e ++ " }"
344 show' env ind (LLet n v e)
345 = "let " ++ show n ++ " = " ++ show' env ind v
346 ++ " in " ++ show' (env ++ [show n]) ind e
348 show' env ind (LLam args e)
349 = "\\ " ++ showSep "," (map show args)
350 ++ " => " ++ show' (env ++ (map show args)) ind e
352 show' env ind (LProj t i) = show t ++ "!" ++ show i
354 show' env ind (LCon loc i n args)
355 = atloc loc ++ show n ++ "(" ++ showSep ", " (map (show' env ind) args) ++ ")"
356 where atloc Nothing = ""
357 atloc (Just l) = "@" ++ show (LV l) ++ ":"
359 show' env ind (LCase up e alts)
360 = "case" ++ update ++ show' env ind e ++ " of \n" ++ fmt alts
367 = "\t" ++ ind ++ "| " ++ showAlt env (ind ++ " ") alt
369 = "\t" ++ ind ++ "| " ++ showAlt env (ind ++ ". ") alt
372 show' env ind (LConst c) = show c
374 show' env ind (LForeign ty n args) = concat
377 , showSep ", " (map (\(ty,x) -> show' env ind x ++ " : " ++ show ty) args)
383 show' env ind (LOp f args)
384 = show f ++ "(" ++ showSep ", " (map (show' env ind) args) ++ ")"
386 show' env ind (LError str) = "error " ++ show str
387 show' env ind LNothing = "____"
389 showAlt env ind (LConCase _ n args e)
390 = show n ++ "(" ++ showSep ", " (map show args) ++ ") => "
393 showAlt env ind (LConstCase c e) = show c ++ " => " ++ show' env ind e
394 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