示例#1
0
        protected T FinalizeAndReturn <T>(SPEEmulator.IEndianBitConverter conv, SPEObjectManager manager, Dictionary <uint, int> transferedObjects, object[] args)
        {
            SPEObjectManager newmanager = new SPEObjectManager(conv);

            //Now extract data back into the objects that are byref
            foreach (KeyValuePair <uint, int> k in transferedObjects)
            {
                if (m_typeSerializeOut[k.Value])
                {
                    newmanager.ReadObjectFromLS(k.Key, args[k.Value]);
                }

                //Remove the entry from the LS object table
                manager.DeleteObject(k.Key);
            }

            //Write back the old, unmodified object table,
            manager.SaveObjectTable();

            Type rtype = typeof(T);

            if (rtype == typeof(ReturnTypeVoid))
            {
                return(default(T));
            }
            else if (rtype.IsPrimitive)
            {
                return((T)newmanager.ReadRegisterPrimitiveValue(rtype, 0));
            }
            else
            {
                uint objindex = (uint)newmanager.ReadRegisterPrimitiveValue(typeof(uint), 0);
                if (objindex == 0)
                {
                    return(default(T));
                }

                return((T)newmanager.ReadObjectFromLS(objindex));
            }
        }
示例#2
0
        protected bool MethodCallback(SPEEmulator.IEndianBitConverter c, uint offset)
        {
            uint sp = c.ReadUInt(offset);

            //Stack size is 77 elements, and return address is next next instruction
            uint call_address = (c.ReadUInt(sp + (16 * 77)) - 4) / 4;

            Mono.Cecil.MethodReference calledmethod;
            m_callpoints.TryGetValue((int)call_address, out calledmethod);

            if (calledmethod == null)
            {
                foreach (KeyValuePair <int, Mono.Cecil.MethodReference> f in m_callpoints)
                {
                    Console.WriteLine("Call registered at {0} for method {1}", f.Key, f.Value.DeclaringType.FullName + "::" + f.Value.Name);
                }
                throw new Exception("No method call registered at " + call_address);
            }

            //All good, we have a real function, now load all required arguments onto PPE
            object[] arguments = new object[calledmethod.Parameters.Count];

            System.Reflection.MethodInfo m = AccCIL.AccCIL.FindReflectionMethod(calledmethod);
            if (m == null)
            {
                throw new Exception("Unable to find function called: " + calledmethod.DeclaringType.FullName + "::" + calledmethod.Name);
            }

            uint   arg_base  = sp + 32;
            uint   sp_offset = arg_base;
            object @this     = null;

            SPEObjectManager manager = new SPEObjectManager(c);

            if (!m.IsStatic)
            {
                Type argtype  = m.DeclaringType;
                uint objindex = (uint)manager.ReadRegisterPrimitiveValue(typeof(uint), sp_offset);
                @this      = manager.ReadObjectFromLS(objindex);
                sp_offset += 16;
            }

            for (int i = 0; i < arguments.Length; i++)
            {
                Type argtype = Type.GetType(calledmethod.Parameters[i].ParameterType.FullName);
                arguments[i] = manager.ReadRegisterPrimitiveValue(argtype.IsPrimitive ? argtype : typeof(uint), sp_offset);

                if (!argtype.IsPrimitive)
                {
                    arguments[i] = manager.ReadObjectFromLS((uint)arguments[i]);
                }

                sp_offset += 16;
            }

            if (calledmethod.Name != "WriteLine" && calledmethod.Name != "Format" && calledmethod.Name != "ToString")
            {
                Console.WriteLine("Invoking {0} with arguments: ", calledmethod.DeclaringType.FullName + "::" + calledmethod.Name);
                foreach (object o in arguments)
                {
                    Console.WriteLine("\t{0}", o == null ? "<null>" : o.ToString());
                }
            }

            object result      = m.Invoke(@this, arguments);
            int    resultIndex = result == null ? 0 : -1;

            foreach (KeyValuePair <uint, object> t in manager.KnownObjectsById)
            {
                if (t.Value != null)
                {
                    //Strings are imutable, so there is no reason to transfer them back
                    if (manager.ObjectTable[t.Key].KnownType == AccCIL.KnownObjectTypes.String)
                    {
                        if (t.Value.Equals(result))
                        {
                            resultIndex = (int)t.Key;
                        }
                        continue;
                    }

                    manager.WriteObjectToLS(t.Key, t.Value);

                    if (t.Value == result)
                    {
                        resultIndex = (int)t.Key;
                    }
                }
            }

            if (m.ReturnType != null)
            {
                if (m.ReturnType.IsPrimitive)
                {
                    manager.WriteRegisterPrimitiveValue(result, arg_base);
                }
                else
                {
                    if (resultIndex < 0)
                    {
                        resultIndex = (int)manager.CreateObjectOnLS(result);
                        manager.ObjectTable[(uint)resultIndex].Refcount = 1;
                        manager.SaveObjectTable();
                    }

                    manager.WriteRegisterPrimitiveValue((uint)resultIndex, arg_base);
                }
            }

            return(true);
        }
示例#3
0
            public object Deserialize(SPEEmulator.IEndianBitConverter conv, ObjectTableEntry e, object storage)
            {
                Type arrtype;
                uint elsize;

                if (e.KnownType == AccCIL.KnownObjectTypes.String)
                {
                    throw new InvalidProgramException("Something is wrong here");
                }

                if (e.Type == 0)
                {
                    arrtype = AccCIL.AccCIL.GetObjType(e.KnownType).MakeArrayType();
                    elsize  = 1u << (int)BuiltInSPEMethods.get_array_elem_len_mult((uint)e.KnownType);
                }
                else
                {
                    arrtype = Type.GetType((string)m_parent.ReadObjectFromLS(e.Type));
                    elsize  = 4;
                }

                Type eltype   = arrtype.GetElementType();
                uint arraylen = e.Size / elsize;

                Array arr;

                if (storage == null)
                {
                    arr = Array.CreateInstance(eltype, arraylen);
                }
                else
                {
                    arr = (Array)storage;
                    if (storage.GetType().GetElementType() != eltype || arr.Length != arraylen)
                    {
                        throw new Exception("Unexpected difference in storage object and actual object");
                    }
                }

                if (eltype.IsPrimitive)
                {
                    ISerializer elserializer;
                    m_parent.Serializers.TryGetValue(eltype, out elserializer);
                    if (elserializer == null)
                    {
                        throw new Exception("Unsupported inner type: " + eltype.FullName);
                    }

                    for (int i = 0; i < arr.Length; i++)
                    {
                        arr.SetValue(elserializer.Deserialize(conv, null, arr.GetValue(i)), i);
                    }
                }
                else
                {
                    //In this case elements may have a different type than what the array states,
                    //because the array elements can be interface or object type, and the actual
                    //instance type is unknown
                    ISerializer uintd = m_parent.Serializers[typeof(uint)];
                    for (int i = 0; i < arr.Length; i++)
                    {
                        arr.SetValue(m_parent.ReadObjectFromLS((uint)uintd.Deserialize(conv, null, null), arr.GetValue(i)), i);
                    }
                }

                return(arr);
            }