public override void AssembleNew(Cosmos.Assembler.Assembler aAssembler, object aMethodInfo) { // method signature: $this, object @object, IntPtr method var xAssembler = aAssembler; var xMethodInfo = (_MethodInfo)aMethodInfo; XS.Comment("Save target ($this) to field"); XS.Comment("-- ldarg 0"); Ldarg.DoExecute(xAssembler, xMethodInfo, 0); XS.Comment("-- ldarg 1"); Ldarg.DoExecute(xAssembler, xMethodInfo, 1); XS.Comment("-- stfld _target"); Stfld.DoExecute(xAssembler, xMethodInfo, "System.Object System.Delegate._target", xMethodInfo.MethodBase.DeclaringType, true, false); XS.Comment("Save method pointer to field"); XS.Comment("-- ldarg 0"); Ldarg.DoExecute(xAssembler, xMethodInfo, 0); XS.Comment("-- ldarg 2"); Ldarg.DoExecute(xAssembler, xMethodInfo, 2); XS.Comment("-- stfld _methodPtr"); Stfld.DoExecute(xAssembler, xMethodInfo, "System.IntPtr System.Delegate._methodPtr", xMethodInfo.MethodBase.DeclaringType, true, false); XS.Comment("Saving ArgSize to field"); uint xSize = 0; foreach (var xArg in xMethodInfo.MethodBase.DeclaringType.GetMethod("Invoke").GetParameters()) { xSize += ILOp.Align(ILOp.SizeOfType(xArg.ParameterType), 4); } XS.Comment("-- ldarg 0"); Ldarg.DoExecute(xAssembler, xMethodInfo, 0); XS.Comment("-- push argsize"); XS.Push(xSize); XS.Comment("-- stfld ArgSize"); Stfld.DoExecute(xAssembler, xMethodInfo, "$$ArgSize$$", xMethodInfo.MethodBase.DeclaringType, true, false); }
private static void RunTest(int aIndex, int aExpected, Type aDeclaringType, MethodInfo aMethod) { var xReturnType = aMethod.ReturnType; var xParameterTypes = aMethod.GetParameters().Select(p => p.ParameterType).ToArray(); bool xIsStatic = aMethod.IsStatic; int xActual = Ldarg.GetArgumentDisplacement((ushort)aIndex, aDeclaringType, xParameterTypes, xReturnType, xIsStatic); Assert.AreEqual(aExpected, xActual); }
public override void AssembleNew(Cosmos.Assembler.Assembler aAssembler, object aMethodInfo) { // method signature: $this, object @object, IntPtr method var xMethodInfo = (MethodInfo)aMethodInfo; var xAssembler = (NewAssembler)aAssembler; new Comment("Save target ($this) to field"); new Comment("-- ldarg 0"); Ldarg.DoExecute(xAssembler, xMethodInfo, 0); //Ldarg.DoExecute(xAssembler, xMethodInfo, 0); new Comment("-- ldarg 1"); Ldarg.DoExecute(xAssembler, xMethodInfo, 1); new Comment("-- stfld _target"); Stfld.DoExecute(xAssembler, xMethodInfo, "System.Object System.Delegate._target", xMethodInfo.MethodBase.DeclaringType, true, false); new Comment("Save method pointer to field"); //Ldarg.DoExecute(xAssembler, xMethodInfo, 0); new Comment("-- ldarg 0"); Ldarg.DoExecute(xAssembler, xMethodInfo, 0); new Comment("-- ldarg 2"); Ldarg.DoExecute(xAssembler, xMethodInfo, 2); new Comment("-- stfld _methodPtr"); Stfld.DoExecute(xAssembler, xMethodInfo, "System.IntPtr System.Delegate._methodPtr", xMethodInfo.MethodBase.DeclaringType, true, false); new Comment("Saving ArgSize to field"); uint xSize = 0; foreach (var xArg in xMethodInfo.MethodBase.DeclaringType.GetMethod("Invoke").GetParameters()) { xSize += ILOp.Align(ILOp.SizeOfType(xArg.ParameterType), 4); } new Comment("-- ldarg 0"); Ldarg.DoExecute(xAssembler, xMethodInfo, 0); if (xMethodInfo.MethodBase.DeclaringType.FullName.Contains("InterruptDelegate")) { Console.Write(""); } new Comment("-- push argsize"); new CPUx86.Push { DestinationValue = xSize }; new Comment("-- stfld ArgSize"); Stfld.DoExecute(xAssembler, xMethodInfo, "$$ArgSize$$", xMethodInfo.MethodBase.DeclaringType, true, false); //public static void Ctor(Delegate aThis, object aObject, IntPtr aMethod, //[FieldAccess(Name = "System.Object System.Delegate._target")] ref object aFldTarget, //[FieldAccess(Name = "System.IntPtr System.Delegate._methodPtr")] ref IntPtr aFldMethod) { }
private int EmulateInstruction(Instruction instruction, int i) { switch (instruction.OpCode.Code) { case Code.UNKNOWN1: throw new NotSupportedException(); case Code.UNKNOWN2: throw new NotSupportedException(); case Code.Add: Add.Emulate(ValueStack); break; case Code.Add_Ovf: case Code.Add_Ovf_Un: Add.Emulate_Ovf(ValueStack); break; case Code.And: And.Emulate(ValueStack); break; case Code.Arglist: throw new NotSupportedException(); case Code.Beq: case Code.Beq_S: var beqResult = Beq.Emulate(ValueStack, instruction, _instructionsToEmulate); return(beqResult == -1 ? i : beqResult); case Code.Bge: case Code.Bge_S: case Code.Bge_Un: case Code.Bge_Un_S: var bgeResult = Bge.Emulate(ValueStack, instruction, _instructionsToEmulate); return(bgeResult == -1 ? i : bgeResult); case Code.Bgt: case Code.Bgt_S: case Code.Bgt_Un: case Code.Bgt_Un_S: var bgtResult = Bgt.Emulate(ValueStack, instruction, _instructionsToEmulate); return(bgtResult == -1 ? i : bgtResult); case Code.Ble: case Code.Ble_S: case Code.Ble_Un: case Code.Ble_Un_S: var bleResult = Ble.Emulate(ValueStack, instruction, _instructionsToEmulate); return(bleResult == -1 ? i : bleResult); case Code.Blt: case Code.Blt_S: case Code.Blt_Un: case Code.Blt_Un_S: var bltResult = Blt.Emulate(ValueStack, instruction, _instructionsToEmulate); return(bltResult == -1 ? i : bltResult); case Code.Bne_Un: case Code.Bne_Un_S: var bneResult = Bne.Emulate(ValueStack, instruction, _instructionsToEmulate); return(bneResult == -1 ? i : bneResult); case Code.Box: Box.Emulate_Box(ValueStack, instruction); break; case Code.Br: case Code.Br_S: return(Br.Emulate(ValueStack, instruction, _instructionsToEmulate)); case Code.Break: break; case Code.Brfalse: case Code.Brfalse_S: var bFalseResult = BrFalse.Emulate(ValueStack, instruction, _instructionsToEmulate); return(bFalseResult == -1 ? i : bFalseResult); case Code.Brtrue: case Code.Brtrue_S: var bTrueResult = BrTrue.Emulate(ValueStack, instruction, _instructionsToEmulate); return(bTrueResult == -1 ? i : bTrueResult); case Code.Call: Call.Emulate(ValueStack, instruction, _method); break; case Code.Calli: throw new NotSupportedException(); case Code.Callvirt: CallVirt.Emulate(ValueStack, instruction, _method); break; case Code.Castclass: CastClass.Emulate(ValueStack, instruction); break; case Code.Ceq: Ceq.Emulate(ValueStack); break; case Code.Cgt: case Code.Cgt_Un: Ceq.Emulate(ValueStack); break; case Code.Ckfinite: throw new NotSupportedException(); case Code.Clt: case Code.Clt_Un: Clt.Emulate(ValueStack); break; case Code.Constrained: throw new NotSupportedException(); case Code.Conv_I: ConvI.Emulation(ValueStack); break; case Code.Conv_I1: throw new NotSupportedException(); case Code.Conv_I2: ConvI2.Emulation(ValueStack); break; case Code.Conv_I4: ConvI4.Emulation(ValueStack); break; case Code.Conv_I8: ConvI8.Emulation(ValueStack); break; case Code.Conv_Ovf_I: throw new NotSupportedException(); case Code.Conv_Ovf_I_Un: throw new NotSupportedException(); case Code.Conv_Ovf_I1: throw new NotSupportedException(); case Code.Conv_Ovf_I1_Un: throw new NotSupportedException(); case Code.Conv_Ovf_I2: throw new NotSupportedException(); case Code.Conv_Ovf_I2_Un: throw new NotSupportedException(); case Code.Conv_Ovf_I4: throw new NotSupportedException(); case Code.Conv_Ovf_I4_Un: throw new NotSupportedException(); case Code.Conv_Ovf_I8: throw new NotSupportedException(); case Code.Conv_Ovf_I8_Un: throw new NotSupportedException(); case Code.Conv_Ovf_U: throw new NotSupportedException(); case Code.Conv_Ovf_U_Un: throw new NotSupportedException(); case Code.Conv_Ovf_U1: throw new NotSupportedException(); case Code.Conv_Ovf_U1_Un: throw new NotSupportedException(); case Code.Conv_Ovf_U2: throw new NotSupportedException(); case Code.Conv_Ovf_U2_Un: throw new NotSupportedException(); case Code.Conv_Ovf_U4: throw new NotSupportedException(); case Code.Conv_Ovf_U4_Un: throw new NotSupportedException(); case Code.Conv_Ovf_U8: throw new NotSupportedException(); case Code.Conv_Ovf_U8_Un: throw new NotSupportedException(); case Code.Conv_R_Un: throw new NotSupportedException(); case Code.Conv_R4: throw new NotSupportedException(); case Code.Conv_R8: throw new NotSupportedException(); case Code.Conv_U: ConvI.UEmulation(ValueStack); break; case Code.Conv_U1: ConvI1.UEmulation(ValueStack); break; case Code.Conv_U2: ConvI2.UEmulation(ValueStack); break; case Code.Conv_U4: ConvI4.UEmulation(ValueStack); break; case Code.Conv_U8: ConvI8.UEmulation(ValueStack); break; case Code.Cpblk: throw new NotSupportedException(); case Code.Cpobj: throw new NotSupportedException(); case Code.Div: Div.Emulate(ValueStack); break; case Code.Div_Un: Div.Emulate_Un(ValueStack); break; case Code.Dup: ValueStack.CallStack.Push(ValueStack.CallStack.Peek()); break; case Code.Endfilter: throw new NotSupportedException(); case Code.Endfinally: throw new NotSupportedException(); case Code.Initblk: throw new NotSupportedException(); case Code.Initobj: ValueStack.CallStack.Pop(); break; case Code.Isinst: IsInst.Emulate_Box(ValueStack, instruction); break; case Code.Jmp: throw new NotSupportedException(); case Code.Ldarg: case Code.Ldarg_0: case Code.Ldarg_1: case Code.Ldarg_2: case Code.Ldarg_3: case Code.Ldarg_S: Ldarg.Emulate(ValueStack, instruction, _method); break; case Code.Ldarga: throw new NotSupportedException(); case Code.Ldarga_S: throw new NotSupportedException(); case Code.Ldc_I4: case Code.Ldc_I4_0: case Code.Ldc_I4_1: case Code.Ldc_I4_2: case Code.Ldc_I4_3: case Code.Ldc_I4_4: case Code.Ldc_I4_5: case Code.Ldc_I4_6: case Code.Ldc_I4_7: case Code.Ldc_I4_8: ValueStack.CallStack.Push(instruction.GetLdcI4Value()); break; case Code.Ldc_I4_M1: ValueStack.CallStack.Push(-1); break; case Code.Ldc_I4_S: ValueStack.CallStack.Push((sbyte)instruction.GetLdcI4Value()); break; case Code.Ldc_I8: ValueStack.CallStack.Push((long)instruction.Operand); break; case Code.Ldc_R4: ValueStack.CallStack.Push((float)instruction.GetOperand()); break; case Code.Ldc_R8: ValueStack.CallStack.Push((double)instruction.GetOperand()); break; case Code.Ldelem: throw new NotSupportedException(); case Code.Ldelem_I: throw new NotSupportedException(); case Code.Ldelem_I1: throw new NotSupportedException(); case Code.Ldelem_I2: throw new NotSupportedException(); case Code.Ldelem_I4: throw new NotSupportedException(); case Code.Ldelem_I8: throw new NotSupportedException(); case Code.Ldelem_R4: throw new NotSupportedException(); case Code.Ldelem_R8: throw new NotSupportedException(); case Code.Ldelem_Ref: LdelemRef.Emulate(ValueStack); break; case Code.Ldelem_U1: LdelemI1.UEmulate(ValueStack); break; case Code.Ldelem_U2: throw new NotSupportedException(); case Code.Ldelem_U4: LdelemI4.UEmulate(ValueStack); break; case Code.Ldelema: Ldelema.Emulate(ValueStack); break; case Code.Ldfld: throw new NotSupportedException(); case Code.Ldflda: throw new NotSupportedException(); case Code.Ldftn: throw new NotSupportedException(); case Code.Ldind_I: throw new NotSupportedException(); case Code.Ldind_I1: throw new NotSupportedException(); case Code.Ldind_I2: throw new NotSupportedException(); case Code.Ldind_I4: LdindI4.Emulate(ValueStack); break; case Code.Ldind_I8: throw new NotSupportedException(); case Code.Ldind_R4: throw new NotSupportedException(); case Code.Ldind_R8: throw new NotSupportedException(); case Code.Ldind_Ref: throw new NotSupportedException(); case Code.Ldind_U1: LdindI1.UEmulate(ValueStack); break; case Code.Ldind_U2: LdindI2.UEmulate(ValueStack); break; case Code.Ldind_U4: LdindI4.UEmulate(ValueStack); break; case Code.Ldlen: Ldlen.Emulate(ValueStack); break; case Code.Ldloc: case Code.Ldloc_0: case Code.Ldloc_1: case Code.Ldloc_2: case Code.Ldloc_3: case Code.Ldloc_S: Ldloc.Emulate(ValueStack, instruction, _method); break; case Code.Ldloca: case Code.Ldloca_S: Ldloc.EmulateLdloca(ValueStack, instruction, _method); break; case Code.Ldnull: ValueStack.CallStack.Push(null); break; case Code.Ldobj: Ldobj.Emulate(ValueStack, instruction); break; case Code.Ldsfld: Ldsfld.Emulate(ValueStack, instruction); break; case Code.Ldsflda: throw new NotSupportedException(); case Code.Ldstr: ValueStack.CallStack.Push(instruction.Operand.ToString()); break; case Code.Ldtoken: Ldtoken.Emulate(ValueStack, instruction); break; case Code.Ldvirtftn: throw new NotSupportedException(); case Code.Leave: case Code.Leave_S: return(LEave.Emulate(ValueStack, instruction, _instructionsToEmulate)); case Code.Localloc: Localloc.Emulate(ValueStack); break; case Code.Mkrefany: throw new NotSupportedException(); case Code.Mul: case Code.Mul_Ovf: case Code.Mul_Ovf_Un: Mul.Emulate(ValueStack); break; case Code.Neg: ValueStack.CallStack.Push(-ValueStack.CallStack.Pop()); break; case Code.Newarr: NewArr.Emulate(ValueStack, instruction); break; case Code.Newobj: throw new NotSupportedException(); case Code.Nop: break; case Code.Not: Not.Emulate(ValueStack); break; case Code.Or: Or.Emulate(ValueStack); break; case Code.Pop: ValueStack.CallStack.Pop(); break; case Code.Prefix1: throw new NotSupportedException(); case Code.Prefix2: throw new NotSupportedException(); case Code.Prefix3: throw new NotSupportedException(); case Code.Prefix4: throw new NotSupportedException(); case Code.Prefix5: throw new NotSupportedException(); case Code.Prefix6: throw new NotSupportedException(); case Code.Prefix7: throw new NotSupportedException(); case Code.Prefixref: throw new NotSupportedException(); case Code.Readonly: throw new NotSupportedException(); case Code.Refanytype: throw new NotSupportedException(); case Code.Refanyval: throw new NotSupportedException(); case Code.Rem: Rem.Emulate(ValueStack); break; case Code.Rem_Un: Rem.Emulate_Un(ValueStack); break; case Code.Ret: break; case Code.Rethrow: throw new NotSupportedException(); case Code.Shl: Shl.Emulate(ValueStack); break; case Code.Shr: Shr.Emulate(ValueStack); break; case Code.Shr_Un: Shr.Emulate_Un(ValueStack); break; case Code.Sizeof: throw new NotSupportedException(); case Code.Starg: case Code.Starg_S: Starg.Emulate(ValueStack, instruction, _method); break; case Code.Stelem: throw new NotSupportedException(); case Code.Stelem_I: throw new NotSupportedException(); case Code.Stelem_I1: StelemI1.Emulate(ValueStack); break; case Code.Stelem_I2: break; case Code.Stelem_I4: Stelem_I4.Emulate(ValueStack); break; case Code.Stelem_I8: throw new NotSupportedException(); case Code.Stelem_R4: throw new NotSupportedException(); case Code.Stelem_R8: throw new NotSupportedException(); case Code.Stelem_Ref: throw new NotSupportedException(); case Code.Stfld: throw new NotSupportedException(); case Code.Stind_I: throw new NotSupportedException(); case Code.Stind_I1: StindI1.Emulate(ValueStack); break; case Code.Stind_I2: throw new NotSupportedException(); case Code.Stind_I4: StindI4.Emulate(ValueStack); break; case Code.Stind_I8: throw new NotSupportedException(); case Code.Stind_R4: throw new NotSupportedException(); case Code.Stind_R8: throw new NotSupportedException(); case Code.Stind_Ref: throw new NotSupportedException(); case Code.Stloc: case Code.Stloc_0: case Code.Stloc_1: case Code.Stloc_2: case Code.Stloc_3: case Code.Stloc_S: Stloc.Emulate(ValueStack, instruction, _method); break; case Code.Stobj: Stobj.Emulate(ValueStack, instruction); break; case Code.Stsfld: Stsfld.Emulate(ValueStack, instruction); break; case Code.Sub: Sub.Emulate(ValueStack); break; case Code.Sub_Ovf: case Code.Sub_Ovf_Un: Sub.Emulate_Ovf(ValueStack); break; case Code.Switch: var switchResult = Switch.Emulate(ValueStack, instruction, _instructionsToEmulate); return(switchResult == -1 ? i : switchResult); case Code.Tailcall: throw new NotSupportedException(); case Code.Throw: throw new NotSupportedException(); case Code.Unaligned: throw new NotSupportedException(); case Code.Unbox: throw new NotSupportedException(); case Code.Unbox_Any: // Box.Emulate_UnBox_Any(ValueStack,instruction); break; case Code.Volatile: throw new NotSupportedException(); case Code.Xor: Xor.Emulate(ValueStack); break; default: throw new ArgumentOutOfRangeException(); } return(i); }
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(); }
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(); }
/// <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. }
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(); }
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(); }