Class: CastOff::Compiler::SimpleIR::TypeContainer

Inherits:
Object
  • Object
show all
Includes:
Util
Defined in:
lib/cast_off/compile/ir/operand.rb

Direct Known Subclasses

ConstWrapper, Literal, Variable

Constant Summary collapse

FloatWrapper =

unboxing end ###

ClassWrapper.new(Float, true)
FixnumWrapper =
ClassWrapper.new(Fixnum, true)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util

#bug, #dlog, #todo, #vlog

Constructor Details

#initializeTypeContainer

ignore に入っていたら @types に加えないannotation に入っていたら dynamic にするときに、annotation で指定された型にする, ignore に含まれているものは加えない



24
25
26
27
28
29
30
31
# File 'lib/cast_off/compile/ir/operand.rb', line 24

def initialize()
  @types = []
  @state = :undefined
  @annotation = []
  @ignore = []
  @unboxing_state = :unboxed
  @negative_cond_value = false
end

Instance Attribute Details

#stateObject (readonly)

Returns the value of attribute state.



13
14
15
# File 'lib/cast_off/compile/ir/operand.rb', line 13

def state
  @state
end

#typesObject (readonly)

Returns the value of attribute types.



13
14
15
# File 'lib/cast_off/compile/ir/operand.rb', line 13

def types
  @types
end

Instance Method Details

#boxObject



53
54
55
56
57
58
59
60
61
62
# File 'lib/cast_off/compile/ir/operand.rb', line 53

def box()
  case @unboxing_state
  when :unboxed
    @unboxing_state = :boxed
    return true
  when :boxed
    return false
  end
  bug()
end

#boxed?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/cast_off/compile/ir/operand.rb', line 64

def boxed?
  @unboxing_state == :boxed
end

#boxed_formObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cast_off/compile/ir/operand.rb', line 72

def boxed_form()
  case @unboxing_state
  when :boxed
    return self.to_s
  when :unboxed
    bug() unless can_unbox?
    case @types.first
    when FixnumWrapper
      return "LONG2FIX(#{self.to_s})"
    when FloatWrapper
      return "DBL2NUM(#{self.to_s})"
    end
  end
  bug()
end

#can_unbox?Boolean

unboxing begin ###

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cast_off/compile/ir/operand.rb', line 34

def can_unbox?
  if instance_of?(LocalVariable) || instance_of?(TmpVariable) || instance_of?(Literal)
    if dynamic? || @types.size != 1
      false
    else
      bug() unless @types[0].instance_of?(ClassWrapper)
      case @types[0]
      when FloatWrapper, FixnumWrapper
        true
      else
        false
      end
    end
  else
    # ConstWrapper, Self, Pointer, Argument, TmpBuffer
    false
  end
end

#class_exact?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'lib/cast_off/compile/ir/operand.rb', line 141

def class_exact?
  bug()
end

#declareObject



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/cast_off/compile/ir/operand.rb', line 109

def declare()
  case declare_class()
  when :double
    return 'double'
  when :long
    return 'long'
  when :VALUE
    return 'VALUE'
  end
  bug()
end

#declare_classObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/cast_off/compile/ir/operand.rb', line 91

def declare_class()
  case @unboxing_state
  when :unboxed
    bug() if dynamic?
    bug() unless @types.size == 1
    c = @types[0]
    case c
    when FloatWrapper
      return :double
    when FixnumWrapper
      return :long
    end
  when :boxed
    return :VALUE
  end
  bug()
end

#dynamic?Boolean

Returns:

  • (Boolean)


206
207
208
# File 'lib/cast_off/compile/ir/operand.rb', line 206

def dynamic?()
  @state == :dynamic
end

#is_also(t) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/cast_off/compile/ir/operand.rb', line 173

def is_also(t)
  t = convert(t)
  return false if @ignore.include?(t)
  case @state
  when :undefined
    @state = :initialized
    bug() unless @types.empty?
    @types << t
    return true
  when :initialized
    bug() if @types.empty?
    if @types.include?(t)
      return false
    else
      @types << t
      return true
    end
  when :static
    bug() if @types.empty?
    bug() unless @types.include?(t)
    return false
  when :dynamic
    return false
  else
    bug()
  end
  bug()
end

#is_also?(c) ⇒ Boolean

Returns:

  • (Boolean)


271
272
273
274
275
276
277
# File 'lib/cast_off/compile/ir/operand.rb', line 271

def is_also?(c)
  case c
  when Class, ClassWrapper
    return is_also_class(c)
  end
  bug()
end

#is_annotated(types) ⇒ Object



249
250
251
252
253
254
255
256
257
258
259
# File 'lib/cast_off/compile/ir/operand.rb', line 249

def is_annotated(types)
  # 代入の対象となるような変数(local, instance, class, global variable)は、
  # static ではなく annotated とする。
  # initialized にしちゃうと、dynamic と union したときに、dynamic になってしまうのがもどかしい。
  bug() unless @state == :undefined
  bug() unless @annotation.empty?
  bug() unless @ignore.empty?
  bug() if types.empty?
  @annotation = types.map{|t| convert(t)}
  @annotation.freeze()
end

#is_class_exactObject



145
146
147
# File 'lib/cast_off/compile/ir/operand.rb', line 145

def is_class_exact()
  bug()
end

#is_dynamicObject



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/cast_off/compile/ir/operand.rb', line 210

def is_dynamic()
  return false if @state == :dynamic
  return false if @state == :static
  if annotated?
    if (@annotation - @ignore - @types).empty?
      return false
    else
      @state = :initialized
      @types |= (@annotation - @ignore)
      return true
    end
  else
    @state = :dynamic
    return true
  end
  bug()
end

#is_just?(c) ⇒ Boolean

Returns:

  • (Boolean)


261
262
263
264
265
266
267
268
269
# File 'lib/cast_off/compile/ir/operand.rb', line 261

def is_just?(c)
  case c
  when Class, ClassWrapper
    return is_just_class(c)
  when Array
    return is_just_classes(c)
  end
  bug()
end

#is_negative_cond_valueObject



334
335
336
# File 'lib/cast_off/compile/ir/operand.rb', line 334

def is_negative_cond_value
  @negative_cond_value = true
end

#is_not(types) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/cast_off/compile/ir/operand.rb', line 159

def is_not(types)
  bug() if @state == :static
  bug() unless (@types & @ignore).empty?
  bug() if types.empty?
  types = types.map{|t| convert(t)}
  if (types - @ignore).empty?
    false
  else
    @ignore |= types
    @types  -= types
    true
  end
end

#is_static(types) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/cast_off/compile/ir/operand.rb', line 232

def is_static(types)
  bug() if types.empty?
  types = types.map{|t| convert(t)}
  bug() unless (@ignore & types).empty?
  case @state
  when :undefined
    bug() unless @types.empty?
    @types = types
    @state = :static
    return true
  when :static
    bug() unless @types == types
    return false
  end
  bug()
end

#not_initializedObject



153
154
155
156
157
# File 'lib/cast_off/compile/ir/operand.rb', line 153

def not_initialized()
  # @ignore のせいで :undefined になっているものとしか交わらなかったため、initialize されなかったものがある。
  # そのため、@ignore が空のときに、ここに来る場合がある。
  @state = :dynamic
end

#resetObject



149
150
151
# File 'lib/cast_off/compile/ir/operand.rb', line 149

def reset()
  bug()
end

#static?Boolean

Returns:

  • (Boolean)


228
229
230
# File 'lib/cast_off/compile/ir/operand.rb', line 228

def static?()
  @state == :static
end

#to_debug_stringObject



125
126
127
# File 'lib/cast_off/compile/ir/operand.rb', line 125

def to_debug_string()
  "#{self}(#{dynamic? ? 'dynamic' : @types.join(", ")})"
end

#to_nameObject



121
122
123
# File 'lib/cast_off/compile/ir/operand.rb', line 121

def to_name()
  bug()
end

#to_sObject



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/cast_off/compile/ir/operand.rb', line 129

def to_s()
  case declare_class()
  when :double
    return "#{to_name()}_Float"
  when :long
    return "#{to_name()}_Fixnum"
  when :VALUE
    return to_name()
  end
  bug()
end

#unboxed?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/cast_off/compile/ir/operand.rb', line 68

def unboxed?
  @unboxing_state == :unboxed
end

#undefined?Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/cast_off/compile/ir/operand.rb', line 202

def undefined?()
  @state == :undefined
end

#union(v) ⇒ Object



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# File 'lib/cast_off/compile/ir/operand.rb', line 279

def union(v)
  bug() if (v.types + @types).find{|t| not t.is_a?(ClassWrapper)}
  union_types = v.types - @ignore
  case @state
  when :undefined
    bug() unless @types.empty?
    case v.state
    when :undefined
      return false
    when :initialized, :static
      return false if union_types.empty?
      @state = :initialized
      @types |= union_types
      return true
    when :dynamic
      return is_dynamic()
    else
      bug()
    end
  when :initialized
    bug() if @types.empty?
    case v.state
    when :undefined
      return false
    when :initialized, :static
      if (union_types - @types).empty?
        return false
      else
        @types |= union_types
        return true
      end
    when :dynamic
      return is_dynamic()
    else
      bug()
    end
  when :static
    bug() if @types.empty?
    case v.state
    when :undefined, :dynamic
      return false
    when :initialized, :static
      bug() unless (union_types - @types).empty? || negative_cond_value?
      return false
    else
      bug()
    end
  when :dynamic
    return false
  else
    bug()
  end
  bug()
end