1/* 2 * Copyright (c) 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 */ 23package org.graalvm.compiler.graph; 24 25import java.util.Objects; 26 27import jdk.vm.ci.code.BytecodePosition; 28import jdk.vm.ci.code.CodeUtil; 29import jdk.vm.ci.meta.JavaConstant; 30import jdk.vm.ci.meta.MetaUtil; 31import jdk.vm.ci.meta.ResolvedJavaMethod; 32 33public class NodeSourcePosition extends BytecodePosition { 34 35 /** 36 * The receiver of the method this frame refers to. 37 */ 38 private final JavaConstant receiver; 39 private final int hashCode; 40 41 public NodeSourcePosition(JavaConstant receiver, NodeSourcePosition caller, ResolvedJavaMethod method, int bci) { 42 super(caller, method, bci); 43 if (caller == null) { 44 this.hashCode = 31 * bci + method.hashCode(); 45 } else { 46 this.hashCode = caller.hashCode * 7 + 31 * bci + method.hashCode(); 47 } 48 this.receiver = receiver; 49 assert receiver == null || method.getDeclaringClass().isInstance(receiver); 50 } 51 52 @Override 53 public boolean equals(Object obj) { 54 if (obj == this) { 55 return true; 56 } 57 if (obj != null && getClass() == obj.getClass()) { 58 NodeSourcePosition that = (NodeSourcePosition) obj; 59 if (hashCode != that.hashCode) { 60 return false; 61 } 62 if (this.getBCI() == that.getBCI() && Objects.equals(this.getMethod(), that.getMethod()) && Objects.equals(this.getCaller(), that.getCaller()) && 63 Objects.equals(this.receiver, that.receiver)) { 64 return true; 65 } 66 } 67 return false; 68 } 69 70 @Override 71 public int hashCode() { 72 return hashCode; 73 } 74 75 public JavaConstant getReceiver() { 76 return receiver; 77 } 78 79 @Override 80 public NodeSourcePosition getCaller() { 81 return (NodeSourcePosition) super.getCaller(); 82 } 83 84 public NodeSourcePosition addCaller(JavaConstant newCallerReceiver, NodeSourcePosition link) { 85 if (getCaller() == null) { 86 assert newCallerReceiver == null || receiver == null : "replacing receiver"; 87 return new NodeSourcePosition(newCallerReceiver, link, getMethod(), getBCI()); 88 } else { 89 return new NodeSourcePosition(receiver, getCaller().addCaller(newCallerReceiver, link), getMethod(), getBCI()); 90 } 91 } 92 93 public NodeSourcePosition addCaller(NodeSourcePosition link) { 94 return addCaller(null, link); 95 } 96 97 @Override 98 public String toString() { 99 StringBuilder sb = new StringBuilder(100); 100 NodeSourcePosition pos = this; 101 while (pos != null) { 102 MetaUtil.appendLocation(sb.append("at "), pos.getMethod(), pos.getBCI()); 103 if (pos.receiver != null) { 104 sb.append("receiver=" + pos.receiver + " "); 105 } 106 pos = pos.getCaller(); 107 if (pos != null) { 108 sb.append(CodeUtil.NEW_LINE); 109 } 110 } 111 return sb.toString(); 112 } 113} 114