annotate python/testasm.py @ 200:5e391d9a3381

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