| 1 |
|
| 2 |
""" Resume bytecode. It goes as following: |
| 3 |
|
| 4 |
# ----- resume section |
| 5 |
[total size of resume section] |
| 6 |
[number of failargs] |
| 7 |
[<length> <virtualizable object> <numb> <numb> <numb>] if vinfo is not None |
| 8 |
-OR- |
| 9 |
[1 <ginfo object>] if ginfo is not None |
| 10 |
-OR- |
| 11 |
[0] if both are None |
| 12 |
|
| 13 |
[<length> <virtual> <vref> <virtual> <vref>] for virtualrefs |
| 14 |
|
| 15 |
[<pc> <jitcode> <numb> <numb> <numb>] the frames |
| 16 |
[<pc> <jitcode> <numb> <numb>] |
| 17 |
... |
| 18 |
|
| 19 |
until the size of the resume section |
| 20 |
|
| 21 |
# ----- optimization section |
| 22 |
<more code> further sections according to bridgeopt.py |
| 23 |
""" |
| 24 |
|
| 25 |
from rpython.rtyper.lltypesystem import rffi, lltype |
| 26 |
from rpython.rlib import objectmodel |
| 27 |
|
| 28 |
NUMBERINGP = lltype.Ptr(lltype.GcForwardReference()) |
| 29 |
NUMBERING = lltype.GcStruct('Numbering', |
| 30 |
('code', lltype.Array(rffi.UCHAR))) |
| 31 |
NUMBERINGP.TO.become(NUMBERING) |
| 32 |
NULL_NUMBER = lltype.nullptr(NUMBERING) |
| 33 |
|
| 34 |
def append_numbering(lst, item): |
| 35 |
item = rffi.cast(lltype.Signed, item) |
| 36 |
item *= 2 |
| 37 |
if item < 0: |
| 38 |
item = -1 - item |
| 39 |
|
| 40 |
assert item >= 0 |
| 41 |
if item < 2**7: |
| 42 |
lst.append(rffi.cast(rffi.UCHAR, item)) |
| 43 |
elif item < 2**14: |
| 44 |
lst.append(rffi.cast(rffi.UCHAR, item | 0x80)) |
| 45 |
lst.append(rffi.cast(rffi.UCHAR, item >> 7)) |
| 46 |
else: |
| 47 |
assert item < 2**16 |
| 48 |
lst.append(rffi.cast(rffi.UCHAR, item | 0x80)) |
| 49 |
lst.append(rffi.cast(rffi.UCHAR, (item >> 7) | 0x80)) |
| 50 |
lst.append(rffi.cast(rffi.UCHAR, item >> 14)) |
| 51 |
|
| 52 |
|
| 53 |
def numb_next_item(numb, index): |
| 54 |
value = rffi.cast(lltype.Signed, numb.code[index]) |
| 55 |
index += 1 |
| 56 |
if value & (2**7): |
| 57 |
value &= 2**7 - 1 |
| 58 |
value |= rffi.cast(lltype.Signed, numb.code[index]) << 7 |
| 59 |
index += 1 |
| 60 |
if value & (2**14): |
| 61 |
value &= 2**14 - 1 |
| 62 |
value |= rffi.cast(lltype.Signed, numb.code[index]) << 14 |
| 63 |
index += 1 |
| 64 |
if value & 1: |
| 65 |
value = -1 - value |
| 66 |
value >>= 1 |
| 67 |
return value, index |
| 68 |
numb_next_item._always_inline_ = True |
| 69 |
|
| 70 |
def numb_next_n_items(numb, size, index): |
| 71 |
for i in range(size): |
| 72 |
_, index = numb_next_item(numb, index) |
| 73 |
return index |
| 74 |
|
| 75 |
def unpack_numbering(numb): |
| 76 |
l = [] |
| 77 |
i = 0 |
| 78 |
while i < len(numb.code): |
| 79 |
next, i = numb_next_item(numb, i) |
| 80 |
l.append(next) |
| 81 |
return l |
| 82 |
|
| 83 |
class Writer(object): |
| 84 |
def __init__(self, size=0): |
| 85 |
self.current = objectmodel.newlist_hint(size) |
| 86 |
|
| 87 |
def append_short(self, item): |
| 88 |
self.current.append(item) |
| 89 |
|
| 90 |
def append_int(self, item): |
| 91 |
short = rffi.cast(rffi.SHORT, item) |
| 92 |
assert rffi.cast(lltype.Signed, short) == item |
| 93 |
return self.append_short(short) |
| 94 |
|
| 95 |
def create_numbering(self): |
| 96 |
final = objectmodel.newlist_hint(len(self.current) * 3) |
| 97 |
for item in self.current: |
| 98 |
append_numbering(final, item) |
| 99 |
numb = lltype.malloc(NUMBERING, len(final)) |
| 100 |
for i, elt in enumerate(final): |
| 101 |
numb.code[i] = elt |
| 102 |
return numb |
| 103 |
|
| 104 |
def patch_current_size(self, index): |
| 105 |
self.patch(index, len(self.current)) |
| 106 |
|
| 107 |
def patch(self, index, item): |
| 108 |
self.current[index] = item |
| 109 |
|
| 110 |
def create_numbering(l): |
| 111 |
w = Writer() |
| 112 |
for item in l: |
| 113 |
w.append_int(item) |
| 114 |
return w.create_numbering() |
| 115 |
|
| 116 |
|
| 117 |
class Reader(object): |
| 118 |
def __init__(self, code): |
| 119 |
self.code = code |
| 120 |
self.cur_pos = 0 |
| 121 |
self.items_read = 0 |
| 122 |
|
| 123 |
def next_item(self): |
| 124 |
result, self.cur_pos = numb_next_item(self.code, self.cur_pos) |
| 125 |
self.items_read += 1 |
| 126 |
return result |
| 127 |
|
| 128 |
def peek(self): |
| 129 |
result, _ = numb_next_item(self.code, self.cur_pos) |
| 130 |
return result |
| 131 |
|
| 132 |
def jump(self, size): |
| 133 |
""" jump n items forward without returning anything """ |
| 134 |
index = self.cur_pos |
| 135 |
for i in range(size): |
| 136 |
_, index = numb_next_item(self.code, index) |
| 137 |
self.items_read += size |
| 138 |
self.cur_pos = index |
| 139 |
|
| 140 |
def unpack(self): |
| 141 |
|
| 142 |
return unpack_numbering(self.code) |