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