hotspot/src/share/vm/oops/symbolOop.hpp
author twisti
Tue, 05 Jan 2010 15:21:25 +0100
changeset 4567 7fc02fbe5c7a
parent 1 489c9b5090e2
child 5547 f4b087cbb361
permissions -rw-r--r--
6893268: additional dynamic language related optimizations in C2 Summary: C2 needs some additional optimizations to be able to handle MethodHandle invokes and invokedynamic instructions at the best performance. Reviewed-by: kvn, never
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 1
diff changeset
     2
 * Copyright 1997-2009 Sun Microsystems, Inc.  All Rights Reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
// A symbolOop is a canonicalized string.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
// All symbolOops reside in global symbolTable.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
// See oopFactory::new_symbol for how to allocate a symbolOop
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
class symbolOopDesc : public oopDesc {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
  friend class VMStructs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  unsigned short _length; // number of UTF8 characters in the symbol
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
  jbyte _body[1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  enum {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
    // max_symbol_length is constrained by type of _length
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
    max_symbol_length = (1 << 16) -1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  // Low-level access (used with care, since not GC-safe)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  jbyte* base() { return &_body[0]; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  // Returns the largest size symbol we can safely hold.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  static int max_length() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
    return max_symbol_length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  static int object_size(int length) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
    int size = header_size() + (sizeof(unsigned short) + length + HeapWordSize - 1) / HeapWordSize;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
    return align_object_size(size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  int object_size() { return object_size(utf8_length()); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  int byte_at(int index) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
    assert(index >=0 && index < _length, "symbol index overflow");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
    return ((symbolOopDesc*)this)->base()[index];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  void byte_at_put(int index, int value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
    assert(index >=0 && index < _length, "symbol index overflow");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
    ((symbolOopDesc*)this)->base()[index] = value;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  jbyte* bytes() { return base(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  int utf8_length() const { return _length; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  void set_utf8_length(int len) { _length = len; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 1
diff changeset
    73
  // Compares the symbol with a string.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  bool equals(const char* str, int len) const;
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 1
diff changeset
    75
  bool equals(const char* str) const { return equals(str, (int) strlen(str)); }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 1
diff changeset
    76
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 1
diff changeset
    77
  // Tests if the symbol starts with the given prefix.
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 1
diff changeset
    78
  bool starts_with(const char* prefix, int len) const;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 1
diff changeset
    79
  bool starts_with(const char* prefix) const {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 1
diff changeset
    80
    return starts_with(prefix, (int) strlen(prefix));
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 1
diff changeset
    81
  }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 1
diff changeset
    82
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 1
diff changeset
    83
  // Tests if the symbol starts with the given prefix.
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 1
diff changeset
    84
  int index_of_at(int i, const char* str, int len) const;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 1
diff changeset
    85
  int index_of_at(int i, const char* str) const {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 1
diff changeset
    86
    return index_of_at(i, str, (int) strlen(str));
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 1
diff changeset
    87
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  // Three-way compare for sorting; returns -1/0/1 if receiver is </==/> than arg
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  // note that the ordering is not alfabetical
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  inline int fast_compare(symbolOop other) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  // Returns receiver converted to null-terminated UTF-8 string; string is
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  // allocated in resource area, or in the char buffer provided by caller.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  char* as_C_string() const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  char* as_C_string(char* buf, int size) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  // Use buf if needed buffer length is <= size.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  char* as_C_string_flexible_buffer(Thread* t, char* buf, int size) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  // Returns a null terminated utf8 string in a resource array
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  char* as_utf8() const { return as_C_string(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  char* as_utf8_flexible_buffer(Thread* t, char* buf, int size) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
    return as_C_string_flexible_buffer(t, buf, size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  jchar* as_unicode(int& length) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  // Treating this symbol as a class name, returns the Java name for the class.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  // String is allocated in resource area if buffer is not provided.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  // See Klass::external_name()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  const char* as_klass_external_name() const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  const char* as_klass_external_name(char* buf, int size) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  bool object_is_parsable() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
    return (utf8_length() > 0 || (oop)this == Universe::emptySymbol());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  // Printing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  void print_symbol_on(outputStream* st = NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
// Note: this comparison is used for vtable sorting only; it doesn't matter
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
// what order it defines, as long as it is a total, time-invariant order
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
// Since symbolOops are in permSpace, their relative order in memory never changes,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
// so use address comparison for speed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
int symbolOopDesc::fast_compare(symbolOop other) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
 return (((uintptr_t)this < (uintptr_t)other) ? -1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
   : ((uintptr_t)this == (uintptr_t) other) ? 0 : 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
}