@@ -25,30 +25,85 @@ returns) are stored.
25data Reg = RVal | L Int | T Int | Tmp
25data Reg = RVal | L Int | T Int | Tmp
26 deriving (Show, Eq)
26 deriving (Show, Eq)
28−data BC = ASSIGN Reg Reg
30+ ASSIGN Reg Reg
29− | ASSIGNCONST Reg Const
33+ | ASSIGNCONST Reg Const
35+ -- reg1 = reg2 (same as assign, it seems)
30− | UPDATE Reg Reg
36+ | UPDATE Reg Reg
38+ -- reg = constructor, where constructor consists of a tag and
39+ -- values from registers, e.g. (cons tag args)
31− | MKCON Reg Int [Reg]
40+ | MKCON Reg Int [Reg]
32− | CASE Bool -- definitely a constructor, no need to check, if true
41+
42+ -- Matching on value of reg: usually (but not always) there are
43+ -- constructors, hence "Int" for patterns (that's a tag on which
44+ -- we should match), and the following [BC] is just a list of
45+ -- instructions for the corresponding case. The last argument is
46+ -- for default case. When it's not necessary a constructor in the
47+ -- reg, the Bool should be False, indicating that it's not safe to
48+ -- work with that as with a constructor, so a check should be
49+ -- added. If it's not a constructor, default case should be used.
33− Reg [(Int, [BC])] (Maybe [BC])
51+ Reg [(Int, [BC])] (Maybe [BC])
34− | PROJECT Reg Int Int -- get all args from reg, put them from Int onwards
52+
53+ -- get a value from register, which should be a constructor, and
54+ -- put its arguments into the stack, starting from (base + int1)
55+ -- and onwards; second Int provides arity
56+ | PROJECT Reg Int Int
35− | PROJECTINTO Reg Reg Int -- project argument from one reg into another
59+ | PROJECTINTO Reg Reg Int -- project argument from one reg into another
61+ -- same as CASE, but there's an exact value (not constructor) in reg
36− | CONSTCASE Reg [(Const, [BC])] (Maybe [BC])
62+ | CONSTCASE Reg [(Const, [BC])] (Maybe [BC])
64+ -- just call a function, passing MYOLDBASE (see below) to it
37− | CALL Name
65+ | CALL Name
67+ -- same, perhaps exists just for TCO
38− | TAILCALL Name
68+ | TAILCALL Name
70+ -- set reg to (apply string args), 2nd argument could be ignored,
71+ -- types could be ignored if they are not required
39− | FOREIGNCALL Reg FLang FType String [(FType, Reg)]
72+ | FOREIGNCALL Reg FLang FType String [(FType, Reg)]
40− | SLIDE Int -- move this number from TOP to BASE
74+ -- move this number of elements from TOP to BASE
77+ -- set BASE = OLDBASE
41− | REBASE -- set BASE = OLDBASE
78+ | REBASE
42− | RESERVE Int -- reserve n more stack items
79+
43− -- (i.e. check there's space, grow if necessary)
80+ -- reserve n more stack items (i.e. check there's space, grow if
44− | ADDTOP Int -- move the top of stack up
84+ -- move the top of stack up
87+ -- set TOP = BASE + n
45− | TOPBASE Int -- set TOP = BASE + n
88+ | TOPBASE Int
90+ -- set BASE = TOP + n
46− | BASETOP Int -- set BASE = TOP + n
91+ | BASETOP Int
93+ -- set MYOLDBASE = BASE, where MYOLDBASE is a function-local
94+ -- variable, set to OLDBASE by default, and passed on function
95+ -- call to called functions as their OLDBASE
47− | STOREOLD -- set OLDBASE = BASE
96+ | STOREOLD
98+ -- reg = apply primitive_function args
48− | OP Reg PrimFn [Reg]
99+ | OP Reg PrimFn [Reg]
49− | NULL Reg -- clear reg
102+ | NULL Reg
50− | ERROR String
105+ | ERROR String
51− deriving Show
106+ deriving Show
53toBC :: (Name, SDecl) -> (Name, [BC])
108toBC :: (Name, SDecl) -> (Name, [BC])
54toBC (n, SFun n' args locs exp)
109toBC (n, SFun n' args locs exp)