Skip to content
← Back

src/ir/brancher.ghul

1
namespace IR is
2
use System.Reflection.Metadata.ILOpCode
3
use IO.Std
4
5
use Values
6
7
class LABEL is
8
_current_pass: string static
9
_next_id: int static
10
11
pass: string
12
id: int
13
14
init() is
15
pass = _current_pass
16
id = _next_id
17
18
_next_id = _next_id + 1
19
si
20
21
set_pass(p: string) static is
22
_current_pass = p
23
si
24
25
reset_id() static is
26
_next_id = 0
27
si
28
29
to_string() -> string => "L{pass}{id}"
30
si
31
32
enum COMPARE is
33
NONE,
34
EQ,
35
GE,
36
GEU,
37
GT,
38
GTU,
39
LE,
40
LEU,
41
LT,
42
LTU,
43
NE,
44
Z,
45
NZ
46
si
47
48
enum BRANCH is
49
NONE,
50
EQ,
51
GE,
52
GEU,
53
GT,
54
GTU,
55
LE,
56
LEU,
57
LT,
58
LTU,
59
NE,
60
Z,
61
NZ,
62
ALWAYS
63
si
64
65
class BRANCHER is
66
_branch_instructions: Collections.MAP[BRANCH, ILOpCode]
67
_compare_instructions: Collections.MAP[COMPARE, string]
68
69
init() is
70
_branch_instructions = Collections.MAP[BRANCH, ILOpCode]()
71
_compare_instructions = Collections.MAP[COMPARE, string]()
72
73
_branch_instructions[BRANCH.EQ] = ILOpCode.BEQ
74
_branch_instructions[BRANCH.GE] = ILOpCode.BGE
75
_branch_instructions[BRANCH.GEU] = ILOpCode.BGE_UN
76
_branch_instructions[BRANCH.GT] = ILOpCode.BGT
77
_branch_instructions[BRANCH.GTU] = ILOpCode.BGT_UN
78
_branch_instructions[BRANCH.LE] = ILOpCode.BLE
79
_branch_instructions[BRANCH.LEU] = ILOpCode.BLE_UN
80
_branch_instructions[BRANCH.LT] = ILOpCode.BLT
81
_branch_instructions[BRANCH.LTU] = ILOpCode.BLT_UN
82
_branch_instructions[BRANCH.NE] = ILOpCode.BNE_UN
83
_branch_instructions[BRANCH.Z] = ILOpCode.BRFALSE
84
_branch_instructions[BRANCH.NZ] = ILOpCode.BRTRUE
85
_branch_instructions[BRANCH.ALWAYS] = ILOpCode.BR
86
87
_compare_instructions[COMPARE.EQ] = "ceq"
88
_compare_instructions[COMPARE.GE] = "cge"
89
_compare_instructions[COMPARE.GEU] = "cge.un"
90
_compare_instructions[COMPARE.GT] = "cgt"
91
_compare_instructions[COMPARE.GTU] = "cgt.un"
92
_compare_instructions[COMPARE.LE] = "cle"
93
_compare_instructions[COMPARE.LEU] = "cle.un"
94
_compare_instructions[COMPARE.LT] = "clt"
95
_compare_instructions[COMPARE.LTU] = "clt.un"
96
_compare_instructions[COMPARE.NE] = "cne.un"
97
_compare_instructions[COMPARE.Z] = "ctrue"
98
_compare_instructions[COMPARE.NZ] = "cfalse"
99
si
100
101
get_for(block: BLOCK) -> BLOCK_BRANCHER =>
102
BLOCK_BRANCHER(block, self)
103
104
label(block: BLOCK, l: LABEL, comment: string) is
105
block.add(Values.MARK_LABEL(l))
106
si
107
108
label(block: BLOCK, l: LABEL) is
109
block.add(Values.MARK_LABEL(l))
110
si
111
112
leave(block: BLOCK, l: LABEL) is
113
block.add(Values.BRANCH_TO(ILOpCode.LEAVE, l))
114
si
115
116
branch(block: BLOCK, b: BRANCH, value: Value, l: LABEL, comment: string) is
117
block.add(value)
118
119
block.add(Values.BRANCH_TO(get_branch_instruction(b), l))
120
si
121
122
branch(block: BLOCK, b: BRANCH, value: Value, l: LABEL) is
123
block.add(value)
124
125
block.add(Values.BRANCH_TO(get_branch_instruction(b), l))
126
si
127
128
branch(block: BLOCK, b: BRANCH, left: Value, right: Value, l: LABEL, comment: string) is
129
block.add(left)
130
block.add(right)
131
132
block.add(Values.BRANCH_TO(get_branch_instruction(b), l))
133
si
134
135
branch(block: BLOCK, b: BRANCH, left: Value, right: Value, l: LABEL) is
136
block.add(left)
137
block.add(right)
138
139
block.add(Values.BRANCH_TO(get_branch_instruction(b), l))
140
si
141
142
branch(block: BLOCK, l: LABEL, comment: string) is
143
block.add(Values.BRANCH_TO(_branch_instructions[BRANCH.ALWAYS], l))
144
si
145
146
branch(block: BLOCK, l: LABEL) is
147
block.add(Values.BRANCH_TO(_branch_instructions[BRANCH.ALWAYS], l))
148
si
149
150
get_branch_instruction(b: BRANCH) -> ILOpCode =>
151
_branch_instructions[b]
152
si
153
154
struct BLOCK_BRANCHER(_block: BLOCK, _brancher: BRANCHER) is
155
label(l: LABEL, comment: string) is
156
_brancher.label(_block, l, comment)
157
si
158
159
label(l: LABEL) is
160
_brancher.label(_block, l)
161
si
162
163
leave(l: LABEL) is
164
_brancher.leave(_block, l)
165
si
166
167
branch(b: BRANCH, value: Value, l: LABEL, comment: string) is
168
_brancher.branch(_block, b, value, l, comment)
169
si
170
171
branch(b: BRANCH, value: Value, l: LABEL) is
172
_brancher.branch(_block, b, value, l)
173
si
174
175
branch(b: BRANCH, left: Value, right: Value, l: LABEL, comment: string) is
176
_brancher.branch(_block, b, left, right, l, comment)
177
si
178
179
branch(b: BRANCH, left: Value, right: Value, l: LABEL) is
180
_brancher.branch(_block, b, left, right, l)
181
si
182
183
branch(l: LABEL, comment: string) is
184
_brancher.branch(_block, l, comment)
185
si
186
187
branch(l: LABEL) is
188
_brancher.branch(_block, l)
189
si
190
si
191
si