1 /*
   2  * Copyright (c) 2013, 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.hotspot.sparc;
  26 
  27 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.BPR;
  28 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.Annul.ANNUL;
  29 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.BranchPredict.PREDICT_TAKEN;
  30 import static org.graalvm.compiler.asm.sparc.SPARCAssembler.RCondition.Rc_z;
  31 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.ILLEGAL;
  32 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
  33 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.STACK;
  34 import static org.graalvm.compiler.lir.sparc.SPARCMove.loadFromConstantTable;
  35 import static jdk.vm.ci.code.ValueUtil.asRegister;
  36 
  37 import org.graalvm.compiler.asm.Label;
  38 import org.graalvm.compiler.asm.sparc.SPARCAddress;
  39 import org.graalvm.compiler.asm.sparc.SPARCAssembler.CC;
  40 import org.graalvm.compiler.asm.sparc.SPARCAssembler.ConditionFlag;
  41 import org.graalvm.compiler.asm.sparc.SPARCMacroAssembler;
  42 import org.graalvm.compiler.asm.sparc.SPARCMacroAssembler.ScratchRegister;
  43 import org.graalvm.compiler.core.common.CompressEncoding;
  44 import org.graalvm.compiler.lir.LIRInstructionClass;
  45 import org.graalvm.compiler.lir.StandardOp.LoadConstantOp;
  46 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
  47 import org.graalvm.compiler.lir.sparc.SPARCDelayedControlTransfer;
  48 import org.graalvm.compiler.lir.sparc.SPARCLIRInstruction;
  49 import org.graalvm.compiler.lir.sparc.SPARCTailDelayedLIRInstruction;
  50 
  51 import jdk.vm.ci.code.Register;
  52 import jdk.vm.ci.code.ValueUtil;
  53 import jdk.vm.ci.hotspot.HotSpotConstant;
  54 import jdk.vm.ci.meta.AllocatableValue;
  55 import jdk.vm.ci.meta.Constant;
  56 
  57 public class SPARCHotSpotMove {
  58 
  59     public static class LoadHotSpotObjectConstantInline extends SPARCLIRInstruction implements SPARCTailDelayedLIRInstruction, LoadConstantOp {
  60         public static final LIRInstructionClass<LoadHotSpotObjectConstantInline> TYPE = LIRInstructionClass.create(LoadHotSpotObjectConstantInline.class);
  61 
  62         public static final SizeEstimate SIZE = SizeEstimate.create(8);
  63         private HotSpotConstant constant;
  64         @Def({REG, STACK}) AllocatableValue result;
  65 
  66         public LoadHotSpotObjectConstantInline(HotSpotConstant constant, AllocatableValue result) {
  67             super(TYPE, SIZE);
  68             this.constant = constant;
  69             this.result = result;
  70         }
  71 
  72         @Override
  73         protected void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
  74             crb.recordInlineDataInCode(constant);
  75             if (constant.isCompressed()) {
  76                 masm.setw(0, asRegister(result), true);
  77             } else {
  78                 masm.setx(0, asRegister(result), true);
  79             }
  80         }
  81 
  82         @Override
  83         public AllocatableValue getResult() {
  84             return result;
  85         }
  86 
  87         @Override
  88         public Constant getConstant() {
  89             return constant;
  90         }
  91     }
  92 
  93     public static class LoadHotSpotObjectConstantFromTable extends SPARCLIRInstruction implements SPARCTailDelayedLIRInstruction {
  94         public static final LIRInstructionClass<LoadHotSpotObjectConstantFromTable> TYPE = LIRInstructionClass.create(LoadHotSpotObjectConstantFromTable.class);
  95 
  96         public static final SizeEstimate SIZE = SizeEstimate.create(2, 8);
  97         private final HotSpotConstant constant;
  98         @Use({REG}) private AllocatableValue constantTableBase;
  99         @Def({REG, STACK}) AllocatableValue result;
 100 
 101         public LoadHotSpotObjectConstantFromTable(HotSpotConstant constant, AllocatableValue result, AllocatableValue constantTableBase) {
 102             super(TYPE, SIZE);
 103             this.constant = constant;
 104             this.result = result;
 105             this.constantTableBase = constantTableBase;
 106         }
 107 
 108         @Override
 109         protected void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
 110             try (ScratchRegister scratch = masm.getScratchRegister()) {
 111                 boolean isStack = ValueUtil.isStackSlot(result);
 112                 Register register;
 113                 if (isStack) {
 114                     register = scratch.getRegister();
 115                 } else {
 116                     register = asRegister(result);
 117                 }
 118                 int bytes = loadFromConstantTable(crb, masm, asRegister(constantTableBase), constant, register, SPARCDelayedControlTransfer.DUMMY);
 119                 if (isStack) {
 120                     masm.st(register, (SPARCAddress) crb.asAddress(result), bytes);
 121                 }
 122             }
 123         }
 124     }
 125 
 126     public static final class CompressPointer extends SPARCLIRInstruction {
 127         public static final LIRInstructionClass<CompressPointer> TYPE = LIRInstructionClass.create(CompressPointer.class);
 128         public static final SizeEstimate SIZE = SizeEstimate.create(5);
 129 
 130         private final CompressEncoding encoding;
 131         private final boolean nonNull;
 132 
 133         @Def({REG}) protected AllocatableValue result;
 134         @Use({REG}) protected AllocatableValue input;
 135         @Alive({REG, ILLEGAL}) protected AllocatableValue baseRegister;
 136 
 137         public CompressPointer(AllocatableValue result, AllocatableValue input, AllocatableValue baseRegister, CompressEncoding encoding, boolean nonNull) {
 138             super(TYPE, SIZE);
 139             this.result = result;
 140             this.input = input;
 141             this.baseRegister = baseRegister;
 142             this.encoding = encoding;
 143             this.nonNull = nonNull;
 144         }
 145 
 146         @Override
 147         public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
 148             Register inputRegister = asRegister(input);
 149             Register resReg = asRegister(result);
 150             if (encoding.hasBase()) {
 151                 Register baseReg = asRegister(baseRegister);
 152                 if (!nonNull) {
 153                     masm.cmp(inputRegister, baseReg);
 154                     masm.movcc(ConditionFlag.Equal, CC.Xcc, baseReg, resReg);
 155                     masm.sub(resReg, baseReg, resReg);
 156                 } else {
 157                     masm.sub(inputRegister, baseReg, resReg);
 158                 }
 159                 if (encoding.getShift() != 0) {
 160                     masm.srlx(resReg, encoding.getShift(), resReg);
 161                 }
 162             } else {
 163                 masm.srlx(inputRegister, encoding.getShift(), resReg);
 164             }
 165         }
 166     }
 167 
 168     public static final class UncompressPointer extends SPARCLIRInstruction {
 169         public static final LIRInstructionClass<UncompressPointer> TYPE = LIRInstructionClass.create(UncompressPointer.class);
 170         public static final SizeEstimate SIZE = SizeEstimate.create(4);
 171 
 172         private final CompressEncoding encoding;
 173         private final boolean nonNull;
 174 
 175         @Def({REG}) protected AllocatableValue result;
 176         @Use({REG}) protected AllocatableValue input;
 177         @Alive({REG, ILLEGAL}) protected AllocatableValue baseRegister;
 178 
 179         public UncompressPointer(AllocatableValue result, AllocatableValue input, AllocatableValue baseRegister, CompressEncoding encoding, boolean nonNull) {
 180             super(TYPE, SIZE);
 181             this.result = result;
 182             this.input = input;
 183             this.baseRegister = baseRegister;
 184             this.encoding = encoding;
 185             this.nonNull = nonNull;
 186         }
 187 
 188         @Override
 189         public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
 190             Register inputRegister = asRegister(input);
 191             Register resReg = asRegister(result);
 192             Register baseReg = encoding.hasBase() ? asRegister(baseRegister) : null;
 193             emitUncompressCode(masm, inputRegister, resReg, baseReg, encoding.getShift(), nonNull);
 194         }
 195 
 196         public static void emitUncompressCode(SPARCMacroAssembler masm, Register inputRegister, Register resReg, Register baseReg, int shift, boolean nonNull) {
 197             Register secondaryInput;
 198             if (shift != 0) {
 199                 masm.sll(inputRegister, shift, resReg);
 200                 secondaryInput = resReg;
 201             } else {
 202                 secondaryInput = inputRegister;
 203             }
 204 
 205             if (baseReg != null) {
 206                 if (nonNull) {
 207                     masm.add(secondaryInput, baseReg, resReg);
 208                 } else {
 209                     Label done = new Label();
 210                     BPR.emit(masm, Rc_z, ANNUL, PREDICT_TAKEN, secondaryInput, done);
 211                     masm.add(baseReg, secondaryInput, resReg);
 212                     masm.bind(done);
 213                 }
 214             }
 215         }
 216     }
 217 
 218 }