Exemple #1
0
        // Accepts 0x[hex] or [dec] format
        private uint EncodeImmediate(string strImmed, int length, uint mask)
        {
            /*
             * if ((strImmed.StartsWith("0x")) || (strImmed.StartsWith("-0x")))
             *  return ASMValueHelper.UnsignedToBinary_WithLength(ASMValueHelper.HexToUnsigned_AnySign(strImmed, length), length);
             *          else
             *  return ASMValueHelper.UnsignedToBinaryAny_WithLength(Convert.ToInt32(strImmed), length);
             */

            /*
             * if ((strImmed.StartsWith("0x")) || (strImmed.StartsWith("-0x")))
             *  return ASMValueHelper.HexToUnsigned_AnySign(strImmed, length) & mask;
             * else
             *  return ASMValueHelper.SignedToUnsigned(Convert.ToInt32(strImmed)) & mask;
             */

            return(ValueHelper.GetAnyUnsignedValue(strImmed) & mask);
        }
Exemple #2
0
        // Translates a single pseudoinstruction
        public EncodeLine[] TranslatePseudoSingle(EncodingFormat encoding, string[] parts, int index, uint pc, bool skipLabelAssertion = false)
        {
            List <EncodeLine> result = new List <EncodeLine>();

            int    startParenIndex = 0;
            int    endParenIndex   = 0;
            string strImmed        = "";

            string[] newParts;
            uint     ivalue = 0;
            //long lvalue = 0;
            ushort ushortval = 0;
            //short shortval = 0;
            bool   useNegativeOffset = false;
            string regS              = "";
            int    argIndex0         = 0;
            int    argIndex1         = 0;
            bool   doesBranchIfEqual = false;
            bool   isStore           = false;
            bool   useAT             = false;

            // Find args
            string strArgs = "";

            string[] args = null;
            if (!string.IsNullOrEmpty(parts[1]))
            {
                strArgs = ASMStringHelper.RemoveSpaces(parts[1]);
                args    = strArgs.Split(',');
            }

            switch (encoding.Command)
            {
            case "jalr":
                if (args.Length == 1)
                {
                    //parts[1] = args[0] + ",ra";
                    parts[1] = "ra," + args[0];
                }
                result.Add(new EncodeLine(parts, index));
                break;

            case "mul":
            case "div":
            case "rem":
            case "mod":
                if ((encoding.Command == "div") && (args.Length < 3))
                {
                    result.Add(new EncodeLine(parts, index));
                    break;
                }

                newParts    = new string[2];
                newParts[0] = (encoding.Command == "mul") ? "mult" : "div";
                newParts[1] = args[1] + "," + args[2];
                result.Add(new EncodeLine(newParts, index));

                parts[0] = ((encoding.Command == "mul") || (encoding.Command == "div")) ? "mflo" : "mfhi";
                parts[1] = args[0];
                result.Add(new EncodeLine(parts, index));

                break;

            case "add":
            case "addu":
            case "and":
            case "min":
            case "max":
            case "nor":
            case "or":
            case "rotrv":
            case "sllv":
            case "slt":
            case "sltu":
            case "srav":
            case "srlv":
            case "sub":
            case "subu":
            case "xor":

                if (args.Length == 2)
                {
                    parts[1] = args[0] + "," + parts[1];
                }

                result.Add(new EncodeLine(parts, index));
                break;


            case "addi":
            case "addiu":
            case "andi":
            case "ori":
            case "rotr":
            case "sll":
            case "slti":
            case "sltiu":
            case "sra":
            case "srl":
            case "xori":

                if (args.Length == 2)
                {
                    parts[1] = args[0] + "," + parts[1];
                }

                result.Add(new EncodeLine(parts, index));
                break;

            case "bgt":
            case "blt":
            case "bge":
            case "ble":
                argIndex0         = ((encoding.Command == "bgt") || (encoding.Command == "ble")) ? 1 : 0;
                argIndex1         = (argIndex0 > 0) ? 0 : 1;
                doesBranchIfEqual = ((encoding.Command == "bge") || (encoding.Command == "ble"));

                newParts    = new string[2];
                newParts[0] = "slt";
                newParts[1] = "at," + args[argIndex0] + "," + args[argIndex1];
                result.Add(new EncodeLine(newParts, index));

                parts[0] = doesBranchIfEqual ? "beq" : "bne";
                parts[1] = "at,zero," + args[2];
                result.Add(new EncodeLine(parts, index));

                break;

            case "lbu":
            case "lb":
            case "lhu":
            case "lh":
            case "lw":
            case "sb":
            case "sh":
            case "sw":
                bool isHiLo = ((args[1].ToLower().StartsWith("%hi")) || (args[1].ToLower().StartsWith("%lo")));

                startParenIndex = args[1].IndexOf('(', (isHiLo ? 4 : 0));                         // check -1
                endParenIndex   = args[1].IndexOf(')', (startParenIndex >= 0) ? startParenIndex : 0);
                isStore         = encoding.Command.ToLower().StartsWith("s");
                useAT           = ((startParenIndex >= 0) || (isStore));

                if (startParenIndex >= 0)
                {
                    regS     = args[1].Substring(startParenIndex + 1, endParenIndex - startParenIndex - 1);
                    strImmed = args[1].Substring(0, startParenIndex);
                }
                else
                {
                    strImmed = args[1];
                }

                ivalue = ValueHelper.GetAnyUnsignedValue(strImmed, skipLabelAssertion);

                bool isLabel = !(
                    ((strImmed.StartsWith("0x")) || (strImmed.StartsWith("-0x"))) ||
                    (ASMStringHelper.StringIsNumeric(strImmed)) ||
                    ((strImmed.StartsWith("-")) && (strImmed.Length > 1)) ||
                    (isHiLo)
                    );

                if (((ivalue > 0x7fff) && (strImmed[0] != '-') && (!isHiLo)) || isLabel)
                {
                    ushortval = (ushort)(ivalue & 0xffff);
                    if (ushortval >= 0x8000)
                    {
                        useNegativeOffset = true;
                        ushortval         = (ushort)(0x10000 - ushortval);
                    }

                    newParts    = new string[2];
                    newParts[0] = "lui";
                    newParts[1] = (useAT ? "at" : args[0]) + "," + ((ivalue >> 16) + (useNegativeOffset ? 1 : 0));
                    result.Add(new EncodeLine(newParts, index));

                    if (startParenIndex >= 0)
                    {
                        newParts    = new string[2];
                        newParts[0] = "addu";
                        newParts[1] = "at,at," + regS;
                        result.Add(new EncodeLine(newParts, index));
                    }

                    parts[1] = args[0] + "," + (useNegativeOffset ? "-" : "") + ushortval + "(" + (useAT ? "at" : args[0]) + ")";
                    result.Add(new EncodeLine(parts, index));
                }
                else
                {
                    result.Add(new EncodeLine(parts, index));
                }

                break;

            // "la" always translates to two instructions, while "li" can translate to one.
            case "la":
            case "li":
                //ivalue = ValueHelper.GetAnyUnsignedValue(args[1]);

                /*
                 * try
                 * {
                 *  ivalue = ValueHelper.GetAnyUnsignedValue(args[1]);
                 *  //ivalue = ValueHelper.FindUnsignedValue(args[1]);
                 * }
                 * catch
                 * {
                 *  ivalue = 0;
                 * }
                 */

                ivalue = ValueHelper.GetAnyUnsignedValue(args[1], skipLabelAssertion);

                bool isLA = encoding.Command.Equals("la");

                if (ivalue >= 0xffff8000)
                {
                    parts[0] = "addiu";
                    parts[1] = args[0] + ",zero," + ((ushort)(ivalue & 0xffff));
                    result.Add(new EncodeLine(parts, index));
                }
                else if ((ivalue > 0xffff) || isLA)
                {
                    newParts    = new string[2];
                    newParts[0] = "lui";
                    newParts[1] = args[0] + "," + (ivalue >> 16);
                    result.Add(new EncodeLine(newParts, index));

                    ushortval = (ushort)(ivalue & 0xffff);
                    if ((ushortval > 0) || (isLA))
                    {
                        parts[0] = "ori";
                        parts[1] = args[0] + "," + args[0] + "," + ushortval;
                        result.Add(new EncodeLine(parts, index));
                    }
                }
                else
                {
                    if (!((args[1].StartsWith("0x") || ASMStringHelper.StringIsNumeric(args[1]))))
                    {
                        parts[1] = args[0] + "," + ValueHelper.LabelHelper.LabelToUnsigned(args[1], skipLabelAssertion);
                    }

                    result.Add(new EncodeLine(parts, index));
                }

                break;

            default:
                result.Add(new EncodeLine(parts, index));
                break;
            }

            return(result.ToArray());
        }