From 39dc1454788d1c6fb06c79871c7cd6e12aeb50a0 Mon Sep 17 00:00:00 2001 From: Damien George Date: Fri, 3 Oct 2014 19:52:22 +0100 Subject: [PATCH] py: Change [u]int to mp_[u]int_t in qstr.[ch], and some other places. This should pretty much resolve issue #50. --- py/bc.h | 2 +- py/compile.c | 4 ++-- py/emitnative.c | 2 +- py/objboundmeth.c | 2 +- py/objclosure.c | 4 ++-- py/objfun.c | 27 ++------------------------- py/objstr.c | 20 ++++++++++---------- py/objstr.h | 6 +++--- py/qstr.c | 20 ++++++++++---------- py/qstr.h | 16 +++++++--------- py/runtime.c | 6 +++--- unix/main.c | 4 ++-- 12 files changed, 44 insertions(+), 69 deletions(-) diff --git a/py/bc.h b/py/bc.h index 08f4ef0d4f..6cda15b046 100644 --- a/py/bc.h +++ b/py/bc.h @@ -42,7 +42,7 @@ typedef struct _mp_code_state { mp_obj_t *sp; // bit 0 is saved currently_in_except_block value mp_exc_stack_t *exc_sp; - uint n_state; + mp_uint_t n_state; // Variable-length mp_obj_t state[0]; // Variable-length, never accessed by name, only as (void*)(state + n_state) diff --git a/py/compile.c b/py/compile.c index 4ccd405c79..2cfea0f5db 100644 --- a/py/compile.c +++ b/py/compile.c @@ -1459,7 +1459,7 @@ STATIC void do_import_name(compiler_t *comp, mp_parse_node_t pn, qstr *q_base) { if (i > 0) { *str_dest++ = '.'; } - uint str_src_len; + mp_uint_t str_src_len; const byte *str_src = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &str_src_len); memcpy(str_dest, str_src, str_src_len); str_dest += str_src_len; @@ -2563,7 +2563,7 @@ void compile_atom_string(compiler_t *comp, mp_parse_node_struct_t *pns) { byte *s_dest = qstr_build_start(n_bytes, &q_ptr); for (int i = 0; i < n; i++) { if (MP_PARSE_NODE_IS_LEAF(pns->nodes[i])) { - uint s_len; + mp_uint_t s_len; const byte *s = qstr_data(MP_PARSE_NODE_LEAF_ARG(pns->nodes[i]), &s_len); memcpy(s_dest, s, s_len); s_dest += s_len; diff --git a/py/emitnative.c b/py/emitnative.c index 504f686bd3..5d3e0f222e 100644 --- a/py/emitnative.c +++ b/py/emitnative.c @@ -1075,7 +1075,7 @@ STATIC void emit_native_import_star(emit_t *emit) { STATIC void emit_native_load_const_tok(emit_t *emit, mp_token_kind_t tok) { DEBUG_printf("load_const_tok(tok=%u)\n", tok); emit_native_pre(emit); - int vtype; + vtype_kind_t vtype; mp_uint_t val; if (emit->do_viper_types) { switch (tok) { diff --git a/py/objboundmeth.c b/py/objboundmeth.c index 6040acee74..6098950328 100644 --- a/py/objboundmeth.c +++ b/py/objboundmeth.c @@ -55,7 +55,7 @@ mp_obj_t bound_meth_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, con // need to insert self->self before all other args and then call self->meth - int n_total = n_args + 2 * n_kw; + mp_uint_t n_total = n_args + 2 * n_kw; if (n_total <= 4) { // use stack to allocate temporary args array mp_obj_t args2[5]; diff --git a/py/objclosure.c b/py/objclosure.c index c7d72d4b3e..ec304de4a0 100644 --- a/py/objclosure.c +++ b/py/objclosure.c @@ -46,7 +46,7 @@ mp_obj_t closure_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const // need to concatenate closed-over-vars and args - int n_total = self->n_closed + n_args + 2 * n_kw; + mp_uint_t n_total = self->n_closed + n_args + 2 * n_kw; if (n_total <= 5) { // use stack to allocate temporary args array mp_obj_t args2[5]; @@ -68,7 +68,7 @@ mp_obj_t closure_call(mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const STATIC void closure_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t o_in, mp_print_kind_t kind) { mp_obj_closure_t *o = o_in; print(env, "fun), o, o->n_closed); - for (int i = 0; i < o->n_closed; i++) { + for (mp_uint_t i = 0; i < o->n_closed; i++) { if (o->closed[i] == MP_OBJ_NULL) { print(env, "(nil)"); } else { diff --git a/py/objfun.c b/py/objfun.c index 49c67c7f7a..e64d088bfa 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -116,29 +116,6 @@ const mp_obj_type_t mp_type_fun_builtin = { .binary_op = mp_obj_fun_binary_op, }; -#if 0 // currently unused, and semi-obsolete -mp_obj_t mp_make_function_var(int n_args_min, mp_fun_var_t fun) { - mp_obj_fun_builtin_t *o = m_new_obj(mp_obj_fun_builtin_t); - o->base.type = &mp_type_fun_native; - o->is_kw = false; - o->n_args_min = n_args_min; - o->n_args_max = MP_OBJ_FUN_ARGS_MAX; - o->fun = fun; - return o; -} - -// min and max are inclusive -mp_obj_t mp_make_function_var_between(int n_args_min, int n_args_max, mp_fun_var_t fun) { - mp_obj_fun_builtin_t *o = m_new_obj(mp_obj_fun_builtin_t); - o->base.type = &mp_type_fun_native; - o->is_kw = false; - o->n_args_min = n_args_min; - o->n_args_max = n_args_max; - o->fun = fun; - return o; -} -#endif - /******************************************************************************/ /* byte code functions */ @@ -161,9 +138,9 @@ STATIC void fun_bc_print(void (*print)(void *env, const char *fmt, ...), void *e #endif #if DEBUG_PRINT -STATIC void dump_args(const mp_obj_t *a, int sz) { +STATIC void dump_args(const mp_obj_t *a, mp_uint_t sz) { DEBUG_printf("%p: ", a); - for (int i = 0; i < sz; i++) { + for (mp_uint_t i = 0; i < sz; i++) { DEBUG_printf("%p ", a[i]); } DEBUG_printf("\n"); diff --git a/py/objstr.c b/py/objstr.c index dd44b0784d..c88c91552b 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -213,7 +213,7 @@ STATIC mp_obj_t bytes_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_k return mp_obj_str_builder_end(o); } - int len; + mp_int_t len; byte *data; vstr_t *vstr = NULL; mp_obj_t o = NULL; @@ -293,7 +293,7 @@ mp_obj_t mp_obj_str_binary_op(mp_uint_t op, mp_obj_t lhs_in, mp_obj_t rhs_in) { // add 2 strings or bytes GET_STR_DATA_LEN(rhs_in, rhs_data, rhs_len); - int alloc_len = lhs_len + rhs_len; + mp_uint_t alloc_len = lhs_len + rhs_len; /* code for making qstr byte *q_ptr; @@ -440,8 +440,8 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) { } // count required length - int required_len = 0; - for (int i = 0; i < seq_len; i++) { + mp_uint_t required_len = 0; + for (mp_uint_t i = 0; i < seq_len; i++) { if (mp_obj_get_type(seq_items[i]) != self_type) { nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, "join expects a list of str/bytes objects consistent with self object")); @@ -456,7 +456,7 @@ STATIC mp_obj_t str_join(mp_obj_t self_in, mp_obj_t arg) { // make joined string byte *data; mp_obj_t joined_str = mp_obj_str_builder_start(self_type, required_len, &data); - for (int i = 0; i < seq_len; i++) { + for (mp_uint_t i = 0; i < seq_len; i++) { if (i > 0) { memcpy(data, sep_str, sep_len); data += sep_len; @@ -562,7 +562,7 @@ STATIC mp_obj_t str_rsplit(mp_uint_t n_args, const mp_obj_t *args) { // Preallocate list to the max expected # of elements, as we // will fill it from the end. mp_obj_list_t *res = mp_obj_new_list(splits + 1, NULL); - int idx = splits; + mp_int_t idx = splits; if (sep == mp_const_none) { assert(!"TODO: rsplit(None,n) not implemented"); @@ -598,7 +598,7 @@ STATIC mp_obj_t str_rsplit(mp_uint_t n_args, const mp_obj_t *args) { } if (idx != 0) { // We split less parts than split limit, now go cleanup surplus - int used = org_splits + 1 - idx; + mp_int_t used = org_splits + 1 - idx; memmove(res->items, &res->items[idx], used * sizeof(mp_obj_t)); mp_seq_clear(res->items, used, res->alloc, sizeof(*res->items)); res->len = used; @@ -1554,7 +1554,7 @@ STATIC mp_obj_t str_caseconv(unichar (*op)(unichar), mp_obj_t self_in) { GET_STR_DATA_LEN(self_in, self_data, self_len); byte *data; mp_obj_t s = mp_obj_str_builder_start(mp_obj_get_type(self_in), self_len, &data); - for (int i = 0; i < self_len; i++) { + for (mp_uint_t i = 0; i < self_len; i++) { *data++ = op(*self_data++); } *data = 0; @@ -1577,7 +1577,7 @@ STATIC mp_obj_t str_uni_istype(bool (*f)(unichar), mp_obj_t self_in) { } if (f != unichar_isupper && f != unichar_islower) { - for (int i = 0; i < self_len; i++) { + for (mp_uint_t i = 0; i < self_len; i++) { if (!f(*self_data++)) { return mp_const_false; } @@ -1585,7 +1585,7 @@ STATIC mp_obj_t str_uni_istype(bool (*f)(unichar), mp_obj_t self_in) { } else { bool contains_alpha = false; - for (int i = 0; i < self_len; i++) { // only check alphanumeric characters + for (mp_uint_t i = 0; i < self_len; i++) { // only check alphanumeric characters if (unichar_isalpha(*self_data++)) { contains_alpha = true; if (!f(*(self_data - 1))) { // -1 because we already incremented above diff --git a/py/objstr.h b/py/objstr.h index 8cd4852943..9bc0600852 100644 --- a/py/objstr.h +++ b/py/objstr.h @@ -36,17 +36,17 @@ typedef struct _mp_obj_str_t { // use this macro to extract the string hash #define GET_STR_HASH(str_obj_in, str_hash) \ - uint str_hash; if (MP_OBJ_IS_QSTR(str_obj_in)) \ + mp_uint_t str_hash; if (MP_OBJ_IS_QSTR(str_obj_in)) \ { str_hash = qstr_hash(MP_OBJ_QSTR_VALUE(str_obj_in)); } else { str_hash = ((mp_obj_str_t*)str_obj_in)->hash; } // use this macro to extract the string length #define GET_STR_LEN(str_obj_in, str_len) \ - uint str_len; if (MP_OBJ_IS_QSTR(str_obj_in)) \ + mp_uint_t str_len; if (MP_OBJ_IS_QSTR(str_obj_in)) \ { str_len = qstr_len(MP_OBJ_QSTR_VALUE(str_obj_in)); } else { str_len = ((mp_obj_str_t*)str_obj_in)->len; } // use this macro to extract the string data and length #define GET_STR_DATA_LEN(str_obj_in, str_data, str_len) \ - const byte *str_data; uint str_len; if (MP_OBJ_IS_QSTR(str_obj_in)) \ + const byte *str_data; mp_uint_t str_len; if (MP_OBJ_IS_QSTR(str_obj_in)) \ { str_data = qstr_data(MP_OBJ_QSTR_VALUE(str_obj_in), &str_len); } \ else { str_len = ((mp_obj_str_t*)str_obj_in)->len; str_data = ((mp_obj_str_t*)str_obj_in)->data; } diff --git a/py/qstr.c b/py/qstr.c index 27f13774e5..f841f1dfe2 100644 --- a/py/qstr.c +++ b/py/qstr.c @@ -55,7 +55,7 @@ #define Q_GET_DATA(q) ((q) + 4) // this must match the equivalent function in makeqstrdata.py -mp_uint_t qstr_compute_hash(const byte *data, uint len) { +mp_uint_t qstr_compute_hash(const byte *data, mp_uint_t len) { // djb2 algorithm; see http://www.cse.yorku.ca/~oz/hash.html mp_uint_t hash = 5381; for (const byte *top = data + len; data < top; data++) { @@ -71,9 +71,9 @@ mp_uint_t qstr_compute_hash(const byte *data, uint len) { typedef struct _qstr_pool_t { struct _qstr_pool_t *prev; - uint total_prev_len; - uint alloc; - uint len; + mp_uint_t total_prev_len; + mp_uint_t alloc; + mp_uint_t len; const byte *qstrs[]; } qstr_pool_t; @@ -130,7 +130,7 @@ STATIC qstr qstr_add(const byte *q_ptr) { return last_pool->total_prev_len + last_pool->len - 1; } -qstr qstr_find_strn(const char *str, uint str_len) { +qstr qstr_find_strn(const char *str, mp_uint_t str_len) { // work out hash of str mp_uint_t str_hash = qstr_compute_hash((const byte*)str, str_len); @@ -151,7 +151,7 @@ qstr qstr_from_str(const char *str) { return qstr_from_strn(str, strlen(str)); } -qstr qstr_from_strn(const char *str, uint len) { +qstr qstr_from_strn(const char *str, mp_uint_t len) { qstr q = qstr_find_strn(str, len); if (q == 0) { mp_uint_t hash = qstr_compute_hash((const byte*)str, len); @@ -167,7 +167,7 @@ qstr qstr_from_strn(const char *str, uint len) { return q; } -byte *qstr_build_start(uint len, byte **q_ptr) { +byte *qstr_build_start(mp_uint_t len, byte **q_ptr) { assert(len <= 65535); *q_ptr = m_new(byte, 4 + len + 1); (*q_ptr)[2] = len; @@ -194,7 +194,7 @@ mp_uint_t qstr_hash(qstr q) { return Q_GET_HASH(find_qstr(q)); } -uint qstr_len(qstr q) { +mp_uint_t qstr_len(qstr q) { const byte *qd = find_qstr(q); return Q_GET_LENGTH(qd); } @@ -205,13 +205,13 @@ const char *qstr_str(qstr q) { return (const char*)Q_GET_DATA(qd); } -const byte *qstr_data(qstr q, uint *len) { +const byte *qstr_data(qstr q, mp_uint_t *len) { const byte *qd = find_qstr(q); *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) { +void qstr_pool_info(mp_uint_t *n_pool, mp_uint_t *n_qstr, mp_uint_t *n_str_data_bytes, mp_uint_t *n_total_bytes) { *n_pool = 0; *n_qstr = 0; *n_str_data_bytes = 0; diff --git a/py/qstr.h b/py/qstr.h index 0d9db19f7b..aa4ed41610 100644 --- a/py/qstr.h +++ b/py/qstr.h @@ -45,20 +45,18 @@ typedef mp_uint_t qstr; void qstr_init(void); -mp_uint_t qstr_compute_hash(const byte *data, uint len); -qstr qstr_find_strn(const char *str, uint str_len); // returns MP_QSTR_NULL if not found +mp_uint_t qstr_compute_hash(const byte *data, mp_uint_t len); +qstr qstr_find_strn(const char *str, mp_uint_t str_len); // returns MP_QSTR_NULL if not found qstr qstr_from_str(const char *str); -qstr qstr_from_strn(const char *str, uint len); -//qstr qstr_from_str_static(const char *str); -//qstr qstr_from_strn_copy(const char *str, int len); +qstr qstr_from_strn(const char *str, mp_uint_t len); -byte* qstr_build_start(uint len, byte **q_ptr); +byte* qstr_build_start(mp_uint_t len, byte **q_ptr); qstr qstr_build_end(byte *q_ptr); mp_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); +mp_uint_t qstr_len(qstr q); +const byte* qstr_data(qstr q, mp_uint_t *len); -void qstr_pool_info(uint *n_pool, uint *n_qstr, uint *n_str_data_bytes, uint *n_total_bytes); +void qstr_pool_info(mp_uint_t *n_pool, mp_uint_t *n_qstr, mp_uint_t *n_str_data_bytes, mp_uint_t *n_total_bytes); diff --git a/py/runtime.c b/py/runtime.c index 1600caa065..c7b95bf7dd 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -109,14 +109,14 @@ void mp_deinit(void) { mp_obj_t mp_load_const_int(qstr qstr) { DEBUG_OP_printf("load '%s'\n", qstr_str(qstr)); - uint len; + mp_uint_t len; const byte* data = qstr_data(qstr, &len); return mp_parse_num_integer((const char*)data, len, 0); } mp_obj_t mp_load_const_dec(qstr qstr) { DEBUG_OP_printf("load '%s'\n", qstr_str(qstr)); - uint len; + mp_uint_t len; const byte* data = qstr_data(qstr, &len); return mp_parse_num_decimal((const char*)data, len, true, false); } @@ -128,7 +128,7 @@ mp_obj_t mp_load_const_str(qstr qstr) { mp_obj_t mp_load_const_bytes(qstr qstr) { DEBUG_OP_printf("load b'%s'\n", qstr_str(qstr)); - uint len; + mp_uint_t len; const byte *data = qstr_data(qstr, &len); return mp_obj_new_bytes(data, len); } diff --git a/unix/main.c b/unix/main.c index 90f8cc6737..12ddfbb095 100644 --- a/unix/main.c +++ b/unix/main.c @@ -242,9 +242,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_0(mem_info_obj, mem_info); #endif STATIC mp_obj_t qstr_info(void) { - uint n_pool, n_qstr, n_str_data_bytes, n_total_bytes; + mp_uint_t 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); + printf("qstr pool: n_pool=" UINT_FMT ", n_qstr=" UINT_FMT ", n_str_data_bytes=" UINT_FMT ", n_total_bytes=" UINT_FMT "\n", n_pool, n_qstr, n_str_data_bytes, n_total_bytes); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_0(qstr_info_obj, qstr_info);