summaryrefslogtreecommitdiffstatshomepage
path: root/py
diff options
context:
space:
mode:
authorJohn R. Lenton <jlenton@gmail.com>2014-01-07 18:01:08 +0000
committerJohn R. Lenton <jlenton@gmail.com>2014-01-07 18:01:08 +0000
commit270112f7312f724e46ae34649dfce3ec43b697e0 (patch)
treed7438ae877350aede062d8ac7e62e52ab35cb018 /py
parentc06763a0207dde7f2060f7b1670a0b99298a01f8 (diff)
parentfd04bb3bacf5dbc4d79c04a49520e3e81abb7352 (diff)
downloadmicropython-270112f7312f724e46ae34649dfce3ec43b697e0.tar.gz
micropython-270112f7312f724e46ae34649dfce3ec43b697e0.zip
Merge remote-tracking branch 'upstream/master' into listsort. Lots of conflict fun.
Conflicts: py/obj.h py/objbool.c py/objboundmeth.c py/objcell.c py/objclass.c py/objclosure.c py/objcomplex.c py/objdict.c py/objexcept.c py/objfun.c py/objgenerator.c py/objinstance.c py/objmodule.c py/objrange.c py/objset.c py/objslice.c
Diffstat (limited to 'py')
-rw-r--r--py/asmx64.c8
-rw-r--r--py/gc.c4
-rw-r--r--py/lexerunix.c5
-rw-r--r--py/misc.h6
-rw-r--r--py/mpconfig.h85
-rw-r--r--py/obj.h31
-rw-r--r--py/objbool.c5
-rw-r--r--py/objboundmeth.c5
-rw-r--r--py/objcell.c5
-rw-r--r--py/objclass.c5
-rw-r--r--py/objclosure.c5
-rw-r--r--py/objcomplex.c5
-rw-r--r--py/objdict.c1
-rw-r--r--py/objexcept.c5
-rw-r--r--py/objfloat.c1
-rw-r--r--py/objfun.c21
-rw-r--r--py/objgenerator.c12
-rw-r--r--py/objinstance.c5
-rw-r--r--py/objint.c1
-rw-r--r--py/objlist.c31
-rw-r--r--py/objmodule.c9
-rw-r--r--py/objnone.c1
-rw-r--r--py/objrange.c10
-rw-r--r--py/objset.c5
-rw-r--r--py/objslice.c6
-rw-r--r--py/objstr.c13
-rw-r--r--py/objtuple.c2
-rw-r--r--py/objtype.c1
-rw-r--r--py/py.mk105
-rw-r--r--py/repl.c5
-rw-r--r--py/repl.h2
-rw-r--r--py/runtime.c43
-rw-r--r--py/showbc.c4
33 files changed, 307 insertions, 145 deletions
diff --git a/py/asmx64.c b/py/asmx64.c
index 226d4efee8..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
diff --git a/py/gc.c b/py/gc.c
index 7d4cac6e74..054a3cc315 100644
--- a/py/gc.c
+++ b/py/gc.c
@@ -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/misc.h b/py/misc.h
index 153218ba2f..1bf4d8f291 100644
--- a/py/misc.h
+++ b/py/misc.h
@@ -5,11 +5,7 @@
/** types *******************************************************/
-typedef int bool;
-enum {
- false = 0,
- true = 1
-};
+#include <stdbool.h>
typedef unsigned char byte;
typedef unsigned int uint;
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
diff --git a/py/obj.h b/py/obj.h
index e4e64fca71..332867a194 100644
--- a/py/obj.h
+++ b/py/obj.h
@@ -15,15 +15,14 @@ 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)
-
-typedef struct _mp_obj_base_t mp_obj_base_t;
-typedef struct _mp_obj_type_t mp_obj_type_t;
+// 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 {
- const mp_obj_type_t *type;
+ const struct _mp_obj_type_t *type;
};
+typedef struct _mp_obj_base_t mp_obj_base_t;
// The NULL object is used to indicate the absence of an object
// It *cannot* be used when an mp_obj_t is expected, except where explicitly allowed
@@ -43,13 +42,14 @@ struct _mp_obj_base_t {
#define MP_DECLARE_CONST_FUN_OBJ(obj_name) extern const mp_obj_fun_native_t obj_name
-#define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, 0, 0, fun_name}
-#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, 1, 1, fun_name}
-#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, 2, 2, fun_name}
-#define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, 3, 3, fun_name}
-#define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, n_args_min, (~((machine_uint_t)0)), fun_name}
-#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, false, n_args_min, n_args_max, fun_name}
-#define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, true, 0, (~((machine_uint_t)0)), fun_name}
+#define MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, is_kw, n_args_min, n_args_max, fun_name) const mp_obj_fun_native_t obj_name = {{&fun_native_type}, is_kw, n_args_min, n_args_max, (void *)fun_name}
+#define MP_DEFINE_CONST_FUN_OBJ_0(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 0, 0, (mp_fun_0_t)fun_name)
+#define MP_DEFINE_CONST_FUN_OBJ_1(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 1, 1, (mp_fun_1_t)fun_name)
+#define MP_DEFINE_CONST_FUN_OBJ_2(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 2, 2, (mp_fun_2_t)fun_name)
+#define MP_DEFINE_CONST_FUN_OBJ_3(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, 3, 3, (mp_fun_3_t)fun_name)
+#define MP_DEFINE_CONST_FUN_OBJ_VAR(obj_name, n_args_min, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, n_args_min, (~((machine_uint_t)0)), (mp_fun_var_t)fun_name)
+#define MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(obj_name, n_args_min, n_args_max, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, false, n_args_min, n_args_max, (mp_fun_var_t)fun_name)
+#define MP_DEFINE_CONST_FUN_OBJ_KW(obj_name, fun_name) MP_DEFINE_CONST_FUN_OBJ_VOID_PTR(obj_name, true, 0, (~((machine_uint_t)0)), (mp_fun_var_t)fun_name)
// Need to declare this here so we are not dependent on map.h
struct _mp_map_t;
@@ -90,7 +90,7 @@ struct _mp_obj_type_t {
mp_fun_1_t getiter;
mp_fun_1_t iternext;
- const mp_method_t methods[];
+ const mp_method_t *methods;
/*
What we might need to add here:
@@ -115,6 +115,8 @@ struct _mp_obj_type_t {
*/
};
+typedef struct _mp_obj_type_t mp_obj_type_t;
+
// Constant objects, globally accessible
extern const mp_obj_type_t mp_const_type;
@@ -245,6 +247,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 3fd35a05f3..cfab7db068 100644
--- a/py/objbool.c
+++ b/py/objbool.c
@@ -32,11 +32,10 @@ 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 = {
- .base = { &mp_const_type },
- .name = "bool",
+ { &mp_const_type },
+ "bool",
.print = bool_print,
.make_new = bool_make_new,
- .methods = {{NULL, NULL},},
};
static const mp_obj_bool_t false_obj = {{&bool_type}, false};
diff --git a/py/objboundmeth.c b/py/objboundmeth.c
index 414fdfbf0a..78e5c62494 100644
--- a/py/objboundmeth.c
+++ b/py/objboundmeth.c
@@ -34,10 +34,9 @@ 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 = {
- .base = { &mp_const_type },
- .name = "bound_method",
+ { &mp_const_type },
+ "bound_method",
.call_n = bound_meth_call_n,
- .methods = {{NULL, NULL},},
};
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 bedaf3d87f..264125bf3a 100644
--- a/py/objcell.c
+++ b/py/objcell.c
@@ -24,9 +24,8 @@ void mp_obj_cell_set(mp_obj_t self_in, mp_obj_t obj) {
}
const mp_obj_type_t cell_type = {
- .base = { &mp_const_type },
- .name = "cell",
- .methods = {{NULL, NULL},},
+ { &mp_const_type },
+ "cell",
};
mp_obj_t mp_obj_new_cell(mp_obj_t obj) {
diff --git a/py/objclass.c b/py/objclass.c
index 441c8e77c3..d13d1775a4 100644
--- a/py/objclass.c
+++ b/py/objclass.c
@@ -61,10 +61,9 @@ mp_map_t *mp_obj_class_get_locals(mp_obj_t self_in) {
}
const mp_obj_type_t class_type = {
- .base = { &mp_const_type },
- .name = "class",
+ { &mp_const_type },
+ "class",
.call_n = class_call_n,
- .methods = {{NULL, NULL},},
};
mp_obj_t mp_obj_new_class(mp_map_t *class_locals) {
diff --git a/py/objclosure.c b/py/objclosure.c
index 7f9bcfafdd..b372ee6fe7 100644
--- a/py/objclosure.c
+++ b/py/objclosure.c
@@ -33,10 +33,9 @@ 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 = {
- .base = { &mp_const_type },
- .name = "closure",
+ { &mp_const_type },
+ "closure",
.call_n = closure_call_n,
- .methods = {{NULL, NULL},},
};
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 dc3439143a..1036bae420 100644
--- a/py/objcomplex.c
+++ b/py/objcomplex.c
@@ -85,13 +85,12 @@ static mp_obj_t complex_binary_op(int op, mp_obj_t lhs_in, mp_obj_t rhs_in) {
}
const mp_obj_type_t complex_type = {
- .base = { &mp_const_type },
- .name = "complex",
+ { &mp_const_type },
+ "complex",
.print = complex_print,
.make_new = complex_make_new,
.unary_op = complex_unary_op,
.binary_op = complex_binary_op,
- .methods = { { NULL, NULL }, },
};
mp_obj_t mp_obj_new_complex(mp_float_t real, mp_float_t imag) {
diff --git a/py/objdict.c b/py/objdict.c
index 1278a99e3c..f226abbd2f 100644
--- a/py/objdict.c
+++ b/py/objdict.c
@@ -66,7 +66,6 @@ const mp_obj_type_t dict_type = {
.print = dict_print,
.make_new = dict_make_new,
.binary_op = dict_binary_op,
- .methods = {{NULL, NULL},},
};
mp_obj_t mp_obj_new_dict(int n_args) {
diff --git a/py/objexcept.c b/py/objexcept.c
index dca375dda0..829b147853 100644
--- a/py/objexcept.c
+++ b/py/objexcept.c
@@ -36,10 +36,9 @@ void exception_print(void (*print)(void *env, const char *fmt, ...), void *env,
}
const mp_obj_type_t exception_type = {
- .base = { &mp_const_type },
- .name = "exception",
+ { &mp_const_type },
+ "exception",
.print = exception_print,
- .methods = {{NULL, NULL},},
};
mp_obj_t mp_obj_new_exception(qstr id) {
diff --git a/py/objfloat.c b/py/objfloat.c
index 6c80cf7e5a..4b51971814 100644
--- a/py/objfloat.c
+++ b/py/objfloat.c
@@ -68,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, NULL }, },
};
mp_obj_t mp_obj_new_float(mp_float_t value) {
diff --git a/py/objfun.c b/py/objfun.c
index 3697c64303..395824b046 100644
--- a/py/objfun.c
+++ b/py/objfun.c
@@ -92,13 +92,10 @@ mp_obj_t fun_native_call_n_kw(mp_obj_t self_in, int n_args, int n_kw, const mp_o
}
const mp_obj_type_t fun_native_type = {
- .base = { &mp_const_type },
- .name = "function",
+ { &mp_const_type },
+ "function",
.call_n = fun_native_call_n,
.call_n_kw = fun_native_call_n_kw,
- .methods = {
- {NULL, NULL}, // end-of-list sentinel
- },
};
mp_obj_t rt_make_function_0(mp_fun_0_t fun) {
@@ -189,12 +186,9 @@ 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 = {
- .base = { &mp_const_type },
- .name = "function",
+ { &mp_const_type },
+ "function",
.call_n = fun_bc_call_n,
- .methods = {
- {NULL, NULL}, // end-of-list sentinel
- },
};
mp_obj_t mp_obj_new_fun_bc(int n_args, uint n_state, const byte *code) {
@@ -297,12 +291,9 @@ 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 = {
- .base = { &mp_const_type },
- .name = "function",
+ { &mp_const_type },
+ "function",
.call_n = fun_asm_call_n,
- .methods = {
- {NULL, NULL}, // end-of-list sentinel
- },
};
mp_obj_t mp_obj_new_fun_asm(uint n_args, void *fun) {
diff --git a/py/objgenerator.c b/py/objgenerator.c
index 7b93d29de5..7eeb4ef80b 100644
--- a/py/objgenerator.c
+++ b/py/objgenerator.c
@@ -37,12 +37,9 @@ 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 = {
- .base = { &mp_const_type },
- .name = "generator",
+ { &mp_const_type },
+ "generator",
.call_n = gen_wrap_call_n,
- .unary_op = NULL,
- .binary_op = NULL,
- .methods = {{NULL, NULL},},
};
mp_obj_t mp_obj_new_gen_wrap(uint n_locals, uint n_stack, mp_obj_t fun) {
@@ -88,12 +85,11 @@ mp_obj_t gen_instance_iternext(mp_obj_t self_in) {
}
const mp_obj_type_t gen_instance_type = {
- .base = { &mp_const_type },
- .name = "generator",
+ { &mp_const_type },
+ "generator",
.print = gen_instance_print,
.getiter = gen_instance_getiter,
.iternext = gen_instance_iternext,
- .methods = {{NULL, NULL},},
};
// args are in reverse order in the array
diff --git a/py/objinstance.c b/py/objinstance.c
index c26f365597..209643e2bf 100644
--- a/py/objinstance.c
+++ b/py/objinstance.c
@@ -90,9 +90,8 @@ void mp_obj_instance_store_attr(mp_obj_t self_in, qstr attr, mp_obj_t value) {
}
const mp_obj_type_t instance_type = {
- .base = { &mp_const_type },
- .name = "instance",
- .methods = {{NULL, NULL},},
+ { &mp_const_type },
+ "instance",
};
mp_obj_t mp_obj_new_instance(mp_obj_t class) {
diff --git a/py/objint.c b/py/objint.c
index 6b6c1694db..cf51b7870a 100644
--- a/py/objint.c
+++ b/py/objint.c
@@ -34,7 +34,6 @@ const mp_obj_type_t int_type = {
.base = { &mp_const_type },
.name = "int",
.make_new = int_make_new,
- .methods = { { NULL, NULL }, },
};
mp_obj_t mp_obj_new_int(machine_int_t value) {
diff --git a/py/objlist.c b/py/objlist.c
index a39586a4cd..0446412e83 100644
--- a/py/objlist.c
+++ b/py/objlist.c
@@ -58,6 +58,7 @@ static mp_obj_t list_make_new(mp_obj_t type_in, int n_args, const mp_obj_t *args
default:
nlr_jump(mp_obj_new_exception_msg_1_arg(MP_QSTR_TypeError, "list takes at most 1 argument, %d given", (void*)(machine_int_t)n_args));
}
+ return NULL;
}
static mp_obj_t list_binary_op(int op, mp_obj_t lhs, mp_obj_t rhs) {
@@ -277,27 +278,28 @@ static MP_DEFINE_CONST_FUN_OBJ_2(list_remove_obj, list_remove);
static MP_DEFINE_CONST_FUN_OBJ_1(list_reverse_obj, list_reverse);
static MP_DEFINE_CONST_FUN_OBJ_KW(list_sort_obj, list_sort);
+static const mp_method_t list_type_methods[] = {
+ { "append", &list_append_obj },
+ { "clear", &list_clear_obj },
+ { "copy", &list_copy_obj },
+ { "count", &list_count_obj },
+ { "index", &list_index_obj },
+ { "insert", &list_insert_obj },
+ { "pop", &list_pop_obj },
+ { "remove", &list_remove_obj },
+ { "reverse", &list_reverse_obj },
+ { "sort", &list_sort_obj },
+ { NULL, NULL }, // end-of-list sentinel
+};
+
const mp_obj_type_t list_type = {
.base = { &mp_const_type },
.name = "list",
.print = list_print,
.make_new = list_make_new,
- .unary_op = NULL,
.binary_op = list_binary_op,
.getiter = list_getiter,
- .methods = {
- { "append", &list_append_obj },
- { "clear", &list_clear_obj },
- { "copy", &list_copy_obj },
- { "count", &list_count_obj },
- { "index", &list_index_obj },
- { "insert", &list_insert_obj },
- { "pop", &list_pop_obj },
- { "remove", &list_remove_obj },
- { "reverse", &list_reverse_obj },
- { "sort", &list_sort_obj },
- { NULL, NULL }, // end-of-list sentinel
- },
+ .methods = list_type_methods,
};
static mp_obj_list_t *list_new(uint n) {
@@ -361,7 +363,6 @@ static const mp_obj_type_t list_it_type = {
.base = { &mp_const_type },
.name = "list_iterator",
.iternext = list_it_iternext,
- .methods = { { NULL, 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 df49ec6707..7b92b76f42 100644
--- a/py/objmodule.c
+++ b/py/objmodule.c
@@ -6,6 +6,7 @@
#include "nlr.h"
#include "misc.h"
#include "mpconfig.h"
+#include "mpqstr.h"
#include "obj.h"
#include "runtime.h"
#include "map.h"
@@ -22,17 +23,17 @@ void module_print(void (*print)(void *env, const char *fmt, ...), void *env, mp_
}
const mp_obj_type_t module_type = {
- .base = { &mp_const_type },
- .name = "module",
+ { &mp_const_type },
+ "module",
.print = module_print,
- .methods = {{NULL, NULL},},
};
mp_obj_t mp_obj_new_module(qstr module_name) {
mp_obj_module_t *o = m_new_obj(mp_obj_module_t);
o->base.type = &module_type;
o->name = module_name;
- o->globals = mp_map_new(MP_MAP_QSTR, 0);
+ o->globals = mp_map_new(MP_MAP_QSTR, 1);
+ mp_qstr_map_lookup(o->globals, MP_QSTR___name__, true)->value = mp_obj_new_str(module_name);
return o;
}
diff --git a/py/objnone.c b/py/objnone.c
index c9a860dd05..dc93c25e2d 100644
--- a/py/objnone.c
+++ b/py/objnone.c
@@ -18,7 +18,6 @@ const mp_obj_type_t none_type = {
.base = { &mp_const_type },
.name = "NoneType",
.print = none_print,
- .methods = {{NULL, NULL},},
};
static const mp_obj_none_t none_obj = {{&none_type}};
diff --git a/py/objrange.c b/py/objrange.c
index aba0fa7680..a2a0e67b00 100644
--- a/py/objrange.c
+++ b/py/objrange.c
@@ -23,10 +23,9 @@ mp_obj_t range_getiter(mp_obj_t o_in) {
}
static const mp_obj_type_t range_type = {
- .base = { &mp_const_type} ,
- .name = "range",
+ { &mp_const_type} ,
+ "range",
.getiter = range_getiter,
- .methods = {{NULL, NULL},},
};
// range is a class and instances are immutable sequence objects
@@ -62,10 +61,9 @@ mp_obj_t range_it_iternext(mp_obj_t o_in) {
}
static const mp_obj_type_t range_it_type = {
- .base = { &mp_const_type },
- .name = "range_iterator",
+ { &mp_const_type },
+ "range_iterator",
.iternext = range_it_iternext,
- .methods = {{NULL, NULL},},
};
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 159036a45e..67dab11dfb 100644
--- a/py/objset.c
+++ b/py/objset.c
@@ -55,11 +55,10 @@ 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 = {
- .base = { &mp_const_type },
- .name = "set",
+ { &mp_const_type },
+ "set",
.print = set_print,
.make_new = set_make_new,
- .methods = { { NULL, NULL }, },
};
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 148d875010..3b1bbb186b 100644
--- a/py/objslice.c
+++ b/py/objslice.c
@@ -21,10 +21,9 @@ void ellipsis_print(void (*print)(void *env, const char *fmt, ...), void *env, m
}
const mp_obj_type_t ellipsis_type = {
- .base = { &mp_const_type },
- .name = "ellipsis",
+ { &mp_const_type },
+ "ellipsis",
.print = ellipsis_print,
- .methods = {{NULL, NULL},},
};
static const mp_obj_ellipsis_t ellipsis_obj = {{&ellipsis_type}};
@@ -52,7 +51,6 @@ const mp_obj_type_t slice_type = {
.base = { &mp_const_type },
.name = "slice",
.print = slice_print,
- .methods = { { NULL, 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 c679003eb8..b95aafe432 100644
--- a/py/objstr.c
+++ b/py/objstr.c
@@ -184,17 +184,19 @@ mp_obj_t str_format(int n_args, const mp_obj_t *args) {
static MP_DEFINE_CONST_FUN_OBJ_2(str_join_obj, str_join);
static MP_DEFINE_CONST_FUN_OBJ_VAR(str_format_obj, 1, str_format);
+static const mp_method_t str_type_methods[] = {
+ { "join", &str_join_obj },
+ { "format", &str_format_obj },
+ { NULL, NULL }, // end-of-list sentinel
+};
+
const mp_obj_type_t str_type = {
.base = { &mp_const_type },
.name = "str",
.print = str_print,
.binary_op = str_binary_op,
.getiter = str_getiter,
- .methods = {
- { "join", &str_join_obj },
- { "format", &str_format_obj },
- { NULL, NULL }, // end-of-list sentinel
- },
+ .methods = str_type_methods,
};
mp_obj_t mp_obj_new_str(qstr qstr) {
@@ -235,7 +237,6 @@ static const mp_obj_type_t str_it_type = {
.base = { &mp_const_type },
.name = "str_iterator",
.iternext = str_it_iternext,
- .methods = { { NULL, 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 2205b869c5..e96f76aeaf 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, NULL},},
};
// the zero-length tuple
@@ -166,7 +165,6 @@ static const mp_obj_type_t tuple_it_type = {
.base = { &mp_const_type },
.name = "tuple_iterator",
.iternext = tuple_it_iternext,
- .methods = {{NULL, 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 8146499ecd..012071d6e1 100644
--- a/py/objtype.c
+++ b/py/objtype.c
@@ -27,5 +27,4 @@ const mp_obj_type_t mp_const_type = {
.name = "type",
.print = type_print,
.call_n = type_call_n,
- .methods = {{NULL, 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
diff --git a/py/repl.c b/py/repl.c
index 4241ef0e4c..473313c1ef 100644
--- a/py/repl.c
+++ b/py/repl.c
@@ -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
diff --git a/py/repl.h b/py/repl.h
index 02fe523ed4..23259fa90d 100644
--- a/py/repl.h
+++ b/py/repl.h
@@ -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 d1625f6e3d..83f26952eb 100644
--- a/py/runtime.c
+++ b/py/runtime.c
@@ -61,7 +61,8 @@ typedef struct _mp_code_t {
} mp_code_t;
static int next_unique_code_id;
-static mp_code_t *unique_codes;
+static machine_uint_t unique_codes_alloc = 0;
+static mp_code_t *unique_codes = NULL;
#ifdef WRITE_CODE
FILE *fp_write_code = NULL;
@@ -126,6 +127,7 @@ void rt_init(void) {
mp_qstr_map_lookup(&map_builtins, MP_QSTR_sum, true)->value = rt_make_function_var(1, mp_builtin_sum);
next_unique_code_id = 1; // 0 indicates "no code"
+ unique_codes_alloc = 0;
unique_codes = NULL;
#ifdef WRITE_CODE
@@ -134,6 +136,7 @@ void rt_init(void) {
}
void rt_deinit(void) {
+ m_del(mp_code_t, unique_codes, unique_codes_alloc);
#ifdef WRITE_CODE
if (fp_write_code != NULL) {
fclose(fp_write_code);
@@ -146,18 +149,20 @@ int rt_get_unique_code_id(void) {
}
static void alloc_unique_codes(void) {
- if (unique_codes == NULL) {
- unique_codes = m_new(mp_code_t, next_unique_code_id + 10); // XXX hack until we fix the REPL allocation problem
- for (int i = 0; i < next_unique_code_id; i++) {
+ if (next_unique_code_id > unique_codes_alloc) {
+ // increase size of unique_codes table
+ unique_codes = m_renew(mp_code_t, unique_codes, unique_codes_alloc, next_unique_code_id);
+ for (int i = unique_codes_alloc; i < next_unique_code_id; i++) {
unique_codes[i].kind = MP_CODE_NONE;
}
+ unique_codes_alloc = next_unique_code_id;
}
}
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) {
alloc_unique_codes();
- assert(1 <= unique_code_id && unique_code_id < next_unique_code_id);
+ assert(1 <= unique_code_id && unique_code_id < next_unique_code_id && unique_codes[unique_code_id].kind == MP_CODE_NONE);
unique_codes[unique_code_id].kind = MP_CODE_BYTE;
unique_codes[unique_code_id].n_args = n_args;
unique_codes[unique_code_id].n_locals = n_locals;
@@ -192,7 +197,7 @@ void rt_assign_byte_code(int unique_code_id, byte *code, uint len, int n_args, i
void rt_assign_native_code(int unique_code_id, void *fun, uint len, int n_args) {
alloc_unique_codes();
- assert(1 <= unique_code_id && unique_code_id < next_unique_code_id);
+ assert(1 <= unique_code_id && unique_code_id < next_unique_code_id && unique_codes[unique_code_id].kind == MP_CODE_NONE);
unique_codes[unique_code_id].kind = MP_CODE_NATIVE;
unique_codes[unique_code_id].n_args = n_args;
unique_codes[unique_code_id].n_locals = 0;
@@ -225,7 +230,7 @@ void rt_assign_native_code(int unique_code_id, void *fun, uint len, int n_args)
void rt_assign_inline_asm_code(int unique_code_id, void *fun, uint len, int n_args) {
alloc_unique_codes();
- assert(1 <= unique_code_id && unique_code_id < next_unique_code_id);
+ assert(1 <= unique_code_id && unique_code_id < next_unique_code_id && unique_codes[unique_code_id].kind == MP_CODE_NONE);
unique_codes[unique_code_id].kind = MP_CODE_INLINE_ASM;
unique_codes[unique_code_id].n_args = n_args;
unique_codes[unique_code_id].n_locals = 0;
@@ -785,10 +790,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);
+ }
}
}
}
@@ -809,12 +816,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;
+ }
}
}
}
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