155
|
1 import ir
|
|
2 from . import astnodes
|
171
|
3 from .scope import boolType
|
|
4
|
170
|
5 class CodeGenerator:
|
|
6 """ Generates intermediate code from a package """
|
|
7 def gencode(self, pkg):
|
|
8 assert type(pkg) is astnodes.Package
|
171
|
9 self.builder = ir.Builder()
|
|
10 m = ir.Module(pkg.name)
|
|
11 self.builder.setModule(m)
|
170
|
12 self.genModule(pkg)
|
171
|
13 return m
|
170
|
14
|
|
15 # inner helpers:
|
|
16 def genModule(self, pkg):
|
|
17 for s in pkg.scope:
|
|
18 if type(s) is astnodes.Variable:
|
|
19 # TODO
|
|
20 pass
|
|
21 elif type(s) is astnodes.Function:
|
|
22 # TODO: handle arguments
|
172
|
23 f = self.builder.newFunc(s.name)
|
|
24 self.builder.setFunc(f)
|
171
|
25 bb = self.builder.newBB()
|
172
|
26 f.entry = bb
|
171
|
27 self.builder.setBB(bb)
|
170
|
28 self.genCode(s.body)
|
172
|
29 # TODO handle return?
|
171
|
30 self.builder.addIns(ir.Return())
|
172
|
31 self.builder.setFunc(None)
|
170
|
32 else:
|
|
33 print(s)
|
158
|
34
|
170
|
35 def genCode(self, code):
|
|
36 if type(code) is astnodes.CompoundStatement:
|
|
37 for s in code.statements:
|
|
38 self.genCode(s)
|
|
39 elif type(code) is astnodes.Assignment:
|
|
40 re = self.genExprCode(code.rval)
|
171
|
41 self.builder.addIns(ir.Store(code.lval, re))
|
170
|
42 elif type(code) is astnodes.IfStatement:
|
171
|
43 bbtrue = self.builder.newBB()
|
|
44 bbfalse = self.builder.newBB()
|
|
45 te = self.builder.newBB()
|
|
46 self.genCondCode(code.condition, bbtrue, bbfalse)
|
|
47 self.builder.setBB(bbtrue)
|
170
|
48 self.genCode(code.truestatement)
|
171
|
49 self.builder.addIns(ir.Branch(te))
|
|
50 self.builder.setBB(bbfalse)
|
170
|
51 self.genCode(code.falsestatement)
|
171
|
52 self.builder.addIns(ir.Branch(te))
|
|
53 self.builder.setBB(te)
|
170
|
54 elif type(code) is astnodes.FunctionCall:
|
|
55 pass
|
|
56 elif type(code) is astnodes.EmptyStatement:
|
|
57 pass
|
|
58 elif type(code) is astnodes.ReturnStatement:
|
|
59 pass
|
|
60 else:
|
|
61 print('Unknown stmt:', code)
|
171
|
62 def genCondCode(self, expr, bbtrue, bbfalse):
|
|
63 # Implement sequential logical operators
|
|
64 assert expr.typ == boolType
|
|
65 if type(expr) is astnodes.Binop:
|
|
66 if expr.op == 'or':
|
|
67 l2 = self.builder.newBB()
|
|
68 self.genCondCode(expr.a, bbtrue, l2)
|
|
69 self.builder.setBB(l2)
|
|
70 self.genCondCode(expr.b, bbtrue, bbfalse)
|
|
71 elif expr.op == 'and':
|
|
72 l2 = self.builder.newBB()
|
|
73 self.genCondCode(expr.a, l2, bbfalse)
|
|
74 self.builder.setBB(l2)
|
|
75 self.genCondCode(expr.b, bbtrue, bbfalse)
|
|
76 elif expr.op in ['==', '>', '<']:
|
|
77 ta = self.genExprCode(expr.a)
|
|
78 tb = self.genExprCode(expr.b)
|
|
79 i = ir.ConditionalBranch(ta, expr.op, tb, bbtrue, bbfalse)
|
|
80 self.builder.addIns(i)
|
|
81 else:
|
|
82 raise NotImlementedError()
|
|
83 print('Unknown cond', expr)
|
|
84 elif type(expr) is astnodes.Literal:
|
|
85 if expr.val:
|
|
86 self.builder.addIns(ir.BranchInstruction(bbtrue))
|
|
87 else:
|
|
88 self.builder.addIns(ir.BranchInstruction(bbfalse))
|
|
89 else:
|
|
90 print('Unknown cond', expr)
|
170
|
91 def genExprCode(self, expr):
|
|
92 if type(expr) is astnodes.Binop:
|
|
93 ra = self.genExprCode(expr.a)
|
|
94 rb = self.genExprCode(expr.b)
|
171
|
95 tmp = self.builder.newTmp()
|
170
|
96 ops = ['+', '-', '*', '/', 'and', 'or']
|
|
97 if expr.op in ops:
|
|
98 op = expr.op
|
|
99 ins = ir.BinaryOperator(tmp, op, ra, rb)
|
171
|
100 self.builder.addIns(ins)
|
170
|
101 return tmp
|
|
102 else:
|
171
|
103 print('Unknown {0}'.format(expr))
|
|
104 # TODO
|
|
105 return tmp
|
170
|
106 elif type(expr) is astnodes.Constant:
|
171
|
107 tmp = self.builder.newTmp()
|
|
108 # TODO
|
|
109 return tmp
|
170
|
110 elif type(expr) is astnodes.VariableUse:
|
171
|
111 tmp = self.builder.newTmp()
|
|
112 i = ir.Load(expr, tmp)
|
|
113 self.builder.addIns(i)
|
|
114 return tmp
|
170
|
115 elif type(expr) is astnodes.Literal:
|
171
|
116 tmp = self.builder.newTmp()
|
|
117 ins = ir.ImmLoad(tmp, expr.val)
|
|
118 self.builder.addIns(ins)
|
158
|
119 return tmp
|
157
|
120 else:
|
170
|
121 print('Unknown expr:', code)
|
157
|
122
|