1 /*
   2  * Copyright (c) 2015, 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 
  26 package org.graalvm.compiler.core.amd64;
  27 
  28 import static jdk.vm.ci.code.ValueUtil.isRegister;
  29 import static org.graalvm.compiler.lir.LIRValueUtil.asConstant;
  30 import static org.graalvm.compiler.lir.LIRValueUtil.isConstantValue;
  31 import static org.graalvm.compiler.lir.LIRValueUtil.isStackSlotValue;
  32 
  33 import org.graalvm.compiler.asm.amd64.AMD64Assembler;
  34 import org.graalvm.compiler.core.common.NumUtil;
  35 import org.graalvm.compiler.core.common.type.DataPointerConstant;
  36 import org.graalvm.compiler.debug.GraalError;
  37 import org.graalvm.compiler.lir.LIRInstruction;
  38 import org.graalvm.compiler.lir.amd64.AMD64AddressValue;
  39 import org.graalvm.compiler.lir.amd64.AMD64LIRInstruction;
  40 import org.graalvm.compiler.lir.amd64.AMD64Move;
  41 import org.graalvm.compiler.lir.amd64.AMD64Move.AMD64StackMove;
  42 import org.graalvm.compiler.lir.amd64.AMD64Move.LeaDataOp;
  43 import org.graalvm.compiler.lir.amd64.AMD64Move.LeaOp;
  44 import org.graalvm.compiler.lir.amd64.AMD64Move.MoveFromConstOp;
  45 import org.graalvm.compiler.lir.amd64.AMD64Move.MoveFromRegOp;
  46 import org.graalvm.compiler.lir.amd64.AMD64Move.MoveToRegOp;
  47 
  48 import jdk.vm.ci.amd64.AMD64Kind;
  49 import jdk.vm.ci.code.Register;
  50 import jdk.vm.ci.meta.AllocatableValue;
  51 import jdk.vm.ci.meta.Constant;
  52 import jdk.vm.ci.meta.JavaConstant;
  53 import jdk.vm.ci.meta.Value;
  54 
  55 public abstract class AMD64MoveFactory extends AMD64MoveFactoryBase {
  56 
  57     public AMD64MoveFactory(BackupSlotProvider backupSlotProvider) {
  58         super(backupSlotProvider);
  59     }
  60 
  61     @Override
  62     public boolean canInlineConstant(Constant con) {
  63         if (con instanceof JavaConstant) {
  64             JavaConstant c = (JavaConstant) con;
  65             switch (c.getJavaKind()) {
  66                 case Long:
  67                     return NumUtil.isInt(c.asLong());
  68                 case Object:
  69                     return c.isNull();
  70                 default:
  71                     return true;
  72             }
  73         }
  74         return false;
  75     }
  76 
  77     @Override
  78     public boolean allowConstantToStackMove(Constant constant) {
  79         if (constant instanceof DataPointerConstant) {
  80             return false;
  81         }
  82         if (constant instanceof JavaConstant && !AMD64Move.canMoveConst2Stack(((JavaConstant) constant))) {
  83             return false;
  84         }
  85         return true;
  86     }
  87 
  88     @Override
  89     public AMD64LIRInstruction createMove(AllocatableValue dst, Value src) {
  90         if (src instanceof AMD64AddressValue) {
  91             return new LeaOp(dst, (AMD64AddressValue) src, AMD64Assembler.OperandSize.QWORD);
  92         } else if (isConstantValue(src)) {
  93             return createLoad(dst, asConstant(src));
  94         } else if (isRegister(src) || isStackSlotValue(dst)) {
  95             return new MoveFromRegOp((AMD64Kind) dst.getPlatformKind(), dst, (AllocatableValue) src);
  96         } else {
  97             return new MoveToRegOp((AMD64Kind) dst.getPlatformKind(), dst, (AllocatableValue) src);
  98         }
  99     }
 100 
 101     @Override
 102     public AMD64LIRInstruction createStackMove(AllocatableValue result, AllocatableValue input, Register scratchRegister, AllocatableValue backupSlot) {
 103         return new AMD64StackMove(result, input, scratchRegister, backupSlot);
 104     }
 105 
 106     @Override
 107     public AMD64LIRInstruction createLoad(AllocatableValue dst, Constant src) {
 108         if (src instanceof JavaConstant) {
 109             return new MoveFromConstOp(dst, (JavaConstant) src);
 110         } else if (src instanceof DataPointerConstant) {
 111             return new LeaDataOp(dst, (DataPointerConstant) src);
 112         } else {
 113             throw GraalError.shouldNotReachHere(String.format("unsupported constant: %s", src));
 114         }
 115     }
 116 
 117     @Override
 118     public LIRInstruction createStackLoad(AllocatableValue result, Constant input) {
 119         if (input instanceof JavaConstant) {
 120             return new MoveFromConstOp(result, (JavaConstant) input);
 121         } else {
 122             throw GraalError.shouldNotReachHere(String.format("unsupported constant for stack load: %s", input));
 123         }
 124     }
 125 }