Mercurial > lcfOS
comparison python/c3/codegenerator.py @ 217:8b2e5f3cd579
Removed some stale python source files
author | Windel Bouwman |
---|---|
date | Fri, 05 Jul 2013 14:13:59 +0200 |
parents | d77cb5962cc5 |
children | 3f6c30a5d234 |
comparison
equal
deleted
inserted
replaced
216:57c032c5e753 | 217:8b2e5f3cd579 |
---|---|
1 import ir | 1 import ir |
2 from . import astnodes | 2 from . import astnodes |
3 from .scope import boolType | 3 from .scope import boolType |
4 | 4 |
5 class CodeGenerator: | 5 class CodeGenerator: |
6 """ Generates intermediate code from a package """ | 6 """ Generates intermediate code from a package """ |
7 def gencode(self, pkg): | 7 def gencode(self, pkg): |
8 assert type(pkg) is astnodes.Package | 8 assert type(pkg) is astnodes.Package |
9 self.varMap = {} # Maps variables to storage locations. | 9 self.varMap = {} # Maps variables to storage locations. |
10 self.funcMap = {} | 10 self.funcMap = {} |
11 self.builder = ir.Builder() | 11 self.builder = ir.Builder() |
12 m = ir.Module(pkg.name) | 12 m = ir.Module(pkg.name) |
13 self.builder.setModule(m) | 13 self.builder.setModule(m) |
14 self.genModule(pkg) | 14 self.genModule(pkg) |
15 return m | 15 return m |
16 | 16 |
17 # inner helpers: | 17 # inner helpers: |
18 def genModule(self, pkg): | 18 def genModule(self, pkg): |
19 # Take care of forward declarations: | 19 # Take care of forward declarations: |
20 for s in pkg.scope: | 20 for s in pkg.innerScope.Functions: |
21 if type(s) is astnodes.Function: | |
22 f = self.builder.newFunction(s.name) | 21 f = self.builder.newFunction(s.name) |
23 self.funcMap[s] = f | 22 self.funcMap[s] = f |
24 for s in pkg.scope: | 23 for s in pkg.innerScope: |
25 if type(s) is astnodes.Variable: | 24 if type(s) is astnodes.Variable: |
26 v = self.builder.newVariable(s.name) | 25 v = self.builder.newVariable(s.name) |
27 #self.builder.addIns(ir.Alloc(v)) | 26 #self.builder.addIns(ir.Alloc(v)) |
28 self.varMap[s] = v | 27 self.varMap[s] = v |
29 elif type(s) is astnodes.Function: | 28 elif type(s) is astnodes.Function: |
33 bb = self.builder.newBB() | 32 bb = self.builder.newBB() |
34 f.entry = bb | 33 f.entry = bb |
35 self.builder.setBB(bb) | 34 self.builder.setBB(bb) |
36 # generate room for locals: | 35 # generate room for locals: |
37 | 36 |
38 for sym in s.scope: | 37 for sym in s.innerScope: |
39 #print(sym, sym.isParameter) | 38 #print(sym, sym.isParameter) |
40 # TODO: handle parameters different | 39 # TODO: handle parameters different |
41 v = self.builder.newTmp(sym.name) | 40 v = self.builder.newTmp(sym.name) |
42 self.builder.addIns(ir.Alloc(v)) | 41 self.builder.addIns(ir.Alloc(v)) |
43 self.varMap[sym] = v | 42 self.varMap[sym] = v |
47 self.builder.addIns(ir.Return()) | 46 self.builder.addIns(ir.Return()) |
48 self.builder.setFunction(None) | 47 self.builder.setFunction(None) |
49 else: | 48 else: |
50 print(s) | 49 print(s) |
51 | 50 |
52 def genCode(self, code): | 51 def genCode(self, code): |
53 if type(code) is astnodes.CompoundStatement: | 52 if type(code) is astnodes.CompoundStatement: |
54 for s in code.statements: | 53 for s in code.statements: |
55 self.genCode(s) | 54 self.genCode(s) |
56 elif type(code) is astnodes.Assignment: | 55 elif type(code) is astnodes.Assignment: |
57 re = self.genExprCode(code.rval) | 56 re = self.genExprCode(code.rval) |
86 te = self.builder.newBB() | 85 te = self.builder.newBB() |
87 self.builder.addIns(ir.Branch(bbtest)) | 86 self.builder.addIns(ir.Branch(bbtest)) |
88 self.builder.setBB(bbtest) | 87 self.builder.setBB(bbtest) |
89 self.genCondCode(code.condition, bbdo, te) | 88 self.genCondCode(code.condition, bbdo, te) |
90 self.builder.setBB(bbdo) | 89 self.builder.setBB(bbdo) |
91 self.genCode(code.dostatement) | 90 self.genCode(code.statement) |
92 self.builder.addIns(ir.Branch(bbtest)) | 91 self.builder.addIns(ir.Branch(bbtest)) |
93 self.builder.setBB(te) | 92 self.builder.setBB(te) |
94 else: | 93 else: |
95 print('Unknown stmt:', code) | 94 print('Unknown stmt:', code) |
96 def genCondCode(self, expr, bbtrue, bbfalse): | 95 def genCondCode(self, expr, bbtrue, bbfalse): |
97 # Implement sequential logical operators | 96 # Implement sequential logical operators |
98 assert expr.typ == boolType | 97 assert expr.typ == boolType |
99 if type(expr) is astnodes.Binop: | 98 if type(expr) is astnodes.Binop: |
100 if expr.op == 'or': | 99 if expr.op == 'or': |
101 l2 = self.builder.newBB() | 100 l2 = self.builder.newBB() |
119 self.builder.addIns(ir.Branch(bbtrue)) | 118 self.builder.addIns(ir.Branch(bbtrue)) |
120 else: | 119 else: |
121 self.builder.addIns(ir.Branch(bbfalse)) | 120 self.builder.addIns(ir.Branch(bbfalse)) |
122 else: | 121 else: |
123 print('Unknown cond', expr) | 122 print('Unknown cond', expr) |
124 def genExprCode(self, expr): | 123 def genExprCode(self, expr): |
125 if type(expr) is astnodes.Binop: | 124 if type(expr) is astnodes.Binop: |
126 ra = self.genExprCode(expr.a) | 125 ra = self.genExprCode(expr.a) |
127 rb = self.genExprCode(expr.b) | 126 rb = self.genExprCode(expr.b) |
128 ops = ['+', '-', '*', '/'] | 127 ops = ['+', '-', '*', '/'] |
129 if expr.op in ops: | 128 if expr.op in ops: |