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