A visitor on the AST of property definition that generate the C code.

Because of inlining, a visitor can visit more than one property.

Introduced properties

fun collect_types(recv: RuntimeVariable): Array[MClassType]

nitc :: GlobalCompilerVisitor :: collect_types

The runtime types that are acceptable for a given receiver.
private fun get_recvtype(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): MClassType

nitc :: GlobalCompilerVisitor :: get_recvtype

Subpart of old call function

Redefined properties

redef type SELF: GlobalCompilerVisitor

nitc $ GlobalCompilerVisitor :: SELF

Type of this instance, automatically specialized in every class
redef fun adapt_signature(m: MMethodDef, args: Array[RuntimeVariable])

nitc $ GlobalCompilerVisitor :: adapt_signature

Adapt the arguments of a method according to targetted MMethodDef
redef fun array_instance(array: Array[RuntimeVariable], elttype: MType): RuntimeVariable

nitc $ GlobalCompilerVisitor :: array_instance

Generate an array value
redef fun autobox(value: RuntimeVariable, mtype: MType): RuntimeVariable

nitc $ GlobalCompilerVisitor :: autobox

Box or unbox a value to another type iff a C type conversion is needed
redef fun box_extern(value: RuntimeVariable, mtype: MType): RuntimeVariable

nitc $ GlobalCompilerVisitor :: box_extern

Box extern classes to be used in the generated code
redef fun call(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): nullable RuntimeVariable

nitc $ GlobalCompilerVisitor :: call

Generate a static call on a method definition
redef fun class_name_string(value: RuntimeVariable): String

nitc $ GlobalCompilerVisitor :: class_name_string

Return a "const char*" variable associated to the classname of the dynamic type of an object
redef fun equal_test(value1: RuntimeVariable, value2: RuntimeVariable): RuntimeVariable

nitc $ GlobalCompilerVisitor :: equal_test

Generate a Nit "is" for two runtime_variables
redef fun init_instance(mtype: MClassType): RuntimeVariable

nitc $ GlobalCompilerVisitor :: init_instance

Generate a alloc-instance + init-attributes
redef fun is_same_type_test(value1: RuntimeVariable, value2: RuntimeVariable): RuntimeVariable

nitc $ GlobalCompilerVisitor :: is_same_type_test

Generate the code required to dynamically check if 2 objects share the same runtime type
redef fun isset_attribute(a: MAttribute, recv: RuntimeVariable): RuntimeVariable

nitc $ GlobalCompilerVisitor :: isset_attribute

Generate a polymorphic attribute is_set test
redef fun native_array_def(pname: String, ret_type: nullable MType, arguments: Array[RuntimeVariable]): Bool

nitc $ GlobalCompilerVisitor :: native_array_def

redef fun native_array_get(nat: RuntimeVariable, i: Int): RuntimeVariable

nitc $ GlobalCompilerVisitor :: native_array_get

Return an element of a native array.
redef fun native_array_set(nat: RuntimeVariable, i: Int, val: RuntimeVariable)

nitc $ GlobalCompilerVisitor :: native_array_set

Store an element in a native array.
redef fun read_attribute(a: MAttribute, recv: RuntimeVariable): RuntimeVariable

nitc $ GlobalCompilerVisitor :: read_attribute

Generate a polymorphic attribute read
redef fun routine_ref_call(mmethoddef: MMethodDef, arguments: Array[RuntimeVariable])

nitc $ GlobalCompilerVisitor :: routine_ref_call

Call the underlying referenced function
redef fun routine_ref_instance(routine_mclass_type: MClassType, recv: RuntimeVariable, callsite: CallSite): RuntimeVariable

nitc $ GlobalCompilerVisitor :: routine_ref_instance

Instantiate a new routine pointer
redef fun send(m: MMethod, args: Array[RuntimeVariable]): nullable RuntimeVariable

nitc $ GlobalCompilerVisitor :: send

Generate a polymorphic send for the method m and the arguments args
redef fun supercall(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): nullable RuntimeVariable

nitc $ GlobalCompilerVisitor :: supercall

Generate a super call from a method definition
redef fun type_test(value: RuntimeVariable, mtype: MType, tag: String): RuntimeVariable

nitc $ GlobalCompilerVisitor :: type_test

Generate a polymorphic subtype test
redef fun unbox_extern(value: RuntimeVariable, mtype: MType): RuntimeVariable

nitc $ GlobalCompilerVisitor :: unbox_extern

Unbox extern classes to be used in extern code (legacy NI and FFI)
redef fun unbox_signature_extern(m: MMethodDef, args: Array[RuntimeVariable])

nitc $ GlobalCompilerVisitor :: unbox_signature_extern

Unbox all the arguments of a method when implemented extern or intern
redef fun vararg_instance(mpropdef: MPropDef, recv: RuntimeVariable, varargs: Array[RuntimeVariable], elttype: MType): RuntimeVariable

nitc $ GlobalCompilerVisitor :: vararg_instance

FIXME: this is currently buggy since recv is not exact
redef fun write_attribute(a: MAttribute, recv: RuntimeVariable, value: RuntimeVariable)

nitc $ GlobalCompilerVisitor :: write_attribute

Generate a polymorphic attribute write

All properties

fun !=(other: nullable Object): Bool

core :: Object :: !=

Have self and other different values?
fun ==(other: nullable Object): Bool

core :: Object :: ==

Have self and other the same value?
type CLASS: Class[SELF]

core :: Object :: CLASS

The type of the class of self.
type SELF: Object

core :: Object :: SELF

Type of this instance, automatically specialized in every class
private var _compiler: COMPILER

nitc :: AbstractCompilerVisitor :: _compiler

The associated compiler
private var _current_node: nullable ANode

nitc :: AbstractCompilerVisitor :: _current_node

The current visited AST node
private var _frame: nullable StaticFrame

nitc :: AbstractCompilerVisitor :: _frame

The current StaticFrame
private var _last: Int

nitc :: AbstractCompilerVisitor :: _last

abstract fun adapt_signature(m: MMethodDef, args: Array[RuntimeVariable])

nitc :: AbstractCompilerVisitor :: adapt_signature

Adapt the arguments of a method according to targetted MMethodDef
fun add(s: String)

nitc :: AbstractCompilerVisitor :: add

Add a line in the main part of the generated C
fun add_abort(message: String)

nitc :: AbstractCompilerVisitor :: add_abort

Generate generic abort
fun add_cast(value: RuntimeVariable, mtype: MType, tag: String)

nitc :: AbstractCompilerVisitor :: add_cast

Add a dynamic cast
fun add_decl(s: String)

nitc :: AbstractCompilerVisitor :: add_decl

Add a line in the
fun add_escape_label(e: nullable EscapeMark)

nitc :: AbstractCompilerVisitor :: add_escape_label

Insert a C label for associated with an escapemark
fun add_extern(mmodule: MModule)

nitc :: AbstractCompilerVisitor :: add_extern

Look for a needed .h and .c file for a given module
fun add_raw_abort

nitc :: AbstractCompilerVisitor :: add_raw_abort

Generate abort without a message.
fun add_raw_throw

nitc :: AbstractCompilerVisitor :: add_raw_throw

Generate a long jump if there is a catch block.
fun anchor(mtype: MType): MType

nitc :: AbstractCompilerVisitor :: anchor

Anchor a type to the main module and the current receiver
abstract fun array_instance(array: Array[RuntimeVariable], elttype: MType): RuntimeVariable

nitc :: AbstractCompilerVisitor :: array_instance

Generate an array value
fun assign(left: RuntimeVariable, right: RuntimeVariable)

nitc :: AbstractCompilerVisitor :: assign

Correctly assign a left and a right value
fun autoadapt(value: RuntimeVariable, mtype: MType): RuntimeVariable

nitc :: AbstractCompilerVisitor :: autoadapt

Unsafely cast a value to a new type
abstract fun autobox(value: RuntimeVariable, mtype: MType): RuntimeVariable

nitc :: AbstractCompilerVisitor :: autobox

Box or unbox a value to another type iff a C type conversion is needed
fun bool_instance(value: Bool): RuntimeVariable

nitc :: AbstractCompilerVisitor :: bool_instance

Generate an integer value
fun bool_type: MClassType

nitc :: AbstractCompilerVisitor :: bool_type

Alias for self.compiler.mainmodule.bool_type
abstract fun box_extern(value: RuntimeVariable, mtype: MType): RuntimeVariable

nitc :: AbstractCompilerVisitor :: box_extern

Box extern classes to be used in the generated code
fun byte_instance(value: Byte): RuntimeVariable

nitc :: AbstractCompilerVisitor :: byte_instance

Generate a byte value
fun c_string_instance(ns: CString, len: Int): RuntimeVariable

nitc :: AbstractCompilerVisitor :: c_string_instance

Generates a CString instance fully escaped in C-style \xHH fashion
abstract fun call(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): nullable RuntimeVariable

nitc :: AbstractCompilerVisitor :: call

Generate a static call on a method definition
fun char_instance(value: Char): RuntimeVariable

nitc :: AbstractCompilerVisitor :: char_instance

Generate a char value
fun check_recv_notnull(recv: RuntimeVariable)

nitc :: AbstractCompilerVisitor :: check_recv_notnull

Add a check and an abort for a null receiver if needed
protected fun class_factory(name: String): CLASS

core :: Object :: class_factory

Implementation used by get_class to create the specific class.
fun class_name: String

core :: Object :: class_name

The class name of the object.
abstract fun class_name_string(value: RuntimeVariable): String

nitc :: AbstractCompilerVisitor :: class_name_string

Return a "const char*" variable associated to the classname of the dynamic type of an object
fun collect_types(recv: RuntimeVariable): Array[MClassType]

nitc :: GlobalCompilerVisitor :: collect_types

The runtime types that are acceptable for a given receiver.
fun compiler: COMPILER

nitc :: AbstractCompilerVisitor :: compiler

The associated compiler
protected fun compiler=(compiler: COMPILER)

nitc :: AbstractCompilerVisitor :: compiler=

The associated compiler
fun current_node: nullable ANode

nitc :: AbstractCompilerVisitor :: current_node

The current visited AST node
fun current_node=(current_node: nullable ANode)

nitc :: AbstractCompilerVisitor :: current_node=

The current visited AST node
fun debug(message: String)

nitc :: AbstractCompilerVisitor :: debug

Safely show a debug message on the current node and repeat the message in the C code as a comment
fun declare_once(s: String)

nitc :: AbstractCompilerVisitor :: declare_once

Add a declaration in the local-header
abstract fun equal_test(value1: RuntimeVariable, value2: RuntimeVariable): RuntimeVariable

nitc :: AbstractCompilerVisitor :: equal_test

Generate a Nit "is" for two runtime_variables
fun escapemark_name(e: nullable EscapeMark): String

nitc :: AbstractCompilerVisitor :: escapemark_name

Return an unique and stable identifier associated with an escapemark
fun expr(nexpr: AExpr, mtype: nullable MType): RuntimeVariable

nitc :: AbstractCompilerVisitor :: expr

Compile an expression an return its result
fun expr_bool(nexpr: AExpr): RuntimeVariable

nitc :: AbstractCompilerVisitor :: expr_bool

Alias for self.expr(nexpr, self.bool_type)
fun float_instance(value: Float): RuntimeVariable

nitc :: AbstractCompilerVisitor :: float_instance

Generate a float value
fun frame: nullable StaticFrame

nitc :: AbstractCompilerVisitor :: frame

The current StaticFrame
fun frame=(frame: nullable StaticFrame)

nitc :: AbstractCompilerVisitor :: frame=

The current StaticFrame
fun get_class: CLASS

core :: Object :: get_class

The meta-object representing the dynamic type of self.
fun get_name(s: String): String

nitc :: AbstractCompilerVisitor :: get_name

Return a new name based on s and unique in the visitor
fun get_property(name: String, recv: MType): MMethod

nitc :: AbstractCompilerVisitor :: get_property

Force to get the primitive property named name in the instance recv or abort
private fun get_recvtype(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): MClassType

nitc :: GlobalCompilerVisitor :: get_recvtype

Subpart of old call function
fun hash: Int

core :: Object :: hash

The hash code of the object.
init init

core :: Object :: init

abstract fun init_instance(mtype: MClassType): RuntimeVariable

nitc :: AbstractCompilerVisitor :: init_instance

Generate a alloc-instance + init-attributes
fun init_instance_or_extern(mtype: MClassType): RuntimeVariable

nitc :: AbstractCompilerVisitor :: init_instance_or_extern

Allocate and init attributes of an instance of a standard or extern class
fun inspect: String

core :: Object :: inspect

Developer readable representation of self.
protected fun inspect_head: String

core :: Object :: inspect_head

Return "CLASSNAME:#OBJECTID".
fun int16_instance(value: Int16): RuntimeVariable

nitc :: AbstractCompilerVisitor :: int16_instance

Generate an int16 value
fun int32_instance(value: Int32): RuntimeVariable

nitc :: AbstractCompilerVisitor :: int32_instance

Generate an int32 value
fun int8_instance(value: Int8): RuntimeVariable

nitc :: AbstractCompilerVisitor :: int8_instance

Generate an int8 value
fun int_instance(value: Int): RuntimeVariable

nitc :: AbstractCompilerVisitor :: int_instance

Generate an integer value
intern fun is_same_instance(other: nullable Object): Bool

core :: Object :: is_same_instance

Return true if self and other are the same instance (i.e. same identity).
fun is_same_serialized(other: nullable Object): Bool

core :: Object :: is_same_serialized

Is self the same as other in a serialization context?
intern fun is_same_type(other: Object): Bool

core :: Object :: is_same_type

Return true if self and other have the same dynamic type.
abstract fun is_same_type_test(value1: RuntimeVariable, value2: RuntimeVariable): RuntimeVariable

nitc :: AbstractCompilerVisitor :: is_same_type_test

Generate the code required to dynamically check if 2 objects share the same runtime type
abstract fun isset_attribute(a: MAttribute, recv: RuntimeVariable): RuntimeVariable

nitc :: AbstractCompilerVisitor :: isset_attribute

Generate a polymorphic attribute is_set test
private fun last: Int

nitc :: AbstractCompilerVisitor :: last

private fun last=(last: Int)

nitc :: AbstractCompilerVisitor :: last=

fun maybe_null(value: RuntimeVariable): Bool

nitc :: AbstractCompilerVisitor :: maybe_null

Can value be null? (according to current knowledge)
fun mmodule: MModule

nitc :: AbstractCompilerVisitor :: mmodule

The currently processed module
fun monomorphic_send(m: MMethod, t: MType, args: Array[RuntimeVariable]): nullable RuntimeVariable

nitc :: AbstractCompilerVisitor :: monomorphic_send

Generate a monomorphic send for the method m, the type t and the arguments args
fun monomorphic_super_send(m: MMethodDef, t: MType, args: Array[RuntimeVariable]): nullable RuntimeVariable

nitc :: AbstractCompilerVisitor :: monomorphic_super_send

Generate a monomorphic super send from the method m, the type t and the arguments args
private fun names=(names: HashSet[String])

nitc :: AbstractCompilerVisitor :: names=

fun native_array_def(pname: String, ret_type: nullable MType, arguments: Array[RuntimeVariable]): Bool

nitc :: AbstractCompilerVisitor :: native_array_def

abstract fun native_array_get(native_array: RuntimeVariable, index: Int): RuntimeVariable

nitc :: AbstractCompilerVisitor :: native_array_get

Return an element of a native array.
abstract fun native_array_set(native_array: RuntimeVariable, index: Int, value: RuntimeVariable)

nitc :: AbstractCompilerVisitor :: native_array_set

Store an element in a native array.
private intern fun native_class_name: CString

core :: Object :: native_class_name

The class name of the object in CString format.
fun new_expr(cexpr: String, mtype: MType): RuntimeVariable

nitc :: AbstractCompilerVisitor :: new_expr

Return a new local runtime_variable initialized with the C expression cexpr.
fun new_named_var(mtype: MType, name: String): RuntimeVariable

nitc :: AbstractCompilerVisitor :: new_named_var

Return a new uninitialized named runtime_variable
fun new_var(mtype: MType): RuntimeVariable

nitc :: AbstractCompilerVisitor :: new_var

Return a new uninitialized local runtime_variable
fun new_var_extern(mtype: MType): RuntimeVariable

nitc :: AbstractCompilerVisitor :: new_var_extern

The difference with new_var is the C static type of the local variable
fun nit_alloc(size: String, tag: nullable String): String

nitc :: AbstractCompilerVisitor :: nit_alloc

Allocate size bytes with the low_level nit_alloc C function
intern fun object_id: Int

core :: Object :: object_id

An internal hash code for the object based on its identity.
fun object_type: MClassType

nitc :: AbstractCompilerVisitor :: object_type

Alias for self.compiler.mainmodule.object_type
fun output

core :: Object :: output

Display self on stdout (debug only).
intern fun output_class_name

core :: Object :: output_class_name

Display class name on stdout (debug only).
abstract fun read_attribute(a: MAttribute, recv: RuntimeVariable): RuntimeVariable

nitc :: AbstractCompilerVisitor :: read_attribute

Generate a polymorphic attribute read
fun require_declaration(key: String)

nitc :: AbstractCompilerVisitor :: require_declaration

Request the presence of a global declaration
fun ret(s: RuntimeVariable)

nitc :: AbstractCompilerVisitor :: ret

Generate a return with the value s
private fun ret_to_c(src: RuntimeVariable, mtype: MType)

nitc :: AbstractCompilerVisitor :: ret_to_c

Return a RuntimeVarible to C user code
abstract fun routine_ref_call(mmethoddef: MMethodDef, args: Array[RuntimeVariable])

nitc :: AbstractCompilerVisitor :: routine_ref_call

Call the underlying referenced function
abstract fun routine_ref_instance(routine_mclass_type: MClassType, recv: RuntimeVariable, callsite: CallSite): RuntimeVariable

nitc :: AbstractCompilerVisitor :: routine_ref_instance

Instantiate a new routine pointer
abstract fun send(m: MMethod, args: Array[RuntimeVariable]): nullable RuntimeVariable

nitc :: AbstractCompilerVisitor :: send

Generate a polymorphic send for the method m and the arguments args
fun serialization_hash: Int

core :: Object :: serialization_hash

Hash value use for serialization
fun set_finalizer(recv: RuntimeVariable)

nitc :: AbstractCompilerVisitor :: set_finalizer

Set a GC finalizer on recv, only if recv isa Finalizable
fun stmt(nexpr: nullable AExpr)

nitc :: AbstractCompilerVisitor :: stmt

Compile a statement (if any)
fun string_instance(string: String): RuntimeVariable

nitc :: AbstractCompilerVisitor :: string_instance

Generate a string value
abstract fun supercall(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): nullable RuntimeVariable

nitc :: AbstractCompilerVisitor :: supercall

Generate a super call from a method definition
intern fun sys: Sys

core :: Object :: sys

Return the global sys object, the only instance of the Sys class.
abstract fun to_jvalue(env: JniEnv): JValue

core :: Object :: to_jvalue

fun to_s: String

core :: Object :: to_s

User readable representation of self.
abstract fun type_test(value: RuntimeVariable, mtype: MType, tag: String): RuntimeVariable

nitc :: AbstractCompilerVisitor :: type_test

Generate a polymorphic subtype test
abstract fun unbox_extern(value: RuntimeVariable, mtype: MType): RuntimeVariable

nitc :: AbstractCompilerVisitor :: unbox_extern

Unbox extern classes to be used in extern code (legacy NI and FFI)
abstract fun unbox_signature_extern(m: MMethodDef, args: Array[RuntimeVariable])

nitc :: AbstractCompilerVisitor :: unbox_signature_extern

Unbox all the arguments of a method when implemented extern or intern
private fun var_from_c(name: String, mtype: MType): RuntimeVariable

nitc :: AbstractCompilerVisitor :: var_from_c

Create a RuntimeVariable for this C variable originating from C user code
abstract fun vararg_instance(mpropdef: MPropDef, recv: RuntimeVariable, varargs: Array[RuntimeVariable], elttype: MType): RuntimeVariable

nitc :: AbstractCompilerVisitor :: vararg_instance

Get an instance of a array for a vararg
fun varargize(mpropdef: MMethodDef, map: nullable SignatureMap, recv: RuntimeVariable, args: SequenceRead[AExpr]): Array[RuntimeVariable]

nitc :: AbstractCompilerVisitor :: varargize

Evaluate args as expressions in the call of mpropdef on recv.
fun variable(variable: Variable): RuntimeVariable

nitc :: AbstractCompilerVisitor :: variable

Return the local runtime_variable associated to a Nit local variable
abstract fun write_attribute(a: MAttribute, recv: RuntimeVariable, value: RuntimeVariable)

nitc :: AbstractCompilerVisitor :: write_attribute

Generate a polymorphic attribute write
protected fun writer=(writer: CodeWriter)

nitc :: AbstractCompilerVisitor :: writer=

package_diagram nitc::GlobalCompilerVisitor GlobalCompilerVisitor nitc::AbstractCompilerVisitor AbstractCompilerVisitor nitc::GlobalCompilerVisitor->nitc::AbstractCompilerVisitor core::Object Object nitc::AbstractCompilerVisitor->core::Object ...core::Object ... ...core::Object->core::Object

Ancestors

interface Object

core :: Object

The root of the class hierarchy.

Parents

abstract class AbstractCompilerVisitor

nitc :: AbstractCompilerVisitor

A visitor on the AST of property definition that generate the C code.

Class definitions

nitc $ GlobalCompilerVisitor
# A visitor on the AST of property definition that generate the C code.
# Because of inlining, a visitor can visit more than one property.
class GlobalCompilerVisitor
	super AbstractCompilerVisitor

	redef type COMPILER: GlobalCompiler

	redef fun autobox(value, mtype)
	do
		if value.mtype == mtype then
			return value
		else if not value.mtype.is_c_primitive and not mtype.is_c_primitive then
			return value
		else if not value.mtype.is_c_primitive then
			return self.new_expr("((struct {mtype.c_name}*){value})->value; /* autounbox from {value.mtype} to {mtype} */", mtype)
		else if not mtype.is_c_primitive then
			var valtype = value.mtype.as(MClassType)
			var res = self.new_var(mtype)
			if not compiler.runtime_type_analysis.live_types.has(valtype) then
				self.add("/*no autobox from {value.mtype} to {mtype}: {value.mtype} is not live! */")
				self.add("PRINT_ERROR(\"Dead code executed!\\n\"); fatal_exit(1);")
				return res
			end
			self.add("{res} = BOX_{valtype.c_name}({value}); /* autobox from {value.mtype} to {mtype} */")
			return res
		else if value.mtype.ctype == "void*" and mtype.ctype == "void*" then
			return value
		else
			# Bad things will appen!
			var res = self.new_var(mtype)
			self.add("/* {res} left unintialized (cannot convert {value.mtype} to {mtype}) */")
			self.add("PRINT_ERROR(\"Cast error: Cannot cast %s to %s.\\n\", \"{value.mtype}\", \"{mtype}\"); fatal_exit(1);")
			return res
		end
	end

	redef fun unbox_extern(value, mtype)
	do
		if mtype isa MClassType and mtype.mclass.kind == extern_kind and
		   mtype.mclass.name != "CString" then
			var res = self.new_var_extern(mtype)
			self.add "{res} = ((struct {mtype.c_name}*){value})->value; /* unboxing {value.mtype} */"
			return res
		else
			return value
		end
	end

	redef fun box_extern(value, mtype)
	do
		if not mtype isa MClassType or mtype.mclass.kind != extern_kind or
			mtype.mclass.name == "CString" then return value

		var valtype = value.mtype.as(MClassType)
		var res = self.new_var(mtype)
		if not compiler.runtime_type_analysis.live_types.has(value.mtype.as(MClassType)) then
			self.add("/*no boxing of {value.mtype}: {value.mtype} is not live! */")
			self.add("PRINT_ERROR(\"Dead code executed!\\n\"); fatal_exit(1);")
			return res
		end
		self.add("{res} = BOX_{valtype.c_name}({value}); /* boxing {value.mtype} */")
		return res
	end

	# The runtime types that are acceptable for a given receiver.
	fun collect_types(recv: RuntimeVariable): Array[MClassType]
	do
		var mtype = recv.mcasttype
		if recv.is_exact then
			assert mtype isa MClassType
			assert self.compiler.runtime_type_analysis.live_types.has(mtype)
			var types = [mtype]
			return types
		end
		var cache = self.compiler.collect_types_cache
		if cache.has_key(mtype) then
			return cache[mtype]
		end
		var types = new Array[MClassType]
		var mainmodule = self.compiler.mainmodule
		for t in self.compiler.runtime_type_analysis.live_types do
			if not t.is_subtype(mainmodule, null, mtype) then continue
			types.add(t)
		end
		cache[mtype] = types
		return types
	end

	redef fun native_array_def(pname, ret_type, arguments)
	do
		var elttype = arguments.first.mtype
		var recv = "((struct {arguments[0].mcasttype.c_name}*){arguments[0]})->values"
		if pname == "[]" then
			self.ret(self.new_expr("{recv}[{arguments[1]}]", ret_type.as(not null)))
			return true
		else if pname == "[]=" then
			self.add("{recv}[{arguments[1]}]={arguments[2]};")
			return true
		else if pname == "length" then
			self.ret(self.new_expr("((struct {arguments[0].mcasttype.c_name}*){arguments[0]})->length", ret_type.as(not null)))
			return true
		else if pname == "copy_to" then
			var recv1 = "((struct {arguments[1].mcasttype.c_name}*){arguments[1]})->values"
			self.add("memmove({recv1},{recv},{arguments[2]}*sizeof({elttype.ctype}));")
			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 {arguments[3].mcasttype.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_instance(elttype, length)
	do
		var ret_type = mmodule.native_array_type(elttype)
		ret_type = anchor(ret_type).as(MClassType)
		length = autobox(length, compiler.mainmodule.int_type)
		return self.new_expr("NEW_{ret_type.c_name}((int){length})", ret_type)
	end

	redef fun native_array_get(nat, i)
	do
		var recv = "((struct {nat.mcasttype.c_name}*){nat})->values"
		var ret_type = nat.mcasttype.as(MClassType).arguments.first
		return self.new_expr("{recv}[{i}]", ret_type)
	end

	redef fun native_array_set(nat, i, val)
	do
		var recv = "((struct {nat.mcasttype.c_name}*){nat})->values"
		self.add("{recv}[{i}]={val};")
	end

        redef fun routine_ref_instance(routine_mclass_type, recv, callsite)
        do
		var mmethoddef = callsite.mpropdef
                var method = new CustomizedRuntimeFunction(mmethoddef, recv.mcasttype.as(MClassType))
                var my_recv = recv
                if recv.mtype.is_c_primitive then
                        var object_type = mmodule.object_type
                        my_recv = autobox(recv, object_type)
                end
                var thunk = new CustomizedThunkFunction(mmethoddef, my_recv.mtype.as(MClassType))
                thunk.polymorph_call_flag = not my_recv.is_exact
                compiler.todo(method)
                compiler.todo(thunk)
		var ret_type = self.anchor(routine_mclass_type).as(MClassType)
                var res = self.new_expr("NEW_{ret_type.c_name}({my_recv}, &{thunk.c_name})", ret_type)
                return res
        end

        redef fun routine_ref_call(mmethoddef, arguments)
        do
                var routine = arguments.first
                var routine_type = routine.mtype.as(MClassType)
                var routine_class = routine_type.mclass
                var underlying_recv = "((struct {routine.mcasttype.c_name}*){routine})->recv"
                var underlying_method = "((struct {routine.mcasttype.c_name}*){routine})->method"
                adapt_signature(mmethoddef, arguments)
                arguments.shift
                var ss = "{underlying_recv}"
                if arguments.length > 0 then
                        ss = "{ss}, {arguments.join(", ")}"
                end
                arguments.unshift routine

                var ret_mtype = mmethoddef.msignature.return_mtype

                if ret_mtype != null then
                        ret_mtype = resolve_for(ret_mtype, routine)
                end
                var callsite = "{underlying_method}({ss})"
                if ret_mtype != null then
                        var subres = new_expr("{callsite}", ret_mtype)
                        ret(subres)
                else
                        add("{callsite};")
		end
        end

	redef fun send(m, args)
	do
		var types = self.collect_types(args.first)

		var res: nullable RuntimeVariable
		var ret = m.intro.msignature.return_mtype
		if ret == null then
			res = null
		else
			ret = self.resolve_for(ret, args.first)
			res = self.new_var(ret)
		end

		self.add("/* send {m} on {args.first.inspect} */")
		if args.first.mtype.is_c_primitive then
			var mclasstype = args.first.mtype.as(MClassType)
			if not self.compiler.runtime_type_analysis.live_types.has(mclasstype) then
				self.add("/* skip, dead class {mclasstype} */")
				return res
			end
			if not mclasstype.has_mproperty(self.compiler.mainmodule, m) then
				self.add("/* skip, no method {m} */")
				return res
			end
			var propdef = m.lookup_first_definition(self.compiler.mainmodule, mclasstype)
			var res2 = self.call(propdef, mclasstype, args)
			if res != null then self.assign(res, res2.as(not null))
			return res
		end
		var consider_null = not self.compiler.modelbuilder.toolcontext.opt_no_check_null.value or m.name == "==" or m.name == "!="
		if args.first.mcasttype isa MNullableType or args.first.mcasttype isa MNullType and consider_null then
			# The reciever is potentially null, so we have to 3 cases: ==, != or NullPointerException
			self.add("if ({args.first} == NULL) \{ /* Special null case */")
			if m.name == "==" or m.name == "is_same_instance" then
				assert res != null
				if args[1].mcasttype isa MNullableType then
					self.add("{res} = ({args[1]} == NULL);")
				else if args[1].mcasttype isa MNullType then
					self.add("{res} = 1; /* is null */")
				else
					self.add("{res} = 0; /* {args[1].inspect} cannot be null */")
				end
			else if m.name == "!=" then
				assert res != null
				if args[1].mcasttype isa MNullableType then
					self.add("{res} = ({args[1]} != NULL);")
				else if args[1].mcasttype isa MNullType then
					self.add("{res} = 0; /* is null */")
				else
					self.add("{res} = 1; /* {args[1].inspect} cannot be null */")
				end
			else
				self.add_abort("Receiver is null")
			end
			self.add "\} else"
		end
		if types.is_empty then
			self.add("\{")
			self.add("/*BUG: no live types for {args.first.inspect} . {m}*/")
			self.bugtype(args.first)
			self.add("\}")
			return res
		end

		self.add("switch({args.first}->classid) \{")
		var last = types.last
		var defaultpropdef: nullable MMethodDef = null
		for t in types do
			var propdef = m.lookup_first_definition(self.compiler.mainmodule, t)
			if propdef.mclassdef.mclass.name == "Object" and not t.is_c_primitive then
				defaultpropdef = propdef
				continue
			end
			if not self.compiler.hardening and t == last and defaultpropdef == null then
				self.add("default: /* test {t} */")
			else
				self.add("case {self.compiler.classid(t)}: /* test {t} */")
			end
			var res2 = self.call(propdef, t, args)
			if res != null then self.assign(res, res2.as(not null))
			self.add "break;"
		end
		if defaultpropdef != null then
			self.add("default: /* default is Object */")
			var res2 = self.call(defaultpropdef, defaultpropdef.mclassdef.bound_mtype, args)
			if res != null then self.assign(res, res2.as(not null))
		else if self.compiler.hardening then
			self.add("default: /* bug */")
			self.bugtype(args.first)
		end
		self.add("\}")
		return res
	end

	fun check_valid_reciever(recvtype: MClassType)
	do
		if self.compiler.runtime_type_analysis.live_types.has(recvtype) or recvtype.mclass.name == "Object" then return
		print_error "{recvtype} is not a live type"
		abort
	end

	# Subpart of old call function
	#
	# Checks if the type of the receiver is valid and corrects it if necessary
	private fun get_recvtype(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): MClassType
	do
		check_valid_reciever(recvtype)
		return recvtype
	end

	redef fun call(m, recvtype, args)
	do
		var recv_type = get_recvtype(m, recvtype, args)
		var recv = self.autoadapt(self.autobox(args.first, recvtype), recvtype)
		if m.is_extern then recv = unbox_extern(recv, recv_type)

		args = args.to_a
		args.first = recv

		assert args.length == m.msignature.arity + 1 else debug("Invalid arity for {m}. {args.length} arguments given.")

		var rm = new CustomizedRuntimeFunction(m, recvtype)
		return rm.call(self, args)
	end

	redef fun supercall(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): nullable RuntimeVariable
	do
		var types = self.collect_types(args.first)

		var res: nullable RuntimeVariable
		var ret = m.mproperty.intro.msignature.return_mtype
		if ret == null then
			res = null
		else
			ret = self.resolve_for(ret, args.first)
			res = self.new_var(ret)
		end

		self.add("/* super {m} on {args.first.inspect} */")
		if args.first.mtype.is_c_primitive then
			var mclasstype = args.first.mtype.as(MClassType)
			if not self.compiler.runtime_type_analysis.live_types.has(mclasstype) then
				self.add("/* skip, no method {m} */")
				return res
			end
			var propdef = m.lookup_next_definition(self.compiler.mainmodule, mclasstype)
			var res2 = self.call(propdef, mclasstype, args)
			if res != null then self.assign(res, res2.as(not null))
			return res
		end

		if types.is_empty then
			self.add("\{")
			self.add("/*BUG: no live types for {args.first.inspect} . {m}*/")
			self.bugtype(args.first)
			self.add("\}")
			return res
		end

		self.add("switch({args.first}->classid) \{")
		var last = types.last
		for t in types do
			var propdef = m.lookup_next_definition(self.compiler.mainmodule, t)
			if not self.compiler.hardening and t == last then
				self.add("default: /* test {t} */")
			else
				self.add("case {self.compiler.classid(t)}: /* test {t} */")
			end
			var res2 = self.call(propdef, t, args)
			if res != null then self.assign(res, res2.as(not null))
			self.add "break;"
		end
		if self.compiler.hardening then
			self.add("default: /* bug */")
			self.bugtype(args.first)
		end
		self.add("\}")
		return res
	end

	redef fun adapt_signature(m, args)
	do
		var recv = args.first
		for i in [0..m.msignature.arity[ do
			var mp = m.msignature.mparameters[i]
			var t = mp.mtype
			if mp.is_vararg then
				t = args[i+1].mtype
			end
			t = self.resolve_for(t, recv)
			args[i+1] = self.autobox(args[i+1], t)
		end
	end

	redef fun unbox_signature_extern(m, args)
	do
		var recv = args.first
		for i in [0..m.msignature.arity[ do
			var mp = m.msignature.mparameters[i]
			var t = mp.mtype
			if mp.is_vararg then
				t = args[i+1].mtype
			end
			t = self.resolve_for(t, recv)
			if m.is_extern then args[i+1] = self.unbox_extern(args[i+1], t)
		end
	end

	# FIXME: this is currently buggy since recv is not exact
	redef fun vararg_instance(mpropdef, recv, varargs, elttype)
	do
		elttype = self.resolve_for(elttype, recv)
		return self.array_instance(varargs, elttype)
	end

	fun bugtype(recv: RuntimeVariable)
	do
		if recv.mtype.is_c_primitive then return
		self.add("PRINT_ERROR(\"BTD BUG: Dynamic type is %s, static type is %s\\n\", class_names[{recv}->classid], \"{recv.mcasttype}\");")
		self.add("fatal_exit(1);")
	end

	redef fun isset_attribute(a, recv)
	do
		check_recv_notnull(recv)

		var types = self.collect_types(recv)
		var res = self.new_var(bool_type)

		if types.is_empty then
			self.add("/*BUG: no live types for {recv.inspect} . {a}*/")
			self.bugtype(recv)
			return res
		end
		self.add("/* isset {a} on {recv.inspect} */")
		self.add("switch({recv}->classid) \{")
		var last = types.last
		for t in types do
			if not self.compiler.hardening and t == last then
				self.add("default: /*{self.compiler.classid(t)}*/")
			else
				self.add("case {self.compiler.classid(t)}:")
			end
			var recv2 = self.autoadapt(recv, t)
			var ta = a.intro.static_mtype.as(not null)
			ta = self.resolve_for(ta, recv2)
			var attr = self.new_expr("((struct {t.c_name}*){recv})->{a.intro.c_name}", ta)
			if not ta isa MNullableType then
				if not ta.is_c_primitive then
					self.add("{res} = ({attr} != NULL);")
				else
					self.add("{res} = 1; /*NOTYET isset on primitive attributes*/")
				end
			end
			self.add("break;")
		end
		if self.compiler.hardening then
			self.add("default: /* Bug */")
			self.bugtype(recv)
		end
		self.add("\}")

		return res
	end

	redef fun read_attribute(a, recv)
	do
		check_recv_notnull(recv)

		var types = self.collect_types(recv)

		var ret = a.intro.static_mtype.as(not null)
		ret = self.resolve_for(ret, recv)
		var res = self.new_var(ret)

		if types.is_empty then
			self.add("/*BUG: no live types for {recv.inspect} . {a}*/")
			self.bugtype(recv)
			return res
		end
		self.add("/* read {a} on {recv.inspect} */")
		self.add("switch({recv}->classid) \{")
		var last = types.last
		for t in types do
			if not self.compiler.hardening and t == last then
				self.add("default: /*{self.compiler.classid(t)}*/")
			else
				self.add("case {self.compiler.classid(t)}:")
			end
			var recv2 = self.autoadapt(recv, t)
			var ta = a.intro.static_mtype.as(not null)
			ta = self.resolve_for(ta, recv2)
			var res2 = self.new_expr("((struct {t.c_name}*){recv})->{a.intro.c_name}", ta)
			if not ta isa MNullableType and not self.compiler.modelbuilder.toolcontext.opt_no_check_attr_isset.value then
				if not ta.is_c_primitive then
					self.add("if ({res2} == NULL) \{")
					self.add_abort("Uninitialized attribute {a.name}")
					self.add("\}")
				else
					self.add("/*NOTYET isset on primitive attributes*/")
				end
			end
			self.assign(res, res2)
			self.add("break;")
		end
		if self.compiler.hardening then
			self.add("default: /* Bug */")
			self.bugtype(recv)
		end
		self.add("\}")

		return res
	end

	redef fun write_attribute(a, recv, value)
	do
		check_recv_notnull(recv)

		var types = self.collect_types(recv)

		if types.is_empty then
			self.add("/*BUG: no live types for {recv.inspect} . {a}*/")
			self.bugtype(recv)
			return
		end
		self.add("/* write {a} on {recv.inspect} */")
		self.add("switch({recv}->classid) \{")
		var last = types.last
		for t in types do
			if not self.compiler.hardening and t == last then
				self.add("default: /*{self.compiler.classid(t)}*/")
			else
				self.add("case {self.compiler.classid(t)}:")
			end
			var recv2 = self.autoadapt(recv, t)
			var ta = a.intro.static_mtype.as(not null)
			ta = self.resolve_for(ta, recv2)
			self.add("((struct {t.c_name}*){recv})->{a.intro.c_name} = {self.autobox(value, ta)};")
			self.add("break;")
		end
		if self.compiler.hardening then
			self.add("default: /* Bug*/")
			self.bugtype(recv)
		end
		self.add("\}")
	end

	redef fun init_instance(mtype)
	do
		mtype = self.anchor(mtype).as(MClassType)
		if not self.compiler.runtime_type_analysis.live_types.has(mtype) then
			debug "problem: {mtype} was detected dead"
		end
		var res = self.new_expr("NEW_{mtype.c_name}()", mtype)
		res.is_exact = true
		return res
	end

	redef fun type_test(value, mtype, tag)
	do
		mtype = self.anchor(mtype)
		if not self.compiler.runtime_type_analysis.live_cast_types.has(mtype) then
			debug "problem: {mtype} was detected cast-dead"
			abort
		end

		var types = self.collect_types(value)
		var res = self.new_var(bool_type)

		self.add("/* isa {mtype} on {value.inspect} */")
		if value.mtype.is_c_primitive then
			if value.mtype.is_subtype(self.compiler.mainmodule, null, mtype) then
				self.add("{res} = 1;")
			else
				self.add("{res} = 0;")
			end
			return res
		end
		if value.mcasttype isa MNullableType or value.mcasttype isa MNullType then
			self.add("if ({value} == NULL) \{")
			if mtype isa MNullableType then
				self.add("{res} = 1; /* isa {mtype} */")
			else
				self.add("{res} = 0; /* not isa {mtype} */")
			end
			self.add("\} else ")
		end
		self.add("switch({value}->classid) \{")
		for t in types do
			if t.is_subtype(self.compiler.mainmodule, null, mtype) then
				self.add("case {self.compiler.classid(t)}: /* {t} */")
			end
		end
		self.add("{res} = 1;")
		self.add("break;")
		self.add("default:")
		self.add("{res} = 0;")
		self.add("\}")

		return res
	end

	redef fun is_same_type_test(value1, value2)
	do
		var res = self.new_var(bool_type)
		if not value2.mtype.is_c_primitive then
			if not value1.mtype.is_c_primitive then
				self.add "{res} = {value1}->classid == {value2}->classid;"
			else
				self.add "{res} = {self.compiler.classid(value1.mtype.as(MClassType))} == {value2}->classid;"
			end
		else
			if not value1.mtype.is_c_primitive then
				self.add "{res} = {value1}->classid == {self.compiler.classid(value2.mtype.as(MClassType))};"
			else if value1.mcasttype == value2.mcasttype then
				self.add "{res} = 1;"
			else
				self.add "{res} = 0;"
			end
		end
		return res
	end

	redef fun class_name_string(value)
	do
		var res = self.get_name("var_class_name")
		self.add_decl("const char* {res};")
		if not value.mtype.is_c_primitive then
			self.add "{res} = class_names[{value}->classid];"
		else
			self.add "{res} = class_names[{self.compiler.classid(value.mtype.as(MClassType))}];"
		end
		return res
	end

	redef fun equal_test(value1, value2)
	do
		var res = self.new_var(bool_type)
		if value2.mtype.is_c_primitive and not value1.mtype.is_c_primitive then
			var tmp = value1
			value1 = value2
			value2 = tmp
		end
		if value1.mtype.is_c_primitive then
			if value2.mtype == value1.mtype then
				self.add("{res} = {value1} == {value2};")
			else if value2.mtype.is_c_primitive then
				self.add("{res} = 0; /* incompatible types {value1.mtype} vs. {value2.mtype}*/")
			else
				var mtype1 = value1.mtype.as(MClassType)
				self.add("{res} = ({value2} != NULL) && ({value2}->classid == {self.compiler.classid(mtype1)});")
				self.add("if ({res}) \{")
				self.add("{res} = ({self.autobox(value2, value1.mtype)} == {value1});")
				self.add("\}")
			end
		else
			var s = new Array[String]
			for t in self.compiler.live_primitive_types do
				if not t.is_subtype(self.compiler.mainmodule, null, value1.mcasttype) then continue
				if not t.is_subtype(self.compiler.mainmodule, null, value2.mcasttype) then continue
				s.add "({value1}->classid == {self.compiler.classid(t)} && ((struct {t.c_name}*){value1})->value == ((struct {t.c_name}*){value2})->value)"
			end

			if self.compiler.mainmodule.model.get_mclasses_by_name("Pointer") != null then
				var pointer_type = self.compiler.mainmodule.pointer_type
				if value1.mcasttype.is_subtype(self.compiler.mainmodule, null, pointer_type) or
					value2.mcasttype.is_subtype(self.compiler.mainmodule, null, pointer_type) then
					s.add "(((struct {pointer_type.c_name}*){value1})->value == ((struct {pointer_type.c_name}*){value2})->value)"
				end
			end

			if s.is_empty then
				self.add("{res} = {value1} == {value2};")
			else
				self.add("{res} = {value1} == {value2} || ({value1} != NULL && {value2} != NULL && {value1}->classid == {value2}->classid && ({s.join(" || ")}));")
			end
		end
		return res
	end

	redef fun array_instance(array, elttype)
	do
		elttype = self.anchor(elttype)
		var arraytype = mmodule.array_type(elttype)
		var res = self.init_instance(arraytype)
		self.add("\{ /* {res} = array_instance Array[{elttype}] */")
		var nat = self.new_var(mmodule.native_array_type(elttype))
		nat.is_exact = true
		self.add("{nat} = NEW_{nat.mtype.c_name}({array.length});")
		for i in [0..array.length[ do
			var r = self.autobox(array[i], elttype)
			self.add("((struct {nat.mtype.c_name}*) {nat})->values[{i}] = {r};")
		end
		var length = self.int_instance(array.length)
		self.send(self.get_property("with_native", arraytype), [res, nat, length])
		self.add("\}")
		return res
	end
end
src/compiler/global_compiler.nit:331,1--1011,3