public unsafe void Load(DarksVMContext ctx, out ExecutionState state) { uint sp = ctx.Registers[DarksVMConstants.REG_SP].U4; DarksVMSlot fieldSlot = ctx.Stack[sp--]; DarksVMSlot valSlot = ctx.Stack[sp--]; DarksVMSlot objSlot = ctx.Stack[sp--]; var field = (FieldInfo)ctx.Instance.Data.LookupReference(fieldSlot.U4); if (!field.IsStatic && objSlot.O == null) { throw new NullReferenceException(); } object value = Type.GetTypeCode(field.FieldType) == TypeCode.String && valSlot.O == null ? ctx.Instance.Data.LookupString(valSlot.U4) : valSlot.ToObject(field.FieldType); if (field.DeclaringType.IsValueType && objSlot.O is IReference) { TypedReference typedRef; ((IReference)objSlot.O).ToTypedReference(ctx, &typedRef, field.DeclaringType); TypedReferenceHelpers.CastTypedRef(&typedRef, field.DeclaringType); field.SetValueDirect(typedRef, value); } else { field.SetValue(objSlot.ToObject(field.DeclaringType), value); } ctx.Stack.SetTopPosition(sp); ctx.Registers[DarksVMConstants.REG_SP].U4 = sp; state = ExecutionState.Next; }
public void Load(DarksVMContext ctx, out ExecutionState state) { var sp = ctx.Registers[DarksVMConstants.REG_SP].U4; var typeSlot = ctx.Stack[sp--]; var valSlot = ctx.Stack[sp]; var unboxPtr = (typeSlot.U4 & 0x80000000) != 0; var valType = (Type)ctx.Instance.Data.LookupReference(typeSlot.U4 & ~0x80000000); if (unboxPtr) { unsafe { TypedReference typedRef; TypedReferenceHelpers.UnboxTypedRef(valSlot.O, &typedRef); var reference = new TypedRef(typedRef); valSlot = DarksVMSlot.FromObject(valSlot.O, valType); ctx.Stack[sp] = valSlot; } } else { if (valType == typeof(object) && valSlot.O != null) { valType = valSlot.O.GetType(); } valSlot = DarksVMSlot.FromObject(valSlot.O, valType); ctx.Stack[sp] = valSlot; } ctx.Stack.SetTopPosition(sp); ctx.Registers[DarksVMConstants.REG_SP].U4 = sp; state = ExecutionState.Next; }
public unsafe void ToTypedReferenceObject(TypedRefPtr typedRef, Type type) { if (o is ValueType && type.IsValueType) { TypedReferenceHelpers.UnboxTypedRef(o, typedRef); } else { *(TypedReference *)typedRef = __makeref(o); } }
public void SetValue(DarksVMContext ctx, DarksVMSlot slot, PointerType type) { TypedReference typedRef; if (this._ptr != null) { *&typedRef = *(TypedReference *)this._ptr.Value; } else { *(PseudoTypedRef *)&typedRef = this._typedRef; } Type refType = __reftype(typedRef); object value = slot.ToObject(refType); TypedReferenceHelpers.SetTypedRef(value, &typedRef); }
public void ToTypedReference(uint pos, TypedRefPtr typedRef, Type type) { if(pos > topPos) throw new ExecutionEngineException(); var section = sections[(int) (pos >> SectionSize)]; var index = pos & IndexMask; if(type.IsEnum) type = Enum.GetUnderlyingType(type); if(type.IsPrimitive || type.IsPointer) { section[index].ToTypedReferencePrimitive(typedRef); TypedReferenceHelpers.CastTypedRef(typedRef, type); } else { section[index].ToTypedReferenceObject(typedRef, type); } }
public void SetValue(VMContext ctx, VMSlot slot, PointerType type) { TypedReference typedRef; if (_ptr != null) { *&typedRef = *(TypedReference *)_ptr.Value; } else { *(PseudoTypedRef *)&typedRef = _typedRef; } var refType = __reftype(typedRef); var value = slot.ToObject(refType); TypedReferenceHelpers.SetTypedRef(value, &typedRef); }
private void Run(ulong codeAddr, uint key, VMFuncSig sig, void *[] arguments, void *retTypedRef) { if (currentCtx != null) { ctxStack.Push(currentCtx); } currentCtx = new VMContext(this); try { Debug.Assert(sig.ParamTypes.Length == arguments.Length); currentCtx.Stack.SetTopPosition((uint)arguments.Length + 1); for (uint i = 0; i < arguments.Length; i++) { var paramType = sig.ParamTypes[i]; if (paramType.IsByRef) { currentCtx.Stack[i + 1] = new VMSlot { O = new TypedRef(arguments[i]) }; } else { var typedRef = *(TypedReference *)arguments[i]; currentCtx.Stack[i + 1] = VMSlot.FromObject(TypedReference.ToObject(typedRef), __reftype(typedRef)); } } currentCtx.Stack[(uint)arguments.Length + 1] = new VMSlot { U8 = 1 }; currentCtx.Registers[Constants.REG_K1] = new VMSlot { U4 = key }; currentCtx.Registers[Constants.REG_BP] = new VMSlot { U4 = 0 }; currentCtx.Registers[Constants.REG_SP] = new VMSlot { U4 = (uint)arguments.Length + 1 }; currentCtx.Registers[Constants.REG_IP] = new VMSlot { U8 = codeAddr }; VMDispatcher.Run(currentCtx); Debug.Assert(currentCtx.EHStack.Count == 0); if (sig.RetType != typeof(void)) { if (sig.RetType.IsByRef) { var retRef = currentCtx.Registers[Constants.REG_R0].O; if (!(retRef is IReference)) { throw new ExecutionEngineException(); } ((IReference)retRef).ToTypedReference(currentCtx, retTypedRef, sig.RetType.GetElementType()); } else { var retSlot = currentCtx.Registers[Constants.REG_R0]; object retVal; if (Type.GetTypeCode(sig.RetType) == TypeCode.String && retSlot.O == null) { retVal = Data.LookupString(retSlot.U4); } else { retVal = retSlot.ToObject(sig.RetType); } TypedReferenceHelpers.SetTypedRef(retVal, retTypedRef); } } } finally { currentCtx.Stack.FreeAllLocalloc(); if (ctxStack.Count > 0) { currentCtx = ctxStack.Pop(); } } }
public void ToTypedReference(VMContext ctx, TypedRefPtr typedRef, Type type) { TypedReferenceHelpers.MakeTypedRef(ptr, typedRef, type); }
public void ToTypedReference(DarksVMContext ctx, TypedRefPtr typedRef, Type type) { TypedReferenceHelpers.GetFieldAddr(ctx, this.instance, this.field, typedRef); }
public void ToTypedReference(VMContext ctx, TypedRefPtr typedRef, Type type) { TypedReferenceHelpers.GetFieldAddr(ctx, instance, field, typedRef); }