-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.py
231 lines (194 loc) · 8.56 KB
/
codegen.py
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
from llvmlite import ir, binding
from ast import Visitor
class CodeGen(Visitor):
# Initialise all the necessary variables that are used by LLVM
def __init__(self):
self.binding = binding
self.binding.initialize()
self.binding.initialize_native_target()
self.binding.initialize_native_asmprinter()
self._config_llvm()
self._create_execution_engine()
self._declare_print_function()
self.print_initialised = False
self.symbol_table = {}
voidptr_ty = ir.IntType(64).as_pointer()
fmt = "%lf \n\0"
c_fmt = ir.Constant(ir.ArrayType(ir.IntType(8), len(fmt)), bytearray(fmt.encode("utf8")))
global_fmt = ir.GlobalVariable(self.module, c_fmt.type, name="fstr")
global_fmt.linkage = 'internal'
global_fmt.global_constant = True
global_fmt.initializer = c_fmt
# builder = ir.IRBuilder()
# builder.position_at_start(self.builder.function.entry_basic_block)
self.fmt_arg = self.builder.bitcast(global_fmt, voidptr_ty)
self.print_initialised = True
# Configure LLVM with the required parameters.
def _config_llvm(self):
self.module = ir.Module(name=__file__)
self.module.triple = self.binding.get_default_triple()
func_type = ir.FunctionType(ir.VoidType(), [])
base_func = ir.Function(self.module, func_type, name="main")
block = base_func.append_basic_block(name="entry")
self.builder = ir.IRBuilder(block)
def _create_execution_engine(self):
target = self.binding.Target.from_default_triple()
target_machine = target.create_target_machine()
backing_mod = binding.parse_assembly("")
engine = binding.create_mcjit_compiler(backing_mod, target_machine)
self.engine = engine
# Creates a basic block. This is the smallest element of a segment
# of code, which has no branches leaving it.
def _create_entry_block_alloca(self, topBuilder):
saved_block = topBuilder.block
builder = ir.IRBuilder(topBuilder.function.entry_basic_block)
var_addr = builder.alloca(ir.DoubleType(), size=None)
topBuilder.position_at_end(saved_block)
return var_addr
def add_variable(self, var_addr, var_name):
self.symbol_table[var_name] = var_addr
# print(self.symbol_table)
# Visitor for integers
def visit_int(self, value):
i = ir.Constant(ir.DoubleType(), float(value))
return i
def visit_float(self, value):
i = ir.Constant(ir.DoubleType(), float(value))
return i
# Visitor for Sum
def visit_sum(self, left, right):
return self.builder.fadd(left, right)
# Visitor for Sum
def visit_sub(self, left, right):
return self.builder.fsub(left, right)
# Visitor for Mult
def visit_mult(self, left, right):
return self.builder.fmul(left, right)
# Visitor for Div
def visit_div(self, left, right):
return self.builder.fdiv(left, right)
# Visitor for Modulus
def visit_mod(self, left, right):
return self.builder.frem(left, right)
# Visitor for unary negation
def visit_uneg(self, value):
return self.builder.fsub(ir.Constant(ir.DoubleType(), float(0)), value)
# Visitor for Equal
def visit_eq(self, left, right):
l = self.builder.sitofp(left, ir.DoubleType())
r = self.builder.sitofp(right, ir.DoubleType())
res = self.builder.fcmp_ordered("==", l, r)
return self.builder.uitofp(res, ir.DoubleType())
# Visitor for NotEqual
def visit_neq(self, left, right):
l = self.builder.sitofp(left, ir.DoubleType())
r = self.builder.sitofp(right, ir.DoubleType())
res = self.builder.fcmp_ordered("!=", l, r)
return self.builder.uitofp(res, ir.DoubleType())
# Visitor for LessThan
def visit_lt(self, left, right):
l = self.builder.sitofp(left, ir.DoubleType())
r = self.builder.sitofp(right, ir.DoubleType())
res = self.builder.fcmp_ordered("<", l, r)
return self.builder.uitofp(res, ir.DoubleType())
# Visitor for GreaterThan
def visit_gt(self, left, right):
l = self.builder.sitofp(left, ir.DoubleType())
r = self.builder.sitofp(right, ir.DoubleType())
res = self.builder.fcmp_ordered(">", l, r)
return self.builder.uitofp(res, ir.DoubleType())
# Visitor for LessEqual
def visit_lte(self, left, right):
l = self.builder.sitofp(left, ir.DoubleType())
r = self.builder.sitofp(right, ir.DoubleType())
res = self.builder.fcmp_ordered("<=", l, r)
return self.builder.uitofp(res, ir.DoubleType())
# Visitor for GreaterEqual
def visit_gte(self, left, right):
l = self.builder.sitofp(left, ir.DoubleType())
r = self.builder.sitofp(right, ir.DoubleType())
res = self.builder.fcmp_ordered(">=", l, r)
return self.builder.uitofp(res, ir.DoubleType())
# Visitor for AND
def visit_and(self, left, right):
l = self.builder.fptosi(left, ir.IntType(64))
r = self.builder.fptosi(right, ir.IntType(64))
res = self.builder.and_(l, r)
return self.builder.uitofp(res, ir.DoubleType())
# Visitor for OR
def visit_or(self, left, right):
l = self.builder.fptosi(left, ir.IntType(64))
r = self.builder.fptosi(right, ir.IntType(64))
res = self.builder.or_(l, r)
return self.builder.uitofp(res, ir.DoubleType())
# Visitor for NOT
def visit_not(self, value):
v = self.builder.fptosi(value, ir.IntType(1))
res = self.builder.not_(v)
return self.builder.uitofp(res, ir.DoubleType())
# Visitor for strings
def visit_string(self, value):
printf = ir.Function(self.module, )
string_addr = self._create_entry_block_alloca(self.builder)
indices = ir.Constant(ir.ArrayType(ir.IntType(8), 8), 8) # unsure about this line
value = self.builder.gep(string_addr, indices)
self.builder.call(printf, value) # unsure about this line
self.builder.ret(ir.Constant(ir.IntType(32), 0))
return value
# Visitor for UDFs
def visit_udf(self, name, args):
pass
# Visitor for variables
def visit_var_dec(self, ident, value):
var_addr = self._create_entry_block_alloca(self.builder)
self.add_variable(var_addr, ident)
value = self.visit(value)
self.builder.store(value, var_addr)
# Visitor for using variables
def visit_var_usage(self, name):
var_addr = self.symbol_table[name]
return self.builder.load(var_addr)
# Visitor for Print
def visit_print(self, value):
# if not(self.print_initialised):
self.builder.call(self.printf, [self.fmt_arg, value])
# Visitor for if-then statements
def visit_if(self, pred, body):
p = self.visit(pred)
bool_cond = self.builder.fptosi(p, ir.IntType(1))
with self.builder.if_then(bool_cond):
for stmt in body:
self.visit(stmt)
# Visitor for if-else statements
def visit_if_else(self, pred, if_body, else_body):
p = self.visit(pred)
bool_cond = self.builder.fptosi(p, ir.IntType(1))
with self.builder.if_else(bool_cond) as (then, otherwise):
with then:
for stmt in if_body:
self.visit(stmt)
with otherwise:
for stmt in else_body:
self.visit(stmt)
# Declare the function that is used for printing text to the console.
def _declare_print_function(self):
voidptr_ty = ir.IntType(64).as_pointer()
printf_ty = ir.FunctionType(ir.IntType(32), [voidptr_ty], var_arg=True)
printf = ir.Function(self.module, printf_ty, name="printf")
self.printf = printf
def _compile_ir(self):
self.builder.ret_void()
llvm_ir = str(self.module)
mod = self.binding.parse_assembly(llvm_ir)
mod.verify()
self.engine.add_module(mod)
self.engine.finalize_object()
self.engine.run_static_constructors()
return mod
# Create and save the .ll file that contains the intermediate
# representation for the compiled program
def create_ir(self):
self._compile_ir()
def save_ir(self, filename):
with open(filename, "w") as output_file:
output_file.write(str(self.module))