| 1 |
""" |
| 2 |
Native type codes. |
| 3 |
The table 'native_fmttable' is also used by pypy.module.array.interp_array. |
| 4 |
""" |
| 5 |
import struct |
| 6 |
|
| 7 |
from rpython.rlib import rutf8, longlong2float |
| 8 |
from rpython.rlib.objectmodel import specialize |
| 9 |
from rpython.rlib.rarithmetic import r_singlefloat, widen, intmask |
| 10 |
from rpython.rlib.rstruct import standardfmttable as std |
| 11 |
from rpython.rlib.rstruct.standardfmttable import native_is_bigendian |
| 12 |
from rpython.rlib.rstruct.error import StructError |
| 13 |
from rpython.rlib.rstruct.ieee import pack_float_to_buffer |
| 14 |
from rpython.rlib.unroll import unrolling_iterable |
| 15 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 16 |
from rpython.rtyper.tool import rffi_platform |
| 17 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 18 |
|
| 19 |
|
| 20 |
native_fmttable = { |
| 21 |
'x': std.standard_fmttable['x'], |
| 22 |
'c': std.standard_fmttable['c'], |
| 23 |
's': std.standard_fmttable['s'], |
| 24 |
'p': std.standard_fmttable['p'], |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
def pack_double(fmtiter): |
| 31 |
doubleval = fmtiter.accept_float_arg() |
| 32 |
if std.pack_fastpath(rffi.DOUBLE)(fmtiter, doubleval): |
| 33 |
return |
| 34 |
|
| 35 |
value = longlong2float.float2longlong(doubleval) |
| 36 |
pack_float_to_buffer(fmtiter.wbuf, fmtiter.pos, value, 8, fmtiter.bigendian) |
| 37 |
fmtiter.advance(8) |
| 38 |
|
| 39 |
def pack_float(fmtiter): |
| 40 |
doubleval = fmtiter.accept_float_arg() |
| 41 |
floatval = r_singlefloat(doubleval) |
| 42 |
if std.pack_fastpath(rffi.FLOAT)(fmtiter, floatval): |
| 43 |
return |
| 44 |
|
| 45 |
value = longlong2float.singlefloat2uint(floatval) |
| 46 |
value = widen(value) |
| 47 |
value = intmask(value) |
| 48 |
pack_float_to_buffer(fmtiter.wbuf, fmtiter.pos, value, 4, fmtiter.bigendian) |
| 49 |
fmtiter.advance(4) |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
def setup(): |
| 56 |
INSPECT = {'b': 'signed char', |
| 57 |
'h': 'signed short', |
| 58 |
'i': 'signed int', |
| 59 |
'l': 'signed long', |
| 60 |
'q': 'signed long long', |
| 61 |
'n': 'ssize_t', |
| 62 |
'B': 'unsigned char', |
| 63 |
'H': 'unsigned short', |
| 64 |
'I': 'unsigned int', |
| 65 |
'L': 'unsigned long', |
| 66 |
'Q': 'unsigned long long', |
| 67 |
'N': 'size_t', |
| 68 |
'P': 'char *', |
| 69 |
'f': 'float', |
| 70 |
'd': 'double', |
| 71 |
'?': '_Bool', |
| 72 |
} |
| 73 |
|
| 74 |
pre_include_bits = [""" |
| 75 |
#include <sys/types.h> |
| 76 |
#ifdef _MSC_VER |
| 77 |
#define _Bool char |
| 78 |
#ifdef _WIN64 |
| 79 |
typedef long long ssize_t; |
| 80 |
typedef unsigned long long size_t; |
| 81 |
#else |
| 82 |
typedef int ssize_t; |
| 83 |
typedef unsigned int size_t; |
| 84 |
#endif |
| 85 |
#endif"""] |
| 86 |
field_names = dict.fromkeys(INSPECT) |
| 87 |
for fmtchar, ctype in INSPECT.iteritems(): |
| 88 |
field_name = ctype.replace(" ", "_").replace("*", "star") |
| 89 |
field_names[fmtchar] = field_name |
| 90 |
pre_include_bits.append(""" |
| 91 |
struct about_%s { |
| 92 |
char pad; |
| 93 |
%s field; |
| 94 |
}; |
| 95 |
""" % (field_name, ctype)) |
| 96 |
|
| 97 |
class CConfig: |
| 98 |
_compilation_info_ = ExternalCompilationInfo( |
| 99 |
pre_include_bits = pre_include_bits |
| 100 |
) |
| 101 |
|
| 102 |
for fmtchar, ctype in INSPECT.items(): |
| 103 |
setattr(CConfig, field_names[fmtchar], rffi_platform.Struct( |
| 104 |
"struct about_%s" % (field_names[fmtchar],), |
| 105 |
[('field', lltype.FixedSizeArray(rffi.CHAR, 1))])) |
| 106 |
|
| 107 |
cConfig = rffi_platform.configure(CConfig) |
| 108 |
|
| 109 |
for fmtchar, ctype in INSPECT.items(): |
| 110 |
S = cConfig[field_names[fmtchar]] |
| 111 |
alignment = rffi.offsetof(S, 'c_field') |
| 112 |
size = rffi.sizeof(S.c_field) |
| 113 |
signed = 'a' <= fmtchar <= 'z' |
| 114 |
|
| 115 |
if fmtchar == 'f': |
| 116 |
pack = pack_float |
| 117 |
unpack = std.unpack_float |
| 118 |
elif fmtchar == 'd': |
| 119 |
pack = pack_double |
| 120 |
unpack = std.unpack_double |
| 121 |
elif fmtchar == '?': |
| 122 |
pack = std.pack_bool |
| 123 |
unpack = std.unpack_bool |
| 124 |
else: |
| 125 |
pack = std.make_int_packer(size, signed) |
| 126 |
unpack = std.make_int_unpacker(size, signed) |
| 127 |
|
| 128 |
native_fmttable[fmtchar] = {'size': size, |
| 129 |
'alignment': alignment, |
| 130 |
'pack': pack, |
| 131 |
'unpack': unpack} |
| 132 |
|
| 133 |
setup() |
| 134 |
|
| 135 |
sizeof_double = native_fmttable['d']['size'] |
| 136 |
sizeof_float = native_fmttable['f']['size'] |
| 137 |
|
| 138 |
|
| 139 |
native_fmttable['e'] = {'size': native_fmttable['h']['size'], |
| 140 |
'alignment': native_fmttable['h']['alignment'], |
| 141 |
'pack': std.pack_halffloat, |
| 142 |
'unpack': std.unpack_halffloat, |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
from rpython.rlib.rstruct import unichar |
| 152 |
|
| 153 |
def pack_unichar(fmtiter): |
| 154 |
utf8, lgt = fmtiter.accept_unicode_arg() |
| 155 |
if lgt != 1: |
| 156 |
raise StructError("expected a unicode string of length 1") |
| 157 |
uchr = rutf8.codepoint_at_pos(utf8, 0) |
| 158 |
unichar.pack_codepoint(uchr, fmtiter.wbuf, fmtiter.pos) |
| 159 |
fmtiter.advance(unichar.UNICODE_SIZE) |
| 160 |
|
| 161 |
@specialize.argtype(0) |
| 162 |
def unpack_unichar(fmtiter): |
| 163 |
data = fmtiter.read(unichar.UNICODE_SIZE) |
| 164 |
fmtiter.append_utf8(unichar.unpack_codepoint(data)) |
| 165 |
|
| 166 |
native_fmttable['u'] = {'size': unichar.UNICODE_SIZE, |
| 167 |
'alignment': unichar.UNICODE_SIZE, |
| 168 |
'pack': pack_unichar, |
| 169 |
'unpack': unpack_unichar, |
| 170 |
} |