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 jdk.vm.ci.code.site;
24
25import java.util.Map;
26import java.util.Objects;
27
28import jdk.vm.ci.code.BytecodePosition;
29import jdk.vm.ci.code.DebugInfo;
30import jdk.vm.ci.code.ReferenceMap;
31import jdk.vm.ci.code.Register;
32import jdk.vm.ci.code.RegisterSaveLayout;
33import jdk.vm.ci.meta.MetaUtil;
34
35/**
36 * Represents an infopoint with associated debug info. Note that safepoints are also infopoints.
37 */
38public class Infopoint extends Site implements Comparable<Infopoint> {
39
40    public final DebugInfo debugInfo;
41
42    public final InfopointReason reason;
43
44    public Infopoint(int pcOffset, DebugInfo debugInfo, InfopointReason reason) {
45        super(pcOffset);
46        this.debugInfo = debugInfo;
47        this.reason = reason;
48    }
49
50    @Override
51    public String toString() {
52        StringBuilder sb = new StringBuilder();
53        sb.append(pcOffset);
54        sb.append("[<infopoint>]");
55        appendDebugInfo(sb, debugInfo);
56        return sb.toString();
57    }
58
59    @Override
60    public int compareTo(Infopoint o) {
61        if (pcOffset < o.pcOffset) {
62            return -1;
63        } else if (pcOffset > o.pcOffset) {
64            return 1;
65        }
66        return this.reason.compareTo(o.reason);
67    }
68
69    @Override
70    public boolean equals(Object obj) {
71        if (this == obj) {
72            return true;
73        }
74        if (obj != null && obj.getClass() == getClass()) {
75            Infopoint that = (Infopoint) obj;
76            if (this.pcOffset == that.pcOffset && Objects.equals(this.debugInfo, that.debugInfo) && Objects.equals(this.reason, that.reason)) {
77                return true;
78            }
79        }
80        return false;
81    }
82
83    protected static void appendDebugInfo(StringBuilder sb, DebugInfo info) {
84        if (info != null) {
85            ReferenceMap refMap = info.getReferenceMap();
86            if (refMap != null) {
87                sb.append(refMap.toString());
88                sb.append(']');
89            }
90            RegisterSaveLayout calleeSaveInfo = info.getCalleeSaveInfo();
91            if (calleeSaveInfo != null) {
92                sb.append(" callee-save-info[");
93                String sep = "";
94                for (Map.Entry<Register, Integer> e : calleeSaveInfo.registersToSlots(true).entrySet()) {
95                    sb.append(sep).append(e.getKey()).append("->").append(e.getValue());
96                    sep = ", ";
97                }
98                sb.append(']');
99            }
100            BytecodePosition codePos = info.getBytecodePosition();
101            if (codePos != null) {
102                MetaUtil.appendLocation(sb.append(" "), codePos.getMethod(), codePos.getBCI());
103                if (info.hasFrame()) {
104                    sb.append(" #locals=").append(info.frame().numLocals).append(" #expr=").append(info.frame().numStack);
105                    if (info.frame().numLocks > 0) {
106                        sb.append(" #locks=").append(info.frame().numLocks);
107                    }
108                }
109            }
110        }
111    }
112}
113