1 /*
   2  * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.lir.sparc;
  24 
  25 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.isSimm13;
  26 import static org.graalvm.compiler.debug.GraalError.shouldNotReachHere;
  27 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.CONST;
  28 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
  29 import static org.graalvm.compiler.lir.LIRValueUtil.asJavaConstant;
  30 import static org.graalvm.compiler.lir.LIRValueUtil.isJavaConstant;
  31 import static jdk.vm.ci.code.ValueUtil.asRegister;
  32 import static jdk.vm.ci.code.ValueUtil.isRegister;
  33 import static jdk.vm.ci.sparc.SPARC.g0;
  34 
  35 import org.graalvm.compiler.asm.sparc.SPARCAssembler;
  36 import org.graalvm.compiler.asm.sparc.SPARCAssembler.Op3s;
  37 import org.graalvm.compiler.asm.sparc.SPARCMacroAssembler;
  38 import org.graalvm.compiler.core.common.LIRKind;
  39 import org.graalvm.compiler.lir.LIRFrameState;
  40 import org.graalvm.compiler.lir.LIRInstructionClass;
  41 import org.graalvm.compiler.lir.Opcode;
  42 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
  43 
  44 import jdk.vm.ci.meta.JavaConstant;
  45 import jdk.vm.ci.meta.Value;
  46 
  47 public final class SPARCOP3Op extends SPARCLIRInstruction implements SPARCTailDelayedLIRInstruction {
  48     public static final LIRInstructionClass<SPARCOP3Op> TYPE = LIRInstructionClass.create(SPARCOP3Op.class);
  49     public static final SizeEstimate SIZE = SizeEstimate.create(1);
  50 
  51     @Opcode private final Op3s op3;
  52     @Use({REG}) protected Value rs1;
  53     @Use({REG, CONST}) protected Value rs2;
  54     @Def({REG}) protected Value rd;
  55     @State protected LIRFrameState state;
  56 
  57     public static SPARCOP3Op newUnary(Op3s op3, Value rs2, Value rd) {
  58         return newUnary(op3, rs2, rd, null);
  59     }
  60 
  61     public static SPARCOP3Op newUnary(Op3s op3, Value rs2, Value rd, LIRFrameState state) {
  62         return new SPARCOP3Op(op3, g0.asValue(LIRKind.value(rs2.getPlatformKind())), rs2, rd, state);
  63     }
  64 
  65     public static SPARCOP3Op newBinaryVoid(Op3s op3, Value rs1, Value rs2) {
  66         return newBinaryVoid(op3, rs1, rs2, null);
  67     }
  68 
  69     public static SPARCOP3Op newBinaryVoid(Op3s op3, Value rs1, Value rs2, LIRFrameState state) {
  70         return new SPARCOP3Op(op3, rs1, rs2, g0.asValue(LIRKind.value(rs2.getPlatformKind())), state);
  71     }
  72 
  73     public SPARCOP3Op(Op3s op3, Value rs1, Value rs2, Value rd) {
  74         this(op3, rs1, rs2, rd, null);
  75     }
  76 
  77     public SPARCOP3Op(Op3s op3, Value rs1, Value rs2, Value rd, LIRFrameState state) {
  78         super(TYPE, SIZE);
  79         this.op3 = op3;
  80         this.rs1 = rs1;
  81         this.rs2 = rs2;
  82         this.rd = rd;
  83         this.state = state;
  84     }
  85 
  86     @Override
  87     protected void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
  88         getDelayedControlTransfer().emitControlTransfer(crb, masm);
  89         if (state != null) {
  90             crb.recordImplicitException(masm.position(), state);
  91         }
  92         emitOp3(masm, op3, rs1, rs2, rd);
  93     }
  94 
  95     public static void emitOp3(SPARCMacroAssembler masm, Op3s op3, Value rs1, Value rs2) {
  96         emitOp3(masm, op3, rs1, rs2, g0.asValue(LIRKind.value(rs2.getPlatformKind())));
  97     }
  98 
  99     public static void emitOp3(SPARCMacroAssembler masm, Op3s op3, Value rs1, Value rs2, Value rd) {
 100         assert isRegister(rs1) : rs1;
 101         if (isJavaConstant(rs2)) {
 102             JavaConstant constant = asJavaConstant(rs2);
 103             long simm13;
 104             if (constant.isNull()) {
 105                 simm13 = 0;
 106             } else {
 107                 // Cast is safe, as isSimm13 assertion is done
 108                 simm13 = constant.asLong();
 109             }
 110             assert isSimm13(constant);
 111             SPARCAssembler.Op3Op.emit(masm, op3, asRegister(rs1), (int) simm13, asRegister(rd));
 112         } else if (isRegister(rs2)) {
 113             SPARCAssembler.Op3Op.emit(masm, op3, asRegister(rs1), asRegister(rs2), asRegister(rd));
 114         } else {
 115             throw shouldNotReachHere(String.format("Got values a: %s b: %s", rs1, rs2));
 116         }
 117     }
 118 }