Skip to content

Commit 63065f4

Browse files
committed
Nil var before loop
Signed-off-by: Stefan Marr <[email protected]>
1 parent ed2f61f commit 63065f4

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/som/compiler/bc/method_generation_context.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -685,21 +685,23 @@ def inline_to_do(self, parser):
685685
block_literal_idx = self._bytecode[-1]
686686

687687
to_be_inlined = self._literals[block_literal_idx]
688+
to_be_inlined.merge_scope_into(self)
689+
690+
block_arg = to_be_inlined.get_argument(1, 0)
691+
i_var_idx = self.get_inlined_local_idx(block_arg, 0)
688692

689693
self._remove_last_bytecodes(1) # remove push_block*
690694

691695
self._is_currently_inlining_a_block = True
692696
emit_dup_second(self)
693697

698+
emit_nil_local(self, i_var_idx)
699+
694700
loop_begin_idx = self.offset_of_next_instruction()
695701
jump_offset_idx_to_end = emit_jump_if_greater_with_dummy_offset(self)
696702

697703
emit_dup(self)
698704

699-
to_be_inlined.merge_scope_into(self)
700-
701-
block_arg = to_be_inlined.get_argument(1, 0)
702-
i_var_idx = self.get_inlined_local_idx(block_arg, 0)
703705
emit_pop_local(self, i_var_idx, 0)
704706

705707
to_be_inlined.inline(self, False)

tests/test_bytecode_generation.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,13 +1172,14 @@ def test_field_read_inlining(cgenc, mgenc):
11721172
def test_inlining_of_to_do(mgenc):
11731173
bytecodes = method_to_bytecodes(mgenc, "test = ( 1 to: 2 do: [:i | i ] )")
11741174

1175-
assert len(bytecodes) == 21
1175+
assert len(bytecodes) == 23
11761176
check(
11771177
bytecodes,
11781178
[
11791179
Bytecodes.push_1,
11801180
Bytecodes.push_constant_0,
11811181
Bytecodes.dup_second, # stack: Top[1, 2, 1]
1182+
Bytecodes.nil_local,
11821183
BC(Bytecodes.jump_if_greater, 17), # consume only on jump
11831184
Bytecodes.dup,
11841185
BC(
@@ -1214,13 +1215,14 @@ def test_to_do_block_block_inlined_self(cgenc, mgenc):
12141215
)""",
12151216
)
12161217

1217-
assert len(bytecodes) == 25
1218+
assert len(bytecodes) == 27
12181219
check(
12191220
bytecodes,
12201221
[
12211222
Bytecodes.push_1,
12221223
Bytecodes.push_constant_0,
12231224
Bytecodes.dup_second,
1225+
Bytecodes.nil_local,
12241226
BC(Bytecodes.jump_if_greater, 21),
12251227
Bytecodes.dup,
12261228
BC(Bytecodes.pop_local, 2, 0),

0 commit comments

Comments
 (0)