| 1 |
from rpython.rlib.objectmodel import we_are_translated |
| 2 |
from rpython.rtyper.extregistry import ExtRegistryEntry |
| 3 |
from rpython.rtyper.lltypesystem import lltype, rffi, llmemory |
| 4 |
from rpython.annotator import model as annmodel |
| 5 |
from rpython.rtyper.llannotation import lltype_to_annotation |
| 6 |
from rpython.rlib.rgc import lltype_is_gc |
| 7 |
from rpython.rlib.objectmodel import specialize, not_rpython |
| 8 |
|
| 9 |
RAW_STORAGE = rffi.CCHARP.TO |
| 10 |
RAW_STORAGE_PTR = rffi.CCHARP |
| 11 |
|
| 12 |
@specialize.arg(1, 2) |
| 13 |
def alloc_raw_storage(size, track_allocation=True, zero=False): |
| 14 |
return lltype.malloc(RAW_STORAGE, size, flavor='raw', |
| 15 |
add_memory_pressure=True, |
| 16 |
track_allocation=track_allocation, |
| 17 |
zero=zero) |
| 18 |
|
| 19 |
@not_rpython |
| 20 |
def raw_storage_getitem(TP, storage, index): |
| 21 |
_check_alignment(TP, index) |
| 22 |
return _raw_storage_getitem_unchecked(TP, storage, index) |
| 23 |
|
| 24 |
@not_rpython |
| 25 |
def _raw_storage_getitem_unchecked(TP, storage, index): |
| 26 |
return rffi.cast(rffi.CArrayPtr(TP), rffi.ptradd(storage, index))[0] |
| 27 |
|
| 28 |
@not_rpython |
| 29 |
def raw_storage_setitem(storage, index, item): |
| 30 |
TP = lltype.typeOf(item) |
| 31 |
_check_alignment(TP, index) |
| 32 |
_raw_storage_setitem_unchecked(storage, index, item) |
| 33 |
|
| 34 |
@not_rpython |
| 35 |
def _raw_storage_setitem_unchecked(storage, index, item): |
| 36 |
TP = lltype.typeOf(item) |
| 37 |
rffi.cast(rffi.CArrayPtr(TP), rffi.ptradd(storage, index))[0] = item |
| 38 |
|
| 39 |
@specialize.arg(1) |
| 40 |
def free_raw_storage(storage, track_allocation=True): |
| 41 |
lltype.free(storage, flavor='raw', track_allocation=track_allocation) |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
from rpython.jit.backend import detect_cpu |
| 48 |
try: |
| 49 |
cpuname = detect_cpu.autodetect() |
| 50 |
misaligned_is_fine = cpuname.startswith('x86') or \ |
| 51 |
cpuname.startswith('s390x') or \ |
| 52 |
cpuname.startswith('ppc') |
| 53 |
del cpuname |
| 54 |
except detect_cpu.ProcessorAutodetectError: |
| 55 |
misaligned_is_fine = False |
| 56 |
|
| 57 |
|
| 58 |
class AlignmentError(Exception): |
| 59 |
"Means that raw_storage_{get,set}item was used on unaligned memory" |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
MAXIMUM_ALIGNMENT = 8 |
| 65 |
|
| 66 |
@specialize.memo() |
| 67 |
def _get_alignment_mask(TP): |
| 68 |
size = rffi.sizeof(TP) |
| 69 |
alignment = 1 |
| 70 |
while (size & alignment) == 0 and alignment < MAXIMUM_ALIGNMENT: |
| 71 |
alignment *= 2 |
| 72 |
return alignment - 1 |
| 73 |
|
| 74 |
def _check_alignment(TP, index): |
| 75 |
"""Check that the 'index' does indeed have the maximum alignment |
| 76 |
for the given type.""" |
| 77 |
mask = _get_alignment_mask(TP) |
| 78 |
if (index & mask) != 0: |
| 79 |
raise AlignmentError |
| 80 |
|
| 81 |
@specialize.ll() |
| 82 |
def raw_storage_getitem_unaligned(TP, storage, index): |
| 83 |
if misaligned_is_fine: |
| 84 |
if we_are_translated(): |
| 85 |
return raw_storage_getitem(TP, storage, index) |
| 86 |
else: |
| 87 |
return _raw_storage_getitem_unchecked(TP, storage, index) |
| 88 |
mask = _get_alignment_mask(TP) |
| 89 |
if (index & mask) == 0: |
| 90 |
if we_are_translated(): |
| 91 |
return raw_storage_getitem(TP, storage, index) |
| 92 |
else: |
| 93 |
return _raw_storage_getitem_unchecked(TP, storage, index) |
| 94 |
ptr = rffi.ptradd(storage, index) |
| 95 |
with lltype.scoped_alloc(rffi.CArray(TP), 1) as s_array: |
| 96 |
rffi.c_memcpy(rffi.cast(rffi.VOIDP, s_array), |
| 97 |
rffi.cast(rffi.CONST_VOIDP, ptr), |
| 98 |
rffi.sizeof(TP)) |
| 99 |
return rffi.cast(rffi.CArrayPtr(TP), s_array)[0] |
| 100 |
|
| 101 |
@specialize.ll() |
| 102 |
def raw_storage_setitem_unaligned(storage, index, item): |
| 103 |
if misaligned_is_fine: |
| 104 |
if we_are_translated(): |
| 105 |
raw_storage_setitem(storage, index, item) |
| 106 |
else: |
| 107 |
_raw_storage_setitem_unchecked(storage, index, item) |
| 108 |
return |
| 109 |
TP = lltype.typeOf(item) |
| 110 |
mask = _get_alignment_mask(TP) |
| 111 |
if (index & mask) == 0: |
| 112 |
if we_are_translated(): |
| 113 |
raw_storage_setitem(storage, index, item) |
| 114 |
else: |
| 115 |
_raw_storage_setitem_unchecked(storage, index, item) |
| 116 |
return |
| 117 |
ptr = rffi.ptradd(storage, index) |
| 118 |
with lltype.scoped_alloc(rffi.CArray(TP), 1) as s_array: |
| 119 |
rffi.cast(rffi.CArrayPtr(TP), s_array)[0] = item |
| 120 |
rffi.c_memcpy(rffi.cast(rffi.VOIDP, ptr), |
| 121 |
rffi.cast(rffi.CONST_VOIDP, s_array), |
| 122 |
rffi.sizeof(TP)) |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
class RawStorageGetitemEntry(ExtRegistryEntry): |
| 128 |
_about_ = raw_storage_getitem |
| 129 |
|
| 130 |
def compute_result_annotation(self, s_TP, s_storage, s_index): |
| 131 |
assert s_TP.is_constant() |
| 132 |
return lltype_to_annotation(s_TP.const) |
| 133 |
|
| 134 |
def specialize_call(self, hop): |
| 135 |
assert hop.args_r[1].lowleveltype == RAW_STORAGE_PTR |
| 136 |
v_storage = hop.inputarg(hop.args_r[1], arg=1) |
| 137 |
v_index = hop.inputarg(lltype.Signed, arg=2) |
| 138 |
hop.exception_cannot_occur() |
| 139 |
v_addr = hop.genop('cast_ptr_to_adr', [v_storage], |
| 140 |
resulttype=llmemory.Address) |
| 141 |
return hop.genop('raw_load', [v_addr, v_index], |
| 142 |
resulttype=hop.r_result.lowleveltype) |
| 143 |
|
| 144 |
class RawStorageSetitemEntry(ExtRegistryEntry): |
| 145 |
_about_ = raw_storage_setitem |
| 146 |
|
| 147 |
def compute_result_annotation(self, s_storage, s_index, s_item): |
| 148 |
assert annmodel.SomeInteger().contains(s_index) |
| 149 |
|
| 150 |
def specialize_call(self, hop): |
| 151 |
assert not lltype_is_gc(hop.args_r[2].lowleveltype) |
| 152 |
assert hop.args_r[0].lowleveltype == RAW_STORAGE_PTR |
| 153 |
v_storage, v_index, v_item = hop.inputargs(hop.args_r[0], |
| 154 |
lltype.Signed, |
| 155 |
hop.args_r[2]) |
| 156 |
hop.exception_cannot_occur() |
| 157 |
v_addr = hop.genop('cast_ptr_to_adr', [v_storage], |
| 158 |
resulttype=llmemory.Address) |
| 159 |
return hop.genop('raw_store', [v_addr, v_index, v_item]) |