comparison python/testasm.py @ 200:5e391d9a3381

Split off asm nodes
author Windel Bouwman
date Sun, 09 Jun 2013 16:06:49 +0200
parents a690473b79e2
children d5debbfc0200
comparison
equal deleted inserted replaced
199:a690473b79e2 200:5e391d9a3381
1 #!/usr/bin/python 1 #!/usr/bin/python
2 2
3 import unittest, cProfile 3 import unittest, cProfile
4 import ppci 4 from ppci import CompilerError
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 import msp430
7 7
8 class AssemblerLexingCase(unittest.TestCase): 8 class AssemblerLexingCase(unittest.TestCase):
9 """ Tests the assemblers lexer """ 9 """ Tests the assemblers lexer """
24 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline)], toks) 24 self.assertSequenceEqual([tok.typ for tok in tokenize(asmline)], toks)
25 25
26 def testLex2(self): 26 def testLex2(self):
27 """ Test if lexer fails on a token that is invalid """ 27 """ Test if lexer fails on a token that is invalid """
28 asmline = '0z4: mov rax, rbx $ ' 28 asmline = '0z4: mov rax, rbx $ '
29 with self.assertRaises(ppci.CompilerError): 29 with self.assertRaises(CompilerError):
30 list(tokenize(asmline)) 30 list(tokenize(asmline))
31 31
32 class AssemblerParsingTestCase(unittest.TestCase): 32 class AssemblerParsingTestCase(unittest.TestCase):
33 """ 33 """
34 Tests the assembler parts 34 Tests the assembler parts
77 def testParse6(self): 77 def testParse6(self):
78 # A line can be empty 78 # A line can be empty
79 self.a.parse_line('') 79 self.a.parse_line('')
80 80
81 class AssemblerOtherTestCase(unittest.TestCase): 81 class AssemblerOtherTestCase(unittest.TestCase):
82 def testWithoutTarget(self):
83 a = Assembler()
84 with self.assertRaises(CompilerError):
85 a.assemble_line('')
86 @unittest.skip
82 def testX86(self): 87 def testX86(self):
83 testsrc = """ ; tst 88 testsrc = """ ; tst
84 begin: 89 begin:
85 mov rax, rbx ; 0x48, 0x89, 0xd8 90 mov rax, rbx ; 0x48, 0x89, 0xd8
86 xor rcx, rbx ; 0x48, 0x31, 0xd9 91 xor rcx, rbx ; 0x48, 0x31, 0xd9
89 a = Assembler() 94 a = Assembler()
90 a.assemble(testsrc) 95 a.assemble(testsrc)
91 # Compare with nasm output: 96 # Compare with nasm output:
92 nasmbytes = [0x48, 0x89, 0xd8, 0x48, 0x31, 0xd9, 0x48, 0xff, 0xc1] 97 nasmbytes = [0x48, 0x89, 0xd8, 0x48, 0x31, 0xd9, 0x48, 0xff, 0xc1]
93 98
94 class AssemblerOtherTestCase(unittest.TestCase): 99 class AssemblerMSP430TestCase(unittest.TestCase):
95 def setUp(self): 100 def setUp(self):
96 self.a = Assembler(target=msp430.MSP430()) 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)
97 116
98 def testMov(self): 117 def testMov(self):
99 line1 = "mov r14, r15" 118 line1 = "mov r14, r15"
100 self.a.assemble_line(line1) 119 self.a.assemble_line(line1)
101 120
102 def testAdd(self): 121 def testAdd(self):
103 line1 = "addw r14, r15" 122 line1 = "add r14, r15"
104 self.a.assemble_line(line1) 123 self.a.assemble_line(line1)
105 124
106 125
107 if __name__ == '__main__': 126 if __name__ == '__main__':
108 # cProfile.run('unittest.main()') 127 # cProfile.run('unittest.main()')