X-Git-Url: http://nitlanguage.org diff --git a/src/abstract_compiler.nit b/src/abstract_compiler.nit index 4705f08..f54857e 100644 --- a/src/abstract_compiler.nit +++ b/src/abstract_compiler.nit @@ -21,6 +21,7 @@ import literal import typing import auto_super_init import frontend +import common_ffi # Add compiling options redef class ToolContext @@ -50,6 +51,8 @@ redef class ToolContext var opt_no_check_other: OptionBool = new OptionBool("Disable implicit tests: unset attribute, null receiver (dangerous)", "--no-check-other") # --typing-test-metrics var opt_typing_test_metrics: OptionBool = new OptionBool("Enable static and dynamic count of all type tests", "--typing-test-metrics") + # --no-stacktrace + var opt_no_stacktrace: OptionBool = new OptionBool("Disables libunwind and generation of C stack traces (can be problematic when compiling to targets such as Android or NaCl)", "--no-stacktrace") # --stack-trace-C-to-Nit-name-binding var opt_stacktrace: OptionBool = new OptionBool("Enables the use of gperf to bind C to Nit function names when encountering a Stack trace at runtime", "--nit-stacktrace") @@ -60,6 +63,7 @@ redef class ToolContext self.option_context.add_option(self.opt_no_check_covariance, self.opt_no_check_initialization, self.opt_no_check_assert, self.opt_no_check_autocast, self.opt_no_check_other) self.option_context.add_option(self.opt_typing_test_metrics) self.option_context.add_option(self.opt_stacktrace) + self.option_context.add_option(self.opt_no_stacktrace) end end @@ -90,6 +94,11 @@ redef class ModelBuilder toolcontext.error(null, "Cannot determine the nit clib path. define envvar NIT_DIR.") end + if toolcontext.opt_no_stacktrace.value and toolcontext.opt_stacktrace.value then + print "Cannot use --nit-stacktrace when --no-stacktrace is activated" + exit(1) + end + # Add user defined cc_paths cc_paths.append(toolcontext.opt_cc_path.value) @@ -98,8 +107,14 @@ redef class ModelBuilder cc_paths.append(path_env.split_with(':')) end + var compile_dir = toolcontext.opt_compile_dir.value + if compile_dir == null then compile_dir = ".nit_compile" + self.compile_dir = compile_dir end + # The compilation directory + var compile_dir: String + protected fun write_and_make(compiler: AbstractCompiler) do var mainmodule = compiler.mainmodule @@ -110,9 +125,6 @@ redef class ModelBuilder var time0 = get_time self.toolcontext.info("*** WRITING C ***", 1) - var compile_dir = toolcontext.opt_compile_dir.value - if compile_dir == null then compile_dir = ".nit_compile" - compile_dir.mkdir if self.toolcontext.opt_stacktrace.value then compiler.build_c_to_nit_bindings @@ -188,6 +200,14 @@ redef class ModelBuilder self.toolcontext.info("Total C source files to compile: {cfiles.length}", 2) + # FFI + for m in mainmodule.in_importation.greaters do if mmodule2nmodule.keys.has(m) then + var amodule = mmodule2nmodule[m] + if m.uses_ffi or amodule.uses_legacy_ni then + compiler.finalize_ffi_for_module(amodule) + end + end + # Generate the Makefile var makename = "{mainmodule.name}.mk" @@ -199,7 +219,18 @@ redef class ModelBuilder p = orig_dir.join_path(p).simplify_path cc_includes += " -I \"" + p + "\"" end - makefile.write("CC = ccache cc\nCFLAGS = -g -O2\nCINCL = {cc_includes}\nLDFLAGS ?= \nLDLIBS ?= -lunwind -lm -lgc\n\n") + + var linker_options = new HashSet[String] + for m in mainmodule.in_importation.greaters do if mmodule2nmodule.keys.has(m) then + var amod = mmodule2nmodule[m] + linker_options.add(amod.c_linker_options) + end + + if not toolcontext.opt_no_stacktrace.value then + linker_options.add("-lunwind") + end + + makefile.write("CC = ccache cc\nCFLAGS = -g -O2\nCINCL = {cc_includes}\nLDFLAGS ?= \nLDLIBS ?= -lm -lgc {linker_options.join(" ")}\n\n") makefile.write("all: {outpath}\n\n") var ofiles = new Array[String] @@ -216,11 +247,13 @@ redef class ModelBuilder # Compile each required extern body into a specific .o for f in compiler.extern_bodies do - var basename = f.filename.basename(".c") - var o = "{basename}.extern.o" - var ff = orig_dir.join_path(f.filename).simplify_path - makefile.write("{o}: {ff}\n\t$(CC) $(CFLAGS) -D NONITCNI {f.cflags} -c -o {o} {ff}\n\n") - ofiles.add(o) + if f isa ExternCFile then + var basename = f.filename.basename(".c") + var o = "{basename}.extern.o" + var ff = orig_dir.join_path(f.filename).simplify_path + makefile.write("{o}: {ff}\n\t$(CC) $(CFLAGS) -D NONITCNI {f.cflags} -c -o {o} {ff}\n\n") + ofiles.add(o) + end end # Link edition @@ -319,8 +352,7 @@ abstract class AbstractCompiler # Binds the generated C function names to Nit function names fun build_c_to_nit_bindings do - var compile_dir = modelbuilder.toolcontext.opt_compile_dir.value - if compile_dir == null then compile_dir = ".nit_compile" + var compile_dir = modelbuilder.compile_dir var stream = new OFStream.open("{compile_dir}/C_fun_names") stream.write("%\{\n#include \"c_functions_hash.h\"\n%\}\n") @@ -353,31 +385,30 @@ abstract class AbstractCompiler # This method call compile_header_strucs method that has to be refined fun compile_header do var v = self.header - self.header.add_decl("#define UNW_LOCAL_ONLY") + var toolctx = modelbuilder.toolcontext self.header.add_decl("#include ") self.header.add_decl("#include ") self.header.add_decl("#include ") - if modelbuilder.toolcontext.opt_stacktrace.value then - self.header.add_decl("#include \"c_functions_hash.h\"") - end - self.header.add_decl("#include ") - self.header.add_decl("#include ") self.header.add_decl("#include ") compile_header_structs + compile_nitni_structs # Signal handler function prototype self.header.add_decl("void show_backtrace(int);") - # Global variable used by the legacy native interface + # Global variable used by intern methods self.header.add_decl("extern int glob_argc;") self.header.add_decl("extern char **glob_argv;") self.header.add_decl("extern val *glob_sys;") end - # Declaration of structures the live Nit types + # Declaration of structures for live Nit types protected fun compile_header_structs is abstract + # Declaration of structures for nitni undelying the FFI + protected fun compile_nitni_structs is abstract + # Generate the main C function. # This function: # * allocate the Sys object if it exists @@ -386,6 +417,14 @@ abstract class AbstractCompiler fun compile_main_function do var v = self.new_visitor + if modelbuilder.toolcontext.opt_stacktrace.value then + v.add_decl("#include \"c_functions_hash.h\"") + end + v.add_decl("#include ") + if not modelbuilder.toolcontext.opt_no_stacktrace.value then + v.add_decl("#define UNW_LOCAL_ONLY") + v.add_decl("#include ") + end v.add_decl("int glob_argc;") v.add_decl("char **glob_argv;") v.add_decl("val *glob_sys;") @@ -401,45 +440,52 @@ abstract class AbstractCompiler end end + v.add_decl("void sig_handler(int signo)\{") + v.add_decl("printf(\"Caught signal : %s\\n\", strsignal(signo));") + v.add_decl("show_backtrace(signo);") + v.add_decl("\}") + v.add_decl("void show_backtrace (int signo) \{") - v.add_decl("char* opt = getenv(\"NIT_NO_STACK\");") - v.add_decl("unw_cursor_t cursor;") - v.add_decl("if(opt==NULL)\{") - v.add_decl("unw_context_t uc;") - v.add_decl("unw_word_t ip;") - v.add_decl("char* procname = malloc(sizeof(char) * 100);") - v.add_decl("unw_getcontext(&uc);") - v.add_decl("unw_init_local(&cursor, &uc);") - v.add_decl("printf(\"-------------------------------------------------\\n\");") - v.add_decl("printf(\"-- Stack Trace ------------------------------\\n\");") - v.add_decl("printf(\"-------------------------------------------------\\n\");") - v.add_decl("while (unw_step(&cursor) > 0) \{") - v.add_decl(" unw_get_proc_name(&cursor, procname, 100, &ip);") - if modelbuilder.toolcontext.opt_stacktrace.value then - v.add_decl(" const C_Nit_Names* recv = get_nit_name(procname, strlen(procname));") - v.add_decl(" if (recv != 0)\{") - v.add_decl(" printf(\"` %s\\n\", recv->nit_name);") - v.add_decl(" \}else\{") - v.add_decl(" printf(\"` %s\\n\", procname);") - v.add_decl(" \}") - else - v.add_decl(" printf(\"` %s \\n\",procname);") + if not modelbuilder.toolcontext.opt_no_stacktrace.value then + v.add_decl("char* opt = getenv(\"NIT_NO_STACK\");") + v.add_decl("unw_cursor_t cursor;") + v.add_decl("if(opt==NULL)\{") + v.add_decl("unw_context_t uc;") + v.add_decl("unw_word_t ip;") + v.add_decl("char* procname = malloc(sizeof(char) * 100);") + v.add_decl("unw_getcontext(&uc);") + v.add_decl("unw_init_local(&cursor, &uc);") + v.add_decl("printf(\"-------------------------------------------------\\n\");") + v.add_decl("printf(\"-- Stack Trace ------------------------------\\n\");") + v.add_decl("printf(\"-------------------------------------------------\\n\");") + v.add_decl("while (unw_step(&cursor) > 0) \{") + v.add_decl(" unw_get_proc_name(&cursor, procname, 100, &ip);") + if modelbuilder.toolcontext.opt_stacktrace.value then + v.add_decl(" const C_Nit_Names* recv = get_nit_name(procname, strlen(procname));") + v.add_decl(" if (recv != 0)\{") + v.add_decl(" printf(\"` %s\\n\", recv->nit_name);") + v.add_decl(" \}else\{") + v.add_decl(" printf(\"` %s\\n\", procname);") + v.add_decl(" \}") + else + v.add_decl(" printf(\"` %s \\n\",procname);") + end + v.add_decl("\}") + v.add_decl("printf(\"-------------------------------------------------\\n\");") + v.add_decl("free(procname);") + v.add_decl("\}") end - v.add_decl("\}") - v.add_decl("printf(\"-------------------------------------------------\\n\");") - v.add_decl("free(procname);") - v.add_decl("\}") v.add_decl("exit(signo);") v.add_decl("\}") v.add_decl("int main(int argc, char** argv) \{") - v.add("signal(SIGABRT, show_backtrace);") - v.add("signal(SIGFPE, show_backtrace);") - v.add("signal(SIGILL, show_backtrace);") - v.add("signal(SIGINT, show_backtrace);") - v.add("signal(SIGTERM, show_backtrace);") - v.add("signal(SIGSEGV, show_backtrace);") + v.add("signal(SIGABRT, sig_handler);") + v.add("signal(SIGFPE, sig_handler);") + v.add("signal(SIGILL, sig_handler);") + v.add("signal(SIGINT, sig_handler);") + v.add("signal(SIGTERM, sig_handler);") + v.add("signal(SIGSEGV, sig_handler);") v.add("glob_argc = argc; glob_argv = argv;") v.add("initialize_gc_option();") @@ -488,15 +534,12 @@ abstract class AbstractCompiler v.add("\}") end - # List of additional .c files required to compile (native interface) - var extern_bodies = new Array[ExternCFile] + # List of additional files required to compile (FFI) + var extern_bodies = new Array[ExternFile] # This is used to avoid adding an extern file more than once private var seen_extern = new ArraySet[String] - # Generate code that check if an instance is correctly initialized - fun generate_check_init_instance(mtype: MClassType) is abstract - # Generate code that initialize the attributes on a new instance fun generate_init_attr(v: VISITOR, recv: RuntimeVariable, mtype: MClassType) do @@ -588,6 +631,16 @@ abstract class AbstractCompiler if b == 0 then return "n/a" return ((a*10000/b).to_f / 100.0).to_precision(2) end + + fun finalize_ffi_for_module(nmodule: AModule) + do + var visitor = new_visitor + nmodule.finalize_ffi(visitor, modelbuilder) + nmodule.finalize_nitni(visitor) + end + + # Does this compiler support the FFI? + fun supports_ffi: Bool do return false end # A file unit (may be more than one file if @@ -804,9 +857,6 @@ abstract class AbstractCompilerVisitor end end - # Generate a check-init-instance - fun check_init_instance(recv: RuntimeVariable, mtype: MClassType) is abstract - # Names handling private var names: HashSet[String] = new HashSet[String] @@ -1181,14 +1231,6 @@ class Frame var returnlabel: nullable String writable = null end -# An extern C file to compile -class ExternCFile - # The filename of the file - var filename: String - # Additionnal specific CC compiler -c flags - var cflags: String -end - redef class MType # Return the C type associated to a given Nit static type fun ctype: String do return "val*" @@ -1654,7 +1696,7 @@ redef class AInternMethPropdef return end if pname == "exit" then - v.add("show_backtrace({arguments[1]});") + v.add("exit({arguments[1]});") return else if pname == "sys" then v.ret(v.new_expr("glob_sys", ret.as(not null))) @@ -2235,7 +2277,6 @@ redef class ACrangeExpr var mtype = self.mtype.as(MClassType) var res = v.init_instance(mtype) var it = v.send(v.get_property("init", res.mtype), [res, i1, i2]) - v.check_init_instance(res, mtype) return res end end @@ -2248,7 +2289,6 @@ redef class AOrangeExpr var mtype = self.mtype.as(MClassType) var res = v.init_instance(mtype) var it = v.send(v.get_property("without_last", res.mtype), [res, i1, i2]) - v.check_init_instance(res, mtype) return res end end @@ -2367,13 +2407,13 @@ redef class ASuperExpr args = v.frame.arguments end - var mproperty = self.mproperty - if mproperty != null then - if mproperty.intro.msignature.arity == 0 then + var callsite = self.callsite + if callsite != null then + if callsite.mproperty.intro.msignature.arity == 0 then args = [recv] end # Super init call - var res = v.send(mproperty, args) + var res = v.compile_callsite(callsite, args) return res end @@ -2405,7 +2445,6 @@ redef class ANewExpr #self.debug("got {res2} from {mproperty}. drop {recv}") return res2 end - v.check_init_instance(recv, mtype) return recv end end @@ -2492,3 +2531,14 @@ redef class MModule end private var properties_cache: Map[MClass, Set[MProperty]] = new HashMap[MClass, Set[MProperty]] end + +redef class AModule + # Does this module use the legacy native interface? + fun uses_legacy_ni: Bool is abstract + + # Write FFI results to file + fun finalize_ffi(v: AbstractCompilerVisitor, modelbuilder: ModelBuilder) is abstract + + # Write nitni results to file + fun finalize_nitni(v: AbstractCompilerVisitor) is abstract +end