annotate test/testasm.py @ 381:6df89163e114

Fix section and ldr pseudo instruction
author Windel Bouwman
date Sat, 26 Apr 2014 17:41:56 +0200
parents 442fb043d149
children 0c44e494ef58
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
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 348
diff changeset
5 from ppci.assembler import tokenize
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
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
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
37 class OustreamTestCase(unittest.TestCase):
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
38 def test1(self):
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
39 obj = ObjectFile()
342
86b02c98a717 Moved target directory
Windel Bouwman
parents: 341
diff changeset
40 o = BinaryOutputStream(obj)
348
442fb043d149 Added log option to zcc
Windel Bouwman
parents: 346
diff changeset
41 o.select_section('.text')
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
42 o.emit(Label('a'))
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
43 self.assertSequenceEqual(bytes(), obj.get_section('.text').data)
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
44
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
45
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
46 class AsmTestCaseBase(unittest.TestCase):
292
534b94b40aa8 Fixup reorganize
Windel Bouwman
parents: 290
diff changeset
47 """ Base testcase for assembly """
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
48 def feed(self, line):
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 348
diff changeset
49 self.assembler.assemble(line, self.ostream)
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
50
381
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 348
diff changeset
51 def check(self, hexstr, layout={}):
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 348
diff changeset
52 self.assembler.flush()
6df89163e114 Fix section and ldr pseudo instruction
Windel Bouwman
parents: 348
diff changeset
53 self.obj = link([self.obj], layout)
335
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
54 data = bytes(self.obj.get_section('.text').data)
582a1aaa3983 Added long branch format
Windel Bouwman
parents: 334
diff changeset
55 self.assertSequenceEqual(bytes.fromhex(hexstr), data)
236
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
56
8786811a5a59 Fix pcrel
Windel Bouwman
parents: 234
diff changeset
57
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
58 if __name__ == '__main__':
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
59 unittest.main()