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.lir.sparc;
  26 
  27 import static jdk.vm.ci.code.ValueUtil.asRegister;
  28 import static jdk.vm.ci.sparc.SPARCKind.WORD;
  29 import static jdk.vm.ci.sparc.SPARCKind.XWORD;
  30 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.HINT;
  31 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
  32 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.STACK;
  33 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.UNINITIALIZED;
  34 
  35 import org.graalvm.compiler.asm.sparc.SPARCAddress;
  36 import org.graalvm.compiler.asm.sparc.SPARCAssembler.Asi;
  37 import org.graalvm.compiler.asm.sparc.SPARCMacroAssembler;
  38 import org.graalvm.compiler.core.common.LIRKind;
  39 import org.graalvm.compiler.debug.GraalError;
  40 import org.graalvm.compiler.lir.LIRInstructionClass;
  41 import org.graalvm.compiler.lir.Opcode;
  42 import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
  43 import org.graalvm.compiler.lir.gen.LIRGeneratorTool;
  44 
  45 import jdk.vm.ci.code.Register;
  46 import jdk.vm.ci.code.ValueUtil;
  47 import jdk.vm.ci.meta.AllocatableValue;
  48 import jdk.vm.ci.sparc.SPARCKind;
  49 
  50 @Opcode("BSWAP")
  51 public final class SPARCByteSwapOp extends SPARCLIRInstruction implements SPARCTailDelayedLIRInstruction {
  52     public static final LIRInstructionClass<SPARCByteSwapOp> TYPE = LIRInstructionClass.create(SPARCByteSwapOp.class);
  53     public static final SizeEstimate SIZE = SizeEstimate.create(3);
  54     @Def({REG, HINT}) protected AllocatableValue result;
  55     @Use({REG}) protected AllocatableValue input;
  56     @Temp({REG}) protected AllocatableValue tempIndex;
  57     @Use({STACK, UNINITIALIZED}) protected AllocatableValue tmpSlot;
  58 
  59     public SPARCByteSwapOp(LIRGeneratorTool tool, AllocatableValue result, AllocatableValue input) {
  60         super(TYPE, SIZE);
  61         this.result = result;
  62         this.input = input;
  63         this.tmpSlot = tool.getResult().getFrameMapBuilder().allocateSpillSlot(LIRKind.value(XWORD));
  64         this.tempIndex = tool.newVariable(LIRKind.value(XWORD));
  65     }
  66 
  67     @Override
  68     public void emitCode(CompilationResultBuilder crb, SPARCMacroAssembler masm) {
  69         SPARCAddress addr = (SPARCAddress) crb.asAddress(tmpSlot);
  70         SPARCMove.emitStore(input, addr, result.getPlatformKind(), SPARCDelayedControlTransfer.DUMMY, null, crb, masm);
  71         if (addr.getIndex().equals(Register.None)) {
  72             Register tempReg = ValueUtil.asRegister(tempIndex, XWORD);
  73             masm.setx(addr.getDisplacement(), tempReg, false);
  74             addr = new SPARCAddress(addr.getBase(), tempReg);
  75         }
  76         getDelayedControlTransfer().emitControlTransfer(crb, masm);
  77         switch ((SPARCKind) input.getPlatformKind()) {
  78             case WORD:
  79                 masm.lduwa(addr.getBase(), addr.getIndex(), asRegister(result, WORD), Asi.ASI_PRIMARY_LITTLE);
  80                 break;
  81             case XWORD:
  82                 masm.ldxa(addr.getBase(), addr.getIndex(), asRegister(result, XWORD), Asi.ASI_PRIMARY_LITTLE);
  83                 break;
  84             default:
  85                 throw GraalError.shouldNotReachHere();
  86         }
  87     }
  88 }