diff --git a/py/mpconfig.h b/py/mpconfig.h index 56495d9156..8ce432a61e 100644 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -4,8 +4,65 @@ #include -#ifndef INT_FMT +// 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 + +// Whether to emit x64 native code +#ifndef MICROPY_EMIT_X64 +#define MICROPY_EMIT_X64 (0) +#endif + +// 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 (0) +#endif + +/*****************************************************************************/ +/* Fine control over Python features */ + +// Whether to include REPL helper function +#ifndef MICROPY_ENABLE_REPL_HELPERS +#define MICROPY_ENABLE_REPL_HELPERS (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 +// slice subscript operators +#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" @@ -16,18 +73,3 @@ #define INT_FMT "%d" #endif #endif //INT_FMT - - -// Any options not explicitly set in mpconfigport.h will get default -// values below. - -// Whether to collect memory allocation stats -#ifndef MICROPY_MEM_STATS -#define MICROPY_MEM_STATS (1) -#endif - -// Whether to support slice object and correspondingly -// slice subscript operators -#ifndef MICROPY_ENABLE_SLICE -#define MICROPY_ENABLE_SLICE (1) -#endif diff --git a/py/py.mk b/py/py.mk new file mode 100644 index 0000000000..e673c713c7 --- /dev/null +++ b/py/py.mk @@ -0,0 +1,100 @@ +# 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 \ + 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 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/stm/mpconfigport.h b/stm/mpconfigport.h index 4cea332f39..ee90db3218 100644 --- a/stm/mpconfigport.h +++ b/stm/mpconfigport.h @@ -2,11 +2,10 @@ // options to control how Micro Python is built -#define MICROPY_ENABLE_FLOAT (1) -#define MICROPY_EMIT_CPYTHON (0) -#define MICROPY_EMIT_X64 (0) #define MICROPY_EMIT_THUMB (1) #define MICROPY_EMIT_INLINE_THUMB (1) +#define MICROPY_ENABLE_REPL_HELPERS (1) +#define MICROPY_ENABLE_FLOAT (1) // type definitions for the specific machine diff --git a/unix-cpy/Makefile b/unix-cpy/Makefile index a1eb9b77e4..d2d698713e 100644 --- a/unix-cpy/Makefile +++ b/unix-cpy/Makefile @@ -1,95 +1,37 @@ -PYSRC=../py -BUILD=build +# define main target +PROG = cpy +all: $(PROG) +# include py core make definitions +include ../py/py.mk + +# program for deletion +RM = /bin/rm + +# compiler settings CC = gcc -CFLAGS = -I. -I$(PYSRC) -Wall -Werror -ansi -std=gnu99 -Os #-DNDEBUG +CFLAGS = -I. -I$(PY_SRC) -Wall -Werror -ansi -std=gnu99 -Os #-DNDEBUG LDFLAGS = -lm +# source files SRC_C = \ main.c \ -PY_O = \ - nlrx86.o \ - nlrx64.o \ - malloc.o \ - qstr.o \ - vstr.o \ - unicode.o \ - lexer.o \ - lexerunix.o \ - parse.o \ - scope.o \ - compile.o \ - emitcommon.o \ - emitpass1.o \ - emitcpy.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 \ - -OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(PY_O)) +OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) $(PY_O) LIB = -PROG = cpy $(PROG): $(BUILD) $(OBJ) $(CC) -o $@ $(OBJ) $(LIB) $(LDFLAGS) - -$(BUILD): - mkdir -p $@ + strip $(PROG) + size $(PROG) $(BUILD)/%.o: %.c $(CC) $(CFLAGS) -c -o $@ $< -$(BUILD)/%.o: $(PYSRC)/%.S - $(CC) $(CFLAGS) -c -o $@ $< - -$(BUILD)/%.o: $(PYSRC)/%.c mpconfigport.h - $(CC) $(CFLAGS) -c -o $@ $< - -$(BUILD)/emitnx64.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h - $(CC) $(CFLAGS) -DN_X64 -c -o $@ $< - -$(BUILD)/emitnthumb.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h - $(CC) $(CFLAGS) -DN_THUMB -c -o $@ $< - -# optimising vm for speed, adds only a small amount to code size but makes a huge difference to speed (20% faster) -$(BUILD)/vm.o: $(PYSRC)/vm.c - $(CC) $(CFLAGS) -O3 -c -o $@ $< - $(BUILD)/main.o: mpconfigport.h -$(BUILD)/parse.o: $(PYSRC)/grammar.h -$(BUILD)/compile.o: $(PYSRC)/grammar.h -$(BUILD)/emitcpy.o: $(PYSRC)/emit.h -$(BUILD)/emitbc.o: $(PYSRC)/emit.h clean: - /bin/rm -rf $(BUILD) + $(RM) -f $(PROG) + $(RM) -rf $(BUILD) -.PHONY: clean +.PHONY: all clean diff --git a/unix-cpy/main.c b/unix-cpy/main.c index eba97f527b..ea85e32757 100644 --- a/unix-cpy/main.c +++ b/unix-cpy/main.c @@ -11,7 +11,6 @@ #include "obj.h" #include "compile.h" #include "runtime0.h" -#include "runtime.h" void do_file(const char *file) { mp_lexer_t *lex = mp_lexer_new_from_file(file); diff --git a/unix-cpy/mpconfigport.h b/unix-cpy/mpconfigport.h index 983b166a55..9e3e32bb44 100644 --- a/unix-cpy/mpconfigport.h +++ b/unix-cpy/mpconfigport.h @@ -2,9 +2,6 @@ #define MICROPY_ENABLE_FLOAT (1) #define MICROPY_EMIT_CPYTHON (1) -#define MICROPY_EMIT_X64 (0) -#define MICROPY_EMIT_THUMB (0) -#define MICROPY_EMIT_INLINE_THUMB (0) // type definitions for the specific machine diff --git a/unix/Makefile b/unix/Makefile index 984f1c3bf3..a3faea290a 100644 --- a/unix/Makefile +++ b/unix/Makefile @@ -1,106 +1,39 @@ -PYSRC=../py -BUILD=build +# define main target +PROG = py +all: $(PROG) +# include py core make definitions +include ../py/py.mk + +# program for deletion +RM = /bin/rm + +# compiler settings CC = gcc -CFLAGS = -I. -I$(PYSRC) -Wall -Werror -ansi -std=gnu99 -Os #-DNDEBUG +CFLAGS = -I. -I$(PY_SRC) -Wall -Werror -ansi -std=gnu99 -Os #-DNDEBUG LDFLAGS = -lm +# source files SRC_C = \ main.c \ -PY_O = \ - nlrx86.o \ - nlrx64.o \ - nlrthumb.o \ - malloc.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 \ - -OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(PY_O)) +OBJ = $(addprefix $(BUILD)/, $(SRC_C:.c=.o)) $(PY_O) LIB = -lreadline # the following is needed for BSD #LIB += -ltermcap -PROG = py $(PROG): $(BUILD) $(OBJ) $(CC) -o $@ $(OBJ) $(LIB) $(LDFLAGS) strip $(PROG) size $(PROG) -$(BUILD): - mkdir -p $@ - $(BUILD)/%.o: %.c $(CC) $(CFLAGS) -c -o $@ $< -$(BUILD)/%.o: $(PYSRC)/%.S - $(CC) $(CFLAGS) -c -o $@ $< - -$(BUILD)/%.o: $(PYSRC)/%.c mpconfigport.h - $(CC) $(CFLAGS) -c -o $@ $< - -$(BUILD)/emitnx64.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h mpconfigport.h - $(CC) $(CFLAGS) -DN_X64 -c -o $@ $< - -$(BUILD)/emitnthumb.o: $(PYSRC)/emitnative.c $(PYSRC)/emit.h mpconfigport.h - $(CC) $(CFLAGS) -DN_THUMB -c -o $@ $< - -# optimising vm for speed, adds only a small amount to code size but makes a huge difference to speed (20% faster) -$(BUILD)/vm.o: $(PYSRC)/vm.c - $(CC) $(CFLAGS) -O3 -c -o $@ $< - $(BUILD)/main.o: mpconfigport.h -$(BUILD)/parse.o: $(PYSRC)/grammar.h -$(BUILD)/compile.o: $(PYSRC)/grammar.h -$(BUILD)/emitcpy.o: $(PYSRC)/emit.h -$(BUILD)/emitbc.o: $(PYSRC)/emit.h clean: - /bin/rm -rf $(BUILD) + $(RM) -f $(PROG) + $(RM) -rf $(BUILD) -.PHONY: clean +.PHONY: all clean diff --git a/unix/main.c b/unix/main.c index a06dc36791..f7277b960c 100644 --- a/unix/main.c +++ b/unix/main.c @@ -20,6 +20,52 @@ #include #endif +static void execute_from_lexer(mp_lexer_t *lex, mp_parse_input_kind_t input_kind, bool is_repl) { + if (lex == NULL) { + return; + } + + if (0) { + // just tokenise + while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) { + mp_token_show(mp_lexer_cur(lex)); + mp_lexer_to_next(lex); + } + mp_lexer_free(lex); + return; + } + + mp_parse_node_t pn = mp_parse(lex, input_kind); + mp_lexer_free(lex); + + if (pn == MP_PARSE_NODE_NULL) { + // parse error + return; + } + + //printf("----------------\n"); + //parse_node_show(pn, 0); + //printf("----------------\n"); + + mp_obj_t module_fun = mp_compile(pn, is_repl); + + if (module_fun == mp_const_none) { + // compile error + return; + } + + // execute it + nlr_buf_t nlr; + if (nlr_push(&nlr) == 0) { + rt_call_function_0(module_fun); + nlr_pop(); + } else { + // uncaught exception + mp_obj_print((mp_obj_t)nlr.ret_val); + printf("\n"); + } +} + static char *str_join(const char *s1, int sep_char, const char *s2) { int l1 = strlen(s1); int l2 = strlen(s2); @@ -80,28 +126,11 @@ static void do_repl(void) { } mp_lexer_t *lex = mp_lexer_new_from_str_len("", line, strlen(line), false); - mp_parse_node_t pn = mp_parse(lex, MP_PARSE_SINGLE_INPUT); - mp_lexer_free(lex); - - if (pn != MP_PARSE_NODE_NULL) { - //mp_parse_node_show(pn, 0); - mp_obj_t module_fun = mp_compile(pn, true); - if (module_fun != mp_const_none) { - nlr_buf_t nlr; - if (nlr_push(&nlr) == 0) { - rt_call_function_0(module_fun); - nlr_pop(); - } else { - // uncaught exception - mp_obj_print((mp_obj_t)nlr.ret_val); - printf("\n"); - } - } - } + execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, true); } } -void do_file(const char *file) { +static void do_file(const char *file) { // hack: set dir for import based on where this file is { const char * s = strrchr(file, '/'); @@ -115,53 +144,12 @@ void do_file(const char *file) { } mp_lexer_t *lex = mp_lexer_new_from_file(file); - //const char *pysrc = "def f():\n x=x+1\n print(42)\n"; - //mp_lexer_t *lex = mp_lexer_from_str_len("<>", pysrc, strlen(pysrc), false); - if (lex == NULL) { - return; - } + execute_from_lexer(lex, MP_PARSE_FILE_INPUT, false); +} - if (0) { - // just tokenise - while (!mp_lexer_is_kind(lex, MP_TOKEN_END)) { - mp_token_show(mp_lexer_cur(lex)); - mp_lexer_to_next(lex); - } - mp_lexer_free(lex); - - } else { - // compile - - mp_parse_node_t pn = mp_parse(lex, MP_PARSE_FILE_INPUT); - mp_lexer_free(lex); - - if (pn != MP_PARSE_NODE_NULL) { - //printf("----------------\n"); - //parse_node_show(pn, 0); - //printf("----------------\n"); - mp_obj_t module_fun = mp_compile(pn, false); - //printf("----------------\n"); - -#if MICROPY_EMIT_CPYTHON - if (!comp_ok) { - printf("compile error\n"); - } -#else - if (1 && module_fun != mp_const_none) { - // execute it - nlr_buf_t nlr; - if (nlr_push(&nlr) == 0) { - rt_call_function_0(module_fun); - nlr_pop(); - } else { - // uncaught exception - mp_obj_print((mp_obj_t)nlr.ret_val); - printf("\n"); - } - } -#endif - } - } +static void do_str(const char *str) { + mp_lexer_t *lex = mp_lexer_new_from_str_len("", str, strlen(str), false); + execute_from_lexer(lex, MP_PARSE_SINGLE_INPUT, false); } typedef struct _test_obj_t { @@ -192,12 +180,6 @@ static const mp_obj_type_t test_type = { { &mp_const_type }, "Test", .print = test_print, - .make_new = NULL, - .call_n = NULL, - .unary_op = NULL, - .binary_op = NULL, - .getiter = NULL, - .iternext = NULL, .methods = { { "get", &test_get_obj }, { "set", &test_set_obj }, @@ -212,6 +194,11 @@ mp_obj_t test_obj_new(int value) { return o; } +int usage(void) { + printf("usage: py [-c ] []\n"); + return 1; +} + int main(int argc, char **argv) { qstr_init(); rt_init(); @@ -227,12 +214,24 @@ int main(int argc, char **argv) { if (argc == 1) { do_repl(); - } else if (argc == 2) { - do_file(argv[1]); } else { - printf("usage: py []\n"); - return 1; + for (int a = 1; a < argc; a++) { + if (argv[a][0] == '-') { + if (strcmp(argv[a], "-c") == 0) { + if (a + 1 >= argc) { + return usage(); + } + do_str(argv[a + 1]); + a += 1; + } else { + return usage(); + } + } else { + do_file(argv[a]); + } + } } + rt_deinit(); //printf("total bytes = %d\n", m_get_total_bytes_allocated()); diff --git a/unix/mpconfigport.h b/unix/mpconfigport.h index 7a4622b8b6..b7901069ac 100644 --- a/unix/mpconfigport.h +++ b/unix/mpconfigport.h @@ -5,11 +5,12 @@ #define MICROPY_USE_READLINE (1) #endif -#define MICROPY_ENABLE_FLOAT (1) -#define MICROPY_EMIT_CPYTHON (0) #define MICROPY_EMIT_X64 (1) #define MICROPY_EMIT_THUMB (0) #define MICROPY_EMIT_INLINE_THUMB (0) +#define MICROPY_MEM_STATS (1) +#define MICROPY_ENABLE_REPL_HELPERS (1) +#define MICROPY_ENABLE_FLOAT (1) // type definitions for the specific machine