annotate test/testasm.py @ 382:0c44e494ef58

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