Mercurial > lcfOS
comparison ide/runtests.py @ 4:0d5ef85b8698
Improved link between ast viewer and code edit
author | windel-eee |
---|---|
date | Wed, 21 Sep 2011 19:05:18 +0200 |
parents | |
children | 818f80afa78b |
comparison
equal
deleted
inserted
replaced
3:77202b0e0f40 | 4:0d5ef85b8698 |
---|---|
1 import unittest | |
2 | |
3 from compiler.compiler import Compiler | |
4 from compiler.errors import CompilerException, printError | |
5 from compiler import lexer | |
6 from compiler.parser import Parser | |
7 from compiler import assembler | |
8 from compiler.codegenerator import CodeGenerator | |
9 | |
10 class CompilerTestCase(unittest.TestCase): | |
11 """ test methods start with 'test*' """ | |
12 def testSource1(self): | |
13 source = """ | |
14 module lcfos; | |
15 var | |
16 a : integer; | |
17 | |
18 procedure putchar(num : integer); | |
19 begin | |
20 end putchar; | |
21 | |
22 procedure WriteNum( num: integer); | |
23 var | |
24 d, base : integer; | |
25 dgt : integer; | |
26 begin | |
27 d := 1; | |
28 base := 10; | |
29 while num div d >= base do | |
30 d := d * base | |
31 end; | |
32 while d <> 0 do | |
33 dgt := num div d; | |
34 num := num mod d; | |
35 d := d div base; | |
36 putchar(48 + dgt) | |
37 end | |
38 end WriteNum; | |
39 | |
40 begin | |
41 a := 1; | |
42 while a < 26 | |
43 do | |
44 putchar(65+a); | |
45 a := a * 2 | |
46 end; | |
47 end lcfos. | |
48 """ | |
49 pc = Compiler() | |
50 pc.compilesource(source) | |
51 def testSource2(self): | |
52 source = """ | |
53 module lcfos; | |
54 var | |
55 a, b : integer; | |
56 arr: array 30 of integer; | |
57 arr2: array 10, 12 of integer; | |
58 procedure t2*() : integer; | |
59 begin | |
60 a := 2; | |
61 while a < 5 do | |
62 b := arr[a-1] + arr[a-2]; | |
63 arr2[a,2] := b; | |
64 arr2[a,3] := arr2[a,2] + arr2[a,2]*3 + b; | |
65 arr[a] := b; | |
66 a := a + 1; | |
67 end; | |
68 return b | |
69 end t2; | |
70 begin | |
71 b := 12; | |
72 arr[0] := 1; | |
73 arr[1] := 1; | |
74 end lcfos. | |
75 """ | |
76 pc = Compiler() | |
77 mod = pc.compilesource(source) | |
78 def testSource5(self): | |
79 source = """ | |
80 module lcfos; | |
81 procedure WriteLn() : integer; | |
82 const zzz = 13; | |
83 var | |
84 a, b, c: integer; | |
85 begin | |
86 a := 2; | |
87 b := 7; | |
88 c := 10 * a + b*10*a; | |
89 return c | |
90 end WriteLn; | |
91 begin end lcfos. | |
92 """ | |
93 pc = Compiler() | |
94 pc.compilesource(source) | |
95 def testForStatement(self): | |
96 source = """ | |
97 module fortest; | |
98 var | |
99 a,b,c : integer; | |
100 begin | |
101 c := 0; | |
102 for a := 1 to 10 by 1 do | |
103 b := a + 15; | |
104 c := c + b * a; | |
105 end; | |
106 end fortest. | |
107 """ | |
108 pc = Compiler() | |
109 pc.compilesource(source) | |
110 def testSourceIfAndWhilePattern(self): | |
111 source = """ | |
112 module lcfos; | |
113 procedure WriteLn() : integer; | |
114 const zzz = 13; | |
115 var | |
116 a, b, c: integer; | |
117 begin | |
118 a := 1; | |
119 b := 2; | |
120 if a * 3 > b then | |
121 c := 10*a + b*10*a*a*a*b; | |
122 else | |
123 c := 13; | |
124 end; | |
125 while a < 101 do | |
126 a := a + 1; | |
127 c := c + 2; | |
128 end; | |
129 return c | |
130 end WriteLn; | |
131 begin end lcfos. | |
132 """ | |
133 pc = Compiler() | |
134 pc.compilesource(source) | |
135 | |
136 def testPattern1(self): | |
137 """ Test if expression can be compiled into byte code """ | |
138 src = "12*13+33-12*2*3" | |
139 tokens = lexer.tokenize(src) | |
140 ast = Parser(tokens).parseExpression() | |
141 code = CodeGenerator().genexprcode(ast) | |
142 | |
143 def testAssembler(self): | |
144 """ Check all kind of assembler cases """ | |
145 assert(assembler.shortjump(5) == [0xeb, 0x5]) | |
146 assert(assembler.shortjump(-2) == [0xeb, 0xfc]) | |
147 assert(assembler.shortjump(10,'GE') == [0x7d, 0xa]) | |
148 assert(assembler.nearjump(5) == [0xe9, 0x5,0x0,0x0,0x0]) | |
149 assert(assembler.nearjump(-2) == [0xe9, 0xf9, 0xff,0xff,0xff]) | |
150 assert(assembler.nearjump(10,'LE') == [0x0f, 0x8e, 0xa,0x0,0x0,0x0]) | |
151 | |
152 def testCall(self): | |
153 assert(assembler.call('r10') == [0x41, 0xff, 0xd2]) | |
154 assert(assembler.call('rcx') == [0xff, 0xd1]) | |
155 def testXOR(self): | |
156 assert(assembler.xorreg64('rax', 'rax') == [0x48, 0x31, 0xc0]) | |
157 assert(assembler.xorreg64('r9', 'r8') == [0x4d, 0x31, 0xc1]) | |
158 assert(assembler.xorreg64('rbx', 'r11') == [0x4c, 0x31, 0xdb]) | |
159 | |
160 def testINC(self): | |
161 assert(assembler.increg64('r11') == [0x49, 0xff, 0xc3]) | |
162 assert(assembler.increg64('rcx') == [0x48, 0xff, 0xc1]) | |
163 | |
164 def testPush(self): | |
165 assert(assembler.push('rbp') == [0x55]) | |
166 assert(assembler.push('rbx') == [0x53]) | |
167 assert(assembler.push('r12') == [0x41, 0x54]) | |
168 def testPop(self): | |
169 assert(assembler.pop('rbx') == [0x5b]) | |
170 assert(assembler.pop('rbp') == [0x5d]) | |
171 assert(assembler.pop('r12') == [0x41, 0x5c]) | |
172 | |
173 def testAsmLoads(self): | |
174 # TODO constant add testcases | |
175 assert(assembler.mov('rbx', 'r14') == [0x4c, 0x89, 0xf3]) | |
176 assert(assembler.mov('r12', 'r8') == [0x4d, 0x89, 0xc4]) | |
177 assert(assembler.mov('rdi', 'rsp') == [0x48, 0x89, 0xe7]) | |
178 | |
179 def testAsmMemLoads(self): | |
180 assert(assembler.mov('rax', ['r8','r15',0x11]) == [0x4b,0x8b,0x44,0x38,0x11]) | |
181 assert(assembler.mov('r13', ['rbp','rcx',0x23]) == [0x4c,0x8b,0x6c,0xd,0x23]) | |
182 | |
183 assert(assembler.mov('r9', ['rbp',-0x33]) == [0x4c,0x8b,0x4d,0xcd]) | |
184 #assert(assembler.movreg64('rbx', ['rax']) == [0x48, 0x8b,0x18]) | |
185 | |
186 assert(assembler.mov('rax', [0xb000]) == [0x48,0x8b,0x4,0x25,0x0,0xb0,0x0,0x0]) | |
187 assert(assembler.mov('r11', [0xa0]) == [0x4c,0x8b,0x1c,0x25,0xa0,0x0,0x0,0x0]) | |
188 | |
189 assert(assembler.mov('r11', ['RIP', 0xf]) == [0x4c,0x8b,0x1d,0x0f,0x0,0x0,0x0]) | |
190 | |
191 def testAsmMemStores(self): | |
192 assert(assembler.mov(['rbp', 0x13],'rbx') == [0x48,0x89,0x5d,0x13]) | |
193 assert(assembler.mov(['r12', 0x12],'r9') == [0x4d,0x89,0x4c,0x24,0x12]) | |
194 assert(assembler.mov(['rcx', 0x11],'r14') == [0x4c,0x89,0x71,0x11]) | |
195 | |
196 | |
197 assert(assembler.mov([0xab], 'rbx') == [0x48,0x89,0x1c,0x25,0xab,0x0,0x0,0x0]) | |
198 assert(assembler.mov([0xcd], 'r13') == [0x4c,0x89,0x2c,0x25,0xcd,0x0,0x0,0x0]) | |
199 | |
200 assert(assembler.mov(['RIP', 0xf], 'r9') == [0x4c,0x89,0x0d,0x0f,0x0,0x0,0x0]) | |
201 | |
202 def testAsmMOV8(self): | |
203 assert(assembler.mov(['rbp', -8], 'al') == [0x88, 0x45, 0xf8]) | |
204 assert(assembler.mov(['r11', 9], 'cl') == [0x41, 0x88, 0x4b, 0x09]) | |
205 | |
206 assert(assembler.mov(['rbx'], 'al') == [0x88, 0x03]) | |
207 assert(assembler.mov(['r11'], 'dl') == [0x41, 0x88, 0x13]) | |
208 | |
209 def testAsmLea(self): | |
210 assert(assembler.leareg64('r11', ['RIP', 0xf]) == [0x4c,0x8d,0x1d,0x0f,0x0,0x0,0x0]) | |
211 assert(assembler.leareg64('rsi', ['RIP', 0x7]) == [0x48,0x8d,0x35,0x07,0x0,0x0,0x0]) | |
212 | |
213 assert(assembler.leareg64('rcx', ['rbp', -8]) == [0x48,0x8d,0x4d,0xf8]) | |
214 | |
215 def testAssemblerCMP(self): | |
216 assert(assembler.cmpreg64('rdi', 'r13') == [0x4c, 0x39, 0xef]) | |
217 assert(assembler.cmpreg64('rbx', 'r14') == [0x4c, 0x39, 0xf3]) | |
218 assert(assembler.cmpreg64('r12', 'r9') == [0x4d, 0x39, 0xcc]) | |
219 | |
220 assert(assembler.cmpreg64('rdi', 1) == [0x48, 0x83, 0xff, 0x01]) | |
221 assert(assembler.cmpreg64('r11', 2) == [0x49, 0x83, 0xfb, 0x02]) | |
222 def testAssemblerADD(self): | |
223 assert(assembler.addreg64('rbx', 'r13') == [0x4c, 0x01, 0xeb]) | |
224 assert(assembler.addreg64('rax', 'rbx') == [0x48, 0x01, 0xd8]) | |
225 assert(assembler.addreg64('r12', 'r13') == [0x4d, 0x01, 0xec]) | |
226 | |
227 assert(assembler.addreg64('rbx', 0x13) == [0x48, 0x83, 0xc3, 0x13]) | |
228 assert(assembler.addreg64('r11', 0x1234567) == [0x49, 0x81, 0xc3, 0x67, 0x45,0x23,0x1]) | |
229 assert(assembler.addreg64('rsp', 0x33) == [0x48, 0x83, 0xc4, 0x33]) | |
230 | |
231 def testAssemblerSUB(self): | |
232 assert(assembler.subreg64('rdx', 'r14') == [0x4c, 0x29, 0xf2]) | |
233 assert(assembler.subreg64('r15', 'rbx') == [0x49, 0x29, 0xdf]) | |
234 assert(assembler.subreg64('r8', 'r9') == [0x4d, 0x29, 0xc8]) | |
235 | |
236 assert(assembler.subreg64('rsp', 0x123456) == [0x48, 0x81, 0xec, 0x56,0x34,0x12,0x0]) | |
237 assert(assembler.subreg64('rsp', 0x12) == [0x48, 0x83, 0xec, 0x12]) | |
238 | |
239 def testAssemblerIDIV(self): | |
240 assert(assembler.idivreg64('r11') == [0x49, 0xf7, 0xfb]) | |
241 assert(assembler.idivreg64('rcx') == [0x48, 0xf7, 0xf9]) | |
242 assert(assembler.idivreg64('rsp') == [0x48, 0xf7, 0xfc]) | |
243 | |
244 def testAssemblerIMUL(self): | |
245 assert(assembler.imulreg64_rax('rdi') == [0x48, 0xf7, 0xef]) | |
246 assert(assembler.imulreg64_rax('r10') == [0x49, 0xf7, 0xea]) | |
247 assert(assembler.imulreg64_rax('rdx') == [0x48, 0xf7, 0xea]) | |
248 | |
249 assert(assembler.imulreg64('r11', 'rdi') == [0x4c, 0xf, 0xaf, 0xdf]) | |
250 assert(assembler.imulreg64('r12', 'rbx') == [0x4c, 0xf, 0xaf, 0xe3]) | |
251 # nasm generates this machine code: 0x4d, 0x6b, 0xff, 0xee | |
252 # This also works: 4D0FAFFE (another variant?? ) | |
253 assert(assembler.imulreg64('r15', 'r14') == [0x4d, 0x0f, 0xaf, 0xfe]) | |
254 | |
255 if __name__ == '__main__': | |
256 unittest.main() | |
257 |