diff --git a/ports/atmel-samd/mpconfigport.mk b/ports/atmel-samd/mpconfigport.mk index c77bf83f1e..7be9e203a8 100644 --- a/ports/atmel-samd/mpconfigport.mk +++ b/ports/atmel-samd/mpconfigport.mk @@ -36,6 +36,7 @@ CIRCUITPY_AUDIOMIXER ?= 0 CIRCUITPY_BINASCII ?= 0 CIRCUITPY_AUDIOMP3 ?= 0 CIRCUITPY_BUILTINS_POW3 ?= 0 +CIRCUITPY_COMPUTED_GOTO_SAVE_SPACE ?= 1 CIRCUITPY_FREQUENCYIO ?= 0 CIRCUITPY_JSON ?= 0 CIRCUITPY_TOUCHIO_USE_NATIVE ?= 1 diff --git a/py/circuitpy_mpconfig.h b/py/circuitpy_mpconfig.h index 3eda3b0049..f61c3959f0 100644 --- a/py/circuitpy_mpconfig.h +++ b/py/circuitpy_mpconfig.h @@ -75,6 +75,7 @@ #define MICROPY_MODULE_BUILTIN_INIT (1) #define MICROPY_NONSTANDARD_TYPECODES (0) #define MICROPY_OPT_COMPUTED_GOTO (1) +#define MICROPY_OPT_COMPUTED_GOTO_SAVE_SPACE (CIRCUITPY_COMPUTED_GOTO_SAVE_SPACE) #define MICROPY_PERSISTENT_CODE_LOAD (1) #define MICROPY_PY_ARRAY (1) diff --git a/py/circuitpy_mpconfig.mk b/py/circuitpy_mpconfig.mk index a5b0ed8a44..496ce918ba 100644 --- a/py/circuitpy_mpconfig.mk +++ b/py/circuitpy_mpconfig.mk @@ -130,6 +130,9 @@ CFLAGS += -DCIRCUITPY_CANIO=$(CIRCUITPY_CANIO) CIRCUITPY_DIGITALIO ?= 1 CFLAGS += -DCIRCUITPY_DIGITALIO=$(CIRCUITPY_DIGITALIO) +CIRCUITPY_COMPUTED_GOTO_SAVE_SPACE ?= 0 +CFLAGS += -DCIRCUITPY_COMPUTED_GOTO_SAVE_SPACE=$(CIRCUITPY_COMPUTED_GOTO_SAVE_SPACE) + CIRCUITPY_COUNTIO ?= $(CIRCUITPY_FULL_BUILD) CFLAGS += -DCIRCUITPY_COUNTIO=$(CIRCUITPY_COUNTIO) diff --git a/py/mpconfig.h b/py/mpconfig.h index 034d39d409..1f6f96bd15 100755 --- a/py/mpconfig.h +++ b/py/mpconfig.h @@ -413,6 +413,14 @@ #define MICROPY_OPT_COMPUTED_GOTO (0) #endif +// Whether to save trade flash space for speed in MICROPY_OPT_COMPUTED_GOTO. +// Costs about 3% speed, saves about 1500 bytes space. In addition to the assumptions +// of MICROPY_OPT_COMPUTED_GOTO, also assumes that mp_execute_bytecode is less than +// 32kB in size. +#ifndef MICROPY_OPT_COMPUTED_GOTO_SAVE_SPACE +#define MICROPY_OPT_COMPUTED_GOTO_SAVE_SPACE (0) +#endif + // Whether to cache result of map lookups in LOAD_NAME, LOAD_GLOBAL, LOAD_ATTR, // STORE_ATTR bytecodes. Uses 1 byte extra RAM for each of these opcodes and // uses a bit of extra code ROM, but greatly improves lookup speed. diff --git a/py/vm.c b/py/vm.c index 283d8e198f..890b5f26f4 100644 --- a/py/vm.c +++ b/py/vm.c @@ -129,12 +129,21 @@ mp_vm_return_kind_t PLACE_IN_ITCM(mp_execute_bytecode)(mp_code_state_t *code_sta #endif #if MICROPY_OPT_COMPUTED_GOTO #include "py/vmentrytable.h" +#if MICROPY_OPT_COMPUTED_GOTO_SAVE_SPACE #define ONE_TRUE_DISPATCH() one_true_dispatch: do { \ TRACE(ip); \ MARK_EXC_IP_GLOBAL(); \ goto *(void*)((char*)&&entry_MP_BC_LOAD_CONST_FALSE + entry_table[*ip++]); \ } while (0) #define DISPATCH() do { goto one_true_dispatch; } while(0) +#else + #define DISPATCH() do { \ + TRACE(ip); \ + MARK_EXC_IP_GLOBAL(); \ + goto *entry_table[*ip++]; \ + } while (0) + #define ONE_TRUE_DISPATCH() DISPATCH() +#endif #define DISPATCH_WITH_PEND_EXC_CHECK() goto pending_exception_check #define ENTRY(op) entry_##op #define ENTRY_DEFAULT entry_default diff --git a/py/vmentrytable.h b/py/vmentrytable.h index 8d9f00ad8e..31832bb580 100644 --- a/py/vmentrytable.h +++ b/py/vmentrytable.h @@ -31,9 +31,15 @@ #include "supervisor/linker.h" +#if MICROPY_OPT_COMPUTED_GOTO_SAVE_SPACE #define COMPUTE_ENTRY(x) ((char*)(x) - (char*)&&entry_MP_BC_LOAD_CONST_FALSE) +typedef int16_t entry_table_type; +#else +#define COMPUTE_ENTRY(x) (x) +typedef void *entry_table_type; +#endif -static int16_t const PLACE_IN_DTCM_DATA(entry_table[256]) = { +static entry_table_type const PLACE_IN_DTCM_DATA(entry_table[256]) = { [0 ... 255] = COMPUTE_ENTRY(&&entry_default), [MP_BC_LOAD_CONST_FALSE] = COMPUTE_ENTRY(&&entry_MP_BC_LOAD_CONST_FALSE), [MP_BC_LOAD_CONST_NONE] = COMPUTE_ENTRY(&&entry_MP_BC_LOAD_CONST_NONE),