comparison python/testasm.py @ 199:a690473b79e2

Added msp430 target
author Windel Bouwman
date Fri, 07 Jun 2013 18:59:57 +0200
parents 33d50727a23c
children 5e391d9a3381
comparison
equal deleted inserted replaced
198:33d50727a23c 199:a690473b79e2
1 #!/usr/bin/python 1 #!/usr/bin/python
2 2
3 import unittest 3 import unittest, cProfile
4 import ppci 4 import ppci
5 from asm import AInstruction, ABinop, AUnop, ASymbol, ALabel, ANumber, tokenize, Assembler 5 from asm import AInstruction, ABinop, AUnop, ASymbol, ALabel, ANumber, tokenize, Assembler
6 import msp430
6 7
7 class AssemblerLexingCase(unittest.TestCase): 8 class AssemblerLexingCase(unittest.TestCase):
8 """ Tests the assemblers lexer """ 9 """ Tests the assemblers lexer """
9 10
10 def testLex0(self): 11 def testLex0(self):
30 31
31 class AssemblerParsingTestCase(unittest.TestCase): 32 class AssemblerParsingTestCase(unittest.TestCase):
32 """ 33 """
33 Tests the assembler parts 34 Tests the assembler parts
34 """ 35 """
36 def setUp(self):
37 self.a = Assembler()
35 38
36 def testParse(self): 39 def testParse(self):
37 asmline = 'lab1: mov rax, rbx' 40 asmline = 'lab1: mov rax, rbx'
38 a = Assembler() 41 self.a.parse_line(asmline)
39 a.parse_line(asmline)
40 42
41 def testParse2(self): 43 def testParse2(self):
42 asmline = 'a: mov rax, [rbx + 2]' 44 asmline = 'a: mov rax, [rbx + 2]'
43 a = Assembler() 45 self.a.parse_line(asmline)
44 a.parse_line(asmline)
45 output = [] 46 output = []
46 output.append(ALabel('a')) 47 output.append(ALabel('a'))
47 output.append(AInstruction('mov', [ASymbol('rax'), AUnop('[]', ASymbol('rbx') + ANumber(2))])) 48 output.append(AInstruction('mov', [ASymbol('rax'), AUnop('[]', ASymbol('rbx') + ANumber(2))]))
48 self.assertSequenceEqual(output, a.output) 49 self.assertSequenceEqual(output, self.a.output)
49 50
50 def testParse3(self): 51 def testParse3(self):
51 # A label must be optional: 52 # A label must be optional:
52 asmline = 'mov rax, 1' 53 asmline = 'mov rax, 1'
53 a = Assembler() 54 self.a.parse_line(asmline)
54 a.parse_line(asmline) 55 output = [AInstruction('mov', [ASymbol('rax'), ANumber(1)])]
55 output = [] 56 self.assertSequenceEqual(output, self.a.output)
56 output.append(AInstruction('mov', [ASymbol('rax'), ANumber(1)]))
57 self.assertSequenceEqual(output, a.output)
58 57
59 def testParse4(self): 58 def testParse4(self):
60 # Test 3 operands: 59 # Test 3 operands:
61 asmline = 'add rax, [4*rbx + 22], rcx' 60 asmline = 'add rax, [4*rbx + 22], rcx'
62 a = Assembler() 61 self.a.parse_line(asmline)
63 a.parse_line(asmline)
64 output = []
65 ops = [] 62 ops = []
66 ops.append(ASymbol('rax')) 63 ops.append(ASymbol('rax'))
67 ops.append(AUnop('[]', ANumber(4) * ASymbol('rbx') + ANumber(22))) 64 ops.append(AUnop('[]', ANumber(4) * ASymbol('rbx') + ANumber(22)))
68 ops.append(ASymbol('rcx')) 65 ops.append(ASymbol('rcx'))
69 output.append(AInstruction('add', ops)) 66 output = [AInstruction('add', ops)]
70 self.assertSequenceEqual(output, a.output) 67 self.assertSequenceEqual(output, self.a.output)
71 68
72 def testParse5(self): 69 def testParse5(self):
73 # An instruction must be optional: 70 # An instruction must be optional:
74 asmline = 'lab1:' 71 asmline = 'lab1:'
75 a = Assembler() 72 self.a.parse_line(asmline)
76 a.parse_line(asmline)
77 output = [] 73 output = []
78 output.append(ALabel('lab1')) 74 output.append(ALabel('lab1'))
79 self.assertSequenceEqual(output, a.output) 75 self.assertSequenceEqual(output, self.a.output)
80 76
81 def testParse6(self): 77 def testParse6(self):
82 # A line can be empty 78 # A line can be empty
83 a = Assembler() 79 self.a.parse_line('')
84 a.parse_line('')
85 80
86 class AssemblerOtherTestCase(unittest.TestCase): 81 class AssemblerOtherTestCase(unittest.TestCase):
87 def testX86(self): 82 def testX86(self):
88 testsrc = """ ; tst 83 testsrc = """ ; tst
89 begin: 84 begin:
94 a = Assembler() 89 a = Assembler()
95 a.assemble(testsrc) 90 a.assemble(testsrc)
96 # Compare with nasm output: 91 # Compare with nasm output:
97 nasmbytes = [0x48, 0x89, 0xd8, 0x48, 0x31, 0xd9, 0x48, 0xff, 0xc1] 92 nasmbytes = [0x48, 0x89, 0xd8, 0x48, 0x31, 0xd9, 0x48, 0xff, 0xc1]
98 93
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
99 if __name__ == '__main__': 107 if __name__ == '__main__':
108 # cProfile.run('unittest.main()')
100 unittest.main() 109 unittest.main()
101 110