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