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

Commit a1236fd

Browse files
committed
Default to VM_CALL_ARGS_SIMPLE
1 parent 6c6b437 commit a1236fd

File tree

6 files changed

+88
-74
lines changed

6 files changed

+88
-74
lines changed

lib/syntax_tree/compiler.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,13 @@ def visit_alias(node)
253253
iseq.putspecialobject(YARV::VM_SPECIAL_OBJECT_CBASE)
254254
visit(node.left)
255255
visit(node.right)
256-
iseq.send(:"core#set_method_alias", 3, YARV::VM_CALL_ARGS_SIMPLE)
256+
iseq.send(:"core#set_method_alias", 3)
257257
end
258258

259259
def visit_aref(node)
260260
visit(node.collection)
261261
visit(node.index)
262-
iseq.send(:[], 1, YARV::VM_CALL_ARGS_SIMPLE)
262+
iseq.send(:[], 1)
263263
end
264264

265265
def visit_arg_block(node)
@@ -313,7 +313,7 @@ def visit_assign(node)
313313
visit(node.target.index)
314314
visit(node.value)
315315
iseq.setn(3)
316-
iseq.send(:[]=, 2, YARV::VM_CALL_ARGS_SIMPLE)
316+
iseq.send(:[]=, 2)
317317
iseq.pop
318318
when ConstPathField
319319
names = constant_names(node.target)
@@ -337,7 +337,7 @@ def visit_assign(node)
337337
visit(node.target)
338338
visit(node.value)
339339
iseq.setn(2)
340-
iseq.send(:"#{node.target.name.value}=", 1, YARV::VM_CALL_ARGS_SIMPLE)
340+
iseq.send(:"#{node.target.name.value}=", 1)
341341
iseq.pop
342342
when TopConstField
343343
name = node.target.constant.value.to_sym
@@ -420,7 +420,7 @@ def visit_binary(node)
420420
else
421421
visit(node.left)
422422
visit(node.right)
423-
iseq.send(node.operator, 1, YARV::VM_CALL_ARGS_SIMPLE)
423+
iseq.send(node.operator, 1)
424424
end
425425
end
426426

@@ -981,7 +981,7 @@ def visit_mrhs(node)
981981

982982
def visit_not(node)
983983
visit(node.statement)
984-
iseq.send(:!, 0, YARV::VM_CALL_ARGS_SIMPLE)
984+
iseq.send(:!, 0)
985985
end
986986

987987
def visit_opassign(node)
@@ -1367,7 +1367,7 @@ def visit_undef(node)
13671367
iseq.putspecialobject(YARV::VM_SPECIAL_OBJECT_VMCORE)
13681368
iseq.putspecialobject(YARV::VM_SPECIAL_OBJECT_CBASE)
13691369
visit(symbol)
1370-
iseq.send(:"core#undef_method", 2, YARV::VM_CALL_ARGS_SIMPLE)
1370+
iseq.send(:"core#undef_method", 2)
13711371
end
13721372
end
13731373

@@ -1523,7 +1523,7 @@ def visit_xstring_literal(node)
15231523
def visit_yield(node)
15241524
parts = argument_parts(node.arguments)
15251525
visit_all(parts)
1526-
iseq.invokeblock(nil, parts.length, YARV::VM_CALL_ARGS_SIMPLE)
1526+
iseq.invokeblock(nil, parts.length)
15271527
end
15281528

15291529
def visit_zsuper(_node)
@@ -1759,12 +1759,12 @@ def with_opassign(node)
17591759
visit(node.target.index)
17601760

17611761
iseq.dupn(2)
1762-
iseq.send(:[], 1, YARV::VM_CALL_ARGS_SIMPLE)
1762+
iseq.send(:[], 1)
17631763

17641764
yield
17651765

17661766
iseq.setn(3)
1767-
iseq.send(:[]=, 2, YARV::VM_CALL_ARGS_SIMPLE)
1767+
iseq.send(:[]=, 2)
17681768
iseq.pop
17691769
when ConstPathField
17701770
name = node.target.constant.value.to_sym

lib/syntax_tree/dsl.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ def BodyStmt(statements, rescue_clause, else_keyword, else_clause, ensure_clause
3838
BodyStmt.new(statements: statements, rescue_clause: rescue_clause, else_keyword: else_keyword, else_clause: else_clause, ensure_clause: ensure_clause, location: Location.default)
3939
end
4040

41+
def Break(arguments)
42+
Break.new(arguments: arguments, location: Location.default)
43+
end
44+
4145
def CallNode(receiver, operator, message, arguments)
4246
CallNode.new(receiver: receiver, operator: operator, message: message, arguments: arguments, location: Location.default)
4347
end

lib/syntax_tree/yarv.rb

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ def intern
460460
push([:intern])
461461
end
462462

463-
def invokeblock(method_id, argc, flag)
463+
def invokeblock(method_id, argc, flag = VM_CALL_ARGS_SIMPLE)
464464
stack.change_by(-argc + 1)
465465
push([:invokeblock, call_data(method_id, argc, flag)])
466466
end
@@ -547,7 +547,7 @@ def opt_newarray_max(length)
547547
push([:opt_newarray_max, length])
548548
else
549549
newarray(length)
550-
send(:max, 0, VM_CALL_ARGS_SIMPLE)
550+
send(:max, 0)
551551
end
552552
end
553553

@@ -557,7 +557,7 @@ def opt_newarray_min(length)
557557
push([:opt_newarray_min, length])
558558
else
559559
newarray(length)
560-
send(:min, 0, VM_CALL_ARGS_SIMPLE)
560+
send(:min, 0)
561561
end
562562
end
563563

@@ -569,22 +569,20 @@ def opt_setinlinecache(inline_storage)
569569
def opt_str_freeze(value)
570570
if specialized_instruction
571571
stack.change_by(+1)
572-
push(
573-
[:opt_str_freeze, value, call_data(:freeze, 0, VM_CALL_ARGS_SIMPLE)]
574-
)
572+
push([:opt_str_freeze, value, call_data(:freeze, 0)])
575573
else
576574
putstring(value)
577-
send(:freeze, 0, VM_CALL_ARGS_SIMPLE)
575+
send(:freeze, 0)
578576
end
579577
end
580578

581579
def opt_str_uminus(value)
582580
if specialized_instruction
583581
stack.change_by(+1)
584-
push([:opt_str_uminus, value, call_data(:-@, 0, VM_CALL_ARGS_SIMPLE)])
582+
push([:opt_str_uminus, value, call_data(:-@, 0)])
585583
else
586584
putstring(value)
587-
send(:-@, 0, VM_CALL_ARGS_SIMPLE)
585+
send(:-@, 0)
588586
end
589587
end
590588

@@ -633,7 +631,7 @@ def putstring(object)
633631
push([:putstring, object])
634632
end
635633

636-
def send(method_id, argc, flag, block_iseq = nil)
634+
def send(method_id, argc, flag = VM_CALL_ARGS_SIMPLE, block_iseq = nil)
637635
stack.change_by(-(argc + 1) + 1)
638636
cdata = call_data(method_id, argc, flag)
639637

@@ -669,8 +667,7 @@ def send(method_id, argc, flag, block_iseq = nil)
669667
when [:|, 1] then push([:opt_or, cdata])
670668
when [:[]=, 2] then push([:opt_aset, cdata])
671669
when [:!=, 1]
672-
eql_data = call_data(:==, 1, VM_CALL_ARGS_SIMPLE)
673-
push([:opt_neq, eql_data, cdata])
670+
push([:opt_neq, call_data(:==, 1), cdata])
674671
else
675672
push([:opt_send_without_block, cdata])
676673
end
@@ -762,7 +759,7 @@ def toregexp(options, length)
762759

763760
# This creates a call data object that is used as the operand for the
764761
# send, invokesuper, and objtostring instructions.
765-
def call_data(method_id, argc, flag)
762+
def call_data(method_id, argc, flag = VM_CALL_ARGS_SIMPLE)
766763
{ mid: method_id, flag: flag, orig_argc: argc }
767764
end
768765

lib/syntax_tree/yarv/bf.rb

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,9 @@ def visit_input(node)
260260
iseq.getglobal(:$tape)
261261
iseq.getglobal(:$cursor)
262262
iseq.getglobal(:$stdin)
263-
iseq.send(:getc, 0, VM_CALL_ARGS_SIMPLE)
264-
iseq.send(:ord, 0, VM_CALL_ARGS_SIMPLE)
265-
iseq.send(:[]=, 2, VM_CALL_ARGS_SIMPLE)
263+
iseq.send(:getc, 0)
264+
iseq.send(:ord, 0)
265+
iseq.send(:[]=, 2)
266266
end
267267

268268
def visit_loop(node)
@@ -273,9 +273,9 @@ def visit_loop(node)
273273
# the loop.
274274
iseq.getglobal(:$tape)
275275
iseq.getglobal(:$cursor)
276-
iseq.send(:[], 1, VM_CALL_ARGS_SIMPLE)
276+
iseq.send(:[], 1)
277277
iseq.putobject(0)
278-
iseq.send(:==, 1, VM_CALL_ARGS_SIMPLE)
278+
iseq.send(:==, 1)
279279
branchunless = iseq.branchunless(-1)
280280

281281
# Otherwise, here we'll execute the loop.
@@ -294,25 +294,24 @@ def visit_output(node)
294294
iseq.getglobal(:$stdout)
295295
iseq.getglobal(:$tape)
296296
iseq.getglobal(:$cursor)
297-
iseq.send(:[], 1, VM_CALL_ARGS_SIMPLE)
298-
iseq.send(:chr, 0, VM_CALL_ARGS_SIMPLE)
299-
iseq.send(:putc, 1, VM_CALL_ARGS_SIMPLE)
297+
iseq.send(:[], 1)
298+
iseq.send(:chr, 0)
299+
iseq.send(:putc, 1)
300300
end
301301

302302
def visit_root(node)
303303
iseq.duphash({ 0 => 0 })
304304
iseq.setglobal(:$tape)
305305
iseq.getglobal(:$tape)
306306
iseq.putobject(0)
307-
iseq.send(:default=, 1, VM_CALL_ARGS_SIMPLE)
307+
iseq.send(:default=, 1)
308308

309309
iseq.putobject(0)
310310
iseq.setglobal(:$cursor)
311311

312312
visit_nodes(node.nodes)
313313

314-
iseq.putself
315-
iseq.send(:exit, 0, VM_CALL_ARGS_SIMPLE)
314+
iseq.leave
316315
iseq
317316
end
318317

@@ -331,28 +330,28 @@ def change_by(value)
331330
iseq.getglobal(:$cursor)
332331
iseq.getglobal(:$tape)
333332
iseq.getglobal(:$cursor)
334-
iseq.send(:[], 1, VM_CALL_ARGS_SIMPLE)
333+
iseq.send(:[], 1)
335334

336335
if value < 0
337336
iseq.putobject(-value)
338-
iseq.send(:-, 1, VM_CALL_ARGS_SIMPLE)
337+
iseq.send(:-, 1)
339338
else
340339
iseq.putobject(value)
341-
iseq.send(:+, 1, VM_CALL_ARGS_SIMPLE)
340+
iseq.send(:+, 1)
342341
end
343342

344-
iseq.send(:[]=, 2, VM_CALL_ARGS_SIMPLE)
343+
iseq.send(:[]=, 2)
345344
end
346345

347346
def shift_by(value)
348347
iseq.getglobal(:$cursor)
349348

350349
if value < 0
351350
iseq.putobject(-value)
352-
iseq.send(:-, 1, VM_CALL_ARGS_SIMPLE)
351+
iseq.send(:-, 1)
353352
else
354353
iseq.putobject(value)
355-
iseq.send(:+, 1, VM_CALL_ARGS_SIMPLE)
354+
iseq.send(:+, 1)
356355
end
357356

358357
iseq.setglobal(:$cursor)

lib/syntax_tree/yarv/disassembler.rb

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def initialize(iseq)
1717
end
1818

1919
def to_ruby
20-
Program(Statements(disassemble(iseq.insns)))
20+
Program(disassemble(iseq))
2121
end
2222

2323
private
@@ -31,12 +31,12 @@ def node_for(value)
3131
end
3232
end
3333

34-
def disassemble(insns)
34+
def disassemble(iseq)
3535
label = :label_0
3636
clauses = {}
3737
clause = []
3838

39-
insns.each do |insn|
39+
iseq.insns.each do |insn|
4040
if insn.is_a?(Symbol) && insn.start_with?("label_")
4141
clause << Assign(label_field, node_for(insn)) unless clause.last.is_a?(Next)
4242
clauses[label] = clause
@@ -61,7 +61,8 @@ def disassemble(insns)
6161
clause << Assign(label_field, node_for(insn[1]))
6262
clause << Next(Args([]))
6363
when :leave
64-
clause << ReturnNode(Args([clause.pop]))
64+
value = Args([clause.pop])
65+
clause << (iseq.type == :top ? Break(value) : ReturnNode(value))
6566
when :opt_and
6667
left, right = clause.pop(2)
6768
clause << Binary(left, :&, right)
@@ -116,14 +117,27 @@ def disassemble(insns)
116117
left, right = clause.pop(2)
117118
clause << Binary(left, :+, right)
118119
when :opt_send_without_block
119-
if insn[1][:orig_argc] == 0
120-
clause << CallNode(clause.pop, Period("."), Ident(insn[1][:mid]), nil)
121-
elsif insn[1][:orig_argc] == 1 && insn[1][:mid].end_with?("=")
122-
receiver, argument = clause.pop(2)
123-
clause << Assign(CallNode(receiver, Period("."), Ident(insn[1][:mid][0..-2]), nil), argument)
120+
if insn[1][:flag] & VM_CALL_FCALL > 0
121+
if insn[1][:orig_argc] == 0
122+
clause.pop
123+
clause << CallNode(nil, nil, Ident(insn[1][:mid]), Args([]))
124+
elsif insn[1][:orig_argc] == 1 && insn[1][:mid].end_with?("=")
125+
_receiver, argument = clause.pop(2)
126+
clause << Assign(CallNode(nil, nil, Ident(insn[1][:mid][0..-2]), nil), argument)
127+
else
128+
_receiver, *arguments = clause.pop(insn[1][:orig_argc] + 1)
129+
clause << CallNode(nil, nil, Ident(insn[1][:mid]), ArgParen(Args(arguments)))
130+
end
124131
else
125-
receiver, *arguments = clause.pop(insn[1][:orig_argc] + 1)
126-
clause << CallNode(receiver, Period("."), Ident(insn[1][:mid]), ArgParen(Args(arguments)))
132+
if insn[1][:orig_argc] == 0
133+
clause << CallNode(clause.pop, Period("."), Ident(insn[1][:mid]), nil)
134+
elsif insn[1][:orig_argc] == 1 && insn[1][:mid].end_with?("=")
135+
receiver, argument = clause.pop(2)
136+
clause << Assign(CallNode(receiver, Period("."), Ident(insn[1][:mid][0..-2]), nil), argument)
137+
else
138+
receiver, *arguments = clause.pop(insn[1][:orig_argc] + 1)
139+
clause << CallNode(receiver, Period("."), Ident(insn[1][:mid]), ArgParen(Args(arguments)))
140+
end
127141
end
128142
when :putobject
129143
case insn[1]
@@ -166,7 +180,7 @@ def disassemble(insns)
166180
# If there's only one clause, then we don't need a case statement, and
167181
# we can just disassemble the first clause.
168182
clauses[label] = clause
169-
return clauses.values.first if clauses.size == 1
183+
return Statements(clauses.values.first) if clauses.size == 1
170184

171185
# Here we're going to build up a big case statement that will handle all
172186
# of the different labels.
@@ -196,7 +210,7 @@ def disassemble(insns)
196210
# statement.
197211
stack << Assign(label_field, node_for(:label_0))
198212
stack << MethodAddBlock(CallNode(nil, nil, Ident("loop"), Args([])), BlockNode(Kw("do"), nil, BodyStmt(Statements([switch]), nil, nil, nil, nil)))
199-
stack
213+
Statements(stack)
200214
end
201215

202216
def local_name(index, level)

0 commit comments

Comments
 (0)