annotate python/tests/runtests.py @ 71:5351594349b0

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