Class: CodeTools::Generator::BasicBlock

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

Constant Summary collapse

SEPARATOR_SIZE =
40

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(generator) ⇒ BasicBlock

Returns a new instance of BasicBlock.



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/rubinius/code/compiler/generator.rb', line 80

def initialize(generator)
  @generator  = generator
  @ip         = generator.ip
  @enter_size = nil
  @max_size   = 0
  @min_size   = 0
  @exit_ip    = 0
  @exit_size  = nil
  @stack      = 0
  @left       = nil
  @right      = nil
  @visited    = false
  @closed     = false
end

Instance Attribute Details

#leftObject

Returns the value of attribute left.



77
78
79
# File 'lib/rubinius/code/compiler/generator.rb', line 77

def left
  @left
end

#rightObject

Returns the value of attribute right.



78
79
80
# File 'lib/rubinius/code/compiler/generator.rb', line 78

def right
  @right
end

Instance Method Details

#add_stack(read, write) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/rubinius/code/compiler/generator.rb', line 95

def add_stack(read, write)
  read_change = @stack - read
  @min_size = read_change if read_change < @min_size

  @stack += (write - read)

  @max_size = @stack if @stack > @max_size
end

#check_stack(stack_size) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/rubinius/code/compiler/generator.rb', line 230

def check_stack(stack_size)
  if @enter_size
    unless stack_size == @enter_size
      invalid "unbalanced stack at #{location}: #{stack_size} != #{@enter_size}"
    end
  else
    if not @closed
      invalid "control fails to exit properly at #{location}"
    end

    @enter_size = stack_size
  end
end

#close(record_exit = false) ⇒ Object



108
109
110
111
112
113
114
115
# File 'lib/rubinius/code/compiler/generator.rb', line 108

def close(record_exit=false)
  @closed = true

  if record_exit
    @exit_size = @stack
    @exit_ip = @generator.ip - 1
  end
end

#flow_stack_size(stack) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/rubinius/code/compiler/generator.rb', line 198

def flow_stack_size(stack)
  unless @visited
    @visited = true

    @generator.accumulate_stack(@enter_size + @max_size)

    net_size = @enter_size + @stack

    if net_size < 0
      invalid "net stack underflow in block starting at #{location}"
    end

    if @enter_size + @min_size < 0
      invalid "minimum stack underflow in block starting at #{location}"
    end

    if @exit_size and @enter_size + @exit_size < 1
      invalid "exit stack underflow in block starting at #{location(@exit_ip)}"
    end

    if @left
      @left.check_stack net_size
      stack.push @left unless @left.visited?
    end

    if @right
      @right.check_stack net_size
      stack.push @right unless @right.visited?
    end
  end
end

#invalid(message) ⇒ Object

Raises:



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/rubinius/code/compiler/generator.rb', line 125

def invalid(message)
  puts message
  name = @generator.name.inspect
  size = (SEPARATOR_SIZE - name.size - 2) / 2
  size = 1 if size <= 0
  puts "\n#{"=" * size} #{name} #{"=" * (size + name.size % 2)}"

  literals = @generator.literals
  names = @generator.local_names
  stream = @generator.stream
  i = 0
  n = stream.size
  stack = 0

  while i < n
    insn = Rubinius::InstructionSet[stream[i]]
    printf "[%3d] %04d  %-28s" % [stack, i, insn.opcode.inspect]

    args = stream[i+1, insn.size-1]
    if insn.size > 1
      insn.args.each_with_index do |kind, index|
        arg = args[index]
        case kind
        when :literal
          printf "%s " % literals[arg].inspect
        when :local
          printf "%s " % (names ? names[arg] : arg)
        else
          printf "%d " % arg
        end
      end
    end

    puts

    if insn.variable_stack?
      use = insn.stack_consumed
      if use.kind_of? Array
        use = args[use[1] - 1] + use[0]
      end

      pro = insn.stack_produced
      if pro.kind_of? Array
        pro = (args[pro[1] - 1] * pro[2]) + pro[0]
      end

      stack += pro - use
    else
      stack += insn.stack_difference
    end

    i += insn.size
  end

  puts "-" * SEPARATOR_SIZE

  raise CompileError, message
end

#location(ip = nil) ⇒ Object



117
118
119
120
121
# File 'lib/rubinius/code/compiler/generator.rb', line 117

def location(ip=nil)
  ip ||= @ip
  line = @generator.ip_to_line(ip)
  "#{@generator.name}: line: #{line}, IP: #{ip}"
end

#openObject



104
105
106
# File 'lib/rubinius/code/compiler/generator.rb', line 104

def open
  @ip = @generator.ip
end

#validate_stackObject



188
189
190
191
192
193
194
195
196
# File 'lib/rubinius/code/compiler/generator.rb', line 188

def validate_stack
  @enter_size = 0

  stack = [self]
  until stack.empty?
    bb = stack.shift
    bb.flow_stack_size stack
  end
end

#visited?Boolean

Returns:

  • (Boolean)


184
185
186
# File 'lib/rubinius/code/compiler/generator.rb', line 184

def visited?
  @visited
end