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