Class: CastOff::Compiler::SimpleIR::InvokeIR

Inherits:
CallIR
  • Object
show all
Defined in:
lib/cast_off/compile/ir/call_ir.rb

Constant Summary collapse

IncompatMethods =
[
  :eval,         # use vm_get_ruby_level_caller_cfp
  :binding,      # use vm_get_ruby_level_caller_cfp
  :block_given?, # use vm_get_ruby_level_caller_cfp
  :iterator?,    # use vm_get_ruby_level_caller_cfp
]
SPLATCALL_TEMPLATE_CFUNC =
ERB.new(<<-EOS, 0, '%-', 'io')
%bug() unless param.size > 0
  {
    int argc = <%= argc - 2 %>;
%splat_param  = param.last()
%normal_param = param.slice(0, param.size() - 1)
    VALUE ary = <%= splat_param %>;
%if splat_param.is_just?(Array)
%  is_ary = true
    VALUE tmp = ary;
%else
%  is_ary = false
    VALUE tmp = rb_check_convert_type(ary, T_ARRAY, "Array", "to_a");
%end
    VALUE *ptr = NULL;

%unless is_ary
    if (NIL_P(tmp)) {
/* do nothing */
    } else {
%end
ptr = RARRAY_PTR(tmp);
argc += RARRAY_LEN(tmp);
%unless is_ary
    }
%end
    if (UNLIKELY(argc != <%= splat_call_argc %>)) {
rb_raise(rb_eCastOffExecutionError, "wrong number of arguments (<%= splat_call_argc %> for %d)", argc);
    }
%unless is_ary
    if (ptr) {
%end
%rest = ""
%(splat_call_argc - normal_param.size).times do |i|
%  rest += ", ptr[\#{i}]"
%end
<%= @return_value %> = <%= fnam_code %>(<%= recv %><%= normal_param.empty? ? nil : ", \#{normal_param.join(", ")}" %><%= rest %>);
%unless is_ary
    } else {
<%= @return_value %> = <%= fnam_code %>(<%= recv %><%= normal_param.empty? ? nil : ", \#{normal_param.join(", ")}" %>);
    }
%end
  }
EOS
SPLATCALL_TEMPLATE_ARGV =
ERB.new(<<-EOS, 0, '%-', 'io')
%bug() unless param.size > 0
  {
    int argc = <%= argc - 2 %>;
    VALUE buf[<%= SPLATCALL_LIMIT %>];
    VALUE *argv = buf;
%splat_param  = param.last()
%normal_param = param.slice(0, param.size() - 1)
    VALUE ary = <%= splat_param %>;
%if splat_param.is_just?(Array)
%  is_ary = true
    VALUE tmp = ary;
%else
%  is_ary = false
    VALUE tmp = rb_check_convert_type(ary, T_ARRAY, "Array", "to_a");
%end

%normal_param.each_with_index do |p, i|
    argv[<%= i %>] = <%= p %>;
%end
%unless is_ary
    if (NIL_P(tmp)) {
/* do nothing */
    } else {
%else
    {
%end
VALUE *ptr;
long i, len = RARRAY_LEN(tmp);
ptr = RARRAY_PTR(tmp);
argc += len;
if (UNLIKELY(argc > <%= SPLATCALL_LIMIT %>)) {
  VALUE *newbuf = ALLOCA_N(VALUE, argc);
%normal_param.size.times do |i|
  newbuf[i] = argv[i];
%end
  argv = newbuf;
}
for (i = 0; i < len; i++) {
  argv[<%= normal_param.size %> + i] = ptr[i];
}
    }
    <%= call_code %>
  }
EOS
StringWrapper =
ClassWrapper.new(String, true)
FixnumWrapper =
ClassWrapper.new(Fixnum, true)
FloatWrapper =
ClassWrapper.new(Float, true)
ArrayWrapper =
ClassWrapper.new(Array, true)
ObjectWrapper =
ClassWrapper.new(Object, true)
NilClassWrapper =
ClassWrapper.new(NilClass, true)
FalseClassWrapper =
ClassWrapper.new(FalseClass, true)
BasicObjectWrapper =
ClassWrapper.new(BasicObject, true)
KernelWrapper =
ModuleWrapper.new(Kernel)
SpecializeTable0 =
{
  [MethodWrapper.new(StringWrapper, :<<), 2]     => [['concat',       [String], nil, String, false, false]],
  [MethodWrapper.new(StringWrapper, :+), 2]      => [['plus',         [String], nil, String, false, false]],
  [MethodWrapper.new(StringWrapper, :==), 2]     => [['eq',           [String], nil, [TrueClass,  FalseClass], false, false]],
  [MethodWrapper.new(StringWrapper, :===), 2]    => [['eqq',          [String], nil, [TrueClass,  FalseClass], false, false]],
  [MethodWrapper.new(StringWrapper, :!=), 2]     => [['neq',          [String], nil, [TrueClass,  FalseClass], false, false]],
  [MethodWrapper.new(StringWrapper, :empty?), 1] => [['empty_p',      [], nil, [TrueClass,  FalseClass], false, false]],
  [MethodWrapper.new(ArrayWrapper,  :[]), 2]     => [['entry',        [Fixnum], nil, nil, false, false]],
  [MethodWrapper.new(ArrayWrapper,  :[]=), 3]    => [['store',        [Fixnum, nil], nil, nil, false, false]],
  [MethodWrapper.new(ArrayWrapper,  :length), 1] => [['length',       [], Fixnum, nil, false, false]], # FIXME
  [MethodWrapper.new(ArrayWrapper,  :size), 1]   => [['size',         [], Fixnum, nil, false, false]], # FIXME
  [MethodWrapper.new(ArrayWrapper,  :empty?), 1] => [['empty_p',      [], nil, [TrueClass,  FalseClass], false, false]],
  [MethodWrapper.new(ArrayWrapper,  :last), 1]   => [['last',         [], nil, nil, false, false]],
  [MethodWrapper.new(ArrayWrapper,  :first), 1]  => [['first',        [], nil, nil, false, false]],
  [MethodWrapper.new(FixnumWrapper, :+), 2]      => [['fixnum_plus',  [Fixnum], nil, [Fixnum, Bignum], false, true], ['float_plus',   [Float],   nil, Float, true, true]], # FIXME
  [MethodWrapper.new(FixnumWrapper, :-), 2]      => [['fixnum_minus', [Fixnum], nil, [Fixnum, Bignum], false, true], ['float_minus',  [Float],   nil, Float, true, true]], # FIXME
  [MethodWrapper.new(FixnumWrapper, :*), 2]      => [['fixnum_mult',  [Fixnum], nil, [Fixnum, Bignum], false, true], ['float_mult',   [Float],   nil, Float, true, true]], # FIXME
  [MethodWrapper.new(FixnumWrapper, :<=), 2]     => [['le',           [Fixnum], nil, [TrueClass,  FalseClass], false, false]],
  [MethodWrapper.new(FixnumWrapper, :<), 2]      => [['lt',           [Fixnum], nil, [TrueClass,  FalseClass], false, false]],
  [MethodWrapper.new(FixnumWrapper, :>=), 2]     => [['ge',           [Fixnum], nil, [TrueClass,  FalseClass], false, false]],
  [MethodWrapper.new(FixnumWrapper, :>), 2]      => [['gt',           [Fixnum], nil, [TrueClass,  FalseClass], false, false]],
  [MethodWrapper.new(FixnumWrapper, :==), 2]     => [['eq',           [Fixnum], nil, [TrueClass,  FalseClass], false, false]],
  [MethodWrapper.new(FixnumWrapper, :===), 2]    => [['eqq',          [Fixnum], nil, [TrueClass,  FalseClass], false, false]],
  [MethodWrapper.new(FixnumWrapper, :&), 2]      => [['and',          [Fixnum], Fixnum, nil, false, false]], # FIXME
  [MethodWrapper.new(FloatWrapper,  :+), 2]      => [['float_plus',   [Float],   nil, Float, true, true], ['fixnum_plus',   [Fixnum],  nil, Float, true, true]],
  [MethodWrapper.new(FloatWrapper,  :-), 2]      => [['float_minus',  [Float],   nil, Float, true, true], ['fixnum_minus',  [Fixnum],  nil, Float, true, true]],
  [MethodWrapper.new(FloatWrapper,  :*), 2]      => [['float_mult',   [Float],   nil, Float, true, true], ['fixnum_mult',   [Fixnum],  nil, Float, true, true]],
  [MethodWrapper.new(FloatWrapper,  :/), 2]      => [['float_div',    [Float],   nil, Float, true, true], ['fixnum_div',    [Fixnum],  nil, Float, true, true]],
  [MethodWrapper.new(FloatWrapper,  :<=), 2]     => [['float_le',     [Float],   nil, [TrueClass,  FalseClass], false, true],
                                                     ['fixnum_le',    [Fixnum],  nil, [TrueClass,  FalseClass], false, true]],
  [MethodWrapper.new(FloatWrapper,  :<), 2]      => [['float_lt',     [Float],   nil, [TrueClass,  FalseClass], false, true],
                                                     ['fixnum_lt',    [Fixnum],  nil, [TrueClass,  FalseClass], false, true]],
  [MethodWrapper.new(FloatWrapper,  :>=), 2]     => [['float_ge',     [Float],   nil, [TrueClass,  FalseClass], false, true],
                                                     ['fixnum_ge',    [Fixnum],  nil, [TrueClass,  FalseClass], false, true]],
  [MethodWrapper.new(FloatWrapper,  :>), 2]      => [['float_gt',     [Float],   nil, [TrueClass,  FalseClass], false, true],
                                                     ['fixnum_gt',    [Fixnum],  nil, [TrueClass,  FalseClass], false, true]],
  [MethodWrapper.new(FloatWrapper,  :==), 2]     => [['float_eq',     [Float],   nil, [TrueClass,  FalseClass], false, true],
                                                     ['fixnum_eq',    [Fixnum],  nil, [TrueClass,  FalseClass], false, true]],
  [MethodWrapper.new(FixnumWrapper, :!=), 2]     => [['fixnum_neq',   [Fixnum],  nil, [TrueClass,  FalseClass], false, true],
                                                     ['float_neq',    [Float],   nil, [TrueClass,  FalseClass], false, true]],
  [MethodWrapper.new(FloatWrapper,  :!=), 2]     => [['float_neq',    [Float],   nil, [TrueClass,  FalseClass], false, true],
                                                     ['fixnum_neq',   [Fixnum],  nil, [TrueClass,  FalseClass], false, true]],
  [MethodWrapper.new(FloatWrapper,  :===), 2]    => [['float_eqq',    [Float],   nil, [TrueClass,  FalseClass], false, true],
                                                     ['fixnum_eqq',   [Fixnum],  nil, [TrueClass,  FalseClass], false, true]],
  [MethodWrapper.new(FixnumWrapper, :-@), 1]     => [['uminus',       [], nil, [Fixnum, Bignum], false, true]],
  [MethodWrapper.new(FloatWrapper, :-@), 1]      => [['uminus',       [], nil, Float, true, true]],
  [MethodWrapper.new(FixnumWrapper, :to_f), 1]   => [['to_f',         [], nil, Float, true, true]],
  [MethodWrapper.new(FloatWrapper, :to_f), 1]    => [['to_f',         [], nil, Float, true, false]],
  [MethodWrapper.new(FloatWrapper, :to_i), 1]    => [['to_i',         [], nil, Float, false, true]],
}
SpecializeTable1 =
{
  [:==,   2, MethodWrapper.new(ObjectWrapper, :==)]      => :specialized_object_eq,
  [:nil?, 1, MethodWrapper.new(NilClassWrapper, :nil?)]  => :specialized_nilclass_nil?,
  [:nil?, 1, MethodWrapper.new(KernelWrapper, :nil?)]    => :specialized_kernel_nil?,
  [:!,    1, MethodWrapper.new(BasicObjectWrapper, :!)]  => :specialized_basicobject_not,
}
FuncallThreshold =
10
MultiCallTemplate =
ERB.new(<<-'end', 0, '%-', 'io')
%ary = []
%mid = @method_id
%if types.size < FuncallThreshold
%  funcall = false
%  types.each do |klass|
%    if @translator.get_c_classname(klass)
%      call_code = not_funcall_code(klass, mid, recv, param, @argc)
%      if call_code
%        ary << [klass, call_code]
%      else
%        begin
%          @dependency.add(klass, @method_id, false)
%          ary << [klass, funcall_code(klass, id, recv, param, @argc)]
%        rescue CompileError => e
%          funcall = true
%          dlog("catch exception #{e}")
%        end
%      end
%    else
%      funcall = true
%    end
%  end
%else
%  funcall = true
%end
%if ary.empty?
%  bug() unless funcall
<%= funcall_code(nil, id, recv, param, @argc) %>
%elsif !funcall && same_call_code?(ary)
<%= ary[0][1] %>
%else
  cast_off_tmp = rb_class_of(<%= recv %>);
  if (0) {
%  ary.each do |(klass, call_code)|
  } else if (cast_off_tmp == <%= @translator.get_c_classname(klass) %>) {
  <%= call_code %>
%  end
  } else {
%  if funcall
  <%= funcall_code(nil, id, recv, param, @argc) %>
%  else # empty_method_table_p に通った特異クラスがくる可能性がある。
  <%= funcall_code(nil, id, recv, param, @argc) %>
%  end
  }
%end

end

Constants included from Instruction

Instruction::BlockSeparator, Instruction::BranchInstruction, Instruction::IgnoreInstruction, Instruction::JumpOrReturnInstruction, Instruction::SupportInstruction, Instruction::TypeInfoUser, Instruction::VM_CALL_ARGS_BLOCKARG_BIT, Instruction::VM_CALL_ARGS_SPLAT_BIT, Instruction::VM_CALL_FCALL_BIT, Instruction::VM_CALL_OPT_SEND_BIT, Instruction::VM_CALL_SUPER_BIT, Instruction::VM_CALL_TAILCALL_BIT, Instruction::VM_CALL_TAILRECURSION_BIT, Instruction::VM_CALL_VCALL_BIT

Instance Attribute Summary collapse

Attributes inherited from CallIR

#argc, #result_variable, #return_value, #values, #variables, #variables_without_result

Attributes inherited from IR

#alias, #insn

Instance Method Summary collapse

Methods inherited from CallIR

#can_not_unbox, #mark, #param_irs, #param_variables, #propergate_boxed_value, #sampling_return_value, #sampling_return_value?

Methods included from Util

#bug, #dlog, #todo, #vlog

Methods inherited from IR

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

Constructor Details

#initialize(mid, flags, param, argc, return_value, insn, cfg) ⇒ InvokeIR

Returns a new instance of InvokeIR.



1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
# File 'lib/cast_off/compile/ir/call_ir.rb', line 1495

def initialize(mid, flags, param, argc, return_value, insn, cfg)
  super(param, argc, return_value, insn, cfg)
  @flags = flags
  @method_id = mid

  if fcall? && IncompatMethods.include?(@method_id)
    raise(UnsupportedError.new(<<-EOS))

Currently, #{@method_id} method is incompatible.
You should not use #{@method_id} method in compilation target of CastOff.
    EOS
  end
end

Instance Attribute Details

#method_idObject (readonly)

Returns the value of attribute method_id.



1486
1487
1488
# File 'lib/cast_off/compile/ir/call_ir.rb', line 1486

def method_id
  @method_id
end

Instance Method Details

#blockarg?Boolean

Returns:

  • (Boolean)


1556
1557
1558
1559
# File 'lib/cast_off/compile/ir/call_ir.rb', line 1556

def blockarg?
  bug() unless @flags
  (@flags & VM_CALL_ARGS_BLOCKARG_BIT) != 0
end

#call_cfunc_code(klass, mid, cfunc_argc, recv, param, argc) ⇒ Object



1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
# File 'lib/cast_off/compile/ir/call_ir.rb', line 1872

def call_cfunc_code(klass, mid, cfunc_argc, recv, param, argc)
  bug() unless klass.is_a?(ClassWrapper)
  bug() unless @configuration.should_be_call_directly?(klass, mid)
  if !splatcall? && sp = specialized_code(klass, mid, argc, recv, param)
    return sp
  else
    id = @translator.allocate_id(mid)
    ary = []
    if @configuration.enable_trace?
      ary << "#ifdef CAST_OFF_ENABLE_TRACE"
      ary << "  trace_recv = #{recv};  trace_klass = rb_class_of(#{recv});"
      ary << "  EXEC_EVENT_HOOK(th, RUBY_EVENT_C_CALL, trace_recv, #{id}, trace_klass);"
      ary << "#endif"
    end
    fptr = @translator.allocate_function_pointer(klass, mid, cfunc_argc, param.size)
    case cfunc_argc
    when -2
      if splatcall?
        return nil # use funcall
      else
        c_ary = @insn.iseq.use_temporary_c_ary(param.size)
        param.each_with_index do |arg, i|
          ary << "  #{c_ary}[#{i}] = #{arg};"
        end
        ary << "  #{@return_value} = (*#{fptr})(#{recv}, rb_ary_new4(#{argc - 1}, #{c_ary}));"
      end
    when -1
      if splatcall?
        call_code = "#{@return_value} = (*#{fptr})(argc, argv, #{recv});"
        ary << SPLATCALL_TEMPLATE_ARGV.trigger(binding)
      else
        c_ary = @insn.iseq.use_temporary_c_ary(param.size)
        param.each_with_index do |arg, i|
          ary << "  #{c_ary}[#{i}] = #{arg};"
        end
        ary << "  #{@return_value} = (*#{fptr})(#{argc - 1}, #{c_ary}, #{recv});"
      end
    when 0..15
      if splatcall?
        splat_call_argc = cfunc_argc
        fnam_code = "(*#{fptr})"
        ary << SPLATCALL_TEMPLATE_CFUNC.trigger(binding)
      else
        ary << "  #{@return_value} = (*#{fptr})(#{recv}#{param.empty? ? nil : ", #{param.join(", ")}"});"
      end
    else
      raise(CompileError.new("too many arguments #{klass}##{mid}"))
    end
    if @configuration.enable_trace?
      ary << "#ifdef CAST_OFF_ENABLE_TRACE"
      ary << "  EXEC_EVENT_HOOK(th, RUBY_EVENT_C_RETURN, trace_recv, #{id}, trace_klass);"
      ary << "#endif"
    end
    return ary.join("\n")
  end
  bug()
end

#fcall?Boolean

Returns:

  • (Boolean)


1551
1552
1553
1554
# File 'lib/cast_off/compile/ir/call_ir.rb', line 1551

def fcall?
  bug() unless @flags
  (@flags & VM_CALL_FCALL_BIT) != 0
end

#funcall_code(klass, id, recv, param, argc) ⇒ Object



1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
# File 'lib/cast_off/compile/ir/call_ir.rb', line 1662

def funcall_code(klass, id, recv, param, argc)
  if blockarg?
    bug() if param.empty?
    blockarg = param.last()
    param = param.slice(0, param.size - 1)
    argc = argc - 1
    ret = "  handle_blockarg(#{blockarg});\n" 
    if splatcall?
      fptr = funcall_fptr(klass, nil, 2)
      call_code = "#{@return_value} = #{fptr}(#{recv}, #{id}, argc, argv);"
      ret += SPLATCALL_TEMPLATE_ARGV.trigger(binding)
    else
      fptr = funcall_fptr(klass, param.size)
      ret += "  #{@return_value} = #{fptr}(#{recv}, #{id}, #{param.size}#{param.empty? ? nil : ", #{param.join(", ")}"});"
    end
    ret
  else
    if splatcall?
      fptr = funcall_fptr(klass, nil, 2)
      call_code = "#{@return_value} = #{fptr}(#{recv}, #{id}, argc, argv);"
      SPLATCALL_TEMPLATE_ARGV.trigger(binding)
    else
      fptr = funcall_fptr(klass, param.size)
      "  #{@return_value} = #{fptr}(#{recv}, #{id}, #{argc - 1}#{param.empty? ? nil : ", #{param.join(", ")}"});"
    end
  end
end

#funcall_fptr(klass, argc, suffix = nil) ⇒ Object



1657
1658
1659
1660
# File 'lib/cast_off/compile/ir/call_ir.rb', line 1657

def funcall_fptr(klass, argc, suffix = nil)
  bug() unless klass.nil? || klass.instance_of?(ClassWrapper)
  (klass && !suffix) ? @translator.allocate_function_pointer(klass, @method_id, -3, argc) : "rb_funcall#{suffix}"
end

#harmless?(recv_p) ⇒ Boolean

Returns:

  • (Boolean)


2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
# File 'lib/cast_off/compile/ir/call_ir.rb', line 2183

def harmless?(recv_p)
  recv = @param[0].param_value
  bug() if recv.undefined?
  return false if recv.dynamic?
  recv.types.each do |t|
    return false unless @configuration.harmless?(t, @method_id, recv_p)
  end
  recv.types.each do |t|
    ok = @configuration.use_method_information(t, @method_id)
    return false unless ok
    # これのせいで依存するクラスなどが結構増える。読み込みが遅くなる。
    @dependency.add(t, @method_id, true)
  end 
  return true
end

#inlining_target?Boolean

unboxing end ###

Returns:

  • (Boolean)


2301
2302
2303
# File 'lib/cast_off/compile/ir/call_ir.rb', line 2301

def inlining_target?
  false # TODO
end

#not_funcall_code(klass, mid, recv, param, argc) ⇒ Object



2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
# File 'lib/cast_off/compile/ir/call_ir.rb', line 2041

def not_funcall_code(klass, mid, recv, param, argc)
  bug() unless klass.is_a?(ClassWrapper)
  code = recursive_call_code(recv, param, argc) if recursive_call?(klass)
  return code if code
  case klass.get_method_type(mid)
  when :cfunc
    cfunc_argc = klass.get_cfunc_argc(mid)
    bug() unless cfunc_argc
    if @configuration.should_be_call_directly?(klass, mid)
      @dependency.add(klass, mid, false)
      return call_cfunc_code(klass, mid, cfunc_argc, recv, param, argc)
    else
      return nil # shoud be use funcall
    end
  when :attrset
    raise(CompileError.new("invalid call site")) if splatcall? || param.size() != 1
    @dependency.add(klass, mid, false)
    return "  #{@return_value} = rb_ivar_set(#{recv}, #{@translator.allocate_id(klass.get_attr_id(mid))}, #{param[0]});"
  when :ivar
    raise(CompileError.new("invalid call site")) if splatcall? || param.size() != 0
    @dependency.add(klass, mid, false)
    return "  #{@return_value} = rb_attr_get(#{recv}, #{@translator.allocate_id(klass.get_attr_id(mid))});"
  when false
    return nil
  end
  bug()
end

#propergate_exact_class(defs) ⇒ Object



2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
# File 'lib/cast_off/compile/ir/call_ir.rb', line 2228

def propergate_exact_class(defs)
  if @return_value.class_exact?
    false
  elsif class_exact?
    @return_value.is_class_exact() 
    true
  else
    false
  end
end

#propergate_guard_usageObject



1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
# File 'lib/cast_off/compile/ir/call_ir.rb', line 1509

def propergate_guard_usage()
  params = param_irs()
  bug() if params.empty?
  recv = params.shift()
  recv.need_guard(true)
  if splatcall? || specializecall?
    params.each{|p| p.need_guard(true)}
  else
    params.each{|p| p.need_guard(false)}
  end
end

#recursive_call?(klass) ⇒ Boolean

Returns:

  • (Boolean)


1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
# File 'lib/cast_off/compile/ir/call_ir.rb', line 1930

def recursive_call?(klass)
  same_class = false
  bug() unless klass.is_a?(ClassWrapper)
  if @translator.reciever_class && @translator.reciever_class.include?(klass)
    same_class = true
  end
  if same_class && @method_id == @translator.mid
    true
  else
    false
  end
end

#recursive_call_code(recv, param, argc) ⇒ Object



1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
# File 'lib/cast_off/compile/ir/call_ir.rb', line 1949

def recursive_call_code(recv, param, argc)
  return nil if !@translator.inline_block?
  fname = @translator.this_function_name()
  if @translator.complex_call?
    if splatcall?
      call_code = "#{@return_value} = #{fname}(argc, argv, #{recv});"
      recursive_call_depend(recv)
      return SPLATCALL_TEMPLATE_ARGV.trigger(binding)
    else
      ret = ""
      c_ary = @insn.iseq.use_temporary_c_ary(param.size)
      param.each_with_index do |arg, i|
        ret += "  #{c_ary}[#{i}] = #{arg};\n"
      end
      recursive_call_depend(recv)
      return ret + "  #{@return_value} = #{fname}(#{argc - 1}, #{c_ary}, #{recv});"
    end
  else
    if splatcall?
      splat_call_argc = @translator.root_iseq.args.arg_size
      fnam_code = fname
      recursive_call_depend(recv)
      return SPLATCALL_TEMPLATE_CFUNC.trigger(binding)
    else
      if param.size == @translator.root_iseq.args.arg_size
        recursive_call_depend(recv)
        return "  #{@return_value} = #{fname}(#{recv}#{param.empty? ? nil : ", #{param.join(", ")}"});"
      else
        return nil
      end
    end
  end
  bug()
end

#recursive_call_depend(recv) ⇒ Object



1943
1944
1945
1946
1947
# File 'lib/cast_off/compile/ir/call_ir.rb', line 1943

def recursive_call_depend(recv)
  bug() if recv.dynamic?
  classes = recv.types
  classes.each{|klass| @dependency.add(klass, @method_id, true)}
end

#same_call_code?(ary) ⇒ Boolean

Returns:

  • (Boolean)


1984
1985
1986
1987
1988
1989
# File 'lib/cast_off/compile/ir/call_ir.rb', line 1984

def same_call_code?(ary)
  bug() if ary.empty?
  code = ary[0][1]
  ary.each{|(k, c)| return false unless code == c}
  true
end

#should_be_alive?Boolean

Returns:

  • (Boolean)


2224
2225
2226
# File 'lib/cast_off/compile/ir/call_ir.rb', line 2224

def should_be_alive?
  side_effect?
end

#side_effect?Boolean

Returns:

  • (Boolean)


2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
# File 'lib/cast_off/compile/ir/call_ir.rb', line 2199

def side_effect?
  recv = @param[0].param_value
  bug() if recv.undefined?
  if recv.dynamic?
    se = true
  else
    se = false
    recv.types.each do |t|
      if @configuration.side_effect?(t, @method_id)
        se = true
        break
      end
    end
  end
  if !se
    recv.types.each do |t|
      ok = @configuration.use_method_information(t, @method_id)
      return true unless ok
      # これのせいで依存するクラスなどが結構増える。読み込みが遅くなる。
      @dependency.add(t, @method_id, true)
    end 
  end
  se
end

#specializecall?Boolean

Returns:

  • (Boolean)


1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
# File 'lib/cast_off/compile/ir/call_ir.rb', line 1521

def specializecall?
  params = param_variables()
  recv, *args = *params
  bug() if recv.undefined?
  if recv.dynamic? || @configuration.force_dispatch_method?
    return false
  else
    bug() if recv.types.empty?
    recv.types.each do |klass|
      bug() unless klass.is_a?(ClassWrapper)
      next if klass.get_method_type(@method_id) != :cfunc
      next unless @configuration.should_be_call_directly?(klass, @method_id)
      cfunc_argc = klass.get_cfunc_argc(@method_id)
      bug() unless cfunc_argc
      if klass.String? || klass.Array? || klass.Fixnum? || klass.Float?
        # != とかのために、クラス単位で分ける必要がある。
        return true if SpecializeTable0[[MethodWrapper.new(klass, @method_id), @argc]]
      end
      # FIXME specializecall 側を修正
      return true if SpecializeTable1[[@method_id, @argc, MethodWrapper.new(klass, @method_id)]]
    end
  end
  false
end

#specialized_basicobject_not(klass, mid, argc, recv, param) ⇒ Object



1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
# File 'lib/cast_off/compile/ir/call_ir.rb', line 1777

def specialized_basicobject_not(klass, mid, argc, recv, param)
  bug() unless mid == :!
  bug() unless argc == 1
  bug() unless param.empty?
  classes = recv.types
  case classes.size
  when 0
    bug()
  when 1
    case classes[0]
    when NilClassWrapper, FalseClassWrapper
      return "  #{@return_value} = Qtrue;"
    else
      return "  #{@return_value} = Qfalse;"
    end
  when 2
    if classes == [NilClassWrapper, FalseClassWrapper] || classes == [FalseClassWrapper, NilClassWrapper]
      return "  #{@return_value} = Qtrue;"
    elsif !classes.include?(NilClassWrapper) && !classes.include?(FalseClassWrapper)
      return "  #{@return_value} = Qfalse;"
    else
      return "  #{@return_value} = RTEST(#{recv}) ? Qfalse : Qtrue;"
    end
  else
    return "  #{@return_value} = RTEST(#{recv}) ? Qfalse : Qtrue;"
  end
  bug()
end

#specialized_code(klass, mid, argc, recv, param) ⇒ Object



1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
# File 'lib/cast_off/compile/ir/call_ir.rb', line 1822

def specialized_code(klass, mid, argc, recv, param)
  bug() unless klass.is_a?(ClassWrapper)
  return false unless @configuration.enable_inline_api?
  # FIXME ロード時に、関数ポインタが想定しているものと同一であることをチェック
  if klass.String? || klass.Array? || klass.Fixnum? || klass.Float?
    entries = SpecializeTable0[[MethodWrapper.new(klass, mid), argc]]
    return false unless entries
    entry = nil
    entries.each do |e|
      bug() unless e.size == 6
      name, t_param, t_result, exact_classes, can_unbox_result, can_unbox_param = e
      bug("param = #{param}, t_param = #{t_param}") unless param.size == t_param.size
      fin = true
      (param + [@return_value]).zip(t_param + [t_result]).each do |p, t|
        next if t.nil?
        unless p.is_just?(t)
          fin = false
          break
        end
      end
      if fin
        entry = e
        break
      end
    end
    return false unless entry
    name, t_param, t_result, exact_classes, can_unbox_result, can_unbox_param = entry

    s_param = param.empty? ? '' : ", #{param.join(", ")}"
    if can_unbox_param || can_unbox_result
      bug() if !can_unbox_param && ([recv] + param).find{|p| p.unboxed? }
      suffix = ([recv] + param).map{|p| p.unboxed? ? unboxed_decl(p) : 'VALUE'}
      bug() if !can_unbox_result && @return_value.unboxed?
      suffix << (@return_value.unboxed? ? unboxed_decl(@return_value) : 'VALUE')
      suffix = suffix.join('_')
      @dependency.add(klass, @method_id, true)
      return "  #{@return_value} = cast_off_inline_#{klass.to_s.downcase}_#{name}_#{suffix}(#{recv}#{s_param});"
    else
      @dependency.add(klass, @method_id, true)
      return "  #{@return_value} = cast_off_inline_#{klass.to_s.downcase}_#{name}(#{recv}#{s_param});"
    end
  else
    m = SpecializeTable1[[mid, argc, MethodWrapper.new(klass, mid)]]
    return false unless m
    @dependency.add(klass, @method_id, true)
    return __send__(m, klass, mid, argc, recv, param)
  end
  bug()
end

#specialized_kernel_nil?(klass, mid, argc, recv, param) ⇒ Boolean

Returns:

  • (Boolean)


1770
1771
1772
1773
1774
1775
# File 'lib/cast_off/compile/ir/call_ir.rb', line 1770

def specialized_kernel_nil?(klass, mid, argc, recv, param)
  bug() unless mid == :nil?
  bug() unless argc == 1
  bug() unless param.empty?
  return "  #{@return_value} = Qfalse;"
end

#specialized_nilclass_nil?(klass, mid, argc, recv, param) ⇒ Boolean

Returns:

  • (Boolean)


1763
1764
1765
1766
1767
1768
# File 'lib/cast_off/compile/ir/call_ir.rb', line 1763

def specialized_nilclass_nil?(klass, mid, argc, recv, param)
  bug() unless mid == :nil?
  bug() unless argc == 1
  bug() unless param.empty?
  return "  #{@return_value} = Qtrue;"
end

#specialized_object_eq(klass, mid, argc, recv, param) ⇒ Object



1756
1757
1758
1759
1760
1761
# File 'lib/cast_off/compile/ir/call_ir.rb', line 1756

def specialized_object_eq(klass, mid, argc, recv, param)
  bug() unless mid == :==
  bug() unless argc == 2
  bug() unless param.size() == 1
  return "  #{@return_value} = (#{recv} == #{param[0]}) ? Qtrue : Qfalse;"
end

#splatcall?Boolean

Returns:

  • (Boolean)


1561
1562
1563
1564
# File 'lib/cast_off/compile/ir/call_ir.rb', line 1561

def splatcall?
  bug() unless @flags
  (@flags & VM_CALL_ARGS_SPLAT_BIT) != 0
end

#to_c(params) ⇒ Object



2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
# File 'lib/cast_off/compile/ir/call_ir.rb', line 2069

def to_c(params)
  ret = []
  ret << super(params)
  param = param_variables()
  recv = param.shift
  id = @translator.allocate_id(@method_id)
  bug() if recv.undefined?
  if @configuration.development? && sampling_return_value?
    ret << "  sampling_tmp = #{recv.boxed_form};"
  end
  if blockarg?
    ret << funcall_code(nil, id, recv, param, @argc)
  elsif recv.dynamic? || @configuration.force_dispatch_method?
    if @configuration.force_dispatch_method?
      # ユーザからの指定に基づいているので、Suggestion は吐かない
      ret << funcall_code(nil, id, recv, param, @argc)
    else
      if @configuration.development? && @source
        @translator.add_type_suggestion([get_definition_str(recv), @method_id.to_s, @source_line, @source])
      end
      ret << funcall_code(nil, id, recv, param, @argc)
    end
  else
    bug() if recv.types.empty?
    if recv.types.size() == 1
      multicall = false
    else
      multicall = true
    end
    if multicall
      types = recv.types
      types.each{|t| bug() unless t.is_a?(ClassWrapper) }
      nil_wrapper = ClassWrapper.new(NilClass, true)
      if types.size() == 2 && (types[0] == nil_wrapper || types[1] == nil_wrapper)
        # FIXME ここで nil もしくは別クラスだったらどうこうという処理は行わずに
        #       データフロー解析時に nil もしくは別のクラスという形だったら
        #       中間コードレベルで nil かどうかの条件分岐を入れたほうが
        #       定数伝播などによる最適化まで期待できる分、性能が向上すると思う。
        nil_index  = (types[0] == nil_wrapper ? 0 : 1)
        else_index = 1 - nil_index
        nil_code = not_funcall_code(nil_wrapper, @method_id, recv, param, @argc)
        if !nil_code
          nil_code = funcall_code(nil, id, recv, param, @argc)
        end
        else_class = types[else_index]
        else_code = not_funcall_code(else_class, @method_id, recv, param, @argc)
        if !else_code
          @dependency.add(else_class, @method_id, false)
          else_code = funcall_code(else_class, id, recv, param, @argc)
        end
        if nil_code == else_code
          ret << "  #{nil_code}"
        else
          ret << <<-EOS
  if (#{recv} == Qnil) {
    #{nil_code}
  } else {
    #{else_code}
  }
          EOS
        end
      else
        ret << MultiCallTemplate.trigger(binding)
      end
    else # singlecall
      klass = recv.types[0]
      c = not_funcall_code(klass, @method_id, recv, param, @argc)
      if c
        ret << c
      else
        begin
          @dependency.add(klass, @method_id, false)
        rescue CompileError
          # 通過していない分岐をコンパイルするときに通る
          klass = nil
        end
        ret << funcall_code(klass, id, recv, param, @argc)
      end
    end
  end
  if @configuration.development? && sampling_return_value?
    ret << "  sampling_poscall(#{@return_value.boxed_form}, sampling_tmp, ID2SYM(rb_intern(#{@method_id.to_s.inspect})));"
  end
  ret.join("\n")
end

#to_verbose_stringObject



1546
1547
1548
1549
# File 'lib/cast_off/compile/ir/call_ir.rb', line 1546

def to_verbose_string()
  recv = param_variables()[0]
  "[#{recv.dynamic? ? "dynamic" : recv.types.join(", ")}]##{@method_id}"
end

#type_propergation(defs) ⇒ Object



2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
# File 'lib/cast_off/compile/ir/call_ir.rb', line 2155

def type_propergation(defs)
  return false if @return_value.dynamic?
  recv = @param[0].param_value
  return if recv.undefined?
  change = false
  recv = @param[0].param_value
  bug() if recv.undefined?
  if recv.dynamic?
    dynamic = true
  else
    dynamic = false
    types = recv.types
    types.each do |t|
      return_value_class = @translator.return_value_class(t, @method_id)
      if return_value_class
        return_value_class.each{|c| change = true if @return_value.is_also(c)}
      else
        dynamic = true
        break
      end
    end
  end
  if dynamic
    change |= @return_value.is_dynamic()
  end
  change
end

#unboxed_decl(v) ⇒ Object



1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
# File 'lib/cast_off/compile/ir/call_ir.rb', line 1806

def unboxed_decl(v)
  bug() unless v.is_a?(Variable)
  bug() if v.dynamic?
  bug() unless v.types.size == 1
  c = v.types[0]
  bug() unless c.is_a?(ClassWrapper)
  case c
  when FloatWrapper
    'double'
  when FixnumWrapper
    'long'
  else
    bug()
  end
end

#unboxing_preludeObject

unboxing begin ###



2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
# File 'lib/cast_off/compile/ir/call_ir.rb', line 2240

def unboxing_prelude()
  params = param_variables()
  recv, *args = *params
  bug() if recv.undefined?
  bug() unless instance_of?(InvokeIR)
  if recv.dynamic? || @configuration.force_dispatch_method?
    can_not_unbox()
    return
  else
    bug() if recv.types.empty?
    unless recv.types.size() == 1
      can_not_unbox()
      return
    end
    klass = recv.types[0]
    bug() unless klass.is_a?(ClassWrapper)
    unless klass.get_method_type(@method_id) == :cfunc
      can_not_unbox()
      return
    end
    unless @configuration.should_be_call_directly?(klass, @method_id)
      can_not_unbox()
      return
    end
    cfunc_argc = klass.get_cfunc_argc(@method_id)
    bug() unless cfunc_argc
    entries = SpecializeTable0[[MethodWrapper.new(klass, @method_id), argc]]
    unless entries
      can_not_unbox()
      return
    end
    entry = nil
    entries.each do |e|
      bug() unless e.size == 6
      name, t_args, t_result, exact_classes, can_unbox_result, can_unbox_param = e
      bug() unless args.size == t_args.size
      fin = true
      (args + [@return_value]).zip(t_args + [t_result]).each do |p, t|
        next if t.nil?
        unless p.is_just?(t)
          fin = false
          break
        end
      end
      if fin
        entry = e
        break
      end
    end
    unless entry
      can_not_unbox()
      return
    end
    name, t_args, t_result, exact_classes, can_unbox_result, can_unbox_param = entry
    can_unbox_param ? params.each{|p| p.box() unless p.can_unbox?} : params.each{|p| p.box()}
    @return_value.box() unless can_unbox_result && @return_value.can_unbox?
    return
  end
end