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
|
337
|
6 from ppci.assembler import tokenize, Assembler, asmParser, Lexer
|
335
|
7 from ppci.objectfile import ObjectFile
|
|
8 from ppci.linker import Linker
|
236
|
9 import outstream
|
|
10 from target 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']
|
198
|
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']
|
198
|
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 """
|
|
28 asmline, toks = 'mov 3.13 0xC 13', ['ID', 'REAL', 'NUMBER', 'NUMBER', 'EOF']
|
|
29 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline)], toks)
|
|
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):
|
198
|
35 list(tokenize(asmline))
|
|
36
|
290
|
37
|
198
|
38 class AssemblerParsingTestCase(unittest.TestCase):
|
|
39 """
|
|
40 Tests the assembler parts
|
|
41 """
|
199
|
42 def setUp(self):
|
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()
|
|
98 o = outstream.BinaryOutputStream(obj)
|
236
|
99 o.selectSection('.text')
|
|
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):
|
|
107 self.a.assemble(line)
|
|
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()
|