001/*
002 * Copyright (c) 2013, 2014, 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 jdk.internal.jvmci.common.*;
026import jdk.internal.jvmci.meta.*;
027import static com.oracle.graal.asm.sparc.SPARCAssembler.*;
028import static com.oracle.graal.asm.sparc.SPARCAssembler.CC.*;
029import static com.oracle.graal.asm.sparc.SPARCAssembler.Opfs.*;
030import static com.oracle.graal.lir.LIRInstruction.OperandFlag.*;
031import static jdk.internal.jvmci.code.ValueUtil.*;
032
033import com.oracle.graal.asm.sparc.*;
034import com.oracle.graal.lir.*;
035import com.oracle.graal.lir.asm.*;
036import com.oracle.graal.lir.sparc.SPARCControlFlow.CompareBranchOp;
037
038public enum SPARCCompare {
039    ICMP,
040    LCMP,
041    ACMP,
042    FCMP,
043    DCMP;
044
045    public static final class CompareOp extends SPARCLIRInstruction {
046        public static final LIRInstructionClass<CompareOp> TYPE = LIRInstructionClass.create(CompareOp.class);
047        public static final SizeEstimate SIZE = SizeEstimate.create(1);
048
049        @Opcode private final SPARCCompare opcode;
050        @Use({REG}) protected Value x;
051        @Use({REG, CONST}) protected Value y;
052
053        public CompareOp(SPARCCompare opcode, Value x, Value y) {
054            super(TYPE, SIZE);
055            this.opcode = opcode;
056            this.x = x;
057            this.y = y;
058        }
059
060        @Override
061        public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
062            emit(crb, masm, opcode, x, y);
063        }
064
065        @Override
066        public void verify() {
067            super.verify();
068            assert CompareBranchOp.SUPPORTED_KINDS.contains(x.getKind()) : x.getKind();
069            assert x.getKind().equals(y.getKind()) : x + " " + y;
070            // @formatter:off
071            assert
072                    (name().startsWith("I") && x.getKind() == Kind.Int && y.getKind().getStackKind() == Kind.Int) ||
073                    (name().startsWith("L") && x.getKind() == Kind.Long && y.getKind() == Kind.Long) ||
074                    (name().startsWith("A") && x.getKind() == Kind.Object && y.getKind() == Kind.Object) ||
075                    (name().startsWith("F") && x.getKind() == Kind.Float && y.getKind() == Kind.Float) ||
076                    (name().startsWith("D") && x.getKind() == Kind.Double && y.getKind() == Kind.Double)
077                    : "Name; " + name() + " x: " + x + " y: " + y;
078            // @formatter:on
079        }
080    }
081
082    public static void emit(CompilationResultBuilder crb, SPARCMacroAssembler masm, SPARCCompare opcode, Value x, Value y) {
083        if (isRegister(y)) {
084            switch (opcode) {
085                case ICMP:
086                    masm.cmp(asIntReg(x), asIntReg(y));
087                    break;
088                case LCMP:
089                    masm.cmp(asLongReg(x), asLongReg(y));
090                    break;
091                case ACMP:
092                    masm.cmp(asObjectReg(x), asObjectReg(y));
093                    break;
094                case FCMP:
095                    masm.fcmp(Fcc0, Fcmps, asFloatReg(x), asFloatReg(y));
096                    break;
097                case DCMP:
098                    masm.fcmp(Fcc0, Fcmpd, asDoubleReg(x), asDoubleReg(y));
099                    break;
100                default:
101                    throw JVMCIError.shouldNotReachHere();
102            }
103        } else {
104            assert isConstant(y);
105            switch (opcode) {
106                case LCMP:
107                    assert isSimm13(crb.asLongConst(y));
108                    masm.cmp(asLongReg(x), (int) crb.asLongConst(y));
109                    break;
110                case ICMP:
111                    assert isSimm13(crb.asIntConst(y));
112                    masm.cmp(asIntReg(x), crb.asIntConst(y));
113                    break;
114                case ACMP:
115                    if (((JavaConstant) y).isNull()) {
116                        masm.cmp(asObjectReg(x), 0);
117                        break;
118                    } else {
119                        throw JVMCIError.shouldNotReachHere("Only null object constants are allowed in comparisons");
120                    }
121                case FCMP:
122                    masm.fcmp(Fcc0, Fcmps, asFloatReg(x), asFloatReg(y));
123                    break;
124                case DCMP:
125                    masm.fcmp(Fcc0, Fcmpd, asDoubleReg(x), asDoubleReg(y));
126                    break;
127                default:
128                    throw JVMCIError.shouldNotReachHere();
129            }
130        }
131    }
132}