annotate test/testasm.py @ 349:13a6e73b448f

Added test load from label
author Windel Bouwman
date Sat, 08 Mar 2014 16:16:41 +0100
parents 442fb043d149
children 6df89163e114
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
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 342
diff changeset
3 import unittest
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
4 from ppci import CompilerError
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 337
diff changeset
5 from ppci.assembler import tokenize, Assembler, Lexer
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
6 from ppci.objectfile import ObjectFile
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
7 from ppci.linker import Linker
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
8 from ppci.outstream import BinaryOutputStream
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
9 from ppci.target.basetarget import Label
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
10
290
7b38782ed496 File moves
Windel Bouwman
parents: 284
diff changeset
11
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
12 class AssemblerLexingCase(unittest.TestCase):
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
13 """ Tests the assemblers lexer """
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
14
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
15 def testLex0(self):
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
16 """ Check if the lexer is OK """
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 292
diff changeset
17 asmline, toks = 'mov rax, rbx ', ['ID', 'ID', ',', 'ID', 'EOF']
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 337
diff changeset
18 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline, [])], toks)
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
19
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
20 def testLex1(self):
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
21 """ Test if lexer correctly maps some tokens """
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 292
diff changeset
22 asmline, toks = 'lab1: mov rax, rbx ', ['ID', ':', 'ID', 'ID', ',', 'ID', 'EOF']
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 337
diff changeset
23 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline, [])], toks)
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
24
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
25 def testLex2(self):
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 292
diff changeset
26 """ Test if lexer correctly maps some tokens """
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 337
diff changeset
27 asmline, toks = 'mov 3.13 0xC 13', ['ID', 'REAL', 'val5', 'val5', 'EOF']
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 337
diff changeset
28 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline, [])], toks)
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 292
diff changeset
29
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 292
diff changeset
30 def testLex3(self):
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
31 """ Test if lexer fails on a token that is invalid """
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
32 asmline = '0z4: mov rax, rbx $ '
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
33 with self.assertRaises(CompilerError):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 337
diff changeset
34 list(tokenize(asmline, []))
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
35
290
7b38782ed496 File moves
Windel Bouwman
parents: 284
diff changeset
36
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
37 class AssemblerParsingTestCase(unittest.TestCase):
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 342
diff changeset
38 """
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
39 Tests the assembler parts
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
40 """
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
41 def setUp(self):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 337
diff changeset
42 self.skipTest('refactoring asm parser')
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
43 self.parser = asmParser
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
44 self.stack = []
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
45
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
46 def emit(self, x):
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
47 self.stack.append(x)
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
48
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
49 def parse_line(self, line):
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
50 self.parser.parse(Lexer(line), self.emit)
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
51
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
52 def testParse(self):
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
53 asmline = 'lab1: mov rax, rbx'
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
54 self.parse_line(asmline)
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
55
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
56 def expectTree(self, asmline, stack):
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
57 self.parse_line(asmline)
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
58 self.assertSequenceEqual(stack, self.stack)
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
59
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
60 def testParse2(self):
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
61 asmline = 'a: mov rax, [rbx + 2]'
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
62 output = []
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
63 output.append(ALabel('a'))
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
64 output.append(AInstruction('mov', [ASymbol('rax'), AUnop('[]', ASymbol('rbx') + ANumber(2))]))
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
65 self.expectTree(asmline, output)
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
66
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
67 def testParse3(self):
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
68 # A label must be optional:
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
69 asmline = 'mov rax, 1'
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
70 output = [AInstruction('mov', [ASymbol('rax'), ANumber(1)])]
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
71 self.expectTree(asmline, output)
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
72
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
73 def testParse4(self):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
74 # Test 3 operands:
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
75 asmline = 'add rax, [4*rbx + 22], rcx'
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
76 ops = []
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
77 ops.append(ASymbol('rax'))
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
78 ops.append(AUnop('[]', ANumber(4) * ASymbol('rbx') + ANumber(22)))
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
79 ops.append(ASymbol('rcx'))
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
80 output = [AInstruction('add', ops)]
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
81 self.expectTree(asmline, output)
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
82
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
83 def testParse5(self):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
84 # An instruction must be optional:
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
85 asmline = 'lab1:'
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
86 output = []
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
87 output.append(ALabel('lab1'))
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
88 self.expectTree(asmline, output)
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
89
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
90 def testParse6(self):
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
91 # A line can be empty
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
92 self.parse_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()
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
98 o = BinaryOutputStream(obj)
348
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
99 o.select_section('.text')
236
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):
346
3bb7dcfe5529 expanded arm target
Windel Bouwman
parents: 342
diff changeset
107 self.a.assemble(line, self.ostream)
236
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()