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