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