self.toolcontext.info("*** GENERATING C ***", 1)
var compiler = new GlobalCompiler(mainmodule, self, runtime_type_analysis)
+ compiler.do_compilation
+ compiler.display_stats
+
+ var time1 = get_time
+ self.toolcontext.info("*** END GENERATING C: {time1-time0} ***", 2)
+ write_and_make(compiler)
+ end
+end
+
+# Compiler that use global compilation and perform hard optimisations like:
+# * customization
+# * switch dispatch
+# * inlining
+class GlobalCompiler
+ super AbstractCompiler
+
+ redef type VISITOR: GlobalCompilerVisitor
+
+ # The result of the RTA (used to know live types and methods)
+ var runtime_type_analysis: RapidTypeAnalysis
+
+ init
+ do
+ var file = new_file("{mainmodule.c_name}.nitgg")
+ self.header = new CodeWriter(file)
+ self.live_primitive_types = new Array[MClassType]
+ for t in runtime_type_analysis.live_types do
+ if t.ctype != "val*" or t.mclass.name == "Pointer" then
+ self.live_primitive_types.add(t)
+ end
+ end
+ end
+
+ redef fun do_compilation
+ do
+ var compiler = self
+
compiler.compile_header
if mainmodule.model.get_mclasses_by_name("Pointer") != null then
# Compile until all runtime_functions are visited
while not compiler.todos.is_empty do
var m = compiler.todos.shift
- self.toolcontext.info("Compile {m} ({compiler.seen.length-compiler.todos.length}/{compiler.seen.length})", 3)
+ modelbuilder.toolcontext.info("Compile {m} ({compiler.seen.length-compiler.todos.length}/{compiler.seen.length})", 3)
m.compile_to_c(compiler)
end
- self.toolcontext.info("Total methods to compile to C: {compiler.seen.length}", 2)
-
- compiler.display_stats
+ modelbuilder.toolcontext.info("Total methods to compile to C: {compiler.seen.length}", 2)
- var time1 = get_time
- self.toolcontext.info("*** END GENERATING C: {time1-time0} ***", 2)
- write_and_make(compiler)
- end
-end
-
-# Compiler that use global compilation and perform hard optimisations like:
-# * customization
-# * switch dispatch
-# * inlining
-class GlobalCompiler
- super AbstractCompiler
-
- redef type VISITOR: GlobalCompilerVisitor
-
- # The result of the RTA (used to know live types and methods)
- var runtime_type_analysis: RapidTypeAnalysis
-
- init(mainmodule: MModule, modelbuilder: ModelBuilder, runtime_type_analysis: RapidTypeAnalysis)
- do
- super(mainmodule, modelbuilder)
- var file = new_file("{mainmodule.name}.nitgg")
- self.header = new CodeWriter(file)
- self.runtime_type_analysis = runtime_type_analysis
- self.live_primitive_types = new Array[MClassType]
- for t in runtime_type_analysis.live_types do
- if t.ctype != "val*" or t.mclass.name == "Pointer" then
- self.live_primitive_types.add(t)
- end
- end
end
# Compile class names (for the class_name and output_class_name methods)
# Subset of runtime_type_analysis.live_types that contains only primitive types
# Used to implement the equal test
- var live_primitive_types: Array[MClassType]
+ var live_primitive_types: Array[MClassType] is noinit
# Add a new todo task
fun todo(m: AbstractRuntimeFunction)
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\"); show_backtrace(1);")
+ 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} */")
# 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}\"); show_backtrace(1);")
+ self.add("PRINT_ERROR(\"Cast error: Cannot cast %s to %s.\\n\", \"{value.mtype}\", \"{mtype}\"); fatal_exit(1);")
return res
end
end
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\"); show_backtrace(1);")
+ 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} */")
redef fun native_array_instance(elttype: MType, length: RuntimeVariable): RuntimeVariable
do
var ret_type = self.get_class("NativeArray").get_mtype([elttype])
+ ret_type = anchor(ret_type).as(MClassType)
return self.new_expr("NEW_{ret_type.c_name}({length})", ret_type)
end
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
+ if ret == null then
res = null
else
ret = self.resolve_for(ret, args.first)
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 == "==" then
+ 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);")
do
if recv.mtype.ctype != "val*" 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("show_backtrace(1);")
+ self.add("fatal_exit(1);")
end
redef fun isset_attribute(a, recv)
# (usually is a live type but no strong guarantee)
var recv: MClassType
- init(mmethoddef: MMethodDef, recv: MClassType)
- do
- super(mmethoddef)
- self.recv = recv
- end
-
redef fun build_c_name
do
var res = self.c_name_cache
selfvar.is_exact = true
end
var arguments = new Array[RuntimeVariable]
- var frame = new Frame(v, mmethoddef, recv, arguments)
+ var frame = new StaticFrame(v, mmethoddef, recv, arguments)
v.frame = frame
var sig = new FlatBuffer
if ret != null then
ret = v.resolve_for(ret, selfvar)
sig.append("{ret.ctype} ")
- else if mmethoddef.mproperty.is_new then
- ret = recv
- sig.append("{ret.ctype} ")
else
sig.append("void ")
end
redef fun call(v: VISITOR, arguments: Array[RuntimeVariable]): nullable RuntimeVariable
do
var ret = self.mmethoddef.msignature.return_mtype
- if self.mmethoddef.mproperty.is_new then
- ret = recv
- end
if ret != null then
ret = v.resolve_for(ret, arguments.first)
end
if self.mmethoddef.can_inline(v) then
- var frame = new Frame(v, self.mmethoddef, self.recv, arguments)
+ var frame = new StaticFrame(v, self.mmethoddef, self.recv, arguments)
frame.returnlabel = v.get_name("RET_LABEL")
if ret != null then
frame.returnvar = v.new_var(ret)