Class: CodeTools::Generator::Label

Inherits:
Object
  • Object
show all
Defined in:
lib/rubinius/code/compiler/generator.rb

Overview

Jump label for the branch instructions. The use scenarios for labels:

1. Used and then set
     g.goto_if_false label
     ...
     label.set!
2. Set and then used
     label.set!
     ...
     g.goto_if_true label
3. 1, 2

Many labels are only used once. This class employs two small optimizations. First, for the case where a label is used once then set, the label merely records the point it was used and updates that location to the concrete IP when the label is set. In the case where the label is used multiple times, it records each location and updates them to an IP when the label is set. In both cases, once the label is set, each use after that updates the instruction stream with a concrete IP at the point the label is used. This avoids the need to ever record all the labels or search through the stream later to change symbolic labels into concrete IP’s.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(generator, basic_block = true) ⇒ Label

Returns a new instance of Label.



35
36
37
38
39
40
41
42
# File 'lib/rubinius/code/compiler/generator.rb', line 35

def initialize(generator, basic_block=true)
  @generator   = generator
  @basic_block = basic_block ? generator.new_basic_block : nil
  @position    = nil
  @used        = false
  @location    = nil
  @locations   = nil
end

Instance Attribute Details

#basic_blockObject (readonly)

Returns the value of attribute basic_block.



32
33
34
# File 'lib/rubinius/code/compiler/generator.rb', line 32

def basic_block
  @basic_block
end

#positionObject

Returns the value of attribute position.



31
32
33
# File 'lib/rubinius/code/compiler/generator.rb', line 31

def position
  @position
end

#usedObject (readonly) Also known as: used?

Returns the value of attribute used.



32
33
34
# File 'lib/rubinius/code/compiler/generator.rb', line 32

def used
  @used
end

Instance Method Details

#placeObject



44
45
46
# File 'lib/rubinius/code/compiler/generator.rb', line 44

def place
  @position = @generator.ip
end

#set!Object



48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rubinius/code/compiler/generator.rb', line 48

def set!
  @position = @generator.ip
  if @locations
    @locations.each { |x| @generator.stream[x] = @position }
  elsif @location
    @generator.stream[@location] = @position
  end

  @generator.current_block.left = @basic_block
  @generator.current_block.close
  @generator.current_block = @basic_block
  @basic_block.open
end

#used_at(ip) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rubinius/code/compiler/generator.rb', line 62

def used_at(ip)
  if @position
    @generator.stream[ip] = @position
  elsif !@location
    @location = ip
  elsif @locations
    @locations << ip
  else
    @locations = [@location, ip]
  end
  @used = true
end