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
|
199
|
7 import msp430
|
191
|
8
|
198
|
9 class AssemblerLexingCase(unittest.TestCase):
|
|
10 """ Tests the assemblers lexer """
|
191
|
11
|
|
12 def testLex0(self):
|
|
13 """ Check if the lexer is OK """
|
|
14 asmline, toks = 'mov rax, rbx ', ['ID', 'ID', ',', 'ID']
|
198
|
15 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline)], toks)
|
191
|
16
|
|
17 def testLex1(self):
|
193
|
18 """ Test if lexer correctly maps some tokens """
|
191
|
19 asmline, toks = 'lab1: mov rax, rbx ', ['ID', ':', 'ID', 'ID', ',', 'ID']
|
198
|
20 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline)], toks)
|
191
|
21
|
193
|
22 def testLex1(self):
|
|
23 """ Test if lexer correctly maps some tokens """
|
|
24 asmline, toks = 'mov 3.13 0xC 13', ['ID', 'REAL', 'NUMBER', 'NUMBER']
|
198
|
25 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline)], toks)
|
193
|
26
|
191
|
27 def testLex2(self):
|
193
|
28 """ Test if lexer fails on a token that is invalid """
|
191
|
29 asmline = '0z4: mov rax, rbx $ '
|
200
|
30 with self.assertRaises(CompilerError):
|
198
|
31 list(tokenize(asmline))
|
|
32
|
|
33 class AssemblerParsingTestCase(unittest.TestCase):
|
|
34 """
|
|
35 Tests the assembler parts
|
|
36 """
|
199
|
37 def setUp(self):
|
|
38 self.a = Assembler()
|
191
|
39
|
|
40 def testParse(self):
|
|
41 asmline = 'lab1: mov rax, rbx'
|
199
|
42 self.a.parse_line(asmline)
|
191
|
43
|
193
|
44 def testParse2(self):
|
|
45 asmline = 'a: mov rax, [rbx + 2]'
|
199
|
46 self.a.parse_line(asmline)
|
195
|
47 output = []
|
|
48 output.append(ALabel('a'))
|
|
49 output.append(AInstruction('mov', [ASymbol('rax'), AUnop('[]', ASymbol('rbx') + ANumber(2))]))
|
199
|
50 self.assertSequenceEqual(output, self.a.output)
|
194
|
51
|
|
52 def testParse3(self):
|
|
53 # A label must be optional:
|
|
54 asmline = 'mov rax, 1'
|
199
|
55 self.a.parse_line(asmline)
|
|
56 output = [AInstruction('mov', [ASymbol('rax'), ANumber(1)])]
|
|
57 self.assertSequenceEqual(output, self.a.output)
|
195
|
58
|
|
59 def testParse4(self):
|
|
60 # Test 3 operands:
|
|
61 asmline = 'add rax, [4*rbx + 22], rcx'
|
199
|
62 self.a.parse_line(asmline)
|
195
|
63 ops = []
|
|
64 ops.append(ASymbol('rax'))
|
|
65 ops.append(AUnop('[]', ANumber(4) * ASymbol('rbx') + ANumber(22)))
|
|
66 ops.append(ASymbol('rcx'))
|
199
|
67 output = [AInstruction('add', ops)]
|
|
68 self.assertSequenceEqual(output, self.a.output)
|
195
|
69
|
|
70 def testParse5(self):
|
|
71 # An instruction must be optional:
|
|
72 asmline = 'lab1:'
|
199
|
73 self.a.parse_line(asmline)
|
195
|
74 output = []
|
|
75 output.append(ALabel('lab1'))
|
199
|
76 self.assertSequenceEqual(output, self.a.output)
|
196
|
77
|
|
78 def testParse6(self):
|
|
79 # A line can be empty
|
199
|
80 self.a.parse_line('')
|
195
|
81
|
198
|
82 class AssemblerOtherTestCase(unittest.TestCase):
|
200
|
83 def testWithoutTarget(self):
|
|
84 a = Assembler()
|
|
85 with self.assertRaises(CompilerError):
|
|
86 a.assemble_line('')
|
201
|
87
|
200
|
88 @unittest.skip
|
195
|
89 def testX86(self):
|
198
|
90 testsrc = """ ; tst
|
196
|
91 begin:
|
198
|
92 mov rax, rbx ; 0x48, 0x89, 0xd8
|
|
93 xor rcx, rbx ; 0x48, 0x31, 0xd9
|
|
94 inc rcx ; 0x48 0xff 0xc1
|
196
|
95 """
|
198
|
96 a = Assembler()
|
196
|
97 a.assemble(testsrc)
|
|
98 # Compare with nasm output:
|
|
99 nasmbytes = [0x48, 0x89, 0xd8, 0x48, 0x31, 0xd9, 0x48, 0xff, 0xc1]
|
193
|
100
|
200
|
101 class AssemblerMSP430TestCase(unittest.TestCase):
|
199
|
102 def setUp(self):
|
201
|
103 self.t = msp430.msp430target
|
200
|
104 self.a = Assembler(target=self.t)
|
|
105
|
201
|
106 def testMapMovInstruction(self):
|
200
|
107 i = AInstruction('mov', [ASymbol('r14'), ASymbol('r15')])
|
201
|
108 ri = self.t.mapInstruction(i)
|
200
|
109
|
201
|
110 def testMapRetiInstruction(self):
|
|
111 i = AInstruction('reti', [])
|
|
112 ri = self.t.mapInstruction(i)
|
|
113
|
|
114 @unittest.skip
|
200
|
115 def testMapOperand(self):
|
|
116 o = ASymbol('r14')
|
|
117 mo = self.t.mapOperand(o)
|
|
118 self.assertEqual(mo, msp430.r14)
|
|
119
|
201
|
120 @unittest.skip
|
200
|
121 def testMapOperandIndirection(self):
|
|
122 o = AUnop('[]', ASymbol('r14'))
|
|
123 mo = self.t.mapOperand(o)
|
199
|
124
|
|
125 def testMov(self):
|
|
126 line1 = "mov r14, r15"
|
|
127 self.a.assemble_line(line1)
|
201
|
128 self.assertEqual(bytes([0x0F, 0x4E]), self.a.binout)
|
|
129
|
|
130 def testMov1337(self):
|
|
131 line1 = "mov 0x1337, r12"
|
|
132 self.a.assemble_line(line1)
|
|
133 self.assertEqual(bytes([0x3C, 0x40, 0x37, 0x13]), self.a.binout)
|
199
|
134
|
|
135 def testAdd(self):
|
201
|
136 line1 = "add r15, r13"
|
199
|
137 self.a.assemble_line(line1)
|
201
|
138 self.assertEqual(bytes([0x0D, 0x5F]), self.a.binout)
|
|
139
|
|
140 def testReti(self):
|
|
141 line1 = "reti"
|
|
142 self.a.assemble_line(line1)
|
|
143 self.assertEqual(bytes([0x0, 0x13]), self.a.binout)
|
|
144
|
|
145 def testMSPinstructionCount(self):
|
|
146 """ Check that there are 27 instructions """
|
|
147 self.assertEqual(27, len(self.t.instructions))
|
199
|
148
|
|
149
|
191
|
150 if __name__ == '__main__':
|
199
|
151 # cProfile.run('unittest.main()')
|
191
|
152 unittest.main()
|
|
153
|