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