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.aarch64;
  27 
  28 import static org.graalvm.compiler.lir.LIRValueUtil.asConstant;
  29 import static org.graalvm.compiler.lir.LIRValueUtil.isConstantValue;
  30 import static org.graalvm.compiler.lir.LIRValueUtil.isStackSlotValue;
  31 
  32 import org.graalvm.compiler.asm.aarch64.AArch64MacroAssembler;
  33 import org.graalvm.compiler.core.common.type.DataPointerConstant;
  34 import org.graalvm.compiler.debug.GraalError;
  35 import org.graalvm.compiler.lir.LIRInstruction;
  36 import org.graalvm.compiler.lir.aarch64.AArch64AddressValue;
  37 import org.graalvm.compiler.lir.aarch64.AArch64LIRInstruction;
  38 import org.graalvm.compiler.lir.aarch64.AArch64Move;
  39 import org.graalvm.compiler.lir.aarch64.AArch64Move.LoadAddressOp;
  40 import org.graalvm.compiler.lir.gen.LIRGeneratorTool.MoveFactory;
  41 
  42 import jdk.vm.ci.meta.AllocatableValue;
  43 import jdk.vm.ci.meta.Constant;
  44 import jdk.vm.ci.meta.JavaConstant;
  45 import jdk.vm.ci.meta.Value;
  46 
  47 public class AArch64MoveFactory implements MoveFactory {
  48 
  49     @Override
  50     public LIRInstruction createMove(AllocatableValue dst, Value src) {
  51         boolean srcIsSlot = isStackSlotValue(src);
  52         boolean dstIsSlot = isStackSlotValue(dst);
  53         if (isConstantValue(src)) {
  54             return createLoad(dst, asConstant(src));
  55         } else if (src instanceof AArch64AddressValue) {
  56             return new LoadAddressOp(dst, (AArch64AddressValue) src);
  57         } else {
  58             assert src instanceof AllocatableValue;
  59             if (srcIsSlot && dstIsSlot) {
  60                 throw GraalError.shouldNotReachHere(src.getClass() + " " + dst.getClass());
  61             } else {
  62                 return new AArch64Move.Move(dst, (AllocatableValue) src);
  63             }
  64         }
  65     }
  66 
  67     @Override
  68     public LIRInstruction createStackMove(AllocatableValue result, AllocatableValue input) {
  69         return new AArch64Move.Move(result, input);
  70     }
  71 
  72     @Override
  73     public AArch64LIRInstruction createLoad(AllocatableValue dst, Constant src) {
  74         if (src instanceof JavaConstant) {
  75             JavaConstant javaConstant = (JavaConstant) src;
  76             if (canInlineConstant(javaConstant)) {
  77                 return new AArch64Move.LoadInlineConstant(javaConstant, dst);
  78             } else {
  79                 // return new AArch64Move.LoadConstantFromTable(javaConstant,
  80                 // constantTableBaseProvider.getConstantTableBase(), dst);
  81                 return new AArch64Move.LoadInlineConstant(javaConstant, dst);
  82             }
  83         } else if (src instanceof DataPointerConstant) {
  84             return new AArch64Move.LoadDataOp(dst, (DataPointerConstant) src);
  85         } else {
  86             // throw GraalError.shouldNotReachHere(src.getClass().toString());
  87             throw GraalError.unimplemented();
  88         }
  89     }
  90 
  91     @Override
  92     public LIRInstruction createStackLoad(AllocatableValue result, Constant input) {
  93         return createLoad(result, input);
  94     }
  95 
  96     @Override
  97     public boolean canInlineConstant(Constant con) {
  98         if (con instanceof JavaConstant) {
  99             JavaConstant c = (JavaConstant) con;
 100             switch (c.getJavaKind()) {
 101                 case Boolean:
 102                 case Byte:
 103                 case Char:
 104                 case Short:
 105                 case Int:
 106                     return AArch64MacroAssembler.isMovableImmediate(c.asInt());
 107                 case Long:
 108                     return AArch64MacroAssembler.isMovableImmediate(c.asLong());
 109                 case Object:
 110                     return c.isNull();
 111                 default:
 112                     return false;
 113             }
 114         }
 115         return false;
 116     }
 117 
 118     @Override
 119     public boolean allowConstantToStackMove(Constant value) {
 120         return false;
 121     }
 122 
 123 }