< prev index next >

src/hotspot/share/runtime/signature.cpp

Print this page




  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 #include "precompiled.hpp"
  26 #include "classfile/symbolTable.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "memory/oopFactory.hpp"
  29 #include "memory/resourceArea.hpp"
  30 #include "memory/universe.hpp"
  31 #include "oops/instanceKlass.hpp"
  32 #include "oops/oop.inline.hpp"
  33 #include "oops/symbol.hpp"
  34 #include "oops/typeArrayKlass.hpp"


  35 #include "runtime/signature.hpp"
  36 
  37 // Implementation of SignatureIterator
  38 
  39 // Signature syntax:
  40 //
  41 // Signature  = "(" {Parameter} ")" ReturnType.
  42 // Parameter  = FieldType.
  43 // ReturnType = FieldType | "V".
  44 // FieldType  = "B" | "C" | "D" | "F" | "I" | "J" | "S" | "Z" | "L" ClassName ";" | "[" FieldType.
  45 // ClassName  = string.
  46 
  47 
  48 SignatureIterator::SignatureIterator(Symbol* signature) {
  49   _signature       = signature;
  50   _parameter_index = 0;
  51 }
  52 
  53 void SignatureIterator::expect(char c) {
  54   if (_signature->char_at(_index) != c) fatal("expecting %c", c);
  55   _index++;
  56 }
  57 
  58 int SignatureIterator::parse_type() {
  59   // Note: This function could be simplified by using "return T_XXX_size;"
  60   //       instead of the assignment and the break statements. However, it
  61   //       seems that the product build for win32_i486 with MS VC++ 6.0 doesn't
  62   //       work (stack underflow for some tests) - this seems to be a VC++ 6.0
  63   //       compiler bug (was problem - gri 4/27/2000).
  64   int size = -1;


  73               _index++; size = T_FLOAT_size  ; break;
  74     case 'I': do_int   (); if (_parameter_index < 0 ) _return_type = T_INT;
  75               _index++; size = T_INT_size    ; break;
  76     case 'J': do_long  (); if (_parameter_index < 0 ) _return_type = T_LONG;
  77               _index++; size = T_LONG_size   ; break;
  78     case 'S': do_short (); if (_parameter_index < 0 ) _return_type = T_SHORT;
  79               _index++; size = T_SHORT_size  ; break;
  80     case 'Z': do_bool  (); if (_parameter_index < 0 ) _return_type = T_BOOLEAN;
  81               _index++; size = T_BOOLEAN_size; break;
  82     case 'V': do_void  (); if (_parameter_index < 0 ) _return_type = T_VOID;
  83               _index++; size = T_VOID_size;  ; break;
  84     case 'L':
  85       { int begin = ++_index;
  86         Symbol* sig = _signature;
  87         while (sig->char_at(_index++) != ';') ;
  88         do_object(begin, _index);
  89       }
  90       if (_parameter_index < 0 ) _return_type = T_OBJECT;
  91       size = T_OBJECT_size;
  92       break;









  93     case '[':
  94       { int begin = ++_index;
  95         Symbol* sig = _signature;
  96         while (sig->char_at(_index) == '[') {
  97           _index++;
  98         }
  99         if (sig->char_at(_index) == 'L') {
 100           while (sig->char_at(_index++) != ';') ;
 101         } else {
 102           _index++;
 103         }
 104         do_array(begin, _index);
 105        if (_parameter_index < 0 ) _return_type = T_ARRAY;
 106       }
 107       size = T_ARRAY_size;
 108       break;
 109     default:
 110       ShouldNotReachHere();
 111       break;
 112   }
 113   assert(size >= 0, "size must be set");
 114   return size;
 115 }
 116 
 117 
 118 void SignatureIterator::check_signature_end() {
 119   if (_index < _signature->utf8_length()) {


 205   expect('(');
 206   Symbol* sig = _signature;
 207   // Need to skip over each type in the signature's argument list until a
 208   // closing ')' is found., then get the return type.  We cannot just scan
 209   // for the first ')' because ')' is a legal character in a type name.
 210   while (sig->char_at(_index) != ')') {
 211     switch(sig->char_at(_index)) {
 212       case 'B':
 213       case 'C':
 214       case 'D':
 215       case 'F':
 216       case 'I':
 217       case 'J':
 218       case 'S':
 219       case 'Z':
 220       case 'V':
 221         {
 222           _index++;
 223         }
 224         break;

 225       case 'L':
 226         {
 227           while (sig->char_at(_index++) != ';') ;
 228         }
 229         break;
 230       case '[':
 231         {
 232           while (sig->char_at(++_index) == '[') ;
 233           if (sig->char_at(_index) == 'L') {
 234             while (sig->char_at(_index++) != ';') ;
 235           } else {
 236             _index++;
 237           }
 238         }
 239         break;
 240       default:
 241         ShouldNotReachHere();
 242         break;
 243     }
 244   }
 245   expect(')');
 246   // Parse return type
 247   _parameter_index = -1;
 248   parse_type();
 249   check_signature_end();
 250   _parameter_index = 0;
 251 }
 252 
 253 


 278   if (_names != NULL) {
 279     for (int i = 0; i < _names->length(); i++) {
 280       _names->at(i)->decrement_refcount();
 281     }
 282   }
 283 }
 284 
 285 bool SignatureStream::is_done() const {
 286   return _end > _signature->utf8_length();
 287 }
 288 
 289 
 290 void SignatureStream::next_non_primitive(int t) {
 291   switch (t) {
 292     case 'L': {
 293       _type = T_OBJECT;
 294       Symbol* sig = _signature;
 295       while (sig->char_at(_end++) != ';');
 296       break;
 297     }






 298     case '[': {
 299       _type = T_ARRAY;
 300       Symbol* sig = _signature;
 301       char c = sig->char_at(_end);
 302       while ('0' <= c && c <= '9') c = sig->char_at(_end++);
 303       while (sig->char_at(_end) == '[') {
 304         _end++;
 305         c = sig->char_at(_end);
 306         while ('0' <= c && c <= '9') c = sig->char_at(_end++);
 307       }
 308       switch(sig->char_at(_end)) {
 309         case 'B':
 310         case 'C':
 311         case 'D':
 312         case 'F':
 313         case 'I':
 314         case 'J':
 315         case 'S':
 316         case 'Z':_end++; break;
 317         default: {
 318           while (sig->char_at(_end++) != ';');
 319           break;
 320         }
 321       }
 322       break;
 323     }
 324     case ')': _end++; next(); _at_return_type = true; break;
 325     default : ShouldNotReachHere();
 326   }
 327 }
 328 
 329 
 330 bool SignatureStream::is_object() const {
 331   return _type == T_OBJECT
 332       || _type == T_ARRAY;

 333 }
 334 
 335 bool SignatureStream::is_array() const {
 336   return _type == T_ARRAY;
 337 }
 338 
 339 Symbol* SignatureStream::as_symbol() {
 340   // Create a symbol from for string _begin _end
 341   int begin = _begin;
 342   int end   = _end;
 343 
 344   if (   _signature->char_at(_begin) == 'L'
 345       && _signature->char_at(_end-1) == ';') {
 346     begin++;
 347     end--;
 348   }
 349 
 350   const char* symbol_chars = (const char*)_signature->base() + begin;
 351   int len = end - begin;
 352 
 353   // Quick check for common symbols in signatures
 354   assert((vmSymbols::java_lang_String()->utf8_length() == 16 && vmSymbols::java_lang_Object()->utf8_length() == 16), "sanity");
 355   if (len == 16 &&
 356       strncmp(symbol_chars, "java/lang/", 10) == 0) {
 357     if (strncmp("String", symbol_chars + 10, 6) == 0) {
 358       return vmSymbols::java_lang_String();
 359     } else if (strncmp("Object", symbol_chars + 10, 6) == 0) {
 360       return vmSymbols::java_lang_Object();
 361     }
 362   }
 363 
 364   Symbol* name = _previous_name;
 365   if (name != NULL && name->equals(symbol_chars, len)) {
 366     return name;
 367   }
 368 
 369   // Save names for cleaning up reference count at the end of
 370   // SignatureStream scope.
 371   name = SymbolTable::new_symbol(symbol_chars, len);
 372   if (!name->is_permanent()) {
 373     if (_names == NULL) {
 374       _names = new GrowableArray<Symbol*>(10);
 375     }
 376     _names->push(name);  // save new symbol for decrementing later
 377   }
 378   _previous_name = name;
 379   return name;
 380 }
 381 









 382 Klass* SignatureStream::as_klass(Handle class_loader, Handle protection_domain,
 383                                  FailureMode failure_mode, TRAPS) {
 384   if (!is_object())  return NULL;
 385   Symbol* name = as_symbol();
 386   if (failure_mode == ReturnNull) {
 387     return SystemDictionary::resolve_or_null(name, class_loader, protection_domain, THREAD);
 388   } else {
 389     bool throw_error = (failure_mode == NCDFError);
 390     return SystemDictionary::resolve_or_fail(name, class_loader, protection_domain, throw_error, THREAD);
 391   }
 392 }
 393 
 394 oop SignatureStream::as_java_mirror(Handle class_loader, Handle protection_domain,
 395                                     FailureMode failure_mode, TRAPS) {
 396   if (!is_object())
 397     return Universe::java_mirror(type());
 398   Klass* klass = as_klass(class_loader, protection_domain, failure_mode, CHECK_NULL);
 399   if (klass == NULL)  return NULL;
 400   return klass->java_mirror();






 401 }
 402 
 403 Symbol* SignatureStream::as_symbol_or_null() {
 404   // Create a symbol from for string _begin _end
 405   ResourceMark rm;
 406 
 407   int begin = _begin;
 408   int end   = _end;
 409 
 410   if (   _signature->char_at(_begin) == 'L'
 411       && _signature->char_at(_end-1) == ';') {
 412     begin++;
 413     end--;
 414   }
 415 
 416   char* buffer = NEW_RESOURCE_ARRAY(char, end - begin);
 417   for (int index = begin; index < end; index++) {
 418     buffer[index - begin] = _signature->char_at(index);
 419   }
 420   Symbol* result = SymbolTable::probe(buffer, end - begin);
 421   return result;
 422 }
 423 
 424 int SignatureStream::reference_parameter_count() {
 425   int args_count = 0;
 426   for ( ; !at_return_type(); next()) {
 427     if (is_object()) {
 428       args_count++;
 429     }
 430   }
 431   return args_count;
 432 }
 433 
 434 #ifdef ASSERT
 435 bool SignatureVerifier::is_valid_method_signature(Symbol* sig) {
 436   const char* method_sig = (const char*)sig->bytes();
 437   ssize_t len = sig->utf8_length();
 438   ssize_t index = 0;
 439   if (method_sig != NULL && len > 1 && method_sig[index] == '(') {
 440     ++index;
 441     while (index < len && method_sig[index] != ')') {
 442       ssize_t res = is_valid_type(&method_sig[index], len - index);
 443       if (res == -1) {
 444         return false;
 445       } else {
 446         index += res;
 447       }
 448     }
 449     if (index < len && method_sig[index] == ')') {
 450       // check the return type
 451       ++index;
 452       return (is_valid_type(&method_sig[index], len - index) == (len - index));
 453     }
 454   }
 455   return false;
 456 }
 457 
 458 bool SignatureVerifier::is_valid_type_signature(Symbol* sig) {
 459   const char* type_sig = (const char*)sig->bytes();
 460   ssize_t len = sig->utf8_length();
 461   return (type_sig != NULL && len >= 1 &&
 462           (is_valid_type(type_sig, len) == len));
 463 }
 464 
 465 // Checks to see if the type (not to go beyond 'limit') refers to a valid type.
 466 // Returns -1 if it is not, or the index of the next character that is not part
 467 // of the type.  The type encoding may end before 'limit' and that's ok.
 468 ssize_t SignatureVerifier::is_valid_type(const char* type, ssize_t limit) {
 469   ssize_t index = 0;
 470 
 471   // Iterate over any number of array dimensions
 472   while (index < limit && type[index] == '[') ++index;
 473   if (index >= limit) {
 474     return -1;
 475   }
 476   switch (type[index]) {
 477     case 'B': case 'C': case 'D': case 'F': case 'I':
 478     case 'J': case 'S': case 'Z': case 'V':
 479       return index + 1;

 480     case 'L':
 481       for (index = index + 1; index < limit; ++index) {
 482         char c = type[index];
 483         switch (c) {
 484           case ';':
 485             return index + 1;
 486           case '\0': case '.': case '[':
 487             return -1;
 488           default: ; // fall through
 489         }
 490       }
 491       // fall through
 492     default: ; // fall through
 493   }
 494   return -1;
 495 }
 496 #endif // ASSERT






















































































  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 #include "precompiled.hpp"
  26 #include "classfile/symbolTable.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "memory/oopFactory.hpp"
  29 #include "memory/resourceArea.hpp"
  30 #include "memory/universe.hpp"
  31 #include "oops/instanceKlass.hpp"
  32 #include "oops/oop.inline.hpp"
  33 #include "oops/symbol.hpp"
  34 #include "oops/typeArrayKlass.hpp"
  35 #include "oops/valueKlass.hpp"
  36 #include "runtime/handles.inline.hpp"
  37 #include "runtime/signature.hpp"
  38 
  39 // Implementation of SignatureIterator
  40 
  41 // Signature syntax:
  42 //
  43 // Signature  = "(" {Parameter} ")" ReturnType.
  44 // Parameter  = FieldType.
  45 // ReturnType = FieldType | "V".
  46 // FieldType  = "B" | "C" | "D" | "F" | "I" | "J" | "S" | "Z" | "L" ClassName ";" | "Q" ValueClassName ";" | "[" FieldType.
  47 // ClassName  = string.
  48 
  49 
  50 SignatureIterator::SignatureIterator(Symbol* signature) {
  51   _signature       = signature;
  52   _parameter_index = 0;
  53 }
  54 
  55 void SignatureIterator::expect(char c) {
  56   if (_signature->char_at(_index) != c) fatal("expecting %c", c);
  57   _index++;
  58 }
  59 
  60 int SignatureIterator::parse_type() {
  61   // Note: This function could be simplified by using "return T_XXX_size;"
  62   //       instead of the assignment and the break statements. However, it
  63   //       seems that the product build for win32_i486 with MS VC++ 6.0 doesn't
  64   //       work (stack underflow for some tests) - this seems to be a VC++ 6.0
  65   //       compiler bug (was problem - gri 4/27/2000).
  66   int size = -1;


  75               _index++; size = T_FLOAT_size  ; break;
  76     case 'I': do_int   (); if (_parameter_index < 0 ) _return_type = T_INT;
  77               _index++; size = T_INT_size    ; break;
  78     case 'J': do_long  (); if (_parameter_index < 0 ) _return_type = T_LONG;
  79               _index++; size = T_LONG_size   ; break;
  80     case 'S': do_short (); if (_parameter_index < 0 ) _return_type = T_SHORT;
  81               _index++; size = T_SHORT_size  ; break;
  82     case 'Z': do_bool  (); if (_parameter_index < 0 ) _return_type = T_BOOLEAN;
  83               _index++; size = T_BOOLEAN_size; break;
  84     case 'V': do_void  (); if (_parameter_index < 0 ) _return_type = T_VOID;
  85               _index++; size = T_VOID_size;  ; break;
  86     case 'L':
  87       { int begin = ++_index;
  88         Symbol* sig = _signature;
  89         while (sig->char_at(_index++) != ';') ;
  90         do_object(begin, _index);
  91       }
  92       if (_parameter_index < 0 ) _return_type = T_OBJECT;
  93       size = T_OBJECT_size;
  94       break;
  95     case 'Q':
  96       { int begin = ++_index;
  97       Symbol* sig = _signature;
  98       while (sig->char_at(_index++) != ';') ;
  99       do_valuetype(begin, _index);
 100       }
 101       if (_parameter_index < 0 ) _return_type = T_VALUETYPE;
 102       size = T_VALUETYPE_size;
 103       break;
 104     case '[':
 105       { int begin = ++_index;
 106         Symbol* sig = _signature;
 107         while (sig->char_at(_index) == '[') {
 108           _index++;
 109         }
 110         if (sig->char_at(_index) == 'L' || sig->char_at(_index) == 'Q') {
 111           while (sig->char_at(_index++) != ';') ;
 112         } else {
 113           _index++;
 114         }
 115         do_array(begin, _index);
 116        if (_parameter_index < 0 ) _return_type = T_ARRAY;
 117       }
 118       size = T_ARRAY_size;
 119       break;
 120     default:
 121       ShouldNotReachHere();
 122       break;
 123   }
 124   assert(size >= 0, "size must be set");
 125   return size;
 126 }
 127 
 128 
 129 void SignatureIterator::check_signature_end() {
 130   if (_index < _signature->utf8_length()) {


 216   expect('(');
 217   Symbol* sig = _signature;
 218   // Need to skip over each type in the signature's argument list until a
 219   // closing ')' is found., then get the return type.  We cannot just scan
 220   // for the first ')' because ')' is a legal character in a type name.
 221   while (sig->char_at(_index) != ')') {
 222     switch(sig->char_at(_index)) {
 223       case 'B':
 224       case 'C':
 225       case 'D':
 226       case 'F':
 227       case 'I':
 228       case 'J':
 229       case 'S':
 230       case 'Z':
 231       case 'V':
 232         {
 233           _index++;
 234         }
 235         break;
 236       case 'Q':
 237       case 'L':
 238         {
 239           while (sig->char_at(_index++) != ';') ;
 240         }
 241         break;
 242       case '[':
 243         {
 244           while (sig->char_at(++_index) == '[') ;
 245           if (sig->char_at(_index) == 'L' || sig->char_at(_index) == 'Q' ) {
 246             while (sig->char_at(_index++) != ';') ;
 247           } else {
 248             _index++;
 249           }
 250         }
 251         break;
 252       default:
 253         ShouldNotReachHere();
 254         break;
 255     }
 256   }
 257   expect(')');
 258   // Parse return type
 259   _parameter_index = -1;
 260   parse_type();
 261   check_signature_end();
 262   _parameter_index = 0;
 263 }
 264 
 265 


 290   if (_names != NULL) {
 291     for (int i = 0; i < _names->length(); i++) {
 292       _names->at(i)->decrement_refcount();
 293     }
 294   }
 295 }
 296 
 297 bool SignatureStream::is_done() const {
 298   return _end > _signature->utf8_length();
 299 }
 300 
 301 
 302 void SignatureStream::next_non_primitive(int t) {
 303   switch (t) {
 304     case 'L': {
 305       _type = T_OBJECT;
 306       Symbol* sig = _signature;
 307       while (sig->char_at(_end++) != ';');
 308       break;
 309     }
 310     case 'Q': {
 311       _type = T_VALUETYPE;
 312       Symbol* sig = _signature;
 313       while (sig->char_at(_end++) != ';');
 314       break;
 315     }
 316     case '[': {
 317       _type = T_ARRAY;
 318       Symbol* sig = _signature;
 319       char c = sig->char_at(_end);
 320       while ('0' <= c && c <= '9') c = sig->char_at(_end++);
 321       while (sig->char_at(_end) == '[') {
 322         _end++;
 323         c = sig->char_at(_end);
 324         while ('0' <= c && c <= '9') c = sig->char_at(_end++);
 325       }
 326       switch(sig->char_at(_end)) {
 327         case 'B':
 328         case 'C':
 329         case 'D':
 330         case 'F':
 331         case 'I':
 332         case 'J':
 333         case 'S':
 334         case 'Z':_end++; break;
 335         default: {
 336           while (sig->char_at(_end++) != ';');
 337           break;
 338         }
 339       }
 340       break;
 341     }
 342     case ')': _end++; next(); _at_return_type = true; break;
 343     default : ShouldNotReachHere();
 344   }
 345 }
 346 
 347 
 348 bool SignatureStream::is_object() const {
 349   return _type == T_OBJECT
 350       || _type == T_ARRAY
 351       || _type == T_VALUETYPE;
 352 }
 353 
 354 bool SignatureStream::is_array() const {
 355   return _type == T_ARRAY;
 356 }
 357 
 358 Symbol* SignatureStream::as_symbol() {
 359   // Create a symbol from for string _begin _end
 360   int begin = _begin;
 361   int end   = _end;
 362 
 363   if (  (_signature->char_at(_begin) == 'L' || _signature->char_at(_begin) == 'Q')
 364       && _signature->char_at(_end-1) == ';') {
 365     begin++;
 366     end--;
 367   }
 368 
 369   const char* symbol_chars = (const char*)_signature->base() + begin;
 370   int len = end - begin;
 371 
 372   // Quick check for common symbols in signatures
 373   assert((vmSymbols::java_lang_String()->utf8_length() == 16 && vmSymbols::java_lang_Object()->utf8_length() == 16), "sanity");
 374   if (len == 16 &&
 375       strncmp(symbol_chars, "java/lang/", 10) == 0) {
 376     if (strncmp("String", symbol_chars + 10, 6) == 0) {
 377       return vmSymbols::java_lang_String();
 378     } else if (strncmp("Object", symbol_chars + 10, 6) == 0) {
 379       return vmSymbols::java_lang_Object();
 380     }
 381   }
 382 
 383   Symbol* name = _previous_name;
 384   if (name != NULL && name->equals(symbol_chars, len)) {
 385     return name;
 386   }
 387 
 388   // Save names for cleaning up reference count at the end of
 389   // SignatureStream scope.
 390   name = SymbolTable::new_symbol(symbol_chars, len);
 391   if (!name->is_permanent()) {
 392     if (_names == NULL) {
 393       _names = new GrowableArray<Symbol*>(10);
 394     }
 395     _names->push(name);  // save new symbol for decrementing later
 396   }
 397   _previous_name = name;
 398   return name;
 399 }
 400 
 401 ValueKlass* SignatureStream::as_value_klass(InstanceKlass* holder) {
 402   Thread* THREAD = Thread::current();
 403   Handle class_loader(THREAD, holder->class_loader());
 404   Handle protection_domain(THREAD, holder->protection_domain());
 405   Klass* k = as_klass(class_loader, protection_domain, SignatureStream::ReturnNull, THREAD);
 406   assert(k != NULL && !HAS_PENDING_EXCEPTION, "unresolved value klass");
 407   return ValueKlass::cast(k);
 408 }
 409 
 410 Klass* SignatureStream::as_klass(Handle class_loader, Handle protection_domain,
 411                                  FailureMode failure_mode, TRAPS) {
 412   if (!is_object())  return NULL;
 413   Symbol* name = as_symbol();
 414   if (failure_mode == ReturnNull) {
 415     return SystemDictionary::resolve_or_null(name, class_loader, protection_domain, THREAD);
 416   } else {
 417     bool throw_error = (failure_mode == NCDFError);
 418     return SystemDictionary::resolve_or_fail(name, class_loader, protection_domain, throw_error, THREAD);
 419   }
 420 }
 421 
 422 oop SignatureStream::as_java_mirror(Handle class_loader, Handle protection_domain,
 423                                     FailureMode failure_mode, TRAPS) {
 424   if (!is_object())
 425     return Universe::java_mirror(type());
 426   Klass* klass = as_klass(class_loader, protection_domain, failure_mode, THREAD);
 427   if (klass == NULL)  return NULL;
 428   if (klass->is_value()) {
 429     ValueKlass* vk = ValueKlass::cast(InstanceKlass::cast(klass));
 430     return _type == T_VALUETYPE ? vk->value_mirror() : vk->indirect_mirror();
 431   } else {
 432     assert(_type != T_VALUETYPE, "must not be value type");
 433     return klass->java_mirror();
 434   }
 435 }
 436 
 437 Symbol* SignatureStream::as_symbol_or_null() {
 438   // Create a symbol from for string _begin _end
 439   ResourceMark rm;
 440 
 441   int begin = _begin;
 442   int end   = _end;
 443 
 444   if (  (_signature->char_at(_begin) == 'L' || _signature->char_at(_begin) == 'Q')
 445       && _signature->char_at(_end-1) == ';') {
 446     begin++;
 447     end--;
 448   }
 449 
 450   char* buffer = NEW_RESOURCE_ARRAY(char, end - begin);
 451   for (int index = begin; index < end; index++) {
 452     buffer[index - begin] = _signature->char_at(index);
 453   }
 454   Symbol* result = SymbolTable::probe(buffer, end - begin);
 455   return result;
 456 }
 457 
 458 int SignatureStream::reference_parameter_count() {
 459   int args_count = 0;
 460   for ( ; !at_return_type(); next()) {
 461     if (is_object()) {
 462       args_count++;
 463     }
 464   }
 465   return args_count;
 466 }
 467 
 468 #ifdef ASSERT
 469 bool SignatureVerifier::is_valid_method_signature(const Symbol* sig) {
 470   const char* method_sig = (const char*)sig->bytes();
 471   ssize_t len = sig->utf8_length();
 472   ssize_t index = 0;
 473   if (method_sig != NULL && len > 1 && method_sig[index] == '(') {
 474     ++index;
 475     while (index < len && method_sig[index] != ')') {
 476       ssize_t res = is_valid_type(&method_sig[index], len - index);
 477       if (res == -1) {
 478         return false;
 479       } else {
 480         index += res;
 481       }
 482     }
 483     if (index < len && method_sig[index] == ')') {
 484       // check the return type
 485       ++index;
 486       return (is_valid_type(&method_sig[index], len - index) == (len - index));
 487     }
 488   }
 489   return false;
 490 }
 491 
 492 bool SignatureVerifier::is_valid_type_signature(const Symbol* sig) {
 493   const char* type_sig = (const char*)sig->bytes();
 494   ssize_t len = sig->utf8_length();
 495   return (type_sig != NULL && len >= 1 &&
 496           (is_valid_type(type_sig, len) == len));
 497 }
 498 
 499 // Checks to see if the type (not to go beyond 'limit') refers to a valid type.
 500 // Returns -1 if it is not, or the index of the next character that is not part
 501 // of the type.  The type encoding may end before 'limit' and that's ok.
 502 ssize_t SignatureVerifier::is_valid_type(const char* type, ssize_t limit) {
 503   ssize_t index = 0;
 504 
 505   // Iterate over any number of array dimensions
 506   while (index < limit && type[index] == '[') ++index;
 507   if (index >= limit) {
 508     return -1;
 509   }
 510   switch (type[index]) {
 511     case 'B': case 'C': case 'D': case 'F': case 'I':
 512     case 'J': case 'S': case 'Z': case 'V':
 513       return index + 1;
 514     case 'Q': // fall through
 515     case 'L':
 516       for (index = index + 1; index < limit; ++index) {
 517         char c = type[index];
 518         switch (c) {
 519           case ';':
 520             return index + 1;
 521           case '\0': case '.': case '[':
 522             return -1;
 523           default: ; // fall through
 524         }
 525       }
 526       // fall through
 527     default: ; // fall through
 528   }
 529   return -1;
 530 }
 531 #endif // ASSERT
 532 
 533 // Adds an argument to the signature
 534 void SigEntry::add_entry(GrowableArray<SigEntry>* sig, BasicType bt, int offset) {
 535   sig->append(SigEntry(bt, offset));
 536   if (bt == T_LONG || bt == T_DOUBLE) {
 537     sig->append(SigEntry(T_VOID, offset)); // Longs and doubles take two stack slots
 538   }
 539 }
 540 
 541 // Inserts a reserved argument at position 'i'
 542 void SigEntry::insert_reserved_entry(GrowableArray<SigEntry>* sig, int i, BasicType bt) {
 543   if (bt == T_OBJECT || bt == T_ARRAY || bt == T_VALUETYPE) {
 544     // Treat this as INT to not confuse the GC
 545     bt = T_INT;
 546   } else if (bt == T_LONG || bt == T_DOUBLE) {
 547     // Longs and doubles take two stack slots
 548     sig->insert_before(i, SigEntry(T_VOID, SigEntry::ReservedOffset));
 549   }
 550   sig->insert_before(i, SigEntry(bt, SigEntry::ReservedOffset));
 551 }
 552 
 553 // Returns true if the argument at index 'i' is a reserved argument
 554 bool SigEntry::is_reserved_entry(const GrowableArray<SigEntry>* sig, int i) {
 555   return sig->at(i)._offset == SigEntry::ReservedOffset;
 556 }
 557 
 558 // Returns true if the argument at index 'i' is not a value type delimiter
 559 bool SigEntry::skip_value_delimiters(const GrowableArray<SigEntry>* sig, int i) {
 560   return (sig->at(i)._bt != T_VALUETYPE &&
 561           (sig->at(i)._bt != T_VOID || sig->at(i-1)._bt == T_LONG || sig->at(i-1)._bt == T_DOUBLE));
 562 }
 563 
 564 // Fill basic type array from signature array
 565 int SigEntry::fill_sig_bt(const GrowableArray<SigEntry>* sig, BasicType* sig_bt) {
 566   int count = 0;
 567   for (int i = 0; i < sig->length(); i++) {
 568     if (skip_value_delimiters(sig, i)) {
 569       sig_bt[count++] = sig->at(i)._bt;
 570     }
 571   }
 572   return count;
 573 }
 574 
 575 // Create a temporary symbol from the signature array
 576 TempNewSymbol SigEntry::create_symbol(const GrowableArray<SigEntry>* sig) {
 577   ResourceMark rm;
 578   int length = sig->length();
 579   char* sig_str = NEW_RESOURCE_ARRAY(char, 2*length + 3);
 580   int idx = 0;
 581   sig_str[idx++] = '(';
 582   for (int i = 0; i < length; i++) {
 583     BasicType bt = sig->at(i)._bt;
 584     if (bt == T_VALUETYPE || bt == T_VOID) {
 585       // Ignore
 586     } else {
 587       if (bt == T_ARRAY) {
 588         bt = T_OBJECT; // We don't know the element type, treat as Object
 589       }
 590       sig_str[idx++] = type2char(bt);
 591       if (bt == T_OBJECT) {
 592         sig_str[idx++] = ';';
 593       }
 594     }
 595   }
 596   sig_str[idx++] = ')';
 597   sig_str[idx++] = '\0';
 598   return SymbolTable::new_symbol(sig_str);
 599 }
 600 
 601 // Increment signature iterator (skips value type delimiters and T_VOID) and check if next entry is reserved
 602 bool SigEntry::next_is_reserved(ExtendedSignature& sig, BasicType& bt, bool can_be_void) {
 603   assert(can_be_void || bt != T_VOID, "should never see void");
 604   if (sig.at_end() || (can_be_void && type2size[bt] == 2 && (*sig)._offset != SigEntry::ReservedOffset)) {
 605     // Don't increment at the end or at a T_LONG/T_DOUBLE which will be followed by a (skipped) T_VOID
 606     return false;
 607   }
 608   assert(bt == T_VOID || type2wfield[bt] == type2wfield[(*sig)._bt], "inconsistent signature");
 609   ++sig;
 610   if (!sig.at_end() && (*sig)._offset == SigEntry::ReservedOffset) {
 611     bt = (*sig)._bt;
 612     return true;
 613   }
 614   return false;
 615 }
< prev index next >