3-- A representation close to the code we'll be outputting
10import Control.Monad.State
12data CAtom = CP Name Int | CL Local | CC Const
15data CExp = CAtom CAtom
17 | CExactApp Name [CAtom]
19 | CFCall String CType [(CAtom, CType)]
20 | CPrimOp SPrim [CAtom]
24data CVal = CTag Local | CIntVal Local
27-- Assignment is to the return value, or some local reference
28-- Local reference <n> means <top of stack> - <n>
29data Reg = RVal | LVar Local
32data CInst = ASSIGN Reg CExp
36 | PROJECT Local Local Int
37 | SWITCH CVal [(Int, PreC)] PreC
39 | TAILCALL Int -- number of stack items to clear
41 | TAILCALLEXACT Int Name [CAtom]
46preCdefs :: [(Name, (Int, Bytecode))] -> [(Name, (Int, PreC))]
47preCdefs xs = map (\ (n, (i, b)) -> (n, preC xs b)) xs
49atom res (BP n i) = CP n i
50atom res (BL n) = CL (res - n)
53preC :: [(Name, (Int, Bytecode))] -> Bytecode -> (Int, PreC)
54preC all (BGetArgs ns bc) = (length ns, pc RVal (length ns) bc)
55 where arity n = do (i, b) <- lookup n all
57 exact (BP n i) as = i == length as
62 pc loc d (BAtom b) = [ASSIGN loc (CAtom (atom d b))]
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)
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 ++
85 notDef (BDefaultCase _) = False
88 pcAlt loc d var (BConCase t args l bc)
89 = (t, PROJECT (d - var) (d - l) (length args) :
91 pcAlt loc d var (BConstCase (I c) bc)
94 getDefault loc d (BDefaultCase bc : _) = pc loc d bc
95 getDefault loc d (_ : xs) = getDefault loc d xs
96 getDefault loc d [] = []
98 caseTy (BConCase _ _ _ _ : _) = CTag
99 caseTy (BConstCase _ _ : _) = CIntVal
100 caseTy (_ : xs) = caseTy xs
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