compile: factorize abort code in the clib
authorJean Privat <jean@pryen.org>
Wed, 5 Jan 2011 03:22:00 +0000 (22:22 -0500)
committerJean Privat <jean@pryen.org>
Wed, 5 Jan 2011 03:22:00 +0000 (22:22 -0500)
Instead of generating again and again the same insructions for aborts
we have now a function nit_abort in the runtime C library.

Signed-off-by: Jean Privat <jean@pryen.org>

clib/nit_common.h
clib/nit_main.c
src/compiling/compiling_icode.nit

index 2fe713c..08fb294 100644 (file)
@@ -145,6 +145,7 @@ extern struct stack_frame_t *stack_frame_head;
 typedef enum {true = (1==1),false = (0==1)} bool;
 
 void nit_exit(int);
+void nit_abort(const char*, const char*, const char*, int);
 
 #define CALL(r,c) ((VAL2VFT(r)[c].f))
 #define ATTR(r,c) (*(val_t*)(VAL2OBJ(r)+c))
index 712c72c..630847f 100644 (file)
@@ -151,3 +151,11 @@ void nit_exit(int i) {
        }
        exit(i);
 }
+
+void nit_abort(const char* s, const char* msg, const char* loc, int line) {
+       fprintf(stderr, s, msg);
+       fprintf(stderr, " (%s", loc);
+       if (line != 0) fprintf(stderr, ":%d", line);
+       fprintf(stderr, ")\n");
+       nit_exit(1);
+}
index 9b14dec..9c79e8b 100644 (file)
@@ -939,29 +939,25 @@ redef class IAbort
        do
                v.add_location(location)
                var w = v.new_instr
-               w.add("fprintf(stderr")
-               for t in texts do
-                       w.add(", \"")
-                       w.add(t)
+               w.add("nit_abort(\"")
+               w.add(texts[0])
+               if texts.length > 1 then
+                       w.add("\", \"")
+                       w.add(texts[1])
                        w.add("\"")
+               else
+                       w.add("\", NULL")
                end
-               w.add(");\n")
-
-               var ll = location
-               w = v.new_instr
-               w.add("fprintf(stderr, \" (%s")
-               if ll != null then
-                       w.add(":%d")
-               end
-               w.add(")\\n\", LOCATE_")
+               w.add(", LOCATE_")
                w.add(module_location.name.to_s)
+               var ll = location
                if ll != null then
                        w.add(", ")
                        w.add(ll.line_start.to_s)
+               else
+                       w.add(", 0")
                end
                w.add(");\n")
-
-               v.add_instr("nit_exit(1);")
        end
 end