0N/A/*
2273N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#ifndef SHARE_VM_RUNTIME_SIGNATURE_HPP
1879N/A#define SHARE_VM_RUNTIME_SIGNATURE_HPP
1879N/A
1879N/A#include "memory/allocation.hpp"
1879N/A#include "oops/methodOop.hpp"
1879N/A#include "utilities/top.hpp"
1879N/A
0N/A// SignatureIterators iterate over a Java signature (or parts of it).
0N/A// (Syntax according to: "The Java Virtual Machine Specification" by
0N/A// Tim Lindholm & Frank Yellin; section 4.3 Descriptors; p. 89ff.)
0N/A//
0N/A// Example: Iterating over ([Lfoo;D)I using
0N/A// 0123456789
0N/A//
0N/A// iterate_parameters() calls: do_array(2, 7); do_double();
0N/A// iterate_returntype() calls: do_int();
0N/A// iterate() calls: do_array(2, 7); do_double(); do_int();
0N/A//
0N/A// is_return_type() is: false ; false ; true
0N/A//
0N/A// NOTE: The new optimizer has an alternate, for-loop based signature
0N/A// iterator implemented in opto/type.cpp, TypeTuple::make().
0N/A
0N/Aclass SignatureIterator: public ResourceObj {
0N/A protected:
2062N/A Symbol* _signature; // the signature to iterate over
0N/A int _index; // the current character index (only valid during iteration)
0N/A int _parameter_index; // the current parameter index (0 outside iteration phase)
0N/A BasicType _return_type;
0N/A
0N/A void expect(char c);
0N/A void skip_optional_size();
0N/A int parse_type(); // returns the parameter size in words (0 for void)
0N/A void check_signature_end();
0N/A
0N/A public:
0N/A // Definitions used in generating and iterating the
0N/A // bit field form of the signature generated by the
0N/A // Fingerprinter.
0N/A enum {
0N/A static_feature_size = 1,
0N/A result_feature_size = 4,
0N/A result_feature_mask = 0xF,
0N/A parameter_feature_size = 4,
0N/A parameter_feature_mask = 0xF,
0N/A
0N/A bool_parm = 1,
0N/A byte_parm = 2,
0N/A char_parm = 3,
0N/A short_parm = 4,
0N/A int_parm = 5,
0N/A long_parm = 6,
0N/A float_parm = 7,
0N/A double_parm = 8,
0N/A obj_parm = 9,
0N/A done_parm = 10, // marker for end of parameters
0N/A
0N/A // max parameters is wordsize minus
0N/A // The sign bit, termination field, the result and static bit fields
0N/A max_size_of_parameters = (BitsPerLong-1 -
0N/A result_feature_size - parameter_feature_size -
0N/A static_feature_size) / parameter_feature_size
0N/A };
0N/A
0N/A // Constructors
2062N/A SignatureIterator(Symbol* signature);
0N/A
0N/A // Iteration
0N/A void dispatch_field(); // dispatches once for field signatures
0N/A void iterate_parameters(); // iterates over parameters only
0N/A void iterate_parameters( uint64_t fingerprint );
0N/A void iterate_returntype(); // iterates over returntype only
0N/A void iterate(); // iterates over whole signature
0N/A // Returns the word index of the current parameter;
0N/A int parameter_index() const { return _parameter_index; }
0N/A bool is_return_type() const { return parameter_index() < 0; }
0N/A BasicType get_ret_type() const { return _return_type; }
0N/A
0N/A // Basic types
0N/A virtual void do_bool () = 0;
0N/A virtual void do_char () = 0;
0N/A virtual void do_float () = 0;
0N/A virtual void do_double() = 0;
0N/A virtual void do_byte () = 0;
0N/A virtual void do_short () = 0;
0N/A virtual void do_int () = 0;
0N/A virtual void do_long () = 0;
0N/A virtual void do_void () = 0;
0N/A
0N/A // Object types (begin indexes the first character of the entry, end indexes the first character after the entry)
0N/A virtual void do_object(int begin, int end) = 0;
0N/A virtual void do_array (int begin, int end) = 0;
0N/A};
0N/A
0N/A
0N/A// Specialized SignatureIterators: Used to compute signature specific values.
0N/A
0N/Aclass SignatureTypeNames : public SignatureIterator {
0N/A protected:
0N/A virtual void type_name(const char* name) = 0;
0N/A
0N/A void do_bool() { type_name("jboolean"); }
0N/A void do_char() { type_name("jchar" ); }
0N/A void do_float() { type_name("jfloat" ); }
0N/A void do_double() { type_name("jdouble" ); }
0N/A void do_byte() { type_name("jbyte" ); }
0N/A void do_short() { type_name("jshort" ); }
0N/A void do_int() { type_name("jint" ); }
0N/A void do_long() { type_name("jlong" ); }
0N/A void do_void() { type_name("void" ); }
0N/A void do_object(int begin, int end) { type_name("jobject" ); }
0N/A void do_array (int begin, int end) { type_name("jobject" ); }
0N/A
0N/A public:
2062N/A SignatureTypeNames(Symbol* signature) : SignatureIterator(signature) {}
0N/A};
0N/A
0N/A
0N/Aclass SignatureInfo: public SignatureIterator {
0N/A protected:
0N/A bool _has_iterated; // need this because iterate cannot be called in constructor (set is virtual!)
0N/A bool _has_iterated_return;
0N/A int _size;
0N/A
0N/A void lazy_iterate_parameters() { if (!_has_iterated) { iterate_parameters(); _has_iterated = true; } }
0N/A void lazy_iterate_return() { if (!_has_iterated_return) { iterate_returntype(); _has_iterated_return = true; } }
0N/A
0N/A virtual void set(int size, BasicType type) = 0;
0N/A
0N/A void do_bool () { set(T_BOOLEAN_size, T_BOOLEAN); }
0N/A void do_char () { set(T_CHAR_size , T_CHAR ); }
0N/A void do_float () { set(T_FLOAT_size , T_FLOAT ); }
0N/A void do_double() { set(T_DOUBLE_size , T_DOUBLE ); }
0N/A void do_byte () { set(T_BYTE_size , T_BYTE ); }
0N/A void do_short () { set(T_SHORT_size , T_SHORT ); }
0N/A void do_int () { set(T_INT_size , T_INT ); }
0N/A void do_long () { set(T_LONG_size , T_LONG ); }
0N/A void do_void () { set(T_VOID_size , T_VOID ); }
0N/A void do_object(int begin, int end) { set(T_OBJECT_size , T_OBJECT ); }
0N/A void do_array (int begin, int end) { set(T_ARRAY_size , T_ARRAY ); }
0N/A
0N/A public:
2062N/A SignatureInfo(Symbol* signature) : SignatureIterator(signature) {
0N/A _has_iterated = _has_iterated_return = false;
0N/A _size = 0;
0N/A _return_type = T_ILLEGAL;
0N/A }
0N/A
0N/A};
0N/A
0N/A
0N/A// Specialized SignatureIterator: Used to compute the argument size.
0N/A
0N/Aclass ArgumentSizeComputer: public SignatureInfo {
0N/A private:
0N/A void set(int size, BasicType type) { _size += size; }
0N/A public:
2062N/A ArgumentSizeComputer(Symbol* signature) : SignatureInfo(signature) {}
0N/A
0N/A int size() { lazy_iterate_parameters(); return _size; }
0N/A};
0N/A
0N/A
0N/Aclass ArgumentCount: public SignatureInfo {
0N/A private:
0N/A void set(int size, BasicType type) { _size ++; }
0N/A public:
2062N/A ArgumentCount(Symbol* signature) : SignatureInfo(signature) {}
0N/A
0N/A int size() { lazy_iterate_parameters(); return _size; }
0N/A};
0N/A
0N/A
0N/A// Specialized SignatureIterator: Used to compute the result type.
0N/A
0N/Aclass ResultTypeFinder: public SignatureInfo {
0N/A private:
0N/A void set(int size, BasicType type) { _return_type = type; }
0N/A public:
0N/A BasicType type() { lazy_iterate_return(); return _return_type; }
0N/A
2062N/A ResultTypeFinder(Symbol* signature) : SignatureInfo(signature) {}
0N/A};
0N/A
0N/A
0N/A// Fingerprinter computes a unique ID for a given method. The ID
0N/A// is a bitvector characterizing the methods signature (incl. the receiver).
0N/Aclass Fingerprinter: public SignatureIterator {
0N/A private:
0N/A uint64_t _fingerprint;
0N/A int _shift_count;
0N/A methodHandle mh;
0N/A
0N/A public:
0N/A
0N/A void do_bool() { _fingerprint |= (((uint64_t)bool_parm) << _shift_count); _shift_count += parameter_feature_size; }
0N/A void do_char() { _fingerprint |= (((uint64_t)char_parm) << _shift_count); _shift_count += parameter_feature_size; }
0N/A void do_byte() { _fingerprint |= (((uint64_t)byte_parm) << _shift_count); _shift_count += parameter_feature_size; }
0N/A void do_short() { _fingerprint |= (((uint64_t)short_parm) << _shift_count); _shift_count += parameter_feature_size; }
0N/A void do_int() { _fingerprint |= (((uint64_t)int_parm) << _shift_count); _shift_count += parameter_feature_size; }
0N/A void do_long() { _fingerprint |= (((uint64_t)long_parm) << _shift_count); _shift_count += parameter_feature_size; }
0N/A void do_float() { _fingerprint |= (((uint64_t)float_parm) << _shift_count); _shift_count += parameter_feature_size; }
0N/A void do_double() { _fingerprint |= (((uint64_t)double_parm) << _shift_count); _shift_count += parameter_feature_size; }
0N/A
0N/A void do_object(int begin, int end) { _fingerprint |= (((uint64_t)obj_parm) << _shift_count); _shift_count += parameter_feature_size; }
0N/A void do_array (int begin, int end) { _fingerprint |= (((uint64_t)obj_parm) << _shift_count); _shift_count += parameter_feature_size; }
0N/A
0N/A void do_void() { ShouldNotReachHere(); }
0N/A
0N/A Fingerprinter(methodHandle method) : SignatureIterator(method->signature()) {
0N/A mh = method;
0N/A _fingerprint = 0;
0N/A }
0N/A
0N/A uint64_t fingerprint() {
0N/A // See if we fingerprinted this method already
0N/A if (mh->constMethod()->fingerprint() != CONST64(0)) {
0N/A return mh->constMethod()->fingerprint();
0N/A }
0N/A
0N/A if (mh->size_of_parameters() > max_size_of_parameters ) {
0N/A _fingerprint = UCONST64(-1);
0N/A mh->constMethod()->set_fingerprint(_fingerprint);
0N/A return _fingerprint;
0N/A }
0N/A
0N/A assert( (int)mh->result_type() <= (int)result_feature_mask, "bad result type");
0N/A _fingerprint = mh->result_type();
0N/A _fingerprint <<= static_feature_size;
0N/A if (mh->is_static()) _fingerprint |= 1;
0N/A _shift_count = result_feature_size + static_feature_size;
0N/A iterate_parameters();
0N/A _fingerprint |= ((uint64_t)done_parm) << _shift_count;// mark end of sig
0N/A mh->constMethod()->set_fingerprint(_fingerprint);
0N/A return _fingerprint;
0N/A }
0N/A};
0N/A
0N/A
0N/A// Specialized SignatureIterator: Used for native call purposes
0N/A
0N/Aclass NativeSignatureIterator: public SignatureIterator {
0N/A private:
0N/A methodHandle _method;
605N/A// We need separate JNI and Java offset values because in 64 bit mode,
0N/A// the argument offsets are not in sync with the Java stack.
0N/A// For example a long takes up 1 "C" stack entry but 2 Java stack entries.
0N/A int _offset; // The java stack offset
0N/A int _prepended; // number of prepended JNI parameters (1 JNIEnv, plus 1 mirror if static)
0N/A int _jni_offset; // the current parameter offset, starting with 0
0N/A
0N/A void do_bool () { pass_int(); _jni_offset++; _offset++; }
0N/A void do_char () { pass_int(); _jni_offset++; _offset++; }
1010N/A void do_float () { pass_float(); _jni_offset++; _offset++; }
0N/A#ifdef _LP64
0N/A void do_double() { pass_double(); _jni_offset++; _offset += 2; }
0N/A#else
0N/A void do_double() { pass_double(); _jni_offset += 2; _offset += 2; }
0N/A#endif
0N/A void do_byte () { pass_int(); _jni_offset++; _offset++; }
0N/A void do_short () { pass_int(); _jni_offset++; _offset++; }
0N/A void do_int () { pass_int(); _jni_offset++; _offset++; }
0N/A#ifdef _LP64
0N/A void do_long () { pass_long(); _jni_offset++; _offset += 2; }
0N/A#else
0N/A void do_long () { pass_long(); _jni_offset += 2; _offset += 2; }
0N/A#endif
0N/A void do_void () { ShouldNotReachHere(); }
0N/A void do_object(int begin, int end) { pass_object(); _jni_offset++; _offset++; }
0N/A void do_array (int begin, int end) { pass_object(); _jni_offset++; _offset++; }
0N/A
0N/A public:
0N/A methodHandle method() const { return _method; }
0N/A int offset() const { return _offset; }
0N/A int jni_offset() const { return _jni_offset + _prepended; }
0N/A// int java_offset() const { return method()->size_of_parameters() - _offset - 1; }
0N/A bool is_static() const { return method()->is_static(); }
0N/A virtual void pass_int() = 0;
0N/A virtual void pass_long() = 0;
0N/A virtual void pass_object() = 0;
1010N/A virtual void pass_float() = 0;
0N/A#ifdef _LP64
0N/A virtual void pass_double() = 0;
0N/A#else
0N/A virtual void pass_double() { pass_long(); } // may be same as long
0N/A#endif
0N/A
0N/A NativeSignatureIterator(methodHandle method) : SignatureIterator(method->signature()) {
0N/A _method = method;
0N/A _offset = 0;
0N/A _jni_offset = 0;
0N/A
0N/A const int JNIEnv_words = 1;
0N/A const int mirror_words = 1;
0N/A _prepended = !is_static() ? JNIEnv_words : JNIEnv_words + mirror_words;
0N/A }
0N/A
0N/A // iterate() calles the 2 virtual methods according to the following invocation syntax:
0N/A //
0N/A // {pass_int | pass_long | pass_object}
0N/A //
0N/A // Arguments are handled from left to right (receiver first, if any).
0N/A // The offset() values refer to the Java stack offsets but are 0 based and increasing.
0N/A // The java_offset() values count down to 0, and refer to the Java TOS.
0N/A // The jni_offset() values increase from 1 or 2, and refer to C arguments.
0N/A
0N/A void iterate() { iterate(Fingerprinter(method()).fingerprint());
0N/A }
0N/A
0N/A
0N/A // Optimized path if we have the bitvector form of signature
0N/A void iterate( uint64_t fingerprint ) {
0N/A
0N/A if (!is_static()) {
0N/A // handle receiver (not handled by iterate because not in signature)
0N/A pass_object(); _jni_offset++; _offset++;
0N/A }
0N/A
0N/A SignatureIterator::iterate_parameters( fingerprint );
0N/A }
0N/A};
0N/A
0N/A
0N/A// Handy stream for iterating over signature
0N/A
0N/Aclass SignatureStream : public StackObj {
0N/A private:
2062N/A Symbol* _signature;
0N/A int _begin;
0N/A int _end;
0N/A BasicType _type;
0N/A bool _at_return_type;
2062N/A GrowableArray<Symbol*>* _names; // symbols created while parsing signature
0N/A
0N/A public:
0N/A bool at_return_type() const { return _at_return_type; }
0N/A bool is_done() const;
0N/A void next_non_primitive(int t);
0N/A void next() {
2062N/A Symbol* sig = _signature;
0N/A int len = sig->utf8_length();
0N/A if (_end >= len) {
0N/A _end = len + 1;
0N/A return;
0N/A }
0N/A
0N/A _begin = _end;
0N/A int t = sig->byte_at(_begin);
0N/A switch (t) {
0N/A case 'B': _type = T_BYTE; break;
0N/A case 'C': _type = T_CHAR; break;
0N/A case 'D': _type = T_DOUBLE; break;
0N/A case 'F': _type = T_FLOAT; break;
0N/A case 'I': _type = T_INT; break;
0N/A case 'J': _type = T_LONG; break;
0N/A case 'S': _type = T_SHORT; break;
0N/A case 'Z': _type = T_BOOLEAN; break;
0N/A case 'V': _type = T_VOID; break;
0N/A default : next_non_primitive(t);
0N/A return;
0N/A }
0N/A _end++;
0N/A }
0N/A
2062N/A SignatureStream(Symbol* signature, bool is_method = true);
2062N/A ~SignatureStream();
0N/A
0N/A bool is_object() const; // True if this argument is an object
0N/A bool is_array() const; // True if this argument is an array
0N/A BasicType type() const { return _type; }
2062N/A Symbol* as_symbol(TRAPS);
1428N/A enum FailureMode { ReturnNull, CNFException, NCDFError };
1428N/A klassOop as_klass(Handle class_loader, Handle protection_domain, FailureMode failure_mode, TRAPS);
1428N/A oop as_java_mirror(Handle class_loader, Handle protection_domain, FailureMode failure_mode, TRAPS);
3932N/A const jbyte* raw_bytes() { return _signature->bytes() + _begin; }
3932N/A int raw_length() { return _end - _begin; }
0N/A
0N/A // return same as_symbol except allocation of new symbols is avoided.
2062N/A Symbol* as_symbol_or_null();
0N/A};
0N/A
0N/Aclass SignatureVerifier : public StackObj {
0N/A public:
0N/A // Returns true if the symbol is valid method or type signature
2062N/A static bool is_valid_signature(Symbol* sig);
0N/A
2062N/A static bool is_valid_method_signature(Symbol* sig);
2062N/A static bool is_valid_type_signature(Symbol* sig);
0N/A private:
0N/A
0N/A static ssize_t is_valid_type(const char*, ssize_t);
0N/A static bool invalid_name_char(char);
0N/A};
1879N/A
1879N/A#endif // SHARE_VM_RUNTIME_SIGNATURE_HPP