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 #ifndef SHARE_VM_ADLC_FORMS_HPP
  26 #define SHARE_VM_ADLC_FORMS_HPP
  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   int   _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     idealN      =  9,  // Narrow oop types
 176     idealV      = 10   // Vector  type
 177   };
 178   // Convert ideal name to a DataType, return DataType::none if not a 'ConX'
 179   Form::DataType  ideal_to_const_type(const char *ideal_type_name) const;
 180   // Convert ideal name to a DataType, return DataType::none if not a 'sRegX
 181   Form::DataType  ideal_to_sReg_type(const char *name) const;
 182   // Convert ideal name to a DataType, return DataType::none if not a 'RegX
 183   Form::DataType  ideal_to_Reg_type(const char *name) const;
 184 
 185   // Convert ideal name to a DataType, return DataType::none if not a 'LoadX
 186   Form::DataType is_load_from_memory(const char *opType) const;
 187   // Convert ideal name to a DataType, return DataType::none if not a 'StoreX
 188   Form::DataType is_store_to_memory(const char *opType)  const;
 189 
 190   // ADLC call types, matched with ideal world
 191   enum CallType {
 192     invalid_type  =  0,  // invalid call type
 193     JAVA_STATIC   =  1,  // monomorphic entry
 194     JAVA_DYNAMIC  =  2,  // possibly megamorphic, inline cache call
 195     JAVA_COMPILED =  3,  // callee will be compiled java
 196     JAVA_INTERP   =  4,  // callee will be executed by interpreter
 197     JAVA_NATIVE   =  5,  // native entrypoint
 198     JAVA_RUNTIME  =  6,  // runtime entrypoint
 199     JAVA_LEAF     =  7   // calling leaf
 200   };
 201 
 202   // Interface types for operands and operand classes
 203   enum InterfaceType {
 204     no_interface          =  0,  // unknown or inconsistent interface type
 205     constant_interface    =  1,  // interface to constants
 206     register_interface    =  2,  // interface to registers
 207     memory_interface      =  3,  // interface to memory
 208     conditional_interface =  4   // interface for condition codes
 209   };
 210   virtual Form::InterfaceType interface_type(FormDict &globals) const;
 211 
 212   enum CiscSpillInfo {
 213     Not_cisc_spillable   =  AdlcVMDeps::Not_cisc_spillable,
 214     Maybe_cisc_spillable =   0,
 215     Is_cisc_spillable    =   1
 216     // ...
 217   };
 218 
 219   // LEGAL FORM TYPES
 220   enum {
 221     INS,
 222     OPER,
 223     OPCLASS,
 224     SRC,
 225     ADEF,
 226     REG,
 227     PIPE,
 228     CNST,
 229     PRED,
 230     ATTR,
 231     MAT,
 232     ENC,
 233     FOR,
 234     EXP,
 235     REW,
 236     EFF,
 237     RDEF,
 238     RCL,
 239     ACL,
 240     RES,
 241     PCL,
 242     PDEF,
 243     REGL,
 244     RESL,
 245     STAL,
 246     COMP,
 247     PEEP,
 248     RESO
 249   };
 250 
 251 };
 252 
 253 //------------------------------FormList---------------------------------------
 254 class FormList {
 255 private:
 256   Form *_root;
 257   Form *_tail;
 258   Form *_cur;
 259   int   _justReset;                // Set immediately after reset
 260   Form *_cur2;                     // Nested iterator
 261   int   _justReset2;
 262 
 263 public:
 264   void addForm(Form * entry) {
 265     if (_tail==NULL) { _root = _tail = _cur = entry;}
 266     else { _tail->_next = entry; _tail = entry;}
 267   };
 268   Form * current() { return _cur; };
 269   Form * iter()    { if (_justReset) _justReset = 0;
 270                      else if (_cur)  _cur = _cur->_next;
 271                      return _cur;};
 272   void   reset()   { if (_root) {_cur = _root; _justReset = 1;} };
 273 
 274   // Second iterator, state is internal
 275   Form * current2(){ return _cur2; };
 276   Form * iter2()   { if (_justReset2) _justReset2 = 0;
 277                     else if (_cur2)  _cur2 = _cur2->_next;
 278                     return _cur2;};
 279   void   reset2()  { if (_root) {_cur2 = _root; _justReset2 = 1;} };
 280 
 281   int  count() {
 282     int  count = 0; reset();
 283     for( Form *cur; (cur =  iter()) != NULL; ) { ++count; };
 284     return count;
 285   }
 286 
 287   void dump() {
 288     reset();
 289     Form *cur;
 290     for(; (cur =  iter()) != NULL; ) {
 291       cur->dump();
 292     };
 293   }
 294 
 295   bool verify() {
 296     bool verified = true;
 297 
 298     reset();
 299     Form *cur;
 300     for(; (cur =  iter()) != NULL; ) {
 301       if ( ! cur->verify() ) verified = false;
 302     };
 303 
 304     return verified;
 305   }
 306 
 307   void output(FILE* fp) {
 308     reset();
 309     Form *cur;
 310     for( ; (cur =  iter()) != NULL; ) {
 311       cur->output(fp);
 312     };
 313   }
 314 
 315   FormList() { _justReset = 1; _justReset2 = 1; _root = NULL; _tail = NULL; _cur = NULL; _cur2 = NULL;};
 316   ~FormList();
 317 };
 318 
 319 //------------------------------NameList---------------------------------------
 320 // Extendable list of pointers, <char *>
 321 class NameList {
 322   friend class PreserveIter;
 323 
 324 private:
 325   int                _cur;         // Insert next entry here; count of entries
 326   int                _max;         // Number of spaces allocated
 327   const char       **_names;       // Array of names
 328 
 329 protected:
 330   int                _iter;        // position during iteration
 331   bool               _justReset;   // Set immediately after reset
 332 
 333 
 334 public:
 335   static const char *_signal;      // reserved user-defined string
 336   static const char *_signal2;      // reserved user-defined string
 337   static const char *_signal3;      // reserved user-defined string
 338   enum               { Not_in_list = -1 };
 339 
 340   void  addName(const char *name);
 341   void  add_signal();
 342   void  clear();                   // Remove all entries
 343 
 344   int   count() const;
 345 
 346   void  reset();                   // Reset iteration
 347   const char *iter();              // after reset(), first element : else next
 348   const char *current();           // return current element in iteration.
 349   const char *peek(int skip = 1);  // returns element + skip in iteration if there is one
 350 
 351   bool  current_is_signal();       // Return 'true' if current entry is signal
 352   bool  is_signal(const char *entry); // Return true if entry is a signal
 353 
 354   bool  search(const char *);      // Search for a name in the list
 355   int   index(const char *);       // Return index of name in list
 356   const char *name (intptr_t index);// Return name at index in list
 357 
 358   void  dump();                    // output to stderr
 359   void  output(FILE *fp);          // Output list of names to 'fp'
 360 
 361   NameList();
 362   ~NameList();
 363 };
 364 
 365 
 366 // Convenience class to preserve iteration state since iterators are
 367 // internal instead of being external.
 368 class PreserveIter {
 369  private:
 370   NameList* _list;
 371   int _iter;
 372   bool _justReset;
 373 
 374  public:
 375   PreserveIter(NameList* nl) {
 376     _list = nl;
 377     _iter = _list->_iter;
 378     _justReset = _list->_justReset;
 379   }
 380   ~PreserveIter() {
 381     _list->_iter = _iter;
 382     _list->_justReset = _justReset;
 383   }
 384 
 385 };
 386 
 387 
 388 //------------------------------NameAndList------------------------------------
 389 // Storage for a name and an associated list of names
 390 class NameAndList {
 391 private:
 392   const char *_name;
 393   NameList    _list;
 394 
 395 public:
 396   NameAndList(char *name);
 397   ~NameAndList();
 398 
 399   // Add to entries in list
 400   void        add_entry(const char *entry);
 401 
 402   // Access the name and its associated list.
 403   const char *name() const;
 404   void        reset();
 405   const char *iter();
 406 
 407   int count() { return _list.count(); }
 408 
 409   // Return the "index" entry in the list, zero-based
 410   const char *operator[](int index);
 411 
 412 
 413   void  dump();                    // output to stderr
 414   void  output(FILE *fp);          // Output list of names to 'fp'
 415 };
 416 
 417 //------------------------------ComponentList---------------------------------
 418 // Component lists always have match rule operands first, followed by parameter
 419 // operands which do not appear in the match list (in order of declaration).
 420 class ComponentList : private NameList {
 421 private:
 422   int   _matchcnt;                 // Count of match rule operands
 423 
 424 public:
 425 
 426   // This is a batch program.  (And I have a destructor bug!)
 427   void operator delete( void *ptr ) {}
 428 
 429   void insert(Component *component, bool mflag);
 430   void insert(const char *name, const char *opType, int usedef, bool mflag);
 431 
 432   int  count();
 433   int  match_count() { return _matchcnt; } // Get count of match rule opers
 434 
 435   Component *iter();               // after reset(), first element : else next
 436   Component *match_iter();         // after reset(), first element : else next
 437   Component *post_match_iter();    // after reset(), first element : else next
 438   void       reset();              // Reset iteration
 439   Component *current();            // return current element in iteration.
 440 
 441   // Return element at "position", else NULL
 442   Component *operator[](int position);
 443   Component *at(int position) { return (*this)[position]; }
 444 
 445   // Return first component having this name.
 446   const Component *search(const char *name);
 447 
 448   // Return number of USEs + number of DEFs
 449   int        num_operands();
 450   // Return zero-based position in list;  -1 if not in list.
 451   int        operand_position(const char *name, int usedef);
 452   // Find position for this name, regardless of use/def information
 453   int        operand_position(const char *name);
 454   // Find position for this name when looked up for output via "format"
 455   int        operand_position_format(const char *name);
 456   // Find position for the Label when looked up for output via "format"
 457   int        label_position();
 458   // Find position for the Method when looked up for output via "format"
 459   int        method_position();
 460 
 461   void       dump();               // output to stderr
 462   void       output(FILE *fp);     // Output list of names to 'fp'
 463 
 464   ComponentList();
 465   ~ComponentList();
 466 };
 467 
 468 //------------------------------SourceForm-------------------------------------
 469 class SourceForm : public Form {
 470 private:
 471 
 472 public:
 473   // Public Data
 474   char *_code;                     // Buffer for storing code text
 475 
 476   // Public Methods
 477   SourceForm(char* code);
 478   ~SourceForm();
 479 
 480   virtual const char* classname() { return "SourceForm"; }
 481 
 482   void dump();                    // Debug printer
 483   void output(FILE *fp);          // Write output files
 484 };
 485 
 486 class HeaderForm : public SourceForm {
 487 public:
 488   HeaderForm(char* code) : SourceForm(code) { }
 489 
 490   virtual const char* classname() { return "HeaderForm"; }
 491 };
 492 
 493 class PreHeaderForm : public SourceForm {
 494 public:
 495   PreHeaderForm(char* code) : SourceForm(code) { }
 496 
 497   virtual const char* classname() { return "PreHeaderForm"; }
 498 };
 499 
 500 
 501 
 502 
 503 //------------------------------Expr------------------------------------------
 504 #define STRING_BUFFER_LENGTH  2048
 505 // class Expr represents integer expressions containing constants and addition
 506 // Value must be in range zero through maximum positive integer. 32bits.
 507 // Expected use: instruction and operand costs
 508 class Expr {
 509 public:
 510   enum {
 511     Zero     = 0,
 512     Max      = 0x7fffffff
 513   };
 514   const char *_external_name;  // if !NULL, then print this instead of _expr
 515   const char *_expr;
 516   int         _min_value;
 517   int         _max_value;
 518 
 519   Expr();
 520   Expr(const char *cost);
 521   Expr(const char *name, const char *expression, int min_value, int max_value);
 522   Expr *clone() const;
 523 
 524   bool  is_unknown() const { return (this == Expr::get_unknown()); }
 525   bool  is_zero()    const { return (_min_value == Expr::Zero && _max_value == Expr::Zero); }
 526   bool  less_than_or_equal(const Expr *c) const { return (_max_value <= c->_min_value); }
 527 
 528   void  add(const Expr *c);
 529   void  add(const char *c);
 530   void  add(const char *c, ArchDesc &AD);   // check if 'c' is defined in <arch>.ad
 531   void  set_external_name(const char *name) { _external_name = name; }
 532 
 533   const char *as_string()  const { return (_external_name != NULL ? _external_name : _expr); }
 534   void  print()            const;
 535   void  print_define(FILE *fp) const;
 536   void  print_assert(FILE *fp) const;
 537 
 538   static Expr *get_unknown();   // Returns pointer to shared unknown cost instance
 539 
 540   static char *buffer()         { return &external_buffer[0]; }
 541   static bool  init_buffers();  // Fill buffers with 0
 542   static bool  check_buffers(); // if buffer use may have overflowed, assert
 543 
 544 private:
 545   static Expr *_unknown_expr;
 546   static char string_buffer[STRING_BUFFER_LENGTH];
 547   static char external_buffer[STRING_BUFFER_LENGTH];
 548   static bool _init_buffers;
 549   const char *compute_expr(const Expr *c1, const Expr *c2);  // cost as string after adding 'c1' and 'c2'
 550   int         compute_min (const Expr *c1, const Expr *c2);  // minimum after adding 'c1' and 'c2'
 551   int         compute_max (const Expr *c1, const Expr *c2);  // maximum after adding 'c1' and 'c2'
 552   const char *compute_external(const Expr *c1, const Expr *c2);  // external name after adding 'c1' and 'c2'
 553 };
 554 
 555 //------------------------------ExprDict---------------------------------------
 556 // Dictionary containing Exprs
 557 class ExprDict {
 558 private:
 559   Dict         _expr;              // map names, char*, to their Expr* or NULL
 560   NameList     _defines;           // record the order of definitions entered with define call
 561 
 562   // Disable public use of constructor, copy-ctor, operator =, operator ==
 563   ExprDict( );
 564   ExprDict( const ExprDict & );    // Deep-copy guts
 565   ExprDict &operator =( const ExprDict & );
 566   // == compares two dictionaries; they must have the same keys (their keys
 567   // must match using CmpKey) and they must have the same values (pointer
 568   // comparison).  If so 1 is returned, if not 0 is returned.
 569   bool operator ==(const ExprDict &d) const; // Compare dictionaries for equal
 570 
 571 public:
 572   // cmp is a key comparision routine.  hash is a routine to hash a key.
 573   ExprDict( CmpKey cmp, Hash hash, Arena *arena );
 574   ~ExprDict();
 575 
 576   // Return # of key-value pairs in dict
 577   int Size(void) const;
 578 
 579   // define inserts the given key-value pair into the dictionary,
 580   // and records the name in order for later output, ...
 581   const Expr  *define(const char *name, Expr *expr);
 582 
 583   // Insert inserts the given key-value pair into the dictionary.  The prior
 584   // value of the key is returned; NULL if the key was not previously defined.
 585   const Expr  *Insert(const char *name, Expr *expr); // A new key-value
 586 
 587   // Find finds the value of a given key; or NULL if not found.
 588   // The dictionary is NOT changed.
 589   const Expr  *operator [](const char *name) const;  // Do a lookup
 590 
 591   void print_defines(FILE *fp);
 592   void print_asserts(FILE *fp);
 593   void dump();
 594 };
 595 
 596 #endif // SHARE_VM_ADLC_FORMS_HPP