annotate python/ppci/target/token.py @ 398:c0d9837acde8

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