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