Esempio n. 1
0
        /*
         *      public int LabelToUnsigned(string label)
         *      {
         *              string newLabel = ASMStringHelper.RemoveSpaces(label).ToUpper();
         *              ASMDebugHelper.assert(LabelDict.ContainsKey(newLabel), "Label not found: " + label);
         *              return LabelDict[newLabel];
         *      }
         */

        public uint GetAnyUnsignedValue(string val, bool skipLabelAssertion = false)
        {
            if ((val.StartsWith("0x")) || (val.StartsWith("-0x")))
            {
                return(ASMValueHelper.HexToUnsigned_AnySign(val, 32));
            }
            else if (ASMStringHelper.StringIsNumeric(val))
            {
                return(Convert.ToUInt32(val));
            }
            else if ((val.StartsWith("-")) && (val.Length > 1))
            {
                string str_uvalue = val.Substring(1);
                if (ASMStringHelper.StringIsNumeric(str_uvalue))
                {
                    uint uvalue = Convert.ToUInt32(str_uvalue);
                    if (uvalue == 0)
                    {
                        return(0);
                    }
                    else
                    {
                        return((uint)(0x100000000 - uvalue));
                    }
                }
                else
                {
                    return(0);
                }
            }
            else
            {
                return(LabelToUnsigned(val, skipLabelAssertion));
            }
        }
Esempio n. 2
0
 public static Nullable <uint> FindUnsignedValueGeneric(string val)
 {
     if (val.StartsWith("0x"))
     {
         //if (val.Length >= 10)
         //	return HexToUnsigned(val.Substring(3,val.Length-3));
         //else
         return(HexToUnsigned(val.Substring(2, val.Length - 2)));
     }
     else if (ASMStringHelper.StringIsNumeric(val))
     {
         return(Convert.ToUInt32(val));
     }
     else
     {
         return(null);
     }
 }
Esempio n. 3
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());
        }