Class: SyntaxTree::YARV::InstructionSequence

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree/yarv/instruction_sequence.rb

Overview

This class is meant to mirror RubyVM::InstructionSequence. It contains a list of instructions along with the metadata pertaining to them. It also functions as a builder for the instruction sequence.

Defined Under Namespace

Classes: CatchBreak, CatchEnsure, CatchEntry, CatchNext, CatchRedo, CatchRescue, CatchRetry, InstructionList, Label, Stack

Constant Summary collapse

MAGIC =
"YARVInstructionSequence/SimpleDataFormat"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, file, line, type, parent_iseq = nil, options = Compiler::Options.new) ⇒ InstructionSequence

Returns a new instance of InstructionSequence.



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 168

def initialize(
  name,
  file,
  line,
  type,
  parent_iseq = nil,
  options = Compiler::Options.new
)
  @name = name
  @file = file
  @line = line
  @type = type
  @parent_iseq = parent_iseq

  @argument_size = 0
  @argument_options = {}
  @catch_table = []

  @local_table = LocalTable.new
  @inline_storages = {}
  @insns = InstructionList.new
  @storage_index = 0
  @stack = Stack.new

  @options = options
end

Instance Attribute Details

#argument_optionsObject (readonly)

Returns the value of attribute argument_options.



143
144
145
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 143

def argument_options
  @argument_options
end

#argument_sizeObject

This is the list of information about the arguments to this instruction sequence.



142
143
144
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 142

def argument_size
  @argument_size
end

#catch_tableObject (readonly)

The catch table for this instruction sequence.



146
147
148
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 146

def catch_table
  @catch_table
end

#fileObject (readonly)

The source location of the instruction sequence.



132
133
134
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 132

def file
  @file
end

#inline_storagesObject (readonly)

The hash of names of instance and class variables pointing to the index of their associated inline storage.



156
157
158
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 156

def inline_storages
  @inline_storages
end

#insnsObject (readonly)

The list of instructions for this instruction sequence.



149
150
151
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 149

def insns
  @insns
end

#lineObject (readonly)

The source location of the instruction sequence.



132
133
134
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 132

def line
  @line
end

#local_tableObject (readonly)

The table of local variables.



152
153
154
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 152

def local_table
  @local_table
end

#nameObject (readonly)

The name of the instruction sequence.



129
130
131
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 129

def name
  @name
end

#optionsObject (readonly)

These are various compilation options provided.



166
167
168
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 166

def options
  @options
end

#parent_iseqObject (readonly)

The parent instruction sequence, if there is one.



138
139
140
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 138

def parent_iseq
  @parent_iseq
end

#stackObject (readonly)

An object that will track the current size of the stack and the maximum size of the stack for this instruction sequence.



163
164
165
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 163

def stack
  @stack
end

#storage_indexObject (readonly)

The index of the next inline storage that will be created.



159
160
161
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 159

def storage_index
  @storage_index
end

#typeObject (readonly)

The type of the instruction sequence.



135
136
137
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 135

def type
  @type
end

Class Method Details

.from(source, options = Compiler::Options.new, parent_iseq = nil) ⇒ Object

This method will create a new instruction sequence from a serialized RubyVM::InstructionSequence object.



999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 999

def self.from(source, options = Compiler::Options.new, parent_iseq = nil)
  iseq =
    new(source[5], source[6], source[8], source[9], parent_iseq, options)

  # set up the labels object so that the labels are shared between the
  # location in the instruction sequence and the instructions that
  # reference them
  labels = Hash.new { |hash, name| hash[name] = Label.new(name) }

  # set up the correct argument size
  iseq.argument_size = source[4][:arg_size]

  # set up all of the locals
  source[10].each { |local| iseq.local_table.plain(local) }

  # set up the argument options
  iseq.argument_options.merge!(source[11])
  if iseq.argument_options[:opt]
    iseq.argument_options[:opt].map! { |opt| labels[opt] }
  end

  # track the child block iseqs so that our catch table can point to the
  # correctly created iseqs
  block_iseqs = []

  # set up all of the instructions
  source[13].each do |insn|
    # add line numbers
    if insn.is_a?(Integer)
      iseq.push(insn)
      next
    end

    # add events and labels
    if insn.is_a?(Symbol)
      if insn.start_with?("label_")
        iseq.push(labels[insn])
      else
        iseq.push(insn)
      end
      next
    end

    # add instructions, mapped to our own instruction classes
    type, *opnds = insn

    case type
    when :adjuststack
      iseq.adjuststack(opnds[0])
    when :anytostring
      iseq.anytostring
    when :branchif
      iseq.branchif(labels[opnds[0]])
    when :branchnil
      iseq.branchnil(labels[opnds[0]])
    when :branchunless
      iseq.branchunless(labels[opnds[0]])
    when :checkkeyword
      iseq.checkkeyword(iseq.local_table.size - opnds[0] + 2, opnds[1])
    when :checkmatch
      iseq.checkmatch(opnds[0])
    when :checktype
      iseq.checktype(opnds[0])
    when :concatarray
      iseq.concatarray
    when :concatstrings
      iseq.concatstrings(opnds[0])
    when :defineclass
      iseq.defineclass(opnds[0], from(opnds[1], options, iseq), opnds[2])
    when :defined
      iseq.defined(opnds[0], opnds[1], opnds[2])
    when :definedivar
      iseq.definedivar(opnds[0], opnds[1], opnds[2])
    when :definemethod
      iseq.definemethod(opnds[0], from(opnds[1], options, iseq))
    when :definesmethod
      iseq.definesmethod(opnds[0], from(opnds[1], options, iseq))
    when :dup
      iseq.dup
    when :duparray
      iseq.duparray(opnds[0])
    when :duphash
      iseq.duphash(opnds[0])
    when :dupn
      iseq.dupn(opnds[0])
    when :expandarray
      iseq.expandarray(opnds[0], opnds[1])
    when :getblockparam, :getblockparamproxy, :getlocal, :getlocal_WC_0,
         :getlocal_WC_1, :setblockparam, :setlocal, :setlocal_WC_0,
         :setlocal_WC_1
      current = iseq
      level = 0

      case type
      when :getlocal_WC_1, :setlocal_WC_1
        level = 1
      when :getblockparam, :getblockparamproxy, :getlocal, :setblockparam,
           :setlocal
        level = opnds[1]
      end

      level.times { current = current.parent_iseq }
      index = current.local_table.size - opnds[0] + 2

      case type
      when :getblockparam
        iseq.getblockparam(index, level)
      when :getblockparamproxy
        iseq.getblockparamproxy(index, level)
      when :getlocal, :getlocal_WC_0, :getlocal_WC_1
        iseq.getlocal(index, level)
      when :setblockparam
        iseq.setblockparam(index, level)
      when :setlocal, :setlocal_WC_0, :setlocal_WC_1
        iseq.setlocal(index, level)
      end
    when :getclassvariable
      iseq.push(GetClassVariable.new(opnds[0], opnds[1]))
    when :getconstant
      iseq.getconstant(opnds[0])
    when :getglobal
      iseq.getglobal(opnds[0])
    when :getinstancevariable
      iseq.push(GetInstanceVariable.new(opnds[0], opnds[1]))
    when :getspecial
      iseq.getspecial(opnds[0], opnds[1])
    when :intern
      iseq.intern
    when :invokeblock
      iseq.invokeblock(CallData.from(opnds[0]))
    when :invokesuper
      block_iseq = opnds[1] ? from(opnds[1], options, iseq) : nil
      iseq.invokesuper(CallData.from(opnds[0]), block_iseq)
    when :jump
      iseq.jump(labels[opnds[0]])
    when :leave
      iseq.leave
    when :newarray
      iseq.newarray(opnds[0])
    when :newarraykwsplat
      iseq.newarraykwsplat(opnds[0])
    when :newhash
      iseq.newhash(opnds[0])
    when :newrange
      iseq.newrange(opnds[0])
    when :nop
      iseq.nop
    when :objtostring
      iseq.objtostring(CallData.from(opnds[0]))
    when :once
      iseq.once(from(opnds[0], options, iseq), opnds[1])
    when :opt_and, :opt_aref, :opt_aset, :opt_div, :opt_empty_p, :opt_eq,
         :opt_ge, :opt_gt, :opt_le, :opt_length, :opt_lt, :opt_ltlt,
         :opt_minus, :opt_mod, :opt_mult, :opt_nil_p, :opt_not, :opt_or,
         :opt_plus, :opt_regexpmatch2, :opt_send_without_block, :opt_size,
         :opt_succ
      iseq.send(CallData.from(opnds[0]), nil)
    when :opt_aref_with
      iseq.opt_aref_with(opnds[0], CallData.from(opnds[1]))
    when :opt_aset_with
      iseq.opt_aset_with(opnds[0], CallData.from(opnds[1]))
    when :opt_case_dispatch
      hash =
        opnds[0]
          .each_slice(2)
          .to_h
          .transform_values { |value| labels[value] }
      iseq.opt_case_dispatch(hash, labels[opnds[1]])
    when :opt_getconstant_path
      iseq.opt_getconstant_path(opnds[0])
    when :opt_getinlinecache
      iseq.opt_getinlinecache(labels[opnds[0]], opnds[1])
    when :opt_newarray_max
      iseq.newarray(opnds[0])
      iseq.send(YARV.calldata(:max))
    when :opt_newarray_min
      iseq.newarray(opnds[0])
      iseq.send(YARV.calldata(:min))
    when :opt_neq
      iseq.push(
        OptNEq.new(CallData.from(opnds[0]), CallData.from(opnds[1]))
      )
    when :opt_setinlinecache
      iseq.opt_setinlinecache(opnds[0])
    when :opt_str_freeze
      iseq.putstring(opnds[0])
      iseq.send(YARV.calldata(:freeze))
    when :opt_str_uminus
      iseq.putstring(opnds[0])
      iseq.send(YARV.calldata(:-@))
    when :pop
      iseq.pop
    when :putnil
      iseq.putnil
    when :putobject
      iseq.putobject(opnds[0])
    when :putobject_INT2FIX_0_
      iseq.putobject(0)
    when :putobject_INT2FIX_1_
      iseq.putobject(1)
    when :putself
      iseq.putself
    when :putstring
      iseq.putstring(opnds[0])
    when :putspecialobject
      iseq.putspecialobject(opnds[0])
    when :send
      block_iseq = opnds[1] ? from(opnds[1], options, iseq) : nil
      block_iseqs << block_iseq if block_iseq
      iseq.send(CallData.from(opnds[0]), block_iseq)
    when :setclassvariable
      iseq.push(SetClassVariable.new(opnds[0], opnds[1]))
    when :setconstant
      iseq.setconstant(opnds[0])
    when :setglobal
      iseq.setglobal(opnds[0])
    when :setinstancevariable
      iseq.push(SetInstanceVariable.new(opnds[0], opnds[1]))
    when :setn
      iseq.setn(opnds[0])
    when :setspecial
      iseq.setspecial(opnds[0])
    when :splatarray
      iseq.splatarray(opnds[0])
    when :swap
      iseq.swap
    when :throw
      iseq.throw(opnds[0])
    when :topn
      iseq.topn(opnds[0])
    when :toregexp
      iseq.toregexp(opnds[0], opnds[1])
    else
      raise "Unknown instruction type: #{type}"
    end
  end

  # set up the catch table
  source[12].each do |entry|
    case entry[0]
    when :break
      if entry[1]
        break_iseq =
          block_iseqs.find do |block_iseq|
            block_iseq.name == entry[1][5] &&
              block_iseq.file == entry[1][6] &&
              block_iseq.line == entry[1][8]
          end

        iseq.catch_break(
          break_iseq || from(entry[1], options, iseq),
          labels[entry[2]],
          labels[entry[3]],
          labels[entry[4]],
          entry[5]
        )
      else
        iseq.catch_break(
          nil,
          labels[entry[2]],
          labels[entry[3]],
          labels[entry[4]],
          entry[5]
        )
      end
    when :ensure
      iseq.catch_ensure(
        from(entry[1], options, iseq),
        labels[entry[2]],
        labels[entry[3]],
        labels[entry[4]],
        entry[5]
      )
    when :next
      iseq.catch_next(
        labels[entry[2]],
        labels[entry[3]],
        labels[entry[4]],
        entry[5]
      )
    when :rescue
      iseq.catch_rescue(
        from(entry[1], options, iseq),
        labels[entry[2]],
        labels[entry[3]],
        labels[entry[4]],
        entry[5]
      )
    when :redo
      iseq.catch_redo(
        labels[entry[2]],
        labels[entry[3]],
        labels[entry[4]],
        entry[5]
      )
    when :retry
      iseq.catch_retry(
        labels[entry[2]],
        labels[entry[3]],
        labels[entry[4]],
        entry[5]
      )
    else
      raise "unknown catch type: #{entry[0]}"
    end
  end

  iseq.compile! if iseq.type == :top
  iseq
end

.iseq_load(iseq) ⇒ Object

This provides a handle to the rb_iseq_load function, which allows you to pass a serialized iseq to Ruby and have it return a RubyVM::InstructionSequence object.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 13

def self.iseq_load(iseq)
  require "fiddle"

  @iseq_load_function ||=
    Fiddle::Function.new(
      Fiddle::Handle::DEFAULT["rb_iseq_load"],
      [Fiddle::TYPE_VOIDP] * 3,
      Fiddle::TYPE_VOIDP
    )

  Fiddle.dlunwrap(@iseq_load_function.call(Fiddle.dlwrap(iseq), 0, nil))
rescue LoadError
  raise "Could not load the Fiddle library"
rescue NameError
  raise "Unable to find rb_iseq_load"
rescue Fiddle::DLError
  raise "Unable to perform a dynamic load"
end

Instance Method Details

#adjuststack(number) ⇒ Object



636
637
638
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 636

def adjuststack(number)
  push(AdjustStack.new(number))
end

#anytostringObject



640
641
642
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 640

def anytostring
  push(AnyToString.new)
end

#block_child_iseq(line) ⇒ Object



456
457
458
459
460
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 456

def block_child_iseq(line)
  current = self
  current = current.parent_iseq while current.type == :block
  child_iseq("block in #{current.name}", line, :block)
end

#branchif(label) ⇒ Object



644
645
646
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 644

def branchif(label)
  push(BranchIf.new(label))
end

#branchnil(label) ⇒ Object



648
649
650
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 648

def branchnil(label)
  push(BranchNil.new(label))
end

#branchunless(label) ⇒ Object



652
653
654
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 652

def branchunless(label)
  push(BranchUnless.new(label))
end

#catch_break(iseq, begin_label, end_label, exit_label, restore_sp) ⇒ Object



549
550
551
552
553
554
555
556
557
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 549

def catch_break(iseq, begin_label, end_label, exit_label, restore_sp)
  catch_table << CatchBreak.new(
    iseq,
    begin_label,
    end_label,
    exit_label,
    restore_sp
  )
end

#catch_ensure(iseq, begin_label, end_label, exit_label, restore_sp) ⇒ Object



559
560
561
562
563
564
565
566
567
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 559

def catch_ensure(iseq, begin_label, end_label, exit_label, restore_sp)
  catch_table << CatchEnsure.new(
    iseq,
    begin_label,
    end_label,
    exit_label,
    restore_sp
  )
end

#catch_next(begin_label, end_label, exit_label, restore_sp) ⇒ Object



569
570
571
572
573
574
575
576
577
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 569

def catch_next(begin_label, end_label, exit_label, restore_sp)
  catch_table << CatchNext.new(
    nil,
    begin_label,
    end_label,
    exit_label,
    restore_sp
  )
end

#catch_redo(begin_label, end_label, exit_label, restore_sp) ⇒ Object



579
580
581
582
583
584
585
586
587
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 579

def catch_redo(begin_label, end_label, exit_label, restore_sp)
  catch_table << CatchRedo.new(
    nil,
    begin_label,
    end_label,
    exit_label,
    restore_sp
  )
end

#catch_rescue(iseq, begin_label, end_label, exit_label, restore_sp) ⇒ Object



589
590
591
592
593
594
595
596
597
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 589

def catch_rescue(iseq, begin_label, end_label, exit_label, restore_sp)
  catch_table << CatchRescue.new(
    iseq,
    begin_label,
    end_label,
    exit_label,
    restore_sp
  )
end

#catch_retry(begin_label, end_label, exit_label, restore_sp) ⇒ Object



599
600
601
602
603
604
605
606
607
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 599

def catch_retry(begin_label, end_label, exit_label, restore_sp)
  catch_table << CatchRetry.new(
    nil,
    begin_label,
    end_label,
    exit_label,
    restore_sp
  )
end

#checkkeyword(keyword_bits_index, keyword_index) ⇒ Object



656
657
658
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 656

def checkkeyword(keyword_bits_index, keyword_index)
  push(CheckKeyword.new(keyword_bits_index, keyword_index))
end

#checkmatch(type) ⇒ Object



660
661
662
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 660

def checkmatch(type)
  push(CheckMatch.new(type))
end

#checktype(type) ⇒ Object



664
665
666
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 664

def checktype(type)
  push(CheckType.new(type))
end

#child_iseq(name, line, type) ⇒ Object

Child instruction sequence methods



452
453
454
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 452

def child_iseq(name, line, type)
  InstructionSequence.new(name, file, line, type, self, options)
end

#class_child_iseq(name, line) ⇒ Object



462
463
464
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 462

def class_child_iseq(name, line)
  child_iseq("<class:#{name}>", line, :class)
end

#compile!Object

This method converts our linked list of instructions into a final array and performs any other compilation steps necessary.



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 305

def compile!
  specialize_instructions! if options.specialized_instruction?

  catch_table.each do |catch_entry|
    if !catch_entry.is_a?(CatchBreak) && catch_entry.iseq
      catch_entry.iseq.compile!
    end
  end

  length = 0
  insns.each do |insn|
    case insn
    when Integer, Symbol
      # skip
    when Label
      insn.patch!(:"label_#{length}")
    when DefineClass
      insn.class_iseq.compile!
      length += insn.length
    when DefineMethod, DefineSMethod
      insn.method_iseq.compile!
      length += insn.length
    when InvokeSuper, Send
      insn.block_iseq.compile! if insn.block_iseq
      length += insn.length
    when Once
      insn.iseq.compile!
      length += insn.length
    else
      length += insn.length
    end
  end

  @insns = insns.to_a
end

#concatarrayObject



668
669
670
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 668

def concatarray
  push(ConcatArray.new)
end

#concatstrings(number) ⇒ Object



672
673
674
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 672

def concatstrings(number)
  push(ConcatStrings.new(number))
end

#defineclass(name, class_iseq, flags) ⇒ Object



676
677
678
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 676

def defineclass(name, class_iseq, flags)
  push(DefineClass.new(name, class_iseq, flags))
end

#defined(type, name, message) ⇒ Object



680
681
682
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 680

def defined(type, name, message)
  push(Defined.new(type, name, message))
end

#definedivar(name, cache, message) ⇒ Object



684
685
686
687
688
689
690
691
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 684

def definedivar(name, cache, message)
  if RUBY_VERSION < "3.3"
    push(PutNil.new)
    push(Defined.new(Defined::TYPE_IVAR, name, message))
  else
    push(DefinedIVar.new(name, cache, message))
  end
end

#definemethod(name, method_iseq) ⇒ Object



693
694
695
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 693

def definemethod(name, method_iseq)
  push(DefineMethod.new(name, method_iseq))
end

#definesmethod(name, method_iseq) ⇒ Object



697
698
699
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 697

def definesmethod(name, method_iseq)
  push(DefineSMethod.new(name, method_iseq))
end

#disasmObject



292
293
294
295
296
297
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 292

def disasm
  fmt = Disassembler.new
  fmt.enqueue(self)
  fmt.format!
  fmt.string
end

#dupObject



701
702
703
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 701

def dup
  push(Dup.new)
end

#duparray(object) ⇒ Object



705
706
707
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 705

def duparray(object)
  push(DupArray.new(object))
end

#duphash(object) ⇒ Object



709
710
711
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 709

def duphash(object)
  push(DupHash.new(object))
end

#dupn(number) ⇒ Object



713
714
715
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 713

def dupn(number)
  push(DupN.new(number))
end

#evalObject



232
233
234
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 232

def eval
  InstructionSequence.iseq_load(to_a).eval
end

#event(name) ⇒ Object



632
633
634
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 632

def event(name)
  push(name)
end

#expandarray(length, flags) ⇒ Object



717
718
719
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 717

def expandarray(length, flags)
  push(ExpandArray.new(length, flags))
end

#getblockparam(index, level) ⇒ Object



721
722
723
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 721

def getblockparam(index, level)
  push(GetBlockParam.new(index, level))
end

#getblockparamproxy(index, level) ⇒ Object



725
726
727
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 725

def getblockparamproxy(index, level)
  push(GetBlockParamProxy.new(index, level))
end

#getclassvariable(name) ⇒ Object



729
730
731
732
733
734
735
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 729

def getclassvariable(name)
  if RUBY_VERSION < "3.0"
    push(Legacy::GetClassVariable.new(name))
  else
    push(GetClassVariable.new(name, inline_storage_for(name)))
  end
end

#getconstant(name) ⇒ Object



737
738
739
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 737

def getconstant(name)
  push(GetConstant.new(name))
end

#getglobal(name) ⇒ Object



741
742
743
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 741

def getglobal(name)
  push(GetGlobal.new(name))
end

#getinstancevariable(name) ⇒ Object



745
746
747
748
749
750
751
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 745

def getinstancevariable(name)
  if RUBY_VERSION < "3.2"
    push(GetInstanceVariable.new(name, inline_storage_for(name)))
  else
    push(GetInstanceVariable.new(name, inline_storage))
  end
end

#getlocal(index, level) ⇒ Object



753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 753

def getlocal(index, level)
  if options.operands_unification?
    # Specialize the getlocal instruction based on the level of the
    # local variable. If it's 0 or 1, then there's a specialized
    # instruction that will look at the current scope or the parent
    # scope, respectively, and requires fewer operands.
    case level
    when 0
      push(GetLocalWC0.new(index))
    when 1
      push(GetLocalWC1.new(index))
    else
      push(GetLocal.new(index, level))
    end
  else
    push(GetLocal.new(index, level))
  end
end

#getspecial(key, type) ⇒ Object



772
773
774
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 772

def getspecial(key, type)
  push(GetSpecial.new(key, type))
end

#inline_storageObject



207
208
209
210
211
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 207

def inline_storage
  storage = storage_index
  @storage_index += 1
  storage
end

#inline_storage_for(name) ⇒ Object



213
214
215
216
217
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 213

def inline_storage_for(name)
  inline_storages[name] = inline_storage unless inline_storages.key?(name)

  inline_storages[name]
end

#inspectObject



299
300
301
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 299

def inspect
  "#<ISeq:#{name}@<compiled>:1 (#{line},0)-(#{line},0)>"
end

#internObject



776
777
778
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 776

def intern
  push(Intern.new)
end

#invokeblock(calldata) ⇒ Object



780
781
782
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 780

def invokeblock(calldata)
  push(InvokeBlock.new(calldata))
end

#invokesuper(calldata, block_iseq) ⇒ Object



784
785
786
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 784

def invokesuper(calldata, block_iseq)
  push(InvokeSuper.new(calldata, block_iseq))
end

#jump(label) ⇒ Object



788
789
790
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 788

def jump(label)
  push(Jump.new(label))
end

#labelObject

Instruction push methods



613
614
615
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 613

def label
  Label.new
end

#leaveObject



792
793
794
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 792

def leave
  push(Leave.new)
end

#lengthObject



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 219

def length
  insns
    .each
    .inject(0) do |sum, insn|
      case insn
      when Integer, Label, Symbol
        sum
      else
        sum + insn.length
      end
    end
end

#local_variable(name, level = 0) ⇒ Object

Query methods



199
200
201
202
203
204
205
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 199

def local_variable(name, level = 0)
  if (lookup = local_table.find(name, level))
    lookup
  elsif parent_iseq
    parent_iseq.local_variable(name, level + 1)
  end
end

#method_child_iseq(name, line) ⇒ Object



466
467
468
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 466

def method_child_iseq(name, line)
  child_iseq(name, line, :method)
end

#module_child_iseq(name, line) ⇒ Object



470
471
472
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 470

def module_child_iseq(name, line)
  child_iseq("<module:#{name}>", line, :class)
end

#newarray(number) ⇒ Object



796
797
798
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 796

def newarray(number)
  push(NewArray.new(number))
end

#newarraykwsplat(number) ⇒ Object



800
801
802
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 800

def newarraykwsplat(number)
  push(NewArrayKwSplat.new(number))
end

#newhash(number) ⇒ Object



804
805
806
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 804

def newhash(number)
  push(NewHash.new(number))
end

#newrange(exclude_end) ⇒ Object



808
809
810
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 808

def newrange(exclude_end)
  push(NewRange.new(exclude_end))
end

#nopObject



812
813
814
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 812

def nop
  push(Nop.new)
end

#objtostring(calldata) ⇒ Object



816
817
818
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 816

def objtostring(calldata)
  push(ObjToString.new(calldata))
end

#once(iseq, cache) ⇒ Object



820
821
822
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 820

def once(iseq, cache)
  push(Once.new(iseq, cache))
end

#opt_aref_with(object, calldata) ⇒ Object



824
825
826
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 824

def opt_aref_with(object, calldata)
  push(OptArefWith.new(object, calldata))
end

#opt_aset_with(object, calldata) ⇒ Object



828
829
830
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 828

def opt_aset_with(object, calldata)
  push(OptAsetWith.new(object, calldata))
end

#opt_case_dispatch(case_dispatch_hash, else_label) ⇒ Object



832
833
834
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 832

def opt_case_dispatch(case_dispatch_hash, else_label)
  push(OptCaseDispatch.new(case_dispatch_hash, else_label))
end

#opt_getconstant_path(names) ⇒ Object



836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 836

def opt_getconstant_path(names)
  if RUBY_VERSION < "3.2" || !options.inline_const_cache?
    cache = nil
    cache_filled_label = nil

    if options.inline_const_cache?
      cache = inline_storage
      cache_filled_label = label
      opt_getinlinecache(cache_filled_label, cache)

      if names[0] == :""
        names.shift
        pop
        putobject(Object)
      end
    elsif names[0] == :""
      names.shift
      putobject(Object)
    else
      putnil
    end

    names.each_with_index do |name, index|
      putobject(index == 0)
      getconstant(name)
    end

    if options.inline_const_cache?
      opt_setinlinecache(cache)
      push(cache_filled_label)
    end
  else
    push(OptGetConstantPath.new(names))
  end
end

#opt_getinlinecache(label, cache) ⇒ Object



872
873
874
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 872

def opt_getinlinecache(label, cache)
  push(Legacy::OptGetInlineCache.new(label, cache))
end

#opt_setinlinecache(cache) ⇒ Object



876
877
878
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 876

def opt_setinlinecache(cache)
  push(Legacy::OptSetInlineCache.new(cache))
end

#popObject



880
881
882
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 880

def pop
  push(Pop.new)
end

#push(value) ⇒ Object



617
618
619
620
621
622
623
624
625
626
627
628
629
630
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 617

def push(value)
  node = insns.push(value)

  case value
  when Array, Integer, Symbol
    value
  when Label
    value.node = node
    value
  else
    stack.change_by(-value.pops + value.pushes)
    value
  end
end

#putnilObject



884
885
886
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 884

def putnil
  push(PutNil.new)
end

#putobject(object) ⇒ Object



888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 888

def putobject(object)
  if options.operands_unification?
    # Specialize the putobject instruction based on the value of the
    # object. If it's 0 or 1, then there's a specialized instruction
    # that will push the object onto the stack and requires fewer
    # operands.
    if object.eql?(0)
      push(PutObjectInt2Fix0.new)
    elsif object.eql?(1)
      push(PutObjectInt2Fix1.new)
    else
      push(PutObject.new(object))
    end
  else
    push(PutObject.new(object))
  end
end

#putselfObject



906
907
908
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 906

def putself
  push(PutSelf.new)
end

#putspecialobject(object) ⇒ Object



910
911
912
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 910

def putspecialobject(object)
  push(PutSpecialObject.new(object))
end

#putstring(object) ⇒ Object



914
915
916
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 914

def putstring(object)
  push(PutString.new(object))
end

#send(calldata, block_iseq = nil) ⇒ Object



918
919
920
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 918

def send(calldata, block_iseq = nil)
  push(Send.new(calldata, block_iseq))
end

#setblockparam(index, level) ⇒ Object



922
923
924
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 922

def setblockparam(index, level)
  push(SetBlockParam.new(index, level))
end

#setclassvariable(name) ⇒ Object



926
927
928
929
930
931
932
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 926

def setclassvariable(name)
  if RUBY_VERSION < "3.0"
    push(Legacy::SetClassVariable.new(name))
  else
    push(SetClassVariable.new(name, inline_storage_for(name)))
  end
end

#setconstant(name) ⇒ Object



934
935
936
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 934

def setconstant(name)
  push(SetConstant.new(name))
end

#setglobal(name) ⇒ Object



938
939
940
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 938

def setglobal(name)
  push(SetGlobal.new(name))
end

#setinstancevariable(name) ⇒ Object



942
943
944
945
946
947
948
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 942

def setinstancevariable(name)
  if RUBY_VERSION < "3.2"
    push(SetInstanceVariable.new(name, inline_storage_for(name)))
  else
    push(SetInstanceVariable.new(name, inline_storage))
  end
end

#setlocal(index, level) ⇒ Object



950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 950

def setlocal(index, level)
  if options.operands_unification?
    # Specialize the setlocal instruction based on the level of the
    # local variable. If it's 0 or 1, then there's a specialized
    # instruction that will write to the current scope or the parent
    # scope, respectively, and requires fewer operands.
    case level
    when 0
      push(SetLocalWC0.new(index))
    when 1
      push(SetLocalWC1.new(index))
    else
      push(SetLocal.new(index, level))
    end
  else
    push(SetLocal.new(index, level))
  end
end

#setn(number) ⇒ Object



969
970
971
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 969

def setn(number)
  push(SetN.new(number))
end

#setspecial(key) ⇒ Object



973
974
975
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 973

def setspecial(key)
  push(SetSpecial.new(key))
end

#singleton_class_child_iseq(line) ⇒ Object



474
475
476
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 474

def singleton_class_child_iseq(line)
  child_iseq("singleton class", line, :class)
end

#specialize_instructions!Object



341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 341

def specialize_instructions!
  insns.each_node do |node, value|
    case value
    when NewArray
      next unless node.next_node

      next_node = node.next_node
      next unless next_node.value.is_a?(Send)
      next if next_node.value.block_iseq

      calldata = next_node.value.calldata
      next unless calldata.flags == CallData::CALL_ARGS_SIMPLE
      next unless calldata.argc == 0

      case calldata.method
      when :max
        node.value = OptNewArrayMax.new(value.number)
        node.next_node = next_node.next_node
      when :min
        node.value = OptNewArrayMin.new(value.number)
        node.next_node = next_node.next_node
      end
    when PutObject, PutString
      next unless node.next_node
      next if value.is_a?(PutObject) && !value.object.is_a?(String)

      next_node = node.next_node
      next unless next_node.value.is_a?(Send)
      next if next_node.value.block_iseq

      calldata = next_node.value.calldata
      next unless calldata.flags == CallData::CALL_ARGS_SIMPLE
      next unless calldata.argc == 0

      case calldata.method
      when :freeze
        node.value = OptStrFreeze.new(value.object, calldata)
        node.next_node = next_node.next_node
      when :-@
        node.value = OptStrUMinus.new(value.object, calldata)
        node.next_node = next_node.next_node
      end
    when Send
      calldata = value.calldata

      if !value.block_iseq &&
           !calldata.flag?(CallData::CALL_ARGS_BLOCKARG)
        # Specialize the send instruction. If it doesn't have a block
        # attached, then we will replace it with an opt_send_without_block
        # and do further specializations based on the called method and
        # the number of arguments.
        node.value =
          case [calldata.method, calldata.argc]
          when [:length, 0]
            OptLength.new(calldata)
          when [:size, 0]
            OptSize.new(calldata)
          when [:empty?, 0]
            OptEmptyP.new(calldata)
          when [:nil?, 0]
            OptNilP.new(calldata)
          when [:succ, 0]
            OptSucc.new(calldata)
          when [:!, 0]
            OptNot.new(calldata)
          when [:+, 1]
            OptPlus.new(calldata)
          when [:-, 1]
            OptMinus.new(calldata)
          when [:*, 1]
            OptMult.new(calldata)
          when [:/, 1]
            OptDiv.new(calldata)
          when [:%, 1]
            OptMod.new(calldata)
          when [:==, 1]
            OptEq.new(calldata)
          when [:!=, 1]
            OptNEq.new(YARV.calldata(:==, 1), calldata)
          when [:=~, 1]
            OptRegExpMatch2.new(calldata)
          when [:<, 1]
            OptLT.new(calldata)
          when [:<=, 1]
            OptLE.new(calldata)
          when [:>, 1]
            OptGT.new(calldata)
          when [:>=, 1]
            OptGE.new(calldata)
          when [:<<, 1]
            OptLTLT.new(calldata)
          when [:[], 1]
            OptAref.new(calldata)
          when [:&, 1]
            OptAnd.new(calldata)
          when [:|, 1]
            OptOr.new(calldata)
          when [:[]=, 2]
            OptAset.new(calldata)
          else
            OptSendWithoutBlock.new(calldata)
          end
      end
    end
  end
end

#splatarray(flag) ⇒ Object



977
978
979
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 977

def splatarray(flag)
  push(SplatArray.new(flag))
end

#swapObject



981
982
983
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 981

def swap
  push(Swap.new)
end

#throw(type) ⇒ Object



985
986
987
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 985

def throw(type)
  push(Throw.new(type))
end

#to_aObject



236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 236

def to_a
  versions = RUBY_VERSION.split(".").map(&:to_i)

  # Dump all of the instructions into a flat list.
  dumped =
    insns.map do |insn|
      case insn
      when Integer, Symbol
        insn
      when Label
        insn.name
      else
        insn.to_a(self)
      end
    end

  dumped_options = argument_options.dup
  dumped_options[:opt].map!(&:name) if dumped_options[:opt]

  # Next, return the instruction sequence as an array.
  [
    MAGIC,
    versions[0],
    versions[1],
    1,
    {
      arg_size: argument_size,
      local_size: local_table.size,
      stack_max: stack.maximum_size,
      node_id: -1,
      node_ids: [-1] * insns.length
    },
    name,
    file,
    "<compiled>",
    line,
    type,
    local_table.names,
    dumped_options,
    catch_table.map(&:to_a),
    dumped
  ]
end

#to_cfgObject



280
281
282
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 280

def to_cfg
  ControlFlowGraph.compile(self)
end

#to_dfgObject



284
285
286
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 284

def to_dfg
  to_cfg.to_dfg
end

#to_sonObject



288
289
290
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 288

def to_son
  to_dfg.to_son
end

#topn(number) ⇒ Object



989
990
991
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 989

def topn(number)
  push(TopN.new(number))
end

#toregexp(options, length) ⇒ Object



993
994
995
# File 'lib/syntax_tree/yarv/instruction_sequence.rb', line 993

def toregexp(options, length)
  push(ToRegExp.new(options, length))
end