From: Jean Privat Date: Thu, 13 Nov 2008 21:29:17 +0000 (-0500) Subject: Factorize common strings in generated C. X-Git-Url: http://nitlanguage.org Factorize common strings in generated C. It reduces the constant pool size in the executable. --- diff --git a/lib/nit_common.h b/lib/nit_common.h index 8960af8..e0b22bf 100644 --- a/lib/nit_common.h +++ b/lib/nit_common.h @@ -100,8 +100,10 @@ extern int glob_argc; extern char ** glob_argv; struct trace_t { - struct trace_t *prev; - const char *text; + struct trace_t *prev; /* previous stack frame */ + const char *file; /* source filename */ + int line; /* line number */ + const char *meth; /* method name */ }; extern struct trace_t *tracehead; typedef enum {true = (1==1),false = (0==1)} bool; diff --git a/lib/nit_main.c b/lib/nit_main.c index 78df533..047cd14 100644 --- a/lib/nit_main.c +++ b/lib/nit_main.c @@ -38,7 +38,7 @@ struct trace_t *tracehead = NULL; void nit_exit(int i) { fprintf(stderr, ",---- Stack trace -- - - -\n"); while(tracehead != NULL) { - fprintf(stderr, "| %s\n",tracehead->text); + fprintf(stderr, "| %s (%s:%d)\n", tracehead->meth, tracehead->file, tracehead->line); if (tracehead == tracehead->prev) break; tracehead = tracehead->prev; } diff --git a/src/compiling/compiling_global.nit b/src/compiling/compiling_global.nit index 4915ea2..5ae0cb0 100644 --- a/src/compiling/compiling_global.nit +++ b/src/compiling/compiling_global.nit @@ -493,6 +493,7 @@ redef class MMSrcModule # Compile sep files meth compile_mod_to_c(v: CompilerVisitor) do + v.add_decl("#define LOCATE_{name} \"{filename}\"") if not v.tc.global then v.add_decl("extern const int SFT_{name}[];") end diff --git a/src/compiling/compiling_methods.nit b/src/compiling/compiling_methods.nit index baa66bf..8a95a4a 100644 --- a/src/compiling/compiling_methods.nit +++ b/src/compiling/compiling_methods.nit @@ -131,7 +131,18 @@ redef class CompilerVisitor # Variable where a functionnal nit return must store its value readable writable attr _return_value: String - + + # Generate an fprintf to display an error location + meth printf_locate_error(node: PNode): String + do + var s = "fprintf(stderr, \"" + if method != null then s.append(" in %s") + s.append(" (%s:%d)\\n\", ") + if method != null then s.append("LOCATE_{method.cname}, ") + s.append("LOCATE_{module.name}, {node.line_number});") + return s + end + redef init(module: MMSrcModule) do super @@ -309,13 +320,14 @@ redef class MMSrcMethod args.add(" param{i}") end var cs = decl_csignature(v, args) + v.add_decl("#define LOCATE_{cname} \"{full_name}\"") v.add_instr("{cs} \{") v.indent var ctx_old = v.ctx v.ctx = new CContext - v.add_decl("struct trace_t trace = \{NULL, \"{module.name}::{local_class.name}::{name} ({node.locate})\"};") + v.add_decl("struct trace_t trace = \{NULL, LOCATE_{module.name}, {node.line_number}, LOCATE_{cname}};") v.add_instr("trace.prev = tracehead; tracehead = &trace;") var s = do_compile_inside(v, args) v.add_instr("tracehead = trace.prev;") @@ -372,7 +384,7 @@ redef class MMType do # Fixme: handle formaltypes var g = local_class.global - v.add_instr("if (({recv}!=NIT_NULL) && !VAL_ISA({recv}, {g.color_id}, {g.id_id})) \{ fprintf(stderr, \"Cast failled at {n.locate}\\n\"); nit_exit(1); } /*cast {self}*/;") + v.add_instr("if (({recv}!=NIT_NULL) && !VAL_ISA({recv}, {g.color_id}, {g.id_id})) \{ fprintf(stderr, \"Cast failled\"); {v.printf_locate_error(n)} nit_exit(1); } /*cast {self}*/;") end end @@ -425,8 +437,8 @@ redef class AConcreteMethPropdef else v.return_value = null end + v.method = method if self isa AConcreteInitPropdef then - v.method = method v.invoke_super_init_calls_after(null) end if n_block != null then @@ -450,7 +462,8 @@ end redef class ADeferredMethPropdef redef meth do_compile_inside(v, method, params) do - v.add_instr("fprintf(stderr, \"Deferred method {name} called ({first_token.locate})\\n\");") + v.add_instr("fprintf(stderr, \"Deferred method %s called\");") + v.add_instr(v.printf_locate_error(self)) v.add_instr("nit_exit(1);") if method.signature.return_type != null then return("NIT_NULL") @@ -735,7 +748,7 @@ end redef class AAbortExpr redef meth compile_stmt(v) do - v.add_instr("fprintf(stderr, \"Aborted: {locate}\\n\"); nit_exit(1);") + v.add_instr("fprintf(stderr, \"Aborted\"); {v.printf_locate_error(self)} nit_exit(1);") end end @@ -888,11 +901,11 @@ redef class AAssertExpr redef meth compile_stmt(v) do var e = v.compile_expr(n_expr) - var s = "Assert" + var s = "" if n_id != null then - s = "Assert '{n_id.text}' " + s = " '{n_id.text}' " end - v.add_instr("if (!UNTAG_Bool({e})) \{ fprintf(stderr, \"{s} failed: {locate}\\n\"); nit_exit(1);}") + v.add_instr("if (!UNTAG_Bool({e})) \{ fprintf(stderr, \"Assert%s failed\", \"{s}\"); {v.printf_locate_error(self)} nit_exit(1);}") end end diff --git a/src/parser/parser_prod.nit b/src/parser/parser_prod.nit index dd9b34e..d2e0ffe 100644 --- a/src/parser/parser_prod.nit +++ b/src/parser/parser_prod.nit @@ -37,6 +37,9 @@ redef class PNode # Give a human readable location of the node. meth locate: String is abstract + # Return only the line number of the node + meth line_number: Int is abstract + # Debug method: output a message prefixed with the location. meth printl(str: String) do @@ -53,6 +56,8 @@ redef class Token do return "{filename}:{line},{pos}" end + + redef meth line_number do return line end redef class Prod @@ -76,11 +81,20 @@ redef class Prod end redef meth replace_with(n: PNode) + do + super + assert n isa Prod + n.first_token = first_token + n.last_token = last_token + end + + redef meth line_number do - super - assert n isa Prod - n.first_token = first_token - n.last_token = last_token + if first_token != null then + return first_token.line + else + return 0 + end end end diff --git a/src/parser/xss/nodes.xss b/src/parser/xss/nodes.xss index 3d59406..46cfafd 100644 --- a/src/parser/xss/nodes.xss +++ b/src/parser/xss/nodes.xss @@ -65,6 +65,9 @@ redef class PNode # Give a human readable location of the node. meth locate: String is abstract + # Return only the line number of the node + meth line_number: Int is abstract + # Debug method: output a message prefixed with the location. meth printl(str: String) do @@ -81,6 +84,8 @@ redef class Token do return "{filename}:{line},{pos}" end + + redef meth line_number do return line end redef class Prod @@ -104,11 +109,20 @@ redef class Prod end redef meth replace_with(n: PNode) + do + super + assert n isa Prod + n.first_token = first_token + n.last_token = last_token + end + + redef meth line_number do - super - assert n isa Prod - n.first_token = first_token - n.last_token = last_token + if first_token != null then + return first_token.line + else + return 0 + end end end