Class: CastOff::Compiler::SimpleIR::JumpIR

Inherits:
IR
  • Object
show all
Defined in:
lib/cast_off/compile/ir/jump_ir.rb

Instance Attribute Summary collapse

Attributes inherited from IR

#alias, #insn

Instance Method Summary collapse

Methods inherited from IR

#add_sampling_variable, #alive, #alive?, #dispatch_method?, #generate_guard, #get_definition, #get_definition_str, #get_usage, #get_variable, #inlining_target?, #propergate_guard_usage, #reset, #sampling_variable, #set_info, #standard_guard_target, #vanish, #vanish?

Methods included from Util

#bug, #dlog, #todo, #vlog

Constructor Details

#initialize(val, insn, cfg) ⇒ JumpIR

Returns a new instance of JumpIR.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cast_off/compile/ir/jump_ir.rb', line 8

def initialize(val, insn, cfg)
  super(insn, cfg)

  @cond_value = val
  @jump_type = insn.op
  argv = insn.argv
  case @jump_type
  when :jump, :branchif, :branchunless, :cast_off_enter_block, :cast_off_leave_block, :cast_off_continue_loop
    @jump_targets = [argv[0]]
    @must = nil
    @post = nil
  when :cast_off_break_block
    @jump_targets = [argv[0]]
    @raw_state = argv[1]
    bug() unless @raw_state
    @exc_pc = argv[2]
    bug() unless @exc_pc >= 0
    @must = nil
    @post = nil
  when :cast_off_handle_optional_args
    @jump_targets = argv[0]
    @must = argv[1]
    @post = argv[2]
  else
    bug()
  end
  bug() unless @jump_targets.is_a?(Array)
  @values = @cond_value ? [@cond_value] : []
  @variables = []
  @variables_without_result = []
  if @cond_value.is_a?(Variable)
    @variables << @cond_value
    @variables_without_result << @cond_value
  end
  @result_variable = nil
end

Instance Attribute Details

#cond_valueObject (readonly)

Returns the value of attribute cond_value.



6
7
8
# File 'lib/cast_off/compile/ir/jump_ir.rb', line 6

def cond_value
  @cond_value
end

#jump_targetsObject (readonly)

Returns the value of attribute jump_targets.



6
7
8
# File 'lib/cast_off/compile/ir/jump_ir.rb', line 6

def jump_targets
  @jump_targets
end

#jump_typeObject (readonly)

Returns the value of attribute jump_type.



6
7
8
# File 'lib/cast_off/compile/ir/jump_ir.rb', line 6

def jump_type
  @jump_type
end

#result_variableObject (readonly)

Returns the value of attribute result_variable.



6
7
8
# File 'lib/cast_off/compile/ir/jump_ir.rb', line 6

def result_variable
  @result_variable
end

#valuesObject (readonly)

Returns the value of attribute values.



6
7
8
# File 'lib/cast_off/compile/ir/jump_ir.rb', line 6

def values
  @values
end

#variablesObject (readonly)

Returns the value of attribute variables.



6
7
8
# File 'lib/cast_off/compile/ir/jump_ir.rb', line 6

def variables
  @variables
end

#variables_without_resultObject (readonly)

Returns the value of attribute variables_without_result.



6
7
8
# File 'lib/cast_off/compile/ir/jump_ir.rb', line 6

def variables_without_result
  @variables_without_result
end

Instance Method Details

#mark(defs) ⇒ Object



199
200
201
202
203
204
205
206
207
# File 'lib/cast_off/compile/ir/jump_ir.rb', line 199

def mark(defs)
  if !alive?
    alive()
    defs.mark(@cond_value) if @cond_value
    true
  else
    false
  end
end

#propergate_boxed_value(defs) ⇒ Object



50
51
52
53
54
# File 'lib/cast_off/compile/ir/jump_ir.rb', line 50

def propergate_boxed_value(defs)
  return false unless @cond_value
  bug() unless @cond_value.boxed?
  defs.propergate_boxed_value_backward(@cond_value)
end

#propergate_exact_class(defs) ⇒ Object

unboxing end ###



57
58
59
# File 'lib/cast_off/compile/ir/jump_ir.rb', line 57

def propergate_exact_class(defs)
  @cond_value ? defs.exact_class_resolve(@cond_value) : false
end

#to_cObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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
# File 'lib/cast_off/compile/ir/jump_ir.rb', line 65

def to_c()
  ret = []
  # FIXME detect backedge
  # FIXME add interrupt check when ehable intterupt check option
  s = sampling_variable()
  ret << s if s
  case @jump_type
  when :cast_off_enter_block
    if @translator.inline_block?
      bug() unless @jump_targets.size() == 1
      ret << "  goto #{@jump_targets[0]};"
    end
  when :cast_off_leave_block
    if @translator.inline_block?
      bug() unless @jump_targets.size() == 1
      ret << "  goto #{@jump_targets[0]};"
    else
      ret << "  return tmp#{@insn.iseq.depth};" # FIXME
    end
  when :cast_off_break_block
    if @translator.inline_block?
      bug() unless @jump_targets.size() == 1
      ret << "  goto #{@jump_targets[0]};"
    else
      ret << "  break_block(#{@raw_state}, #{@exc_pc}, tmp#{@insn.iseq.depth});" # FIXME
    end
  when :cast_off_continue_loop
    if @translator.inline_block?
      bug() unless @jump_targets.size() == 1
      ret << "  if (RTEST(#{@cond_value})) goto #{@jump_targets[0]};"
    end
  when :jump
    bug() unless @jump_targets.size() == 1
    #ret << "  RUBY_VM_CHECK_INTS();"
    ret << "  goto #{@jump_targets[0]};"
  when :branchunless
    bug() unless @jump_targets.size() == 1
    ret << "  if (!RTEST(#{@cond_value})) goto #{@jump_targets[0]};"
=begin
      ret << <<-EOS
  if (!RTEST(#{@cond_value})) {
    RUBY_VM_CHECK_INTS();
    goto #{@jump_targets[0]};
  }
      EOS
=end
    when :branchif
      bug() unless @jump_targets.size() == 1
      ret << "  if (RTEST(#{@cond_value})) goto #{@jump_targets[0]};"
=begin
      ret << <<-EOS
  if (RTEST(#{@cond_value})) {
    RUBY_VM_CHECK_INTS();
    goto #{@jump_targets[0]};
  }
      EOS
=end
  when :cast_off_handle_optional_args
    bug() unless @jump_targets.size() > 1
    bug() unless @cond_value.is_just?(Fixnum)
    ret << "  switch(#{@cond_value}) {"
    @jump_targets.each_with_index do |t, i|
      ret << "    case(INT2FIX(#{i + @must})): goto #{t};"
    end
    if @post
      ret << <<-EOS
    default: {
int num = FIX2INT(#{@cond_value});
if (num >= #{@jump_targets.size() + @must}) {
  goto #{@jump_targets.last};
} else {
  rb_bug("should not be reached");
}
    }
      EOS
    else
      ret << "    default: rb_bug(\"should not be reached\");"
    end
    ret << "  }"
  else
    bug("unexpected jump type #{@jump_type}")
  end
  ret.join("\n")
end

#to_debug_stringObject



61
62
63
# File 'lib/cast_off/compile/ir/jump_ir.rb', line 61

def to_debug_string()
  "Jump(#{@jump_type}#{@cond_value ? " : #{@cond_value.to_debug_string()}" : ''}) => #{@jump_targets}"
end

#type_propergation(defs) ⇒ Object



150
151
152
# File 'lib/cast_off/compile/ir/jump_ir.rb', line 150

def type_propergation(defs)
  @cond_value ? defs.type_resolve(@cond_value) : false
end

#unboxing_preludeObject

unboxing begin ###



46
47
48
# File 'lib/cast_off/compile/ir/jump_ir.rb', line 46

def unboxing_prelude()
  @cond_value.box() if @cond_value
end

#unused_targetObject



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
197
# File 'lib/cast_off/compile/ir/jump_ir.rb', line 154

def unused_target()
  case @jump_type
  when :jump
    bug()
  when :cast_off_handle_optional_args, :cast_off_enter_block, :cast_off_leave_block, :cast_off_continue_loop, \
       :cast_off_break_block
    # nothing to do
    return nil
  when :branchif, :branchunless
    bug() if @cond_value.undefined?
    return nil if @cond_value.dynamic?
    nil_wrapper = ClassWrapper.new(NilClass, true)
    false_wrapper = ClassWrapper.new(FalseClass, true)
    classes = @cond_value.types
    bug() if classes.empty?
    constant = true
    bool = nil
    classes.each do |c|
      if c == nil_wrapper || c == false_wrapper
        __bool = false
      else
        __bool = true
      end
      if bool.nil?
        bool = __bool
      else
        unless bool == __bool
          constant = false
          break
        end
      end
    end
    if constant
      bug() if bool.nil?
      fallthrough = (bool && @jump_type == :branchunless) || (!bool && @jump_type == :branchif)
      bug() if @jump_targets.size() != 1
      target = @jump_targets[0]
      return fallthrough ? target : :fallthrough
    else
      return nil
    end
  end
  bug()
end