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 */ 24 25 #ifndef SHARE_RUNTIME_SIGNATURE_HPP 26 #define SHARE_RUNTIME_SIGNATURE_HPP 27 28 #include "memory/allocation.hpp" 29 #include "oops/method.hpp" 30 31 // SignatureIterators iterate over a Java signature (or parts of it). 32 // (Syntax according to: "The Java Virtual Machine Specification" by 33 // Tim Lindholm & Frank Yellin; section 4.3 Descriptors; p. 89ff.) 34 // 35 // Example: Iterating over ([Lfoo;D)I using 36 // 0123456789 37 // 38 // iterate_parameters() calls: do_array(2, 7); do_double(); 39 // iterate_returntype() calls: do_int(); 40 // iterate() calls: do_array(2, 7); do_double(); do_int(); 41 // 42 // is_return_type() is: false ; false ; true 43 // 44 // NOTE: The new optimizer has an alternate, for-loop based signature 45 // iterator implemented in opto/type.cpp, TypeTuple::make(). 46 47 class SignatureIterator: public ResourceObj { 48 protected: 49 Symbol* _signature; // the signature to iterate over 50 int _index; // the current character index (only valid during iteration) 51 int _parameter_index; // the current parameter index (0 outside iteration phase) 52 BasicType _return_type; 53 54 void expect(char c); 55 int parse_type(); // returns the parameter size in words (0 for void) 56 void check_signature_end(); 57 58 public: 59 // Definitions used in generating and iterating the 60 // bit field form of the signature generated by the 61 // Fingerprinter. 62 enum { 63 static_feature_size = 1, 64 is_static_bit = 1, 65 66 result_feature_size = 4, 67 result_feature_mask = 0xF, 68 parameter_feature_size = 4, 69 parameter_feature_mask = 0xF, 70 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 81 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 }; 88 89 // Constructors 90 SignatureIterator(Symbol* signature); 91 92 // Iteration 93 void iterate_parameters(); // iterates over parameters only 94 void iterate_parameters( uint64_t fingerprint ); 95 void iterate_returntype(); // iterates over returntype only 96 void iterate(); // iterates over whole signature 97 // Returns the word index of the current parameter; 98 int parameter_index() const { return _parameter_index; } 99 bool is_return_type() const { return parameter_index() < 0; } 100 BasicType get_ret_type() const { return _return_type; } 101 102 // Basic types 103 virtual void do_bool () = 0; 104 virtual void do_char () = 0; 105 virtual void do_float () = 0; 106 virtual void do_double() = 0; 107 virtual void do_byte () = 0; 108 virtual void do_short () = 0; 109 virtual void do_int () = 0; 110 virtual void do_long () = 0; 111 virtual void do_void () = 0; 112 113 // Object types (begin indexes the first character of the entry, end indexes the first character after the entry) 114 virtual void do_object(int begin, int end) = 0; 115 virtual void do_array (int begin, int end) = 0; 116 117 static bool is_static(uint64_t fingerprint) { 118 assert(fingerprint != (uint64_t)CONST64(-1), "invalid fingerprint"); 119 return fingerprint & is_static_bit; 120 } 121 static BasicType return_type(uint64_t fingerprint) { 122 assert(fingerprint != (uint64_t)CONST64(-1), "invalid fingerprint"); 123 return (BasicType) ((fingerprint >> static_feature_size) & result_feature_mask); 124 } 125 }; 126 127 128 // Specialized SignatureIterators: Used to compute signature specific values. 129 130 class SignatureTypeNames : public SignatureIterator { 131 protected: 132 virtual void type_name(const char* name) = 0; 133 134 void do_bool() { type_name("jboolean"); } 135 void do_char() { type_name("jchar" ); } 136 void do_float() { type_name("jfloat" ); } 137 void do_double() { type_name("jdouble" ); } 138 void do_byte() { type_name("jbyte" ); } 139 void do_short() { type_name("jshort" ); } 140 void do_int() { type_name("jint" ); } 141 void do_long() { type_name("jlong" ); } 142 void do_void() { type_name("void" ); } 143 void do_object(int begin, int end) { type_name("jobject" ); } 144 void do_array (int begin, int end) { type_name("jobject" ); } 145 146 public: 147 SignatureTypeNames(Symbol* signature) : SignatureIterator(signature) {} 148 }; 149 150 151 class SignatureInfo: public SignatureIterator { 152 protected: 153 bool _has_iterated; // need this because iterate cannot be called in constructor (set is virtual!) 154 bool _has_iterated_return; 155 int _size; 156 157 void lazy_iterate_parameters() { if (!_has_iterated) { iterate_parameters(); _has_iterated = true; } } 158 void lazy_iterate_return() { if (!_has_iterated_return) { iterate_returntype(); _has_iterated_return = true; } } 159 160 virtual void set(int size, BasicType type) = 0; 161 162 void do_bool () { set(T_BOOLEAN_size, T_BOOLEAN); } 163 void do_char () { set(T_CHAR_size , T_CHAR ); } 164 void do_float () { set(T_FLOAT_size , T_FLOAT ); } 165 void do_double() { set(T_DOUBLE_size , T_DOUBLE ); } 166 void do_byte () { set(T_BYTE_size , T_BYTE ); } 167 void do_short () { set(T_SHORT_size , T_SHORT ); } 168 void do_int () { set(T_INT_size , T_INT ); } 169 void do_long () { set(T_LONG_size , T_LONG ); } 170 void do_void () { set(T_VOID_size , T_VOID ); } 171 void do_object(int begin, int end) { set(T_OBJECT_size , T_OBJECT ); } 172 void do_array (int begin, int end) { set(T_ARRAY_size , T_ARRAY ); } 173 174 public: 175 SignatureInfo(Symbol* signature) : SignatureIterator(signature) { 176 _has_iterated = _has_iterated_return = false; 177 _size = 0; 178 _return_type = T_ILLEGAL; 179 } 180 181 }; 182 183 184 // Specialized SignatureIterator: Used to compute the argument size. 185 186 class ArgumentSizeComputer: public SignatureInfo { 187 private: 188 void set(int size, BasicType type) { _size += size; } 189 public: 190 ArgumentSizeComputer(Symbol* signature) : SignatureInfo(signature) {} 191 192 int size() { lazy_iterate_parameters(); return _size; } 193 }; 194 195 196 class ArgumentCount: public SignatureInfo { 197 private: 198 void set(int size, BasicType type) { _size ++; } 199 public: 200 ArgumentCount(Symbol* signature) : SignatureInfo(signature) {} 201 202 int size() { lazy_iterate_parameters(); return _size; } 203 }; 204 205 206 // Specialized SignatureIterator: Used to compute the result type. 207 208 class ResultTypeFinder: public SignatureInfo { 209 private: 210 void set(int size, BasicType type) { _return_type = type; } 211 public: 212 BasicType type() { lazy_iterate_return(); return _return_type; } 213 214 ResultTypeFinder(Symbol* signature) : SignatureInfo(signature) {} 215 }; 216 217 218 // Fingerprinter computes a unique ID for a given method. The ID 219 // is a bitvector characterizing the methods signature (incl. the receiver). 220 class Fingerprinter: public SignatureIterator { 221 private: 222 uint64_t _fingerprint; 223 int _shift_count; 224 methodHandle mh; 225 226 public: 227 228 void do_bool() { _fingerprint |= (((uint64_t)bool_parm) << _shift_count); _shift_count += parameter_feature_size; } 229 void do_char() { _fingerprint |= (((uint64_t)char_parm) << _shift_count); _shift_count += parameter_feature_size; } 230 void do_byte() { _fingerprint |= (((uint64_t)byte_parm) << _shift_count); _shift_count += parameter_feature_size; } 231 void do_short() { _fingerprint |= (((uint64_t)short_parm) << _shift_count); _shift_count += parameter_feature_size; } 232 void do_int() { _fingerprint |= (((uint64_t)int_parm) << _shift_count); _shift_count += parameter_feature_size; } 233 void do_long() { _fingerprint |= (((uint64_t)long_parm) << _shift_count); _shift_count += parameter_feature_size; } 234 void do_float() { _fingerprint |= (((uint64_t)float_parm) << _shift_count); _shift_count += parameter_feature_size; } 235 void do_double() { _fingerprint |= (((uint64_t)double_parm) << _shift_count); _shift_count += parameter_feature_size; } 236 237 void do_object(int begin, int end) { _fingerprint |= (((uint64_t)obj_parm) << _shift_count); _shift_count += parameter_feature_size; } 238 void do_array (int begin, int end) { _fingerprint |= (((uint64_t)obj_parm) << _shift_count); _shift_count += parameter_feature_size; } 239 240 void do_void() { ShouldNotReachHere(); } 241 242 Fingerprinter(const methodHandle& method) : SignatureIterator(method->signature()) { 243 mh = method; 244 _fingerprint = 0; 245 } 246 247 uint64_t fingerprint() { 248 // See if we fingerprinted this method already 249 if (mh->constMethod()->fingerprint() != CONST64(0)) { 250 return mh->constMethod()->fingerprint(); 251 } 252 253 if (mh->size_of_parameters() > max_size_of_parameters ) { 254 _fingerprint = (uint64_t)CONST64(-1); 255 mh->constMethod()->set_fingerprint(_fingerprint); 256 return _fingerprint; 257 } 258 259 assert( (int)mh->result_type() <= (int)result_feature_mask, "bad result type"); 260 _fingerprint = mh->result_type(); 261 _fingerprint <<= static_feature_size; 262 if (mh->is_static()) _fingerprint |= 1; 263 _shift_count = result_feature_size + static_feature_size; 264 iterate_parameters(); 265 _fingerprint |= ((uint64_t)done_parm) << _shift_count;// mark end of sig 266 mh->constMethod()->set_fingerprint(_fingerprint); 267 return _fingerprint; 268 } 269 }; 270 271 272 // Specialized SignatureIterator: Used for native call purposes 273 274 class NativeSignatureIterator: public SignatureIterator { 275 private: 276 methodHandle _method; 277 // We need separate JNI and Java offset values because in 64 bit mode, 278 // the argument offsets are not in sync with the Java stack. 279 // For example a long takes up 1 "C" stack entry but 2 Java stack entries. 280 int _offset; // The java stack offset 281 int _prepended; // number of prepended JNI parameters (1 JNIEnv, plus 1 mirror if static) 282 int _jni_offset; // the current parameter offset, starting with 0 283 284 void do_bool () { pass_int(); _jni_offset++; _offset++; } 285 void do_char () { pass_int(); _jni_offset++; _offset++; } 286 void do_float () { pass_float(); _jni_offset++; _offset++; } 287 #ifdef _LP64 288 void do_double() { pass_double(); _jni_offset++; _offset += 2; } 289 #else 290 void do_double() { pass_double(); _jni_offset += 2; _offset += 2; } 291 #endif 292 void do_byte () { pass_int(); _jni_offset++; _offset++; } 293 void do_short () { pass_int(); _jni_offset++; _offset++; } 294 void do_int () { pass_int(); _jni_offset++; _offset++; } 295 #ifdef _LP64 296 void do_long () { pass_long(); _jni_offset++; _offset += 2; } 297 #else 298 void do_long () { pass_long(); _jni_offset += 2; _offset += 2; } 299 #endif 300 void do_void () { ShouldNotReachHere(); } 301 void do_object(int begin, int end) { pass_object(); _jni_offset++; _offset++; } 302 void do_array (int begin, int end) { pass_object(); _jni_offset++; _offset++; } 303 304 public: 305 methodHandle method() const { return _method; } 306 int offset() const { return _offset; } 307 int jni_offset() const { return _jni_offset + _prepended; } 308 // int java_offset() const { return method()->size_of_parameters() - _offset - 1; } 309 bool is_static() const { return method()->is_static(); } 310 virtual void pass_int() = 0; 311 virtual void pass_long() = 0; 312 virtual void pass_object() = 0; 313 virtual void pass_float() = 0; 314 #ifdef _LP64 315 virtual void pass_double() = 0; 316 #else 317 virtual void pass_double() { pass_long(); } // may be same as long 318 #endif 319 320 NativeSignatureIterator(const methodHandle& method) : SignatureIterator(method->signature()) { 321 _method = method; 322 _offset = 0; 323 _jni_offset = 0; 324 325 const int JNIEnv_words = 1; 326 const int mirror_words = 1; 327 _prepended = !is_static() ? JNIEnv_words : JNIEnv_words + mirror_words; 328 } 329 330 // iterate() calles the 2 virtual methods according to the following invocation syntax: 331 // 332 // {pass_int | pass_long | pass_object} 333 // 334 // Arguments are handled from left to right (receiver first, if any). 335 // The offset() values refer to the Java stack offsets but are 0 based and increasing. 336 // The java_offset() values count down to 0, and refer to the Java TOS. 337 // The jni_offset() values increase from 1 or 2, and refer to C arguments. 338 339 void iterate() { iterate(Fingerprinter(method()).fingerprint()); 340 } 341 342 343 // Optimized path if we have the bitvector form of signature 344 void iterate( uint64_t fingerprint ) { 345 346 if (!is_static()) { 347 // handle receiver (not handled by iterate because not in signature) 348 pass_object(); _jni_offset++; _offset++; 349 } 350 351 SignatureIterator::iterate_parameters( fingerprint ); 352 } 353 }; 354 355 356 // Handy stream for iterating over signature 357 358 class SignatureStream : public StackObj { 359 private: 360 Symbol* _signature; 361 int _begin; 362 int _end; 363 BasicType _type; 364 bool _at_return_type; 365 Symbol* _previous_name; // cache the previously looked up symbol to avoid lookups 366 GrowableArray<Symbol*>* _names; // symbols created while parsing that need to be dereferenced 367 public: 368 bool at_return_type() const { return _at_return_type; } 369 bool is_done() const; 370 void next_non_primitive(int t); 371 void next() { 372 Symbol* sig = _signature; 373 int len = sig->utf8_length(); 374 if (_end >= len) { 375 _end = len + 1; 376 return; 377 } 378 379 _begin = _end; 380 int t = sig->char_at(_begin); 381 switch (t) { 382 case JVM_SIGNATURE_BYTE: _type = T_BYTE; break; 383 case JVM_SIGNATURE_CHAR: _type = T_CHAR; break; 384 case JVM_SIGNATURE_DOUBLE: _type = T_DOUBLE; break; 385 case JVM_SIGNATURE_FLOAT: _type = T_FLOAT; break; 386 case JVM_SIGNATURE_INT: _type = T_INT; break; 387 case JVM_SIGNATURE_LONG: _type = T_LONG; break; 388 case JVM_SIGNATURE_SHORT: _type = T_SHORT; break; 389 case JVM_SIGNATURE_BOOLEAN: _type = T_BOOLEAN; break; 390 case JVM_SIGNATURE_VOID: _type = T_VOID; break; 391 default : next_non_primitive(t); 392 return; 393 } 394 _end++; 395 } 396 397 SignatureStream(Symbol* signature, bool is_method = true); 398 ~SignatureStream(); 399 400 bool is_object() const; // True if this argument is an object 401 bool is_array() const; // True if this argument is an array 402 BasicType type() const { return _type; } 403 Symbol* as_symbol(); 404 enum FailureMode { ReturnNull, NCDFError }; 405 Klass* as_klass(Handle class_loader, Handle protection_domain, FailureMode failure_mode, TRAPS); 406 oop as_java_mirror(Handle class_loader, Handle protection_domain, FailureMode failure_mode, TRAPS); 407 const u1* raw_bytes() { return _signature->bytes() + _begin; } 408 int raw_length() { return _end - _begin; } 409 410 // return same as_symbol except allocation of new symbols is avoided. 411 Symbol* as_symbol_or_null(); 412 413 // count the number of references in the signature 414 int reference_parameter_count(); 415 }; 416 417 #ifdef ASSERT 418 class SignatureVerifier : public StackObj { 419 public: 420 static bool is_valid_method_signature(Symbol* sig); 421 static bool is_valid_type_signature(Symbol* sig); 422 private: 423 static ssize_t is_valid_type(const char*, ssize_t); 424 }; 425 #endif 426 #endif // SHARE_RUNTIME_SIGNATURE_HPP | 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 */ 24 25 #ifndef SHARE_RUNTIME_SIGNATURE_HPP 26 #define SHARE_RUNTIME_SIGNATURE_HPP 27 28 #include "classfile/symbolTable.hpp" 29 #include "memory/allocation.hpp" 30 #include "oops/method.hpp" 31 32 33 // Static routines and parsing loops for processing field and method 34 // descriptors. In the HotSpot sources we call them "signatures". 35 // 36 // A SignatureStream iterates over a Java descriptor (or parts of it). 37 // The syntax is documented in the Java Virtual Machine Specification, 38 // section 4.3. 39 // 40 // The syntax may be summarized as follows: 41 // 42 // MethodType: '(' {FieldType}* ')' (FieldType | 'V') 43 // FieldType: PrimitiveType | ObjectType | ArrayType 44 // PrimitiveType: 'B' | 'C' | 'D' | 'F' | 'I' | 'J' | 'S' | 'Z' 45 // ObjectType: ('L' | 'Q') ClassName ';' | ArrayType 46 // ArrayType: '[' FieldType 47 // ClassName: {UnqualifiedName '/'}* UnqualifiedName 48 // UnqualifiedName: NameChar {NameChar}* 49 // NameChar: ANY_CHAR_EXCEPT('/' | '.' | ';' | '[') 50 // 51 // All of the concrete characters in the above grammar are given 52 // standard manifest constant names of the form JVM_SIGNATURE_x. 53 // Executable code uses these constant names in preference to raw 54 // character constants. Comments and assertion code sometimes use 55 // the raw character constants for brevity. 56 // 57 // The primitive field types (like 'I') correspond 1-1 with type codes 58 // (like T_INT) which part of the specfication of the 'newarray' 59 // instruction (JVMS 6.5, section on newarray). These type codes are 60 // widely used in the HotSpot code. They are joined by ad hoc codes 61 // like T_OBJECT and T_ARRAY (defined in HotSpot but not in the JVMS) 62 // so that each "basic type" of field descriptor (or void return type) 63 // has a corresponding T_x code. Thus, while T_x codes play a very 64 // minor role in the JVMS, they play a major role in the HotSpot 65 // sources. There are fewer than 16 such "basic types", so they fit 66 // nicely into bitfields. 67 // 68 // The syntax of ClassName overlaps slightly with the descriptor 69 // syntaxes. The strings "I" and "(I)V" are both class names 70 // *and* descriptors. If a class name contains any character other 71 // than "BCDFIJSZ()V" it cannot be confused with a descriptor. 72 // Class names inside of descriptors are always contained in an 73 // "envelope" syntax which starts with 'L' (or 'Q') and ends with ';'. 74 // 75 // As a confounding factor, array types report their type name strings 76 // in descriptor format. These name strings are easy to recognize, 77 // since they begin with '['. For this reason some API points on 78 // HotSpot look for array descriptors as well as proper class names. 79 // 80 // For historical reasons some API points that accept class names and 81 // array names also look for class names wrapped inside an envelope 82 // (like "LFoo;") and unwrap them on the fly (to a name like "Foo"). 83 84 class Signature : AllStatic { 85 private: 86 static bool is_valid_array_signature(const Symbol* sig); 87 88 public: 89 90 // Returns the basic type of a field signature (or T_VOID for "V"). 91 // Assumes the signature is a valid field descriptor. 92 // Do not apply this function to class names or method signatures. 93 static BasicType basic_type(const Symbol* signature) { 94 return basic_type(signature->char_at(0)); 95 } 96 97 // Returns T_ILLEGAL for an illegal signature char. 98 static BasicType basic_type(int ch); 99 100 // Assuming it is either a class name or signature, 101 // determine if it (in fact) cannot be a class name. 102 // This means it either starts with '[' or ends with ';' 103 static bool not_class_name(const Symbol* signature) { 104 return (signature->starts_with(JVM_SIGNATURE_ARRAY) || 105 signature->ends_with(JVM_SIGNATURE_ENDCLASS)); 106 } 107 108 // Assuming it is either a class name or signature, 109 // determine if it (in fact) is an array descriptor. 110 static bool is_array(const Symbol* signature) { 111 return (signature->utf8_length() > 1 && 112 signature->char_at(0) == JVM_SIGNATURE_ARRAY && 113 is_valid_array_signature(signature)); 114 } 115 116 // Assuming it is either a class name or signature, 117 // determine if it contains a class name plus ';'. 118 static bool has_envelope(const Symbol* signature) { 119 return ((signature->utf8_length() > 0) && 120 signature->ends_with(JVM_SIGNATURE_ENDCLASS) && 121 has_envelope(signature->char_at(0))); 122 } 123 124 // Determine if this signature char introduces an 125 // envelope, which is a class name plus ';'. 126 static bool has_envelope(char signature_char) { 127 return (signature_char == JVM_SIGNATURE_CLASS); 128 } 129 130 // Assuming has_envelope is true, return the symbol 131 // inside the envelope, by stripping 'L' and ';'. 132 // Caller is responsible for decrementing the newly created 133 // Symbol's refcount, use TempNewSymbol. 134 static Symbol* strip_envelope(const Symbol* signature) { 135 assert(has_envelope(signature), "precondition"); 136 return SymbolTable::new_symbol((char*) signature->bytes() + 1, 137 signature->utf8_length() - 2); 138 } 139 140 // Assuming it's either a field or method descriptor, determine 141 // whether it is in fact a method descriptor: 142 static bool is_method(const Symbol* signature) { 143 return signature->starts_with(JVM_SIGNATURE_FUNC); 144 } 145 146 // Assuming it's a method signature, determine if it must 147 // return void. 148 static bool is_void_method(const Symbol* signature) { 149 assert(is_method(signature), "signature is not for a method"); 150 return signature->ends_with(JVM_SIGNATURE_VOID); 151 } 152 }; 153 154 // A SignatureIterator uses a SignatureStream to produces BasicType 155 // results, discarding class names. This means it can be accelerated 156 // using a fingerprint mechanism, in many cases, without loss of type 157 // information. The FingerPrinter class computes and caches this 158 // reduced information for faster iteration. 159 160 class SignatureIterator: public ResourceObj { 161 public: 162 typedef uint64_t fingerprint_t; 163 164 protected: 165 Symbol* _signature; // the signature to iterate over 166 BasicType _return_type; 167 fingerprint_t _fingerprint; 168 169 public: 170 // Definitions used in generating and iterating the 171 // bit field form of the signature generated by the 172 // Fingerprinter. 173 enum { 174 fp_static_feature_size = 1, 175 fp_is_static_bit = 1, 176 177 fp_result_feature_size = 4, 178 fp_result_feature_mask = right_n_bits(fp_result_feature_size), 179 fp_parameter_feature_size = 4, 180 fp_parameter_feature_mask = right_n_bits(fp_parameter_feature_size), 181 182 fp_parameters_done = 0, // marker for end of parameters (must be zero) 183 184 // Parameters take up full wordsize, minus the result and static bit fields. 185 // Since fp_parameters_done is zero, termination field arises from shifting 186 // in zero bits, and therefore occupies no extra space. 187 // The sentinel value is all-zero-bits, which is impossible for a true 188 // fingerprint, since at least the result field will be non-zero. 189 fp_max_size_of_parameters = ((BitsPerLong 190 - (fp_result_feature_size + fp_static_feature_size)) 191 / fp_parameter_feature_size) 192 }; 193 194 static bool fp_is_valid_type(BasicType type, bool for_return_type = false); 195 196 // Sentinel values are zero and not-zero (-1). 197 // No need to protect the sign bit, since every valid return type is non-zero 198 // (even T_VOID), and there are no valid parameter fields which are 0xF (T_VOID). 199 static fingerprint_t zero_fingerprint() { return (fingerprint_t)0; } 200 static fingerprint_t overflow_fingerprint() { return ~(fingerprint_t)0; } 201 static bool fp_is_valid(fingerprint_t fingerprint) { 202 return (fingerprint != zero_fingerprint()) && (fingerprint != overflow_fingerprint()); 203 } 204 205 // Constructors 206 SignatureIterator(Symbol* signature, fingerprint_t fingerprint = zero_fingerprint()) { 207 _signature = signature; 208 _return_type = T_ILLEGAL; // sentinel value for uninitialized 209 _fingerprint = zero_fingerprint(); 210 if (fingerprint != _fingerprint) { 211 set_fingerprint(fingerprint); 212 } 213 } 214 215 // If the fingerprint is present, we can use an accelerated loop. 216 void set_fingerprint(fingerprint_t fingerprint); 217 218 // Returns the set fingerprint, or zero_fingerprint() 219 // if none has been set already. 220 fingerprint_t fingerprint() const { return _fingerprint; } 221 222 // Iteration 223 // Hey look: There are no virtual methods in this class. 224 // So how is it customized? By calling do_parameters_on 225 // an object which answers to "do_type(BasicType)". 226 // By convention, this object is in the subclass 227 // itself, so the call is "do_parameters_on(this)". 228 // The effect of this is to inline the parsing loop 229 // everywhere "do_parameters_on" is called. 230 // If there is a valid fingerprint in the object, 231 // an improved loop is called which just unpacks the 232 // bitfields from the fingerprint. Otherwise, the 233 // symbol is parsed. 234 template<typename T> inline void do_parameters_on(T* callback); // iterates over parameters only 235 void skip_parameters(); // skips over parameters to find return type 236 BasicType return_type(); // computes the value on the fly if necessary 237 238 static bool fp_is_static(fingerprint_t fingerprint) { 239 assert(fp_is_valid(fingerprint), "invalid fingerprint"); 240 return fingerprint & fp_is_static_bit; 241 } 242 static BasicType fp_return_type(fingerprint_t fingerprint) { 243 assert(fp_is_valid(fingerprint), "invalid fingerprint"); 244 return (BasicType) ((fingerprint >> fp_static_feature_size) & fp_result_feature_mask); 245 } 246 static fingerprint_t fp_start_parameters(fingerprint_t fingerprint) { 247 assert(fp_is_valid(fingerprint), "invalid fingerprint"); 248 return fingerprint >> (fp_static_feature_size + fp_result_feature_size); 249 } 250 static BasicType fp_next_parameter(fingerprint_t& mask) { 251 int result = (mask & fp_parameter_feature_mask); 252 mask >>= fp_parameter_feature_size; 253 return (BasicType) result; 254 } 255 }; 256 257 258 // Specialized SignatureIterators: Used to compute signature specific values. 259 260 class SignatureTypeNames : public SignatureIterator { 261 protected: 262 virtual void type_name(const char* name) = 0; 263 264 friend class SignatureIterator; // so do_parameters_on can call do_type 265 void do_type(BasicType type) { 266 switch (type) { 267 case T_BOOLEAN: type_name("jboolean"); break; 268 case T_CHAR: type_name("jchar" ); break; 269 case T_FLOAT: type_name("jfloat" ); break; 270 case T_DOUBLE: type_name("jdouble" ); break; 271 case T_BYTE: type_name("jbyte" ); break; 272 case T_SHORT: type_name("jshort" ); break; 273 case T_INT: type_name("jint" ); break; 274 case T_LONG: type_name("jlong" ); break; 275 case T_VOID: type_name("void" ); break; 276 case T_ARRAY: 277 case T_OBJECT: type_name("jobject" ); break; 278 default: ShouldNotReachHere(); 279 } 280 } 281 282 public: 283 SignatureTypeNames(Symbol* signature) : SignatureIterator(signature) {} 284 }; 285 286 287 // Specialized SignatureIterator: Used to compute the argument size. 288 289 class ArgumentSizeComputer: public SignatureIterator { 290 private: 291 int _size; 292 friend class SignatureIterator; // so do_parameters_on can call do_type 293 void do_type(BasicType type) { _size += parameter_type_word_count(type); } 294 public: 295 ArgumentSizeComputer(Symbol* signature); 296 int size() { return _size; } 297 }; 298 299 300 class ArgumentCount: public SignatureIterator { 301 private: 302 int _size; 303 friend class SignatureIterator; // so do_parameters_on can call do_type 304 void do_type(BasicType type) { _size++; } 305 public: 306 ArgumentCount(Symbol* signature); 307 int size() { return _size; } 308 }; 309 310 311 class ReferenceArgumentCount: public SignatureIterator { 312 private: 313 int _refs; 314 friend class SignatureIterator; // so do_parameters_on can call do_type 315 void do_type(BasicType type) { if (is_reference_type(type)) _refs++; } 316 public: 317 ReferenceArgumentCount(Symbol* signature); 318 int count() { return _refs; } 319 }; 320 321 322 // Specialized SignatureIterator: Used to compute the result type. 323 324 class ResultTypeFinder: public SignatureIterator { 325 public: 326 BasicType type() { return return_type(); } 327 ResultTypeFinder(Symbol* signature) : SignatureIterator(signature) { } 328 }; 329 330 331 // Fingerprinter computes a unique ID for a given method. The ID 332 // is a bitvector characterizing the methods signature (incl. the receiver). 333 class Fingerprinter: public SignatureIterator { 334 private: 335 fingerprint_t _accumulator; 336 int _param_size; 337 int _shift_count; 338 const Method* _method; 339 340 void initialize_accumulator() { 341 _accumulator = 0; 342 _shift_count = fp_result_feature_size + fp_static_feature_size; 343 _param_size = 0; 344 } 345 346 // Out-of-line method does it all in constructor: 347 void compute_fingerprint_and_return_type(bool static_flag = false); 348 349 friend class SignatureIterator; // so do_parameters_on can call do_type 350 void do_type(BasicType type) { 351 assert(fp_is_valid_type(type), "bad parameter type"); 352 _accumulator |= ((fingerprint_t)type << _shift_count); 353 _shift_count += fp_parameter_feature_size; 354 _param_size += (is_double_word_type(type) ? 2 : 1); 355 } 356 357 public: 358 int size_of_parameters() const { return _param_size; } 359 // fingerprint() and return_type() are in super class 360 361 Fingerprinter(const methodHandle& method) 362 : SignatureIterator(method->signature()), 363 _method(method()) { 364 compute_fingerprint_and_return_type(); 365 } 366 Fingerprinter(Symbol* signature, bool is_static) 367 : SignatureIterator(signature), 368 _method(NULL) { 369 compute_fingerprint_and_return_type(is_static); 370 } 371 }; 372 373 374 // Specialized SignatureIterator: Used for native call purposes 375 376 class NativeSignatureIterator: public SignatureIterator { 377 private: 378 methodHandle _method; 379 // We need separate JNI and Java offset values because in 64 bit mode, 380 // the argument offsets are not in sync with the Java stack. 381 // For example a long takes up 1 "C" stack entry but 2 Java stack entries. 382 int _offset; // The java stack offset 383 int _prepended; // number of prepended JNI parameters (1 JNIEnv, plus 1 mirror if static) 384 int _jni_offset; // the current parameter offset, starting with 0 385 386 friend class SignatureIterator; // so do_parameters_on can call do_type 387 void do_type(BasicType type) { 388 switch (type) { 389 case T_BYTE: case T_SHORT: case T_INT: 390 case T_BOOLEAN: case T_CHAR: 391 pass_int(); _jni_offset++; _offset++; 392 break; 393 case T_FLOAT: 394 pass_float(); _jni_offset++; _offset++; 395 break; 396 case T_DOUBLE: 397 #ifdef _LP64 398 pass_double(); _jni_offset++; _offset += 2; 399 #else 400 pass_double(); _jni_offset += 2; _offset += 2; 401 #endif 402 break; 403 case T_LONG: 404 #ifdef _LP64 405 pass_long(); _jni_offset++; _offset += 2; 406 #else 407 pass_long(); _jni_offset += 2; _offset += 2; 408 #endif 409 break; 410 case T_ARRAY: 411 case T_OBJECT: 412 pass_object(); _jni_offset++; _offset++; 413 break; 414 default: 415 ShouldNotReachHere(); 416 } 417 } 418 419 public: 420 methodHandle method() const { return _method; } 421 int offset() const { return _offset; } 422 int jni_offset() const { return _jni_offset + _prepended; } 423 bool is_static() const { return method()->is_static(); } 424 virtual void pass_int() = 0; 425 virtual void pass_long() = 0; 426 virtual void pass_object() = 0; // objects, arrays, inlines 427 virtual void pass_float() = 0; 428 #ifdef _LP64 429 virtual void pass_double() = 0; 430 #else 431 virtual void pass_double() { pass_long(); } // may be same as long 432 #endif 433 434 NativeSignatureIterator(const methodHandle& method) : SignatureIterator(method->signature()) { 435 _method = method; 436 _offset = 0; 437 _jni_offset = 0; 438 439 const int JNIEnv_words = 1; 440 const int mirror_words = 1; 441 _prepended = !is_static() ? JNIEnv_words : JNIEnv_words + mirror_words; 442 } 443 444 void iterate() { iterate(Fingerprinter(method()).fingerprint()); } 445 446 // iterate() calls the 3 virtual methods according to the following invocation syntax: 447 // 448 // {pass_int | pass_long | pass_object} 449 // 450 // Arguments are handled from left to right (receiver first, if any). 451 // The offset() values refer to the Java stack offsets but are 0 based and increasing. 452 // The java_offset() values count down to 0, and refer to the Java TOS. 453 // The jni_offset() values increase from 1 or 2, and refer to C arguments. 454 // The method's return type is ignored. 455 456 void iterate(fingerprint_t fingerprint) { 457 set_fingerprint(fingerprint); 458 if (!is_static()) { 459 // handle receiver (not handled by iterate because not in signature) 460 pass_object(); _jni_offset++; _offset++; 461 } 462 do_parameters_on(this); 463 } 464 }; 465 466 467 // This is the core parsing logic for iterating over signatures. 468 // All of the previous classes use this for doing their work. 469 470 class SignatureStream : public StackObj { 471 private: 472 const Symbol* _signature; 473 int _begin; 474 int _end; 475 int _limit; 476 int _array_prefix; // count of '[' before the array element descr 477 BasicType _type; 478 int _state; 479 Symbol* _previous_name; // cache the previously looked up symbol to avoid lookups 480 GrowableArray<Symbol*>* _names; // symbols created while parsing that need to be dereferenced 481 482 inline int scan_non_primitive(BasicType type); 483 484 Symbol* find_symbol(); 485 486 enum { _s_field = 0, _s_method = 1, _s_method_return = 3 }; 487 void set_done() { 488 _state |= -2; // preserve s_method bit 489 assert(is_done(), "Unable to set state to done"); 490 } 491 492 public: 493 bool is_method_signature() const { return (_state & (int)_s_method) != 0; } 494 bool at_return_type() const { return _state == (int)_s_method_return; } 495 bool is_done() const { return _state < 0; } 496 void next(); 497 498 SignatureStream(const Symbol* signature, bool is_method = true); 499 ~SignatureStream(); 500 501 bool is_reference() const { return is_reference_type(_type); } 502 bool is_array() const { return _type == T_ARRAY; } 503 bool is_primitive() const { return is_java_primitive(_type); } 504 BasicType type() const { return _type; } 505 506 const u1* raw_bytes() const { return _signature->bytes() + _begin; } 507 int raw_length() const { return _end - _begin; } 508 int raw_begin() const { return _begin; } 509 int raw_end() const { return _end; } 510 int raw_symbol_begin() const { return _begin + (has_envelope() ? 1 : 0); } 511 int raw_symbol_end() const { return _end - (has_envelope() ? 1 : 0); } 512 char raw_char_at(int i) const { 513 assert(i < _limit, "index for raw_char_at is over the limit"); 514 return _signature->char_at(i); 515 } 516 517 // True if there is an embedded class name in this type, 518 // followed by ';'. 519 bool has_envelope() const { 520 if (!Signature::has_envelope(_signature->char_at(_begin))) 521 return false; 522 // this should always be true, but let's test it: 523 assert(_signature->char_at(_end-1) == JVM_SIGNATURE_ENDCLASS, "signature envelope has no semi-colon at end"); 524 return true; 525 } 526 527 // return the symbol for chars in symbol_begin()..symbol_end() 528 Symbol* as_symbol() { 529 return find_symbol(); 530 } 531 532 // in case you want only the return type: 533 void skip_to_return_type(); 534 535 // number of '[' in array prefix 536 int array_prefix_length() { 537 return _type == T_ARRAY ? _array_prefix : 0; 538 } 539 540 // In case you want only the array base type, 541 // reset the stream after skipping some brackets '['. 542 // (The argument is clipped to array_prefix_length(), 543 // and if it ends up as zero this call is a nop. 544 // The default is value skips all brackets '['.) 545 int skip_array_prefix(int prefix_length = 9999); 546 547 // free-standing lookups (bring your own CL/PD pair) 548 enum FailureMode { ReturnNull, NCDFError, CachedOrNull }; 549 Klass* as_klass(Handle class_loader, Handle protection_domain, FailureMode failure_mode, TRAPS); 550 oop as_java_mirror(Handle class_loader, Handle protection_domain, FailureMode failure_mode, TRAPS); 551 }; 552 553 // Here is how all the SignatureIterator classes invoke the 554 // SignatureStream engine to do their parsing. 555 template<typename T> inline 556 void SignatureIterator::do_parameters_on(T* callback) { 557 fingerprint_t unaccumulator = _fingerprint; 558 559 // Check for too many arguments, or missing fingerprint: 560 if (!fp_is_valid(unaccumulator)) { 561 SignatureStream ss(_signature); 562 for (; !ss.at_return_type(); ss.next()) { 563 callback->do_type(ss.type()); 564 } 565 // while we are here, capture the return type 566 _return_type = ss.type(); 567 } else { 568 // Optimized version of do_parameters when fingerprint is known 569 assert(_return_type != T_ILLEGAL, "return type already captured from fp"); 570 unaccumulator = fp_start_parameters(unaccumulator); 571 for (BasicType type; (type = fp_next_parameter(unaccumulator)) != (BasicType)fp_parameters_done; ) { 572 assert(fp_is_valid_type(type), "garbled fingerprint"); 573 callback->do_type(type); 574 } 575 } 576 } 577 578 #ifdef ASSERT 579 class SignatureVerifier : public StackObj { 580 public: 581 static bool is_valid_method_signature(Symbol* sig); 582 static bool is_valid_type_signature(Symbol* sig); 583 private: 584 static ssize_t is_valid_type(const char*, ssize_t); 585 }; 586 #endif 587 #endif // SHARE_RUNTIME_SIGNATURE_HPP |