1 /*
   2  * Copyright (c) 2012, 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 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 import jdk.vm.ci.meta.UnresolvedJavaType;
  35 
  36 public class ObjectStamp extends AbstractObjectStamp {
  37 
  38     public ObjectStamp(ResolvedJavaType type, boolean exactType, boolean nonNull, boolean alwaysNull) {
  39         super(type, exactType, nonNull, alwaysNull);
  40     }
  41 
  42     @Override
  43     protected ObjectStamp copyWith(ResolvedJavaType type, boolean exactType, boolean nonNull, boolean alwaysNull) {
  44         return new ObjectStamp(type, exactType, nonNull, alwaysNull);
  45     }
  46 
  47     @Override
  48     public Stamp unrestricted() {
  49         return StampFactory.object();
  50     }
  51 
  52     @Override
  53     public String toString() {
  54         StringBuilder str = new StringBuilder();
  55         str.append('a');
  56         appendString(str);
  57         return str.toString();
  58     }
  59 
  60     @Override
  61     public boolean isCompatible(Stamp other) {
  62         if (this == other) {
  63             return true;
  64         }
  65         if (other instanceof ObjectStamp) {
  66             return true;
  67         }
  68         return false;
  69     }
  70 
  71     @Override
  72     public boolean isCompatible(Constant constant) {
  73         if (constant instanceof JavaConstant) {
  74             return ((JavaConstant) constant).getJavaKind().isObject();
  75         }
  76         return false;
  77     }
  78 
  79     @Override
  80     public LIRKind getLIRKind(LIRKindTool tool) {
  81         return tool.getObjectKind();
  82     }
  83 
  84     @Override
  85     public Constant readConstant(MemoryAccessProvider provider, Constant base, long displacement) {
  86         try {
  87             return provider.readObjectConstant(base, displacement);
  88         } catch (IllegalArgumentException e) {
  89             /*
  90              * It's possible that the base and displacement aren't valid together so simply return
  91              * null.
  92              */
  93             return null;
  94         }
  95     }
  96 
  97     /**
  98      * Convert an ObjectStamp into a representation that can be resolved symbolically into the
  99      * original stamp.
 100      */
 101     @Override
 102     public SymbolicJVMCIReference<ObjectStamp> makeSymbolic() {
 103         if (type() == null) {
 104             return null;
 105         }
 106         return new SymbolicObjectStamp(this);
 107     }
 108 
 109     static class SymbolicObjectStamp implements SymbolicJVMCIReference<ObjectStamp> {
 110         UnresolvedJavaType type;
 111         private boolean exactType;
 112         private boolean nonNull;
 113         private boolean alwaysNull;
 114 
 115         SymbolicObjectStamp(ObjectStamp stamp) {
 116             if (stamp.type() != null) {
 117                 type = UnresolvedJavaType.create(stamp.type().getName());
 118             }
 119             exactType = stamp.isExactType();
 120             nonNull = stamp.nonNull();
 121             alwaysNull = stamp.alwaysNull();
 122         }
 123 
 124         @Override
 125         public ObjectStamp resolve(ResolvedJavaType accessingClass) {
 126             return new ObjectStamp(type != null ? type.resolve(accessingClass) : null, exactType, nonNull, alwaysNull);
 127         }
 128 
 129         @Override
 130         public String toString() {
 131             return "SymbolicObjectStamp{" +
 132                             "declaringType=" + type +
 133                             ", exactType=" + exactType +
 134                             ", nonNull=" + nonNull +
 135                             ", alwaysNull=" + alwaysNull +
 136                             '}';
 137         }
 138     }
 139 
 140 }