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