1 /*
   2  * Copyright (c) 2015, 2018, 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 
  24 
  25 package org.graalvm.compiler.lir.sparc;
  26 
  27 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.isSimm13;
  28 import static org.graalvm.compiler.debug.GraalError.shouldNotReachHere;
  29 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.CONST;
  30 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
  31 import static org.graalvm.compiler.lir.LIRValueUtil.asJavaConstant;
  32 import static org.graalvm.compiler.lir.LIRValueUtil.isJavaConstant;
  33 import static jdk.vm.ci.code.ValueUtil.asRegister;
  34 import static jdk.vm.ci.code.ValueUtil.isRegister;
  35 import static jdk.vm.ci.sparc.SPARC.g0;
  36 
  37 import org.graalvm.compiler.asm.sparc.SPARCAssembler;
  38 import org.graalvm.compiler.asm.sparc.SPARCAssembler.Op3s;
  39 import org.graalvm.compiler.asm.sparc.SPARCMacroAssembler;
  40 import org.graalvm.compiler.core.common.LIRKind;
  41 import org.graalvm.compiler.lir.LIRFrameState;
  42 import org.graalvm.compiler.lir.LIRInstructionClass;
  43 import org.graalvm.compiler.lir.Opcode;
  44 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
  45 
  46 import jdk.vm.ci.meta.AllocatableValue;
  47 import jdk.vm.ci.meta.JavaConstant;
  48 import jdk.vm.ci.meta.Value;
  49 
  50 public final class SPARCOP3Op extends SPARCLIRInstruction implements SPARCTailDelayedLIRInstruction {
  51     public static final LIRInstructionClass<SPARCOP3Op> TYPE = LIRInstructionClass.create(SPARCOP3Op.class);
  52     public static final SizeEstimate SIZE = SizeEstimate.create(1);
  53 
  54     @Opcode private final Op3s op3;
  55     @Use({REG}) protected AllocatableValue rs1;
  56     @Use({REG, CONST}) protected Value rs2;
  57     @Def({REG}) protected AllocatableValue rd;
  58     @State protected LIRFrameState state;
  59 
  60     public static SPARCOP3Op newUnary(Op3s op3, Value rs2, AllocatableValue rd) {
  61         return newUnary(op3, rs2, rd, null);
  62     }
  63 
  64     public static SPARCOP3Op newUnary(Op3s op3, Value rs2, AllocatableValue rd, LIRFrameState state) {
  65         return new SPARCOP3Op(op3, g0.asValue(LIRKind.value(rs2.getPlatformKind())), rs2, rd, state);
  66     }
  67 
  68     public static SPARCOP3Op newBinaryVoid(Op3s op3, AllocatableValue rs1, Value rs2) {
  69         return newBinaryVoid(op3, rs1, rs2, null);
  70     }
  71 
  72     public static SPARCOP3Op newBinaryVoid(Op3s op3, AllocatableValue rs1, Value rs2, LIRFrameState state) {
  73         return new SPARCOP3Op(op3, rs1, rs2, g0.asValue(LIRKind.value(rs2.getPlatformKind())), state);
  74     }
  75 
  76     public SPARCOP3Op(Op3s op3, AllocatableValue rs1, Value rs2, AllocatableValue rd) {
  77         this(op3, rs1, rs2, rd, null);
  78     }
  79 
  80     public SPARCOP3Op(Op3s op3, AllocatableValue rs1, Value rs2, AllocatableValue rd, LIRFrameState state) {
  81         super(TYPE, SIZE);
  82         this.op3 = op3;
  83         this.rs1 = rs1;
  84         this.rs2 = rs2;
  85         this.rd = rd;
  86         this.state = state;
  87     }
  88 
  89     @Override
  90     protected void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
  91         getDelayedControlTransfer().emitControlTransfer(crb, masm);
  92         if (state != null) {
  93             crb.recordImplicitException(masm.position(), state);
  94         }
  95         emitOp3(masm, op3, rs1, rs2, rd);
  96     }
  97 
  98     public static void emitOp3(SPARCMacroAssembler masm, Op3s op3, Value rs1, Value rs2) {
  99         emitOp3(masm, op3, rs1, rs2, g0.asValue(LIRKind.value(rs2.getPlatformKind())));
 100     }
 101 
 102     public static void emitOp3(SPARCMacroAssembler masm, Op3s op3, Value rs1, Value rs2, Value rd) {
 103         assert isRegister(rs1) : rs1;
 104         if (isJavaConstant(rs2)) {
 105             JavaConstant constant = asJavaConstant(rs2);
 106             long simm13;
 107             if (constant.isNull()) {
 108                 simm13 = 0;
 109             } else {
 110                 // Cast is safe, as isSimm13 assertion is done
 111                 simm13 = constant.asLong();
 112             }
 113             assert isSimm13(constant);
 114             SPARCAssembler.Op3Op.emit(masm, op3, asRegister(rs1), (int) simm13, asRegister(rd));
 115         } else if (isRegister(rs2)) {
 116             SPARCAssembler.Op3Op.emit(masm, op3, asRegister(rs1), asRegister(rs2), asRegister(rd));
 117         } else {
 118             throw shouldNotReachHere(String.format("Got values a: %s b: %s", rs1, rs2));
 119         }
 120     }
 121 }