1 /* 2 * Copyright (c) 2012, 2017, 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 */ 24 25 #include "precompiled.hpp" 26 27 #include "classfile/bytecodeAssembler.hpp" 28 #include "interpreter/bytecodes.hpp" 29 #include "memory/oopFactory.hpp" 30 #include "oops/constantPool.hpp" 31 #include "runtime/handles.inline.hpp" 32 #include "utilities/bytes.hpp" 33 34 u2 BytecodeConstantPool::find_or_add(BytecodeCPEntry const& bcpe) { 35 u2 index; 36 u2* probe = _indices.get(bcpe); 37 if (probe == NULL) { 38 index = _entries.length(); 39 _entries.append(bcpe); 40 _indices.put(bcpe, index); 41 } else { 42 index = *probe; 43 } 44 return index + _orig->length(); 45 } 46 47 ConstantPool* BytecodeConstantPool::create_constant_pool(TRAPS) const { 48 if (_entries.length() == 0) { 49 return _orig; 50 } 51 52 ConstantPool* cp = ConstantPool::allocate( 53 _orig->pool_holder()->class_loader_data(), 54 _orig->length() + _entries.length(), CHECK_NULL); 55 56 cp->set_pool_holder(_orig->pool_holder()); 57 _orig->copy_cp_to(1, _orig->length() - 1, cp, 1, CHECK_NULL); 58 59 // Preserve dynamic constant information from the original pool 60 if (_orig->has_dynamic_constant()) { 61 cp->set_has_dynamic_constant(); 62 } 63 64 for (int i = 0; i < _entries.length(); ++i) { 65 BytecodeCPEntry entry = _entries.at(i); 66 int idx = i + _orig->length(); 67 switch (entry._tag) { 68 case BytecodeCPEntry::UTF8: 69 entry._u.utf8->increment_refcount(); 70 cp->symbol_at_put(idx, entry._u.utf8); 71 break; 72 case BytecodeCPEntry::KLASS: 73 cp->klass_index_at_put( 74 idx, entry._u.klass); 75 break; 76 case BytecodeCPEntry::STRING: 77 cp->unresolved_string_at_put( 78 idx, cp->symbol_at(entry._u.string)); 79 break; 80 case BytecodeCPEntry::NAME_AND_TYPE: 81 cp->name_and_type_at_put(idx, 82 entry._u.name_and_type.name_index, 83 entry._u.name_and_type.type_index); 84 break; 85 case BytecodeCPEntry::METHODREF: 86 cp->method_at_put(idx, 87 entry._u.methodref.class_index, 88 entry._u.methodref.name_and_type_index); 89 break; 90 default: 91 ShouldNotReachHere(); 92 } 93 } 94 95 cp->initialize_unresolved_klasses(_orig->pool_holder()->class_loader_data(), 96 CHECK_NULL); 97 return cp; 98 } 99 100 void BytecodeAssembler::append(u1 imm_u1) { 101 _code->append(imm_u1); 102 } 103 104 void BytecodeAssembler::append(u2 imm_u2) { 105 _code->append(0); 106 _code->append(0); 107 Bytes::put_Java_u2(_code->adr_at(_code->length() - 2), imm_u2); 108 } 109 110 void BytecodeAssembler::append(u4 imm_u4) { 111 _code->append(0); 112 _code->append(0); 113 _code->append(0); 114 _code->append(0); 115 Bytes::put_Java_u4(_code->adr_at(_code->length() - 4), imm_u4); 116 } 117 118 void BytecodeAssembler::xload(u4 index, u1 onebyteop, u1 twobyteop) { 119 if (index < 4) { 120 _code->append(onebyteop + index); 121 } else { 122 _code->append(twobyteop); 123 _code->append((u2)index); 124 } 125 } 126 127 void BytecodeAssembler::dup() { 128 _code->append(Bytecodes::_dup); 129 } 130 131 void BytecodeAssembler::_new(Symbol* sym) { 132 u2 cpool_index = _cp->klass(sym); 133 _code->append(Bytecodes::_new); 134 append(cpool_index); 135 } 136 137 void BytecodeAssembler::load_string(Symbol* sym) { 138 u2 cpool_index = _cp->string(sym); 139 if (cpool_index < 0x100) { 140 ldc(cpool_index); 141 } else { 142 ldc_w(cpool_index); 143 } 144 } 145 146 void BytecodeAssembler::ldc(u1 index) { 147 _code->append(Bytecodes::_ldc); 148 append(index); 149 } 150 151 void BytecodeAssembler::ldc_w(u2 index) { 152 _code->append(Bytecodes::_ldc_w); 153 append(index); 154 } 155 156 void BytecodeAssembler::athrow() { 157 _code->append(Bytecodes::_athrow); 158 } 159 160 void BytecodeAssembler::iload(u4 index) { 161 xload(index, Bytecodes::_iload_0, Bytecodes::_iload); 162 } 163 164 void BytecodeAssembler::lload(u4 index) { 165 xload(index, Bytecodes::_lload_0, Bytecodes::_lload); 166 } 167 168 void BytecodeAssembler::fload(u4 index) { 169 xload(index, Bytecodes::_fload_0, Bytecodes::_fload); 170 } 171 172 void BytecodeAssembler::dload(u4 index) { 173 xload(index, Bytecodes::_dload_0, Bytecodes::_dload); 174 } 175 176 void BytecodeAssembler::aload(u4 index) { 177 xload(index, Bytecodes::_aload_0, Bytecodes::_aload); 178 } 179 180 void BytecodeAssembler::load(BasicType bt, u4 index) { 181 switch (bt) { 182 case T_BOOLEAN: 183 case T_CHAR: 184 case T_BYTE: 185 case T_SHORT: 186 case T_INT: iload(index); break; 187 case T_FLOAT: fload(index); break; 188 case T_DOUBLE: dload(index); break; 189 case T_LONG: lload(index); break; 190 case T_OBJECT: 191 case T_ARRAY: aload(index); break; 192 default: 193 ShouldNotReachHere(); 194 } 195 } 196 197 void BytecodeAssembler::checkcast(Symbol* sym) { 198 u2 cpool_index = _cp->klass(sym); 199 _code->append(Bytecodes::_checkcast); 200 append(cpool_index); 201 } 202 203 void BytecodeAssembler::invokespecial(Method* method) { 204 invokespecial(method->klass_name(), method->name(), method->signature()); 205 } 206 207 void BytecodeAssembler::invokespecial(Symbol* klss, Symbol* name, Symbol* sig) { 208 u2 methodref_index = _cp->methodref(klss, name, sig); 209 _code->append(Bytecodes::_invokespecial); 210 append(methodref_index); 211 } 212 213 void BytecodeAssembler::invokevirtual(Method* method) { 214 invokevirtual(method->klass_name(), method->name(), method->signature()); 215 } 216 217 void BytecodeAssembler::invokevirtual(Symbol* klss, Symbol* name, Symbol* sig) { 218 u2 methodref_index = _cp->methodref(klss, name, sig); 219 _code->append(Bytecodes::_invokevirtual); 220 append(methodref_index); 221 } 222 223 void BytecodeAssembler::ireturn() { 224 _code->append(Bytecodes::_ireturn); 225 } 226 227 void BytecodeAssembler::lreturn() { 228 _code->append(Bytecodes::_lreturn); 229 } 230 231 void BytecodeAssembler::freturn() { 232 _code->append(Bytecodes::_freturn); 233 } 234 235 void BytecodeAssembler::dreturn() { 236 _code->append(Bytecodes::_dreturn); 237 } 238 239 void BytecodeAssembler::areturn() { 240 _code->append(Bytecodes::_areturn); 241 } 242 243 void BytecodeAssembler::_return() { 244 _code->append(Bytecodes::_return); 245 } 246 247 void BytecodeAssembler::_return(BasicType bt) { 248 switch (bt) { 249 case T_BOOLEAN: 250 case T_CHAR: 251 case T_BYTE: 252 case T_SHORT: 253 case T_INT: ireturn(); break; 254 case T_FLOAT: freturn(); break; 255 case T_DOUBLE: dreturn(); break; 256 case T_LONG: lreturn(); break; 257 case T_OBJECT: 258 case T_ARRAY: areturn(); break; 259 case T_VOID: _return(); break; 260 default: 261 ShouldNotReachHere(); 262 } 263 }