annotate python/target/armtoken.py @ 341:4d204f6f7d4e devel

Rewrite of assembler parts
author Windel Bouwman
date Fri, 28 Feb 2014 18:07:14 +0100
parents c7cc54c0dfdf
children
rev   line source
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
1
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
2 import struct
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
3
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
4 def u16(h):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
5 return struct.pack('<H', h)
340
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
6
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
7 def val2bit(v, bits):
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
8 b = []
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
9 for i in range(bits):
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
10 b.append(bool((1<<i) & v))
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
11 #b.reverse()
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
12 return b
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
13
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
14
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
15 def bit_range(b, e):
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
16 getter = lambda s: s[b:e]
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
17 def setter(s, v):
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
18 s[b:e] = v
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
19 return property(getter, setter)
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
20
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
21
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
22 class Token:
340
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
23 def set_bit(self, i, value):
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
24 value = bool(value)
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
25 assert i in range(0, 16)
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
26 mask = 1 << i
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
27 if value:
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
28 self.bit_value |= mask
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
29 else:
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
30 self.bit_value &= (~mask)
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
31
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
32 def __getitem__(self, key):
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
33 return False
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
34
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
35 def __setitem__(self, key, value):
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
36 if type(key) is int:
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
37 self.set_bit(key, value)
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
38 elif type(key) is slice:
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
39 assert key.step is None
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
40 bits = key.stop - key.start
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
41 value_bits = val2bit(value, bits)
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
42 for i in range(key.start, key.stop):
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
43 self.set_bit(i, value_bits[i - key.start])
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
44 else:
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
45 raise KeyError()
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
46
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
47 class ThumbToken(Token):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
48 def __init__(self):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
49 self.bit_value = 0
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
50
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
51
340
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
52 rd = bit_range(0, 3)
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
53
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
54 def encode(self):
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
55 return u16(self.bit_value)
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
56
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
57
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
58 class ArmToken(Token):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
59 def __init__(self):
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 340
diff changeset
60 self.bit_value = 0
340
c7cc54c0dfdf Test featurebranch
Windel Bouwman
parents:
diff changeset
61