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