icode: use the icb to helps the icode duplication
[nit.git] / src / icode / icode_tools.nit
index 2d059e2..3f6c91e 100644 (file)
@@ -15,7 +15,7 @@
 # limitations under the License.
 
 # Tools to manipulate intermediace nit code representation
-import icode_base
+import icode_builder
 
 # A simple visitor to visit icode structures
 class ICodeVisitor
@@ -91,44 +91,54 @@ class ICodeVisitor
        end
 end
 
-redef class IRoutine
-       # Inline an iroutine in an icode sequence
-       fun inline_in_seq(seq: ISeq, args: Sequence[IRegister]): nullable IRegister
+redef class ICodeBuilder
+       # Inline an iroutine in the current icode sequence
+       fun inline_routine(routine: IRoutine, args: Sequence[IRegister]): nullable IRegister
        do
-               var d = new ICodeDupContext
-               if args.length != params.length then print "{args.length} != {params.length}"
-               assert args.length == params.length
+               var d = new ICodeDupContext(self)
+               assert args.length == routine.params.length
+
+               # Fill register duplicate association
+               var dico = d._registers
+               var res = routine.result
+               if res != null then
+                       var res2 = new_register(res.stype)
+                       dico[res] = res2
+                       res = res2
+               end
+               for reg in routine.registers do
+                       assert not dico.has_key(reg)
+                       dico[reg] = new_register(reg.stype)
+               end
                for i in [0..args.length[ do
                        # FIXME The following assumes that params are readonly.
                        # The alternative is safe but add one move :/
-                       d._registers[params[i]] = args[i]
-                       #seq.icodes.add(new IMove(d.dup_ireg(params[i]), args[i]))
+                       dico[routine.params[i]] = args[i]
+                       #seq.icodes.add(new IMove(dico[routine.params[i]]), args[i]))
                end
-               seq.icodes.add(body.dup_with(d))
-               var r = result
-               if r != null then r = d.dup_ireg(r)
-               return r
+
+               # Process inlining
+               routine.body.dup_with(d)
+               return res
        end
 end
 
 # This class stores reference to allow correct duplication of icodes
 private class ICodeDupContext
-       # Duplicate one register
-       # Subsequent invocation will return the same register
+       # Return the correct register
+       # * a duplicate of the local register 'r' of the inlined iroutine
+       # * 'r' else (it is a register of the caller iroutine)
        fun dup_ireg(r: IRegister): IRegister
        do
                var rs = _registers
                if rs.has_key(r) then
                        return rs[r]
                else
-                       var r2 = new IRegister(r.stype)
-                       rs[r] = r2
-                       return r2
+                       return r
                end
        end
 
-       # Duplicate a bunch of registers
-       # Subsequent invocation will return the same registers
+       # Return a correct bunch of registers
        fun dup_iregs(regs: Sequence[IRegister]): Sequence[IRegister]
        do
                var a = new Array[IRegister].with_capacity(regs.length)
@@ -145,20 +155,28 @@ private class ICodeDupContext
        # The assocation between old_ireg and new_ireg
        # Used by dup_ireg
        var _registers: Map[IRegister, IRegister] = new HashMap[IRegister, IRegister]
+
+       # The current code builder
+       var _icb: ICodeBuilder
+
+       init(icb: ICodeBuilder)
+       do
+               _icb = icb
+       end
 end
 
 redef class ICode
-       # Duplicate the current icode (generic part)
-       private fun dup_with(d: ICodeDupContext): ICode
+       # Duplicate the current icode in the icode builder of the ICodeDupContext
+       private fun dup_with(d: ICodeDupContext)
        do
                var c = inner_dup_with(d)
                var r = result
                if r != null then c.result = d.dup_ireg(r)
                c.location = location
-               return c
+               d._icb.seq.icodes.add(c)
        end
 
-       # Duplicate the current icode (specific part)
+       # Simle partial duplication of the current icode
        private fun inner_dup_with(d: ICodeDupContext): ICode is abstract
 end
 
@@ -174,10 +192,13 @@ redef class ISeq
        # Note: dest must be empty and not modified afted duplication or IEscapes may be wrongly duplicated
        private fun dup_seq_to(d: ICodeDupContext, dest: ISeq)
        do
+               var old_seq = d._icb.seq
+               d._icb.seq = dest
                d._seqs[self] = dest
                for c in icodes do
-                       dest.icodes.add(c.dup_with(d))
+                       c.dup_with(d)
                end
+               d._icb.seq = old_seq
        end
 end
 
@@ -216,7 +237,7 @@ end
 redef class IAbort
        redef fun inner_dup_with(d)
        do
-               return new IAbort(texts, property_location, module_location)
+               return new IAbort(texts, module_location)
        end
 end