Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5dcd672

Browse files
committed
Use label objects instead of symbols
1 parent 42a15a7 commit 5dcd672

File tree

5 files changed

+115
-62
lines changed

5 files changed

+115
-62
lines changed

lib/syntax_tree/yarv/bf.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ def input_char(iseq)
153153

154154
# unless $tape[$cursor] == 0
155155
def loop_start(iseq)
156-
start_label = iseq.label
156+
start_label = iseq.label_at_index
157157

158158
iseq.getglobal(:$tape)
159159
iseq.getglobal(:$cursor)

lib/syntax_tree/yarv/compiler.rb

Lines changed: 49 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,8 @@ def visit_aryptn(node)
417417
# First, check if the #deconstruct cache is nil. If it is, we're going
418418
# to call #deconstruct on the object and cache the result.
419419
iseq.topn(2)
420-
branchnil = iseq.branchnil(-1)
420+
deconstruct_label = iseq.label
421+
iseq.branchnil(deconstruct_label)
421422

422423
# Next, ensure that the cached value was cached correctly, otherwise
423424
# fail the match.
@@ -432,7 +433,7 @@ def visit_aryptn(node)
432433

433434
# Check if the object responds to #deconstruct, fail the match
434435
# otherwise.
435-
branchnil.patch!(iseq)
436+
iseq.event(deconstruct_label)
436437
iseq.dup
437438
iseq.putobject(:deconstruct)
438439
iseq.send(YARV.calldata(:respond_to?, 1))
@@ -634,11 +635,12 @@ def visit_binary(node)
634635
visit(node.left)
635636
iseq.dup
636637

637-
branchif = iseq.branchif(-1)
638+
skip_right_label = iseq.label
639+
iseq.branchif(skip_right_label)
638640
iseq.pop
639641

640642
visit(node.right)
641-
branchif.patch!(iseq)
643+
iseq.push(skip_right_label)
642644
else
643645
visit(node.left)
644646
visit(node.right)
@@ -758,11 +760,12 @@ def visit_call(node)
758760
iseq.putself
759761
end
760762

761-
branchnil =
762-
if node.operator&.value == "&."
763-
iseq.dup
764-
iseq.branchnil(-1)
765-
end
763+
after_call_label = nil
764+
if node.operator&.value == "&."
765+
iseq.dup
766+
after_call_label = iseq.label
767+
iseq.branchnil(after_call_label)
768+
end
766769

767770
flag = 0
768771

@@ -815,7 +818,7 @@ def visit_call(node)
815818
YARV.calldata(node.message.value.to_sym, argc, flag),
816819
block_iseq
817820
)
818-
branchnil.patch!(iseq) if branchnil
821+
iseq.event(after_call_label) if after_call_label
819822
end
820823

821824
def visit_case(node)
@@ -845,16 +848,19 @@ def visit_case(node)
845848
CallData::CALL_FCALL | CallData::CALL_ARGS_SIMPLE
846849
)
847850
)
848-
[clause, iseq.branchif(:label_00)]
851+
852+
label = iseq.label
853+
iseq.branchif(label)
854+
[clause, label]
849855
end
850856

851857
iseq.pop
852858
else_clause ? visit(else_clause) : iseq.putnil
853859
iseq.leave
854860

855-
branches.each_with_index do |(clause, branchif), index|
861+
branches.each_with_index do |(clause, label), index|
856862
iseq.leave if index != 0
857-
branchif.patch!(iseq)
863+
iseq.push(label)
858864
iseq.pop
859865
visit(clause)
860866
end
@@ -1100,26 +1106,28 @@ def visit_heredoc(node)
11001106

11011107
def visit_if(node)
11021108
if node.predicate.is_a?(RangeNode)
1109+
true_label = iseq.label
1110+
11031111
iseq.getspecial(GetSpecial::SVAR_FLIPFLOP_START, 0)
1104-
branchif = iseq.branchif(-1)
1112+
iseq.branchif(true_label)
11051113

11061114
visit(node.predicate.left)
1107-
branchunless_true = iseq.branchunless(-1)
1115+
end_branch = iseq.branchunless(-1)
11081116

11091117
iseq.putobject(true)
11101118
iseq.setspecial(GetSpecial::SVAR_FLIPFLOP_START)
1111-
branchif.patch!(iseq)
11121119

1120+
iseq.push(true_label)
11131121
visit(node.predicate.right)
1114-
branchunless_false = iseq.branchunless(-1)
1122+
false_branch = iseq.branchunless(-1)
11151123

11161124
iseq.putobject(false)
11171125
iseq.setspecial(GetSpecial::SVAR_FLIPFLOP_START)
1118-
branchunless_false.patch!(iseq)
11191126

1127+
false_branch.patch!(iseq)
11201128
visit(node.statements)
11211129
iseq.leave
1122-
branchunless_true.patch!(iseq)
1130+
end_branch.patch!(iseq)
11231131
iseq.putnil
11241132
else
11251133
visit(node.predicate)
@@ -1317,22 +1325,22 @@ def visit_opassign(node)
13171325
[Const, CVar, GVar].include?(node.target.value.class)
13181326
opassign_defined(node)
13191327
else
1320-
branchif = nil
1328+
skip_value_label = iseq.label
13211329

13221330
with_opassign(node) do
13231331
iseq.dup
1324-
branchif = iseq.branchif(-1)
1332+
iseq.branchif(skip_value_label)
13251333
iseq.pop
13261334
visit(node.value)
13271335
end
13281336

13291337
if node.target.is_a?(ARefField)
13301338
iseq.leave
1331-
branchif.patch!(iseq)
1339+
iseq.push(skip_value_label)
13321340
iseq.setn(3)
13331341
iseq.adjuststack(3)
13341342
else
1335-
branchif.patch!(iseq)
1343+
iseq.push(skip_value_label)
13361344
end
13371345
end
13381346
else
@@ -1363,13 +1371,11 @@ def visit_params(node)
13631371
iseq.local_table.plain(name)
13641372
iseq.argument_size += 1
13651373

1366-
argument_options[:opt] = [iseq.label] unless argument_options.key?(
1367-
:opt
1368-
)
1374+
argument_options[:opt] = [iseq.label_at_index] unless argument_options.key?(:opt)
13691375

13701376
visit(value)
13711377
iseq.setlocal(index, 0)
1372-
iseq.argument_options[:opt] << iseq.label
1378+
iseq.argument_options[:opt] << iseq.label_at_index
13731379
end
13741380

13751381
visit(node.rest) if node.rest
@@ -1406,12 +1412,14 @@ def visit_params(node)
14061412
elsif (compiled = RubyVisitor.compile(value))
14071413
argument_options[:keyword] << [name, compiled]
14081414
else
1415+
skip_value_label = iseq.label
1416+
14091417
argument_options[:keyword] << [name]
14101418
iseq.checkkeyword(keyword_bits_index, keyword_index)
1411-
branchif = iseq.branchif(-1)
1419+
iseq.branchif(skip_value_label)
14121420
visit(value)
14131421
iseq.setlocal(index, 0)
1414-
branchif.patch!(iseq)
1422+
iseq.push(skip_value_label)
14151423
end
14161424
end
14171425

@@ -1558,13 +1566,15 @@ def visit_rassign(node)
15581566
jumps_to_match.concat(visit(node.pattern))
15591567
end
15601568

1569+
no_key_label = iseq.label
1570+
15611571
# First we're going to push the core onto the stack, then we'll check
15621572
# if the value to match is truthy. If it is, we'll jump down to raise
15631573
# NoMatchingPatternKeyError. Otherwise we'll raise
15641574
# NoMatchingPatternError.
15651575
iseq.putspecialobject(PutSpecialObject::OBJECT_VMCORE)
15661576
iseq.topn(4)
1567-
branchif_no_key = iseq.branchif(-1)
1577+
iseq.branchif(no_key_label)
15681578

15691579
# Here we're going to raise NoMatchingPatternError.
15701580
iseq.putobject(NoMatchingPatternError)
@@ -1577,7 +1587,7 @@ def visit_rassign(node)
15771587
jump_to_exit = iseq.jump(-1)
15781588

15791589
# Here we're going to raise NoMatchingPatternKeyError.
1580-
branchif_no_key.patch!(iseq)
1590+
iseq.push(no_key_label)
15811591
iseq.putobject(NoMatchingPatternKeyError)
15821592
iseq.putspecialobject(PutSpecialObject::OBJECT_VMCORE)
15831593
iseq.putobject("%p: %s")
@@ -1797,7 +1807,7 @@ def visit_unless(node)
17971807
jump = iseq.jump(-1)
17981808
branchunless.patch!(iseq)
17991809
visit(node.consequent)
1800-
jump.patch!(iseq.label)
1810+
jump.patch!(iseq.label_at_index)
18011811
else
18021812
branchunless.patch!(iseq)
18031813
end
@@ -1812,7 +1822,7 @@ def visit_until(node)
18121822
iseq.pop
18131823
jumps << iseq.jump(-1)
18141824

1815-
label = iseq.label
1825+
label = iseq.label_at_index
18161826
visit(node.statements)
18171827
iseq.pop
18181828
jumps.each { |jump| jump.patch!(iseq) }
@@ -1891,20 +1901,21 @@ def visit_when(node)
18911901
end
18921902

18931903
def visit_while(node)
1904+
repeat_label = iseq.label
18941905
jumps = []
18951906

18961907
jumps << iseq.jump(-1)
18971908
iseq.putnil
18981909
iseq.pop
18991910
jumps << iseq.jump(-1)
19001911

1901-
label = iseq.label
1912+
iseq.push(repeat_label)
19021913
visit(node.statements)
19031914
iseq.pop
19041915
jumps.each { |jump| jump.patch!(iseq) }
19051916

19061917
visit(node.predicate)
1907-
iseq.branchif(label)
1918+
iseq.branchif(repeat_label)
19081919
iseq.putnil if last_statement?
19091920
end
19101921

@@ -2060,7 +2071,8 @@ def opassign_defined(node)
20602071
end
20612072

20622073
iseq.dup
2063-
branchif = iseq.branchif(-1)
2074+
skip_value_label = iseq.label
2075+
iseq.branchif(skip_value_label)
20642076
iseq.pop
20652077

20662078
branchunless.patch!(iseq)
@@ -2085,7 +2097,7 @@ def opassign_defined(node)
20852097
end
20862098
end
20872099

2088-
branchif.patch!(iseq)
2100+
iseq.push(skip_value_label)
20892101
end
20902102

20912103
# Whenever a value is interpolated into a string-like structure, these

0 commit comments

Comments
 (0)