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