Skip to content

Commit d94bc67

Browse files
committed
py/compile: Optimise emitter label indices to save a word of heap.
Previous to this patch, a label with value "0" was used to indicate an invalid label, but that meant a wasted word (at slot 0) in the array of label offsets. This patch adjusts the label indices so the first one starts at 0, and the maximum value indicates an invalid label.
1 parent 4492293 commit d94bc67

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

py/compile.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040

4141
// TODO need to mangle __attr names
4242

43+
#define INVALID_LABEL (0xffff)
44+
4345
typedef enum {
4446
// define rules with a compile function
4547
#define DEF_RULE(rule, comp, kind, ...) PN_##rule,
@@ -954,15 +956,15 @@ STATIC void compile_del_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
954956
}
955957

956958
STATIC void compile_break_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
957-
if (comp->break_label == 0) {
959+
if (comp->break_label == INVALID_LABEL) {
958960
compile_syntax_error(comp, (mp_parse_node_t)pns, "'break' outside loop");
959961
}
960962
assert(comp->cur_except_level >= comp->break_continue_except_level);
961963
EMIT_ARG(break_loop, comp->break_label, comp->cur_except_level - comp->break_continue_except_level);
962964
}
963965

964966
STATIC void compile_continue_stmt(compiler_t *comp, mp_parse_node_struct_t *pns) {
965-
if (comp->continue_label == 0) {
967+
if (comp->continue_label == INVALID_LABEL) {
966968
compile_syntax_error(comp, (mp_parse_node_t)pns, "'continue' outside loop");
967969
}
968970
assert(comp->cur_except_level >= comp->break_continue_except_level);
@@ -2960,7 +2962,7 @@ STATIC void check_for_doc_string(compiler_t *comp, mp_parse_node_t pn) {
29602962
STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
29612963
comp->pass = pass;
29622964
comp->scope_cur = scope;
2963-
comp->next_label = 1;
2965+
comp->next_label = 0;
29642966
EMIT_ARG(start_pass, pass, scope);
29652967

29662968
if (comp->pass == MP_PASS_SCOPE) {
@@ -3135,7 +3137,7 @@ STATIC void compile_scope(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
31353137
STATIC void compile_scope_inline_asm(compiler_t *comp, scope_t *scope, pass_kind_t pass) {
31363138
comp->pass = pass;
31373139
comp->scope_cur = scope;
3138-
comp->next_label = 1;
3140+
comp->next_label = 0;
31393141

31403142
if (scope->kind != SCOPE_FUNCTION) {
31413143
compile_syntax_error(comp, MP_PARSE_NODE_NULL, "inline assembler must be a function");
@@ -3381,6 +3383,8 @@ mp_raw_code_t *mp_compile_to_raw_code(mp_parse_tree_t *parse_tree, qstr source_f
33813383

33823384
comp->source_file = source_file;
33833385
comp->is_repl = is_repl;
3386+
comp->break_label = INVALID_LABEL;
3387+
comp->continue_label = INVALID_LABEL;
33843388

33853389
// create the module scope
33863390
scope_t *module_scope = scope_new_and_link(comp, SCOPE_MODULE, parse_tree->root, emit_opt);

0 commit comments

Comments
 (0)