~boringcactus/crowbar-reference-compiler

ref: 65afa06491818dc3187399c3f16dbf284bec761d crowbar-reference-compiler/crowbar_reference_compiler/ssagen.py -rw-r--r-- 13.7 KiB
65afa064 — Melody Horn retain function call return value 1 year, 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
import dataclasses
from dataclasses import dataclass
from functools import singledispatch
from typing import List

from .ast import ImplementationFile, FunctionDefinition, ExpressionStatement, FunctionCallExpression, \
    VariableExpression, ConstantExpression, ReturnStatement, BasicType, IfStatement, ComparisonExpression, \
    AddExpression, StructPointerElementExpression, Declaration, PointerType, StructDeclaration, VariableDefinition, \
    MultiplyExpression, LogicalNotExpression, DirectAssignment, UpdateAssignment, SizeofExpression, Expression, \
    ConstType, ArrayIndexExpression, ArrayType, NegativeExpression, SubtractExpression, AddressOfExpression


@dataclass
class SsaResult:
    data: List[str]
    code: List[str]

    def __add__(self, other: 'SsaResult') -> 'SsaResult':
        if not isinstance(other, SsaResult):
            return NotImplemented
        return SsaResult(self.data + other.data, self.code + other.code)

    def __radd__(self, other: 'SsaResult'):
        if not isinstance(other, SsaResult):
            return NotImplemented
        self.data += other.data
        self.code += other.code


@dataclass
class CompileContext:
    declarations: List[Declaration]
    next_data: int = 0
    next_temp: int = 0
    next_label: int = 0


def build_ssa(file: ImplementationFile) -> str:
    result = compile_to_ssa(file, CompileContext(file.get_declarations()))
    data = '\n'.join(result.data)
    code = '\n'.join(result.code)
    return data + '\n\n' + code


@singledispatch
def compile_to_ssa(target, context: CompileContext) -> SsaResult:
    raise NotImplementedError('unannotated compile on ' + str(type(target)))


@compile_to_ssa.register
def _(target: ImplementationFile, context: CompileContext):
    result = SsaResult([], [])
    for target in target.contents:
        result += compile_to_ssa(target, context)
    return result


@compile_to_ssa.register
def _(target: FunctionDefinition, context: CompileContext) -> SsaResult:
    result = SsaResult([], [])
    context = dataclasses.replace(context, declarations=target.args + context.declarations)
    for statement in target.body:
        result += compile_to_ssa(statement, context)
        if isinstance(statement, Declaration):
            context = dataclasses.replace(context, declarations=[statement]+context.declarations)
    if not result.code[-1].startswith('ret'):
        result.code.append('ret')
    code = ['    ' + instr for instr in result.code]
    # TODO types
    args = ','.join(f"l %{x.name}" for x in target.args)
    ret_type = ''
    if target.return_type != BasicType('void'):
        ret_type = 'l'
    code = [f"export function {ret_type} ${target.name}({args}) {{", "@start", *code, "}"]
    return SsaResult(result.data, code)


@compile_to_ssa.register
def _(target: ExpressionStatement, context: CompileContext) -> SsaResult:
    return compile_to_ssa(target.body, context)


@compile_to_ssa.register
def _(target: FunctionCallExpression, context: CompileContext) -> SsaResult:
    assert isinstance(target.function, VariableExpression)
    result = SsaResult([], [])
    args = []
    for i, expr in enumerate(target.arguments):
        result += compile_to_ssa(expr, context)
        arg_dest = context.next_temp - 1
        args += [f"l %t{arg_dest}"]
    result_dest = context.next_temp
    context.next_temp += 1
    # TODO size
    result.code.append(f"%t{result_dest} =l call ${target.function.name}({','.join(args)})")
    return result


@compile_to_ssa.register
def _(target: ConstantExpression, context: CompileContext) -> SsaResult:
    if target.type(context.declarations) == PointerType(ConstType(BasicType('char'))):
        data_dest = context.next_data
        context.next_data += 1
        data = [f"data $data{data_dest} = {{ b {target.value}, b 0 }}"]
        temp = context.next_temp
        context.next_temp += 1
        code = [f"%t{temp} =l copy $data{data_dest}"]
    elif target.type(context.declarations) == BasicType('char'):
        value = target.value.strip("'")
        if len(value) == 1:
            value = ord(value)
        elif value == r'\0':
            value = 0
        else:
            raise NotImplementedError('escape sequence ' + value)
        data = []
        temp = context.next_temp
        context.next_temp += 1
        code = [f"%t{temp} =l copy {value}"]
    elif target.type(context.declarations) == BasicType('bool'):
        data = []
        temp = context.next_temp
        context.next_temp += 1
        if target.value == 'true':
            value = 1
        else:
            value = 0
        code = [f"%t{temp} =l copy {value}"]
    elif target.type(context.declarations) == BasicType('int?'):
        assert not target.value.startswith('0b')
        assert not target.value.startswith('0B')
        assert not target.value.startswith('0o')
        assert not target.value.startswith('0x')
        assert not target.value.startswith('0X')
        assert not target.value.startswith('0f')
        assert not target.value.startswith('0F')
        assert '.' not in target.value
        assert not target.value.startswith("'")
        data = []
        temp = context.next_temp
        context.next_temp += 1
        code = [f"%t{temp} =l copy {target.value}"]
    else:
        raise NotImplementedError('compiling ' + str(target))
    return SsaResult(data, code)


@compile_to_ssa.register
def _(target: ReturnStatement, context: CompileContext) -> SsaResult:
    if target.body is None:
        return SsaResult([], ['ret'])
    result = compile_to_ssa(target.body, context)
    ret_val_dest = context.next_temp - 1
    result.code.append(f"ret %t{ret_val_dest}")
    return result


@compile_to_ssa.register
def _(target: IfStatement, context: CompileContext) -> SsaResult:
    result = compile_to_ssa(target.condition, context)
    condition_dest = context.next_temp - 1
    true_label = context.next_label
    context.next_label += 1
    false_label = context.next_label
    context.next_label += 1
    after_label = context.next_label
    context.next_label += 1
    result.code.append(f"jnz %t{condition_dest}, @l{true_label}, @l{false_label}")
    result.code.append(f"@l{true_label}")
    for statement in target.then:
        result += compile_to_ssa(statement, context)
    if not result.code[-1].startswith('ret'):
        result.code.append(f"jmp @l{after_label}")
    result.code.append(f"@l{false_label}")
    if target.els is not None:
        for statement in target.els:
            result += compile_to_ssa(statement, context)
    if not result.code[-1].startswith('ret'):
        result.code.append(f"jmp @l{after_label}")
    result.code.append(f"@l{after_label}")
    return result


@compile_to_ssa.register
def _(target: ComparisonExpression, context: CompileContext) -> SsaResult:
    result = compile_to_ssa(target.value1, context)
    value1_dest = context.next_temp - 1
    result += compile_to_ssa(target.value2, context)
    value2_dest = context.next_temp - 1
    result_dest = context.next_temp
    context.next_temp += 1
    # TODO types, and signedness
    if target.op == '==':
        op = "ceqw"
    elif target.op == '>=':
        op = "cugew"
    elif target.op == '<=':
        op = "culew"
    else:
        raise NotImplementedError('comparison ' + target.op)
    result.code.append(f"%t{result_dest} =l {op} %t{value1_dest}, %t{value2_dest}")
    return result


@compile_to_ssa.register
def _(target: AddExpression, context: CompileContext) -> SsaResult:
    result = compile_to_ssa(target.term1, context)
    value1_dest = context.next_temp - 1
    result += compile_to_ssa(target.term2, context)
    value2_dest = context.next_temp - 1
    result_reg = context.next_temp
    context.next_temp += 1
    # TODO make sure the types are correct
    result.code.append(f"%t{result_reg} =l add %t{value1_dest}, %t{value2_dest}")
    return result


@compile_to_ssa.register
def _(target: SubtractExpression, context: CompileContext) -> SsaResult:
    result = compile_to_ssa(target.term1, context)
    value1_dest = context.next_temp - 1
    result += compile_to_ssa(target.term2, context)
    value2_dest = context.next_temp - 1
    result_reg = context.next_temp
    context.next_temp += 1
    # TODO make sure the types are correct
    result.code.append(f"%t{result_reg} =l sub %t{value1_dest}, %t{value2_dest}")
    return result


@compile_to_ssa.register
def _(target: MultiplyExpression, context: CompileContext) -> SsaResult:
    result = compile_to_ssa(target.factor1, context)
    value1_dest = context.next_temp - 1
    result += compile_to_ssa(target.factor2, context)
    value2_dest = context.next_temp - 1
    result_reg = context.next_temp
    context.next_temp += 1
    # TODO make sure the types are correct
    result.code.append(f"%t{result_reg} =l mul %t{value1_dest}, %t{value2_dest}")
    return result


@compile_to_ssa.register
def _(target: VariableExpression, context: CompileContext) -> SsaResult:
    # TODO make sure any of this is reasonable
    result = context.next_temp
    context.next_temp += 1
    return SsaResult([], [f"%t{result} =l copy %{target.name}"])


@compile_to_ssa.register
def _(target: VariableDefinition, context: CompileContext) -> SsaResult:
    # TODO figure some shit out
    result = compile_to_ssa(target.value, context)
    result_dest = context.next_temp - 1
    result.code.append(f"%{target.name} =l copy %t{result_dest}")
    return result


@compile_to_ssa.register
def _(target: LogicalNotExpression, context: CompileContext) -> SsaResult:
    result = compile_to_ssa(target.body, context)
    inner_result_dest = context.next_temp - 1
    result_dest = context.next_temp
    context.next_temp += 1
    result.code.append(f"%t{result_dest} =l ceqw %t{inner_result_dest}, 0")
    return result


@compile_to_ssa.register
def _(target: NegativeExpression, context: CompileContext) -> SsaResult:
    return compile_to_ssa(SubtractExpression(ConstantExpression('0'), target.body), context)


@compile_to_ssa.register
def _(target: ArrayIndexExpression, context: CompileContext) -> SsaResult:
    result = compile_to_ssa(target.array, context)
    base = context.next_temp - 1
    result += compile_to_ssa(target.index, context)
    index = context.next_temp - 1
    array_type = target.array.type(context.declarations)
    if isinstance(array_type, PointerType):
        array_type = array_type.target
    assert isinstance(array_type, ArrayType)
    content_type = array_type.contents
    scale = content_type.size_bytes(context.declarations)
    offset = context.next_temp
    context.next_temp += 1
    address = context.next_temp
    context.next_temp += 1
    dest = context.next_temp
    context.next_temp += 1
    # TODO types
    result.code.append(f"%t{offset} =l mul %t{index}, {scale}")
    result.code.append(f"%t{address} =l add %t{base}, %t{offset}")
    result.code.append(f"%t{dest} =l loadl %t{address}")
    return result


@compile_to_ssa.register
def _(target: StructPointerElementExpression, context: CompileContext) -> SsaResult:
    result = compile_to_ssa(target.base, context)
    base_dest = context.next_temp - 1
    # hoooo boy.
    base_type = target.base.type(context.declarations)
    assert isinstance(base_type, PointerType)
    assert isinstance(base_type.target, BasicType)
    hopefully_struct, struct_name = base_type.target.name.split(' ')
    assert hopefully_struct == 'struct'
    target_struct = None
    for decl in context.declarations:
        if isinstance(decl, StructDeclaration) and decl.name == struct_name:
            if decl.fields is None:
                raise KeyError('struct ' + struct_name + ' is opaque')
            target_struct = decl
            break
    if target_struct is None:
        raise KeyError('struct ' + struct_name + ' not found')
    offset = 0
    for field in target_struct.fields:
        if field.name == target.element:
            break
        else:
            offset += field.type.size_bytes(context.declarations)
    temp = context.next_temp
    context.next_temp += 1
    result_dest = context.next_temp
    context.next_temp += 1
    # TODO types
    result.code.append(f"%t{temp} =l add %t{base_dest}, {offset}")
    result.code.append(f"%t{result_dest} =l loadl %t{temp}")
    return result


@compile_to_ssa.register
def _(target: AddressOfExpression, context: CompileContext) -> SsaResult:
    if isinstance(target.body, StructPointerElementExpression) or isinstance(target.body, ArrayIndexExpression):
        result = compile_to_ssa(target.body, context)
        result.code.pop()
        context.next_temp -= 1
    else:
        raise NotImplementedError('address of ' + str(type(target.body)))
    return result


@compile_to_ssa.register
def _(target: DirectAssignment, context: CompileContext) -> SsaResult:
    result = compile_to_ssa(target.value, context)
    result_dest = context.next_temp - 1
    if isinstance(target.destination, VariableExpression):
        raise NotImplementedError('assign directly to variable')
    elif isinstance(target.destination, StructPointerElementExpression) or isinstance(target.destination, ArrayIndexExpression):
        sub_result = compile_to_ssa(target.destination, context)
        last_instr = sub_result.code.pop()
        _, _, _, location = last_instr.split(' ')
        # TODO type
        sub_result.code.append(f"storel %t{result_dest}, {location}")
        result += sub_result
    else:
        raise NotImplementedError('assign to ' + str(type(target.destination)))
    return result


@compile_to_ssa.register
def _(target: UpdateAssignment, context: CompileContext) -> SsaResult:
    return compile_to_ssa(target.deconstruct(), context)


@compile_to_ssa.register
def _(target: SizeofExpression, context: CompileContext) -> SsaResult:
    target = target.body
    if isinstance(target, Expression):
        target = target.type(context.declarations)
    size = target.size_bytes(context.declarations)
    return compile_to_ssa(ConstantExpression(str(size)), context)