diff options
author | Damien <damien.p.george@gmail.com> | 2013-10-12 14:30:21 +0100 |
---|---|---|
committer | Damien <damien.p.george@gmail.com> | 2013-10-12 14:30:21 +0100 |
commit | c025ebb2dc38891fff1d451d13382d9717fb1d59 (patch) | |
tree | 803f297d238417c90893a121bf6926519f469f44 /py | |
parent | a56f29262661ba6affc50d2e1f6e27ecc7aac116 (diff) | |
download | micropython-c025ebb2dc38891fff1d451d13382d9717fb1d59.tar.gz micropython-c025ebb2dc38891fff1d451d13382d9717fb1d59.zip |
Separate out mpy core and unix version.
Diffstat (limited to 'py')
-rw-r--r-- | py/.gitignore | 4 | ||||
-rw-r--r-- | py/Makefile | 52 | ||||
-rw-r--r-- | py/asmthumb.c | 2 | ||||
-rw-r--r-- | py/compile.c | 43 | ||||
-rw-r--r-- | py/emitbc.c | 2 | ||||
-rw-r--r-- | py/emitcommon.c | 2 | ||||
-rw-r--r-- | py/emitcpy.c | 6 | ||||
-rw-r--r-- | py/emitinlinethumb.c | 6 | ||||
-rw-r--r-- | py/emitnative.c | 11 | ||||
-rw-r--r-- | py/emitpass1.c | 2 | ||||
-rw-r--r-- | py/emitthumb.c | 6 | ||||
-rw-r--r-- | py/machine.h | 4 | ||||
-rw-r--r-- | py/main.c | 60 | ||||
-rw-r--r-- | py/parse.c | 2 | ||||
-rw-r--r-- | py/runtime.c | 21 | ||||
-rw-r--r-- | py/scope.c | 2 | ||||
-rw-r--r-- | py/vm.c | 2 |
17 files changed, 64 insertions, 163 deletions
diff --git a/py/.gitignore b/py/.gitignore deleted file mode 100644 index e7f3545dc6..0000000000 --- a/py/.gitignore +++ /dev/null @@ -1,4 +0,0 @@ -*.o -py -*.py -.*.swp diff --git a/py/Makefile b/py/Makefile deleted file mode 100644 index 0f4483d6a2..0000000000 --- a/py/Makefile +++ /dev/null @@ -1,52 +0,0 @@ -CC = gcc -CFLAGS = -Wall -ansi -std=gnu99 -Os -DEMIT_ENABLE_CPY -DEMIT_ENABLE_THUMB #-DNDEBUG -LDFLAGS = - -SRC = \ - malloc.c \ - misc.c \ - qstr.c \ - lexer.c \ - lexerfile.c \ - parse.c \ - scope.c \ - compile.c \ - emitcommon.c \ - emitpass1.c \ - emitcpy.c \ - emitbc.c \ - asmx64.c \ - asmthumb.c \ - emitinlinethumb.c \ - runtime.c \ - vm.c \ - main.c \ - -SRC_ASM = \ - -OBJ = $(SRC:.c=.o) $(SRC_ASM:.s=.o) emitnx64.o emitnthumb.o -LIB = -PROG = py - -$(PROG): $(OBJ) - $(CC) -o $@ $(OBJ) $(LIB) $(LDFLAGS) - -runtime.o: runtime.c - $(CC) $(CFLAGS) -O3 -c -o $@ $< - -vm.o: vm.c - $(CC) $(CFLAGS) -O3 -c -o $@ $< - -parse.o: grammar.h -compile.o: grammar.h -emitcpy.o: emit.h -emitbc.o: emit.h - -emitnx64.o: emitnative.c emit.h - $(CC) $(CFLAGS) -DN_X64 -c -o $@ $< - -emitnthumb.o: emitnative.c emit.h - $(CC) $(CFLAGS) -DN_THUMB -c -o $@ $< - -clean: - /bin/rm $(OBJ) diff --git a/py/asmthumb.c b/py/asmthumb.c index ba61c31f65..a6b95764ba 100644 --- a/py/asmthumb.c +++ b/py/asmthumb.c @@ -4,7 +4,7 @@ #include <string.h> #include "misc.h" -#include "machine.h" +#include "mpyconfig.h" #include "asmthumb.h" #define UNSIGNED_FIT8(x) (((x) & 0xffffff00) == 0) diff --git a/py/compile.c b/py/compile.c index 3d5a29a192..810446189d 100644 --- a/py/compile.c +++ b/py/compile.c @@ -6,8 +6,8 @@ #include <assert.h> #include "misc.h" +#include "mpyconfig.h" #include "lexer.h" -#include "machine.h" #include "parse.h" #include "scope.h" #include "compile.h" @@ -768,8 +768,10 @@ static bool compile_built_in_decorator(compiler_t *comp, int name_len, py_parse_ *emit_options = EMIT_OPT_NATIVE_PYTHON; } else if (attr == comp->qstr_viper) { *emit_options = EMIT_OPT_VIPER; +#if defined(MICROPY_EMIT_ENABLE_INLINE_THUMB) } else if (attr == comp->qstr_asm_thumb) { *emit_options = EMIT_OPT_ASM_THUMB; +#endif } else { printf("SyntaxError: invalid micropython decorator\n"); } @@ -2673,8 +2675,11 @@ void py_compile(py_parse_node_t pn) { comp->emit_inline_asm_method_table = NULL; uint max_num_labels = 0; for (scope_t *s = comp->scope_head; s != NULL; s = s->next) { - if (s->emit_options == EMIT_OPT_ASM_THUMB) { + if (false) { +#ifdef MICROPY_EMIT_ENABLE_INLINE_THUMB + } else if (s->emit_options == EMIT_OPT_ASM_THUMB) { compile_scope_inline_asm(comp, s, PASS_1); +#endif } else { compile_scope(comp, s, PASS_1); } @@ -2694,11 +2699,20 @@ void py_compile(py_parse_node_t pn) { emit_pass1_free(comp->emit); // compile pass 2 and 3 +#if !defined(MICROPY_EMIT_ENABLE_CPYTHON) emit_t *emit_bc = NULL; emit_t *emit_native = NULL; +#endif +#if defined(MICROPY_EMIT_ENABLE_INLINE_THUMB) emit_inline_asm_t *emit_inline_thumb = NULL; +#endif for (scope_t *s = comp->scope_head; s != NULL; s = s->next) { - if (s->emit_options == EMIT_OPT_ASM_THUMB) { + if (false) { + // dummy + +#if defined(MICROPY_EMIT_ENABLE_INLINE_THUMB) + } else if (s->emit_options == EMIT_OPT_ASM_THUMB) { + // inline assembly for thumb if (emit_inline_thumb == NULL) { emit_inline_thumb = emit_inline_thumb_new(max_num_labels); } @@ -2708,15 +2722,31 @@ void py_compile(py_parse_node_t pn) { comp->emit_inline_asm_method_table = &emit_inline_thumb_method_table; compile_scope_inline_asm(comp, s, PASS_2); compile_scope_inline_asm(comp, s, PASS_3); +#endif + } else { + + // choose the emit type + +#if defined(MICROPY_EMIT_ENABLE_CPYTHON) + comp->emit = emit_cpython_new(max_num_labels); + comp->emit_method_table = &emit_cpython_method_table; +#else switch (s->emit_options) { case EMIT_OPT_NATIVE_PYTHON: case EMIT_OPT_VIPER: +#if defined(MICROPY_EMIT_ENABLE_X64) if (emit_native == NULL) { emit_native = emit_native_x64_new(max_num_labels); } - comp->emit = emit_native; comp->emit_method_table = &emit_native_x64_method_table; +#elif defined(MICROPY_EMIT_ENABLE_THUMB) + if (emit_native == NULL) { + emit_native = emit_native_thumb_new(max_num_labels); + } + comp->emit_method_table = &emit_native_thumb_method_table; +#endif + comp->emit = emit_native; comp->emit_method_table->set_native_types(comp->emit, s->emit_options == EMIT_OPT_VIPER); break; @@ -2728,8 +2758,9 @@ void py_compile(py_parse_node_t pn) { comp->emit_method_table = &emit_bc_method_table; break; } - //comp->emit = emit_cpython_new(max_num_labels); - //comp->emit_method_table = &emit_cpython_method_table; +#endif + + // compile pass 2 and pass 3 compile_scope(comp, s, PASS_2); compile_scope(comp, s, PASS_3); } diff --git a/py/emitbc.c b/py/emitbc.c index f242828e62..8f28910c08 100644 --- a/py/emitbc.c +++ b/py/emitbc.c @@ -6,8 +6,8 @@ #include <assert.h> #include "misc.h" +#include "mpyconfig.h" #include "lexer.h" -#include "machine.h" #include "parse.h" #include "compile.h" #include "scope.h" diff --git a/py/emitcommon.c b/py/emitcommon.c index 6d4c071419..07eb1812fd 100644 --- a/py/emitcommon.c +++ b/py/emitcommon.c @@ -5,8 +5,8 @@ #include <assert.h> #include "misc.h" +#include "mpyconfig.h" #include "lexer.h" -#include "machine.h" #include "parse.h" #include "scope.h" #include "runtime.h" diff --git a/py/emitcpy.c b/py/emitcpy.c index 7c9615c09a..596e04eb82 100644 --- a/py/emitcpy.c +++ b/py/emitcpy.c @@ -6,15 +6,15 @@ #include <assert.h> #include "misc.h" +#include "mpyconfig.h" #include "lexer.h" -#include "machine.h" #include "parse.h" #include "compile.h" #include "scope.h" #include "runtime.h" #include "emit.h" -#ifdef EMIT_ENABLE_CPY +#ifdef MICROPY_EMIT_ENABLE_CPYTHON struct _emit_t { int pass; @@ -925,4 +925,4 @@ const emit_method_table_t emit_cpython_method_table = { emit_cpy_yield_from, }; -#endif // EMIT_ENABLE_CPY +#endif // MICROPY_EMIT_ENABLE_CPYTHON diff --git a/py/emitinlinethumb.c b/py/emitinlinethumb.c index ae226b74e7..74bc6d129f 100644 --- a/py/emitinlinethumb.c +++ b/py/emitinlinethumb.c @@ -6,15 +6,15 @@ #include <assert.h> #include "misc.h" +#include "mpyconfig.h" #include "lexer.h" -#include "machine.h" #include "parse.h" #include "scope.h" #include "runtime.h" #include "emit.h" #include "asmthumb.h" -#ifdef EMIT_ENABLE_THUMB +#ifdef MICROPY_EMIT_ENABLE_INLINE_THUMB struct _emit_inline_asm_t { int pass; @@ -204,4 +204,4 @@ const emit_inline_asm_method_table_t emit_inline_thumb_method_table = { emit_inline_thumb_op, }; -#endif // EMIT_ENABLE_THUMB +#endif // MICROPY_EMIT_ENABLE_INLINE_THUMB diff --git a/py/emitnative.c b/py/emitnative.c index db375181af..0f09c079a0 100644 --- a/py/emitnative.c +++ b/py/emitnative.c @@ -24,22 +24,13 @@ #include <assert.h> #include "misc.h" +#include "mpyconfig.h" #include "lexer.h" -#include "machine.h" #include "parse.h" #include "scope.h" #include "runtime.h" #include "emit.h" -// select a machine architecture -#if 0 -#if defined(EMIT_ENABLE_NATIVE_X64) -#define N_X64 -#elif defined(EMIT_ENABLE_NATIVE_THUMB) -#define N_THUMB -#endif -#endif - // wrapper around everything in this file #if defined(N_X64) || defined(N_THUMB) diff --git a/py/emitpass1.c b/py/emitpass1.c index 661a622bbc..f14ccc5111 100644 --- a/py/emitpass1.c +++ b/py/emitpass1.c @@ -6,8 +6,8 @@ #include <assert.h> #include "misc.h" +#include "mpyconfig.h" #include "lexer.h" -#include "machine.h" #include "parse.h" #include "compile.h" #include "scope.h" diff --git a/py/emitthumb.c b/py/emitthumb.c index 7bcdf9e43d..1866e00b9a 100644 --- a/py/emitthumb.c +++ b/py/emitthumb.c @@ -6,15 +6,15 @@ #include <assert.h> #include "misc.h" +#include "mpyconfig.h" #include "lexer.h" -#include "machine.h" #include "parse.h" #include "scope.h" #include "runtime.h" #include "emit.h" #include "asmthumb.h" -#ifdef EMIT_ENABLE_THUMB +#ifdef MICROPY_EMIT_ENABLE_THUMB #define REG_LOCAL_1 (REG_R4) #define REG_LOCAL_2 (REG_R5) @@ -775,4 +775,4 @@ const emit_method_table_t emit_thumb_method_table = { emit_thumb_yield_from, }; -#endif // EMIT_ENABLE_THUMB +#endif // MICROPY_EMIT_ENABLE_THUMB diff --git a/py/machine.h b/py/machine.h deleted file mode 100644 index fa39c8f2d0..0000000000 --- a/py/machine.h +++ /dev/null @@ -1,4 +0,0 @@ -typedef int64_t machine_int_t; // must be pointer size -typedef uint64_t machine_uint_t; // must be pointer size -typedef void *machine_ptr_t; // must be of pointer size -typedef double machine_float_t; diff --git a/py/main.c b/py/main.c deleted file mode 100644 index 2059a8fc48..0000000000 --- a/py/main.c +++ /dev/null @@ -1,60 +0,0 @@ -#include <stdint.h> -#include <stdio.h> -#include <string.h> - -#include "misc.h" -#include "lexer.h" -#include "machine.h" -#include "parse.h" -#include "compile.h" -#include "runtime.h" - -int main(int argc, char **argv) { - qstr_init(); - rt_init(); - - if (argc != 2) { - printf("usage: py <file>\n"); - return 1; - } - py_lexer_t *lex = py_lexer_from_file(argv[1]); - //const char *pysrc = "def f():\n x=x+1\n print(42)\n"; - //py_lexer_t *lex = py_lexer_from_str_len("<>", pysrc, strlen(pysrc), false); - if (lex == NULL) { - return 1; - } - - if (0) { - while (!py_lexer_is_kind(lex, PY_TOKEN_END)) { - py_token_show(py_lexer_cur(lex)); - py_lexer_to_next(lex); - } - } else { - py_parse_node_t pn = py_parse(lex, 0); - if (pn != PY_PARSE_NODE_NULL) { - //printf("----------------\n"); - //parse_node_show(pn, 0); - //printf("----------------\n"); - py_compile(pn); - //printf("----------------\n"); - } - } - - py_lexer_free(lex); - - if (1) { - // execute it - py_obj_t module_fun = rt_make_function_from_id(1); - if (module_fun != py_const_none) { - py_obj_t ret = rt_call_function_0(module_fun); - printf("done! got: "); - py_obj_print(ret); - printf("\n"); - } - } - - rt_deinit(); - - //printf("total bytes = %d\n", m_get_total_bytes_allocated()); - return 0; -} diff --git a/py/parse.c b/py/parse.c index 124d00ffeb..541c4eb3f5 100644 --- a/py/parse.c +++ b/py/parse.c @@ -7,8 +7,8 @@ #include <assert.h> #include "misc.h" +#include "mpyconfig.h" #include "lexer.h" -#include "machine.h" #include "parse.h" #define RULE_ACT_KIND_MASK (0xf0) diff --git a/py/runtime.c b/py/runtime.c index 958507521f..ae24646295 100644 --- a/py/runtime.c +++ b/py/runtime.c @@ -5,7 +5,7 @@ #include <assert.h> #include "misc.h" -#include "machine.h" +#include "mpyconfig.h" #include "runtime.h" #include "bc.h" @@ -19,9 +19,6 @@ #define DEBUG_OP_printf(args...) (void)0 #endif -// enable/disable float support with this definition -#define PY_FLOAT (1) - typedef machine_int_t py_small_int_t; #define IS_O(o, k) (((((py_small_int_t)(o)) & 1) == 0) && (((py_obj_base_t*)(o))->kind == (k))) @@ -29,14 +26,14 @@ typedef machine_int_t py_small_int_t; #define FROM_SMALL_INT(o) (((py_small_int_t)(o)) >> 1) #define TO_SMALL_INT(o) ((py_obj_t)(((o) << 1) | 1)) -#ifdef PY_FLOAT +#ifdef MICROPY_ENABLE_FLOAT typedef machine_float_t float_t; #endif typedef enum { O_CONST, O_STR, -#ifdef PY_FLOAT +#ifdef MICROPY_ENABLE_FLOAT O_FLOAT, #endif O_FUN_0, @@ -77,7 +74,7 @@ struct _py_obj_base_t { union { const char *id; qstr u_str; -#ifdef PY_FLOAT +#ifdef MICROPY_ENABLE_FLOAT float_t u_flt; #endif struct { // for O_FUN_[012N] @@ -260,7 +257,7 @@ py_obj_t py_obj_new_str(qstr qstr) { return (py_obj_t)o; } -#ifdef PY_FLOAT +#ifdef MICROPY_ENABLE_FLOAT py_obj_t py_obj_new_float(float_t val) { py_obj_base_t *o = m_new(py_obj_base_t, 1); o->kind = O_FLOAT; @@ -514,7 +511,7 @@ const char *py_obj_get_type_str(py_obj_t o_in) { } case O_STR: return "str"; -#ifdef PY_FLOAT +#ifdef MICROPY_ENABLE_FLOAT case O_FLOAT: return "float"; #endif @@ -557,7 +554,7 @@ void py_obj_print(py_obj_t o_in) { // TODO need to escape chars etc printf("'%s'", qstr_str(o->u_str)); break; -#ifdef PY_FLOAT +#ifdef MICROPY_ENABLE_FLOAT case O_FLOAT: printf("%f", o->u_flt); break; @@ -719,7 +716,7 @@ py_obj_t rt_binary_op(int op, py_obj_t lhs, py_obj_t rhs) { case RT_BINARY_OP_SUBTRACT: val = FROM_SMALL_INT(lhs) - FROM_SMALL_INT(rhs); break; case RT_BINARY_OP_MULTIPLY: val = FROM_SMALL_INT(lhs) * FROM_SMALL_INT(rhs); break; case RT_BINARY_OP_FLOOR_DIVIDE: val = FROM_SMALL_INT(lhs) / FROM_SMALL_INT(rhs); break; -#ifdef PY_FLOAT +#ifdef MICROPY_ENABLE_FLOAT case RT_BINARY_OP_TRUE_DIVIDE: return py_obj_new_float((float_t)FROM_SMALL_INT(lhs) / (float_t)FROM_SMALL_INT(rhs)); #endif default: printf("%d\n", op); assert(0); val = 0; @@ -864,9 +861,11 @@ machine_uint_t rt_convert_obj_for_inline_asm(py_obj_t obj) { // pointer to the string (it's probably constant though!) return (machine_uint_t)qstr_str(o->u_str); +#ifdef MICROPY_ENABLE_FLOAT case O_FLOAT: // convert float to int (could also pass in float registers) return (machine_int_t)o->u_flt; +#endif case O_LIST: // pointer to start of list (could pass length, but then could use len(x) for that) diff --git a/py/scope.c b/py/scope.c index 640d4f742b..c5816871c2 100644 --- a/py/scope.c +++ b/py/scope.c @@ -4,7 +4,7 @@ #include <assert.h> #include "misc.h" -#include "machine.h" +#include "mpyconfig.h" #include "parse.h" #include "scope.h" @@ -5,7 +5,7 @@ #include <assert.h> #include "misc.h" -#include "machine.h" +#include "mpyconfig.h" #include "runtime.h" #include "bc.h" |