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