annotate python/testc3.py @ 157:8f3924b6076e

Added some code generator things
author Windel Bouwman
date Sun, 03 Mar 2013 18:14:35 +0100
parents b28a11c01dbe
children 9683a4cd848f
rev   line source
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
1 import c3, time, ppci, x86
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
2
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
3 testsrc = """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
4 package test;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
5
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
6 var u32 c, d;
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
7
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
8 function void test1()
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
9 {
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
10 var u32 b;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
11 var int a = 10;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
12 b = 20;
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
13 var int buf;
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
14 var int i;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
15 i = 2;
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
16 var int zero = i - 2;
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
17 if (i > 1)
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
18 {
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
19 buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44 - 1;
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
20 }
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
21 else
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
22 {
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
23 ;;;
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
24 }
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
25
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
26 t2(2, 3);
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
27 }
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
28
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
29 function int t2(u32 a, u32 b)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
30 {
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
31 if (a > 0)
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
32 {
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
33 a = 2 + t2(a - 1);
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
34 }
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
35
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
36 return a + b;
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
37 }
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
38
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
39 var int hahaa = 23 * 2;
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
40
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
41
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
42 """
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
43
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
44 def printAst(ast, indent=''):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
45 print(indent + str(ast))
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
46 if isinstance(ast, c3.astnodes.Package):
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
47 for s in ast.scope:
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
48 printAst(s, indent + ' ')
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
49 if isinstance(ast, c3.astnodes.Function):
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
50 for s in ast.scope:
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
51 printAst(s, indent + ' ')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
52 for c in ast.getChildren():
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
53 printAst(c, indent + ' ')
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
54
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 151
diff changeset
55 def c3compile(src, diag):
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
56 print('[0] source:')
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
57 #print(src)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
58 print('[1] parsing')
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
59 sema = c3.Semantics(diag)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
60 p = c3.Parser(sema, diag)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
61 tc = c3.TypeChecker(diag)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
62 al = c3.Analyzer(diag)
151
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents: 150
diff changeset
63 cg = c3.CodeGenerator()
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
64 x86gen = x86.X86CodeGen(diag)
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
65 t1 = time.time()
151
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents: 150
diff changeset
66 p.parseSource(src)
149
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
67 t2 = time.time()
74241ca312cc Fixes on parser and semantics
Windel Bouwman
parents: 148
diff changeset
68 print('parsetime: {0} [s]'.format(t2 - t1))
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
69 t2 = time.time()
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
70 tc.checkPackage(sema.mod)
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
71 t3 = time.time()
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
72 print('checktime: {0} [s]'.format(t3 - t2))
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
73 print('{0} errors'.format(len(diag.diags)))
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
74
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
75 for d in diag.diags:
150
4ae0e02599de Added type check start and analyze phase
Windel Bouwman
parents: 149
diff changeset
76 print('ERROR:')
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 151
diff changeset
77 ppci.printError(testsrc, d)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
78 print('[2] ast:')
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
79 #printAst(sema.mod)
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
80
151
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents: 150
diff changeset
81 ok = len(diag.diags) == 0
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents: 150
diff changeset
82 if ok:
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
83 print('Generating code')
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
84 i = cg.gencode(sema.mod)
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
85
155
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
86 print(i)
b28a11c01dbe Simplified IR classes
Windel Bouwman
parents: 152
diff changeset
87 print(i.Globals)
157
8f3924b6076e Added some code generator things
Windel Bouwman
parents: 155
diff changeset
88 x86gen.genBin(i)
151
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents: 150
diff changeset
89 else:
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents: 150
diff changeset
90 print('Not generating code')
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents: 150
diff changeset
91
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents: 150
diff changeset
92 def do():
152
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 151
diff changeset
93 diag = ppci.DiagnosticsManager()
b73bc14a3aa3 Light coupling ide and c3 frontend
Windel Bouwman
parents: 151
diff changeset
94 c3compile(testsrc, diag)
151
afc8c0207984 Added ir code generator stub
Windel Bouwman
parents: 150
diff changeset
95
148
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
96 do()
e5263f74b287 Added c3 language frontend initial parser
Windel Bouwman
parents:
diff changeset
97