annotate test/testasm.py @ 292:534b94b40aa8

Fixup reorganize
author Windel Bouwman
date Wed, 27 Nov 2013 08:06:42 +0100
parents 7b38782ed496
children e84047f29c78
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
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
7 import outstream
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
8 from target import Label
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
9
290
7b38782ed496 File moves
Windel Bouwman
parents: 284
diff changeset
10
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
11 class AssemblerLexingCase(unittest.TestCase):
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
12 """ Tests the assemblers lexer """
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
13
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
14 def testLex0(self):
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
15 """ Check if the lexer is OK """
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
16 asmline, toks = 'mov rax, rbx ', ['ID', 'ID', ',', 'ID']
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
17 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline)], toks)
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
18
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
19 def testLex1(self):
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
20 """ Test if lexer correctly maps some tokens """
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
21 asmline, toks = 'lab1: mov rax, rbx ', ['ID', ':', 'ID', 'ID', ',', 'ID']
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
22 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline)], toks)
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
23
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
24 def testLex1(self):
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
25 """ Test if lexer correctly maps some tokens """
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
26 asmline, toks = 'mov 3.13 0xC 13', ['ID', 'REAL', 'NUMBER', 'NUMBER']
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
27 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline)], toks)
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
28
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
29 def testLex2(self):
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
30 """ Test if lexer fails on a token that is invalid """
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
31 asmline = '0z4: mov rax, rbx $ '
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
32 with self.assertRaises(CompilerError):
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
33 list(tokenize(asmline))
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
34
290
7b38782ed496 File moves
Windel Bouwman
parents: 284
diff changeset
35
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
36 class AssemblerParsingTestCase(unittest.TestCase):
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
37 """
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
38 Tests the assembler parts
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
39 """
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
40 def setUp(self):
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
41 self.a = Assembler()
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
42
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
43 def testParse(self):
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
44 asmline = 'lab1: mov rax, rbx'
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
45 self.a.parse_line(asmline)
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
46
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
47 def expectTree(self, asmline, stack):
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
48 self.a.parse_line(asmline)
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
49 self.assertSequenceEqual(stack, self.a.stack)
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
50
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
51 def testParse2(self):
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
52 asmline = 'a: mov rax, [rbx + 2]'
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
53 output = []
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
54 output.append(ALabel('a'))
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
55 output.append(AInstruction('mov', [ASymbol('rax'), AUnop('[]', ASymbol('rbx') + ANumber(2))]))
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
56 self.expectTree(asmline, output)
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
57
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
58 def testParse3(self):
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
59 # A label must be optional:
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
60 asmline = 'mov rax, 1'
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
61 output = [AInstruction('mov', [ASymbol('rax'), ANumber(1)])]
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
62 self.expectTree(asmline, output)
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
63
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
64 def testParse4(self):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
65 # Test 3 operands:
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
66 asmline = 'add rax, [4*rbx + 22], rcx'
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
67 ops = []
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
68 ops.append(ASymbol('rax'))
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
69 ops.append(AUnop('[]', ANumber(4) * ASymbol('rbx') + ANumber(22)))
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
70 ops.append(ASymbol('rcx'))
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
71 output = [AInstruction('add', ops)]
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
72 self.expectTree(asmline, output)
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
73
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
74 def testParse5(self):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
75 # An instruction must be optional:
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
76 asmline = 'lab1:'
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
77 output = []
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
78 output.append(ALabel('lab1'))
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
79 self.expectTree(asmline, output)
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
80
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
81 def testParse6(self):
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
82 # A line can be empty
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
83 self.a.parse_line('')
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
84
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
85
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
86 class AssemblerOtherTestCase(unittest.TestCase):
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
87 def testWithoutTarget(self):
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
88 a = Assembler()
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
89 with self.assertRaises(CompilerError):
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
90 a.assemble_line('')
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
91
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
92
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
93 class OustreamTestCase(unittest.TestCase):
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
94 def test1(self):
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
95 o = outstream.BinOutputStream()
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
96 o.selectSection('.text')
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
97 o.emit(Label('a'))
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
98 self.assertSequenceEqual(bytes(), o.Data)
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
99
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
100
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
101 class AsmTestCaseBase(unittest.TestCase):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
102 """ Base testcase for assembly """
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
103 def feed(self, line):
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
104 self.a.assemble(line)
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
105
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
106 def check(self, hexstr):
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
107 self.assertSequenceEqual(bytes.fromhex(hexstr), self.o.Data)
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
108
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
109
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
110 if __name__ == '__main__':
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
111 unittest.main()