当前位置: 首页>>代码示例>>C#>>正文


C# Emit.ILEmitter类代码示例

本文整理汇总了C#中PHP.Core.Emit.ILEmitter的典型用法代码示例。如果您正苦于以下问题:C# ILEmitter类的具体用法?C# ILEmitter怎么用?C# ILEmitter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


ILEmitter类属于PHP.Core.Emit命名空间,在下文中一共展示了ILEmitter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: EmitAddFrame

		public static void EmitAddFrame(ILEmitter/*!*/ il, IPlace/*!*/ scriptContextPlace, int typeArgCount, int argCount,
		  Action<ILEmitter, int> typeArgEmitter, Action<ILEmitter, int>/*!*/ argEmitter)
		{
			Debug.Assert(typeArgCount == 0 || typeArgEmitter != null);

			// type args:
			if (typeArgCount > 0)
			{
				scriptContextPlace.EmitLoad(il);
				il.Emit(OpCodes.Ldfld, Fields.ScriptContext_Stack);

				il.EmitOverloadedArgs(Types.DTypeDesc[0], typeArgCount, Methods.PhpStack.AddTypeFrame.ExplicitOverloads, typeArgEmitter);
			}

			// args:
			scriptContextPlace.EmitLoad(il);
			il.Emit(OpCodes.Ldfld, Fields.ScriptContext_Stack);

			il.EmitOverloadedArgs(Types.Object[0], argCount, Methods.PhpStack.AddFrame.ExplicitOverloads, argEmitter);

			il.Emit(OpCodes.Call, Methods.PhpStack.AddFrame.Overload(argCount));

			// AddFrame adds empty type frame by default, so if there are no type parameters, we can skip AddTypeFrame call:
			if (typeArgCount > 0)
				il.Emit(OpCodes.Call, Methods.PhpStack.AddTypeFrame.Overload(typeArgCount));
		}
开发者ID:tiaohai,项目名称:Phalanger,代码行数:26,代码来源:PhpStackBuilder.cs

示例2: ClrStubBuilder

		public ClrStubBuilder(ILEmitter/*!*/ il, IPlace/*!*/ scriptContextPlace, int paramCount, int paramOffset)
		{
			this.il = il;
			this.scriptContextPlace = scriptContextPlace;
			this.paramOffset = paramOffset;

			this.referenceLocals = new LocalBuilder[paramCount];
		}
开发者ID:dw4dev,项目名称:Phalanger,代码行数:8,代码来源:ClrStubBuilder.cs

示例3: EmitLoad

		/// <summary>
		/// Emits code that loads current <see cref="PHP.Core.ScriptContext"/> by calling
		/// <see cref="PHP.Core.ScriptContext.CurrentContext"/> and remembers it in a local.
		/// </summary>
		/// <param name="il"></param>
		public void EmitLoad(ILEmitter il)
		{
			if (localBuilder == null)
			{
				localBuilder = il.DeclareLocal(typeof(ScriptContext));
				il.EmitCall(OpCodes.Call, Methods.ScriptContext.GetCurrentContext, null);
				il.Stloc(localBuilder);
			}
			il.Ldloc(localBuilder);
		}
开发者ID:Ashod,项目名称:Phalanger,代码行数:15,代码来源:Places.cs

示例4: Chain

			/// <summary>
			/// Initializes a new empty instance of the <see cref="Chain"/>.
			/// </summary>
			/// <param name="il">An IL emitter.</param>
			public Chain(ILEmitter il)
			{
				isChainMember = false;
				isArrayItem = false;
				IsLastChainMember = false;
				length = 0;
				errorLabelHasValue = false;
				this.il = il;
				this.QuietRead = false;
			}
开发者ID:hansdude,项目名称:Phalanger,代码行数:14,代码来源:Chains.cs

示例5: EmitArgFullPostCall

		public static void EmitArgFullPostCall(ILEmitter/*!*/ il, IPlace/*!*/ stack, LocalBuilder locArgsCount)
		{
			// args-aware:
			if (locArgsCount != null)
			{
				// CALL stack.RemoveArgsAwareFrame(count);
				stack.EmitLoad(il);
				il.Ldloc(locArgsCount);
				il.Emit(OpCodes.Call, Methods.PhpStack.RemoveArgsAwareFrame);
			}
		}
开发者ID:tiaohai,项目名称:Phalanger,代码行数:11,代码来源:PhpStackBuilder.cs

示例6: EmitReturnValueCopy

        /// <summary>
        /// Emit <see cref="PhpVariable.Copy"/> if needed. It means <see cref="Expression.Access"/> has to be <see cref="AccessType.Read"/> and <paramref name="returnType"/> has to be copiable.
        /// </summary>
        /// <param name="il">The <see cref="ILEmitter"/>.</param>
        /// <param name="returnType"><see cref="PhpTypeCode"/> of function call return value.</param>
        protected void EmitReturnValueCopy(ILEmitter/*!*/il, PhpTypeCode returnType)
        {
            Debug.Assert(il != null);

            // copy only if we are reading the return value &&
            // only if return type is copiable:
            if (access != AccessType.None &&   // reading, not literals:
                PhpTypeCodeEnum.IsDeeplyCopied(returnType) &&
                returnType != PhpTypeCode.PhpReference) // PhpSmartReference can be an issue if method returns an object field (but this is handled by binders)
            {
                il.LdcI4((int)CopyReason.ReturnedByCopy);
                il.Emit(OpCodes.Call, Methods.PhpVariable.Copy);
            }
        }
开发者ID:MpApQ,项目名称:Phalanger,代码行数:19,代码来源:FunctionCall.cs

示例7: EmitArgFullPreCall

		public static void EmitArgFullPreCall(ILEmitter/*!*/ il, IPlace/*!*/ stack, bool argsAware,
		  int formalParamCount, int formalTypeParamCount, out LocalBuilder locArgsCount)
		{
			if (argsAware)
			{
				locArgsCount = il.DeclareLocal(typeof(int));

				// locArgsCount = stack.MakeArgsAware(<formal tpye param count | formal param count>);
				stack.EmitLoad(il);
				il.LdcI4((formalTypeParamCount << 16) | formalParamCount);
				il.Emit(OpCodes.Call, Methods.PhpStack.MakeArgsAware);
				il.Stloc(locArgsCount);
			}
			else
			{
				locArgsCount = null;

				// CALL stack.RemoveFrame();
				stack.EmitLoad(il);
				il.Emit(OpCodes.Call, Methods.PhpStack.RemoveFrame);
			}
		}
开发者ID:tiaohai,项目名称:Phalanger,代码行数:22,代码来源:PhpStackBuilder.cs

示例8: DefineContextType

		public void DefineContextType()
		{
			linqContextBuilder = cg.IL.TypeBuilder.DefineNestedType(ContextTypeName + cg.IL.GetNextUniqueIndex(),
				TypeAttributes.Class | TypeAttributes.NestedPrivate | TypeAttributes.Sealed,
				typeof(PHP.Core.LinqContext), null);

			// .ctor:

			ConstructorBuilder ctor = linqContextBuilder.DefineConstructor(MethodAttributes.Assembly,
				CallingConventions.HasThis, Types.LinqContextArgs);

			ILEmitter il = new ILEmitter(ctor);
			il.Ldarg(0);
			il.Ldarg(1);
			il.Ldarg(2);
			il.Ldarg(3);
			il.Ldarg(4);
			il.Emit(OpCodes.Call, Constructors.LinqContext);
			il.Emit(OpCodes.Ret);

			linqContextCtor = ctor;
		}
开发者ID:jdluzen,项目名称:Phalanger,代码行数:22,代码来源:LinqBuilder.cs

示例9: EmitLoadInstance

        /// <summary>
        /// Emit LOAD <paramref name="instance"/>.
        /// </summary>ILEmiter
        /// <param name="il"><see cref="ILEmitter"/> object instance.</param>
        /// <param name="instance">The place where to load the instance from.</param>
        /// <param name="declaringType">The type of resulting instance.</param>
        /// <remarks>Instance of value types are wrapped in <see cref="ClrValue&lt;T&gt;"/> object instance.</remarks>
        internal static void EmitLoadInstance(ILEmitter/*!*/il, IPlace/*!*/instance, Type/*!*/declaringType)
        {
            Debug.Assert(il != null && instance != null && declaringType != null, "ClrOverloadBuilder.EmitLoadInstance() null argument!");

            // LOAD <instance>
            instance.EmitLoad(il);

            if (declaringType.IsValueType)
            {
                var clrValueType = ClrObject.valueTypesCache.Get(declaringType).Item1;
                Debug.Assert(clrValueType != null, "Specific ClrValue<T> not found!");

                // CAST (ClrValue<T>)
                il.Emit(OpCodes.Castclass, clrValueType);

                // LOAD .realValue
                var realValueField = clrValueType.GetField("realValue");
                Debug.Assert(realValueField != null, "ClrValue<T>.realValue field not found!");
                il.Emit(OpCodes.Ldflda, clrValueType.GetField("realValue"));
            }
            else
            {
                // CAST (T)
                il.Emit(OpCodes.Castclass, declaringType);
            }
        }
开发者ID:hansdude,项目名称:Phalanger,代码行数:33,代码来源:ClrOverloadBuilder.cs

示例10: EmitConvertToPhp

		/// <summary>
		/// Converts a value of the given CLR type to PHP value.
		/// </summary>
		internal static PhpTypeCode EmitConvertToPhp(ILEmitter/*!*/ il, Type/*!*/ type)
		{
			// box generic parameter
			if (type.IsGenericParameter)
			{
				il.Emit(OpCodes.Box, type);
				type = Types.Object[0];
			}

			switch (Type.GetTypeCode(type))
			{
				// primitives:
				case TypeCode.Boolean: return PhpTypeCode.Boolean;
				case TypeCode.Int32: return PhpTypeCode.Integer;
				case TypeCode.Int64: return PhpTypeCode.LongInteger;
				case TypeCode.Double: return PhpTypeCode.Double;
				case TypeCode.String: return PhpTypeCode.String;

				// coercion:
				case TypeCode.SByte:
				case TypeCode.Int16:
				case TypeCode.Byte:
				case TypeCode.UInt16:
					{
						il.Emit(OpCodes.Conv_I4);
						return PhpTypeCode.Integer;
					}

				case TypeCode.UInt32: EmitConstrainedCoercion(il, typeof(int), typeof(long), Int32.MaxValue); return PhpTypeCode.Object;
				case TypeCode.UInt64: EmitConstrainedCoercion(il, typeof(int), typeof(long), Int32.MaxValue); return PhpTypeCode.Object;

				case TypeCode.Single: il.Emit(OpCodes.Conv_R8); return PhpTypeCode.Double;
                case TypeCode.Char:
                    il.Emit(OpCodes.Box, type);
                    il.Emit(OpCodes.Callvirt, Methods.Object_ToString);
                    return PhpTypeCode.String;

				case TypeCode.DBNull:
					{
						il.Emit(OpCodes.Pop);
						il.Emit(OpCodes.Ldnull);
						return PhpTypeCode.Object;
					}

				case TypeCode.Decimal: // TODO: what to do with this guy?
				case TypeCode.DateTime:
					{
						il.Emit(OpCodes.Box, type);
						il.Emit(OpCodes.Call, Methods.ClrObject_Wrap);
						return PhpTypeCode.DObject;
					}

				case TypeCode.Object:
					{
						if (!typeof(IPhpVariable).IsAssignableFrom(type))
						{
							if (type.IsValueType)
								il.Emit(OpCodes.Box, type);

							il.Emit(OpCodes.Call, Methods.ClrObject_WrapDynamic);

							return PhpTypeCode.Object;
						}
						else return PhpTypeCodeEnum.FromType(type);
					}

				default:
					{
						Debug.Fail();
						return PhpTypeCode.Invalid;
					}
			}
		}
开发者ID:hansdude,项目名称:Phalanger,代码行数:76,代码来源:ClrOverloadBuilder.cs

示例11: EmitConstrainedCoercion

		internal static void EmitConstrainedCoercion(ILEmitter/*!*/ il, Type/*!*/ narrow, Type/*!*/ wide, object threshold)
		{
			Label else_label = il.DefineLabel();
			Label endif_label = il.DefineLabel();

			il.Emit(OpCodes.Dup);

			// IF (STACK <= threshold) THEN
			il.LoadLiteral(threshold);
			il.Emit(OpCodes.Bgt_S, else_label);

			// LOAD (narrow)STACK
			il.Conv(narrow, false);
			il.Emit(OpCodes.Box, narrow);

			il.Emit(OpCodes.Br_S, endif_label);

			// ELSE
			il.MarkLabel(else_label);

			// LOAD (wide)STACK

			il.Conv(wide, false);
			il.Emit(OpCodes.Box, wide);

			// ENDIF
			il.MarkLabel(endif_label);
		}
开发者ID:hansdude,项目名称:Phalanger,代码行数:28,代码来源:ClrOverloadBuilder.cs

示例12: EmitConvertObjectToClr

		/// <summary>
		/// Converts object to CLR type
		/// </summary>
		private static bool EmitConvertObjectToClr(ILEmitter il, PhpTypeCode typeCode, Type formalType, LocalBuilder strictnessLocal)
		{
			MethodInfo convert_method = null;
			switch (Type.GetTypeCode(formalType))
			{
				case TypeCode.Boolean: if (typeCode != PhpTypeCode.Boolean)
						convert_method = Methods.ConvertToClr.TryObjectToBoolean; break;
				case TypeCode.Int32: if (typeCode != PhpTypeCode.Integer)
						convert_method = Methods.ConvertToClr.TryObjectToInt32; break;
				case TypeCode.Int64: if (typeCode != PhpTypeCode.LongInteger)
						convert_method = Methods.ConvertToClr.TryObjectToInt64; break;
				case TypeCode.Double: if (typeCode != PhpTypeCode.Double)
						convert_method = Methods.ConvertToClr.TryObjectToDouble; break;
				case TypeCode.String: if (typeCode != PhpTypeCode.String)
						convert_method = Methods.ConvertToClr.TryObjectToString; break;

				case TypeCode.SByte: convert_method = Methods.ConvertToClr.TryObjectToInt8; break;
				case TypeCode.Int16: convert_method = Methods.ConvertToClr.TryObjectToInt16; break;
				case TypeCode.Byte: convert_method = Methods.ConvertToClr.TryObjectToUInt8; break;
				case TypeCode.UInt16: convert_method = Methods.ConvertToClr.TryObjectToUInt16; break;
				case TypeCode.UInt32: convert_method = Methods.ConvertToClr.TryObjectToUInt32; break;
				case TypeCode.UInt64: convert_method = Methods.ConvertToClr.TryObjectToUInt64; break;
				case TypeCode.Single: convert_method = Methods.ConvertToClr.TryObjectToSingle; break;
				case TypeCode.Decimal: convert_method = Methods.ConvertToClr.TryObjectToDecimal; break;
				case TypeCode.Char: convert_method = Methods.ConvertToClr.TryObjectToChar; break;
				case TypeCode.DateTime: convert_method = Methods.ConvertToClr.TryObjectToDateTime; break;
				case TypeCode.DBNull: convert_method = Methods.ConvertToClr.TryObjectToDBNull; break;

				case TypeCode.Object:
					{
						if (formalType.IsValueType)
						{
							if (formalType.IsGenericType && NullableType == formalType.GetGenericTypeDefinition())
							{
								// This is an ugly corner case (using generic TryObjectToStruct wouldn't work, because
								// for nullables .IsValueType returns true, but it doesn't match "T : struct" constraint)!
								// We have to try converting object to Nullable<T> first and then to T
								// (which requires a new call to 'EmitConvertObjectToClr') 
								Type nullableArg = formalType.GetGenericArguments()[0];
								Type nullableType = NullableType.MakeGenericType(nullableArg);
								
								LocalBuilder tmpVar = il.DeclareLocal(typeof(object));
								
								// This succeeds only for exact match
								il.Emit(OpCodes.Call, Methods.ConvertToClr.UnwrapNullable);
								il.Emit(OpCodes.Dup);
								il.Stloc(tmpVar);

								// <stack_0> = tmpVar = UnwrapNullable(...)
								// if (<stack_0> != null) 
								Label lblNull = il.DefineLabel(), lblDone = il.DefineLabel();
								il.Emit(OpCodes.Ldnull);
								il.Emit(OpCodes.Beq, lblNull);
								// {

								// Convert tmpVar to T and wrap it into Nullable<T>
								il.Ldloc(tmpVar);
								bool ret = EmitConvertObjectToClr(il, typeCode, nullableArg, strictnessLocal);
								// TODO: use reflection cache?
								il.Emit(OpCodes.Newobj, nullableType.GetConstructors()[0]);
								il.Emit(OpCodes.Br, lblDone);
								
								// } else /* == null */ {
								il.MarkLabel(lblNull);

								// return (T?)null;
								LocalBuilder tmpNull = il.DeclareLocal(nullableType);
								il.Ldloca(tmpNull);
								il.Emit(OpCodes.Initobj, nullableType);
								il.Ldloc(tmpNull);
								// }
								
								il.MarkLabel(lblDone);
								return ret;
							}
							else
								convert_method = Methods.ConvertToClr.TryObjectToStruct.MakeGenericMethod(formalType);
						}
						else
						{
							if (formalType.IsArray)
								convert_method = Methods.ConvertToClr.TryObjectToArray.MakeGenericMethod(formalType.GetElementType());
							else if (typeof(Delegate).IsAssignableFrom(formalType))
								convert_method = Methods.ConvertToClr.TryObjectToDelegate.MakeGenericMethod(formalType);
							else
								convert_method = Methods.ConvertToClr.TryObjectToClass.MakeGenericMethod(formalType);
						}
						break;
					}

				default:
					Debug.Fail();
					return true;
			}

			if (convert_method != null)
			{
//.........这里部分代码省略.........
开发者ID:hansdude,项目名称:Phalanger,代码行数:101,代码来源:ClrOverloadBuilder.cs

示例13: ClrOverloadBuilder

		public ClrOverloadBuilder(ILEmitter/*!*/ il, ClrMethod/*!*/ method, ConstructedType constructedType,
			IPlace/*!*/ stack, IPlace/*!*/ instance, bool emitParentCtorCall,
			ParameterLoader/*!*/ loadValueArg, ParameterLoader/*!*/ loadReferenceArg)
		{
			this.il = il;
			this.method = method;
			this.constructedType = constructedType;
			this.stack = stack;
			this.instance = instance;
			this.loadValueArg = loadValueArg;
			this.loadReferenceArg = loadReferenceArg;
			this.emitParentCtorCall = emitParentCtorCall;

			this.overloads = new List<Overload>(method.Overloads);
			SortOverloads(this.overloads);
		}
开发者ID:hansdude,项目名称:Phalanger,代码行数:16,代码来源:ClrOverloadBuilder.cs

示例14: EmitConvertToClr

		/// <summary>
		/// Converts a PHP value to the given CLR type (the caller is not interested in the success of the conversion).
		/// </summary>
		public static void EmitConvertToClr(ILEmitter/*!*/ il, PhpTypeCode typeCode, Type/*!*/ formalType)
		{
			EmitConvertToClr(il, typeCode, formalType, il.GetTemporaryLocal(typeof(ConversionStrictness), true));
		}
开发者ID:hansdude,项目名称:Phalanger,代码行数:7,代码来源:ClrOverloadBuilder.cs

示例15: ILEmitter

        /// <summary>
        /// Emit publically accessible stub that just calls argfull of <paramref name="function"/>.
        /// </summary>
        /// <returns><see cref="MethodInfo"/> of newly created function stub.</returns>
        private MethodInfo/*!*/EmitPhpFunctionPublicStub(ref TypeBuilder publicsContainer, PhpFunction/*!*/function)
        {
            Debug.Assert(function != null);
            Debug.Assert(function.ArgFullInfo != null, "!function.ArgFullInfo");

            if (publicsContainer == null)
            {
                publicsContainer = PureAssemblyBuilder.RealModuleBuilder.DefineType(
                    string.Format("{1}<{0}>",
                        StringUtils.ToClsCompliantIdentifier(Path.ChangeExtension(PureAssemblyBuilder.FileName, "")),
                        QualifiedName.Global.ToString()),
                    TypeAttributes.Public | TypeAttributes.Sealed | TypeAttributes.Class | TypeAttributes.SpecialName);
            }

            Type returnType;
            var parameterTypes = function.Signature.ToArgfullSignature(1, out returnType);
            parameterTypes[0] = Types.ScriptContext[0];

            var mi = publicsContainer.DefineMethod(function.GetFullName(), MethodAttributes.Public | MethodAttributes.Static, returnType, parameterTypes);
            var il = new ILEmitter(mi);

            // load arguments
            for (int i = 0; i < parameterTypes.Length; i++)
            {
                if (function.Builder != null)
                    mi.DefineParameter(i + 1, ParameterAttributes.None, function.Builder.ParameterBuilders[i].Name);

                il.Ldarg(i);
            }
            
            // call function.ArgFullInfo
            il.Emit(OpCodes.Call, function.ArgFullInfo);
            
            // .ret
            il.Emit(OpCodes.Ret);

            //
            return mi;
        }
开发者ID:dw4dev,项目名称:Phalanger,代码行数:43,代码来源:ModuleBuilders.CLR.cs


注:本文中的PHP.Core.Emit.ILEmitter类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。