1 #ifdef USE_PRAGMA_IDENT_HDR
   2 #pragma ident "@(#)vmreg.hpp    1.37 07/05/05 17:05:22 JVM"
   3 #endif
   4 /*
   5  * Copyright 1998-2007 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 //------------------------------VMReg------------------------------------------
  29 // The VM uses 'unwarped' stack slots; the compiler uses 'warped' stack slots.
  30 // Register numbers below VMRegImpl::stack0 are the same for both.  Register
  31 // numbers above stack0 are either warped (in the compiler) or unwarped
  32 // (in the VM).  Unwarped numbers represent stack indices, offsets from
  33 // the current stack pointer.  Warped numbers are required during compilation
  34 // when we do not yet know how big the frame will be.
  35 
  36 class VMRegImpl;
  37 typedef VMRegImpl* VMReg;
  38 
  39 class VMRegImpl { 
  40 // friend class OopMap;
  41 friend class VMStructs;
  42 friend class OptoReg;
  43 // friend class Location;
  44 private:
  45   enum {
  46     BAD = -1
  47   };
  48 
  49 
  50 
  51   static VMReg stack0;
  52   // Names for registers
  53   static const char *regName[];
  54   static const int register_count;
  55 
  56 
  57 public:
  58 
  59   static VMReg  as_VMReg(int val, bool bad_ok = false) { assert(val > BAD || bad_ok, "invalid"); return (VMReg) (intptr_t) val; }
  60 
  61   const char*  name() {
  62     if (is_reg()) {
  63       return regName[value()];
  64     } else if (!is_valid()) {
  65       return "BAD";
  66     } else {
  67       // shouldn't really be called with stack
  68       return "STACKED REG";
  69     }
  70   }
  71   static VMReg Bad() { return (VMReg) (intptr_t) BAD; }
  72   bool is_valid() { return ((intptr_t) this) != BAD; }
  73   bool is_stack() { return (intptr_t) this >= (intptr_t) stack0; }
  74   bool is_reg() { return is_valid() && !is_stack(); }
  75 
  76   // A concrete register is a value that returns true for is_reg() and is
  77   // also a register you could use in the assembler. On machines with
  78   // 64bit registers only one half of the VMReg (and OptoReg) is considered
  79   // concrete. 
  80   bool is_concrete();
  81 
  82   // VMRegs are 4 bytes wide on all platforms
  83   static const int stack_slot_size;
  84   static const int slots_per_word;
  85 
  86 
  87   // This really ought to check that the register is "real" in the sense that
  88   // we don't try and get the VMReg number of a physical register that doesn't
  89   // have an expressible part. That would be pd specific code
  90   VMReg next() {
  91     assert((is_reg() && value() < stack0->value() - 1) || is_stack(), "must be");
  92     return (VMReg)(intptr_t)(value() + 1);
  93   }
  94   VMReg prev() {
  95     assert((is_stack() && value() > stack0->value()) || (is_reg() && value() != 0), "must be");
  96     return (VMReg)(intptr_t)(value() - 1);
  97   }
  98 
  99 
 100   intptr_t value() const         {return (intptr_t) this; }
 101 
 102   void print();
 103 
 104   // bias a stack slot.
 105   // Typically used to adjust a virtual frame slots by amounts that are offset by
 106   // amounts that are part of the native abi. The VMReg must be a stack slot
 107   // and the result must be also.
 108 
 109   VMReg bias(int offset) {
 110     assert(is_stack(), "must be");
 111     // VMReg res = VMRegImpl::as_VMReg(value() + offset);
 112     VMReg res = stack2reg(reg2stack() + offset);
 113     assert(res->is_stack(), "must be");
 114     return res;
 115   }
 116 
 117   // Convert register numbers to stack slots and vice versa
 118   static VMReg stack2reg( int idx ) { 
 119     return (VMReg) (intptr_t) (stack0->value() + idx);
 120   }
 121 
 122   uintptr_t reg2stack() {
 123     assert( is_stack(), "Not a stack-based register" );
 124     return value() - stack0->value(); 
 125   }
 126 
 127   static void set_regName();
 128 
 129 #include "incls/_vmreg_pd.hpp.incl"
 130   
 131 };
 132 
 133 //---------------------------VMRegPair-------------------------------------------
 134 // Pairs of 32-bit registers for arguments.
 135 // SharedRuntime::java_calling_convention will overwrite the structs with
 136 // the calling convention's registers.  VMRegImpl::Bad is returned for any
 137 // unused 32-bit register.  This happens for the unused high half of Int
 138 // arguments, or for 32-bit pointers or for longs in the 32-bit sparc build
 139 // (which are passed to natives in low 32-bits of e.g. O0/O1 and the high
 140 // 32-bits of O0/O1 are set to VMRegImpl::Bad).  Longs in one register & doubles
 141 // always return a high and a low register, as do 64-bit pointers.
 142 //
 143 class VMRegPair {
 144 private:
 145   VMReg _second;
 146   VMReg _first;
 147 public:
 148   void set_bad (                   ) { _second=VMRegImpl::Bad(); _first=VMRegImpl::Bad(); }
 149   void set1    (         VMReg v  ) { _second=VMRegImpl::Bad(); _first=v; }
 150   void set2    (         VMReg v  ) { _second=v->next();  _first=v; }
 151   void set_pair( VMReg second, VMReg first    ) { _second= second;    _first= first; }
 152   void set_ptr ( VMReg ptr ) {
 153 #ifdef _LP64
 154     _second = ptr->next();
 155 #else
 156     _second = VMRegImpl::Bad();
 157 #endif
 158     _first = ptr;
 159   }
 160   // Return true if single register, even if the pair is really just adjacent stack slots
 161   bool is_single_reg() {
 162     return (_first->is_valid()) && (_first->value() + 1 == _second->value());
 163   }
 164 
 165   // Return true if single stack based "register" where the slot alignment matches input alignment
 166   bool is_adjacent_on_stack(int alignment) {
 167     return (_first->is_stack() && (_first->value() + 1 == _second->value()) && ((_first->value() & (alignment-1)) == 0));
 168   }
 169 
 170   // Return true if single stack based "register" where the slot alignment matches input alignment
 171   bool is_adjacent_aligned_on_stack(int alignment) {
 172     return (_first->is_stack() && (_first->value() + 1 == _second->value()) && ((_first->value() & (alignment-1)) == 0));
 173   }
 174 
 175   // Return true if single register but adjacent stack slots do not count
 176   bool is_single_phys_reg() {
 177     return (_first->is_reg() && (_first->value() + 1 == _second->value()));
 178   }
 179 
 180   VMReg second() const { return _second; }
 181   VMReg first()  const { return _first; }
 182   VMRegPair(VMReg s, VMReg f) {  _second = s; _first = f; }
 183   VMRegPair(VMReg f) { _second = VMRegImpl::Bad(); _first = f; }
 184   VMRegPair() { _second = VMRegImpl::Bad(); _first = VMRegImpl::Bad(); }
 185 };