158
|
1 import c3
|
|
2 import time, ppci, x86, ir
|
167
|
3 import unittest
|
148
|
4
|
162
|
5 testsrc = """package test;
|
148
|
6
|
149
|
7 var u32 c, d;
|
163
|
8 var double e;
|
|
9 var int f;
|
|
10
|
167
|
11 const int A = 1337;
|
149
|
12
|
|
13 function void test1()
|
148
|
14 {
|
163
|
15 var u32 bdd;
|
148
|
16 var int a = 10;
|
163
|
17 bd = 20;
|
155
|
18 var int buf;
|
148
|
19 var int i;
|
|
20 i = 2;
|
155
|
21 var int zero = i - 2;
|
148
|
22 if (i > 1)
|
|
23 {
|
157
|
24 buf = b + 22 * i - 13 + (55 * 2 *9-2) / 44 - 1;
|
148
|
25 }
|
155
|
26 else
|
|
27 {
|
|
28 ;;;
|
157
|
29 }
|
155
|
30
|
|
31 t2(2, 3);
|
148
|
32 }
|
|
33
|
149
|
34 function int t2(u32 a, u32 b)
|
148
|
35 {
|
157
|
36 if (a > 0)
|
|
37 {
|
167
|
38 a = 2 + t2(a - 1, 1.0);
|
157
|
39 }
|
|
40
|
148
|
41 return a + b;
|
|
42 }
|
|
43
|
166
|
44 function int t3(int aap, int blah)
|
|
45 {
|
|
46 if (a > blah and blah < 45 + 33 or 33 > aap or 6 and true)
|
|
47 {
|
|
48 a = 2 + t2(a - 1);
|
|
49 }
|
|
50
|
|
51 return a + b;
|
|
52 }
|
|
53
|
155
|
54 var int hahaa = 23 * 2;
|
150
|
55
|
149
|
56
|
148
|
57 """
|
|
58
|
152
|
59 def c3compile(src, diag):
|
163
|
60 # Structures:
|
165
|
61 builder = c3.Builder(diag)
|
|
62 ir = builder.build(src)
|
|
63 # optional optimize here
|
157
|
64 x86gen = x86.X86CodeGen(diag)
|
162
|
65 ok = len(diag.diags) == 0
|
|
66 if not ok:
|
165
|
67 print('Not generating code')
|
162
|
68 return
|
165
|
69 print('generating x86 code')
|
|
70 x86gen.genBin(ir)
|
|
71 with open('dummydummy.asm', 'w') as f:
|
|
72 f.write('bits 64\n')
|
|
73 for a in x86gen.asm:
|
|
74 print(a)
|
|
75 f.write(str(a) + '\n')
|
151
|
76
|
|
77 def do():
|
152
|
78 diag = ppci.DiagnosticsManager()
|
|
79 c3compile(testsrc, diag)
|
151
|
80
|
167
|
81 class testA(unittest.TestCase):
|
|
82 def setUp(self):
|
|
83 self.diag = ppci.DiagnosticsManager()
|
|
84 self.builder = c3.Builder(self.diag)
|
|
85 def testFunctArgs(self):
|
|
86 snippet = """
|
|
87 package testargs;
|
|
88 function void t2(int a, double b)
|
|
89 {
|
|
90 t2(2, 2);
|
|
91 t2(2);
|
|
92 t2(1, 1.2);
|
|
93 }
|
|
94 """
|
|
95 self.diag.clear()
|
|
96 ir = self.builder.build(snippet)
|
|
97 print(self.diag.diags)
|
148
|
98
|
167
|
99 def testExpressions(self):
|
|
100 snippet = """
|
|
101 package test;
|
|
102 function void t(int a, double b)
|
|
103 {
|
|
104 var int a2;
|
|
105 var bool c;
|
|
106
|
|
107 a2 = b * a;
|
|
108 c = a;
|
|
109 c = b > 1;
|
|
110 }
|
|
111 """
|
|
112 self.diag.clear()
|
|
113 ir = self.builder.build(snippet)
|
|
114 print(self.diag.diags)
|
|
115
|
|
116 if __name__ == '__main__':
|
|
117 do()
|
|
118 unittest.main()
|
|
119
|
|
120
|