1module IRTS.Bytecode where
3-- Bytecode for a stack based VM (e.g. for generating C code with an accurate
8import IRTS.Defunctionalise
14BASE: Current stack frame's base
16OLDBASE: Passed in to each function, the previous stack frame's base
18L i refers to the stack item at BASE + i
19T i refers to the stack item at TOP + i
21RVal is a register in which computed values (essentially, what a function
26data Reg = RVal | L Int | T Int | Tmp
34 | ASSIGNCONST Reg Const
36 -- reg1 = reg2 (same as assign, it seems)
39 -- reg = constructor, where constructor consists of a tag and
40 -- values from registers, e.g. (cons tag args)
41 -- the 'Maybe Reg', if set, is a register which can be overwritten
42 -- (i.e. safe for mutable update), though this can be ignored
43 | MKCON Reg (Maybe Reg) Int [Reg]
45 -- Matching on value of reg: usually (but not always) there are
46 -- constructors, hence "Int" for patterns (that's a tag on which
47 -- we should match), and the following [BC] is just a list of
48 -- instructions for the corresponding case. The last argument is
49 -- for default case. When it's not necessary a constructor in the
50 -- reg, the Bool should be False, indicating that it's not safe to
51 -- work with that as with a constructor, so a check should be
52 -- added. If it's not a constructor, default case should be used.
54 Reg [(Int, [BC])] (Maybe [BC])
56 -- get a value from register, which should be a constructor, and
57 -- put its arguments into the stack, starting from (base + int1)
58 -- and onwards; second Int provides arity
62 | PROJECTINTO Reg Reg Int -- project argument from one reg into another
64 -- same as CASE, but there's an exact value (not constructor) in reg
65 | CONSTCASE Reg [(Const, [BC])] (Maybe [BC])
67 -- just call a function, passing MYOLDBASE (see below) to it
70 -- same, perhaps exists just for TCO
73 -- set reg to (apply string args),
74 | FOREIGNCALL Reg FDesc FDesc [(FDesc, Reg)]
76 -- move this number of elements from TOP to BASE
82 -- reserve n more stack items (i.e. check there's space, grow if
86 -- move the top of stack up
95 -- set MYOLDBASE = BASE, where MYOLDBASE is a function-local
96 -- variable, set to OLDBASE by default, and passed on function
97 -- call to called functions as their OLDBASE
100 -- reg = apply primitive_function args
101 | OP Reg PrimFn [Reg]
110toBC :: (Name, SDecl) -> (Name, [BC])
111toBC (n, SFun n' args locs exp)
112 = (n, reserve locs ++ bc RVal exp True)
114 reserve n = [RESERVE n, ADDTOP n]
116clean True = [TOPBASE 0, REBASE]
119bc :: Reg -> SExp -> Bool -> -- returning
121bc reg (SV (Glob n)) r = bc reg (SApp False n []) r
122bc reg (SV (Loc i)) r = assign reg (L i) ++ clean r
123bc reg (SApp False f vs) r =
125 then moveReg 0 vs ++ [STOREOLD, BASETOP 0, CALL f] ++ ret
126 else RESERVE argCount : moveReg 0 vs ++
127 [STOREOLD, BASETOP 0, ADDTOP argCount, CALL f] ++ ret
129 ret = assign reg RVal ++ clean r
131bc reg (SApp True f vs) r
132 = RESERVE (length vs) : moveReg 0 vs
133 ++ [SLIDE (length vs), TOPBASE (length vs), TAILCALL f]
134bc reg (SForeign t fname args) r
135 = FOREIGNCALL reg t fname (map farg args) : clean r
136 where farg (ty, Loc i) = (ty, L i)
137bc reg (SLet (Loc i) e sc) r = bc (L i) e False ++ bc reg sc r
138bc reg (SUpdate (Loc i) sc) r = bc reg sc False ++ [ASSIGN (L i) reg]
140-- bc reg (SUpdate x sc) r = bc reg sc r -- can't update, just do it
141bc reg (SCon atloc i _ vs) r
142 = MKCON reg (getAllocLoc atloc) i (map getL vs) : clean r
143 where getL (Loc x) = L x
144 getAllocLoc (Just (Loc x)) = Just (L x)
145 getAllocLoc _ = Nothing
146bc reg (SProj (Loc l) i) r = PROJECTINTO reg (L l) i : clean r
147bc reg (SConst i) r = ASSIGNCONST reg i : clean r
148bc reg (SOp p vs) r = OP reg p (map getL vs) : clean r
149 where getL (Loc x) = L x
150bc reg (SError str) r = [ERROR str]
151bc reg SNothing r = NULL reg : clean r
152bc reg (SCase up (Loc l) alts) r
153 | isConst alts = constCase reg (L l) alts r
154 | otherwise = conCase True reg (L l) alts r
155bc reg (SChkCase (Loc l) alts) r
156 = conCase False reg (L l) alts r
157bc reg t r = error $ "Can't compile " ++ show t
160isConst (SConstCase _ _ : xs) = True
161isConst (SConCase _ _ _ _ _ : xs) = False
162isConst (_ : xs) = False
165moveReg off (Loc x : xs) = assign (T off) (L x) ++ moveReg (off + 1) xs
167assign r1 r2 | r1 == r2 = []
168 | otherwise = [ASSIGN r1 r2]
170conCase safe reg l xs r = [CASE safe l (mapMaybe (caseAlt l reg r) xs)
171 (defaultAlt reg xs r)]
173constCase reg l xs r = [CONSTCASE l (mapMaybe (constAlt l reg r) xs)
174 (defaultAlt reg xs r)]
176caseAlt l reg r (SConCase lvar tag _ args e)
177 = Just (tag, PROJECT l lvar (length args) : bc reg e r)
178caseAlt l reg r _ = Nothing
180constAlt l reg r (SConstCase c e)
181 = Just (c, bc reg e r)
182constAlt l reg r _ = Nothing
184defaultAlt reg [] r = Nothing
185defaultAlt reg (SDefaultCase e : _) r = Just (bc reg e r)
186defaultAlt reg (_ : xs) r = defaultAlt reg xs r
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