Exemple #1
0
        private static int GetExceptionVariable(TranslationContext context, Block b)
        {
            if (b.Code == null)
            {
                return(-1);
            }
            int entryIndex = b.EntryIndex;

            if (entryIndex < 0)
            {
                return(-1);
            }
            int exitIndex = b.ExitIndex;

            int n = 0;

            for (int i = entryIndex; i <= exitIndex; ++i)
            {
                var instr = context.Body.Code[i];
                int pop   = CIL.GetPopCount(context.Method, instr);
                if (pop == 1 && n == 0)
                {
                    switch (instr.Code)
                    {
                    case InstructionCode.Stloc:
                    case InstructionCode.Stloc_S:
                        return((int)instr.Value);

                    case InstructionCode.Stloc_0: return(0);

                    case InstructionCode.Stloc_1: return(1);

                    case InstructionCode.Stloc_2: return(2);

                    case InstructionCode.Stloc_3: return(3);
                    }
                    break;
                }
                n -= pop;
                int push = CIL.GetPushCount(instr);
                n += push;
            }

            return(-1);
        }
        /// <summary>
        /// Converts the specified opcode.
        /// </summary>
        /// <param name="opcode">The opcode.</param>
        /// <returns></returns>
        public static IR.ConditionCode ConvertCondition(CIL.OpCode opcode)
        {
            switch (opcode)
            {
                // Signed
                case CIL.OpCode.Beq_s: return IR.ConditionCode.Equal;
                case CIL.OpCode.Bge_s: return IR.ConditionCode.GreaterOrEqual;
                case CIL.OpCode.Bgt_s: return IR.ConditionCode.GreaterThan;
                case CIL.OpCode.Ble_s: return IR.ConditionCode.LessOrEqual;
                case CIL.OpCode.Blt_s: return IR.ConditionCode.LessThan;
                // Unsigned
                case CIL.OpCode.Bne_un_s: return IR.ConditionCode.NotEqual;
                case CIL.OpCode.Bge_un_s: return IR.ConditionCode.UnsignedGreaterOrEqual;
                case CIL.OpCode.Bgt_un_s: return IR.ConditionCode.UnsignedGreaterThan;
                case CIL.OpCode.Ble_un_s: return IR.ConditionCode.UnsignedLessOrEqual;
                case CIL.OpCode.Blt_un_s: return IR.ConditionCode.UnsignedLessThan;
                // Long form signed
                case CIL.OpCode.Beq: goto case CIL.OpCode.Beq_s;
                case CIL.OpCode.Bge: goto case CIL.OpCode.Bge_s;
                case CIL.OpCode.Bgt: goto case CIL.OpCode.Bgt_s;
                case CIL.OpCode.Ble: goto case CIL.OpCode.Ble_s;
                case CIL.OpCode.Blt: goto case CIL.OpCode.Blt_s;
                // Long form unsigned
                case CIL.OpCode.Bne_un: goto case CIL.OpCode.Bne_un_s;
                case CIL.OpCode.Bge_un: goto case CIL.OpCode.Bge_un_s;
                case CIL.OpCode.Bgt_un: goto case CIL.OpCode.Bgt_un_s;
                case CIL.OpCode.Ble_un: goto case CIL.OpCode.Ble_un_s;
                case CIL.OpCode.Blt_un: goto case CIL.OpCode.Blt_un_s;
                // Compare
                case CIL.OpCode.Ceq: return IR.ConditionCode.Equal;
                case CIL.OpCode.Cgt: return IR.ConditionCode.GreaterThan;
                case CIL.OpCode.Cgt_un: return IR.ConditionCode.UnsignedGreaterThan;
                case CIL.OpCode.Clt: return IR.ConditionCode.LessThan;
                case CIL.OpCode.Clt_un: return IR.ConditionCode.UnsignedLessThan;

                default: throw new NotImplementedException();
            }
        }