@@ -25,30 +25,85 @@ returns) are stored.
2525data Reg = RVal | L Int | T Int | Tmp
2626 deriving (Show, Eq)
2727
28−data BC = ASSIGN Reg Reg
29− | ASSIGNCONST Reg Const
30− | UPDATE Reg Reg
31− | MKCON Reg Int [Reg]
32− | CASE Bool -- definitely a constructor, no need to check, if true
33− Reg [(Int, [BC])] (Maybe [BC])
34− | PROJECT Reg Int Int -- get all args from reg, put them from Int onwards
35− | PROJECTINTO Reg Reg Int -- project argument from one reg into another
36− | CONSTCASE Reg [(Const, [BC])] (Maybe [BC])
37− | CALL Name
38− | TAILCALL Name
39− | FOREIGNCALL Reg FLang FType String [(FType, Reg)]
40− | SLIDE Int -- move this number from TOP to BASE
41− | REBASE -- set BASE = OLDBASE
42− | RESERVE Int -- reserve n more stack items
43− -- (i.e. check there's space, grow if necessary)
44− | ADDTOP Int -- move the top of stack up
45− | TOPBASE Int -- set TOP = BASE + n
46− | BASETOP Int -- set BASE = TOP + n
47− | STOREOLD -- set OLDBASE = BASE
48− | OP Reg PrimFn [Reg]
49− | NULL Reg -- clear reg
50− | ERROR String
51− deriving Show
28+data BC =
29+ -- reg1 = reg2
30+ ASSIGN Reg Reg
31+
32+ -- reg = const
33+ | ASSIGNCONST Reg Const
34+
35+ -- reg1 = reg2 (same as assign, it seems)
36+ | UPDATE Reg Reg
37+
38+ -- reg = constructor, where constructor consists of a tag and
39+ -- values from registers, e.g. (cons tag args)
40+ | MKCON Reg Int [Reg]
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.
50+ | CASE Bool
51+ Reg [(Int, [BC])] (Maybe [BC])
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
57+
58+ -- probably not used
59+ | PROJECTINTO Reg Reg Int -- project argument from one reg into another
60+
61+ -- same as CASE, but there's an exact value (not constructor) in reg
62+ | CONSTCASE Reg [(Const, [BC])] (Maybe [BC])
63+
64+ -- just call a function, passing MYOLDBASE (see below) to it
65+ | CALL Name
66+
67+ -- same, perhaps exists just for TCO
68+ | TAILCALL Name
69+
70+ -- set reg to (apply string args), 2nd argument could be ignored,
71+ -- types could be ignored if they are not required
72+ | FOREIGNCALL Reg FLang FType String [(FType, Reg)]
73+
74+ -- move this number of elements from TOP to BASE
75+ | SLIDE Int
76+
77+ -- set BASE = OLDBASE
78+ | REBASE
79+
80+ -- reserve n more stack items (i.e. check there's space, grow if
81+ -- necessary)
82+ | RESERVE Int
83+
84+ -- move the top of stack up
85+ | ADDTOP Int
86+
87+ -- set TOP = BASE + n
88+ | TOPBASE Int
89+
90+ -- set BASE = TOP + n
91+ | BASETOP Int
92+
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
96+ | STOREOLD
97+
98+ -- reg = apply primitive_function args
99+ | OP Reg PrimFn [Reg]
100+
101+ -- clear reg
102+ | NULL Reg
103+
104+ -- throw an error
105+ | ERROR String
106+ deriving Show
52107
53108toBC :: (Name, SDecl) -> (Name, [BC])
54109toBC (n, SFun n' args locs exp)