annotate python/ppci/target/token.py @ 394:988f3fb861e4

c3 code generator rewrite
author Windel Bouwman
date Thu, 22 May 2014 08:14:12 +0200
parents 86b02c98a717
children c0d9837acde8
rev   line source
342
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
1
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
2 import struct
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
3
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
4 def u16(h):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
5 return struct.pack('<H', h)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
6
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
7
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
8 def u32(x):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
9 return struct.pack('<I', x)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
10
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
11
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
12 def val2bit(v, bits):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
13 b = []
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
14 for i in range(bits):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
15 b.append(bool((1<<i) & v))
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
16 return b
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
17
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
18
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
19 def bit_range(b, e):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
20 """ Property generator function """
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
21 getter = lambda s: s[b:e]
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
22 def setter(s, v):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
23 s[b:e] = v
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
24 return property(getter, setter)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
25
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
26
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
27 class Token:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
28 def __init__(self, bitsize):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
29 self.bitsize = bitsize
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
30 self.bit_value = 0
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
31
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
32 def set_bit(self, i, value):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
33 value = bool(value)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
34 assert i in range(0, self.bitsize)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
35 mask = 1 << i
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
36 if value:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
37 self.bit_value |= mask
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
38 else:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
39 self.bit_value &= (~mask)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
40
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
41 def __getitem__(self, key):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
42 return False
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
43
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
44 def __setitem__(self, key, value):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
45 if type(key) is int:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
46 self.set_bit(key, value)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
47 elif type(key) is slice:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
48 assert key.step is None
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
49 bits = key.stop - key.start
394
988f3fb861e4 c3 code generator rewrite
Windel Bouwman
parents: 342
diff changeset
50 assert value < (2**bits)
342
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
51 value_bits = val2bit(value, bits)
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
52 for i in range(key.start, key.stop):
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
53 self.set_bit(i, value_bits[i - key.start])
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
54 else:
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
55 raise KeyError()
86b02c98a717 Moved target directory
Windel Bouwman
parents:
diff changeset
56