1 /*
   2  * Copyright 1997-2009 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  *
  23  */
  24 
  25 # include "incls/_precompiled.incl"
  26 # include "incls/_stackValue.cpp.incl"
  27 
  28 StackValue* StackValue::create_stack_value(const frame* fr, const RegisterMap* reg_map, ScopeValue* sv) {
  29   if (sv->is_location()) {
  30     // Stack or register value
  31     Location loc = ((LocationValue *)sv)->location();
  32 
  33 #ifdef SPARC
  34     // %%%%% Callee-save floats will NOT be working on a Sparc until we
  35     // handle the case of a 2 floats in a single double register.
  36     assert( !(loc.is_register() && loc.type() == Location::float_in_dbl), "Sparc does not handle callee-save floats yet" );
  37 #endif // SPARC
  38 
  39     // First find address of value
  40 
  41     address value_addr = loc.is_register()
  42       // Value was in a callee-save register
  43       ? reg_map->location(VMRegImpl::as_VMReg(loc.register_number()))
  44       // Else value was directly saved on the stack. The frame's original stack pointer,
  45       // before any extension by its callee (due to Compiler1 linkage on SPARC), must be used.
  46       : ((address)fr->unextended_sp()) + loc.stack_offset();
  47 
  48     // Then package it right depending on type
  49     // Note: the transfer of the data is thru a union that contains
  50     // an intptr_t. This is because an interpreter stack slot is
  51     // really an intptr_t. The use of a union containing an intptr_t
  52     // ensures that on a 64 bit platform we have proper alignment
  53     // and that we store the value where the interpreter will expect
  54     // to find it (i.e. proper endian). Similarly on a 32bit platform
  55     // using the intptr_t ensures that when a value is larger than
  56     // a stack slot (jlong/jdouble) that we capture the proper part
  57     // of the value for the stack slot in question.
  58     //
  59     switch( loc.type() ) {
  60     case Location::float_in_dbl: { // Holds a float in a double register?
  61       assert( loc.is_register(), "floats always saved to stack in 1 word" );
  62       // Call platform specific function defined in sharedRuntime_<arch>.cpp.
  63       return create_float_stack_value(value_addr);
  64     }
  65     case Location::int_in_long: { // Holds an int in a long register?
  66       // The callee has no clue whether the register holds an int,
  67       // long or is unused.  He always saves a long.  Here we know
  68       // a long was saved, but we only want an int back.  Narrow the
  69       // saved long to the int that the JVM wants.
  70       assert( loc.is_register(), "ints always saved to stack in 1 word" );
  71       union { intptr_t p; jint ji;} value;
  72       value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
  73       value.ji = (jint) *(jlong*) value_addr;
  74       return new StackValue(value.p); // 64-bit high half is stack junk
  75     }
  76 #ifdef _LP64
  77     case Location::dbl:
  78       // Double value in an aligned adjacent pair
  79       return new StackValue(*(intptr_t*)value_addr);
  80     case Location::lng:
  81       // Long   value in an aligned adjacent pair
  82       return new StackValue(*(intptr_t*)value_addr);
  83     case Location::narrowoop: {
  84       union { intptr_t p; narrowOop noop;} value;
  85       value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
  86       if (loc.is_register()) {
  87         // The callee has no clue whether the register holds an int,
  88         // long or is unused.  He always saves a long.  Here we know
  89         // a long was saved, but we only want an int back.  Narrow the
  90         // saved long to the int that the JVM wants.
  91         value.noop =  (narrowOop) *(julong*) value_addr;
  92       } else {
  93         value.noop = *(narrowOop*) value_addr;
  94       }
  95       // Decode narrowoop and wrap a handle around the oop
  96       Handle h(oopDesc::decode_heap_oop(value.noop));
  97       return new StackValue(h);
  98     }
  99 #endif
 100     case Location::oop: {
 101       oop val = *(oop *)value_addr;
 102 #ifdef _LP64
 103       if (Universe::is_narrow_oop_base(val)) {
 104          // Compiled code may produce decoded oop = narrow_oop_base
 105          // when a narrow oop implicit null check is used.
 106          // The narrow_oop_base could be NULL or be the address
 107          // of the page below heap. Use NULL value for both cases.
 108          val = (oop)NULL;
 109       }
 110 #endif
 111       Handle h(val); // Wrap a handle around the oop
 112       return new StackValue(h);
 113     }
 114     case Location::addr: {
 115       ShouldNotReachHere(); // both C1 and C2 now inline jsrs
 116     }
 117     case Location::normal: {
 118       // Just copy all other bits straight through
 119       union { intptr_t p; jint ji;} value;
 120       value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
 121       value.ji = *(jint*)value_addr;
 122       return new StackValue(value.p);
 123     }
 124     case Location::invalid:
 125       return new StackValue();
 126     default:
 127       ShouldNotReachHere();
 128     }
 129 
 130   } else if (sv->is_constant_int()) {
 131     // Constant int: treat same as register int.
 132     union { intptr_t p; jint ji;} value;
 133     value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
 134     value.ji = (jint)((ConstantIntValue*)sv)->value();
 135     return new StackValue(value.p);
 136   } else if (sv->is_constant_oop()) {
 137     // constant oop
 138     return new StackValue(((ConstantOopReadValue *)sv)->value());
 139 #ifdef _LP64
 140   } else if (sv->is_constant_double()) {
 141     // Constant double in a single stack slot
 142     union { intptr_t p; double d; } value;
 143     value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
 144     value.d = ((ConstantDoubleValue *)sv)->value();
 145     return new StackValue(value.p);
 146   } else if (sv->is_constant_long()) {
 147     // Constant long in a single stack slot
 148     union { intptr_t p; jlong jl; } value;
 149     value.p = (intptr_t) CONST64(0xDEADDEAFDEADDEAF);
 150     value.jl = ((ConstantLongValue *)sv)->value();
 151     return new StackValue(value.p);
 152 #endif
 153   } else if (sv->is_object()) { // Scalar replaced object in compiled frame
 154     Handle ov = ((ObjectValue *)sv)->value();
 155     return new StackValue(ov, (ov.is_null()) ? 1 : 0);
 156   }
 157 
 158   // Unknown ScopeValue type
 159   ShouldNotReachHere();
 160   return new StackValue((intptr_t) 0);   // dummy
 161 }
 162 
 163 
 164 BasicLock* StackValue::resolve_monitor_lock(const frame* fr, Location location) {
 165   assert(location.is_stack(), "for now we only look at the stack");
 166   int word_offset = location.stack_offset() / wordSize;
 167   // (stack picture)
 168   // high: [     ]  word_offset + 1
 169   // low   [     ]  word_offset
 170   //
 171   // sp->  [     ]  0
 172   // the word_offset is the distance from the stack pointer to the lowest address
 173   // The frame's original stack pointer, before any extension by its callee
 174   // (due to Compiler1 linkage on SPARC), must be used.
 175   return (BasicLock*) (fr->unextended_sp() + word_offset);
 176 }
 177 
 178 
 179 #ifndef PRODUCT
 180 
 181 void StackValue::print_on(outputStream* st) const {
 182   switch(_type) {
 183     case T_INT:
 184       st->print("%d (int) %f (float) %x (hex)",  *(int *)&_i, *(float *)&_i,  *(int *)&_i);
 185       break;
 186 
 187     case T_OBJECT:
 188      _o()->print_value_on(st);
 189       st->print(" <" INTPTR_FORMAT ">", (address)_o());
 190      break;
 191 
 192     case T_CONFLICT:
 193      st->print("conflict");
 194      break;
 195 
 196     default:
 197      ShouldNotReachHere();
 198   }
 199 }
 200 
 201 #endif