1 /*
   2  * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * Copyright 2012, 2013 SAP AG. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  *
  24  */
  25 
  26 #include "precompiled.hpp"
  27 #include "asm/assembler.inline.hpp"
  28 #include "assembler_ppc.inline.hpp"
  29 #include "code/relocInfo.hpp"
  30 #include "nativeInst_ppc.hpp"
  31 #include "oops/oop.inline.hpp"
  32 #include "runtime/safepoint.hpp"
  33 
  34 void Relocation::pd_set_data_value(address x, intptr_t o, bool verify_only) {
  35   bool copy_back_to_oop_pool = true;  // TODO: PPC port
  36   // The following comment is from the declaration of DataRelocation:
  37   //
  38   //  "The "o" (displacement) argument is relevant only to split relocations
  39   //   on RISC machines.  In some CPUs (SPARC), the set-hi and set-lo ins'ns
  40   //   can encode more than 32 bits between them.  This allows compilers to
  41   //   share set-hi instructions between addresses that differ by a small
  42   //   offset (e.g., different static variables in the same class).
  43   //   On such machines, the "x" argument to set_value on all set-lo
  44   //   instructions must be the same as the "x" argument for the
  45   //   corresponding set-hi instructions.  The "o" arguments for the
  46   //   set-hi instructions are ignored, and must not affect the high-half
  47   //   immediate constant.  The "o" arguments for the set-lo instructions are
  48   //   added into the low-half immediate constant, and must not overflow it."
  49   //
  50   // Currently we don't support splitting of relocations, so o must be
  51   // zero:
  52   assert(o == 0, "tried to split relocations");
  53 
  54   if (!verify_only) {
  55     if (format() != 1) {
  56       nativeMovConstReg_at(addr())->set_data_plain(((intptr_t)x), code());
  57     } else {
  58       assert(type() == relocInfo::oop_type || type() == relocInfo::metadata_type,
  59              "how to encode else?");
  60       narrowOop no = (type() == relocInfo::oop_type) ?
  61         oopDesc::encode_heap_oop((oop)x) : oopDesc::encode_klass((Klass*)x);
  62       nativeMovConstReg_at(addr())->set_narrow_oop(no, code());
  63     }
  64   } else {
  65     assert((address) (nativeMovConstReg_at(addr())->data()) == x, "data must match");
  66   }
  67 }
  68 
  69 address Relocation::pd_call_destination(address orig_addr) {
  70   intptr_t adj = 0;
  71   address inst_loc = addr();
  72 
  73   if (orig_addr != NULL) {
  74     // We just moved this call instruction from orig_addr to addr().
  75     // This means its target will appear to have grown by addr() - orig_addr.
  76     adj = -(inst_loc - orig_addr);
  77   }
  78   if (NativeFarCall::is_far_call_at(inst_loc)) {
  79     NativeFarCall* call = nativeFarCall_at(inst_loc);
  80     return call->destination() + (intptr_t)(call->is_pcrelative() ? adj : 0);
  81   } else if (NativeJump::is_jump_at(inst_loc)) {
  82     NativeJump* jump = nativeJump_at(inst_loc);
  83     return jump->jump_destination() + (intptr_t)(jump->is_pcrelative() ? adj : 0);
  84   } else if (NativeConditionalFarBranch::is_conditional_far_branch_at(inst_loc)) {
  85     NativeConditionalFarBranch* branch = NativeConditionalFarBranch_at(inst_loc);
  86     return branch->branch_destination();
  87   } else {
  88     // There are two instructions at the beginning of a stub, therefore we
  89     // load at orig_addr + 8.
  90     orig_addr = nativeCall_at(inst_loc)->get_trampoline();
  91     if (orig_addr == NULL) {
  92       return (address) -1;
  93     } else {
  94       return (address) nativeMovConstReg_at(orig_addr + 8)->data();
  95     }
  96   }
  97 }
  98 
  99 void Relocation::pd_set_call_destination(address x) {
 100   address inst_loc = addr();
 101 
 102   if (NativeFarCall::is_far_call_at(inst_loc)) {
 103     NativeFarCall* call = nativeFarCall_at(inst_loc);
 104     call->set_destination(x);
 105   } else if (NativeJump::is_jump_at(inst_loc)) {
 106     NativeJump* jump= nativeJump_at(inst_loc);
 107     jump->set_jump_destination(x);
 108   } else if (NativeConditionalFarBranch::is_conditional_far_branch_at(inst_loc)) {
 109     NativeConditionalFarBranch* branch = NativeConditionalFarBranch_at(inst_loc);
 110     branch->set_branch_destination(x);
 111   } else {
 112     NativeCall* call = nativeCall_at(inst_loc);
 113     call->set_destination_mt_safe(x, false);
 114   }
 115 }
 116 
 117 address* Relocation::pd_address_in_code() {
 118   ShouldNotReachHere();
 119   return 0;
 120 }
 121 
 122 address Relocation::pd_get_address_from_code() {
 123   return (address)(nativeMovConstReg_at(addr())->data());
 124 }
 125 
 126 void poll_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
 127 }
 128 
 129 void poll_return_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
 130 }
 131 
 132 void metadata_Relocation::pd_fix_value(address x) {
 133 }