1 /*
   2  * Copyright (c) 2012, 2014, 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 package org.graalvm.compiler.core.common.type;
  26 
  27 import org.graalvm.compiler.core.common.LIRKind;
  28 import org.graalvm.compiler.core.common.spi.LIRKindTool;
  29 
  30 import jdk.vm.ci.meta.Constant;
  31 import jdk.vm.ci.meta.JavaConstant;
  32 import jdk.vm.ci.meta.MemoryAccessProvider;
  33 import jdk.vm.ci.meta.ResolvedJavaType;
  34 
  35 public class ObjectStamp extends AbstractObjectStamp {
  36 
  37     public ObjectStamp(ResolvedJavaType type, boolean exactType, boolean nonNull, boolean alwaysNull) {
  38         super(type, exactType, nonNull, alwaysNull);
  39     }
  40 
  41     @Override
  42     protected ObjectStamp copyWith(ResolvedJavaType type, boolean exactType, boolean nonNull, boolean alwaysNull) {
  43         return new ObjectStamp(type, exactType, nonNull, alwaysNull);
  44     }
  45 
  46     @Override
  47     public Stamp unrestricted() {
  48         return StampFactory.object();
  49     }
  50 
  51     @Override
  52     public String toString() {
  53         StringBuilder str = new StringBuilder();
  54         str.append('a');
  55         appendString(str);
  56         return str.toString();
  57     }
  58 
  59     @Override
  60     public boolean isCompatible(Stamp other) {
  61         if (this == other) {
  62             return true;
  63         }
  64         if (other instanceof ObjectStamp) {
  65             return true;
  66         }
  67         return false;
  68     }
  69 
  70     @Override
  71     public boolean isCompatible(Constant constant) {
  72         if (constant instanceof JavaConstant) {
  73             return ((JavaConstant) constant).getJavaKind().isObject();
  74         }
  75         return false;
  76     }
  77 
  78     @Override
  79     public LIRKind getLIRKind(LIRKindTool tool) {
  80         return tool.getObjectKind();
  81     }
  82 
  83     @Override
  84     public Constant readConstant(MemoryAccessProvider provider, Constant base, long displacement) {
  85         try {
  86             return provider.readObjectConstant(base, displacement);
  87         } catch (IllegalArgumentException e) {
  88             /*
  89              * It's possible that the base and displacement aren't valid together so simply return
  90              * null.
  91              */
  92             return null;
  93         }
  94     }
  95 }