AMD64MoveFactory.java revision 12651:6ef01bd40ce2
1/*
2 * Copyright (c) 2015, 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
24package org.graalvm.compiler.core.amd64;
25
26import static org.graalvm.compiler.lir.LIRValueUtil.asConstant;
27import static org.graalvm.compiler.lir.LIRValueUtil.isConstantValue;
28import static org.graalvm.compiler.lir.LIRValueUtil.isStackSlotValue;
29import static jdk.vm.ci.code.ValueUtil.isRegister;
30
31import org.graalvm.compiler.asm.NumUtil;
32import org.graalvm.compiler.core.common.type.DataPointerConstant;
33import org.graalvm.compiler.debug.GraalError;
34import org.graalvm.compiler.lir.amd64.AMD64AddressValue;
35import org.graalvm.compiler.lir.amd64.AMD64LIRInstruction;
36import org.graalvm.compiler.lir.amd64.AMD64Move.AMD64StackMove;
37import org.graalvm.compiler.lir.amd64.AMD64Move.LeaDataOp;
38import org.graalvm.compiler.lir.amd64.AMD64Move.LeaOp;
39import org.graalvm.compiler.lir.amd64.AMD64Move.MoveFromConstOp;
40import org.graalvm.compiler.lir.amd64.AMD64Move.MoveFromRegOp;
41import org.graalvm.compiler.lir.amd64.AMD64Move.MoveToRegOp;
42
43import jdk.vm.ci.amd64.AMD64Kind;
44import jdk.vm.ci.code.Register;
45import jdk.vm.ci.meta.AllocatableValue;
46import jdk.vm.ci.meta.Constant;
47import jdk.vm.ci.meta.JavaConstant;
48import jdk.vm.ci.meta.Value;
49
50public abstract class AMD64MoveFactory extends AMD64MoveFactoryBase {
51
52    public AMD64MoveFactory(BackupSlotProvider backupSlotProvider) {
53        super(backupSlotProvider);
54    }
55
56    @Override
57    public boolean canInlineConstant(JavaConstant c) {
58        switch (c.getJavaKind()) {
59            case Long:
60                return NumUtil.isInt(c.asLong());
61            case Object:
62                return c.isNull();
63            default:
64                return true;
65        }
66    }
67
68    @Override
69    public AMD64LIRInstruction createMove(AllocatableValue dst, Value src) {
70        if (src instanceof AMD64AddressValue) {
71            return new LeaOp(dst, (AMD64AddressValue) src);
72        } else if (isConstantValue(src)) {
73            return createLoad(dst, asConstant(src));
74        } else if (isRegister(src) || isStackSlotValue(dst)) {
75            return new MoveFromRegOp((AMD64Kind) dst.getPlatformKind(), dst, (AllocatableValue) src);
76        } else {
77            return new MoveToRegOp((AMD64Kind) dst.getPlatformKind(), dst, (AllocatableValue) src);
78        }
79    }
80
81    @Override
82    public AMD64LIRInstruction createStackMove(AllocatableValue result, AllocatableValue input, Register scratchRegister, AllocatableValue backupSlot) {
83        return new AMD64StackMove(result, input, scratchRegister, backupSlot);
84    }
85
86    @Override
87    public AMD64LIRInstruction createLoad(AllocatableValue dst, Constant src) {
88        if (src instanceof JavaConstant) {
89            return new MoveFromConstOp(dst, (JavaConstant) src);
90        } else if (src instanceof DataPointerConstant) {
91            return new LeaDataOp(dst, (DataPointerConstant) src);
92        } else {
93            throw GraalError.shouldNotReachHere(String.format("unsupported constant: %s", src));
94        }
95    }
96}
97