1
/*
2
 * $Id$
3
 *
4
 * Copyright (C) 2003-2009 JNode.org
5
 *
6
 * This library is free software; you can redistribute it and/or modify it
7
 * under the terms of the GNU Lesser General Public License as published
8
 * by the Free Software Foundation; either version 2.1 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * This library is distributed in the hope that it will be useful, but 
12
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
14
 * License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this library; If not, write to the Free Software Foundation, Inc., 
18
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
 */
20
 
21
package org.jnode.vm;
22
23
import org.jnode.vm.classmgr.VmByteCode;
24
import org.jnode.vm.classmgr.VmMethod;
25
import org.jnode.vm.classmgr.VmType;
26
27
/**
28
 * A VmFrame is the execution frame (locals & stack) for a method during
29
 * execution. Note that this class is not meant to be a normal java class,
30
 * instead it is a record structure that maps directly on how a method frame is
31
 * push on the stack by the compiler. Don't add any methods, since during
32
 * runtime instances of this class will have no header and thus no virtual
33
 * method table.
34
 */
35
public final class VmStackFrame extends VmSystemObject {
36
37
    public static final int MAGIC_MASK = 0xFFFFFF00;
38
    public static final int MAGIC_COMPILED = 0x21A52F00;
39
40
    /**
41
     * The method executing in this frame
42
     */
43
    private final VmMethod sfMethod;
44
    private final int programCounter;
45
46
    /**
47
     * Initialize this instance.
48
     *
49
     * @param src
50
     * @param reader
51
     */
52
    VmStackFrame(VmMethod method, int programCounter) {
53
        this.sfMethod = method;
54
        this.programCounter = programCounter;
55
    }
56
57
    /**
58
     * @return Returns the method.
59
     */
60
    public final VmMethod getMethod() {
61
        return this.sfMethod;
62
    }
63
64
    /**
65
     * Gets the line number of the current instruction of this frame.
66
     *
67
     * @return The line number, or -1 if not found.
68
     */
69
    public final String getLocationInfo() {
70
        int lineNo = -1;
71
        if (sfMethod != null) {
72
            final VmByteCode bc = sfMethod.getBytecode();
73
            if (bc != null) {
74
                lineNo = bc.getLineNr(programCounter);
75
            }
76
        }
77
        if (lineNo >= 0) {
78
            return String.valueOf(lineNo);
79
        } else {
80
            return "?";
81
        }
82
    }
83
84
    /**
85
     * Convert to a String representation.
86
     *
87
     * @see java.lang.Object#toString()
88
     */
89
    public String toString() {
90
        final VmMethod method = sfMethod;
91
        final VmType<?> vmClass = (method == null) ? null : method.getDeclaringClass();
92
        final String cname = (vmClass == null) ? "<unknown class>" : vmClass.getName();
93
        final String mname = (method == null) ? "<unknown method>" : method.getName();
94
        final String location = getLocationInfo();
95
96
        return cname + "!" + mname + " (" + location + ")";
97
    }
98
}