annotate python/target/target_x86.py @ 336:d1ecc493384e

Added spiffy armtoken class for bit fiddeling. Added cool test that checks for build repeatability
author Windel Bouwman
date Wed, 19 Feb 2014 22:32:15 +0100
parents b07d28a5ca56
children
rev   line source
291
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
1 from target import Register, Instruction, Target
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
2
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
3 class x86Register(Register):
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
4 def __init__(self, name):
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
5 self.name = name
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
6
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
7 class REG16(x86Register):
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
8 pass
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
9
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
10 def addRegs(cls, names):
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
11 for name in names:
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
12 r = cls(name)
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
13 globals()[name] = r
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
14
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
15 addRegs(REG16, ['ax', 'bx', 'cx'])
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
16
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
17 regs = """
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
18 ax; reg16
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
19 """
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
20
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
21 class MO:
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
22 def __init__(self):
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
23 pass
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
24
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
25 instrs = """
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
26 add; 0x0; mem8/reg8; reg8
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
27 """
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
28
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
29 # machine op table:
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
30 mot = []
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
31
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
32 for i in instrs.split('\n'):
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
33 i = i.strip()
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
34 if i:
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
35 print('INS:', i)
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
36 mnemonic, opcode, op1, op2 = [a.strip() for a in i.split(';')]
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
37 print(op1.split('/'), op2.split('/'))
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
38
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
39
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
40 print(mot)
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
41
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
42 # Test first with these 3 instructions:
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
43 """
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
44 mov reg64, reg64 : opcode=0x89
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
45 xor reg64, reg64 : opcode=0x31
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
46 inc reg64 : opcode=0xff
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
47 """
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
48
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
49 class x86Machine:
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
50 def __init__(self):
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
51 self.table = []
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
52 self.table.append((0x0, 'add', 'reg8/mem8, reg8'))
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
53 self.table.append((0x1, 'add', 'reg16/mem16/reg32/mem32, reg16/reg32'))
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
54 self.table.append((0x2, 'add', 'reg8, reg8/mem8'))
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
55 def forMnemonic(self, m):
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
56 return [i for i in self.table if i[1] == m]
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
57 def emit(self, m, ops):
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
58 print(m, ops)
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
59 ops = self.forMnemonic(m)
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
60 print(ops)
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
61
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
62
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
63 if __name__ == '__main__':
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
64 m = x86Machine()
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
65 m.emit('add', [ax, cx])
b07d28a5ca56 Added some forgotten files
Windel Bouwman
parents:
diff changeset
66 m.emit('mov', [bx, 1337])