Class: SyntaxTree::YARV::GetLocal

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

Overview

### Summary

getlocal fetches the value of a local variable from a frame determined by the level and index arguments. The level is the number of frames back to look and the index is the index in the local table. It pushes the value it finds onto the stack.

### Usage

~~~ruby value = 5 tap { tap { value } } ~~~

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Instruction

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

Constructor Details

#initialize(index, level) ⇒ GetLocal

Returns a new instance of GetLocal.



1773
1774
1775
1776
# File 'lib/syntax_tree/yarv/instructions.rb', line 1773

def initialize(index, level)
  @index = index
  @level = level
end

Instance Attribute Details

#indexObject (readonly)

Returns the value of attribute index.



1771
1772
1773
# File 'lib/syntax_tree/yarv/instructions.rb', line 1771

def index
  @index
end

#levelObject (readonly)

Returns the value of attribute level.



1771
1772
1773
# File 'lib/syntax_tree/yarv/instructions.rb', line 1771

def level
  @level
end

Instance Method Details

#==(other) ⇒ Object



1792
1793
1794
# File 'lib/syntax_tree/yarv/instructions.rb', line 1792

def ==(other)
  other.is_a?(GetLocal) && other.index == index && other.level == level
end

#call(vm) ⇒ Object



1804
1805
1806
# File 'lib/syntax_tree/yarv/instructions.rb', line 1804

def call(vm)
  vm.push(vm.local_get(index, level))
end

#deconstruct_keys(_keys) ⇒ Object



1788
1789
1790
# File 'lib/syntax_tree/yarv/instructions.rb', line 1788

def deconstruct_keys(_keys)
  { index: index, level: level }
end

#disasm(fmt) ⇒ Object



1778
1779
1780
# File 'lib/syntax_tree/yarv/instructions.rb', line 1778

def disasm(fmt)
  fmt.instruction("getlocal", [fmt.local(index, explicit: level)])
end

#lengthObject



1796
1797
1798
# File 'lib/syntax_tree/yarv/instructions.rb', line 1796

def length
  3
end

#pushesObject



1800
1801
1802
# File 'lib/syntax_tree/yarv/instructions.rb', line 1800

def pushes
  1
end

#to_a(iseq) ⇒ Object



1782
1783
1784
1785
1786
# File 'lib/syntax_tree/yarv/instructions.rb', line 1782

def to_a(iseq)
  current = iseq
  level.times { current = current.parent_iseq }
  [:getlocal, current.local_table.offset(index), level]
end