private void VisitInstr(ILInstrList instrs, ILInstruction instr, ref int index, ILPostTransformer tr)
        {
            if (instr.OpCode != ILOpCode.__BEGINCALL && instr.OpCode != ILOpCode.__ENDCALL)
            {
                return;
            }

            var callInfo = (InstrCallInfo)instr.Annotation;

            if (callInfo.IsECall)
            {
                instrs.RemoveAt(index);
                index--;
                return;
            }

            var saving = new HashSet <DarksVMRegisters>(saveRegs);
            var retVar = (IRVariable)callInfo.ReturnValue;

            // R0 = return register, need to save if retVar register is not R0
            //Debug.Assert(!(retVar == null ^ (callInfo.ReturnRegister == null ^ callInfo.ReturnSlot == null)));
            if (retVar != null)
            {
                if (callInfo.ReturnSlot == null)
                {
                    var retReg = callInfo.ReturnRegister.Register;
                    saving.Remove(retReg);
                    if (retReg != DarksVMRegisters.R0)
                    {
                        saving.Add(DarksVMRegisters.R0);
                    }
                }
                else
                {
                    saving.Add(DarksVMRegisters.R0);
                }
            }
            else
            {
                saving.Add(DarksVMRegisters.R0);
            }

            if (instr.OpCode == ILOpCode.__BEGINCALL)
            {
                instrs.Replace(index, saving
                               .Select(reg => new ILInstruction(ILOpCode.PUSHR_OBJECT, ILRegister.LookupRegister(reg), instr)));
            }
            else
            {
                instrs.Replace(index, saving
                               .Select(reg => new ILInstruction(ILOpCode.POP, ILRegister.LookupRegister(reg), instr))
                               .Reverse());
            }
            index--;
        }
示例#2
0
 private void VisitInstr(ILInstrList instrs, ILInstruction instr, ref int index, ILTransformer tr)
 {
     if (instr.OpCode == ILOpCode.__ENTRY)
     {
         instrs.RemoveAt(index);
         index--;
     }
     else if (instr.OpCode == ILOpCode.__EXIT)
     {
         instrs[index] = new ILInstruction(ILOpCode.RET, null, instr);
     }
 }
        void VisitInstr(ILInstrList instrs, ILInstruction instr, ref int index, ILPostTransformer tr)
        {
            if (instr.OpCode != ILOpCode.__BEGINCALL && instr.OpCode != ILOpCode.__ENDCALL)
            {
                return;
            }

            var callInfo = (InstrCallInfo)instr.Annotation;

            if (callInfo.IsECall)
            {
                instrs.RemoveAt(index);
                index--;
                return;
            }

            var saving = new HashSet <VMRegisters>(saveRegs);
            var retVar = (IRVariable)callInfo.ReturnValue;

            // R0 = return register, need to save if retVar register is not R07
            if (retVar == null ^ (callInfo.ReturnRegister == null ^ callInfo.ReturnSlot == null))
            {
                ConsoleColor c = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("Debug.Assert [SaveRegistersTransform.cs] L36");
                Console.WriteLine($"Method target->{callInfo.Method.FullName}");
                Console.ForegroundColor = c;
            }
            if (retVar != null)
            {
                if (callInfo.ReturnSlot == null)
                {
                    var retReg = callInfo.ReturnRegister.Register;
                    saving.Remove(retReg);
                    if (retReg != VMRegisters.R0)
                    {
                        saving.Add(VMRegisters.R0);
                    }
                }
                else
                {
                    saving.Add(VMRegisters.R0);
                }
            }
            else
            {
                saving.Add(VMRegisters.R0);
            }

            if (instr.OpCode == ILOpCode.__BEGINCALL)
            {
                instrs.Replace(index, saving
                               .Select(reg => new ILInstruction(ILOpCode.PUSHR_OBJECT, ILRegister.LookupRegister(reg), instr)));
            }
            else
            {
                instrs.Replace(index, saving
                               .Select(reg => new ILInstruction(ILOpCode.POP, ILRegister.LookupRegister(reg), instr))
                               .Reverse());
            }
            index--;
        }