annotate python/testasm.py @ 199:a690473b79e2

Added msp430 target
author Windel Bouwman
date Fri, 07 Jun 2013 18:59:57 +0200
parents 33d50727a23c
children 5e391d9a3381
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
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
4 import ppci
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 $ '
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
29 with self.assertRaises(ppci.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):
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
82 def testX86(self):
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
83 testsrc = """ ; tst
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
84 begin:
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
85 mov rax, rbx ; 0x48, 0x89, 0xd8
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
86 xor rcx, rbx ; 0x48, 0x31, 0xd9
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
87 inc rcx ; 0x48 0xff 0xc1
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
88 """
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
89 a = Assembler()
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
90 a.assemble(testsrc)
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
91 # Compare with nasm output:
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
92 nasmbytes = [0x48, 0x89, 0xd8, 0x48, 0x31, 0xd9, 0x48, 0xff, 0xc1]
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
93
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
94 class AssemblerOtherTestCase(unittest.TestCase):
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
95 def setUp(self):
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
96 self.a = Assembler(target=msp430.MSP430())
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
97
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
98 def testMov(self):
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
99 line1 = "mov r14, r15"
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
100 self.a.assemble_line(line1)
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
101
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
102 def testAdd(self):
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
103 line1 = "addw r14, r15"
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
104 self.a.assemble_line(line1)
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
105
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
106
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
107 if __name__ == '__main__':
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
108 # cProfile.run('unittest.main()')
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
109 unittest.main()
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
110