2014-02-14 15:16:35 +00:00
|
|
|
#include <stdint.h>
|
2014-04-18 23:25:49 +01:00
|
|
|
#include <stdlib.h>
|
2014-02-14 15:16:35 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "misc.h"
|
|
|
|
#include "mpconfig.h"
|
|
|
|
#include "qstr.h"
|
|
|
|
#include "obj.h"
|
|
|
|
#include "binary.h"
|
|
|
|
|
|
|
|
// Helpers to work with binary-encoded data
|
|
|
|
|
2014-04-18 23:25:49 +01:00
|
|
|
int mp_binary_get_size(char struct_type, char val_type, uint *palign) {
|
|
|
|
int size = 0;
|
|
|
|
int align = 1;
|
|
|
|
switch (struct_type) {
|
|
|
|
case '<': case '>':
|
|
|
|
switch (val_type) {
|
|
|
|
case 'b': case 'B':
|
|
|
|
size = 1; break;
|
|
|
|
case 'h': case 'H':
|
|
|
|
size = 2; break;
|
|
|
|
case 'i': case 'I':
|
|
|
|
size = 4; break;
|
|
|
|
case 'l': case 'L':
|
|
|
|
size = 4; break;
|
|
|
|
case 'q': case 'Q':
|
|
|
|
size = 8; break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '@': {
|
|
|
|
// TODO:
|
|
|
|
// The simplest heuristic for alignment is to align by value
|
|
|
|
// size, but that doesn't work for "bigger than int" types,
|
|
|
|
// for example, long long may very well have long alignment
|
|
|
|
// So, we introduce separate alignment handling, but having
|
|
|
|
// formal support for that is different from actually supporting
|
|
|
|
// particular (or any) ABI.
|
|
|
|
switch (val_type) {
|
|
|
|
case BYTEARRAY_TYPECODE:
|
|
|
|
case 'b': case 'B':
|
|
|
|
align = size = 1; break;
|
|
|
|
case 'h': case 'H':
|
|
|
|
align = size = sizeof(short); break;
|
|
|
|
case 'i': case 'I':
|
|
|
|
align = size = sizeof(int); break;
|
|
|
|
case 'l': case 'L':
|
|
|
|
align = size = sizeof(long); break;
|
|
|
|
case 'q': case 'Q':
|
|
|
|
// TODO: This is for x86
|
|
|
|
align = sizeof(int); size = sizeof(long long); break;
|
2014-04-20 03:19:10 +01:00
|
|
|
case 'P': case 'O':
|
|
|
|
align = size = sizeof(void*); break;
|
2014-04-18 23:25:49 +01:00
|
|
|
}
|
|
|
|
}
|
2014-02-14 18:21:50 +00:00
|
|
|
}
|
2014-04-18 23:25:49 +01:00
|
|
|
if (palign != NULL) {
|
|
|
|
*palign = align;
|
|
|
|
}
|
|
|
|
return size;
|
2014-02-14 18:21:50 +00:00
|
|
|
}
|
|
|
|
|
2014-04-11 01:46:09 +01:00
|
|
|
mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index) {
|
2014-02-14 22:28:41 +00:00
|
|
|
machine_int_t val = 0;
|
2014-02-14 15:16:35 +00:00
|
|
|
switch (typecode) {
|
|
|
|
case 'b':
|
|
|
|
val = ((int8_t*)p)[index];
|
|
|
|
break;
|
|
|
|
case BYTEARRAY_TYPECODE:
|
|
|
|
case 'B':
|
|
|
|
val = ((uint8_t*)p)[index];
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
val = ((int16_t*)p)[index];
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
val = ((uint16_t*)p)[index];
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
case 'l':
|
|
|
|
return mp_obj_new_int(((int32_t*)p)[index]);
|
|
|
|
case 'I':
|
|
|
|
case 'L':
|
|
|
|
return mp_obj_new_int_from_uint(((uint32_t*)p)[index]);
|
2014-02-15 23:02:00 +00:00
|
|
|
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
|
2014-02-14 15:16:35 +00:00
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
|
|
|
// TODO: Explode API more to cover signedness
|
|
|
|
return mp_obj_new_int_from_ll(((long long*)p)[index]);
|
|
|
|
#endif
|
|
|
|
#if MICROPY_ENABLE_FLOAT
|
|
|
|
case 'f':
|
|
|
|
return mp_obj_new_float(((float*)p)[index]);
|
|
|
|
case 'd':
|
|
|
|
return mp_obj_new_float(((double*)p)[index]);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
return MP_OBJ_NEW_SMALL_INT(val);
|
|
|
|
}
|
|
|
|
|
2014-04-10 20:19:32 +01:00
|
|
|
#define is_signed(typecode) (typecode > 'Z')
|
2014-04-11 01:47:21 +01:00
|
|
|
mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
|
2014-04-10 01:45:38 +01:00
|
|
|
byte *p = *ptr;
|
2014-04-18 23:25:49 +01:00
|
|
|
uint align;
|
|
|
|
|
|
|
|
int size = mp_binary_get_size(struct_type, val_type, &align);
|
|
|
|
if (struct_type == '@') {
|
|
|
|
// Make pointer aligned
|
|
|
|
p = (byte*)(((machine_uint_t)p + align - 1) & ~(align - 1));
|
|
|
|
#if MP_ENDIANNESS_LITTLE
|
|
|
|
struct_type = '<';
|
|
|
|
#else
|
|
|
|
struct_type = '>';
|
|
|
|
#endif
|
2014-04-10 01:45:38 +01:00
|
|
|
}
|
2014-04-10 20:19:32 +01:00
|
|
|
|
|
|
|
int delta;
|
2014-04-11 01:47:21 +01:00
|
|
|
if (struct_type == '<') {
|
2014-04-10 20:19:32 +01:00
|
|
|
delta = -1;
|
|
|
|
p += size - 1;
|
|
|
|
} else {
|
|
|
|
delta = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
machine_int_t val = 0;
|
2014-04-11 01:47:21 +01:00
|
|
|
if (is_signed(val_type) && *p & 0x80) {
|
2014-04-10 20:19:32 +01:00
|
|
|
val = -1;
|
|
|
|
}
|
|
|
|
for (uint i = 0; i < size; i++) {
|
|
|
|
val <<= 8;
|
|
|
|
val |= *p;
|
|
|
|
p += delta;
|
|
|
|
}
|
|
|
|
|
2014-04-11 01:47:21 +01:00
|
|
|
*ptr += size;
|
2014-04-20 03:19:10 +01:00
|
|
|
if (val_type == 'O') {
|
|
|
|
return (mp_obj_t)val;
|
|
|
|
} else if (is_signed(val_type)) {
|
2014-04-10 20:19:32 +01:00
|
|
|
return mp_obj_new_int(val);
|
|
|
|
} else {
|
|
|
|
return mp_obj_new_int_from_uint(val);
|
|
|
|
}
|
2014-04-10 01:45:38 +01:00
|
|
|
}
|
|
|
|
|
2014-04-19 01:13:15 +01:00
|
|
|
void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **ptr) {
|
|
|
|
byte *p = *ptr;
|
|
|
|
uint align;
|
|
|
|
|
|
|
|
int size = mp_binary_get_size(struct_type, val_type, &align);
|
|
|
|
if (struct_type == '@') {
|
|
|
|
// Make pointer aligned
|
|
|
|
p = (byte*)(((machine_uint_t)p + align - 1) & ~(align - 1));
|
|
|
|
#if MP_ENDIANNESS_LITTLE
|
|
|
|
struct_type = '<';
|
|
|
|
#else
|
|
|
|
struct_type = '>';
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
#if MP_ENDIANNESS_BIG
|
|
|
|
#error Not implemented
|
|
|
|
#endif
|
2014-04-20 03:19:10 +01:00
|
|
|
machine_int_t val;
|
2014-04-19 01:13:15 +01:00
|
|
|
byte *in = (byte*)&val;
|
2014-04-20 03:19:10 +01:00
|
|
|
switch (val_type) {
|
|
|
|
case 'O':
|
|
|
|
in = (byte*)&val_in;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
val = mp_obj_get_int(val_in);
|
|
|
|
}
|
|
|
|
|
2014-04-19 01:13:15 +01:00
|
|
|
int in_delta, out_delta;
|
|
|
|
uint val_sz = MIN(size, sizeof(val));
|
|
|
|
if (struct_type == '>') {
|
|
|
|
in_delta = -1;
|
|
|
|
out_delta = 1;
|
|
|
|
in += val_sz - 1;
|
|
|
|
} else {
|
|
|
|
in_delta = out_delta = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint i = val_sz; i > 0; i--) {
|
|
|
|
*p = *in;
|
|
|
|
p += out_delta;
|
|
|
|
in += in_delta;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ptr += size;
|
|
|
|
}
|
|
|
|
|
2014-04-11 01:46:09 +01:00
|
|
|
void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in) {
|
2014-04-18 23:28:12 +01:00
|
|
|
switch (typecode) {
|
|
|
|
#if MICROPY_ENABLE_FLOAT
|
|
|
|
case 'f':
|
|
|
|
((float*)p)[index] = mp_obj_float_get(val_in);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
((double*)p)[index] = mp_obj_float_get(val_in);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
mp_binary_set_val_array_from_int(typecode, p, index, mp_obj_get_int(val_in));
|
2014-02-14 15:16:35 +00:00
|
|
|
}
|
2014-04-18 23:28:12 +01:00
|
|
|
}
|
2014-02-14 15:16:35 +00:00
|
|
|
|
2014-04-18 23:28:12 +01:00
|
|
|
void mp_binary_set_val_array_from_int(char typecode, void *p, int index, machine_int_t val) {
|
2014-02-14 15:16:35 +00:00
|
|
|
switch (typecode) {
|
|
|
|
case 'b':
|
|
|
|
((int8_t*)p)[index] = val;
|
|
|
|
break;
|
|
|
|
case BYTEARRAY_TYPECODE:
|
|
|
|
case 'B':
|
|
|
|
val = ((uint8_t*)p)[index] = val;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
val = ((int16_t*)p)[index] = val;
|
|
|
|
break;
|
|
|
|
case 'H':
|
|
|
|
val = ((uint16_t*)p)[index] = val;
|
|
|
|
break;
|
|
|
|
case 'i':
|
|
|
|
case 'l':
|
|
|
|
((int32_t*)p)[index] = val;
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
case 'L':
|
|
|
|
((uint32_t*)p)[index] = val;
|
|
|
|
break;
|
|
|
|
#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
|
|
|
|
case 'q':
|
|
|
|
case 'Q':
|
|
|
|
assert(0);
|
|
|
|
((long long*)p)[index] = val;
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
#if MICROPY_ENABLE_FLOAT
|
|
|
|
case 'f':
|
2014-04-18 23:28:12 +01:00
|
|
|
((float*)p)[index] = val;
|
2014-02-14 15:16:35 +00:00
|
|
|
break;
|
|
|
|
case 'd':
|
2014-04-18 23:28:12 +01:00
|
|
|
((double*)p)[index] = val;
|
2014-02-14 15:16:35 +00:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|