aoqi@0: /* aoqi@0: * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: */ aoqi@0: aoqi@0: #include "precompiled.hpp" aoqi@0: #include "asm/macroAssembler.hpp" aoqi@0: #include "asm/macroAssembler.inline.hpp" aoqi@0: #include "asm/codeBuffer.hpp" aoqi@0: #include "runtime/atomic.hpp" aoqi@0: #include "runtime/atomic.inline.hpp" aoqi@0: #include "runtime/icache.hpp" aoqi@0: #include "runtime/os.hpp" aoqi@0: aoqi@0: aoqi@0: // Implementation of AbstractAssembler aoqi@0: // aoqi@0: // The AbstractAssembler is generating code into a CodeBuffer. To make code generation faster, aoqi@0: // the assembler keeps a copy of the code buffers boundaries & modifies them when aoqi@0: // emitting bytes rather than using the code buffers accessor functions all the time. aoqi@0: // The code buffer is updated via set_code_end(...) after emitting a whole instruction. aoqi@0: aoqi@0: AbstractAssembler::AbstractAssembler(CodeBuffer* code) { aoqi@0: if (code == NULL) return; aoqi@0: CodeSection* cs = code->insts(); aoqi@0: cs->clear_mark(); // new assembler kills old mark aoqi@0: if (cs->start() == NULL) { aoqi@0: vm_exit_out_of_memory(0, OOM_MMAP_ERROR, err_msg("CodeCache: no room for %s", aoqi@0: code->name())); aoqi@0: } aoqi@0: _code_section = cs; aoqi@0: _oop_recorder= code->oop_recorder(); aoqi@0: DEBUG_ONLY( _short_branch_delta = 0; ) aoqi@0: } aoqi@0: aoqi@0: void AbstractAssembler::set_code_section(CodeSection* cs) { aoqi@0: assert(cs->outer() == code_section()->outer(), "sanity"); aoqi@0: assert(cs->is_allocated(), "need to pre-allocate this section"); aoqi@0: cs->clear_mark(); // new assembly into this section kills old mark aoqi@0: _code_section = cs; aoqi@0: } aoqi@0: aoqi@0: // Inform CodeBuffer that incoming code and relocation will be for stubs aoqi@0: address AbstractAssembler::start_a_stub(int required_space) { aoqi@0: CodeBuffer* cb = code(); aoqi@0: CodeSection* cs = cb->stubs(); aoqi@0: assert(_code_section == cb->insts(), "not in insts?"); aoqi@0: if (cs->maybe_expand_to_ensure_remaining(required_space) aoqi@0: && cb->blob() == NULL) { aoqi@0: return NULL; aoqi@0: } aoqi@0: set_code_section(cs); aoqi@0: return pc(); aoqi@0: } aoqi@0: aoqi@0: // Inform CodeBuffer that incoming code and relocation will be code aoqi@0: // Should not be called if start_a_stub() returned NULL aoqi@0: void AbstractAssembler::end_a_stub() { aoqi@0: assert(_code_section == code()->stubs(), "not in stubs?"); aoqi@0: set_code_section(code()->insts()); aoqi@0: } aoqi@0: aoqi@0: // Inform CodeBuffer that incoming code and relocation will be for stubs aoqi@0: address AbstractAssembler::start_a_const(int required_space, int required_align) { aoqi@0: CodeBuffer* cb = code(); aoqi@0: CodeSection* cs = cb->consts(); aoqi@0: assert(_code_section == cb->insts() || _code_section == cb->stubs(), "not in insts/stubs?"); aoqi@0: address end = cs->end(); aoqi@0: int pad = -(intptr_t)end & (required_align-1); aoqi@0: if (cs->maybe_expand_to_ensure_remaining(pad + required_space)) { aoqi@0: if (cb->blob() == NULL) return NULL; aoqi@0: end = cs->end(); // refresh pointer aoqi@0: } aoqi@0: if (pad > 0) { aoqi@0: while (--pad >= 0) { *end++ = 0; } aoqi@0: cs->set_end(end); aoqi@0: } aoqi@0: set_code_section(cs); aoqi@0: return end; aoqi@0: } aoqi@0: aoqi@0: // Inform CodeBuffer that incoming code and relocation will be code aoqi@0: // in section cs (insts or stubs). aoqi@0: void AbstractAssembler::end_a_const(CodeSection* cs) { aoqi@0: assert(_code_section == code()->consts(), "not in consts?"); aoqi@0: set_code_section(cs); aoqi@0: } aoqi@0: aoqi@0: void AbstractAssembler::flush() { aoqi@0: ICache::invalidate_range(addr_at(0), offset()); aoqi@0: } aoqi@0: aoqi@0: void AbstractAssembler::bind(Label& L) { aoqi@0: if (L.is_bound()) { aoqi@0: // Assembler can bind a label more than once to the same place. aoqi@0: guarantee(L.loc() == locator(), "attempt to redefine label"); aoqi@0: return; aoqi@0: } aoqi@0: L.bind_loc(locator()); aoqi@0: L.patch_instructions((MacroAssembler*)this); aoqi@0: } aoqi@0: aoqi@0: void AbstractAssembler::generate_stack_overflow_check( int frame_size_in_bytes) { aoqi@0: if (UseStackBanging) { aoqi@0: // Each code entry causes one stack bang n pages down the stack where n aoqi@0: // is configurable by StackShadowPages. The setting depends on the maximum aoqi@0: // depth of VM call stack or native before going back into java code, aoqi@0: // since only java code can raise a stack overflow exception using the aoqi@0: // stack banging mechanism. The VM and native code does not detect stack aoqi@0: // overflow. aoqi@0: // The code in JavaCalls::call() checks that there is at least n pages aoqi@0: // available, so all entry code needs to do is bang once for the end of aoqi@0: // this shadow zone. aoqi@0: // The entry code may need to bang additional pages if the framesize aoqi@0: // is greater than a page. aoqi@0: aoqi@0: const int page_size = os::vm_page_size(); aoqi@0: int bang_end = StackShadowPages*page_size; aoqi@0: aoqi@0: // This is how far the previous frame's stack banging extended. aoqi@0: const int bang_end_safe = bang_end; aoqi@0: aoqi@0: if (frame_size_in_bytes > page_size) { aoqi@0: bang_end += frame_size_in_bytes; aoqi@0: } aoqi@0: aoqi@0: int bang_offset = bang_end_safe; aoqi@0: while (bang_offset <= bang_end) { aoqi@0: // Need at least one stack bang at end of shadow zone. aoqi@0: bang_stack_with_offset(bang_offset); aoqi@0: bang_offset += page_size; aoqi@0: } aoqi@0: } // end (UseStackBanging) aoqi@0: } aoqi@0: aoqi@0: void Label::add_patch_at(CodeBuffer* cb, int branch_loc) { aoqi@0: assert(_loc == -1, "Label is unbound"); aoqi@0: if (_patch_index < PatchCacheSize) { aoqi@0: _patches[_patch_index] = branch_loc; aoqi@0: } else { aoqi@0: if (_patch_overflow == NULL) { aoqi@0: _patch_overflow = cb->create_patch_overflow(); aoqi@0: } aoqi@0: _patch_overflow->push(branch_loc); aoqi@0: } aoqi@0: ++_patch_index; aoqi@0: } aoqi@0: aoqi@0: void Label::patch_instructions(MacroAssembler* masm) { aoqi@0: assert(is_bound(), "Label is bound"); aoqi@0: CodeBuffer* cb = masm->code(); aoqi@0: int target_sect = CodeBuffer::locator_sect(loc()); aoqi@0: address target = cb->locator_address(loc()); aoqi@0: while (_patch_index > 0) { aoqi@0: --_patch_index; aoqi@0: int branch_loc; aoqi@0: if (_patch_index >= PatchCacheSize) { aoqi@0: branch_loc = _patch_overflow->pop(); aoqi@0: } else { aoqi@0: branch_loc = _patches[_patch_index]; aoqi@0: } aoqi@0: int branch_sect = CodeBuffer::locator_sect(branch_loc); aoqi@0: address branch = cb->locator_address(branch_loc); aoqi@0: if (branch_sect == CodeBuffer::SECT_CONSTS) { aoqi@0: // The thing to patch is a constant word. aoqi@0: *(address*)branch = target; aoqi@0: continue; aoqi@0: } aoqi@0: aoqi@0: #ifdef ASSERT aoqi@0: // Cross-section branches only work if the aoqi@0: // intermediate section boundaries are frozen. aoqi@0: if (target_sect != branch_sect) { aoqi@0: for (int n = MIN2(target_sect, branch_sect), aoqi@0: nlimit = (target_sect + branch_sect) - n; aoqi@0: n < nlimit; n++) { aoqi@0: CodeSection* cs = cb->code_section(n); aoqi@0: assert(cs->is_frozen(), "cross-section branch needs stable offsets"); aoqi@0: } aoqi@0: } aoqi@0: #endif //ASSERT aoqi@0: aoqi@0: // Push the target offset into the branch instruction. aoqi@0: masm->pd_patch_instruction(branch, target); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: struct DelayedConstant { aoqi@0: typedef void (*value_fn_t)(); aoqi@0: BasicType type; aoqi@0: intptr_t value; aoqi@0: value_fn_t value_fn; aoqi@0: // This limit of 20 is generous for initial uses. aoqi@0: // The limit needs to be large enough to store the field offsets aoqi@0: // into classes which do not have statically fixed layouts. aoqi@0: // (Initial use is for method handle object offsets.) aoqi@0: // Look for uses of "delayed_value" in the source code aoqi@0: // and make sure this number is generous enough to handle all of them. aoqi@0: enum { DC_LIMIT = 20 }; aoqi@0: static DelayedConstant delayed_constants[DC_LIMIT]; aoqi@0: static DelayedConstant* add(BasicType type, value_fn_t value_fn); aoqi@0: bool match(BasicType t, value_fn_t cfn) { aoqi@0: return type == t && value_fn == cfn; aoqi@0: } aoqi@0: static void update_all(); aoqi@0: }; aoqi@0: aoqi@0: DelayedConstant DelayedConstant::delayed_constants[DC_LIMIT]; aoqi@0: // Default C structure initialization rules have the following effect here: aoqi@0: // = { { (BasicType)0, (intptr_t)NULL }, ... }; aoqi@0: aoqi@0: DelayedConstant* DelayedConstant::add(BasicType type, aoqi@0: DelayedConstant::value_fn_t cfn) { aoqi@0: for (int i = 0; i < DC_LIMIT; i++) { aoqi@0: DelayedConstant* dcon = &delayed_constants[i]; aoqi@0: if (dcon->match(type, cfn)) aoqi@0: return dcon; aoqi@0: if (dcon->value_fn == NULL) { aoqi@0: // (cmpxchg not because this is multi-threaded but because I'm paranoid) aoqi@0: if (Atomic::cmpxchg_ptr(CAST_FROM_FN_PTR(void*, cfn), &dcon->value_fn, NULL) == NULL) { aoqi@0: dcon->type = type; aoqi@0: return dcon; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: // If this assert is hit (in pre-integration testing!) then re-evaluate aoqi@0: // the comment on the definition of DC_LIMIT. aoqi@0: guarantee(false, "too many delayed constants"); aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: void DelayedConstant::update_all() { aoqi@0: for (int i = 0; i < DC_LIMIT; i++) { aoqi@0: DelayedConstant* dcon = &delayed_constants[i]; aoqi@0: if (dcon->value_fn != NULL && dcon->value == 0) { aoqi@0: typedef int (*int_fn_t)(); aoqi@0: typedef address (*address_fn_t)(); aoqi@0: switch (dcon->type) { aoqi@0: case T_INT: dcon->value = (intptr_t) ((int_fn_t) dcon->value_fn)(); break; aoqi@0: case T_ADDRESS: dcon->value = (intptr_t) ((address_fn_t)dcon->value_fn)(); break; aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: RegisterOrConstant AbstractAssembler::delayed_value(int(*value_fn)(), Register tmp, int offset) { aoqi@0: intptr_t val = (intptr_t) (*value_fn)(); aoqi@0: if (val != 0) return val + offset; aoqi@0: return delayed_value_impl(delayed_value_addr(value_fn), tmp, offset); aoqi@0: } aoqi@0: RegisterOrConstant AbstractAssembler::delayed_value(address(*value_fn)(), Register tmp, int offset) { aoqi@0: intptr_t val = (intptr_t) (*value_fn)(); aoqi@0: if (val != 0) return val + offset; aoqi@0: return delayed_value_impl(delayed_value_addr(value_fn), tmp, offset); aoqi@0: } aoqi@0: intptr_t* AbstractAssembler::delayed_value_addr(int(*value_fn)()) { aoqi@0: DelayedConstant* dcon = DelayedConstant::add(T_INT, (DelayedConstant::value_fn_t) value_fn); aoqi@0: return &dcon->value; aoqi@0: } aoqi@0: intptr_t* AbstractAssembler::delayed_value_addr(address(*value_fn)()) { aoqi@0: DelayedConstant* dcon = DelayedConstant::add(T_ADDRESS, (DelayedConstant::value_fn_t) value_fn); aoqi@0: return &dcon->value; aoqi@0: } aoqi@0: void AbstractAssembler::update_delayed_values() { aoqi@0: DelayedConstant::update_all(); aoqi@0: } aoqi@0: aoqi@0: void AbstractAssembler::block_comment(const char* comment) { aoqi@0: if (sect() == CodeBuffer::SECT_INSTS) { aoqi@0: code_section()->outer()->block_comment(offset(), comment); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: const char* AbstractAssembler::code_string(const char* str) { aoqi@0: if (sect() == CodeBuffer::SECT_INSTS || sect() == CodeBuffer::SECT_STUBS) { aoqi@0: return code_section()->outer()->code_string(str); aoqi@0: } aoqi@0: return NULL; aoqi@0: } aoqi@0: aoqi@0: bool MacroAssembler::needs_explicit_null_check(intptr_t offset) { aoqi@0: // Exception handler checks the nmethod's implicit null checks table aoqi@0: // only when this method returns false. aoqi@0: #ifdef _LP64 aoqi@0: if (UseCompressedOops && Universe::narrow_oop_base() != NULL) { aoqi@0: assert (Universe::heap() != NULL, "java heap should be initialized"); aoqi@0: // The first page after heap_base is unmapped and aoqi@0: // the 'offset' is equal to [heap_base + offset] for aoqi@0: // narrow oop implicit null checks. aoqi@0: uintptr_t base = (uintptr_t)Universe::narrow_oop_base(); aoqi@0: if ((uintptr_t)offset >= base) { aoqi@0: // Normalize offset for the next check. aoqi@0: offset = (intptr_t)(pointer_delta((void*)offset, (void*)base, 1)); aoqi@0: } aoqi@0: } aoqi@0: #endif aoqi@0: return offset < 0 || os::vm_page_size() <= offset; aoqi@0: }