src/share/vm/runtime/signature.hpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6198
55fb97c4c58d
parent 0
f90c822e73f8
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #ifndef SHARE_VM_RUNTIME_SIGNATURE_HPP
aoqi@0 26 #define SHARE_VM_RUNTIME_SIGNATURE_HPP
aoqi@0 27
aoqi@0 28 #include "memory/allocation.hpp"
aoqi@0 29 #include "oops/method.hpp"
aoqi@0 30 #include "utilities/top.hpp"
aoqi@0 31
aoqi@0 32 // SignatureIterators iterate over a Java signature (or parts of it).
aoqi@0 33 // (Syntax according to: "The Java Virtual Machine Specification" by
aoqi@0 34 // Tim Lindholm & Frank Yellin; section 4.3 Descriptors; p. 89ff.)
aoqi@0 35 //
aoqi@0 36 // Example: Iterating over ([Lfoo;D)I using
aoqi@0 37 // 0123456789
aoqi@0 38 //
aoqi@0 39 // iterate_parameters() calls: do_array(2, 7); do_double();
aoqi@0 40 // iterate_returntype() calls: do_int();
aoqi@0 41 // iterate() calls: do_array(2, 7); do_double(); do_int();
aoqi@0 42 //
aoqi@0 43 // is_return_type() is: false ; false ; true
aoqi@0 44 //
aoqi@0 45 // NOTE: The new optimizer has an alternate, for-loop based signature
aoqi@0 46 // iterator implemented in opto/type.cpp, TypeTuple::make().
aoqi@0 47
aoqi@0 48 class SignatureIterator: public ResourceObj {
aoqi@0 49 protected:
aoqi@0 50 Symbol* _signature; // the signature to iterate over
aoqi@0 51 int _index; // the current character index (only valid during iteration)
aoqi@0 52 int _parameter_index; // the current parameter index (0 outside iteration phase)
aoqi@0 53 BasicType _return_type;
aoqi@0 54
aoqi@0 55 void expect(char c);
aoqi@0 56 void skip_optional_size();
aoqi@0 57 int parse_type(); // returns the parameter size in words (0 for void)
aoqi@0 58 void check_signature_end();
aoqi@0 59
aoqi@0 60 public:
aoqi@0 61 // Definitions used in generating and iterating the
aoqi@0 62 // bit field form of the signature generated by the
aoqi@0 63 // Fingerprinter.
aoqi@0 64 enum {
aoqi@0 65 static_feature_size = 1,
aoqi@0 66 result_feature_size = 4,
aoqi@0 67 result_feature_mask = 0xF,
aoqi@0 68 parameter_feature_size = 4,
aoqi@0 69 parameter_feature_mask = 0xF,
aoqi@0 70
aoqi@0 71 bool_parm = 1,
aoqi@0 72 byte_parm = 2,
aoqi@0 73 char_parm = 3,
aoqi@0 74 short_parm = 4,
aoqi@0 75 int_parm = 5,
aoqi@0 76 long_parm = 6,
aoqi@0 77 float_parm = 7,
aoqi@0 78 double_parm = 8,
aoqi@0 79 obj_parm = 9,
aoqi@0 80 done_parm = 10, // marker for end of parameters
aoqi@0 81
aoqi@0 82 // max parameters is wordsize minus
aoqi@0 83 // The sign bit, termination field, the result and static bit fields
aoqi@0 84 max_size_of_parameters = (BitsPerLong-1 -
aoqi@0 85 result_feature_size - parameter_feature_size -
aoqi@0 86 static_feature_size) / parameter_feature_size
aoqi@0 87 };
aoqi@0 88
aoqi@0 89 // Constructors
aoqi@0 90 SignatureIterator(Symbol* signature);
aoqi@0 91
aoqi@0 92 // Iteration
aoqi@0 93 void dispatch_field(); // dispatches once for field signatures
aoqi@0 94 void iterate_parameters(); // iterates over parameters only
aoqi@0 95 void iterate_parameters( uint64_t fingerprint );
aoqi@0 96 void iterate_returntype(); // iterates over returntype only
aoqi@0 97 void iterate(); // iterates over whole signature
aoqi@0 98 // Returns the word index of the current parameter;
aoqi@0 99 int parameter_index() const { return _parameter_index; }
aoqi@0 100 bool is_return_type() const { return parameter_index() < 0; }
aoqi@0 101 BasicType get_ret_type() const { return _return_type; }
aoqi@0 102
aoqi@0 103 // Basic types
aoqi@0 104 virtual void do_bool () = 0;
aoqi@0 105 virtual void do_char () = 0;
aoqi@0 106 virtual void do_float () = 0;
aoqi@0 107 virtual void do_double() = 0;
aoqi@0 108 virtual void do_byte () = 0;
aoqi@0 109 virtual void do_short () = 0;
aoqi@0 110 virtual void do_int () = 0;
aoqi@0 111 virtual void do_long () = 0;
aoqi@0 112 virtual void do_void () = 0;
aoqi@0 113
aoqi@0 114 // Object types (begin indexes the first character of the entry, end indexes the first character after the entry)
aoqi@0 115 virtual void do_object(int begin, int end) = 0;
aoqi@0 116 virtual void do_array (int begin, int end) = 0;
aoqi@0 117 };
aoqi@0 118
aoqi@0 119
aoqi@0 120 // Specialized SignatureIterators: Used to compute signature specific values.
aoqi@0 121
aoqi@0 122 class SignatureTypeNames : public SignatureIterator {
aoqi@0 123 protected:
aoqi@0 124 virtual void type_name(const char* name) = 0;
aoqi@0 125
aoqi@0 126 void do_bool() { type_name("jboolean"); }
aoqi@0 127 void do_char() { type_name("jchar" ); }
aoqi@0 128 void do_float() { type_name("jfloat" ); }
aoqi@0 129 void do_double() { type_name("jdouble" ); }
aoqi@0 130 void do_byte() { type_name("jbyte" ); }
aoqi@0 131 void do_short() { type_name("jshort" ); }
aoqi@0 132 void do_int() { type_name("jint" ); }
aoqi@0 133 void do_long() { type_name("jlong" ); }
aoqi@0 134 void do_void() { type_name("void" ); }
aoqi@0 135 void do_object(int begin, int end) { type_name("jobject" ); }
aoqi@0 136 void do_array (int begin, int end) { type_name("jobject" ); }
aoqi@0 137
aoqi@0 138 public:
aoqi@0 139 SignatureTypeNames(Symbol* signature) : SignatureIterator(signature) {}
aoqi@0 140 };
aoqi@0 141
aoqi@0 142
aoqi@0 143 class SignatureInfo: public SignatureIterator {
aoqi@0 144 protected:
aoqi@0 145 bool _has_iterated; // need this because iterate cannot be called in constructor (set is virtual!)
aoqi@0 146 bool _has_iterated_return;
aoqi@0 147 int _size;
aoqi@0 148
aoqi@0 149 void lazy_iterate_parameters() { if (!_has_iterated) { iterate_parameters(); _has_iterated = true; } }
aoqi@0 150 void lazy_iterate_return() { if (!_has_iterated_return) { iterate_returntype(); _has_iterated_return = true; } }
aoqi@0 151
aoqi@0 152 virtual void set(int size, BasicType type) = 0;
aoqi@0 153
aoqi@0 154 void do_bool () { set(T_BOOLEAN_size, T_BOOLEAN); }
aoqi@0 155 void do_char () { set(T_CHAR_size , T_CHAR ); }
aoqi@0 156 void do_float () { set(T_FLOAT_size , T_FLOAT ); }
aoqi@0 157 void do_double() { set(T_DOUBLE_size , T_DOUBLE ); }
aoqi@0 158 void do_byte () { set(T_BYTE_size , T_BYTE ); }
aoqi@0 159 void do_short () { set(T_SHORT_size , T_SHORT ); }
aoqi@0 160 void do_int () { set(T_INT_size , T_INT ); }
aoqi@0 161 void do_long () { set(T_LONG_size , T_LONG ); }
aoqi@0 162 void do_void () { set(T_VOID_size , T_VOID ); }
aoqi@0 163 void do_object(int begin, int end) { set(T_OBJECT_size , T_OBJECT ); }
aoqi@0 164 void do_array (int begin, int end) { set(T_ARRAY_size , T_ARRAY ); }
aoqi@0 165
aoqi@0 166 public:
aoqi@0 167 SignatureInfo(Symbol* signature) : SignatureIterator(signature) {
aoqi@0 168 _has_iterated = _has_iterated_return = false;
aoqi@0 169 _size = 0;
aoqi@0 170 _return_type = T_ILLEGAL;
aoqi@0 171 }
aoqi@0 172
aoqi@0 173 };
aoqi@0 174
aoqi@0 175
aoqi@0 176 // Specialized SignatureIterator: Used to compute the argument size.
aoqi@0 177
aoqi@0 178 class ArgumentSizeComputer: public SignatureInfo {
aoqi@0 179 private:
aoqi@0 180 void set(int size, BasicType type) { _size += size; }
aoqi@0 181 public:
aoqi@0 182 ArgumentSizeComputer(Symbol* signature) : SignatureInfo(signature) {}
aoqi@0 183
aoqi@0 184 int size() { lazy_iterate_parameters(); return _size; }
aoqi@0 185 };
aoqi@0 186
aoqi@0 187
aoqi@0 188 class ArgumentCount: public SignatureInfo {
aoqi@0 189 private:
aoqi@0 190 void set(int size, BasicType type) { _size ++; }
aoqi@0 191 public:
aoqi@0 192 ArgumentCount(Symbol* signature) : SignatureInfo(signature) {}
aoqi@0 193
aoqi@0 194 int size() { lazy_iterate_parameters(); return _size; }
aoqi@0 195 };
aoqi@0 196
aoqi@0 197
aoqi@0 198 // Specialized SignatureIterator: Used to compute the result type.
aoqi@0 199
aoqi@0 200 class ResultTypeFinder: public SignatureInfo {
aoqi@0 201 private:
aoqi@0 202 void set(int size, BasicType type) { _return_type = type; }
aoqi@0 203 public:
aoqi@0 204 BasicType type() { lazy_iterate_return(); return _return_type; }
aoqi@0 205
aoqi@0 206 ResultTypeFinder(Symbol* signature) : SignatureInfo(signature) {}
aoqi@0 207 };
aoqi@0 208
aoqi@0 209
aoqi@0 210 // Fingerprinter computes a unique ID for a given method. The ID
aoqi@0 211 // is a bitvector characterizing the methods signature (incl. the receiver).
aoqi@0 212 class Fingerprinter: public SignatureIterator {
aoqi@0 213 private:
aoqi@0 214 uint64_t _fingerprint;
aoqi@0 215 int _shift_count;
aoqi@0 216 methodHandle mh;
aoqi@0 217
aoqi@0 218 public:
aoqi@0 219
aoqi@0 220 void do_bool() { _fingerprint |= (((uint64_t)bool_parm) << _shift_count); _shift_count += parameter_feature_size; }
aoqi@0 221 void do_char() { _fingerprint |= (((uint64_t)char_parm) << _shift_count); _shift_count += parameter_feature_size; }
aoqi@0 222 void do_byte() { _fingerprint |= (((uint64_t)byte_parm) << _shift_count); _shift_count += parameter_feature_size; }
aoqi@0 223 void do_short() { _fingerprint |= (((uint64_t)short_parm) << _shift_count); _shift_count += parameter_feature_size; }
aoqi@0 224 void do_int() { _fingerprint |= (((uint64_t)int_parm) << _shift_count); _shift_count += parameter_feature_size; }
aoqi@0 225 void do_long() { _fingerprint |= (((uint64_t)long_parm) << _shift_count); _shift_count += parameter_feature_size; }
aoqi@0 226 void do_float() { _fingerprint |= (((uint64_t)float_parm) << _shift_count); _shift_count += parameter_feature_size; }
aoqi@0 227 void do_double() { _fingerprint |= (((uint64_t)double_parm) << _shift_count); _shift_count += parameter_feature_size; }
aoqi@0 228
aoqi@0 229 void do_object(int begin, int end) { _fingerprint |= (((uint64_t)obj_parm) << _shift_count); _shift_count += parameter_feature_size; }
aoqi@0 230 void do_array (int begin, int end) { _fingerprint |= (((uint64_t)obj_parm) << _shift_count); _shift_count += parameter_feature_size; }
aoqi@0 231
aoqi@0 232 void do_void() { ShouldNotReachHere(); }
aoqi@0 233
aoqi@0 234 Fingerprinter(methodHandle method) : SignatureIterator(method->signature()) {
aoqi@0 235 mh = method;
aoqi@0 236 _fingerprint = 0;
aoqi@0 237 }
aoqi@0 238
aoqi@0 239 uint64_t fingerprint() {
aoqi@0 240 // See if we fingerprinted this method already
aoqi@0 241 if (mh->constMethod()->fingerprint() != CONST64(0)) {
aoqi@0 242 return mh->constMethod()->fingerprint();
aoqi@0 243 }
aoqi@0 244
aoqi@0 245 if (mh->size_of_parameters() > max_size_of_parameters ) {
aoqi@0 246 _fingerprint = UCONST64(-1);
aoqi@0 247 mh->constMethod()->set_fingerprint(_fingerprint);
aoqi@0 248 return _fingerprint;
aoqi@0 249 }
aoqi@0 250
aoqi@0 251 assert( (int)mh->result_type() <= (int)result_feature_mask, "bad result type");
aoqi@0 252 _fingerprint = mh->result_type();
aoqi@0 253 _fingerprint <<= static_feature_size;
aoqi@0 254 if (mh->is_static()) _fingerprint |= 1;
aoqi@0 255 _shift_count = result_feature_size + static_feature_size;
aoqi@0 256 iterate_parameters();
aoqi@0 257 _fingerprint |= ((uint64_t)done_parm) << _shift_count;// mark end of sig
aoqi@0 258 mh->constMethod()->set_fingerprint(_fingerprint);
aoqi@0 259 return _fingerprint;
aoqi@0 260 }
aoqi@0 261 };
aoqi@0 262
aoqi@0 263
aoqi@0 264 // Specialized SignatureIterator: Used for native call purposes
aoqi@0 265
aoqi@0 266 class NativeSignatureIterator: public SignatureIterator {
aoqi@0 267 private:
aoqi@0 268 methodHandle _method;
aoqi@0 269 // We need separate JNI and Java offset values because in 64 bit mode,
aoqi@0 270 // the argument offsets are not in sync with the Java stack.
aoqi@0 271 // For example a long takes up 1 "C" stack entry but 2 Java stack entries.
aoqi@0 272 int _offset; // The java stack offset
aoqi@0 273 int _prepended; // number of prepended JNI parameters (1 JNIEnv, plus 1 mirror if static)
aoqi@0 274 int _jni_offset; // the current parameter offset, starting with 0
aoqi@0 275
aoqi@0 276 void do_bool () { pass_int(); _jni_offset++; _offset++; }
aoqi@0 277 void do_char () { pass_int(); _jni_offset++; _offset++; }
aoqi@0 278 void do_float () { pass_float(); _jni_offset++; _offset++; }
aoqi@0 279 #ifdef _LP64
aoqi@0 280 void do_double() { pass_double(); _jni_offset++; _offset += 2; }
aoqi@0 281 #else
aoqi@0 282 void do_double() { pass_double(); _jni_offset += 2; _offset += 2; }
aoqi@0 283 #endif
aoqi@0 284 void do_byte () { pass_int(); _jni_offset++; _offset++; }
aoqi@0 285 void do_short () { pass_int(); _jni_offset++; _offset++; }
aoqi@0 286 void do_int () { pass_int(); _jni_offset++; _offset++; }
aoqi@0 287 #ifdef _LP64
aoqi@0 288 void do_long () { pass_long(); _jni_offset++; _offset += 2; }
aoqi@0 289 #else
aoqi@0 290 void do_long () { pass_long(); _jni_offset += 2; _offset += 2; }
aoqi@0 291 #endif
aoqi@0 292 void do_void () { ShouldNotReachHere(); }
aoqi@0 293 void do_object(int begin, int end) { pass_object(); _jni_offset++; _offset++; }
aoqi@0 294 void do_array (int begin, int end) { pass_object(); _jni_offset++; _offset++; }
aoqi@0 295
aoqi@0 296 public:
aoqi@0 297 methodHandle method() const { return _method; }
aoqi@0 298 int offset() const { return _offset; }
aoqi@0 299 int jni_offset() const { return _jni_offset + _prepended; }
aoqi@0 300 // int java_offset() const { return method()->size_of_parameters() - _offset - 1; }
aoqi@0 301 bool is_static() const { return method()->is_static(); }
aoqi@0 302 virtual void pass_int() = 0;
aoqi@0 303 virtual void pass_long() = 0;
aoqi@0 304 virtual void pass_object() = 0;
aoqi@0 305 virtual void pass_float() = 0;
aoqi@0 306 #ifdef _LP64
aoqi@0 307 virtual void pass_double() = 0;
aoqi@0 308 #else
aoqi@0 309 virtual void pass_double() { pass_long(); } // may be same as long
aoqi@0 310 #endif
aoqi@0 311
aoqi@0 312 NativeSignatureIterator(methodHandle method) : SignatureIterator(method->signature()) {
aoqi@0 313 _method = method;
aoqi@0 314 _offset = 0;
aoqi@0 315 _jni_offset = 0;
aoqi@0 316
aoqi@0 317 const int JNIEnv_words = 1;
aoqi@0 318 const int mirror_words = 1;
aoqi@0 319 _prepended = !is_static() ? JNIEnv_words : JNIEnv_words + mirror_words;
aoqi@0 320 }
aoqi@0 321
aoqi@0 322 // iterate() calles the 2 virtual methods according to the following invocation syntax:
aoqi@0 323 //
aoqi@0 324 // {pass_int | pass_long | pass_object}
aoqi@0 325 //
aoqi@0 326 // Arguments are handled from left to right (receiver first, if any).
aoqi@0 327 // The offset() values refer to the Java stack offsets but are 0 based and increasing.
aoqi@0 328 // The java_offset() values count down to 0, and refer to the Java TOS.
aoqi@0 329 // The jni_offset() values increase from 1 or 2, and refer to C arguments.
aoqi@0 330
aoqi@0 331 void iterate() { iterate(Fingerprinter(method()).fingerprint());
aoqi@0 332 }
aoqi@0 333
aoqi@0 334
aoqi@0 335 // Optimized path if we have the bitvector form of signature
aoqi@0 336 void iterate( uint64_t fingerprint ) {
aoqi@0 337
aoqi@0 338 if (!is_static()) {
aoqi@0 339 // handle receiver (not handled by iterate because not in signature)
aoqi@0 340 pass_object(); _jni_offset++; _offset++;
aoqi@0 341 }
aoqi@0 342
aoqi@0 343 SignatureIterator::iterate_parameters( fingerprint );
aoqi@0 344 }
aoqi@0 345 };
aoqi@0 346
aoqi@0 347
aoqi@0 348 // Handy stream for iterating over signature
aoqi@0 349
aoqi@0 350 class SignatureStream : public StackObj {
aoqi@0 351 private:
aoqi@0 352 Symbol* _signature;
aoqi@0 353 int _begin;
aoqi@0 354 int _end;
aoqi@0 355 BasicType _type;
aoqi@0 356 bool _at_return_type;
aoqi@0 357 GrowableArray<Symbol*>* _names; // symbols created while parsing signature
aoqi@0 358
aoqi@0 359 public:
aoqi@0 360 bool at_return_type() const { return _at_return_type; }
aoqi@0 361 bool is_done() const;
aoqi@0 362 void next_non_primitive(int t);
aoqi@0 363 void next() {
aoqi@0 364 Symbol* sig = _signature;
aoqi@0 365 int len = sig->utf8_length();
aoqi@0 366 if (_end >= len) {
aoqi@0 367 _end = len + 1;
aoqi@0 368 return;
aoqi@0 369 }
aoqi@0 370
aoqi@0 371 _begin = _end;
aoqi@0 372 int t = sig->byte_at(_begin);
aoqi@0 373 switch (t) {
aoqi@0 374 case 'B': _type = T_BYTE; break;
aoqi@0 375 case 'C': _type = T_CHAR; break;
aoqi@0 376 case 'D': _type = T_DOUBLE; break;
aoqi@0 377 case 'F': _type = T_FLOAT; break;
aoqi@0 378 case 'I': _type = T_INT; break;
aoqi@0 379 case 'J': _type = T_LONG; break;
aoqi@0 380 case 'S': _type = T_SHORT; break;
aoqi@0 381 case 'Z': _type = T_BOOLEAN; break;
aoqi@0 382 case 'V': _type = T_VOID; break;
aoqi@0 383 default : next_non_primitive(t);
aoqi@0 384 return;
aoqi@0 385 }
aoqi@0 386 _end++;
aoqi@0 387 }
aoqi@0 388
aoqi@0 389 SignatureStream(Symbol* signature, bool is_method = true);
aoqi@0 390 ~SignatureStream();
aoqi@0 391
aoqi@0 392 bool is_object() const; // True if this argument is an object
aoqi@0 393 bool is_array() const; // True if this argument is an array
aoqi@0 394 BasicType type() const { return _type; }
aoqi@0 395 Symbol* as_symbol(TRAPS);
aoqi@0 396 enum FailureMode { ReturnNull, CNFException, NCDFError };
aoqi@0 397 Klass* as_klass(Handle class_loader, Handle protection_domain, FailureMode failure_mode, TRAPS);
aoqi@0 398 oop as_java_mirror(Handle class_loader, Handle protection_domain, FailureMode failure_mode, TRAPS);
aoqi@0 399 const jbyte* raw_bytes() { return _signature->bytes() + _begin; }
aoqi@0 400 int raw_length() { return _end - _begin; }
aoqi@0 401
aoqi@0 402 // return same as_symbol except allocation of new symbols is avoided.
aoqi@0 403 Symbol* as_symbol_or_null();
aoqi@0 404
aoqi@0 405 // count the number of references in the signature
aoqi@0 406 int reference_parameter_count();
aoqi@0 407 };
aoqi@0 408
aoqi@0 409 class SignatureVerifier : public StackObj {
aoqi@0 410 public:
aoqi@0 411 // Returns true if the symbol is valid method or type signature
aoqi@0 412 static bool is_valid_signature(Symbol* sig);
aoqi@0 413
aoqi@0 414 static bool is_valid_method_signature(Symbol* sig);
aoqi@0 415 static bool is_valid_type_signature(Symbol* sig);
aoqi@0 416 private:
aoqi@0 417
aoqi@0 418 static ssize_t is_valid_type(const char*, ssize_t);
aoqi@0 419 static bool invalid_name_char(char);
aoqi@0 420 };
aoqi@0 421
aoqi@0 422 #endif // SHARE_VM_RUNTIME_SIGNATURE_HPP

mercurial