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