Class: SyntaxTree::YARV::GetInstanceVariable

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

Overview

### Summary

‘getinstancevariable` pushes the value of an instance variable onto the stack. It uses an inline cache to avoid having to look up the instance variable in the class hierarchy every time.

This instruction has two forms, but both have the same structure. Before Ruby 3.2, the inline cache corresponded to both the get and set instructions and could be shared. Since Ruby 3.2, it uses object shapes instead so the caches are unique per instruction.

### Usage

~~~ruby ~~~

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Instruction

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

Constructor Details

#initialize(name, cache) ⇒ GetInstanceVariable

Returns a new instance of GetInstanceVariable.



1717
1718
1719
1720
# File 'lib/syntax_tree/yarv/instructions.rb', line 1717

def initialize(name, cache)
  @name = name
  @cache = cache
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



1715
1716
1717
# File 'lib/syntax_tree/yarv/instructions.rb', line 1715

def cache
  @cache
end

#nameObject (readonly)

Returns the value of attribute name.



1715
1716
1717
# File 'lib/syntax_tree/yarv/instructions.rb', line 1715

def name
  @name
end

Instance Method Details

#==(other) ⇒ Object



1737
1738
1739
1740
# File 'lib/syntax_tree/yarv/instructions.rb', line 1737

def ==(other)
  other.is_a?(GetInstanceVariable) && other.name == name &&
    other.cache == cache
end

#call(vm) ⇒ Object



1750
1751
1752
1753
# File 'lib/syntax_tree/yarv/instructions.rb', line 1750

def call(vm)
  method = Object.instance_method(:instance_variable_get)
  vm.push(method.bind(vm.frame._self).call(name))
end

#deconstruct_keys(_keys) ⇒ Object



1733
1734
1735
# File 'lib/syntax_tree/yarv/instructions.rb', line 1733

def deconstruct_keys(_keys)
  { name: name, cache: cache }
end

#disasm(fmt) ⇒ Object



1722
1723
1724
1725
1726
1727
# File 'lib/syntax_tree/yarv/instructions.rb', line 1722

def disasm(fmt)
  fmt.instruction(
    "getinstancevariable",
    [fmt.object(name), fmt.inline_storage(cache)]
  )
end

#lengthObject



1742
1743
1744
# File 'lib/syntax_tree/yarv/instructions.rb', line 1742

def length
  3
end

#pushesObject



1746
1747
1748
# File 'lib/syntax_tree/yarv/instructions.rb', line 1746

def pushes
  1
end

#to_a(_iseq) ⇒ Object



1729
1730
1731
# File 'lib/syntax_tree/yarv/instructions.rb', line 1729

def to_a(_iseq)
  [:getinstancevariable, name, cache]
end