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.



109
110
111
112
113
114
115
# File 'lib/ytljit/vm_codegen.rb', line 109

def initialize(tnode)
  @top_node = tnode
  @current_method_signature_node = [[]]
  @current_method = [tnode]
  @convergent = false
  @visited_top_node = {}
end

Instance Attribute Details

#convergentObject

Returns the value of attribute convergent.



228
229
230
# File 'lib/ytljit/vm_codegen.rb', line 228

def convergent
  @convergent
end

#current_method_signature_nodeObject (readonly)

Returns the value of attribute current_method_signature_node.



227
228
229
# File 'lib/ytljit/vm_codegen.rb', line 227

def current_method_signature_node
  @current_method_signature_node
end

#top_nodeObject (readonly)

Returns the value of attribute top_node.



226
227
228
# File 'lib/ytljit/vm_codegen.rb', line 226

def top_node
  @top_node
end

#visited_top_nodeObject

Returns the value of attribute visited_top_node.



229
230
231
# File 'lib/ytljit/vm_codegen.rb', line 229

def visited_top_node
  @visited_top_node
end

Instance Method Details

#pop_signatureObject



221
222
223
224
# File 'lib/ytljit/vm_codegen.rb', line 221

def pop_signature
  @current_method.pop
  @current_method_signature_node.pop
end

#push_signature(signode, method) ⇒ Object



216
217
218
219
# File 'lib/ytljit/vm_codegen.rb', line 216

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

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



117
118
119
120
121
122
123
124
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
# File 'lib/ytljit/vm_codegen.rb', line 117

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 this TopNode has only one signature 
      sigc = offset.signature_cache
      if sigc.size == 1 then
        return sigc[0]
      else
        raise "I can't type inference..."
      end
    end
  end

  cursignode = @current_method_signature_node[offset]
  curmethod = @current_method[offset]

  sigc = curmethod.signature_cache
  if sigc.size == 1 then
    return sigc[0]
  end
  
  if rsig = cache[cursignode] then
    return rsig
  end

  if curmethod.is_a?(Node::ClassTopNode) then
    rsig = to_signature_aux(cursignode, offset, cache)
    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)
    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



173
174
175
176
177
178
179
# File 'lib/ytljit/vm_codegen.rb', line 173

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

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



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/ytljit/vm_codegen.rb', line 181

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

  if mt and (ynode = mt.yield_node[0]) then
    yargs = ynode.arguments
    push_signature(args, mt)
    ysig = to_signature_aux3(yargs, -1, cache)
    args[1].type = nil
    args[1].decide_type_once(ysig)
    res[1] = args[1].type
    pop_signature
  end
  
  res
end

#to_signature_aux3(cursignode, offset, cache) ⇒ Object



201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/ytljit/vm_codegen.rb', line 201

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

  res = cursignode.map { |enode|
    cursignode = @current_method_signature_node[offset]
    sig = to_signature_aux3(cursignode, offset - 1, cache)
    enode.decide_type_once(sig)
  }
  cache[cursignode] = res
  
  res
end