py: Convert [u]int to mp_[u]int_t where appropriate.
Addressing issue #50.
diff --git a/py/bc.c b/py/bc.c
index e9cba38..91ef9c0 100644
--- a/py/bc.c
+++ b/py/bc.c
@@ -75,9 +75,9 @@
}
#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");
@@ -179,7 +179,7 @@
// fill in defaults for positional args
mp_obj_t *d = &code_state->state[n_state - self->n_pos_args];
mp_obj_t *s = &self->extra_args[self->n_def_args - 1];
- for (int i = self->n_def_args; i > 0; i--, d++, s--) {
+ for (mp_uint_t i = self->n_def_args; i > 0; i--, d++, s--) {
if (*d == MP_OBJ_NULL) {
*d = *s;
}
diff --git a/py/bc.h b/py/bc.h
index b92342d..08f4ef0 100644
--- a/py/bc.h
+++ b/py/bc.h
@@ -53,8 +53,8 @@
mp_vm_return_kind_t mp_execute_bytecode(mp_code_state *code_state, volatile mp_obj_t inject_exc);
void mp_setup_code_state(mp_code_state *code_state, mp_obj_t self_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args);
-void mp_bytecode_print(const void *descr, const byte *code, int len);
-void mp_bytecode_print2(const byte *code, int len);
+void mp_bytecode_print(const void *descr, const byte *code, mp_uint_t len);
+void mp_bytecode_print2(const byte *code, mp_uint_t len);
// Helper macros to access pointer with least significant bit holding a flag
#define MP_TAGPTR_PTR(x) ((void*)((mp_uint_t)(x) & ~((mp_uint_t)1)))
diff --git a/py/binary.c b/py/binary.c
index 835ba8a..1201ec3 100644
--- a/py/binary.c
+++ b/py/binary.c
@@ -99,7 +99,7 @@
return size;
}
-mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index) {
+mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index) {
mp_int_t val = 0;
switch (typecode) {
case 'b':
@@ -250,7 +250,7 @@
mp_binary_set_int(MIN(size, sizeof(val)), struct_type == '>', p, in);
}
-void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in) {
+void mp_binary_set_val_array(char typecode, void *p, mp_uint_t index, mp_obj_t val_in) {
switch (typecode) {
#if MICROPY_PY_BUILTINS_FLOAT
case 'f':
@@ -265,7 +265,7 @@
}
}
-void mp_binary_set_val_array_from_int(char typecode, void *p, int index, mp_int_t val) {
+void mp_binary_set_val_array_from_int(char typecode, void *p, mp_uint_t index, mp_int_t val) {
switch (typecode) {
case 'b':
((int8_t*)p)[index] = val;
diff --git a/py/binary.h b/py/binary.h
index 5f577da..2a63ba9 100644
--- a/py/binary.h
+++ b/py/binary.h
@@ -29,9 +29,9 @@
#define BYTEARRAY_TYPECODE 0
int mp_binary_get_size(char struct_type, char val_type, mp_uint_t *palign);
-mp_obj_t mp_binary_get_val_array(char typecode, void *p, int index);
-void mp_binary_set_val_array(char typecode, void *p, int index, mp_obj_t val_in);
-void mp_binary_set_val_array_from_int(char typecode, void *p, int index, mp_int_t val);
+mp_obj_t mp_binary_get_val_array(char typecode, void *p, mp_uint_t index);
+void mp_binary_set_val_array(char typecode, void *p, mp_uint_t index, mp_obj_t val_in);
+void mp_binary_set_val_array_from_int(char typecode, void *p, mp_uint_t index, mp_int_t val);
mp_obj_t mp_binary_get_val(char struct_type, char val_type, byte **ptr);
void mp_binary_set_val(char struct_type, char val_type, mp_obj_t val_in, byte **ptr);
long long mp_binary_get_int(mp_uint_t size, bool is_signed, bool big_endian, byte *p);
diff --git a/py/builtin.c b/py/builtin.c
index 471e294..1af92a4 100644
--- a/py/builtin.c
+++ b/py/builtin.c
@@ -415,9 +415,9 @@
pfenv_t pfenv;
pfenv.data = stream_obj;
- pfenv.print_strn = (void (*)(void *, const char *, unsigned int))mp_stream_write;
+ pfenv.print_strn = (void (*)(void *, const char *, mp_uint_t))mp_stream_write;
#endif
- for (int i = 0; i < n_args; i++) {
+ for (mp_uint_t i = 0; i < n_args; i++) {
if (i > 0) {
#if MICROPY_PY_IO
mp_stream_write(stream_obj, sep_data, sep_len);
diff --git a/py/builtinimport.c b/py/builtinimport.c
index 07b7c19..7270e1f 100644
--- a/py/builtinimport.c
+++ b/py/builtinimport.c
@@ -82,7 +82,7 @@
return stat_dir_or_file(dest);
} else {
// go through each path looking for a directory or file
- for (int i = 0; i < path_num; i++) {
+ for (mp_uint_t i = 0; i < path_num; i++) {
vstr_reset(dest);
mp_uint_t p_len;
const char *p = mp_obj_str_get_data(path_items[i], &p_len);
@@ -167,7 +167,7 @@
mp_obj_t mp_builtin___import__(mp_uint_t n_args, mp_obj_t *args) {
#if DEBUG_PRINT
DEBUG_printf("__import__:\n");
- for (int i = 0; i < n_args; i++) {
+ for (mp_uint_t i = 0; i < n_args; i++) {
DEBUG_printf(" ");
mp_obj_print(args[i], PRINT_REPR);
DEBUG_printf("\n");
@@ -176,7 +176,7 @@
mp_obj_t module_name = args[0];
mp_obj_t fromtuple = mp_const_none;
- int level = 0;
+ mp_int_t level = 0;
if (n_args >= 4) {
fromtuple = args[3];
if (n_args >= 5) {
diff --git a/py/compile.c b/py/compile.c
index a9d19d2..4ccd405 100644
--- a/py/compile.c
+++ b/py/compile.c
@@ -493,7 +493,7 @@
return;
}
- int arg = MP_PARSE_NODE_LEAF_ARG(pn);
+ mp_uint_t arg = MP_PARSE_NODE_LEAF_ARG(pn);
switch (MP_PARSE_NODE_LEAF_KIND(pn)) {
case MP_PARSE_NODE_ID: assert(0);
case MP_PARSE_NODE_INTEGER: vstr_printf(vstr, "%s", qstr_str(arg)); break;
@@ -853,7 +853,7 @@
assert(0);
} else if (MP_PARSE_NODE_IS_LEAF(pn)) {
if (MP_PARSE_NODE_IS_ID(pn)) {
- int arg = MP_PARSE_NODE_LEAF_ARG(pn);
+ qstr arg = MP_PARSE_NODE_LEAF_ARG(pn);
switch (assign_kind) {
case ASSIGN_STORE:
case ASSIGN_AUG_STORE:
diff --git a/py/modstruct.c b/py/modstruct.c
index 8d44006..8122a96 100644
--- a/py/modstruct.c
+++ b/py/modstruct.c
@@ -164,7 +164,7 @@
// TODO: "The arguments must match the values required by the format exactly."
const char *fmt = mp_obj_str_get_str(args[0]);
char fmt_type = get_fmt_type(&fmt);
- int size = MP_OBJ_SMALL_INT_VALUE(struct_calcsize(args[0]));
+ mp_int_t size = MP_OBJ_SMALL_INT_VALUE(struct_calcsize(args[0]));
byte *p;
mp_obj_t res = mp_obj_str_builder_start(&mp_type_bytes, size, &p);
memset(p, 0, size);
diff --git a/py/mpz.c b/py/mpz.c
index 0599656..b5cc7d1 100644
--- a/py/mpz.c
+++ b/py/mpz.c
@@ -58,7 +58,7 @@
returns sign(i - j)
assumes i, j are normalised
*/
-STATIC mp_int_t mpn_cmp(const mpz_dig_t *idig, mp_uint_t ilen, const mpz_dig_t *jdig, mp_uint_t jlen) {
+STATIC int mpn_cmp(const mpz_dig_t *idig, mp_uint_t ilen, const mpz_dig_t *jdig, mp_uint_t jlen) {
if (ilen < jlen) { return -1; }
if (ilen > jlen) { return 1; }
@@ -361,7 +361,7 @@
// handle simple cases
{
- mp_int_t cmp = mpn_cmp(num_dig, *num_len, den_dig, den_len);
+ int cmp = mpn_cmp(num_dig, *num_len, den_dig, den_len);
if (cmp == 0) {
*num_len = 0;
quo_dig[0] = 1;
@@ -744,8 +744,8 @@
return z->len == 0 || (z->dig[0] & 1) == 0;
}
-mp_int_t mpz_cmp(const mpz_t *z1, const mpz_t *z2) {
- mp_int_t cmp = z2->neg - z1->neg;
+int mpz_cmp(const mpz_t *z1, const mpz_t *z2) {
+ int cmp = (int)z2->neg - (int)z1->neg;
if (cmp != 0) {
return cmp;
}
diff --git a/py/mpz.h b/py/mpz.h
index a2d8923..9dbbbc3 100644
--- a/py/mpz.h
+++ b/py/mpz.h
@@ -88,7 +88,7 @@
bool mpz_is_odd(const mpz_t *z);
bool mpz_is_even(const mpz_t *z);
-mp_int_t mpz_cmp(const mpz_t *lhs, const mpz_t *rhs);
+int mpz_cmp(const mpz_t *lhs, const mpz_t *rhs);
mpz_t *mpz_abs(const mpz_t *z);
mpz_t *mpz_neg(const mpz_t *z);
diff --git a/py/obj.h b/py/obj.h
index 0156286..7d714a3 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -212,9 +212,9 @@
// them with ver = sizeof(struct). Cons: overkill for *micro*?
//int ver; // ?
- void *buf; // can be NULL if len == 0
- mp_int_t len; // in bytes; TODO should it be mp_uint_t?
- int typecode; // as per binary.h; TODO what is the correct type to use?
+ void *buf; // can be NULL if len == 0
+ mp_uint_t len; // in bytes
+ int typecode; // as per binary.h
// Rationale: to load arbitrary-sized sprites directly to LCD
// Cons: a bit adhoc usecase
diff --git a/py/objarray.c b/py/objarray.c
index 654c38f..333ab47 100644
--- a/py/objarray.c
+++ b/py/objarray.c
@@ -66,7 +66,7 @@
print(env, "array('%c'", o->typecode);
if (o->len > 0) {
print(env, ", [");
- for (int i = 0; i < o->len; i++) {
+ for (mp_uint_t i = 0; i < o->len; i++) {
if (i > 0) {
print(env, ", ");
}
@@ -92,7 +92,7 @@
mp_obj_t iterable = mp_getiter(initializer);
mp_obj_t item;
- int i = 0;
+ mp_uint_t i = 0;
while ((item = mp_iternext(iterable)) != MP_OBJ_STOP_ITERATION) {
if (len == 0) {
array_append(array, item);
@@ -210,7 +210,7 @@
return res;
#endif
} else {
- uint index = mp_get_index(o->base.type, o->len, index_in, false);
+ mp_uint_t index = mp_get_index(o->base.type, o->len, index_in, false);
if (value == MP_OBJ_SENTINEL) {
// load
return mp_binary_get_val_array(o->typecode, o->items, index);
diff --git a/py/objint.c b/py/objint.c
index 240be32..e0fa7d6 100644
--- a/py/objint.c
+++ b/py/objint.c
@@ -88,8 +88,8 @@
// enough, a dynamic one will be allocated.
char stack_buf[sizeof(mp_int_t) * 4];
char *buf = stack_buf;
- int buf_size = sizeof(stack_buf);
- int fmt_size;
+ mp_uint_t buf_size = sizeof(stack_buf);
+ mp_uint_t fmt_size;
char *str = mp_obj_int_formatted(&buf, &buf_size, &fmt_size, self_in, 10, NULL, '\0', '\0');
print(env, "%s", str);
@@ -135,7 +135,7 @@
//
// The resulting formatted string will be returned from this function and the
// formatted size will be in *fmt_size.
-char *mp_obj_int_formatted(char **buf, int *buf_size, int *fmt_size, mp_const_obj_t self_in,
+char *mp_obj_int_formatted(char **buf, mp_uint_t *buf_size, mp_uint_t *fmt_size, mp_const_obj_t self_in,
int base, const char *prefix, char base_char, char comma) {
fmt_int_t num;
if (MP_OBJ_IS_SMALL_INT(self_in)) {
diff --git a/py/objint.h b/py/objint.h
index 638742a..70d182b 100644
--- a/py/objint.h
+++ b/py/objint.h
@@ -36,9 +36,9 @@
extern const mp_obj_int_t mp_maxsize_obj;
void mp_obj_int_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_obj_t self_in, mp_print_kind_t kind);
-char *mp_obj_int_formatted(char **buf, int *buf_size, int *fmt_size, mp_const_obj_t self_in,
+char *mp_obj_int_formatted(char **buf, mp_uint_t *buf_size, mp_uint_t *fmt_size, mp_const_obj_t self_in,
int base, const char *prefix, char base_char, char comma);
-char *mp_obj_int_formatted_impl(char **buf, int *buf_size, int *fmt_size, mp_const_obj_t self_in,
+char *mp_obj_int_formatted_impl(char **buf, mp_uint_t *buf_size, mp_uint_t *fmt_size, mp_const_obj_t self_in,
int base, const char *prefix, char base_char, char comma);
mp_int_t mp_obj_int_hash(mp_obj_t self_in);
bool mp_obj_int_is_positive(mp_obj_t self_in);
diff --git a/py/objint_mpz.c b/py/objint_mpz.c
index 39806bb..b75cc8c 100644
--- a/py/objint_mpz.c
+++ b/py/objint_mpz.c
@@ -81,12 +81,12 @@
// formatted size will be in *fmt_size.
//
// This particular routine should only be called for the mpz representation of the int.
-char *mp_obj_int_formatted_impl(char **buf, int *buf_size, int *fmt_size, mp_const_obj_t self_in,
+char *mp_obj_int_formatted_impl(char **buf, mp_uint_t *buf_size, mp_uint_t *fmt_size, mp_const_obj_t self_in,
int base, const char *prefix, char base_char, char comma) {
assert(MP_OBJ_IS_TYPE(self_in, &mp_type_int));
const mp_obj_int_t *self = self_in;
- uint needed_size = mpz_as_str_size(&self->mpz, base, prefix, comma);
+ mp_uint_t needed_size = mpz_as_str_size(&self->mpz, base, prefix, comma);
if (needed_size > *buf_size) {
*buf = m_new(char, needed_size);
*buf_size = needed_size;
diff --git a/py/objlist.c b/py/objlist.c
index 789a160..1531b6b 100644
--- a/py/objlist.c
+++ b/py/objlist.c
@@ -164,7 +164,7 @@
assert(0);
}
- int len_adj = slice.start - slice.stop;
+ mp_int_t len_adj = slice.start - slice.stop;
//printf("Len adj: %d\n", len_adj);
assert(len_adj <= 0);
mp_seq_replace_slice_no_grow(self->items, self->len, slice.start, slice.stop, self->items/*NULL*/, 0, mp_obj_t);
@@ -203,7 +203,7 @@
if (!mp_seq_get_fast_slice_indexes(self->len, index, &slice_out)) {
assert(0);
}
- int len_adj = slice->len - (slice_out.stop - slice_out.start);
+ mp_int_t len_adj = slice->len - (slice_out.stop - slice_out.start);
//printf("Len adj: %d\n", len_adj);
if (len_adj > 0) {
if (self->len + len_adj > self->alloc) {
diff --git a/py/objnamedtuple.c b/py/objnamedtuple.c
index 1a8b328..018c8bb 100644
--- a/py/objnamedtuple.c
+++ b/py/objnamedtuple.c
@@ -81,7 +81,7 @@
STATIC int namedtuple_find_field(const char *name, const char *namedef) {
int id = 0;
- int len = strlen(name);
+ size_t len = strlen(name);
while (namedef) {
if (memcmp(name, namedef, len) == 0) {
namedef += len;
@@ -101,9 +101,9 @@
print(env, "%s(", qstr_str(o->tuple.base.type->name));
const char *fields = ((mp_obj_namedtuple_type_t*)o->tuple.base.type)->fields;
- for (int i = 0; i < o->tuple.len; i++) {
+ for (mp_uint_t i = 0; i < o->tuple.len; i++) {
if (i > 0) {
- print(env, ", ");
+ print(env, ", ");
}
const char *next = fields;
diff --git a/py/objrange.c b/py/objrange.c
index dc3662b..702413f 100644
--- a/py/objrange.c
+++ b/py/objrange.c
@@ -63,7 +63,7 @@
.iternext = range_it_iternext,
};
-mp_obj_t mp_obj_new_range_iterator(int cur, int stop, int step) {
+STATIC mp_obj_t mp_obj_new_range_iterator(mp_int_t cur, mp_int_t stop, mp_int_t step) {
mp_obj_range_it_t *o = m_new_obj(mp_obj_range_it_t);
o->base.type = &range_it_type;
o->cur = cur;
diff --git a/py/parse.h b/py/parse.h
index f8c1d57..4564595 100644
--- a/py/parse.h
+++ b/py/parse.h
@@ -68,7 +68,6 @@
#define MP_PARSE_NODE_IS_TOKEN_KIND(pn, k) ((pn) == (MP_PARSE_NODE_TOKEN | ((k) << 5)))
#define MP_PARSE_NODE_LEAF_KIND(pn) ((pn) & 0x1f)
-// TODO should probably have int and uint versions of this macro
#define MP_PARSE_NODE_LEAF_ARG(pn) (((mp_uint_t)(pn)) >> 5)
#define MP_PARSE_NODE_LEAF_SMALL_INT(pn) (((mp_int_t)(pn)) >> 1)
#define MP_PARSE_NODE_STRUCT_KIND(pns) ((pns)->kind_num_nodes & 0xff)
diff --git a/py/pfenv.c b/py/pfenv.c
index 15793ff..965636a 100644
--- a/py/pfenv.c
+++ b/py/pfenv.c
@@ -46,11 +46,11 @@
static const char pad_spaces[] = " ";
static const char pad_zeroes[] = "0000000000000000";
-void pfenv_vstr_add_strn(void *data, const char *str, unsigned int len){
+void pfenv_vstr_add_strn(void *data, const char *str, mp_uint_t len){
vstr_add_strn(data, str, len);
}
-int pfenv_print_strn(const pfenv_t *pfenv, const char *str, unsigned int len, int flags, char fill, int width) {
+int pfenv_print_strn(const pfenv_t *pfenv, const char *str, mp_uint_t len, int flags, char fill, int width) {
int left_pad = 0;
int right_pad = 0;
int pad = width - len;
@@ -234,8 +234,8 @@
// enough, a dynamic one will be allocated.
char stack_buf[sizeof(mp_int_t) * 4];
char *buf = stack_buf;
- int buf_size = sizeof(stack_buf);
- int fmt_size = 0;
+ mp_uint_t buf_size = sizeof(stack_buf);
+ mp_uint_t fmt_size = 0;
char *str;
if (prec > 1) {
diff --git a/py/pfenv.h b/py/pfenv.h
index 781738f..fea2c88 100644
--- a/py/pfenv.h
+++ b/py/pfenv.h
@@ -38,12 +38,12 @@
typedef struct _pfenv_t {
void *data;
- void (*print_strn)(void *, const char *str, unsigned int len);
+ void (*print_strn)(void *, const char *str, mp_uint_t len);
} pfenv_t;
-void pfenv_vstr_add_strn(void *data, const char *str, unsigned int len);
+void pfenv_vstr_add_strn(void *data, const char *str, mp_uint_t len);
-int pfenv_print_strn(const pfenv_t *pfenv, const char *str, unsigned int len, int flags, char fill, int width);
+int pfenv_print_strn(const pfenv_t *pfenv, const char *str, mp_uint_t len, int flags, char fill, int width);
int pfenv_print_int(const pfenv_t *pfenv, mp_uint_t x, int sgn, int base, int base_char, int flags, char fill, int width);
int pfenv_print_mp_int(const pfenv_t *pfenv, mp_obj_t x, int sgn, int base, int base_char, int flags, char fill, int width, int prec);
#if MICROPY_PY_BUILTINS_FLOAT
diff --git a/py/repl.c b/py/repl.c
index 2c3dfbd..66a67e3 100644
--- a/py/repl.c
+++ b/py/repl.c
@@ -31,7 +31,7 @@
#if MICROPY_HELPER_REPL
bool str_startswith_word(const char *str, const char *head) {
- int i;
+ mp_uint_t i;
for (i = 0; str[i] && head[i]; i++) {
if (str[i] != head[i]) {
return false;
diff --git a/py/sequence.c b/py/sequence.c
index 6e92b6b..8cc7519 100644
--- a/py/sequence.c
+++ b/py/sequence.c
@@ -44,7 +44,7 @@
// Implements backend of sequence * integer operation. Assumes elements are
// memory-adjacent in sequence.
void mp_seq_multiply(const void *items, mp_uint_t item_sz, mp_uint_t len, mp_uint_t times, void *dest) {
- for (int i = 0; i < times; i++) {
+ for (mp_uint_t i = 0; i < times; i++) {
uint copy_sz = item_sz * len;
memcpy(dest, items, copy_sz);
dest = (char*)dest + copy_sz;
@@ -185,8 +185,8 @@
}
}
- int len = len1 < len2 ? len1 : len2;
- for (int i = 0; i < len; i++) {
+ mp_uint_t len = len1 < len2 ? len1 : len2;
+ for (mp_uint_t i = 0; i < len; i++) {
// If current elements equal, can't decide anything - go on
if (mp_obj_equal(items1[i], items2[i])) {
continue;
diff --git a/py/showbc.c b/py/showbc.c
index 67c5d7a..c25cec8 100644
--- a/py/showbc.c
+++ b/py/showbc.c
@@ -57,9 +57,7 @@
ip += sizeof(mp_uint_t); \
} while (0)
-void mp_bytecode_print2(const byte *ip, int len);
-
-void mp_bytecode_print(const void *descr, const byte *ip, int len) {
+void mp_bytecode_print(const void *descr, const byte *ip, mp_uint_t len) {
const byte *ip_start = ip;
// get code info size
@@ -115,14 +113,13 @@
mp_bytecode_print2(ip, len - 0);
}
-void mp_bytecode_print2(const byte *ip, int len) {
+void mp_bytecode_print2(const byte *ip, mp_uint_t len) {
const byte *ip_start = ip;
mp_uint_t unum;
qstr qstr;
while (ip - ip_start < len) {
printf("%02u ", (uint)(ip - ip_start));
- int op = *ip++;
- switch (op) {
+ switch (*ip++) {
case MP_BC_LOAD_CONST_FALSE:
printf("LOAD_CONST_FALSE");
break;
@@ -520,7 +517,7 @@
break;
default:
- printf("code %p, byte code 0x%02x not implemented\n", ip, op);
+ printf("code %p, byte code 0x%02x not implemented\n", ip, ip[-1]);
assert(0);
return;
}
diff --git a/py/vstr.c b/py/vstr.c
index 3a021a9..c23bbaa 100644
--- a/py/vstr.c
+++ b/py/vstr.c
@@ -263,24 +263,6 @@
vstr->buf[vstr->len] = 0;
}
-/*
-void vstr_add_le16(vstr_t *vstr, unsigned short v) {
- byte *buf = (byte*)vstr_add_len(vstr, 2);
- if (buf == NULL) {
- return;
- }
- encode_le16(buf, v);
-}
-
-void vstr_add_le32(vstr_t *vstr, unsigned int v) {
- byte *buf = (byte*)vstr_add_len(vstr, 4);
- if (buf == NULL) {
- return;
- }
- encode_le32(buf, v);
-}
-*/
-
char *vstr_ins_blank_bytes(vstr_t *vstr, size_t byte_pos, size_t byte_len) {
if (vstr->had_error) {
return NULL;