hotspot/src/share/vm/oops/symbolOop.cpp
author twisti
Tue, 05 Jan 2010 15:21:25 +0100
changeset 4567 7fc02fbe5c7a
parent 768 d0bebc7eefc2
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: 768
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
# include "incls/_precompiled.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
# include "incls/_symbolOop.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    28
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    29
// ------------------------------------------------------------------
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    30
// symbolOopDesc::equals
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    31
//
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    32
// Compares the symbol with a string of the given length.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
bool symbolOopDesc::equals(const char* str, int len) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
  int l = utf8_length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  if (l != len) return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
  while (l-- > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
    if (str[l] != (char) byte_at(l))
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  assert(l == -1, "we should be at the beginning");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
4567
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    44
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    45
// ------------------------------------------------------------------
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    46
// symbolOopDesc::starts_with
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    47
//
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    48
// Tests if the symbol starts with the specified prefix of the given
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    49
// length.
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    50
bool symbolOopDesc::starts_with(const char* prefix, int len) const {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    51
  if (len > utf8_length()) return false;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    52
  while (len-- > 0) {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    53
    if (prefix[len] != (char) byte_at(len))
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    54
      return false;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    55
  }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    56
  assert(len == -1, "we should be at the beginning");
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    57
  return true;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    58
}
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    59
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    60
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    61
// ------------------------------------------------------------------
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    62
// symbolOopDesc::index_of
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    63
//
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    64
// Finds if the given string is a substring of this symbol's utf8 bytes.
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    65
// Return -1 on failure.  Otherwise return the first index where str occurs.
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    66
int symbolOopDesc::index_of_at(int i, const char* str, int len) const {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    67
  assert(i >= 0 && i <= utf8_length(), "oob");
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    68
  if (len <= 0)  return 0;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    69
  char first_char = str[0];
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    70
  address bytes = (address) ((symbolOopDesc*)this)->base();
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    71
  address limit = bytes + utf8_length() - len;  // inclusive limit
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    72
  address scan = bytes + i;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    73
  if (scan > limit)
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    74
    return -1;
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    75
  for (;;) {
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    76
    scan = (address) memchr(scan, first_char, (limit + 1 - scan));
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    77
    if (scan == NULL)
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    78
      return -1;  // not found
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    79
    assert(scan >= bytes+i && scan <= limit, "scan oob");
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    80
    if (memcmp(scan, str, len) == 0)
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    81
      return (int)(scan - bytes);
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    82
  }
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    83
}
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    84
7fc02fbe5c7a 6893268: additional dynamic language related optimizations in C2
twisti
parents: 768
diff changeset
    85
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
char* symbolOopDesc::as_C_string(char* buf, int size) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  if (size > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
    int len = MIN2(size - 1, utf8_length());
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
    for (int i = 0; i < len; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
      buf[i] = byte_at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
    buf[len] = '\0';
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  return buf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
char* symbolOopDesc::as_C_string() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  int len = utf8_length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  char* str = NEW_RESOURCE_ARRAY(char, len + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  return as_C_string(str, len + 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
char* symbolOopDesc::as_C_string_flexible_buffer(Thread* t,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
                                                 char* buf, int size) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  char* str;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  int len = utf8_length();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  int buf_len = len + 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  if (size < buf_len) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
    str = NEW_RESOURCE_ARRAY(char, buf_len);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
    str = buf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  return as_C_string(str, buf_len);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
void symbolOopDesc::print_symbol_on(outputStream* st) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  st = st ? st : tty;
768
d0bebc7eefc2 6718676: putback for 6604014 is incomplete
never
parents: 1
diff changeset
   118
  int length = UTF8::unicode_length((const char*)bytes(), utf8_length());
d0bebc7eefc2 6718676: putback for 6604014 is incomplete
never
parents: 1
diff changeset
   119
  const char *ptr = (const char *)bytes();
d0bebc7eefc2 6718676: putback for 6604014 is incomplete
never
parents: 1
diff changeset
   120
  jchar value;
d0bebc7eefc2 6718676: putback for 6604014 is incomplete
never
parents: 1
diff changeset
   121
  for (int index = 0; index < length; index++) {
d0bebc7eefc2 6718676: putback for 6604014 is incomplete
never
parents: 1
diff changeset
   122
    ptr = UTF8::next(ptr, &value);
d0bebc7eefc2 6718676: putback for 6604014 is incomplete
never
parents: 1
diff changeset
   123
    if (value >= 32 && value < 127 || value == '\'' || value == '\\') {
d0bebc7eefc2 6718676: putback for 6604014 is incomplete
never
parents: 1
diff changeset
   124
      st->put(value);
d0bebc7eefc2 6718676: putback for 6604014 is incomplete
never
parents: 1
diff changeset
   125
    } else {
d0bebc7eefc2 6718676: putback for 6604014 is incomplete
never
parents: 1
diff changeset
   126
      st->print("\\u%04x", value);
d0bebc7eefc2 6718676: putback for 6604014 is incomplete
never
parents: 1
diff changeset
   127
    }
d0bebc7eefc2 6718676: putback for 6604014 is incomplete
never
parents: 1
diff changeset
   128
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
jchar* symbolOopDesc::as_unicode(int& length) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  symbolOopDesc* this_ptr = (symbolOopDesc*)this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  length = UTF8::unicode_length((char*)this_ptr->bytes(), utf8_length());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
  jchar* result = NEW_RESOURCE_ARRAY(jchar, length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  if (length > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
    UTF8::convert_to_unicode((char*)this_ptr->bytes(), result, length);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  return result;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
const char* symbolOopDesc::as_klass_external_name(char* buf, int size) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  if (size > 0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
    char* str    = as_C_string(buf, size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
    int   length = (int)strlen(str);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
    // Turn all '/'s into '.'s (also for array klasses)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
    for (int index = 0; index < length; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
      if (str[index] == '/') {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
        str[index] = '.';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    return str;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
    return buf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
const char* symbolOopDesc::as_klass_external_name() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  char* str    = as_C_string();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  int   length = (int)strlen(str);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  // Turn all '/'s into '.'s (also for array klasses)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  for (int index = 0; index < length; index++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
    if (str[index] == '/') {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
      str[index] = '.';
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
  return str;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
}