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