Skip to content

Commit 767f5d0

Browse files
committed
[PRISM] Use lvar depths for starting
1 parent 529700d commit 767f5d0

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

prism_compile.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -786,23 +786,27 @@ pm_interpolated_node_compile(pm_node_list_t *parts, rb_iseq_t *iseq, NODE dummy_
786786
static pm_local_index_t
787787
pm_lookup_local_index(rb_iseq_t *iseq, pm_scope_node_t *scope_node, pm_constant_id_t constant_id, int start_depth)
788788
{
789-
pm_local_index_t lindex = {0};
790-
int level = (start_depth) ? start_depth : 0;
789+
pm_local_index_t lindex = { 0 };
791790
st_data_t local_index;
792791

793-
while(!st_lookup(scope_node->index_lookup_table, constant_id, &local_index)) {
792+
int level;
793+
for (level = 0; level < start_depth; level++) {
794+
scope_node = scope_node->previous;
795+
}
796+
797+
while (!st_lookup(scope_node->index_lookup_table, constant_id, &local_index)) {
794798
level++;
795799
if (scope_node->previous) {
796800
scope_node = scope_node->previous;
797801
} else {
798802
// We have recursed up all scope nodes
799803
// and have not found the local yet
800-
rb_bug("Local with constant_id %u does not exist", (unsigned int)constant_id);
804+
rb_bug("Local with constant_id %u does not exist", (unsigned int) constant_id);
801805
}
802806
}
803807

804808
lindex.level = level;
805-
lindex.index = scope_node->local_table_for_iseq_size - (int)local_index;
809+
lindex.index = scope_node->local_table_for_iseq_size - (int) local_index;
806810
return lindex;
807811
}
808812

@@ -2991,7 +2995,7 @@ pm_compile_target_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *cons
29912995
// for i in []; end
29922996
//
29932997
pm_local_variable_target_node_t *cast = (pm_local_variable_target_node_t *) node;
2994-
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, 0);
2998+
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, cast->name, cast->depth);
29952999

29963000
ADD_SETLOCAL(writes, &dummy_line_node, index.index, index.level);
29973001
break;
@@ -5368,7 +5372,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
53685372
pm_local_variable_operator_write_node_t *local_variable_operator_write_node = (pm_local_variable_operator_write_node_t*) node;
53695373

53705374
pm_constant_id_t constant_id = local_variable_operator_write_node->name;
5371-
pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, constant_id, 0);
5375+
pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, constant_id, local_variable_operator_write_node->depth);
53725376

53735377
ADD_GETLOCAL(ret, &dummy_line_node, local_index.index, local_index.level);
53745378

@@ -5394,7 +5398,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
53945398
ADD_INSNL(ret, &dummy_line_node, branchunless, set_label);
53955399

53965400
pm_constant_id_t constant_id = local_variable_or_write_node->name;
5397-
pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, constant_id, 0);
5401+
pm_local_index_t local_index = pm_lookup_local_index(iseq, scope_node, constant_id, local_variable_or_write_node->depth);
53985402
ADD_GETLOCAL(ret, &dummy_line_node, local_index.index, local_index.level);
53995403

54005404
PM_DUP_UNLESS_POPPED;
@@ -5417,7 +5421,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
54175421
pm_local_variable_read_node_t *local_read_node = (pm_local_variable_read_node_t *) node;
54185422

54195423
if (!popped) {
5420-
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_read_node->name, 0);
5424+
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_read_node->name, local_read_node->depth);
54215425
ADD_GETLOCAL(ret, &dummy_line_node, index.index, index.level);
54225426
}
54235427
return;
@@ -5426,7 +5430,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
54265430
pm_local_variable_target_node_t *local_write_node = (pm_local_variable_target_node_t *) node;
54275431

54285432
pm_constant_id_t constant_id = local_write_node->name;
5429-
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, constant_id, 0);
5433+
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, constant_id, local_write_node->depth);
54305434

54315435
ADD_SETLOCAL(ret, &dummy_line_node, index.index, index.level);
54325436
return;
@@ -5439,7 +5443,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
54395443

54405444
pm_constant_id_t constant_id = local_write_node->name;
54415445

5442-
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, constant_id, 0);
5446+
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, constant_id, local_write_node->depth);
54435447

54445448
ADD_SETLOCAL(ret, &dummy_line_node, index.index, index.level);
54455449
return;
@@ -5580,7 +5584,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
55805584
assert(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
55815585

55825586
pm_local_variable_target_node_t *local_target = (pm_local_variable_target_node_t *) target;
5583-
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, 0);
5587+
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, local_target->depth);
55845588

55855589
ADD_INSN1(ret, &dummy_line_node, putobject, rb_id2sym(pm_constant_id_lookup(scope_node, local_target->name)));
55865590
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,
55975601
assert(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
55985602

55995603
pm_local_variable_target_node_t *local_target = (pm_local_variable_target_node_t *) target;
5600-
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, 0);
5604+
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, local_target->depth);
56015605

56025606
if (((size_t) targets_index) < (targets_count - 1)) {
56035607
PM_DUP;
@@ -5620,7 +5624,7 @@ pm_compile_node(rb_iseq_t *iseq, const pm_node_t *node, LINK_ANCHOR *const ret,
56205624
assert(PM_NODE_TYPE_P(target, PM_LOCAL_VARIABLE_TARGET_NODE));
56215625

56225626
pm_local_variable_target_node_t *local_target = (pm_local_variable_target_node_t *) target;
5623-
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, 0);
5627+
pm_local_index_t index = pm_lookup_local_index(iseq, scope_node, local_target->name, local_target->depth);
56245628

56255629
PM_PUTNIL;
56265630
ADD_SETLOCAL(ret, &dummy_line_node, index.index, index.level);

0 commit comments

Comments
 (0)