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
|
202
|
8 import arm_cm3
|
191
|
9
|
198
|
10 class AssemblerLexingCase(unittest.TestCase):
|
|
11 """ Tests the assemblers lexer """
|
191
|
12
|
|
13 def testLex0(self):
|
|
14 """ Check if the lexer is OK """
|
|
15 asmline, toks = 'mov rax, rbx ', ['ID', 'ID', ',', 'ID']
|
198
|
16 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline)], toks)
|
191
|
17
|
|
18 def testLex1(self):
|
193
|
19 """ Test if lexer correctly maps some tokens """
|
191
|
20 asmline, toks = 'lab1: mov rax, rbx ', ['ID', ':', 'ID', 'ID', ',', 'ID']
|
198
|
21 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline)], toks)
|
191
|
22
|
193
|
23 def testLex1(self):
|
|
24 """ Test if lexer correctly maps some tokens """
|
|
25 asmline, toks = 'mov 3.13 0xC 13', ['ID', 'REAL', 'NUMBER', 'NUMBER']
|
198
|
26 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline)], toks)
|
193
|
27
|
191
|
28 def testLex2(self):
|
193
|
29 """ Test if lexer fails on a token that is invalid """
|
191
|
30 asmline = '0z4: mov rax, rbx $ '
|
200
|
31 with self.assertRaises(CompilerError):
|
198
|
32 list(tokenize(asmline))
|
|
33
|
|
34 class AssemblerParsingTestCase(unittest.TestCase):
|
|
35 """
|
|
36 Tests the assembler parts
|
|
37 """
|
199
|
38 def setUp(self):
|
|
39 self.a = Assembler()
|
191
|
40
|
|
41 def testParse(self):
|
|
42 asmline = 'lab1: mov rax, rbx'
|
199
|
43 self.a.parse_line(asmline)
|
191
|
44
|
193
|
45 def testParse2(self):
|
|
46 asmline = 'a: mov rax, [rbx + 2]'
|
199
|
47 self.a.parse_line(asmline)
|
195
|
48 output = []
|
|
49 output.append(ALabel('a'))
|
|
50 output.append(AInstruction('mov', [ASymbol('rax'), AUnop('[]', ASymbol('rbx') + ANumber(2))]))
|
199
|
51 self.assertSequenceEqual(output, self.a.output)
|
194
|
52
|
|
53 def testParse3(self):
|
|
54 # A label must be optional:
|
|
55 asmline = 'mov rax, 1'
|
199
|
56 self.a.parse_line(asmline)
|
|
57 output = [AInstruction('mov', [ASymbol('rax'), ANumber(1)])]
|
|
58 self.assertSequenceEqual(output, self.a.output)
|
195
|
59
|
|
60 def testParse4(self):
|
|
61 # Test 3 operands:
|
|
62 asmline = 'add rax, [4*rbx + 22], rcx'
|
199
|
63 self.a.parse_line(asmline)
|
195
|
64 ops = []
|
|
65 ops.append(ASymbol('rax'))
|
|
66 ops.append(AUnop('[]', ANumber(4) * ASymbol('rbx') + ANumber(22)))
|
|
67 ops.append(ASymbol('rcx'))
|
199
|
68 output = [AInstruction('add', ops)]
|
|
69 self.assertSequenceEqual(output, self.a.output)
|
195
|
70
|
|
71 def testParse5(self):
|
|
72 # An instruction must be optional:
|
|
73 asmline = 'lab1:'
|
199
|
74 self.a.parse_line(asmline)
|
195
|
75 output = []
|
|
76 output.append(ALabel('lab1'))
|
199
|
77 self.assertSequenceEqual(output, self.a.output)
|
196
|
78
|
|
79 def testParse6(self):
|
|
80 # A line can be empty
|
199
|
81 self.a.parse_line('')
|
195
|
82
|
198
|
83 class AssemblerOtherTestCase(unittest.TestCase):
|
200
|
84 def testWithoutTarget(self):
|
|
85 a = Assembler()
|
|
86 with self.assertRaises(CompilerError):
|
|
87 a.assemble_line('')
|
201
|
88
|
200
|
89 @unittest.skip
|
195
|
90 def testX86(self):
|
198
|
91 testsrc = """ ; tst
|
196
|
92 begin:
|
198
|
93 mov rax, rbx ; 0x48, 0x89, 0xd8
|
|
94 xor rcx, rbx ; 0x48, 0x31, 0xd9
|
|
95 inc rcx ; 0x48 0xff 0xc1
|
196
|
96 """
|
198
|
97 a = Assembler()
|
196
|
98 a.assemble(testsrc)
|
|
99 # Compare with nasm output:
|
|
100 nasmbytes = [0x48, 0x89, 0xd8, 0x48, 0x31, 0xd9, 0x48, 0xff, 0xc1]
|
193
|
101
|
200
|
102 class AssemblerMSP430TestCase(unittest.TestCase):
|
199
|
103 def setUp(self):
|
201
|
104 self.t = msp430.msp430target
|
200
|
105 self.a = Assembler(target=self.t)
|
|
106
|
201
|
107 def testMapMovInstruction(self):
|
200
|
108 i = AInstruction('mov', [ASymbol('r14'), ASymbol('r15')])
|
201
|
109 ri = self.t.mapInstruction(i)
|
200
|
110
|
201
|
111 def testMapRetiInstruction(self):
|
|
112 i = AInstruction('reti', [])
|
|
113 ri = self.t.mapInstruction(i)
|
|
114
|
|
115 @unittest.skip
|
200
|
116 def testMapOperand(self):
|
|
117 o = ASymbol('r14')
|
|
118 mo = self.t.mapOperand(o)
|
|
119 self.assertEqual(mo, msp430.r14)
|
|
120
|
201
|
121 @unittest.skip
|
200
|
122 def testMapOperandIndirection(self):
|
|
123 o = AUnop('[]', ASymbol('r14'))
|
|
124 mo = self.t.mapOperand(o)
|
199
|
125
|
|
126 def testMov(self):
|
|
127 line1 = "mov r14, r15"
|
|
128 self.a.assemble_line(line1)
|
201
|
129 self.assertEqual(bytes([0x0F, 0x4E]), self.a.binout)
|
|
130
|
|
131 def testMov1337(self):
|
|
132 line1 = "mov 0x1337, r12"
|
|
133 self.a.assemble_line(line1)
|
202
|
134 self.assertEqual(bytes.fromhex('3C403713'), self.a.binout)
|
199
|
135
|
|
136 def testAdd(self):
|
201
|
137 line1 = "add r15, r13"
|
199
|
138 self.a.assemble_line(line1)
|
202
|
139 self.assertEqual(bytes.fromhex('0D5F'), self.a.binout)
|
201
|
140
|
|
141 def testReti(self):
|
|
142 line1 = "reti"
|
|
143 self.a.assemble_line(line1)
|
|
144 self.assertEqual(bytes([0x0, 0x13]), self.a.binout)
|
|
145
|
|
146 def testMSPinstructionCount(self):
|
|
147 """ Check that there are 27 instructions """
|
|
148 self.assertEqual(27, len(self.t.instructions))
|
199
|
149
|
|
150
|
202
|
151 class AssemblerARMTestCase(unittest.TestCase):
|
|
152 def setUp(self):
|
|
153 self.t = arm_cm3.armtarget
|
|
154 self.a = Assembler(target=self.t)
|
|
155
|
206
|
156 def feed(self, line):
|
|
157 self.a.assemble(line)
|
|
158
|
|
159 def check(self, hexstr):
|
|
160 self.assertEqual(bytes.fromhex(hexstr), self.a.binout)
|
|
161
|
202
|
162 def testMapOperand(self):
|
|
163 pass
|
|
164
|
|
165 def testMovImm8(self):
|
206
|
166 self.feed('mov r4, 100')
|
|
167 self.check('6424')
|
202
|
168
|
|
169 def testYield(self):
|
206
|
170 self.feed('yield')
|
|
171 self.check('10bf')
|
|
172
|
|
173 def testPush(self):
|
|
174 self.feed('push {r2,r3,lr}')
|
|
175 self.check('0cb5')
|
|
176
|
|
177 def testPop(self):
|
|
178 self.feed('pop {r4-r6, pc}')
|
|
179 self.check('70bd')
|
202
|
180
|
203
|
181 def testSequence1(self):
|
206
|
182 self.feed('mov r5, 3')
|
|
183 self.feed('add r4, r5, 0')
|
|
184 self.feed('loop: add r6, r4, 7')
|
|
185 self.feed('cmp r6, 5')
|
207
|
186 self.check('0325 2c1c e61d 052e')
|
202
|
187
|
191
|
188 if __name__ == '__main__':
|
203
|
189 #cProfile.run('unittest.main()')
|
191
|
190 unittest.main()
|
|
191
|