All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.royale.abc.optimize.PeepholeOptimizerMethodBodyVisitor Maven / Gradle / Ivy

There is a newer version: 0.9.10
Show newest version
/*
 *
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

package org.apache.royale.abc.optimize;

import static org.apache.royale.abc.ABCConstants.*;

import org.apache.royale.abc.instructionlist.InstructionList;
import org.apache.royale.abc.semantics.ECMASupport;
import org.apache.royale.abc.semantics.Instruction;
import org.apache.royale.abc.semantics.InstructionFactory;
import org.apache.royale.abc.semantics.Label;
import org.apache.royale.abc.visitors.DelegatingMethodBodyVisitor;
import org.apache.royale.abc.visitors.IMethodBodyVisitor;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * IMethodBodyVisitor that will do peephole optimization of the method body.
 * TODO: Not copmlete yet - should be equivalent to the peephole optimizer in
 * MXMLC 4.5 Optimizations implemented: Eliminate unnecessary convert_d
 * instructions Eliminate unnecessary convert_b instructions Eliminate
 * unnecessary convert_i instructions Eliminate unnecessary convert_s
 * instructions Eliminate unnecessary convert_u instructions Replace callprops
 * where the return value is unused, with callpropvoid
 */
public class PeepholeOptimizerMethodBodyVisitor extends DelegatingMethodBodyVisitor
{
    /**
     * How many instructions we can look back on
     */
    private static final int PEEPHOLE_WINDOW_SIZE = 4;

    /**
     * Constant that means we don't have any labels in the peephole window
     */
    private static final int NO_LABEL = -1;

    public PeepholeOptimizerMethodBodyVisitor(IMethodBodyVisitor delegate)
    {
        super(delegate);
    }

    /**
     * Hold a small window of previous instructions so that the optimizer can
     * rewind and rewrite small sections of ABC
     */
    private List instructions = new LinkedList();

    /**
     * The index of the instruction with the most recently seen label - we can't
     * look back past a label.
     */
    private int lastLabelSeen = NO_LABEL;

    /**
     * If we delete instructions that have labeling actions associated with
     * them, we have to apply those labeling instructions to whatever
     * instruction ends up replacing the deleted instruction(s). This list holds
     * those labels.
     */
    private List




© 2015 - 2024 Weber Informatics LLC | Privacy Policy