Example #1
0
        protected void FormatCommand(uint address, AVM2Command cmd)
        {
            string output = "";

            //output += String.Format("{0:X4}\t", address);

            //output += String.Format("{0}\t", address);

            if (((byte)Op.Label == cmd.OpCode) || _labels.IsMarked(address))
            {
                output += String.Format("\r\n.label{0}:\r\n", _labels.GetLabelAt(address).id);
            }

            output += "\t" + cmd.StringRepresentation + "\t\t";

            if (cmd.StringRepresentation.Length < 8)
            {
                output += "\t";
            }

            int n = cmd.Parameters.Count;
            int m = n - 1;

            switch (cmd.OpCode)
            {
            // Param 1: U30 -> Multiname
            case (byte)Op.AsType:
            case (byte)Op.Coerce:
            case (byte)Op.DeleteProperty:
            case (byte)Op.FindProperty:
            case (byte)Op.FindPropertyStrict:
            case (byte)Op.GetDescendants:
            case (byte)Op.GetLex:

            case (byte)Op.GetSuper:
            case (byte)Op.InitProperty:
            case (byte)Op.IsType:
            case (byte)Op.SetProperty:
                output += NameUtil.ResolveMultiname(_abc, GetMultiname(cmd, 0));
                break;

            case (byte)Op.GetProperty:
                output += NameUtil.ResolveMultiname(_abc, GetMultiname(cmd, 0));
                break;

            // Param 1: U30 -> Multiname
            // Param 2: Param count
            case (byte)Op.CallProperty:
            case (byte)Op.CallPropertyLex:
            case (byte)Op.CallPropertyVoid:
            case (byte)Op.CallSuper:
            case (byte)Op.CallSuperVoid:
            case (byte)Op.ConstructProperty:
                output += NameUtil.ResolveMultiname(_abc, GetMultiname(cmd, 0));
                output += ", " + ((U30)cmd.Parameters[1]).Value.ToString();
                break;

            // Param 1: U30 -> MethodInfo
            // Param 2: Param count
            case (byte)Op.CallStatic:
                output += ((StringInfo)(_abc.ConstantPool.StringTable[(int)GetMethod(cmd, 0).Name.Value])).ToString();
                output += ", " + ((U30)cmd.Parameters[1]).Value.ToString();
                break;

            // Param 1: U30 -> InstanceInfo
            case (byte)Op.NewClass:
                InstanceInfo ii = (InstanceInfo)_abc.Instances[(int)((U30)cmd.Parameters[0]).Value];
                output += NameUtil.ResolveClass(_abc, ii);
                break;

            // Param 1: ?
            case (byte)Op.PushNamespace:
                output += GetNamespace(cmd, 0);
                break;

            // Param 1: U30 -> DoubleTable
            case (byte)Op.PushDouble:
                //TODO fix this and do not use replace...
                output += GetDouble(cmd, 0).ToString().Replace(',', '.');
                break;

            // Param 1: U30 -> IntTable
            case (byte)Op.PushInt:
                output += GetInt(cmd, 0);
                break;

            // Param 1: U30 -> UIntTable
            case (byte)Op.PushUInt:
                output += GetUInt(cmd, 0);
                break;

            // Param 1: U30 -> StringTable
            case (byte)Op.DebugFile:
            case (byte)Op.PushString:
                output += '"' + GetString(cmd, 0).ToString() + '"';
                break;

            // Param 1: ?
            case (byte)Op.NewFunction:
                output += ((U30)cmd.Parameters[0]).Value + " ;call to anonymous method " + ((U30)cmd.Parameters[0]).Value;
                break;

            // Param 1: S24 -> Jump Offset
            case (byte)Op.IfEqual:
            case (byte)Op.IfFalse:
            case (byte)Op.IfGreaterEqual:
            case (byte)Op.IfGreaterThan:
            case (byte)Op.IfLessEqual:
            case (byte)Op.IfLowerThan:
            case (byte)Op.IfNotEqual:
            case (byte)Op.IfNotGreaterEqual:
            case (byte)Op.IfNotGreaterThan:
            case (byte)Op.IfNotLowerEqual:
            case (byte)Op.IfNotLowerThan:
            case (byte)Op.IfStrictEqual:
            case (byte)Op.IfStrictNotEqual:
            case (byte)Op.IfTrue:
            case (byte)Op.Jump:
                S24 offset = (S24)cmd.Parameters[0];
                // addr + (1byte opcode) + (offset byte length) + (offset value)
                output += String.Format(".label{0}", _labels.GetLabelAt((uint)(address + 1 + offset.Length + offset.Value)).id) + "\r\n";
                break;

            case (byte)Op.LookupSwitch:
                S24 defaultLabel = (S24)cmd.Parameters[0];
                U30 count        = (U30)cmd.Parameters[1];

                output += String.Format(".label{0}", _labels.GetLabelAt((uint)(address + defaultLabel.Value)).id) + ", ";

                for (int i = 0, o = (int)count.Value + 1; i < o; ++i)
                {
                    S24 offsetLabel = (S24)cmd.Parameters[2 + i];

                    output += String.Format(".label{0}", _labels.GetLabelAt((uint)(address + offsetLabel.Value)).id);

                    if (i != o - 1)
                    {
                        output += ", ";
                    }
                }

                output += "\r\n";

                break;

            default:
                for (int i = 0; i < n; ++i)
                {
                    object t = cmd.Parameters[i];

                    if (t is byte)
                    {
                        output += String.Format("{0}", (byte)t);
                    }
                    else if (t is S24)
                    {
                        output += String.Format("{0}", ((S24)t).Value);
                    }
                    else if (t is U30)
                    {
                        output += String.Format("{0}", ((U30)t).Value);
                    }
                    else if (t is U32)
                    {
                        output += String.Format("{0}", ((U32)t).Value);
                    }

                    if (i != m)
                    {
                        output += ", ";
                    }
                }
                break;
            }

            output += "\r\n";

            _output.Add(output);
        }