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