Class: YTLJit::VM::TypeInferenceContext

Inherits:
Object
  • Object
show all
Defined in:
lib/ytljit/vm_codegen.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tnode) ⇒ TypeInferenceContext

Returns a new instance of TypeInferenceContext.



115
116
117
118
119
120
121
122
123
# File 'lib/ytljit/vm_codegen.rb', line 115

def initialize(tnode)
  @top_node = tnode
  @current_method_signature_node = [[]]
  @current_method = [tnode]
  @convergent = false
  @visited_top_node = {}
  # Options from user
  @options = {}
end

Instance Attribute Details

#convergentObject

Returns the value of attribute convergent.



275
276
277
# File 'lib/ytljit/vm_codegen.rb', line 275

def convergent
  @convergent
end

#current_methodObject (readonly)

Returns the value of attribute current_method.



274
275
276
# File 'lib/ytljit/vm_codegen.rb', line 274

def current_method
  @current_method
end

#current_method_signature_nodeObject (readonly)

Returns the value of attribute current_method_signature_node.



273
274
275
# File 'lib/ytljit/vm_codegen.rb', line 273

def current_method_signature_node
  @current_method_signature_node
end

#optionsObject

Returns the value of attribute options.



277
278
279
# File 'lib/ytljit/vm_codegen.rb', line 277

def options
  @options
end

#top_nodeObject (readonly)

Returns the value of attribute top_node.



272
273
274
# File 'lib/ytljit/vm_codegen.rb', line 272

def top_node
  @top_node
end

#visited_top_nodeObject

Returns the value of attribute visited_top_node.



276
277
278
# File 'lib/ytljit/vm_codegen.rb', line 276

def visited_top_node
  @visited_top_node
end

Instance Method Details

#pop_signatureObject



267
268
269
270
# File 'lib/ytljit/vm_codegen.rb', line 267

def pop_signature
  @current_method.pop
  @current_method_signature_node.pop
end

#push_signature(signode, method) ⇒ Object



262
263
264
265
# File 'lib/ytljit/vm_codegen.rb', line 262

def push_signature(signode, method)
  @current_method_signature_node.push signode
  @current_method.push method
end

#to_signature(offset = -1,, cache = {}) ⇒ Object



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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/ytljit/vm_codegen.rb', line 125

def to_signature(offset = -1, cache = {})
  if offset.is_a?(Node::TopNode) then
    i = -1
    while @current_method[i] and @current_method[i] != offset
      i = i - 1
    end
    if @current_method[i] == offset then
      offset = i
    else
      # This is legal if this TopNode has only one signature 
      sigc = offset.signature_cache
      if sigc.size == 1 then
        return sigc[0]
      else
        p offset.signature_cache
        p i
        p offset.debug_info
        p @current_method.map {|e| e.debug_info}
        p @current_method.map {|e| e.class}
        raise "I can't type inference..."
      end
    end
  end

  cursignode = @current_method_signature_node[offset]
  curmethod = @current_method[offset]
  if curmethod == nil then
    return nil
  end

  cursig = curmethod.current_signature
  if cursig then
    return cursig
  end

  sigc = curmethod.signature_cache
  if sigc.size == 1 then
    return sigc[0]
  end
  
  if rsig = cache[cursignode] then
    rsig = rsig.map {|e| e.copy_type}
    return rsig
  end

  if curmethod.is_a?(Node::ClassTopNode) then
    # Can't pass block when entering a class definition
    rsig = to_signature_aux(cursignode, offset, cache)
    rsig = rsig.map {|e| e.copy_type}
    cache[cursignode] = rsig
    rsig

  elsif curmethod.is_a?(Node::TopNode) then
    prevsig = to_signature(offset - 1, cache)
    rsig = to_signature_aux2(curmethod, cursignode, 
                             prevsig, offset, cache)
    rsig = rsig.map {|e| e.copy_type}
    cache[cursignode] = rsig
    rsig
    
  else
    raise "Maybe bug"
=begin
    prevsig = to_signature(offset - 1, cache)
    mt, slf = curmethod.get_send_method_node(prevsig)

    rsig = to_signature_aux2(mt, cursignode, prevsig, offset, cache)
    cache[cursignode] = rsig
    return rsig
=end
  end
end

#to_signature_aux(cursignode, offset, cache) ⇒ Object



198
199
200
201
202
203
204
205
# File 'lib/ytljit/vm_codegen.rb', line 198

def to_signature_aux(cursignode, offset, cache)
  sig = to_signature(offset - 1, cache)
  res = cursignode.map { |enode|
    enode.decide_type_once(sig)
  }
  
  res
end

#to_signature_aux2(mt, args, cursig, offset, cache) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/ytljit/vm_codegen.rb', line 207

def to_signature_aux2(mt, args, cursig, offset, cache)
  res = []
  args.each do |ele|
    res.push ele.decide_type_once(cursig)
  end

  if !args[1].is_a?(Node::BlockTopNode) then
    return res
  end

  ynode = mt.yield_node[0]
  if ynode and false then
    yargs = ynode.arguments
    push_signature(yargs, mt)
    ysig = to_signature_aux3(yargs, -1, cache)
    # inherit self and block from caller node
    ysig[1] = cursig[1]
    ysig[2] = cursig[2]

    args[1].type = nil
    res[1] = args[1].decide_type_once(ysig)
    #p res
    #p res[1]
    pop_signature
  else
    sig =  args[1].search_valid_signature
    if sig then
      args[1].type = nil
      res[1] = args[1].decide_type_once(sig)
    end
  end
  
  res
end

#to_signature_aux3(cursignode, offset, cache) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/ytljit/vm_codegen.rb', line 242

def to_signature_aux3(cursignode, offset, cache)
  if res = cache[cursignode] then
    return res
  end

  node = @current_method[offset]
  if node.is_a?(Node::ClassTopNode) then
    node.signature_cache[0]
  else
    cursignode2 = @current_method_signature_node[offset]
    sig = to_signature_aux3(cursignode2, offset - 1, cache)
    res = cursignode.map { |enode|
      enode.decide_type_once(sig)
    }

    cache[cursignode] = res
    res
  end
end