001/*
002 * Copyright (c) 2011, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.lir.sparc;
024
025import com.oracle.graal.asm.sparc.*;
026import com.oracle.graal.lir.*;
027import com.oracle.graal.lir.asm.*;
028
029/**
030 * Convenience class to provide SPARCMacroAssembler for the {@link #emitCode} method.
031 */
032public abstract class SPARCLIRInstruction extends LIRInstruction {
033    public static final LIRInstructionClass<SPARCLIRInstruction> TYPE = LIRInstructionClass.create(SPARCLIRInstruction.class);
034    private final SizeEstimate size;
035
036    protected SPARCLIRInstruction(LIRInstructionClass<? extends LIRInstruction> c) {
037        this(c, null);
038    }
039
040    protected SPARCLIRInstruction(LIRInstructionClass<? extends LIRInstruction> c, SizeEstimate size) {
041        super(c);
042        this.size = size;
043    }
044
045    protected SPARCDelayedControlTransfer delayedControlTransfer = SPARCDelayedControlTransfer.DUMMY;
046
047    @Override
048    public final void emitCode(CompilationResultBuilder crb) {
049        emitCode(crb, (SPARCMacroAssembler) crb.asm);
050    }
051
052    public abstract void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm);
053
054    public boolean leavesRegisterWindow() {
055        return false;
056    }
057
058    public void setDelayedControlTransfer(SPARCDelayedControlTransfer holder) {
059        this.delayedControlTransfer = holder;
060    }
061
062    public SizeEstimate estimateSize() {
063        return size;
064    }
065
066    /**
067     * This class represents a size estimation of a particular LIR instruction. It contains a
068     * pessimistic estimate of emitted SPARC instructions and emitted bytes into the constant
069     * section.
070     */
071    public static class SizeEstimate {
072        /**
073         * Cache the first size definition (with just 0 as constant size).
074         */
075        private static final SizeEstimate[] cache = new SizeEstimate[5];
076        static {
077            for (int i = 0; i < cache.length; i++) {
078                cache[i] = new SizeEstimate(i, 0);
079            }
080        }
081        public final int instructionSize;
082        public final int constantSize;
083
084        public SizeEstimate(int instructionSize, int constantSize) {
085            this.instructionSize = instructionSize;
086            this.constantSize = constantSize;
087        }
088
089        public static SizeEstimate create(int instructionSize, int constantSize) {
090            if (constantSize == 0 && instructionSize < cache.length) {
091                return cache[instructionSize];
092            } else {
093                return new SizeEstimate(instructionSize, constantSize);
094            }
095        }
096
097        public static SizeEstimate create(int instructionSize) {
098            if (instructionSize < cache.length) {
099                return cache[instructionSize];
100            } else {
101                return new SizeEstimate(instructionSize, 0);
102            }
103        }
104
105        @Override
106        public String toString() {
107            return "SE[i=" + instructionSize + ", c=" + constantSize + "]";
108        }
109    }
110}