Beispiel #1
0
        public void Allocate(BasicBlock <IRInstrList> block)
        {
            var blockLiveness = liveness[block];
            var instrLiveness = LivenessAnalysis.ComputeLiveness(block, blockLiveness);
            var pool          = RegisterPool.Create(baseOffset, globalVars);

            for (var i = 0; i < block.Content.Count; i++)
            {
                var instr = block.Content[i];
                pool.CheckLiveness(instrLiveness[instr]);

                // Allocates
                if (instr.Operand1 != null)
                {
                    instr.Operand1 = AllocateOperand(instr.Operand1, pool);
                }
                if (instr.Operand2 != null)
                {
                    instr.Operand2 = AllocateOperand(instr.Operand2, pool);
                }
            }
            if (pool.SpillOffset - 1 > LocalSize)
            {
                LocalSize = pool.SpillOffset - 1;
            }
            baseOffset = pool.SpillOffset;
        }
Beispiel #2
0
        private IIROperand AllocateOperand(IIROperand operand, RegisterPool pool)
        {
            if (operand is IRVariable)
            {
                var variable = (IRVariable)operand;

                StackSlot?slot;
                var       reg = AllocateVariable(pool, variable, out slot);
                if (reg != null)
                {
                    return new IRRegister(reg.Value)
                           {
                               SourceVariable = variable,
                               Type           = variable.Type
                           }
                }
                ;
                variable.Annotation = slot.Value;
                return(new IRPointer
                {
                    Register = IRRegister.BP,
                    Offset = slot.Value.Offset,
                    SourceVariable = variable,
                    Type = variable.Type
                });
            }
            return(operand);
        }
Beispiel #3
0
            public static RegisterPool Create(int baseOffset, Dictionary <IRVariable, StackSlot> globalVars)
            {
                var pool = new RegisterPool();

                pool.regAlloc    = new IRVariable[NumRegisters];
                pool.spillVars   = new Dictionary <IRVariable, StackSlot>(globalVars);
                pool.SpillOffset = baseOffset;
                return(pool);
            }
Beispiel #4
0
        IIROperand AllocateOperand(IIROperand operand, RegisterPool pool)
        {
            if (operand is IRVariable)
            {
                var variable = (IRVariable)operand;

                StackSlot?slot;
                var       reg = AllocateVariable(pool, variable, out slot);
                if (reg != null)
                {
                    return new IRRegister(reg.Value)
                           {
                               SourceVariable = variable,
                               Type           = variable.Type
                           }
                }
                ;
                variable.Annotation = slot.Value;
                return(new IRPointer {
                    Register = IRRegister.BP,
                    Offset = slot.Value.Offset,
                    SourceVariable = variable,
                    Type = variable.Type
                });
            }
            return(operand);
        }

        VMRegisters?AllocateVariable(RegisterPool pool, IRVariable var, out StackSlot?stackSlot)
        {
            stackSlot = pool.CheckSpill(var);
            if (stackSlot == null)
            {
                var allocReg = var.Annotation == null ? (VMRegisters?)null : (VMRegisters)var.Annotation;
                if (allocReg == null)
                {
                    allocReg = pool.Allocate(var);
                }
                if (allocReg != null)
                {
                    if (var.Annotation == null)
                    {
                        var.Annotation = allocReg.Value;
                    }
                    return(allocReg);
                }
                // Spill variable
                stackSlot = pool.SpillVariable(var);
            }
            return(null);
        }
    }
Beispiel #5
0
 private VMRegisters?AllocateVariable(RegisterPool pool, IRVariable var, out StackSlot?stackSlot)
 {
     stackSlot = pool.CheckSpill(var);
     if (stackSlot == null)
     {
         var allocReg = var.Annotation == null ? (VMRegisters?)null : (VMRegisters)var.Annotation;
         if (allocReg == null)
         {
             allocReg = pool.Allocate(var);
         }
         if (allocReg != null)
         {
             if (var.Annotation == null)
             {
                 var.Annotation = allocReg.Value;
             }
             return(allocReg);
         }
         // Spill variable
         stackSlot = pool.SpillVariable(var);
     }
     return(null);
 }