Example #1
0
        public static uint toUInt(this GMCode t, GM_Type type)
        {
            byte b  = (byte)t;
            byte bt = (byte)type;

            return((uint)(b << 24) | (uint)(bt << 16));
        }
Example #2
0
            ILBlock CreateScriptBlock()
            {
                if (_scriptIndex < 0)
                {
                    return(null);
                }
                ILBlock           block = File.Codes[_scriptIndex].Block;
                HashSet <GM_Type> types = new HashSet <GM_Type>();

                _argumentCount = 0;
                foreach (var e in block.GetSelfAndChildrenRecursive <ILExpression>(x => x.Code == GMCode.Ret || x.Code == GMCode.Var))
                {
                    switch (e.Code)
                    {
                    case GMCode.Ret:
                        types.Add(e.Type);
                        break;

                    case GMCode.Var:
                        CountArguments(e.Operand as ILVariable, ref _argumentCount);
                        break;
                    }
                }
                _returnType = GM_Type.NoType;
                foreach (var t in types)
                {
                    _returnType = _returnType.ConvertType(t);
                }
                return(block);
            }
Example #3
0
        public static bool MatchType(this ILNode node, GM_Type type, out ILExpression expr)
        {
            ILExpression e = node as ILExpression;

            if (e != null && e.Type == type)
            {
                expr = e;
                return(true);
            }
            expr = default(ILExpression);
            return(false);
        }
Example #4
0
        public static int GetSize(this GM_Type t)
        {
            switch (t)
            {
            case GM_Type.Double:
            case GM_Type.Long:
                return(2);

            case GM_Type.Short:
                return(0);

            default:
                return(1);
            }
        }
Example #5
0
 public ILValue(object o, GM_Type type)
 {
     if (o is short)
     {
         this.Value = (int)((short)o);
     }
     else if (type == GM_Type.String)
     {
         this.ValueText = GMCodeUtil.EscapeString(o as string);
     }
     else
     {
         this.Value = o;
     }
     Type = type;
 }
Example #6
0
        ILValue OperandToIValue(object obj, GM_Type type)
        { // throws things if the cast is bad
            switch (type)
            {
            case GM_Type.Bool:
            case GM_Type.Double:
            case GM_Type.Float:
            case GM_Type.Long:
            case GM_Type.Int:
            case GM_Type.String:
            case GM_Type.Short:
                return(new ILValue(obj, type));

            default:
                throw new Exception("Cannot convert simple type");
            }
        }
Example #7
0
        protected static GM_Type[] ReadTypes(uint rawCode)
        {
            uint topByte       = (rawCode >> 24) & 255;
            uint secondTopByte = (rawCode >> 16) & 255;

            GM_Type[] types = null;
            if ((topByte & 160) == 128)
            {
                types = new GM_Type[] { ReadRaw(secondTopByte & 15) }
            }
            ;
            else if ((topByte & 160) == 0)
            {
                types = new GM_Type[] { ReadRaw(secondTopByte & 15), ReadRaw((secondTopByte >> 4) & 15) }
            }
            ;
            return(types);
        }
Example #8
0
 public static GM_Type ConvertType(this GM_Type t0, GM_Type t1)
 {
     if (t0 == t1) return t0;
     if (t1 == GM_Type.Bool) return t1; // bool ALWAYS overrides
     if (t1 == GM_Type.String && t0.isInstance()) throw new Exception("bad type");
     if (t1.isBestVar()) return t1;
     if (t0.isBestVar()) return t0;
     switch (t0)
     {
         case GM_Type.Var:
             return t1; // Vars are general variables so we don't want that
         case GM_Type.String:
             Debug.Assert(t1.isInstance()); // check in case
             return t0;
         case GM_Type.Bool:
             return t0; // bool ALWAYS overrides eveything
         case GM_Type.Short:
         case GM_Type.NoType:
             return t1; // whatever t1 is its better
         case GM_Type.Double:
             Debug.Assert(!t1.isInstance());
             if (t1.isNumber()) return t0; // we can convert
             else if (t1.isInstance() || t1 == GM_Type.String) return t1; // instance is MUCH better than double, more important
             return t0;
         case GM_Type.Sound:
         case GM_Type.Instance:
         case GM_Type.Sprite:
             if (t1 == GM_Type.Int || t1 == GM_Type.Short) return t0; // we can be an instance
             throw new Exception("Cannot convert this type to this");
         case GM_Type.Int:
             if (t1 == GM_Type.Long || t1 == GM_Type.Float || t1 == GM_Type.Double || t1 == GM_Type.Instance || t1 == GM_Type.Sprite) return t1;
             else return t0;
         case GM_Type.Long:
             if (t1 == GM_Type.Double) return t1;
             else return t0;
         case GM_Type.Float:
             if (t1 == GM_Type.Double) return t1;
             else return t0;
         default:
             throw new Exception("Canot get here");
     }
 }
        private static int smethod_1(int int_5)
        {
            int     num  = 0;
            GM_Type int5 = (GM_Type)(int_5 & 15);

            switch (int5)
            {
            case GM_Type.Double:
            {
                num = 8;
                break;
            }

            case GM_Type.Float:
            case GM_Type.Int:
            case GM_Type.Bool:
            case GM_Type.Var:
            case GM_Type.String:
            {
                num = 4;
                break;
            }

            case GM_Type.Long:
            {
                num = 8;
                break;
            }

            default:
            {
                if (int5 == GM_Type.Short)
                {
                    break;
                }
                break;
            }
            }
            return(num);
        }
Example #10
0
        public static string TypeToStringPostfix(this GM_Type t)
        {
            switch (t)
            {
            case GM_Type.Double: return(".d");

            case GM_Type.Float: return(".f");

            case GM_Type.Int: return(".i");

            case GM_Type.Long: return(".l");

            case GM_Type.Bool: return(".b");

            case GM_Type.Var: return(".v");

            case GM_Type.String: return(".s");

            case GM_Type.Short: return(".e");
            }
            throw new Exception("Not sure how we got here");
        }
Example #11
0
        static protected ILValue ReadConstant(BinaryReader r, GM_Type t)
        {
            ILValue v      = null;
            int     offset = (int)r.BaseStream.Position;

            switch (t)
            {
            case GM_Type.Double: v = new ILValue(r.ReadDouble()); break;

            case GM_Type.Float: v = new ILValue(r.ReadSingle()); break;

            case GM_Type.Int: v = new ILValue(r.ReadInt32()); break;   // function?

            case GM_Type.Long: v = new ILValue(r.ReadInt64()); break;  // function?

            case GM_Type.Bool: v = new ILValue(r.ReadInt32() != 0); break;

            case GM_Type.String:
            {
                int i = r.ReadInt32();
                if (i < 0 || i >= File.Strings.Count)
                {
                    v = new ILValue("$BADSTRINGVALUE$");
                }
                else
                {
                    v = new ILValue(File.Strings[i].String);
                }
            }
            break;

            default:
                throw new Exception("Should not get here");
            }
            v.DataOffset = offset;
            return(v);
        }
Example #12
0
 public ILValue(long i)
 {
     this.Value = i; Type = GM_Type.Long;
 }
Example #13
0
 public ILValue(short i)
 {
     this.Value = (int)i; Type = GM_Type.Short;
 }
Example #14
0
 public ILValue(float i)
 {
     this.Value = i; Type = GM_Type.Float;
 }
Example #15
0
 public ILValue(double i)
 {
     this.Value = i; Type = GM_Type.Double;
 }
Example #16
0
 public static bool isInstance(this GM_Type t)
 {
     return(t == GM_Type.Instance || t == GM_Type.Sprite || t == GM_Type.Sound || t == GM_Type.Path);
 }
Example #17
0
 public ILValue(string i)
 {
     this.Value = i; Type = GM_Type.String; this.ValueText = GMCodeUtil.EscapeString(i as string);
 }
Example #18
0
 public ILValue(ILValue v)
 {
     this.Value = v.Value; Type = v.Type; this.ValueText = v.ValueText;
 }
Example #19
0
 public static bool canBeInstance(this GM_Type t)
 {
     return(t == GM_Type.Int || t == GM_Type.Short || t.isInstance());
 }
Example #20
0
 public static bool isInteger(this GM_Type t)
 {
     return(t == GM_Type.Int || t == GM_Type.Long || t == GM_Type.Short);
 }
Example #21
0
        int DissasembleOneOpcode()
        {
            int size          = 1;
            int pos           = (int)r.BaseStream.Position;
            int opcode        = r.ReadInt32();
            int topByte       = GetTopByte(opcode);
            int secondTopByte = GetSecondTopByte(opcode);
            int length        = 11;

            writer.Write("{0:x8} : ", pos);
            writer.Write("{0:x8}", opcode);
            byte[] operand = null;
            if ((topByte & 64) != 0)
            {
                int extra = smethod_1(secondTopByte);
                operand = r.ReadBytes(extra);
                size   += extra;
                foreach (var b in operand)
                {
                    writer.Write("{0:x2}", b);
                    length += 2;
                }
            }
            while (length < 36)
            {
                writer.Write(" ");
                length++;
            }
            string str = ((NewOpcode)topByte).ToString();

            writer.Write(str);
            length = length + str.Length;
            if ((topByte & 160) == 128)
            {
                writer.Write(((GM_Type)(secondTopByte & 15)).GMTypeToPostfix());
                length = length + 2;
            }
            else if ((topByte & 160) == 0)
            {
                writer.Write(((GM_Type)(secondTopByte & 15)).GMTypeToPostfix());
                writer.Write(((GM_Type)((secondTopByte >> 4) & 15)).GMTypeToPostfix());
                length = length + 4;
            }
            while (length < 46)
            {
                writer.Write(" ");
                length++;
            }
            if ((topByte & 64) != 0)
            {
                GM_Type eVMType = (GM_Type)(secondTopByte & 15);
                switch (eVMType)
                {
                case GM_Type.Double:
                {
                    writer.Write("{0}", BitConverter.ToDouble(operand, 0));
                    break;
                }

                case GM_Type.Float:
                {
                    writer.Write("{0}", BitConverter.ToSingle(operand, 0));
                    break;
                }

                case GM_Type.Int:
                {
                    int i = BitConverter.ToInt32(operand, 0);
                    if (topByte == 217)
                    {
                        if (i < 0 || i >= File.Strings.Count)
                        {
                            writer.Write("$unknown_function$");
                            break;
                        }
                        else
                        {
                            writer.Write("${0}$", File.Strings[i]);
                            break;
                        }
                    }
                    else
                    {
                        writer.Write("{0}", i);
                        break;
                    }
                }

                case GM_Type.Long:
                {
                    writer.Write("{0}", BitConverter.ToInt64(operand, 0));
                    break;
                }

                case GM_Type.Bool:
                {
                    writer.Write("{0}", BitConverter.ToInt32(operand, 0) != 0);
                    break;
                }

                case GM_Type.Var:
                {
                    int i = (BitConverter.ToInt32(operand, 0) & 0xFFFFFFF) + 1;
                    if (i < 0 || i >= File.Strings.Count)
                    {
                        writer.Write("$null$");
                        break;
                    }
                    else
                    {
                        string s = File.Strings[i].String;
                        writer.Write("${0}$", s);
                        break;
                    }
                }

                case GM_Type.String:
                {
                    int i = BitConverter.ToInt32(operand, 0);
                    if (i < 0 || i >= File.Strings.Count)
                    {
                        writer.Write("null");
                        break;
                    }
                    else
                    {
                        writer.Write("\"{0}\"", File.Strings[i]);
                        break;
                    }
                }

                case GM_Type.Short:
                {
                    int i = (opcode << 16) >> 16;
                    writer.Write("{0}", i);
                }
                break;

                default:
                    writer.Write("T{0} ", eVMType.ToString());
                    if (operand != null)
                    {
                        foreach (var b in operand)
                        {
                            writer.Write("{0:x2}", b);
                        }
                    }
                    break;
                }
            }
            else if ((topByte & 32) != 0)
            {
                writer.Write("0x{0:x8}", pos + (opcode << 9 >> 7));
            }
            writer.WriteLine();
            return(size);
        }
Example #22
0
        public static GM_Type ConvertType(this GM_Type t0, GM_Type t1)
        {
            if (t0 == t1)
            {
                return(t0);
            }
            if (t1 == GM_Type.Bool)
            {
                return(t1);                    // bool ALWAYS overrides
            }
            if (t1 == GM_Type.String && t0.isInstance())
            {
                throw new Exception("bad type");
            }
            if (t1.isBestVar())
            {
                return(t1);
            }
            if (t0.isBestVar())
            {
                return(t0);
            }
            switch (t0)
            {
            case GM_Type.Var:
                return(t1);    // Vars are general variables so we don't want that

            case GM_Type.String:
                Debug.Assert(t1.isInstance());     // check in case
                return(t0);

            case GM_Type.Bool:
                return(t0);    // bool ALWAYS overrides eveything

            case GM_Type.Short:
            case GM_Type.NoType:
                return(t1);    // whatever t1 is its better

            case GM_Type.Double:
                Debug.Assert(!t1.isInstance());
                if (t1.isNumber())
                {
                    return(t0);                   // we can convert
                }
                else if (t1.isInstance() || t1 == GM_Type.String)
                {
                    return(t1);                                                  // instance is MUCH better than double, more important
                }
                return(t0);

            case GM_Type.Sound:
            case GM_Type.Instance:
            case GM_Type.Sprite:
                if (t1 == GM_Type.Int || t1 == GM_Type.Short)
                {
                    return(t0);                                              // we can be an instance
                }
                throw new Exception("Cannot convert this type to this");

            case GM_Type.Int:
                if (t1 == GM_Type.Long || t1 == GM_Type.Float || t1 == GM_Type.Double || t1 == GM_Type.Instance || t1 == GM_Type.Sprite)
                {
                    return(t1);
                }
                else
                {
                    return(t0);
                }

            case GM_Type.Long:
                if (t1 == GM_Type.Double)
                {
                    return(t1);
                }
                else
                {
                    return(t0);
                }

            case GM_Type.Float:
                if (t1 == GM_Type.Double)
                {
                    return(t1);
                }
                else
                {
                    return(t0);
                }

            default:
                throw new Exception("Canot get here");
            }
        }
Example #23
0
 // This is the top dog, you can't convert this downward without a function or some cast
 public static bool isBestVar(this GM_Type t)
 {
     return(t.isInstance() || t == GM_Type.String || t == GM_Type.Double || t == GM_Type.Bool);
 }
Example #24
0
 public static bool isNumber(this GM_Type t)
 {
     return(t.isReal() || t.isInteger() || !t.isInstance());
 }
Example #25
0
 public ILValue(ILVariable v)
 {
     this.Value = v; Type = GM_Type.Var;
 }
Example #26
0
 static protected ILValue ReadConstant(BinaryReader r, GM_Type t)
 {
     ILValue v = null;
     int offset = (int)r.BaseStream.Position;
     switch (t)
     {
         case GM_Type.Double: v= new ILValue(r.ReadDouble()); break;
         case GM_Type.Float: v= new ILValue(r.ReadSingle()); break;
         case GM_Type.Int: v= new ILValue(r.ReadInt32()); break;// function?
         case GM_Type.Long: v= new ILValue(r.ReadInt64()); break;// function?
         case GM_Type.Bool: v= new ILValue(r.ReadInt32() != 0); break;
         case GM_Type.String:
             {
                 int i = r.ReadInt32();
                 if (i < 0 || i >= File.Strings.Count) v= new ILValue("$BADSTRINGVALUE$");
                 else v= new ILValue(File.Strings[i].String);
             }
             break;
         default:
             throw new Exception("Should not get here");
     }
     v.DataOffset = offset;
     return v;
 }
Example #27
0
 public ILValue(ILExpression e)
 {
     this.Value = e; Type = GM_Type.ConstantExpression;
 }
Example #28
0
 protected  ILExpression CreateExpression(GMCode code,  GM_Type[] types, ILLabel operand)
 {
     Debug.Assert(operand != null);
     ILExpression e = new ILExpression(code, operand);
     e.Types = types;
     e.Extra = (int)(CurrentRaw & 0xFFFF);
     e.AddILRange(CurrentPC);
     return e;
 }
Example #29
0
        public static bool MatchType(this ILNode node, GM_Type type)
        {
            ILExpression call;

            return(node.MatchType(type, out call));
        }
Example #30
0
 public static bool isReal(this GM_Type t)
 {
     return(t == GM_Type.Double || t == GM_Type.Float);
 }
Example #31
0
 protected static GM_Type[] ReadTypes(uint rawCode)
 {
     uint topByte = (rawCode >> 24) & 255;
     uint secondTopByte = (rawCode >> 16) & 255;
     GM_Type[] types = null;
     if ((topByte & 160) == 128) types = new GM_Type[] { ReadRaw(secondTopByte & 15) };
     else if ((topByte & 160) == 0) types = new GM_Type[] { ReadRaw(secondTopByte & 15), ReadRaw((secondTopByte >> 4) & 15) };
     return types;
 }
Example #32
0
 public ILValue(bool i)
 {
     this.Value = i; Type = GM_Type.Bool;
 }
Example #33
0
 protected ILExpression CreateExpression(GMCode code,  GM_Type[] types)
 {
     ILExpression e = new ILExpression(code, null);
     e.Types = types;
     e.Extra = (int)(CurrentRaw & 0xFFFF);
     e.AddILRange(CurrentPC);
     return e;
 }
Example #34
0
 public ILValue(int i)
 {
     this.Value = i; Type = GM_Type.Int;
 }
Example #35
0
 protected ILExpression CreatePushExpression(GMCode code, GM_Type[] types)
 {
     ILExpression e = new ILExpression(code, null);
     e.Types = types;
     e.Extra = (int)(CurrentRaw & 0xFFFF);
     Debug.Assert(e.ILRanges.Count == 0);
     e.AddILRange(CurrentPC);
     ILValue v = null;
     switch (types[0])
     {
         case GM_Type.Var:
                 e.Operand = BuildUnresolvedVar(r.ReadInt32());
             break;
         case GM_Type.Short:
             {
                 v = new ILValue((short)(CurrentRaw & 0xFFFF));
                 v.DataOffset = (int)(r.BaseStream.Position - 4);
                 e.Arguments.Add(new ILExpression(GMCode.Constant, v));
             }
             break;
         default:
             v = ReadConstant(r, types[0]);
             e.Arguments.Add(new ILExpression(GMCode.Constant, v));
             break;
     }
     if (v != null)
     {
         v.DataOffset += StartingOffset ;
         DebugILValueOffset(v);
     }
    
     return e;
 }
Example #36
0
 public ILValue(object i)
 {
     this.Value = i; Type = GM_Type.Var;
 }