@@ -25,30 +25,88 @@ 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 (Maybe 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+ -- the 'Maybe Reg', if set, is a register which can be overwritten
41+ -- (i.e. safe for mutable update), though this can be ignored
42+ | MKCON Reg (Maybe Reg) Int [Reg]
43+
44+ -- Matching on value of reg: usually (but not always) there are
45+ -- constructors, hence "Int" for patterns (that's a tag on which
46+ -- we should match), and the following [BC] is just a list of
47+ -- instructions for the corresponding case. The last argument is
48+ -- for default case. When it's not necessary a constructor in the
49+ -- reg, the Bool should be False, indicating that it's not safe to
50+ -- work with that as with a constructor, so a check should be
51+ -- added. If it's not a constructor, default case should be used.
52+ | CASE Bool
53+ Reg [(Int, [BC])] (Maybe [BC])
54+
55+ -- get a value from register, which should be a constructor, and
56+ -- put its arguments into the stack, starting from (base + int1)
57+ -- and onwards; second Int provides arity
58+ | PROJECT Reg Int Int
59+
60+ -- probably not used
61+ | PROJECTINTO Reg Reg Int -- project argument from one reg into another
62+
63+ -- same as CASE, but there's an exact value (not constructor) in reg
64+ | CONSTCASE Reg [(Const, [BC])] (Maybe [BC])
65+
66+ -- just call a function, passing MYOLDBASE (see below) to it
67+ | CALL Name
68+
69+ -- same, perhaps exists just for TCO
70+ | TAILCALL Name
71+
72+ -- set reg to (apply string args), FLang argument is the language to
73+ -- be called. FType is the foreign language type, which may be used to
74+ -- control marshaling.
75+ | FOREIGNCALL Reg FLang FType String [(FType, Reg)]
76+
77+ -- move this number of elements from TOP to BASE
78+ | SLIDE Int
79+
80+ -- set BASE = OLDBASE
81+ | REBASE
82+
83+ -- reserve n more stack items (i.e. check there's space, grow if
84+ -- necessary)
85+ | RESERVE Int
86+
87+ -- move the top of stack up
88+ | ADDTOP Int
89+
90+ -- set TOP = BASE + n
91+ | TOPBASE Int
92+
93+ -- set BASE = TOP + n
94+ | BASETOP Int
95+
96+ -- set MYOLDBASE = BASE, where MYOLDBASE is a function-local
97+ -- variable, set to OLDBASE by default, and passed on function
98+ -- call to called functions as their OLDBASE
99+ | STOREOLD
100+
101+ -- reg = apply primitive_function args
102+ | OP Reg PrimFn [Reg]
103+
104+ -- clear reg
105+ | NULL Reg
106+
107+ -- throw an error
108+ | ERROR String
109+ deriving Show
52110
53111toBC :: (Name, SDecl) -> (Name, [BC])
54112toBC (n, SFun n' args locs exp)