assembler.cpp revision 6646:b596a1063e90
1179237Sjb/*
2179237Sjb * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
3179237Sjb * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4179237Sjb *
5179237Sjb * This code is free software; you can redistribute it and/or modify it
6179237Sjb * under the terms of the GNU General Public License version 2 only, as
7179237Sjb * published by the Free Software Foundation.
8179237Sjb *
9179237Sjb * This code is distributed in the hope that it will be useful, but WITHOUT
10179237Sjb * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11179237Sjb * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12179237Sjb * version 2 for more details (a copy is included in the LICENSE file that
13179237Sjb * accompanied this code).
14179237Sjb *
15179237Sjb * You should have received a copy of the GNU General Public License version
16179237Sjb * 2 along with this work; if not, write to the Free Software Foundation,
17179237Sjb * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18179237Sjb *
19179237Sjb * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20179237Sjb * or visit www.oracle.com if you need additional information or have any
21179237Sjb * questions.
22179237Sjb *
23179237Sjb */
24179237Sjb
25179237Sjb#include "precompiled.hpp"
26179237Sjb#include "asm/macroAssembler.hpp"
27179237Sjb#include "asm/macroAssembler.inline.hpp"
28179237Sjb#include "asm/codeBuffer.hpp"
29179237Sjb#include "runtime/atomic.inline.hpp"
30236567Sgnn#include "runtime/icache.hpp"
31236567Sgnn#include "runtime/os.hpp"
32236567Sgnn
33236567Sgnn
34179237Sjb// Implementation of AbstractAssembler
35179237Sjb//
36179237Sjb// The AbstractAssembler is generating code into a CodeBuffer. To make code generation faster,
37222813Sattilio// the assembler keeps a copy of the code buffers boundaries & modifies them when
38179237Sjb// emitting bytes rather than using the code buffers accessor functions all the time.
39179237Sjb// The code buffer is updated via set_code_end(...) after emitting a whole instruction.
40179237Sjb
41179237SjbAbstractAssembler::AbstractAssembler(CodeBuffer* code) {
42179237Sjb  if (code == NULL)  return;
43179237Sjb  CodeSection* cs = code->insts();
44179237Sjb  cs->clear_mark();   // new assembler kills old mark
45179237Sjb  if (cs->start() == NULL)  {
46179237Sjb    vm_exit_out_of_memory(0, OOM_MMAP_ERROR, err_msg("CodeCache: no room for %s",
47179237Sjb                                     code->name()));
48179237Sjb  }
49179237Sjb  _code_section = cs;
50179237Sjb  _oop_recorder= code->oop_recorder();
51179237Sjb  DEBUG_ONLY( _short_branch_delta = 0; )
52238537Sgnn}
53238537Sgnn
54179237Sjbvoid AbstractAssembler::set_code_section(CodeSection* cs) {
55179237Sjb  assert(cs->outer() == code_section()->outer(), "sanity");
56179237Sjb  assert(cs->is_allocated(), "need to pre-allocate this section");
57179237Sjb  cs->clear_mark();  // new assembly into this section kills old mark
58179237Sjb  _code_section = cs;
59179237Sjb}
60179237Sjb
61179237Sjb// Inform CodeBuffer that incoming code and relocation will be for stubs
62179237Sjbaddress AbstractAssembler::start_a_stub(int required_space) {
63179237Sjb  CodeBuffer*  cb = code();
64179237Sjb  CodeSection* cs = cb->stubs();
65179237Sjb  assert(_code_section == cb->insts(), "not in insts?");
66179237Sjb  if (cs->maybe_expand_to_ensure_remaining(required_space)
67179237Sjb      && cb->blob() == NULL) {
68179237Sjb    return NULL;
69179237Sjb  }
70179237Sjb  set_code_section(cs);
71179237Sjb  return pc();
72179237Sjb}
73179237Sjb
74179237Sjb// Inform CodeBuffer that incoming code and relocation will be code
75179237Sjb// Should not be called if start_a_stub() returned NULL
76179237Sjbvoid AbstractAssembler::end_a_stub() {
77179237Sjb  assert(_code_section == code()->stubs(), "not in stubs?");
78179237Sjb  set_code_section(code()->insts());
79179237Sjb}
80179237Sjb
81179237Sjb// Inform CodeBuffer that incoming code and relocation will be for stubs
82179237Sjbaddress AbstractAssembler::start_a_const(int required_space, int required_align) {
83179237Sjb  CodeBuffer*  cb = code();
84179237Sjb  CodeSection* cs = cb->consts();
85179237Sjb  assert(_code_section == cb->insts() || _code_section == cb->stubs(), "not in insts/stubs?");
86179237Sjb  address end = cs->end();
87179237Sjb  int pad = -(intptr_t)end & (required_align-1);
88179237Sjb  if (cs->maybe_expand_to_ensure_remaining(pad + required_space)) {
89179237Sjb    if (cb->blob() == NULL)  return NULL;
90179237Sjb    end = cs->end();  // refresh pointer
91179237Sjb  }
92179237Sjb  if (pad > 0) {
93179237Sjb    while (--pad >= 0) { *end++ = 0; }
94179237Sjb    cs->set_end(end);
95179237Sjb  }
96179237Sjb  set_code_section(cs);
97179237Sjb  return end;
98179237Sjb}
99179237Sjb
100179237Sjb// Inform CodeBuffer that incoming code and relocation will be code
101179237Sjb// in section cs (insts or stubs).
102179237Sjbvoid AbstractAssembler::end_a_const(CodeSection* cs) {
103179237Sjb  assert(_code_section == code()->consts(), "not in consts?");
104179237Sjb  set_code_section(cs);
105179237Sjb}
106179237Sjb
107179237Sjbvoid AbstractAssembler::flush() {
108179237Sjb  ICache::invalidate_range(addr_at(0), offset());
109179237Sjb}
110179237Sjb
111179237Sjbvoid AbstractAssembler::bind(Label& L) {
112179237Sjb  if (L.is_bound()) {
113179237Sjb    // Assembler can bind a label more than once to the same place.
114179237Sjb    guarantee(L.loc() == locator(), "attempt to redefine label");
115179237Sjb    return;
116179237Sjb  }
117179237Sjb  L.bind_loc(locator());
118179237Sjb  L.patch_instructions((MacroAssembler*)this);
119179237Sjb}
120179237Sjb
121179237Sjbvoid AbstractAssembler::generate_stack_overflow_check(int frame_size_in_bytes) {
122179237Sjb  if (UseStackBanging) {
123222813Sattilio    // Each code entry causes one stack bang n pages down the stack where n
124179237Sjb    // is configurable by StackShadowPages.  The setting depends on the maximum
125179237Sjb    // depth of VM call stack or native before going back into java code,
126179237Sjb    // since only java code can raise a stack overflow exception using the
127179237Sjb    // stack banging mechanism.  The VM and native code does not detect stack
128222813Sattilio    // overflow.
129179237Sjb    // The code in JavaCalls::call() checks that there is at least n pages
130216251Savg    // available, so all entry code needs to do is bang once for the end of
131216251Savg    // this shadow zone.
132179237Sjb    // The entry code may need to bang additional pages if the framesize
133179237Sjb    // is greater than a page.
134179237Sjb
135179237Sjb    const int page_size = os::vm_page_size();
136179237Sjb    int bang_end = StackShadowPages * page_size;
137179237Sjb
138179237Sjb    // This is how far the previous frame's stack banging extended.
139179237Sjb    const int bang_end_safe = bang_end;
140179237Sjb
141179237Sjb    if (frame_size_in_bytes > page_size) {
142179237Sjb      bang_end += frame_size_in_bytes;
143179237Sjb    }
144179237Sjb
145179237Sjb    int bang_offset = bang_end_safe;
146179237Sjb    while (bang_offset <= bang_end) {
147179237Sjb      // Need at least one stack bang at end of shadow zone.
148179237Sjb      bang_stack_with_offset(bang_offset);
149179237Sjb      bang_offset += page_size;
150179237Sjb    }
151179237Sjb  } // end (UseStackBanging)
152179237Sjb}
153179237Sjb
154179237Sjbvoid Label::add_patch_at(CodeBuffer* cb, int branch_loc) {
155179237Sjb  assert(_loc == -1, "Label is unbound");
156179237Sjb  if (_patch_index < PatchCacheSize) {
157179237Sjb    _patches[_patch_index] = branch_loc;
158179237Sjb  } else {
159179237Sjb    if (_patch_overflow == NULL) {
160179237Sjb      _patch_overflow = cb->create_patch_overflow();
161179237Sjb    }
162179237Sjb    _patch_overflow->push(branch_loc);
163179237Sjb  }
164179237Sjb  ++_patch_index;
165179237Sjb}
166179237Sjb
167179237Sjbvoid Label::patch_instructions(MacroAssembler* masm) {
168179237Sjb  assert(is_bound(), "Label is bound");
169179237Sjb  CodeBuffer* cb = masm->code();
170179237Sjb  int target_sect = CodeBuffer::locator_sect(loc());
171179237Sjb  address target = cb->locator_address(loc());
172179237Sjb  while (_patch_index > 0) {
173179237Sjb    --_patch_index;
174179237Sjb    int branch_loc;
175179237Sjb    if (_patch_index >= PatchCacheSize) {
176179237Sjb      branch_loc = _patch_overflow->pop();
177179237Sjb    } else {
178179237Sjb      branch_loc = _patches[_patch_index];
179179237Sjb    }
180179237Sjb    int branch_sect = CodeBuffer::locator_sect(branch_loc);
181179237Sjb    address branch = cb->locator_address(branch_loc);
182179237Sjb    if (branch_sect == CodeBuffer::SECT_CONSTS) {
183179237Sjb      // The thing to patch is a constant word.
184179237Sjb      *(address*)branch = target;
185179237Sjb      continue;
186179237Sjb    }
187179237Sjb
188179237Sjb#ifdef ASSERT
189179237Sjb    // Cross-section branches only work if the
190179237Sjb    // intermediate section boundaries are frozen.
191179237Sjb    if (target_sect != branch_sect) {
192179237Sjb      for (int n = MIN2(target_sect, branch_sect),
193179237Sjb               nlimit = (target_sect + branch_sect) - n;
194179237Sjb           n < nlimit; n++) {
195179237Sjb        CodeSection* cs = cb->code_section(n);
196179237Sjb        assert(cs->is_frozen(), "cross-section branch needs stable offsets");
197179237Sjb      }
198179237Sjb    }
199179237Sjb#endif //ASSERT
200179237Sjb
201179237Sjb    // Push the target offset into the branch instruction.
202179237Sjb    masm->pd_patch_instruction(branch, target);
203179237Sjb  }
204179237Sjb}
205179237Sjb
206179237Sjbstruct DelayedConstant {
207179237Sjb  typedef void (*value_fn_t)();
208179237Sjb  BasicType type;
209179237Sjb  intptr_t value;
210179237Sjb  value_fn_t value_fn;
211179237Sjb  // This limit of 20 is generous for initial uses.
212179237Sjb  // The limit needs to be large enough to store the field offsets
213179237Sjb  // into classes which do not have statically fixed layouts.
214179237Sjb  // (Initial use is for method handle object offsets.)
215179237Sjb  // Look for uses of "delayed_value" in the source code
216179237Sjb  // and make sure this number is generous enough to handle all of them.
217179237Sjb  enum { DC_LIMIT = 20 };
218179237Sjb  static DelayedConstant delayed_constants[DC_LIMIT];
219179237Sjb  static DelayedConstant* add(BasicType type, value_fn_t value_fn);
220179237Sjb  bool match(BasicType t, value_fn_t cfn) {
221179237Sjb    return type == t && value_fn == cfn;
222179237Sjb  }
223179237Sjb  static void update_all();
224179237Sjb};
225179237Sjb
226179237SjbDelayedConstant DelayedConstant::delayed_constants[DC_LIMIT];
227179237Sjb// Default C structure initialization rules have the following effect here:
228179237Sjb// = { { (BasicType)0, (intptr_t)NULL }, ... };
229179237Sjb
230179237SjbDelayedConstant* DelayedConstant::add(BasicType type,
231179237Sjb                                      DelayedConstant::value_fn_t cfn) {
232179237Sjb  for (int i = 0; i < DC_LIMIT; i++) {
233179237Sjb    DelayedConstant* dcon = &delayed_constants[i];
234179237Sjb    if (dcon->match(type, cfn))
235179237Sjb      return dcon;
236179237Sjb    if (dcon->value_fn == NULL) {
237179237Sjb      // (cmpxchg not because this is multi-threaded but because I'm paranoid)
238179237Sjb      if (Atomic::cmpxchg_ptr(CAST_FROM_FN_PTR(void*, cfn), &dcon->value_fn, NULL) == NULL) {
239179237Sjb        dcon->type = type;
240179237Sjb        return dcon;
241179237Sjb      }
242179237Sjb    }
243179237Sjb  }
244179237Sjb  // If this assert is hit (in pre-integration testing!) then re-evaluate
245179237Sjb  // the comment on the definition of DC_LIMIT.
246179237Sjb  guarantee(false, "too many delayed constants");
247179237Sjb  return NULL;
248179237Sjb}
249179237Sjb
250179237Sjbvoid DelayedConstant::update_all() {
251179237Sjb  for (int i = 0; i < DC_LIMIT; i++) {
252179237Sjb    DelayedConstant* dcon = &delayed_constants[i];
253179237Sjb    if (dcon->value_fn != NULL && dcon->value == 0) {
254179237Sjb      typedef int     (*int_fn_t)();
255179237Sjb      typedef address (*address_fn_t)();
256179237Sjb      switch (dcon->type) {
257179237Sjb      case T_INT:     dcon->value = (intptr_t) ((int_fn_t)    dcon->value_fn)(); break;
258179237Sjb      case T_ADDRESS: dcon->value = (intptr_t) ((address_fn_t)dcon->value_fn)(); break;
259179237Sjb      }
260179237Sjb    }
261179237Sjb  }
262179237Sjb}
263179237Sjb
264179237SjbRegisterOrConstant AbstractAssembler::delayed_value(int(*value_fn)(), Register tmp, int offset) {
265179237Sjb  intptr_t val = (intptr_t) (*value_fn)();
266179237Sjb  if (val != 0)  return val + offset;
267179237Sjb  return delayed_value_impl(delayed_value_addr(value_fn), tmp, offset);
268179237Sjb}
269179237SjbRegisterOrConstant AbstractAssembler::delayed_value(address(*value_fn)(), Register tmp, int offset) {
270179237Sjb  intptr_t val = (intptr_t) (*value_fn)();
271179237Sjb  if (val != 0)  return val + offset;
272179237Sjb  return delayed_value_impl(delayed_value_addr(value_fn), tmp, offset);
273179237Sjb}
274179237Sjbintptr_t* AbstractAssembler::delayed_value_addr(int(*value_fn)()) {
275179237Sjb  DelayedConstant* dcon = DelayedConstant::add(T_INT, (DelayedConstant::value_fn_t) value_fn);
276179237Sjb  return &dcon->value;
277179237Sjb}
278179237Sjbintptr_t* AbstractAssembler::delayed_value_addr(address(*value_fn)()) {
279179237Sjb  DelayedConstant* dcon = DelayedConstant::add(T_ADDRESS, (DelayedConstant::value_fn_t) value_fn);
280179237Sjb  return &dcon->value;
281179237Sjb}
282179237Sjbvoid AbstractAssembler::update_delayed_values() {
283179237Sjb  DelayedConstant::update_all();
284179237Sjb}
285179237Sjb
286179237Sjbvoid AbstractAssembler::block_comment(const char* comment) {
287179237Sjb  if (sect() == CodeBuffer::SECT_INSTS) {
288179237Sjb    code_section()->outer()->block_comment(offset(), comment);
289179237Sjb  }
290179237Sjb}
291179237Sjb
292179237Sjbconst char* AbstractAssembler::code_string(const char* str) {
293179237Sjb  if (sect() == CodeBuffer::SECT_INSTS || sect() == CodeBuffer::SECT_STUBS) {
294179237Sjb    return code_section()->outer()->code_string(str);
295179237Sjb  }
296179237Sjb  return NULL;
297179237Sjb}
298179237Sjb
299179237Sjbbool MacroAssembler::needs_explicit_null_check(intptr_t offset) {
300179237Sjb  // Exception handler checks the nmethod's implicit null checks table
301179237Sjb  // only when this method returns false.
302179237Sjb#ifdef _LP64
303179237Sjb  if (UseCompressedOops && Universe::narrow_oop_base() != NULL) {
304179237Sjb    assert (Universe::heap() != NULL, "java heap should be initialized");
305179237Sjb    // The first page after heap_base is unmapped and
306179237Sjb    // the 'offset' is equal to [heap_base + offset] for
307236567Sgnn    // narrow oop implicit null checks.
308236567Sgnn    uintptr_t base = (uintptr_t)Universe::narrow_oop_base();
309236567Sgnn    if ((uintptr_t)offset >= base) {
310236567Sgnn      // Normalize offset for the next check.
311236567Sgnn      offset = (intptr_t)(pointer_delta((void*)offset, (void*)base, 1));
312236567Sgnn    }
313179237Sjb  }
314236567Sgnn#endif
315236567Sgnn  return offset < 0 || os::vm_page_size() <= offset;
316179237Sjb}
317179237Sjb