import abstract_compiler
import rapid_type_analysis
+import compiler_ffi
redef class ModelBuilder
# Entry point to performs a global compilation on the AST of a complete program.
for t in runtime_type_analysis.live_types do
if t.ctype == "val*" then
compiler.generate_init_instance(t)
- compiler.generate_check_init_instance(t)
else
compiler.generate_box_instance(t)
end
v.add("\}")
end
- redef fun generate_check_init_instance(mtype)
- do
- if self.modelbuilder.toolcontext.opt_no_check_initialization.value then return
-
- var v = self.new_visitor
- var res = new RuntimeVariable("self", mtype, mtype)
- self.header.add_decl("void CHECK_NEW_{mtype.c_name}({mtype.ctype});")
- v.add_decl("/* allocate {mtype} */")
- v.add_decl("void CHECK_NEW_{mtype.c_name}({mtype.ctype} {res}) \{")
- self.generate_check_attr(v, res, mtype)
- v.add("\}")
- end
-
fun generate_box_instance(mtype: MClassType)
do
assert self.runtime_type_analysis.live_types.has(mtype)
v.add("res->value = value;")
v.add("return (val*)res;")
v.add("\}")
-
end
redef fun new_visitor do return new GlobalCompilerVisitor(self)
private var collect_types_cache: HashMap[MType, Array[MClassType]] = new HashMap[MType, Array[MClassType]]
+
+ redef fun compile_nitni_structs
+ do
+ self.header.add_decl("struct nitni_instance \{ val *value; \};")
+ end
end
# A visitor on the AST of property definition that generate the C code.
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);")
+ self.add("printf(\"Dead code executed!\\n\"); show_backtrace(1);")
return res
end
self.add("{res} = BOX_{valtype.c_name}({value}); /* autobox from {value.mtype} to {mtype} */")
return res
+ else if value.mtype.cname_blind == "void*" and mtype.cname_blind == "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("printf(\"Cast error: Cannot cast %s to %s.\\n\", \"{value.mtype}\", \"{mtype}\"); exit(1);")
+ self.add("printf(\"Cast error: Cannot cast %s to %s.\\n\", \"{value.mtype}\", \"{mtype}\"); show_backtrace(1);")
return res
end
end
# Finalizes a call to a method ´m´ on type ´recvtype´ with arguments ´args´
private fun finalize_call(m: MMethodDef, recvtype: MClassType, args: Array[RuntimeVariable]): nullable RuntimeVariable
do
- if args.length != m.msignature.arity + 1 then # because of self
- add("printf(\"NOT YET IMPLEMENTED: Invalid arity for {m}. {args.length} arguments given.\\n\"); exit(1);")
- debug("NOT YET IMPLEMENTED: Invalid arity for {m}. {args.length} arguments given.")
- return null
- end
+ 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)
do
if recv.mtype.ctype != "val*" then return
self.add("fprintf(stderr, \"BTD BUG: Dynamic type is %s, static type is %s\\n\", class_names[{recv}->classid], \"{recv.mcasttype}\");")
- self.add("exit(1);")
+ self.add("show_backtrace(1);")
end
redef fun isset_attribute(a, recv)
redef fun type_test(value, mtype, tag)
do
mtype = self.anchor(mtype)
- var mclasstype = mtype
- if mtype isa MNullableType then mclasstype = mtype.mtype
- assert mclasstype isa MClassType
- if not self.compiler.runtime_type_analysis.live_cast_types.has(mclasstype) then
+ if not self.compiler.runtime_type_analysis.live_cast_types.has(mtype) then
debug "problem: {mtype} was detected cast-dead"
abort
end
return res
end
- redef fun check_init_instance(recv, mtype)
- do
- if self.compiler.modelbuilder.toolcontext.opt_no_check_initialization.value then return
-
- 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
-
- self.add("CHECK_NEW_{mtype.c_name}({recv});")
- end
-
redef fun array_instance(array, elttype)
do
elttype = self.anchor(elttype)
end
var length = self.int_instance(array.length)
self.send(self.get_property("with_native", arraytype), [res, nat, length])
- self.check_init_instance(res, arraytype)
self.add("\}")
return res
end
v.add("return {frame.returnvar.as(not null)};")
end
v.add("\}")
+ if not self.c_name.has_substring("VIRTUAL", 0) then compiler.names[self.c_name] = "{mmethoddef.mclassdef.mmodule.name}::{mmethoddef.mclassdef.mclass.name}::{mmethoddef.mproperty.name} ({mmethoddef.location.file.filename}:{mmethoddef.location.line_start})"
end
redef fun call(v: VISITOR, arguments: Array[RuntimeVariable]): nullable RuntimeVariable