Mercurial > lcfOS
annotate python/testc3.py @ 201:d5debbfc0200
Added all 27 core instructions of msp430
author | Windel Bouwman |
---|---|
date | Thu, 13 Jun 2013 00:07:28 +0200 |
parents | b01429a5d695 |
children | de3a68f677a5 |
rev | line source |
---|---|
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 | |
170 | 59 testsrc2 = """ |
60 package test2; | |
61 | |
62 function void tst() | |
63 { | |
64 var int a, b; | |
65 a = 2 * 33 - 12; | |
66 b = a * 2 + 13; | |
67 a = b + a; | |
68 if (a > b and b == 3) | |
69 { | |
70 var int x = a; | |
71 x = b * 2 - a; | |
72 a = x*x; | |
73 } | |
74 else | |
75 { | |
76 a = b + a; | |
77 } | |
78 } | |
79 | |
80 """ | |
81 | |
152 | 82 def c3compile(src, diag): |
163 | 83 # Structures: |
165 | 84 builder = c3.Builder(diag) |
85 ir = builder.build(src) | |
86 # optional optimize here | |
182
e9b27f7193e3
Replace clear function by function also supported in python 3.2
Windel Bouwman
parents:
180
diff
changeset
|
87 x86gen = x86.X86CodeGenSimple(diag) |
162 | 88 ok = len(diag.diags) == 0 |
89 if not ok: | |
90 return | |
165 | 91 print('generating x86 code') |
92 x86gen.genBin(ir) | |
93 with open('dummydummy.asm', 'w') as f: | |
94 f.write('bits 64\n') | |
95 for a in x86gen.asm: | |
96 print(a) | |
97 f.write(str(a) + '\n') | |
151 | 98 |
99 def do(): | |
152 | 100 diag = ppci.DiagnosticsManager() |
101 c3compile(testsrc, diag) | |
151 | 102 |
167 | 103 class testA(unittest.TestCase): |
104 def setUp(self): | |
105 self.diag = ppci.DiagnosticsManager() | |
106 self.builder = c3.Builder(self.diag) | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
107 def testSrc(self): |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
108 self.builder.build(testsrc) |
167 | 109 def testFunctArgs(self): |
110 snippet = """ | |
111 package testargs; | |
112 function void t2(int a, double b) | |
113 { | |
114 t2(2, 2); | |
115 t2(2); | |
116 t2(1, 1.2); | |
117 } | |
118 """ | |
119 self.diag.clear() | |
120 ir = self.builder.build(snippet) | |
168 | 121 assert len(self.diag.diags) == 2 |
122 assert self.diag.diags[0].loc.row == 5 | |
123 assert self.diag.diags[1].loc.row == 6 | |
148 | 124 |
167 | 125 def testExpressions(self): |
126 snippet = """ | |
127 package test; | |
128 function void t(int a, double b) | |
129 { | |
130 var int a2; | |
131 var bool c; | |
132 | |
133 a2 = b * a; | |
134 c = a; | |
135 c = b > 1; | |
136 } | |
137 """ | |
138 self.diag.clear() | |
194 | 139 ircode = self.builder.build(snippet) |
187 | 140 self.assertEqual(len(self.diag.diags), 3) |
186 | 141 self.assertEqual(self.diag.diags[0].loc.row, 8) |
142 self.assertEqual(self.diag.diags[1].loc.row, 9) | |
143 self.assertEqual(self.diag.diags[2].loc.row, 10) | |
194 | 144 self.assertFalse(ircode) |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
145 def testEmpty(self): |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
146 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
147 package A |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
148 """ |
194 | 149 ircode = self.builder.build(snippet) |
150 self.assertFalse(ircode) | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
151 def testEmpty2(self): |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
152 snippet = "" |
194 | 153 self.diag.clear() |
154 ircode = self.builder.build(snippet) | |
155 self.assertFalse(ircode) | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
156 def testRedefine(self): |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
157 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
158 package test; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
159 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
160 var int b; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
161 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
162 """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
163 self.diag.clear() |
194 | 164 ircode = self.builder.build(snippet) |
165 self.assertFalse(ircode) | |
186 | 166 self.assertEqual(len(self.diag.diags), 1) |
167 self.assertEqual(self.diag.diags[0].loc.row, 5) | |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
168 def testWhile(self): |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
169 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
170 package tstwhile; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
171 var int a; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
172 function void t() |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
173 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
174 var int i = 0; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
175 while (i < 1054) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
176 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
177 i = i + 3; |
186 | 178 a = a + i; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
179 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
180 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
181 """ |
187 | 182 ircode = self.builder.build(snippet) |
183 if not ircode: | |
186 | 184 self.diag.printErrors(snippet) |
187 | 185 self.assertTrue(ircode) |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
186 def testIf(self): |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
187 snippet = """ |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
188 package tstIFF; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
189 function void t(int b) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
190 { |
180 | 191 var int a; |
169
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
192 a = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
193 if (a > b) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
194 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
195 if (a > 1337) |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
196 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
197 b = 2; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
198 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
199 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
200 else |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
201 { |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
202 b = 1; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
203 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
204 |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
205 return b; |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
206 } |
ee0d30533dae
Added more tests and improved the diagnostic update
Windel Bouwman
parents:
168
diff
changeset
|
207 """ |
191 | 208 ircode = self.builder.build(snippet) |
209 if not ircode: | |
210 self.diag.printErrors(snippet) | |
211 self.assertTrue(ircode) | |
170 | 212 def test2(self): |
213 # testsrc2 is valid code: | |
214 self.diag.clear() | |
215 ir = self.builder.build(testsrc2) | |
187 | 216 self.assertTrue(ir) |
167 | 217 |
218 if __name__ == '__main__': | |
219 do() | |
220 unittest.main() | |
221 | |
222 |