Skip to content

Commit 0c43cf9

Browse files
committed
modstruct: Basic implementation of native struct alignment and types.
1 parent ef9124f commit 0c43cf9

File tree

4 files changed

+46
-15
lines changed

4 files changed

+46
-15
lines changed

py/binary.c

Lines changed: 38 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,33 +78,62 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index) {
7878
}
7979

8080
#define is_signed(typecode) (typecode > 'Z')
81-
mp_obj_t mp_binary_get_val_unaligned(char typecode, byte **ptr) {
82-
char type = '<';
81+
mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
8382
byte *p = *ptr;
84-
uint size = 0, align = 0;
85-
switch (type) {
83+
uint size = 0;
84+
switch (struct_type) {
8685
case '<': case '>':
87-
switch (typecode) {
86+
switch (val_type) {
8887
case 'b': case 'B':
8988
size = 1; break;
9089
case 'h': case 'H':
9190
size = 2; break;
9291
case 'i': case 'I':
9392
size = 4; break;
93+
case 'l': case 'L':
94+
size = 4; break;
95+
}
96+
break;
97+
case '@': {
98+
// TODO:
99+
// The simplest heuristic for alignment is to align by value
100+
// size, but that doesn't work for "bigger than int" types,
101+
// for example, long long may very well have long alignment
102+
// So, we introduce separate alignment handling, but having
103+
// formal support for that is different from actually supporting
104+
// particular (or any) ABI.
105+
uint align = 0;
106+
switch (val_type) {
107+
case 'b': case 'B':
108+
align = size = 1; break;
109+
case 'h': case 'H':
110+
align = size = sizeof(short); break;
111+
case 'i': case 'I':
112+
align = size = sizeof(int); break;
113+
case 'l': case 'L':
114+
align = size = sizeof(long); break;
94115
}
116+
// Make pointer aligned
117+
p = (byte*)(((machine_uint_t)p + align - 1) & ~(align - 1));
118+
#if MP_ENDIANNESS_LITTLE
119+
struct_type = '<';
120+
#else
121+
struct_type = '>';
122+
#endif
95123
break;
124+
}
96125
}
97126

98127
int delta;
99-
if (type == '<') {
128+
if (struct_type == '<') {
100129
delta = -1;
101130
p += size - 1;
102131
} else {
103132
delta = 1;
104133
}
105134

106135
machine_int_t val = 0;
107-
if (is_signed(typecode) && *p & 0x80) {
136+
if (is_signed(val_type) && *p & 0x80) {
108137
val = -1;
109138
}
110139
for (uint i = 0; i < size; i++) {
@@ -113,8 +142,8 @@ mp_obj_t mp_binary_get_val_unaligned(char typecode, byte **ptr) {
113142
p += delta;
114143
}
115144

116-
*ptr += size + align;
117-
if (is_signed(typecode)) {
145+
*ptr += size;
146+
if (is_signed(val_type)) {
118147
return mp_obj_new_int(val);
119148
} else {
120149
return mp_obj_new_int_from_uint(val);

py/binary.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
#define BYTEARRAY_TYPECODE 0
44

55
int mp_binary_get_size(char typecode);
6-
mp_obj_t mp_binary_get_val(char typecode, void *p, int index);
7-
mp_obj_t mp_binary_get_val_unaligned(char typecode, byte **ptr);
8-
void mp_binary_set_val(char typecode, void *p, int index, mp_obj_t val_in);
6+
mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index);
7+
mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr);
8+
void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in);

py/modstruct.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ STATIC uint calcsize_items(const char *fmt) {
3737
STATIC mp_obj_t struct_calcsize(mp_obj_t fmt_in) {
3838
const char *fmt = mp_obj_str_get_str(fmt_in);
3939
char fmt_type = get_fmt_type(&fmt);
40-
assert(fmt_type == '<' || fmt_type == '>'); (void)fmt_type;
40+
(void)fmt_type;
4141
machine_uint_t size;
4242
for (size = 0; *fmt; fmt++) {
4343
int sz = mp_binary_get_size(*fmt);
@@ -53,15 +53,14 @@ STATIC mp_obj_t struct_unpack(mp_obj_t fmt_in, mp_obj_t data_in) {
5353
// TODO: "The buffer must contain exactly the amount of data required by the format (len(bytes) must equal calcsize(fmt))."
5454
const char *fmt = mp_obj_str_get_str(fmt_in);
5555
char fmt_type = get_fmt_type(&fmt);
56-
assert(fmt_type == '<' || fmt_type == '>'); (void)fmt_type;
5756
uint size = calcsize_items(fmt);
5857
mp_obj_tuple_t *res = mp_obj_new_tuple(size, NULL);
5958
buffer_info_t bufinfo;
6059
mp_get_buffer_raise(data_in, &bufinfo);
6160
byte *p = bufinfo.buf;
6261

6362
for (uint i = 0; i < size; i++) {
64-
mp_obj_t item = mp_binary_get_val_unaligned(*fmt++, &p);
63+
mp_obj_t item = mp_binary_get_val(fmt_type, *fmt++, &p);
6564
res->items[i] = item;
6665
}
6766
return res;

tests/basics/struct1.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
print(struct.unpack("<bI", b"\x80\0\0\x01\0"))
44
print(struct.calcsize(">bI"))
55
print(struct.unpack(">bI", b"\x80\0\0\x01\0"))
6+
7+
# 32-bit little-endian specific
8+
#print(struct.unpack("bI", b"\x80\xaa\x55\xaa\0\0\x01\0"))

0 commit comments

Comments
 (0)