8000 teensy: Switch over to using frozen modules instead of memzip · micropython/micropython@53ea2b5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 53ea2b5

Browse files
dhylandsdpgeorge
authored andcommitted
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.
1 parent 074d713 commit 53ea2b5

File tree

3 files changed

+77
-17
lines changed

3 files changed

+77
-17
lines changed

teensy/Makefile

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,16 @@ endif
7474
CFLAGS += -fdata-sections -ffunction-sections
7575
LDFLAGS += -Wl,--gc-sections
7676

77+
USE_FROZEN = 1
78+
USE_MEMZIP = 0
79+
7780
SRC_C = \
7881
hal_ftm.c \
7982
hal_gpio.c \
8083
help.c \
8184
main.c \
8285
lcd.c \
8386
led.c \
84-
lib/memzip/import.c \
85-
lib/memzip/lexermemzip.c \
86-
lib/memzip/memzip.c \
8787
modpyb.c \
8888
pin_defs_teensy.c \
8989
reg.c \
@@ -92,7 +92,6 @@ SRC_C = \
9292
uart.c \
9393
usb.c \
9494

95-
9695
STM_SRC_C = $(addprefix stmhal/,\
9796
gccollect.c \
9897
input.c \
@@ -127,11 +126,55 @@ SRC_TEENSY = $(addprefix core/,\
127126
OBJ = $(PY_O) $(addprefix $(BUILD)/, $(SRC_C:.c=.o) $(STM_SRC_C:.c=.o) $(STM_SRC_S:.s=.o) $(SRC_TEENSY:.c=.o))
128127
OBJ += $(addprefix $(BUILD)/, $(LIB_SRC_C:.c=.o))
129128
OBJ += $(BUILD)/pins_gen.o
130-
OBJ += $(BUILD)/memzip-files.o
131129

132130
all: hex
133131
hex: $(BUILD)/micropython.hex
134132

133+
ifeq ($(USE_MEMZIP),1)
134+
SRC_C += \
135+
lib/memzip/import.c \
136+
lib/memzip/lexermemzip.c \
137+
lib/memzip/memzip.c \
138+
139+
OBJ += $(BUILD)/memzip-files.o
140+
141+
MAKE_MEMZIP = ../lib/memzip/make-memzip.py
142+
ifeq ($(MEMZIP_DIR),)
143+
MEMZIP_DIR = memzip_files
144+
endif
145+
146+
$(BUILD)/memzip-files.o: $(BUILD)/memzip-files.c
147+
$(call compile_c)
148+
149+
$(BUILD)/memzip-files.c: $(shell find ${MEMZIP_DIR} -type f)
150+
@$(ECHO) "Creating $@"
151+
$(Q)$(PYTHON) $(MAKE_MEMZIP) --zip-file $(BUILD)/memzip-files.zip --c-file $@ $(MEMZIP_DIR)
152+
153+
endif # USE_MEMZIP
154+
155+
ifeq ($(USE_FROZEN),1)
156+
157+
CFLAGS += -DMICROPY_MODULE_FROZEN
158+
159+
SRC_C += \
160+
lexerfrozen.c
161+
162+
OBJ += $(BUILD)/frozen-files.o
163+
164+
MAKE_FROZEN = ../tools/make-frozen.py
165+
ifeq ($(FROZEN_DIR),)
166+
FROZEN_DIR = memzip_files
167+
endif
168+
169+
$(BUILD)/frozen-files.o: $(BUILD)/frozen-files.c
170+
$(call compile_c)
171+
172+
$(BUILD)/frozen-files.c: $(shell find ${FROZEN_DIR} -type f)
173+
@$(ECHO) "Creating $@"
174+
$(Q)$(PYTHON) $(MAKE_FROZEN) $(FROZEN_DIR) > $@
175+
176+
endif # USE_FROZEN
177+
135178
ifeq ($(ARDUINO),)
136179
post_compile: $(BUILD)/micropython.hex
137180
$(ECHO) "Please define ARDUINO (where TeensyDuino is installed)"
@@ -161,16 +204,11 @@ $(BUILD)/micropython.elf: $(OBJ)
161204
$(Q)$(CC) $(LDFLAGS) -o "$@" -Wl,-Map,$(@:.elf=.map) $^ $(LIBS)
162205
$(Q)$(SIZE) $@
163206

164-
ifeq ($(MEMZIP_DIR),)
165-
MEMZIP_DIR = memzip_files
166-
endif
167-
168207
$(BUILD)/%.hex: $(BUILD)/%.elf
169208
$(ECHO) "HEX $<"
170209
$(Q)$(OBJCOPY) -O ihex -R .eeprom "$<" "$@"
171210

172211
MAKE_PINS = make-pins.py
173-
MAKE_MEMZIP = ../lib/memzip/make-memzip.py
174212
BOARD_PINS = teensy_pins.csv
175213
AF_FILE = mk20dx256_af.csv
176214
PREFIX_FILE = mk20dx256_prefix.c
@@ -196,13 +234,6 @@ $(BUILD)/%_gen.c $(HEADER_BUILD)/%.h $(HEADER_BUILD)/%_af_const.h $(BUILD)/%_qst
196234
$(BUILD)/pins_gen.o: $(BUILD)/pins_gen.c
197235
$(call compile_c)
198236

199-
$(BUILD)/memzip-files.o: $(BUILD)/memzip-files.c
200-
$(call compile_c)
201-
202-
$(BUILD)/memzip-files.c: $(shell find ${MEMZIP_DIR} -type f)
203-
@$(ECHO) "Creating $@"
204-
$(Q)$(PYTHON) $(MAKE_MEMZIP) --zip-file $(BUILD)/memzip-files.zip --c-file $@ $(MEMZIP_DIR)
205-
206237
$(BUILD)/%.pp: $(BUILD)/%.c
207238
$(ECHO) "PreProcess $<"
208239
$(Q)$(CC) $(CFLAGS) -E -Wp,-C,-dD,-dI -o $@ $<

teensy/lexerfrozen.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <stdio.h>
2+
3+
#include "py/lexer.h"
4+
5+
mp_import_stat_t mp_import_stat(const char *path) {
6+
return MP_IMPORT_STAT_NO_EXIST;
7+
}
8+
9+
mp_lexer_t *mp_lexer_new_from_file(const char *filename) {
10+
return NULL;
11+
}

teensy/main.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
#include "uart.h"
2323
#include "pin.h"
2424

25+
#if MICROPY_MODULE_FROZEN
26+
#include "py/compile.h"
27+
#include "py/frozenmod.h"
28+
#endif
2529

2630
extern uint32_t _heap_start;
2731

@@ -301,14 +305,27 @@ int main(void) {
301305
}
302306
#endif
303307

308+
#if MICROPY_MODULE_FROZEN
309+
{
310+
mp_lexer_t *lex = mp_find_frozen_module("boot", 4);
311+
mp_parse_compile_execute(lex, MP_PARSE_FILE_INPUT, mp_globals_get(), mp_locals_get());
312+
}
313+
#else
304314
if (!pyexec_file("/boot.py")) {
305315
flash_error(4);
306316
}
317+
#endif
307318

308319
// Turn bootup LED off
309320
led_state(PYB_LED_BUILTIN, 0);
310321

311322
// run main script
323+
#if MICROPY_MODULE_FROZEN
324+
{
325+
mp_lexer_t *lex = mp_find_frozen_module("main", 4);
326+
mp_parse_compile_execute(lex, MP_PARSE_FILE_INPUT, mp_globals_get(), mp_locals_get());
327+
}
328+
#else
312329
{
313330
vstr_t *vstr = vstr_new();
314331
vstr_add_str(vstr, "/");
@@ -322,6 +339,7 @@ int main(void) {
322339
}
323340
vstr_free(vstr);
324341
}
342+
#endif
325343

326344
// enter REPL
327345
// REPL mode can change, or it can request a soft reset

0 commit comments

Comments
 (0)
0