public override void AssembleNew(Assembler aAssembler, object aMethodInfo)
        {
            var xAssembler        = aAssembler;
            var xMethodInfo       = (_MethodInfo)aMethodInfo;
            var xMethodBaseAsInfo = xMethodInfo.MethodBase as global::System.Reflection.MethodInfo;

            if (xMethodBaseAsInfo.ReturnType != typeof(void))
            {
                throw new Exception("Events with return type not yet supported!");
            }

            /*
             * EAX contains the GetInvocationList() array at the index at which it was last used
             * EDX contains the index at which the EAX is
             * EBX contains the number of items in the array
             * ECX contains the argument size
             */

            XS.ClearInterruptFlag();

            XS.Comment("Get Invoke list count");
            var xGetInvocationListMethod = typeof(MulticastDelegate).GetMethod("GetInvocationList");

            Ldarg.DoExecute(aAssembler, xMethodInfo, 0);
            XS.Call(LabelName.Get(xGetInvocationListMethod));
            XS.Add(XSRegisters.ESP, 4);
            XS.Pop(XSRegisters.EAX);
            XS.Add(XSRegisters.EAX, 8);
            XS.Set(XSRegisters.EBX, XSRegisters.EAX, sourceIsIndirect: true);

            XS.Comment("Get invoke method");
            XS.Add(XSRegisters.EAX, 8);
            XS.Set(XSRegisters.EDI, XSRegisters.EAX, sourceIsIndirect: true, sourceDisplacement: 4);

            XS.Comment("Get ArgSize");
            int xArgSizeOffset = Ldfld.GetFieldOffset(typeof(global::System.Delegate), "$$ArgSize$$");

            Ldarg.DoExecute(aAssembler, xMethodInfo, 0);
            XS.Add(XSRegisters.ESP, 4);
            XS.Pop(XSRegisters.ECX);
            XS.Add(XSRegisters.ECX, (uint)xArgSizeOffset);
            XS.Set(XSRegisters.ECX, XSRegisters.ECX, sourceIsIndirect: true);

            XS.Comment("Set current invoke list index");
            XS.Set(XSRegisters.EDX, 0);

            XS.Label(".BEGIN_OF_LOOP");
            {
                XS.Compare(XSRegisters.EDX, XSRegisters.EBX);
                XS.Jump(x86.ConditionalTestEnum.GreaterThanOrEqualTo, ".END_OF_INVOKE");

                XS.PushAllRegisters();

                XS.Comment("Check if delegate has $this");
                XS.Set(XSRegisters.EDI, XSRegisters.EBP, sourceDisplacement: Ldarg.GetArgumentDisplacement(xMethodInfo, 0));
                XS.Add(XSRegisters.EDI, 4);
                XS.Set(XSRegisters.EDI, XSRegisters.EDI, sourceDisplacement: Ldfld.GetFieldOffset(xMethodInfo.MethodBase.DeclaringType, "System.Object System.Delegate._target"));
                XS.Compare(XSRegisters.EDI, 0);
                XS.Jump(x86.ConditionalTestEnum.Zero, ".NO_THIS");
                XS.Label(".HAS_THIS");
                XS.Push(XSRegisters.EDI);
                XS.Push(0);
                XS.Label(".NO_THIS");
                XS.Set(XSRegisters.EDI, XSRegisters.EAX, sourceIsIndirect: true, sourceDisplacement: 4);
                XS.Set(XSRegisters.EDI, XSRegisters.EDI, sourceDisplacement: Ldfld.GetFieldOffset(xMethodInfo.MethodBase.DeclaringType, "System.IntPtr System.Delegate._methodPtr"));

                XS.Comment("Check if delegate has args");
                XS.Compare(XSRegisters.ECX, 0);
                XS.Jump(x86.ConditionalTestEnum.Zero, ".NO_ARGS");
                XS.Label(".HAS_ARGS");
                XS.Sub(XSRegisters.ESP, XSRegisters.ECX);
                XS.Push(XSRegisters.EDI);
                XS.Set(XSRegisters.EDI, XSRegisters.ESP);
                XS.Add(XSRegisters.EDI, 4);
                XS.Set(XSRegisters.ESI, XSRegisters.EBP);
                XS.Add(XSRegisters.ESI, 8);
                new x86.Movs {
                    Size = 8, Prefixes = x86.InstructionPrefixes.Repeat
                };
                XS.Pop(XSRegisters.EDI);
                XS.Label(".NO_ARGS");
                XS.Call(XSRegisters.EDI);

                XS.PopAllRegisters();
                XS.Increment(XSRegisters.EDX);
                XS.Jump(".BEGIN_OF_LOOP");
            }

            XS.Label(".END_OF_INVOKE");
            XS.Set(XSRegisters.EDX, XSRegisters.EBP, sourceDisplacement: Ldarg.GetArgumentDisplacement(xMethodInfo, 0));
            XS.Set(XSRegisters.EDX, XSRegisters.EDX, sourceDisplacement: Ldfld.GetFieldOffset(xMethodInfo.MethodBase.DeclaringType, "$$ReturnsValue$$"));
            XS.Compare(XSRegisters.EDX, 0);
            XS.Jump(x86.ConditionalTestEnum.Equal, ".NO_RETURN");

            XS.Label(".HAS_RETURN");
            XS.Exchange(XSRegisters.EBP, XSRegisters.EDX, destinationDisplacement: 8);
            XS.Exchange(XSRegisters.EBP, XSRegisters.EDX, destinationDisplacement: 4);
            XS.Exchange(XSRegisters.EBP, XSRegisters.EDX, destinationIsIndirect: true);
            XS.Push(XSRegisters.EDX);
            XS.Set(XSRegisters.ESP, XSRegisters.EDI, destinationDisplacement: 12);

            XS.Label(".NO_RETURN");
            XS.EnableInterrupts();
        }
예제 #2
0
        public override void AssembleNew(Cosmos.Assembler.Assembler aAssembler, object aMethodInfo)
        {
            var xAssembler        = (Cosmos.Assembler.Assembler)aAssembler;
            var xMethodInfo       = (Cosmos.IL2CPU.MethodInfo)aMethodInfo;
            var xMethodBaseAsInfo = xMethodInfo.MethodBase as global::System.Reflection.MethodInfo;

            if (xMethodBaseAsInfo.ReturnType != typeof(void))
            {
                throw new Exception("Events with return type not yet supported!");
            }
            new Comment("XXXXXXX");
            new CPUx86.Xchg {
                DestinationReg = CPUx86.Registers.BX, SourceReg = CPUx86.Registers.BX, Size = 16
            };

            /*
             * EAX contains the GetInvocationList() array at the index at which it was last used
             * EDX contains the index at which the EAX is
             * EBX contains the number of items in the array
             * ECX contains the argument size
             */
            new CPUx86.ClrInterruptFlag();
            new CPU.Label(".DEBUG");
            //new CPU.Label("____DEBUG_FOR_MULTICAST___");
            new CPU.Comment("move address of delgate to eax");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.EAX, SourceReg = CPUx86.Registers.EBP, SourceIsIndirect = true, SourceDisplacement = Ldarg.GetArgumentDisplacement(xMethodInfo, 0)
            };
            var xGetInvocationListMethod = typeof(MulticastDelegate).GetMethod("GetInvocationList");

            new CPU.Comment("push address of delgate to stack");
            new CPUx86.Push {
                DestinationReg = CPUx86.Registers.EAX
            };                                                  //addrof this
            new CPUx86.Call {
                DestinationLabel = CPU.LabelName.Get(xGetInvocationListMethod)
            };
            new CPU.Comment("get address from return value -> eax");
            new CPUx86.Pop {
                DestinationReg = CPUx86.Registers.EAX
            };
            ;//list
            new CPU.Comment("eax+=8 is where the offset where an array's count is");
            new CPUx86.Add {
                DestinationReg = CPUx86.Registers.EAX, SourceValue = 8
            };                                                                  //addrof list.count??
            new CPU.Comment("store count in ebx");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.EBX, SourceReg = CPUx86.Registers.EAX, SourceIsIndirect = true
            };                                                                                                            //list.count
            new CPU.Comment("eax+=8 is where the offset where an array's items start");
            new CPUx86.Add {
                DestinationReg = CPUx86.Registers.EAX, SourceValue = 8
            };                                                                  //why? -- start of list i think? MtW: the array's .Length is at +8
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.EDI, SourceValue = 0
            };
            new CPU.Comment("ecx = ptr to delegate object");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.ECX, SourceReg = CPUx86.Registers.EBP, SourceIsIndirect = true, SourceDisplacement = Ldarg.GetArgumentDisplacement(xMethodInfo, 0)
            };                                                                                                                                                                                //addrof the delegate
            new CPU.Comment("ecx points to the size of the delegated methods arguments");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.ECX, SourceReg = CPUx86.Registers.ECX, SourceIsIndirect = true, SourceDisplacement = Ldfld.GetFieldOffset(xMethodInfo.MethodBase.DeclaringType, "$$ArgSize$$")
            };                                                                                                                                                                                                            //the size of the arguments to the method? + 12??? -- 12 is the size of the current call stack.. i think
            new CPUx86.Xor {
                DestinationReg = CPUx86.Registers.EDX, SourceReg = CPUx86.Registers.EDX
            };
            ;//make sure edx is 0
            new CPU.Label(".BEGIN_OF_LOOP");
            new CPUx86.Compare {
                DestinationReg = CPUx86.Registers.EDX, SourceReg = CPUx86.Registers.EBX
            };                                                                                       //are we at the end of this list
            new CPUx86.ConditionalJump {
                Condition = CPUx86.ConditionalTestEnum.Equal, DestinationLabel = ".END_OF_INVOKE_"
            };                                                                                                          //then we better stop
            new CPUx86.Pushad();
            new CPU.Comment("esi points to where we will copy the methods argumetns from");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.ESI, SourceReg = CPUx86.Registers.ESP
            };
            new CPU.Comment("edi = ptr to delegate object");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.EDI, SourceReg = CPUx86.Registers.EBP, SourceIsIndirect = true, SourceDisplacement = Ldarg.GetArgumentDisplacement(xMethodInfo, 0)
            };
            new CPU.Comment("edi = ptr to delegate object should be a pointer to the delgates context ie (this) for the methods ");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.EDI, SourceReg = CPUx86.Registers.EDI, SourceIsIndirect = true, SourceDisplacement = Ldfld.GetFieldOffset(xMethodInfo.MethodBase.DeclaringType, "System.Object System.Delegate._target")
            };
            new CPUx86.Compare {
                DestinationReg = CPUx86.Registers.EDI, SourceValue = 0
            };
            new CPUx86.ConditionalJump {
                Condition = CPUx86.ConditionalTestEnum.Zero, DestinationLabel = ".NO_THIS"
            };
            new CPUx86.Push {
                DestinationReg = CPUx86.Registers.EDI
            };

            new CPU.Label(".NO_THIS");

            new CPU.Comment("make space for us to copy the arguments too");
            new CPUx86.Sub {
                DestinationReg = CPUx86.Registers.ESP, SourceReg = CPUx86.Registers.ECX
            };
            new CPU.Comment("move the current delegate to edi");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.EDI, SourceReg = CPUx86.Registers.EAX, SourceIsIndirect = true
            };
            new CPU.Comment("move the methodptr from that delegate to edi ");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.EDI, SourceReg = CPUx86.Registers.EDI, SourceIsIndirect = true, SourceDisplacement = Ldfld.GetFieldOffset(xMethodInfo.MethodBase.DeclaringType, "System.IntPtr System.Delegate._methodPtr")
            };                                                                                                                                                                                                                                         //
            new CPU.Comment("save methodptr on the stack");
            new CPUx86.Push {
                DestinationReg = CPUx86.Registers.EDI
            };
            new CPU.Comment("move location to copy args to");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.EDI, SourceReg = CPUx86.Registers.ESP
            };
            new CPU.Comment("get above the saved methodptr");
            new CPUx86.Add {
                DestinationReg = CPUx86.Registers.EDI, SourceValue = 4
            };
            //we allocated the argsize on the stack once, and it we need to get above the original args
            new CPU.Comment("we allocated argsize on the stack once");
            new CPU.Comment("add 32 for the Pushad + 16 for the current stack + 4 for the return value");
            //uint xToAdd = 32; // skip pushad data
            //xToAdd += 4; // method pointer
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.ESI, SourceReg = CPUx86.Registers.EBP
            };
            new CPUx86.Add {
                DestinationReg = CPUx86.Registers.ESI, SourceValue = 8
            };                                                                   // ebp+8 is first argument
            new CPUx86.Movs {
                Size = 8, Prefixes = CPUx86.InstructionPrefixes.Repeat
            };
            new CPUx86.Pop {
                DestinationReg = CPUx86.Registers.EDI
            };
            new CPUx86.Call {
                DestinationReg = CPUx86.Registers.EDI
            };
            new CPU.Comment("store return -- return stored into edi after popad");
            new CPU.Comment("edi = ptr to delegate object");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.EDI, SourceReg = CPUx86.Registers.EBP, SourceIsIndirect = true, SourceDisplacement = Ldarg.GetArgumentDisplacement(xMethodInfo, 0)
            };
            new CPU.Comment("edi = ptr to delegate object should be a pointer to the delgates context ie (this) for the methods ");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.EDI, SourceReg = CPUx86.Registers.EDI, SourceIsIndirect = true, SourceDisplacement = Ldfld.GetFieldOffset(xMethodInfo.MethodBase.DeclaringType, "System.Object System.Delegate._target")
            };                                                                                                                                                                                                                                      //i really dont get the +12, MtW: that's for the object header
            new CPU.Label(".noTHIStoPop");
            new CPUx86.Popad();
            new CPUx86.INC {
                DestinationReg = CPUx86.Registers.EDX
            };
            new CPUx86.Add {
                DestinationReg = CPUx86.Registers.EAX, SourceValue = 4
            };
            new CPUx86.Jump {
                DestinationLabel = ".BEGIN_OF_LOOP"
            };
            new CPU.Label(".END_OF_INVOKE_");
            new CPU.Comment("get the return value");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.EDX, SourceReg = CPUx86.Registers.EBP, SourceIsIndirect = true, SourceDisplacement = Ldarg.GetArgumentDisplacement(xMethodInfo, 0)
            };                                                                                                                                                                                //addrof the delegate
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.EDX, SourceReg = CPUx86.Registers.EDX, SourceIsIndirect = true, SourceDisplacement = Ldfld.GetFieldOffset(xMethodInfo.MethodBase.DeclaringType, "$$ReturnsValue$$")
            };
            new CPUx86.Compare {
                DestinationReg = CPUx86.Registers.EDX, SourceValue = 0
            };
            new CPUx86.ConditionalJump {
                Condition = CPUx86.ConditionalTestEnum.Equal, DestinationLabel = ".noReturn"
            };
            //may have to expand the return... idk
            new CPUx86.Xchg {
                DestinationReg = CPUx86.Registers.EBP, DestinationIsIndirect = true, DestinationDisplacement = 8, SourceReg = CPUx86.Registers.EDX
            };
            new CPUx86.Xchg {
                DestinationReg = CPUx86.Registers.EBP, DestinationIsIndirect = true, DestinationDisplacement = 4, SourceReg = CPUx86.Registers.EDX
            };
            new CPUx86.Xchg {
                DestinationReg = CPUx86.Registers.EBP, DestinationIsIndirect = true, SourceReg = CPUx86.Registers.EDX
            };
            new CPUx86.Push {
                DestinationReg = CPUx86.Registers.EDX
            };                                                  //ebp
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.ESP, DestinationIsIndirect = true, DestinationDisplacement = 12, SourceReg = CPUx86.Registers.EDI
            };
            new CPU.Label(".noReturn");
            new CPUx86.Sti();
        }
예제 #3
0
        public override void AssembleNew(Assembler aAssembler, object aMethodInfo)
        {
            var xMethodInfo = (Il2cpuMethodInfo)aMethodInfo;

            /*
             * EAX contains the GetInvocationList() array at the index at which it was last used
             * EBX contains the number of items in the array
             * ECX contains the argument size
             * EDX contains the current index in the array
             * ESI contains the size of the return value
             * EDI contains the function pointer
             */

            XS.ClearInterruptFlag();

            XS.Comment("Get Invoke list count");
            var xGetInvocationListMethod = typeof(MulticastDelegate).GetMethod("GetInvocationList");

            Ldarg.DoExecute(aAssembler, xMethodInfo, 0);
            XS.Call(LabelName.Get(xGetInvocationListMethod));
            XS.Add(ESP, 4);
            XS.Pop(EAX);
            XS.Add(EAX, 8);
            XS.Set(EBX, EAX, sourceIsIndirect: true);

            XS.Comment("Get invoke method");
            XS.Add(EAX, 8);
            XS.Set(EDI, EAX, sourceIsIndirect: true, sourceDisplacement: 4); // this line can propably can be removed

            XS.Comment("Get ArgSize");
            int xArgSizeOffset = Ldfld.GetFieldOffset(typeof(Delegate), "$$ArgSize$$");

            Ldarg.DoExecute(aAssembler, xMethodInfo, 0);
            XS.Add(ESP, 4);
            XS.Pop(ECX);
            XS.Add(ECX, (uint)xArgSizeOffset);
            XS.Set(ECX, ECX, sourceIsIndirect: true);

            XS.Comment("Set current invoke list index");
            XS.Set(EDX, 0);

            XS.Comment("Make space for return value");
            int returnSizeOffset = Ldfld.GetFieldOffset(typeof(Delegate), "$$ReturnSize$$");

            Ldarg.DoExecute(aAssembler, xMethodInfo, 0);
            XS.Add(ESP, 4);
            XS.Pop(ESI);
            XS.Add(ESI, (uint)returnSizeOffset);
            XS.Set(ESI, ESI, sourceIsIndirect: true);
            XS.Sub(ESP, ESI);

            XS.Label(".BEGIN_OF_LOOP");
            {
                XS.Compare(EDX, EBX);
                XS.Jump(x86.ConditionalTestEnum.GreaterThanOrEqualTo, ".END_OF_INVOKE");
                XS.PushAllRegisters();

                XS.Comment("Check if delegate has $this");
                XS.Set(EDI, EBP, sourceDisplacement: Ldarg.GetArgumentDisplacement(xMethodInfo, 0));
                XS.Add(EDI, 4);
                XS.Set(EDI, EDI, sourceDisplacement: Ldfld.GetFieldOffset(xMethodInfo.MethodBase.DeclaringType, "System.Object System.Delegate._target"));
                XS.Set(EDX, ECX); // edx contains the size of the arguments including $this
                XS.Compare(EDI, 0);
                XS.Jump(x86.ConditionalTestEnum.Zero, ".NO_THIS");
                XS.Label(".HAS_THIS");
                XS.Push(EDI);
                XS.Set(EDI, EDI, sourceIsIndirect: true); // get type of target object
                XS.Add(EDX, 4);                           // we have at least one int of $this

                //TODO: In future we might be able to replace the following call with a check
                //if the object is boxed and in that case assume its a struct

                // safe info from registers which get trashed
                XS.Push(EAX);
                XS.Push(EBX);
                XS.Push(ECX);
                XS.Push(EDX);

                XS.Push(EDI);
                XS.Call(LabelName.Get(VTablesImplRefs.IsStructRef));
                XS.Pop(EDI);

                // restore values
                XS.Pop(EDX);
                XS.Pop(ECX);
                XS.Pop(EBX);
                XS.Pop(EAX);

                // now check if target turned out to be struct
                XS.Compare(EDI, 1);
                XS.Jump(x86.ConditionalTestEnum.Equal, ".Struct"); //structs are just the pointer so we are already done
                XS.Push(0);
                XS.Add(EDX, 4);
                XS.Jump(".NO_THIS");
                XS.Label(".Struct");
                XS.Add(ESP, ObjectUtils.FieldDataOffset, destinationIsIndirect: true);
                XS.Label(".NO_THIS");
                XS.Set(EDI, EAX, sourceIsIndirect: true, sourceDisplacement: 4);
                XS.Set(EDI, EDI, sourceDisplacement: Ldfld.GetFieldOffset(xMethodInfo.MethodBase.DeclaringType, "System.IntPtr System.Delegate._methodPtr"));

                XS.Set(EBX, 0); // initialise required extra space to 0
                XS.Compare(ESI, EDX);
                XS.Jump(x86.ConditionalTestEnum.LessThanOrEqualTo, ".NO_RETURN_VALUE_SPACE");
                XS.Set(EBX, ESI);
                XS.Sub(EBX, ECX);
                XS.Label(".NO_RETURN_VALUE_SPACE");

                XS.Comment("Check if delegate has args");
                XS.Compare(ECX, 0);
                XS.Jump(x86.ConditionalTestEnum.Zero, ".NO_ARGS");
                XS.Label(".HAS_ARGS");
                XS.Sub(ESP, ECX);
                XS.Push(EDI);
                XS.Set(EDI, ESP);
                XS.Add(EDI, 4);
                XS.Set(ESI, EBP);
                XS.Compare(EBX, 0);
                XS.Jump(x86.ConditionalTestEnum.Equal, ".NO_RETURN_EXTRA");
                XS.Add(ESI, EBX); // to skip the extra space reserved for the return value
                XS.Jump(".AFTER_ADJUST_ESI");
                XS.Label(".NO_RETURN_EXTRA");
                XS.Add(ESI, 8);
                XS.Label(".AFTER_ADJUST_ESI");
                new x86.Movs {
                    Size = 8, Prefixes = x86.InstructionPrefixes.Repeat
                };
                XS.Pop(EDI);
                XS.Label(".NO_ARGS");

                XS.Sub(ESP, EBX); // make extra space for the return value
                XS.Call(EDI);

                XS.Comment("If there is a return value copy it to holding place now");
                Ldarg.DoExecute(aAssembler, xMethodInfo, 0);
                XS.Add(ESP, 4);
                XS.Pop(EAX);
                XS.Add(EAX, (uint)returnSizeOffset);
                XS.Set(EAX, EAX, sourceIsIndirect: true); // got size of return value

                XS.Set(EDI, EBP);
                XS.Sub(EDI, EAX);
                XS.Label(".RETURN_VALUE_LOOP_START");
                XS.Compare(EAX, 0);
                XS.Jump(x86.ConditionalTestEnum.LessThanOrEqualTo, ".RETURN_VALUE_LOOP_END");
                XS.Pop(EBX);
                XS.Set(EDI, EBX, destinationIsIndirect: true);
                XS.Add(EDI, 4);
                XS.Sub(EAX, 4);
                XS.Jump(".RETURN_VALUE_LOOP_START");
                XS.Label(".RETURN_VALUE_LOOP_END");

                XS.PopAllRegisters();
                XS.Increment(EDX);
                XS.Jump(".BEGIN_OF_LOOP");
            }

            XS.Label(".END_OF_INVOKE");

            XS.EnableInterrupts();
        }
예제 #4
0
        /// <summary>
        /// <para>This method implements Multicast Invoke. This means that it should call all delegates
        /// in the current multicast delegate. </para>
        /// <para>The argument size is available in the <code>$$ArgSize$$</code> field. This value is already rounded to 4byte boundaries</para>
        /// </summary>
        /// <param name="aAssembler"></param>
        public override void AssembleNew(Assembler.Assembler aAssembler, object aMethodInfo)
        {
            if (aMethodInfo == null)
            {
                throw new Exception("This AssemblerMethod needs MethodInfo!");
            }

            /*
             * EAX contains the GetInvocationList() array at the index at which it was last used
             * EDX contains the index at which the EAX is
             * EBX contains the number of items in the array
             * ECX contains the argument size
             */
            var xMethodInfo = (MethodInfo)aMethodInfo;

            new Label("____DEBUG_FOR_MULTICAST___");
            //            new CPUx86.Cli();//DEBUG ONLY
            //#warning reenable interupts when issue is fixed!!!
            new Comment("move address of delgate to eax");
            new CPUx86.Mov
            {
                DestinationReg     = CPUx86.Registers.EAX,
                SourceReg          = CPUx86.Registers.EBP,
                SourceIsIndirect   = true,
                SourceDisplacement = Ldarg.GetArgumentDisplacement(xMethodInfo, 0)
            };
            var xGetInvocationListMethod = typeof(MulticastDelegate).GetMethod("GetInvocationList");

            new Comment("push address of delgate to stack");
            new CPUx86.Push {
                DestinationReg = CPUx86.Registers.EAX
            };                                                        //addrof this
            new CPUx86.Call {
                DestinationLabel = LabelName.Get(xGetInvocationListMethod)
            };
            new Comment("get address from return value -> eax");
            new CPUx86.Pop {
                DestinationReg = CPUx86.Registers.EAX
            };;                                                        //list
            new Comment("eax+=8 is where the offset where an array's count is");
            new CPUx86.Add {
                DestinationReg = CPUx86.Registers.EAX, SourceValue = 8
            };                                                                        //addrof list.count??
            new Comment("store count in ebx");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.EBX, SourceReg = CPUx86.Registers.EAX, SourceIsIndirect = true
            };                                                                                                                  //list.count
            new Comment("eax+=8 is where the offset where an array's items start");
            new CPUx86.Add {
                DestinationReg = CPUx86.Registers.EAX, SourceValue = 8
            };                                                                        //why? -- start of list i think? MtW: the array's .Length is at +8
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.EDI, SourceValue = 0
            };
            new Comment("ecx = ptr to delegate object");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.ECX, SourceReg = CPUx86.Registers.EBP, SourceIsIndirect = true, SourceDisplacement = Ldarg.GetArgumentDisplacement(xMethodInfo, 0)
            };                                                                                                                                                                                      //addrof the delegate
            new Comment("ecx points to the size of the delegated methods arguments");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.ECX, SourceReg = CPUx86.Registers.ECX, SourceIsIndirect = true, SourceDisplacement = (Ldfld.GetFieldOffset(xMethodInfo.MethodBase.DeclaringType, "$$ArgSize$$"))
            };                                                                                                                                                                                                                    //the size of the arguments to the method? + 12??? -- 12 is the size of the current call stack.. i think
            new CPUx86.Xor {
                DestinationReg = CPUx86.Registers.EDX, SourceReg = CPUx86.Registers.EDX
            };;                                                                                          //make sure edx is 0
            new Label(".BEGIN_OF_LOOP");
            new CPUx86.Compare {
                DestinationReg = CPUx86.Registers.EDX, SourceReg = CPUx86.Registers.EBX
            };                                                                                             //are we at the end of this list
            new CPUx86.ConditionalJump {
                Condition = CPUx86.ConditionalTestEnum.Equal, DestinationLabel = ".END_OF_INVOKE_"
            };                                                                                                                //then we better stop
                                                                                                                              //new CPUx86.Compare("edx", 0);
                                                                                                                              //new CPUx86.JumpIfLessOrEqual(".noreturnYet");
                                                                                                                              //new CPUx86.Add("esp", 4);
                                                                                                                              //new CPU.Label(".noreturnYet");
                                                                                                                              //new CPU.Comment("space for the return value");
                                                                                                                              //new CPUx86.Pushd("0");
            new CPUx86.Pushad();
            new Comment("esi points to where we will copy the methods argumetns from");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.ESI, SourceReg = CPUx86.Registers.ESP
            };
            new Comment("edi = ptr to delegate object");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.EDI, SourceReg = CPUx86.Registers.EBP, SourceIsIndirect = true, SourceDisplacement = Ldarg.GetArgumentDisplacement(xMethodInfo, 0)
            };
            new Comment("edi = ptr to delegate object should be a pointer to the delgates context ie (this) for the methods ");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.EDI, SourceReg = CPUx86.Registers.EDI, SourceIsIndirect = true, SourceDisplacement = (Ldfld.GetFieldOffset(xMethodInfo.MethodBase.DeclaringType, "System.Object System.Delegate._target"))
            };                                                                                                                                                                                                                                              //i really dont get the +12. MtW: +12 because of extra header of the type (object type, object id, field count)
            new CPUx86.Compare {
                DestinationReg = CPUx86.Registers.EDI, SourceValue = 0
            };
            new CPUx86.ConditionalJump {
                Condition = CPUx86.ConditionalTestEnum.Zero, DestinationLabel = ".NO_THIS"
            };
            new CPUx86.Push {
                DestinationReg = CPUx86.Registers.EDI
            };

            new Label(".NO_THIS");

            new Comment("make space for us to copy the arguments too");
            new CPUx86.Sub {
                DestinationReg = CPUx86.Registers.ESP, SourceReg = CPUx86.Registers.ECX
            };
            new Comment("move the current delegate to edi");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.EDI, SourceReg = CPUx86.Registers.EAX, SourceIsIndirect = true
            };
            new Comment("move the methodptr from that delegate to edi ");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.EDI, SourceReg = CPUx86.Registers.EDI, SourceIsIndirect = true, SourceDisplacement = Ldfld.GetFieldOffset(xMethodInfo.MethodBase.DeclaringType, "System.IntPtr System.Delegate._methodPtr")
            };                                                                                                                                                                                                                                               //
            new Comment("save methodptr on the stack");
            new CPUx86.Push {
                DestinationReg = CPUx86.Registers.EDI
            };
            new Comment("move location to copy args to");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.EDI, SourceReg = CPUx86.Registers.ESP
            };
            new Comment("get above the saved methodptr");
            new CPUx86.Add {
                DestinationReg = CPUx86.Registers.EDI, SourceValue = 4
            };
            //we allocated the argsize on the stack once, and it we need to get above the original args
            new Comment("we allocated argsize on the stack once");
            new Comment("add 32 for the Pushad + 16 for the current stack + 4 for the return value");
            new CPUx86.Add {
                DestinationReg = CPUx86.Registers.ESI, SourceValue = 52
            };
            new CPUx86.Movs {
                Size = 8, Prefixes = CPUx86.InstructionPrefixes.Repeat
            };
            new CPUx86.Pop {
                DestinationReg = CPUx86.Registers.EDI
            };
            new CPUx86.Call {
                DestinationReg = CPUx86.Registers.EDI
            };
            new Comment("store return -- return stored into edi after popad");
            //new CPUx86.Move("edx", "[" + MethodInfo.Arguments[0].VirtualAddresses[0] + "]");//addrof the delegate
            //new CPUx86.Move("edx", "[edx+" + (MethodInfo.Arguments[0].TypeInfo.Fields["$$ReturnsValue$$"].Offset + 12) + "]");
            //new CPUx86.Compare(Registers_Old.EDX, 0);
            //new CPUx86.JumpIfEqual(".getReturn");
            //new CPUx86.Move(Registers_Old.EAX, "[esp]");
            //new CPUx86.Move("[esp+0x20]", Registers_Old.EAX);
            //new CPU.Label(".getReturn");
            new Comment("edi = ptr to delegate object");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.EDI, SourceReg = CPUx86.Registers.EBP, SourceIsIndirect = true, SourceDisplacement = Cosmos.IL2CPU.X86.IL.Ldarg.GetArgumentDisplacement(xMethodInfo, 0)
            };
            new Comment("edi = ptr to delegate object should be a pointer to the delgates context ie (this) for the methods ");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.EDI, SourceReg = CPUx86.Registers.EDI, SourceIsIndirect = true, SourceDisplacement = Ldfld.GetFieldOffset(xMethodInfo.MethodBase.DeclaringType, "System.Object System.Delegate._target")
            };                                                                                                                                                                                                                                            //i really dont get the +12, MtW: that's for the object header
                                                                                                                                                                                                                                                          //new CPUx86.Compare("edi", "0");
                                                                                                                                                                                                                                                          //new CPUx86.JumpIfEqual(".noTHIStoPop");
                                                                                                                                                                                                                                                          //new CPUx86.Move("edx", "[" + MethodInfo.Arguments[0].VirtualAddresses[0] + "]");//addrof the delegate
                                                                                                                                                                                                                                                          //new CPUx86.Move("edx", "[edx+" + (MethodInfo.Arguments[0].TypeInfo.Fields["$$ReturnsValue$$"].Offset + 12) + "]");
                                                                                                                                                                                                                                                          //new CPUx86.Compare(Registers_Old.EDX, 0);
                                                                                                                                                                                                                                                          //new JumpIfNotEqual(".needToPopThis");
                                                                                                                                                                                                                                                          //new CPU.Comment("ecx = ptr to delegate object");
                                                                                                                                                                                                                                                          //new CPUx86.Move("ecx", "[" + MethodInfo.Arguments[0].VirtualAddresses[0] + "]");//addrof the delegate
                                                                                                                                                                                                                                                          //new CPU.Comment("ecx points to the size of the delegated methods arguments");
                                                                                                                                                                                                                                                          //new CPUx86.Move("ecx", "[ecx + " + (MethodInfo.Arguments[0].TypeInfo.Fields["$$ArgSize$$"].Offset + 12) + "]");//the size of the arguments to the method? + 12??? -- 12 is the size of the current call stack.. i think
                                                                                                                                                                                                                                                          //new CPUx86.Compare("ecx", "0");
                                                                                                                                                                                                                                                          //new CPUx86.JumpIfLessOrEqual(".noTHIStoPop");
                                                                                                                                                                                                                                                          //new CPU.Label(".needToPopThis");
                                                                                                                                                                                                                                                          //new CPUx86.Pop("edi");
                                                                                                                                                                                                                                                          //new CPUx86.Move("[esp]", "edi");
            new Label(".noTHIStoPop");
            new CPUx86.Popad();
            new CPUx86.INC {
                DestinationReg = CPUx86.Registers.EDX
            };
            new CPUx86.Add {
                DestinationReg = CPUx86.Registers.EAX, SourceValue = 4
            };
            new CPUx86.Jump {
                DestinationLabel = ".BEGIN_OF_LOOP"
            };
            new Label(".END_OF_INVOKE_");
            new Comment("get the return value");
            //new CPUx86.Pop("eax");
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.EDX, SourceReg = CPUx86.Registers.EBP, SourceIsIndirect = true, SourceDisplacement = Ldarg.GetArgumentDisplacement(xMethodInfo, 0)
            };                                                                                                                                                                                      //addrof the delegate
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.EDX, SourceReg = CPUx86.Registers.EDX, SourceIsIndirect = true, SourceDisplacement = Ldfld.GetFieldOffset(xMethodInfo.MethodBase.DeclaringType, "$$ReturnsValue$$")
            };
            new CPUx86.Compare {
                DestinationReg = CPUx86.Registers.EDX, SourceValue = 0
            };
            new CPUx86.ConditionalJump {
                Condition = CPUx86.ConditionalTestEnum.Equal, DestinationLabel = ".noReturn"
            };
            //may have to expand the return... idk
            new CPUx86.Xchg {
                DestinationReg = CPUx86.Registers.EBP, DestinationIsIndirect = true, DestinationDisplacement = 8, SourceReg = CPUx86.Registers.EDX
            };
            new CPUx86.Xchg {
                DestinationReg = CPUx86.Registers.EBP, DestinationIsIndirect = true, DestinationDisplacement = 4, SourceReg = CPUx86.Registers.EDX
            };
            new CPUx86.Xchg {
                DestinationReg = CPUx86.Registers.EBP, DestinationIsIndirect = true, SourceReg = CPUx86.Registers.EDX
            };
            new CPUx86.Push {
                DestinationReg = CPUx86.Registers.EDX
            };                                                        //ebp
            new CPUx86.Mov {
                DestinationReg = CPUx86.Registers.ESP, DestinationIsIndirect = true, DestinationDisplacement = 12, SourceReg = CPUx86.Registers.EDI
            };
            new Label(".noReturn");
            //            new CPUx86.Sti();
            //#warning remove this ^ sti call when issue is fixed!!!
            //MethodInfo.Arguments[0].
            //            new CPUx86.Move("ebx", "[eax + " + (MethodInfo.Arguments[0].TypeInfo.Fields["$$ArgSize$$"].Offset + 12) + "]");

            //new CPUx86.Move("eax", CPUx86.Registers_Old.
        }
예제 #5
0
        public override void AssembleNew(Cosmos.Assembler.Assembler aAssembler, object aMethodInfo)
        {
            var xAssembler        = (Cosmos.Assembler.Assembler)aAssembler;
            var xMethodInfo       = (Cosmos.IL2CPU.MethodInfo)aMethodInfo;
            var xMethodBaseAsInfo = xMethodInfo.MethodBase as global::System.Reflection.MethodInfo;

            if (xMethodBaseAsInfo.ReturnType != typeof(void))
            {
                throw new Exception("Events with return type not yet supported!");
            }
            XS.Comment("XXXXXXX");
            XS.Exchange(XSRegisters.BX, XSRegisters.BX);

            /*
             * EAX contains the GetInvocationList() array at the index at which it was last used
             * EDX contains the index at which the EAX is
             * EBX contains the number of items in the array
             * ECX contains the argument size
             */
            XS.ClearInterruptFlag();
            XS.Label(".DEBUG");
            //XS.Label("____DEBUG_FOR_MULTICAST___");
            XS.Comment("move address of delegate to eax");
            XS.Set(XSRegisters.EAX, XSRegisters.EBP, sourceDisplacement: Ldarg.GetArgumentDisplacement(xMethodInfo, 0));

            var xGetInvocationListMethod = typeof(MulticastDelegate).GetMethod("GetInvocationList");

            XS.Comment("push address of delgate to stack");
            XS.Push(XSRegisters.EAX);//addrof this
            XS.Call(LabelName.Get(xGetInvocationListMethod));
            XS.Comment("get address from return value -> eax");
            XS.Pop(XSRegisters.EAX);
            ;//list
            XS.Comment("eax+=8 is where the offset where an array's count is");
            XS.Set(XSRegisters.EAX, XSRegisters.EAX, sourceIsIndirect: true);
            XS.Add(XSRegisters.EAX, 8);                                       //addrof list.Length
            XS.Comment("store count in ebx");
            XS.Set(XSRegisters.EBX, XSRegisters.EAX, sourceIsIndirect: true); //list.count
            XS.Comment("eax+=8 is where the offset where an array's items start");
            XS.Add(XSRegisters.EAX, 8);                                       // Put pointer at the first item in the list.
            XS.Set(XSRegisters.EDI, 0);
            XS.Comment("ecx = ptr to delegate object");
            XS.Set(XSRegisters.ECX, XSRegisters.EBP, sourceDisplacement: Ldarg.GetArgumentDisplacement(xMethodInfo, 0));//addrof the delegate
            XS.Comment("ecx points to the size of the delegated methods arguments");
            XS.Set(XSRegisters.ECX, XSRegisters.ECX, sourceIsIndirect: true);
            XS.Set(XSRegisters.ECX, XSRegisters.ECX, sourceDisplacement: Ldfld.GetFieldOffset(xMethodInfo.MethodBase.DeclaringType, "$$ArgSize$$")); //the size of the arguments to the method? + 12??? -- 12 is the size of the current call stack.. i think
            XS.Xor(XSRegisters.EDX, XSRegisters.EDX);
            ;                                                                                                                                        //make sure edx is 0
            XS.Label(".BEGIN_OF_LOOP");
            {
                XS.Compare(XSRegisters.EDX, XSRegisters.EBX);                                //are we at the end of this list
                XS.Jump(CPUx86.ConditionalTestEnum.GreaterThanOrEqualTo, ".END_OF_INVOKE_"); //then we better stop
                XS.PushAllRegisters();
                XS.Comment("esi points to where we will copy the methods argumetns from");
                XS.Set(XSRegisters.ESI, XSRegisters.ESP);
                XS.Comment("edi = ptr to delegate object");
                XS.Set(XSRegisters.EDI, XSRegisters.EBP, sourceDisplacement: Ldarg.GetArgumentDisplacement(xMethodInfo, 0));
                XS.Set(XSRegisters.EDI, XSRegisters.EDI, sourceIsIndirect: true); // dereference handle
                XS.Comment("edi = ptr to delegate object should be a pointer to the delgates context ie (this) for the methods ");
                XS.Set(XSRegisters.EDI, XSRegisters.EDI, sourceDisplacement: Ldfld.GetFieldOffset(xMethodInfo.MethodBase.DeclaringType, "System.Object System.Delegate._target"));
                XS.Compare(XSRegisters.EDI, 0);
                XS.Jump(CPUx86.ConditionalTestEnum.Zero, ".NO_THIS");
                XS.Push(XSRegisters.EDI);

                XS.Label(".NO_THIS");

                XS.Comment("make space for us to copy the arguments too");
                XS.Sub(XSRegisters.ESP, XSRegisters.ECX);
                XS.Comment("move the current delegate to edi");
                XS.Set(XSRegisters.EDI, XSRegisters.EAX, sourceIsIndirect: true);
                XS.Set(XSRegisters.EDI, XSRegisters.EDI, sourceIsIndirect: true);                                                                                                     // dereference
                XS.Comment("move the methodptr from that delegate to edi ");
                XS.Set(XSRegisters.EDI, XSRegisters.EDI, sourceDisplacement: Ldfld.GetFieldOffset(xMethodInfo.MethodBase.DeclaringType, "System.IntPtr System.Delegate._methodPtr")); //
                XS.Comment("save methodptr on the stack");
                XS.Push(XSRegisters.EDI);
                XS.Comment("move location to copy args to");
                XS.Set(XSRegisters.EDI, XSRegisters.ESP);
                XS.Add(XSRegisters.EDI, 4);
                //new CPU.Comment("get above the saved methodptr");
                //XS.Sub(XSRegisters.ESP, 4);
                //we allocated the argsize on the stack once, and it we need to get above the original args
                XS.Comment("we allocated argsize on the stack once");
                XS.Comment("add 32 for the Pushad + 16 for the current stack + 4 for the return value");
                //uint xToAdd = 32; // skip pushad data
                //xToAdd += 4; // method pointer
                XS.Set(XSRegisters.ESI, XSRegisters.EBP);
                XS.Add(XSRegisters.ESI, 8); // ebp+8 is first argument
                new CPUx86.Movs {
                    Size = 8, Prefixes = CPUx86.InstructionPrefixes.Repeat
                };
                XS.Pop(XSRegisters.EDI);
                XS.Label(".BeforeCall");
                XS.Call(XSRegisters.EDI);
                XS.Comment("store return -- return stored into edi after popad");
                XS.Comment("edi = ptr to delegate object");
                XS.Set(XSRegisters.EDI, XSRegisters.EBP, sourceDisplacement: Ldarg.GetArgumentDisplacement(xMethodInfo, 0));
                XS.Comment("edi = ptr to delegate object should be a pointer to the delgates context ie (this) for the methods ");
                XS.Set(XSRegisters.EDI, XSRegisters.EDI, sourceIsIndirect: true);                                                                                                  // dereference handle
                XS.Set(XSRegisters.EDI, XSRegisters.EDI, sourceDisplacement: Ldfld.GetFieldOffset(xMethodInfo.MethodBase.DeclaringType, "System.Object System.Delegate._target")); //i really dont get the +12, MtW: that's for the object header
                XS.Label(".noTHIStoPop");
                XS.PopAllRegisters();
                XS.Increment(XSRegisters.EDX);
                XS.Add(XSRegisters.EAX, 4);
                XS.Jump(".BEGIN_OF_LOOP");
            }
            XS.Label(".END_OF_INVOKE_");
            XS.Comment("get the return value");
            XS.Set(XSRegisters.EDX, XSRegisters.EBP, sourceDisplacement: Ldarg.GetArgumentDisplacement(xMethodInfo, 0)); //addrof the delegate
            XS.Set(XSRegisters.EDX, XSRegisters.EDX, sourceIsIndirect: true);                                            // dereference handle
            XS.Set(XSRegisters.EDX, XSRegisters.EDX, sourceDisplacement: Ldfld.GetFieldOffset(xMethodInfo.MethodBase.DeclaringType, "$$ReturnsValue$$"));
            XS.Compare(XSRegisters.EDX, 0);
            XS.Jump(CPUx86.ConditionalTestEnum.Equal, ".noReturn");
            //may have to expand the return... idk
            XS.Exchange(XSRegisters.EBP, XSRegisters.EDX, destinationDisplacement: 8);
            XS.Exchange(XSRegisters.EBP, XSRegisters.EDX, destinationDisplacement: 4);
            XS.Exchange(XSRegisters.EBP, XSRegisters.EDX, destinationIsIndirect: true);
            XS.Push(XSRegisters.EDX);//ebp
            XS.Set(XSRegisters.ESP, XSRegisters.EDI, destinationDisplacement: 12);
            XS.Label(".noReturn");
            XS.EnableInterrupts();
        }