var outname = outfile(mainmodule)
- var orig_dir=".." # FIXME only works if `compile_dir` is a subdirectory of cwd
+ var orig_dir = compile_dir.relpath(".")
var outpath = orig_dir.join_path(outname).simplify_path
var makename = makefile_name(mainmodule)
var makepath = "{compile_dir}/{makename}"
if not initializers.is_empty then
var recv = arguments.first
- assert initializers.length == arguments.length - 1 else debug("expected {initializers.length}, got {arguments.length - 1}")
var i = 1
for p in initializers do
if p isa MMethod then
- self.send(p, [recv, arguments[i]])
+ var args = [recv]
+ for x in p.intro.msignature.mparameters do
+ args.add arguments[i]
+ i += 1
+ end
+ self.send(p, args)
else if p isa MAttribute then
self.write_attribute(p, recv, arguments[i])
+ i += 1
else abort
- i += 1
end
+ assert i == arguments.length
return self.send(callsite.mproperty, [recv])
end
# Generate a super call from a method definition
fun supercall(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): nullable RuntimeVariable is abstract
+ # Adapt the arguments of a method according to targetted `MMethodDef`
fun adapt_signature(m: MMethodDef, args: Array[RuntimeVariable]) is abstract
+ # Unbox all the arguments of a method when implemented `extern` or `intern`
+ fun unbox_signature_extern(m: MMethodDef, args: Array[RuntimeVariable]) is abstract
+
# 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 is abstract
+ # Box extern classes to be used in the generated code
+ fun box_extern(value: RuntimeVariable, mtype: MType): RuntimeVariable is abstract
+
+ # Unbox extern classes to be used in extern code (legacy NI and FFI)
+ fun unbox_extern(value: RuntimeVariable, mtype: MType): RuntimeVariable is abstract
+
# Generate a polymorphic subtype test
fun type_test(value: RuntimeVariable, mtype: MType, tag: String): RuntimeVariable is abstract
return name
end
+ # Insert a C label for associated with an escapemark
+ fun add_escape_label(e: nullable EscapeMark)
+ do
+ if e == null then return
+ if e.escapes.is_empty then return
+ add("BREAK_{escapemark_name(e)}: (void)0;")
+ end
+
private var escapemark_names = new HashMap[EscapeMark, String]
# Return a "const char*" variable associated to the classname of the dynamic type of an object
return res
end
+ # The difference with `new_var` is the C static type of the local variable
+ fun new_var_extern(mtype: MType): RuntimeVariable
+ do
+ mtype = self.anchor(mtype)
+ var name = self.get_name("var")
+ var res = new RuntimeVariable(name, mtype, mtype)
+ self.add_decl("{mtype.ctype_extern} {name} /* : {mtype} for extern */;")
+ return res
+ end
+
# Return a new uninitialized named runtime_variable
fun new_named_var(mtype: MType, name: String): RuntimeVariable
do
# Return the C type associated to a given Nit static type
fun ctype: String do return "val*"
+ # C type outside of the compiler code and in boxes
+ fun ctype_extern: String do return "val*"
+
+ # Short name of the `ctype` to use in unions
fun ctypename: String do return "val"
# Return the name of the C structure associated to a Nit live type
return "char*"
else if mclass.name == "NativeArray" then
return "val*"
- else if mclass.kind == extern_kind then
- return "void*"
else
return "val*"
end
end
+ redef fun ctype_extern: String
+ do
+ if mclass.kind == extern_kind then
+ return "void*"
+ else
+ return ctype
+ end
+ end
+
redef fun ctypename: String
do
if mclass.name == "Int" then
else if mclass.name == "NativeArray" then
#return "{self.arguments.first.ctype}*"
return "val"
- else if mclass.kind == extern_kind then
- return "ptr"
else
return "val"
end
end
if pname != "==" and pname != "!=" then
v.adapt_signature(mpropdef, arguments)
+ v.unbox_signature_extern(mpropdef, arguments)
end
if cname == "Int" then
if pname == "output" then
externname = at.arg_as_string(v.compiler.modelbuilder)
if externname == null then return false
else
- var nextern = self.n_extern
- if nextern == null then return false
- externname = nextern.text.substring(1, nextern.text.length-2)
+ return false
end
if location.file != null then
var file = location.file.filename
var ret = mpropdef.msignature.return_mtype
if ret != null then
ret = v.resolve_for(ret, arguments.first)
- res = v.new_var(ret)
+ res = v.new_var_extern(ret)
end
v.adapt_signature(mpropdef, arguments)
+ v.unbox_signature_extern(mpropdef, arguments)
if res == null then
v.add("{externname}({arguments.join(", ")});")
else
v.add("{res} = {externname}({arguments.join(", ")});")
+ res = v.box_extern(res, ret.as(not null))
v.ret(res)
end
return true
externname = at.arg_as_string(v.compiler.modelbuilder)
if externname == null then return false
else
- var nextern = self.n_extern
- if nextern == null then return false
- externname = nextern.text.substring(1, nextern.text.length-2)
+ return false
end
if location.file != null then
var file = location.file.filename
v.add_extern(file)
end
v.adapt_signature(mpropdef, arguments)
+ v.unbox_signature_extern(mpropdef, arguments)
var ret = arguments.first.mtype
- var res = v.new_var(ret)
+ var res = v.new_var_extern(ret)
arguments.shift
v.add("{res} = {externname}({arguments.join(", ")});")
+ res = v.box_extern(res, ret)
v.ret(res)
return true
end
private fun compile_to_c(v: AbstractCompilerVisitor, mpropdef: MMethodDef, arguments: Array[RuntimeVariable])
do
if mpropdef == self.mfree_init then
- if mpropdef.mproperty.is_root_init then
- assert self.super_inits == null
- assert arguments.length == 1
- if not mpropdef.is_intro then
- v.supercall(mpropdef, arguments.first.mtype.as(MClassType), arguments)
- end
- return
- end
-
- var super_inits = self.super_inits
- if super_inits != null then
- var args_of_super = arguments
- if arguments.length > 1 then args_of_super = [arguments.first]
- for su in super_inits do
- v.send(su, args_of_super)
- end
- end
-
- var recv = arguments.first
- var i = 1
- # Collect undefined attributes
- for npropdef in self.n_propdefs do
- if npropdef isa AAttrPropdef and npropdef.n_expr == null and not npropdef.noinit then
- v.write_attribute(npropdef.mpropdef.mproperty, recv, arguments[i])
- i += 1
- end
+ assert mpropdef.mproperty.is_root_init
+ assert arguments.length == 1
+ if not mpropdef.is_intro then
+ v.supercall(mpropdef, arguments.first.mtype.as(MClassType), arguments)
end
+ return
else
abort
end
redef fun expr(v) do return v.frame.arguments.first
end
-redef class AContinueExpr
- redef fun stmt(v) do v.add("goto CONTINUE_{v.escapemark_name(self.escapemark)};")
-end
-
-redef class ABreakExpr
+redef class AEscapeExpr
redef fun stmt(v) do v.add("goto BREAK_{v.escapemark_name(self.escapemark)};")
end
redef fun stmt(v)
do
v.stmt(self.n_block)
- var escapemark = self.escapemark
- if escapemark != null then
- v.add("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
- end
+ v.add_escape_label(break_mark)
end
end
var cond = v.expr_bool(self.n_expr)
v.add("if (!{cond}) break;")
v.stmt(self.n_block)
- v.add("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
+ v.add_escape_label(continue_mark)
v.add("\}")
- v.add("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
+ v.add_escape_label(break_mark)
end
end
do
v.add("for(;;) \{")
v.stmt(self.n_block)
- v.add("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
+ v.add_escape_label(continue_mark)
v.add("\}")
- v.add("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
+ v.add_escape_label(break_mark)
end
end
v.stmt(self.n_block)
- v.add("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
+ v.add_escape_label(continue_mark)
var succ = v.send(v.get_property("successor", variable.mtype), [variable, one])
assert succ != null
v.assign(variable, succ)
v.add("\}")
- v.add("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
+ v.add_escape_label(break_mark)
return
end
abort
end
v.stmt(self.n_block)
- v.add("CONTINUE_{v.escapemark_name(escapemark)}: (void)0;")
+ v.add_escape_label(continue_mark)
var next_meth = self.method_next
assert next_meth != null
v.compile_callsite(next_meth, [it])
v.add("\}")
- v.add("BREAK_{v.escapemark_name(escapemark)}: (void)0;")
+ v.add_escape_label(break_mark)
+
+ var method_finish = self.method_finish
+ if method_finish != null then
+ # TODO: Find a way to call this also in long escape (e.g. return)
+ v.compile_callsite(method_finish, [it])
+ end
end
end
return v.native_array_instance(elttype, l)
else if ctype == "val*" then
recv = v.init_instance(mtype)
- else if ctype == "void*" then
+ else if ctype == "char*" then
recv = v.new_expr("NULL/*special!*/", mtype)
else
recv = v.new_expr("({ctype})0/*special!*/", mtype)