annotate test/testasm.py @ 342:86b02c98a717 devel

Moved target directory
author Windel Bouwman
date Sat, 01 Mar 2014 15:40:31 +0100
parents 4d204f6f7d4e
children 3bb7dcfe5529
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
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 337
diff changeset
6 from ppci.assembler import tokenize, Assembler, Lexer
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
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
9 from ppci.outstream import BinaryOutputStream
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
10 from ppci.target.basetarget 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']
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 337
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']
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 337
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 """
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 337
diff changeset
28 asmline, toks = 'mov 3.13 0xC 13', ['ID', 'REAL', 'val5', 'val5', 'EOF']
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 337
diff changeset
29 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline, [])], toks)
318
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):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 337
diff changeset
35 list(tokenize(asmline, []))
198
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):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 337
diff changeset
43 self.skipTest('refactoring asm parser')
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
44 self.parser = asmParser
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
45 self.stack = []
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
46
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
47 def emit(self, x):
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
48 self.stack.append(x)
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
49
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
50 def parse_line(self, line):
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
51 self.parser.parse(Lexer(line), self.emit)
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
52
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
53 def testParse(self):
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
54 asmline = 'lab1: mov rax, rbx'
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
55 self.parse_line(asmline)
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
56
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
57 def expectTree(self, asmline, stack):
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
58 self.parse_line(asmline)
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
59 self.assertSequenceEqual(stack, self.stack)
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
60
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
61 def testParse2(self):
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
62 asmline = 'a: mov rax, [rbx + 2]'
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
63 output = []
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
64 output.append(ALabel('a'))
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
65 output.append(AInstruction('mov', [ASymbol('rax'), AUnop('[]', ASymbol('rbx') + ANumber(2))]))
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
66 self.expectTree(asmline, output)
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
67
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
68 def testParse3(self):
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
69 # A label must be optional:
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
70 asmline = 'mov rax, 1'
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
71 output = [AInstruction('mov', [ASymbol('rax'), ANumber(1)])]
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 testParse4(self):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
75 # Test 3 operands:
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
76 asmline = 'add rax, [4*rbx + 22], rcx'
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
77 ops = []
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
78 ops.append(ASymbol('rax'))
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
79 ops.append(AUnop('[]', ANumber(4) * ASymbol('rbx') + ANumber(22)))
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
80 ops.append(ASymbol('rcx'))
199
a690473b79e2 Added msp430 target
Windel Bouwman
parents: 198
diff changeset
81 output = [AInstruction('add', ops)]
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
82 self.expectTree(asmline, output)
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
83
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
84 def testParse5(self):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
85 # An instruction must be optional:
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
86 asmline = 'lab1:'
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
87 output = []
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
88 output.append(ALabel('lab1'))
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
89 self.expectTree(asmline, output)
196
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
90
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
91 def testParse6(self):
ec2b423cdbea Merge asm and asmlib files
Windel Bouwman
parents: 195
diff changeset
92 # A line can be empty
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
93 self.parse_line('')
201
d5debbfc0200 Added all 27 core instructions of msp430
Windel Bouwman
parents: 200
diff changeset
94
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
95
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
96 class OustreamTestCase(unittest.TestCase):
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
97 def test1(self):
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
98 obj = ObjectFile()
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
99 o = BinaryOutputStream(obj)
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
100 o.selectSection('.text')
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
101 o.emit(Label('a'))
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
102 self.assertSequenceEqual(bytes(), obj.get_section('.text').data)
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
103
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
104
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
105 class AsmTestCaseBase(unittest.TestCase):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
106 """ Base testcase for assembly """
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
107 def feed(self, line):
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
108 self.a.assemble(line)
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
109
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
110 def check(self, hexstr):
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
111 l = Linker()
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
112 self.obj = l.link([self.obj])
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
113 data = bytes(self.obj.get_section('.text').data)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
114 self.assertSequenceEqual(bytes.fromhex(hexstr), data)
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
115
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
116
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
117 if __name__ == '__main__':
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
118 unittest.main()