- end
-end
-
-# A runtime variable hold a runtime value in C
-# Runtime variables are associated to local variables and intermediate results
-private class RuntimeVariable
- # The name of the variable in the C code
- var name: String
-
- # The static type of the variable (as declard in C)
- var mtype: MType
-
- # The current casted type of the variable (as known in Nit)
- var mcasttype: MType
-
- # If the variable exaclty a mcasttype?
- # false (usual value) means that the variable is a mcasttype or a subtype.
- var is_exact: Bool = false
-
- redef fun to_s do return name
-
- redef fun inspect
- do
- var exact_str
- if self.is_exact then
- exact_str = " exact"
- else
- exact_str = ""
- end
- var type_str
- if self.mtype == self.mcasttype then
- type_str = "{mtype}{exact_str}"
- else
- type_str = "{mtype}({mcasttype}{exact_str})"
- end
- return "<{name}:{type_str}>"
- end
-end
-
-# Visit the AST to generate the C code.
-# Because of inlining, a visitor can visit more than one property.
-private class GlobalCompilerVisitor
- # The associated compiler
- var compiler: GlobalCompiler
-
- init(compiler: GlobalCompiler)
- do
- self.compiler = compiler
- compiler.visitors.add(self)
- end
-
- # Alias for self.compiler.mainmodule.object_type
- fun object_type: MClassType do return self.compiler.mainmodule.object_type
-
- # Alias for self.compiler.mainmodule.bool_type
- fun bool_type: MClassType do return self.compiler.mainmodule.bool_type
-
- # Force to get the primitive class named `name' or abort
- fun get_class(name: String): MClass
- do
- return self.compiler.mainmodule.get_primitive_class(name)
- end
-
- # Force to get the primitive property named `name' in the instance `recv' or abort
- fun get_property(name: String, recv: MType): MMethod
- do
- return self.compiler.mainmodule.force_get_primitive_method(name, recv)
- end
-
- # The current Frame
- var frame: nullable Frame
-
- # Anchor a type to the main module and the current receiver
- fun anchor(mtype: MType): MType
- do
- if not mtype.need_anchor then return mtype
- #debug("anchor {mtype} to {self.reciever.as(not null)}:{self.reciever.mtype}")
- return mtype.anchor_to(self.compiler.mainmodule, self.frame.receiver)
- end
-
- # Add a line in the main part of the generated C
- fun add(s: String)
- do
- self.lines.add(s)
- end
-
- # Add a line in the
- # (used for local or global declaration)
- fun add_decl(s: String)
- do
- self.decl_lines.add(s)
- end
-
- private var lines: List[String] = new List[String]
- private var decl_lines: List[String] = new List[String]
-
- # The current visited AST node
- var current_node: nullable AExpr = null
-
- # Compile an expression an return its
- fun expr(nexpr: AExpr, mtype: nullable MType): RuntimeVariable
- do
- var old = self.current_node
- self.current_node = nexpr
- var res = nexpr.expr(self).as(not null)
- if mtype != null then
- mtype = self.anchor(mtype)
- res = self.autobox(res, mtype)
- end
- self.current_node = old
- return res
- end
-
- # Unsafely cast a value to a new type
- # ENSURE: return.name == value.name
- fun autoadapt(value: RuntimeVariable, mtype: MType): RuntimeVariable
- do
- mtype = self.anchor(mtype)
- if value.mtype.is_subtype(self.compiler.mainmodule, null, mtype) then
- return value
- end
-
- var valmtype = value.mtype
- if valmtype isa MNullableType and valmtype.mtype.is_subtype(self.compiler.mainmodule, null, mtype) then
- var res = new RuntimeVariable(value.name, value.mtype, valmtype.mtype)
- return res
- else
- var res = new RuntimeVariable(value.name, value.mtype, mtype)
- return res
- end
- end
-
- # Box or unbox a value to another type iff a C type conversion is needed
- # ENSURE: result.mtype.ctype == mtype.ctype
- fun autobox(value: RuntimeVariable, mtype: MType): RuntimeVariable
- do
- if value.mtype.ctype == mtype.ctype then
- return value
- else if value.mtype.ctype == "val*" then
- return self.new_expr("((struct {mtype.c_name}*){value})->value /* autounbox from {value.mtype} to {mtype} */", mtype)
- else if mtype.ctype == "val*" 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("printf(\"Dead code executed!\\n\"); exit(1);")
- return res
- end
- self.add("{res} = GC_MALLOC(sizeof(struct {valtype.c_name})); /* autobox from {value.mtype} to {mtype} */")
- self.add("{res}->classid = {self.compiler.classid(valtype)};")
- self.add("((struct {valtype.c_name}*){res})->value = {value};")
- return res
- else
- # Bad things will append!
- var res = self.new_var(mtype)
- self.add("/* {res} left unintialized (cannot convert {value.mtype} to {mtype}) */")
- self.add("printf(\"Cast error: Cannot cast %s to %s.\\n\", \"{value.mtype}\", \"{mtype}\"); exit(1);")
- return res
- end
- end
-
- # Correctly assign a left and a right value
- # Boxing and unboxing is performed if required
- fun assign(left, right: RuntimeVariable)
- do
- right = self.autobox(right, left.mtype)
- self.add("{left} = {right};")
- end
-
-
- # Alias for `self.expr(nexpr, self.bool_type)'
- fun expr_bool(nexpr: AExpr): RuntimeVariable
- do
- return expr(nexpr, bool_type)
- end
-
- # Compile statement
- fun stmt(nexpr: nullable AExpr)
- do
- if nexpr == null then return
- var old = self.current_node
- self.current_node = nexpr
- nexpr.stmt(self)
- self.current_node = old
- end
-
- # Safely show a debug message on the current node and repeat the message in the C code
- fun debug(message: String)
- do
- var node = self.current_node
- if node == null then
- print "?: {message}"
- else
- node.debug(message)
- end
- self.add("/* DEBUG: {message} */")
- end
-
- # Return a new uninitialized local runtime_variable
- fun new_var(mtype: MType): RuntimeVariable
- do
- if mtype isa MNullType then
- mtype = self.object_type
- else
- mtype = self.anchor(mtype)
- end
- var name = self.get_name("var")
- var res = new RuntimeVariable(name, mtype, mtype)
- self.add_decl("{mtype.ctype} {name} /* : {mtype} */;")
- return res
- end
-
- # Return a new local runtime_variable initialized with the C expression `cexpr'.
- fun new_expr(cexpr: String, mtype: MType): RuntimeVariable
- do
- var res = new_var(mtype)
- self.add("{res} = {cexpr};")
- return res
- end
-
- # Return the local runtime_variable associated to a Nit local variable
- fun variable(variable: Variable): RuntimeVariable
- do
- if self.variables.has_key(variable) then
- return self.variables[variable]
- else
- var name = self.get_name("var_{variable.name}")
- var mtype = variable.declared_type.as(not null)
- mtype = self.anchor(mtype)
- var res = new RuntimeVariable(name, mtype, mtype)
- self.add_decl("{mtype.ctype} {name} /* var {variable}: {mtype} */;")
- self.variables[variable] = res
- return res
- end
- end
-
- private var variables: HashMap[Variable, RuntimeVariable] = new HashMap[Variable, RuntimeVariable]
-
- # Return an unique and stable identifier associated with an escapemark
- fun escapemark_name(e: nullable EscapeMark): String
- do
- assert e != null
- if escapemark_names.has_key(e) then return escapemark_names[e]
- var name = e.name
- if name == null then name = "label"
- name = get_name(name)
- escapemark_names[e] = name
- return name
- end
-
- private var escapemark_names = new HashMap[EscapeMark, String]
-
- # Return a new name based on `s' and unique in the visitor
- fun get_name(s: String): String
- do
- if not self.names.has(s) then
- self.names.add(s)
- return s
- end
- var i = self.last + 1
- loop
- var s2 = s + i.to_s
- if not self.names.has(s2) then
- self.last = i
- self.names.add(s2)
- return s2
- end
- i = i + 1
- end
- end
-
- private var last: Int = 0
-
- private var names: HashSet[String] = new HashSet[String]
-
- # Generate a return with the value `s'
- fun ret(s: RuntimeVariable)
- do
- self.assign(self.frame.returnvar.as(not null), s)
- self.add("goto {self.frame.returnlabel.as(not null)};")
- 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.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
-
- private var collect_types_cache: HashMap[MType, Array[MClassType]] = new HashMap[MType, Array[MClassType]]
-
- fun resolve_for(mtype: MType, recv: RuntimeVariable): MType
- do
- if not mtype.need_anchor then return mtype
- #debug("resolve for {mtype} to {recv}:{recv.mcasttype}(declared as {recv.mtype}) (in {self.reciever.to_s}:{self.reciever.mtype})")
- var res = mtype.resolve_for(recv.mcasttype, self.frame.receiver, self.compiler.mainmodule, true)
- return res
- end
-
- # Generate a polymorphic send for the method `m' and the arguments `args'
- fun send(m: MMethod, args: Array[RuntimeVariable]): nullable RuntimeVariable
- do
- var types = self.collect_types(args.first)
-
- var res: nullable RuntimeVariable
- var ret = m.intro.msignature.return_mtype
- if m.is_new then
- ret = args.first.mtype
- res = self.new_var(ret)
- else if ret == null then
- res = null
- else
- ret = self.resolve_for(ret, args.first)
- res = self.new_var(ret)
- end
-
- if types.is_empty then
- self.add("/*BUG: no live types for {args.first.inspect} . {m}*/")
- return res
- end
- self.add("/* send {m} on {args.first.inspect} */")
- if args.first.mtype.ctype != "val*" then
- var propdefs = m.lookup_definitions(self.compiler.mainmodule, args.first.mtype)
- if propdefs.length == 0 then
- self.add("/* skip, no method {m} */")
- return res
- end
- assert propdefs.length == 1
- var propdef = propdefs.first
- var res2 = self.call(propdef, args.first.mtype.as(MClassType), args)
- if res != null then self.assign(res, res2.as(not null))
- return res
- end
- if args.first.mcasttype isa MNullableType then