summaryrefslogtreecommitdiffstatshomepage
path: root/py/runtime.c
diff options
context:
space:
mode:
Diffstat (limited to 'py/runtime.c')
-rw-r--r--py/runtime.c142
1 files changed, 73 insertions, 69 deletions
diff --git a/py/runtime.c b/py/runtime.c
index 197c08b55a..86e297d7bd 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -45,7 +45,7 @@ typedef struct _mp_code_t {
int n_args;
int n_locals;
int n_stack;
- bool is_generator;
+ MP_BOOL is_generator;
union {
struct {
byte *code;
@@ -70,60 +70,60 @@ FILE *fp_write_code = NULL;
void rt_init(void) {
// locals = globals for outer module (see Objects/frameobject.c/PyFrame_New())
map_locals = map_globals = mp_map_new(MP_MAP_QSTR, 1);
- mp_qstr_map_lookup(map_globals, MP_QSTR___name__, true)->value = mp_obj_new_str(MP_QSTR___main__);
+ mp_qstr_map_lookup(map_globals, MP_QSTR___name__, MP_TRUE)->value = mp_obj_new_str(MP_QSTR___main__);
// init built-in hash table
mp_map_init(&map_builtins, MP_MAP_QSTR, 3);
// built-in exceptions (TODO, make these proper classes)
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_AttributeError, true)->value = mp_obj_new_exception(MP_QSTR_AttributeError);
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_IndexError, true)->value = mp_obj_new_exception(MP_QSTR_IndexError);
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_KeyError, true)->value = mp_obj_new_exception(MP_QSTR_KeyError);
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_NameError, true)->value = mp_obj_new_exception(MP_QSTR_NameError);
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_TypeError, true)->value = mp_obj_new_exception(MP_QSTR_TypeError);
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_SyntaxError, true)->value = mp_obj_new_exception(MP_QSTR_SyntaxError);
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_ValueError, true)->value = mp_obj_new_exception(MP_QSTR_ValueError);
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_AttributeError, MP_TRUE)->value = mp_obj_new_exception(MP_QSTR_AttributeError);
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_IndexError, MP_TRUE)->value = mp_obj_new_exception(MP_QSTR_IndexError);
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_KeyError, MP_TRUE)->value = mp_obj_new_exception(MP_QSTR_KeyError);
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_NameError, MP_TRUE)->value = mp_obj_new_exception(MP_QSTR_NameError);
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_TypeError, MP_TRUE)->value = mp_obj_new_exception(MP_QSTR_TypeError);
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_SyntaxError, MP_TRUE)->value = mp_obj_new_exception(MP_QSTR_SyntaxError);
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_ValueError, MP_TRUE)->value = mp_obj_new_exception(MP_QSTR_ValueError);
// built-in objects
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_Ellipsis, true)->value = mp_const_ellipsis;
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_Ellipsis, MP_TRUE)->value = mp_const_ellipsis;
// built-in core functions
- mp_qstr_map_lookup(&map_builtins, MP_QSTR___build_class__, true)->value = rt_make_function_2(mp_builtin___build_class__);
- mp_qstr_map_lookup(&map_builtins, MP_QSTR___repl_print__, true)->value = rt_make_function_1(mp_builtin___repl_print__);
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR___build_class__, MP_TRUE)->value = rt_make_function_2(mp_builtin___build_class__);
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR___repl_print__, MP_TRUE)->value = rt_make_function_1(mp_builtin___repl_print__);
// built-in types
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_bool, true)->value = (mp_obj_t)&bool_type;
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_bool, MP_TRUE)->value = (mp_obj_t)&bool_type;
#if MICROPY_ENABLE_FLOAT
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_complex, true)->value = (mp_obj_t)&complex_type;
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_complex, MP_TRUE)->value = (mp_obj_t)&complex_type;
#endif
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_dict, true)->value = (mp_obj_t)&dict_type;
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_dict, MP_TRUE)->value = (mp_obj_t)&dict_type;
#if MICROPY_ENABLE_FLOAT
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_float, true)->value = (mp_obj_t)&float_type;
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_float, MP_TRUE)->value = (mp_obj_t)&float_type;
#endif
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_int, true)->value = (mp_obj_t)&int_type;
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_list, true)->value = (mp_obj_t)&list_type;
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_set, true)->value = (mp_obj_t)&set_type;
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_tuple, true)->value = (mp_obj_t)&tuple_type;
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_type, true)->value = (mp_obj_t)&mp_builtin_type_obj; // TODO
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_int, MP_TRUE)->value = (mp_obj_t)&int_type;
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_list, MP_TRUE)->value = (mp_obj_t)&list_type;
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_set, MP_TRUE)->value = (mp_obj_t)&set_type;
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_tuple, MP_TRUE)->value = (mp_obj_t)&tuple_type;
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_type, MP_TRUE)->value = (mp_obj_t)&mp_builtin_type_obj; // TODO
// built-in user functions; TODO covert all to &mp_builtin_xxx's
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_abs, true)->value = rt_make_function_1(mp_builtin_abs);
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_all, true)->value = rt_make_function_1(mp_builtin_all);
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_any, true)->value = rt_make_function_1(mp_builtin_any);
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_callable, true)->value = rt_make_function_1(mp_builtin_callable);
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_chr, true)->value = rt_make_function_1(mp_builtin_chr);
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_divmod, true)->value = rt_make_function_2(mp_builtin_divmod);
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_hash, true)->value = (mp_obj_t)&mp_builtin_hash_obj;
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_iter, true)->value = (mp_obj_t)&mp_builtin_iter_obj;
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_len, true)->value = rt_make_function_1(mp_builtin_len);
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_max, true)->value = rt_make_function_var(1, mp_builtin_max);
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_min, true)->value = rt_make_function_var(1, mp_builtin_min);
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_next, true)->value = (mp_obj_t)&mp_builtin_next_obj;
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_ord, true)->value = rt_make_function_1(mp_builtin_ord);
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_pow, true)->value = rt_make_function_var(2, mp_builtin_pow);
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_print, true)->value = rt_make_function_var(0, mp_builtin_print);
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_range, true)->value = rt_make_function_var(1, mp_builtin_range);
- mp_qstr_map_lookup(&map_builtins, MP_QSTR_sum, true)->value = rt_make_function_var(1, mp_builtin_sum);
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_abs, MP_TRUE)->value = rt_make_function_1(mp_builtin_abs);
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_all, MP_TRUE)->value = rt_make_function_1(mp_builtin_all);
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_any, MP_TRUE)->value = rt_make_function_1(mp_builtin_any);
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_callable, MP_TRUE)->value = rt_make_function_1(mp_builtin_callable);
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_chr, MP_TRUE)->value = rt_make_function_1(mp_builtin_chr);
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_divmod, MP_TRUE)->value = rt_make_function_2(mp_builtin_divmod);
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_hash, MP_TRUE)->value = (mp_obj_t)&mp_builtin_hash_obj;
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_iter, MP_TRUE)->value = (mp_obj_t)&mp_builtin_iter_obj;
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_len, MP_TRUE)->value = rt_make_function_1(mp_builtin_len);
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_max, MP_TRUE)->value = rt_make_function_var(1, mp_builtin_max);
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_min, MP_TRUE)->value = rt_make_function_var(1, mp_builtin_min);
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_next, MP_TRUE)->value = (mp_obj_t)&mp_builtin_next_obj;
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_ord, MP_TRUE)->value = rt_make_function_1(mp_builtin_ord);
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_pow, MP_TRUE)->value = rt_make_function_var(2, mp_builtin_pow);
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_print, MP_TRUE)->value = rt_make_function_var(0, mp_builtin_print);
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_range, MP_TRUE)->value = rt_make_function_var(1, mp_builtin_range);
+ mp_qstr_map_lookup(&map_builtins, MP_QSTR_sum, MP_TRUE)->value = rt_make_function_var(1, mp_builtin_sum);
next_unique_code_id = 1; // 0 indicates "no code"
unique_codes = NULL;
@@ -154,7 +154,7 @@ static void alloc_unique_codes(void) {
}
}
-void rt_assign_byte_code(int unique_code_id, byte *code, uint len, int n_args, int n_locals, int n_stack, bool is_generator) {
+void rt_assign_byte_code(int unique_code_id, byte *code, uint len, int n_args, int n_locals, int n_stack, MP_BOOL is_generator) {
alloc_unique_codes();
assert(1 <= unique_code_id && unique_code_id < next_unique_code_id);
@@ -197,7 +197,7 @@ void rt_assign_native_code(int unique_code_id, void *fun, uint len, int n_args)
unique_codes[unique_code_id].n_args = n_args;
unique_codes[unique_code_id].n_locals = 0;
unique_codes[unique_code_id].n_stack = 0;
- unique_codes[unique_code_id].is_generator = false;
+ unique_codes[unique_code_id].is_generator = MP_FALSE;
unique_codes[unique_code_id].u_native.fun = fun;
//printf("native code: %d bytes\n", len);
@@ -230,7 +230,7 @@ void rt_assign_inline_asm_code(int unique_code_id, void *fun, uint len, int n_ar
unique_codes[unique_code_id].n_args = n_args;
unique_codes[unique_code_id].n_locals = 0;
unique_codes[unique_code_id].n_stack = 0;
- unique_codes[unique_code_id].is_generator = false;
+ unique_codes[unique_code_id].is_generator = MP_FALSE;
unique_codes[unique_code_id].u_inline_asm.fun = fun;
#ifdef DEBUG_PRINT
@@ -252,8 +252,8 @@ void rt_assign_inline_asm_code(int unique_code_id, void *fun, uint len, int n_ar
#endif
}
-static bool fit_small_int(mp_small_int_t o) {
- return true;
+static MP_BOOL fit_small_int(mp_small_int_t o) {
+ return MP_TRUE;
}
int rt_is_true(mp_obj_t arg) {
@@ -290,10 +290,10 @@ mp_obj_t rt_load_const_dec(qstr qstr) {
const char *s = qstr_str(qstr);
int in = PARSE_DEC_IN_INTG;
mp_float_t dec_val = 0;
- bool exp_neg = false;
+ MP_BOOL exp_neg = MP_FALSE;
int exp_val = 0;
int exp_extra = 0;
- bool imag = false;
+ MP_BOOL imag = MP_FALSE;
for (; *s; s++) {
int dig = *s;
if ('0' <= dig && dig <= '9') {
@@ -314,11 +314,11 @@ mp_obj_t rt_load_const_dec(qstr qstr) {
s++;
} else if (s[1] == '-') {
s++;
- exp_neg = true;
+ exp_neg = MP_TRUE;
}
} else if (dig == 'J' || dig == 'j') {
s++;
- imag = true;
+ imag = MP_TRUE;
break;
} else {
// unknown character
@@ -356,11 +356,11 @@ mp_obj_t rt_load_const_str(qstr qstr) {
mp_obj_t rt_load_name(qstr qstr) {
// logic: search locals, globals, builtins
DEBUG_OP_printf("load name %s\n", qstr_str(qstr));
- mp_map_elem_t *elem = mp_qstr_map_lookup(map_locals, qstr, false);
+ mp_map_elem_t *elem = mp_qstr_map_lookup(map_locals, qstr, MP_FALSE);
if (elem == NULL) {
- elem = mp_qstr_map_lookup(map_globals, qstr, false);
+ elem = mp_qstr_map_lookup(map_globals, qstr, MP_FALSE);
if (elem == NULL) {
- elem = mp_qstr_map_lookup(&map_builtins, qstr, false);
+ elem = mp_qstr_map_lookup(&map_builtins, qstr, MP_FALSE);
if (elem == NULL) {
nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_NameError, "name '%s' is not defined", qstr_str(qstr)));
}
@@ -372,9 +372,9 @@ mp_obj_t rt_load_name(qstr qstr) {
mp_obj_t rt_load_global(qstr qstr) {
// logic: search globals, builtins
DEBUG_OP_printf("load global %s\n", qstr_str(qstr));
- mp_map_elem_t *elem = mp_qstr_map_lookup(map_globals, qstr, false);
+ mp_map_elem_t *elem = mp_qstr_map_lookup(map_globals, qstr, MP_FALSE);
if (elem == NULL) {
- elem = mp_qstr_map_lookup(&map_builtins, qstr, false);
+ elem = mp_qstr_map_lookup(&map_builtins, qstr, MP_FALSE);
if (elem == NULL) {
nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_NameError, "name '%s' is not defined", qstr_str(qstr)));
}
@@ -384,7 +384,7 @@ mp_obj_t rt_load_global(qstr qstr) {
mp_obj_t rt_load_build_class(void) {
DEBUG_OP_printf("load_build_class\n");
- mp_map_elem_t *elem = mp_qstr_map_lookup(&map_builtins, MP_QSTR___build_class__, false);
+ mp_map_elem_t *elem = mp_qstr_map_lookup(&map_builtins, MP_QSTR___build_class__, MP_FALSE);
if (elem == NULL) {
nlr_jump(mp_obj_new_exception_msg(MP_QSTR_NameError, "name '__build_class__' is not defined"));
}
@@ -401,12 +401,12 @@ void rt_set_cell(mp_obj_t cell, mp_obj_t val) {
void rt_store_name(qstr qstr, mp_obj_t obj) {
DEBUG_OP_printf("store name %s <- %p\n", qstr_str(qstr), obj);
- mp_qstr_map_lookup(map_locals, qstr, true)->value = obj;
+ mp_qstr_map_lookup(map_locals, qstr, MP_TRUE)->value = obj;
}
void rt_store_global(qstr qstr, mp_obj_t obj) {
DEBUG_OP_printf("store global %s <- %p\n", qstr_str(qstr), obj);
- mp_qstr_map_lookup(map_globals, qstr, true)->value = obj;
+ mp_qstr_map_lookup(map_globals, qstr, MP_TRUE)->value = obj;
}
mp_obj_t rt_unary_op(int op, mp_obj_t arg) {
@@ -750,7 +750,7 @@ mp_obj_t rt_store_map(mp_obj_t map, mp_obj_t key, mp_obj_t value) {
mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
DEBUG_OP_printf("load attr %s\n", qstr_str(attr));
if (MP_OBJ_IS_TYPE(base, &class_type)) {
- mp_map_elem_t *elem = mp_qstr_map_lookup(mp_obj_class_get_locals(base), attr, false);
+ mp_map_elem_t *elem = mp_qstr_map_lookup(mp_obj_class_get_locals(base), attr, MP_FALSE);
if (elem == NULL) {
// TODO what about generic method lookup?
goto no_attr;
@@ -760,7 +760,7 @@ mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
return mp_obj_instance_load_attr(base, attr);
} else if (MP_OBJ_IS_TYPE(base, &module_type)) {
DEBUG_OP_printf("lookup module map %p\n", mp_obj_module_get_globals(base));
- mp_map_elem_t *elem = mp_qstr_map_lookup(mp_obj_module_get_globals(base), attr, false);
+ mp_map_elem_t *elem = mp_qstr_map_lookup(mp_obj_module_get_globals(base), attr, MP_FALSE);
if (elem == NULL) {
// TODO what about generic method lookup?
goto no_attr;
@@ -769,10 +769,12 @@ mp_obj_t rt_load_attr(mp_obj_t base, qstr attr) {
} else if (MP_OBJ_IS_OBJ(base)) {
// generic method lookup
mp_obj_base_t *o = base;
- const mp_method_t *meth = &o->type->methods[0];
- for (; meth->name != NULL; meth++) {
- if (strcmp(meth->name, qstr_str(attr)) == 0) {
- return mp_obj_new_bound_meth(base, (mp_obj_t)meth->fun);
+ const mp_method_t *meth = o->type->methods;
+ if (meth != NULL) {
+ for (; meth->name != NULL; meth++) {
+ if (strcmp(meth->name, qstr_str(attr)) == 0) {
+ return mp_obj_new_bound_meth(base, (mp_obj_t)meth->fun);
+ }
}
}
}
@@ -793,12 +795,14 @@ void rt_load_method(mp_obj_t base, qstr attr, mp_obj_t *dest) {
} else if (MP_OBJ_IS_OBJ(base)) {
// generic method lookup
mp_obj_base_t *o = base;
- const mp_method_t *meth = &o->type->methods[0];
- for (; meth->name != NULL; meth++) {
- if (strcmp(meth->name, qstr_str(attr)) == 0) {
- dest[1] = (mp_obj_t)meth->fun;
- dest[0] = base;
- return;
+ const mp_method_t *meth = o->type->methods;
+ if (meth != NULL) {
+ for (; meth->name != NULL; meth++) {
+ if (strcmp(meth->name, qstr_str(attr)) == 0) {
+ dest[1] = (mp_obj_t)meth->fun;
+ dest[0] = base;
+ return;
+ }
}
}
}
@@ -813,13 +817,13 @@ void rt_store_attr(mp_obj_t base, qstr attr, mp_obj_t value) {
if (MP_OBJ_IS_TYPE(base, &class_type)) {
// TODO CPython allows STORE_ATTR to a class, but is this the correct implementation?
mp_map_t *locals = mp_obj_class_get_locals(base);
- mp_qstr_map_lookup(locals, attr, true)->value = value;
+ mp_qstr_map_lookup(locals, attr, MP_TRUE)->value = value;
} else if (MP_OBJ_IS_TYPE(base, &instance_type)) {
mp_obj_instance_store_attr(base, attr, value);
} else if (MP_OBJ_IS_TYPE(base, &module_type)) {
// TODO CPython allows STORE_ATTR to a module, but is this the correct implementation?
mp_map_t *globals = mp_obj_module_get_globals(base);
- mp_qstr_map_lookup(globals, attr, true)->value = value;
+ mp_qstr_map_lookup(globals, attr, MP_TRUE)->value = value;
} else {
nlr_jump(mp_obj_new_exception_msg_2_args(MP_QSTR_AttributeError, "'%s' object has no attribute '%s'", mp_obj_get_type_str(base), qstr_str(attr)));
}