From 53ea2b5ce2f7088c871c6c264899e4eae7973859 Mon Sep 17 00:00:00 2001 From: Dave Hylands Date: Sat, 31 Oct 2015 22:55:13 -0700 Subject: [PATCH] teensy: Switch over to using frozen modules instead of memzip I left memzip in for the time being, so you can choose in the Makefile whether to USE_FROZEN or USE_MEMZIP. It looks like using frozen saves about 2472 bytes (using my set of 15 python files), mostly due to overheads in the zip file format. --- teensy/Makefile | 65 ++++++++++++++++++++++++++++++++------------ teensy/lexerfrozen.c | 11 ++++++++ teensy/main.c | 18 ++++++++++++ 3 files changed, 77 insertions(+), 17 deletions(-) create mode 100644 teensy/lexerfrozen.c diff --git a/teensy/Makefile b/teensy/Makefile index 9db22918c3..b458485ce5 100644 --- a/teensy/Makefile +++ b/teensy/Makefile @@ -74,6 +74,9 @@ endif CFLAGS += -fdata-sections -ffunction-sections LDFLAGS += -Wl,--gc-sections +USE_FROZEN = 1 +USE_MEMZIP = 0 + SRC_C = \ hal_ftm.c \ hal_gpio.c \ @@ -81,9 +84,6 @@ SRC_C = \ main.c \ lcd.c \ led.c \ - lib/memzip/import.c \ - lib/memzip/lexermemzip.c \ - lib/memzip/memzip.c \ modpyb.c \ pin_defs_teensy.c \ reg.c \ @@ -92,7 +92,6 @@ SRC_C = \ uart.c \ usb.c \ - STM_SRC_C = $(addprefix stmhal/,\ gccollect.c \ input.c \ @@ -127,11 +126,55 @@ SRC_TEENSY = $(addprefix core/,\ OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(STM_SRC_C:.c=.o) $(STM_SRC_S:.s=.o) $(SRC_TEENSY:.c=.o)) OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o)) OBJ += $(BUILD)/pins_gen.o -OBJ += $(BUILD)/memzip-files.o all: hex hex: $(BUILD)/micropython.hex +ifeq ($(USE_MEMZIP),1) +SRC_C += \ + lib/memzip/import.c \ + lib/memzip/lexermemzip.c \ + lib/memzip/memzip.c \ + +OBJ += $(BUILD)/memzip-files.o + +MAKE_MEMZIP = ../lib/memzip/make-memzip.py +ifeq ($(MEMZIP_DIR),) +MEMZIP_DIR = memzip_files +endif + +$(BUILD)/memzip-files.o: $(BUILD)/memzip-files.c + $(call compile_c) + +$(BUILD)/memzip-files.c: $(shell find ${MEMZIP_DIR} -type f) + @$(ECHO) "Creating $@" + $(Q)$(PYTHON) $(MAKE_MEMZIP) --zip-file $(BUILD)/memzip-files.zip --c-file $@ $(MEMZIP_DIR) + +endif # USE_MEMZIP + +ifeq ($(USE_FROZEN),1) + +CFLAGS += -DMICROPY_MODULE_FROZEN + +SRC_C += \ + lexerfrozen.c + +OBJ += $(BUILD)/frozen-files.o + +MAKE_FROZEN = ../tools/make-frozen.py +ifeq ($(FROZEN_DIR),) +FROZEN_DIR = memzip_files +endif + +$(BUILD)/frozen-files.o: $(BUILD)/frozen-files.c + $(call compile_c) + +$(BUILD)/frozen-files.c: $(shell find ${FROZEN_DIR} -type f) + @$(ECHO) "Creating $@" + $(Q)$(PYTHON) $(MAKE_FROZEN) $(FROZEN_DIR) > $@ + +endif # USE_FROZEN + ifeq ($(ARDUINO),) post_compile: $(BUILD)/micropython.hex $(ECHO) "Please define ARDUINO (where TeensyDuino is installed)" @@ -161,16 +204,11 @@ $(BUILD)/micropython.elf: $(OBJ) $(Q)$(CC) $(LDFLAGS) -o "$@" -Wl,-Map,$(@:.elf=.map) $^ $(LIBS) $(Q)$(SIZE) $@ -ifeq ($(MEMZIP_DIR),) -MEMZIP_DIR = memzip_files -endif - $(BUILD)/%.hex: $(BUILD)/%.elf $(ECHO) "HEX $<" $(Q)$(OBJCOPY) -O ihex -R .eeprom "$<" "$@" MAKE_PINS = make-pins.py -MAKE_MEMZIP = ../lib/memzip/make-memzip.py BOARD_PINS = teensy_pins.csv AF_FILE = mk20dx256_af.csv PREFIX_FILE = mk20dx256_prefix.c @@ -196,13 +234,6 @@ $(BUILD)/%_gen.c $(HEADER_BUILD)/%.h $(HEADER_BUILD)/%_af_const.h $(BUILD)/%_qst $(BUILD)/pins_gen.o: $(BUILD)/pins_gen.c $(call compile_c) -$(BUILD)/memzip-files.o: $(BUILD)/memzip-files.c - $(call compile_c) - -$(BUILD)/memzip-files.c: $(shell find ${MEMZIP_DIR} -type f) - @$(ECHO) "Creating $@" - $(Q)$(PYTHON) $(MAKE_MEMZIP) --zip-file $(BUILD)/memzip-files.zip --c-file $@ $(MEMZIP_DIR) - $(BUILD)/%.pp: $(BUILD)/%.c $(ECHO) "PreProcess $<" $(Q)$(CC) $(CFLAGS) -E -Wp,-C,-dD,-dI -o $@ $< diff --git a/teensy/lexerfrozen.c b/teensy/lexerfrozen.c new file mode 100644 index 0000000000..03ef800876 --- /dev/null +++ b/teensy/lexerfrozen.c @@ -0,0 +1,11 @@ +#include + +#include "py/lexer.h" + +mp_import_stat_t mp_import_stat(const char *path) { + return MP_IMPORT_STAT_NO_EXIST; +} + +mp_lexer_t *mp_lexer_new_from_file(const char *filename) { + return NULL; +} diff --git a/teensy/main.c b/teensy/main.c index c31be6e01b..417bde78be 100644 --- a/teensy/main.c +++ b/teensy/main.c @@ -22,6 +22,10 @@ #include "uart.h" #include "pin.h" +#if MICROPY_MODULE_FROZEN +#include "py/compile.h" +#include "py/frozenmod.h" +#endif extern uint32_t _heap_start; @@ -301,14 +305,27 @@ soft_reset: } #endif +#if MICROPY_MODULE_FROZEN + { + mp_lexer_t *lex = mp_find_frozen_module("boot", 4); + mp_parse_compile_execute(lex, MP_PARSE_FILE_INPUT, mp_globals_get(), mp_locals_get()); + } +#else if (!pyexec_file("/boot.py")) { flash_error(4); } +#endif // Turn bootup LED off led_state(PYB_LED_BUILTIN, 0); // run main script +#if MICROPY_MODULE_FROZEN + { + mp_lexer_t *lex = mp_find_frozen_module("main", 4); + mp_parse_compile_execute(lex, MP_PARSE_FILE_INPUT, mp_globals_get(), mp_locals_get()); + } +#else { vstr_t *vstr = vstr_new(); vstr_add_str(vstr, "/"); @@ -322,6 +339,7 @@ soft_reset: } vstr_free(vstr); } +#endif // enter REPL // REPL mode can change, or it can request a soft reset