Wed, 03 Jul 2019 20:42:37 +0800
Merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@1 | 25 | /* |
aoqi@1 | 26 | * This file has been modified by Loongson Technology in 2015. These |
aoqi@1 | 27 | * modifications are Copyright (c) 2015 Loongson Technology, and are made |
aoqi@1 | 28 | * available on the same license terms set forth above. |
aoqi@1 | 29 | */ |
aoqi@1 | 30 | |
aoqi@0 | 31 | #include "precompiled.hpp" |
aoqi@0 | 32 | |
aoqi@0 | 33 | #include "classfile/bytecodeAssembler.hpp" |
aoqi@0 | 34 | #include "interpreter/bytecodes.hpp" |
aoqi@0 | 35 | #include "memory/oopFactory.hpp" |
aoqi@0 | 36 | #include "oops/constantPool.hpp" |
aoqi@0 | 37 | |
aoqi@0 | 38 | #ifdef TARGET_ARCH_x86 |
aoqi@0 | 39 | # include "bytes_x86.hpp" |
aoqi@0 | 40 | #endif |
aoqi@1 | 41 | #ifdef TARGET_ARCH_mips |
aoqi@1 | 42 | # include "bytes_mips.hpp" |
aoqi@1 | 43 | #endif |
aoqi@0 | 44 | #ifdef TARGET_ARCH_sparc |
aoqi@0 | 45 | # include "bytes_sparc.hpp" |
aoqi@0 | 46 | #endif |
aoqi@0 | 47 | #ifdef TARGET_ARCH_zero |
aoqi@0 | 48 | # include "bytes_zero.hpp" |
aoqi@0 | 49 | #endif |
aoqi@0 | 50 | #ifdef TARGET_ARCH_arm |
aoqi@0 | 51 | # include "bytes_arm.hpp" |
aoqi@0 | 52 | #endif |
aoqi@0 | 53 | #ifdef TARGET_ARCH_ppc |
aoqi@0 | 54 | # include "bytes_ppc.hpp" |
aoqi@0 | 55 | #endif |
aoqi@0 | 56 | |
aoqi@0 | 57 | u2 BytecodeConstantPool::find_or_add(BytecodeCPEntry const& bcpe) { |
aoqi@0 | 58 | u2 index; |
aoqi@0 | 59 | u2* probe = _indices.get(bcpe); |
aoqi@0 | 60 | if (probe == NULL) { |
aoqi@0 | 61 | index = _entries.length(); |
aoqi@0 | 62 | _entries.append(bcpe); |
aoqi@0 | 63 | _indices.put(bcpe, index); |
aoqi@0 | 64 | } else { |
aoqi@0 | 65 | index = *probe; |
aoqi@0 | 66 | } |
aoqi@0 | 67 | return index + _orig->length(); |
aoqi@0 | 68 | } |
aoqi@0 | 69 | |
aoqi@0 | 70 | ConstantPool* BytecodeConstantPool::create_constant_pool(TRAPS) const { |
aoqi@0 | 71 | if (_entries.length() == 0) { |
aoqi@0 | 72 | return _orig; |
aoqi@0 | 73 | } |
aoqi@0 | 74 | |
aoqi@0 | 75 | ConstantPool* cp = ConstantPool::allocate( |
aoqi@0 | 76 | _orig->pool_holder()->class_loader_data(), |
aoqi@0 | 77 | _orig->length() + _entries.length(), CHECK_NULL); |
aoqi@0 | 78 | |
aoqi@0 | 79 | cp->set_pool_holder(_orig->pool_holder()); |
aoqi@0 | 80 | _orig->copy_cp_to(1, _orig->length() - 1, cp, 1, CHECK_NULL); |
aoqi@0 | 81 | |
aoqi@0 | 82 | for (int i = 0; i < _entries.length(); ++i) { |
aoqi@0 | 83 | BytecodeCPEntry entry = _entries.at(i); |
aoqi@0 | 84 | int idx = i + _orig->length(); |
aoqi@0 | 85 | switch (entry._tag) { |
aoqi@0 | 86 | case BytecodeCPEntry::UTF8: |
aoqi@0 | 87 | entry._u.utf8->increment_refcount(); |
aoqi@0 | 88 | cp->symbol_at_put(idx, entry._u.utf8); |
aoqi@0 | 89 | break; |
aoqi@0 | 90 | case BytecodeCPEntry::KLASS: |
aoqi@0 | 91 | cp->unresolved_klass_at_put( |
aoqi@0 | 92 | idx, cp->symbol_at(entry._u.klass)); |
aoqi@0 | 93 | break; |
aoqi@0 | 94 | case BytecodeCPEntry::STRING: |
aoqi@0 | 95 | cp->unresolved_string_at_put( |
aoqi@0 | 96 | idx, cp->symbol_at(entry._u.string)); |
aoqi@0 | 97 | break; |
aoqi@0 | 98 | case BytecodeCPEntry::NAME_AND_TYPE: |
aoqi@0 | 99 | cp->name_and_type_at_put(idx, |
aoqi@0 | 100 | entry._u.name_and_type.name_index, |
aoqi@0 | 101 | entry._u.name_and_type.type_index); |
aoqi@0 | 102 | break; |
aoqi@0 | 103 | case BytecodeCPEntry::METHODREF: |
aoqi@0 | 104 | cp->method_at_put(idx, |
aoqi@0 | 105 | entry._u.methodref.class_index, |
aoqi@0 | 106 | entry._u.methodref.name_and_type_index); |
aoqi@0 | 107 | break; |
aoqi@0 | 108 | default: |
aoqi@0 | 109 | ShouldNotReachHere(); |
aoqi@0 | 110 | } |
aoqi@0 | 111 | } |
aoqi@0 | 112 | return cp; |
aoqi@0 | 113 | } |
aoqi@0 | 114 | |
aoqi@0 | 115 | void BytecodeAssembler::append(u1 imm_u1) { |
aoqi@0 | 116 | _code->append(imm_u1); |
aoqi@0 | 117 | } |
aoqi@0 | 118 | |
aoqi@0 | 119 | void BytecodeAssembler::append(u2 imm_u2) { |
aoqi@0 | 120 | _code->append(0); |
aoqi@0 | 121 | _code->append(0); |
aoqi@0 | 122 | Bytes::put_Java_u2(_code->adr_at(_code->length() - 2), imm_u2); |
aoqi@0 | 123 | } |
aoqi@0 | 124 | |
aoqi@0 | 125 | void BytecodeAssembler::append(u4 imm_u4) { |
aoqi@0 | 126 | _code->append(0); |
aoqi@0 | 127 | _code->append(0); |
aoqi@0 | 128 | _code->append(0); |
aoqi@0 | 129 | _code->append(0); |
aoqi@0 | 130 | Bytes::put_Java_u4(_code->adr_at(_code->length() - 4), imm_u4); |
aoqi@0 | 131 | } |
aoqi@0 | 132 | |
aoqi@0 | 133 | void BytecodeAssembler::xload(u4 index, u1 onebyteop, u1 twobyteop) { |
aoqi@0 | 134 | if (index < 4) { |
aoqi@0 | 135 | _code->append(onebyteop + index); |
aoqi@0 | 136 | } else { |
aoqi@0 | 137 | _code->append(twobyteop); |
aoqi@0 | 138 | _code->append((u2)index); |
aoqi@0 | 139 | } |
aoqi@0 | 140 | } |
aoqi@0 | 141 | |
aoqi@0 | 142 | void BytecodeAssembler::dup() { |
aoqi@0 | 143 | _code->append(Bytecodes::_dup); |
aoqi@0 | 144 | } |
aoqi@0 | 145 | |
aoqi@0 | 146 | void BytecodeAssembler::_new(Symbol* sym) { |
aoqi@0 | 147 | u2 cpool_index = _cp->klass(sym); |
aoqi@0 | 148 | _code->append(Bytecodes::_new); |
aoqi@0 | 149 | append(cpool_index); |
aoqi@0 | 150 | } |
aoqi@0 | 151 | |
aoqi@0 | 152 | void BytecodeAssembler::load_string(Symbol* sym) { |
aoqi@0 | 153 | u2 cpool_index = _cp->string(sym); |
aoqi@0 | 154 | if (cpool_index < 0x100) { |
aoqi@0 | 155 | ldc(cpool_index); |
aoqi@0 | 156 | } else { |
aoqi@0 | 157 | ldc_w(cpool_index); |
aoqi@0 | 158 | } |
aoqi@0 | 159 | } |
aoqi@0 | 160 | |
aoqi@0 | 161 | void BytecodeAssembler::ldc(u1 index) { |
aoqi@0 | 162 | _code->append(Bytecodes::_ldc); |
aoqi@0 | 163 | append(index); |
aoqi@0 | 164 | } |
aoqi@0 | 165 | |
aoqi@0 | 166 | void BytecodeAssembler::ldc_w(u2 index) { |
aoqi@0 | 167 | _code->append(Bytecodes::_ldc_w); |
aoqi@0 | 168 | append(index); |
aoqi@0 | 169 | } |
aoqi@0 | 170 | |
aoqi@0 | 171 | void BytecodeAssembler::athrow() { |
aoqi@0 | 172 | _code->append(Bytecodes::_athrow); |
aoqi@0 | 173 | } |
aoqi@0 | 174 | |
aoqi@0 | 175 | void BytecodeAssembler::iload(u4 index) { |
aoqi@0 | 176 | xload(index, Bytecodes::_iload_0, Bytecodes::_iload); |
aoqi@0 | 177 | } |
aoqi@0 | 178 | |
aoqi@0 | 179 | void BytecodeAssembler::lload(u4 index) { |
aoqi@0 | 180 | xload(index, Bytecodes::_lload_0, Bytecodes::_lload); |
aoqi@0 | 181 | } |
aoqi@0 | 182 | |
aoqi@0 | 183 | void BytecodeAssembler::fload(u4 index) { |
aoqi@0 | 184 | xload(index, Bytecodes::_fload_0, Bytecodes::_fload); |
aoqi@0 | 185 | } |
aoqi@0 | 186 | |
aoqi@0 | 187 | void BytecodeAssembler::dload(u4 index) { |
aoqi@0 | 188 | xload(index, Bytecodes::_dload_0, Bytecodes::_dload); |
aoqi@0 | 189 | } |
aoqi@0 | 190 | |
aoqi@0 | 191 | void BytecodeAssembler::aload(u4 index) { |
aoqi@0 | 192 | xload(index, Bytecodes::_aload_0, Bytecodes::_aload); |
aoqi@0 | 193 | } |
aoqi@0 | 194 | |
aoqi@0 | 195 | void BytecodeAssembler::load(BasicType bt, u4 index) { |
aoqi@0 | 196 | switch (bt) { |
aoqi@0 | 197 | case T_BOOLEAN: |
aoqi@0 | 198 | case T_CHAR: |
aoqi@0 | 199 | case T_BYTE: |
aoqi@0 | 200 | case T_SHORT: |
aoqi@0 | 201 | case T_INT: iload(index); break; |
aoqi@0 | 202 | case T_FLOAT: fload(index); break; |
aoqi@0 | 203 | case T_DOUBLE: dload(index); break; |
aoqi@0 | 204 | case T_LONG: lload(index); break; |
aoqi@0 | 205 | case T_OBJECT: |
aoqi@0 | 206 | case T_ARRAY: aload(index); break; |
aoqi@0 | 207 | default: |
aoqi@0 | 208 | ShouldNotReachHere(); |
aoqi@0 | 209 | } |
aoqi@0 | 210 | } |
aoqi@0 | 211 | |
aoqi@0 | 212 | void BytecodeAssembler::checkcast(Symbol* sym) { |
aoqi@0 | 213 | u2 cpool_index = _cp->klass(sym); |
aoqi@0 | 214 | _code->append(Bytecodes::_checkcast); |
aoqi@0 | 215 | append(cpool_index); |
aoqi@0 | 216 | } |
aoqi@0 | 217 | |
aoqi@0 | 218 | void BytecodeAssembler::invokespecial(Method* method) { |
aoqi@0 | 219 | invokespecial(method->klass_name(), method->name(), method->signature()); |
aoqi@0 | 220 | } |
aoqi@0 | 221 | |
aoqi@0 | 222 | void BytecodeAssembler::invokespecial(Symbol* klss, Symbol* name, Symbol* sig) { |
aoqi@0 | 223 | u2 methodref_index = _cp->methodref(klss, name, sig); |
aoqi@0 | 224 | _code->append(Bytecodes::_invokespecial); |
aoqi@0 | 225 | append(methodref_index); |
aoqi@0 | 226 | } |
aoqi@0 | 227 | |
aoqi@0 | 228 | void BytecodeAssembler::invokevirtual(Method* method) { |
aoqi@0 | 229 | invokevirtual(method->klass_name(), method->name(), method->signature()); |
aoqi@0 | 230 | } |
aoqi@0 | 231 | |
aoqi@0 | 232 | void BytecodeAssembler::invokevirtual(Symbol* klss, Symbol* name, Symbol* sig) { |
aoqi@0 | 233 | u2 methodref_index = _cp->methodref(klss, name, sig); |
aoqi@0 | 234 | _code->append(Bytecodes::_invokevirtual); |
aoqi@0 | 235 | append(methodref_index); |
aoqi@0 | 236 | } |
aoqi@0 | 237 | |
aoqi@0 | 238 | void BytecodeAssembler::ireturn() { |
aoqi@0 | 239 | _code->append(Bytecodes::_ireturn); |
aoqi@0 | 240 | } |
aoqi@0 | 241 | |
aoqi@0 | 242 | void BytecodeAssembler::lreturn() { |
aoqi@0 | 243 | _code->append(Bytecodes::_lreturn); |
aoqi@0 | 244 | } |
aoqi@0 | 245 | |
aoqi@0 | 246 | void BytecodeAssembler::freturn() { |
aoqi@0 | 247 | _code->append(Bytecodes::_freturn); |
aoqi@0 | 248 | } |
aoqi@0 | 249 | |
aoqi@0 | 250 | void BytecodeAssembler::dreturn() { |
aoqi@0 | 251 | _code->append(Bytecodes::_dreturn); |
aoqi@0 | 252 | } |
aoqi@0 | 253 | |
aoqi@0 | 254 | void BytecodeAssembler::areturn() { |
aoqi@0 | 255 | _code->append(Bytecodes::_areturn); |
aoqi@0 | 256 | } |
aoqi@0 | 257 | |
aoqi@0 | 258 | void BytecodeAssembler::_return() { |
aoqi@0 | 259 | _code->append(Bytecodes::_return); |
aoqi@0 | 260 | } |
aoqi@0 | 261 | |
aoqi@0 | 262 | void BytecodeAssembler::_return(BasicType bt) { |
aoqi@0 | 263 | switch (bt) { |
aoqi@0 | 264 | case T_BOOLEAN: |
aoqi@0 | 265 | case T_CHAR: |
aoqi@0 | 266 | case T_BYTE: |
aoqi@0 | 267 | case T_SHORT: |
aoqi@0 | 268 | case T_INT: ireturn(); break; |
aoqi@0 | 269 | case T_FLOAT: freturn(); break; |
aoqi@0 | 270 | case T_DOUBLE: dreturn(); break; |
aoqi@0 | 271 | case T_LONG: lreturn(); break; |
aoqi@0 | 272 | case T_OBJECT: |
aoqi@0 | 273 | case T_ARRAY: areturn(); break; |
aoqi@0 | 274 | case T_VOID: _return(); break; |
aoqi@0 | 275 | default: |
aoqi@0 | 276 | ShouldNotReachHere(); |
aoqi@0 | 277 | } |
aoqi@0 | 278 | } |