compile: traces know REG variables
[nit.git] / src / compiling / compiling_icode.nit
index 5f44ca1..e41e277 100644 (file)
@@ -46,7 +46,7 @@ class I2CCompilerVisitor
                        else
                                var s: String
                                var strs: HashMap[Int, String]
-                               if e.in_bool_slots then
+                               if e.in_tag_slots then
                                        strs = once new HashMap[Int, String]
                                        if not strs.has_key(i) then strs[i] = "REGB{i}"
                                else if closure and not e.is_local then
@@ -264,17 +264,22 @@ redef class IRoutine
                if location != null then
                        ll = location.line_start
                end
-               v.add_decl("struct trace_t trace = \{NULL, NULL, {ll}, LOCATE_{v.basecname}\};")
+               v.add_decl("struct trace_t trace = \{NULL, NULL, {ll}, LOCATE_{v.basecname}, {std_slots_nb}\};")
                v.add_instr("trace.prev = tracehead; tracehead = &trace;")
                v.add_instr("trace.file = LOCATE_{v.visitor.module.name};")
+               v.add_instr("trace.REG_pointer = (val_t **)®")
 
                # Add local variables
                if std_slots_nb == 0 then
                        v.add_decl("val_t *REG = NULL;")
                else
-                       v.add_decl("val_t REG[{std_slots_nb}];")
+                       var init_vals = new Buffer
+                       init_vals.append "val_t REG[{std_slots_nb}] = \{ NIT_NULL"
+                       for i in [1..std_slots_nb[ do init_vals.append(", NIT_NULL")
+                       init_vals.append " \};"
+                       v.add_decl(init_vals.to_s)
                end
-               for i in [0..bool_slots_nb[ do
+               for i in [0..tag_slots_nb[ do
                        v.add_decl("val_t REGB{i};")
                end
                var iclosdecls = closure_decls
@@ -283,12 +288,11 @@ redef class IRoutine
                else
                        v.add_decl("struct WBT_ **CREG = NULL;")
                end
-               var i = 0
+               var k = 0
                for r in params do
-                       if r.slot_index != null then v.add_assignment(v.register(r), args[i])
-                       i += 1
+                       if r.slot_index != null then v.add_assignment(v.register(r), args[k])
+                       k += 1
                end
-               var iclosdecls = closure_decls
                if iclosdecls != null then
                        for i in [0..iclosdecls.length[ do
                                var iclosdecl = iclosdecls[i]
@@ -330,7 +334,7 @@ redef class IRoutine
        # cv must be in the correct function
        fun compile_to_c(cv: CompilerVisitor, cname: String, args: Array[String]): nullable String
        do
-               optimize
+               optimize(cv.module)
                var v = new I2CCompilerVisitor(cv, self, cname)
                return compile_inside_to_c(v, args)
        end
@@ -358,7 +362,9 @@ redef class ICode
                if r != null and r.slot_index != null then
                        assert s != null
                        v.add_assignment(v.register(r), s)
-               else if s != null and not self isa IMove then
+               else if s != null and not is_pure then
+                       # ICode with side effects must be evaluated
+                       # even if the result is not wanted
                        v.add_instr(s + ";")
                end
        end
@@ -573,16 +579,11 @@ redef class IAbort
                v.add_instr(s.to_s)
 
                var ll = location
-               var pl = property_location
-               s = new Buffer.from("fprintf(stderr, \"")
-               if pl != null then s.append(" in %s")
-               s.append(" (%s")
+               s = new Buffer.from("fprintf(stderr, \" (%s")
                if ll != null then
                        s.append(":%d")
                end
-               s.append(")\\n\", ")
-               if pl != null then s.append("LOCATE_{pl.cname}, ")
-               s.append("LOCATE_{module_location.name}")
+               s.append(")\\n\", LOCATE_{module_location.name}")
                if ll != null then
                        s.append(", {ll.line_start}")
                end