src: Modified compilers for the support of the new Integers
[nit.git] / src / compiler / abstract_compiler.nit
index dbe6fc1..f0a66ac 100644 (file)
@@ -220,7 +220,7 @@ class MakefileToolchain
 
                # Copy original .[ch] files to compile_dir
                for src in compiler.files_to_copy do
-                       var basename = src.basename("")
+                       var basename = src.basename
                        var dst = "{compile_dir}/{basename}"
                        src.file_copy_to dst
                end
@@ -389,6 +389,15 @@ endif
                        makefile.write("CFLAGS += -D NO_STACKTRACE\n\n")
                end
 
+               makefile.write """
+# Special configuration for Darwin
+ifeq ($(uname_S),Darwin)
+       # Remove POSIX flag -lrt
+       LDLIBS := $(filter-out -lrt,$(LDLIBS))
+endif
+
+"""
+
                makefile.write("all: {outpath}\n")
                if outpath != real_outpath then
                        makefile.write("\tcp -- {outpath.escape_to_sh} {real_outpath.escape_to_sh.replace("$","$$")}")
@@ -444,7 +453,7 @@ endif
                # Compile each required extern body into a specific .o
                for f in compiler.extern_bodies do
                        var o = f.makefile_rule_name
-                       var ff = f.filename.basename("")
+                       var ff = f.filename.basename
                        makefile.write("{o}: {ff}\n")
                        makefile.write("\t{f.makefile_rule_content}\n\n")
                        dep_rules.add(f.makefile_rule_name)
@@ -842,7 +851,7 @@ extern void nitni_global_ref_decr( struct nitni_ref *ref );
                v.add("signal(SIGINT, sig_handler);")
                v.add("signal(SIGTERM, sig_handler);")
                v.add("signal(SIGSEGV, sig_handler);")
-               v.add("signal(SIGPIPE, sig_handler);")
+               v.add("signal(SIGPIPE, SIG_IGN);")
 
                v.add("glob_argc = argc; glob_argv = argv;")
                v.add("initialize_gc_option();")
@@ -1494,8 +1503,12 @@ abstract class AbstractCompilerVisitor
        fun char_instance(value: Char): RuntimeVariable
        do
                var t = mmodule.char_type
-               var res = new RuntimeVariable("'{value.to_s.escape_to_c}'", t, t)
-               return res
+
+               if value.ascii < 128 then
+                       return new RuntimeVariable("'{value.to_s.escape_to_c}'", t, t)
+               else
+                       return new RuntimeVariable("{value.ascii}", t, t)
+               end
        end
 
        # Generate a float value
@@ -1537,7 +1550,7 @@ abstract class AbstractCompilerVisitor
                var native_mtype = mmodule.native_string_type
                var nat = self.new_var(native_mtype)
                self.add("{nat} = \"{string.escape_to_c}\";")
-               var length = self.int_instance(string.length)
+               var length = self.int_instance(string.bytelen)
                self.add("{res} = {self.send(self.get_property("to_s_with_length", native_mtype), [nat, length]).as(not null)};")
                self.add("{name} = {res};")
                self.add("\}")
@@ -1598,12 +1611,12 @@ abstract class AbstractCompilerVisitor
                file = file.strip_extension(".nit")
                var tryfile = file + ".nit.h"
                if tryfile.file_exists then
-                       self.declare_once("#include \"{tryfile.basename("")}\"")
+                       self.declare_once("#include \"{tryfile.basename}\"")
                        self.compiler.files_to_copy.add(tryfile)
                end
                tryfile = file + "_nit.h"
                if tryfile.file_exists then
-                       self.declare_once("#include \"{tryfile.basename("")}\"")
+                       self.declare_once("#include \"{tryfile.basename}\"")
                        self.compiler.files_to_copy.add(tryfile)
                end
 
@@ -1614,7 +1627,7 @@ abstract class AbstractCompilerVisitor
                        tryfile = file + "_nit.c"
                        if not tryfile.file_exists then return
                end
-               var f = new ExternCFile(tryfile.basename(""), "")
+               var f = new ExternCFile(tryfile.basename, "")
                self.compiler.extern_bodies.add(f)
                self.compiler.files_to_copy.add(tryfile)
        end
@@ -1696,18 +1709,27 @@ abstract class AbstractCompilerVisitor
        # `mtype` is the expected return type, pass null if no specific type is expected.
        fun expr(nexpr: AExpr, mtype: nullable MType): RuntimeVariable
        do
-               if nexpr.mtype == null then
+               var old = self.current_node
+               self.current_node = nexpr
+
+               var res = null
+               if nexpr.mtype != null then
+                       res = nexpr.expr(self)
+               end
+
+               if res == null then
                        # Untyped expression.
                        # Might mean dead code or invalid code.
                        # so aborts
                        add_abort("FATAL: bad expression executed.")
                        # and return a placebo result to please the C compiler
                        if mtype == null then mtype = compiler.mainmodule.object_type
-                       return new_var(mtype)
+                       res = new_var(mtype)
+
+                       self.current_node = old
+                       return res
                end
-               var old = self.current_node
-               self.current_node = nexpr
-               var res = nexpr.expr(self).as(not null)
+
                if mtype != null then
                        mtype = self.anchor(mtype)
                        res = self.autobox(res, mtype)
@@ -2148,10 +2170,7 @@ redef class AMethPropdef
                                return true
                        end
                else if cname == "Char" then
-                       if pname == "output" then
-                               v.add("printf(\"%c\", ((unsigned char){arguments.first}));")
-                               return true
-                       else if pname == "object_id" then
+                       if pname == "object_id" then
                                v.ret(v.new_expr("(long){arguments.first}", ret.as(not null)))
                                return true
                        else if pname == "successor" then
@@ -2323,7 +2342,7 @@ redef class AMethPropdef
                        end
                else if cname == "NativeString" then
                        if pname == "[]" then
-                               v.ret(v.new_expr("(uint32_t)(unsigned char){arguments[0]}[{arguments[1]}]", ret.as(not null)))
+                               v.ret(v.new_expr("(unsigned char)((int){arguments[0]}[{arguments[1]}])", ret.as(not null)))
                                return true
                        else if pname == "[]=" then
                                v.add("{arguments[0]}[{arguments[1]}]=(unsigned char){arguments[2]};")
@@ -2892,12 +2911,13 @@ redef class AOrElseExpr
        end
 end
 
-redef class AIntExpr
-       redef fun expr(v) do return v.int_instance(self.value.as(not null))
-end
-
-redef class AByteExpr
-       redef fun expr(v) do return v.byte_instance(self.value.as(not null))
+redef class AIntegerExpr
+       redef fun expr(v) do
+               if value isa Int then return v.int_instance(value.as(Int))
+               if value isa Byte then return v.byte_instance(value.as(Byte))
+               # Should never happen
+               abort
+       end
 end
 
 redef class AFloatExpr
@@ -3035,7 +3055,9 @@ redef class AIsaExpr
        redef fun expr(v)
        do
                var i = v.expr(self.n_expr, null)
-               return v.type_test(i, self.cast_type.as(not null), "isa")
+               var cast_type = self.cast_type
+               if cast_type == null then return null # no-no on broken node
+               return v.type_test(i, cast_type, "isa")
        end
 end