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
|
|
23 # TODO handle return?
|
171
|
24 bb = self.builder.newBB()
|
|
25 self.builder.setBB(bb)
|
170
|
26 self.genCode(s.body)
|
171
|
27 self.builder.addIns(ir.Return())
|
170
|
28 else:
|
|
29 print(s)
|
158
|
30
|
170
|
31 def genCode(self, code):
|
|
32 if type(code) is astnodes.CompoundStatement:
|
|
33 for s in code.statements:
|
|
34 self.genCode(s)
|
|
35 elif type(code) is astnodes.Assignment:
|
|
36 re = self.genExprCode(code.rval)
|
171
|
37 self.builder.addIns(ir.Store(code.lval, re))
|
170
|
38 elif type(code) is astnodes.IfStatement:
|
171
|
39 bbtrue = self.builder.newBB()
|
|
40 bbfalse = self.builder.newBB()
|
|
41 te = self.builder.newBB()
|
|
42 self.genCondCode(code.condition, bbtrue, bbfalse)
|
|
43 self.builder.setBB(bbtrue)
|
170
|
44 self.genCode(code.truestatement)
|
171
|
45 self.builder.addIns(ir.Branch(te))
|
|
46 self.builder.setBB(bbfalse)
|
170
|
47 self.genCode(code.falsestatement)
|
171
|
48 self.builder.addIns(ir.Branch(te))
|
|
49 self.builder.setBB(te)
|
170
|
50 elif type(code) is astnodes.FunctionCall:
|
|
51 pass
|
|
52 elif type(code) is astnodes.EmptyStatement:
|
|
53 pass
|
|
54 elif type(code) is astnodes.ReturnStatement:
|
|
55 pass
|
|
56 else:
|
|
57 print('Unknown stmt:', code)
|
171
|
58 def genCondCode(self, expr, bbtrue, bbfalse):
|
|
59 # Implement sequential logical operators
|
|
60 assert expr.typ == boolType
|
|
61 if type(expr) is astnodes.Binop:
|
|
62 if expr.op == 'or':
|
|
63 l2 = self.builder.newBB()
|
|
64 self.genCondCode(expr.a, bbtrue, l2)
|
|
65 self.builder.setBB(l2)
|
|
66 self.genCondCode(expr.b, bbtrue, bbfalse)
|
|
67 elif expr.op == 'and':
|
|
68 l2 = self.builder.newBB()
|
|
69 self.genCondCode(expr.a, l2, bbfalse)
|
|
70 self.builder.setBB(l2)
|
|
71 self.genCondCode(expr.b, bbtrue, bbfalse)
|
|
72 elif expr.op in ['==', '>', '<']:
|
|
73 ta = self.genExprCode(expr.a)
|
|
74 tb = self.genExprCode(expr.b)
|
|
75 i = ir.ConditionalBranch(ta, expr.op, tb, bbtrue, bbfalse)
|
|
76 self.builder.addIns(i)
|
|
77 else:
|
|
78 raise NotImlementedError()
|
|
79 print('Unknown cond', expr)
|
|
80 elif type(expr) is astnodes.Literal:
|
|
81 if expr.val:
|
|
82 self.builder.addIns(ir.BranchInstruction(bbtrue))
|
|
83 else:
|
|
84 self.builder.addIns(ir.BranchInstruction(bbfalse))
|
|
85 else:
|
|
86 print('Unknown cond', expr)
|
170
|
87 def genExprCode(self, expr):
|
|
88 if type(expr) is astnodes.Binop:
|
|
89 ra = self.genExprCode(expr.a)
|
|
90 rb = self.genExprCode(expr.b)
|
171
|
91 tmp = self.builder.newTmp()
|
170
|
92 ops = ['+', '-', '*', '/', 'and', 'or']
|
|
93 if expr.op in ops:
|
|
94 op = expr.op
|
|
95 ins = ir.BinaryOperator(tmp, op, ra, rb)
|
171
|
96 self.builder.addIns(ins)
|
170
|
97 return tmp
|
|
98 else:
|
171
|
99 print('Unknown {0}'.format(expr))
|
|
100 # TODO
|
|
101 return tmp
|
170
|
102 elif type(expr) is astnodes.Constant:
|
171
|
103 tmp = self.builder.newTmp()
|
|
104 # TODO
|
|
105 return tmp
|
170
|
106 elif type(expr) is astnodes.VariableUse:
|
171
|
107 tmp = self.builder.newTmp()
|
|
108 i = ir.Load(expr, tmp)
|
|
109 self.builder.addIns(i)
|
|
110 return tmp
|
170
|
111 elif type(expr) is astnodes.Literal:
|
171
|
112 tmp = self.builder.newTmp()
|
|
113 ins = ir.ImmLoad(tmp, expr.val)
|
|
114 self.builder.addIns(ins)
|
158
|
115 return tmp
|
157
|
116 else:
|
170
|
117 print('Unknown expr:', code)
|
157
|
118
|