Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/asm/assembler.cpp
32285 views
/*1* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "asm/macroAssembler.hpp"26#include "asm/macroAssembler.inline.hpp"27#include "asm/codeBuffer.hpp"28#include "runtime/atomic.hpp"29#include "runtime/atomic.inline.hpp"30#include "runtime/icache.hpp"31#include "runtime/os.hpp"323334// Implementation of AbstractAssembler35//36// The AbstractAssembler is generating code into a CodeBuffer. To make code generation faster,37// the assembler keeps a copy of the code buffers boundaries & modifies them when38// emitting bytes rather than using the code buffers accessor functions all the time.39// The code buffer is updated via set_code_end(...) after emitting a whole instruction.4041AbstractAssembler::AbstractAssembler(CodeBuffer* code) {42if (code == NULL) return;43CodeSection* cs = code->insts();44cs->clear_mark(); // new assembler kills old mark45if (cs->start() == NULL) {46vm_exit_out_of_memory(0, OOM_MMAP_ERROR, err_msg("CodeCache: no room for %s",47code->name()));48}49_code_section = cs;50_oop_recorder= code->oop_recorder();51DEBUG_ONLY( _short_branch_delta = 0; )52}5354void AbstractAssembler::set_code_section(CodeSection* cs) {55assert(cs->outer() == code_section()->outer(), "sanity");56assert(cs->is_allocated(), "need to pre-allocate this section");57cs->clear_mark(); // new assembly into this section kills old mark58_code_section = cs;59}6061// Inform CodeBuffer that incoming code and relocation will be for stubs62address AbstractAssembler::start_a_stub(int required_space) {63CodeBuffer* cb = code();64CodeSection* cs = cb->stubs();65assert(_code_section == cb->insts(), "not in insts?");66if (cs->maybe_expand_to_ensure_remaining(required_space)67&& cb->blob() == NULL) {68return NULL;69}70set_code_section(cs);71return pc();72}7374// Inform CodeBuffer that incoming code and relocation will be code75// Should not be called if start_a_stub() returned NULL76void AbstractAssembler::end_a_stub() {77assert(_code_section == code()->stubs(), "not in stubs?");78set_code_section(code()->insts());79}8081// Inform CodeBuffer that incoming code and relocation will be for stubs82address AbstractAssembler::start_a_const(int required_space, int required_align) {83CodeBuffer* cb = code();84CodeSection* cs = cb->consts();85assert(_code_section == cb->insts() || _code_section == cb->stubs(), "not in insts/stubs?");86address end = cs->end();87int pad = -(intptr_t)end & (required_align-1);88if (cs->maybe_expand_to_ensure_remaining(pad + required_space)) {89if (cb->blob() == NULL) return NULL;90end = cs->end(); // refresh pointer91}92if (pad > 0) {93while (--pad >= 0) { *end++ = 0; }94cs->set_end(end);95}96set_code_section(cs);97return end;98}99100// Inform CodeBuffer that incoming code and relocation will be code101// in section cs (insts or stubs).102void AbstractAssembler::end_a_const(CodeSection* cs) {103assert(_code_section == code()->consts(), "not in consts?");104set_code_section(cs);105}106107void AbstractAssembler::flush() {108ICache::invalidate_range(addr_at(0), offset());109}110111void AbstractAssembler::bind(Label& L) {112if (L.is_bound()) {113// Assembler can bind a label more than once to the same place.114guarantee(L.loc() == locator(), "attempt to redefine label");115return;116}117L.bind_loc(locator());118L.patch_instructions((MacroAssembler*)this);119}120121void AbstractAssembler::generate_stack_overflow_check( int frame_size_in_bytes) {122if (UseStackBanging) {123// Each code entry causes one stack bang n pages down the stack where n124// is configurable by StackShadowPages. The setting depends on the maximum125// depth of VM call stack or native before going back into java code,126// since only java code can raise a stack overflow exception using the127// stack banging mechanism. The VM and native code does not detect stack128// overflow.129// The code in JavaCalls::call() checks that there is at least n pages130// available, so all entry code needs to do is bang once for the end of131// this shadow zone.132// The entry code may need to bang additional pages if the framesize133// is greater than a page.134135const int page_size = os::vm_page_size();136int bang_end = StackShadowPages*page_size;137138// This is how far the previous frame's stack banging extended.139const int bang_end_safe = bang_end;140141if (frame_size_in_bytes > page_size) {142bang_end += frame_size_in_bytes;143}144145int bang_offset = bang_end_safe;146while (bang_offset <= bang_end) {147// Need at least one stack bang at end of shadow zone.148bang_stack_with_offset(bang_offset);149bang_offset += page_size;150}151} // end (UseStackBanging)152}153154void Label::add_patch_at(CodeBuffer* cb, int branch_loc) {155assert(_loc == -1, "Label is unbound");156if (_patch_index < PatchCacheSize) {157_patches[_patch_index] = branch_loc;158} else {159if (_patch_overflow == NULL) {160_patch_overflow = cb->create_patch_overflow();161}162_patch_overflow->push(branch_loc);163}164++_patch_index;165}166167void Label::patch_instructions(MacroAssembler* masm) {168assert(is_bound(), "Label is bound");169CodeBuffer* cb = masm->code();170int target_sect = CodeBuffer::locator_sect(loc());171address target = cb->locator_address(loc());172while (_patch_index > 0) {173--_patch_index;174int branch_loc;175if (_patch_index >= PatchCacheSize) {176branch_loc = _patch_overflow->pop();177} else {178branch_loc = _patches[_patch_index];179}180int branch_sect = CodeBuffer::locator_sect(branch_loc);181address branch = cb->locator_address(branch_loc);182if (branch_sect == CodeBuffer::SECT_CONSTS) {183// The thing to patch is a constant word.184*(address*)branch = target;185continue;186}187188#ifdef ASSERT189// Cross-section branches only work if the190// intermediate section boundaries are frozen.191if (target_sect != branch_sect) {192for (int n = MIN2(target_sect, branch_sect),193nlimit = (target_sect + branch_sect) - n;194n < nlimit; n++) {195CodeSection* cs = cb->code_section(n);196assert(cs->is_frozen(), "cross-section branch needs stable offsets");197}198}199#endif //ASSERT200201// Push the target offset into the branch instruction.202masm->pd_patch_instruction(branch, target);203}204}205206struct DelayedConstant {207typedef void (*value_fn_t)();208BasicType type;209intptr_t value;210value_fn_t value_fn;211// This limit of 20 is generous for initial uses.212// The limit needs to be large enough to store the field offsets213// into classes which do not have statically fixed layouts.214// (Initial use is for method handle object offsets.)215// Look for uses of "delayed_value" in the source code216// and make sure this number is generous enough to handle all of them.217enum { DC_LIMIT = 20 };218static DelayedConstant delayed_constants[DC_LIMIT];219static DelayedConstant* add(BasicType type, value_fn_t value_fn);220bool match(BasicType t, value_fn_t cfn) {221return type == t && value_fn == cfn;222}223static void update_all();224};225226DelayedConstant DelayedConstant::delayed_constants[DC_LIMIT];227// Default C structure initialization rules have the following effect here:228// = { { (BasicType)0, (intptr_t)NULL }, ... };229230DelayedConstant* DelayedConstant::add(BasicType type,231DelayedConstant::value_fn_t cfn) {232for (int i = 0; i < DC_LIMIT; i++) {233DelayedConstant* dcon = &delayed_constants[i];234if (dcon->match(type, cfn))235return dcon;236if (dcon->value_fn == NULL) {237// (cmpxchg not because this is multi-threaded but because I'm paranoid)238if (Atomic::cmpxchg_ptr(CAST_FROM_FN_PTR(void*, cfn), &dcon->value_fn, NULL) == NULL) {239dcon->type = type;240return dcon;241}242}243}244// If this assert is hit (in pre-integration testing!) then re-evaluate245// the comment on the definition of DC_LIMIT.246guarantee(false, "too many delayed constants");247return NULL;248}249250void DelayedConstant::update_all() {251for (int i = 0; i < DC_LIMIT; i++) {252DelayedConstant* dcon = &delayed_constants[i];253if (dcon->value_fn != NULL && dcon->value == 0) {254typedef int (*int_fn_t)();255typedef address (*address_fn_t)();256switch (dcon->type) {257case T_INT: dcon->value = (intptr_t) ((int_fn_t) dcon->value_fn)(); break;258case T_ADDRESS: dcon->value = (intptr_t) ((address_fn_t)dcon->value_fn)(); break;259}260}261}262}263264RegisterOrConstant AbstractAssembler::delayed_value(int(*value_fn)(), Register tmp, int offset) {265intptr_t val = (intptr_t) (*value_fn)();266if (val != 0) return val + offset;267return delayed_value_impl(delayed_value_addr(value_fn), tmp, offset);268}269RegisterOrConstant AbstractAssembler::delayed_value(address(*value_fn)(), Register tmp, int offset) {270intptr_t val = (intptr_t) (*value_fn)();271if (val != 0) return val + offset;272return delayed_value_impl(delayed_value_addr(value_fn), tmp, offset);273}274intptr_t* AbstractAssembler::delayed_value_addr(int(*value_fn)()) {275DelayedConstant* dcon = DelayedConstant::add(T_INT, (DelayedConstant::value_fn_t) value_fn);276return &dcon->value;277}278intptr_t* AbstractAssembler::delayed_value_addr(address(*value_fn)()) {279DelayedConstant* dcon = DelayedConstant::add(T_ADDRESS, (DelayedConstant::value_fn_t) value_fn);280return &dcon->value;281}282void AbstractAssembler::update_delayed_values() {283DelayedConstant::update_all();284}285286void AbstractAssembler::block_comment(const char* comment) {287if (sect() == CodeBuffer::SECT_INSTS) {288code_section()->outer()->block_comment(offset(), comment);289}290}291292const char* AbstractAssembler::code_string(const char* str) {293if (sect() == CodeBuffer::SECT_INSTS || sect() == CodeBuffer::SECT_STUBS) {294return code_section()->outer()->code_string(str);295}296return NULL;297}298299bool MacroAssembler::needs_explicit_null_check(intptr_t offset) {300// Exception handler checks the nmethod's implicit null checks table301// only when this method returns false.302#ifdef _LP64303if (UseCompressedOops && Universe::narrow_oop_base() != NULL) {304assert (Universe::heap() != NULL, "java heap should be initialized");305// The first page after heap_base is unmapped and306// the 'offset' is equal to [heap_base + offset] for307// narrow oop implicit null checks.308uintptr_t base = (uintptr_t)Universe::narrow_oop_base();309if ((uintptr_t)offset >= base) {310// Normalize offset for the next check.311offset = (intptr_t)(pointer_delta((void*)offset, (void*)base, 1));312}313}314#endif315return offset < 0 || os::vm_page_size() <= offset;316}317318319