annotate test/testasm.py @ 335:582a1aaa3983

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