aboutsummaryrefslogtreecommitdiff
path: root/agent/src/share/classes/sun/jvm/hotspot/runtime/SignatureIterator.java
blob: faf5aaf34b7f5ad512c7dc10f3d9b83801816dd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * Copyright 2001-2010 Sun Microsystems, Inc.  All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
 * CA 95054 USA or visit www.sun.com if you need additional information or
 * have any questions.
 *
 */

package sun.jvm.hotspot.runtime;

import sun.jvm.hotspot.oops.*;

/** <P> SignatureIterators iterate over a Java signature (or parts of it).
    (Syntax according to: "The Java Virtual Machine Specification" by
    Tim Lindholm & Frank Yellin; section 4.3 Descriptors; p. 89ff.) </P>

    <P> Example: Iterating over
<PRE>
([Lfoo;D)I
0123456789
</PRE>

    using </P>

<PRE>
iterateParameters() calls: do_array(2, 7); do_double();
iterateReturntype() calls:                              do_int();
iterate()           calls: do_array(2, 7); do_double(); do_int();

is_returnType()        is: false         ; false      ; true
</PRE>
*/

public abstract class SignatureIterator {
  protected Symbol _signature;       // the signature to iterate over
  protected int    _index;           // the current character index (only valid during iteration)
  protected int    _parameter_index; // the current parameter index (0 outside iteration phase)

  protected void expect(char c) {
    if (_signature.getByteAt(_index) != (byte) c) {
      throw new RuntimeException("expecting '" + c + "'");
    }
    _index++;
  }
  protected void skipOptionalSize() {
    byte c = _signature.getByteAt(_index);
    while ('0' <= c && c <= '9') {
      c = _signature.getByteAt(++_index);
    }
  }
  // returns the parameter size in words (0 for void)
  protected int parseType() {
    switch(_signature.getByteAt(_index)) {
    case 'B': doByte  (); _index++; return BasicTypeSize.getTByteSize();
    case 'C': doChar  (); _index++; return BasicTypeSize.getTCharSize();
    case 'D': doDouble(); _index++; return BasicTypeSize.getTDoubleSize();
    case 'F': doFloat (); _index++; return BasicTypeSize.getTFloatSize();
    case 'I': doInt   (); _index++; return BasicTypeSize.getTIntSize();
    case 'J': doLong  (); _index++; return BasicTypeSize.getTLongSize();
    case 'S': doShort (); _index++; return BasicTypeSize.getTShortSize();
    case 'Z': doBool  (); _index++; return BasicTypeSize.getTBooleanSize();
    case 'V':
      {
        if (!isReturnType()) {
          throw new RuntimeException("illegal parameter type V (void)");
        }

        doVoid(); _index++;
        return BasicTypeSize.getTVoidSize();
      }
    case 'L':
      {
        int begin = ++_index;
        while (_signature.getByteAt(_index++) != ';') ;
        doObject(begin, _index);
        return BasicTypeSize.getTObjectSize();
      }
    case '[':
      {
        int begin = ++_index;
        skipOptionalSize();
        while (_signature.getByteAt(_index) == '[') {
          _index++;
          skipOptionalSize();
        }
        if (_signature.getByteAt(_index) == 'L') {
          while (_signature.getByteAt(_index++) != ';') ;
        } else {
          _index++;
        }
        doArray(begin, _index);
        return BasicTypeSize.getTArraySize();
      }
    }
    throw new RuntimeException("Should not reach here: char " + (char)_signature.getByteAt(_index) + " @ " + _index + " in " + _signature.asString());
  }
  protected void checkSignatureEnd() {
    if (_index < _signature.getLength()) {
      System.err.println("too many chars in signature");
      _signature.printValueOn(System.err);
      System.err.println(" @ " + _index);
    }
  }

  public SignatureIterator(Symbol signature) {
    _signature       = signature;
    _parameter_index = 0;
  }

  //
  // Iteration
  //

  // dispatches once for field signatures
  public void dispatchField() {
    // no '(', just one (field) type
    _index = 0;
    _parameter_index = 0;
    parseType();
    checkSignatureEnd();
  }

  // iterates over parameters only
  public void iterateParameters() {
    // Parse parameters
    _index = 0;
    _parameter_index = 0;
    expect('(');
    while (_signature.getByteAt(_index) != ')') {
      _parameter_index += parseType();
    }
    expect(')');
    _parameter_index = 0; // so isReturnType() is false outside iteration
  }

  // iterates over returntype only
  public void iterateReturntype() {
    // Ignore parameters
    _index = 0;
    expect('(');
    while (_signature.getByteAt(_index) != ')') {
      _index++;
    }
    expect(')');
    // Parse return type
    _parameter_index = -1;
    parseType();
    checkSignatureEnd();
    _parameter_index = 0; // so isReturnType() is false outside iteration
  }

  // iterates over whole signature
  public void iterate() {
    // Parse parameters
    _index = 0;
    _parameter_index = 0;
    expect('(');
    while (_signature.getByteAt(_index) != ')') {
      _parameter_index += parseType();
    }
    expect(')');
    // Parse return type
    _parameter_index = -1;
    parseType();
    checkSignatureEnd();
    _parameter_index = 0; // so isReturnType() is false outside iteration
  }

  // Returns the word index of the current parameter; returns a negative value at the return type
  public int  parameterIndex()               { return _parameter_index; }
  public boolean isReturnType()              { return (parameterIndex() < 0); }

  // Basic types
  public abstract void doBool  ();
  public abstract void doChar  ();
  public abstract void doFloat ();
  public abstract void doDouble();
  public abstract void doByte  ();
  public abstract void doShort ();
  public abstract void doInt   ();
  public abstract void doLong  ();
  public abstract void doVoid  ();

  // Object types (begin indexes the first character of the entry, end
  // indexes the first character after the entry)
  public abstract void doObject(int begin, int end);
  public abstract void doArray (int begin, int end);
}