1 /*
   2  * Copyright (c) 2006, 2014, 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 #ifndef SHARE_VM_OPTO_OPTOREG_HPP
  26 #define SHARE_VM_OPTO_OPTOREG_HPP
  27 
  28 // AdGlobals contains c2 specific register handling code as specified
  29 // in the .ad files.
  30 #ifdef TARGET_ARCH_MODEL_x86_32
  31 # include "adfiles/adGlobals_x86_32.hpp"
  32 #endif
  33 #ifdef TARGET_ARCH_MODEL_x86_64
  34 # include "adfiles/adGlobals_x86_64.hpp"
  35 #endif
  36 #ifdef TARGET_ARCH_MODEL_sparc
  37 # include "adfiles/adGlobals_sparc.hpp"
  38 #endif
  39 #ifdef TARGET_ARCH_MODEL_zero
  40 # include "adfiles/adGlobals_zero.hpp"
  41 #endif
  42 #ifdef TARGET_ARCH_MODEL_arm
  43 # include "adfiles/adGlobals_arm.hpp"
  44 #endif
  45 #ifdef TARGET_ARCH_MODEL_ppc_32
  46 # include "adfiles/adGlobals_ppc_32.hpp"
  47 #endif
  48 #ifdef TARGET_ARCH_MODEL_ppc_64
  49 # include "adfiles/adGlobals_ppc_64.hpp"
  50 #endif
  51 #ifdef TARGET_ARCH_MODEL_aarch64
  52 # include "adfiles/adGlobals_aarch64.hpp"
  53 #endif
  54 
  55 //------------------------------OptoReg----------------------------------------
  56 // We eventually need Registers for the Real World.  Registers are essentially
  57 // non-SSA names.  A Register is represented as a number.  Non-regular values
  58 // (e.g., Control, Memory, I/O) use the Special register.  The actual machine
  59 // registers (as described in the ADL file for a machine) start at zero.
  60 // Stack-slots (spill locations) start at the nest Chunk past the last machine
  61 // register.
  62 //
  63 // Note that stack spill-slots are treated as a very large register set.
  64 // They have all the correct properties for a Register: not aliased (unique
  65 // named).  There is some simple mapping from a stack-slot register number
  66 // to the actual location on the stack; this mapping depends on the calling
  67 // conventions and is described in the ADL.
  68 //
  69 // Note that Name is not enum. C++ standard defines that the range of enum
  70 // is the range of smallest bit-field that can represent all enumerators
  71 // declared in the enum. The result of assigning a value to enum is undefined
  72 // if the value is outside the enumeration's valid range. OptoReg::Name is
  73 // typedef'ed as int, because it needs to be able to represent spill-slots.
  74 //
  75 class OptoReg VALUE_OBJ_CLASS_SPEC {
  76 
  77  friend class C2Compiler;
  78  public:
  79   typedef int Name;
  80   enum {
  81     // Chunk 0
  82     Physical = AdlcVMDeps::Physical, // Start of physical regs
  83     // A few oddballs at the edge of the world
  84     Special = -2,               // All special (not allocated) values
  85     Bad = -1                    // Not a register
  86   };
  87 
  88  private:
  89 
  90  static const VMReg opto2vm[REG_COUNT];
  91  static Name vm2opto[ConcreteRegisterImpl::number_of_registers];
  92 
  93  public:
  94 
  95   // Stack pointer register
  96   static OptoReg::Name c_frame_pointer;
  97 
  98 
  99 
 100   // Increment a register number.  As in:
 101   //    "for ( OptoReg::Name i; i=Control; i = add(i,1) ) ..."
 102   static Name add( Name x, int y ) { return Name(x+y); }
 103 
 104   // (We would like to have an operator+ for RegName, but it is not
 105   // a class, so this would be illegal in C++.)
 106 
 107   static void dump(int, outputStream *st = tty);
 108 
 109   // Get the stack slot number of an OptoReg::Name
 110   static unsigned int reg2stack( OptoReg::Name r) {
 111     assert( r >= stack0(), " must be");
 112     return r - stack0();
 113   }
 114 
 115   // convert a stack slot number into an OptoReg::Name
 116   static OptoReg::Name stack2reg( int idx) {
 117     return Name(stack0() + idx);
 118   }
 119 
 120   static bool is_stack(Name n) {
 121     return n >= stack0();
 122   }
 123 
 124   static bool is_valid(Name n) {
 125     return (n != Bad);
 126   }
 127 
 128   static bool is_reg(Name n) {
 129     return  is_valid(n) && !is_stack(n);
 130   }
 131 
 132   static VMReg as_VMReg(OptoReg::Name n) {
 133     if (is_reg(n)) {
 134       // Must use table, it'd be nice if Bad was indexable...
 135       return opto2vm[n];
 136     } else {
 137       assert(!is_stack(n), "must un warp");
 138       return VMRegImpl::Bad();
 139     }
 140   }
 141 
 142   // Can un-warp a stack slot or convert a register or Bad
 143   static VMReg as_VMReg(OptoReg::Name n, int frame_size, int arg_count) {
 144     if (is_reg(n)) {
 145       // Must use table, it'd be nice if Bad was indexable...
 146       return opto2vm[n];
 147     } else if (is_stack(n)) {
 148       int stack_slot = reg2stack(n);
 149       if (stack_slot < arg_count) {
 150         return VMRegImpl::stack2reg(stack_slot + frame_size);
 151       }
 152       return VMRegImpl::stack2reg(stack_slot - arg_count);
 153       // return return VMRegImpl::stack2reg(reg2stack(OptoReg::add(n, -arg_count)));
 154     } else {
 155       return VMRegImpl::Bad();
 156     }
 157   }
 158 
 159   static OptoReg::Name as_OptoReg(VMReg r) {
 160     if (r->is_stack()) {
 161       assert(false, "must warp");
 162       return stack2reg(r->reg2stack());
 163     } else if (r->is_valid()) {
 164       // Must use table, it'd be nice if Bad was indexable...
 165       return vm2opto[r->value()];
 166     } else {
 167       return Bad;
 168     }
 169   }
 170 
 171   static OptoReg::Name stack0() {
 172     return VMRegImpl::stack0->value();
 173   }
 174 
 175   static const char* regname(OptoReg::Name n) {
 176     return as_VMReg(n)->name();
 177   }
 178 
 179 };
 180 
 181 //---------------------------OptoRegPair-------------------------------------------
 182 // Pairs of 32-bit registers for the allocator.
 183 // This is a very similar class to VMRegPair. C2 only interfaces with VMRegPair
 184 // via the calling convention code which is shared between the compilers.
 185 // Since C2 uses OptoRegs for register allocation it is more efficient to use
 186 // VMRegPair internally for nodes that can contain a pair of OptoRegs rather
 187 // than use VMRegPair and continually be converting back and forth. So normally
 188 // C2 will take in a VMRegPair from the calling convention code and immediately
 189 // convert them to an OptoRegPair and stay in the OptoReg world. The only over
 190 // conversion between OptoRegs and VMRegs is for debug info and oopMaps. This
 191 // is not a high bandwidth spot and so it is not an issue.
 192 // Note that onde other consequence of staying in the OptoReg world with OptoRegPairs
 193 // is that there are "physical" OptoRegs that are not representable in the VMReg
 194 // world, notably flags. [ But by design there is "space" in the VMReg world
 195 // for such registers they just may not be concrete ]. So if we were to use VMRegPair
 196 // then the VMReg world would have to have a representation for these registers
 197 // so that a OptoReg->VMReg->OptoReg would reproduce ther original OptoReg. As it
 198 // stands if you convert a flag (condition code) to a VMReg you will get VMRegImpl::Bad
 199 // and converting that will return OptoReg::Bad losing the identity of the OptoReg.
 200 
 201 class OptoRegPair {
 202   friend class VMStructs;
 203 private:
 204   short _second;
 205   short _first;
 206 public:
 207   void set_bad (                   ) { _second = OptoReg::Bad; _first = OptoReg::Bad; }
 208   void set1    ( OptoReg::Name n  ) { _second = OptoReg::Bad; _first = n; }
 209   void set2    ( OptoReg::Name n  ) { _second = n + 1;       _first = n; }
 210   void set_pair( OptoReg::Name second, OptoReg::Name first    ) { _second= second;    _first= first; }
 211   void set_ptr ( OptoReg::Name ptr ) {
 212 #ifdef _LP64
 213     _second = ptr+1;
 214 #else
 215     _second = OptoReg::Bad;
 216 #endif
 217     _first = ptr;
 218   }
 219 
 220   OptoReg::Name second() const { return _second; }
 221   OptoReg::Name first() const { return _first; }
 222   OptoRegPair(OptoReg::Name second, OptoReg::Name first) {  _second = second; _first = first; }
 223   OptoRegPair(OptoReg::Name f) { _second = OptoReg::Bad; _first = f; }
 224   OptoRegPair() { _second = OptoReg::Bad; _first = OptoReg::Bad; }
 225 };
 226 
 227 #endif // SHARE_VM_OPTO_OPTOREG_HPP