1 #ifdef USE_PRAGMA_IDENT_HDR
   2 #pragma ident "@(#)adlparse.hpp 1.79 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 // ADLPARSE.HPP - Definitions for Architecture Description Language Parser
  29 // Authors: Chris Vick and Mike Paleczny
  30 
  31 // Class List
  32 class Form;
  33 // ***** Top Level, 1, classes  *****
  34 class InstructForm;
  35 class OperandForm;
  36 class OpClassForm;
  37 class AttributeForm;
  38 class RegisterForm;
  39 class PipelineForm;
  40 class SourceForm;
  41 class Peephole;
  42 // ***** Level 2 classes *****
  43 class Component;
  44 class Predicate;
  45 class MatchRule;
  46 class Encode;
  47 class Attribute;
  48 class Effect;
  49 class ExpandRule;
  50 class RewriteRule;
  51 class Constraint;
  52 class ConstructRule;
  53 // ***** Register Section *****
  54 class RegDef;
  55 class RegClass;
  56 class AllocClass;
  57 class ResourceForm;
  58 // ***** Pipeline Section *****
  59 class PipeDesc;
  60 class PipeClass;
  61 class RegList;
  62 // ***** Peephole Section *****
  63 class PeepMatch;
  64 class PeepConstraint;
  65 class PeepReplace;
  66 
  67 // class ostream; // ostream is a typedef in some systems
  68 
  69 extern char *toUpper(const char *str);
  70 
  71 //---------------------------ADLParser-----------------------------------------
  72 class ADLParser {
  73 protected:
  74   char     *_curline;           // Start of current line
  75   char     *_ptr;               // Pointer into current location in File Buffer
  76   int       _linenum;           // Count of line numbers seen so far
  77   char      _curchar;           // Current character from buffer
  78   FormDict &_globalNames;       // Global names
  79 
  80   enum { _preproc_limit = 20 };
  81   int       _preproc_depth;                 // How deep are we into ifdefs?
  82   int       _preproc_not_taken;             // How deep in not-taken ifdefs?
  83   bool      _preproc_taken[_preproc_limit]; // Are we taking this ifdef level?
  84   bool      _preproc_else[_preproc_limit];  // Did this level have an else yet?
  85 
  86   // ***** Level 1 Parse functions *****
  87   void instr_parse(void);       // Parse instruction definitions
  88   void oper_parse(void);        // Parse operand definitions
  89   void opclass_parse(void);     // Parse operand class definitions
  90   void ins_attr_parse(void);    // Parse instruction attrubute definitions
  91   void op_attr_parse(void);     // Parse operand attrubute definitions
  92   void source_parse(void);      // Parse source section
  93   void source_hpp_parse(void);  // Parse source_hpp section
  94   void reg_parse(void);         // Parse register section
  95   void encode_parse(void);      // Parse encoding section
  96   void frame_parse(void);       // Parse frame section
  97   void pipe_parse(void);        // Parse pipeline section
  98   void definitions_parse(void); // Parse definitions section
  99   void peep_parse(void);        // Parse peephole rule definitions
 100   void preproc_define(void);    // Parse a #define statement
 101   void preproc_undef(void);     // Parse an #undef statement
 102 
 103   // Helper functions for instr_parse().
 104   void adjust_set_rule(InstructForm *instr);
 105   void matchrule_clone_and_swap(MatchRule *rule, const char* instr_ident, int& match_rules_cnt);
 106 
 107   // ***** Level 2 Parse functions *****
 108   // Parse the components of the encode section
 109   void enc_class_parse(void);   // Parse encoding class definition
 110   void enc_class_parse_block(EncClass* encoding, char* ec_name);
 111 
 112   // Parse the components of the frame section
 113   void stack_dir_parse(FrameForm *frame);   // Parse the stack direction entry
 114   void sync_stack_slots_parse(FrameForm *frame);
 115   void frame_pointer_parse(FrameForm *frame, bool native);
 116   void interpreter_frame_pointer_parse(FrameForm *frame, bool native);
 117   void inline_cache_parse(FrameForm *frame, bool native);
 118   void interpreter_arg_ptr_parse(FrameForm *frame, bool native);
 119   void interpreter_method_oop_parse(FrameForm *frame, bool native);
 120   void cisc_spilling_operand_name_parse(FrameForm *frame, bool native);
 121   void stack_alignment_parse(FrameForm *frame);
 122   void return_addr_parse(FrameForm *frame, bool native);
 123   void preserve_stack_parse(FrameForm *frame);
 124   char *calling_convention_parse();
 125   char *return_value_parse();
 126 
 127   // Parse components of the register section
 128   void reg_def_parse(void);              // Parse register definition
 129   void reg_class_parse(void);            // Parse register class definition
 130   void alloc_class_parse(void);          // Parse allocation class definition
 131 
 132   // Parse components of the definition section
 133   void int_def_parse(void);              // Parse an integer definition
 134 
 135   // Parse components of a pipeline rule
 136   void resource_parse(PipelineForm &pipe);   // Parse resource definition
 137   void pipe_desc_parse(PipelineForm &pipe);  // Parse pipeline description definition
 138   void pipe_class_parse(PipelineForm &pipe); // Parse pipeline class definition
 139 
 140   // Parse components of a peephole rule
 141   void peep_match_parse(Peephole &peep);     // Parse the peephole match rule
 142   void peep_constraint_parse(Peephole &peep);// Parse the peephole constraints
 143   void peep_replace_parse(Peephole &peep);   // Parse peephole replacement rule
 144 
 145   // Parse the peep match rule tree
 146   InstructForm *peep_match_child_parse(PeepMatch &match, int parent, int &position, int input);
 147 
 148   // Parse components of an operand and/or instruction form
 149   Predicate     *pred_parse(void);       // Parse predicate rule
 150   // Parse match rule, and internal nodes
 151   MatchRule     *match_parse(FormDict &operands);
 152   MatchNode     *matchNode_parse(FormDict &operands, int &depth,
 153                                  int &numleaves, bool atroot);
 154   MatchNode     *matchChild_parse(FormDict &operands, int &depth,
 155                                   int &numleaves, bool atroot);
 156 
 157   Attribute     *attr_parse(char *ident);// Parse instr/operand attribute rule
 158   // Parse instruction encode rule
 159   InsEncode     *ins_encode_parse(InstructForm &inst);
 160   InsEncode     *ins_encode_parse_block(InstructForm &inst);
 161   Opcode        *opcode_parse(InstructForm *insr); // Parse instruction opcode
 162   char          *size_parse(InstructForm *insr); // Parse instruction size
 163   Interface     *interface_parse();      // Parse operand interface rule
 164   Interface     *mem_interface_parse();  // Parse memory interface rule
 165   Interface     *cond_interface_parse(); // Parse conditional interface rule
 166   char          *interface_field_parse();// Parse field contents
 167 
 168   FormatRule    *format_parse(void);     // Parse format rule
 169   void           effect_parse(InstructForm *instr); // Parse effect rule
 170   ExpandRule    *expand_parse(InstructForm *instr); // Parse expand rule
 171   RewriteRule   *rewrite_parse(void);    // Parse rewrite rule
 172   Constraint    *constraint_parse(void); // Parse constraint rule
 173   ConstructRule *construct_parse(void);  // Parse construct rule
 174   void           ins_pipe_parse(InstructForm &instr); // Parse ins_pipe rule
 175 
 176   // ***** Preprocessor functions *****
 177   void begin_if_def(bool taken) {
 178     assert(_preproc_depth < _preproc_limit, "#ifdef nesting limit");
 179     int ppn = _preproc_depth++;
 180     _preproc_taken[ppn] = taken;
 181     // Invariant:  _preproc_not_taken = SUM !_preproc_taken[0.._preproc_depth)
 182     if (!_preproc_taken[ppn])  _preproc_not_taken += 1;
 183     _preproc_else[ppn] = false;
 184   }
 185   void invert_if_def() {
 186     assert(_preproc_depth > 0, "#ifdef matching");
 187     int ppn = _preproc_depth - 1;
 188     assert(!_preproc_else[ppn], "multiple #else lines");
 189     _preproc_else[ppn] = true;
 190     if (!_preproc_taken[ppn])  _preproc_not_taken -= 1;
 191     _preproc_taken[ppn] = !_preproc_taken[ppn];
 192     if (!_preproc_taken[ppn])  _preproc_not_taken += 1;
 193   }
 194   void end_if_def() {
 195     assert(_preproc_depth > 0, "#ifdef matching");
 196     int ppn = --_preproc_depth;
 197     if (!_preproc_taken[ppn])  _preproc_not_taken -= 1;
 198   }
 199   bool preproc_taken() {
 200     // Return true only if there is no directive hiding this text position.
 201     return _preproc_not_taken == 0;
 202   }
 203   // Handle a '#' token.  Return true if it disappeared.
 204   bool handle_preproc_token();
 205 
 206   // ***** Utility Functions for ADL Parser ******
 207 
 208   // Parse one string argument inside parens:  '(' string ')' ';'
 209   char *parse_one_arg(const char *description);
 210 
 211   // Return the next identifier given a pointer into a line of the buffer.
 212   char *get_ident()            { return get_ident_common(true); }
 213   char *get_ident_no_preproc() { return get_ident_common(false); }
 214   char *get_ident_common(bool do_preproc);      // Grab it from the file buffer
 215   char *get_ident_dup(void);    // Grab a duplicate of the identifier
 216   char *get_ident_or_literal_constant(const char* description);
 217   // Grab unique identifier from file buffer
 218   char *get_unique_ident(FormDict &dict, const char *nameDescription);
 219   // Return the next replacement variable identifier
 220   char *get_rep_var_ident(void);
 221   // Skip first '$' and make a duplicate of the string
 222   char *get_rep_var_ident_dup(void);     
 223   // Return the next token given as a signed integer.
 224   int   get_int(void);
 225   // Return the next token, a relational operator { ==, !=, <=, >= }
 226   char *get_relation_dup(void);
 227 
 228   void  get_oplist(NameList &parameters, FormDict &operands);// Parse type-operand pairs 
 229   void  get_effectlist(FormDict &effects, FormDict &operands); // Parse effect-operand pairs 
 230   // Return the contents of a parenthesized expression.
 231   // Requires initial '(' and consumes final ')', which is replaced by '\0'.
 232   char *get_paren_expr(const char *description);
 233   // Return expression up to next stop-char, which terminator replaces.
 234   // Does not require initial '('.  Does not consume final stop-char.
 235   // Final stop-char is left in _curchar, but is also is replaced by '\0'.
 236   char *get_expr(const char *description, const char *stop_chars);
 237   char *find_cpp_block(const char *description); // Parse a C++ code block
 238   // Issue parser error message & go to EOL
 239   void parse_err(int flag, const char *fmt, ...);
 240 
 241   // Return pointer to current character
 242   inline char  cur_char(void);
 243   // Advance to next character, assign this to _curchar
 244   inline void  next_char(void);
 245   inline void  next_char_or_line(void);
 246   // Advance File Buffer to next line, updating _curline
 247   inline void  next_line(void);
 248   // Issue an error if we are not at the beginning of a line (exc. whitespace).
 249   void ensure_start_of_line(void);
 250   // Issue an error if we are not at the end of a line (exc. whitespace).
 251   void ensure_end_of_line(void);
 252   // Skip whitespace, leaving ptr pointing to first non-whitespace character
 253   // Also handle preprocessor constructs like "#ifdef".
 254   void skipws()                { skipws_common(true); }
 255   // Skip comments and spaces but not newlines or preprocessor constructs.
 256   void skipws_no_preproc()     { skipws_common(false); }
 257   void skipws_common(bool do_preproc);
 258 
 259   FileBuff &_buf;               // File buffer to be parsed
 260   ArchDesc &_AD;                // Architecture Description being built
 261 
 262 public:
 263 
 264   ADLParser(FileBuff &buf, ArchDesc &archDesc); // Create new ADLParser object
 265   ~ADLParser();                 // Destroy ADLParser object
 266 
 267   void parse(void);             // Do the parsing & build forms lists
 268 
 269   int getlines( ) { return _linenum; }
 270 
 271   static bool is_literal_constant(const char *hex_string);
 272   static bool is_hex_digit(char digit);
 273   static bool is_int_token(const char* token, int& intval);
 274   static void trim(char* &token);  // trim leading & trailing spaces
 275 };
 276 
 277