on master help sign in
Merge pull request #451 from Ralith/rts-cleanup

Delete unused code

commitdb84f5ec99ca7daac526f65aa4010bfa7d2b6d8d authorEdwin Brady <edwin.brady@gmail.com> dateMon 12 Aug 2013, 15:01 (13 years ago) parent6633a453aa61b3
12 files changed+1−1051
idris.cabal+1−1 src/Idris/AbsSyntaxTree.hs−2 src/Idris/Compiler.hs−239 src/Idris/IBC.hs−1 src/Idris/REPL.hs−18 src/Idris/REPLParser.hs−1 src/RTS/Assembler.hs−8 src/RTS/BCParser.hs−96 src/RTS/Bytecode.hs−114 src/RTS/CodegenC.hs−219 src/RTS/PreC.hs−103 src/RTS/SC.hs−249
idris.cabal+1−1
@@ -254,7 +254,7 @@ Executable idris
254254 Idris.Parser, Idris.Help, Idris.IdeSlave, Idris.REPL,
255255 Idris.REPLParser, Idris.ElabDecls, Idris.Error,
256256 Idris.Delaborate, Idris.Primitives, Idris.Imports,
257− Idris.Compiler, Idris.Prover, Idris.ElabTerm,
257+ Idris.Prover, Idris.ElabTerm,
258258 Idris.Coverage, Idris.IBC, Idris.Unlit,
259259 Idris.DataOpts, Idris.Transforms, Idris.DSL,
260260 Idris.UnusedArgs, Idris.Docs, Idris.Completion,
src/Idris/AbsSyntaxTree.hs−2
@@ -202,7 +202,6 @@ data Command = Quit
202202 | Compile Codegen String
203203 | Execute
204204 | ExecVal PTerm
205− | NewCompile String
206205 | Metavars
207206 | Prove Name
208207 | AddProof (Maybe Name)
@@ -238,7 +237,6 @@ data Opt = Filename String
238237 | NoREPL
239238 | OLogging Int
240239 | Output String
241− | NewOutput String
242240 | TypeCase
243241 | TypeInType
244242 | DefaultTotal
src/Idris/Compiler.hsdeleted−239
@@ -1,239 +0,0 @@
1−{-# LANGUAGE PatternGuards #-}
2−
3−module Idris.Compiler where
4−
5−import Idris.AbsSyntax
6−import Core.TT
7−
8−{-
9−import Idris.Transforms
10−
11−import Core.Evaluate
12−import Core.CaseTree
13−
14−import Control.Monad.State
15−import Data.List
16−import System.Process
17−import System.IO
18−import System.Directory
19−import System.Environment
20−
21−import Paths_idris
22−
23−import Epic.Epic hiding (Term, Type, Name, fn, compile)
24−import qualified Epic.Epic as E
25−-}
26−
27−compileEpic :: FilePath -> Term -> Idris ()
28−compileEpic f t = fail "Epic backend disabled"
29−
30−{-
31−compile f tm
32− = do checkMVs
33− let tmnames = namesUsed (STerm tm)
34− used <- mapM (allNames []) tmnames
35− ds <- mkDecls tm (concat used)
36− objs <- getObjectFiles
37− libs <- getLibs
38− hdrs <- getHdrs
39− ddir <- liftIO $ getDataDir
40− -- if any includes exist in the data directory, use that
41− hdrs' <- liftIO $ mapM (inDir ddir) hdrs
42− let incs = map Include hdrs'
43− so <- getSO
44−-- let ilib = ddir ++ "/libidris.a"
45− case so of
46− Nothing ->
47− do m <- epicMain tm
48− let mainval = EpicFn (name "main") m
49− liftIO $ compileObjWith []
50− (mkProgram (incs ++ mainval : ds)) (f ++ ".o")
51− liftIO $ link ((f ++ ".o") : objs ++ (map ("-l"++) libs)) f
52− where checkMVs = do i <- get
53− case idris_metavars i \\ primDefs of
54− [] -> return ()
55− ms -> fail $ "There are undefined metavariables: " ++ show ms
56− inDir d h = do let f = d ++ "/" ++ h
57− ex <- doesFileExist f
58− if ex then return f else return h
59−
60−
61−allNames :: [Name] -> Name -> Idris [Name]
62−allNames ns n | n `elem` ns = return []
63−allNames ns n = do i <- get
64− case lookupCtxt Nothing n (idris_callgraph i) of
65− [ns'] -> do more <- mapM (allNames (n:ns)) ns'
66− return (nub (n : concat more))
67− _ -> return [n]
68−
69−mkDecls :: Term -> [Name] -> Idris [EpicDecl]
70−mkDecls t used
71− = do i <- getIState
72− let ds = filter (\ (n, d) -> n `elem` used) $ ctxtAlist (tt_ctxt i)
73− decls <- mapM build ds
74− return $ basic_defs ++ decls
75−
76−-- EpicFn (name "main") epicMain : decls
77−
78−ename x = name ("idris_" ++ show x)
79−aname x = name ("a_" ++ show x)
80−
81−epicMain tm = do e <- epic tm
82− return $ effect_ e
83−
84−-- epicMain = effect_ $ -- ref (ename (UN "run__IO")) @@
85−-- ref (ename (NS (UN "main") ["main"]))
86−
87−class ToEpic a where
88− epic :: a -> Idris E.Term
89−
90−build :: (Name, Def) -> Idris EpicDecl
91−build (n, d) = do i <- getIState
92− case lookup n (idris_prims i) of
93− Just opDef -> return $ EpicFn (ename n) opDef
94− _ -> do def <- epic d
95− logLvl 3 $ "Compiled " ++ show n ++ " =\n\t" ++ show def
96− return $ EpicFn (ename n) def
97−
98−impossible = int 42424242
99−
100−instance ToEpic Def where
101− epic (Function tm _) = epic tm
102− epic (CaseOp _ _ pats _ _ args sc) = epic (args, sc) -- optimised version
103− epic _ = return impossible
104−
105−instance ToEpic (TT Name) where
106− epic tm = epic' [] tm where
107− epic' env tm@(App f a)
108− | (P _ (UN "mkForeign") _, args) <- unApply tm
109− = doForeign False args
110− | (P _ (UN "mkLazyForeign") _, args) <- unApply tm
111− = doForeign True args
112− | (P _ (UN "unsafePerformIO") _, [_, arg]) <- unApply tm
113− = epic' env arg
114− | (P _ (UN "lazy") _, [_,arg]) <- unApply tm
115− = do arg' <- epic' env arg
116− return $ lazy_ arg'
117− | (P _ (UN "prim__IO") _, [v]) <- unApply tm
118− = do v' <- epic' env v
119− return (effect_ v')
120− | (P _ (UN "io_bind") _, [_,_,v,k]) <- unApply tm
121− = do v' <- epic' env v
122− k' <- epic' env k
123− return (effect_ (k' @@ (effect_ v')))
124− | (P _ (UN "malloc") _, [_,size,t]) <- unApply tm
125− = do size' <- epic' env size
126− t' <- epic' env t
127− return $ malloc_ size' t'
128− | (P _ (UN "trace_malloc") _, [_,t]) <- unApply tm
129− = do t' <- epic' env t
130− return $ mallocTrace_ t'
131− | (P (DCon t a) n _, args) <- unApply tm
132− = epicCon env t a n args
133− epic' env (P (DCon t a) n _) = return $ con_ t
134− epic' env (P (TCon t a) n _) = return $ con_ t
135− epic' env (P _ n _) = return $ ref (ename n)
136− epic' env (V i) = return $ ref (env!!i)
137− epic' env (Bind n (Lam _) sc)
138− = do sc' <- epic' (aname n : env) sc
139− return $ term ([aname n], sc')
140− epic' env (Bind n (Let _ v) sc)
141− = do sc' <- epic' (aname n : env) sc
142− v' <- epic' env v
143− return $ let_ v' (aname n, sc')
144− epic' env (Bind _ _ _) = return impossible
145− epic' env (App f a) = do f' <- epic' env f
146− a' <- epic' env a
147− return (f' @@ a')
148− epic' env (Constant c) = epic c
149− epic' env Erased = return impossible
150− epic' env (TType _) = return impossible
151−
152− epicCon env t arity n args
153− | length args == arity = buildApp env (con_ t) args
154− | otherwise = let extra = satArgs (arity - length args) in
155− do sc' <- epicCon env t arity n
156− (args ++ map (\n -> P Bound n undefined) extra)
157− return $ term (map ename extra, sc')
158−
159− satArgs n = map (\i -> MN i "sat") [1..n]
160−
161− buildApp env e [] = return e
162− buildApp env e (x:xs) = do x' <- epic' env x
163− buildApp env (e @@ x') xs
164−
165−
166−doForeign :: Bool -> [TT Name] -> Idris E.Term
167−doForeign lazy (_ : fgn : args)
168− | (_, (Constant (Str fgnName) : fgnArgTys : ret : [])) <- unApply fgn
169− = let tys = getFTypes fgnArgTys
170− rty = mkEty' ret in
171− do args' <- mapM epic args
172− -- wrap it in a prim__IO
173− -- return $ con_ 0 @@ impossible @@
174− if lazy
175− then return $ lazy_ $ foreignL_ rty fgnName (zip args' tys)
176− else return $ lazy_ $ foreign_ rty fgnName (zip args' tys)
177− | otherwise = fail "Badly formed foreign function call"
178−
179−getFTypes :: TT Name -> [E.Type]
180−getFTypes tm = case unApply tm of
181− (nil, []) -> []
182− (cons, [ty, xs]) ->
183− let rest = getFTypes xs in
184− mkEty' ty : rest
185−
186−mkEty' (P _ (UN ty) _) = mkEty ty
187−mkEty' _ = tyAny
188−
189−mkEty "FInt" = tyInt
190−mkEty "FFloat" = tyFloat
191−mkEty "FChar" = tyChar
192−mkEty "FString" = tyString
193−mkEty "FPtr" = tyPtr
194−mkEty "FUnit" = tyUnit
195−
196−instance ToEpic Const where
197− epic (I i) = return (int i)
198− epic (BI i) = return (bigint i)
199− epic (Fl f) = return (float f)
200− epic (Str s) = return (str s)
201− epic (Ch c) = return (char c)
202− epic IType = return $ con_ 1
203− epic FlType = return $ con_ 2
204− epic ChType = return $ con_ 3
205− epic StrType = return $ con_ 4
206− epic PtrType = return $ con_ 5
207− epic BIType = return $ con_ 6
208−
209−instance ToEpic ([Name], SC) where
210− epic (args, tree) = do logLvl 3 $ "Compiling " ++ show args ++ "\n" ++ show tree
211− tree' <- epic tree
212− return $ term (map ename args, tree')
213−
214−instance ToEpic SC where
215− epic (Case n [ConCase _ i ns sc])
216− = epicLet n ns 0 sc
217− where
218− epicLet x [] _ sc = epic sc
219− epicLet x (n:ns) i sc
220− = do sc' <- epicLet x ns (i+1) sc
221− return $ let_ (ref (ename x) !. i) (ename n, sc')
222−
223− epic (STerm t) = epic t
224− epic (UnmatchedCase str) = return $ error_ str
225− epic (Case n alts) = do alts' <- mapM mkEpicAlt alts
226− return $ case_ (ref (ename n)) alts'
227− where
228− mkEpicAlt (ConCase n t args rhs) = do rhs' <- epic rhs
229− return $ con t (map ename args, rhs')
230− mkEpicAlt (ConstCase (I i) rhs) = do rhs' <- epic rhs
231− return $ constcase i rhs'
232− mkEpicAlt (ConstCase IType rhs) = do rhs' <- epic rhs
233− return $ defaultcase rhs'
234− mkEpicAlt (ConstCase c rhs)
235− = fail $ "Can only pattern match on integer constants (" ++ show c ++ ")"
236− mkEpicAlt (DefaultCase rhs) = do rhs' <- epic rhs
237− return $ defaultcase rhs'
238−
239−-}
src/Idris/IBC.hs−1
@@ -5,7 +5,6 @@ module Idris.IBC where
55import Core.Evaluate
66import Core.TT
77import Core.CaseTree
8−import Idris.Compiler
98import Idris.AbsSyntax
109import Idris.Imports
1110import Idris.Error
src/Idris/REPL.hs−18
@@ -9,7 +9,6 @@ import Idris.ElabDecls
99import Idris.ElabTerm
1010import Idris.Error
1111import Idris.Delaborate
12−import Idris.Compiler
1312import Idris.Prover
1413import Idris.Parser
1514import Idris.Primitives
@@ -166,8 +165,6 @@ ideslaveProcess fn (HNF t) = process fn (HNF t)
166165--input/output of the executed binary...
167166ideslaveProcess fn Execute = do process fn Execute
168167 iResult ""
169−ideslaveProcess fn (NewCompile f) = do process fn (NewCompile f)
170− iResult ""
171168ideslaveProcess fn (Compile codegen f) = do process fn (Compile codegen f)
172169 iResult ""
173170ideslaveProcess fn (LogLvl i) = do process fn (LogLvl i)
@@ -489,12 +486,6 @@ process fn Execute = do (m, _) <- elabVal toplevel False
489486 liftIO $ system tmpn
490487 return ()
491488 where fc = FC "main" 0
492−process fn (NewCompile f)
493− = do (m, _) <- elabVal toplevel False
494− (PApp fc (PRef fc (UN "run__IO"))
495− [pexp $ PRef fc (NS (UN "main") ["Main"])])
496− compileEpic f m
497− where fc = FC "main" 0
498489process fn (Compile codegen f)
499490 = do (m, _) <- elabVal toplevel False
500491 (PApp fc (PRef fc (UN "run__IO"))
@@ -596,7 +587,6 @@ parseArgs ("--log":lvl:ns) = OLogging (read lvl) : (parseArgs ns)
596587parseArgs ("--noprelude":ns) = NoPrelude : (parseArgs ns)
597588parseArgs ("--check":ns) = NoREPL : (parseArgs ns)
598589parseArgs ("-o":n:ns) = NoREPL : Output n : (parseArgs ns)
599−parseArgs ("-no":n:ns) = NoREPL : NewOutput n : (parseArgs ns)
600590parseArgs ("--typecase":ns) = TypeCase : (parseArgs ns)
601591parseArgs ("--typeintype":ns) = TypeInType : (parseArgs ns)
602592parseArgs ("--total":ns) = DefaultTotal : (parseArgs ns)
@@ -657,7 +647,6 @@ idrisMain opts =
657647 let idesl = Ideslave `elem` opts
658648 let runrepl = not (NoREPL `elem` opts)
659649 let output = opt getOutput opts
660− let newoutput = opt getNewOutput opts
661650 let ibcsubdir = opt getIBCSubDir opts
662651 let importdirs = opt getImportDir opts
663652 let bcs = opt getBC opts
@@ -723,9 +712,6 @@ idrisMain opts =
723712 when ok $ case output of
724713 [] -> return ()
725714 (o:_) -> process "" (Compile cgn o)
726− when ok $ case newoutput of
727− [] -> return ()
728− (o:_) -> process "" (NewCompile o)
729715 case script of
730716 Nothing -> return ()
731717 Just expr -> execScript expr
@@ -771,10 +757,6 @@ getOutput :: Opt -> Maybe String
771757getOutput (Output str) = Just str
772758getOutput _ = Nothing
773759
774−getNewOutput :: Opt -> Maybe String
775−getNewOutput (NewOutput str) = Just str
776−getNewOutput _ = Nothing
777−
778760getIBCSubDir :: Opt -> Maybe String
779761getIBCSubDir (IBCSubDir str) = Just str
780762getIBCSubDir _ = Nothing
src/Idris/REPLParser.hs−1
@@ -34,7 +34,6 @@ pCmd = try (do cmd ["q", "quit"]; eof; return Quit)
3434 <|> try (do cmd ["c", "compile"]; f <- identifier; eof; return (Compile ViaC f))
3535 <|> try (do cmd ["jc", "newcompile"]; f <- identifier; eof; return (Compile ViaJava f))
3636 <|> try (do cmd ["js", "javascript"]; f <- identifier; eof; return (Compile ViaJavaScript f))
37− <|> try (do cmd ["nc", "newcompile"]; f <- identifier; eof; return (NewCompile f))
3837 <|> try (do cmd ["m", "metavars"]; eof; return Metavars)
3938 <|> try (do cmd ["proofs"]; eof; return Proofs)
4039 <|> try (do cmd ["p", "prove"]; n <- pName; eof; return (Prove n))
src/RTS/Assembler.hsdeleted−8
@@ -1,8 +0,0 @@
1−module RTS.Assembler where
2−
3−import RTS.BCParser
4−import RTS.Bytecode
5−
6−bcAsm :: FilePath -> IO ()
7−bcAsm f = do bc <- parseFile f
8− print bc
src/RTS/BCParser.hsdeleted−96
@@ -1,96 +0,0 @@
1−module RTS.BCParser where
2−
3−import Core.CoreParser
4−import Core.TT
5−
6−import RTS.Bytecode
7−
8−import Text.ParserCombinators.Parsec
9−import Text.ParserCombinators.Parsec.Error
10−import Text.ParserCombinators.Parsec.Expr
11−import Text.ParserCombinators.Parsec.Language
12−import qualified Text.ParserCombinators.Parsec.Token as PTok
13−
14−import Data.List
15−import Control.Monad.State
16−import Debug.Trace
17−import Data.Maybe
18−import System.FilePath
19−
20−type TokenParser a = PTok.TokenParser a
21−
22−type BParser = GenParser Char ()
23−
24−lexer :: TokenParser ()
25−lexer = PTok.makeTokenParser idrisDef
26−
27−whiteSpace= PTok.whiteSpace lexer
28−lexeme = PTok.lexeme lexer
29−symbol = PTok.symbol lexer
30−natural = PTok.natural lexer
31−parens = PTok.parens lexer
32−semi = PTok.semi lexer
33−comma = PTok.comma lexer
34−identifier= PTok.identifier lexer
35−reserved = PTok.reserved lexer
36−operator = PTok.operator lexer
37−reservedOp= PTok.reservedOp lexer
38−integer = PTok.integer lexer
39−float = PTok.float lexer
40−strlit = PTok.stringLiteral lexer
41−chlit = PTok.charLiteral lexer
42−lchar = lexeme.char
43−
44−parseFile :: FilePath -> IO BCProg
45−parseFile fname = do fp <- readFile fname
46− case runParser pProgram () fname fp of
47− Left err-> fail (show err)
48− Right x -> return x
49−
50−pProgram :: BParser BCProg
51−pProgram = do fs <- many1 pFun
52− return $ BCProg fs
53−
54−pFun :: BParser (Name, Bytecode)
55−pFun = do n <- iName []
56− lchar ':'
57− bc <- many1 pInstruction
58− return (n, bc)
59−
60−pInstruction :: BParser BCOp
61−pInstruction =
62− try (do reserved "PUSH"; v <- pValue; return (PUSH v))
63− <|> try (do reserved "SLIDE"; v <- integer; return (SLIDE (fromInteger v)))
64− <|> try (do reserved "DISCARD"; v <- integer; return (DISCARD (fromInteger v)))
65− <|> try (do reserved "DISCARDINT"; v <- integer
66− return (DISCARDINT (fromInteger v)))
67− <|> try (do reserved "DISCARDFLOAT"; v <- integer
68− return (DISCARDFLOAT (fromInteger v)))
69− <|> try (do reserved "EVAL"; return (EVAL True))
70− <|> try (do reserved "EVAL_NOUPDATE"; return (EVAL False))
71− <|> try (do reserved "MKCON"; t <- integer; a <- integer;
72− return (MKCON (fromInteger t) (fromInteger a)))
73− <|> try (do reserved "MKTHUNK"; n <- iName []; arg <- integer; arity <- integer
74− return (MKTHUNK n (fromInteger arg) (fromInteger arity)))
75− <|> try (do reserved "MKUNIT"; return MKUNIT)
76− <|> try (do reserved "CALL"; n <- iName []; i <- integer;
77− return (CALL n (fromInteger i)))
78− <|> try (do reserved "ERROR"; s <- strlit; return (ERROR s))
79− <|> try (do reserved "SPLIT"; return SPLIT)
80− <|> try (do reserved "DUMP"; return DUMP)
81− <|> do reserved "CASE"; s <- sepBy1 pAlt (lchar '|')
82− def <- option Nothing (do reserved "default"; symbol "->";
83− bc <- many1 pInstruction
84− return (Just bc))
85− return (CASE s def)
86− where pAlt = try (do t <- integer; symbol "->"
87− bc <- many1 pInstruction
88− return (fromInteger t, bc))
89−
90−pValue :: BParser SValue
91−pValue = try (do x <- integer; lchar 'L'; return (VBigInt x))
92− <|> try (do x <- integer; return (VInt (fromInteger x)))
93− <|> try (do x <- float; return (VFloat x))
94− <|> try (do x <- strlit; return (VString x))
95− <|> try (do x <- chlit; return (VChar x))
96− <|> try (do lchar 'S'; x <- integer; return (VRef (fromInteger x)))
src/RTS/Bytecode.hsdeleted−114
@@ -1,114 +0,0 @@
1−module RTS.Bytecode where
2−
3−import Core.TT
4−import Core.CaseTree
5−import Core.Evaluate
6−
7−import Idris.AbsSyntax
8−import RTS.SC
9−
10−import Control.Monad.State
11−
12−type Local = Int
13−
14−data BAtom = BP Name Int | BL Local | BC Const
15− deriving Show
16−
17−-- Like SC, but with explicit evaluation, de Bruijn levels for locals, and all
18−-- intermediate values put into variables (which can be stored on the stack or
19−-- in registers when generating code)
20−
21−data Bytecode = BAtom BAtom
22− | BApp BAtom [BAtom]
23− | BTailApp BAtom [BAtom]
24− | BLazy BAtom [BAtom]
25− | BLet Local Bytecode Bytecode
26− | BFCall String CType [(BAtom, CType)]
27− | BCon Tag [BAtom]
28− | BCase Local [BAlt]
29− | BPrimOp SPrim [BAtom]
30− | BError String
31− | BGetArgs [Name] Bytecode -- get function arguments
32− | BReserve Int Bytecode -- reserve stack space, clear on exit
33− deriving Show
34−
35−data BAlt = BConCase Tag [Name] Int Bytecode
36− | BConstCase Const Bytecode
37− | BDefaultCase Bytecode
38− deriving Show
39−
40−bcdefs :: [(Name, SCDef)] -> [(Name, (Int, Bytecode))]
41−bcdefs xs = map (\ (n, s) -> (n, bc xs s)) xs
42−
43−bc all (SCDef args max c) = (length args,
44− BGetArgs (map fst args) (bcExp all max (length args) c))
45−
46−bcExp all v arity x
47− = let (code, max) = runState (bc' True arity x) v
48− space = max - arity in
49− if (space > 0) then BReserve space code else code
50− where
51− ref i = do s <- get
52− when (i > s) $ put i
53− next = do s <- get
54− put (s + 1)
55− return s
56− scarity n = case lookup n all of
57− Just (SCDef args _ _) -> length args
58−
59− bc' :: Bool -> Int -> SCExp -> State Int Bytecode
60− bc' tl d (SRef n) = if tl then return $ BTailApp (BP n (scarity n)) []
61− else return $ BApp (BP n (scarity n)) []
62− bc' tl d (SLoc i) = do ref i; return $ BAtom (BL i)
63− bc' tl d (SApp f args)
64− = do f' <- case f of
65− SRef n -> return $ BAtom (BP n (scarity n))
66− _ -> bc' False d f
67− args' <- mapM (bc' False d) args
68− let bapp = if tl then BTailApp else BApp
69− case f' of
70− BAtom r -> mkApp (\x -> bapp r x) args' []
71− bc -> do v <- next
72− mkApp (\x -> BLet v bc (bapp (BL v) x)) args' []
73− bc' tl d (SLazyApp f args) = do args' <- mapM (bc' False d) args
74− -- let bapp = if tl then BTailApp else BApp
75− mkApp (\x -> BLazy (BP f (scarity f)) x) args' []
76− bc' tl d (SLet n val sc) = do v' <- bc' False d val
77− sc' <- bc' False (d + 1) sc
78− return $ BLet d v' sc'
79− bc' tl d (SCon t args) = do args' <- mapM (bc' False d) args
80− mkApp (\x -> BCon t x) args' []
81− bc' tl d (SFCall c t args) = do args' <- mapM (bc' False d) (map fst args)
82− mkFApp c t (zip args' (map snd args)) []
83− bc' tl d (SConst c) = return $ BAtom (BC c)
84− bc' tl d (SError s) = return $ BError s
85− bc' tl d (SCase e alts) = do e' <- bc' False d e
86− alts' <- mapM (bcAlt tl d) alts
87− case e' of
88− BAtom (BL i) -> return $ BCase i alts'
89− bc -> do v <- next
90− return $ BLet v bc (BCase v alts')
91− bc' tl d (SPrimOp p args) = do args' <- mapM (bc' tl d) args
92− mkApp (\x -> BPrimOp p x) args' []
93−
94− mkApp ap [] locs = return $ ap locs
95− mkApp ap (BAtom (BL i) : as) locs = mkApp ap as (locs ++ [BL i])
96− mkApp ap (a : as) locs = do v <- next
97− app <- mkApp ap as (locs ++ [BL v])
98− return $ BLet v a app
99−
100− bcAlt tl d (SConCase t args e) = do e' <- bc' tl (d + length args) e
101− return $ BConCase t args d e'
102− bcAlt tl d (SConstCase i e) = do e' <- bc' tl d e
103− return $ BConstCase i e'
104− bcAlt tl d (SDefaultCase e) = do e' <- bc' tl d e
105− return $ BDefaultCase e'
106−
107− mkFApp c ty [] locs = return $ BFCall c ty locs
108− mkFApp c ty ((BAtom (BL i), t) : as) locs
109− = mkFApp c ty as (locs ++ [(BL i, t)])
110− mkFApp c ty ((a, t) : as) locs
111− = do v <- next
112− app <- mkFApp c ty as (locs ++ [(BL v, t)])
113− return $ BLet v a app
114−
src/RTS/CodegenC.hsdeleted−219
@@ -1,219 +0,0 @@
1−module RTS.CodegenC where
2−
3−import Core.TT
4−import Core.Evaluate
5−import Core.CaseTree
6−
7−import RTS.PreC
8−import RTS.SC
9−import RTS.Bytecode
10−
11−import Idris.AbsSyntax
12−
13−import Data.Char
14−import Data.List
15−import Control.Monad.State
16−import System.Process
17−import System.IO
18−import System.Directory
19−import System.Environment
20−import System.FilePath ((</>))
21−import Debug.Trace
22−
23−import Paths_idris
24−
25−compileC :: FilePath -> Term -> Idris ()
26−compileC f tm
27− = do checkMVs
28− let tmnames = namesUsed (STerm tm)
29− used <- mapM (allNames []) tmnames
30− i <- get
31− let entry = mainFn (idris_scprims i) tm
32− scs' <- mkDecls tm (concat used)
33− let scs = scs' ++ entry
34− objs <- getObjectFiles
35− libs <- getLibs
36− hdrs <- getHdrs
37− ddir <- liftIO $ getDataDir
38− -- if any includes exist in the data directory, use that
39− hdrs' <- liftIO $ mapM (inDir ddir) hdrs
40− let bcs = bcdefs scs
41− let pcs = preCdefs bcs
42− let code = cdefs pcs
43− liftIO $ writeFile (f ++ ".c") code
44− where checkMVs = do i <- get
45− case idris_metavars i \\ primDefs of
46− [] -> return ()
47− ms -> fail $ "There are undefined metavariables: " ++ show ms
48− inDir d h = do let f = d </> h
49− ex <- doesFileExist f
50− if ex then return f else return h
51−
52−
53−mainFn ps tm = toSC ps (MN 0 "_main", Function undefined tm)
54−
55−allNames :: [Name] -> Name -> Idris [Name]
56−allNames ns n | n `elem` ns = return []
57−allNames ns n = do i <- get
58− case lookupCtxt Nothing n (idris_callgraph i) of
59− [ns'] -> do more <- mapM (allNames (n:ns)) ns'
60− return (nub (n : concat more))
61− _ -> return [n]
62−
63−mkDecls :: Term -> [Name] -> Idris [(Name, SCDef)]
64−mkDecls t used
65− = do i <- getIState
66− let ds = filter (\ (n, d) -> n `elem` used) $ ctxtAlist (tt_ctxt i)
67− return $ concatMap (toSC (idris_scprims i)) ds
68−
69−cdefs :: [(Name, (Int, PreC))] -> String
70−cdefs xs = concatMap (\ (n, (i, c)) -> proto n) xs ++ "\n" ++
71− concatMap (\ (n, (i, c)) -> codegenC n i c) xs
72−
73−codegenC :: Name -> Int -> PreC -> String
74−codegenC n args prec
75− = "/* " ++ show prec ++ " */\n\n" ++
76− "void " ++ cname n ++ "(VM* vm) {\n" ++
77− indent 1 ++ "int i;\n" ++
78− concatMap (cg 1) prec ++ "}\n\n"
79−
80−indent i = take (i * 4) (repeat ' ')
81−
82−reg RVal = "vm->ret"
83−reg (LVar v) = "*(vm->valstack_ptr-" ++ show v ++ ")"
84−
85−catom :: CAtom -> String
86−catom (CL l) = reg (LVar l)
87−catom (CC (I i)) = "MKINT(" ++ show i ++ ")"
88−catom (CC v) = "const(" ++ show v ++ ")"
89−catom (CP n i) = "MKCLOSURE(" ++ cname n ++ ", " ++ show i ++ ")"
90−
91−off o (CL i) = CL (i + o)
92−off o x = x
93−
94−assignCon i r tag args
95− = indent i ++ reg r ++ " = MKCON(" ++ show tag ++ ", " ++
96− show (length args) ++ ");\n" ++
97− indent i ++ setArgs 0 args ++ "\n"
98− where
99− setArgs i [] = ""
100− setArgs i (a : as) = "SETARG(" ++ reg r ++ ", " ++
101− show i ++ ", " ++ catom a ++ "); "
102− ++ setArgs (i + 1) as
103−
104−assignFn i r f args
105− = indent i ++ "EXTEND(" ++ show (length args) ++ ");\n" ++
106− indent i ++ setArgs 1 (map (off (length args)) (reverse args)) ++ "\n" ++
107− indent i ++ cname f ++ "(vm);\n" ++
108− indent i ++ "CLEAR(" ++ show (length args) ++ ");\n" ++
109− case r of
110− RVal -> ""
111− r -> indent i ++ reg r ++ " = " ++ reg RVal ++ "\n"
112− where
113− setArgs i [] = ""
114− setArgs i (a : as) = reg (LVar i) ++ " = " ++ catom a ++ "; "
115− ++ setArgs (i + 1) as
116−
117−assignClosure i lazy r atom args
118− = indent i ++ reg r ++ " = APPLY(" ++ catom atom ++ ", " ++ show (length args) ++
119− ");\n" ++
120− indent i ++ setArgs 0 args ++ "\n" ++
121− if not lazy then
122− indent i ++ "if (READY(" ++ reg r ++ ")) {\n" ++
123− indent (i + 1) ++ reg r ++ " = EVAL(" ++ reg r ++ ");\n" ++
124− indent i ++ "} else {\n" ++
125− indent (i + 1) ++ "// do nothing \n" ++
126− indent i ++ "}\n"
127− else ""
128− where
129− setArgs i [] = ""
130− setArgs i (a : as) = "ADDARG(" ++ show i ++ ", " ++ reg r ++ ", " ++ catom a ++ "); "
131− ++ setArgs (i + 1) as
132−
133−doTailCall i d f args
134− = indent i ++ "EXTEND(" ++ show (length args) ++ ");\n" ++
135− indent i ++ setArgs 1 (map (off (length args)) (reverse args)) ++ "\n" ++
136− indent i ++ "SLIDE(" ++ show d ++ ", " ++ show (length args) ++ ");\n" ++
137− indent i ++ cname f ++ "(vm); return;\n"
138− where
139− setArgs i [] = ""
140− setArgs i (a : as) = reg (LVar i) ++ " = " ++ catom a ++ "; "
141− ++ setArgs (i + 1) as
142−
143−assignFCall i r cfn rt args
144− = indent i ++ reg r ++ " = " ++ fromC rt ++
145− "(" ++
146− cfn ++ "(" ++ showSep ", " (map toCa args) ++ "))" ++ ";\n"
147− where fromC (Just IType) = "MKINT"
148− fromC (Just StrType) = "MKSTR"
149− fromC _ = "MKVAL"
150−
151− toC (Just IType) = "GETINT"
152− toC (Just StrType) = "GETSTR"
153− toC _ = "GETVAL"
154−
155− toCa (a, ty) = toC ty ++ "(" ++ catom a ++ ")"
156−
157−assignPrimOp i r p args
158− = indent i ++ reg r ++ " = " ++ op p args ++ ";\n"
159−
160−cg :: Int -> CInst -> String
161−cg i (ASSIGN r (CCon t args))
162− = assignCon i r t args
163−cg i (ASSIGN r (CExactApp f args))
164− = assignFn i r f args
165−cg i (ASSIGN r (CApp f args))
166− = assignClosure i False r f args
167−cg i (ASSIGN r (CLazy f args))
168− = assignClosure i True r f args
169−cg i (ASSIGN r (CFCall cfn rt args))
170− = assignFCall i r cfn rt args
171−cg i (ASSIGN r (CPrimOp p args))
172− = assignPrimOp i r p args
173−cg i (ASSIGN r (CAtom e)) = indent i ++ reg r ++ " = " ++ catom e ++ ";\n"
174−cg i (RESERVE s) = indent i ++ "EXTEND(" ++ show s ++ ");\n"
175−cg i (CLEAR s) = indent i ++ "CLEAR(" ++ show s ++ ");\n"
176−cg i (EVAL e) = indent i ++ "EVAL(" ++ reg (LVar e) ++ ");\n"
177−cg i (PROJECT scr loc num)
178− = indent i ++ "PROJECT(" ++ reg (LVar scr) ++ ", " ++
179− reg (LVar loc) ++ ", " ++ show num ++ ");\n"
180−cg i (SWITCH v bs def)
181− = indent i ++ "switch(" ++ sval v ++ ") {\n" ++
182− concatMap branch bs ++
183− indent i ++ "default:\n" ++ concatMap (cg (i+1)) def ++
184− indent (i+1) ++ "break;\n" ++
185− indent i ++ "};\n"
186− where sval (CTag l) = "TAG(" ++ reg (LVar l) ++ ")"
187− sval (CIntVal l) = "GETINT(" ++ reg (LVar l) ++ ")"
188−
189− branch (t, c) = indent i ++ "case " ++ show t ++ ":\n" ++
190− concatMap (cg (i+1)) c ++
191− indent (i+1) ++ "break;\n"
192−cg i (TAILCALL d f args) = assignClosure i False RVal f args -- doTailClosure i d f args
193−cg i (TAILCALLEXACT d f args) = doTailCall i d f args
194−cg i (ERROR s) = indent i ++ "ERROR(" ++ show s ++ ")\n";
195−cg i x = trace (show x ++ " not done") $ indent i ++ "NOT DONE;\n"
196−
197−cname :: Name -> String
198−cname (UN s) = "_I_" ++ toC s
199−cname (MN i s) = "_M_I_" ++ show i ++ "_" ++ toC s
200−cname (NS n ss) = "_" ++ showSep "_" (map toC ss) ++ cname n
201−
202−toC s = concatMap special s
203− where special c | isAlphaNum c = [c]
204− | c == '_' = "__"
205− | otherwise = "_" ++ show (fromEnum c) ++ "_"
206−
207−proto :: Name -> String
208−proto n = "void " ++ cname n ++ "(VM* vm);\n"
209−
210−op AddI [l, r] = "INTOP(+, " ++ catom l ++ ", " ++ catom r ++ ")"
211−op SubI [l, r] = "INTOP(-, " ++ catom l ++ ", " ++ catom r ++ ")"
212−op MulI [l, r] = "INTOP(*, " ++ catom l ++ ", " ++ catom r ++ ")"
213−op DivI [l, r] = "INTOP(/, " ++ catom l ++ ", " ++ catom r ++ ")"
214−
215−op ConcatS [l,r] = "CONCAT(" ++ catom l ++ ", " ++ catom r ++ ")"
216−
217−op o args = trace (show (op, args) ++ " operator undefined")
218− $ "0 /* Op not defined " ++ show o ++ " */"
219−
src/RTS/PreC.hsdeleted−103
@@ -1,103 +0,0 @@
1−module RTS.PreC where
2−
3−-- A representation close to the code we'll be outputting
4−
5−import Core.TT
6−
7−import RTS.Bytecode
8−import RTS.SC
9−
10−import Control.Monad.State
11−
12−data CAtom = CP Name Int | CL Local | CC Const
13− deriving Show
14−
15−data CExp = CAtom CAtom
16− | CApp CAtom [CAtom]
17− | CExactApp Name [CAtom]
18− | CLazy CAtom [CAtom]
19− | CFCall String CType [(CAtom, CType)]
20− | CPrimOp SPrim [CAtom]
21− | CCon Tag [CAtom]
22− deriving Show
23−
24−data CVal = CTag Local | CIntVal Local
25− deriving Show
26−
27−-- Assignment is to the return value, or some local reference
28−-- Local reference <n> means <top of stack> - <n>
29−data Reg = RVal | LVar Local
30− deriving Show
31−
32−data CInst = ASSIGN Reg CExp
33− | RESERVE Int
34− | CLEAR Int
35− | EVAL Local
36− | PROJECT Local Local Int
37− | SWITCH CVal [(Int, PreC)] PreC
38− | ERROR String
39− | TAILCALL Int -- number of stack items to clear
40− CAtom [CAtom]
41− | TAILCALLEXACT Int Name [CAtom]
42− deriving Show
43−
44−type PreC = [CInst]
45−
46−preCdefs :: [(Name, (Int, Bytecode))] -> [(Name, (Int, PreC))]
47−preCdefs xs = map (\ (n, (i, b)) -> (n, preC xs b)) xs
48−
49−atom res (BP n i) = CP n i
50−atom res (BL n) = CL (res - n)
51−atom res (BC c) = CC c
52−
53−preC :: [(Name, (Int, Bytecode))] -> Bytecode -> (Int, PreC)
54−preC all (BGetArgs ns bc) = (length ns, pc RVal (length ns) bc)
55− where arity n = do (i, b) <- lookup n all
56− return i
57− exact (BP n i) as = i == length as
58− exact _ as = False
59−
60− getName (BP n i) = n
61−
62− pc loc d (BAtom b) = [ASSIGN loc (CAtom (atom d b))]
63− pc loc d (BApp f as)
64− | exact f as = [ASSIGN loc (CExactApp (getName f) (map (atom d) as))]
65− | otherwise = [ASSIGN loc (CApp (atom d f) (map (atom d) as))]
66− pc loc d (BTailApp f as)
67− | exact f as = [TAILCALLEXACT d (getName f) (map (atom d) as)]
68− | otherwise = [TAILCALL d (atom d f) (map (atom d) as)]
69− pc loc d (BLazy f as) = [ASSIGN loc (CLazy (atom d f) (map (atom d) as))]
70− pc loc d (BLet x val sc) = pc (LVar (d - x)) d val ++ pc loc d sc
71− pc loc d (BFCall c t args)
72− = [ASSIGN loc (CFCall c t (map (\ (a, ty) -> (atom d a, ty)) args))]
73− pc loc d (BCon t args) = [ASSIGN loc (CCon t (map (atom d) args))]
74− pc loc d (BPrimOp s args)
75− = [ASSIGN loc (CPrimOp s (map (atom d) args))]
76− pc loc d (BError s) = [ERROR s]
77− pc loc d (BCase l alts)
78− = EVAL (d - l) :
79− SWITCH (caseTy alts (d - l))
80− (map (pcAlt loc d l) (filter notDef alts))
81− (getDefault loc d alts) : []
82− pc loc d (BReserve s bc) = RESERVE s : pc loc (d + s) bc ++
83− [CLEAR s]
84−
85− notDef (BDefaultCase _) = False
86− notDef _ = True
87−
88− pcAlt loc d var (BConCase t args l bc)
89− = (t, PROJECT (d - var) (d - l) (length args) :
90− pc loc d bc)
91− pcAlt loc d var (BConstCase (I c) bc)
92− = (c, pc loc d bc)
93−
94− getDefault loc d (BDefaultCase bc : _) = pc loc d bc
95− getDefault loc d (_ : xs) = getDefault loc d xs
96− getDefault loc d [] = []
97−
98− caseTy (BConCase _ _ _ _ : _) = CTag
99− caseTy (BConstCase _ _ : _) = CIntVal
100− caseTy (_ : xs) = caseTy xs
101− caseTy [] = CTag
102−
103−
src/RTS/SC.hsdeleted−249
@@ -1,249 +0,0 @@
1−{-# LANGUAGE PatternGuards #-}
2−
3−module RTS.SC where
4−
5−import Core.TT
6−import Core.Evaluate
7−import Core.CaseTree
8−
9−import Control.Monad.State
10−
11−type CType = Maybe Const
12−type Tag = Int
13−type Arity = Int
14−
15−data SCDef = SCDef { sc_args :: [(Name, CType)],
16− sc_locals :: Int,
17− sc_def :: SCExp }
18− deriving Show
19−
20−data SCExp = SRef Name
21− | SLoc Int -- de Bruijn level
22− | SApp SCExp [SCExp]
23− | SLazyApp Name [SCExp]
24− | SLet Name SCExp SCExp
25− | SFCall String CType [(SCExp, CType)] -- foreign call with types
26− | SCon Tag [SCExp] -- constructor, assume saturated (forcing does this)
27− | SConst Const
28− | SError String
29− | SCase SCExp [SAlt]
30− | SPrimOp SPrim [SCExp]
31− deriving Show
32−
33−data SPrim = AddI | SubI | MulI | DivI
34− | EqI | LtI | LteI | GtI | GteI
35− | EqC | LtC | LteC | GtC | GteC
36− | AddBI | SubBI | MulBI | DivBI
37− | EqBI | LtBI | LteBI | GtBI | GteBI
38− | AddF | SubF | MulF | DivF
39− | EqF | LtF | LteF | GtF | GteF
40− | ConcatS | EqS | LtS
41− | StoI | ItoS
42− | CtoI | ItoC
43− | ItoBI | BItoI
44− | StoBI | BItoS
45− | StoF | FtoS
46− | ItoF | FtoI
47− | ExpF | LogF | SinF | CosF | TanF
48− | ASinF | ACosF | ATanF | SqrtF | FloorF | CeilF
49− | HeadS | TailS | ConsS | IndexS | RevS
50− | BelieveMe
51− deriving Show
52−
53−data SAlt = SConCase Tag [Name] SCExp
54− | SConstCase Const SCExp
55− | SDefaultCase SCExp
56− deriving Show
57−
58−type Prims = [(Name, ([CType], CType, SPrim))]
59−
60−sInt, sFloat, sChar, sString, sPtr, sBigInt :: CType
61−sInt = Just IType
62−sFloat = Just FlType
63−sChar = Just ChType
64−sString = Just StrType
65−sPtr = Just PtrType
66−sBigInt = Just BIType
67−
68−type SCState = ([(Name, SCDef)], Name)
69−
70−toSC :: Prims -> (Name, Def) -> [(Name, SCDef)]
71−toSC prims (n, d)
72− = case lookup n prims of
73− Nothing -> sclift (n, d)
74− Just (args, rt, op) ->
75− let anames = zipWith mkA args [0..] in
76− [(n, SCDef anames (length anames)
77− (SPrimOp op (take (length anames)
78− (map SLoc [0..]))))]
79− where mkA t i = (MN i "primArg", t)
80−
81−sclift :: (Name, Def) -> [(Name, SCDef)]
82−sclift (n, d) = fst (execState (sc [] d) ([], n))
83−
84−add (n, SCDef args _ def)
85− = do (xs, b) <- get
86− let (def', maxloc) = dbl (map fst args) def
87− put ((n, SCDef args maxloc def') : xs, b)
88−
89−nextSC :: State SCState Name
90−nextSC = do (xs, n) <- get
91− let n' = getNext n
92− put (xs, n')
93− return n'
94− where getNext (UN n) = MN 0 n
95− getNext (MN i n) = MN (i+1) n
96− getNext (NS n ns) = NS (getNext n) ns
97−
98−sname :: State SCState Name
99−sname = do (xs, n) <- get
100− return n
101−
102−class Lift s t | s -> t where
103− sc :: [Name] -> s -> State SCState t
104−
105−instance Lift Def () where
106− sc env (Function ty tm) = do n <- sname
107− tm' <- sc env tm
108− add (n, SCDef (zip env (repeat Nothing)) 0 tm')
109− sc env (TyDecl _ _) = do n <- sname
110− add (n, SCDef (zip env (repeat Nothing)) 0
111− (SError $ "undefined " ++ show n))
112− sc env (Operator _ _ _) = return ()
113− sc env (CaseOp _ _ _ _ _ args cases)
114− = do n <- sname
115− cases' <- sc (env ++ args) cases
116− add (n, SCDef (zip (env ++ args) (repeat Nothing)) 0 cases')
117−
118−instance Lift (TT Name) SCExp where
119− sc env (P (DCon t a) _ _) = return $ SCon t []
120− sc env (P (TCon t a) _ _) = return $ SCon t []
121− sc env (P _ n _) = return $ SRef n
122− sc env (V i) = return $ SRef (env!!i)
123− sc env fn@(App _ _)
124− = do let (f, args) = unApply fn
125− prim <- sPrim env f args
126− case prim of
127− Just t -> return t
128− Nothing -> do args' <- mapM (sc env) args
129− case f of
130− (P (DCon t a) _ _) -> return $ SCon t args'
131− (P (TCon t a) _ _) -> return $ SCon t args'
132− _ -> do f' <- sc env f
133− return $ SApp f' args'
134− sc env (Constant c) = return $ SConst c
135− sc env (Bind n (Let _ v) e) = do v' <- sc env v
136− e' <- sc (n : env) e
137− return (SLet n v' e')
138− sc env (Bind n (Lam _) e) = scLam [n] e
139− where
140− scLam args (Bind n (Lam _) e) = scLam (n : args) e
141− scLam args x = do x' <- sc (args ++ env) x
142− fn <- nextSC
143− add (fn, SCDef (zip (env ++ reverse args) (repeat Nothing)) 0 x')
144− return $ SApp (SRef fn) (map SRef env)
145− sc _ _ = return $ SConst (I 42424242)
146−
147−
148−instance Lift SC SCExp where
149− sc env (Case n alts) = do alts' <- mapM (sc env) alts
150− return (SCase (SRef n) alts')
151− sc env (STerm t) = do t' <- sc env t
152− return t'
153− sc env (UnmatchedCase s) = return (SError s)
154−
155−instance Lift CaseAlt SAlt where
156− sc env (ConCase n t args e) = do e' <- sc env e
157− return (SConCase t args e')
158− sc env (ConstCase c e) = do e' <- sc env e
159− return (SConstCase c e')
160− sc env (DefaultCase e) = do e' <- sc env e
161− return (SDefaultCase e')
162−
163−-- Deal with primitives: mkForeign, -- lazy, prim__IO, io_bind, malloc, trace_malloc
164−
165−sPrim :: [Name] -> TT Name -> [TT Name] -> State SCState (Maybe SCExp)
166−sPrim env (P _ (UN "mkForeign") _) args = do x <- doForeign env args
167− return (Just x)
168−sPrim env (P _ (UN "lazy") _) [_, arg]
169− = do arg' <- sc env arg
170− fn <- nextSC
171− add (fn, SCDef (zip env (repeat Nothing)) 0 arg')
172− return $ Just $ SLazyApp fn (map SRef env)
173−sPrim env (P _ (UN "prim__IO") _) [v] = do v' <- sc env v
174− return $ Just v'
175−sPrim env (P _ (UN "io_bind") _) [_,_,v,k]
176− = do v' <- sc env v
177− k' <- sc env k
178− return $ Just $ SApp k'[v']
179−sPrim env f args = return Nothing
180−
181−doForeign env (_ : fgn : args)
182− | (_, (Constant (Str fgnName) : fgnArgTys : ret : [])) <- unApply fgn
183− = let tys = getFTypes fgnArgTys
184− rty = mkEty' ret in
185− do args' <- mapM (sc env) args
186− return $ SFCall fgnName rty (zip args' tys)
187− | otherwise = fail "Badly formed foreign function call"
188−
189−getFTypes :: TT Name -> [CType]
190−getFTypes tm = case unApply tm of
191− (nil, []) -> []
192− (cons, [ty, xs]) ->
193− let rest = getFTypes xs in
194− mkEty' ty : rest
195−
196−mkEty' (P _ (UN ty) _) = mkEty ty
197−mkEty' _ = Nothing
198−
199−mkEty "FInt" = Just IType
200−mkEty "FFloat" = Just FlType
201−mkEty "FChar" = Just ChType
202−mkEty "FString" = Just StrType
203−mkEty "FPtr" = Just PtrType
204−mkEty "FUnit" = Just VoidType
205−
206−-- Convert local variable names to de Bruijn levels, return the biggest
207−
208−dbl :: [Name] -> SCExp -> (SCExp, Int)
209−dbl ns exp = runState (db' (extends [] ns) exp) (length ns) where
210− db' :: [(Name, Int)] -> SCExp -> State Int SCExp
211− db' env (SRef n) = case lookup n env of
212− Just i -> do ref i; return $ SLoc i
213− Nothing -> return $ SRef n
214− db' env (SApp f args) = do f' <- db' env f
215− args' <- mapM (db' env) args
216− return $ SApp f' args'
217− db' env (SLazyApp n args) = do args' <- mapM (db' env) args
218− return $ SLazyApp n args'
219− db' env (SLet n v sc) = do v' <- db' env v
220− sc' <- db' (extend env n) sc
221− ref (length env + 1)
222− return $ SLet n v' sc'
223− db' env (SFCall n t args) = do args' <- mapM (\ (e,t) -> do e' <- db' env e
224− return (e', t)) args
225− return $ SFCall n t args'
226− db' env (SCon t args) = do args' <- mapM (db' env) args
227− return $ SCon t args'
228− db' env (SCase n alts) = do n' <- db' env n
229− alts' <- mapM (dbAlt env) alts
230− return $ SCase n' alts'
231− db' env (SPrimOp p args) = do args' <- mapM (db' env) args
232− return $ SPrimOp p args'
233− db' env x = return x
234−
235− dbAlt env (SConCase t ns exp) = do exp' <- db' (extends env ns) exp
236− ref (length env + length ns)
237− return $ SConCase t ns exp'
238− dbAlt env (SConstCase c e) = do e' <- db' env e
239− return $ SConstCase c e'
240− dbAlt env (SDefaultCase e) = do e' <- db' env e
241− return $ SDefaultCase e'
242−
243− extend env n = env ++ [(n, length env)]
244− extends env [] = env
245− extends env (n : ns) = extends (extend env n) ns
246−
247− ref i = do s <- get
248− when (i > s) $ put i
249−