1 #ifdef USE_PRAGMA_IDENT_HDR
   2 #pragma ident "@(#)forms.hpp    1.150 07/05/05 17:05:00 JVM"
   3 #endif
   4 /*
   5  * Copyright 1997-2007 Sun Microsystems, Inc.  All Rights Reserved.
   6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   7  *
   8  * This code is free software; you can redistribute it and/or modify it
   9  * under the terms of the GNU General Public License version 2 only, as
  10  * published by the Free Software Foundation.
  11  *
  12  * This code is distributed in the hope that it will be useful, but WITHOUT
  13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15  * version 2 for more details (a copy is included in the LICENSE file that
  16  * accompanied this code).
  17  *
  18  * You should have received a copy of the GNU General Public License version
  19  * 2 along with this work; if not, write to the Free Software Foundation,
  20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  21  *
  22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  23  * CA 95054 USA or visit www.sun.com if you need additional information or
  24  * have any questions.
  25  *  
  26  */
  27 
  28 // FORMS.HPP - ADL Parser Generic and Utility Forms Classes
  29 
  30 #define TRUE 1
  31 #define FALSE 0
  32 
  33 // DEFINITIONS OF LEGAL ATTRIBUTE TYPES
  34 #define INS_ATTR 0
  35 #define OP_ATTR  1
  36 
  37 // DEFINITIONS OF LEGAL CONSTRAINT TYPES
  38 
  39 // Class List
  40 class Form;
  41 class InstructForm;
  42 class MachNodeForm;
  43 class OperandForm;
  44 class OpClassForm;
  45 class AttributeForm;
  46 class RegisterForm;
  47 class PipelineForm;
  48 class SourceForm;
  49 class EncodeForm;
  50 class Component;
  51 class Constraint;
  52 class Predicate;
  53 class MatchRule;
  54 class Attribute;
  55 class Effect;
  56 class ExpandRule;
  57 class RewriteRule;
  58 class ConstructRule;
  59 class FormatRule;
  60 class Peephole;
  61 class EncClass;
  62 class Interface;
  63 class RegInterface;
  64 class ConstInterface;
  65 class MemInterface;
  66 class CondInterface;
  67 class Opcode;
  68 class InsEncode;
  69 class RegDef;
  70 class RegClass;
  71 class AllocClass;
  72 class ResourceForm;
  73 class PipeClassForm;
  74 class PeepMatch;
  75 class PeepConstraint;
  76 class PeepReplace;
  77 class MatchList;
  78 
  79 class ArchDesc;
  80 
  81 //------------------------------FormDict---------------------------------------
  82 // Dictionary containing Forms, and objects derived from forms
  83 class FormDict {
  84 private:
  85   Dict         _form;              // map names, char*, to their Form* or NULL
  86 
  87   // Disable public use of constructor, copy-ctor, operator =, operator ==
  88   FormDict( );
  89   FormDict &operator =( const FormDict & );
  90   // == compares two dictionaries; they must have the same keys (their keys
  91   // must match using CmpKey) and they must have the same values (pointer
  92   // comparison).  If so 1 is returned, if not 0 is returned.
  93   bool operator ==(const FormDict &d) const; // Compare dictionaries for equal
  94 
  95 public:
  96   // cmp is a key comparision routine.  hash is a routine to hash a key.
  97   // FormDict( CmpKey cmp, Hash hash );
  98   FormDict( CmpKey cmp, Hash hash, Arena *arena );
  99   FormDict( const FormDict & fd );    // Deep-copy guts
 100   ~FormDict();
 101 
 102   // Return # of key-value pairs in dict
 103   int Size(void) const;
 104 
 105   // Insert inserts the given key-value pair into the dictionary.  The prior
 106   // value of the key is returned; NULL if the key was not previously defined.
 107   const Form  *Insert(const char *name, Form *form); // A new key-value
 108 
 109   // Find finds the value of a given key; or NULL if not found.  
 110   // The dictionary is NOT changed.
 111   const Form  *operator [](const char *name) const;  // Do a lookup
 112 
 113   void dump();
 114 };
 115 
 116 // ***** Master Class for ADL Parser Forms *****
 117 //------------------------------Form-------------------------------------------
 118 class Form {
 119 public:
 120   static Arena  *arena;            // arena used by forms
 121 private:
 122   static Arena  *generate_arena(); // allocate arena used by forms
 123 
 124 protected:
 125   int   _ftype;                    // Indicator for derived class type
 126 
 127 public:
 128   // Public Data
 129   Form *_next;                     // Next pointer for form lists
 130   long  _linenum;                  // Line number for debugging
 131 
 132   // Dynamic type check for common forms.
 133   virtual OpClassForm   *is_opclass()     const;
 134   virtual OperandForm   *is_operand()     const;
 135   virtual InstructForm  *is_instruction() const;
 136   virtual MachNodeForm  *is_machnode()    const;
 137   virtual AttributeForm *is_attribute()   const;
 138   virtual Effect        *is_effect()      const;
 139   virtual ResourceForm  *is_resource()    const;
 140   virtual PipeClassForm *is_pipeclass()   const;
 141 
 142   // Check if this form is an operand usable for cisc-spilling
 143   virtual bool           is_cisc_reg(FormDict &globals) const { return false; }
 144   virtual bool           is_cisc_mem(FormDict &globals) const { return false; }
 145 
 146   // Public Methods
 147   Form(int formType=0, int line=0) 
 148     : _next(NULL), _linenum(line), _ftype(formType) { };
 149   ~Form() {};
 150 
 151   virtual bool ideal_only() const {
 152     assert(0,"Check of ideal status on non-instruction/operand form.\n");
 153     return FALSE;
 154   }
 155 
 156   // Check constraints after parsing
 157   virtual bool verify()    { return true; }       
 158  
 159   virtual void dump()      { output(stderr); }    // Debug printer
 160   // Write info to output files
 161   virtual void output(FILE *fp)    { fprintf(fp,"Form Output"); }
 162 
 163 public:
 164   // ADLC types, match the last character on ideal operands and instructions
 165   enum DataType {
 166     none        =  0,  // Not a simple type
 167     idealI      =  1,  // Integer type
 168     idealP      =  2,  // Pointer types, oop(s)
 169     idealL      =  3,  // Long    type
 170     idealF      =  4,  // Float   type
 171     idealD      =  5,  // Double  type
 172     idealB      =  6,  // Byte    type
 173     idealC      =  7,  // Char    type
 174     idealS      =  8   // String  type
 175   };
 176   // Convert ideal name to a DataType, return DataType::none if not a 'ConX'
 177   Form::DataType  ideal_to_const_type(const char *ideal_type_name) const;
 178   // Convert ideal name to a DataType, return DataType::none if not a 'sRegX
 179   Form::DataType  ideal_to_sReg_type(const char *name) const;
 180   // Convert ideal name to a DataType, return DataType::none if not a 'RegX
 181   Form::DataType  ideal_to_Reg_type(const char *name) const;
 182   
 183   // Convert ideal name to a DataType, return DataType::none if not a 'LoadX
 184   Form::DataType is_load_from_memory(const char *opType) const;
 185   // Convert ideal name to a DataType, return DataType::none if not a 'StoreX
 186   Form::DataType is_store_to_memory(const char *opType)  const;
 187 
 188   // ADLC call types, matched with ideal world
 189   enum CallType {
 190     invalid_type  =  0,  // invalid call type
 191     JAVA_STATIC   =  1,  // monomorphic entry
 192     JAVA_DYNAMIC  =  2,  // possibly megamorphic, inline cache call
 193     JAVA_COMPILED =  3,  // callee will be compiled java
 194     JAVA_INTERP   =  4,  // callee will be executed by interpreter
 195     JAVA_NATIVE   =  5,  // native entrypoint
 196     JAVA_RUNTIME  =  6,  // runtime entrypoint
 197     JAVA_LEAF     =  7   // calling leaf
 198   };
 199 
 200   // Interface types for operands and operand classes
 201   enum InterfaceType {
 202     no_interface          =  0,  // unknown or inconsistent interface type
 203     constant_interface    =  1,  // interface to constants
 204     register_interface    =  2,  // interface to registers
 205     memory_interface      =  3,  // interface to memory
 206     conditional_interface =  4   // interface for condition codes
 207   };
 208   virtual Form::InterfaceType interface_type(FormDict &globals) const;
 209 
 210   enum CiscSpillInfo {
 211     Not_cisc_spillable   =  AdlcVMDeps::Not_cisc_spillable,
 212     Maybe_cisc_spillable =   0,
 213     Is_cisc_spillable    =   1
 214     // ...
 215   };
 216 
 217   // LEGAL FORM TYPES
 218   enum {
 219     INS,
 220     OPER,
 221     OPCLASS,
 222     SRC,
 223     ADEF,
 224     REG,
 225     PIPE,
 226     CNST,
 227     PRED,
 228     ATTR,
 229     MAT,
 230     ENC,
 231     FOR,
 232     EXP,
 233     REW,
 234     EFF,
 235     RDEF,
 236     RCL,
 237     ACL,
 238     RES,
 239     PCL,
 240     PDEF,
 241     REGL,
 242     RESL,
 243     STAL,
 244     COMP,
 245     PEEP,
 246     RESO
 247   };
 248 
 249 };
 250 
 251 //------------------------------FormList---------------------------------------
 252 class FormList {
 253 private:
 254   Form *_root;
 255   Form *_tail;
 256   Form *_cur;
 257   int   _justReset;                // Set immediately after reset
 258   Form *_cur2;                     // Nested iterator
 259   int   _justReset2;
 260 
 261 public: 
 262   void addForm(Form * entry) { 
 263     if (_tail==NULL) { _root = _tail = _cur = entry;}
 264     else { _tail->_next = entry; _tail = entry;}
 265   };
 266   Form * current() { return _cur; };
 267   Form * iter()    { if (_justReset) _justReset = 0; 
 268                      else if (_cur)  _cur = _cur->_next; 
 269                      return _cur;};
 270   void   reset()   { if (_root) {_cur = _root; _justReset = 1;} };
 271 
 272   // Second iterator, state is internal
 273   Form * current2(){ return _cur2; };
 274   Form * iter2()   { if (_justReset2) _justReset2 = 0; 
 275                     else if (_cur2)  _cur2 = _cur2->_next; 
 276                     return _cur2;};
 277   void   reset2()  { if (_root) {_cur2 = _root; _justReset2 = 1;} };
 278 
 279   int  count() { 
 280     int  count = 0; reset();
 281     for( Form *cur; (cur =  iter()) != NULL; ) { ++count; };
 282     return count;
 283   }
 284 
 285   void dump() { 
 286     reset();
 287     Form *cur;
 288     for(; (cur =  iter()) != NULL; ) {
 289       cur->dump();
 290     };
 291   }
 292 
 293   bool verify() { 
 294     bool verified = true;
 295 
 296     reset();
 297     Form *cur;
 298     for(; (cur =  iter()) != NULL; ) {
 299       if ( ! cur->verify() ) verified = false;
 300     };
 301 
 302     return verified;
 303   }
 304 
 305   void output(FILE* fp) { 
 306     reset();
 307     Form *cur;
 308     for( ; (cur =  iter()) != NULL; ) {
 309       cur->output(fp);
 310     };
 311   }
 312 
 313   FormList() { _justReset = 1; _justReset2 = 1; _root = NULL; _tail = NULL; _cur = NULL; _cur2 = NULL;};
 314   ~FormList();
 315 };
 316 
 317 //------------------------------NameList---------------------------------------
 318 // Extendable list of pointers, <char *>
 319 class NameList {
 320   friend class PreserveIter;
 321 
 322 private:
 323   int                _cur;         // Insert next entry here; count of entries
 324   int                _max;         // Number of spaces allocated
 325   const char       **_names;       // Array of names
 326 
 327 protected:
 328   int                _iter;        // position during iteration
 329   bool               _justReset;   // Set immediately after reset
 330 
 331                                    
 332 public:                            
 333   static const char *_signal;      // reserved user-defined string
 334   enum               { Not_in_list = -1 };
 335 
 336   void  addName(const char *name);       
 337   void  add_signal();
 338   void  clear();                   // Remove all entries
 339 
 340   int   count() const;                   
 341                                    
 342   void  reset();                   // Reset iteration
 343   const char *iter();              // after reset(), first element : else next
 344   const char *current();           // return current element in iteration.
 345 
 346   bool  current_is_signal();       // Return 'true' if current entry is signal
 347   bool  is_signal(const char *entry); // Return true if entry is a signal
 348 
 349   bool  search(const char *);      // Search for a name in the list
 350   int   index(const char *);       // Return index of name in list
 351   const char *name (intptr_t index);// Return name at index in list
 352 
 353   void  dump();                    // output to stderr
 354   void  output(FILE *fp);          // Output list of names to 'fp'
 355 
 356   NameList();
 357   ~NameList();
 358 };
 359 
 360 
 361 // Convenience class to preserve iteration state since iterators are
 362 // internal instead of being external.
 363 class PreserveIter {
 364  private:
 365   NameList* _list;
 366   int _iter;
 367   bool _justReset;
 368 
 369  public:
 370   PreserveIter(NameList* nl) {
 371     _list = nl;
 372     _iter = _list->_iter;
 373     _justReset = _list->_justReset;
 374   }
 375   ~PreserveIter() {
 376     _list->_iter = _iter;
 377     _list->_justReset = _justReset;
 378   }
 379 
 380 };
 381   
 382 
 383 //------------------------------NameAndList------------------------------------
 384 // Storage for a name and an associated list of names
 385 class NameAndList {
 386 private:
 387   const char *_name;
 388   NameList    _list;
 389 
 390 public:
 391   NameAndList(char *name);
 392   ~NameAndList();
 393 
 394   // Add to entries in list
 395   void        add_entry(const char *entry);
 396 
 397   // Access the name and its associated list.
 398   const char *name() const;
 399   void        reset();
 400   const char *iter();
 401 
 402   int count() { return _list.count(); }
 403 
 404   // Return the "index" entry in the list, zero-based
 405   const char *operator[](int index);
 406 
 407 
 408   void  dump();                    // output to stderr
 409   void  output(FILE *fp);          // Output list of names to 'fp'
 410 };
 411   
 412 //------------------------------ComponentList---------------------------------
 413 // Component lists always have match rule operands first, followed by parameter
 414 // operands which do not appear in the match list (in order of declaration).
 415 class ComponentList : private NameList {
 416 private:
 417   int   _matchcnt;                 // Count of match rule operands
 418                                    
 419 public:
 420 
 421   // This is a batch program.  (And I have a destructor bug!)
 422   void operator delete( void *ptr ) {}
 423 
 424   void insert(Component *component, bool mflag);
 425   void insert(const char *name, const char *opType, int usedef, bool mflag);
 426 
 427   int  count();
 428   int  match_count() { return _matchcnt; } // Get count of match rule opers
 429                                    
 430   Component *iter();               // after reset(), first element : else next
 431   Component *match_iter();         // after reset(), first element : else next
 432   Component *post_match_iter();    // after reset(), first element : else next
 433   void       reset();              // Reset iteration
 434   Component *current();            // return current element in iteration.
 435 
 436   // Return element at "position", else NULL
 437   Component *operator[](int position);
 438   Component *at(int position) { return (*this)[position]; }
 439 
 440   // Return first component having this name.
 441   const Component *search(const char *name);
 442 
 443   // Return number of USEs + number of DEFs
 444   int        num_operands();
 445   // Return zero-based position in list;  -1 if not in list.
 446   int        operand_position(const char *name, int usedef);
 447   // Find position for this name, regardless of use/def information
 448   int        operand_position(const char *name);
 449   // Find position for this name when looked up for output via "format"
 450   int        operand_position_format(const char *name);
 451   // Find position for the Label when looked up for output via "format"
 452   int        label_position();
 453   // Find position for the Method when looked up for output via "format"
 454   int        method_position();
 455                                   
 456   void       dump();               // output to stderr
 457   void       output(FILE *fp);     // Output list of names to 'fp'
 458 
 459   ComponentList();
 460   ~ComponentList();
 461 };
 462 
 463 //------------------------------SourceForm-------------------------------------
 464 class SourceForm : public Form {
 465 private:
 466 
 467 public:
 468   // Public Data
 469   char *_code;                     // Buffer for storing code text
 470 
 471   // Public Methods
 472   SourceForm(char* code);
 473   ~SourceForm();
 474 
 475   virtual const char* classname() { return "SourceForm"; }
 476 
 477   void dump();                    // Debug printer
 478   void output(FILE *fp);          // Write output files
 479 };
 480 
 481 class HeaderForm : public SourceForm {
 482 public:
 483   HeaderForm(char* code) : SourceForm(code) { }
 484 
 485   virtual const char* classname() { return "HeaderForm"; }
 486 };
 487 
 488 class PreHeaderForm : public SourceForm {
 489 public:
 490   PreHeaderForm(char* code) : SourceForm(code) { }
 491 
 492   virtual const char* classname() { return "PreHeaderForm"; }
 493 };
 494 
 495 
 496 
 497 
 498 //------------------------------Expr------------------------------------------
 499 #define STRING_BUFFER_LENGTH  2048
 500 // class Expr represents integer expressions containing constants and addition
 501 // Value must be in range zero through maximum positive integer. 32bits.
 502 // Expected use: instruction and operand costs
 503 class Expr {
 504 public:
 505   enum {
 506     Zero     = 0,
 507     Max      = 0x7fffffff
 508   };
 509   const char *_external_name;  // if !NULL, then print this instead of _expr 
 510   const char *_expr;
 511   int         _min_value;
 512   int         _max_value;
 513 
 514   Expr();
 515   Expr(const char *cost);
 516   Expr(const char *name, const char *expression, int min_value, int max_value);
 517   Expr *clone() const;
 518 
 519   bool  is_unknown() const { return (this == Expr::get_unknown()); }
 520   bool  is_zero()    const { return (_min_value == Expr::Zero && _max_value == Expr::Zero); }
 521   bool  less_than_or_equal(const Expr *c) const { return (_max_value <= c->_min_value); }
 522 
 523   void  add(const Expr *c);
 524   void  add(const char *c);
 525   void  add(const char *c, ArchDesc &AD);   // check if 'c' is defined in <arch>.ad
 526   void  set_external_name(const char *name) { _external_name = name; }
 527 
 528   const char *as_string()  const { return (_external_name != NULL ? _external_name : _expr); }
 529   void  print()            const;
 530   void  print_define(FILE *fp) const;
 531   void  print_assert(FILE *fp) const;
 532 
 533   static Expr *get_unknown();   // Returns pointer to shared unknown cost instance
 534 
 535   static char *buffer()         { return &external_buffer[0]; }
 536   static bool  init_buffers();  // Fill buffers with 0
 537   static bool  check_buffers(); // if buffer use may have overflowed, assert
 538 
 539 private:
 540   static Expr *_unknown_expr;
 541   static char string_buffer[STRING_BUFFER_LENGTH];
 542   static char external_buffer[STRING_BUFFER_LENGTH];
 543   static bool _init_buffers;
 544   const char *compute_expr(const Expr *c1, const Expr *c2);  // cost as string after adding 'c1' and 'c2'
 545   int         compute_min (const Expr *c1, const Expr *c2);  // minimum after adding 'c1' and 'c2'
 546   int         compute_max (const Expr *c1, const Expr *c2);  // maximum after adding 'c1' and 'c2'
 547   const char *compute_external(const Expr *c1, const Expr *c2);  // external name after adding 'c1' and 'c2'
 548 };
 549 
 550 //------------------------------ExprDict---------------------------------------
 551 // Dictionary containing Exprs
 552 class ExprDict {
 553 private:
 554   Dict         _expr;              // map names, char*, to their Expr* or NULL
 555   NameList     _defines;           // record the order of definitions entered with define call
 556 
 557   // Disable public use of constructor, copy-ctor, operator =, operator ==
 558   ExprDict( );
 559   ExprDict( const ExprDict & );    // Deep-copy guts
 560   ExprDict &operator =( const ExprDict & );
 561   // == compares two dictionaries; they must have the same keys (their keys
 562   // must match using CmpKey) and they must have the same values (pointer
 563   // comparison).  If so 1 is returned, if not 0 is returned.
 564   bool operator ==(const ExprDict &d) const; // Compare dictionaries for equal
 565 
 566 public:
 567   // cmp is a key comparision routine.  hash is a routine to hash a key.
 568   ExprDict( CmpKey cmp, Hash hash, Arena *arena );
 569   ~ExprDict();
 570 
 571   // Return # of key-value pairs in dict
 572   int Size(void) const;
 573 
 574   // define inserts the given key-value pair into the dictionary, 
 575   // and records the name in order for later output, ...
 576   const Expr  *define(const char *name, Expr *expr);
 577 
 578   // Insert inserts the given key-value pair into the dictionary.  The prior
 579   // value of the key is returned; NULL if the key was not previously defined.
 580   const Expr  *Insert(const char *name, Expr *expr); // A new key-value
 581 
 582   // Find finds the value of a given key; or NULL if not found.  
 583   // The dictionary is NOT changed.
 584   const Expr  *operator [](const char *name) const;  // Do a lookup
 585 
 586   void print_defines(FILE *fp);
 587   void print_asserts(FILE *fp);
 588   void dump();
 589 };
 590 
 591 
 592