annotate test/testasm.py @ 337:b00219172a42

Added cool lm3s811 qemu project
author Windel Bouwman
date Thu, 20 Feb 2014 20:04:52 +0100
parents 582a1aaa3983
children 4d204f6f7d4e
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
337
b00219172a42 Added cool lm3s811 qemu project
Windel Bouwman
parents: 335
diff changeset
6 from ppci.assembler import tokenize, Assembler, asmParser, 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
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):
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()
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()