Ejemplo n.º 1
0
            public override bool Rewrite(Meta.CodeDescriptor decompilee, SysDOM.Expression waitObject, Analysis.IDecompiler stack, SysDOM.IFunctionBuilder builder)
            {
                var evt    = waitObject.ResultType.GetSampleInstance();
                var sevent = evt as Event;

                if (sevent == null)
                {
                    // This workaround is for the following situation:
                    //   Signal s;
                    //   await s;
                    // This will actually await the "changed event" which is associated with the signal. However, decompilation
                    // will treat the signal instance as the awaited object and pass a Signal instance instead of the event.
                    // The code will try to restore the Event instance from the Awaiter.
                    Awaiter awaiter = null;
                    try
                    {
                        awaiter = ((object)AwaitableExtensionMethods.GetAwaiter((dynamic)evt)) as Awaiter;
                    }
                    catch
                    {
                    }
                    if (awaiter == null)
                    {
                        throw new InvalidOperationException("Unable to resolve awaited MultiEvent");
                    }
                    sevent = awaiter._event;
                }

                var signal    = (SignalBase)sevent.Owner;
                var signalRef = SignalRef.Create(signal, SignalRef.EReferencedProperty.Instance);
                var arg       = (LiteralReference)signalRef;

                var fspec = new FunctionSpec(typeof(void))
                {
                    IntrinsicRep = IntrinsicFunctions.Wait(WaitParams.EWaitKind.WaitOn)
                };

                builder.Call(fspec, arg);
                return(true);
            }
Ejemplo n.º 2
0
            public override bool Rewrite(Meta.CodeDescriptor decompilee, SysDOM.Expression waitObject, Analysis.IDecompiler stack, SysDOM.IFunctionBuilder builder)
            {
                var evt    = waitObject.ResultType.GetSampleInstance();
                var mevent = evt as MultiEvent;

                if (mevent == null)
                {
                    throw new InvalidOperationException("Unable to resolve awaited MultiEvent");
                }

                var events     = mevent._events.Cast <Event>();
                var signals    = events.Select(e => (SignalBase)e.Owner);
                var signalRefs = signals.Select(s => SignalRef.Create(s, SignalRef.EReferencedProperty.Instance));
                var args       = signalRefs.Select(sr => (LiteralReference)sr).ToArray();

                var fspec = new FunctionSpec(typeof(void))
                {
                    IntrinsicRep = IntrinsicFunctions.Wait(WaitParams.EWaitKind.WaitOn)
                };

                builder.Call(fspec, args);
                return(true);
            }
Ejemplo n.º 3
0
            public override bool Rewrite(Meta.CodeDescriptor decompilee, SysDOM.Expression waitObject, Analysis.IDecompiler stack, SysDOM.IFunctionBuilder builder)
            {
                var evt     = waitObject.ResultType.GetSampleInstance();
                var peevent = evt as PredicatedEvent;

                if (peevent == null)
                {
                    throw new InvalidOperationException("Unable to resolve awaited PredicatedEvent");
                }

                var pred = peevent._pred;
                var rwc  = pred.Method.GetCustomOrInjectedAttribute <RewriteCall>();

                if (rwc == null)
                {
                    throw new InvalidOperationException("Awaited predicate is not synthesizable.");
                }

                var lr   = LiteralReference.CreateConstant(pred.Target);
                var se   = new StackElement(lr, pred.Target, Analysis.Msil.EVariability.Constant);
                var pstk = stack.CreatePrivateStack();

                if (!rwc.Rewrite(decompilee, pred.Method, new StackElement[] { se }, pstk, builder))
                {
                    throw new InvalidOperationException("Unable to implement awaited predicate.");
                }

                var predEx = pstk.Pop();
                var fspec  = new FunctionSpec(typeof(void))
                {
                    IntrinsicRep = IntrinsicFunctions.Wait(WaitParams.EWaitKind.WaitUntil)
                };

                builder.Call(fspec, predEx.Expr);

                return(true);
            }