Thu, 11 Jul 2013 12:56:29 -0700
Merge
1 /*
2 * Copyright (c) 1998, 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 */
25 #ifndef SHARE_VM_INTERPRETER_REWRITER_HPP
26 #define SHARE_VM_INTERPRETER_REWRITER_HPP
28 #include "memory/allocation.hpp"
29 #include "runtime/handles.inline.hpp"
30 #include "utilities/growableArray.hpp"
32 // The Rewriter adds caches to the constant pool and rewrites bytecode indices
33 // pointing into the constant pool for better interpreter performance.
35 class Rewriter: public StackObj {
36 private:
37 instanceKlassHandle _klass;
38 constantPoolHandle _pool;
39 Array<Method*>* _methods;
40 intArray _cp_map;
41 intStack _cp_cache_map; // for Methodref, Fieldref,
42 // InterfaceMethodref and InvokeDynamic
43 intArray _reference_map; // maps from cp index to resolved_refs index (or -1)
44 intStack _resolved_references_map; // for strings, methodHandle, methodType
45 intStack _invokedynamic_references_map; // for invokedynamic resolved refs
46 intArray _method_handle_invokers;
47 int _resolved_reference_limit;
49 void init_maps(int length) {
50 _cp_map.initialize(length, -1);
51 // Choose an initial value large enough that we don't get frequent
52 // calls to grow().
53 _cp_cache_map.initialize(length / 2);
54 // Also cache resolved objects, in another different cache.
55 _reference_map.initialize(length, -1);
56 _resolved_references_map.initialize(length / 2);
57 _invokedynamic_references_map.initialize(length / 2);
58 _resolved_reference_limit = -1;
59 DEBUG_ONLY(_cp_cache_index_limit = -1);
60 }
62 int _cp_cache_index_limit;
63 void record_map_limits() {
64 #ifdef ASSERT
65 // Record initial size of the two arrays generated for the CP cache:
66 _cp_cache_index_limit = _cp_cache_map.length();
67 #endif //ASSERT
68 _resolved_reference_limit = _resolved_references_map.length();
69 }
71 int cp_entry_to_cp_cache(int i) { assert(has_cp_cache(i), "oob"); return _cp_map[i]; }
72 bool has_cp_cache(int i) { return (uint)i < (uint)_cp_map.length() && _cp_map[i] >= 0; }
74 int add_cp_cache_entry(int cp_index) {
75 assert(_pool->tag_at(cp_index).value() != JVM_CONSTANT_InvokeDynamic, "use indy version");
76 assert(_cp_map[cp_index] == -1, "not twice on same cp_index");
77 assert(_cp_cache_index_limit == -1, "do not add cache entries after first iteration");
78 int cache_index = _cp_cache_map.append(cp_index);
79 _cp_map.at_put(cp_index, cache_index);
80 assert(cp_entry_to_cp_cache(cp_index) == cache_index, "");
81 assert(cp_cache_entry_pool_index(cache_index) == cp_index, "");
82 return cache_index;
83 }
85 // add a new CP cache entry beyond the normal cache (for invokedynamic only)
86 int add_invokedynamic_cp_cache_entry(int cp_index) {
87 assert(_pool->tag_at(cp_index).value() == JVM_CONSTANT_InvokeDynamic, "use non-indy version");
88 assert(_cp_map[cp_index] == -1, "do not map from cp_index");
89 assert(_cp_cache_index_limit >= 0, "add indy cache entries after first iteration");
90 int cache_index = _cp_cache_map.append(cp_index);
91 assert(cache_index >= _cp_cache_index_limit, "");
92 // do not update _cp_map, since the mapping is one-to-many
93 assert(cp_cache_entry_pool_index(cache_index) == cp_index, "");
94 return cache_index;
95 }
97 // fix duplicated code later
98 int cp_entry_to_resolved_references(int cp_index) const {
99 assert(has_entry_in_resolved_references(cp_index), "oob");
100 return _reference_map[cp_index];
101 }
102 bool has_entry_in_resolved_references(int cp_index) const {
103 return (uint)cp_index < (uint)_reference_map.length() && _reference_map[cp_index] >= 0;
104 }
106 // add a new entry to the resolved_references map
107 int add_resolved_references_entry(int cp_index) {
108 assert(_reference_map[cp_index] == -1, "not twice on same cp_index");
109 assert(_resolved_reference_limit == -1, "do not add CP refs after first iteration");
110 int ref_index = _resolved_references_map.append(cp_index);
111 _reference_map.at_put(cp_index, ref_index);
112 assert(cp_entry_to_resolved_references(cp_index) == ref_index, "");
113 return ref_index;
114 }
116 // add a new entries to the resolved_references map (for invokedynamic and invokehandle only)
117 int add_invokedynamic_resolved_references_entries(int cp_index, int cache_index) {
118 assert(_resolved_reference_limit >= 0, "must add indy refs after first iteration");
119 int ref_index = -1;
120 for (int entry = 0; entry < ConstantPoolCacheEntry::_indy_resolved_references_entries; entry++) {
121 const int index = _resolved_references_map.append(cp_index); // many-to-one
122 assert(index >= _resolved_reference_limit, "");
123 if (entry == 0) {
124 ref_index = index;
125 }
126 assert((index - entry) == ref_index, "entries must be consecutive");
127 _invokedynamic_references_map.at_put_grow(index, cache_index, -1);
128 }
129 return ref_index;
130 }
132 int resolved_references_entry_to_pool_index(int ref_index) {
133 int cp_index = _resolved_references_map[ref_index];
134 return cp_index;
135 }
137 // Access the contents of _cp_cache_map to determine CP cache layout.
138 int cp_cache_entry_pool_index(int cache_index) {
139 int cp_index = _cp_cache_map[cache_index];
140 return cp_index;
141 }
143 // All the work goes in here:
144 Rewriter(instanceKlassHandle klass, constantPoolHandle cpool, Array<Method*>* methods, TRAPS);
146 void compute_index_maps();
147 void make_constant_pool_cache(TRAPS);
148 void scan_method(Method* m, bool reverse = false);
149 void rewrite_Object_init(methodHandle m, TRAPS);
150 void rewrite_member_reference(address bcp, int offset, bool reverse = false);
151 void maybe_rewrite_invokehandle(address opc, int cp_index, int cache_index, bool reverse = false);
152 void rewrite_invokedynamic(address bcp, int offset, bool reverse = false);
153 void maybe_rewrite_ldc(address bcp, int offset, bool is_wide, bool reverse = false);
154 // Revert bytecodes in case of an exception.
155 void restore_bytecodes();
157 static methodHandle rewrite_jsrs(methodHandle m, TRAPS);
158 public:
159 // Driver routine:
160 static void rewrite(instanceKlassHandle klass, TRAPS);
161 };
163 #endif // SHARE_VM_INTERPRETER_REWRITER_HPP