C-Breeze
C Compiler Infrastructure

[ Project home page]
Main Page   Modules   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

register.h

Go to the documentation of this file.
00001 // $Id: register.h,v 1.8 2003/08/11 17:38:53 abrown Exp $
00002 // ----------------------------------------------------------------------
00003 //
00004 //  C-Breeze
00005 //  C Compiler Framework
00006 // 
00007 //  Copyright (c) 2002 University of Texas at Austin
00008 // 
00009 //  Paul Arthur Navratil
00010 //  Charles Nevill
00011 //  Calvin Lin
00012 // 
00013 //  Permission is hereby granted, free of charge, to any person
00014 //  obtaining a copy of this software and associated documentation
00015 //  files (the "Software"), to deal in the Software without
00016 //  restriction, including without limitation the rights to use, copy,
00017 //  modify, merge, publish, distribute, sublicense, and/or sell copies
00018 //  of the Software, and to permit persons to whom the Software is
00019 //  furnished to do so, subject to the following conditions:
00020 //  
00021 //  The above copyright notice and this permission notice shall be
00022 //  included in all copies or substantial portions of the Software.
00023 //  
00024 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025 //  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026 //  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027 //  NONINFRINGEMENT.  IN NO EVENT SHALL THE UNIVERSITY OF TEXAS AT
00028 //  AUSTIN BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
00029 //  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
00030 //  OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00031 //  THE SOFTWARE.
00032 //
00033 //  We acknowledge the C-to-C Translator from MIT Laboratory for
00034 //  Computer Science for inspiring parts of the C-Breeze design.
00035 //
00036 // ----------------------------------------------------------------------
00037 
00038 #ifndef _REGISTER_H
00039 #define _REGISTER_H
00040 
00041 #include "constant.h"
00042 
00043 // types of registers
00044 // when assigning registers, we need to know whether it is integer (gpr), 
00045 // floating point, or address register (e.g. frame ptr, stack ptr).
00046 // if your machine has another type of register, add it here.
00047 // register type is updated through class Register, in LIR.cc
00048 enum Reg_type
00049 {
00050 
00051   reg_unknown,
00052   reg_gpr,
00053   reg_fpr,
00054   reg_stack_ptr,
00055   reg_frame_ptr
00056 };
00057 
00058 // Class Register encapsulates functions to be performed on registers.
00059 // Methods are all called from LIR.cc
00060 // Overloaded operators are used throughout the code generator to assign and increment reg#
00061 // The accessors are used when you need to directly compare the type or reg#
00062 // Instantiate class register whenever you need a register (source, destination, register
00063 // allocator)
00064 class Register
00065 {
00066   friend class LirInst;
00067 
00068 private:
00069 
00070   // the identifier of this register
00071   unsigned int _num;
00072 
00073   // the type of this register
00074   Reg_type _type;
00075 
00076 public:
00077 
00078   // default constructor
00079     Register (Reg_type type = reg_unknown)
00080   {
00081     _num = (unsigned int) -1;
00082     _type = type;
00083   }
00084 
00085   // set-everything constructor
00086   Register (unsigned int n, Reg_type rt)
00087   {
00088     _num = n;
00089     _type = rt;
00090   }
00091 
00092   // copy constructor
00093   Register (const Register & r)
00094   {
00095     // use assignment operator
00096     *this = r;
00097   }
00098 
00099   // accessors
00100   bool isValid () const
00101   {
00102     return (_type != reg_unknown);
00103   }
00104   unsigned int num () const
00105   {
00106     return _num;
00107   }
00108   void num (unsigned int n)
00109   {
00110     _num = n;
00111   }
00112   Reg_type type () const
00113   {
00114     return _type;
00115   }
00116   void type (Reg_type t)
00117   {
00118     _type = t;
00119   }
00120 
00121   // methods to change the type
00122   Register & makeInvalid ()
00123   {
00124     _type = reg_unknown;
00125     return *this;
00126   }
00127   Register & makeGPR ()
00128   {
00129     _type = reg_gpr;
00130     return *this;
00131   }
00132   Register & makeFPR ()
00133   {
00134     _type = reg_fpr;
00135     return *this;
00136   }
00137   Register & makeStackPtr ()
00138   {
00139     _type = reg_stack_ptr;
00140     _num = (unsigned int) -1;
00141     return *this;
00142   }
00143   Register & makeFramePtr ()
00144   {
00145     _type = reg_frame_ptr;
00146     _num = (unsigned int) -1;
00147     return *this;
00148   }
00149 
00150   // figure this thing's type out based on a given variable type
00151   void setType (typeNode * the_type);
00152 
00153   // assignment operators
00154   Register & operator= (const Register & rightR)
00155   {
00156     _type = rightR._type;
00157     _num = rightR._num;
00158     return *this;
00159   }
00160   Register & operator= (unsigned int val)
00161   {
00162     _num = val;
00163     return *this;
00164   }
00165 
00166   // compare for equality
00167   bool operator== (const Register & rightR) const
00168   {
00169     if ((_num == rightR._num) && (_type == rightR._type))
00170       return true;
00171     return false;
00172   }
00173 
00174   // compare for inequality
00175   bool operator!= (const Register & rightR) const
00176   {
00177     return ((_num != rightR._num) || (_type != rightR._type));
00178   }
00179 
00180   // compare for less/greater, so we can put this in a set (uses RB tree)
00181   bool operator < (const Register & rightR) const
00182   {
00183     // just use numbers
00184     return _num < rightR._num;
00185   }
00186 
00187   // to stringize this thing
00188   // wantPrefixAdd : add extra prefixes specified in arch spec
00189   // wantPrefixRemove : remove extra prefixes specified in arch spec
00190   string to_string (bool wantPrefixAdd = true, bool wantPrefixRemove =
00191                     false) const;
00192 
00193   // get special registers
00194   static Register getRegSp ();
00195   static Register getRegFp ();
00196   static Register getRegRetValFixed ();
00197   static Register getRegRetValFloat ();
00198 
00199   // find the right return value register for the given type, or return false 
00200   // if not possible
00201   static bool getRegRetVal (typeNode * the_type, Register & reg);
00202 
00203 };
00204 
00205 // a vector of registers
00206 typedef vector < Register > register_vector;
00207 typedef
00208   register_vector::iterator
00209   register_vector_p;
00210 
00211 // a set of register ids
00212 typedef
00213 hash_set_ex < unsigned int >
00214   reg_id_set;
00215 typedef
00216   reg_id_set::iterator
00217   reg_id_set_p;
00218 
00219 // a set of registers
00220 typedef
00221   hash_set_ex <
00222   Register >
00223   reg_set;
00224 typedef
00225   reg_set::iterator
00226   reg_set_p;
00227 
00228 // a map from lir instruction to set of registers
00229 typedef
00230   map <
00231   LirInst *,
00232   reg_id_set >
00233   inst_to_reg_id_map;
00234 typedef
00235   inst_to_reg_id_map::iterator
00236   inst_to_reg_id_map_p;
00237 
00238 
00239 // a register or a constant value
00240 struct reg_or_const
00241 {
00242   reg_or_const ():
00243   _is_const (false)
00244   {
00245   }
00246 
00247   reg_or_const (const constant & con)
00248   {
00249     // save this
00250     *this = con;
00251   }
00252 
00253   reg_or_const (const Register & reg)
00254   {
00255     // save this
00256     *this = reg;
00257   }
00258 
00259   // whether or not we are constant
00260   bool
00261     _is_const;
00262 
00263   // our constant value, if we are a constant...
00264   constant
00265     _const;
00266 
00267   // our register, if we are a register...
00268   Register
00269     _reg;
00270 
00271   // to set us more easily
00272   reg_or_const & operator = (const Register & reg)
00273   {
00274     _is_const = false;
00275     _reg = reg;
00276     _const = constant::constant(0);
00277     return (*this);
00278   }
00279   reg_or_const & operator = (const constant & con)
00280   {
00281     _is_const = true;
00282     _const = con;
00283     return (*this);
00284   }
00285 
00286   // to stringize this thing
00287   string
00288   to_string (bool quote_strings, bool addAsmConstPrefix = false) const;
00289 
00290 };
00291 
00292 #endif

Generated on August 27, 2003
Back to the C-Breeze home page