diff options
-rw-r--r-- | py/compile.c | 5 | ||||
-rw-r--r-- | py/emit.h | 2 | ||||
-rw-r--r-- | py/emitbc.c | 8 | ||||
-rw-r--r-- | py/emitcpy.c | 4 | ||||
-rw-r--r-- | py/emitnative.c | 2 | ||||
-rw-r--r-- | py/objexcept.c | 18 | ||||
-rw-r--r-- | py/parse.c | 6 | ||||
-rw-r--r-- | py/qstr.c | 16 | ||||
-rw-r--r-- | py/qstr.h | 2 | ||||
-rw-r--r-- | py/runtime.c | 2 | ||||
-rw-r--r-- | py/showbc.c | 8 | ||||
-rw-r--r-- | py/vm.c | 5 | ||||
-rw-r--r-- | unix/main.c | 14 |
13 files changed, 59 insertions, 33 deletions
diff --git a/py/compile.c b/py/compile.c index ebd2abb5c4..c1a7955dcd 100644 --- a/py/compile.c +++ b/py/compile.c @@ -2501,7 +2501,7 @@ void compile_node(compiler_t *comp, mp_parse_node_t pn) { if (MP_PARSE_NODE_IS_NULL(pn)) { // pass } else if (MP_PARSE_NODE_IS_LEAF(pn)) { - int arg = MP_PARSE_NODE_LEAF_ARG(pn); + machine_int_t arg = MP_PARSE_NODE_LEAF_ARG(pn); switch (MP_PARSE_NODE_LEAF_KIND(pn)) { case MP_PARSE_NODE_ID: EMIT_ARG(load_id, arg); break; case MP_PARSE_NODE_SMALL_INT: EMIT_ARG(load_const_small_int, arg); break; @@ -3030,7 +3030,8 @@ void compile_scope_compute_things(compiler_t *comp, scope_t *scope) { scope->flags |= SCOPE_FLAG_OPTIMISED; // TODO possibly other ways it can be nested - if (scope->parent->kind == SCOPE_FUNCTION || (scope->parent->kind == SCOPE_CLASS && scope->parent->parent->kind == SCOPE_FUNCTION)) { + // Note that we don't actually use this information at the moment (for CPython compat only) + if ((SCOPE_FUNCTION <= scope->parent->kind && scope->parent->kind <= SCOPE_SET_COMP) || (scope->parent->kind == SCOPE_CLASS && scope->parent->parent->kind == SCOPE_FUNCTION)) { scope->flags |= SCOPE_FLAG_NESTED; } } @@ -34,7 +34,7 @@ typedef struct _emit_method_table_t { void (*import_from)(emit_t *emit, qstr qstr); void (*import_star)(emit_t *emit); void (*load_const_tok)(emit_t *emit, mp_token_kind_t tok); - void (*load_const_small_int)(emit_t *emit, int arg); + void (*load_const_small_int)(emit_t *emit, machine_int_t arg); void (*load_const_int)(emit_t *emit, qstr qstr); void (*load_const_dec)(emit_t *emit, qstr qstr); void (*load_const_id)(emit_t *emit, qstr qstr); diff --git a/py/emitbc.c b/py/emitbc.c index 9fa2880ecb..6d9523fe56 100644 --- a/py/emitbc.c +++ b/py/emitbc.c @@ -108,7 +108,7 @@ static void emit_write_byte_code_byte_byte(emit_t* emit, byte b1, uint b2) { } // integers (for small ints) are stored as 24 bits, in excess -static void emit_write_byte_code_byte_int(emit_t* emit, byte b1, int num) { +static void emit_write_byte_code_byte_int(emit_t* emit, byte b1, machine_int_t num) { num += 0x800000; assert(0 <= num && num <= 0xffffff); byte* c = emit_get_cur_to_write_byte_code(emit, 4); @@ -319,7 +319,7 @@ static void emit_bc_load_const_tok(emit_t *emit, mp_token_kind_t tok) { } } -static void emit_bc_load_const_small_int(emit_t *emit, int arg) { +static void emit_bc_load_const_small_int(emit_t *emit, machine_int_t arg) { emit_pre(emit, 1); emit_write_byte_code_byte_int(emit, MP_BC_LOAD_CONST_SMALL_INT, arg); } @@ -390,7 +390,7 @@ static void emit_bc_load_attr(emit_t *emit, qstr qstr) { } static void emit_bc_load_method(emit_t *emit, qstr qstr) { - emit_pre(emit, 0); + emit_pre(emit, 1); emit_write_byte_code_byte_qstr(emit, MP_BC_LOAD_METHOD, qstr); } @@ -707,7 +707,7 @@ static void emit_bc_call_method(emit_t *emit, int n_positional, int n_keyword, b if (have_dbl_star_arg) { s += 1; } - emit_pre(emit, -n_positional - 2 * n_keyword - s); + emit_pre(emit, -1 - n_positional - 2 * n_keyword - s); int op; if (have_star_arg) { if (have_dbl_star_arg) { diff --git a/py/emitcpy.c b/py/emitcpy.c index 71861c918d..2e5c34cb2b 100644 --- a/py/emitcpy.c +++ b/py/emitcpy.c @@ -149,10 +149,10 @@ static void emit_cpy_load_const_tok(emit_t *emit, mp_token_kind_t tok) { } } -static void emit_cpy_load_const_small_int(emit_t *emit, int arg) { +static void emit_cpy_load_const_small_int(emit_t *emit, machine_int_t arg) { emit_pre(emit, 1, 3); if (emit->pass == PASS_3) { - printf("LOAD_CONST %d\n", arg); + printf("LOAD_CONST " INT_FMT "\n", arg); } } diff --git a/py/emitnative.c b/py/emitnative.c index 258aa9fce5..1e5ea1fa9b 100644 --- a/py/emitnative.c +++ b/py/emitnative.c @@ -599,7 +599,7 @@ static void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) { emit_post_push_imm(emit, vtype, val); } -static void emit_native_load_const_small_int(emit_t *emit, int arg) { +static void emit_native_load_const_small_int(emit_t *emit, machine_int_t arg) { emit_pre(emit); if (emit->do_viper_types) { emit_post_push_imm(emit, VTYPE_INT, arg); diff --git a/py/objexcept.c b/py/objexcept.c index c91b71dd9e..8eb4e966e9 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -13,20 +13,19 @@ // This is unified class for C-level and Python-level exceptions // Python-level exception have empty ->msg and all arguments are in -// args tuple. C-level excepttion likely have ->msg, and may as well -// have args tuple (or otherwise have it as NULL). +// args tuple. C-level excepttion likely have ->msg and args is empty. typedef struct mp_obj_exception_t { mp_obj_base_t base; mp_obj_t traceback; // a list object, holding (file,line,block) as numbers (not Python objects); a hack for now qstr id; - qstr msg; + vstr_t *msg; mp_obj_tuple_t args; } mp_obj_exception_t; void exception_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) { mp_obj_exception_t *o = o_in; - if (o->msg != 0) { - print(env, "%s: %s", qstr_str(o->id), qstr_str(o->msg)); + if (o->msg != NULL) { + print(env, "%s: %s", qstr_str(o->id), vstr_str(o->msg)); } else { // Yes, that's how CPython has it if (kind == PRINT_REPR) { @@ -55,7 +54,7 @@ static mp_obj_t exception_call(mp_obj_t self_in, uint n_args, uint n_kw, const m mp_obj_exception_t *o = m_new_obj_var(mp_obj_exception_t, mp_obj_t, n_args); o->base.type = &exception_type; o->id = base->id; - o->msg = 0; + o->msg = NULL; o->args.len = n_args; memcpy(o->args.items, args, n_args * sizeof(mp_obj_t)); return o; @@ -92,15 +91,14 @@ mp_obj_t mp_obj_new_exception_msg_varg(qstr id, const char *fmt, ...) { o->id = id; o->args.len = 0; if (fmt == NULL) { - o->msg = 0; + o->msg = NULL; } else { // render exception message - vstr_t *vstr = vstr_new(); + o->msg = vstr_new(); va_list ap; va_start(ap, fmt); - vstr_vprintf(vstr, fmt, ap); + vstr_vprintf(o->msg, fmt, ap); va_end(ap); - o->msg = qstr_from_strn_take(vstr->buf, vstr->alloc, vstr->len); } return o; diff --git a/py/parse.c b/py/parse.c index d9969d6785..d0776cefba 100644 --- a/py/parse.c +++ b/py/parse.c @@ -172,15 +172,15 @@ void mp_parse_node_print(mp_parse_node_t pn, int indent) { if (MP_PARSE_NODE_IS_NULL(pn)) { printf("NULL\n"); } else if (MP_PARSE_NODE_IS_LEAF(pn)) { - int arg = MP_PARSE_NODE_LEAF_ARG(pn); + machine_int_t arg = MP_PARSE_NODE_LEAF_ARG(pn); switch (MP_PARSE_NODE_LEAF_KIND(pn)) { case MP_PARSE_NODE_ID: printf("id(%s)\n", qstr_str(arg)); break; - case MP_PARSE_NODE_SMALL_INT: printf("int(%d)\n", arg); break; + case MP_PARSE_NODE_SMALL_INT: printf("int(" INT_FMT ")\n", arg); break; case MP_PARSE_NODE_INTEGER: printf("int(%s)\n", qstr_str(arg)); break; case MP_PARSE_NODE_DECIMAL: printf("dec(%s)\n", qstr_str(arg)); break; case MP_PARSE_NODE_STRING: printf("str(%s)\n", qstr_str(arg)); break; case MP_PARSE_NODE_BYTES: printf("bytes(%s)\n", qstr_str(arg)); break; - case MP_PARSE_NODE_TOKEN: printf("tok(%d)\n", arg); break; + case MP_PARSE_NODE_TOKEN: printf("tok(" INT_FMT ")\n", arg); break; default: assert(0); } } else { @@ -185,3 +185,19 @@ const byte *qstr_data(qstr q, uint *len) { *len = Q_GET_LENGTH(qd); return Q_GET_DATA(qd); } + +void qstr_pool_info(uint *n_pool, uint *n_qstr, uint *n_str_data_bytes, uint *n_total_bytes) { + *n_pool = 0; + *n_qstr = 0; + *n_str_data_bytes = 0; + *n_total_bytes = 0; + for (qstr_pool_t *pool = last_pool; pool != NULL && pool != &const_pool; pool = pool->prev) { + *n_pool += 1; + *n_qstr += pool->len; + for (const byte **q = pool->qstrs, **q_top = pool->qstrs + pool->len; q < q_top; q++) { + *n_str_data_bytes += Q_GET_ALLOC(*q); + } + *n_total_bytes += sizeof(qstr_pool_t) + sizeof(qstr) * pool->alloc; + } + *n_total_bytes += *n_str_data_bytes; +} @@ -36,3 +36,5 @@ machine_uint_t qstr_hash(qstr q); const char* qstr_str(qstr q); uint qstr_len(qstr q); const byte* qstr_data(qstr q, uint *len); + +void qstr_pool_info(uint *n_pool, uint *n_qstr, uint *n_str_data_bytes, uint *n_total_bytes); diff --git a/py/runtime.c b/py/runtime.c index 39f297fce0..3c97505bb7 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -217,7 +217,7 @@ void rt_assign_byte_code(int unique_code_id, byte *code, uint len, int n_args, i //printf("byte code: %d bytes\n", len); #ifdef DEBUG_PRINT - DEBUG_printf("assign byte code: id=%d code=%p len=%u n_args=%d\n", unique_code_id, code, len, n_args); + DEBUG_printf("assign byte code: id=%d code=%p len=%u n_args=%d n_locals=%d n_stack=%d\n", unique_code_id, code, len, n_args, n_locals, n_stack); for (int i = 0; i < 128 && i < len; i++) { if (i > 0 && i % 16 == 0) { DEBUG_printf("\n"); diff --git a/py/showbc.c b/py/showbc.c index f914223933..d7ae17c2e3 100644 --- a/py/showbc.c +++ b/py/showbc.c @@ -68,12 +68,10 @@ void mp_byte_code_print(const byte *ip, int len) { printf("LOAD_CONST_INT %s", qstr_str(qstr)); break; - /* case MP_BC_LOAD_CONST_DEC: DECODE_QSTR; - PUSH(rt_load_const_dec(qstr)); + printf("LOAD_CONST_DEC %s", qstr_str(qstr)); break; - */ case MP_BC_LOAD_CONST_ID: DECODE_QSTR; @@ -351,12 +349,12 @@ void mp_byte_code_print(const byte *ip, int len) { case MP_BC_IMPORT_NAME: DECODE_QSTR; - printf("IMPORT NAME %s", qstr_str(qstr)); + printf("IMPORT_NAME %s", qstr_str(qstr)); break; case MP_BC_IMPORT_FROM: DECODE_QSTR; - printf("IMPORT NAME %s", qstr_str(qstr)); + printf("IMPORT_FROM %s", qstr_str(qstr)); break; default: @@ -26,8 +26,6 @@ #define SET_TOP(val) *sp = (val) mp_obj_t mp_execute_byte_code(const byte *code, const mp_obj_t *args, uint n_args, uint n_state) { - n_state += 1; // XXX there is a bug somewhere which doesn't count enough state... (conwaylife and mandel have the bug) - // allocate state for locals and stack mp_obj_t temp_state[10]; mp_obj_t *state = &temp_state[0]; @@ -479,8 +477,7 @@ bool mp_execute_byte_code_2(const byte *code_info, const byte **ip_in_out, mp_ob case MP_BC_MAKE_CLOSURE: DECODE_UINT; - obj1 = POP(); - PUSH(rt_make_closure_from_id(unum, obj1)); + SET_TOP(rt_make_closure_from_id(unum, TOP())); break; case MP_BC_CALL_FUNCTION: diff --git a/unix/main.c b/unix/main.c index 69bac044f7..cd45e3be86 100644 --- a/unix/main.c +++ b/unix/main.c @@ -216,6 +216,18 @@ int usage(void) { return 1; } +mp_obj_t mem_info(void) { + printf("mem: total=%d, current=%d, peak=%d\n", m_get_total_bytes_allocated(), m_get_current_bytes_allocated(), m_get_peak_bytes_allocated()); + return mp_const_none; +} + +mp_obj_t qstr_info(void) { + uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes; + qstr_pool_info(&n_pool, &n_qstr, &n_str_data_bytes, &n_total_bytes); + printf("qstr pool: n_pool=%u, n_qstr=%u, n_str_data_bytes=%u, n_total_bytes=%u\n", n_pool, n_qstr, n_str_data_bytes, n_total_bytes); + return mp_const_none; +} + int main(int argc, char **argv) { qstr_init(); rt_init(); @@ -225,6 +237,8 @@ int main(int argc, char **argv) { rt_store_attr(m_sys, MP_QSTR_argv, py_argv); rt_store_name(qstr_from_str("test"), test_obj_new(42)); + rt_store_name(qstr_from_str("mem_info"), rt_make_function_n(0, mem_info)); + rt_store_name(qstr_from_str("qstr_info"), rt_make_function_n(0, qstr_info)); file_init(); rawsocket_init(); |