Skip to content

Commit c5176b5

Browse files
committed
Nil var before loop
Signed-off-by: Stefan Marr <[email protected]>
1 parent 11ab84b commit c5176b5

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

src/som/compiler/bc/method_generation_context.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -770,21 +770,23 @@ def inline_to_do(self, parser):
770770
block_literal_idx = self._bytecode[-1]
771771

772772
to_be_inlined = self._literals[block_literal_idx]
773+
to_be_inlined.merge_scope_into(self)
774+
775+
block_arg = to_be_inlined.get_argument(1, 0)
776+
i_var_idx = self.get_inlined_local_idx(block_arg, 0)
773777

774778
self._remove_last_bytecodes(1) # remove push_block*
775779

776780
self._is_currently_inlining_a_block = True
777781
emit_dup_second(self)
778782

783+
emit_nil_local(self, i_var_idx)
784+
779785
loop_begin_idx = self.offset_of_next_instruction()
780786
jump_offset_idx_to_end = emit_jump_if_greater_with_dummy_offset(self)
781787

782788
emit_dup(self)
783789

784-
to_be_inlined.merge_scope_into(self)
785-
786-
block_arg = to_be_inlined.get_argument(1, 0)
787-
i_var_idx = self.get_inlined_local_idx(block_arg, 0)
788790
emit_pop_local(self, i_var_idx, 0)
789791

790792
to_be_inlined.inline(self, False)

tests/test_bytecode_generation.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -1382,13 +1382,14 @@ def test_field_read_inlining(cgenc, mgenc):
13821382
def test_inlining_of_to_do(mgenc):
13831383
bytecodes = method_to_bytecodes(mgenc, "test = ( 1 to: 2 do: [:i | i ] )")
13841384

1385-
assert len(bytecodes) == 21
1385+
assert len(bytecodes) == 23
13861386
check(
13871387
bytecodes,
13881388
[
13891389
Bytecodes.push_1,
13901390
Bytecodes.push_constant_0,
13911391
Bytecodes.dup_second, # stack: Top[1, 2, 1]
1392+
Bytecodes.nil_local,
13921393
BC(Bytecodes.jump_if_greater, 17), # consume only on jump
13931394
Bytecodes.dup,
13941395
BC(
@@ -1424,13 +1425,14 @@ def test_to_do_block_block_inlined_self(cgenc, mgenc):
14241425
)""",
14251426
)
14261427

1427-
assert len(bytecodes) == 25
1428+
assert len(bytecodes) == 27
14281429
check(
14291430
bytecodes,
14301431
[
14311432
Bytecodes.push_1,
14321433
Bytecodes.push_constant_0,
14331434
Bytecodes.dup_second,
1435+
Bytecodes.nil_local,
14341436
BC(Bytecodes.jump_if_greater, 21),
14351437
Bytecodes.dup,
14361438
BC(Bytecodes.pop_local, 2, 0),

0 commit comments

Comments
 (0)