158
|
1 import c3
|
|
2 import time, ppci, x86, ir
|
148
|
3
|
162
|
4 testsrc = """package test;
|
148
|
5
|
149
|
6 var u32 c, d;
|
|
7
|
|
8 function void test1()
|
148
|
9 {
|
|
10 var u32 b;
|
|
11 var int a = 10;
|
|
12 b = 20;
|
155
|
13 var int buf;
|
148
|
14 var int i;
|
|
15 i = 2;
|
155
|
16 var int zero = i - 2;
|
148
|
17 if (i > 1)
|
|
18 {
|
157
|
19 buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44 - 1;
|
148
|
20 }
|
155
|
21 else
|
|
22 {
|
|
23 ;;;
|
157
|
24 }
|
155
|
25
|
|
26 t2(2, 3);
|
148
|
27 }
|
|
28
|
149
|
29 function int t2(u32 a, u32 b)
|
148
|
30 {
|
157
|
31 if (a > 0)
|
|
32 {
|
|
33 a = 2 + t2(a - 1);
|
|
34 }
|
|
35
|
148
|
36 return a + b;
|
|
37 }
|
|
38
|
155
|
39 var int hahaa = 23 * 2;
|
150
|
40
|
149
|
41
|
148
|
42 """
|
|
43
|
149
|
44 def printAst(ast, indent=''):
|
|
45 print(indent + str(ast))
|
|
46 if isinstance(ast, c3.astnodes.Package):
|
|
47 for s in ast.scope:
|
|
48 printAst(s, indent + ' ')
|
150
|
49 if isinstance(ast, c3.astnodes.Function):
|
149
|
50 for s in ast.scope:
|
|
51 printAst(s, indent + ' ')
|
148
|
52 for c in ast.getChildren():
|
149
|
53 printAst(c, indent + ' ')
|
148
|
54
|
152
|
55 def c3compile(src, diag):
|
148
|
56 print('[0] source:')
|
155
|
57 #print(src)
|
148
|
58 print('[1] parsing')
|
150
|
59 sema = c3.Semantics(diag)
|
|
60 p = c3.Parser(sema, diag)
|
|
61 tc = c3.TypeChecker(diag)
|
|
62 al = c3.Analyzer(diag)
|
151
|
63 cg = c3.CodeGenerator()
|
157
|
64 x86gen = x86.X86CodeGen(diag)
|
151
|
65 p.parseSource(src)
|
162
|
66 ok = len(diag.diags) == 0
|
|
67 if not ok:
|
|
68 return
|
150
|
69 tc.checkPackage(sema.mod)
|
|
70 print('{0} errors'.format(len(diag.diags)))
|
148
|
71
|
|
72 for d in diag.diags:
|
150
|
73 print('ERROR:')
|
152
|
74 ppci.printError(testsrc, d)
|
148
|
75 print('[2] ast:')
|
155
|
76 #printAst(sema.mod)
|
148
|
77
|
151
|
78 ok = len(diag.diags) == 0
|
|
79 if ok:
|
158
|
80 print('Generating ir-code')
|
155
|
81 i = cg.gencode(sema.mod)
|
158
|
82 #ir.printIr(i)
|
|
83
|
|
84 print('generating x86 code')
|
157
|
85
|
|
86 x86gen.genBin(i)
|
158
|
87
|
|
88 with open('dummydummy.asm', 'w') as f:
|
|
89 f.write('bits 64\n')
|
|
90 for a in x86gen.asm:
|
160
|
91 print(a)
|
158
|
92 f.write(str(a) + '\n')
|
151
|
93 else:
|
|
94 print('Not generating code')
|
|
95
|
|
96 def do():
|
152
|
97 diag = ppci.DiagnosticsManager()
|
|
98 c3compile(testsrc, diag)
|
151
|
99
|
148
|
100 do()
|
|
101
|