annotate python/testc3.py @ 160:10330be89bc2

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