1 /*
   2  * Copyright (c) 1997, 2012, 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 // FORMS.CPP - Definitions for ADL Parser Generic & Utility Forms Classes
  26 #include "adlc.hpp"
  27 
  28 //------------------------------Static Initializers----------------------------
  29 // allocate arena used by forms
  30 Arena  *Form::arena = Form::generate_arena(); //  = Form::generate_arena();
  31 Arena *Form::generate_arena() {
  32   return (new Arena);
  33 }
  34 
  35 //------------------------------NameList---------------------------------------
  36 // reserved user-defined string
  37 const char  *NameList::_signal   = "$$SIGNAL$$";
  38 const char  *NameList::_signal2  = "$$SIGNAL2$$";
  39 const char  *NameList::_signal3  = "$$SIGNAL3$$";
  40 
  41 // Constructor and Destructor
  42 NameList::NameList() : _cur(0), _max(4), _iter(0), _justReset(true) {
  43   _names = (const char**) AllocateHeap(_max*sizeof(char*));
  44 }
  45 NameList::~NameList() {
  46   // The following free is a double-free, and crashes the program:
  47   //free(_names);                   // not owner of strings
  48 }
  49 
  50 void   NameList::addName(const char *name) {
  51   if (_cur == _max) {
  52     _names = (const char**) ReAllocateHeap(_names, (_max *=2)*sizeof(char*));
  53   }
  54   _names[_cur++] = name;
  55 }
  56 
  57 void   NameList::add_signal() {
  58   addName( _signal );
  59 }
  60 void   NameList::clear() {
  61   _cur   = 0;
  62   _iter  = 0;
  63   _justReset = true;
  64   // _max   = 4; Already allocated
  65 }
  66 
  67 int    NameList::count()  const { return _cur; }
  68 
  69 void   NameList::reset()   { _iter = 0; _justReset = true;}
  70 const char  *NameList::iter()    {
  71   if (_justReset) {_justReset=false; return (_iter < _cur ? _names[_iter] : NULL);}
  72   else return (_iter <_cur-1 ? _names[++_iter] : NULL);
  73 }
  74 const char  *NameList::current() { return (_iter < _cur ? _names[_iter] : NULL); }
  75 const char  *NameList::peek(int skip) { return (_iter + skip < _cur ? _names[_iter + skip] : NULL); }
  76 
  77 // Return 'true' if current entry is signal
  78 bool  NameList::current_is_signal() {
  79   const char *entry = current();
  80   return is_signal(entry);
  81 }
  82 
  83 // Return true if entry is a signal
  84 bool  NameList::is_signal(const char *entry) {
  85   return ( (strcmp(entry,NameList::_signal) == 0) ? true : false);
  86 }
  87 
  88 // Search for a name in the list
  89 bool   NameList::search(const char *name) {
  90   const char *entry;
  91   for(reset(); (entry = iter()) != NULL; ) {
  92     if(!strcmp(entry,name)) return true;
  93   }
  94   return false;
  95 }
  96 
  97 // Return index of name in list
  98 int    NameList::index(const char *name) {
  99   int         cnt = 0;
 100   const char *entry;
 101   for(reset(); (entry = iter()) != NULL; ) {
 102     if(!strcmp(entry,name)) return cnt;
 103     cnt++;
 104   }
 105   return Not_in_list;
 106 }
 107 
 108 // Return name at index in list
 109 const char  *NameList::name(intptr_t  index) {
 110   return ( index < _cur ? _names[index] : NULL);
 111 }
 112 
 113 void   NameList::dump() { output(stderr); }
 114 
 115 void   NameList::output(FILE *fp) {
 116   fprintf(fp, "\n");
 117 
 118   // Run iteration over all entries, independent of position of iterator.
 119   const char *name       = NULL;
 120   int         iter       = 0;
 121   bool        justReset  = true;
 122 
 123   while( ( name  = (justReset ?
 124                     (justReset=false, (iter < _cur ? _names[iter] : NULL)) :
 125                     (iter < _cur-1 ? _names[++iter] : NULL)) )
 126          != NULL ) {
 127     fprintf( fp, "  %s,\n", name);
 128   }
 129   fprintf(fp, "\n");
 130 }
 131 
 132 //------------------------------NameAndList------------------------------------
 133 // Storage for a name and an associated list of names
 134 NameAndList::NameAndList(char *name) : _name(name) {
 135 }
 136 NameAndList::~NameAndList() {
 137 }
 138 
 139 // Add to entries in list
 140 void NameAndList::add_entry(const char *entry) {
 141   _list.addName(entry);
 142 }
 143 
 144 // Access the name and its associated list.
 145 const char *NameAndList::name()  const {  return _name;  }
 146 void        NameAndList::reset()       { _list.reset();  }
 147 const char *NameAndList::iter()        { return _list.iter(); }
 148 
 149 // Return the "index" entry in the list, zero-based
 150 const char *NameAndList::operator[](int index) {
 151   assert( index >= 0, "Internal Error(): index less than 0.");
 152 
 153   _list.reset();
 154   const char *entry = _list.iter();
 155   // Iterate further if it isn't at index 0.
 156   for ( int position = 0; position != index; ++position ) {
 157     entry = _list.iter();
 158   }
 159 
 160   return entry;
 161 }
 162 
 163 
 164 void   NameAndList::dump() { output(stderr); }
 165 void   NameAndList::output(FILE *fp) {
 166   fprintf(fp, "\n");
 167 
 168   // Output the Name
 169   fprintf(fp, "Name == %s", (_name ? _name : "") );
 170 
 171   // Output the associated list of names
 172   const char *name;
 173   fprintf(fp, " (");
 174   for (reset(); (name = iter()) != NULL;) {
 175     fprintf(fp, "  %s,\n", name);
 176   }
 177   fprintf(fp, ")");
 178   fprintf(fp, "\n");
 179 }
 180 
 181 //------------------------------Form-------------------------------------------
 182 OpClassForm   *Form::is_opclass()     const {
 183   return NULL;
 184 }
 185 
 186 OperandForm   *Form::is_operand()     const {
 187   return NULL;
 188 }
 189 
 190 InstructForm  *Form::is_instruction() const {
 191   return NULL;
 192 }
 193 
 194 MachNodeForm  *Form::is_machnode() const {
 195   return NULL;
 196 }
 197 
 198 AttributeForm *Form::is_attribute() const {
 199   return NULL;
 200 }
 201 
 202 Effect        *Form::is_effect() const {
 203   return NULL;
 204 }
 205 
 206 ResourceForm  *Form::is_resource() const {
 207   return NULL;
 208 }
 209 
 210 PipeClassForm *Form::is_pipeclass() const {
 211   return NULL;
 212 }
 213 
 214 Form::DataType Form::ideal_to_const_type(const char *name) const {
 215   if( name == NULL ) { return Form::none; }
 216 
 217   if (strcmp(name,"ConI")==0) return Form::idealI;
 218   if (strcmp(name,"ConP")==0) return Form::idealP;
 219   if (strcmp(name,"ConN")==0) return Form::idealN;
 220   if (strcmp(name,"ConNKlass")==0) return Form::idealNKlass;
 221   if (strcmp(name,"ConL")==0) return Form::idealL;
 222   if (strcmp(name,"ConF")==0) return Form::idealF;
 223   if (strcmp(name,"ConD")==0) return Form::idealD;
 224   if (strcmp(name,"Bool")==0) return Form::idealI;
 225 
 226   return Form::none;
 227 }
 228 
 229 Form::DataType Form::ideal_to_sReg_type(const char *name) const {
 230   if( name == NULL ) { return Form::none; }
 231 
 232   if (strcmp(name,"sRegI")==0) return Form::idealI;
 233   if (strcmp(name,"sRegP")==0) return Form::idealP;
 234   if (strcmp(name,"sRegF")==0) return Form::idealF;
 235   if (strcmp(name,"sRegD")==0) return Form::idealD;
 236   if (strcmp(name,"sRegL")==0) return Form::idealL;
 237   return Form::none;
 238 }
 239 
 240 Form::DataType Form::ideal_to_Reg_type(const char *name) const {
 241   if( name == NULL ) { return Form::none; }
 242 
 243   if (strcmp(name,"RegI")==0) return Form::idealI;
 244   if (strcmp(name,"RegP")==0) return Form::idealP;
 245   if (strcmp(name,"RegF")==0) return Form::idealF;
 246   if (strcmp(name,"RegD")==0) return Form::idealD;
 247   if (strcmp(name,"RegL")==0) return Form::idealL;
 248 
 249   return Form::none;
 250 }
 251 
 252 // True if 'opType', an ideal name, loads or stores.
 253 Form::DataType Form::is_load_from_memory(const char *opType) const {
 254   if( strcmp(opType,"LoadB")==0 )  return Form::idealB;
 255   if( strcmp(opType,"LoadUB")==0 )  return Form::idealB;
 256   if( strcmp(opType,"LoadUS")==0 )  return Form::idealC;
 257   if( strcmp(opType,"LoadD")==0 )  return Form::idealD;
 258   if( strcmp(opType,"LoadD_unaligned")==0 )  return Form::idealD;
 259   if( strcmp(opType,"LoadF")==0 )  return Form::idealF;
 260   if( strcmp(opType,"LoadI")==0 )  return Form::idealI;
 261   if( strcmp(opType,"LoadKlass")==0 )  return Form::idealP;
 262   if( strcmp(opType,"LoadNKlass")==0 ) return Form::idealNKlass;
 263   if( strcmp(opType,"LoadL")==0 )  return Form::idealL;
 264   if( strcmp(opType,"LoadL_unaligned")==0 )  return Form::idealL;
 265   if( strcmp(opType,"LoadPLocked")==0 )  return Form::idealP;
 266   if( strcmp(opType,"LoadP")==0 )  return Form::idealP;
 267   if( strcmp(opType,"LoadN")==0 )  return Form::idealN;
 268   if( strcmp(opType,"LoadRange")==0 )  return Form::idealI;
 269   if( strcmp(opType,"LoadS")==0 )  return Form::idealS;
 270   if( strcmp(opType,"LoadVector")==0 )  return Form::idealV;
 271   if( strcmp(opType,"LoadVectorGather")==0 )  return Form::idealV;
 272   assert( strcmp(opType,"Load") != 0, "Must type Loads" );
 273   return Form::none;
 274 }
 275 
 276 Form::DataType Form::is_store_to_memory(const char *opType) const {
 277   if( strcmp(opType,"StoreB")==0)  return Form::idealB;
 278   if( strcmp(opType,"StoreCM")==0) return Form::idealB;
 279   if( strcmp(opType,"StoreC")==0)  return Form::idealC;
 280   if( strcmp(opType,"StoreD")==0)  return Form::idealD;
 281   if( strcmp(opType,"StoreF")==0)  return Form::idealF;
 282   if( strcmp(opType,"StoreI")==0)  return Form::idealI;
 283   if( strcmp(opType,"StoreL")==0)  return Form::idealL;
 284   if( strcmp(opType,"StoreP")==0)  return Form::idealP;
 285   if( strcmp(opType,"StoreN")==0)  return Form::idealN;
 286   if( strcmp(opType,"StoreNKlass")==0)  return Form::idealNKlass;
 287   if( strcmp(opType,"StoreVector")==0 )  return Form::idealV;
 288   if( strcmp(opType,"StoreVectorScatter")==0 )  return Form::idealV;
 289   assert( strcmp(opType,"Store") != 0, "Must type Stores" );
 290   return Form::none;
 291 }
 292 
 293 Form::InterfaceType Form::interface_type(FormDict &globals) const {
 294   return Form::no_interface;
 295 }
 296 
 297 //------------------------------FormList---------------------------------------
 298 // Destructor
 299 FormList::~FormList()  {
 300   // // This list may not own its elements
 301   // Form *cur  = _root;
 302   // Form *next = NULL;
 303   // for( ; (cur = next) != NULL; ) {
 304   //   next = (Form *)cur->_next;
 305   //   delete cur;
 306   // }
 307 };
 308 
 309 //------------------------------FormDict---------------------------------------
 310 // Constructor
 311 FormDict::FormDict( CmpKey cmp, Hash hash, Arena *arena )
 312   : _form(cmp, hash, arena) {
 313 }
 314 FormDict::~FormDict() {
 315 }
 316 
 317 // Return # of name-Form pairs in dict
 318 int FormDict::Size(void) const {
 319   return _form.Size();
 320 }
 321 
 322 // Insert inserts the given key-value pair into the dictionary.  The prior
 323 // value of the key is returned; NULL if the key was not previously defined.
 324 const Form  *FormDict::Insert(const char *name, Form *form) {
 325   return (Form*)_form.Insert((void*)name, (void*)form);
 326 }
 327 
 328 // Finds the value of a given key; or NULL if not found.
 329 // The dictionary is NOT changed.
 330 const Form  *FormDict::operator [](const char *name) const {
 331   return (Form*)_form[name];
 332 }
 333 
 334 //------------------------------FormDict::private------------------------------
 335 // Disable public use of constructor, copy-ctor, operator =, operator ==
 336 FormDict::FormDict( ) : _form(cmpkey,hashkey) {
 337   assert( false, "NotImplemented");
 338 }
 339 FormDict::FormDict( const FormDict & fd) : _form(fd._form) {
 340 }
 341 FormDict &FormDict::operator =( const FormDict &rhs) {
 342   assert( false, "NotImplemented");
 343   _form = rhs._form;
 344   return *this;
 345 }
 346 // == compares two dictionaries; they must have the same keys (their keys
 347 // must match using CmpKey) and they must have the same values (pointer
 348 // comparison).  If so 1 is returned, if not 0 is returned.
 349 bool FormDict::operator ==(const FormDict &d) const {
 350   assert( false, "NotImplemented");
 351   return false;
 352 }
 353 
 354 // Print out the dictionary contents as key-value pairs
 355 static void dumpkey (const void* key)  { fprintf(stdout, "%s", (char*) key); }
 356 static void dumpform(const void* form) { fflush(stdout); ((Form*)form)->dump(); }
 357 
 358 void FormDict::dump() {
 359   _form.print(dumpkey, dumpform);
 360 }
 361 
 362 //------------------------------SourceForm-------------------------------------
 363 SourceForm::SourceForm(char* code) : _code(code) { }; // Constructor
 364 SourceForm::~SourceForm() {
 365 }
 366 
 367 void SourceForm::dump() {                    // Debug printer
 368   output(stderr);
 369 }
 370 
 371 void SourceForm::output(FILE *fp) {
 372   fprintf(fp,"\n//%s\n%s\n",classname(),(_code?_code:""));
 373 }