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