191
|
1 #!/usr/bin/python
|
|
2
|
|
3 import unittest
|
|
4 import ppci
|
198
|
5 from asm import AInstruction, ABinop, AUnop, ASymbol, ALabel, ANumber, tokenize, Assembler
|
191
|
6
|
198
|
7 class AssemblerLexingCase(unittest.TestCase):
|
|
8 """ Tests the assemblers lexer """
|
191
|
9
|
|
10 def testLex0(self):
|
|
11 """ Check if the lexer is OK """
|
|
12 asmline, toks = 'mov rax, rbx ', ['ID', 'ID', ',', 'ID']
|
198
|
13 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline)], toks)
|
191
|
14
|
|
15 def testLex1(self):
|
193
|
16 """ Test if lexer correctly maps some tokens """
|
191
|
17 asmline, toks = 'lab1: mov rax, rbx ', ['ID', ':', 'ID', 'ID', ',', 'ID']
|
198
|
18 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline)], toks)
|
191
|
19
|
193
|
20 def testLex1(self):
|
|
21 """ Test if lexer correctly maps some tokens """
|
|
22 asmline, toks = 'mov 3.13 0xC 13', ['ID', 'REAL', 'NUMBER', 'NUMBER']
|
198
|
23 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline)], toks)
|
193
|
24
|
191
|
25 def testLex2(self):
|
193
|
26 """ Test if lexer fails on a token that is invalid """
|
191
|
27 asmline = '0z4: mov rax, rbx $ '
|
|
28 with self.assertRaises(ppci.CompilerError):
|
198
|
29 list(tokenize(asmline))
|
|
30
|
|
31 class AssemblerParsingTestCase(unittest.TestCase):
|
|
32 """
|
|
33 Tests the assembler parts
|
|
34 """
|
191
|
35
|
|
36 def testParse(self):
|
|
37 asmline = 'lab1: mov rax, rbx'
|
198
|
38 a = Assembler()
|
194
|
39 a.parse_line(asmline)
|
191
|
40
|
193
|
41 def testParse2(self):
|
|
42 asmline = 'a: mov rax, [rbx + 2]'
|
198
|
43 a = Assembler()
|
194
|
44 a.parse_line(asmline)
|
195
|
45 output = []
|
|
46 output.append(ALabel('a'))
|
|
47 output.append(AInstruction('mov', [ASymbol('rax'), AUnop('[]', ASymbol('rbx') + ANumber(2))]))
|
|
48 self.assertSequenceEqual(output, a.output)
|
194
|
49
|
|
50 def testParse3(self):
|
|
51 # A label must be optional:
|
|
52 asmline = 'mov rax, 1'
|
198
|
53 a = Assembler()
|
194
|
54 a.parse_line(asmline)
|
195
|
55 output = []
|
|
56 output.append(AInstruction('mov', [ASymbol('rax'), ANumber(1)]))
|
|
57 self.assertSequenceEqual(output, a.output)
|
|
58
|
|
59 def testParse4(self):
|
|
60 # Test 3 operands:
|
|
61 asmline = 'add rax, [4*rbx + 22], rcx'
|
198
|
62 a = Assembler()
|
195
|
63 a.parse_line(asmline)
|
|
64 output = []
|
|
65 ops = []
|
|
66 ops.append(ASymbol('rax'))
|
|
67 ops.append(AUnop('[]', ANumber(4) * ASymbol('rbx') + ANumber(22)))
|
|
68 ops.append(ASymbol('rcx'))
|
|
69 output.append(AInstruction('add', ops))
|
|
70 self.assertSequenceEqual(output, a.output)
|
|
71
|
|
72 def testParse5(self):
|
|
73 # An instruction must be optional:
|
|
74 asmline = 'lab1:'
|
198
|
75 a = Assembler()
|
195
|
76 a.parse_line(asmline)
|
|
77 output = []
|
|
78 output.append(ALabel('lab1'))
|
|
79 self.assertSequenceEqual(output, a.output)
|
196
|
80
|
|
81 def testParse6(self):
|
|
82 # A line can be empty
|
198
|
83 a = Assembler()
|
196
|
84 a.parse_line('')
|
195
|
85
|
198
|
86 class AssemblerOtherTestCase(unittest.TestCase):
|
195
|
87 def testX86(self):
|
198
|
88 testsrc = """ ; tst
|
196
|
89 begin:
|
198
|
90 mov rax, rbx ; 0x48, 0x89, 0xd8
|
|
91 xor rcx, rbx ; 0x48, 0x31, 0xd9
|
|
92 inc rcx ; 0x48 0xff 0xc1
|
196
|
93 """
|
198
|
94 a = Assembler()
|
196
|
95 a.assemble(testsrc)
|
|
96 # Compare with nasm output:
|
|
97 nasmbytes = [0x48, 0x89, 0xd8, 0x48, 0x31, 0xd9, 0x48, 0xff, 0xc1]
|
193
|
98
|
191
|
99 if __name__ == '__main__':
|
|
100 unittest.main()
|
|
101
|