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 #unckyde "oops/klass.inline.hpp"
  32 #include "oops/oop.inline.hpp"
  33 #include "runtime/safepoint.hpp"
  34 
  35 void Relocation::pd_set_data_value(address x, intptr_t o, bool verify_only) {
  36   bool copy_back_to_oop_pool = true;  // TODO: PPC port
  37   // The following comment is from the declaration of DataRelocation:
  38   //
  39   //  "The "o" (displacement) argument is relevant only to split relocations
  40   //   on RISC machines.  In some CPUs (SPARC), the set-hi and set-lo ins'ns
  41   //   can encode more than 32 bits between them.  This allows compilers to
  42   //   share set-hi instructions between addresses that differ by a small
  43   //   offset (e.g., different static variables in the same class).
  44   //   On such machines, the "x" argument to set_value on all set-lo
  45   //   instructions must be the same as the "x" argument for the
  46   //   corresponding set-hi instructions.  The "o" arguments for the
  47   //   set-hi instructions are ignored, and must not affect the high-half
  48   //   immediate constant.  The "o" arguments for the set-lo instructions are
  49   //   added into the low-half immediate constant, and must not overflow it."
  50   //
  51   // Currently we don't support splitting of relocations, so o must be
  52   // zero:
  53   assert(o == 0, "tried to split relocations");
  54 
  55   if (!verify_only) {
  56     if (format() != 1) {
  57       nativeMovConstReg_at(addr())->set_data_plain(((intptr_t)x), code());
  58     } else {
  59       assert(type() == relocInfo::oop_type || type() == relocInfo::metadata_type,
  60              "how to encode else?");
  61       narrowOop no = (type() == relocInfo::oop_type) ?
  62         oopDesc::encode_heap_oop((oop)x) : Klass::encode_klass((Klass*)x);
  63       nativeMovConstReg_at(addr())->set_narrow_oop(no, code());
  64     }
  65   } else {
  66     assert((address) (nativeMovConstReg_at(addr())->data()) == x, "data must match");
  67   }
  68 }
  69 
  70 address Relocation::pd_call_destination(address orig_addr) {
  71   intptr_t adj = 0;
  72   address inst_loc = addr();
  73 
  74   if (orig_addr != NULL) {
  75     // We just moved this call instruction from orig_addr to addr().
  76     // This means its target will appear to have grown by addr() - orig_addr.
  77     adj = -(inst_loc - orig_addr);
  78   }
  79   if (NativeFarCall::is_far_call_at(inst_loc)) {
  80     NativeFarCall* call = nativeFarCall_at(inst_loc);
  81     return call->destination() + (intptr_t)(call->is_pcrelative() ? adj : 0);
  82   } else if (NativeJump::is_jump_at(inst_loc)) {
  83     NativeJump* jump = nativeJump_at(inst_loc);
  84     return jump->jump_destination() + (intptr_t)(jump->is_pcrelative() ? adj : 0);
  85   } else if (NativeConditionalFarBranch::is_conditional_far_branch_at(inst_loc)) {
  86     NativeConditionalFarBranch* branch = NativeConditionalFarBranch_at(inst_loc);
  87     return branch->branch_destination();
  88   } else {
  89     // There are two instructions at the beginning of a stub, therefore we
  90     // load at orig_addr + 8.
  91     orig_addr = nativeCall_at(inst_loc)->get_trampoline();
  92     if (orig_addr == NULL) {
  93       return (address) -1;
  94     } else {
  95       return (address) nativeMovConstReg_at(orig_addr + 8)->data();
  96     }
  97   }
  98 }
  99 
 100 void Relocation::pd_set_call_destination(address x) {
 101   address inst_loc = addr();
 102 
 103   if (NativeFarCall::is_far_call_at(inst_loc)) {
 104     NativeFarCall* call = nativeFarCall_at(inst_loc);
 105     call->set_destination(x);
 106   } else if (NativeJump::is_jump_at(inst_loc)) {
 107     NativeJump* jump= nativeJump_at(inst_loc);
 108     jump->set_jump_destination(x);
 109   } else if (NativeConditionalFarBranch::is_conditional_far_branch_at(inst_loc)) {
 110     NativeConditionalFarBranch* branch = NativeConditionalFarBranch_at(inst_loc);
 111     branch->set_branch_destination(x);
 112   } else {
 113     NativeCall* call = nativeCall_at(inst_loc);
 114     call->set_destination_mt_safe(x, false);
 115   }
 116 }
 117 
 118 address* Relocation::pd_address_in_code() {
 119   ShouldNotReachHere();
 120   return 0;
 121 }
 122 
 123 address Relocation::pd_get_address_from_code() {
 124   return (address)(nativeMovConstReg_at(addr())->data());
 125 }
 126 
 127 void poll_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
 128 }
 129 
 130 void poll_return_Relocation::fix_relocation_after_move(const CodeBuffer* src, CodeBuffer* dest) {
 131 }
 132 
 133 void metadata_Relocation::pd_fix_value(address x) {
 134 }