annotate python/testasm.py @ 201:d5debbfc0200

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