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.cc

Go to the documentation of this file.
00001 // $Id: register.cc,v 1.11 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 #include "c_breeze.h"
00039 #include "register.h"
00040 
00041 void
00042 Register::setType (typeNode * the_type)
00043 {
00044   if ( the_type->is_char()
00045        || the_type->is_int()
00046        || the_type->is_pointer() )
00047     makeGPR();
00048   else if ( the_type->is_float() )
00049     makeFPR();
00050   else
00051     CBZFAIL(("don't know how to put low-level type %d into a register.",
00052              the_type->typ()));
00053 }
00054 
00055 string
00056 Register::to_string (bool wantPrefixAdd, bool wantPrefixRemove) const
00057 {
00058   // are we a real register? if so return our name with the right prefix
00059   string name;
00060   if (CBZ::ArchInfo.
00061       get_reg_name (*this, name, wantPrefixAdd, wantPrefixRemove))
00062     return name;
00063 
00064   // we are a virtual register - format our name properly
00065   char *pBuf = new char[20];
00066   switch (_type)
00067     {
00068     case reg_gpr:
00069       return cbz_util::string_format ("gpr_%d", _num);
00070     case reg_fpr:
00071       return cbz_util::string_format ("fpr_%d", _num);
00072     case reg_stack_ptr:
00073       return cbz_util::string_format ("sp");
00074     case reg_frame_ptr:
00075       return cbz_util::string_format ("fp");
00076     default:
00077       return cbz_util::string_format ("R_%d", _num);
00078     }
00079 }
00080 
00081 string
00082 reg_or_const::to_string (bool quote_strings, bool addAsmConstPrefix) const
00083 {
00084   // if we are a string and we do not want to quote strings, do things
00085   // specially
00086   if (_is_const && _const.is_str () && !quote_strings)
00087     return _const.Str ();
00088 
00089   // are we a register?
00090   if (!_is_const)
00091     return _reg.to_string ();
00092 
00093   // we are a constant - we may need to add some prefix to this name
00094   string str = _const.to_string (false);
00095   cbz_util::string_replace (str, "UL", "");
00096   cbz_util::string_replace (str, "U", "");
00097   if (addAsmConstPrefix && !_const.is_str ())
00098     str = CBZ::ArchInfo.get_asm_const_prefix () + str;
00099   return str;
00100 }
00101 
00102 // handy print operator for reg_or_const
00103 ostream & operator << (ostream & os, const reg_or_const & rc)
00104 {
00105   // stringize the thing
00106   os << rc.to_string (true);
00107   return os;
00108 }
00109 
00110 Register
00111 Register::getRegSp ()
00112 {
00113   assert (CBZ::ArchInfo.is_valid ());
00114   return *(CBZ::ArchInfo.get_reg_sp ());
00115 }
00116 
00117 Register
00118 Register::getRegFp ()
00119 {
00120   assert (CBZ::ArchInfo.is_valid ());
00121   return *(CBZ::ArchInfo.get_reg_fp ());
00122 }
00123 
00124 Register
00125 Register::getRegRetValFixed ()
00126 {
00127   assert (CBZ::ArchInfo.is_valid ());
00128   const arch_info::register_info * info =
00129     CBZ::ArchInfo.get_reg_retval_fixed ();
00130   Register reg;
00131   if (info)
00132     reg = *info;
00133   return reg;
00134 }
00135 
00136 Register
00137 Register::getRegRetValFloat ()
00138 {
00139   assert (CBZ::ArchInfo.is_valid ());
00140   const arch_info::register_info * info =
00141     CBZ::ArchInfo.get_reg_retval_float ();
00142   Register reg;
00143   if (info)
00144     reg = *info;
00145   return reg;
00146 }
00147 
00148 bool
00149 Register::getRegRetVal (typeNode * the_type, Register & reg)
00150 {
00151   if ( the_type->is_void() )
00152     return false;
00153   else if ( the_type->is_integer()
00154             || the_type->is_pointer() )
00155     reg = Register::getRegRetValFixed();
00156   else if ( the_type->is_float() )
00157     reg = Register::getRegRetValFloat();
00158   else
00159     CBZFAIL(("low-level type %d can't be returned in a register.",
00160              the_type->typ()));
00161   return true;
00162 }
00163 
00164 //--------------------------------------------------------------

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