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.core.common.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(Constant con) {
  58         if (con instanceof JavaConstant) {
  59             JavaConstant c = (JavaConstant) con;
  60             switch (c.getJavaKind()) {
  61                 case Long:
  62                     return NumUtil.isInt(c.asLong());
  63                 case Object:
  64                     return c.isNull();
  65                 default:
  66                     return true;
  67             }
  68         }
  69         return false;
  70     }
  71 
  72     @Override
  73     public AMD64LIRInstruction createMove(AllocatableValue dst, Value src) {
  74         if (src instanceof AMD64AddressValue) {
  75             return new LeaOp(dst, (AMD64AddressValue) src);
  76         } else if (isConstantValue(src)) {
  77             return createLoad(dst, asConstant(src));
  78         } else if (isRegister(src) || isStackSlotValue(dst)) {
  79             return new MoveFromRegOp((AMD64Kind) dst.getPlatformKind(), dst, (AllocatableValue) src);
  80         } else {
  81             return new MoveToRegOp((AMD64Kind) dst.getPlatformKind(), dst, (AllocatableValue) src);
  82         }
  83     }
  84 
  85     @Override
  86     public AMD64LIRInstruction createStackMove(AllocatableValue result, AllocatableValue input, Register scratchRegister, AllocatableValue backupSlot) {
  87         return new AMD64StackMove(result, input, scratchRegister, backupSlot);
  88     }
  89 
  90     @Override
  91     public AMD64LIRInstruction createLoad(AllocatableValue dst, Constant src) {
  92         if (src instanceof JavaConstant) {
  93             return new MoveFromConstOp(dst, (JavaConstant) src);
  94         } else if (src instanceof DataPointerConstant) {
  95             return new LeaDataOp(dst, (DataPointerConstant) src);
  96         } else {
  97             throw GraalError.shouldNotReachHere(String.format("unsupported constant: %s", src));
  98         }
  99     }
 100 }