예제 #1
0
 public static bool isRegister(string keyword)
 {
     return(RegisterTools.parseRn(keyword) != Rn.NOREG);
 }
예제 #2
0
        /// <summary>
        /// return Offset = Base + (Index * Scale) + Displacement
        /// </summary>
        public static Tuple <bool, Rn, Rn, int, long, int> parseMemOperand(string token)
        {
            int length = token.Length;

            if (length < 3)
            {
                return(new Tuple <bool, Rn, Rn, int, long, int>(false, Rn.NOREG, Rn.NOREG, 0, 0, 0));
            }

            // 1] select everything between []
            int beginPos = length;

            for (int i = 0; i < length; ++i)
            {
                if (token[i] == '[')
                {
                    beginPos = i + 1;
                }
            }

            int nBits = getNbitsMemOperand(token);

            int endPos = length;

            for (int i = beginPos; i < length; ++i)
            {
                if (token[i] == ']')
                {
                    endPos = i;
                }
            }

            token  = token.Substring(beginPos, endPos - beginPos).Trim();
            length = token.Length;
            if (length == 0)
            {
                return(new Tuple <bool, Rn, Rn, int, long, int>(false, Rn.NOREG, Rn.NOREG, 0, 0, 0));
            }

            // 2] check if the displacement is negative
            bool negativeDisplacement = token.Contains('-');

            if (negativeDisplacement)
            {
                token = token.Replace('-', '+');
            }

            // 3] remove superfluous initial +
            if (token[0] == '+')
            {
                token = token.Substring(1, length - 1).Trim();
            }

            // 4] split based on +
            string[] x = token.Split('+');

            Rn   baseRn       = Rn.NOREG;
            Rn   indexRn      = Rn.NOREG;
            int  scale        = 0;
            long displacement = 0;

            bool foundDisplacement = false;


            for (int i = 0; i < x.Length; ++i)
            {
                string y = x[i].Trim();

                var t2 = AsmSourceTools.toConstant(y);
                if (t2.Item1)
                {
                    if (foundDisplacement)
                    {
                        // found an second displacement, error
                        return(new Tuple <bool, Rn, Rn, int, long, int>(false, Rn.NOREG, Rn.NOREG, 0, 0, 0));
                    }
                    else
                    {
                        foundDisplacement = true;
                        displacement      = (negativeDisplacement) ? -(long)t2.Item2 : (long)t2.Item2;
                    }
                }
                else
                {
                    Rn t1 = RegisterTools.parseRn(y);
                    if (t1 != Rn.NOREG)
                    {
                        if (baseRn == Rn.NOREG)
                        {
                            baseRn = t1;
                        }
                        else
                        {
                            indexRn = t1;
                            scale   = 1;
                        }
                    }

                    if (y.Contains('*'))
                    {
                        string[] z   = y.Split('*');
                        string   z0  = z[0].Trim();
                        string   z1  = z[1].Trim();
                        Rn       z0r = RegisterTools.parseRn(z0);
                        if (z0r != Rn.NOREG)
                        {
                            indexRn = z0r;
                            scale   = parseScale(z1);
                        }
                        else
                        {
                            Rn z1r = RegisterTools.parseRn(z1);
                            if (z1r != Rn.NOREG)
                            {
                                indexRn = z1r;
                                scale   = parseScale(z0);
                            }
                        }
                    }
                }
            }

            if (scale == -1)
            {
                return(new Tuple <bool, Rn, Rn, int, long, int>(false, Rn.NOREG, Rn.NOREG, 0, 0, 0));
            }
            if ((baseRn != Rn.NOREG) && (indexRn != Rn.NOREG))
            {
                if (RegisterTools.nBits(baseRn) != RegisterTools.nBits(indexRn))
                {
                    return(new Tuple <bool, Rn, Rn, int, long, int>(false, Rn.NOREG, Rn.NOREG, 0, 0, 0));
                }
            }
            return(new Tuple <bool, Rn, Rn, int, long, int>(true, baseRn, indexRn, scale, displacement, nBits));
        }