Пример #1
0
        protected void EliminateVariable(JSNode context, JSVariable variable, JSExpression replaceWith, QualifiedMemberIdentifier method)
        {
            {
                var replacer = new VariableEliminator(
                    variable,
                    JSChangeTypeExpression.New(replaceWith, variable.GetActualType(TypeSystem), TypeSystem)
                    );
                replacer.Visit(context);
            }

            {
                var replacer    = new VariableEliminator(variable, replaceWith);
                var assignments = (from a in FirstPass.Assignments where
                                   variable.Equals(a.NewValue) ||
                                   a.NewValue.SelfAndChildrenRecursive.Any(variable.Equals)
                                   select a).ToArray();

                foreach (var a in assignments)
                {
                    if (!variable.Equals(a.NewValue))
                    {
                        replacer.Visit(a.NewValue);
                    }
                }
            }

            Variables.Remove(variable.Identifier);
            FunctionSource.InvalidateFirstPass(method);
        }
Пример #2
0
        private bool SimplifyControlFlow()
        {
            if (Configuration.CodeGenerator.EliminateRedundantControlFlow.GetValueOrDefault(true))
            {
                bool shouldInvalidate = false;
                bool shouldCollapse   = false;
                bool shouldRun        = true;

                while (shouldRun)
                {
                    var cfs = new ControlFlowSimplifier();
                    cfs.Visit(Function);
                    shouldRun         = cfs.MadeChanges;
                    shouldCollapse   |= cfs.MadeChanges;
                    shouldInvalidate |= cfs.MadeChanges;
                }

                // HACK: Control flow simplification probably generated lots of nulls, so let's collapse them.
                // This makes it possible for loop simplification to work right later on.
                if (shouldCollapse)
                {
                    CollapseNulls();
                }

                if (shouldInvalidate)
                {
                    FunctionSource.InvalidateFirstPass(Identifier);
                }
            }

            return(true);
        }
Пример #3
0
        public void VisitNode(JSFunctionExpression fn)
        {
            Function  = fn;
            FirstPass = GetFirstPass(Function.Method.QualifiedIdentifier);

            VisitChildren(fn);

            if (EnumeratorsToKill.Count > 0)
            {
                // Rerun the static analyzer since we made major changes
                FunctionSource.InvalidateFirstPass(Function.Method.QualifiedIdentifier);
                FirstPass = GetFirstPass(Function.Method.QualifiedIdentifier);

                // Scan to see if any of the enumerators we eliminated uses of are now
                //  unreferenced. If they are, eliminate them entirely.
                foreach (var variable in EnumeratorsToKill)
                {
                    var variableName = variable.Name;
                    var accesses     = (
                        from a in FirstPass.Accesses
                        where a.Source.Name == variableName
                        select a
                        );

                    if (!accesses.Any())
                    {
                        var eliminator = new VariableEliminator(
                            variable, new JSNullExpression()
                            );
                        eliminator.Visit(fn);
                    }
                }
            }
        }