diff options
Diffstat (limited to 'py')
-rw-r--r-- | py/asmx64.c | 10 | ||||
-rw-r--r-- | py/emit.h | 2 | ||||
-rw-r--r-- | py/gc.c | 4 | ||||
-rw-r--r-- | py/lexerunix.c | 5 | ||||
-rw-r--r-- | py/mpconfig.h | 85 | ||||
-rw-r--r-- | py/obj.h | 8 | ||||
-rw-r--r-- | py/objbool.c | 10 | ||||
-rw-r--r-- | py/objboundmeth.c | 9 | ||||
-rw-r--r-- | py/objcell.c | 8 | ||||
-rw-r--r-- | py/objclass.c | 9 | ||||
-rw-r--r-- | py/objclosure.c | 9 | ||||
-rw-r--r-- | py/objcomplex.c | 66 | ||||
-rw-r--r-- | py/objdict.c | 2 | ||||
-rw-r--r-- | py/objexcept.c | 9 | ||||
-rw-r--r-- | py/objfloat.c | 44 | ||||
-rw-r--r-- | py/objfun.c | 27 | ||||
-rw-r--r-- | py/objgenerator.c | 20 | ||||
-rw-r--r-- | py/objinstance.c | 8 | ||||
-rw-r--r-- | py/objint.c | 1 | ||||
-rw-r--r-- | py/objlist.c | 3 | ||||
-rw-r--r-- | py/objmodule.c | 9 | ||||
-rw-r--r-- | py/objnone.c | 1 | ||||
-rw-r--r-- | py/objrange.c | 18 | ||||
-rw-r--r-- | py/objset.c | 10 | ||||
-rw-r--r-- | py/objslice.c | 10 | ||||
-rw-r--r-- | py/objstr.c | 2 | ||||
-rw-r--r-- | py/objtuple.c | 2 | ||||
-rw-r--r-- | py/objtype.c | 1 | ||||
-rw-r--r-- | py/py.mk | 105 | ||||
-rw-r--r-- | py/repl.c | 5 | ||||
-rw-r--r-- | py/repl.h | 2 | ||||
-rw-r--r-- | py/runtime.c | 100 | ||||
-rw-r--r-- | py/showbc.c | 4 | ||||
-rw-r--r-- | py/vm.c | 2 |
34 files changed, 341 insertions, 269 deletions
diff --git a/py/asmx64.c b/py/asmx64.c index ed9ca80f5c..d3158170fb 100644 --- a/py/asmx64.c +++ b/py/asmx64.c @@ -1,16 +1,18 @@ #include <stdio.h> #include <assert.h> -#include <sys/types.h> -#include <sys/mman.h> #include <string.h> #include "misc.h" -#include "asmx64.h" #include "mpconfig.h" // wrapper around everything in this file #if MICROPY_EMIT_X64 +#include <sys/types.h> +#include <sys/mman.h> + +#include "asmx64.h" + #if defined(__OpenBSD__) || defined(__MACH__) #define MAP_ANONYMOUS MAP_ANON #endif @@ -155,7 +157,7 @@ void asm_x64_end_pass(asm_x64_t *as) { //as->code_base = m_new(byte, as->code_size); need to allocale executable memory uint actual_alloc; as->code_base = alloc_mem(as->code_size, &actual_alloc, true); - printf("code_size: %u\n", as->code_size); + //printf("code_size: %u\n", as->code_size); } /* @@ -5,7 +5,7 @@ * is not known until the end of pass 1. * As a consequence, we don't know the maximum stack size until the end of pass 2. * This is problematic for some emitters (x64) since they need to know the maximum - * stack size to compile the entry to the function, and this effects code size. + * stack size to compile the entry to the function, and this affects code size. */ typedef enum { @@ -6,6 +6,8 @@ #include "mpconfig.h" #include "gc.h" +#if MICROPY_ENABLE_GC + // a machine word is big enough to hold a pointer /* #define BYTES_PER_WORD (8) @@ -380,3 +382,5 @@ int main(void) { gc_dump_at(); } */ + +#endif // MICROPY_ENABLE_GC diff --git a/py/lexerunix.c b/py/lexerunix.c index 14c28c16d9..5336610bae 100644 --- a/py/lexerunix.c +++ b/py/lexerunix.c @@ -4,8 +4,11 @@ #include <fcntl.h> #include "misc.h" +#include "mpconfig.h" #include "lexer.h" +#if MICROPY_ENABLE_LEXER_UNIX + typedef struct _str_buf_t { bool free; // free src_beg when done const char *src_beg; // beginning of source @@ -78,3 +81,5 @@ mp_lexer_t *mp_import_open_file(qstr mod_name) { vstr_printf(vstr, "%s.py", qstr_str(mod_name)); return mp_lexer_new_from_file(vstr_str(vstr)); // TODO does lexer need to copy the string? can we free it here? } + +#endif // MICROPY_ENABLE_LEXER_UNIX diff --git a/py/mpconfig.h b/py/mpconfig.h index 56495d9156..2017ba366a 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -4,26 +4,67 @@ #include <mpconfigport.h> -#ifndef INT_FMT -// printf format spec to use for machine_int_t and friends -#ifdef __LP64__ -// Archs where machine_int_t == long, long != int -#define UINT_FMT "%lu" -#define INT_FMT "%ld" -#else -// Archs where machine_int_t == int -#define UINT_FMT "%u" -#define INT_FMT "%d" +// Any options not explicitly set in mpconfigport.h will get default +// values below. + +/*****************************************************************************/ +/* Micro Python emitters */ + +// Whether to emit CPython byte codes (for debugging/testing) +// Enabling this overrides all other emitters +#ifndef MICROPY_EMIT_CPYTHON +#define MICROPY_EMIT_CPYTHON (0) #endif -#endif //INT_FMT +// Whether to emit x64 native code +#ifndef MICROPY_EMIT_X64 +#define MICROPY_EMIT_X64 (0) +#endif -// Any options not explicitly set in mpconfigport.h will get default -// values below. +// Whether to emit thumb native code +#ifndef MICROPY_EMIT_THUMB +#define MICROPY_EMIT_THUMB (0) +#endif + +// Whether to enable the thumb inline assembler +#ifndef MICROPY_EMIT_INLINE_THUMB +#define MICROPY_EMIT_INLINE_THUMB (0) +#endif + +/*****************************************************************************/ +/* Internal debugging stuff */ // Whether to collect memory allocation stats #ifndef MICROPY_MEM_STATS -#define MICROPY_MEM_STATS (1) +#define MICROPY_MEM_STATS (0) +#endif + +// Whether to build code to show byte code +#ifndef MICROPY_SHOW_BC +#define MICROPY_SHOW_BC (0) +#endif + +/*****************************************************************************/ +/* Fine control over Python features */ + +// Whether to include the garbage collector +#ifndef MICROPY_ENABLE_GC +#define MICROPY_ENABLE_GC (0) +#endif + +// Whether to include REPL helper function +#ifndef MICROPY_ENABLE_REPL_HELPERS +#define MICROPY_ENABLE_REPL_HELPERS (0) +#endif + +// Whether to include lexer helper function for unix +#ifndef MICROPY_ENABLE_LEXER_UNIX +#define MICROPY_ENABLE_LEXER_UNIX (0) +#endif + +// Whether to support float and complex types +#ifndef MICROPY_ENABLE_FLOAT +#define MICROPY_ENABLE_FLOAT (0) #endif // Whether to support slice object and correspondingly @@ -31,3 +72,19 @@ #ifndef MICROPY_ENABLE_SLICE #define MICROPY_ENABLE_SLICE (1) #endif + +/*****************************************************************************/ +/* Miscellaneous settings */ + +// printf format spec to use for machine_int_t and friends +#ifndef INT_FMT +#ifdef __LP64__ +// Archs where machine_int_t == long, long != int +#define UINT_FMT "%lu" +#define INT_FMT "%ld" +#else +// Archs where machine_int_t == int +#define UINT_FMT "%u" +#define INT_FMT "%d" +#endif +#endif //INT_FMT @@ -15,8 +15,8 @@ typedef machine_int_t mp_small_int_t; typedef machine_float_t mp_float_t; #endif -// Anything that wants to be a Micro Python object must -// have mp_obj_base_t as its first member (except NULL and small ints) +// Anything that wants to be a Micro Python object must have +// mp_obj_base_t as its first member (except NULL and small ints) struct _mp_obj_type_t; struct _mp_obj_base_t { @@ -107,6 +107,7 @@ struct _mp_obj_type_t { __next__ gen-instance */ }; + typedef struct _mp_obj_type_t mp_obj_type_t; // Constant objects, globally accessible @@ -201,10 +202,12 @@ qstr mp_obj_str_get(mp_obj_t self_in); // float extern const mp_obj_type_t float_type; mp_float_t mp_obj_float_get(mp_obj_t self_in); +mp_obj_t mp_obj_float_binary_op(int op, mp_float_t lhs_val, mp_obj_t rhs); // complex extern const mp_obj_type_t complex_type; void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag); +mp_obj_t mp_obj_complex_binary_op(int op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in); #endif // tuple @@ -240,6 +243,7 @@ typedef struct _mp_obj_fun_native_t { // need this so we can define const object // for const function objects, make an empty, const map // such functions won't be able to access the global scope, but that's probably okay } mp_obj_fun_native_t; + extern const mp_obj_type_t fun_native_type; extern const mp_obj_type_t fun_bc_type; void mp_obj_fun_bc_get(mp_obj_t self_in, int *n_args, uint *n_state, const byte **code); diff --git a/py/objbool.c b/py/objbool.c index 54f2b5da1e..cfab7db068 100644 --- a/py/objbool.c +++ b/py/objbool.c @@ -34,14 +34,8 @@ static mp_obj_t bool_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args const mp_obj_type_t bool_type = { { &mp_const_type }, "bool", - bool_print, // print - bool_make_new, // make_new - NULL, // call_n - NULL, // unary_op - NULL, // binary_op - NULL, // getiter - NULL, // iternext - .methods = NULL, + .print = bool_print, + .make_new = bool_make_new, }; static const mp_obj_bool_t false_obj = {{&bool_type}, false}; diff --git a/py/objboundmeth.c b/py/objboundmeth.c index f4f306ae0a..78e5c62494 100644 --- a/py/objboundmeth.c +++ b/py/objboundmeth.c @@ -36,14 +36,7 @@ mp_obj_t bound_meth_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) { const mp_obj_type_t bound_meth_type = { { &mp_const_type }, "bound_method", - NULL, // print - NULL, // make_new - bound_meth_call_n, // call_n - NULL, // unary_op - NULL, // binary_op - NULL, // getiter - NULL, // iternext - .methods = NULL, + .call_n = bound_meth_call_n, }; mp_obj_t mp_obj_new_bound_meth(mp_obj_t self, mp_obj_t meth) { diff --git a/py/objcell.c b/py/objcell.c index 628a289d58..264125bf3a 100644 --- a/py/objcell.c +++ b/py/objcell.c @@ -26,14 +26,6 @@ void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj) { const mp_obj_type_t cell_type = { { &mp_const_type }, "cell", - NULL, // print - NULL, // make_new - NULL, // call_n - NULL, // unary_op - NULL, // binary_op - NULL, // getiter - NULL, // iternext - .methods = NULL, }; mp_obj_t mp_obj_new_cell(mp_obj_t obj) { diff --git a/py/objclass.c b/py/objclass.c index 086645cbb4..d13d1775a4 100644 --- a/py/objclass.c +++ b/py/objclass.c @@ -63,14 +63,7 @@ mp_map_t *mp_obj_class_get_locals(mp_obj_t self_in) { const mp_obj_type_t class_type = { { &mp_const_type }, "class", - NULL, // print - NULL, // make_new - class_call_n, // call_n - NULL, // unary_op - NULL, // binary_op - NULL, // getiter - NULL, // iternext - .methods = NULL, + .call_n = class_call_n, }; mp_obj_t mp_obj_new_class(mp_map_t *class_locals) { diff --git a/py/objclosure.c b/py/objclosure.c index 347f7a79fa..b372ee6fe7 100644 --- a/py/objclosure.c +++ b/py/objclosure.c @@ -35,14 +35,7 @@ mp_obj_t closure_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) { const mp_obj_type_t closure_type = { { &mp_const_type }, "closure", - NULL, // print - NULL, // make_new - closure_call_n, // call_n - NULL, // unary_op - NULL, // binary_op - NULL, // getiter - NULL, // iternext - .methods = NULL, + .call_n = closure_call_n, }; mp_obj_t mp_obj_new_closure(mp_obj_t fun, mp_obj_t closure_tuple) { diff --git a/py/objcomplex.c b/py/objcomplex.c index 9a814980c2..1036bae420 100644 --- a/py/objcomplex.c +++ b/py/objcomplex.c @@ -48,14 +48,14 @@ static mp_obj_t complex_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *a { mp_float_t real, imag; if (MP_OBJ_IS_TYPE(args[1], &complex_type)) { - mp_obj_get_complex(args[1], &real, &imag); + mp_obj_complex_get(args[1], &real, &imag); } else { real = mp_obj_get_float(args[1]); imag = 0; } if (MP_OBJ_IS_TYPE(args[0], &complex_type)) { mp_float_t real2, imag2; - mp_obj_get_complex(args[0], &real2, &imag2); + mp_obj_complex_get(args[0], &real2, &imag2); real -= imag2; imag += real2; } else { @@ -80,9 +80,37 @@ static mp_obj_t complex_unary_op(int op, mp_obj_t o_in) { } static mp_obj_t complex_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { - mp_float_t lhs_real, lhs_imag, rhs_real, rhs_imag; - mp_obj_complex_get(lhs_in, &lhs_real, &lhs_imag); - mp_obj_complex_get(rhs_in, &rhs_real, &rhs_imag); + mp_obj_complex_t *lhs = lhs_in; + return mp_obj_complex_binary_op(op, lhs->real, lhs->imag, rhs_in); +} + +const mp_obj_type_t complex_type = { + { &mp_const_type }, + "complex", + .print = complex_print, + .make_new = complex_make_new, + .unary_op = complex_unary_op, + .binary_op = complex_binary_op, +}; + +mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) { + mp_obj_complex_t *o = m_new_obj(mp_obj_complex_t); + o->base.type = &complex_type; + o->real = real; + o->imag = imag; + return o; +} + +void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag) { + assert(MP_OBJ_IS_TYPE(self_in, &complex_type)); + mp_obj_complex_t *self = self_in; + *real = self->real; + *imag = self->imag; +} + +mp_obj_t mp_obj_complex_binary_op(int op, mp_float_t lhs_real, mp_float_t lhs_imag, mp_obj_t rhs_in) { + mp_float_t rhs_real, rhs_imag; + mp_obj_get_complex(rhs_in, &rhs_real, &rhs_imag); // can be any type, this function will convert to float (if possible) switch (op) { case RT_BINARY_OP_ADD: case RT_BINARY_OP_INPLACE_ADD: @@ -115,32 +143,4 @@ static mp_obj_t complex_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { return mp_obj_new_complex(lhs_real, lhs_imag); } -const mp_obj_type_t complex_type = { - { &mp_const_type }, - "complex", - complex_print, // print - complex_make_new, // make_new - NULL, // call_n - complex_unary_op, // unary_op - complex_binary_op, // binary_op - NULL, // getiter - NULL, // iternext - .methods = NULL, -}; - -mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) { - mp_obj_complex_t *o = m_new_obj(mp_obj_complex_t); - o->base.type = &complex_type; - o->real = real; - o->imag = imag; - return o; -} - -void mp_obj_complex_get(mp_obj_t self_in, mp_float_t *real, mp_float_t *imag) { - assert(MP_OBJ_IS_TYPE(self_in, &complex_type)); - mp_obj_complex_t *self = self_in; - *real = self->real; - *imag = self->imag; -} - #endif diff --git a/py/objdict.c b/py/objdict.c index a00d172cf3..66a442fbaa 100644 --- a/py/objdict.c +++ b/py/objdict.c @@ -66,8 +66,6 @@ const mp_obj_type_t dict_type = { .print = dict_print, .make_new = dict_make_new, .binary_op = dict_binary_op, - .getiter = NULL, - .methods = NULL, }; mp_obj_t mp_obj_new_dict(int n_args) { diff --git a/py/objexcept.c b/py/objexcept.c index 8414fb0f6d..829b147853 100644 --- a/py/objexcept.c +++ b/py/objexcept.c @@ -38,14 +38,7 @@ void exception_print(void (*print)(void *env, const char *fmt, ...), void *env, const mp_obj_type_t exception_type = { { &mp_const_type }, "exception", - exception_print, // print - NULL, // make_new - NULL, // call_n - NULL, // unary_op - NULL, // binary_op - NULL, // getiter - NULL, // iternext - .methods = NULL, + .print = exception_print, }; mp_obj_t mp_obj_new_exception(qstr id) { diff --git a/py/objfloat.c b/py/objfloat.c index 1176a1671e..d21472d5d6 100644 --- a/py/objfloat.c +++ b/py/objfloat.c @@ -53,27 +53,12 @@ static mp_obj_t float_unary_op(int op, mp_obj_t o_in) { } static mp_obj_t float_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) { + mp_obj_float_t *lhs = lhs_in; if (MP_OBJ_IS_TYPE(rhs_in, &complex_type)) { - return complex_type.binary_op(op, lhs_in, rhs_in); + return mp_obj_complex_binary_op(op, lhs->value, 0, rhs_in); + } else { + return mp_obj_float_binary_op(op, lhs->value, rhs_in); } - mp_float_t lhs_val = mp_obj_get_float(lhs_in); - mp_float_t rhs_val = mp_obj_get_float(rhs_in); - switch (op) { - case RT_BINARY_OP_ADD: - case RT_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break; - case RT_BINARY_OP_SUBTRACT: - case RT_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break; - case RT_BINARY_OP_MULTIPLY: - case RT_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break; - /* TODO floor(?) the value - case RT_BINARY_OP_FLOOR_DIVIDE: - case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: val = lhs_val / rhs_val; break; - */ - case RT_BINARY_OP_TRUE_DIVIDE: - case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: lhs_val /= rhs_val; break; - return NULL; // op not supported - } - return mp_obj_new_float(lhs_val); } const mp_obj_type_t float_type = { @@ -83,7 +68,6 @@ const mp_obj_type_t float_type = { .make_new = float_make_new, .unary_op = float_unary_op, .binary_op = float_binary_op, - .methods = NULL, }; mp_obj_t mp_obj_new_float(mp_float_t value) { @@ -99,4 +83,24 @@ mp_float_t mp_obj_float_get(mp_obj_t self_in) { return self->value; } +mp_obj_t mp_obj_float_binary_op(int op, mp_float_t lhs_val, mp_obj_t rhs_in) { + mp_float_t rhs_val = mp_obj_get_float(rhs_in); // can be any type, this function will convert to float (if possible) + switch (op) { + case RT_BINARY_OP_ADD: + case RT_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break; + case RT_BINARY_OP_SUBTRACT: + case RT_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break; + case RT_BINARY_OP_MULTIPLY: + case RT_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break; + /* TODO floor(?) the value + case RT_BINARY_OP_FLOOR_DIVIDE: + case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: val = lhs_val / rhs_val; break; + */ + case RT_BINARY_OP_TRUE_DIVIDE: + case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: lhs_val /= rhs_val; break; + return NULL; // op not supported + } + return mp_obj_new_float(lhs_val); +} + #endif diff --git a/py/objfun.c b/py/objfun.c index 4945462817..51b4329c63 100644 --- a/py/objfun.c +++ b/py/objfun.c @@ -72,14 +72,7 @@ mp_obj_t fun_native_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) { const mp_obj_type_t fun_native_type = { { &mp_const_type }, "function", - NULL, // print - NULL, // make_new - fun_native_call_n, // call_n - NULL, // unary_op - NULL, // binary_op - NULL, // getiter - NULL, // iternext - .methods = NULL, + .call_n = fun_native_call_n, }; mp_obj_t rt_make_function_0(mp_fun_0_t fun) { @@ -172,14 +165,7 @@ mp_obj_t fun_bc_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) { const mp_obj_type_t fun_bc_type = { { &mp_const_type }, "function", - NULL, // print - NULL, // make_new - fun_bc_call_n, // call_n - NULL, // unary_op - NULL, // binary_op - NULL, // getiter - NULL, // iternext - .methods = NULL, + .call_n = fun_bc_call_n, }; mp_obj_t mp_obj_new_fun_bc(int n_args, uint n_state, const byte *code) { @@ -284,14 +270,7 @@ mp_obj_t fun_asm_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) { static const mp_obj_type_t fun_asm_type = { { &mp_const_type }, "function", - NULL, // print - NULL, // make_new - fun_asm_call_n, // call_n - NULL, // unary_op - NULL, // binary_op - NULL, // getiter - NULL, // iternext - .methods = NULL, + .call_n = fun_asm_call_n, }; mp_obj_t mp_obj_new_fun_asm(uint n_args, void *fun) { diff --git a/py/objgenerator.c b/py/objgenerator.c index cc3d90de1a..7eeb4ef80b 100644 --- a/py/objgenerator.c +++ b/py/objgenerator.c @@ -39,14 +39,7 @@ mp_obj_t gen_wrap_call_n(mp_obj_t self_in, int n_args, const mp_obj_t *args) { const mp_obj_type_t gen_wrap_type = { { &mp_const_type }, "generator", - NULL, // print - NULL, // make_new - gen_wrap_call_n, // call_n - NULL, // unary_op - NULL, // binary_op - NULL, // getiter - NULL, // iternext - .methods = NULL, + .call_n = gen_wrap_call_n, }; mp_obj_t mp_obj_new_gen_wrap(uint n_locals, uint n_stack, mp_obj_t fun) { @@ -94,14 +87,9 @@ mp_obj_t gen_instance_iternext(mp_obj_t self_in) { const mp_obj_type_t gen_instance_type = { { &mp_const_type }, "generator", - gen_instance_print, // print - NULL, // make_new - NULL, // call_n - NULL, // unary_op - NULL, // binary_op - gen_instance_getiter, // getiter - gen_instance_iternext, // iternext - .methods = NULL, + .print = gen_instance_print, + .getiter = gen_instance_getiter, + .iternext = gen_instance_iternext, }; // args are in reverse order in the array diff --git a/py/objinstance.c b/py/objinstance.c index a1d71093aa..209643e2bf 100644 --- a/py/objinstance.c +++ b/py/objinstance.c @@ -92,14 +92,6 @@ void mp_obj_instance_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) { const mp_obj_type_t instance_type = { { &mp_const_type }, "instance", - NULL, // print - NULL, // make_new - NULL, // call_n - NULL, // unary_op - NULL, // binary_op - NULL, // getiter - NULL, // iternext - .methods = NULL, }; mp_obj_t mp_obj_new_instance(mp_obj_t class) { diff --git a/py/objint.c b/py/objint.c index 0ba42d10ce..9cd5ebae29 100644 --- a/py/objint.c +++ b/py/objint.c @@ -34,7 +34,6 @@ const mp_obj_type_t int_type = { { &mp_const_type }, "int", .make_new = int_make_new, - .methods = NULL, }; mp_obj_t mp_obj_new_int(machine_int_t value) { diff --git a/py/objlist.c b/py/objlist.c index b642b7015a..df9e1974f9 100644 --- a/py/objlist.c +++ b/py/objlist.c @@ -274,12 +274,12 @@ static const mp_method_t list_type_methods[] = { { "sort", &list_sort_obj }, { NULL, NULL }, // end-of-list sentinel }; + const mp_obj_type_t list_type = { { &mp_const_type }, "list", .print = list_print, .make_new = list_make_new, - .unary_op = NULL, .binary_op = list_binary_op, .getiter = list_getiter, .methods = list_type_methods, @@ -346,7 +346,6 @@ static const mp_obj_type_t list_it_type = { { &mp_const_type }, "list_iterator", .iternext = list_it_iternext, - .methods = NULL, }; mp_obj_t mp_obj_new_list_iterator(mp_obj_list_t *list, int cur) { diff --git a/py/objmodule.c b/py/objmodule.c index 3412573eb4..3e7a0f7fc4 100644 --- a/py/objmodule.c +++ b/py/objmodule.c @@ -24,14 +24,7 @@ void module_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_ const mp_obj_type_t module_type = { { &mp_const_type }, "module", - module_print, // print - NULL, // make_new - NULL, // call_n - NULL, // unary_op - NULL, // binary_op - NULL, // getiter - NULL, // iternext - .methods = NULL, + .print = module_print, }; mp_obj_t mp_obj_new_module(qstr module_name) { diff --git a/py/objnone.c b/py/objnone.c index 173817a4df..c0efe6c0f5 100644 --- a/py/objnone.c +++ b/py/objnone.c @@ -18,7 +18,6 @@ const mp_obj_type_t none_type = { { &mp_const_type }, "NoneType", .print = none_print, - .methods = NULL, }; static const mp_obj_none_t none_obj = {{&none_type}}; diff --git a/py/objrange.c b/py/objrange.c index 5c51656e83..a2a0e67b00 100644 --- a/py/objrange.c +++ b/py/objrange.c @@ -25,14 +25,7 @@ mp_obj_t range_getiter(mp_obj_t o_in) { static const mp_obj_type_t range_type = { { &mp_const_type} , "range", - NULL, // print - NULL, // make_new - NULL, // call_n - NULL, // unary_op - NULL, // binary_op - range_getiter, - NULL, // iternext - .methods = NULL, + .getiter = range_getiter, }; // range is a class and instances are immutable sequence objects @@ -70,14 +63,7 @@ mp_obj_t range_it_iternext(mp_obj_t o_in) { static const mp_obj_type_t range_it_type = { { &mp_const_type }, "range_iterator", - NULL, // print - NULL, // make_new - NULL, // call_n - NULL, // unary_op - NULL, // binary_op - NULL, // getiter - range_it_iternext, - .methods = NULL, + .iternext = range_it_iternext, }; mp_obj_t mp_obj_new_range_iterator(int cur, int stop, int step) { diff --git a/py/objset.c b/py/objset.c index 264e142375..67dab11dfb 100644 --- a/py/objset.c +++ b/py/objset.c @@ -57,14 +57,8 @@ static mp_obj_t set_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args) const mp_obj_type_t set_type = { { &mp_const_type }, "set", - set_print, // print - set_make_new, // make_new - NULL, // call_n - NULL, // unary_op - NULL, // binary_op - NULL, // getiter - NULL, // iternext - .methods = NULL, + .print = set_print, + .make_new = set_make_new, }; mp_obj_t mp_obj_new_set(int n_args, mp_obj_t *items) { diff --git a/py/objslice.c b/py/objslice.c index 360869a494..d95ccef06f 100644 --- a/py/objslice.c +++ b/py/objslice.c @@ -23,14 +23,7 @@ void ellipsis_print(void (*print)(void *env, const char *fmt, ...), void *env, m const mp_obj_type_t ellipsis_type = { { &mp_const_type }, "ellipsis", - ellipsis_print, // print - NULL, // make_new - NULL, // call_n - NULL, // unary_op - NULL, // binary_op - NULL, // getiter - NULL, // iternext - .methods = NULL, + .print = ellipsis_print, }; static const mp_obj_ellipsis_t ellipsis_obj = {{&ellipsis_type}}; @@ -58,7 +51,6 @@ const mp_obj_type_t slice_type = { { &mp_const_type }, "slice", .print = slice_print, - .methods = NULL, }; // TODO: Make sure to handle "empty" values, which are signified by None in CPython diff --git a/py/objstr.c b/py/objstr.c index 66041c587f..cc9f7f85b4 100644 --- a/py/objstr.c +++ b/py/objstr.c @@ -189,6 +189,7 @@ static const mp_method_t str_type_methods[] = { { "format", &str_format_obj }, { NULL, NULL }, // end-of-list sentinel }; + const mp_obj_type_t str_type = { { &mp_const_type }, "str", @@ -236,7 +237,6 @@ static const mp_obj_type_t str_it_type = { { &mp_const_type }, "str_iterator", .iternext = str_it_iternext, - .methods = NULL, }; mp_obj_t mp_obj_new_str_iterator(mp_obj_str_t *str, int cur) { diff --git a/py/objtuple.c b/py/objtuple.c index a59e674b19..593b14e9f5 100644 --- a/py/objtuple.c +++ b/py/objtuple.c @@ -103,7 +103,6 @@ const mp_obj_type_t tuple_type = { .make_new = tuple_make_new, .binary_op = tuple_binary_op, .getiter = tuple_getiter, - .methods = NULL, }; // the zero-length tuple @@ -166,7 +165,6 @@ static const mp_obj_type_t tuple_it_type = { { &mp_const_type }, "tuple_iterator", .iternext = tuple_it_iternext, - .methods = NULL, }; static mp_obj_t mp_obj_new_tuple_iterator(mp_obj_tuple_t *tuple, int cur) { diff --git a/py/objtype.c b/py/objtype.c index 993de040be..8778c180ca 100644 --- a/py/objtype.c +++ b/py/objtype.c @@ -27,5 +27,4 @@ const mp_obj_type_t mp_const_type = { "type", .print = type_print, .call_n = type_call_n, - .methods = NULL, }; diff --git a/py/py.mk b/py/py.mk new file mode 100644 index 0000000000..3ed8a3e3d7 --- /dev/null +++ b/py/py.mk @@ -0,0 +1,105 @@ +# default settings; can be overriden in main Makefile + +ifndef PY_SRC +PY_SRC = ../py +endif + +ifndef BUILD +BUILD = build +endif + +# to create the build directory + +$(BUILD): + mkdir -p $@ + +# where py object files go (they have a name prefix to prevent filename clashes) + +PY_BUILD = $(BUILD)/py. + +# py object files + +PY_O_BASENAME = \ + nlrx86.o \ + nlrx64.o \ + nlrthumb.o \ + malloc.o \ + gc.o \ + qstr.o \ + vstr.o \ + unicode.o \ + lexer.o \ + lexerunix.o \ + parse.o \ + scope.o \ + compile.o \ + emitcommon.o \ + emitpass1.o \ + emitcpy.o \ + emitbc.o \ + asmx64.o \ + emitnx64.o \ + asmthumb.o \ + emitnthumb.o \ + emitinlinethumb.o \ + runtime.o \ + map.o \ + obj.o \ + objbool.o \ + objboundmeth.o \ + objcell.o \ + objclass.o \ + objclosure.o \ + objcomplex.o \ + objdict.o \ + objexcept.o \ + objfloat.o \ + objfun.o \ + objgenerator.o \ + objinstance.o \ + objint.o \ + objlist.o \ + objmodule.o \ + objnone.o \ + objrange.o \ + objset.o \ + objslice.o \ + objstr.o \ + objtuple.o \ + objtype.o \ + builtin.o \ + builtinimport.o \ + vm.o \ + showbc.o \ + repl.o \ + +# prepend the build destination prefix to the py object files + +PY_O = $(addprefix $(PY_BUILD), $(PY_O_BASENAME)) + +$(PY_BUILD)emitnx64.o: $(PY_SRC)/emitnative.c $(PY_SRC)/emit.h mpconfigport.h + $(CC) $(CFLAGS) -DN_X64 -c -o $@ $< + +$(PY_BUILD)emitnthumb.o: $(PY_SRC)/emitnative.c $(PY_SRC)/emit.h mpconfigport.h + $(CC) $(CFLAGS) -DN_THUMB -c -o $@ $< + +$(PY_BUILD)%.o: $(PY_SRC)/%.S + $(CC) $(CFLAGS) -c -o $@ $< + +$(PY_BUILD)%.o: $(PY_SRC)/%.c mpconfigport.h + $(CC) $(CFLAGS) -c -o $@ $< + +# optimising gc for speed; 5ms down to 4ms on pybv2 +$(PY_BUILD)gc.o: $(PY_SRC)/gc.c + $(CC) $(CFLAGS) -O3 -c -o $@ $< + +# optimising vm for speed, adds only a small amount to code size but makes a huge difference to speed (20% faster) +$(PY_BUILD)vm.o: $(PY_SRC)/vm.c + $(CC) $(CFLAGS) -O3 -c -o $@ $< + +# header dependencies + +$(PY_BUILD)parse.o: $(PY_SRC)/grammar.h +$(PY_BUILD)compile.o: $(PY_SRC)/grammar.h +$(PY_BUILD)/emitcpy.o: $(PY_SRC)/emit.h +$(PY_BUILD)emitbc.o: $(PY_SRC)/emit.h @@ -1,6 +1,9 @@ #include "misc.h" +#include "mpconfig.h" #include "repl.h" +#if MICROPY_ENABLE_REPL_HELPERS + bool str_startswith_word(const char *str, const char *head) { int i; for (i = 0; str[i] && head[i]; i++) { @@ -42,3 +45,5 @@ bool mp_repl_is_compound_stmt(const char *line) { } return n_paren > 0 || n_brack > 0 || n_brace > 0; } + +#endif // MICROPY_ENABLE_REPL_HELPERS @@ -1 +1,3 @@ +#if MICROPY_ENABLE_REPL_HELPERS bool mp_repl_is_compound_stmt(const char *line); +#endif diff --git a/py/runtime.c b/py/runtime.c index 7f9ce20279..0457f0df41 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -452,57 +452,63 @@ mp_obj_t rt_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) { // then fail // note that list does not implement + or +=, so that inplace_concat is reached first for += - if (MP_OBJ_IS_SMALL_INT(lhs) && MP_OBJ_IS_SMALL_INT(rhs)) { + if (MP_OBJ_IS_SMALL_INT(lhs)) { mp_small_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(lhs); - mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs); - switch (op) { - case RT_BINARY_OP_OR: - case RT_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break; - case RT_BINARY_OP_XOR: - case RT_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break; - case RT_BINARY_OP_AND: - case RT_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break; - case RT_BINARY_OP_LSHIFT: - case RT_BINARY_OP_INPLACE_LSHIFT: lhs_val <<= rhs_val; break; - case RT_BINARY_OP_RSHIFT: - case RT_BINARY_OP_INPLACE_RSHIFT: lhs_val >>= rhs_val; break; - case RT_BINARY_OP_ADD: - case RT_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break; - case RT_BINARY_OP_SUBTRACT: - case RT_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break; - case RT_BINARY_OP_MULTIPLY: - case RT_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break; - case RT_BINARY_OP_FLOOR_DIVIDE: - case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: lhs_val /= rhs_val; break; -#if MICROPY_ENABLE_FLOAT - case RT_BINARY_OP_TRUE_DIVIDE: - case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val); -#endif - - // TODO implement modulo as specified by Python - case RT_BINARY_OP_MODULO: - case RT_BINARY_OP_INPLACE_MODULO: lhs_val %= rhs_val; break; - - // TODO check for negative power, and overflow - case RT_BINARY_OP_POWER: - case RT_BINARY_OP_INPLACE_POWER: - { - int ans = 1; - while (rhs_val > 0) { - if (rhs_val & 1) { - ans *= lhs_val; + if (MP_OBJ_IS_SMALL_INT(rhs)) { + mp_small_int_t rhs_val = MP_OBJ_SMALL_INT_VALUE(rhs); + switch (op) { + case RT_BINARY_OP_OR: + case RT_BINARY_OP_INPLACE_OR: lhs_val |= rhs_val; break; + case RT_BINARY_OP_XOR: + case RT_BINARY_OP_INPLACE_XOR: lhs_val ^= rhs_val; break; + case RT_BINARY_OP_AND: + case RT_BINARY_OP_INPLACE_AND: lhs_val &= rhs_val; break; + case RT_BINARY_OP_LSHIFT: + case RT_BINARY_OP_INPLACE_LSHIFT: lhs_val <<= rhs_val; break; + case RT_BINARY_OP_RSHIFT: + case RT_BINARY_OP_INPLACE_RSHIFT: lhs_val >>= rhs_val; break; + case RT_BINARY_OP_ADD: + case RT_BINARY_OP_INPLACE_ADD: lhs_val += rhs_val; break; + case RT_BINARY_OP_SUBTRACT: + case RT_BINARY_OP_INPLACE_SUBTRACT: lhs_val -= rhs_val; break; + case RT_BINARY_OP_MULTIPLY: + case RT_BINARY_OP_INPLACE_MULTIPLY: lhs_val *= rhs_val; break; + case RT_BINARY_OP_FLOOR_DIVIDE: + case RT_BINARY_OP_INPLACE_FLOOR_DIVIDE: lhs_val /= rhs_val; break; + #if MICROPY_ENABLE_FLOAT + case RT_BINARY_OP_TRUE_DIVIDE: + case RT_BINARY_OP_INPLACE_TRUE_DIVIDE: return mp_obj_new_float((mp_float_t)lhs_val / (mp_float_t)rhs_val); + #endif + + // TODO implement modulo as specified by Python + case RT_BINARY_OP_MODULO: + case RT_BINARY_OP_INPLACE_MODULO: lhs_val %= rhs_val; break; + + // TODO check for negative power, and overflow + case RT_BINARY_OP_POWER: + case RT_BINARY_OP_INPLACE_POWER: + { + int ans = 1; + while (rhs_val > 0) { + if (rhs_val & 1) { + ans *= lhs_val; + } + lhs_val *= lhs_val; + rhs_val /= 2; } - lhs_val *= lhs_val; - rhs_val /= 2; + lhs_val = ans; + break; } - lhs_val = ans; - break; - } - default: assert(0); - } - if (fit_small_int(lhs_val)) { - return MP_OBJ_NEW_SMALL_INT(lhs_val); + default: assert(0); + } + if (fit_small_int(lhs_val)) { + return MP_OBJ_NEW_SMALL_INT(lhs_val); + } + } else if (MP_OBJ_IS_TYPE(rhs, &float_type)) { + return mp_obj_float_binary_op(op, lhs_val, rhs); + } else if (MP_OBJ_IS_TYPE(rhs, &complex_type)) { + return mp_obj_complex_binary_op(op, lhs_val, 0, rhs); } } else if (MP_OBJ_IS_OBJ(lhs)) { mp_obj_base_t *o = lhs; diff --git a/py/showbc.c b/py/showbc.c index eb7d41b24d..aea0aff67a 100644 --- a/py/showbc.c +++ b/py/showbc.c @@ -8,6 +8,8 @@ #include "mpconfig.h" #include "bc0.h" +#if MICROPY_SHOW_BC + #define DECODE_UINT do { unum = *ip++; if (unum > 127) { unum = ((unum & 0x3f) << 8) | (*ip++); } } while (0) #define DECODE_ULABEL do { unum = (ip[0] | (ip[1] << 8)); ip += 2; } while (0) #define DECODE_SLABEL do { unum = (ip[0] | (ip[1] << 8)) - 0x8000; ip += 2; } while (0) @@ -363,3 +365,5 @@ void mp_show_byte_code(const byte *ip, int len) { printf("\n"); } } + +#endif // MICROPY_SHOW_BC @@ -106,7 +106,7 @@ bool mp_execute_byte_code_2(const byte **ip_in_out, mp_obj_t *fastn, mp_obj_t ** case MP_BC_LOAD_CONST_SMALL_INT: unum = (ip[0] | (ip[1] << 8) | (ip[2] << 16)) - 0x800000; ip += 3; - PUSH((mp_obj_t)(unum << 1 | 1)); + PUSH(MP_OBJ_NEW_SMALL_INT(unum)); break; case MP_BC_LOAD_CONST_DEC: |