diff options
-rw-r--r-- | examples/conwaylife.py | 6 | ||||
-rw-r--r-- | examples/mandel.py | 3 | ||||
-rw-r--r-- | examples/pyb.py | 4 | ||||
-rw-r--r-- | py/compile.c | 30 | ||||
-rw-r--r-- | py/emit.h | 8 | ||||
-rw-r--r-- | py/emitbc.c | 5 | ||||
-rw-r--r-- | py/emitinlinethumb.c | 10 | ||||
-rw-r--r-- | py/emitnative.c | 9 | ||||
-rw-r--r-- | py/makeqstrdata.py | 22 | ||||
-rw-r--r-- | py/qstrdefs.h | 8 | ||||
-rw-r--r-- | py/scope.c | 12 |
11 files changed, 92 insertions, 25 deletions
diff --git a/examples/conwaylife.py b/examples/conwaylife.py index 7c6ac4d548..89ef94c8c6 100644 --- a/examples/conwaylife.py +++ b/examples/conwaylife.py @@ -1,3 +1,7 @@ +#import essential libraries +import lcd +import pyb + # do 1 iteration of Conway's Game of Life def conway_step(): for x in range(128): # loop over x coordinates @@ -37,8 +41,6 @@ def conway_go(num_frames): pyb.delay(300) # PC testing -import lcd -import pyb lcd = lcd.LCD(128, 32) conway_rand() conway_go(1000) diff --git a/examples/mandel.py b/examples/mandel.py index d2b34fff83..bbb8086470 100644 --- a/examples/mandel.py +++ b/examples/mandel.py @@ -3,10 +3,9 @@ try: except: pass - def mandelbrot(): # returns True if c, complex, is in the Mandelbrot set - @micropython.native + #@micropython.native def in_set(c): z = 0 for i in range(40): diff --git a/examples/pyb.py b/examples/pyb.py index f7b52a2a1e..65fed6647e 100644 --- a/examples/pyb.py +++ b/examples/pyb.py @@ -1,9 +1,9 @@ # pyboard testing functions for CPython import time - def delay(n): - time.sleep(float(n) / 1000) + #time.sleep(float(n) / 1000) + pass rand_seed = 1 def rand(): diff --git a/py/compile.c b/py/compile.c index 7e77832bc3..e1b62a11bf 100644 --- a/py/compile.c +++ b/py/compile.c @@ -3154,8 +3154,28 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) { } } - bool had_error = comp->had_error; - m_del_obj(compiler_t, comp); + // free the emitters +#if !MICROPY_EMIT_CPYTHON + if (emit_bc != NULL) { + emit_bc_free(emit_bc); + } +#if MICROPY_EMIT_NATIVE + if (emit_native != NULL) { +#if MICROPY_EMIT_X64 + emit_native_x64_free(emit_native); +#elif MICROPY_EMIT_THUMB + emit_native_thumb_free(emit_native); +#endif + } +#endif +#if MICROPY_EMIT_INLINE_THUMB + if (emit_inline_thumb != NULL) { + emit_inline_thumb_free(emit_inline_thumb); + } +#endif +#endif // !MICROPY_EMIT_CPYTHON + + // free the scopes uint unique_code_id = module_scope->unique_code_id; for (scope_t *s = module_scope; s;) { scope_t *next = s->next; @@ -3163,13 +3183,17 @@ mp_obj_t mp_compile(mp_parse_node_t pn, qstr source_file, bool is_repl) { s = next; } + // free the compiler + bool had_error = comp->had_error; + m_del_obj(compiler_t, comp); + if (had_error) { // TODO return a proper error message return mp_const_none; } else { #if MICROPY_EMIT_CPYTHON // can't create code, so just return true - (void)unique_code_id; // to suppress warning that module_scope is unused + (void)unique_code_id; // to suppress warning that unique_code_id is unused return mp_const_true; #else // return function that executes the outer module @@ -118,12 +118,16 @@ extern const emit_method_table_t emit_native_x64_method_table; extern const emit_method_table_t emit_native_thumb_method_table; emit_t *emit_pass1_new(qstr qstr___class__); -void emit_pass1_free(emit_t *emit); emit_t *emit_cpython_new(uint max_num_labels); emit_t *emit_bc_new(uint max_num_labels); emit_t *emit_native_x64_new(uint max_num_labels); emit_t *emit_native_thumb_new(uint max_num_labels); +void emit_pass1_free(emit_t *emit); +void emit_bc_free(emit_t *emit); +void emit_native_x64_free(emit_t *emit); +void emit_native_thumb_free(emit_t *emit); + typedef struct _emit_inline_asm_t emit_inline_asm_t; typedef struct _emit_inline_asm_method_table_t { @@ -137,3 +141,5 @@ typedef struct _emit_inline_asm_method_table_t { extern const emit_inline_asm_method_table_t emit_inline_thumb_method_table; emit_inline_asm_t *emit_inline_thumb_new(uint max_num_labels); +void emit_inline_thumb_free(emit_inline_asm_t *emit); + diff --git a/py/emitbc.c b/py/emitbc.c index c3385e0b66..10a95fbcfa 100644 --- a/py/emitbc.c +++ b/py/emitbc.c @@ -43,6 +43,11 @@ emit_t *emit_bc_new(uint max_num_labels) { return emit; } +void emit_bc_free(emit_t *emit) { + m_del(uint, emit->label_offsets, emit->max_num_labels); + m_del_obj(emit_t, emit); +} + // all functions must go through this one to emit code info static byte* emit_get_cur_to_write_code_info(emit_t* emit, int num_bytes_to_write) { //printf("emit %d\n", num_bytes_to_write); diff --git a/py/emitinlinethumb.c b/py/emitinlinethumb.c index 9d5a4206a0..c35192210c 100644 --- a/py/emitinlinethumb.c +++ b/py/emitinlinethumb.c @@ -20,13 +20,13 @@ struct _emit_inline_asm_t { int pass; scope_t *scope; - int max_num_labels; + uint max_num_labels; qstr *label_lookup; asm_thumb_t *as; }; emit_inline_asm_t *emit_inline_thumb_new(uint max_num_labels) { - emit_inline_asm_t *emit = m_new(emit_inline_asm_t, 1); + emit_inline_asm_t *emit = m_new_obj(emit_inline_asm_t); emit->max_num_labels = max_num_labels; emit->label_lookup = m_new(qstr, max_num_labels); memset(emit->label_lookup, 0, emit->max_num_labels * sizeof(qstr)); @@ -34,6 +34,12 @@ emit_inline_asm_t *emit_inline_thumb_new(uint max_num_labels) { return emit; } +void emit_inline_thumb_free(emit_inline_asm_t *emit) { + m_del(qstr, emit->label_lookup, emit->max_num_labels); + asm_thumb_free(emit->as, false); + m_del_obj(emit_inline_asm_t, emit); +} + static void emit_inline_thumb_start_pass(emit_inline_asm_t *emit, pass_kind_t pass, scope_t *scope) { emit->pass = pass; emit->scope = scope; diff --git a/py/emitnative.c b/py/emitnative.c index 6fc1742489..258aa9fce5 100644 --- a/py/emitnative.c +++ b/py/emitnative.c @@ -146,6 +146,15 @@ emit_t *EXPORT_FUN(new)(uint max_num_labels) { return emit; } +void EXPORT_FUN(free)(emit_t *emit) { +#if N_X64 + asm_x64_free(emit->as, false); +#elif N_THUMB + asm_thumb_free(emit->as, false); +#endif + m_del_obj(emit_t, emit); +} + static void emit_native_set_viper_types(emit_t *emit, bool do_viper_types) { emit->do_viper_types = do_viper_types; } diff --git a/py/makeqstrdata.py b/py/makeqstrdata.py index c4b7be42e7..c5ad708e89 100644 --- a/py/makeqstrdata.py +++ b/py/makeqstrdata.py @@ -1,6 +1,13 @@ import argparse import re +# codepoint2name is different in Python 2 to Python 3 +import platform +if platform.python_version_tuple()[0] == '2': + from htmlentitydefs import codepoint2name +elif platform.python_version_tuple()[0] == '3': + from html.entities import codepoint2name + # this must match the equivalent function in qstr.c def compute_hash(qstr): hash = 0 @@ -10,7 +17,7 @@ def compute_hash(qstr): def do_work(infiles): # read the qstrs in from the input files - qstrs = [] + qstrs = {} for infile in infiles: with open(infile, 'rt') as f: line_number = 0 @@ -23,28 +30,29 @@ def do_work(infiles): continue # verify line is of the correct form - match = re.match(r'Q\(([0-9A-Za-z_]+)\)$', line) + match = re.match(r'Q\((.+)\)$', line) if not match: print('({}:{}) bad qstr format, got {}'.format(infile, line_number, line)) return False # get the qstr value qstr = match.group(1) + ident = re.sub(r'[^A-Za-z0-9_]', lambda s: "_" + codepoint2name[ord(s.group(0))] + "_", qstr) # don't add duplicates - if qstr in qstrs: + if ident in qstrs: continue - # add the qstr to the list - qstrs.append(qstr) + # add the qstr to the list, with order number to retain original order in file + qstrs[ident] = (len(qstrs), ident, qstr) # process the qstrs, printing out the generated C header file print('// This file was automatically generated by makeqstrdata.py') print('') - for qstr in qstrs: + for order, ident, qstr in sorted(qstrs.values(), key=lambda x: x[0]): qhash = compute_hash(qstr) qlen = len(qstr) - print('Q({}, (const byte*)"\\x{:02x}\\x{:02x}\\x{:02x}\\x{:02x}" "{}")'.format(qstr, qhash & 0xff, (qhash >> 8) & 0xff, qlen & 0xff, (qlen >> 8) & 0xff, qstr)) + print('Q({}, (const byte*)"\\x{:02x}\\x{:02x}\\x{:02x}\\x{:02x}" "{}")'.format(ident, qhash & 0xff, (qhash >> 8) & 0xff, qlen & 0xff, (qlen >> 8) & 0xff, qstr)) return True diff --git a/py/qstrdefs.h b/py/qstrdefs.h index 9bc01c5851..f2c4dfd97f 100644 --- a/py/qstrdefs.h +++ b/py/qstrdefs.h @@ -80,3 +80,11 @@ Q(sort) Q(join) Q(strip) Q(format) + +Q(<module>) +Q(<lambda>) +Q(<listcomp>) +Q(<dictcomp>) +Q(<setcomp>) +Q(<genexpr>) +Q(<stdin>) diff --git a/py/scope.c b/py/scope.c index 1f602ac9c0..ab20f61f37 100644 --- a/py/scope.c +++ b/py/scope.c @@ -18,7 +18,7 @@ scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, uint scope->source_file = source_file; switch (kind) { case SCOPE_MODULE: - scope->simple_name = QSTR_FROM_STR_STATIC("<module>"); + scope->simple_name = MP_QSTR__lt_module_gt_; break; case SCOPE_FUNCTION: case SCOPE_CLASS: @@ -26,19 +26,19 @@ scope_t *scope_new(scope_kind_t kind, mp_parse_node_t pn, qstr source_file, uint scope->simple_name = MP_PARSE_NODE_LEAF_ARG(((mp_parse_node_struct_t*)pn)->nodes[0]); break; case SCOPE_LAMBDA: - scope->simple_name = QSTR_FROM_STR_STATIC("<lambda>"); + scope->simple_name = MP_QSTR__lt_lambda_gt_; break; case SCOPE_LIST_COMP: - scope->simple_name = QSTR_FROM_STR_STATIC("<listcomp>"); + scope->simple_name = MP_QSTR__lt_listcomp_gt_; break; case SCOPE_DICT_COMP: - scope->simple_name = QSTR_FROM_STR_STATIC("<dictcomp>"); + scope->simple_name = MP_QSTR__lt_dictcomp_gt_; break; case SCOPE_SET_COMP: - scope->simple_name = QSTR_FROM_STR_STATIC("<setcomp>"); + scope->simple_name = MP_QSTR__lt_setcomp_gt_; break; case SCOPE_GEN_EXPR: - scope->simple_name = QSTR_FROM_STR_STATIC("<genexpr>"); + scope->simple_name = MP_QSTR__lt_genexpr_gt_; break; default: assert(0); |