summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
Diffstat (limited to 'py')
-rw-r--r--py/binary.c115
-rw-r--r--py/binary.h6
-rw-r--r--py/modstruct.c5
-rw-r--r--py/mpconfig.h5
-rw-r--r--py/obj.c2
-rw-r--r--py/objarray.c12
-rw-r--r--py/objdict.c25
7 files changed, 110 insertions, 60 deletions
diff --git a/py/binary.c b/py/binary.c
index 95e82a688d..1ddf4569b9 100644
--- a/py/binary.c
+++ b/py/binary.c
@@ -39,7 +39,7 @@ int mp_binary_get_size(char typecode) {
return -1;
}
-mp_obj_t mp_binary_get_val(char typecode, void *p, int index) {
+mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index) {
machine_int_t val = 0;
switch (typecode) {
case 'b':
@@ -77,53 +77,80 @@ mp_obj_t mp_binary_get_val(char typecode, void *p, int index) {
return MP_OBJ_NEW_SMALL_INT(val);
}
-mp_obj_t mp_binary_get_val_unaligned_le(char typecode, byte **ptr) {
- machine_int_t val = 0;
+#define is_signed(typecode) (typecode > 'Z')
+mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
byte *p = *ptr;
- switch (typecode) {
- case 'b':
- val = (int8_t)*p++;
- break;
- case BYTEARRAY_TYPECODE:
- case 'B':
- val = *p++;
- break;
- case 'h':
- val = (int16_t)((p[1] << 8) | p[0]);
- break;
- case 'H':
- val = (p[1] << 8) | p[0];
- break;
- case 'i':
- case 'l':
- val = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
- *ptr = p + 4;
- return mp_obj_new_int(val);
- case 'I':
- case 'L':
- val = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
- *ptr = p + 4;
- return mp_obj_new_int_from_uint(val);
-#if 0 //TODO
-#if MICROPY_LONGINT_IMPL != MICROPY_LONGINT_IMPL_NONE
- 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
-#endif
+ uint size = 0;
+ 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;
+ }
+ 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.
+ uint align = 0;
+ switch (val_type) {
+ 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;
+ }
+ // Make pointer aligned
+ p = (byte*)(((machine_uint_t)p + align - 1) & ~(align - 1));
+ #if MP_ENDIANNESS_LITTLE
+ struct_type = '<';
+ #else
+ struct_type = '>';
+ #endif
+ break;
+ }
+ }
+
+ int delta;
+ if (struct_type == '<') {
+ delta = -1;
+ p += size - 1;
+ } else {
+ delta = 1;
+ }
+
+ machine_int_t val = 0;
+ if (is_signed(val_type) && *p & 0x80) {
+ val = -1;
+ }
+ for (uint i = 0; i < size; i++) {
+ val <<= 8;
+ val |= *p;
+ p += delta;
+ }
+
+ *ptr += size;
+ if (is_signed(val_type)) {
+ return mp_obj_new_int(val);
+ } else {
+ return mp_obj_new_int_from_uint(val);
}
- *ptr = p;
- return MP_OBJ_NEW_SMALL_INT(val);
}
-void mp_binary_set_val(char typecode, void *p, int index, mp_obj_t val_in) {
+void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in) {
machine_int_t val = 0;
if (MP_OBJ_IS_INT(val_in)) {
val = mp_obj_int_get(val_in);
diff --git a/py/binary.h b/py/binary.h
index d6bab14d05..e9fb38f13f 100644
--- a/py/binary.h
+++ b/py/binary.h
@@ -3,6 +3,6 @@
#define BYTEARRAY_TYPECODE 0
int mp_binary_get_size(char typecode);
-mp_obj_t mp_binary_get_val(char typecode, void *p, int index);
-mp_obj_t mp_binary_get_val_unaligned_le(char typecode, byte **ptr);
-void mp_binary_set_val(char typecode, void *p, int index, mp_obj_t val_in);
+mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index);
+mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr);
+void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in);
diff --git a/py/modstruct.c b/py/modstruct.c
index 23c2c903de..3b9679a4c2 100644
--- a/py/modstruct.c
+++ b/py/modstruct.c
@@ -37,7 +37,7 @@ STATIC uint calcsize_items(const char *fmt) {
STATIC mp_obj_t struct_calcsize(mp_obj_t fmt_in) {
const char *fmt = mp_obj_str_get_str(fmt_in);
char fmt_type = get_fmt_type(&fmt);
- assert(fmt_type == '<'); (void)fmt_type;
+ (void)fmt_type;
machine_uint_t size;
for (size = 0; *fmt; fmt++) {
int sz = mp_binary_get_size(*fmt);
@@ -53,7 +53,6 @@ STATIC mp_obj_t struct_unpack(mp_obj_t fmt_in, mp_obj_t data_in) {
// TODO: "The buffer must contain exactly the amount of data required by the format (len(bytes) must equal calcsize(fmt))."
const char *fmt = mp_obj_str_get_str(fmt_in);
char fmt_type = get_fmt_type(&fmt);
- assert(fmt_type == '<'); (void)fmt_type;
uint size = calcsize_items(fmt);
mp_obj_tuple_t *res = mp_obj_new_tuple(size, NULL);
buffer_info_t bufinfo;
@@ -61,7 +60,7 @@ STATIC mp_obj_t struct_unpack(mp_obj_t fmt_in, mp_obj_t data_in) {
byte *p = bufinfo.buf;
for (uint i = 0; i < size; i++) {
- mp_obj_t item = mp_binary_get_val_unaligned_le(*fmt++, &p);
+ mp_obj_t item = mp_binary_get_val(fmt_type, *fmt++, &p);
res->items[i] = item;
}
return res;
diff --git a/py/mpconfig.h b/py/mpconfig.h
index b120c4bb4a..2c118b4bba 100644
--- a/py/mpconfig.h
+++ b/py/mpconfig.h
@@ -169,6 +169,11 @@ typedef double mp_float_t;
// machine_int_t value with most significant bit set
#define WORD_MSBIT_HIGH (((machine_uint_t)1) << (BYTES_PER_WORD * 8 - 1))
+#if !defined(MP_ENDIANNESS_LITTLE) && !defined(MP_ENDIANNESS_BIG)
+// Just because most archs are such?
+#define MP_ENDIANNESS_LITTLE (1)
+#endif
+
// printf format spec to use for machine_int_t and friends
#ifndef INT_FMT
#ifdef __LP64__
diff --git a/py/obj.c b/py/obj.c
index 844ec41216..623b396422 100644
--- a/py/obj.c
+++ b/py/obj.c
@@ -280,7 +280,7 @@ void mp_obj_get_array_fixed_n(mp_obj_t o, uint len, mp_obj_t **items) {
mp_obj_list_get(o, &seq_len, items);
}
if (seq_len != len) {
- nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_IndexError, "requested length %d but object has length %d", len, seq_len));
+ nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, "requested length %d but object has length %d", len, seq_len));
}
} else {
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "object '%s' is not a tuple or list", mp_obj_get_type_str(o)));
diff --git a/py/objarray.c b/py/objarray.c
index 15268112a2..5fb3693f37 100644
--- a/py/objarray.c
+++ b/py/objarray.c
@@ -40,7 +40,7 @@ STATIC void array_print(void (*print)(void *env, const char *fmt, ...), void *en
if (i > 0) {
print(env, ", ");
}
- mp_obj_print_helper(print, env, mp_binary_get_val(o->typecode, o->items, i), PRINT_REPR);
+ mp_obj_print_helper(print, env, mp_binary_get_val_array(o->typecode, o->items, i), PRINT_REPR);
}
print(env, "]");
}
@@ -67,7 +67,7 @@ STATIC mp_obj_t array_construct(char typecode, mp_obj_t initializer) {
if (len == 0) {
array_append(array, item);
} else {
- mp_binary_set_val(typecode, array->items, i++, item);
+ mp_binary_set_val_array(typecode, array->items, i++, item);
}
}
@@ -118,7 +118,7 @@ STATIC mp_obj_t array_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
case MP_BINARY_OP_SUBSCR:
{
uint index = mp_get_index(o->base.type, o->len, rhs, false);
- return mp_binary_get_val(o->typecode, o->items, index);
+ return mp_binary_get_val_array(o->typecode, o->items, index);
}
default:
@@ -136,7 +136,7 @@ STATIC mp_obj_t array_append(mp_obj_t self_in, mp_obj_t arg) {
self->free = 8;
self->items = m_realloc(self->items, item_sz * self->len, item_sz * (self->len + self->free));
}
- mp_binary_set_val(self->typecode, self->items, self->len++, arg);
+ mp_binary_set_val_array(self->typecode, self->items, self->len++, arg);
self->free--;
return mp_const_none; // return None, as per CPython
}
@@ -149,7 +149,7 @@ STATIC bool array_store_item(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value
}
mp_obj_array_t *o = self_in;
uint index = mp_get_index(o->base.type, o->len, index_in, false);
- mp_binary_set_val(o->typecode, o->items, index, value);
+ mp_binary_set_val_array(o->typecode, o->items, index, value);
return true;
}
@@ -235,7 +235,7 @@ typedef struct _mp_obj_array_it_t {
STATIC mp_obj_t array_it_iternext(mp_obj_t self_in) {
mp_obj_array_it_t *self = self_in;
if (self->cur < self->array->len) {
- return mp_binary_get_val(self->array->typecode, self->array->items, self->cur++);
+ return mp_binary_get_val_array(self->array->typecode, self->array->items, self->cur++);
} else {
return MP_OBJ_NULL;
}
diff --git a/py/objdict.c b/py/objdict.c
index 85986448c7..963e188074 100644
--- a/py/objdict.c
+++ b/py/objdict.c
@@ -7,6 +7,7 @@
#include "mpconfig.h"
#include "qstr.h"
#include "obj.h"
+#include "objtuple.h"
#include "runtime0.h"
#include "runtime.h"
@@ -39,13 +40,24 @@ STATIC mp_obj_t dict_make_new(mp_obj_t type_in, uint n_args, uint n_kw, const mp
dict = mp_obj_new_dict(0);
break;
- case 1:
+ case 1: {
if (MP_OBJ_IS_TYPE(args[0], &mp_type_dict)) {
return dict_copy(args[0]);
}
// TODO create dict from an arbitrary mapping!
- // TODO create dict from an iterable!
- assert(false);
+
+ // Make dict from iterable of pairs
+ mp_obj_t iterable = mp_getiter(args[0]);
+ mp_obj_t dict = mp_obj_new_dict(0);
+ // TODO: support arbitrary seq as a pair
+ mp_obj_t item;
+ while ((item = mp_iternext(iterable)) != MP_OBJ_NULL) {
+ mp_obj_t *sub_items;
+ mp_obj_get_array_fixed_n(item, 2, &sub_items);
+ mp_obj_dict_store(dict, sub_items[0], sub_items[1]);
+ }
+ return dict;
+ }
default:
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError, "dict takes at most 1 argument"));
@@ -123,6 +135,12 @@ STATIC bool dict_store_item(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
return true;
}
+STATIC mp_obj_t dict_getitem(mp_obj_t lhs_in, mp_obj_t rhs_in) {
+ return dict_binary_op(MP_BINARY_OP_SUBSCR, lhs_in, rhs_in);
+}
+
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(dict_getitem_obj, dict_getitem);
+
/******************************************************************************/
/* dict iterator */
@@ -481,6 +499,7 @@ STATIC const mp_map_elem_t dict_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_setdefault), (mp_obj_t)&dict_setdefault_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_update), (mp_obj_t)&dict_update_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_values), (mp_obj_t)&dict_values_obj },
+ { MP_OBJ_NEW_QSTR(MP_QSTR___getitem__), (mp_obj_t)&dict_getitem_obj },
};
STATIC MP_DEFINE_CONST_DICT(dict_locals_dict, dict_locals_dict_table);