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