duke@435: /* never@3138: * Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: stefank@2314: #ifndef SHARE_VM_OPTO_OPTOREG_HPP stefank@2314: #define SHARE_VM_OPTO_OPTOREG_HPP stefank@2314: duke@435: //------------------------------OptoReg---------------------------------------- duke@435: // We eventually need Registers for the Real World. Registers are essentially duke@435: // non-SSA names. A Register is represented as a number. Non-regular values duke@435: // (e.g., Control, Memory, I/O) use the Special register. The actual machine duke@435: // registers (as described in the ADL file for a machine) start at zero. duke@435: // Stack-slots (spill locations) start at the nest Chunk past the last machine duke@435: // register. duke@435: // duke@435: // Note that stack spill-slots are treated as a very large register set. duke@435: // They have all the correct properties for a Register: not aliased (unique duke@435: // named). There is some simple mapping from a stack-slot register number duke@435: // to the actual location on the stack; this mapping depends on the calling duke@435: // conventions and is described in the ADL. duke@435: // duke@435: // Note that Name is not enum. C++ standard defines that the range of enum duke@435: // is the range of smallest bit-field that can represent all enumerators duke@435: // declared in the enum. The result of assigning a value to enum is undefined duke@435: // if the value is outside the enumeration's valid range. OptoReg::Name is duke@435: // typedef'ed as int, because it needs to be able to represent spill-slots. duke@435: // duke@435: class OptoReg VALUE_OBJ_CLASS_SPEC { duke@435: duke@435: friend class C2Compiler; duke@435: public: duke@435: typedef int Name; duke@435: enum { duke@435: // Chunk 0 duke@435: Physical = AdlcVMDeps::Physical, // Start of physical regs duke@435: // A few oddballs at the edge of the world duke@435: Special = -2, // All special (not allocated) values duke@435: Bad = -1 // Not a register duke@435: }; duke@435: duke@435: private: duke@435: duke@435: static const VMReg opto2vm[REG_COUNT]; duke@435: static Name vm2opto[ConcreteRegisterImpl::number_of_registers]; duke@435: duke@435: public: duke@435: duke@435: // Stack pointer register duke@435: static OptoReg::Name c_frame_pointer; duke@435: duke@435: duke@435: duke@435: // Increment a register number. As in: duke@435: // "for ( OptoReg::Name i; i=Control; i = add(i,1) ) ..." duke@435: static Name add( Name x, int y ) { return Name(x+y); } duke@435: duke@435: // (We would like to have an operator+ for RegName, but it is not duke@435: // a class, so this would be illegal in C++.) duke@435: kvn@4478: static void dump(int, outputStream *st = tty); duke@435: duke@435: // Get the stack slot number of an OptoReg::Name duke@435: static unsigned int reg2stack( OptoReg::Name r) { duke@435: assert( r >= stack0(), " must be"); duke@435: return r - stack0(); duke@435: } duke@435: duke@435: // convert a stack slot number into an OptoReg::Name duke@435: static OptoReg::Name stack2reg( int idx) { duke@435: return Name(stack0() + idx); duke@435: } duke@435: duke@435: static bool is_stack(Name n) { duke@435: return n >= stack0(); duke@435: } duke@435: duke@435: static bool is_valid(Name n) { duke@435: return (n != Bad); duke@435: } duke@435: duke@435: static bool is_reg(Name n) { duke@435: return is_valid(n) && !is_stack(n); duke@435: } duke@435: duke@435: static VMReg as_VMReg(OptoReg::Name n) { duke@435: if (is_reg(n)) { duke@435: // Must use table, it'd be nice if Bad was indexable... duke@435: return opto2vm[n]; duke@435: } else { duke@435: assert(!is_stack(n), "must un warp"); duke@435: return VMRegImpl::Bad(); duke@435: } duke@435: } duke@435: duke@435: // Can un-warp a stack slot or convert a register or Bad duke@435: static VMReg as_VMReg(OptoReg::Name n, int frame_size, int arg_count) { duke@435: if (is_reg(n)) { duke@435: // Must use table, it'd be nice if Bad was indexable... duke@435: return opto2vm[n]; duke@435: } else if (is_stack(n)) { duke@435: int stack_slot = reg2stack(n); duke@435: if (stack_slot < arg_count) { duke@435: return VMRegImpl::stack2reg(stack_slot + frame_size); duke@435: } duke@435: return VMRegImpl::stack2reg(stack_slot - arg_count); duke@435: // return return VMRegImpl::stack2reg(reg2stack(OptoReg::add(n, -arg_count))); duke@435: } else { duke@435: return VMRegImpl::Bad(); duke@435: } duke@435: } duke@435: duke@435: static OptoReg::Name as_OptoReg(VMReg r) { duke@435: if (r->is_stack()) { duke@435: assert(false, "must warp"); duke@435: return stack2reg(r->reg2stack()); duke@435: } else if (r->is_valid()) { duke@435: // Must use table, it'd be nice if Bad was indexable... duke@435: return vm2opto[r->value()]; duke@435: } else { duke@435: return Bad; duke@435: } duke@435: } duke@435: duke@435: static OptoReg::Name stack0() { duke@435: return VMRegImpl::stack0->value(); duke@435: } duke@435: duke@435: static const char* regname(OptoReg::Name n) { duke@435: return as_VMReg(n)->name(); duke@435: } duke@435: duke@435: }; duke@435: duke@435: //---------------------------OptoRegPair------------------------------------------- duke@435: // Pairs of 32-bit registers for the allocator. duke@435: // This is a very similar class to VMRegPair. C2 only interfaces with VMRegPair duke@435: // via the calling convention code which is shared between the compilers. duke@435: // Since C2 uses OptoRegs for register allocation it is more efficient to use duke@435: // VMRegPair internally for nodes that can contain a pair of OptoRegs rather duke@435: // than use VMRegPair and continually be converting back and forth. So normally duke@435: // C2 will take in a VMRegPair from the calling convention code and immediately duke@435: // convert them to an OptoRegPair and stay in the OptoReg world. The only over duke@435: // conversion between OptoRegs and VMRegs is for debug info and oopMaps. This duke@435: // is not a high bandwidth spot and so it is not an issue. duke@435: // Note that onde other consequence of staying in the OptoReg world with OptoRegPairs duke@435: // is that there are "physical" OptoRegs that are not representable in the VMReg duke@435: // world, notably flags. [ But by design there is "space" in the VMReg world duke@435: // for such registers they just may not be concrete ]. So if we were to use VMRegPair duke@435: // then the VMReg world would have to have a representation for these registers duke@435: // so that a OptoReg->VMReg->OptoReg would reproduce ther original OptoReg. As it duke@435: // stands if you convert a flag (condition code) to a VMReg you will get VMRegImpl::Bad duke@435: // and converting that will return OptoReg::Bad losing the identity of the OptoReg. duke@435: duke@435: class OptoRegPair { never@3138: friend class VMStructs; duke@435: private: duke@435: short _second; duke@435: short _first; duke@435: public: duke@435: void set_bad ( ) { _second = OptoReg::Bad; _first = OptoReg::Bad; } duke@435: void set1 ( OptoReg::Name n ) { _second = OptoReg::Bad; _first = n; } duke@435: void set2 ( OptoReg::Name n ) { _second = n + 1; _first = n; } duke@435: void set_pair( OptoReg::Name second, OptoReg::Name first ) { _second= second; _first= first; } duke@435: void set_ptr ( OptoReg::Name ptr ) { duke@435: #ifdef _LP64 duke@435: _second = ptr+1; duke@435: #else duke@435: _second = OptoReg::Bad; duke@435: #endif duke@435: _first = ptr; duke@435: } duke@435: duke@435: OptoReg::Name second() const { return _second; } duke@435: OptoReg::Name first() const { return _first; } duke@435: OptoRegPair(OptoReg::Name second, OptoReg::Name first) { _second = second; _first = first; } duke@435: OptoRegPair(OptoReg::Name f) { _second = OptoReg::Bad; _first = f; } duke@435: OptoRegPair() { _second = OptoReg::Bad; _first = OptoReg::Bad; } duke@435: }; stefank@2314: stefank@2314: #endif // SHARE_VM_OPTO_OPTOREG_HPP