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