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