Class: SyntaxTree::YARV::OptNEq

Inherits:
Instruction show all
Defined in:
lib/syntax_tree/yarv/instructions.rb

Overview

### Summary

opt_neq is an optimization that tests whether two values at the top of the stack are not equal by testing their equality and calling the ‘!` on the result. This allows opt_neq to use the fast paths optimized in opt_eq when both operands are Integers, Floats, Symbols, or Strings. It pops both the receiver and the argument off the stack and pushes on the result.

### Usage

~~~ruby 2 != 2 ~~~

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Instruction

#branch_targets, #canonical, #falls_through?, #leaves?, #side_effects?

Constructor Details

#initialize(eq_calldata, neq_calldata) ⇒ OptNEq

Returns a new instance of OptNEq.



3718
3719
3720
3721
# File 'lib/syntax_tree/yarv/instructions.rb', line 3718

def initialize(eq_calldata, neq_calldata)
  @eq_calldata = eq_calldata
  @neq_calldata = neq_calldata
end

Instance Attribute Details

#eq_calldataObject (readonly)

Returns the value of attribute eq_calldata.



3716
3717
3718
# File 'lib/syntax_tree/yarv/instructions.rb', line 3716

def eq_calldata
  @eq_calldata
end

#neq_calldataObject (readonly)

Returns the value of attribute neq_calldata.



3716
3717
3718
# File 'lib/syntax_tree/yarv/instructions.rb', line 3716

def neq_calldata
  @neq_calldata
end

Instance Method Details

#==(other) ⇒ Object



3738
3739
3740
3741
# File 'lib/syntax_tree/yarv/instructions.rb', line 3738

def ==(other)
  other.is_a?(OptNEq) && other.eq_calldata == eq_calldata &&
    other.neq_calldata == neq_calldata
end

#call(vm) ⇒ Object



3755
3756
3757
3758
# File 'lib/syntax_tree/yarv/instructions.rb', line 3755

def call(vm)
  receiver, argument = vm.pop(2)
  vm.push(receiver != argument)
end

#deconstruct_keys(_keys) ⇒ Object



3734
3735
3736
# File 'lib/syntax_tree/yarv/instructions.rb', line 3734

def deconstruct_keys(_keys)
  { eq_calldata: eq_calldata, neq_calldata: neq_calldata }
end

#disasm(fmt) ⇒ Object



3723
3724
3725
3726
3727
3728
# File 'lib/syntax_tree/yarv/instructions.rb', line 3723

def disasm(fmt)
  fmt.instruction(
    "opt_neq",
    [fmt.calldata(eq_calldata), fmt.calldata(neq_calldata)]
  )
end

#lengthObject



3743
3744
3745
# File 'lib/syntax_tree/yarv/instructions.rb', line 3743

def length
  3
end

#popsObject



3747
3748
3749
# File 'lib/syntax_tree/yarv/instructions.rb', line 3747

def pops
  2
end

#pushesObject



3751
3752
3753
# File 'lib/syntax_tree/yarv/instructions.rb', line 3751

def pushes
  1
end

#to_a(_iseq) ⇒ Object



3730
3731
3732
# File 'lib/syntax_tree/yarv/instructions.rb', line 3730

def to_a(_iseq)
  [:opt_neq, eq_calldata.to_h, neq_calldata.to_h]
end