annotate python/testasm.py @ 195:37ac6c016e0f

Expanded asm subsystem
author Windel Bouwman
date Fri, 31 May 2013 21:06:44 +0200
parents b01429a5d695
children ec2b423cdbea
rev   line source
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
1 #!/usr/bin/python
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
2
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
3 import unittest
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
4 import libasm
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
5 import ppci
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
6 from libasm import AInstruction, ABinop, AUnop, ASymbol, ALabel, ANumber
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
7
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
8 class AssemblerTestCase(unittest.TestCase):
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
9 """
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
10 Tests the assembler parts
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
11 """
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
12 def setUp(self):
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
13 pass
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
14
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
15 def testLex0(self):
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
16 """ Check if the lexer is OK """
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
17 asmline, toks = 'mov rax, rbx ', ['ID', 'ID', ',', 'ID']
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
18 self.assertSequenceEqual([tok.typ for tok in libasm.tokenize(asmline)], toks)
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
19
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
20 def testLex1(self):
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
21 """ Test if lexer correctly maps some tokens """
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
22 asmline, toks = 'lab1: mov rax, rbx ', ['ID', ':', 'ID', 'ID', ',', 'ID']
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
23 self.assertSequenceEqual([tok.typ for tok in libasm.tokenize(asmline)], toks)
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
24
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
25 def testLex1(self):
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
26 """ Test if lexer correctly maps some tokens """
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
27 asmline, toks = 'mov 3.13 0xC 13', ['ID', 'REAL', 'NUMBER', 'NUMBER']
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
28 self.assertSequenceEqual([tok.typ for tok in libasm.tokenize(asmline)], toks)
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
29
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
30 def testLex2(self):
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
31 """ Test if lexer fails on a token that is invalid """
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
32 asmline = '0z4: mov rax, rbx $ '
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
33 with self.assertRaises(ppci.CompilerError):
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
34 list(libasm.tokenize(asmline))
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
35
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
36 def testParse(self):
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
37 asmline = 'lab1: mov rax, rbx'
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
38 a = libasm.Assembler()
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
39 a.parse_line(asmline)
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
40
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
41 def testParse2(self):
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
42 asmline = 'a: mov rax, [rbx + 2]'
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
43 a = libasm.Assembler()
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
44 a.parse_line(asmline)
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
45 output = []
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
46 output.append(ALabel('a'))
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
47 output.append(AInstruction('mov', [ASymbol('rax'), AUnop('[]', ASymbol('rbx') + ANumber(2))]))
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
48 self.assertSequenceEqual(output, a.output)
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
49
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
50 def testParse3(self):
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
51 # A label must be optional:
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
52 asmline = 'mov rax, 1'
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
53 a = libasm.Assembler()
b01429a5d695 Fixed test
Windel Bouwman
parents: 193
diff changeset
54 a.parse_line(asmline)
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
55 output = []
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
56 output.append(AInstruction('mov', [ASymbol('rax'), ANumber(1)]))
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
57 self.assertSequenceEqual(output, a.output)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
58
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
59 def testParse4(self):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
60 # Test 3 operands:
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
61 asmline = 'add rax, [4*rbx + 22], rcx'
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
62 a = libasm.Assembler()
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
63 a.parse_line(asmline)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
64 output = []
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
65 ops = []
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
66 ops.append(ASymbol('rax'))
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
67 ops.append(AUnop('[]', ANumber(4) * ASymbol('rbx') + ANumber(22)))
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
68 ops.append(ASymbol('rcx'))
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
69 output.append(AInstruction('add', ops))
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
70 self.assertSequenceEqual(output, a.output)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
71
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
72 def testParse5(self):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
73 # An instruction must be optional:
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
74 asmline = 'lab1:'
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
75 a = libasm.Assembler()
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
76 a.parse_line(asmline)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
77 output = []
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
78 output.append(ALabel('lab1'))
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
79 self.assertSequenceEqual(output, a.output)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
80
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
81 def testX86(self):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
82 # TODO
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
83 pass
193
f091e7d70996 Added even more checks
Windel Bouwman
parents: 191
diff changeset
84
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
85 if __name__ == '__main__':
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
86 unittest.main()
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents:
diff changeset
87