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