signature.hpp revision 7331:110ec5963eb1
1254721Semaste/*
2254721Semaste * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3254721Semaste * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4254721Semaste *
5254721Semaste * This code is free software; you can redistribute it and/or modify it
6254721Semaste * under the terms of the GNU General Public License version 2 only, as
7254721Semaste * published by the Free Software Foundation.
8254721Semaste *
9254721Semaste * This code is distributed in the hope that it will be useful, but WITHOUT
10254721Semaste * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11254721Semaste * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12254721Semaste * version 2 for more details (a copy is included in the LICENSE file that
13254721Semaste * accompanied this code).
14254721Semaste *
15254721Semaste * You should have received a copy of the GNU General Public License version
16254721Semaste * 2 along with this work; if not, write to the Free Software Foundation,
17254721Semaste * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18254721Semaste *
19254721Semaste * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20254721Semaste * or visit www.oracle.com if you need additional information or have any
21254721Semaste * questions.
22254721Semaste *
23254721Semaste */
24254721Semaste
25254721Semaste#ifndef SHARE_VM_RUNTIME_SIGNATURE_HPP
26254721Semaste#define SHARE_VM_RUNTIME_SIGNATURE_HPP
27254721Semaste
28254721Semaste#include "memory/allocation.hpp"
29254721Semaste#include "oops/method.hpp"
30254721Semaste#include "utilities/top.hpp"
31254721Semaste
32254721Semaste// SignatureIterators iterate over a Java signature (or parts of it).
33254721Semaste// (Syntax according to: "The Java Virtual Machine Specification" by
34254721Semaste// Tim Lindholm & Frank Yellin; section 4.3 Descriptors; p. 89ff.)
35254721Semaste//
36254721Semaste// Example: Iterating over ([Lfoo;D)I using
37254721Semaste//                         0123456789
38254721Semaste//
39254721Semaste// iterate_parameters() calls: do_array(2, 7); do_double();
40254721Semaste// iterate_returntype() calls:                              do_int();
41254721Semaste// iterate()            calls: do_array(2, 7); do_double(); do_int();
42254721Semaste//
43254721Semaste// is_return_type()        is: false         ; false      ; true
44254721Semaste//
45254721Semaste// NOTE: The new optimizer has an alternate, for-loop based signature
46254721Semaste// iterator implemented in opto/type.cpp, TypeTuple::make().
47254721Semaste
48254721Semasteclass SignatureIterator: public ResourceObj {
49254721Semaste protected:
50254721Semaste  Symbol*      _signature;             // the signature to iterate over
51254721Semaste  int          _index;                 // the current character index (only valid during iteration)
52254721Semaste  int          _parameter_index;       // the current parameter index (0 outside iteration phase)
53254721Semaste  BasicType    _return_type;
54254721Semaste
55254721Semaste  void expect(char c);
56254721Semaste  void skip_optional_size();
57254721Semaste  int  parse_type();                   // returns the parameter size in words (0 for void)
58254721Semaste  void check_signature_end();
59254721Semaste
60254721Semaste public:
61254721Semaste  // Definitions used in generating and iterating the
62254721Semaste  // bit field form of the signature generated by the
63254721Semaste  // Fingerprinter.
64254721Semaste  enum {
65254721Semaste    static_feature_size    = 1,
66254721Semaste    result_feature_size    = 4,
67254721Semaste    result_feature_mask    = 0xF,
68254721Semaste    parameter_feature_size = 4,
69254721Semaste    parameter_feature_mask = 0xF,
70254721Semaste
71254721Semaste      bool_parm            = 1,
72263367Semaste      byte_parm            = 2,
73263367Semaste      char_parm            = 3,
74263367Semaste      short_parm           = 4,
75263367Semaste      int_parm             = 5,
76263367Semaste      long_parm            = 6,
77263367Semaste      float_parm           = 7,
78263367Semaste      double_parm          = 8,
79263367Semaste      obj_parm             = 9,
80263367Semaste      done_parm            = 10,  // marker for end of parameters
81254721Semaste
82254721Semaste    // max parameters is wordsize minus
83254721Semaste    //    The sign bit, termination field, the result and static bit fields
84263367Semaste    max_size_of_parameters = (BitsPerLong-1 -
85263367Semaste                              result_feature_size - parameter_feature_size -
86254721Semaste                              static_feature_size) / parameter_feature_size
87254721Semaste  };
88254721Semaste
89254721Semaste  // Constructors
90254721Semaste  SignatureIterator(Symbol* signature);
91254721Semaste
92254721Semaste  // Iteration
93254721Semaste  void dispatch_field();               // dispatches once for field signatures
94254721Semaste  void iterate_parameters();           // iterates over parameters only
95254721Semaste  void iterate_parameters( uint64_t fingerprint );
96254721Semaste  void iterate_returntype();           // iterates over returntype only
97254721Semaste  void iterate();                      // iterates over whole signature
98254721Semaste  // Returns the word index of the current parameter;
99254721Semaste  int  parameter_index() const         { return _parameter_index; }
100254721Semaste  bool is_return_type() const          { return parameter_index() < 0; }
101254721Semaste  BasicType get_ret_type() const       { return _return_type; }
102254721Semaste
103254721Semaste  // Basic types
104254721Semaste  virtual void do_bool  ()             = 0;
105254721Semaste  virtual void do_char  ()             = 0;
106254721Semaste  virtual void do_float ()             = 0;
107254721Semaste  virtual void do_double()             = 0;
108254721Semaste  virtual void do_byte  ()             = 0;
109254721Semaste  virtual void do_short ()             = 0;
110254721Semaste  virtual void do_int   ()             = 0;
111254721Semaste  virtual void do_long  ()             = 0;
112254721Semaste  virtual void do_void  ()             = 0;
113254721Semaste
114254721Semaste  // Object types (begin indexes the first character of the entry, end indexes the first character after the entry)
115254721Semaste  virtual void do_object(int begin, int end) = 0;
116254721Semaste  virtual void do_array (int begin, int end) = 0;
117254721Semaste};
118254721Semaste
119254721Semaste
120254721Semaste// Specialized SignatureIterators: Used to compute signature specific values.
121254721Semaste
122254721Semasteclass SignatureTypeNames : public SignatureIterator {
123254721Semaste protected:
124254721Semaste  virtual void type_name(const char* name)   = 0;
125254721Semaste
126254721Semaste  void do_bool()                       { type_name("jboolean"); }
127254721Semaste  void do_char()                       { type_name("jchar"   ); }
128254721Semaste  void do_float()                      { type_name("jfloat"  ); }
129254721Semaste  void do_double()                     { type_name("jdouble" ); }
130254721Semaste  void do_byte()                       { type_name("jbyte"   ); }
131254721Semaste  void do_short()                      { type_name("jshort"  ); }
132254721Semaste  void do_int()                        { type_name("jint"    ); }
133254721Semaste  void do_long()                       { type_name("jlong"   ); }
134254721Semaste  void do_void()                       { type_name("void"    ); }
135254721Semaste  void do_object(int begin, int end)   { type_name("jobject" ); }
136254721Semaste  void do_array (int begin, int end)   { type_name("jobject" ); }
137254721Semaste
138254721Semaste public:
139254721Semaste  SignatureTypeNames(Symbol* signature) : SignatureIterator(signature) {}
140254721Semaste};
141254721Semaste
142254721Semaste
143254721Semasteclass SignatureInfo: public SignatureIterator {
144254721Semaste protected:
145254721Semaste  bool      _has_iterated;             // need this because iterate cannot be called in constructor (set is virtual!)
146254721Semaste  bool      _has_iterated_return;
147254721Semaste  int       _size;
148254721Semaste
149254721Semaste  void lazy_iterate_parameters()       { if (!_has_iterated) { iterate_parameters(); _has_iterated = true; } }
150254721Semaste  void lazy_iterate_return()           { if (!_has_iterated_return) { iterate_returntype(); _has_iterated_return = true; } }
151254721Semaste
152254721Semaste  virtual void set(int size, BasicType type) = 0;
153254721Semaste
154254721Semaste  void do_bool  ()                     { set(T_BOOLEAN_size, T_BOOLEAN); }
155254721Semaste  void do_char  ()                     { set(T_CHAR_size   , T_CHAR   ); }
156254721Semaste  void do_float ()                     { set(T_FLOAT_size  , T_FLOAT  ); }
157254721Semaste  void do_double()                     { set(T_DOUBLE_size , T_DOUBLE ); }
158254721Semaste  void do_byte  ()                     { set(T_BYTE_size   , T_BYTE   ); }
159254721Semaste  void do_short ()                     { set(T_SHORT_size  , T_SHORT  ); }
160254721Semaste  void do_int   ()                     { set(T_INT_size    , T_INT    ); }
161254721Semaste  void do_long  ()                     { set(T_LONG_size   , T_LONG   ); }
162254721Semaste  void do_void  ()                     { set(T_VOID_size   , T_VOID   ); }
163254721Semaste  void do_object(int begin, int end)   { set(T_OBJECT_size , T_OBJECT ); }
164254721Semaste  void do_array (int begin, int end)   { set(T_ARRAY_size  , T_ARRAY  ); }
165254721Semaste
166254721Semaste public:
167254721Semaste  SignatureInfo(Symbol* signature) : SignatureIterator(signature) {
168254721Semaste    _has_iterated = _has_iterated_return = false;
169254721Semaste    _size         = 0;
170254721Semaste    _return_type  = T_ILLEGAL;
171254721Semaste  }
172254721Semaste
173254721Semaste};
174263367Semaste
175263367Semaste
176254721Semaste// Specialized SignatureIterator: Used to compute the argument size.
177254721Semaste
178254721Semasteclass ArgumentSizeComputer: public SignatureInfo {
179254721Semaste private:
180254721Semaste  void set(int size, BasicType type)   { _size += size; }
181254721Semaste public:
182254721Semaste  ArgumentSizeComputer(Symbol* signature) : SignatureInfo(signature) {}
183254721Semaste
184254721Semaste  int       size()                     { lazy_iterate_parameters(); return _size; }
185254721Semaste};
186254721Semaste
187254721Semaste
188254721Semasteclass ArgumentCount: public SignatureInfo {
189254721Semaste private:
190254721Semaste  void set(int size, BasicType type)   { _size ++; }
191254721Semaste public:
192254721Semaste  ArgumentCount(Symbol* signature) : SignatureInfo(signature) {}
193254721Semaste
194254721Semaste  int       size()                     { lazy_iterate_parameters(); return _size; }
195254721Semaste};
196254721Semaste
197254721Semaste
198254721Semaste// Specialized SignatureIterator: Used to compute the result type.
199254721Semaste
200254721Semasteclass ResultTypeFinder: public SignatureInfo {
201254721Semaste private:
202254721Semaste  void set(int size, BasicType type)   { _return_type = type; }
203254721Semaste public:
204254721Semaste  BasicType type()                     { lazy_iterate_return(); return _return_type; }
205254721Semaste
206254721Semaste  ResultTypeFinder(Symbol* signature) : SignatureInfo(signature) {}
207254721Semaste};
208254721Semaste
209254721Semaste
210254721Semaste// Fingerprinter computes a unique ID for a given method. The ID
211254721Semaste// is a bitvector characterizing the methods signature (incl. the receiver).
212254721Semasteclass Fingerprinter: public SignatureIterator {
213263363Semaste private:
214254721Semaste  uint64_t _fingerprint;
215254721Semaste  int _shift_count;
216263367Semaste  methodHandle mh;
217263367Semaste
218254721Semaste public:
219254721Semaste
220254721Semaste  void do_bool()    { _fingerprint |= (((uint64_t)bool_parm) << _shift_count); _shift_count += parameter_feature_size; }
221254721Semaste  void do_char()    { _fingerprint |= (((uint64_t)char_parm) << _shift_count); _shift_count += parameter_feature_size; }
222254721Semaste  void do_byte()    { _fingerprint |= (((uint64_t)byte_parm) << _shift_count); _shift_count += parameter_feature_size; }
223254721Semaste  void do_short()   { _fingerprint |= (((uint64_t)short_parm) << _shift_count); _shift_count += parameter_feature_size; }
224254721Semaste  void do_int()     { _fingerprint |= (((uint64_t)int_parm) << _shift_count); _shift_count += parameter_feature_size; }
225263367Semaste  void do_long()    { _fingerprint |= (((uint64_t)long_parm) << _shift_count); _shift_count += parameter_feature_size; }
226263367Semaste  void do_float()   { _fingerprint |= (((uint64_t)float_parm) << _shift_count); _shift_count += parameter_feature_size; }
227254721Semaste  void do_double()  { _fingerprint |= (((uint64_t)double_parm) << _shift_count); _shift_count += parameter_feature_size; }
228254721Semaste
229254721Semaste  void do_object(int begin, int end)  { _fingerprint |= (((uint64_t)obj_parm) << _shift_count); _shift_count += parameter_feature_size; }
230254721Semaste  void do_array (int begin, int end)  { _fingerprint |= (((uint64_t)obj_parm) << _shift_count); _shift_count += parameter_feature_size; }
231254721Semaste
232254721Semaste  void do_void()    { ShouldNotReachHere(); }
233254721Semaste
234263367Semaste  Fingerprinter(methodHandle method) : SignatureIterator(method->signature()) {
235263367Semaste    mh = method;
236254721Semaste    _fingerprint = 0;
237254721Semaste  }
238254721Semaste
239254721Semaste  uint64_t fingerprint() {
240254721Semaste    // See if we fingerprinted this method already
241254721Semaste    if (mh->constMethod()->fingerprint() != CONST64(0)) {
242254721Semaste      return mh->constMethod()->fingerprint();
243254721Semaste    }
244
245    if (mh->size_of_parameters() > max_size_of_parameters ) {
246      _fingerprint = (uint64_t)CONST64(-1);
247      mh->constMethod()->set_fingerprint(_fingerprint);
248      return _fingerprint;
249    }
250
251    assert( (int)mh->result_type() <= (int)result_feature_mask, "bad result type");
252    _fingerprint = mh->result_type();
253    _fingerprint <<= static_feature_size;
254    if (mh->is_static())  _fingerprint |= 1;
255    _shift_count = result_feature_size + static_feature_size;
256    iterate_parameters();
257    _fingerprint |= ((uint64_t)done_parm) << _shift_count;// mark end of sig
258    mh->constMethod()->set_fingerprint(_fingerprint);
259    return _fingerprint;
260  }
261};
262
263
264// Specialized SignatureIterator: Used for native call purposes
265
266class NativeSignatureIterator: public SignatureIterator {
267 private:
268  methodHandle _method;
269// We need separate JNI and Java offset values because in 64 bit mode,
270// the argument offsets are not in sync with the Java stack.
271// For example a long takes up 1 "C" stack entry but 2 Java stack entries.
272  int          _offset;                // The java stack offset
273  int          _prepended;             // number of prepended JNI parameters (1 JNIEnv, plus 1 mirror if static)
274  int          _jni_offset;            // the current parameter offset, starting with 0
275
276  void do_bool  ()                     { pass_int();    _jni_offset++; _offset++;       }
277  void do_char  ()                     { pass_int();    _jni_offset++; _offset++;       }
278  void do_float ()                     { pass_float();  _jni_offset++; _offset++;       }
279#ifdef _LP64
280  void do_double()                     { pass_double(); _jni_offset++; _offset += 2;    }
281#else
282  void do_double()                     { pass_double(); _jni_offset += 2; _offset += 2; }
283#endif
284  void do_byte  ()                     { pass_int();    _jni_offset++; _offset++;       }
285  void do_short ()                     { pass_int();    _jni_offset++; _offset++;       }
286  void do_int   ()                     { pass_int();    _jni_offset++; _offset++;       }
287#ifdef _LP64
288  void do_long  ()                     { pass_long();   _jni_offset++; _offset += 2;    }
289#else
290  void do_long  ()                     { pass_long();   _jni_offset += 2; _offset += 2; }
291#endif
292  void do_void  ()                     { ShouldNotReachHere();                               }
293  void do_object(int begin, int end)   { pass_object(); _jni_offset++; _offset++;        }
294  void do_array (int begin, int end)   { pass_object(); _jni_offset++; _offset++;        }
295
296 public:
297  methodHandle method() const          { return _method; }
298  int          offset() const          { return _offset; }
299  int      jni_offset() const          { return _jni_offset + _prepended; }
300//  int     java_offset() const          { return method()->size_of_parameters() - _offset - 1; }
301  bool      is_static() const          { return method()->is_static(); }
302  virtual void pass_int()              = 0;
303  virtual void pass_long()             = 0;
304  virtual void pass_object()           = 0;
305  virtual void pass_float()            = 0;
306#ifdef _LP64
307  virtual void pass_double()           = 0;
308#else
309  virtual void pass_double()           { pass_long(); }  // may be same as long
310#endif
311
312  NativeSignatureIterator(methodHandle method) : SignatureIterator(method->signature()) {
313    _method = method;
314    _offset = 0;
315    _jni_offset = 0;
316
317    const int JNIEnv_words = 1;
318    const int mirror_words = 1;
319    _prepended = !is_static() ? JNIEnv_words : JNIEnv_words + mirror_words;
320  }
321
322  // iterate() calles the 2 virtual methods according to the following invocation syntax:
323  //
324  // {pass_int | pass_long | pass_object}
325  //
326  // Arguments are handled from left to right (receiver first, if any).
327  // The offset() values refer to the Java stack offsets but are 0 based and increasing.
328  // The java_offset() values count down to 0, and refer to the Java TOS.
329  // The jni_offset() values increase from 1 or 2, and refer to C arguments.
330
331  void iterate() { iterate(Fingerprinter(method()).fingerprint());
332  }
333
334
335  // Optimized path if we have the bitvector form of signature
336  void iterate( uint64_t fingerprint ) {
337
338    if (!is_static()) {
339      // handle receiver (not handled by iterate because not in signature)
340      pass_object(); _jni_offset++; _offset++;
341    }
342
343    SignatureIterator::iterate_parameters( fingerprint );
344  }
345};
346
347
348// Handy stream for iterating over signature
349
350class SignatureStream : public StackObj {
351 private:
352  Symbol*      _signature;
353  int          _begin;
354  int          _end;
355  BasicType    _type;
356  bool         _at_return_type;
357  GrowableArray<Symbol*>* _names;  // symbols created while parsing signature
358
359 public:
360  bool at_return_type() const                    { return _at_return_type; }
361  bool is_done() const;
362  void next_non_primitive(int t);
363  void next() {
364    Symbol* sig = _signature;
365    int len = sig->utf8_length();
366    if (_end >= len) {
367      _end = len + 1;
368      return;
369    }
370
371    _begin = _end;
372    int t = sig->byte_at(_begin);
373    switch (t) {
374      case 'B': _type = T_BYTE;    break;
375      case 'C': _type = T_CHAR;    break;
376      case 'D': _type = T_DOUBLE;  break;
377      case 'F': _type = T_FLOAT;   break;
378      case 'I': _type = T_INT;     break;
379      case 'J': _type = T_LONG;    break;
380      case 'S': _type = T_SHORT;   break;
381      case 'Z': _type = T_BOOLEAN; break;
382      case 'V': _type = T_VOID;    break;
383      default : next_non_primitive(t);
384                return;
385    }
386    _end++;
387  }
388
389  SignatureStream(Symbol* signature, bool is_method = true);
390  ~SignatureStream();
391
392  bool is_object() const;                        // True if this argument is an object
393  bool is_array() const;                         // True if this argument is an array
394  BasicType type() const                         { return _type; }
395  Symbol* as_symbol(TRAPS);
396  enum FailureMode { ReturnNull, CNFException, NCDFError };
397  Klass* as_klass(Handle class_loader, Handle protection_domain, FailureMode failure_mode, TRAPS);
398  oop as_java_mirror(Handle class_loader, Handle protection_domain, FailureMode failure_mode, TRAPS);
399  const jbyte* raw_bytes()  { return _signature->bytes() + _begin; }
400  int          raw_length() { return _end - _begin; }
401
402  // return same as_symbol except allocation of new symbols is avoided.
403  Symbol* as_symbol_or_null();
404
405  // count the number of references in the signature
406  int reference_parameter_count();
407};
408
409class SignatureVerifier : public StackObj {
410  public:
411    // Returns true if the symbol is valid method or type signature
412    static bool is_valid_signature(Symbol* sig);
413
414    static bool is_valid_method_signature(Symbol* sig);
415    static bool is_valid_type_signature(Symbol* sig);
416  private:
417
418    static ssize_t is_valid_type(const char*, ssize_t);
419    static bool invalid_name_char(char);
420};
421
422#endif // SHARE_VM_RUNTIME_SIGNATURE_HPP
423