191
|
1 #!/usr/bin/python
|
|
2
|
199
|
3 import unittest, cProfile
|
200
|
4 from ppci import CompilerError
|
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 $ '
|
200
|
29 with self.assertRaises(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):
|
200
|
82 def testWithoutTarget(self):
|
|
83 a = Assembler()
|
|
84 with self.assertRaises(CompilerError):
|
|
85 a.assemble_line('')
|
|
86 @unittest.skip
|
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
|
200
|
99 class AssemblerMSP430TestCase(unittest.TestCase):
|
199
|
100 def setUp(self):
|
200
|
101 self.t = msp430.MSP430()
|
|
102 self.a = Assembler(target=self.t)
|
|
103
|
|
104 def testMapInstruction(self):
|
|
105 i = AInstruction('mov', [ASymbol('r14'), ASymbol('r15')])
|
|
106 self.t.mapInstruction(i)
|
|
107
|
|
108 def testMapOperand(self):
|
|
109 o = ASymbol('r14')
|
|
110 mo = self.t.mapOperand(o)
|
|
111 self.assertEqual(mo, msp430.r14)
|
|
112
|
|
113 def testMapOperandIndirection(self):
|
|
114 o = AUnop('[]', ASymbol('r14'))
|
|
115 mo = self.t.mapOperand(o)
|
199
|
116
|
|
117 def testMov(self):
|
|
118 line1 = "mov r14, r15"
|
|
119 self.a.assemble_line(line1)
|
|
120
|
|
121 def testAdd(self):
|
200
|
122 line1 = "add r14, r15"
|
199
|
123 self.a.assemble_line(line1)
|
|
124
|
|
125
|
191
|
126 if __name__ == '__main__':
|
199
|
127 # cProfile.run('unittest.main()')
|
191
|
128 unittest.main()
|
|
129
|