src/share/vm/runtime/signature.hpp

Tue, 23 Nov 2010 13:22:55 -0800

author
stefank
date
Tue, 23 Nov 2010 13:22:55 -0800
changeset 2314
f95d63e2154a
parent 2036
126ea7725993
child 2497
3582bf76420e
permissions
-rw-r--r--

6989984: Use standard include model for Hospot
Summary: Replaced MakeDeps and the includeDB files with more standardized solutions.
Reviewed-by: coleenp, kvn, kamg

     1 /*
     2  * Copyright (c) 1997, 2010, 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  *
    23  */
    25 #ifndef SHARE_VM_RUNTIME_SIGNATURE_HPP
    26 #define SHARE_VM_RUNTIME_SIGNATURE_HPP
    28 #include "memory/allocation.hpp"
    29 #include "oops/methodOop.hpp"
    30 #include "utilities/top.hpp"
    32 // SignatureIterators iterate over a Java signature (or parts of it).
    33 // (Syntax according to: "The Java Virtual Machine Specification" by
    34 // Tim Lindholm & Frank Yellin; section 4.3 Descriptors; p. 89ff.)
    35 //
    36 // Example: Iterating over ([Lfoo;D)I using
    37 //                         0123456789
    38 //
    39 // iterate_parameters() calls: do_array(2, 7); do_double();
    40 // iterate_returntype() calls:                              do_int();
    41 // iterate()            calls: do_array(2, 7); do_double(); do_int();
    42 //
    43 // is_return_type()        is: false         ; false      ; true
    44 //
    45 // NOTE: The new optimizer has an alternate, for-loop based signature
    46 // iterator implemented in opto/type.cpp, TypeTuple::make().
    48 class SignatureIterator: public ResourceObj {
    49  protected:
    50   symbolHandle _signature;             // the signature to iterate over
    51   int          _index;                 // the current character index (only valid during iteration)
    52   int          _parameter_index;       // the current parameter index (0 outside iteration phase)
    53   BasicType    _return_type;
    55   void expect(char c);
    56   void skip_optional_size();
    57   int  parse_type();                   // returns the parameter size in words (0 for void)
    58   void check_signature_end();
    60  public:
    61   // Definitions used in generating and iterating the
    62   // bit field form of the signature generated by the
    63   // Fingerprinter.
    64   enum {
    65     static_feature_size    = 1,
    66     result_feature_size    = 4,
    67     result_feature_mask    = 0xF,
    68     parameter_feature_size = 4,
    69     parameter_feature_mask = 0xF,
    71       bool_parm            = 1,
    72       byte_parm            = 2,
    73       char_parm            = 3,
    74       short_parm           = 4,
    75       int_parm             = 5,
    76       long_parm            = 6,
    77       float_parm           = 7,
    78       double_parm          = 8,
    79       obj_parm             = 9,
    80       done_parm            = 10,  // marker for end of parameters
    82     // max parameters is wordsize minus
    83     //    The sign bit, termination field, the result and static bit fields
    84     max_size_of_parameters = (BitsPerLong-1 -
    85                               result_feature_size - parameter_feature_size -
    86                               static_feature_size) / parameter_feature_size
    87   };
    89   // Constructors
    90   SignatureIterator(symbolOop signature);
    91   SignatureIterator(Thread *thread, symbolOop signature);
    92   SignatureIterator(symbolHandle signature);
    94   // Iteration
    95   void dispatch_field();               // dispatches once for field signatures
    96   void iterate_parameters();           // iterates over parameters only
    97   void iterate_parameters( uint64_t fingerprint );
    98   void iterate_returntype();           // iterates over returntype only
    99   void iterate();                      // iterates over whole signature
   100   // Returns the word index of the current parameter;
   101   int  parameter_index() const         { return _parameter_index; }
   102   bool is_return_type() const          { return parameter_index() < 0; }
   103   BasicType get_ret_type() const       { return _return_type; }
   105   // Basic types
   106   virtual void do_bool  ()             = 0;
   107   virtual void do_char  ()             = 0;
   108   virtual void do_float ()             = 0;
   109   virtual void do_double()             = 0;
   110   virtual void do_byte  ()             = 0;
   111   virtual void do_short ()             = 0;
   112   virtual void do_int   ()             = 0;
   113   virtual void do_long  ()             = 0;
   114   virtual void do_void  ()             = 0;
   116   // Object types (begin indexes the first character of the entry, end indexes the first character after the entry)
   117   virtual void do_object(int begin, int end) = 0;
   118   virtual void do_array (int begin, int end) = 0;
   119 };
   122 // Specialized SignatureIterators: Used to compute signature specific values.
   124 class SignatureTypeNames : public SignatureIterator {
   125  protected:
   126   virtual void type_name(const char* name)   = 0;
   128   void do_bool()                       { type_name("jboolean"); }
   129   void do_char()                       { type_name("jchar"   ); }
   130   void do_float()                      { type_name("jfloat"  ); }
   131   void do_double()                     { type_name("jdouble" ); }
   132   void do_byte()                       { type_name("jbyte"   ); }
   133   void do_short()                      { type_name("jshort"  ); }
   134   void do_int()                        { type_name("jint"    ); }
   135   void do_long()                       { type_name("jlong"   ); }
   136   void do_void()                       { type_name("void"    ); }
   137   void do_object(int begin, int end)   { type_name("jobject" ); }
   138   void do_array (int begin, int end)   { type_name("jobject" ); }
   140  public:
   141   SignatureTypeNames(symbolHandle signature) : SignatureIterator(signature) {}
   142 };
   145 class SignatureInfo: public SignatureIterator {
   146  protected:
   147   bool      _has_iterated;             // need this because iterate cannot be called in constructor (set is virtual!)
   148   bool      _has_iterated_return;
   149   int       _size;
   151   void lazy_iterate_parameters()       { if (!_has_iterated) { iterate_parameters(); _has_iterated = true; } }
   152   void lazy_iterate_return()           { if (!_has_iterated_return) { iterate_returntype(); _has_iterated_return = true; } }
   154   virtual void set(int size, BasicType type) = 0;
   156   void do_bool  ()                     { set(T_BOOLEAN_size, T_BOOLEAN); }
   157   void do_char  ()                     { set(T_CHAR_size   , T_CHAR   ); }
   158   void do_float ()                     { set(T_FLOAT_size  , T_FLOAT  ); }
   159   void do_double()                     { set(T_DOUBLE_size , T_DOUBLE ); }
   160   void do_byte  ()                     { set(T_BYTE_size   , T_BYTE   ); }
   161   void do_short ()                     { set(T_SHORT_size  , T_SHORT  ); }
   162   void do_int   ()                     { set(T_INT_size    , T_INT    ); }
   163   void do_long  ()                     { set(T_LONG_size   , T_LONG   ); }
   164   void do_void  ()                     { set(T_VOID_size   , T_VOID   ); }
   165   void do_object(int begin, int end)   { set(T_OBJECT_size , T_OBJECT ); }
   166   void do_array (int begin, int end)   { set(T_ARRAY_size  , T_ARRAY  ); }
   168  public:
   169   SignatureInfo(symbolHandle signature) : SignatureIterator(signature) {
   170     _has_iterated = _has_iterated_return = false;
   171     _size         = 0;
   172     _return_type  = T_ILLEGAL;
   173   }
   175 };
   178 // Specialized SignatureIterator: Used to compute the argument size.
   180 class ArgumentSizeComputer: public SignatureInfo {
   181  private:
   182   void set(int size, BasicType type)   { _size += size; }
   183  public:
   184   ArgumentSizeComputer(symbolHandle signature) : SignatureInfo(signature) {}
   186   int       size()                     { lazy_iterate_parameters(); return _size; }
   187 };
   190 class ArgumentCount: public SignatureInfo {
   191  private:
   192   void set(int size, BasicType type)   { _size ++; }
   193  public:
   194   ArgumentCount(symbolHandle signature) : SignatureInfo(signature) {}
   196   int       size()                     { lazy_iterate_parameters(); return _size; }
   197 };
   200 // Specialized SignatureIterator: Used to compute the result type.
   202 class ResultTypeFinder: public SignatureInfo {
   203  private:
   204   void set(int size, BasicType type)   { _return_type = type; }
   205  public:
   206   BasicType type()                     { lazy_iterate_return(); return _return_type; }
   208   ResultTypeFinder(symbolHandle signature) : SignatureInfo(signature) {}
   209 };
   212 // Fingerprinter computes a unique ID for a given method. The ID
   213 // is a bitvector characterizing the methods signature (incl. the receiver).
   214 class Fingerprinter: public SignatureIterator {
   215  private:
   216   uint64_t _fingerprint;
   217   int _shift_count;
   218   methodHandle mh;
   220  public:
   222   void do_bool()    { _fingerprint |= (((uint64_t)bool_parm) << _shift_count); _shift_count += parameter_feature_size; }
   223   void do_char()    { _fingerprint |= (((uint64_t)char_parm) << _shift_count); _shift_count += parameter_feature_size; }
   224   void do_byte()    { _fingerprint |= (((uint64_t)byte_parm) << _shift_count); _shift_count += parameter_feature_size; }
   225   void do_short()   { _fingerprint |= (((uint64_t)short_parm) << _shift_count); _shift_count += parameter_feature_size; }
   226   void do_int()     { _fingerprint |= (((uint64_t)int_parm) << _shift_count); _shift_count += parameter_feature_size; }
   227   void do_long()    { _fingerprint |= (((uint64_t)long_parm) << _shift_count); _shift_count += parameter_feature_size; }
   228   void do_float()   { _fingerprint |= (((uint64_t)float_parm) << _shift_count); _shift_count += parameter_feature_size; }
   229   void do_double()  { _fingerprint |= (((uint64_t)double_parm) << _shift_count); _shift_count += parameter_feature_size; }
   231   void do_object(int begin, int end)  { _fingerprint |= (((uint64_t)obj_parm) << _shift_count); _shift_count += parameter_feature_size; }
   232   void do_array (int begin, int end)  { _fingerprint |= (((uint64_t)obj_parm) << _shift_count); _shift_count += parameter_feature_size; }
   234   void do_void()    { ShouldNotReachHere(); }
   236   Fingerprinter(methodHandle method) : SignatureIterator(method->signature()) {
   237     mh = method;
   238     _fingerprint = 0;
   239   }
   241   Fingerprinter(Thread *thread, methodHandle method) : SignatureIterator(thread, method->signature()) {
   242     mh = method;
   243     _fingerprint = 0;
   244   }
   246   uint64_t fingerprint() {
   247     // See if we fingerprinted this method already
   248     if (mh->constMethod()->fingerprint() != CONST64(0)) {
   249       return mh->constMethod()->fingerprint();
   250     }
   252     if (mh->size_of_parameters() > max_size_of_parameters ) {
   253       _fingerprint = UCONST64(-1);
   254       mh->constMethod()->set_fingerprint(_fingerprint);
   255       return _fingerprint;
   256     }
   258     assert( (int)mh->result_type() <= (int)result_feature_mask, "bad result type");
   259     _fingerprint = mh->result_type();
   260     _fingerprint <<= static_feature_size;
   261     if (mh->is_static())  _fingerprint |= 1;
   262     _shift_count = result_feature_size + static_feature_size;
   263     iterate_parameters();
   264     _fingerprint |= ((uint64_t)done_parm) << _shift_count;// mark end of sig
   265     mh->constMethod()->set_fingerprint(_fingerprint);
   266     return _fingerprint;
   267   }
   268 };
   271 // Specialized SignatureIterator: Used for native call purposes
   273 class NativeSignatureIterator: public SignatureIterator {
   274  private:
   275   methodHandle _method;
   276 // We need separate JNI and Java offset values because in 64 bit mode,
   277 // the argument offsets are not in sync with the Java stack.
   278 // For example a long takes up 1 "C" stack entry but 2 Java stack entries.
   279   int          _offset;                // The java stack offset
   280   int          _prepended;             // number of prepended JNI parameters (1 JNIEnv, plus 1 mirror if static)
   281   int          _jni_offset;            // the current parameter offset, starting with 0
   283   void do_bool  ()                     { pass_int();    _jni_offset++; _offset++;       }
   284   void do_char  ()                     { pass_int();    _jni_offset++; _offset++;       }
   285   void do_float ()                     { pass_float();  _jni_offset++; _offset++;       }
   286 #ifdef _LP64
   287   void do_double()                     { pass_double(); _jni_offset++; _offset += 2;    }
   288 #else
   289   void do_double()                     { pass_double(); _jni_offset += 2; _offset += 2; }
   290 #endif
   291   void do_byte  ()                     { pass_int();    _jni_offset++; _offset++;       }
   292   void do_short ()                     { pass_int();    _jni_offset++; _offset++;       }
   293   void do_int   ()                     { pass_int();    _jni_offset++; _offset++;       }
   294 #ifdef _LP64
   295   void do_long  ()                     { pass_long();   _jni_offset++; _offset += 2;    }
   296 #else
   297   void do_long  ()                     { pass_long();   _jni_offset += 2; _offset += 2; }
   298 #endif
   299   void do_void  ()                     { ShouldNotReachHere();                               }
   300   void do_object(int begin, int end)   { pass_object(); _jni_offset++; _offset++;        }
   301   void do_array (int begin, int end)   { pass_object(); _jni_offset++; _offset++;        }
   303  public:
   304   methodHandle method() const          { return _method; }
   305   int          offset() const          { return _offset; }
   306   int      jni_offset() const          { return _jni_offset + _prepended; }
   307 //  int     java_offset() const          { return method()->size_of_parameters() - _offset - 1; }
   308   bool      is_static() const          { return method()->is_static(); }
   309   virtual void pass_int()              = 0;
   310   virtual void pass_long()             = 0;
   311   virtual void pass_object()           = 0;
   312   virtual void pass_float()            = 0;
   313 #ifdef _LP64
   314   virtual void pass_double()           = 0;
   315 #else
   316   virtual void pass_double()           { pass_long(); }  // may be same as long
   317 #endif
   319   NativeSignatureIterator(methodHandle method) : SignatureIterator(method->signature()) {
   320     _method = method;
   321     _offset = 0;
   322     _jni_offset = 0;
   324     const int JNIEnv_words = 1;
   325     const int mirror_words = 1;
   326     _prepended = !is_static() ? JNIEnv_words : JNIEnv_words + mirror_words;
   327   }
   329   // iterate() calles the 2 virtual methods according to the following invocation syntax:
   330   //
   331   // {pass_int | pass_long | pass_object}
   332   //
   333   // Arguments are handled from left to right (receiver first, if any).
   334   // The offset() values refer to the Java stack offsets but are 0 based and increasing.
   335   // The java_offset() values count down to 0, and refer to the Java TOS.
   336   // The jni_offset() values increase from 1 or 2, and refer to C arguments.
   338   void iterate() { iterate(Fingerprinter(method()).fingerprint());
   339   }
   342   // Optimized path if we have the bitvector form of signature
   343   void iterate( uint64_t fingerprint ) {
   345     if (!is_static()) {
   346       // handle receiver (not handled by iterate because not in signature)
   347       pass_object(); _jni_offset++; _offset++;
   348     }
   350     SignatureIterator::iterate_parameters( fingerprint );
   351   }
   352 };
   355 // Handy stream for iterating over signature
   357 class SignatureStream : public StackObj {
   358  private:
   359   symbolHandle _signature;
   360   int          _begin;
   361   int          _end;
   362   BasicType    _type;
   363   bool         _at_return_type;
   365  public:
   366   bool at_return_type() const                    { return _at_return_type; }
   367   bool is_done() const;
   368   void next_non_primitive(int t);
   369   void next() {
   370     symbolOop sig = _signature();
   371     int len = sig->utf8_length();
   372     if (_end >= len) {
   373       _end = len + 1;
   374       return;
   375     }
   377     _begin = _end;
   378     int t = sig->byte_at(_begin);
   379     switch (t) {
   380       case 'B': _type = T_BYTE;    break;
   381       case 'C': _type = T_CHAR;    break;
   382       case 'D': _type = T_DOUBLE;  break;
   383       case 'F': _type = T_FLOAT;   break;
   384       case 'I': _type = T_INT;     break;
   385       case 'J': _type = T_LONG;    break;
   386       case 'S': _type = T_SHORT;   break;
   387       case 'Z': _type = T_BOOLEAN; break;
   388       case 'V': _type = T_VOID;    break;
   389       default : next_non_primitive(t);
   390                 return;
   391     }
   392     _end++;
   393   }
   395   SignatureStream(symbolHandle signature,
   396                   bool is_method = true) :
   397                    _signature(signature), _at_return_type(false) {
   398     _begin = _end = (is_method ? 1 : 0);  // skip first '(' in method signatures
   399     next();
   400   }
   402   bool is_object() const;                        // True if this argument is an object
   403   bool is_array() const;                         // True if this argument is an array
   404   BasicType type() const                         { return _type; }
   405   symbolOop as_symbol(TRAPS);
   406   enum FailureMode { ReturnNull, CNFException, NCDFError };
   407   klassOop as_klass(Handle class_loader, Handle protection_domain, FailureMode failure_mode, TRAPS);
   408   oop as_java_mirror(Handle class_loader, Handle protection_domain, FailureMode failure_mode, TRAPS);
   410   // return same as_symbol except allocation of new symbols is avoided.
   411   symbolOop as_symbol_or_null();
   412 };
   414 class SignatureVerifier : public StackObj {
   415   public:
   416     // Returns true if the symbol is valid method or type signature
   417     static bool is_valid_signature(symbolHandle sig);
   419     static bool is_valid_method_signature(symbolHandle sig);
   420     static bool is_valid_type_signature(symbolHandle sig);
   421   private:
   423     static ssize_t is_valid_type(const char*, ssize_t);
   424     static bool invalid_name_char(char);
   425 };
   427 #endif // SHARE_VM_RUNTIME_SIGNATURE_HPP

mercurial