191
|
1 #!/usr/bin/python
|
|
2
|
346
|
3 import unittest
|
200
|
4 from ppci import CompilerError
|
341
|
5 from ppci.assembler import tokenize, Assembler, Lexer
|
335
|
6 from ppci.objectfile import ObjectFile
|
|
7 from ppci.linker import Linker
|
342
|
8 from ppci.outstream import BinaryOutputStream
|
|
9 from ppci.target.basetarget import Label
|
191
|
10
|
290
|
11
|
198
|
12 class AssemblerLexingCase(unittest.TestCase):
|
|
13 """ Tests the assemblers lexer """
|
191
|
14
|
|
15 def testLex0(self):
|
|
16 """ Check if the lexer is OK """
|
318
|
17 asmline, toks = 'mov rax, rbx ', ['ID', 'ID', ',', 'ID', 'EOF']
|
341
|
18 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline, [])], toks)
|
191
|
19
|
|
20 def testLex1(self):
|
193
|
21 """ Test if lexer correctly maps some tokens """
|
318
|
22 asmline, toks = 'lab1: mov rax, rbx ', ['ID', ':', 'ID', 'ID', ',', 'ID', 'EOF']
|
341
|
23 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline, [])], toks)
|
193
|
24
|
191
|
25 def testLex2(self):
|
318
|
26 """ Test if lexer correctly maps some tokens """
|
341
|
27 asmline, toks = 'mov 3.13 0xC 13', ['ID', 'REAL', 'val5', 'val5', 'EOF']
|
|
28 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline, [])], toks)
|
318
|
29
|
|
30 def testLex3(self):
|
193
|
31 """ Test if lexer fails on a token that is invalid """
|
191
|
32 asmline = '0z4: mov rax, rbx $ '
|
200
|
33 with self.assertRaises(CompilerError):
|
341
|
34 list(tokenize(asmline, []))
|
198
|
35
|
290
|
36
|
198
|
37 class AssemblerParsingTestCase(unittest.TestCase):
|
346
|
38 """
|
198
|
39 Tests the assembler parts
|
|
40 """
|
199
|
41 def setUp(self):
|
341
|
42 self.skipTest('refactoring asm parser')
|
337
|
43 self.parser = asmParser
|
|
44 self.stack = []
|
|
45
|
|
46 def emit(self, x):
|
|
47 self.stack.append(x)
|
|
48
|
|
49 def parse_line(self, line):
|
|
50 self.parser.parse(Lexer(line), self.emit)
|
191
|
51
|
|
52 def testParse(self):
|
|
53 asmline = 'lab1: mov rax, rbx'
|
337
|
54 self.parse_line(asmline)
|
191
|
55
|
236
|
56 def expectTree(self, asmline, stack):
|
337
|
57 self.parse_line(asmline)
|
|
58 self.assertSequenceEqual(stack, self.stack)
|
236
|
59
|
193
|
60 def testParse2(self):
|
|
61 asmline = 'a: mov rax, [rbx + 2]'
|
195
|
62 output = []
|
|
63 output.append(ALabel('a'))
|
|
64 output.append(AInstruction('mov', [ASymbol('rax'), AUnop('[]', ASymbol('rbx') + ANumber(2))]))
|
236
|
65 self.expectTree(asmline, output)
|
194
|
66
|
|
67 def testParse3(self):
|
|
68 # A label must be optional:
|
|
69 asmline = 'mov rax, 1'
|
199
|
70 output = [AInstruction('mov', [ASymbol('rax'), ANumber(1)])]
|
236
|
71 self.expectTree(asmline, output)
|
195
|
72
|
|
73 def testParse4(self):
|
|
74 # Test 3 operands:
|
|
75 asmline = 'add rax, [4*rbx + 22], rcx'
|
|
76 ops = []
|
|
77 ops.append(ASymbol('rax'))
|
|
78 ops.append(AUnop('[]', ANumber(4) * ASymbol('rbx') + ANumber(22)))
|
|
79 ops.append(ASymbol('rcx'))
|
199
|
80 output = [AInstruction('add', ops)]
|
236
|
81 self.expectTree(asmline, output)
|
195
|
82
|
|
83 def testParse5(self):
|
|
84 # An instruction must be optional:
|
|
85 asmline = 'lab1:'
|
|
86 output = []
|
|
87 output.append(ALabel('lab1'))
|
236
|
88 self.expectTree(asmline, output)
|
196
|
89
|
|
90 def testParse6(self):
|
|
91 # A line can be empty
|
337
|
92 self.parse_line('')
|
201
|
93
|
236
|
94
|
|
95 class OustreamTestCase(unittest.TestCase):
|
|
96 def test1(self):
|
335
|
97 obj = ObjectFile()
|
342
|
98 o = BinaryOutputStream(obj)
|
348
|
99 o.select_section('.text')
|
236
|
100 o.emit(Label('a'))
|
335
|
101 self.assertSequenceEqual(bytes(), obj.get_section('.text').data)
|
236
|
102
|
|
103
|
|
104 class AsmTestCaseBase(unittest.TestCase):
|
292
|
105 """ Base testcase for assembly """
|
236
|
106 def feed(self, line):
|
346
|
107 self.a.assemble(line, self.ostream)
|
236
|
108
|
|
109 def check(self, hexstr):
|
335
|
110 l = Linker()
|
|
111 self.obj = l.link([self.obj])
|
|
112 data = bytes(self.obj.get_section('.text').data)
|
|
113 self.assertSequenceEqual(bytes.fromhex(hexstr), data)
|
236
|
114
|
|
115
|
191
|
116 if __name__ == '__main__':
|
|
117 unittest.main()
|