+ return true
+ else if pname == "memmove" then
+ # fun memmove(start: Int, length: Int, dest: NativeArray[E], dest_start: Int) is intern do
+ var recv1 = "((struct instance_{nclass.c_name}*){arguments[3]})->values"
+ self.add("memmove({recv1}+{arguments[4]}, {recv}+{arguments[1]}, {arguments[2]}*sizeof({elttype.ctype}));")
+ return true
+ end
+ return false
+ end
+
+ redef fun native_array_get(nat, i)
+ do
+ var nclass = mmodule.native_array_class
+ var recv = "((struct instance_{nclass.c_name}*){nat})->values"
+ # Because the objects are boxed, return the box to avoid unnecessary (or broken) unboxing/reboxing
+ var res = self.new_expr("{recv}[{i}]", compiler.mainmodule.object_type)
+ return res
+ end
+
+ redef fun native_array_set(nat, i, val)
+ do
+ var nclass = mmodule.native_array_class
+ var recv = "((struct instance_{nclass.c_name}*){nat})->values"
+ self.add("{recv}[{i}]={val};")
+ end
+
+ redef fun routine_ref_instance(routine_type, recv, callsite)
+ do
+ #debug "ENTER ref_instance"
+ var mmethoddef = callsite.mpropdef
+ var mmethod = mmethoddef.mproperty
+ # routine_mclass is the specialized one, e.g: FunRef1, ProcRef2, etc..
+ var routine_mclass = routine_type.mclass
+
+ var nclasses = mmodule.model.get_mclasses_by_name("RoutineRef").as(not null)
+ var base_routine_mclass = nclasses.first
+
+ # All routine classes use the same `NEW` constructor.
+ # However, they have different declared `class` and `type` value.
+ self.require_declaration("NEW_{base_routine_mclass.c_name}")
+
+ var recv_class_cname = recv.mcasttype.as(MClassType).mclass.c_name
+ var my_recv = recv
+
+ if recv.mtype.is_c_primitive then
+ my_recv = autobox(recv, mmodule.object_type)
+ end
+ var my_recv_mclass_type = my_recv.mtype.as(MClassType)
+
+ # The class of the concrete Routine must exist (e.g ProcRef0, FunRef0, etc.)
+ self.require_declaration("class_{routine_mclass.c_name}")
+ self.require_declaration(mmethoddef.c_name)
+
+ var thunk_function = mmethoddef.callref_thunk(my_recv_mclass_type)
+ # If the receiver is exact, then there's no need to make a
+ # polymorph call to the underlying method.
+ thunk_function.polymorph_call_flag = not my_recv.is_exact
+ var runtime_function = mmethoddef.virtual_runtime_function
+
+ var is_c_equiv = runtime_function.msignature.c_equiv(thunk_function.msignature)
+
+ var c_ref = thunk_function.c_ref
+ if is_c_equiv then
+ var const_color = mmethoddef.mproperty.const_color
+ c_ref = "{class_info(my_recv)}->vft[{const_color}]"
+ self.require_declaration(const_color)
+ else
+ self.require_declaration(thunk_function.c_name)
+ compiler.thunk_todo(thunk_function)
+ end
+ var res: RuntimeVariable
+ if routine_type.need_anchor then
+ hardening_live_open_type(routine_type)
+ link_unresolved_type(self.frame.mpropdef.mclassdef, routine_type)
+ var recv2 = self.frame.arguments.first
+ var recv2_type_info = self.type_info(recv2)
+ self.require_declaration(routine_type.const_color)
+ res = self.new_expr("NEW_{base_routine_mclass.c_name}({my_recv}, (nitmethod_t){c_ref}, &class_{routine_mclass.c_name}, {recv2_type_info}->resolution_table->types[{routine_type.const_color}])", routine_type)
+ else
+ self.require_declaration("type_{routine_type.c_name}")
+ compiler.undead_types.add(routine_type)
+ res = self.new_expr("NEW_{base_routine_mclass.c_name}({my_recv}, (nitmethod_t){c_ref}, &class_{routine_mclass.c_name}, &type_{routine_type.c_name})", routine_type)
+ end
+ return res
+ end
+
+ redef fun routine_ref_call(mmethoddef, arguments)
+ do
+ #debug "ENTER ref_call"
+ compiler.modelbuilder.nb_invok_by_tables += 1
+ if compiler.modelbuilder.toolcontext.opt_invocation_metrics.value then add("count_invoke_by_tables++;")
+ var nclasses = mmodule.model.get_mclasses_by_name("RoutineRef").as(not null)
+ var nclass = nclasses.first
+ var runtime_function = mmethoddef.virtual_runtime_function
+
+ # Save the current receiver since adapt_signature will autobox
+ # the routine receiver which is not the underlying receiver.
+ # The underlying receiver has already been adapted in the
+ # `routine_ref_instance` method. Here we just want to adapt the
+ # rest of the signature, but it's easier to pass the wrong
+ # receiver in adapt_signature then discards it with `shift`.
+ #
+ # ~~~~nitish
+ # class A; def toto do print "toto"; end
+ # var a = new A
+ # var f = &a.toto # `a` is the underlying receiver
+ # f.call # here `f` is the routine receiver
+ # ~~~~
+ var routine = arguments.first
+
+ # Retrieve the concrete routine type
+ var original_recv_c = "(((struct instance_{nclass.c_name}*){arguments[0]})->recv)"
+ var nitmethod = "(({runtime_function.c_funptrtype})(((struct instance_{nclass.c_name}*){arguments[0]})->method))"
+ if arguments.length > 1 then
+ adapt_signature(mmethoddef, arguments)
+ end
+
+ var ret_mtype = runtime_function.called_signature.return_mtype
+
+ if ret_mtype != null then
+ # `ret` is actually always nullable Object. When invoking
+ # a callref, we don't have the original callsite information.
+ # Thus, we need to recompute the return type of the callsite.
+ ret_mtype = resolve_for(ret_mtype, routine)
+ end
+
+ # remove the routine's receiver
+ arguments.shift
+ var ss = arguments.join(", ")
+ # replace the receiver with the original one
+ if arguments.length > 0 then
+ ss = "{original_recv_c}, {ss}"
+ else
+ ss = original_recv_c
+ end
+
+ arguments.unshift routine # put back the routine ref receiver
+ add "/* {mmethoddef.mproperty} on {arguments.first.inspect}*/"
+ var callsite = "{nitmethod}({ss})"
+ if ret_mtype != null then
+ var subres = new_expr("{callsite}", ret_mtype)
+ ret(subres)
+ else
+ add("{callsite};")