Commit 999cedb9 authored by Damien George's avatar Damien George
Browse files

py: Wrap all obj-ptr conversions in MP_OBJ_TO_PTR/MP_OBJ_FROM_PTR.

This allows the mp_obj_t type to be configured to something other than a
pointer-sized primitive type.

This patch also includes additional changes to allow the code to compile
when sizeof(mp_uint_t) != sizeof(void*), such as using size_t instead of
mp_uint_t, and various casts.
parent cbf76740
......@@ -77,13 +77,13 @@ typedef struct _machine_mem_obj_t {
STATIC void machine_mem_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
(void)kind;
machine_mem_obj_t *self = self_in;
machine_mem_obj_t *self = MP_OBJ_TO_PTR(self_in);
mp_printf(print, "<%u-bit memory>", 8 * self->elem_size);
}
STATIC mp_obj_t machine_mem_subscr(mp_obj_t self_in, mp_obj_t index, mp_obj_t value) {
// TODO support slice index to read/write multiple values at once
machine_mem_obj_t *self = self_in;
machine_mem_obj_t *self = MP_OBJ_TO_PTR(self_in);
if (value == MP_OBJ_NULL) {
// delete
return MP_OBJ_NULL; // op not supported
......
......@@ -124,24 +124,24 @@ STATIC NORETURN void syntax_error(void) {
STATIC mp_obj_t uctypes_struct_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 2, 3, false);
mp_obj_uctypes_struct_t *o = m_new_obj(mp_obj_uctypes_struct_t);
o->base.type = type_in;
o->addr = (void*)mp_obj_get_int(args[0]);
o->base.type = MP_OBJ_TO_PTR(type_in);
o->addr = (void*)(uintptr_t)mp_obj_get_int(args[0]);
o->desc = args[1];
o->flags = LAYOUT_NATIVE;
if (n_args == 3) {
o->flags = mp_obj_get_int(args[2]);
}
return o;
return MP_OBJ_FROM_PTR(o);
}
STATIC void uctypes_struct_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
(void)kind;
mp_obj_uctypes_struct_t *self = self_in;
mp_obj_uctypes_struct_t *self = MP_OBJ_TO_PTR(self_in);
const char *typen = "unk";
if (MP_OBJ_IS_TYPE(self->desc, &mp_type_dict)) {
typen = "STRUCT";
} else if (MP_OBJ_IS_TYPE(self->desc, &mp_type_tuple)) {
mp_obj_tuple_t *t = (mp_obj_tuple_t*)self->desc;
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(self->desc);
mp_int_t offset = MP_OBJ_SMALL_INT_VALUE(t->items[0]);
uint agg_type = GET_TYPE(offset, AGG_TYPE_BITS);
switch (agg_type) {
......@@ -207,12 +207,9 @@ STATIC mp_uint_t uctypes_struct_agg_size(mp_obj_tuple_t *t, mp_uint_t *max_field
}
STATIC mp_uint_t uctypes_struct_size(mp_obj_t desc_in, mp_uint_t *max_field_size) {
mp_obj_dict_t *d = desc_in;
mp_uint_t total_size = 0;
if (!MP_OBJ_IS_TYPE(desc_in, &mp_type_dict)) {
if (MP_OBJ_IS_TYPE(desc_in, &mp_type_tuple)) {
return uctypes_struct_agg_size((mp_obj_tuple_t*)desc_in, max_field_size);
return uctypes_struct_agg_size((mp_obj_tuple_t*)MP_OBJ_TO_PTR(desc_in), max_field_size);
} else if (MP_OBJ_IS_SMALL_INT(desc_in)) {
// We allow sizeof on both type definitions and structures/structure fields,
// but scalar structure field is lowered into native Python int, so all
......@@ -223,6 +220,9 @@ STATIC mp_uint_t uctypes_struct_size(mp_obj_t desc_in, mp_uint_t *max_field_size
syntax_error();
}
mp_obj_dict_t *d = MP_OBJ_TO_PTR(desc_in);
mp_uint_t total_size = 0;
for (mp_uint_t i = 0; i < d->map.alloc; i++) {
if (MP_MAP_SLOT_IS_FILLED(&d->map, i)) {
mp_obj_t v = d->map.table[i].value;
......@@ -241,7 +241,7 @@ STATIC mp_uint_t uctypes_struct_size(mp_obj_t desc_in, mp_uint_t *max_field_size
if (!MP_OBJ_IS_TYPE(v, &mp_type_tuple)) {
syntax_error();
}
mp_obj_tuple_t *t = (mp_obj_tuple_t*)v;
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(v);
mp_int_t offset = MP_OBJ_SMALL_INT_VALUE(t->items[0]);
offset &= VALUE_MASK(AGG_TYPE_BITS);
mp_uint_t s = uctypes_struct_agg_size(t, max_field_size);
......@@ -266,7 +266,7 @@ STATIC mp_obj_t uctypes_struct_sizeof(mp_obj_t obj_in) {
// or to instantiated structure
if (MP_OBJ_IS_TYPE(obj_in, &uctypes_struct_type)) {
// Extract structure definition
mp_obj_uctypes_struct_t *obj = obj_in;
mp_obj_uctypes_struct_t *obj = MP_OBJ_TO_PTR(obj_in);
obj_in = obj->desc;
}
mp_uint_t size = uctypes_struct_size(obj_in, &max_field_size);
......@@ -365,7 +365,7 @@ STATIC void set_aligned(uint val_type, void *p, mp_int_t index, mp_obj_t val) {
}
STATIC mp_obj_t uctypes_struct_attr_op(mp_obj_t self_in, qstr attr, mp_obj_t set_val) {
mp_obj_uctypes_struct_t *self = self_in;
mp_obj_uctypes_struct_t *self = MP_OBJ_TO_PTR(self_in);
// TODO: Support at least OrderedDict in addition
if (!MP_OBJ_IS_TYPE(self->desc, &mp_type_dict)) {
......@@ -443,7 +443,7 @@ STATIC mp_obj_t uctypes_struct_attr_op(mp_obj_t self_in, qstr attr, mp_obj_t set
syntax_error();
}
mp_obj_tuple_t *sub = (mp_obj_tuple_t*)deref;
mp_obj_tuple_t *sub = MP_OBJ_TO_PTR(deref);
mp_int_t offset = MP_OBJ_SMALL_INT_VALUE(sub->items[0]);
mp_uint_t agg_type = GET_TYPE(offset, AGG_TYPE_BITS);
offset &= VALUE_MASK(AGG_TYPE_BITS);
......@@ -456,7 +456,7 @@ STATIC mp_obj_t uctypes_struct_attr_op(mp_obj_t self_in, qstr attr, mp_obj_t set
o->desc = sub->items[1];
o->addr = self->addr + offset;
o->flags = self->flags;
return o;
return MP_OBJ_FROM_PTR(o);
}
case ARRAY: {
mp_uint_t dummy;
......@@ -468,11 +468,11 @@ STATIC mp_obj_t uctypes_struct_attr_op(mp_obj_t self_in, qstr attr, mp_obj_t set
case PTR: {
mp_obj_uctypes_struct_t *o = m_new_obj(mp_obj_uctypes_struct_t);
o->base.type = &uctypes_struct_type;
o->desc = sub;
o->desc = MP_OBJ_FROM_PTR(sub);
o->addr = self->addr + offset;
o->flags = self->flags;
//printf("PTR/ARR base addr=%p\n", o->addr);
return o;
return MP_OBJ_FROM_PTR(o);
}
}
......@@ -494,7 +494,7 @@ STATIC void uctypes_struct_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
}
STATIC mp_obj_t uctypes_struct_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) {
mp_obj_uctypes_struct_t *self = self_in;
mp_obj_uctypes_struct_t *self = MP_OBJ_TO_PTR(self_in);
if (value == MP_OBJ_NULL) {
// delete
......@@ -505,7 +505,7 @@ STATIC mp_obj_t uctypes_struct_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_ob
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "struct: cannot index"));
}
mp_obj_tuple_t *t = (mp_obj_tuple_t*)self->desc;
mp_obj_tuple_t *t = MP_OBJ_TO_PTR(self->desc);
mp_int_t offset = MP_OBJ_SMALL_INT_VALUE(t->items[0]);
uint agg_type = GET_TYPE(offset, AGG_TYPE_BITS);
......@@ -530,7 +530,7 @@ STATIC mp_obj_t uctypes_struct_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_ob
o->desc = t->items[2];
o->addr = self->addr + size * index;
o->flags = self->flags;
return o;
return MP_OBJ_FROM_PTR(o);
}
} else if (agg_type == PTR) {
byte *p = *(void**)self->addr;
......@@ -545,7 +545,7 @@ STATIC mp_obj_t uctypes_struct_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_ob
o->desc = t->items[1];
o->addr = p + size * index;
o->flags = self->flags;
return o;
return MP_OBJ_FROM_PTR(o);
}
}
......@@ -559,7 +559,7 @@ STATIC mp_obj_t uctypes_struct_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_ob
STATIC mp_int_t uctypes_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo, mp_uint_t flags) {
(void)flags;
mp_obj_uctypes_struct_t *self = self_in;
mp_obj_uctypes_struct_t *self = MP_OBJ_TO_PTR(self_in);
mp_uint_t max_field_size = 0;
mp_uint_t size = uctypes_struct_size(self->desc, &max_field_size);
......@@ -575,7 +575,7 @@ STATIC mp_int_t uctypes_get_buffer(mp_obj_t self_in, mp_buffer_info_t *bufinfo,
STATIC mp_obj_t uctypes_struct_addressof(mp_obj_t buf) {
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(buf, &bufinfo, MP_BUFFER_READ);
return mp_obj_new_int((mp_int_t)bufinfo.buf);
return mp_obj_new_int((mp_int_t)(uintptr_t)bufinfo.buf);
}
MP_DEFINE_CONST_FUN_OBJ_1(uctypes_struct_addressof_obj, uctypes_struct_addressof);
......@@ -584,7 +584,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(uctypes_struct_addressof_obj, uctypes_struct_addressof
/// captured by reference (and thus memory pointed by bytearray may change
/// or become invalid at later time). Use bytes_at() to capture by value.
STATIC mp_obj_t uctypes_struct_bytearray_at(mp_obj_t ptr, mp_obj_t size) {
return mp_obj_new_bytearray_by_ref(mp_obj_int_get_truncated(size), (void*)mp_obj_int_get_truncated(ptr));
return mp_obj_new_bytearray_by_ref(mp_obj_int_get_truncated(size), (void*)(uintptr_t)mp_obj_int_get_truncated(ptr));
}
MP_DEFINE_CONST_FUN_OBJ_2(uctypes_struct_bytearray_at_obj, uctypes_struct_bytearray_at);
......@@ -593,7 +593,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(uctypes_struct_bytearray_at_obj, uctypes_struct_bytear
/// captured by value, i.e. copied. Use bytearray_at() to capture by reference
/// ("zero copy").
STATIC mp_obj_t uctypes_struct_bytes_at(mp_obj_t ptr, mp_obj_t size) {
return mp_obj_new_bytes((void*)mp_obj_int_get_truncated(ptr), mp_obj_int_get_truncated(size));
return mp_obj_new_bytes((void*)(uintptr_t)mp_obj_int_get_truncated(ptr), mp_obj_int_get_truncated(size));
}
MP_DEFINE_CONST_FUN_OBJ_2(uctypes_struct_bytes_at_obj, uctypes_struct_bytes_at);
......
......@@ -44,16 +44,16 @@ STATIC mp_obj_t hash_update(mp_obj_t self_in, mp_obj_t arg);
STATIC mp_obj_t hash_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
mp_arg_check_num(n_args, n_kw, 0, 1, false);
mp_obj_hash_t *o = m_new_obj_var(mp_obj_hash_t, char, sizeof(SHA256_CTX));
o->base.type = type_in;
o->base.type = MP_OBJ_TO_PTR(type_in);
sha256_init((SHA256_CTX*)o->state);
if (n_args == 1) {
hash_update(o, args[0]);
hash_update(MP_OBJ_FROM_PTR(o), args[0]);
}
return o;
return MP_OBJ_FROM_PTR(o);
}
STATIC mp_obj_t hash_update(mp_obj_t self_in, mp_obj_t arg) {
mp_obj_hash_t *self = self_in;
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
mp_buffer_info_t bufinfo;
mp_get_buffer_raise(arg, &bufinfo, MP_BUFFER_READ);
sha256_update((SHA256_CTX*)self->state, bufinfo.buf, bufinfo.len);
......@@ -62,7 +62,7 @@ STATIC mp_obj_t hash_update(mp_obj_t self_in, mp_obj_t arg) {
MP_DEFINE_CONST_FUN_OBJ_2(hash_update_obj, hash_update);
STATIC mp_obj_t hash_digest(mp_obj_t self_in) {
mp_obj_hash_t *self = self_in;
mp_obj_hash_t *self = MP_OBJ_TO_PTR(self_in);
vstr_t vstr;
vstr_init_len(&vstr, SHA256_BLOCK_SIZE);
sha256_final((SHA256_CTX*)self->state, (byte*)vstr.buf);
......
......@@ -37,7 +37,7 @@ STATIC mp_obj_list_t *get_heap(mp_obj_t heap_in) {
if (!MP_OBJ_IS_TYPE(heap_in, &mp_type_list)) {
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "heap must be a list"));
}
return heap_in;
return MP_OBJ_TO_PTR(heap_in);
}
STATIC void heap_siftdown(mp_obj_list_t *heap, mp_uint_t start_pos, mp_uint_t pos) {
......@@ -74,7 +74,7 @@ STATIC void heap_siftup(mp_obj_list_t *heap, mp_uint_t pos) {
STATIC mp_obj_t mod_uheapq_heappush(mp_obj_t heap_in, mp_obj_t item) {
mp_obj_list_t *heap = get_heap(heap_in);
mp_obj_list_append(heap, item);
mp_obj_list_append(heap_in, item);
heap_siftdown(heap, 0, heap->len - 1);
return mp_const_none;
}
......
......@@ -222,7 +222,7 @@ STATIC mp_obj_t mod_ujson_loads(mp_obj_t obj) {
mp_obj_list_init(&stack, 1);
stack.items[0] = stack_top;
} else {
mp_obj_list_append(&stack, stack_top);
mp_obj_list_append(MP_OBJ_FROM_PTR(&stack), stack_top);
}
stack_top = next;
stack_top_type = mp_obj_get_type(stack_top);
......
......@@ -53,12 +53,12 @@ typedef struct _mp_obj_match_t {
STATIC void match_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
(void)kind;
mp_obj_match_t *self = self_in;
mp_obj_match_t *self = MP_OBJ_TO_PTR(self_in);
mp_printf(print, "<match num=%d>", self->num_matches);
}
STATIC mp_obj_t match_group(mp_obj_t self_in, mp_obj_t no_in) {
mp_obj_match_t *self = self_in;
mp_obj_match_t *self = MP_OBJ_TO_PTR(self_in);
mp_int_t no = mp_obj_get_int(no_in);
if (no < 0 || no >= self->num_matches) {
nlr_raise(mp_obj_new_exception_arg1(&mp_type_IndexError, no_in));
......@@ -83,18 +83,18 @@ STATIC const mp_obj_type_t match_type = {
{ &mp_type_type },
.name = MP_QSTR_match,
.print = match_print,
.locals_dict = (mp_obj_t)&match_locals_dict,
.locals_dict = (void*)&match_locals_dict,
};
STATIC void re_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
(void)kind;
mp_obj_re_t *self = self_in;
mp_obj_re_t *self = MP_OBJ_TO_PTR(self_in);
mp_printf(print, "<re %p>", self);
}
STATIC mp_obj_t re_exec(bool is_anchored, uint n_args, const mp_obj_t *args) {
(void)n_args;
mp_obj_re_t *self = args[0];
mp_obj_re_t *self = MP_OBJ_TO_PTR(args[0]);
Subject subj;
mp_uint_t len;
subj.begin = mp_obj_str_get_data(args[1], &len);
......@@ -112,7 +112,7 @@ STATIC mp_obj_t re_exec(bool is_anchored, uint n_args, const mp_obj_t *args) {
match->base.type = &match_type;
match->num_matches = caps_num / 2; // caps_num counts start and end pointers
match->str = args[1];
return match;
return MP_OBJ_FROM_PTR(match);
}
STATIC mp_obj_t re_match(mp_uint_t n_args, const mp_obj_t *args) {
......@@ -126,7 +126,7 @@ STATIC mp_obj_t re_search(mp_uint_t n_args, const mp_obj_t *args) {
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(re_search_obj, 2, 4, re_search);
STATIC mp_obj_t re_split(mp_uint_t n_args, const mp_obj_t *args) {
mp_obj_re_t *self = args[0];
mp_obj_re_t *self = MP_OBJ_TO_PTR(args[0]);
Subject subj;
mp_uint_t len;
subj.begin = mp_obj_str_get_data(args[1], &len);
......@@ -179,7 +179,7 @@ STATIC const mp_obj_type_t re_type = {
{ &mp_type_type },
.name = MP_QSTR_ure,
.print = re_print,
.locals_dict = (mp_obj_t)&re_locals_dict,
.locals_dict = (void*)&re_locals_dict,
};
STATIC mp_obj_t mod_re_compile(mp_uint_t n_args, const mp_obj_t *args) {
......@@ -202,16 +202,16 @@ error:
if (flags & FLAG_DEBUG) {
re1_5_dumpcode(&o->re);
}
return o;
return MP_OBJ_FROM_PTR(o);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_re_compile_obj, 1, 2, mod_re_compile);
STATIC mp_obj_t mod_re_exec(bool is_anchored, uint n_args, const mp_obj_t *args) {
(void)n_args;
mp_obj_re_t *self = mod_re_compile(1, args);
mp_obj_t self = mod_re_compile(1, args);
const mp_obj_t args2[] = {self, args[1]};
mp_obj_match_t *match = re_exec(is_anchored, 2, args2);
mp_obj_t match = re_exec(is_anchored, 2, args2);
return match;
}
......
......@@ -68,7 +68,7 @@ STATIC NORETURN void fun_pos_args_mismatch(mp_obj_fun_bc_t *f, mp_uint_t expecte
#elif MICROPY_ERROR_REPORTING == MICROPY_ERROR_REPORTING_DETAILED
nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_TypeError,
"%q() takes %d positional arguments but %d were given",
mp_obj_fun_get_name(f), expected, given));
mp_obj_fun_get_name(MP_OBJ_FROM_PTR(f)), expected, given));
#endif
}
......@@ -95,7 +95,7 @@ void mp_setup_code_state(mp_code_state *code_state, mp_obj_fun_bc_t *self, mp_ui
mp_uint_t n_state = code_state->n_state;
// ip comes in as an offset into bytecode, so turn it into a true pointer
code_state->ip = self->bytecode + (mp_uint_t)code_state->ip;
code_state->ip = self->bytecode + (size_t)code_state->ip;
// store pointer to constant table
code_state->const_table = self->const_table;
......@@ -219,7 +219,7 @@ continue2:;
if (code_state->state[n_state - 1 - n_pos_args - i] == MP_OBJ_NULL) {
mp_map_elem_t *elem = NULL;
if ((scope_flags & MP_SCOPE_FLAG_DEFKWARGS) != 0) {
elem = mp_map_lookup(&((mp_obj_dict_t*)self->extra_args[n_def_pos_args])->map, arg_names[n_pos_args + i], MP_MAP_LOOKUP);
elem = mp_map_lookup(&((mp_obj_dict_t*)MP_OBJ_TO_PTR(self->extra_args[n_def_pos_args]))->map, arg_names[n_pos_args + i], MP_MAP_LOOKUP);
}
if (elem != NULL) {
code_state->state[n_state - 1 - n_pos_args - i] = elem->value;
......
......@@ -66,7 +66,7 @@ typedef struct _mp_exc_stack {
// bit 1 is whether the opcode was SETUP_WITH or SETUP_FINALLY
mp_obj_t *val_sp;
// Saved exception, valid if currently_in_except_block bit is 1
mp_obj_t prev_exc;
mp_obj_base_t *prev_exc;
} mp_exc_stack_t;
typedef struct _mp_code_state {
......
......@@ -147,7 +147,7 @@ mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index) {
return ((mp_obj_t*)p)[index];
// Extension to CPython: array of pointers
case 'P':
return mp_obj_new_int((mp_int_t)((void**)p)[index]);
return mp_obj_new_int((mp_int_t)(uintptr_t)((void**)p)[index]);
}
return MP_OBJ_NEW_SMALL_INT(val);
}
......@@ -199,7 +199,7 @@ mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr) {
if (val_type == 'O') {
return (mp_obj_t)(mp_uint_t)val;
} else if (val_type == 'S') {
const char *s_val = (const char*)(mp_uint_t)val;
const char *s_val = (const char*)(uintptr_t)(mp_uint_t)val;
return mp_obj_new_str(s_val, strlen(s_val), false);
#if MICROPY_PY_BUILTINS_FLOAT
} else if (val_type == 'f') {
......@@ -374,6 +374,6 @@ void mp_binary_set_val_array_from_int(char typecode, void *p, mp_uint_t index, m
#endif
// Extension to CPython: array of pointers
case 'P':
((void**)p)[index] = (void*)val;
((void**)p)[index] = (void*)(uintptr_t)val;
}
}
......@@ -44,7 +44,7 @@ STATIC const mp_obj_type_t mp_type_code = {
.name = MP_QSTR_code,
};
STATIC mp_obj_t code_execute(mp_obj_code_t *self, mp_obj_t globals, mp_obj_t locals) {
STATIC mp_obj_t code_execute(mp_obj_code_t *self, mp_obj_dict_t *globals, mp_obj_dict_t *locals) {
// save context and set new context
mp_obj_dict_t *old_globals = mp_globals_get();
mp_obj_dict_t *old_locals = mp_locals_get();
......@@ -54,7 +54,7 @@ STATIC mp_obj_t code_execute(mp_obj_code_t *self, mp_obj_t globals, mp_obj_t loc
// a bit of a hack: fun_bc will re-set globals, so need to make sure it's
// the correct one
if (MP_OBJ_IS_TYPE(self->module_fun, &mp_type_fun_bc)) {
mp_obj_fun_bc_t *fun_bc = self->module_fun;
mp_obj_fun_bc_t *fun_bc = MP_OBJ_TO_PTR(self->module_fun);
fun_bc->globals = globals;
}
......@@ -70,7 +70,7 @@ STATIC mp_obj_t code_execute(mp_obj_code_t *self, mp_obj_t globals, mp_obj_t loc
// exception; restore context and re-raise same exception
mp_globals_set(old_globals);
mp_locals_set(old_locals);
nlr_raise(nlr.ret_val);
nlr_jump(nlr.ret_val);
}
}
......@@ -101,7 +101,7 @@ STATIC mp_obj_t mp_builtin_compile(mp_uint_t n_args, const mp_obj_t *args) {
mp_obj_code_t *code = m_new_obj(mp_obj_code_t);
code->base.type = &mp_type_code;
code->module_fun = mp_parse_compile_execute(lex, parse_input_kind, NULL, NULL);
return code;
return MP_OBJ_FROM_PTR(code);
}
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_builtin_compile_obj, 3, 6, mp_builtin_compile);
......@@ -112,9 +112,9 @@ STATIC mp_obj_t eval_exec_helper(mp_uint_t n_args, const mp_obj_t *args, mp_pars
mp_obj_dict_t *globals = mp_globals_get();
mp_obj_dict_t *locals = mp_locals_get();
if (n_args > 1) {
globals = args[1];
globals = MP_OBJ_TO_PTR(args[1]);
if (n_args > 2) {
locals = args[2];
locals = MP_OBJ_TO_PTR(args[2]);
} else {
locals = globals;
}
......@@ -122,7 +122,7 @@ STATIC mp_obj_t eval_exec_helper(mp_uint_t n_args, const mp_obj_t *args, mp_pars
#if MICROPY_PY_BUILTINS_COMPILE
if (MP_OBJ_IS_TYPE(args[0], &mp_type_code)) {
return code_execute(args[0], globals, locals);
return code_execute(MP_OBJ_TO_PTR(args[0]), globals, locals);
}
#endif
......
......@@ -174,7 +174,7 @@ STATIC void do_execute_raw_code(mp_obj_t module_obj, mp_raw_code_t *raw_code) {
// exception; restore context and re-raise same exception
mp_globals_set(old_globals);
mp_locals_set(old_locals);
nlr_raise(nlr.ret_val);
nlr_jump(nlr.ret_val);
}
}
#endif
......@@ -237,15 +237,15 @@ mp_obj_t mp_builtin___import__(mp_uint_t n_args, const mp_obj_t *args) {
// "Relative imports use a module's __name__ attribute to determine that
// module's position in the package hierarchy."
level--;
mp_obj_t this_name_q = mp_obj_dict_get(mp_globals_get(), MP_OBJ_NEW_QSTR(MP_QSTR___name__));
mp_obj_t this_name_q = mp_obj_dict_get(MP_OBJ_FROM_PTR(mp_globals_get()), MP_OBJ_NEW_QSTR(MP_QSTR___name__));
assert(this_name_q != MP_OBJ_NULL);
#if MICROPY_CPYTHON_COMPAT
if (MP_OBJ_QSTR_VALUE(this_name_q) == MP_QSTR___main__) {
// This is a module run by -m command-line switch, get its real name from backup attribute
this_name_q = mp_obj_dict_get(mp_globals_get(), MP_OBJ_NEW_QSTR(MP_QSTR___main__));
this_name_q = mp_obj_dict_get(MP_OBJ_FROM_PTR(mp_globals_get()), MP_OBJ_NEW_QSTR(MP_QSTR___main__));
}
#endif
mp_map_t *globals_map = mp_obj_dict_get_map(mp_globals_get());
mp_map_t *globals_map = &mp_globals_get()->map;
mp_map_elem_t *elem = mp_map_lookup(globals_map, MP_OBJ_NEW_QSTR(MP_QSTR___path__), MP_MAP_LOOKUP);
bool is_pkg = (elem != NULL);
......@@ -338,8 +338,8 @@ mp_obj_t mp_builtin___import__(mp_uint_t n_args, const mp_obj_t *args) {
// name to __main__ instead of real name).
// TODO: Duplicated below too.
if (fromtuple == mp_const_false) {
mp_obj_module_t *o = module_obj;
mp_obj_dict_store(o->globals, MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR___main__));
mp_obj_module_t *o = MP_OBJ_TO_PTR(module_obj);
mp_obj_dict_store(MP_OBJ_FROM_PTR(o->globals), MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR___main__));
}
do_load_from_lexer(module_obj, lex, mod_str);
return module_obj;
......@@ -409,11 +409,11 @@ mp_obj_t mp_builtin___import__(mp_uint_t n_args, const mp_obj_t *args) {
// this module for command-line "-m" option (set module's
// name to __main__ instead of real name).
if (i == mod_len && fromtuple == mp_const_false) {
mp_obj_module_t *o = module_obj;
mp_obj_dict_store(o->globals, MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR___main__));
mp_obj_module_t *o = MP_OBJ_TO_PTR(module_obj);
mp_obj_dict_store(MP_OBJ_FROM_PTR(o->globals), MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_OBJ_NEW_QSTR(MP_QSTR___main__));
#if MICROPY_CPYTHON_COMPAT
// Store real name in "__main__" attribute. Choosen semi-randonly, to reuse existing qstr's.
mp_obj_dict_store(o->globals, MP_OBJ_NEW_QSTR(MP_QSTR___main__), MP_OBJ_NEW_QSTR(mod_name));
mp_obj_dict_store(MP_OBJ_FROM_PTR(o->globals), MP_OBJ_NEW_QSTR(MP_QSTR___main__), MP_OBJ_NEW_QSTR(mod_name));
#endif
}
......
......@@ -38,6 +38,11 @@
#define DUMMY_DATA_SIZE (BYTES_FOR_INT)
struct _emit_t {
// Accessed as mp_obj_t, so must be aligned as such, and we rely on the
// memory allocator returning a suitably aligned pointer.
// Should work for cases when mp_obj_t is 64-bit on a 32-bit machine.
byte dummy_data[DUMMY_DATA_SIZE];
pass_kind_t pass : 8;
mp_uint_t last_emit_was_return_value : 8;
......@@ -51,10 +56,10 @@ struct _emit_t {
mp_uint_t max_num_labels;
mp_uint_t *label_offsets;
mp_uint_t code_info_offset;
mp_uint_t code_info_size;
mp_uint_t bytecode_offset;
mp_uint_t bytecode_size;
size_t code_info_offset;
size_t code_info_size;
size_t bytecode_offset;
size_t bytecode_size;
byte *code_base; // stores both byte code and code info
#if MICROPY_PERSISTENT_CODE
......@@ -63,9 +68,6 @@ struct _emit_t {
uint16_t ct_cur_raw_code;
#endif
mp_uint_t *const_table;
// Accessed as mp_uint_t, so must be aligned as such
byte dummy_data[DUMMY_DATA_SIZE];
};
emit_t *emit_bc_new(void) {
......@@ -224,16 +226,6 @@ STATIC void emit_write_bytecode_byte_const(emit_t *emit, byte b, mp_uint_t n, mp
}
emit_write_bytecode_byte_uint(emit, b, n);
}
#else
STATIC void emit_write_bytecode_byte_ptr(emit_t *emit, byte b, void *ptr) {
// aligns the pointer so it is friendly to GC
emit_write_bytecode_byte(emit, b);
emit->bytecode_offset = (mp_uint_t)MP_ALIGN(emit->bytecode_offset, sizeof(mp_uint_t));
mp_uint_t *c = (mp_uint_t*)emit_get_cur_to_write_bytecode(emit, sizeof(mp_uint_t));
// Verify thar c is already uint-aligned
assert(c == MP_ALIGN(c, sizeof(mp_uint_t)));
*c = (mp_uint_t)ptr;
}
#endif
STATIC void emit_write_bytecode_byte_qstr(emit_t* emit, byte b, qstr qst) {
......@@ -248,13 +240,19 @@ STATIC void emit_write_bytecode_byte_qstr(emit_t* emit, byte b, qstr qst) {
#endif
}
STATIC void emit_write_bytecode_byte_obj(emit_t *emit, byte b, void *ptr) {
STATIC void emit_write_bytecode_byte_obj(emit_t *emit, byte b, mp_obj_t obj) {
#if MICROPY_PERSISTENT_CODE
emit_write_bytecode_byte_const(emit, b,
emit->scope->num_pos_args + emit->scope->num_kwonly_args
+ emit->ct_cur_obj++, (mp_uint_t)ptr);
+ emit->ct_cur_obj++, (mp_uint_t)obj);
#else
emit_write_bytecode_byte_ptr(emit, b, ptr);
// aligns the pointer so it is friendly to GC
emit_write_bytecode_byte(emit, b);
emit->bytecode_offset = (size_t)MP_ALIGN(emit->bytecode_offset, sizeof(mp_obj_t));
mp_obj_t *c = (mp_obj_t*)emit_get_cur_to_write_bytecode(emit, sizeof(mp_obj_t));
// Verify thar c is already uint-aligned
assert(c == MP_ALIGN(c, sizeof(mp_obj_t)));
*c = obj;
#endif
}
......@@ -262,9 +260,15 @@ STATIC void emit_write_bytecode_byte_raw_code(emit_t *emit, byte b, mp_raw_code_
#if MICROPY_PERSISTENT_CODE
emit_write_bytecode_byte_const(emit, b,
emit->scope->num_pos_args + emit->scope->num_kwonly_args
+ emit->ct_num_obj + emit->ct_cur_raw_code++, (mp_uint_t)rc);
+ emit->ct_num_obj + emit->ct_cur_raw_code++, (mp_uint_t)(uintptr_t)rc);
#else
emit_write_bytecode_byte_ptr(emit, b, rc);
// aligns the pointer so it is friendly to GC
emit_write_bytecode_byte(emit, b);
emit->bytecode_offset = (size_t)MP_ALIGN(emit->bytecode_offset, sizeof(void*));
void **c = (void**)emit_get_cur_to_write_bytecode(emit, sizeof(void*));
// Verify thar c is already uint-aligned
assert(c == MP_ALIGN(c, sizeof(void*)));
*c = rc;
#endif
}
......@@ -416,7 +420,7 @@ void mp_emit_bc_end_pass(emit_t *emit) {
if (emit->pass == MP_PASS_CODE_SIZE) {
#if !MICROPY_PERSISTENT_CODE
// so bytecode is aligned
emit->code_info_offset = (mp_uint_t)MP_ALIGN(emit->code_info_offset, sizeof(mp_uint_t));
emit->code_info_offset = (size_t)MP_ALIGN(emit->code_info_offset, sizeof(mp_uint_t));
#endif
// calculate size of total code-info + bytecode, in bytes
......@@ -523,7 +527,7 @@ void mp_emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) {
case MP_TOKEN_KW_NONE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_NONE); break;
case MP_TOKEN_KW_TRUE: emit_write_bytecode_byte(emit, MP_BC_LOAD_CONST_TRUE); break;