Tue, 24 Jul 2012 10:51:00 -0700
7023639: JSR 292 method handle invocation needs a fast path for compiled code
6984705: JSR 292 method handle creation should not go through JNI
Summary: remove assembly code for JDK 7 chained method handles
Reviewed-by: jrose, twisti, kvn, mhaupt
Contributed-by: John Rose <john.r.rose@oracle.com>, Christian Thalinger <christian.thalinger@oracle.com>, Michael Haupt <michael.haupt@oracle.com>
1 /*
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
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 */
26 #include "precompiled.hpp"
27 #include "oops/oop.inline.hpp"
28 #include "oops/symbol.hpp"
29 #include "runtime/os.hpp"
30 #include "memory/allocation.inline.hpp"
32 Symbol::Symbol(const u1* name, int length, int refcount) : _refcount(refcount), _length(length) {
33 _identity_hash = os::random();
34 for (int i = 0; i < _length; i++) {
35 byte_at_put(i, name[i]);
36 }
37 }
39 void* Symbol::operator new(size_t sz, int len, TRAPS) {
40 int alloc_size = object_size(len)*HeapWordSize;
41 address res = (address) AllocateHeap(alloc_size, mtSymbol);
42 DEBUG_ONLY(set_allocation_type(res, ResourceObj::C_HEAP);)
43 return res;
44 }
46 void* Symbol::operator new(size_t sz, int len, Arena* arena, TRAPS) {
47 int alloc_size = object_size(len)*HeapWordSize;
48 address res = (address)arena->Amalloc(alloc_size);
49 DEBUG_ONLY(set_allocation_type(res, ResourceObj::ARENA);)
50 return res;
51 }
53 // ------------------------------------------------------------------
54 // Symbol::equals
55 //
56 // Compares the symbol with a string of the given length.
57 bool Symbol::equals(const char* str, int len) const {
58 int l = utf8_length();
59 if (l != len) return false;
60 while (l-- > 0) {
61 if (str[l] != (char) byte_at(l))
62 return false;
63 }
64 assert(l == -1, "we should be at the beginning");
65 return true;
66 }
69 // ------------------------------------------------------------------
70 // Symbol::starts_with
71 //
72 // Tests if the symbol starts with the specified prefix of the given
73 // length.
74 bool Symbol::starts_with(const char* prefix, int len) const {
75 if (len > utf8_length()) return false;
76 while (len-- > 0) {
77 if (prefix[len] != (char) byte_at(len))
78 return false;
79 }
80 assert(len == -1, "we should be at the beginning");
81 return true;
82 }
85 // ------------------------------------------------------------------
86 // Symbol::index_of
87 //
88 // Finds if the given string is a substring of this symbol's utf8 bytes.
89 // Return -1 on failure. Otherwise return the first index where str occurs.
90 int Symbol::index_of_at(int i, const char* str, int len) const {
91 assert(i >= 0 && i <= utf8_length(), "oob");
92 if (len <= 0) return 0;
93 char first_char = str[0];
94 address bytes = (address) ((Symbol*)this)->base();
95 address limit = bytes + utf8_length() - len; // inclusive limit
96 address scan = bytes + i;
97 if (scan > limit)
98 return -1;
99 for (; scan <= limit; scan++) {
100 scan = (address) memchr(scan, first_char, (limit + 1 - scan));
101 if (scan == NULL)
102 return -1; // not found
103 assert(scan >= bytes+i && scan <= limit, "scan oob");
104 if (memcmp(scan, str, len) == 0)
105 return (int)(scan - bytes);
106 }
107 return -1;
108 }
111 char* Symbol::as_C_string(char* buf, int size) const {
112 if (size > 0) {
113 int len = MIN2(size - 1, utf8_length());
114 for (int i = 0; i < len; i++) {
115 buf[i] = byte_at(i);
116 }
117 buf[len] = '\0';
118 }
119 return buf;
120 }
122 char* Symbol::as_C_string() const {
123 int len = utf8_length();
124 char* str = NEW_RESOURCE_ARRAY(char, len + 1);
125 return as_C_string(str, len + 1);
126 }
128 char* Symbol::as_C_string_flexible_buffer(Thread* t,
129 char* buf, int size) const {
130 char* str;
131 int len = utf8_length();
132 int buf_len = len + 1;
133 if (size < buf_len) {
134 str = NEW_RESOURCE_ARRAY(char, buf_len);
135 } else {
136 str = buf;
137 }
138 return as_C_string(str, buf_len);
139 }
141 void Symbol::print_symbol_on(outputStream* st) const {
142 st = st ? st : tty;
143 int length = UTF8::unicode_length((const char*)bytes(), utf8_length());
144 const char *ptr = (const char *)bytes();
145 jchar value;
146 for (int index = 0; index < length; index++) {
147 ptr = UTF8::next(ptr, &value);
148 if (value >= 32 && value < 127 || value == '\'' || value == '\\') {
149 st->put(value);
150 } else {
151 st->print("\\u%04x", value);
152 }
153 }
154 }
156 jchar* Symbol::as_unicode(int& length) const {
157 Symbol* this_ptr = (Symbol*)this;
158 length = UTF8::unicode_length((char*)this_ptr->bytes(), utf8_length());
159 jchar* result = NEW_RESOURCE_ARRAY(jchar, length);
160 if (length > 0) {
161 UTF8::convert_to_unicode((char*)this_ptr->bytes(), result, length);
162 }
163 return result;
164 }
166 const char* Symbol::as_klass_external_name(char* buf, int size) const {
167 if (size > 0) {
168 char* str = as_C_string(buf, size);
169 int length = (int)strlen(str);
170 // Turn all '/'s into '.'s (also for array klasses)
171 for (int index = 0; index < length; index++) {
172 if (str[index] == '/') {
173 str[index] = '.';
174 }
175 }
176 return str;
177 } else {
178 return buf;
179 }
180 }
182 const char* Symbol::as_klass_external_name() const {
183 char* str = as_C_string();
184 int length = (int)strlen(str);
185 // Turn all '/'s into '.'s (also for array klasses)
186 for (int index = 0; index < length; index++) {
187 if (str[index] == '/') {
188 str[index] = '.';
189 }
190 }
191 return str;
192 }
195 void Symbol::print_on(outputStream* st) const {
196 if (this == NULL) {
197 st->print_cr("NULL");
198 } else {
199 st->print("Symbol: '");
200 print_symbol_on(st);
201 st->print("'");
202 st->print(" count %d", refcount());
203 }
204 }
206 // The print_value functions are present in all builds, to support the
207 // disassembler and error reporting.
208 void Symbol::print_value_on(outputStream* st) const {
209 if (this == NULL) {
210 st->print("NULL");
211 } else {
212 st->print("'");
213 for (int i = 0; i < utf8_length(); i++) {
214 st->print("%c", byte_at(i));
215 }
216 st->print("'");
217 }
218 }
220 // SymbolTable prints this in its statistics
221 NOT_PRODUCT(int Symbol::_total_count = 0;)