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

Commit 154e75f

Browse files
committed
Put child iseq methods on iseq
1 parent 0047065 commit 154e75f

File tree

3 files changed

+69
-37
lines changed

3 files changed

+69
-37
lines changed

lib/syntax_tree/compiler.rb

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,17 @@ def visit_CHAR(node)
225225
end
226226

227227
def visit_END(node)
228-
name = "block in #{iseq.name}"
229228
once_iseq =
230-
with_instruction_sequence(:block, name, node) do
229+
with_child_iseq(iseq.block_child_iseq(node.location)) do
231230
postexe_iseq =
232-
with_instruction_sequence(:block, name, node) do
231+
with_child_iseq(iseq.block_child_iseq(node.location)) do
232+
iseq.event(:RUBY_EVENT_B_CALL)
233+
233234
*statements, last_statement = node.statements.body
234235
visit_all(statements)
235236
with_last_statement { visit(last_statement) }
237+
238+
iseq.event(:RUBY_EVENT_B_RETURN)
236239
iseq.leave
237240
end
238241

@@ -422,7 +425,7 @@ def visit_binary(node)
422425
end
423426

424427
def visit_block(node)
425-
with_instruction_sequence(:block, "block in #{iseq.name}", node) do
428+
with_child_iseq(iseq.block_child_iseq(node.location)) do
426429
iseq.event(:RUBY_EVENT_B_CALL)
427430
visit(node.block_var)
428431
visit(node.bodystmt)
@@ -606,7 +609,7 @@ def visit_case(node)
606609
def visit_class(node)
607610
name = node.constant.constant.value.to_sym
608611
class_iseq =
609-
with_instruction_sequence(:class, "<class:#{name}>", node) do
612+
with_child_iseq(iseq.class_child_iseq(name, node.location)) do
610613
iseq.event(:RUBY_EVENT_CLASS)
611614
visit(node.bodystmt)
612615
iseq.event(:RUBY_EVENT_END)
@@ -673,7 +676,7 @@ def visit_const_path_ref(node)
673676

674677
def visit_def(node)
675678
method_iseq =
676-
with_instruction_sequence(:method, node.name.value, node) do
679+
with_child_iseq(iseq.method_child_iseq(node.name.value, node.location)) do
677680
visit(node.params) if node.params
678681
iseq.event(:RUBY_EVENT_CALL)
679682
visit(node.bodystmt)
@@ -788,11 +791,7 @@ def visit_for(node)
788791
iseq.local_table.plain(name)
789792

790793
block_iseq =
791-
with_instruction_sequence(
792-
:block,
793-
"block in #{iseq.name}",
794-
node.statements
795-
) do
794+
with_child_iseq(iseq.block_child_iseq(node.statements.location)) do
796795
iseq.argument_options[:lead_num] ||= 0
797796
iseq.argument_options[:lead_num] += 1
798797
iseq.argument_options[:ambiguous_param0] = true
@@ -896,7 +895,7 @@ def visit_label(node)
896895

897896
def visit_lambda(node)
898897
lambda_iseq =
899-
with_instruction_sequence(:block, "block in #{iseq.name}", node) do
898+
with_child_iseq(iseq.block_child_iseq(node.location)) do
900899
iseq.event(:RUBY_EVENT_B_CALL)
901900
visit(node.params)
902901
visit(node.statements)
@@ -947,7 +946,7 @@ def visit_mlhs(node)
947946
def visit_module(node)
948947
name = node.constant.constant.value.to_sym
949948
module_iseq =
950-
with_instruction_sequence(:class, "<module:#{name}>", node) do
949+
with_child_iseq(iseq.module_child_iseq(name, node.location)) do
951950
iseq.event(:RUBY_EVENT_CLASS)
952951
visit(node.bodystmt)
953952
iseq.event(:RUBY_EVENT_END)
@@ -1168,7 +1167,18 @@ def visit_program(node)
11681167
end
11691168
end
11701169

1171-
with_instruction_sequence(:top, "<compiled>", node) do
1170+
top_iseq =
1171+
YARV::InstructionSequence.new(
1172+
:top,
1173+
"<compiled>",
1174+
nil,
1175+
node.location,
1176+
frozen_string_literal: frozen_string_literal,
1177+
operands_unification: operands_unification,
1178+
specialized_instruction: specialized_instruction
1179+
)
1180+
1181+
with_child_iseq(top_iseq) do
11721182
visit_all(preexes)
11731183

11741184
if statements.empty?
@@ -1231,7 +1241,7 @@ def visit_sclass(node)
12311241
iseq.putnil
12321242

12331243
singleton_iseq =
1234-
with_instruction_sequence(:class, "singleton class", node) do
1244+
with_child_iseq(iseq.singleton_class_child_iseq(node.location)) do
12351245
iseq.event(:RUBY_EVENT_CLASS)
12361246
visit(node.bodystmt)
12371247
iseq.event(:RUBY_EVENT_END)
@@ -1702,24 +1712,13 @@ def visit_string_parts(node)
17021712
# on the compiler. When we descend into a node that has its own
17031713
# instruction sequence, this method can be called to temporarily set the
17041714
# new value of the instruction sequence, yield, and then set it back.
1705-
def with_instruction_sequence(type, name, node)
1715+
def with_child_iseq(child_iseq)
17061716
parent_iseq = iseq
17071717

17081718
begin
1709-
iseq =
1710-
YARV::InstructionSequence.new(
1711-
type,
1712-
name,
1713-
parent_iseq,
1714-
node.location,
1715-
frozen_string_literal: frozen_string_literal,
1716-
operands_unification: operands_unification,
1717-
specialized_instruction: specialized_instruction
1718-
)
1719-
1720-
@iseq = iseq
1719+
@iseq = child_iseq
17211720
yield
1722-
iseq
1721+
child_iseq
17231722
ensure
17241723
@iseq = parent_iseq
17251724
end

lib/syntax_tree/yarv.rb

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,6 @@ def length
210210
end
211211
end
212212

213-
def each_child
214-
insns.each do |insn|
215-
insn[1..].each do |operand|
216-
yield operand if operand.is_a?(InstructionSequence)
217-
end
218-
end
219-
end
220-
221213
def eval
222214
compiled = to_a
223215

@@ -253,6 +245,44 @@ def to_a
253245
]
254246
end
255247

248+
##########################################################################
249+
# Child instruction sequence methods
250+
##########################################################################
251+
252+
def child_iseq(type, name, location)
253+
InstructionSequence.new(
254+
type,
255+
name,
256+
self,
257+
location,
258+
frozen_string_literal: frozen_string_literal,
259+
operands_unification: operands_unification,
260+
specialized_instruction: specialized_instruction
261+
)
262+
end
263+
264+
def block_child_iseq(location)
265+
current = self
266+
current = current.parent_iseq while current.type == :block
267+
child_iseq(:block, "block in #{current.name}", location)
268+
end
269+
270+
def class_child_iseq(name, location)
271+
child_iseq(:class, "<class:#{name}>", location)
272+
end
273+
274+
def method_child_iseq(name, location)
275+
child_iseq(:method, name, location)
276+
end
277+
278+
def module_child_iseq(name, location)
279+
child_iseq(:class, "<module:#{name}>", location)
280+
end
281+
282+
def singleton_class_child_iseq(location)
283+
child_iseq(:class, "singleton class", location)
284+
end
285+
256286
##########################################################################
257287
# Instruction push methods
258288
##########################################################################

test/compiler_test.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
module SyntaxTree
77
class CompilerTest < Minitest::Test
88
CASES = [
9+
# Hooks
10+
"BEGIN { a = 1 }",
11+
"a = 1; END { a = 1 }; a",
912
# Various literals placed on the stack
1013
"true",
1114
"false",

0 commit comments

Comments
 (0)