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 
  24 package org.graalvm.compiler.core.amd64;
  25 
  26 import static org.graalvm.compiler.lir.LIRValueUtil.asConstant;
  27 import static org.graalvm.compiler.lir.LIRValueUtil.isConstantValue;
  28 import static org.graalvm.compiler.lir.LIRValueUtil.isStackSlotValue;
  29 import static jdk.vm.ci.code.ValueUtil.isRegister;
  30 
  31 import org.graalvm.compiler.asm.NumUtil;
  32 import org.graalvm.compiler.core.common.type.DataPointerConstant;
  33 import org.graalvm.compiler.debug.GraalError;
  34 import org.graalvm.compiler.lir.amd64.AMD64AddressValue;
  35 import org.graalvm.compiler.lir.amd64.AMD64LIRInstruction;
  36 import org.graalvm.compiler.lir.amd64.AMD64Move.AMD64StackMove;
  37 import org.graalvm.compiler.lir.amd64.AMD64Move.LeaDataOp;
  38 import org.graalvm.compiler.lir.amd64.AMD64Move.LeaOp;
  39 import org.graalvm.compiler.lir.amd64.AMD64Move.MoveFromConstOp;
  40 import org.graalvm.compiler.lir.amd64.AMD64Move.MoveFromRegOp;
  41 import org.graalvm.compiler.lir.amd64.AMD64Move.MoveToRegOp;
  42 
  43 import jdk.vm.ci.amd64.AMD64Kind;
  44 import jdk.vm.ci.code.Register;
  45 import jdk.vm.ci.meta.AllocatableValue;
  46 import jdk.vm.ci.meta.Constant;
  47 import jdk.vm.ci.meta.JavaConstant;
  48 import jdk.vm.ci.meta.Value;
  49 
  50 public 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 }