Wed, 03 Jul 2019 20:42:37 +0800
Merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@0 | 25 | #include "precompiled.hpp" |
aoqi@0 | 26 | #include "asm/macroAssembler.hpp" |
aoqi@0 | 27 | #include "asm/macroAssembler.inline.hpp" |
aoqi@0 | 28 | #include "asm/codeBuffer.hpp" |
aoqi@0 | 29 | #include "runtime/atomic.hpp" |
aoqi@0 | 30 | #include "runtime/atomic.inline.hpp" |
aoqi@0 | 31 | #include "runtime/icache.hpp" |
aoqi@0 | 32 | #include "runtime/os.hpp" |
aoqi@0 | 33 | |
aoqi@0 | 34 | |
aoqi@0 | 35 | // Implementation of AbstractAssembler |
aoqi@0 | 36 | // |
aoqi@0 | 37 | // The AbstractAssembler is generating code into a CodeBuffer. To make code generation faster, |
aoqi@0 | 38 | // the assembler keeps a copy of the code buffers boundaries & modifies them when |
aoqi@0 | 39 | // emitting bytes rather than using the code buffers accessor functions all the time. |
aoqi@0 | 40 | // The code buffer is updated via set_code_end(...) after emitting a whole instruction. |
aoqi@0 | 41 | |
aoqi@0 | 42 | AbstractAssembler::AbstractAssembler(CodeBuffer* code) { |
aoqi@0 | 43 | if (code == NULL) return; |
aoqi@0 | 44 | CodeSection* cs = code->insts(); |
aoqi@0 | 45 | cs->clear_mark(); // new assembler kills old mark |
aoqi@0 | 46 | if (cs->start() == NULL) { |
aoqi@0 | 47 | vm_exit_out_of_memory(0, OOM_MMAP_ERROR, err_msg("CodeCache: no room for %s", |
aoqi@0 | 48 | code->name())); |
aoqi@0 | 49 | } |
aoqi@0 | 50 | _code_section = cs; |
aoqi@0 | 51 | _oop_recorder= code->oop_recorder(); |
aoqi@0 | 52 | DEBUG_ONLY( _short_branch_delta = 0; ) |
aoqi@0 | 53 | } |
aoqi@0 | 54 | |
aoqi@0 | 55 | void AbstractAssembler::set_code_section(CodeSection* cs) { |
aoqi@0 | 56 | assert(cs->outer() == code_section()->outer(), "sanity"); |
aoqi@0 | 57 | assert(cs->is_allocated(), "need to pre-allocate this section"); |
aoqi@0 | 58 | cs->clear_mark(); // new assembly into this section kills old mark |
aoqi@0 | 59 | _code_section = cs; |
aoqi@0 | 60 | } |
aoqi@0 | 61 | |
aoqi@0 | 62 | // Inform CodeBuffer that incoming code and relocation will be for stubs |
aoqi@0 | 63 | address AbstractAssembler::start_a_stub(int required_space) { |
aoqi@0 | 64 | CodeBuffer* cb = code(); |
aoqi@0 | 65 | CodeSection* cs = cb->stubs(); |
aoqi@0 | 66 | assert(_code_section == cb->insts(), "not in insts?"); |
aoqi@0 | 67 | if (cs->maybe_expand_to_ensure_remaining(required_space) |
aoqi@0 | 68 | && cb->blob() == NULL) { |
aoqi@0 | 69 | return NULL; |
aoqi@0 | 70 | } |
aoqi@0 | 71 | set_code_section(cs); |
aoqi@0 | 72 | return pc(); |
aoqi@0 | 73 | } |
aoqi@0 | 74 | |
aoqi@0 | 75 | // Inform CodeBuffer that incoming code and relocation will be code |
aoqi@0 | 76 | // Should not be called if start_a_stub() returned NULL |
aoqi@0 | 77 | void AbstractAssembler::end_a_stub() { |
aoqi@0 | 78 | assert(_code_section == code()->stubs(), "not in stubs?"); |
aoqi@0 | 79 | set_code_section(code()->insts()); |
aoqi@0 | 80 | } |
aoqi@0 | 81 | |
aoqi@0 | 82 | // Inform CodeBuffer that incoming code and relocation will be for stubs |
aoqi@0 | 83 | address AbstractAssembler::start_a_const(int required_space, int required_align) { |
aoqi@0 | 84 | CodeBuffer* cb = code(); |
aoqi@0 | 85 | CodeSection* cs = cb->consts(); |
aoqi@0 | 86 | assert(_code_section == cb->insts() || _code_section == cb->stubs(), "not in insts/stubs?"); |
aoqi@0 | 87 | address end = cs->end(); |
aoqi@0 | 88 | int pad = -(intptr_t)end & (required_align-1); |
aoqi@0 | 89 | if (cs->maybe_expand_to_ensure_remaining(pad + required_space)) { |
aoqi@0 | 90 | if (cb->blob() == NULL) return NULL; |
aoqi@0 | 91 | end = cs->end(); // refresh pointer |
aoqi@0 | 92 | } |
aoqi@0 | 93 | if (pad > 0) { |
aoqi@0 | 94 | while (--pad >= 0) { *end++ = 0; } |
aoqi@0 | 95 | cs->set_end(end); |
aoqi@0 | 96 | } |
aoqi@0 | 97 | set_code_section(cs); |
aoqi@0 | 98 | return end; |
aoqi@0 | 99 | } |
aoqi@0 | 100 | |
aoqi@0 | 101 | // Inform CodeBuffer that incoming code and relocation will be code |
aoqi@0 | 102 | // in section cs (insts or stubs). |
aoqi@0 | 103 | void AbstractAssembler::end_a_const(CodeSection* cs) { |
aoqi@0 | 104 | assert(_code_section == code()->consts(), "not in consts?"); |
aoqi@0 | 105 | set_code_section(cs); |
aoqi@0 | 106 | } |
aoqi@0 | 107 | |
aoqi@0 | 108 | void AbstractAssembler::flush() { |
aoqi@0 | 109 | ICache::invalidate_range(addr_at(0), offset()); |
aoqi@0 | 110 | } |
aoqi@0 | 111 | |
aoqi@0 | 112 | void AbstractAssembler::bind(Label& L) { |
aoqi@0 | 113 | if (L.is_bound()) { |
aoqi@0 | 114 | // Assembler can bind a label more than once to the same place. |
aoqi@0 | 115 | guarantee(L.loc() == locator(), "attempt to redefine label"); |
aoqi@0 | 116 | return; |
aoqi@0 | 117 | } |
aoqi@0 | 118 | L.bind_loc(locator()); |
aoqi@0 | 119 | L.patch_instructions((MacroAssembler*)this); |
aoqi@0 | 120 | } |
aoqi@0 | 121 | |
aoqi@0 | 122 | void AbstractAssembler::generate_stack_overflow_check( int frame_size_in_bytes) { |
aoqi@0 | 123 | if (UseStackBanging) { |
aoqi@0 | 124 | // Each code entry causes one stack bang n pages down the stack where n |
aoqi@0 | 125 | // is configurable by StackShadowPages. The setting depends on the maximum |
aoqi@0 | 126 | // depth of VM call stack or native before going back into java code, |
aoqi@0 | 127 | // since only java code can raise a stack overflow exception using the |
aoqi@0 | 128 | // stack banging mechanism. The VM and native code does not detect stack |
aoqi@0 | 129 | // overflow. |
aoqi@0 | 130 | // The code in JavaCalls::call() checks that there is at least n pages |
aoqi@0 | 131 | // available, so all entry code needs to do is bang once for the end of |
aoqi@0 | 132 | // this shadow zone. |
aoqi@0 | 133 | // The entry code may need to bang additional pages if the framesize |
aoqi@0 | 134 | // is greater than a page. |
aoqi@0 | 135 | |
aoqi@0 | 136 | const int page_size = os::vm_page_size(); |
aoqi@0 | 137 | int bang_end = StackShadowPages*page_size; |
aoqi@0 | 138 | |
aoqi@0 | 139 | // This is how far the previous frame's stack banging extended. |
aoqi@0 | 140 | const int bang_end_safe = bang_end; |
aoqi@0 | 141 | |
aoqi@0 | 142 | if (frame_size_in_bytes > page_size) { |
aoqi@0 | 143 | bang_end += frame_size_in_bytes; |
aoqi@0 | 144 | } |
aoqi@0 | 145 | |
aoqi@0 | 146 | int bang_offset = bang_end_safe; |
aoqi@0 | 147 | while (bang_offset <= bang_end) { |
aoqi@0 | 148 | // Need at least one stack bang at end of shadow zone. |
aoqi@0 | 149 | bang_stack_with_offset(bang_offset); |
aoqi@0 | 150 | bang_offset += page_size; |
aoqi@0 | 151 | } |
aoqi@0 | 152 | } // end (UseStackBanging) |
aoqi@0 | 153 | } |
aoqi@0 | 154 | |
aoqi@0 | 155 | void Label::add_patch_at(CodeBuffer* cb, int branch_loc) { |
aoqi@0 | 156 | assert(_loc == -1, "Label is unbound"); |
aoqi@0 | 157 | if (_patch_index < PatchCacheSize) { |
aoqi@0 | 158 | _patches[_patch_index] = branch_loc; |
aoqi@0 | 159 | } else { |
aoqi@0 | 160 | if (_patch_overflow == NULL) { |
aoqi@0 | 161 | _patch_overflow = cb->create_patch_overflow(); |
aoqi@0 | 162 | } |
aoqi@0 | 163 | _patch_overflow->push(branch_loc); |
aoqi@0 | 164 | } |
aoqi@0 | 165 | ++_patch_index; |
aoqi@0 | 166 | } |
aoqi@0 | 167 | |
aoqi@0 | 168 | void Label::patch_instructions(MacroAssembler* masm) { |
aoqi@0 | 169 | assert(is_bound(), "Label is bound"); |
aoqi@0 | 170 | CodeBuffer* cb = masm->code(); |
aoqi@0 | 171 | int target_sect = CodeBuffer::locator_sect(loc()); |
aoqi@0 | 172 | address target = cb->locator_address(loc()); |
aoqi@0 | 173 | while (_patch_index > 0) { |
aoqi@0 | 174 | --_patch_index; |
aoqi@0 | 175 | int branch_loc; |
aoqi@0 | 176 | if (_patch_index >= PatchCacheSize) { |
aoqi@0 | 177 | branch_loc = _patch_overflow->pop(); |
aoqi@0 | 178 | } else { |
aoqi@0 | 179 | branch_loc = _patches[_patch_index]; |
aoqi@0 | 180 | } |
aoqi@0 | 181 | int branch_sect = CodeBuffer::locator_sect(branch_loc); |
aoqi@0 | 182 | address branch = cb->locator_address(branch_loc); |
aoqi@0 | 183 | if (branch_sect == CodeBuffer::SECT_CONSTS) { |
aoqi@0 | 184 | // The thing to patch is a constant word. |
aoqi@0 | 185 | *(address*)branch = target; |
aoqi@0 | 186 | continue; |
aoqi@0 | 187 | } |
aoqi@0 | 188 | |
aoqi@0 | 189 | #ifdef ASSERT |
aoqi@0 | 190 | // Cross-section branches only work if the |
aoqi@0 | 191 | // intermediate section boundaries are frozen. |
aoqi@0 | 192 | if (target_sect != branch_sect) { |
aoqi@0 | 193 | for (int n = MIN2(target_sect, branch_sect), |
aoqi@0 | 194 | nlimit = (target_sect + branch_sect) - n; |
aoqi@0 | 195 | n < nlimit; n++) { |
aoqi@0 | 196 | CodeSection* cs = cb->code_section(n); |
aoqi@0 | 197 | assert(cs->is_frozen(), "cross-section branch needs stable offsets"); |
aoqi@0 | 198 | } |
aoqi@0 | 199 | } |
aoqi@0 | 200 | #endif //ASSERT |
aoqi@0 | 201 | |
aoqi@0 | 202 | // Push the target offset into the branch instruction. |
aoqi@0 | 203 | masm->pd_patch_instruction(branch, target); |
aoqi@0 | 204 | } |
aoqi@0 | 205 | } |
aoqi@0 | 206 | |
aoqi@0 | 207 | struct DelayedConstant { |
aoqi@0 | 208 | typedef void (*value_fn_t)(); |
aoqi@0 | 209 | BasicType type; |
aoqi@0 | 210 | intptr_t value; |
aoqi@0 | 211 | value_fn_t value_fn; |
aoqi@0 | 212 | // This limit of 20 is generous for initial uses. |
aoqi@0 | 213 | // The limit needs to be large enough to store the field offsets |
aoqi@0 | 214 | // into classes which do not have statically fixed layouts. |
aoqi@0 | 215 | // (Initial use is for method handle object offsets.) |
aoqi@0 | 216 | // Look for uses of "delayed_value" in the source code |
aoqi@0 | 217 | // and make sure this number is generous enough to handle all of them. |
aoqi@0 | 218 | enum { DC_LIMIT = 20 }; |
aoqi@0 | 219 | static DelayedConstant delayed_constants[DC_LIMIT]; |
aoqi@0 | 220 | static DelayedConstant* add(BasicType type, value_fn_t value_fn); |
aoqi@0 | 221 | bool match(BasicType t, value_fn_t cfn) { |
aoqi@0 | 222 | return type == t && value_fn == cfn; |
aoqi@0 | 223 | } |
aoqi@0 | 224 | static void update_all(); |
aoqi@0 | 225 | }; |
aoqi@0 | 226 | |
aoqi@0 | 227 | DelayedConstant DelayedConstant::delayed_constants[DC_LIMIT]; |
aoqi@0 | 228 | // Default C structure initialization rules have the following effect here: |
aoqi@0 | 229 | // = { { (BasicType)0, (intptr_t)NULL }, ... }; |
aoqi@0 | 230 | |
aoqi@0 | 231 | DelayedConstant* DelayedConstant::add(BasicType type, |
aoqi@0 | 232 | DelayedConstant::value_fn_t cfn) { |
aoqi@0 | 233 | for (int i = 0; i < DC_LIMIT; i++) { |
aoqi@0 | 234 | DelayedConstant* dcon = &delayed_constants[i]; |
aoqi@0 | 235 | if (dcon->match(type, cfn)) |
aoqi@0 | 236 | return dcon; |
aoqi@0 | 237 | if (dcon->value_fn == NULL) { |
aoqi@0 | 238 | // (cmpxchg not because this is multi-threaded but because I'm paranoid) |
aoqi@0 | 239 | if (Atomic::cmpxchg_ptr(CAST_FROM_FN_PTR(void*, cfn), &dcon->value_fn, NULL) == NULL) { |
aoqi@0 | 240 | dcon->type = type; |
aoqi@0 | 241 | return dcon; |
aoqi@0 | 242 | } |
aoqi@0 | 243 | } |
aoqi@0 | 244 | } |
aoqi@0 | 245 | // If this assert is hit (in pre-integration testing!) then re-evaluate |
aoqi@0 | 246 | // the comment on the definition of DC_LIMIT. |
aoqi@0 | 247 | guarantee(false, "too many delayed constants"); |
aoqi@0 | 248 | return NULL; |
aoqi@0 | 249 | } |
aoqi@0 | 250 | |
aoqi@0 | 251 | void DelayedConstant::update_all() { |
aoqi@0 | 252 | for (int i = 0; i < DC_LIMIT; i++) { |
aoqi@0 | 253 | DelayedConstant* dcon = &delayed_constants[i]; |
aoqi@0 | 254 | if (dcon->value_fn != NULL && dcon->value == 0) { |
aoqi@0 | 255 | typedef int (*int_fn_t)(); |
aoqi@0 | 256 | typedef address (*address_fn_t)(); |
aoqi@0 | 257 | switch (dcon->type) { |
aoqi@0 | 258 | case T_INT: dcon->value = (intptr_t) ((int_fn_t) dcon->value_fn)(); break; |
aoqi@0 | 259 | case T_ADDRESS: dcon->value = (intptr_t) ((address_fn_t)dcon->value_fn)(); break; |
aoqi@0 | 260 | } |
aoqi@0 | 261 | } |
aoqi@0 | 262 | } |
aoqi@0 | 263 | } |
aoqi@0 | 264 | |
aoqi@0 | 265 | RegisterOrConstant AbstractAssembler::delayed_value(int(*value_fn)(), Register tmp, int offset) { |
aoqi@0 | 266 | intptr_t val = (intptr_t) (*value_fn)(); |
aoqi@0 | 267 | if (val != 0) return val + offset; |
aoqi@0 | 268 | return delayed_value_impl(delayed_value_addr(value_fn), tmp, offset); |
aoqi@0 | 269 | } |
aoqi@0 | 270 | RegisterOrConstant AbstractAssembler::delayed_value(address(*value_fn)(), Register tmp, int offset) { |
aoqi@0 | 271 | intptr_t val = (intptr_t) (*value_fn)(); |
aoqi@0 | 272 | if (val != 0) return val + offset; |
aoqi@0 | 273 | return delayed_value_impl(delayed_value_addr(value_fn), tmp, offset); |
aoqi@0 | 274 | } |
aoqi@0 | 275 | intptr_t* AbstractAssembler::delayed_value_addr(int(*value_fn)()) { |
aoqi@0 | 276 | DelayedConstant* dcon = DelayedConstant::add(T_INT, (DelayedConstant::value_fn_t) value_fn); |
aoqi@0 | 277 | return &dcon->value; |
aoqi@0 | 278 | } |
aoqi@0 | 279 | intptr_t* AbstractAssembler::delayed_value_addr(address(*value_fn)()) { |
aoqi@0 | 280 | DelayedConstant* dcon = DelayedConstant::add(T_ADDRESS, (DelayedConstant::value_fn_t) value_fn); |
aoqi@0 | 281 | return &dcon->value; |
aoqi@0 | 282 | } |
aoqi@0 | 283 | void AbstractAssembler::update_delayed_values() { |
aoqi@0 | 284 | DelayedConstant::update_all(); |
aoqi@0 | 285 | } |
aoqi@0 | 286 | |
aoqi@0 | 287 | void AbstractAssembler::block_comment(const char* comment) { |
aoqi@0 | 288 | if (sect() == CodeBuffer::SECT_INSTS) { |
aoqi@0 | 289 | code_section()->outer()->block_comment(offset(), comment); |
aoqi@0 | 290 | } |
aoqi@0 | 291 | } |
aoqi@0 | 292 | |
aoqi@0 | 293 | const char* AbstractAssembler::code_string(const char* str) { |
aoqi@0 | 294 | if (sect() == CodeBuffer::SECT_INSTS || sect() == CodeBuffer::SECT_STUBS) { |
aoqi@0 | 295 | return code_section()->outer()->code_string(str); |
aoqi@0 | 296 | } |
aoqi@0 | 297 | return NULL; |
aoqi@0 | 298 | } |
aoqi@0 | 299 | |
aoqi@0 | 300 | bool MacroAssembler::needs_explicit_null_check(intptr_t offset) { |
aoqi@0 | 301 | // Exception handler checks the nmethod's implicit null checks table |
aoqi@0 | 302 | // only when this method returns false. |
aoqi@0 | 303 | #ifdef _LP64 |
aoqi@0 | 304 | if (UseCompressedOops && Universe::narrow_oop_base() != NULL) { |
aoqi@0 | 305 | assert (Universe::heap() != NULL, "java heap should be initialized"); |
aoqi@0 | 306 | // The first page after heap_base is unmapped and |
aoqi@0 | 307 | // the 'offset' is equal to [heap_base + offset] for |
aoqi@0 | 308 | // narrow oop implicit null checks. |
aoqi@0 | 309 | uintptr_t base = (uintptr_t)Universe::narrow_oop_base(); |
aoqi@0 | 310 | if ((uintptr_t)offset >= base) { |
aoqi@0 | 311 | // Normalize offset for the next check. |
aoqi@0 | 312 | offset = (intptr_t)(pointer_delta((void*)offset, (void*)base, 1)); |
aoqi@0 | 313 | } |
aoqi@0 | 314 | } |
aoqi@0 | 315 | #endif |
aoqi@0 | 316 | return offset < 0 || os::vm_page_size() <= offset; |
aoqi@0 | 317 | } |