From 767f5d045f76ff07336c1f55608247a2d1a5b45c Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Tue, 23 Jan 2024 15:33:45 -0500 Subject: [PATCH] [PRISM] Use lvar depths for starting --- prism_compile.c | 32 ++++++++++++++++++-------------- 1 file changed, 18 insertions(+), 14 deletions(-) diff --git a/prism_compile.c b/prism_compile.c index 654291170d..baa41bc031 100644 --- a/prism_compile.c +++ b/prism_compile.c @@ -786,23 +786,27 @@ pm_interpolated_node_compile(pm_node_list_t *parts, rb_iseq_t *iseq, NODE dummy_ static pm_local_index_t pm_lookup_local_index(rb_iseq_t *iseq, pm_scope_node_t *scope_node, pm_constant_id_t constant_id, int start_depth) { - pm_local_index_t lindex = {0}; - int level = (start_depth) ? start_depth : 0; + pm_local_index_t lindex = { 0 }; st_data_t local_index; - while(!st_lookup(scope_node->index_lookup_table, constant_id, &local_index)) { + int level; + for (level = 0; level < start_depth; level++) { + scope_node = scope_node->previous; + } + + while (!st_lookup(scope_node->index_lookup_table, constant_id, &local_index)) { level++; if (scope_node->previous) { scope_node = scope_node->previous; } else { // We have recursed up all scope nodes // and have not found the local yet - rb_bug("Local with constant_id %u does not exist", (unsigned int)constant_id); + rb_bug("Local with constant_id %u does not exist", (unsigned int) constant_id); } } lindex.level = level; - lindex.index = scope_node->local_table_for_iseq_size - (int)local_index; + lindex.index = scope_node->local_table_for_iseq_size - (int) local_index; return lindex; } @@ -2991,7 +2995,7 @@ pm_compile_target_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *cons // for i in []; end // pm_local_variable_target_node_t *cast = (pm_local_variable_target_node_t *) node; - pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, 0); + pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth); ADD_SETLOCAL(writes, &dummy_line_node, index.index, index.level); break; @@ -5368,7 +5372,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, pm_local_variable_operator_write_node_t *local_variable_operator_write_node = (pm_local_variable_operator_write_node_t*) node; pm_constant_id_t constant_id = local_variable_operator_write_node->name; - pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, constant_id, 0); + pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, constant_id, local_variable_operator_write_node->depth); ADD_GETLOCAL(ret, &dummy_line_node, local_index.index, local_index.level); @@ -5394,7 +5398,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, ADD_INSNL(ret, &dummy_line_node, branchunless, set_label); pm_constant_id_t constant_id = local_variable_or_write_node->name; - pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, constant_id, 0); + pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, constant_id, local_variable_or_write_node->depth); ADD_GETLOCAL(ret, &dummy_line_node, local_index.index, local_index.level); PM_DUP_UNLESS_POPPED; @@ -5417,7 +5421,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, pm_local_variable_read_node_t *local_read_node = (pm_local_variable_read_node_t *) node; if (!popped) { - pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_read_node->name, 0); + pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_read_node->name, local_read_node->depth); ADD_GETLOCAL(ret, &dummy_line_node, index.index, index.level); } return; @@ -5426,7 +5430,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, pm_local_variable_target_node_t *local_write_node = (pm_local_variable_target_node_t *) node; pm_constant_id_t constant_id = local_write_node->name; - pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, constant_id, 0); + pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, constant_id, local_write_node->depth); ADD_SETLOCAL(ret, &dummy_line_node, index.index, index.level); return; @@ -5439,7 +5443,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, pm_constant_id_t constant_id = local_write_node->name; - pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, constant_id, 0); + pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, constant_id, local_write_node->depth); ADD_SETLOCAL(ret, &dummy_line_node, index.index, index.level); return; @@ -5580,7 +5584,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, assert(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE)); pm_local_variable_target_node_t *local_target = (pm_local_variable_target_node_t *) target; - pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, 0); + pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, local_target->depth); ADD_INSN1(ret, &dummy_line_node, putobject, rb_id2sym(pm_constant_id_lookup(scope_node, local_target->name))); ADD_SEND(ret, &dummy_line_node, idAREF, INT2FIX(1)); @@ -5597,7 +5601,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, assert(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE)); pm_local_variable_target_node_t *local_target = (pm_local_variable_target_node_t *) target; - pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, 0); + pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, local_target->depth); if (((size_t) targets_index) < (targets_count - 1)) { PM_DUP; @@ -5620,7 +5624,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret, assert(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE)); pm_local_variable_target_node_t *local_target = (pm_local_variable_target_node_t *) target; - pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, 0); + pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, local_target->depth); PM_PUTNIL; ADD_SETLOCAL(ret, &dummy_line_node, index.index, index.level);