示例#1
0
        public static void EmitUnpackerEndReadSubtree(TracingILGenerator il, LocalBuilder subtreeUnpacker)
        {
            Contract.Requires(il != null);
            Contract.Requires(subtreeUnpacker != null);

            /*
             *	finally
             *	{
             *		if( subtreeUnpacker != null )
             *		{
             *			subtreeUnpacker.Dispose();
             *		}
             *	}
             */

            il.BeginFinallyBlock();
            il.EmitAnyLdloc(subtreeUnpacker);
            var endIf = il.DefineLabel("END_IF");

            il.EmitBrfalse_S(endIf);
            il.EmitAnyLdloc(subtreeUnpacker);
            il.EmitAnyCall(Metadata._IDisposable.Dispose);
            il.MarkLabel(endIf);
            il.EndExceptionBlock();
        }
示例#2
0
        public static void EmitUnpackerEndReadSubtree(TracingILGenerator il, LocalBuilder subtreeUnpacker)
        {
            Contract.Requires(il != null);
            Contract.Requires(subtreeUnpacker != null);
            il.BeginFinallyBlock();
            il.EmitAnyLdloc(subtreeUnpacker);
            Label target = il.DefineLabel("END_IF");

            il.EmitBrfalse_S(target);
            il.EmitAnyLdloc(subtreeUnpacker);
            il.EmitAnyCall(_IDisposable.Dispose);
            il.MarkLabel(target);
            il.EndExceptionBlock();
        }
        /// <summary>
        ///		Emits helper function to avoid lexical closure emitting.
        /// </summary>
        /// <param name="il"><see cref="TracingILGenerator"/>.</param>
        /// <param name="targetOperation"><see cref="MethodInfo"/> of the method to be invoked.</param>
        /// <param name="tupleTypes">The array of <see cref="Type"/> of nested tuples. The outermost is the first, innermost is the last.</param>
        /// <param name="itemTypes">The array of <see cref="Type"/> of flatten tuple items.</param>
        private static void EmitPrivateInvoke(TracingILGenerator il, MethodInfo targetOperation, IList <Type> tupleTypes, Type[] itemTypes)
        {
            /*
             * private void/T PrivateInvoke( object state )
             * {
             *		T result;
             *		Dispatcher.BeginOperation();
             *		try
             *		{
             *			var tuple = state as Tuple<...>;
             *			result =
             *				tuple.Item1.Target(
             *					tuple.Item2,
             *						:
             *					tuple.Rest.Rest....ItemN
             *				);
             *		}
             *		catch( TheradAbortException )
             *		{
             *			Dispatcher.HandleThreadAbortException( ex );
             *		}
             *		finally
             *		{
             *			Dispatcher.EndOperation();
             *		}
             *
             *		return result;
             * }
             */
            var tuple  = il.DeclareLocal(tupleTypes.First(), "tuple");
            var result = targetOperation.ReturnType == typeof(void) ? null : il.DeclareLocal(targetOperation.ReturnType, "result");

            il.EmitAnyLdarg(0);
            il.EmitCall(_dispatcherBeginOperationMethod);
            il.BeginExceptionBlock();

            il.EmitAnyLdarg(1);
            il.EmitIsinst(tupleTypes.First());
            il.EmitAnyStloc(tuple);

            int depth = -1;

            for (int i = 0; i < itemTypes.Length; i++)
            {
                if (i % 7 == 0)
                {
                    depth++;
                }

                il.EmitAnyLdloc(tuple);

                for (int j = 0; j < depth; j++)
                {
                    // .TRest.TRest ...
                    var rest = tupleTypes[j].GetProperty("Rest");
                    il.EmitGetProperty(rest);
                }

                var itemn = tupleTypes[depth].GetProperty("Item" + ((i % 7) + 1));
                il.EmitGetProperty(itemn);
            }

            il.EmitAnyCall(targetOperation);
            if (targetOperation.ReturnType != typeof(void))
            {
                il.EmitAnyStloc(result);
            }

            il.BeginCatchBlock(typeof(ThreadAbortException));
            var ex = il.DeclareLocal(typeof(ThreadAbortException), "ex");

            il.EmitAnyStloc(ex);
            il.EmitAnyLdarg(0);
            il.EmitAnyLdloc(ex);
            il.EmitCall(_dispatcherHandleThreadAbortExceptionMethod);

            il.BeginFinallyBlock();
            il.EmitAnyLdarg(0);
            il.EmitCall(_dispatcherEndOperationMethod);
            il.EndExceptionBlock();

            if (targetOperation.ReturnType != typeof(void))
            {
                il.EmitAnyLdloc(result);
            }

            il.EmitRet();
        }
示例#4
0
        public static void EmitForEach(TracingILGenerator il, CollectionTraits traits, LocalBuilder collection, Action <TracingILGenerator, Action> bodyEmitter)
        {
            Contract.Requires(il != null);
            Contract.Requires(collection != null);
            Contract.Requires(bodyEmitter != null);
            LocalBuilder enumerator = il.DeclareLocal(traits.GetEnumeratorMethod.ReturnType, "enumerator");

            if (collection.LocalType.IsValueType)
            {
                il.EmitAnyLdloca(collection);
            }
            else
            {
                il.EmitAnyLdloc(collection);
            }
            il.EmitAnyCall(traits.GetEnumeratorMethod);
            il.EmitAnyStloc(enumerator);
            if (typeof(IDisposable).IsAssignableFrom(traits.GetEnumeratorMethod.ReturnType))
            {
                il.BeginExceptionBlock();
            }
            Label label = il.DefineLabel("START_LOOP");

            il.MarkLabel(label);
            Label        target          = il.DefineLabel("END_LOOP");
            Type         returnType      = traits.GetEnumeratorMethod.ReturnType;
            MethodInfo   method          = returnType.GetMethod("MoveNext", Type.EmptyTypes);
            PropertyInfo currentProperty = traits.GetEnumeratorMethod.ReturnType.GetProperty("Current");

            if (method == null)
            {
                method = _IEnumerator.MoveNext;
            }
            if (currentProperty == null)
            {
                if (returnType == typeof(IDictionaryEnumerator))
                {
                    currentProperty = _IDictionaryEnumerator.Current;
                }
                else if (returnType.IsInterface)
                {
                    if (returnType.IsGenericType && (returnType.GetGenericTypeDefinition() == typeof(IEnumerator <>)))
                    {
                        currentProperty = typeof(IEnumerator <>).MakeGenericType(new Type[] { traits.ElementType }).GetProperty("Current");
                    }
                    else
                    {
                        currentProperty = _IEnumerator.Current;
                    }
                }
            }
            Contract.Assert(currentProperty != null, returnType.ToString());
            if (traits.GetEnumeratorMethod.ReturnType.IsValueType)
            {
                il.EmitAnyLdloca(enumerator);
            }
            else
            {
                il.EmitAnyLdloc(enumerator);
            }
            il.EmitAnyCall(method);
            il.EmitBrfalse(target);
            bodyEmitter(il, delegate {
                if (traits.GetEnumeratorMethod.ReturnType.IsValueType)
                {
                    il.EmitAnyLdloca(enumerator);
                }
                else
                {
                    il.EmitAnyLdloc(enumerator);
                }
                il.EmitGetProperty(currentProperty);
            });
            il.EmitBr(label);
            il.MarkLabel(target);
            if (typeof(IDisposable).IsAssignableFrom(traits.GetEnumeratorMethod.ReturnType))
            {
                il.BeginFinallyBlock();
                if (traits.GetEnumeratorMethod.ReturnType.IsValueType)
                {
                    MethodInfo info2 = traits.GetEnumeratorMethod.ReturnType.GetMethod("Dispose");
                    if (((info2 != null) && (info2.GetParameters().Length == 0)) && (info2.ReturnType == typeof(void)))
                    {
                        il.EmitAnyLdloca(enumerator);
                        il.EmitAnyCall(info2);
                    }
                    else
                    {
                        il.EmitAnyLdloc(enumerator);
                        il.EmitBox(traits.GetEnumeratorMethod.ReturnType);
                        il.EmitAnyCall(_IDisposable.Dispose);
                    }
                }
                else
                {
                    il.EmitAnyLdloc(enumerator);
                    il.EmitAnyCall(_IDisposable.Dispose);
                }
                il.EndExceptionBlock();
            }
        }
示例#5
0
        /// <summary>
        ///		Emits 'foreach' statement on the IL stream.
        /// </summary>
        /// <param name="il">IL generator to be emitted to.</param>
        /// <param name="traits"><see cref="CollectionTraits"/> which contains traits of the iterating collection.</param>
        /// <param name="collection">'collection' argument index.</param>
        /// <param name="bodyEmitter">Delegate to emit body statement.</param>
        public static void EmitForEach(TracingILGenerator il, CollectionTraits traits, LocalBuilder collection, Action <TracingILGenerator, Action> bodyEmitter)
        {
            Contract.Requires(il != null);
            Contract.Requires(collection != null);
            Contract.Requires(bodyEmitter != null);

            var enumerator = il.DeclareLocal(traits.GetEnumeratorMethod.ReturnType, "enumerator");

            // gets enumerator
            if (collection.LocalType.IsValueType)
            {
                il.EmitAnyLdloca(collection);
            }
            else
            {
                il.EmitAnyLdloc(collection);
            }

            il.EmitAnyCall(traits.GetEnumeratorMethod);
            il.EmitAnyStloc(enumerator);

            if (typeof(IDisposable).IsAssignableFrom(traits.GetEnumeratorMethod.ReturnType))
            {
                il.BeginExceptionBlock();
            }

            var startLoop = il.DefineLabel("START_LOOP");

            il.MarkLabel(startLoop);
            var          endLoop         = il.DefineLabel("END_LOOP");
            var          enumeratorType  = traits.GetEnumeratorMethod.ReturnType;
            MethodInfo   moveNextMethod  = enumeratorType.GetMethod("MoveNext", Type.EmptyTypes);
            PropertyInfo currentProperty = traits.GetEnumeratorMethod.ReturnType.GetProperty("Current");

            if (moveNextMethod == null)
            {
                moveNextMethod = Metadata._IEnumerator.MoveNext;
            }

            if (currentProperty == null)
            {
                if (enumeratorType == typeof(IDictionaryEnumerator))
                {
                    currentProperty = Metadata._IDictionaryEnumerator.Current;
                }
                else if (enumeratorType.IsInterface)
                {
                    if (enumeratorType.IsGenericType && enumeratorType.GetGenericTypeDefinition() == typeof(IEnumerator <>))
                    {
                        currentProperty = typeof(IEnumerator <>).MakeGenericType(traits.ElementType).GetProperty("Current");
                    }
                    else
                    {
                        currentProperty = Metadata._IEnumerator.Current;
                    }
                }
            }

            Contract.Assert(currentProperty != null, enumeratorType.ToString());

            // iterates
            if (traits.GetEnumeratorMethod.ReturnType.IsValueType)
            {
                il.EmitAnyLdloca(enumerator);
            }
            else
            {
                il.EmitAnyLdloc(enumerator);
            }

            il.EmitAnyCall(moveNextMethod);
            il.EmitBrfalse(endLoop);

            bodyEmitter(
                il,
                () =>
            {
                if (traits.GetEnumeratorMethod.ReturnType.IsValueType)
                {
                    il.EmitAnyLdloca(enumerator);
                }
                else
                {
                    il.EmitAnyLdloc(enumerator);
                }
                il.EmitGetProperty(currentProperty);
            }
                );

            il.EmitBr(startLoop);
            il.MarkLabel(endLoop);

            // Dispose
            if (typeof(IDisposable).IsAssignableFrom(traits.GetEnumeratorMethod.ReturnType))
            {
                il.BeginFinallyBlock();

                if (traits.GetEnumeratorMethod.ReturnType.IsValueType)
                {
                    var disposeMethod = traits.GetEnumeratorMethod.ReturnType.GetMethod("Dispose");
                    if (disposeMethod != null && disposeMethod.GetParameters().Length == 0 && disposeMethod.ReturnType == typeof(void))
                    {
                        il.EmitAnyLdloca(enumerator);
                        il.EmitAnyCall(disposeMethod);
                    }
                    else
                    {
                        il.EmitAnyLdloc(enumerator);
                        il.EmitBox(traits.GetEnumeratorMethod.ReturnType);
                        il.EmitAnyCall(Metadata._IDisposable.Dispose);
                    }
                }
                else
                {
                    il.EmitAnyLdloc(enumerator);
                    il.EmitAnyCall(Metadata._IDisposable.Dispose);
                }

                il.EndExceptionBlock();
            }
        }