annotate test/testasm.py @ 396:fb3c1f029b30

Added baselexer into c3 lexer
author Windel Bouwman
date Tue, 27 May 2014 22:19:32 +0200
parents d056b552d3f4
children
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
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents: 381
diff changeset
5 from ppci.assembler import AsmLexer
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
6 from ppci.objectfile import ObjectFile
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
7 from ppci.outstream import BinaryOutputStream
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
8 from ppci.target.basetarget import Label
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 348
diff changeset
9 from ppci.buildfunctions import link
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
10 from ppci.layout import Layout
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
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents: 381
diff changeset
16 def setUp(self):
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents: 381
diff changeset
17 self.lexer = AsmLexer([])
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents: 381
diff changeset
18
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents: 381
diff changeset
19 def do(self, asmline, toks):
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents: 381
diff changeset
20 output = []
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents: 381
diff changeset
21 self.lexer.feed(asmline)
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents: 381
diff changeset
22 while 'EOF' not in output:
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents: 381
diff changeset
23 output.append(self.lexer.next_token().typ)
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents: 381
diff changeset
24 self.assertSequenceEqual(toks, output)
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents: 381
diff changeset
25
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
26 def testLex0(self):
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
27 """ Check if the lexer is OK """
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents: 381
diff changeset
28 asmline = 'mov rax, rbx '
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents: 381
diff changeset
29 toks = ['ID', 'ID', ',', 'ID', 'EOF']
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents: 381
diff changeset
30 self.do(asmline, toks)
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
31
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
32 def testLex1(self):
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
33 """ Test if lexer correctly maps some tokens """
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents: 381
diff changeset
34 asmline = 'lab1: mov rax, rbx '
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents: 381
diff changeset
35 toks = ['ID', ':', 'ID', 'ID', ',', 'ID', 'EOF']
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents: 381
diff changeset
36 self.do(asmline, toks)
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
37
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
38 def testLex2(self):
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 292
diff changeset
39 """ Test if lexer correctly maps some tokens """
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 337
diff changeset
40 asmline, toks = 'mov 3.13 0xC 13', ['ID', 'REAL', 'val5', 'val5', 'EOF']
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents: 381
diff changeset
41 self.do(asmline, toks)
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 292
diff changeset
42
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 292
diff changeset
43 def testLex3(self):
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
44 """ Test if lexer fails on a token that is invalid """
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
45 asmline = '0z4: mov rax, rbx $ '
200
5e391d9a3381 Split off asm nodes
Windel Bouwman
parents: 199
diff changeset
46 with self.assertRaises(CompilerError):
382
0c44e494ef58 Made lexer more generic
Windel Bouwman
parents: 381
diff changeset
47 self.do(asmline, [])
198
33d50727a23c Fixup testscript
Windel Bouwman
parents: 196
diff changeset
48
290
7b38782ed496 File moves
Windel Bouwman
parents: 284
diff changeset
49
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
50 class OustreamTestCase(unittest.TestCase):
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
51 def test1(self):
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
52 obj = ObjectFile()
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
53 o = BinaryOutputStream(obj)
348
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
54 o.select_section('.text')
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
55 o.emit(Label('a'))
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
56 self.assertSequenceEqual(bytes(), obj.get_section('.text').data)
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
57
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
58
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
59 class AsmTestCaseBase(unittest.TestCase):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
60 """ Base testcase for assembly """
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
61 def feed(self, line):
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 348
diff changeset
62 self.assembler.assemble(line, self.ostream)
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
63
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
64 def check(self, hexstr, layout=Layout()):
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 348
diff changeset
65 self.assembler.flush()
385
d056b552d3f4 Made better use of layout
Windel Bouwman
parents: 383
diff changeset
66 self.obj = link([self.obj], layout, self.target)
383
173e20a47fda Added linker description loader
Windel Bouwman
parents: 382
diff changeset
67 data = bytes(self.obj.get_section('code').data)
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
68 self.assertSequenceEqual(bytes.fromhex(hexstr), data)
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
69
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
70
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
71 if __name__ == '__main__':
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
72 unittest.main()