annotate python/testasm.py @ 202:f22b431f4113

Added arm add instruction
author Windel Bouwman
date Sat, 15 Jun 2013 10:02:50 +0200
parents d5debbfc0200
children ca1ea402f6a1
rev   line source
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
1 #!/usr/bin/python
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
2
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
3 import unittest, cProfile
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
4 from ppci import CompilerError
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
5 from asmnodes import AInstruction, ABinop, AUnop, ASymbol, ALabel, ANumber
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
6 from asm import tokenize, Assembler
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
7 import msp430
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
8 import arm_cm3
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
9
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
10 class AssemblerLexingCase(unittest.TestCase):
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
11 """ Tests the assemblers lexer """
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
12
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
13 def testLex0(self):
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
14 """ Check if the lexer is OK """
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
15 asmline, toks = 'mov rax, rbx ', ['ID', 'ID', ',', 'ID']
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
16 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline)], toks)
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
17
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
18 def testLex1(self):
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
19 """ Test if lexer correctly maps some tokens """
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
20 asmline, toks = 'lab1: mov rax, rbx ', ['ID', ':', 'ID', 'ID', ',', 'ID']
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
21 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline)], toks)
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
22
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
23 def testLex1(self):
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
24 """ Test if lexer correctly maps some tokens """
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
25 asmline, toks = 'mov 3.13 0xC 13', ['ID', 'REAL', 'NUMBER', 'NUMBER']
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
26 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline)], toks)
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
27
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
28 def testLex2(self):
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
29 """ Test if lexer fails on a token that is invalid """
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
30 asmline = '0z4: mov rax, rbx $ '
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
31 with self.assertRaises(CompilerError):
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
32 list(tokenize(asmline))
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
33
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
34 class AssemblerParsingTestCase(unittest.TestCase):
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
35 """
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
36 Tests the assembler parts
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
37 """
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
38 def setUp(self):
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
39 self.a = Assembler()
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
40
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
41 def testParse(self):
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
42 asmline = 'lab1: mov rax, rbx'
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
43 self.a.parse_line(asmline)
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
44
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
45 def testParse2(self):
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
46 asmline = 'a: mov rax, [rbx + 2]'
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
47 self.a.parse_line(asmline)
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
48 output = []
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
49 output.append(ALabel('a'))
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
50 output.append(AInstruction('mov', [ASymbol('rax'), AUnop('[]', ASymbol('rbx') + ANumber(2))]))
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
51 self.assertSequenceEqual(output, self.a.output)
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
52
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
53 def testParse3(self):
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
54 # A label must be optional:
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
55 asmline = 'mov rax, 1'
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
56 self.a.parse_line(asmline)
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
57 output = [AInstruction('mov', [ASymbol('rax'), ANumber(1)])]
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
58 self.assertSequenceEqual(output, self.a.output)
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
59
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
60 def testParse4(self):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
61 # Test 3 operands:
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
62 asmline = 'add rax, [4*rbx + 22], rcx'
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
63 self.a.parse_line(asmline)
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
64 ops = []
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
65 ops.append(ASymbol('rax'))
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
66 ops.append(AUnop('[]', ANumber(4) * ASymbol('rbx') + ANumber(22)))
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
67 ops.append(ASymbol('rcx'))
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
68 output = [AInstruction('add', ops)]
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
69 self.assertSequenceEqual(output, self.a.output)
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
70
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
71 def testParse5(self):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
72 # An instruction must be optional:
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
73 asmline = 'lab1:'
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
74 self.a.parse_line(asmline)
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
75 output = []
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
76 output.append(ALabel('lab1'))
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
77 self.assertSequenceEqual(output, self.a.output)
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
78
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
79 def testParse6(self):
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
80 # A line can be empty
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
81 self.a.parse_line('')
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
82
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
83 class AssemblerOtherTestCase(unittest.TestCase):
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
84 def testWithoutTarget(self):
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
85 a = Assembler()
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
86 with self.assertRaises(CompilerError):
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
87 a.assemble_line('')
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
88
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
89 @unittest.skip
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
90 def testX86(self):
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
91 testsrc = """ ; tst
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
92 begin:
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
93 mov rax, rbx ; 0x48, 0x89, 0xd8
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
94 xor rcx, rbx ; 0x48, 0x31, 0xd9
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
95 inc rcx ; 0x48 0xff 0xc1
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
96 """
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
97 a = Assembler()
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
98 a.assemble(testsrc)
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
99 # Compare with nasm output:
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
100 nasmbytes = [0x48, 0x89, 0xd8, 0x48, 0x31, 0xd9, 0x48, 0xff, 0xc1]
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
101
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
102 class AssemblerMSP430TestCase(unittest.TestCase):
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
103 def setUp(self):
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
104 self.t = msp430.msp430target
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
105 self.a = Assembler(target=self.t)
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
106
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
107 def testMapMovInstruction(self):
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
108 i = AInstruction('mov', [ASymbol('r14'), ASymbol('r15')])
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
109 ri = self.t.mapInstruction(i)
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
110
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
111 def testMapRetiInstruction(self):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
112 i = AInstruction('reti', [])
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
113 ri = self.t.mapInstruction(i)
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
114
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
115 @unittest.skip
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
116 def testMapOperand(self):
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
117 o = ASymbol('r14')
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
118 mo = self.t.mapOperand(o)
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
119 self.assertEqual(mo, msp430.r14)
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
120
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
121 @unittest.skip
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
122 def testMapOperandIndirection(self):
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
123 o = AUnop('[]', ASymbol('r14'))
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
124 mo = self.t.mapOperand(o)
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
125
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
126 def testMov(self):
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
127 line1 = "mov r14, r15"
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
128 self.a.assemble_line(line1)
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
129 self.assertEqual(bytes([0x0F, 0x4E]), self.a.binout)
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
130
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
131 def testMov1337(self):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
132 line1 = "mov 0x1337, r12"
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
133 self.a.assemble_line(line1)
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
134 self.assertEqual(bytes.fromhex('3C403713'), self.a.binout)
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
135
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
136 def testAdd(self):
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
137 line1 = "add r15, r13"
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
138 self.a.assemble_line(line1)
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
139 self.assertEqual(bytes.fromhex('0D5F'), self.a.binout)
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
140
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
141 def testReti(self):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
142 line1 = "reti"
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
143 self.a.assemble_line(line1)
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
144 self.assertEqual(bytes([0x0, 0x13]), self.a.binout)
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
145
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
146 def testMSPinstructionCount(self):
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
147 """ Check that there are 27 instructions """
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
148 self.assertEqual(27, len(self.t.instructions))
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
149
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
150
202
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
151 class AssemblerARMTestCase(unittest.TestCase):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
152 def setUp(self):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
153 self.t = arm_cm3.armtarget
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
154 self.a = Assembler(target=self.t)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
155
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
156 def testMapOperand(self):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
157 pass
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
158
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
159 def testMovImm8(self):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
160 self.a.assemble('mov r4, 100')
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
161 self.assertEqual(bytes.fromhex('6424'), self.a.binout)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
162
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
163 def testYield(self):
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
164 self.a.assemble('yield')
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
165 self.assertEqual(bytes.fromhex('10bf'), self.a.binout)
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
166
f22b431f4113 Added arm add instruction
Windel Bouwman
parents: 201
diff changeset
167
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
168 if __name__ == '__main__':
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
169 # cProfile.run('unittest.main()')
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
170 unittest.main()
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
171