annotate python/runtests.py @ 103:28a35161ef23

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