Print this page
rev 1838 : 6961690: load oops from constant table on SPARC
Summary: oops should be loaded from the constant table of an nmethod instead of materializing them with a long code sequence.
Reviewed-by:
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/vm/adlc/adlparse.cpp
+++ new/src/share/vm/adlc/adlparse.cpp
1 1 /*
2 - * Copyright (c) 1997, 2009, Oracle and/or its affiliates. All rights reserved.
2 + * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation.
8 8 *
9 9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 12 * version 2 for more details (a copy is included in the LICENSE file that
13 13 * accompanied this code).
14 14 *
15 15 * You should have received a copy of the GNU General Public License version
16 16 * 2 along with this work; if not, write to the Free Software Foundation,
17 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 18 *
19 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 20 * or visit www.oracle.com if you need additional information or have any
21 21 * questions.
22 22 *
23 23 */
24 24
25 25 // ADLPARSE.CPP - Architecture Description Language Parser
26 26 // Authors: Chris Vick and Mike Paleczny
27 27 #include "adlc.hpp"
28 28
29 29 //----------------------------ADLParser----------------------------------------
30 30 // Create a new ADL parser
31 31 ADLParser::ADLParser(FileBuff& buffer, ArchDesc& archDesc)
32 32 : _buf(buffer), _AD(archDesc),
33 33 _globalNames(archDesc.globalNames()) {
34 34 _AD._syntax_errs = _AD._semantic_errs = 0; // No errors so far this file
35 35 _AD._warnings = 0; // No warnings either
36 36 _curline = _ptr = NULL; // No pointers into buffer yet
37 37
38 38 _preproc_depth = 0;
39 39 _preproc_not_taken = 0;
40 40
41 41 // Delimit command-line definitions from in-file definitions:
42 42 _AD._preproc_list.add_signal();
43 43 }
44 44
45 45 //------------------------------~ADLParser-------------------------------------
46 46 // Delete an ADL parser.
47 47 ADLParser::~ADLParser() {
48 48 if (!_AD._quiet_mode)
49 49 fprintf(stderr,"---------------------------- Errors and Warnings ----------------------------\n");
50 50 #ifndef ASSERT
51 51 fprintf(stderr, "**************************************************************\n");
52 52 fprintf(stderr, "***** WARNING: ASSERT is undefined, assertions disabled. *****\n");
53 53 fprintf(stderr, "**************************************************************\n");
54 54 #endif
55 55 if( _AD._syntax_errs + _AD._semantic_errs + _AD._warnings == 0 ) {
56 56 if (!_AD._quiet_mode)
57 57 fprintf(stderr,"No errors or warnings to report from phase-1 parse.\n" );
58 58 }
59 59 else {
60 60 if( _AD._syntax_errs ) { // Any syntax errors?
61 61 fprintf(stderr,"%s: Found %d syntax error", _buf._fp->_name, _AD._syntax_errs);
62 62 if( _AD._syntax_errs > 1 ) fprintf(stderr,"s.\n\n");
63 63 else fprintf(stderr,".\n\n");
64 64 }
65 65 if( _AD._semantic_errs ) { // Any semantic errors?
66 66 fprintf(stderr,"%s: Found %d semantic error", _buf._fp->_name, _AD._semantic_errs);
67 67 if( _AD._semantic_errs > 1 ) fprintf(stderr,"s.\n\n");
68 68 else fprintf(stderr,".\n\n");
69 69 }
70 70 if( _AD._warnings ) { // Any warnings?
71 71 fprintf(stderr,"%s: Found %d warning", _buf._fp->_name, _AD._warnings);
72 72 if( _AD._warnings > 1 ) fprintf(stderr,"s.\n\n");
73 73 else fprintf(stderr,".\n\n");
74 74 }
75 75 }
76 76 if (!_AD._quiet_mode)
77 77 fprintf(stderr,"-----------------------------------------------------------------------------\n");
78 78 _AD._TotalLines += linenum()-1; // -1 for overshoot in "nextline" routine
79 79
80 80 // Write out information we have stored
81 81 // // UNIXism == fsync(stderr);
82 82 }
83 83
84 84 //------------------------------parse------------------------------------------
85 85 // Each top-level keyword should appear as the first non-whitespace on a line.
86 86 //
87 87 void ADLParser::parse() {
↓ open down ↓ |
75 lines elided |
↑ open up ↑ |
88 88 char *ident;
89 89
90 90 // Iterate over the lines in the file buffer parsing Level 1 objects
91 91 for( next_line(); _curline != NULL; next_line()) {
92 92 _ptr = _curline; // Reset ptr to start of new line
93 93 skipws(); // Skip any leading whitespace
94 94 ident = get_ident(); // Get first token
95 95 if (ident == NULL) { // Empty line
96 96 continue; // Get the next line
97 97 }
98 - if (!strcmp(ident, "instruct")) instr_parse();
98 + if (!strcmp(ident, "instruct")) instr_parse();
99 99 else if (!strcmp(ident, "operand")) oper_parse();
100 100 else if (!strcmp(ident, "opclass")) opclass_parse();
101 101 else if (!strcmp(ident, "ins_attrib")) ins_attr_parse();
102 102 else if (!strcmp(ident, "op_attrib")) op_attr_parse();
103 103 else if (!strcmp(ident, "source")) source_parse();
104 104 else if (!strcmp(ident, "source_hpp")) source_hpp_parse();
105 105 else if (!strcmp(ident, "register")) reg_parse();
106 106 else if (!strcmp(ident, "frame")) frame_parse();
107 107 else if (!strcmp(ident, "encode")) encode_parse();
108 108 else if (!strcmp(ident, "pipeline")) pipe_parse();
109 109 else if (!strcmp(ident, "definitions")) definitions_parse();
110 110 else if (!strcmp(ident, "peephole")) peep_parse();
111 111 else if (!strcmp(ident, "#line")) preproc_line();
112 112 else if (!strcmp(ident, "#define")) preproc_define();
113 113 else if (!strcmp(ident, "#undef")) preproc_undef();
114 114 else {
115 115 parse_err(SYNERR, "expected one of - instruct, operand, ins_attrib, op_attrib, source, register, pipeline, encode\n Found %s",ident);
116 116 }
117 117 }
118 118
119 119 // Done with parsing, check consistency.
120 120
121 121 if (_preproc_depth != 0) {
122 122 parse_err(SYNERR, "End of file inside #ifdef");
123 123 }
124 124
125 125 // AttributeForms ins_cost and op_cost must be defined for default behaviour
126 126 if (_globalNames[AttributeForm::_ins_cost] == NULL) {
127 127 parse_err(SEMERR, "Did not declare 'ins_cost' attribute");
128 128 }
129 129 if (_globalNames[AttributeForm::_ins_pc_relative] == NULL) {
130 130 parse_err(SEMERR, "Did not declare 'ins_pc_relative' attribute");
131 131 }
132 132 if (_globalNames[AttributeForm::_op_cost] == NULL) {
133 133 parse_err(SEMERR, "Did not declare 'op_cost' attribute");
134 134 }
135 135 }
136 136
137 137 // ******************** Private Level 1 Parse Functions ********************
138 138 //------------------------------instr_parse------------------------------------
139 139 // Parse the contents of an instruction definition, build the InstructForm to
140 140 // represent that instruction, and add it to the InstructForm list.
141 141 void ADLParser::instr_parse(void) {
142 142 char *ident;
143 143 InstructForm *instr;
144 144 MatchRule *rule;
145 145 int match_rules_cnt = 0;
146 146
147 147 // First get the name of the instruction
148 148 if( (ident = get_unique_ident(_globalNames,"instruction")) == NULL )
149 149 return;
150 150 instr = new InstructForm(ident); // Create new instruction form
151 151 instr->_linenum = linenum();
152 152 _globalNames.Insert(ident, instr); // Add name to the name table
153 153 // Debugging Stuff
154 154 if (_AD._adl_debug > 1)
155 155 fprintf(stderr,"Parsing Instruction Form %s\n", ident);
156 156
157 157 // Then get the operands
158 158 skipws();
159 159 if (_curchar != '(') {
160 160 parse_err(SYNERR, "missing '(' in instruct definition\n");
161 161 }
162 162 // Parse the operand list
163 163 else get_oplist(instr->_parameters, instr->_localNames);
164 164 skipws(); // Skip leading whitespace
165 165 // Check for block delimiter
166 166 if ( (_curchar != '%')
167 167 || ( next_char(), (_curchar != '{')) ) {
168 168 parse_err(SYNERR, "missing '%{' in instruction definition\n");
169 169 return;
170 170 }
171 171 next_char(); // Maintain the invariant
172 172 do {
173 173 ident = get_ident(); // Grab next identifier
174 174 if (ident == NULL) {
175 175 parse_err(SYNERR, "keyword identifier expected at %c\n", _curchar);
176 176 continue;
177 177 }
178 178 if (!strcmp(ident, "predicate")) instr->_predicate = pred_parse();
179 179 else if (!strcmp(ident, "match")) {
180 180 // Allow one instruction have several match rules.
181 181 rule = instr->_matrule;
182 182 if (rule == NULL) {
183 183 // This is first match rule encountered
184 184 rule = match_parse(instr->_localNames);
185 185 if (rule) {
186 186 instr->_matrule = rule;
187 187 // Special case the treatment of Control instructions.
188 188 if( instr->is_ideal_control() ) {
189 189 // Control instructions return a special result, 'Universe'
190 190 rule->_result = "Universe";
191 191 }
192 192 // Check for commutative operations with tree operands.
193 193 matchrule_clone_and_swap(rule, instr->_ident, match_rules_cnt);
194 194 }
195 195 } else {
196 196 // Find the end of the match rule list
197 197 while (rule->_next != NULL)
198 198 rule = rule->_next;
199 199 // Add the new match rule to the list
200 200 rule->_next = match_parse(instr->_localNames);
201 201 if (rule->_next) {
202 202 rule = rule->_next;
203 203 if( instr->is_ideal_control() ) {
204 204 parse_err(SYNERR, "unique match rule expected for %s\n", rule->_name);
205 205 return;
206 206 }
207 207 assert(match_rules_cnt < 100," too many match rule clones");
208 208 char* buf = (char*) malloc(strlen(instr->_ident) + 4);
↓ open down ↓ |
100 lines elided |
↑ open up ↑ |
209 209 sprintf(buf, "%s_%d", instr->_ident, match_rules_cnt++);
210 210 rule->_result = buf;
211 211 // Check for commutative operations with tree operands.
212 212 matchrule_clone_and_swap(rule, instr->_ident, match_rules_cnt);
213 213 }
214 214 }
215 215 }
216 216 else if (!strcmp(ident, "encode")) {
217 217 parse_err(SYNERR, "Instructions specify ins_encode, not encode\n");
218 218 }
219 - else if (!strcmp(ident, "ins_encode"))
220 - instr->_insencode = ins_encode_parse(*instr);
221 - else if (!strcmp(ident, "opcode")) instr->_opcode = opcode_parse(instr);
222 - else if (!strcmp(ident, "size")) instr->_size = size_parse(instr);
223 - else if (!strcmp(ident, "effect")) effect_parse(instr);
224 - else if (!strcmp(ident, "expand")) instr->_exprule = expand_parse(instr);
225 - else if (!strcmp(ident, "rewrite")) instr->_rewrule = rewrite_parse();
219 + else if (!strcmp(ident, "ins_encode")) ins_encode_parse(*instr);
220 + else if (!strcmp(ident, "opcode")) instr->_opcode = opcode_parse(instr);
221 + else if (!strcmp(ident, "size")) instr->_size = size_parse(instr);
222 + else if (!strcmp(ident, "effect")) effect_parse(instr);
223 + else if (!strcmp(ident, "expand")) instr->_exprule = expand_parse(instr);
224 + else if (!strcmp(ident, "rewrite")) instr->_rewrule = rewrite_parse();
226 225 else if (!strcmp(ident, "constraint")) {
227 226 parse_err(SYNERR, "Instructions do not specify a constraint\n");
228 227 }
229 228 else if (!strcmp(ident, "construct")) {
230 229 parse_err(SYNERR, "Instructions do not specify a construct\n");
231 230 }
232 - else if (!strcmp(ident, "format")) instr->_format = format_parse();
231 + else if (!strcmp(ident, "format")) instr->_format = format_parse();
233 232 else if (!strcmp(ident, "interface")) {
234 233 parse_err(SYNERR, "Instructions do not specify an interface\n");
235 234 }
236 - else if (!strcmp(ident, "ins_pipe")) ins_pipe_parse(*instr);
235 + else if (!strcmp(ident, "ins_pipe")) ins_pipe_parse(*instr);
237 236 else { // Done with staticly defined parts of instruction definition
238 237 // Check identifier to see if it is the name of an attribute
239 238 const Form *form = _globalNames[ident];
240 239 AttributeForm *attr = form ? form->is_attribute() : NULL;
241 240 if( attr && (attr->_atype == INS_ATTR) ) {
242 241 // Insert the new attribute into the linked list.
243 242 Attribute *temp = attr_parse(ident);
244 243 temp->_next = instr->_attribs;
245 244 instr->_attribs = temp;
246 245 } else {
247 246 parse_err(SYNERR, "expected one of:\n predicate, match, encode, or the name of an instruction attribute at %s\n", ident);
248 247 }
249 248 }
250 249 skipws();
251 250 } while(_curchar != '%');
252 251 next_char();
253 252 if (_curchar != '}') {
254 253 parse_err(SYNERR, "missing '%}' in instruction definition\n");
255 254 return;
256 255 }
257 256 // Check for "Set" form of chain rule
258 257 adjust_set_rule(instr);
259 258 if (_AD._pipeline ) {
260 259 if( instr->expands() ) {
261 260 if( instr->_ins_pipe )
262 261 parse_err(WARN, "ins_pipe and expand rule both specified for instruction \"%s\"; ins_pipe will be unused\n", instr->_ident);
263 262 } else {
264 263 if( !instr->_ins_pipe )
265 264 parse_err(WARN, "No ins_pipe specified for instruction \"%s\"\n", instr->_ident);
266 265 }
267 266 }
268 267 // Add instruction to tail of instruction list
269 268 _AD.addForm(instr);
270 269
271 270 // Create instruction form for each additional match rule
272 271 rule = instr->_matrule;
273 272 if (rule != NULL) {
274 273 rule = rule->_next;
275 274 while (rule != NULL) {
276 275 ident = (char*)rule->_result;
277 276 InstructForm *clone = new InstructForm(ident, instr, rule); // Create new instruction form
278 277 _globalNames.Insert(ident, clone); // Add name to the name table
279 278 // Debugging Stuff
280 279 if (_AD._adl_debug > 1)
281 280 fprintf(stderr,"Parsing Instruction Form %s\n", ident);
282 281 // Check for "Set" form of chain rule
283 282 adjust_set_rule(clone);
284 283 // Add instruction to tail of instruction list
285 284 _AD.addForm(clone);
286 285 rule = rule->_next;
287 286 clone->_matrule->_next = NULL; // One match rule per clone
288 287 }
289 288 }
290 289 }
291 290
292 291 //------------------------------matchrule_clone_and_swap-----------------------
293 292 // Check for commutative operations with subtree operands,
294 293 // create clones and swap operands.
295 294 void ADLParser::matchrule_clone_and_swap(MatchRule* rule, const char* instr_ident, int& match_rules_cnt) {
296 295 // Check for commutative operations with tree operands.
297 296 int count = 0;
298 297 rule->count_commutative_op(count);
299 298 if (count > 0) {
300 299 // Clone match rule and swap commutative operation's operands.
301 300 rule->matchrule_swap_commutative_op(instr_ident, count, match_rules_cnt);
302 301 }
303 302 }
304 303
305 304 //------------------------------adjust_set_rule--------------------------------
306 305 // Check for "Set" form of chain rule
307 306 void ADLParser::adjust_set_rule(InstructForm *instr) {
308 307 if (instr->_matrule == NULL || instr->_matrule->_rChild == NULL) return;
309 308 const char *rch = instr->_matrule->_rChild->_opType;
310 309 const Form *frm = _globalNames[rch];
311 310 if( (! strcmp(instr->_matrule->_opType,"Set")) &&
312 311 frm && frm->is_operand() && (! frm->ideal_only()) ) {
313 312 // Previous implementation, which missed leaP*, but worked for loadCon*
314 313 unsigned position = 0;
315 314 const char *result = NULL;
316 315 const char *name = NULL;
317 316 const char *optype = NULL;
318 317 MatchNode *right = instr->_matrule->_rChild;
319 318 if (right->base_operand(position, _globalNames, result, name, optype)) {
320 319 position = 1;
321 320 const char *result2 = NULL;
322 321 const char *name2 = NULL;
323 322 const char *optype2 = NULL;
324 323 // Can not have additional base operands in right side of match!
325 324 if ( ! right->base_operand( position, _globalNames, result2, name2, optype2) ) {
326 325 assert( instr->_predicate == NULL, "ADLC does not support instruction chain rules with predicates");
327 326 // Chain from input _ideal_operand_type_,
328 327 // Needed for shared roots of match-trees
329 328 ChainList *lst = (ChainList *)_AD._chainRules[optype];
330 329 if (lst == NULL) {
331 330 lst = new ChainList();
332 331 _AD._chainRules.Insert(optype, lst);
333 332 }
334 333 if (!lst->search(instr->_matrule->_lChild->_opType)) {
335 334 const char *cost = instr->cost();
336 335 if (cost == NULL) {
337 336 cost = ((AttributeForm*)_globalNames[AttributeForm::_ins_cost])->_attrdef;
338 337 }
339 338 // The ADLC does not support chaining from the ideal operand type
340 339 // of a predicated user-defined operand
341 340 if( frm->is_operand() == NULL || frm->is_operand()->_predicate == NULL ) {
342 341 lst->insert(instr->_matrule->_lChild->_opType,cost,instr->_ident);
343 342 }
344 343 }
345 344 // Chain from input _user_defined_operand_type_,
346 345 lst = (ChainList *)_AD._chainRules[result];
347 346 if (lst == NULL) {
348 347 lst = new ChainList();
349 348 _AD._chainRules.Insert(result, lst);
350 349 }
351 350 if (!lst->search(instr->_matrule->_lChild->_opType)) {
352 351 const char *cost = instr->cost();
353 352 if (cost == NULL) {
354 353 cost = ((AttributeForm*)_globalNames[AttributeForm::_ins_cost])->_attrdef;
355 354 }
356 355 // It is safe to chain from the top-level user-defined operand even
357 356 // if it has a predicate, since the predicate is checked before
358 357 // the user-defined type is available.
359 358 lst->insert(instr->_matrule->_lChild->_opType,cost,instr->_ident);
360 359 }
361 360 } else {
362 361 // May have instruction chain rule if root of right-tree is an ideal
363 362 OperandForm *rightOp = _globalNames[right->_opType]->is_operand();
364 363 if( rightOp ) {
365 364 const Form *rightRoot = _globalNames[rightOp->_matrule->_opType];
366 365 if( rightRoot && rightRoot->ideal_only() ) {
367 366 const char *chain_op = NULL;
368 367 if( rightRoot->is_instruction() )
369 368 chain_op = rightOp->_ident;
370 369 if( chain_op ) {
371 370 // Look-up the operation in chain rule table
372 371 ChainList *lst = (ChainList *)_AD._chainRules[chain_op];
373 372 if (lst == NULL) {
374 373 lst = new ChainList();
375 374 _AD._chainRules.Insert(chain_op, lst);
376 375 }
377 376 // if (!lst->search(instr->_matrule->_lChild->_opType)) {
378 377 const char *cost = instr->cost();
379 378 if (cost == NULL) {
380 379 cost = ((AttributeForm*)_globalNames[AttributeForm::_ins_cost])->_attrdef;
381 380 }
382 381 // This chains from a top-level operand whose predicate, if any,
383 382 // has been checked.
384 383 lst->insert(instr->_matrule->_lChild->_opType,cost,instr->_ident);
385 384 // }
386 385 }
387 386 }
388 387 }
389 388 } // end chain rule from right-tree's ideal root
390 389 }
391 390 }
392 391 }
393 392
394 393
395 394 //------------------------------oper_parse-------------------------------------
396 395 void ADLParser::oper_parse(void) {
397 396 char *ident;
398 397 OperandForm *oper;
399 398 AttributeForm *attr;
400 399 MatchRule *rule;
401 400
402 401 // First get the name of the operand
403 402 skipws();
404 403 if( (ident = get_unique_ident(_globalNames,"operand")) == NULL )
405 404 return;
406 405 oper = new OperandForm(ident); // Create new operand form
407 406 oper->_linenum = linenum();
408 407 _globalNames.Insert(ident, oper); // Add name to the name table
409 408
410 409 // Debugging Stuff
411 410 if (_AD._adl_debug > 1) fprintf(stderr,"Parsing Operand Form %s\n", ident);
412 411
413 412 // Get the component operands
414 413 skipws();
415 414 if (_curchar != '(') {
416 415 parse_err(SYNERR, "missing '(' in operand definition\n");
417 416 return;
418 417 }
419 418 else get_oplist(oper->_parameters, oper->_localNames); // Parse the component operand list
420 419 skipws();
421 420 // Check for block delimiter
422 421 if ((_curchar != '%') || (*(_ptr+1) != '{')) { // If not open block
423 422 parse_err(SYNERR, "missing '%c{' in operand definition\n","%");
424 423 return;
425 424 }
426 425 next_char(); next_char(); // Skip over "%{" symbol
427 426 do {
428 427 ident = get_ident(); // Grab next identifier
429 428 if (ident == NULL) {
430 429 parse_err(SYNERR, "keyword identifier expected at %c\n", _curchar);
431 430 continue;
432 431 }
433 432 if (!strcmp(ident, "predicate")) oper->_predicate = pred_parse();
434 433 else if (!strcmp(ident, "match")) {
435 434 // Find the end of the match rule list
436 435 rule = oper->_matrule;
437 436 if (rule) {
438 437 while (rule->_next) rule = rule->_next;
439 438 // Add the new match rule to the list
440 439 rule->_next = match_parse(oper->_localNames);
441 440 if (rule->_next) {
442 441 rule->_next->_result = oper->_ident;
443 442 }
444 443 }
445 444 else {
446 445 // This is first match rule encountered
447 446 oper->_matrule = match_parse(oper->_localNames);
448 447 if (oper->_matrule) {
449 448 oper->_matrule->_result = oper->_ident;
450 449 }
451 450 }
452 451 }
453 452 else if (!strcmp(ident, "encode")) oper->_interface = interface_parse();
454 453 else if (!strcmp(ident, "ins_encode")) {
455 454 parse_err(SYNERR, "Operands specify 'encode', not 'ins_encode'\n");
456 455 }
457 456 else if (!strcmp(ident, "opcode")) {
458 457 parse_err(SYNERR, "Operands do not specify an opcode\n");
459 458 }
460 459 else if (!strcmp(ident, "effect")) {
461 460 parse_err(SYNERR, "Operands do not specify an effect\n");
462 461 }
463 462 else if (!strcmp(ident, "expand")) {
464 463 parse_err(SYNERR, "Operands do not specify an expand\n");
465 464 }
466 465 else if (!strcmp(ident, "rewrite")) {
467 466 parse_err(SYNERR, "Operands do not specify a rewrite\n");
468 467 }
469 468 else if (!strcmp(ident, "constraint"))oper->_constraint= constraint_parse();
470 469 else if (!strcmp(ident, "construct")) oper->_construct = construct_parse();
471 470 else if (!strcmp(ident, "format")) oper->_format = format_parse();
472 471 else if (!strcmp(ident, "interface")) oper->_interface = interface_parse();
473 472 // Check identifier to see if it is the name of an attribute
474 473 else if (((attr = _globalNames[ident]->is_attribute()) != NULL) &&
475 474 (attr->_atype == OP_ATTR)) oper->_attribs = attr_parse(ident);
476 475 else {
477 476 parse_err(SYNERR, "expected one of - constraint, predicate, match, encode, format, construct, or the name of a defined operand attribute at %s\n", ident);
478 477 }
479 478 skipws();
480 479 } while(_curchar != '%');
481 480 next_char();
482 481 if (_curchar != '}') {
483 482 parse_err(SYNERR, "missing '%}' in operand definition\n");
484 483 return;
485 484 }
486 485 // Add operand to tail of operand list
487 486 _AD.addForm(oper);
488 487 }
489 488
490 489 //------------------------------opclass_parse----------------------------------
491 490 // Operand Classes are a block with a comma delimited list of operand names
492 491 void ADLParser::opclass_parse(void) {
493 492 char *ident;
494 493 OpClassForm *opc;
495 494 OperandForm *opForm;
496 495
497 496 // First get the name of the operand class
498 497 skipws();
499 498 if( (ident = get_unique_ident(_globalNames,"opclass")) == NULL )
500 499 return;
501 500 opc = new OpClassForm(ident); // Create new operand class form
502 501 _globalNames.Insert(ident, opc); // Add name to the name table
503 502
504 503 // Debugging Stuff
505 504 if (_AD._adl_debug > 1)
506 505 fprintf(stderr,"Parsing Operand Class Form %s\n", ident);
507 506
508 507 // Get the list of operands
509 508 skipws();
510 509 if (_curchar != '(') {
511 510 parse_err(SYNERR, "missing '(' in operand definition\n");
512 511 return;
513 512 }
514 513 do {
515 514 next_char(); // Skip past open paren or comma
516 515 ident = get_ident(); // Grab next identifier
517 516 if (ident == NULL) {
518 517 parse_err(SYNERR, "keyword identifier expected at %c\n", _curchar);
519 518 continue;
520 519 }
521 520 // Check identifier to see if it is the name of an operand
522 521 const Form *form = _globalNames[ident];
523 522 opForm = form ? form->is_operand() : NULL;
524 523 if ( opForm ) {
525 524 opc->_oplst.addName(ident); // Add operand to opclass list
526 525 opForm->_classes.addName(opc->_ident);// Add opclass to operand list
527 526 }
528 527 else {
529 528 parse_err(SYNERR, "expected name of a defined operand at %s\n", ident);
530 529 }
531 530 skipws(); // skip trailing whitespace
532 531 } while (_curchar == ','); // Check for the comma
533 532 // Check for closing ')'
534 533 if (_curchar != ')') {
535 534 parse_err(SYNERR, "missing ')' or ',' in opclass definition\n");
536 535 return;
537 536 }
538 537 next_char(); // Consume the ')'
539 538 skipws();
540 539 // Check for closing ';'
541 540 if (_curchar != ';') {
542 541 parse_err(SYNERR, "missing ';' in opclass definition\n");
543 542 return;
544 543 }
545 544 next_char(); // Consume the ';'
546 545 // Add operand to tail of operand list
547 546 _AD.addForm(opc);
548 547 }
549 548
550 549 //------------------------------ins_attr_parse---------------------------------
551 550 void ADLParser::ins_attr_parse(void) {
552 551 char *ident;
553 552 char *aexpr;
554 553 AttributeForm *attrib;
555 554
556 555 // get name for the instruction attribute
557 556 skipws(); // Skip leading whitespace
558 557 if( (ident = get_unique_ident(_globalNames,"inst_attrib")) == NULL )
559 558 return;
560 559 // Debugging Stuff
561 560 if (_AD._adl_debug > 1) fprintf(stderr,"Parsing Ins_Attribute Form %s\n", ident);
562 561
563 562 // Get default value of the instruction attribute
564 563 skipws(); // Skip whitespace
565 564 if ((aexpr = get_paren_expr("attribute default expression string")) == NULL) {
566 565 parse_err(SYNERR, "missing '(' in ins_attrib definition\n");
567 566 return;
568 567 }
569 568 // Debug Stuff
570 569 if (_AD._adl_debug > 1) fprintf(stderr,"Attribute Expression: %s\n", aexpr);
571 570
572 571 // Check for terminator
573 572 if (_curchar != ';') {
574 573 parse_err(SYNERR, "missing ';' in ins_attrib definition\n");
575 574 return;
576 575 }
577 576 next_char(); // Advance past the ';'
578 577
579 578 // Construct the attribute, record global name, and store in ArchDesc
580 579 attrib = new AttributeForm(ident, INS_ATTR, aexpr);
581 580 _globalNames.Insert(ident, attrib); // Add name to the name table
582 581 _AD.addForm(attrib);
583 582 }
584 583
585 584 //------------------------------op_attr_parse----------------------------------
586 585 void ADLParser::op_attr_parse(void) {
587 586 char *ident;
588 587 char *aexpr;
589 588 AttributeForm *attrib;
590 589
591 590 // get name for the operand attribute
592 591 skipws(); // Skip leading whitespace
593 592 if( (ident = get_unique_ident(_globalNames,"op_attrib")) == NULL )
594 593 return;
595 594 // Debugging Stuff
596 595 if (_AD._adl_debug > 1) fprintf(stderr,"Parsing Op_Attribute Form %s\n", ident);
597 596
598 597 // Get default value of the instruction attribute
599 598 skipws(); // Skip whitespace
600 599 if ((aexpr = get_paren_expr("attribute default expression string")) == NULL) {
601 600 parse_err(SYNERR, "missing '(' in op_attrib definition\n");
602 601 return;
603 602 }
604 603 // Debug Stuff
605 604 if (_AD._adl_debug > 1) fprintf(stderr,"Attribute Expression: %s\n", aexpr);
606 605
607 606 // Check for terminator
608 607 if (_curchar != ';') {
609 608 parse_err(SYNERR, "missing ';' in op_attrib definition\n");
610 609 return;
611 610 }
612 611 next_char(); // Advance past the ';'
613 612
614 613 // Construct the attribute, record global name, and store in ArchDesc
615 614 attrib = new AttributeForm(ident, OP_ATTR, aexpr);
616 615 _globalNames.Insert(ident, attrib);
617 616 _AD.addForm(attrib);
618 617 }
619 618
620 619 //------------------------------definitions_parse-----------------------------------
621 620 void ADLParser::definitions_parse(void) {
622 621 skipws(); // Skip leading whitespace
623 622 if (_curchar == '%' && *(_ptr+1) == '{') {
624 623 next_char(); next_char(); // Skip "%{"
625 624 skipws();
626 625 while (_curchar != '%' && *(_ptr+1) != '}') {
627 626 // Process each definition until finding closing string "%}"
628 627 char *token = get_ident();
629 628 if (token == NULL) {
630 629 parse_err(SYNERR, "missing identifier inside definitions block.\n");
631 630 return;
632 631 }
633 632 if (strcmp(token,"int_def")==0) { int_def_parse(); }
634 633 // if (strcmp(token,"str_def")==0) { str_def_parse(); }
635 634 skipws();
636 635 }
637 636 }
638 637 else {
639 638 parse_err(SYNERR, "Missing %%{ ... %%} block after definitions keyword.\n");
640 639 return;
641 640 }
642 641 }
643 642
644 643 //------------------------------int_def_parse----------------------------------
645 644 // Parse Example:
646 645 // int_def MEMORY_REF_COST ( 200, DEFAULT_COST * 2);
647 646 // <keyword> <name> ( <int_value>, <description> );
648 647 //
649 648 void ADLParser::int_def_parse(void) {
650 649 char *name = NULL; // Name of definition
651 650 char *value = NULL; // its value,
652 651 int int_value = -1; // positive values only
653 652 char *description = NULL; // textual description
654 653
655 654 // Get definition name
656 655 skipws(); // Skip whitespace
657 656 name = get_ident();
658 657 if (name == NULL) {
659 658 parse_err(SYNERR, "missing definition name after int_def\n");
660 659 return;
661 660 }
662 661
663 662 // Check for value of int_def dname( integer_value [, string_expression ] )
664 663 skipws();
665 664 if (_curchar == '(') {
666 665
667 666 // Parse the integer value.
668 667 next_char();
669 668 value = get_ident();
670 669 if (value == NULL) {
671 670 parse_err(SYNERR, "missing value in int_def\n");
672 671 return;
673 672 }
674 673 if( !is_int_token(value, int_value) ) {
675 674 parse_err(SYNERR, "value in int_def is not recognized as integer\n");
676 675 return;
677 676 }
678 677 skipws();
679 678
680 679 // Check for description
681 680 if (_curchar == ',') {
682 681 next_char(); // skip ','
683 682
684 683 description = get_expr("int_def description", ")");
685 684 if (description == NULL) {
686 685 parse_err(SYNERR, "invalid or missing description in int_def\n");
687 686 return;
688 687 }
689 688 trim(description);
690 689 }
691 690
692 691 if (_curchar != ')') {
693 692 parse_err(SYNERR, "missing ')' in register definition statement\n");
694 693 return;
695 694 }
696 695 next_char();
697 696 }
698 697
699 698 // Check for closing ';'
700 699 skipws();
701 700 if (_curchar != ';') {
702 701 parse_err(SYNERR, "missing ';' after int_def\n");
703 702 return;
704 703 }
705 704 next_char(); // move past ';'
706 705
707 706 // Debug Stuff
708 707 if (_AD._adl_debug > 1) {
709 708 fprintf(stderr,"int_def: %s ( %s, %s )\n", name,
710 709 (value), (description ? description : ""));
711 710 }
712 711
713 712 // Record new definition.
714 713 Expr *expr = new Expr(name, description, int_value, int_value);
715 714 const Expr *old_expr = _AD.globalDefs().define(name, expr);
716 715 if (old_expr != NULL) {
717 716 parse_err(SYNERR, "Duplicate definition\n");
718 717 return;
719 718 }
720 719
721 720 return;
722 721 }
723 722
724 723
725 724 //------------------------------source_parse-----------------------------------
726 725 void ADLParser::source_parse(void) {
727 726 SourceForm *source; // Encode class for instruction/operand
728 727 char *rule = NULL; // String representation of encode rule
729 728
730 729 skipws(); // Skip leading whitespace
731 730 if ( (rule = find_cpp_block("source block")) == NULL ) {
732 731 parse_err(SYNERR, "incorrect or missing block for 'source'.\n");
733 732 return;
734 733 }
735 734 // Debug Stuff
736 735 if (_AD._adl_debug > 1) fprintf(stderr,"Source Form: %s\n", rule);
737 736
738 737 source = new SourceForm(rule); // Build new Source object
739 738 _AD.addForm(source);
740 739 // skipws();
741 740 }
742 741
743 742 //------------------------------source_hpp_parse-------------------------------
744 743 // Parse a source_hpp %{ ... %} block.
745 744 // The code gets stuck into the ad_<arch>.hpp file.
746 745 // If the source_hpp block appears before the register block in the AD
747 746 // file, it goes up at the very top of the ad_<arch>.hpp file, so that
748 747 // it can be used by register encodings, etc. Otherwise, it goes towards
749 748 // the bottom, where it's useful as a global definition to *.cpp files.
750 749 void ADLParser::source_hpp_parse(void) {
751 750 char *rule = NULL; // String representation of encode rule
752 751
753 752 skipws(); // Skip leading whitespace
754 753 if ( (rule = find_cpp_block("source_hpp block")) == NULL ) {
755 754 parse_err(SYNERR, "incorrect or missing block for 'source_hpp'.\n");
756 755 return;
757 756 }
758 757 // Debug Stuff
759 758 if (_AD._adl_debug > 1) fprintf(stderr,"Header Form: %s\n", rule);
760 759
761 760 if (_AD.get_registers() == NULL) {
762 761 // Very early in the file, before reg_defs, we collect pre-headers.
763 762 PreHeaderForm* pre_header = new PreHeaderForm(rule);
764 763 _AD.addForm(pre_header);
765 764 } else {
766 765 // Normally, we collect header info, placed at the bottom of the hpp file.
767 766 HeaderForm* header = new HeaderForm(rule);
768 767 _AD.addForm(header);
769 768 }
770 769 }
771 770
772 771 //------------------------------reg_parse--------------------------------------
773 772 void ADLParser::reg_parse(void) {
774 773
775 774 // Create the RegisterForm for the architecture description.
776 775 RegisterForm *regBlock = new RegisterForm(); // Build new Source object
777 776 regBlock->_linenum = linenum();
778 777 _AD.addForm(regBlock);
779 778
780 779 skipws(); // Skip leading whitespace
781 780 if (_curchar == '%' && *(_ptr+1) == '{') {
782 781 next_char(); next_char(); // Skip "%{"
783 782 skipws();
784 783 while (_curchar != '%' && *(_ptr+1) != '}') {
785 784 char *token = get_ident();
786 785 if (token == NULL) {
787 786 parse_err(SYNERR, "missing identifier inside register block.\n");
788 787 return;
789 788 }
790 789 if (strcmp(token,"reg_def")==0) { reg_def_parse(); }
791 790 else if (strcmp(token,"reg_class")==0) { reg_class_parse(); }
792 791 else if (strcmp(token,"alloc_class")==0) { alloc_class_parse(); }
793 792 else if (strcmp(token,"#define")==0) { preproc_define(); }
794 793 else { parse_err(SYNERR, "bad token %s inside register block.\n", token); break; }
795 794 skipws();
796 795 }
797 796 }
798 797 else {
799 798 parse_err(SYNERR, "Missing %c{ ... %c} block after register keyword.\n",'%','%');
800 799 return;
801 800 }
802 801
803 802 // Add reg_class spill_regs
804 803 regBlock->addSpillRegClass();
805 804 }
806 805
807 806 //------------------------------encode_parse-----------------------------------
808 807 void ADLParser::encode_parse(void) {
809 808 EncodeForm *encBlock; // Information about instruction/operand encoding
810 809 char *desc = NULL; // String representation of encode rule
811 810
812 811 _AD.getForm(&encBlock);
813 812 if ( encBlock == NULL) {
814 813 // Create the EncodeForm for the architecture description.
815 814 encBlock = new EncodeForm(); // Build new Source object
816 815 _AD.addForm(encBlock);
817 816 }
818 817
819 818 skipws(); // Skip leading whitespace
820 819 if (_curchar == '%' && *(_ptr+1) == '{') {
821 820 next_char(); next_char(); // Skip "%{"
822 821 skipws();
823 822 while (_curchar != '%' && *(_ptr+1) != '}') {
824 823 char *token = get_ident();
825 824 if (token == NULL) {
826 825 parse_err(SYNERR, "missing identifier inside encoding block.\n");
827 826 return;
828 827 }
829 828 if (strcmp(token,"enc_class")==0) { enc_class_parse(); }
830 829 skipws();
831 830 }
832 831 }
833 832 else {
834 833 parse_err(SYNERR, "Missing %c{ ... %c} block after encode keyword.\n",'%','%');
835 834 return;
836 835 }
837 836 }
838 837
839 838 //------------------------------enc_class_parse--------------------------------
840 839 void ADLParser::enc_class_parse(void) {
841 840 char *ec_name; // Name of encoding class being defined
842 841
843 842 // Get encoding class name
844 843 skipws(); // Skip whitespace
845 844 ec_name = get_ident();
846 845 if (ec_name == NULL) {
847 846 parse_err(SYNERR, "missing encoding class name after encode.\n");
848 847 return;
849 848 }
850 849
851 850 EncClass *encoding = _AD._encode->add_EncClass(ec_name);
852 851 encoding->_linenum = linenum();
853 852
854 853 skipws(); // Skip leading whitespace
855 854 // Check for optional parameter list
856 855 if (_curchar == '(') {
857 856 do {
858 857 char *pType = NULL; // parameter type
859 858 char *pName = NULL; // parameter name
860 859
861 860 next_char(); // skip open paren & comma characters
862 861 skipws();
863 862 if (_curchar == ')') break;
864 863
865 864 // Get parameter type
866 865 pType = get_ident();
867 866 if (pType == NULL) {
868 867 parse_err(SYNERR, "parameter type expected at %c\n", _curchar);
869 868 return;
870 869 }
871 870
872 871 skipws();
873 872 // Get parameter name
874 873 pName = get_ident();
875 874 if (pName == NULL) {
876 875 parse_err(SYNERR, "parameter name expected at %c\n", _curchar);
877 876 return;
878 877 }
879 878
880 879 // Record parameter type and name
881 880 encoding->add_parameter( pType, pName );
882 881
883 882 skipws();
884 883 } while(_curchar == ',');
885 884
886 885 if (_curchar != ')') parse_err(SYNERR, "missing ')'\n");
887 886 else {
888 887 next_char(); // Skip ')'
889 888 }
890 889 } // Done with parameter list
891 890
892 891 skipws();
893 892 // Check for block starting delimiters
894 893 if ((_curchar != '%') || (*(_ptr+1) != '{')) { // If not open block
895 894 parse_err(SYNERR, "missing '%c{' in enc_class definition\n", '%');
896 895 return;
897 896 }
898 897 next_char(); // Skip '%'
899 898 next_char(); // Skip '{'
900 899
901 900 enc_class_parse_block(encoding, ec_name);
902 901 }
903 902
904 903
905 904 void ADLParser::enc_class_parse_block(EncClass* encoding, char* ec_name) {
906 905 skipws_no_preproc(); // Skip leading whitespace
907 906 // Prepend location descriptor, for debugging; cf. ADLParser::find_cpp_block
908 907 if (_AD._adlocation_debug) {
909 908 encoding->add_code(get_line_string());
910 909 }
911 910
912 911 // Collect the parts of the encode description
913 912 // (1) strings that are passed through to output
914 913 // (2) replacement/substitution variable, preceeded by a '$'
915 914 while ( (_curchar != '%') && (*(_ptr+1) != '}') ) {
916 915
917 916 // (1)
918 917 // Check if there is a string to pass through to output
919 918 char *start = _ptr; // Record start of the next string
920 919 while ((_curchar != '$') && ((_curchar != '%') || (*(_ptr+1) != '}')) ) {
921 920 // If at the start of a comment, skip past it
922 921 if( (_curchar == '/') && ((*(_ptr+1) == '/') || (*(_ptr+1) == '*')) ) {
923 922 skipws_no_preproc();
924 923 } else {
925 924 // ELSE advance to the next character, or start of the next line
926 925 next_char_or_line();
927 926 }
↓ open down ↓ |
681 lines elided |
↑ open up ↑ |
928 927 }
929 928 // If a string was found, terminate it and record in EncClass
930 929 if ( start != _ptr ) {
931 930 *_ptr = '\0'; // Terminate the string
932 931 encoding->add_code(start);
933 932 }
934 933
935 934 // (2)
936 935 // If we are at a replacement variable,
937 936 // copy it and record in EncClass
938 - if ( _curchar == '$' ) {
937 + if (_curchar == '$') {
939 938 // Found replacement Variable
940 - char *rep_var = get_rep_var_ident_dup();
939 + char* rep_var = get_rep_var_ident_dup();
941 940 // Add flag to _strings list indicating we should check _rep_vars
942 941 encoding->add_rep_var(rep_var);
943 942 }
944 943 } // end while part of format description
945 944 next_char(); // Skip '%'
946 945 next_char(); // Skip '}'
947 946
948 947 skipws();
949 948
950 949 if (_AD._adlocation_debug) {
951 950 encoding->add_code(end_line_marker());
952 951 }
953 952
954 953 // Debug Stuff
955 954 if (_AD._adl_debug > 1) fprintf(stderr,"EncodingClass Form: %s\n", ec_name);
956 955 }
957 956
958 957 //------------------------------frame_parse-----------------------------------
959 958 void ADLParser::frame_parse(void) {
960 959 FrameForm *frame; // Information about stack-frame layout
961 960 char *desc = NULL; // String representation of frame
962 961
963 962 skipws(); // Skip leading whitespace
964 963
965 964 frame = new FrameForm(); // Build new Frame object
966 965 // Check for open block sequence
967 966 skipws(); // Skip leading whitespace
968 967 if (_curchar == '%' && *(_ptr+1) == '{') {
969 968 next_char(); next_char(); // Skip "%{"
970 969 skipws();
971 970 while (_curchar != '%' && *(_ptr+1) != '}') {
972 971 char *token = get_ident();
973 972 if (token == NULL) {
974 973 parse_err(SYNERR, "missing identifier inside frame block.\n");
975 974 return;
976 975 }
977 976 if (strcmp(token,"stack_direction")==0) {
978 977 stack_dir_parse(frame);
979 978 }
980 979 if (strcmp(token,"sync_stack_slots")==0) {
981 980 sync_stack_slots_parse(frame);
982 981 }
983 982 if (strcmp(token,"frame_pointer")==0) {
984 983 frame_pointer_parse(frame, false);
985 984 }
986 985 if (strcmp(token,"interpreter_frame_pointer")==0) {
987 986 interpreter_frame_pointer_parse(frame, false);
988 987 // Add reg_class interpreter_frame_pointer_reg
989 988 if( _AD._register != NULL ) {
990 989 RegClass *reg_class = _AD._register->addRegClass("interpreter_frame_pointer_reg");
991 990 char *interpreter_frame_pointer_reg = frame->_interpreter_frame_pointer_reg;
992 991 if( interpreter_frame_pointer_reg != NULL ) {
993 992 RegDef *regDef = _AD._register->getRegDef(interpreter_frame_pointer_reg);
994 993 reg_class->addReg(regDef); // add regDef to regClass
995 994 }
996 995 }
997 996 }
998 997 if (strcmp(token,"inline_cache_reg")==0) {
999 998 inline_cache_parse(frame, false);
1000 999 // Add reg_class inline_cache_reg
1001 1000 if( _AD._register != NULL ) {
1002 1001 RegClass *reg_class = _AD._register->addRegClass("inline_cache_reg");
1003 1002 char *inline_cache_reg = frame->_inline_cache_reg;
1004 1003 if( inline_cache_reg != NULL ) {
1005 1004 RegDef *regDef = _AD._register->getRegDef(inline_cache_reg);
1006 1005 reg_class->addReg(regDef); // add regDef to regClass
1007 1006 }
1008 1007 }
1009 1008 }
1010 1009 if (strcmp(token,"compiler_method_oop_reg")==0) {
1011 1010 parse_err(WARN, "Using obsolete Token, compiler_method_oop_reg");
1012 1011 skipws();
1013 1012 }
1014 1013 if (strcmp(token,"interpreter_method_oop_reg")==0) {
1015 1014 interpreter_method_oop_parse(frame, false);
1016 1015 // Add reg_class interpreter_method_oop_reg
1017 1016 if( _AD._register != NULL ) {
1018 1017 RegClass *reg_class = _AD._register->addRegClass("interpreter_method_oop_reg");
1019 1018 char *method_oop_reg = frame->_interpreter_method_oop_reg;
1020 1019 if( method_oop_reg != NULL ) {
1021 1020 RegDef *regDef = _AD._register->getRegDef(method_oop_reg);
1022 1021 reg_class->addReg(regDef); // add regDef to regClass
1023 1022 }
1024 1023 }
1025 1024 }
1026 1025 if (strcmp(token,"cisc_spilling_operand_name")==0) {
1027 1026 cisc_spilling_operand_name_parse(frame, false);
1028 1027 }
1029 1028 if (strcmp(token,"stack_alignment")==0) {
1030 1029 stack_alignment_parse(frame);
1031 1030 }
1032 1031 if (strcmp(token,"return_addr")==0) {
1033 1032 return_addr_parse(frame, false);
1034 1033 }
1035 1034 if (strcmp(token,"in_preserve_stack_slots")==0) {
1036 1035 preserve_stack_parse(frame);
1037 1036 }
1038 1037 if (strcmp(token,"out_preserve_stack_slots")==0) {
1039 1038 parse_err(WARN, "Using obsolete token, out_preserve_stack_slots");
1040 1039 skipws();
1041 1040 }
1042 1041 if (strcmp(token,"varargs_C_out_slots_killed")==0) {
1043 1042 frame->_varargs_C_out_slots_killed = parse_one_arg("varargs C out slots killed");
1044 1043 }
1045 1044 if (strcmp(token,"calling_convention")==0) {
1046 1045 frame->_calling_convention = calling_convention_parse();
1047 1046 }
1048 1047 if (strcmp(token,"return_value")==0) {
1049 1048 frame->_return_value = return_value_parse();
1050 1049 }
1051 1050 if (strcmp(token,"c_frame_pointer")==0) {
1052 1051 frame_pointer_parse(frame, true);
1053 1052 }
1054 1053 if (strcmp(token,"c_return_addr")==0) {
1055 1054 return_addr_parse(frame, true);
1056 1055 }
1057 1056 if (strcmp(token,"c_calling_convention")==0) {
1058 1057 frame->_c_calling_convention = calling_convention_parse();
1059 1058 }
1060 1059 if (strcmp(token,"c_return_value")==0) {
1061 1060 frame->_c_return_value = return_value_parse();
1062 1061 }
1063 1062
1064 1063 skipws();
1065 1064 }
1066 1065 }
1067 1066 else {
1068 1067 parse_err(SYNERR, "Missing %c{ ... %c} block after encode keyword.\n",'%','%');
1069 1068 return;
1070 1069 }
1071 1070 // All Java versions are required, native versions are optional
1072 1071 if(frame->_frame_pointer == NULL) {
1073 1072 parse_err(SYNERR, "missing frame pointer definition in frame section.\n");
1074 1073 return;
1075 1074 }
1076 1075 // !!!!! !!!!!
1077 1076 // if(frame->_interpreter_frame_ptr_reg == NULL) {
1078 1077 // parse_err(SYNERR, "missing interpreter frame pointer definition in frame section.\n");
1079 1078 // return;
1080 1079 // }
1081 1080 if(frame->_alignment == NULL) {
1082 1081 parse_err(SYNERR, "missing alignment definition in frame section.\n");
1083 1082 return;
1084 1083 }
1085 1084 if(frame->_return_addr == NULL) {
1086 1085 parse_err(SYNERR, "missing return address location in frame section.\n");
1087 1086 return;
1088 1087 }
1089 1088 if(frame->_in_preserve_slots == NULL) {
1090 1089 parse_err(SYNERR, "missing stack slot preservation definition in frame section.\n");
1091 1090 return;
1092 1091 }
1093 1092 if(frame->_varargs_C_out_slots_killed == NULL) {
1094 1093 parse_err(SYNERR, "missing varargs C out slots killed definition in frame section.\n");
1095 1094 return;
1096 1095 }
1097 1096 if(frame->_calling_convention == NULL) {
1098 1097 parse_err(SYNERR, "missing calling convention definition in frame section.\n");
1099 1098 return;
1100 1099 }
1101 1100 if(frame->_return_value == NULL) {
1102 1101 parse_err(SYNERR, "missing return value definition in frame section.\n");
1103 1102 return;
1104 1103 }
1105 1104 // Fill natives in identically with the Java versions if not present.
1106 1105 if(frame->_c_frame_pointer == NULL) {
1107 1106 frame->_c_frame_pointer = frame->_frame_pointer;
1108 1107 }
1109 1108 if(frame->_c_return_addr == NULL) {
1110 1109 frame->_c_return_addr = frame->_return_addr;
1111 1110 frame->_c_return_addr_loc = frame->_return_addr_loc;
1112 1111 }
1113 1112 if(frame->_c_calling_convention == NULL) {
1114 1113 frame->_c_calling_convention = frame->_calling_convention;
1115 1114 }
1116 1115 if(frame->_c_return_value == NULL) {
1117 1116 frame->_c_return_value = frame->_return_value;
1118 1117 }
1119 1118
1120 1119 // Debug Stuff
1121 1120 if (_AD._adl_debug > 1) fprintf(stderr,"Frame Form: %s\n", desc);
1122 1121
1123 1122 // Create the EncodeForm for the architecture description.
1124 1123 _AD.addForm(frame);
1125 1124 // skipws();
1126 1125 }
1127 1126
1128 1127 //------------------------------stack_dir_parse--------------------------------
1129 1128 void ADLParser::stack_dir_parse(FrameForm *frame) {
1130 1129 char *direction = parse_one_arg("stack direction entry");
1131 1130 if (strcmp(direction, "TOWARDS_LOW") == 0) {
1132 1131 frame->_direction = false;
1133 1132 }
1134 1133 else if (strcmp(direction, "TOWARDS_HIGH") == 0) {
1135 1134 frame->_direction = true;
1136 1135 }
1137 1136 else {
1138 1137 parse_err(SYNERR, "invalid value inside stack direction entry.\n");
1139 1138 return;
1140 1139 }
1141 1140 }
1142 1141
1143 1142 //------------------------------sync_stack_slots_parse-------------------------
1144 1143 void ADLParser::sync_stack_slots_parse(FrameForm *frame) {
1145 1144 // Assign value into frame form
1146 1145 frame->_sync_stack_slots = parse_one_arg("sync stack slots entry");
1147 1146 }
1148 1147
1149 1148 //------------------------------frame_pointer_parse----------------------------
1150 1149 void ADLParser::frame_pointer_parse(FrameForm *frame, bool native) {
1151 1150 char *frame_pointer = parse_one_arg("frame pointer entry");
1152 1151 // Assign value into frame form
1153 1152 if (native) { frame->_c_frame_pointer = frame_pointer; }
1154 1153 else { frame->_frame_pointer = frame_pointer; }
1155 1154 }
1156 1155
1157 1156 //------------------------------interpreter_frame_pointer_parse----------------------------
1158 1157 void ADLParser::interpreter_frame_pointer_parse(FrameForm *frame, bool native) {
1159 1158 frame->_interpreter_frame_pointer_reg = parse_one_arg("interpreter frame pointer entry");
1160 1159 }
1161 1160
1162 1161 //------------------------------inline_cache_parse-----------------------------
1163 1162 void ADLParser::inline_cache_parse(FrameForm *frame, bool native) {
1164 1163 frame->_inline_cache_reg = parse_one_arg("inline cache reg entry");
1165 1164 }
1166 1165
1167 1166 //------------------------------interpreter_method_oop_parse------------------
1168 1167 void ADLParser::interpreter_method_oop_parse(FrameForm *frame, bool native) {
1169 1168 frame->_interpreter_method_oop_reg = parse_one_arg("method oop reg entry");
1170 1169 }
1171 1170
1172 1171 //------------------------------cisc_spilling_operand_parse---------------------
1173 1172 void ADLParser::cisc_spilling_operand_name_parse(FrameForm *frame, bool native) {
1174 1173 frame->_cisc_spilling_operand_name = parse_one_arg("cisc spilling operand name");
1175 1174 }
1176 1175
1177 1176 //------------------------------stack_alignment_parse--------------------------
1178 1177 void ADLParser::stack_alignment_parse(FrameForm *frame) {
1179 1178 char *alignment = parse_one_arg("stack alignment entry");
1180 1179 // Assign value into frame
1181 1180 frame->_alignment = alignment;
1182 1181 }
1183 1182
1184 1183 //------------------------------parse_one_arg-------------------------------
1185 1184 char *ADLParser::parse_one_arg(const char *description) {
1186 1185 char *token = NULL;
1187 1186 if(_curchar == '(') {
1188 1187 next_char();
1189 1188 skipws();
1190 1189 token = get_expr(description, ")");
1191 1190 if (token == NULL) {
1192 1191 parse_err(SYNERR, "missing value inside %s.\n", description);
1193 1192 return NULL;
1194 1193 }
1195 1194 next_char(); // skip the close paren
1196 1195 if(_curchar != ';') { // check for semi-colon
1197 1196 parse_err(SYNERR, "missing %c in.\n", ';', description);
1198 1197 return NULL;
1199 1198 }
1200 1199 next_char(); // skip the semi-colon
1201 1200 }
1202 1201 else {
1203 1202 parse_err(SYNERR, "Missing %c in.\n", '(', description);
1204 1203 return NULL;
1205 1204 }
1206 1205
1207 1206 trim(token);
1208 1207 return token;
1209 1208 }
1210 1209
1211 1210 //------------------------------return_addr_parse------------------------------
1212 1211 void ADLParser::return_addr_parse(FrameForm *frame, bool native) {
1213 1212 bool in_register = true;
1214 1213 if(_curchar == '(') {
1215 1214 next_char();
1216 1215 skipws();
1217 1216 char *token = get_ident();
1218 1217 if (token == NULL) {
1219 1218 parse_err(SYNERR, "missing value inside return address entry.\n");
1220 1219 return;
1221 1220 }
1222 1221 // check for valid values for stack/register
1223 1222 if (strcmp(token, "REG") == 0) {
1224 1223 in_register = true;
1225 1224 }
1226 1225 else if (strcmp(token, "STACK") == 0) {
1227 1226 in_register = false;
1228 1227 }
1229 1228 else {
1230 1229 parse_err(SYNERR, "invalid value inside return_address entry.\n");
1231 1230 return;
1232 1231 }
1233 1232 if (native) { frame->_c_return_addr_loc = in_register; }
1234 1233 else { frame->_return_addr_loc = in_register; }
1235 1234
1236 1235 // Parse expression that specifies register or stack position
1237 1236 skipws();
1238 1237 char *token2 = get_expr("return address entry", ")");
1239 1238 if (token2 == NULL) {
1240 1239 parse_err(SYNERR, "missing value inside return address entry.\n");
1241 1240 return;
1242 1241 }
1243 1242 next_char(); // skip the close paren
1244 1243 if (native) { frame->_c_return_addr = token2; }
1245 1244 else { frame->_return_addr = token2; }
1246 1245
1247 1246 if(_curchar != ';') { // check for semi-colon
1248 1247 parse_err(SYNERR, "missing %c in return address entry.\n", ';');
1249 1248 return;
1250 1249 }
1251 1250 next_char(); // skip the semi-colon
1252 1251 }
1253 1252 else {
1254 1253 parse_err(SYNERR, "Missing %c in return_address entry.\n", '(');
1255 1254 }
1256 1255 }
1257 1256
1258 1257 //------------------------------preserve_stack_parse---------------------------
1259 1258 void ADLParser::preserve_stack_parse(FrameForm *frame) {
1260 1259 if(_curchar == '(') {
1261 1260 char *token = get_paren_expr("preserve_stack_slots");
1262 1261 frame->_in_preserve_slots = token;
1263 1262
1264 1263 if(_curchar != ';') { // check for semi-colon
1265 1264 parse_err(SYNERR, "missing %c in preserve stack slot entry.\n", ';');
1266 1265 return;
1267 1266 }
1268 1267 next_char(); // skip the semi-colon
1269 1268 }
1270 1269 else {
1271 1270 parse_err(SYNERR, "Missing %c in preserve stack slot entry.\n", '(');
1272 1271 }
1273 1272 }
1274 1273
1275 1274 //------------------------------calling_convention_parse-----------------------
1276 1275 char *ADLParser::calling_convention_parse() {
1277 1276 char *desc = NULL; // String representation of calling_convention
1278 1277
1279 1278 skipws(); // Skip leading whitespace
1280 1279 if ( (desc = find_cpp_block("calling convention block")) == NULL ) {
1281 1280 parse_err(SYNERR, "incorrect or missing block for 'calling_convention'.\n");
1282 1281 }
1283 1282 return desc;
1284 1283 }
1285 1284
1286 1285 //------------------------------return_value_parse-----------------------------
1287 1286 char *ADLParser::return_value_parse() {
1288 1287 char *desc = NULL; // String representation of calling_convention
1289 1288
1290 1289 skipws(); // Skip leading whitespace
1291 1290 if ( (desc = find_cpp_block("return value block")) == NULL ) {
1292 1291 parse_err(SYNERR, "incorrect or missing block for 'return_value'.\n");
1293 1292 }
1294 1293 return desc;
1295 1294 }
1296 1295
1297 1296 //------------------------------ins_pipe_parse---------------------------------
1298 1297 void ADLParser::ins_pipe_parse(InstructForm &instr) {
1299 1298 char * ident;
1300 1299
1301 1300 skipws();
1302 1301 if ( _curchar != '(' ) { // Check for delimiter
1303 1302 parse_err(SYNERR, "missing \"(\" in ins_pipe definition\n");
1304 1303 return;
1305 1304 }
1306 1305
1307 1306 next_char();
1308 1307 ident = get_ident(); // Grab next identifier
1309 1308
1310 1309 if (ident == NULL) {
1311 1310 parse_err(SYNERR, "keyword identifier expected at %c\n", _curchar);
1312 1311 return;
1313 1312 }
1314 1313
1315 1314 skipws();
1316 1315 if ( _curchar != ')' ) { // Check for delimiter
1317 1316 parse_err(SYNERR, "missing \")\" in ins_pipe definition\n");
1318 1317 return;
1319 1318 }
1320 1319
1321 1320 next_char(); // skip the close paren
1322 1321 if(_curchar != ';') { // check for semi-colon
1323 1322 parse_err(SYNERR, "missing %c in return value entry.\n", ';');
1324 1323 return;
1325 1324 }
1326 1325 next_char(); // skip the semi-colon
1327 1326
1328 1327 // Check ident for validity
1329 1328 if (_AD._pipeline && !_AD._pipeline->_classlist.search(ident)) {
1330 1329 parse_err(SYNERR, "\"%s\" is not a valid pipeline class\n", ident);
1331 1330 return;
1332 1331 }
1333 1332
1334 1333 // Add this instruction to the list in the pipeline class
1335 1334 _AD._pipeline->_classdict[ident]->is_pipeclass()->_instructs.addName(instr._ident);
1336 1335
1337 1336 // Set the name of the pipeline class in the instruction
1338 1337 instr._ins_pipe = ident;
1339 1338 return;
1340 1339 }
1341 1340
1342 1341 //------------------------------pipe_parse-------------------------------------
1343 1342 void ADLParser::pipe_parse(void) {
1344 1343 PipelineForm *pipeline; // Encode class for instruction/operand
1345 1344 char * ident;
1346 1345
1347 1346 pipeline = new PipelineForm(); // Build new Source object
1348 1347 _AD.addForm(pipeline);
1349 1348
1350 1349 skipws(); // Skip leading whitespace
1351 1350 // Check for block delimiter
1352 1351 if ( (_curchar != '%')
1353 1352 || ( next_char(), (_curchar != '{')) ) {
1354 1353 parse_err(SYNERR, "missing '%{' in pipeline definition\n");
1355 1354 return;
1356 1355 }
1357 1356 next_char(); // Maintain the invariant
1358 1357 do {
1359 1358 ident = get_ident(); // Grab next identifier
1360 1359 if (ident == NULL) {
1361 1360 parse_err(SYNERR, "keyword identifier expected at %c\n", _curchar);
1362 1361 continue;
1363 1362 }
1364 1363 if (!strcmp(ident, "resources" )) resource_parse(*pipeline);
1365 1364 else if (!strcmp(ident, "pipe_desc" )) pipe_desc_parse(*pipeline);
1366 1365 else if (!strcmp(ident, "pipe_class")) pipe_class_parse(*pipeline);
1367 1366 else if (!strcmp(ident, "define")) {
1368 1367 skipws();
1369 1368 if ( (_curchar != '%')
1370 1369 || ( next_char(), (_curchar != '{')) ) {
1371 1370 parse_err(SYNERR, "expected '%{'\n");
1372 1371 return;
1373 1372 }
1374 1373 next_char(); skipws();
1375 1374
1376 1375 char *node_class = get_ident();
1377 1376 if (node_class == NULL) {
1378 1377 parse_err(SYNERR, "expected identifier, found \"%c\"\n", _curchar);
1379 1378 return;
1380 1379 }
1381 1380
1382 1381 skipws();
1383 1382 if (_curchar != ',' && _curchar != '=') {
1384 1383 parse_err(SYNERR, "expected `=`, found '%c'\n", _curchar);
1385 1384 break;
1386 1385 }
1387 1386 next_char(); skipws();
1388 1387
1389 1388 char *pipe_class = get_ident();
1390 1389 if (pipe_class == NULL) {
1391 1390 parse_err(SYNERR, "expected identifier, found \"%c\"\n", _curchar);
1392 1391 return;
1393 1392 }
1394 1393 if (_curchar != ';' ) {
1395 1394 parse_err(SYNERR, "expected `;`, found '%c'\n", _curchar);
1396 1395 break;
1397 1396 }
1398 1397 next_char(); // Skip over semi-colon
1399 1398
1400 1399 skipws();
1401 1400 if ( (_curchar != '%')
1402 1401 || ( next_char(), (_curchar != '}')) ) {
1403 1402 parse_err(SYNERR, "expected '%%}', found \"%c\"\n", _curchar);
1404 1403 }
1405 1404 next_char();
1406 1405
1407 1406 // Check ident for validity
1408 1407 if (_AD._pipeline && !_AD._pipeline->_classlist.search(pipe_class)) {
1409 1408 parse_err(SYNERR, "\"%s\" is not a valid pipeline class\n", pipe_class);
1410 1409 return;
1411 1410 }
1412 1411
1413 1412 // Add this machine node to the list in the pipeline class
1414 1413 _AD._pipeline->_classdict[pipe_class]->is_pipeclass()->_instructs.addName(node_class);
1415 1414
1416 1415 MachNodeForm *machnode = new MachNodeForm(node_class); // Create new machnode form
1417 1416 machnode->_machnode_pipe = pipe_class;
1418 1417
1419 1418 _AD.addForm(machnode);
1420 1419 }
1421 1420 else if (!strcmp(ident, "attributes")) {
1422 1421 bool vsi_seen = false, bhds_seen = false;
1423 1422
1424 1423 skipws();
1425 1424 if ( (_curchar != '%')
1426 1425 || ( next_char(), (_curchar != '{')) ) {
1427 1426 parse_err(SYNERR, "expected '%{'\n");
1428 1427 return;
1429 1428 }
1430 1429 next_char(); skipws();
1431 1430
1432 1431 while (_curchar != '%') {
1433 1432 ident = get_ident();
1434 1433 if (ident == NULL)
1435 1434 break;
1436 1435
1437 1436 if (!strcmp(ident, "variable_size_instructions")) {
1438 1437 skipws();
1439 1438 if (_curchar == ';') {
1440 1439 next_char(); skipws();
1441 1440 }
1442 1441
1443 1442 pipeline->_variableSizeInstrs = true;
1444 1443 vsi_seen = true;
1445 1444 continue;
1446 1445 }
1447 1446
1448 1447 if (!strcmp(ident, "fixed_size_instructions")) {
1449 1448 skipws();
1450 1449 if (_curchar == ';') {
1451 1450 next_char(); skipws();
1452 1451 }
1453 1452
1454 1453 pipeline->_variableSizeInstrs = false;
1455 1454 vsi_seen = true;
1456 1455 continue;
1457 1456 }
1458 1457
1459 1458 if (!strcmp(ident, "branch_has_delay_slot")) {
1460 1459 skipws();
1461 1460 if (_curchar == ';') {
1462 1461 next_char(); skipws();
1463 1462 }
1464 1463
1465 1464 pipeline->_branchHasDelaySlot = true;
1466 1465 bhds_seen = true;
1467 1466 continue;
1468 1467 }
1469 1468
1470 1469 if (!strcmp(ident, "max_instructions_per_bundle")) {
1471 1470 skipws();
1472 1471 if (_curchar != '=') {
1473 1472 parse_err(SYNERR, "expected `=`\n");
1474 1473 break;
1475 1474 }
1476 1475
1477 1476 next_char(); skipws();
1478 1477 pipeline->_maxInstrsPerBundle = get_int();
1479 1478 skipws();
1480 1479
1481 1480 if (_curchar == ';') {
1482 1481 next_char(); skipws();
1483 1482 }
1484 1483
1485 1484 continue;
1486 1485 }
1487 1486
1488 1487 if (!strcmp(ident, "max_bundles_per_cycle")) {
1489 1488 skipws();
1490 1489 if (_curchar != '=') {
1491 1490 parse_err(SYNERR, "expected `=`\n");
1492 1491 break;
1493 1492 }
1494 1493
1495 1494 next_char(); skipws();
1496 1495 pipeline->_maxBundlesPerCycle = get_int();
1497 1496 skipws();
1498 1497
1499 1498 if (_curchar == ';') {
1500 1499 next_char(); skipws();
1501 1500 }
1502 1501
1503 1502 continue;
1504 1503 }
1505 1504
1506 1505 if (!strcmp(ident, "instruction_unit_size")) {
1507 1506 skipws();
1508 1507 if (_curchar != '=') {
1509 1508 parse_err(SYNERR, "expected `=`, found '%c'\n", _curchar);
1510 1509 break;
1511 1510 }
1512 1511
1513 1512 next_char(); skipws();
1514 1513 pipeline->_instrUnitSize = get_int();
1515 1514 skipws();
1516 1515
1517 1516 if (_curchar == ';') {
1518 1517 next_char(); skipws();
1519 1518 }
1520 1519
1521 1520 continue;
1522 1521 }
1523 1522
1524 1523 if (!strcmp(ident, "bundle_unit_size")) {
1525 1524 skipws();
1526 1525 if (_curchar != '=') {
1527 1526 parse_err(SYNERR, "expected `=`, found '%c'\n", _curchar);
1528 1527 break;
1529 1528 }
1530 1529
1531 1530 next_char(); skipws();
1532 1531 pipeline->_bundleUnitSize = get_int();
1533 1532 skipws();
1534 1533
1535 1534 if (_curchar == ';') {
1536 1535 next_char(); skipws();
1537 1536 }
1538 1537
1539 1538 continue;
1540 1539 }
1541 1540
1542 1541 if (!strcmp(ident, "instruction_fetch_unit_size")) {
1543 1542 skipws();
1544 1543 if (_curchar != '=') {
1545 1544 parse_err(SYNERR, "expected `=`, found '%c'\n", _curchar);
1546 1545 break;
1547 1546 }
1548 1547
1549 1548 next_char(); skipws();
1550 1549 pipeline->_instrFetchUnitSize = get_int();
1551 1550 skipws();
1552 1551
1553 1552 if (_curchar == ';') {
1554 1553 next_char(); skipws();
1555 1554 }
1556 1555
1557 1556 continue;
1558 1557 }
1559 1558
1560 1559 if (!strcmp(ident, "instruction_fetch_units")) {
1561 1560 skipws();
1562 1561 if (_curchar != '=') {
1563 1562 parse_err(SYNERR, "expected `=`, found '%c'\n", _curchar);
1564 1563 break;
1565 1564 }
1566 1565
1567 1566 next_char(); skipws();
1568 1567 pipeline->_instrFetchUnits = get_int();
1569 1568 skipws();
1570 1569
1571 1570 if (_curchar == ';') {
1572 1571 next_char(); skipws();
1573 1572 }
1574 1573
1575 1574 continue;
1576 1575 }
1577 1576
1578 1577 if (!strcmp(ident, "nops")) {
1579 1578 skipws();
1580 1579 if (_curchar != '(') {
1581 1580 parse_err(SYNERR, "expected `(`, found '%c'\n", _curchar);
1582 1581 break;
1583 1582 }
1584 1583
1585 1584 next_char(); skipws();
1586 1585
1587 1586 while (_curchar != ')') {
1588 1587 ident = get_ident();
1589 1588 if (ident == NULL) {
1590 1589 parse_err(SYNERR, "expected identifier for nop instruction, found '%c'\n", _curchar);
1591 1590 break;
1592 1591 }
1593 1592
1594 1593 pipeline->_noplist.addName(ident);
1595 1594 pipeline->_nopcnt++;
1596 1595 skipws();
1597 1596
1598 1597 if (_curchar == ',') {
1599 1598 next_char(); skipws();
1600 1599 }
1601 1600 }
1602 1601
1603 1602 next_char(); skipws();
1604 1603
1605 1604 if (_curchar == ';') {
1606 1605 next_char(); skipws();
1607 1606 }
1608 1607
1609 1608 continue;
1610 1609 }
1611 1610
1612 1611 parse_err(SYNERR, "unknown specifier \"%s\"\n", ident);
1613 1612 }
1614 1613
1615 1614 if ( (_curchar != '%')
1616 1615 || ( next_char(), (_curchar != '}')) ) {
1617 1616 parse_err(SYNERR, "expected '%}', found \"%c\"\n", _curchar);
1618 1617 }
1619 1618 next_char(); skipws();
1620 1619
1621 1620 if (pipeline->_maxInstrsPerBundle == 0)
1622 1621 parse_err(SYNERR, "\"max_instructions_per_bundle\" unspecified\n");
1623 1622 if (pipeline->_instrUnitSize == 0 && pipeline->_bundleUnitSize == 0)
1624 1623 parse_err(SYNERR, "\"instruction_unit_size\" and \"bundle_unit_size\" unspecified\n");
1625 1624 if (pipeline->_instrFetchUnitSize == 0)
1626 1625 parse_err(SYNERR, "\"instruction_fetch_unit_size\" unspecified\n");
1627 1626 if (pipeline->_instrFetchUnits == 0)
1628 1627 parse_err(SYNERR, "\"instruction_fetch_units\" unspecified\n");
1629 1628 if (!vsi_seen)
1630 1629 parse_err(SYNERR, "\"variable_size_instruction\" or \"fixed_size_instruction\" unspecified\n");
1631 1630 }
1632 1631 else { // Done with staticly defined parts of instruction definition
1633 1632 parse_err(SYNERR, "expected one of \"resources\", \"pipe_desc\", \"pipe_class\", found \"%s\"\n", ident);
1634 1633 return;
1635 1634 }
1636 1635 skipws();
1637 1636 if (_curchar == ';')
1638 1637 skipws();
1639 1638 } while(_curchar != '%');
1640 1639
1641 1640 next_char();
1642 1641 if (_curchar != '}') {
1643 1642 parse_err(SYNERR, "missing \"%}\" in pipeline definition\n");
1644 1643 return;
1645 1644 }
1646 1645
1647 1646 next_char();
1648 1647 }
1649 1648
1650 1649 //------------------------------resource_parse----------------------------
1651 1650 void ADLParser::resource_parse(PipelineForm &pipeline) {
1652 1651 ResourceForm *resource;
1653 1652 char * ident;
1654 1653 char * expr;
1655 1654 unsigned mask;
1656 1655 pipeline._rescount = 0;
1657 1656
1658 1657 skipws(); // Skip leading whitespace
1659 1658
1660 1659 if (_curchar != '(') {
1661 1660 parse_err(SYNERR, "missing \"(\" in resource definition\n");
1662 1661 return;
1663 1662 }
1664 1663
1665 1664 do {
1666 1665 next_char(); // Skip "(" or ","
1667 1666 ident = get_ident(); // Grab next identifier
1668 1667
1669 1668 if (ident == NULL) {
1670 1669 parse_err(SYNERR, "keyword identifier expected at \"%c\"\n", _curchar);
1671 1670 return;
1672 1671 }
1673 1672 skipws();
1674 1673
1675 1674 if (_curchar != '=') {
1676 1675 mask = (1 << pipeline._rescount++);
1677 1676 }
1678 1677 else {
1679 1678 next_char(); skipws();
1680 1679 expr = get_ident(); // Grab next identifier
1681 1680 if (expr == NULL) {
1682 1681 parse_err(SYNERR, "keyword identifier expected at \"%c\"\n", _curchar);
1683 1682 return;
1684 1683 }
1685 1684 resource = (ResourceForm *) pipeline._resdict[expr];
1686 1685 if (resource == NULL) {
1687 1686 parse_err(SYNERR, "resource \"%s\" is not defined\n", expr);
1688 1687 return;
1689 1688 }
1690 1689 mask = resource->mask();
1691 1690
1692 1691 skipws();
1693 1692 while (_curchar == '|') {
1694 1693 next_char(); skipws();
1695 1694
1696 1695 expr = get_ident(); // Grab next identifier
1697 1696 if (expr == NULL) {
1698 1697 parse_err(SYNERR, "keyword identifier expected at \"%c\"\n", _curchar);
1699 1698 return;
1700 1699 }
1701 1700
1702 1701 resource = (ResourceForm *) pipeline._resdict[expr]; // Look up the value
1703 1702 if (resource == NULL) {
1704 1703 parse_err(SYNERR, "resource \"%s\" is not defined\n", expr);
1705 1704 return;
1706 1705 }
1707 1706
1708 1707 mask |= resource->mask();
1709 1708 skipws();
1710 1709 }
1711 1710 }
1712 1711
1713 1712 resource = new ResourceForm(mask);
1714 1713
1715 1714 pipeline._resdict.Insert(ident, resource);
1716 1715 pipeline._reslist.addName(ident);
1717 1716 } while (_curchar == ',');
1718 1717
1719 1718 if (_curchar != ')') {
1720 1719 parse_err(SYNERR, "\")\" expected at \"%c\"\n", _curchar);
1721 1720 return;
1722 1721 }
1723 1722
1724 1723 next_char(); // Skip ")"
1725 1724 if (_curchar == ';')
1726 1725 next_char(); // Skip ";"
1727 1726 }
1728 1727
1729 1728 //------------------------------resource_parse----------------------------
1730 1729 void ADLParser::pipe_desc_parse(PipelineForm &pipeline) {
1731 1730 char * ident;
1732 1731
1733 1732 skipws(); // Skip leading whitespace
1734 1733
1735 1734 if (_curchar != '(') {
1736 1735 parse_err(SYNERR, "missing \"(\" in pipe_desc definition\n");
1737 1736 return;
1738 1737 }
1739 1738
1740 1739 do {
1741 1740 next_char(); // Skip "(" or ","
1742 1741 ident = get_ident(); // Grab next identifier
1743 1742 if (ident == NULL) {
1744 1743 parse_err(SYNERR, "keyword identifier expected at \"%c\"\n", _curchar);
1745 1744 return;
1746 1745 }
1747 1746
1748 1747 // Add the name to the list
1749 1748 pipeline._stages.addName(ident);
1750 1749 pipeline._stagecnt++;
1751 1750
1752 1751 skipws();
1753 1752 } while (_curchar == ',');
1754 1753
1755 1754 if (_curchar != ')') {
1756 1755 parse_err(SYNERR, "\")\" expected at \"%c\"\n", _curchar);
1757 1756 return;
1758 1757 }
1759 1758
1760 1759 next_char(); // Skip ")"
1761 1760 if (_curchar == ';')
1762 1761 next_char(); // Skip ";"
1763 1762 }
1764 1763
1765 1764 //------------------------------pipe_class_parse--------------------------
1766 1765 void ADLParser::pipe_class_parse(PipelineForm &pipeline) {
1767 1766 PipeClassForm *pipe_class;
1768 1767 char * ident;
1769 1768 char * stage;
1770 1769 char * read_or_write;
1771 1770 int is_write;
1772 1771 int is_read;
1773 1772 OperandForm *oper;
1774 1773
1775 1774 skipws(); // Skip leading whitespace
1776 1775
1777 1776 ident = get_ident(); // Grab next identifier
1778 1777
1779 1778 if (ident == NULL) {
1780 1779 parse_err(SYNERR, "keyword identifier expected at \"%c\"\n", _curchar);
1781 1780 return;
1782 1781 }
1783 1782
1784 1783 // Create a record for the pipe_class
1785 1784 pipe_class = new PipeClassForm(ident, ++pipeline._classcnt);
1786 1785 pipeline._classdict.Insert(ident, pipe_class);
1787 1786 pipeline._classlist.addName(ident);
1788 1787
1789 1788 // Then get the operands
1790 1789 skipws();
1791 1790 if (_curchar != '(') {
1792 1791 parse_err(SYNERR, "missing \"(\" in pipe_class definition\n");
1793 1792 }
1794 1793 // Parse the operand list
1795 1794 else get_oplist(pipe_class->_parameters, pipe_class->_localNames);
1796 1795 skipws(); // Skip leading whitespace
1797 1796 // Check for block delimiter
1798 1797 if ( (_curchar != '%')
1799 1798 || ( next_char(), (_curchar != '{')) ) {
1800 1799 parse_err(SYNERR, "missing \"%{\" in pipe_class definition\n");
1801 1800 return;
1802 1801 }
1803 1802 next_char();
1804 1803
1805 1804 do {
1806 1805 ident = get_ident(); // Grab next identifier
1807 1806 if (ident == NULL) {
1808 1807 parse_err(SYNERR, "keyword identifier expected at \"%c\"\n", _curchar);
1809 1808 continue;
1810 1809 }
1811 1810 skipws();
1812 1811
1813 1812 if (!strcmp(ident, "fixed_latency")) {
1814 1813 skipws();
1815 1814 if (_curchar != '(') {
1816 1815 parse_err(SYNERR, "missing \"(\" in latency definition\n");
1817 1816 return;
1818 1817 }
1819 1818 next_char(); skipws();
1820 1819 if( !isdigit(_curchar) ) {
1821 1820 parse_err(SYNERR, "number expected for \"%c\" in latency definition\n", _curchar);
1822 1821 return;
1823 1822 }
1824 1823 int fixed_latency = get_int();
1825 1824 skipws();
1826 1825 if (_curchar != ')') {
1827 1826 parse_err(SYNERR, "missing \")\" in latency definition\n");
1828 1827 return;
1829 1828 }
1830 1829 next_char(); skipws();
1831 1830 if (_curchar != ';') {
1832 1831 parse_err(SYNERR, "missing \";\" in latency definition\n");
1833 1832 return;
1834 1833 }
1835 1834
1836 1835 pipe_class->setFixedLatency(fixed_latency);
1837 1836 next_char(); skipws();
1838 1837 continue;
1839 1838 }
1840 1839
1841 1840 if (!strcmp(ident, "zero_instructions") ||
1842 1841 !strcmp(ident, "no_instructions")) {
1843 1842 skipws();
1844 1843 if (_curchar != ';') {
1845 1844 parse_err(SYNERR, "missing \";\" in latency definition\n");
1846 1845 return;
1847 1846 }
1848 1847
1849 1848 pipe_class->setInstructionCount(0);
1850 1849 next_char(); skipws();
1851 1850 continue;
1852 1851 }
1853 1852
1854 1853 if (!strcmp(ident, "one_instruction_with_delay_slot") ||
1855 1854 !strcmp(ident, "single_instruction_with_delay_slot")) {
1856 1855 skipws();
1857 1856 if (_curchar != ';') {
1858 1857 parse_err(SYNERR, "missing \";\" in latency definition\n");
1859 1858 return;
1860 1859 }
1861 1860
1862 1861 pipe_class->setInstructionCount(1);
1863 1862 pipe_class->setBranchDelay(true);
1864 1863 next_char(); skipws();
1865 1864 continue;
1866 1865 }
1867 1866
1868 1867 if (!strcmp(ident, "one_instruction") ||
1869 1868 !strcmp(ident, "single_instruction")) {
1870 1869 skipws();
1871 1870 if (_curchar != ';') {
1872 1871 parse_err(SYNERR, "missing \";\" in latency definition\n");
1873 1872 return;
1874 1873 }
1875 1874
1876 1875 pipe_class->setInstructionCount(1);
1877 1876 next_char(); skipws();
1878 1877 continue;
1879 1878 }
1880 1879
1881 1880 if (!strcmp(ident, "instructions_in_first_bundle") ||
1882 1881 !strcmp(ident, "instruction_count")) {
1883 1882 skipws();
1884 1883
1885 1884 int number_of_instructions = 1;
1886 1885
1887 1886 if (_curchar != '(') {
1888 1887 parse_err(SYNERR, "\"(\" expected at \"%c\"\n", _curchar);
1889 1888 continue;
1890 1889 }
1891 1890
1892 1891 next_char(); skipws();
1893 1892 number_of_instructions = get_int();
1894 1893
1895 1894 skipws();
1896 1895 if (_curchar != ')') {
1897 1896 parse_err(SYNERR, "\")\" expected at \"%c\"\n", _curchar);
1898 1897 continue;
1899 1898 }
1900 1899
1901 1900 next_char(); skipws();
1902 1901 if (_curchar != ';') {
1903 1902 parse_err(SYNERR, "missing \";\" in latency definition\n");
1904 1903 return;
1905 1904 }
1906 1905
1907 1906 pipe_class->setInstructionCount(number_of_instructions);
1908 1907 next_char(); skipws();
1909 1908 continue;
1910 1909 }
1911 1910
1912 1911 if (!strcmp(ident, "multiple_bundles")) {
1913 1912 skipws();
1914 1913 if (_curchar != ';') {
1915 1914 parse_err(SYNERR, "missing \";\" after multiple bundles\n");
1916 1915 return;
1917 1916 }
1918 1917
1919 1918 pipe_class->setMultipleBundles(true);
1920 1919 next_char(); skipws();
1921 1920 continue;
1922 1921 }
1923 1922
1924 1923 if (!strcmp(ident, "has_delay_slot")) {
1925 1924 skipws();
1926 1925 if (_curchar != ';') {
1927 1926 parse_err(SYNERR, "missing \";\" after \"has_delay_slot\"\n");
1928 1927 return;
1929 1928 }
1930 1929
1931 1930 pipe_class->setBranchDelay(true);
1932 1931 next_char(); skipws();
1933 1932 continue;
1934 1933 }
1935 1934
1936 1935 if (!strcmp(ident, "force_serialization")) {
1937 1936 skipws();
1938 1937 if (_curchar != ';') {
1939 1938 parse_err(SYNERR, "missing \";\" after \"force_serialization\"\n");
1940 1939 return;
1941 1940 }
1942 1941
1943 1942 pipe_class->setForceSerialization(true);
1944 1943 next_char(); skipws();
1945 1944 continue;
1946 1945 }
1947 1946
1948 1947 if (!strcmp(ident, "may_have_no_code")) {
1949 1948 skipws();
1950 1949 if (_curchar != ';') {
1951 1950 parse_err(SYNERR, "missing \";\" after \"may_have_no_code\"\n");
1952 1951 return;
1953 1952 }
1954 1953
1955 1954 pipe_class->setMayHaveNoCode(true);
1956 1955 next_char(); skipws();
1957 1956 continue;
1958 1957 }
1959 1958
1960 1959 const Form *parm = pipe_class->_localNames[ident];
1961 1960 if (parm != NULL) {
1962 1961 oper = parm->is_operand();
1963 1962 if (oper == NULL && !parm->is_opclass()) {
1964 1963 parse_err(SYNERR, "operand name expected at %s\n", ident);
1965 1964 continue;
1966 1965 }
1967 1966
1968 1967 if (_curchar != ':') {
1969 1968 parse_err(SYNERR, "\":\" expected at \"%c\"\n", _curchar);
1970 1969 continue;
1971 1970 }
1972 1971 next_char(); skipws();
1973 1972 stage = get_ident();
1974 1973 if (stage == NULL) {
1975 1974 parse_err(SYNERR, "pipeline stage identifier expected at \"%c\"\n", _curchar);
1976 1975 continue;
1977 1976 }
1978 1977
1979 1978 skipws();
1980 1979 if (_curchar != '(') {
1981 1980 parse_err(SYNERR, "\"(\" expected at \"%c\"\n", _curchar);
1982 1981 continue;
1983 1982 }
1984 1983
1985 1984 next_char();
1986 1985 read_or_write = get_ident();
1987 1986 if (read_or_write == NULL) {
1988 1987 parse_err(SYNERR, "\"read\" or \"write\" expected at \"%c\"\n", _curchar);
1989 1988 continue;
1990 1989 }
1991 1990
1992 1991 is_read = strcmp(read_or_write, "read") == 0;
1993 1992 is_write = strcmp(read_or_write, "write") == 0;
1994 1993 if (!is_read && !is_write) {
1995 1994 parse_err(SYNERR, "\"read\" or \"write\" expected at \"%c\"\n", _curchar);
1996 1995 continue;
1997 1996 }
1998 1997
1999 1998 skipws();
2000 1999 if (_curchar != ')') {
2001 2000 parse_err(SYNERR, "\")\" expected at \"%c\"\n", _curchar);
2002 2001 continue;
2003 2002 }
2004 2003
2005 2004 next_char(); skipws();
2006 2005 int more_instrs = 0;
2007 2006 if (_curchar == '+') {
2008 2007 next_char(); skipws();
2009 2008 if (_curchar < '0' || _curchar > '9') {
2010 2009 parse_err(SYNERR, "<number> expected at \"%c\"\n", _curchar);
2011 2010 continue;
2012 2011 }
2013 2012 while (_curchar >= '0' && _curchar <= '9') {
2014 2013 more_instrs *= 10;
2015 2014 more_instrs += _curchar - '0';
2016 2015 next_char();
2017 2016 }
2018 2017 skipws();
2019 2018 }
2020 2019
2021 2020 PipeClassOperandForm *pipe_operand = new PipeClassOperandForm(stage, is_write, more_instrs);
2022 2021 pipe_class->_localUsage.Insert(ident, pipe_operand);
2023 2022
2024 2023 if (_curchar == '%')
2025 2024 continue;
2026 2025
2027 2026 if (_curchar != ';') {
2028 2027 parse_err(SYNERR, "\";\" expected at \"%c\"\n", _curchar);
2029 2028 continue;
2030 2029 }
2031 2030 next_char(); skipws();
2032 2031 continue;
2033 2032 }
2034 2033
2035 2034 // Scan for Resource Specifier
2036 2035 const Form *res = pipeline._resdict[ident];
2037 2036 if (res != NULL) {
2038 2037 int cyclecnt = 1;
2039 2038 if (_curchar != ':') {
2040 2039 parse_err(SYNERR, "\":\" expected at \"%c\"\n", _curchar);
2041 2040 continue;
2042 2041 }
2043 2042 next_char(); skipws();
2044 2043 stage = get_ident();
2045 2044 if (stage == NULL) {
2046 2045 parse_err(SYNERR, "pipeline stage identifier expected at \"%c\"\n", _curchar);
2047 2046 continue;
2048 2047 }
2049 2048
2050 2049 skipws();
2051 2050 if (_curchar == '(') {
2052 2051 next_char();
2053 2052 cyclecnt = get_int();
2054 2053
2055 2054 skipws();
2056 2055 if (_curchar != ')') {
2057 2056 parse_err(SYNERR, "\")\" expected at \"%c\"\n", _curchar);
2058 2057 continue;
2059 2058 }
2060 2059
2061 2060 next_char(); skipws();
2062 2061 }
2063 2062
2064 2063 PipeClassResourceForm *resource = new PipeClassResourceForm(ident, stage, cyclecnt);
2065 2064 int stagenum = pipeline._stages.index(stage);
2066 2065 if (pipeline._maxcycleused < (stagenum+cyclecnt))
2067 2066 pipeline._maxcycleused = (stagenum+cyclecnt);
2068 2067 pipe_class->_resUsage.addForm(resource);
2069 2068
2070 2069 if (_curchar == '%')
2071 2070 continue;
2072 2071
2073 2072 if (_curchar != ';') {
2074 2073 parse_err(SYNERR, "\";\" expected at \"%c\"\n", _curchar);
2075 2074 continue;
2076 2075 }
2077 2076 next_char(); skipws();
2078 2077 continue;
2079 2078 }
2080 2079
2081 2080 parse_err(SYNERR, "resource expected at \"%s\"\n", ident);
2082 2081 return;
2083 2082 } while(_curchar != '%');
2084 2083
2085 2084 next_char();
2086 2085 if (_curchar != '}') {
2087 2086 parse_err(SYNERR, "missing \"%}\" in pipe_class definition\n");
2088 2087 return;
2089 2088 }
2090 2089
2091 2090 next_char();
2092 2091 }
2093 2092
2094 2093 //------------------------------peep_parse-------------------------------------
2095 2094 void ADLParser::peep_parse(void) {
2096 2095 Peephole *peep; // Pointer to current peephole rule form
2097 2096 char *desc = NULL; // String representation of rule
2098 2097
2099 2098 skipws(); // Skip leading whitespace
2100 2099
2101 2100 peep = new Peephole(); // Build new Peephole object
2102 2101 // Check for open block sequence
2103 2102 skipws(); // Skip leading whitespace
2104 2103 if (_curchar == '%' && *(_ptr+1) == '{') {
2105 2104 next_char(); next_char(); // Skip "%{"
2106 2105 skipws();
2107 2106 while (_curchar != '%' && *(_ptr+1) != '}') {
2108 2107 char *token = get_ident();
2109 2108 if (token == NULL) {
2110 2109 parse_err(SYNERR, "missing identifier inside peephole rule.\n");
2111 2110 return;
2112 2111 }
2113 2112 // check for legal subsections of peephole rule
2114 2113 if (strcmp(token,"peepmatch")==0) {
2115 2114 peep_match_parse(*peep); }
2116 2115 else if (strcmp(token,"peepconstraint")==0) {
2117 2116 peep_constraint_parse(*peep); }
2118 2117 else if (strcmp(token,"peepreplace")==0) {
2119 2118 peep_replace_parse(*peep); }
2120 2119 else {
2121 2120 parse_err(SYNERR, "expected peepmatch, peepconstraint, or peepreplace for identifier %s.\n", token);
2122 2121 }
2123 2122 skipws();
2124 2123 }
2125 2124 }
2126 2125 else {
2127 2126 parse_err(SYNERR, "Missing %%{ ... %%} block after peephole keyword.\n");
2128 2127 return;
2129 2128 }
2130 2129 next_char(); // Skip past '%'
2131 2130 next_char(); // Skip past '}'
2132 2131 }
2133 2132
2134 2133 // ******************** Private Level 2 Parse Functions ********************
2135 2134 //------------------------------constraint_parse------------------------------
2136 2135 Constraint *ADLParser::constraint_parse(void) {
2137 2136 char *func;
2138 2137 char *arg;
2139 2138
2140 2139 // Check for constraint expression
2141 2140 skipws();
2142 2141 if (_curchar != '(') {
2143 2142 parse_err(SYNERR, "missing constraint expression, (...)\n");
2144 2143 return NULL;
2145 2144 }
2146 2145 next_char(); // Skip past '('
2147 2146
2148 2147 // Get constraint function
2149 2148 skipws();
2150 2149 func = get_ident();
2151 2150 if (func == NULL) {
2152 2151 parse_err(SYNERR, "missing function in constraint expression.\n");
2153 2152 return NULL;
2154 2153 }
2155 2154 if (strcmp(func,"ALLOC_IN_RC")==0
2156 2155 || strcmp(func,"IS_R_CLASS")==0) {
2157 2156 // Check for '(' before argument
2158 2157 skipws();
2159 2158 if (_curchar != '(') {
2160 2159 parse_err(SYNERR, "missing '(' for constraint function's argument.\n");
2161 2160 return NULL;
2162 2161 }
2163 2162 next_char();
2164 2163
2165 2164 // Get it's argument
2166 2165 skipws();
2167 2166 arg = get_ident();
2168 2167 if (arg == NULL) {
2169 2168 parse_err(SYNERR, "missing argument for constraint function %s\n",func);
2170 2169 return NULL;
2171 2170 }
2172 2171 // Check for ')' after argument
2173 2172 skipws();
2174 2173 if (_curchar != ')') {
2175 2174 parse_err(SYNERR, "missing ')' after constraint function argument %s\n",arg);
2176 2175 return NULL;
2177 2176 }
2178 2177 next_char();
2179 2178 } else {
2180 2179 parse_err(SYNERR, "Invalid constraint function %s\n",func);
2181 2180 return NULL;
2182 2181 }
2183 2182
2184 2183 // Check for closing paren and ';'
2185 2184 skipws();
2186 2185 if (_curchar != ')') {
2187 2186 parse_err(SYNERR, "Missing ')' for constraint function %s\n",func);
2188 2187 return NULL;
2189 2188 }
2190 2189 next_char();
2191 2190 skipws();
2192 2191 if (_curchar != ';') {
2193 2192 parse_err(SYNERR, "Missing ';' after constraint.\n");
2194 2193 return NULL;
2195 2194 }
2196 2195 next_char();
2197 2196
2198 2197 // Create new "Constraint"
2199 2198 Constraint *constraint = new Constraint(func,arg);
2200 2199 return constraint;
2201 2200 }
2202 2201
2203 2202 //------------------------------constr_parse-----------------------------------
2204 2203 ConstructRule *ADLParser::construct_parse(void) {
2205 2204 return NULL;
2206 2205 }
2207 2206
2208 2207
2209 2208 //------------------------------reg_def_parse----------------------------------
2210 2209 void ADLParser::reg_def_parse(void) {
2211 2210 char *rname; // Name of register being defined
2212 2211
2213 2212 // Get register name
2214 2213 skipws(); // Skip whitespace
2215 2214 rname = get_ident();
2216 2215 if (rname == NULL) {
2217 2216 parse_err(SYNERR, "missing register name after reg_def\n");
2218 2217 return;
2219 2218 }
2220 2219
2221 2220 // Check for definition of register calling convention (save on call, ...),
2222 2221 // register save type, and register encoding value.
2223 2222 skipws();
2224 2223 char *callconv = NULL;
2225 2224 char *c_conv = NULL;
2226 2225 char *idealtype = NULL;
2227 2226 char *encoding = NULL;
2228 2227 char *concrete = NULL;
2229 2228 if (_curchar == '(') {
2230 2229 next_char();
2231 2230 callconv = get_ident();
2232 2231 // Parse the internal calling convention, must be NS, SOC, SOE, or AS.
2233 2232 if (callconv == NULL) {
2234 2233 parse_err(SYNERR, "missing register calling convention value\n");
2235 2234 return;
2236 2235 }
2237 2236 if(strcmp(callconv, "SOC") && strcmp(callconv,"SOE") &&
2238 2237 strcmp(callconv, "NS") && strcmp(callconv, "AS")) {
2239 2238 parse_err(SYNERR, "invalid value for register calling convention\n");
2240 2239 }
2241 2240 skipws();
2242 2241 if (_curchar != ',') {
2243 2242 parse_err(SYNERR, "missing comma in register definition statement\n");
2244 2243 return;
2245 2244 }
2246 2245 next_char();
2247 2246
2248 2247 // Parse the native calling convention, must be NS, SOC, SOE, AS
2249 2248 c_conv = get_ident();
2250 2249 if (c_conv == NULL) {
2251 2250 parse_err(SYNERR, "missing register native calling convention value\n");
2252 2251 return;
2253 2252 }
2254 2253 if(strcmp(c_conv, "SOC") && strcmp(c_conv,"SOE") &&
2255 2254 strcmp(c_conv, "NS") && strcmp(c_conv, "AS")) {
2256 2255 parse_err(SYNERR, "invalid value for register calling convention\n");
2257 2256 }
2258 2257 skipws();
2259 2258 if (_curchar != ',') {
2260 2259 parse_err(SYNERR, "missing comma in register definition statement\n");
2261 2260 return;
2262 2261 }
2263 2262 next_char();
2264 2263 skipws();
2265 2264
2266 2265 // Parse the ideal save type
2267 2266 idealtype = get_ident();
2268 2267 if (idealtype == NULL) {
2269 2268 parse_err(SYNERR, "missing register save type value\n");
2270 2269 return;
2271 2270 }
2272 2271 skipws();
2273 2272 if (_curchar != ',') {
2274 2273 parse_err(SYNERR, "missing comma in register definition statement\n");
2275 2274 return;
2276 2275 }
2277 2276 next_char();
2278 2277 skipws();
2279 2278
2280 2279 // Parse the encoding value
2281 2280 encoding = get_expr("encoding", ",");
2282 2281 if (encoding == NULL) {
2283 2282 parse_err(SYNERR, "missing register encoding value\n");
2284 2283 return;
2285 2284 }
2286 2285 trim(encoding);
2287 2286 if (_curchar != ',') {
2288 2287 parse_err(SYNERR, "missing comma in register definition statement\n");
2289 2288 return;
2290 2289 }
2291 2290 next_char();
2292 2291 skipws();
2293 2292 // Parse the concrete name type
2294 2293 // concrete = get_ident();
2295 2294 concrete = get_expr("concrete", ")");
2296 2295 if (concrete == NULL) {
2297 2296 parse_err(SYNERR, "missing vm register name value\n");
2298 2297 return;
2299 2298 }
2300 2299
2301 2300 if (_curchar != ')') {
2302 2301 parse_err(SYNERR, "missing ')' in register definition statement\n");
2303 2302 return;
2304 2303 }
2305 2304 next_char();
2306 2305 }
2307 2306
2308 2307 // Check for closing ';'
2309 2308 skipws();
2310 2309 if (_curchar != ';') {
2311 2310 parse_err(SYNERR, "missing ';' after reg_def\n");
2312 2311 return;
2313 2312 }
2314 2313 next_char(); // move past ';'
2315 2314
2316 2315 // Debug Stuff
2317 2316 if (_AD._adl_debug > 1) {
2318 2317 fprintf(stderr,"Register Definition: %s ( %s, %s %s )\n", rname,
2319 2318 (callconv ? callconv : ""), (c_conv ? c_conv : ""), concrete);
2320 2319 }
2321 2320
2322 2321 // Record new register definition.
2323 2322 _AD._register->addRegDef(rname, callconv, c_conv, idealtype, encoding, concrete);
2324 2323 return;
2325 2324 }
2326 2325
2327 2326 //------------------------------reg_class_parse--------------------------------
2328 2327 void ADLParser::reg_class_parse(void) {
2329 2328 char *cname; // Name of register class being defined
2330 2329
2331 2330 // Get register class name
2332 2331 skipws(); // Skip leading whitespace
2333 2332 cname = get_ident();
2334 2333 if (cname == NULL) {
2335 2334 parse_err(SYNERR, "missing register class name after 'reg_class'\n");
2336 2335 return;
2337 2336 }
2338 2337 // Debug Stuff
2339 2338 if (_AD._adl_debug >1) fprintf(stderr,"Register Class: %s\n", cname);
2340 2339
2341 2340 RegClass *reg_class = _AD._register->addRegClass(cname);
2342 2341
2343 2342 // Collect registers in class
2344 2343 skipws();
2345 2344 if (_curchar == '(') {
2346 2345 next_char(); // Skip '('
2347 2346 skipws();
2348 2347 while (_curchar != ')') {
2349 2348 char *rname = get_ident();
2350 2349 if (rname==NULL) {
2351 2350 parse_err(SYNERR, "missing identifier inside reg_class list.\n");
2352 2351 return;
2353 2352 }
2354 2353 RegDef *regDef = _AD._register->getRegDef(rname);
2355 2354 if (!regDef) {
2356 2355 parse_err(SEMERR, "unknown identifier %s inside reg_class list.\n", rname);
2357 2356 } else {
2358 2357 reg_class->addReg(regDef); // add regDef to regClass
2359 2358 }
2360 2359
2361 2360 // Check for ',' and position to next token.
2362 2361 skipws();
2363 2362 if (_curchar == ',') {
2364 2363 next_char(); // Skip trailing ','
2365 2364 skipws();
2366 2365 }
2367 2366 }
2368 2367 next_char(); // Skip closing ')'
2369 2368 }
2370 2369
2371 2370 // Check for terminating ';'
2372 2371 skipws();
2373 2372 if (_curchar != ';') {
2374 2373 parse_err(SYNERR, "missing ';' at end of reg_class definition.\n");
2375 2374 return;
2376 2375 }
2377 2376 next_char(); // Skip trailing ';'
2378 2377
2379 2378 // Check RegClass size, must be <= 32 registers in class.
2380 2379
2381 2380 return;
2382 2381 }
2383 2382
2384 2383 //------------------------------alloc_class_parse------------------------------
2385 2384 void ADLParser::alloc_class_parse(void) {
2386 2385 char *name; // Name of allocation class being defined
2387 2386
2388 2387 // Get allocation class name
2389 2388 skipws(); // Skip leading whitespace
2390 2389 name = get_ident();
2391 2390 if (name == NULL) {
2392 2391 parse_err(SYNERR, "missing allocation class name after 'reg_class'\n");
2393 2392 return;
2394 2393 }
2395 2394 // Debug Stuff
2396 2395 if (_AD._adl_debug >1) fprintf(stderr,"Allocation Class: %s\n", name);
2397 2396
2398 2397 AllocClass *alloc_class = _AD._register->addAllocClass(name);
2399 2398
2400 2399 // Collect registers in class
2401 2400 skipws();
2402 2401 if (_curchar == '(') {
2403 2402 next_char(); // Skip '('
2404 2403 skipws();
2405 2404 while (_curchar != ')') {
2406 2405 char *rname = get_ident();
2407 2406 if (rname==NULL) {
2408 2407 parse_err(SYNERR, "missing identifier inside reg_class list.\n");
2409 2408 return;
2410 2409 }
2411 2410 // Check if name is a RegDef
2412 2411 RegDef *regDef = _AD._register->getRegDef(rname);
2413 2412 if (regDef) {
2414 2413 alloc_class->addReg(regDef); // add regDef to allocClass
2415 2414 } else {
2416 2415
2417 2416 // name must be a RegDef or a RegClass
2418 2417 parse_err(SYNERR, "name %s should be a previously defined reg_def.\n", rname);
2419 2418 return;
2420 2419 }
2421 2420
2422 2421 // Check for ',' and position to next token.
2423 2422 skipws();
2424 2423 if (_curchar == ',') {
2425 2424 next_char(); // Skip trailing ','
2426 2425 skipws();
2427 2426 }
2428 2427 }
2429 2428 next_char(); // Skip closing ')'
2430 2429 }
2431 2430
2432 2431 // Check for terminating ';'
2433 2432 skipws();
2434 2433 if (_curchar != ';') {
2435 2434 parse_err(SYNERR, "missing ';' at end of reg_class definition.\n");
2436 2435 return;
2437 2436 }
2438 2437 next_char(); // Skip trailing ';'
2439 2438
2440 2439 return;
2441 2440 }
2442 2441
2443 2442 //------------------------------peep_match_child_parse-------------------------
2444 2443 InstructForm *ADLParser::peep_match_child_parse(PeepMatch &match, int parent, int &position, int input){
2445 2444 char *token = NULL;
2446 2445 int lparen = 0; // keep track of parenthesis nesting depth
2447 2446 int rparen = 0; // position of instruction at this depth
2448 2447 InstructForm *inst_seen = NULL;
2449 2448 InstructForm *child_seen = NULL;
2450 2449
2451 2450 // Walk the match tree,
2452 2451 // Record <parent, position, instruction name, input position>
2453 2452 while ( lparen >= rparen ) {
2454 2453 skipws();
2455 2454 // Left paren signals start of an input, collect with recursive call
2456 2455 if (_curchar == '(') {
2457 2456 ++lparen;
2458 2457 next_char();
2459 2458 child_seen = peep_match_child_parse(match, parent, position, rparen);
2460 2459 }
2461 2460 // Right paren signals end of an input, may be more
2462 2461 else if (_curchar == ')') {
2463 2462 ++rparen;
2464 2463 if( rparen == lparen ) { // IF rparen matches an lparen I've seen
2465 2464 next_char(); // move past ')'
2466 2465 } else { // ELSE leave ')' for parent
2467 2466 assert( rparen == lparen + 1, "Should only see one extra ')'");
2468 2467 // if an instruction was not specified for this paren-pair
2469 2468 if( ! inst_seen ) { // record signal entry
2470 2469 match.add_instruction( parent, position, NameList::_signal, input );
2471 2470 ++position;
2472 2471 }
2473 2472 // ++input; // TEMPORARY
2474 2473 return inst_seen;
2475 2474 }
2476 2475 }
2477 2476 // if no parens, then check for instruction name
2478 2477 // This instruction is the parent of a sub-tree
2479 2478 else if ((token = get_ident_dup()) != NULL) {
2480 2479 const Form *form = _AD._globalNames[token];
2481 2480 if (form) {
2482 2481 InstructForm *inst = form->is_instruction();
2483 2482 // Record the first instruction at this level
2484 2483 if( inst_seen == NULL ) {
2485 2484 inst_seen = inst;
2486 2485 }
2487 2486 if (inst) {
2488 2487 match.add_instruction( parent, position, token, input );
2489 2488 parent = position;
2490 2489 ++position;
2491 2490 } else {
2492 2491 parse_err(SYNERR, "instruction name expected at identifier %s.\n",
2493 2492 token);
2494 2493 return inst_seen;
2495 2494 }
2496 2495 }
2497 2496 else {
2498 2497 parse_err(SYNERR, "missing identifier in peepmatch rule.\n");
2499 2498 return NULL;
2500 2499 }
2501 2500 }
2502 2501 else {
2503 2502 parse_err(SYNERR, "missing identifier in peepmatch rule.\n");
2504 2503 return NULL;
2505 2504 }
2506 2505
2507 2506 } // end while
2508 2507
2509 2508 assert( false, "ShouldNotReachHere();");
2510 2509 return NULL;
2511 2510 }
2512 2511
2513 2512 //------------------------------peep_match_parse-------------------------------
2514 2513 // Syntax for a peepmatch rule
2515 2514 //
2516 2515 // peepmatch ( root_instr_name [(instruction subtree)] [,(instruction subtree)]* );
2517 2516 //
2518 2517 void ADLParser::peep_match_parse(Peephole &peep) {
2519 2518
2520 2519 skipws();
2521 2520 // Check the structure of the rule
2522 2521 // Check for open paren
2523 2522 if (_curchar != '(') {
2524 2523 parse_err(SYNERR, "missing '(' at start of peepmatch rule.\n");
2525 2524 return;
2526 2525 }
2527 2526 next_char(); // skip '('
2528 2527
2529 2528 // Construct PeepMatch and parse the peepmatch rule.
2530 2529 PeepMatch *match = new PeepMatch(_ptr);
2531 2530 int parent = -1; // parent of root
2532 2531 int position = 0; // zero-based positions
2533 2532 int input = 0; // input position in parent's operands
2534 2533 InstructForm *root= peep_match_child_parse( *match, parent, position, input);
2535 2534 if( root == NULL ) {
2536 2535 parse_err(SYNERR, "missing instruction-name at start of peepmatch.\n");
2537 2536 return;
2538 2537 }
2539 2538
2540 2539 if( _curchar != ')' ) {
2541 2540 parse_err(SYNERR, "missing ')' at end of peepmatch.\n");
2542 2541 return;
2543 2542 }
2544 2543 next_char(); // skip ')'
2545 2544
2546 2545 // Check for closing semicolon
2547 2546 skipws();
2548 2547 if( _curchar != ';' ) {
2549 2548 parse_err(SYNERR, "missing ';' at end of peepmatch.\n");
2550 2549 return;
2551 2550 }
2552 2551 next_char(); // skip ';'
2553 2552
2554 2553 // Store match into peep, and store peep into instruction
2555 2554 peep.add_match(match);
2556 2555 root->append_peephole(&peep);
2557 2556 }
2558 2557
2559 2558 //------------------------------peep_constraint_parse--------------------------
2560 2559 // Syntax for a peepconstraint rule
2561 2560 // A parenthesized list of relations between operands in peepmatch subtree
2562 2561 //
2563 2562 // peepconstraint %{
2564 2563 // (instruction_number.operand_name
2565 2564 // relational_op
2566 2565 // instruction_number.operand_name OR register_name
2567 2566 // [, ...] );
2568 2567 //
2569 2568 // // instruction numbers are zero-based using topological order in peepmatch
2570 2569 //
2571 2570 void ADLParser::peep_constraint_parse(Peephole &peep) {
2572 2571
2573 2572 skipws();
2574 2573 // Check the structure of the rule
2575 2574 // Check for open paren
2576 2575 if (_curchar != '(') {
2577 2576 parse_err(SYNERR, "missing '(' at start of peepconstraint rule.\n");
2578 2577 return;
2579 2578 }
2580 2579 else {
2581 2580 next_char(); // Skip '('
2582 2581 }
2583 2582
2584 2583 // Check for a constraint
2585 2584 skipws();
2586 2585 while( _curchar != ')' ) {
2587 2586 // Get information on the left instruction and its operand
2588 2587 // left-instructions's number
2589 2588 int left_inst = get_int();
2590 2589 // Left-instruction's operand
2591 2590 skipws();
2592 2591 if( _curchar != '.' ) {
2593 2592 parse_err(SYNERR, "missing '.' in peepconstraint after instruction number.\n");
2594 2593 return;
2595 2594 }
2596 2595 next_char(); // Skip '.'
2597 2596 char *left_op = get_ident_dup();
2598 2597
2599 2598 skipws();
2600 2599 // Collect relational operator
2601 2600 char *relation = get_relation_dup();
2602 2601
2603 2602 skipws();
2604 2603 // Get information on the right instruction and its operand
2605 2604 int right_inst; // Right-instructions's number
2606 2605 if( isdigit(_curchar) ) {
2607 2606 right_inst = get_int();
2608 2607 // Right-instruction's operand
2609 2608 skipws();
2610 2609 if( _curchar != '.' ) {
2611 2610 parse_err(SYNERR, "missing '.' in peepconstraint after instruction number.\n");
2612 2611 return;
2613 2612 }
2614 2613 next_char(); // Skip '.'
2615 2614 } else {
2616 2615 right_inst = -1; // Flag as being a register constraint
2617 2616 }
2618 2617
2619 2618 char *right_op = get_ident_dup();
2620 2619
2621 2620 // Construct the next PeepConstraint
2622 2621 PeepConstraint *constraint = new PeepConstraint( left_inst, left_op,
2623 2622 relation,
2624 2623 right_inst, right_op );
2625 2624 // And append it to the list for this peephole rule
2626 2625 peep.append_constraint( constraint );
2627 2626
2628 2627 // Check for another constraint, or end of rule
2629 2628 skipws();
2630 2629 if( _curchar == ',' ) {
2631 2630 next_char(); // Skip ','
2632 2631 skipws();
2633 2632 }
2634 2633 else if( _curchar != ')' ) {
2635 2634 parse_err(SYNERR, "expected ',' or ')' after peephole constraint.\n");
2636 2635 return;
2637 2636 }
2638 2637 } // end while( processing constraints )
2639 2638 next_char(); // Skip ')'
2640 2639
2641 2640 // Check for terminating ';'
2642 2641 skipws();
2643 2642 if (_curchar != ';') {
2644 2643 parse_err(SYNERR, "missing ';' at end of peepconstraint.\n");
2645 2644 return;
2646 2645 }
2647 2646 next_char(); // Skip trailing ';'
2648 2647 }
2649 2648
2650 2649
2651 2650 //------------------------------peep_replace_parse-----------------------------
2652 2651 // Syntax for a peepreplace rule
2653 2652 // root instruction name followed by a
2654 2653 // parenthesized list of whitespace separated instruction.operand specifiers
2655 2654 //
2656 2655 // peepreplace ( instr_name ( [instruction_number.operand_name]* ) );
2657 2656 //
2658 2657 //
2659 2658 void ADLParser::peep_replace_parse(Peephole &peep) {
2660 2659 int lparen = 0; // keep track of parenthesis nesting depth
2661 2660 int rparen = 0; // keep track of parenthesis nesting depth
2662 2661 int icount = 0; // count of instructions in rule for naming
2663 2662 char *str = NULL;
2664 2663 char *token = NULL;
2665 2664
2666 2665 skipws();
2667 2666 // Check for open paren
2668 2667 if (_curchar != '(') {
2669 2668 parse_err(SYNERR, "missing '(' at start of peepreplace rule.\n");
2670 2669 return;
2671 2670 }
2672 2671 else {
2673 2672 lparen++;
2674 2673 next_char();
2675 2674 }
2676 2675
2677 2676 // Check for root instruction
2678 2677 char *inst = get_ident_dup();
2679 2678 const Form *form = _AD._globalNames[inst];
2680 2679 if( form == NULL || form->is_instruction() == NULL ) {
2681 2680 parse_err(SYNERR, "Instruction name expected at start of peepreplace.\n");
2682 2681 return;
2683 2682 }
2684 2683
2685 2684 // Store string representation of rule into replace
2686 2685 PeepReplace *replace = new PeepReplace(str);
2687 2686 replace->add_instruction( inst );
2688 2687
2689 2688 skipws();
2690 2689 // Start of root's operand-list
2691 2690 if (_curchar != '(') {
2692 2691 parse_err(SYNERR, "missing '(' at peepreplace root's operand-list.\n");
2693 2692 return;
2694 2693 }
2695 2694 else {
2696 2695 lparen++;
2697 2696 next_char();
2698 2697 }
2699 2698
2700 2699 skipws();
2701 2700 // Get the list of operands
2702 2701 while( _curchar != ')' ) {
2703 2702 // Get information on an instruction and its operand
2704 2703 // instructions's number
2705 2704 int inst_num = get_int();
2706 2705 // Left-instruction's operand
2707 2706 skipws();
2708 2707 if( _curchar != '.' ) {
2709 2708 parse_err(SYNERR, "missing '.' in peepreplace after instruction number.\n");
2710 2709 return;
2711 2710 }
2712 2711 next_char(); // Skip '.'
2713 2712 char *inst_op = get_ident_dup();
2714 2713 if( inst_op == NULL ) {
2715 2714 parse_err(SYNERR, "missing operand identifier in peepreplace.\n");
2716 2715 return;
2717 2716 }
2718 2717
2719 2718 // Record this operand's position in peepmatch
2720 2719 replace->add_operand( inst_num, inst_op );
2721 2720 skipws();
2722 2721 }
2723 2722
2724 2723 // Check for the end of operands list
2725 2724 skipws();
2726 2725 assert( _curchar == ')', "While loop should have advanced to ')'.");
2727 2726 next_char(); // Skip ')'
2728 2727
2729 2728 skipws();
2730 2729 // Check for end of peepreplace
2731 2730 if( _curchar != ')' ) {
2732 2731 parse_err(SYNERR, "missing ')' at end of peepmatch.\n");
2733 2732 parse_err(SYNERR, "Support one replacement instruction.\n");
2734 2733 return;
2735 2734 }
2736 2735 next_char(); // Skip ')'
2737 2736
2738 2737 // Check for closing semicolon
2739 2738 skipws();
2740 2739 if( _curchar != ';' ) {
2741 2740 parse_err(SYNERR, "missing ';' at end of peepreplace.\n");
2742 2741 return;
2743 2742 }
2744 2743 next_char(); // skip ';'
2745 2744
2746 2745 // Store replace into peep
2747 2746 peep.add_replace( replace );
2748 2747 }
2749 2748
2750 2749 //------------------------------pred_parse-------------------------------------
2751 2750 Predicate *ADLParser::pred_parse(void) {
2752 2751 Predicate *predicate; // Predicate class for operand
2753 2752 char *rule = NULL; // String representation of predicate
2754 2753
2755 2754 skipws(); // Skip leading whitespace
2756 2755 int line = linenum();
2757 2756 if ( (rule = get_paren_expr("pred expression", true)) == NULL ) {
2758 2757 parse_err(SYNERR, "incorrect or missing expression for 'predicate'\n");
2759 2758 return NULL;
2760 2759 }
2761 2760 // Debug Stuff
2762 2761 if (_AD._adl_debug > 1) fprintf(stderr,"Predicate: %s\n", rule);
2763 2762 if (_curchar != ';') {
2764 2763 parse_err(SYNERR, "missing ';' in predicate definition\n");
2765 2764 return NULL;
2766 2765 }
↓ open down ↓ |
1816 lines elided |
↑ open up ↑ |
2767 2766 next_char(); // Point after the terminator
2768 2767
2769 2768 predicate = new Predicate(rule); // Build new predicate object
2770 2769 skipws();
2771 2770 return predicate;
2772 2771 }
2773 2772
2774 2773
2775 2774 //------------------------------ins_encode_parse_block-------------------------
2776 2775 // Parse the block form of ins_encode. See ins_encode_parse for more details
2777 -InsEncode *ADLParser::ins_encode_parse_block(InstructForm &inst) {
2776 +void ADLParser::ins_encode_parse_block(InstructForm& inst) {
2778 2777 // Create a new encoding name based on the name of the instruction
2779 2778 // definition, which should be unique.
2780 - const char * prefix = "__enc_";
2781 - char* ec_name = (char*)malloc(strlen(inst._ident) + strlen(prefix) + 1);
2779 + const char* prefix = "__ins_encode_";
2780 + char* ec_name = (char*) malloc(strlen(inst._ident) + strlen(prefix) + 1);
2782 2781 sprintf(ec_name, "%s%s", prefix, inst._ident);
2783 2782
2784 2783 assert(_AD._encode->encClass(ec_name) == NULL, "shouldn't already exist");
2785 - EncClass *encoding = _AD._encode->add_EncClass(ec_name);
2784 + EncClass* encoding = _AD._encode->add_EncClass(ec_name);
2786 2785 encoding->_linenum = linenum();
2787 2786
2788 2787 // synthesize the arguments list for the enc_class from the
2789 2788 // arguments to the instruct definition.
2790 - const char * param = NULL;
2789 + const char* param = NULL;
2791 2790 inst._parameters.reset();
2792 2791 while ((param = inst._parameters.iter()) != NULL) {
2793 - OperandForm *opForm = (OperandForm*)inst._localNames[param];
2792 + OperandForm* opForm = (OperandForm*) inst._localNames[param];
2794 2793 encoding->add_parameter(opForm->_ident, param);
2795 2794 }
2796 2795
2797 - // Add the prologue to create the MacroAssembler
2798 - encoding->add_code("\n"
2799 - " // Define a MacroAssembler instance for use by the encoding. The\n"
2800 - " // name is chosen to match the __ idiom used for assembly in other\n"
2801 - " // parts of hotspot and assumes the existence of the standard\n"
2802 - " // #define __ _masm.\n"
2803 - " MacroAssembler _masm(&cbuf);\n");
2796 + // Define a MacroAssembler instance for use by the encoding. The
2797 + // name is chosen to match the __ idiom used for assembly in other
2798 + // parts of hotspot and assumes the existence of the standard
2799 + // #define __ _masm.
2800 + encoding->add_code(" MacroAssembler _masm(&cbuf);\n");
2804 2801
2805 2802 // Parse the following %{ }% block
2806 - enc_class_parse_block(encoding, ec_name);
2803 + ins_encode_parse_block_impl(inst, encoding, ec_name);
2807 2804
2808 2805 // Build an encoding rule which invokes the encoding rule we just
2809 2806 // created, passing all arguments that we received.
2810 - InsEncode *encrule = new InsEncode(); // Encode class for instruction
2811 - NameAndList *params = encrule->add_encode(ec_name);
2807 + InsEncode* encrule = new InsEncode(); // Encode class for instruction
2808 + NameAndList* params = encrule->add_encode(ec_name);
2812 2809 inst._parameters.reset();
2813 2810 while ((param = inst._parameters.iter()) != NULL) {
2814 2811 params->add_entry(param);
2815 2812 }
2816 2813
2817 - return encrule;
2814 + // Set encode class of this instruction.
2815 + inst._insencode = encrule;
2816 +}
2817 +
2818 +
2819 +void ADLParser::ins_encode_parse_block_impl(InstructForm& inst, EncClass* encoding, char* ec_name) {
2820 + skipws_no_preproc(); // Skip leading whitespace
2821 + // Prepend location descriptor, for debugging; cf. ADLParser::find_cpp_block
2822 + if (_AD._adlocation_debug) {
2823 + encoding->add_code(get_line_string());
2824 + }
2825 +
2826 + // Collect the parts of the encode description
2827 + // (1) strings that are passed through to output
2828 + // (2) replacement/substitution variable, preceeded by a '$'
2829 + while ((_curchar != '%') && (*(_ptr+1) != '}')) {
2830 +
2831 + // (1)
2832 + // Check if there is a string to pass through to output
2833 + char *start = _ptr; // Record start of the next string
2834 + while ((_curchar != '$') && ((_curchar != '%') || (*(_ptr+1) != '}')) ) {
2835 + // If at the start of a comment, skip past it
2836 + if( (_curchar == '/') && ((*(_ptr+1) == '/') || (*(_ptr+1) == '*')) ) {
2837 + skipws_no_preproc();
2838 + } else {
2839 + // ELSE advance to the next character, or start of the next line
2840 + next_char_or_line();
2841 + }
2842 + }
2843 + // If a string was found, terminate it and record in EncClass
2844 + if (start != _ptr) {
2845 + *_ptr = '\0'; // Terminate the string
2846 + encoding->add_code(start);
2847 + }
2848 +
2849 + // (2)
2850 + // If we are at a replacement variable,
2851 + // copy it and record in EncClass
2852 + if (_curchar == '$') {
2853 + // Found replacement Variable
2854 + char* rep_var = get_rep_var_ident_dup();
2855 +
2856 + // Add flag to _strings list indicating we should check _rep_vars
2857 + encoding->add_rep_var(rep_var);
2858 +
2859 + skipws();
2860 +
2861 + // Check if this instruct is a MachConstantNode.
2862 + if (strcmp(rep_var, "constanttablebase") == 0) {
2863 + // This instruct is a MachConstantNode.
2864 + inst.set_is_mach_constant(true);
2865 +
2866 + if (_curchar == '(') {
2867 + parse_err(SYNERR, "constanttablebase in instruct %s cannot have an argument (only constantaddress and constantoffset)", ec_name);
2868 + return;
2869 + }
2870 + }
2871 + else if ((strcmp(rep_var, "constantaddress") == 0) ||
2872 + (strcmp(rep_var, "constantoffset") == 0)) {
2873 + // This instruct is a MachConstantNode.
2874 + inst.set_is_mach_constant(true);
2875 +
2876 + // If the constant keyword has an argument, parse it.
2877 + if (_curchar == '(') constant_parse(inst);
2878 + }
2879 + }
2880 + } // end while part of format description
2881 + next_char(); // Skip '%'
2882 + next_char(); // Skip '}'
2883 +
2884 + skipws();
2885 +
2886 + if (_AD._adlocation_debug) {
2887 + encoding->add_code(end_line_marker());
2888 + }
2889 +
2890 + // Debug Stuff
2891 + if (_AD._adl_debug > 1) fprintf(stderr, "EncodingClass Form: %s\n", ec_name);
2818 2892 }
2819 2893
2820 2894
2821 2895 //------------------------------ins_encode_parse-------------------------------
2822 2896 // Encode rules have the form
2823 2897 // ins_encode( encode_class_name(parameter_list), ... );
2824 2898 //
2825 2899 // The "encode_class_name" must be defined in the encode section
2826 2900 // The parameter list contains $names that are locals.
2827 2901 //
2828 2902 // Alternatively it can be written like this:
2829 2903 //
2830 2904 // ins_encode %{
↓ open down ↓ |
3 lines elided |
↑ open up ↑ |
2831 2905 // ... // body
2832 2906 // %}
2833 2907 //
2834 2908 // which synthesizes a new encoding class taking the same arguments as
2835 2909 // the InstructForm, and automatically prefixes the definition with:
2836 2910 //
2837 2911 // MacroAssembler masm(&cbuf);\n");
2838 2912 //
2839 2913 // making it more compact to take advantage of the MacroAssembler and
2840 2914 // placing the assembly closer to it's use by instructions.
2841 -InsEncode *ADLParser::ins_encode_parse(InstructForm &inst) {
2915 +void ADLParser::ins_encode_parse(InstructForm& inst) {
2842 2916
2843 2917 // Parse encode class name
2844 2918 skipws(); // Skip whitespace
2845 2919 if (_curchar != '(') {
2846 2920 // Check for ins_encode %{ form
2847 2921 if ((_curchar == '%') && (*(_ptr+1) == '{')) {
2848 2922 next_char(); // Skip '%'
2849 2923 next_char(); // Skip '{'
2850 2924
2851 2925 // Parse the block form of ins_encode
2852 - return ins_encode_parse_block(inst);
2926 + ins_encode_parse_block(inst);
2927 + return;
2853 2928 }
2854 2929
2855 2930 parse_err(SYNERR, "missing '%%{' or '(' in ins_encode definition\n");
2856 - return NULL;
2931 + return;
2857 2932 }
2858 2933 next_char(); // move past '('
2859 2934 skipws();
2860 2935
2861 2936 InsEncode *encrule = new InsEncode(); // Encode class for instruction
2862 2937 encrule->_linenum = linenum();
2863 2938 char *ec_name = NULL; // String representation of encode rule
2864 2939 // identifier is optional.
2865 2940 while (_curchar != ')') {
2866 2941 ec_name = get_ident();
2867 2942 if (ec_name == NULL) {
2868 2943 parse_err(SYNERR, "Invalid encode class name after 'ins_encode('.\n");
2869 - return NULL;
2944 + return;
2870 2945 }
2871 2946 // Check that encoding is defined in the encode section
2872 2947 EncClass *encode_class = _AD._encode->encClass(ec_name);
2873 2948 if (encode_class == NULL) {
2874 2949 // Like to defer checking these till later...
2875 2950 // parse_err(WARN, "Using an undefined encode class '%s' in 'ins_encode'.\n", ec_name);
2876 2951 }
2877 2952
2878 2953 // Get list for encode method's parameters
2879 2954 NameAndList *params = encrule->add_encode(ec_name);
2880 2955
2881 2956 // Parse the parameters to this encode method.
2882 2957 skipws();
2883 2958 if ( _curchar == '(' ) {
2884 2959 next_char(); // move past '(' for parameters
2885 2960
2886 2961 // Parse the encode method's parameters
2887 2962 while (_curchar != ')') {
2888 2963 char *param = get_ident_or_literal_constant("encoding operand");
2889 2964 if ( param != NULL ) {
2890 2965 // Found a parameter:
↓ open down ↓ |
11 lines elided |
↑ open up ↑ |
2891 2966 // Check it is a local name, add it to the list, then check for more
2892 2967 // New: allow hex constants as parameters to an encode method.
2893 2968 // New: allow parenthesized expressions as parameters.
2894 2969 // New: allow "primary", "secondary", "tertiary" as parameters.
2895 2970 // New: allow user-defined register name as parameter
2896 2971 if ( (inst._localNames[param] == NULL) &&
2897 2972 !ADLParser::is_literal_constant(param) &&
2898 2973 (Opcode::as_opcode_type(param) == Opcode::NOT_AN_OPCODE) &&
2899 2974 ((_AD._register == NULL ) || (_AD._register->getRegDef(param) == NULL)) ) {
2900 2975 parse_err(SYNERR, "Using non-locally defined parameter %s for encoding %s.\n", param, ec_name);
2901 - return NULL;
2976 + return;
2902 2977 }
2903 2978 params->add_entry(param);
2904 2979
2905 2980 skipws();
2906 2981 if (_curchar == ',' ) {
2907 2982 // More parameters to come
2908 2983 next_char(); // move past ',' between parameters
2909 2984 skipws(); // Skip to next parameter
2910 2985 }
2911 2986 else if (_curchar == ')') {
2912 2987 // Done with parameter list
2913 2988 }
2914 2989 else {
2915 2990 // Only ',' or ')' are valid after a parameter name
2916 2991 parse_err(SYNERR, "expected ',' or ')' after parameter %s.\n",
2917 2992 ec_name);
2918 - return NULL;
2993 + return;
2919 2994 }
2920 2995
2921 2996 } else {
2922 2997 skipws();
2923 2998 // Did not find a parameter
2924 2999 if (_curchar == ',') {
2925 3000 parse_err(SYNERR, "Expected encode parameter before ',' in encoding %s.\n", ec_name);
2926 - return NULL;
3001 + return;
2927 3002 }
2928 3003 if (_curchar != ')') {
2929 3004 parse_err(SYNERR, "Expected ')' after encode parameters.\n");
2930 - return NULL;
3005 + return;
2931 3006 }
2932 3007 }
2933 3008 } // WHILE loop collecting parameters
2934 3009 next_char(); // move past ')' at end of parameters
2935 3010 } // done with parameter list for encoding
2936 3011
2937 3012 // Check for ',' or ')' after encoding
2938 3013 skipws(); // move to character after parameters
2939 3014 if ( _curchar == ',' ) {
2940 3015 // Found a ','
2941 3016 next_char(); // move past ',' between encode methods
2942 3017 skipws();
2943 3018 }
2944 3019 else if ( _curchar != ')' ) {
2945 3020 // If not a ',' then only a ')' is allowed
2946 3021 parse_err(SYNERR, "Expected ')' after encoding %s.\n", ec_name);
2947 - return NULL;
3022 + return;
2948 3023 }
2949 3024
2950 3025 // Check for ',' separating parameters
2951 3026 // if ( _curchar != ',' && _curchar != ')' ) {
2952 3027 // parse_err(SYNERR, "expected ',' or ')' after encode method inside ins_encode.\n");
2953 3028 // return NULL;
2954 3029 // }
2955 3030
2956 3031 } // done parsing ins_encode methods and their parameters
2957 3032 if (_curchar != ')') {
2958 3033 parse_err(SYNERR, "Missing ')' at end of ins_encode description.\n");
2959 - return NULL;
3034 + return;
2960 3035 }
2961 3036 next_char(); // move past ')'
2962 3037 skipws(); // Skip leading whitespace
2963 3038
2964 3039 if ( _curchar != ';' ) {
2965 3040 parse_err(SYNERR, "Missing ';' at end of ins_encode.\n");
2966 - return NULL;
3041 + return;
2967 3042 }
2968 3043 next_char(); // move past ';'
2969 3044 skipws(); // be friendly to oper_parse()
2970 3045
2971 3046 // Debug Stuff
2972 3047 if (_AD._adl_debug > 1) fprintf(stderr,"Instruction Encode: %s\n", ec_name);
2973 3048
2974 - return encrule;
3049 + // Set encode class of this instruction.
3050 + inst._insencode = encrule;
3051 +}
3052 +
3053 +
3054 +//------------------------------constant_parse---------------------------------
3055 +// Parse a constant expression.
3056 +void ADLParser::constant_parse(InstructForm& inst) {
3057 + // Create a new encoding name based on the name of the instruction
3058 + // definition, which should be unique.
3059 + const char* prefix = "__constant_";
3060 + char* ec_name = (char*) malloc(strlen(inst._ident) + strlen(prefix) + 1);
3061 + sprintf(ec_name, "%s%s", prefix, inst._ident);
3062 +
3063 + assert(_AD._encode->encClass(ec_name) == NULL, "shouldn't already exist");
3064 + EncClass* encoding = _AD._encode->add_EncClass(ec_name);
3065 + encoding->_linenum = linenum();
3066 +
3067 + // synthesize the arguments list for the enc_class from the
3068 + // arguments to the instruct definition.
3069 + const char* param = NULL;
3070 + inst._parameters.reset();
3071 + while ((param = inst._parameters.iter()) != NULL) {
3072 + OperandForm* opForm = (OperandForm*) inst._localNames[param];
3073 + encoding->add_parameter(opForm->_ident, param);
3074 + }
3075 +
3076 + // Parse the following ( ) expression.
3077 + constant_parse_expression(encoding, ec_name);
3078 +
3079 + // Build an encoding rule which invokes the encoding rule we just
3080 + // created, passing all arguments that we received.
3081 + InsEncode* encrule = new InsEncode(); // Encode class for instruction
3082 + NameAndList* params = encrule->add_encode(ec_name);
3083 + inst._parameters.reset();
3084 + while ((param = inst._parameters.iter()) != NULL) {
3085 + params->add_entry(param);
3086 + }
3087 +
3088 + // Set encode class of this instruction.
3089 + inst._constant = encrule;
3090 +}
3091 +
3092 +
3093 +//------------------------------constant_parse_expression----------------------
3094 +void ADLParser::constant_parse_expression(EncClass* encoding, char* ec_name) {
3095 + skipws();
3096 +
3097 + // Prepend location descriptor, for debugging; cf. ADLParser::find_cpp_block
3098 + if (_AD._adlocation_debug) {
3099 + encoding->add_code(get_line_string());
3100 + }
3101 +
3102 + // Start code line.
3103 + encoding->add_code(" add_to_constant_table");
3104 +
3105 + // Parse everything in ( ) expression.
3106 + encoding->add_code("(");
3107 + next_char(); // Skip '('
3108 + int parens_depth = 1;
3109 +
3110 + // Collect the parts of the constant expression.
3111 + // (1) strings that are passed through to output
3112 + // (2) replacement/substitution variable, preceeded by a '$'
3113 + while (parens_depth > 0) {
3114 + if (_curchar == '(') {
3115 + parens_depth++;
3116 + encoding->add_code("(");
3117 + next_char();
3118 + }
3119 + else if (_curchar == ')') {
3120 + parens_depth--;
3121 + encoding->add_code(")");
3122 + next_char();
3123 + }
3124 + else {
3125 + // (1)
3126 + // Check if there is a string to pass through to output
3127 + char *start = _ptr; // Record start of the next string
3128 + while ((_curchar != '$') && (_curchar != '(') && (_curchar != ')')) {
3129 + next_char();
3130 + }
3131 + // If a string was found, terminate it and record in EncClass
3132 + if (start != _ptr) {
3133 + *_ptr = '\0'; // Terminate the string
3134 + encoding->add_code(start);
3135 + }
3136 +
3137 + // (2)
3138 + // If we are at a replacement variable, copy it and record in EncClass.
3139 + if (_curchar == '$') {
3140 + // Found replacement Variable
3141 + char* rep_var = get_rep_var_ident_dup();
3142 + encoding->add_rep_var(rep_var);
3143 + }
3144 + }
3145 + }
3146 +
3147 + // Finish code line.
3148 + encoding->add_code(";");
3149 +
3150 + if (_AD._adlocation_debug) {
3151 + encoding->add_code(end_line_marker());
3152 + }
3153 +
3154 + // Debug Stuff
3155 + if (_AD._adl_debug > 1) fprintf(stderr, "EncodingClass Form: %s\n", ec_name);
2975 3156 }
2976 3157
2977 3158
2978 3159 //------------------------------size_parse-----------------------------------
2979 3160 char* ADLParser::size_parse(InstructForm *instr) {
2980 3161 char* sizeOfInstr = NULL;
2981 3162
2982 3163 // Get value of the instruction's size
2983 3164 skipws();
2984 3165
2985 3166 // Parse size
2986 3167 sizeOfInstr = get_paren_expr("size expression");
2987 3168 if (sizeOfInstr == NULL) {
2988 3169 parse_err(SYNERR, "size of opcode expected at %c\n", _curchar);
2989 3170 return NULL;
2990 3171 }
2991 3172
2992 3173 skipws();
2993 3174
2994 3175 // Check for terminator
2995 3176 if (_curchar != ';') {
2996 3177 parse_err(SYNERR, "missing ';' in ins_attrib definition\n");
2997 3178 return NULL;
2998 3179 }
2999 3180 next_char(); // Advance past the ';'
3000 3181 skipws(); // necessary for instr_parse()
3001 3182
3002 3183 // Debug Stuff
3003 3184 if (_AD._adl_debug > 1) {
3004 3185 if (sizeOfInstr != NULL) {
3005 3186 fprintf(stderr,"size of opcode: %s\n", sizeOfInstr);
3006 3187 }
3007 3188 }
3008 3189
3009 3190 return sizeOfInstr;
3010 3191 }
3011 3192
3012 3193
3013 3194 //------------------------------opcode_parse-----------------------------------
3014 3195 Opcode * ADLParser::opcode_parse(InstructForm *instr) {
3015 3196 char *primary = NULL;
3016 3197 char *secondary = NULL;
3017 3198 char *tertiary = NULL;
3018 3199
3019 3200 char *val = NULL;
3020 3201 Opcode *opcode = NULL;
3021 3202
3022 3203 // Get value of the instruction's opcode
3023 3204 skipws();
3024 3205 if (_curchar != '(') { // Check for parenthesized operand list
3025 3206 parse_err(SYNERR, "missing '(' in expand instruction declaration\n");
3026 3207 return NULL;
3027 3208 }
3028 3209 next_char(); // skip open paren
3029 3210 skipws();
3030 3211 if (_curchar != ')') {
3031 3212 // Parse primary, secondary, and tertiary opcodes, if provided.
3032 3213 if ( ((primary = get_ident_or_literal_constant("primary opcode")) == NULL) ) {
3033 3214 parse_err(SYNERR, "primary hex opcode expected at %c\n", _curchar);
3034 3215 return NULL;
3035 3216 }
3036 3217 skipws();
3037 3218 if (_curchar == ',') {
3038 3219 next_char();
3039 3220 skipws();
3040 3221 // Parse secondary opcode
3041 3222 if ( ((secondary = get_ident_or_literal_constant("secondary opcode")) == NULL) ) {
3042 3223 parse_err(SYNERR, "secondary hex opcode expected at %c\n", _curchar);
3043 3224 return NULL;
3044 3225 }
3045 3226 skipws();
3046 3227 if (_curchar == ',') {
3047 3228 next_char();
3048 3229 skipws();
3049 3230 // Parse tertiary opcode
3050 3231 if ( ((tertiary = get_ident_or_literal_constant("tertiary opcode")) == NULL) ) {
3051 3232 parse_err(SYNERR,"tertiary hex opcode expected at %c\n", _curchar);
3052 3233 return NULL;
3053 3234 }
3054 3235 skipws();
3055 3236 }
3056 3237 }
3057 3238 skipws();
3058 3239 if (_curchar != ')') {
3059 3240 parse_err(SYNERR, "Missing ')' in opcode description\n");
3060 3241 return NULL;
3061 3242 }
3062 3243 }
3063 3244 next_char(); // Skip ')'
3064 3245 skipws();
3065 3246 // Check for terminator
3066 3247 if (_curchar != ';') {
3067 3248 parse_err(SYNERR, "missing ';' in ins_attrib definition\n");
3068 3249 return NULL;
3069 3250 }
3070 3251 next_char(); // Advance past the ';'
3071 3252 skipws(); // necessary for instr_parse()
3072 3253
3073 3254 // Debug Stuff
3074 3255 if (_AD._adl_debug > 1) {
3075 3256 if (primary != NULL) fprintf(stderr,"primary opcode: %s\n", primary);
3076 3257 if (secondary != NULL) fprintf(stderr,"secondary opcode: %s\n", secondary);
3077 3258 if (tertiary != NULL) fprintf(stderr,"tertiary opcode: %s\n", tertiary);
3078 3259 }
3079 3260
3080 3261 // Generate new object and return
3081 3262 opcode = new Opcode(primary, secondary, tertiary);
3082 3263 return opcode;
3083 3264 }
3084 3265
3085 3266
3086 3267 //------------------------------interface_parse--------------------------------
3087 3268 Interface *ADLParser::interface_parse(void) {
3088 3269 char *iface_name = NULL; // Name of interface class being used
3089 3270 char *iface_code = NULL; // Describe components of this class
3090 3271
3091 3272 // Get interface class name
3092 3273 skipws(); // Skip whitespace
3093 3274 if (_curchar != '(') {
3094 3275 parse_err(SYNERR, "Missing '(' at start of interface description.\n");
3095 3276 return NULL;
3096 3277 }
3097 3278 next_char(); // move past '('
3098 3279 skipws();
3099 3280 iface_name = get_ident();
3100 3281 if (iface_name == NULL) {
3101 3282 parse_err(SYNERR, "missing interface name after 'interface'.\n");
3102 3283 return NULL;
3103 3284 }
3104 3285 skipws();
3105 3286 if (_curchar != ')') {
3106 3287 parse_err(SYNERR, "Missing ')' after name of interface.\n");
3107 3288 return NULL;
3108 3289 }
3109 3290 next_char(); // move past ')'
3110 3291
3111 3292 // Get details of the interface,
3112 3293 // for the type of interface indicated by iface_name.
3113 3294 Interface *inter = NULL;
3114 3295 skipws();
3115 3296 if ( _curchar != ';' ) {
3116 3297 if ( strcmp(iface_name,"MEMORY_INTER") == 0 ) {
3117 3298 inter = mem_interface_parse();
3118 3299 }
3119 3300 else if ( strcmp(iface_name,"COND_INTER") == 0 ) {
3120 3301 inter = cond_interface_parse();
3121 3302 }
3122 3303 // The parse routines consume the "%}"
3123 3304
3124 3305 // Check for probable extra ';' after defining block.
3125 3306 if ( _curchar == ';' ) {
3126 3307 parse_err(SYNERR, "Extra ';' after defining interface block.\n");
3127 3308 next_char(); // Skip ';'
3128 3309 return NULL;
3129 3310 }
3130 3311 } else {
3131 3312 next_char(); // move past ';'
3132 3313
3133 3314 // Create appropriate interface object
3134 3315 if ( strcmp(iface_name,"REG_INTER") == 0 ) {
3135 3316 inter = new RegInterface();
3136 3317 }
3137 3318 else if ( strcmp(iface_name,"CONST_INTER") == 0 ) {
3138 3319 inter = new ConstInterface();
3139 3320 }
3140 3321 }
3141 3322 skipws(); // be friendly to oper_parse()
3142 3323 // Debug Stuff
3143 3324 if (_AD._adl_debug > 1) fprintf(stderr,"Interface Form: %s\n", iface_name);
3144 3325
3145 3326 // Create appropriate interface object and return.
3146 3327 return inter;
3147 3328 }
3148 3329
3149 3330
3150 3331 //------------------------------mem_interface_parse----------------------------
3151 3332 Interface *ADLParser::mem_interface_parse(void) {
3152 3333 // Fields for MemInterface
3153 3334 char *base = NULL;
3154 3335 char *index = NULL;
3155 3336 char *scale = NULL;
3156 3337 char *disp = NULL;
3157 3338
3158 3339 if (_curchar != '%') {
3159 3340 parse_err(SYNERR, "Missing '%{' for 'interface' block.\n");
3160 3341 return NULL;
3161 3342 }
3162 3343 next_char(); // Skip '%'
3163 3344 if (_curchar != '{') {
3164 3345 parse_err(SYNERR, "Missing '%{' for 'interface' block.\n");
3165 3346 return NULL;
3166 3347 }
3167 3348 next_char(); // Skip '{'
3168 3349 skipws();
3169 3350 do {
3170 3351 char *field = get_ident();
3171 3352 if (field == NULL) {
3172 3353 parse_err(SYNERR, "Expected keyword, base|index|scale|disp, or '%}' ending interface.\n");
3173 3354 return NULL;
3174 3355 }
3175 3356 if ( strcmp(field,"base") == 0 ) {
3176 3357 base = interface_field_parse();
3177 3358 }
3178 3359 else if ( strcmp(field,"index") == 0 ) {
3179 3360 index = interface_field_parse();
3180 3361 }
3181 3362 else if ( strcmp(field,"scale") == 0 ) {
3182 3363 scale = interface_field_parse();
3183 3364 }
3184 3365 else if ( strcmp(field,"disp") == 0 ) {
3185 3366 disp = interface_field_parse();
3186 3367 }
3187 3368 else {
3188 3369 parse_err(SYNERR, "Expected keyword, base|index|scale|disp, or '%}' ending interface.\n");
3189 3370 return NULL;
3190 3371 }
3191 3372 } while( _curchar != '%' );
3192 3373 next_char(); // Skip '%'
3193 3374 if ( _curchar != '}' ) {
3194 3375 parse_err(SYNERR, "Missing '%}' for 'interface' block.\n");
3195 3376 return NULL;
3196 3377 }
3197 3378 next_char(); // Skip '}'
3198 3379
3199 3380 // Construct desired object and return
3200 3381 Interface *inter = new MemInterface(base, index, scale, disp);
3201 3382 return inter;
3202 3383 }
3203 3384
3204 3385
3205 3386 //------------------------------cond_interface_parse---------------------------
3206 3387 Interface *ADLParser::cond_interface_parse(void) {
3207 3388 char *equal;
3208 3389 char *not_equal;
3209 3390 char *less;
3210 3391 char *greater_equal;
3211 3392 char *less_equal;
3212 3393 char *greater;
3213 3394 const char *equal_format = "eq";
3214 3395 const char *not_equal_format = "ne";
3215 3396 const char *less_format = "lt";
3216 3397 const char *greater_equal_format = "ge";
3217 3398 const char *less_equal_format = "le";
3218 3399 const char *greater_format = "gt";
3219 3400
3220 3401 if (_curchar != '%') {
3221 3402 parse_err(SYNERR, "Missing '%{' for 'cond_interface' block.\n");
3222 3403 return NULL;
3223 3404 }
3224 3405 next_char(); // Skip '%'
3225 3406 if (_curchar != '{') {
3226 3407 parse_err(SYNERR, "Missing '%{' for 'cond_interface' block.\n");
3227 3408 return NULL;
3228 3409 }
3229 3410 next_char(); // Skip '{'
3230 3411 skipws();
3231 3412 do {
3232 3413 char *field = get_ident();
3233 3414 if (field == NULL) {
3234 3415 parse_err(SYNERR, "Expected keyword, base|index|scale|disp, or '%}' ending interface.\n");
3235 3416 return NULL;
3236 3417 }
3237 3418 if ( strcmp(field,"equal") == 0 ) {
3238 3419 equal = interface_field_parse(&equal_format);
3239 3420 }
3240 3421 else if ( strcmp(field,"not_equal") == 0 ) {
3241 3422 not_equal = interface_field_parse(¬_equal_format);
3242 3423 }
3243 3424 else if ( strcmp(field,"less") == 0 ) {
3244 3425 less = interface_field_parse(&less_format);
3245 3426 }
3246 3427 else if ( strcmp(field,"greater_equal") == 0 ) {
3247 3428 greater_equal = interface_field_parse(&greater_equal_format);
3248 3429 }
3249 3430 else if ( strcmp(field,"less_equal") == 0 ) {
3250 3431 less_equal = interface_field_parse(&less_equal_format);
3251 3432 }
3252 3433 else if ( strcmp(field,"greater") == 0 ) {
3253 3434 greater = interface_field_parse(&greater_format);
3254 3435 }
3255 3436 else {
3256 3437 parse_err(SYNERR, "Expected keyword, base|index|scale|disp, or '%}' ending interface.\n");
3257 3438 return NULL;
3258 3439 }
3259 3440 } while( _curchar != '%' );
3260 3441 next_char(); // Skip '%'
3261 3442 if ( _curchar != '}' ) {
3262 3443 parse_err(SYNERR, "Missing '%}' for 'interface' block.\n");
3263 3444 return NULL;
3264 3445 }
3265 3446 next_char(); // Skip '}'
3266 3447
3267 3448 // Construct desired object and return
3268 3449 Interface *inter = new CondInterface(equal, equal_format,
3269 3450 not_equal, not_equal_format,
3270 3451 less, less_format,
3271 3452 greater_equal, greater_equal_format,
3272 3453 less_equal, less_equal_format,
3273 3454 greater, greater_format);
3274 3455 return inter;
3275 3456 }
3276 3457
3277 3458
3278 3459 //------------------------------interface_field_parse--------------------------
3279 3460 char *ADLParser::interface_field_parse(const char ** format) {
3280 3461 char *iface_field = NULL;
3281 3462
3282 3463 // Get interface field
3283 3464 skipws(); // Skip whitespace
3284 3465 if (_curchar != '(') {
3285 3466 parse_err(SYNERR, "Missing '(' at start of interface field.\n");
3286 3467 return NULL;
3287 3468 }
3288 3469 next_char(); // move past '('
3289 3470 skipws();
3290 3471 if ( _curchar != '0' && _curchar != '$' ) {
3291 3472 parse_err(SYNERR, "missing or invalid interface field contents.\n");
3292 3473 return NULL;
3293 3474 }
3294 3475 iface_field = get_rep_var_ident();
3295 3476 if (iface_field == NULL) {
3296 3477 parse_err(SYNERR, "missing or invalid interface field contents.\n");
3297 3478 return NULL;
3298 3479 }
3299 3480 skipws();
3300 3481 if (format != NULL && _curchar == ',') {
3301 3482 next_char();
3302 3483 skipws();
3303 3484 if (_curchar != '"') {
3304 3485 parse_err(SYNERR, "Missing '\"' in field format .\n");
3305 3486 return NULL;
3306 3487 }
3307 3488 next_char();
3308 3489 char *start = _ptr; // Record start of the next string
3309 3490 while ((_curchar != '"') && (_curchar != '%') && (_curchar != '\n')) {
3310 3491 if (_curchar == '\\') next_char(); // superquote
3311 3492 if (_curchar == '\n') parse_err(SYNERR, "newline in string"); // unimplemented!
3312 3493 next_char();
3313 3494 }
3314 3495 if (_curchar != '"') {
3315 3496 parse_err(SYNERR, "Missing '\"' at end of field format .\n");
3316 3497 return NULL;
3317 3498 }
3318 3499 // If a string was found, terminate it and record in FormatRule
3319 3500 if ( start != _ptr ) {
3320 3501 *_ptr = '\0'; // Terminate the string
3321 3502 *format = start;
3322 3503 }
3323 3504 next_char();
3324 3505 skipws();
3325 3506 }
3326 3507 if (_curchar != ')') {
3327 3508 parse_err(SYNERR, "Missing ')' after interface field.\n");
3328 3509 return NULL;
3329 3510 }
3330 3511 next_char(); // move past ')'
3331 3512 skipws();
3332 3513 if ( _curchar != ';' ) {
3333 3514 parse_err(SYNERR, "Missing ';' at end of interface field.\n");
3334 3515 return NULL;
3335 3516 }
3336 3517 next_char(); // move past ';'
3337 3518 skipws(); // be friendly to interface_parse()
3338 3519
3339 3520 return iface_field;
3340 3521 }
3341 3522
3342 3523
3343 3524 //------------------------------match_parse------------------------------------
3344 3525 MatchRule *ADLParser::match_parse(FormDict &operands) {
3345 3526 MatchRule *match; // Match Rule class for instruction/operand
3346 3527 char *cnstr = NULL; // Code for constructor
3347 3528 int depth = 0; // Counter for matching parentheses
3348 3529 int numleaves = 0; // Counter for number of leaves in rule
3349 3530
3350 3531 // Parse the match rule tree
3351 3532 MatchNode *mnode = matchNode_parse(operands, depth, numleaves, true);
3352 3533
3353 3534 // Either there is a block with a constructor, or a ';' here
3354 3535 skipws(); // Skip whitespace
3355 3536 if ( _curchar == ';' ) { // Semicolon is valid terminator
3356 3537 cnstr = NULL; // no constructor for this form
3357 3538 next_char(); // Move past the ';', replaced with '\0'
3358 3539 }
3359 3540 else if ((cnstr = find_cpp_block("match constructor")) == NULL ) {
3360 3541 parse_err(SYNERR, "invalid construction of match rule\n"
3361 3542 "Missing ';' or invalid '%{' and '%}' constructor\n");
3362 3543 return NULL; // No MatchRule to return
3363 3544 }
3364 3545 if (_AD._adl_debug > 1)
3365 3546 if (cnstr) fprintf(stderr,"Match Constructor: %s\n", cnstr);
3366 3547 // Build new MatchRule object
3367 3548 match = new MatchRule(_AD, mnode, depth, cnstr, numleaves);
3368 3549 skipws(); // Skip any trailing whitespace
3369 3550 return match; // Return MatchRule object
3370 3551 }
3371 3552
3372 3553 //------------------------------format_parse-----------------------------------
3373 3554 FormatRule* ADLParser::format_parse(void) {
3374 3555 char *desc = NULL;
3375 3556 FormatRule *format = (new FormatRule(desc));
3376 3557
3377 3558 // Without expression form, MUST have a code block;
3378 3559 skipws(); // Skip whitespace
3379 3560 if ( _curchar == ';' ) { // Semicolon is valid terminator
3380 3561 desc = NULL; // no constructor for this form
3381 3562 next_char(); // Move past the ';', replaced with '\0'
3382 3563 }
3383 3564 else if ( _curchar == '%' && *(_ptr+1) == '{') {
3384 3565 next_char(); // Move past the '%'
3385 3566 next_char(); // Move past the '{'
3386 3567
3387 3568 skipws();
3388 3569 if (_curchar == '$') {
3389 3570 char* ident = get_rep_var_ident();
3390 3571 if (strcmp(ident, "$$template") == 0) return template_parse();
3391 3572 parse_err(SYNERR, "Unknown \"%s\" directive in format", ident);
3392 3573 return NULL;
3393 3574 }
3394 3575 // Check for the opening '"' inside the format description
3395 3576 if ( _curchar == '"' ) {
3396 3577 next_char(); // Move past the initial '"'
3397 3578 if( _curchar == '"' ) { // Handle empty format string case
3398 3579 *_ptr = '\0'; // Terminate empty string
3399 3580 format->_strings.addName(_ptr);
3400 3581 }
3401 3582
3402 3583 // Collect the parts of the format description
3403 3584 // (1) strings that are passed through to tty->print
3404 3585 // (2) replacement/substitution variable, preceeded by a '$'
3405 3586 // (3) multi-token ANSIY C style strings
3406 3587 while ( true ) {
3407 3588 if ( _curchar == '%' || _curchar == '\n' ) {
3408 3589 if ( _curchar != '"' ) {
3409 3590 parse_err(SYNERR, "missing '\"' at end of format block");
3410 3591 return NULL;
3411 3592 }
3412 3593 }
3413 3594
3414 3595 // (1)
3415 3596 // Check if there is a string to pass through to output
3416 3597 char *start = _ptr; // Record start of the next string
3417 3598 while ((_curchar != '$') && (_curchar != '"') && (_curchar != '%') && (_curchar != '\n')) {
3418 3599 if (_curchar == '\\') {
3419 3600 next_char(); // superquote
3420 3601 if ((_curchar == '$') || (_curchar == '%'))
3421 3602 // hack to avoid % escapes and warnings about undefined \ escapes
3422 3603 *(_ptr-1) = _curchar;
3423 3604 }
3424 3605 if (_curchar == '\n') parse_err(SYNERR, "newline in string"); // unimplemented!
3425 3606 next_char();
3426 3607 }
3427 3608 // If a string was found, terminate it and record in FormatRule
3428 3609 if ( start != _ptr ) {
3429 3610 *_ptr = '\0'; // Terminate the string
3430 3611 format->_strings.addName(start);
3431 3612 }
3432 3613
3433 3614 // (2)
3434 3615 // If we are at a replacement variable,
3435 3616 // copy it and record in FormatRule
3436 3617 if ( _curchar == '$' ) {
3437 3618 next_char(); // Move past the '$'
3438 3619 char* rep_var = get_ident(); // Nil terminate the variable name
3439 3620 rep_var = strdup(rep_var);// Copy the string
3440 3621 *_ptr = _curchar; // and replace Nil with original character
3441 3622 format->_rep_vars.addName(rep_var);
3442 3623 // Add flag to _strings list indicating we should check _rep_vars
3443 3624 format->_strings.addName(NameList::_signal);
3444 3625 }
3445 3626
3446 3627 // (3)
3447 3628 // Allow very long strings to be broken up,
3448 3629 // using the ANSI C syntax "foo\n" <newline> "bar"
3449 3630 if ( _curchar == '"') {
3450 3631 next_char(); // Move past the '"'
3451 3632 skipws(); // Skip white space before next string token
3452 3633 if ( _curchar != '"') {
3453 3634 break;
3454 3635 } else {
3455 3636 // Found one. Skip both " and the whitespace in between.
3456 3637 next_char();
3457 3638 }
3458 3639 }
3459 3640 } // end while part of format description
3460 3641
3461 3642 // Check for closing '"' and '%}' in format description
3462 3643 skipws(); // Move to closing '%}'
3463 3644 if ( _curchar != '%' ) {
3464 3645 parse_err(SYNERR, "non-blank characters between closing '\"' and '%' in format");
3465 3646 return NULL;
3466 3647 }
3467 3648 } // Done with format description inside
3468 3649
3469 3650 skipws();
3470 3651 // Past format description, at '%'
3471 3652 if ( _curchar != '%' || *(_ptr+1) != '}' ) {
3472 3653 parse_err(SYNERR, "missing '%}' at end of format block");
3473 3654 return NULL;
3474 3655 }
3475 3656 next_char(); // Move past the '%'
3476 3657 next_char(); // Move past the '}'
3477 3658 }
3478 3659 else { // parameter list alone must terminate with a ';'
3479 3660 parse_err(SYNERR, "missing ';' after Format expression");
3480 3661 return NULL;
3481 3662 }
3482 3663 // Debug Stuff
3483 3664 if (_AD._adl_debug > 1) fprintf(stderr,"Format Rule: %s\n", desc);
3484 3665
3485 3666 skipws();
3486 3667 return format;
3487 3668 }
3488 3669
3489 3670
3490 3671 //------------------------------template_parse-----------------------------------
3491 3672 FormatRule* ADLParser::template_parse(void) {
3492 3673 char *desc = NULL;
3493 3674 FormatRule *format = (new FormatRule(desc));
3494 3675
3495 3676 skipws();
3496 3677 while ( (_curchar != '%') && (*(_ptr+1) != '}') ) {
3497 3678
3498 3679 // (1)
3499 3680 // Check if there is a string to pass through to output
3500 3681 {
3501 3682 char *start = _ptr; // Record start of the next string
3502 3683 while ((_curchar != '$') && ((_curchar != '%') || (*(_ptr+1) != '}')) ) {
3503 3684 // If at the start of a comment, skip past it
3504 3685 if( (_curchar == '/') && ((*(_ptr+1) == '/') || (*(_ptr+1) == '*')) ) {
3505 3686 skipws_no_preproc();
3506 3687 } else {
3507 3688 // ELSE advance to the next character, or start of the next line
3508 3689 next_char_or_line();
3509 3690 }
3510 3691 }
3511 3692 // If a string was found, terminate it and record in EncClass
3512 3693 if ( start != _ptr ) {
3513 3694 *_ptr = '\0'; // Terminate the string
3514 3695 // Add flag to _strings list indicating we should check _rep_vars
3515 3696 format->_strings.addName(NameList::_signal2);
3516 3697 format->_strings.addName(start);
3517 3698 }
3518 3699 }
3519 3700
3520 3701 // (2)
3521 3702 // If we are at a replacement variable,
3522 3703 // copy it and record in EncClass
3523 3704 if ( _curchar == '$' ) {
3524 3705 // Found replacement Variable
3525 3706 char *rep_var = get_rep_var_ident_dup();
3526 3707 if (strcmp(rep_var, "$emit") == 0) {
3527 3708 // switch to normal format parsing
3528 3709 next_char();
3529 3710 next_char();
3530 3711 skipws();
3531 3712 // Check for the opening '"' inside the format description
3532 3713 if ( _curchar == '"' ) {
3533 3714 next_char(); // Move past the initial '"'
3534 3715 if( _curchar == '"' ) { // Handle empty format string case
3535 3716 *_ptr = '\0'; // Terminate empty string
3536 3717 format->_strings.addName(_ptr);
3537 3718 }
3538 3719
3539 3720 // Collect the parts of the format description
3540 3721 // (1) strings that are passed through to tty->print
3541 3722 // (2) replacement/substitution variable, preceeded by a '$'
3542 3723 // (3) multi-token ANSIY C style strings
3543 3724 while ( true ) {
3544 3725 if ( _curchar == '%' || _curchar == '\n' ) {
3545 3726 parse_err(SYNERR, "missing '\"' at end of format block");
3546 3727 return NULL;
3547 3728 }
3548 3729
3549 3730 // (1)
3550 3731 // Check if there is a string to pass through to output
3551 3732 char *start = _ptr; // Record start of the next string
3552 3733 while ((_curchar != '$') && (_curchar != '"') && (_curchar != '%') && (_curchar != '\n')) {
3553 3734 if (_curchar == '\\') next_char(); // superquote
3554 3735 if (_curchar == '\n') parse_err(SYNERR, "newline in string"); // unimplemented!
3555 3736 next_char();
3556 3737 }
3557 3738 // If a string was found, terminate it and record in FormatRule
3558 3739 if ( start != _ptr ) {
3559 3740 *_ptr = '\0'; // Terminate the string
3560 3741 format->_strings.addName(start);
3561 3742 }
3562 3743
3563 3744 // (2)
3564 3745 // If we are at a replacement variable,
3565 3746 // copy it and record in FormatRule
3566 3747 if ( _curchar == '$' ) {
3567 3748 next_char(); // Move past the '$'
3568 3749 char* next_rep_var = get_ident(); // Nil terminate the variable name
3569 3750 next_rep_var = strdup(next_rep_var);// Copy the string
3570 3751 *_ptr = _curchar; // and replace Nil with original character
3571 3752 format->_rep_vars.addName(next_rep_var);
3572 3753 // Add flag to _strings list indicating we should check _rep_vars
3573 3754 format->_strings.addName(NameList::_signal);
3574 3755 }
3575 3756
3576 3757 // (3)
3577 3758 // Allow very long strings to be broken up,
3578 3759 // using the ANSI C syntax "foo\n" <newline> "bar"
3579 3760 if ( _curchar == '"') {
3580 3761 next_char(); // Move past the '"'
3581 3762 skipws(); // Skip white space before next string token
3582 3763 if ( _curchar != '"') {
3583 3764 break;
3584 3765 } else {
3585 3766 // Found one. Skip both " and the whitespace in between.
3586 3767 next_char();
3587 3768 }
3588 3769 }
3589 3770 } // end while part of format description
3590 3771 }
3591 3772 } else {
3592 3773 // Add flag to _strings list indicating we should check _rep_vars
3593 3774 format->_rep_vars.addName(rep_var);
3594 3775 // Add flag to _strings list indicating we should check _rep_vars
3595 3776 format->_strings.addName(NameList::_signal3);
3596 3777 }
3597 3778 } // end while part of format description
3598 3779 }
3599 3780
3600 3781 skipws();
3601 3782 // Past format description, at '%'
3602 3783 if ( _curchar != '%' || *(_ptr+1) != '}' ) {
3603 3784 parse_err(SYNERR, "missing '%}' at end of format block");
3604 3785 return NULL;
3605 3786 }
3606 3787 next_char(); // Move past the '%'
3607 3788 next_char(); // Move past the '}'
3608 3789
3609 3790 // Debug Stuff
3610 3791 if (_AD._adl_debug > 1) fprintf(stderr,"Format Rule: %s\n", desc);
3611 3792
3612 3793 skipws();
3613 3794 return format;
3614 3795 }
3615 3796
3616 3797
3617 3798 //------------------------------effect_parse-----------------------------------
3618 3799 void ADLParser::effect_parse(InstructForm *instr) {
3619 3800 char* desc = NULL;
3620 3801
3621 3802 skipws(); // Skip whitespace
3622 3803 if (_curchar != '(') {
3623 3804 parse_err(SYNERR, "missing '(' in effect definition\n");
3624 3805 return;
3625 3806 }
3626 3807 // Get list of effect-operand pairs and insert into dictionary
3627 3808 else get_effectlist(instr->_effects, instr->_localNames);
3628 3809
3629 3810 // Debug Stuff
3630 3811 if (_AD._adl_debug > 1) fprintf(stderr,"Effect description: %s\n", desc);
3631 3812 if (_curchar != ';') {
3632 3813 parse_err(SYNERR, "missing ';' in Effect definition\n");
3633 3814 }
3634 3815 next_char(); // Skip ';'
3635 3816
3636 3817 }
3637 3818
3638 3819 //------------------------------expand_parse-----------------------------------
3639 3820 ExpandRule* ADLParser::expand_parse(InstructForm *instr) {
3640 3821 char *ident, *ident2;
3641 3822 OperandForm *oper;
3642 3823 InstructForm *ins;
3643 3824 NameAndList *instr_and_operands = NULL;
3644 3825 ExpandRule *exp = new ExpandRule();
3645 3826
3646 3827 // Expand is a block containing an ordered list of instructions, each of
3647 3828 // which has an ordered list of operands.
3648 3829 // Check for block delimiter
3649 3830 skipws(); // Skip leading whitespace
3650 3831 if ((_curchar != '%')
3651 3832 || (next_char(), (_curchar != '{')) ) { // If not open block
3652 3833 parse_err(SYNERR, "missing '%{' in expand definition\n");
3653 3834 return(NULL);
3654 3835 }
3655 3836 next_char(); // Maintain the invariant
3656 3837 do {
3657 3838 ident = get_ident(); // Grab next identifier
3658 3839 if (ident == NULL) {
3659 3840 parse_err(SYNERR, "identifier expected at %c\n", _curchar);
3660 3841 continue;
3661 3842 } // Check that you have a valid instruction
3662 3843 const Form *form = _globalNames[ident];
3663 3844 ins = form ? form->is_instruction() : NULL;
3664 3845 if (ins == NULL) {
3665 3846 // This is a new operand
3666 3847 oper = form ? form->is_operand() : NULL;
3667 3848 if (oper == NULL) {
3668 3849 parse_err(SYNERR, "instruction/operand name expected at %s\n", ident);
3669 3850 continue;
3670 3851 }
3671 3852 // Throw the operand on the _newopers list
3672 3853 skipws();
3673 3854 ident = get_unique_ident(instr->_localNames,"Operand");
3674 3855 if (ident == NULL) {
3675 3856 parse_err(SYNERR, "identifier expected at %c\n", _curchar);
3676 3857 continue;
3677 3858 }
3678 3859 exp->_newopers.addName(ident);
3679 3860 // Add new operand to LocalNames
3680 3861 instr->_localNames.Insert(ident, oper);
3681 3862 // Grab any constructor code and save as a string
3682 3863 char *c = NULL;
3683 3864 skipws();
3684 3865 if (_curchar == '%') { // Need a constructor for the operand
3685 3866 c = find_cpp_block("Operand Constructor");
3686 3867 if (c == NULL) {
3687 3868 parse_err(SYNERR, "Invalid code block for operand constructor\n", _curchar);
3688 3869 continue;
3689 3870 }
3690 3871 // Add constructor to _newopconst Dict
3691 3872 exp->_newopconst.Insert(ident, c);
3692 3873 }
3693 3874 else if (_curchar != ';') { // If no constructor, need a ;
3694 3875 parse_err(SYNERR, "Missing ; in expand rule operand declaration\n");
3695 3876 continue;
3696 3877 }
3697 3878 else next_char(); // Skip the ;
3698 3879 skipws();
3699 3880 }
3700 3881 else {
3701 3882 // Add instruction to list
3702 3883 instr_and_operands = new NameAndList(ident);
3703 3884 // Grab operands, build nameList of them, and then put into dictionary
3704 3885 skipws();
3705 3886 if (_curchar != '(') { // Check for parenthesized operand list
3706 3887 parse_err(SYNERR, "missing '(' in expand instruction declaration\n");
3707 3888 continue;
3708 3889 }
3709 3890 do {
3710 3891 next_char(); // skip open paren & comma characters
3711 3892 skipws();
3712 3893 if (_curchar == ')') break;
3713 3894 ident2 = get_ident();
3714 3895 skipws();
3715 3896 if (ident2 == NULL) {
3716 3897 parse_err(SYNERR, "identifier expected at %c\n", _curchar);
3717 3898 continue;
3718 3899 } // Check that you have a valid operand
3719 3900 const Form *form2 = instr->_localNames[ident2];
3720 3901 if (!form2) {
3721 3902 parse_err(SYNERR, "operand name expected at %s\n", ident2);
3722 3903 continue;
3723 3904 }
3724 3905 oper = form2->is_operand();
3725 3906 if (oper == NULL && !form2->is_opclass()) {
3726 3907 parse_err(SYNERR, "operand name expected at %s\n", ident2);
3727 3908 continue;
3728 3909 } // Add operand to list
3729 3910 instr_and_operands->add_entry(ident2);
3730 3911 } while(_curchar == ',');
3731 3912 if (_curchar != ')') {
3732 3913 parse_err(SYNERR, "missing ')'in expand instruction declaration\n");
3733 3914 continue;
3734 3915 }
3735 3916 next_char();
3736 3917 if (_curchar != ';') {
3737 3918 parse_err(SYNERR, "missing ';'in expand instruction declaration\n");
3738 3919 continue;
3739 3920 }
3740 3921 next_char();
3741 3922
3742 3923 // Record both instruction name and its operand list
3743 3924 exp->add_instruction(instr_and_operands);
3744 3925
3745 3926 skipws();
3746 3927 }
3747 3928
3748 3929 } while(_curchar != '%');
3749 3930 next_char();
3750 3931 if (_curchar != '}') {
3751 3932 parse_err(SYNERR, "missing '%}' in expand rule definition\n");
3752 3933 return(NULL);
3753 3934 }
3754 3935 next_char();
3755 3936
3756 3937 // Debug Stuff
3757 3938 if (_AD._adl_debug > 1) fprintf(stderr,"Expand Rule:\n");
3758 3939
3759 3940 skipws();
3760 3941 return (exp);
3761 3942 }
3762 3943
3763 3944 //------------------------------rewrite_parse----------------------------------
3764 3945 RewriteRule* ADLParser::rewrite_parse(void) {
3765 3946 char* params = NULL;
3766 3947 char* desc = NULL;
3767 3948
3768 3949
3769 3950 // This feature targeted for second generation description language.
3770 3951
3771 3952 skipws(); // Skip whitespace
3772 3953 // Get parameters for rewrite
3773 3954 if ((params = get_paren_expr("rewrite parameters")) == NULL) {
3774 3955 parse_err(SYNERR, "missing '(' in rewrite rule\n");
3775 3956 return NULL;
3776 3957 }
3777 3958 // Debug Stuff
3778 3959 if (_AD._adl_debug > 1) fprintf(stderr,"Rewrite parameters: %s\n", params);
3779 3960
3780 3961 // For now, grab entire block;
3781 3962 skipws();
3782 3963 if ( (desc = find_cpp_block("rewrite block")) == NULL ) {
3783 3964 parse_err(SYNERR, "incorrect or missing block for 'rewrite'.\n");
3784 3965 return NULL;
3785 3966 }
3786 3967 // Debug Stuff
3787 3968 if (_AD._adl_debug > 1) fprintf(stderr,"Rewrite Rule: %s\n", desc);
3788 3969
3789 3970 skipws();
3790 3971 return (new RewriteRule(params,desc));
3791 3972 }
3792 3973
3793 3974 //------------------------------attr_parse-------------------------------------
3794 3975 Attribute *ADLParser::attr_parse(char* ident) {
3795 3976 Attribute *attrib; // Attribute class
3796 3977 char *cost = NULL; // String representation of cost attribute
3797 3978
3798 3979 skipws(); // Skip leading whitespace
3799 3980 if ( (cost = get_paren_expr("attribute")) == NULL ) {
3800 3981 parse_err(SYNERR, "incorrect or missing expression for 'attribute'\n");
3801 3982 return NULL;
3802 3983 }
3803 3984 // Debug Stuff
3804 3985 if (_AD._adl_debug > 1) fprintf(stderr,"Attribute: %s\n", cost);
3805 3986 if (_curchar != ';') {
3806 3987 parse_err(SYNERR, "missing ';' in attribute definition\n");
3807 3988 return NULL;
3808 3989 }
3809 3990 next_char(); // Point after the terminator
3810 3991
3811 3992 skipws();
3812 3993 attrib = new Attribute(ident,cost,INS_ATTR); // Build new predicate object
3813 3994 return attrib;
3814 3995 }
3815 3996
3816 3997
3817 3998 //------------------------------matchNode_parse--------------------------------
3818 3999 MatchNode *ADLParser::matchNode_parse(FormDict &operands, int &depth, int &numleaves, bool atroot) {
3819 4000 // Count depth of parenthesis nesting for both left and right children
3820 4001 int lParens = depth;
3821 4002 int rParens = depth;
3822 4003
3823 4004 // MatchNode objects for left, right, and root of subtree.
3824 4005 MatchNode *lChild = NULL;
3825 4006 MatchNode *rChild = NULL;
3826 4007 char *token; // Identifier which may be opcode or operand
3827 4008
3828 4009 // Match expression starts with a '('
3829 4010 if (cur_char() != '(')
3830 4011 return NULL;
3831 4012
3832 4013 next_char(); // advance past '('
3833 4014
3834 4015 // Parse the opcode
3835 4016 token = get_ident(); // Get identifier, opcode
3836 4017 if (token == NULL) {
3837 4018 parse_err(SYNERR, "missing opcode in match expression\n");
3838 4019 return NULL;
3839 4020 }
3840 4021
3841 4022 // Take note if we see one of a few special operations - those that are
3842 4023 // treated differently on different architectures in the sense that on
3843 4024 // one architecture there is a match rule and on another there isn't (so
3844 4025 // a call will eventually be generated).
3845 4026
3846 4027 for (int i = _last_machine_leaf + 1; i < _last_opcode; i++) {
3847 4028 if (strcmp(token, NodeClassNames[i]) == 0) {
3848 4029 _AD.has_match_rule(i, true);
3849 4030 }
3850 4031 }
3851 4032
3852 4033 // Lookup the root value in the operands dict to perform substitution
3853 4034 const char *result = NULL; // Result type will be filled in later
3854 4035 const char *name = token; // local name associated with this node
3855 4036 const char *operation = token; // remember valid operation for later
3856 4037 const Form *form = operands[token];
3857 4038 OpClassForm *opcForm = form ? form->is_opclass() : NULL;
3858 4039 if (opcForm != NULL) {
3859 4040 // If this token is an entry in the local names table, record its type
3860 4041 if (!opcForm->ideal_only()) {
3861 4042 operation = opcForm->_ident;
3862 4043 result = operation; // Operands result in their own type
3863 4044 }
3864 4045 // Otherwise it is an ideal type, and so, has no local name
3865 4046 else name = NULL;
3866 4047 }
3867 4048
3868 4049 // Parse the operands
3869 4050 skipws();
3870 4051 if (cur_char() != ')') {
3871 4052
3872 4053 // Parse the left child
3873 4054 if (strcmp(operation,"Set"))
3874 4055 lChild = matchChild_parse(operands, lParens, numleaves, false);
3875 4056 else
3876 4057 lChild = matchChild_parse(operands, lParens, numleaves, true);
3877 4058
3878 4059 skipws();
3879 4060 if (cur_char() != ')' ) {
3880 4061 if(strcmp(operation, "Set"))
3881 4062 rChild = matchChild_parse(operands,rParens,numleaves,false);
3882 4063 else
3883 4064 rChild = matchChild_parse(operands,rParens,numleaves,true);
3884 4065 }
3885 4066 }
3886 4067
3887 4068 // Check for required ')'
3888 4069 skipws();
3889 4070 if (cur_char() != ')') {
3890 4071 parse_err(SYNERR, "missing ')' in match expression\n");
3891 4072 return NULL;
3892 4073 }
3893 4074 next_char(); // skip the ')'
3894 4075
3895 4076 MatchNode* mroot = new MatchNode(_AD,result,name,operation,lChild,rChild);
3896 4077
3897 4078 // If not the root, reduce this subtree to an internal operand
3898 4079 if (!atroot) {
3899 4080 mroot->build_internalop();
3900 4081 }
3901 4082 // depth is greater of left and right paths.
3902 4083 depth = (lParens > rParens) ? lParens : rParens;
3903 4084
3904 4085 return mroot;
3905 4086 }
3906 4087
3907 4088
3908 4089 //------------------------------matchChild_parse-------------------------------
3909 4090 MatchNode *ADLParser::matchChild_parse(FormDict &operands, int &parens, int &numleaves, bool atroot) {
3910 4091 MatchNode *child = NULL;
3911 4092 const char *result = NULL;
3912 4093 const char *token = NULL;
3913 4094 const char *opType = NULL;
3914 4095
3915 4096 if (cur_char() == '(') { // child is an operation
3916 4097 ++parens;
3917 4098 child = matchNode_parse(operands, parens, numleaves, atroot);
3918 4099 }
3919 4100 else { // child is an operand
3920 4101 token = get_ident();
3921 4102 const Form *form = operands[token];
3922 4103 OpClassForm *opcForm = form ? form->is_opclass() : NULL;
3923 4104 if (opcForm != NULL) {
3924 4105 opType = opcForm->_ident;
3925 4106 result = opcForm->_ident; // an operand's result matches its type
3926 4107 } else {
3927 4108 parse_err(SYNERR, "undefined operand %s in match rule\n", token);
3928 4109 return NULL;
3929 4110 }
3930 4111
3931 4112 if (opType == NULL) {
3932 4113 parse_err(SYNERR, "missing type for argument '%s'\n", token);
3933 4114 }
3934 4115
3935 4116 child = new MatchNode(_AD, result, token, opType);
3936 4117 ++numleaves;
3937 4118 }
3938 4119
3939 4120 return child;
3940 4121 }
3941 4122
3942 4123
3943 4124
3944 4125 // ******************** Private Utility Functions *************************
3945 4126
3946 4127
3947 4128 char* ADLParser::find_cpp_block(const char* description) {
3948 4129 char *next; // Pointer for finding block delimiters
3949 4130 char* cppBlock = NULL; // Beginning of C++ code block
3950 4131
3951 4132 if (_curchar == '%') { // Encoding is a C++ expression
3952 4133 next_char();
3953 4134 if (_curchar != '{') {
3954 4135 parse_err(SYNERR, "missing '{' in %s \n", description);
3955 4136 return NULL;
3956 4137 }
3957 4138 next_char(); // Skip block delimiter
3958 4139 skipws_no_preproc(); // Skip leading whitespace
3959 4140 cppBlock = _ptr; // Point to start of expression
3960 4141 int line = linenum();
3961 4142 next = _ptr + 1;
3962 4143 while(((_curchar != '%') || (*next != '}')) && (_curchar != '\0')) {
3963 4144 next_char_or_line();
3964 4145 next = _ptr+1; // Maintain the next pointer
3965 4146 } // Grab string
3966 4147 if (_curchar == '\0') {
3967 4148 parse_err(SYNERR, "invalid termination of %s \n", description);
3968 4149 return NULL;
3969 4150 }
3970 4151 *_ptr = '\0'; // Terminate string
3971 4152 _ptr += 2; // Skip block delimiter
3972 4153 _curchar = *_ptr; // Maintain invariant
3973 4154
3974 4155 // Prepend location descriptor, for debugging.
3975 4156 if (_AD._adlocation_debug) {
3976 4157 char* location = get_line_string(line);
3977 4158 char* end_loc = end_line_marker();
3978 4159 char* result = (char *)malloc(strlen(location) + strlen(cppBlock) + strlen(end_loc) + 1);
3979 4160 strcpy(result, location);
3980 4161 strcat(result, cppBlock);
3981 4162 strcat(result, end_loc);
3982 4163 cppBlock = result;
3983 4164 free(location);
3984 4165 }
3985 4166 }
3986 4167
3987 4168 return cppBlock;
3988 4169 }
3989 4170
3990 4171 // Move to the closing token of the expression we are currently at,
3991 4172 // as defined by stop_chars. Match parens and quotes.
3992 4173 char* ADLParser::get_expr(const char *desc, const char *stop_chars) {
3993 4174 char* expr = NULL;
3994 4175 int paren = 0;
3995 4176
3996 4177 expr = _ptr;
3997 4178 while (paren > 0 || !strchr(stop_chars, _curchar)) {
3998 4179 if (_curchar == '(') { // Down level of nesting
3999 4180 paren++; // Bump the parenthesis counter
4000 4181 next_char(); // maintain the invariant
4001 4182 }
4002 4183 else if (_curchar == ')') { // Up one level of nesting
4003 4184 if (paren == 0) {
4004 4185 // Paren underflow: We didn't encounter the required stop-char.
4005 4186 parse_err(SYNERR, "too many )'s, did not find %s after %s\n",
4006 4187 stop_chars, desc);
4007 4188 return NULL;
4008 4189 }
4009 4190 paren--; // Drop the parenthesis counter
4010 4191 next_char(); // Maintain the invariant
4011 4192 }
4012 4193 else if (_curchar == '"' || _curchar == '\'') {
4013 4194 int qchar = _curchar;
4014 4195 while (true) {
4015 4196 next_char();
4016 4197 if (_curchar == qchar) { next_char(); break; }
4017 4198 if (_curchar == '\\') next_char(); // superquote
4018 4199 if (_curchar == '\n' || _curchar == '\0') {
4019 4200 parse_err(SYNERR, "newline in string in %s\n", desc);
4020 4201 return NULL;
4021 4202 }
4022 4203 }
4023 4204 }
4024 4205 else if (_curchar == '%' && (_ptr[1] == '{' || _ptr[1] == '}')) {
4025 4206 // Make sure we do not stray into the next ADLC-level form.
4026 4207 parse_err(SYNERR, "unexpected %%%c in %s\n", _ptr[1], desc);
4027 4208 return NULL;
4028 4209 }
4029 4210 else if (_curchar == '\0') {
4030 4211 parse_err(SYNERR, "unexpected EOF in %s\n", desc);
4031 4212 return NULL;
4032 4213 }
4033 4214 else {
4034 4215 // Always walk over whitespace, comments, preprocessor directives, etc.
4035 4216 char* pre_skip_ptr = _ptr;
4036 4217 skipws();
4037 4218 // If the parser declined to make progress on whitespace,
4038 4219 // skip the next character, which is therefore NOT whitespace.
4039 4220 if (pre_skip_ptr == _ptr) {
4040 4221 next_char();
4041 4222 } else if (pre_skip_ptr+strlen(pre_skip_ptr) != _ptr+strlen(_ptr)) {
4042 4223 parse_err(SYNERR, "unimplemented: preprocessor must not elide subexpression in %s", desc);
4043 4224 }
4044 4225 }
4045 4226 }
4046 4227
4047 4228 assert(strchr(stop_chars, _curchar), "non-null return must be at stop-char");
4048 4229 *_ptr = '\0'; // Replace ')' or other stop-char with '\0'
4049 4230 return expr;
4050 4231 }
4051 4232
4052 4233 // Helper function around get_expr
4053 4234 // Sets _curchar to '(' so that get_paren_expr will search for a matching ')'
4054 4235 char *ADLParser::get_paren_expr(const char *description, bool include_location) {
4055 4236 int line = linenum();
4056 4237 if (_curchar != '(') // Escape if not valid starting position
4057 4238 return NULL;
4058 4239 next_char(); // Skip the required initial paren.
4059 4240 char *token2 = get_expr(description, ")");
4060 4241 if (_curchar == ')')
4061 4242 next_char(); // Skip required final paren.
4062 4243 int junk = 0;
4063 4244 if (include_location && _AD._adlocation_debug && !is_int_token(token2, junk)) {
4064 4245 // Prepend location descriptor, for debugging.
4065 4246 char* location = get_line_string(line);
4066 4247 char* end_loc = end_line_marker();
4067 4248 char* result = (char *)malloc(strlen(location) + strlen(token2) + strlen(end_loc) + 1);
4068 4249 strcpy(result, location);
4069 4250 strcat(result, token2);
4070 4251 strcat(result, end_loc);
4071 4252 token2 = result;
4072 4253 free(location);
4073 4254 }
4074 4255 return token2;
4075 4256 }
4076 4257
4077 4258 //------------------------------get_ident_common-------------------------------
4078 4259 // Looks for an identifier in the buffer, and turns it into a null terminated
4079 4260 // string(still inside the file buffer). Returns a pointer to the string or
4080 4261 // NULL if some other token is found instead.
4081 4262 char *ADLParser::get_ident_common(bool do_preproc) {
4082 4263 register char c;
4083 4264 char *start; // Pointer to start of token
4084 4265 char *end; // Pointer to end of token
4085 4266
4086 4267 if( _curline == NULL ) // Return NULL at EOF.
4087 4268 return NULL;
4088 4269
4089 4270 skipws_common(do_preproc); // Skip whitespace before identifier
4090 4271 start = end = _ptr; // Start points at first character
4091 4272 end--; // unwind end by one to prepare for loop
4092 4273 do {
4093 4274 end++; // Increment end pointer
4094 4275 c = *end; // Grab character to test
4095 4276 } while ( ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'))
4096 4277 || ((c >= '0') && (c <= '9'))
4097 4278 || ((c == '_')) || ((c == ':')) || ((c == '#')) );
4098 4279 if (start == end) { // We popped out on the first try
4099 4280 parse_err(SYNERR, "identifier expected at %c\n", c);
4100 4281 start = NULL;
4101 4282 }
4102 4283 else {
4103 4284 _curchar = c; // Save the first character of next token
4104 4285 *end = '\0'; // NULL terminate the string in place
4105 4286 }
4106 4287 _ptr = end; // Reset _ptr to point to next char after token
4107 4288
4108 4289 // Make sure we do not try to use #defined identifiers. If start is
4109 4290 // NULL an error was already reported.
4110 4291 if (do_preproc && start != NULL) {
4111 4292 const char* def = _AD.get_preproc_def(start);
4112 4293 if (def != NULL && strcmp(def, start)) {
4113 4294 const char* def1 = def;
4114 4295 const char* def2 = _AD.get_preproc_def(def1);
4115 4296 // implement up to 2 levels of #define
4116 4297 if (def2 != NULL && strcmp(def2, def1)) {
4117 4298 def = def2;
4118 4299 const char* def3 = _AD.get_preproc_def(def2);
4119 4300 if (def3 != NULL && strcmp(def3, def2) && strcmp(def3, def1)) {
4120 4301 parse_err(SYNERR, "unimplemented: using %s defined as %s => %s => %s",
4121 4302 start, def1, def2, def3);
4122 4303 }
4123 4304 }
4124 4305 start = strdup(def);
4125 4306 }
4126 4307 }
4127 4308
4128 4309 return start; // Pointer to token in filebuf
4129 4310 }
4130 4311
4131 4312 //------------------------------get_ident_dup----------------------------------
4132 4313 // Looks for an identifier in the buffer, and returns a duplicate
4133 4314 // or NULL if some other token is found instead.
4134 4315 char *ADLParser::get_ident_dup(void) {
4135 4316 char *ident = get_ident();
4136 4317
4137 4318 // Duplicate an identifier before returning and restore string.
4138 4319 if( ident != NULL ) {
4139 4320 ident = strdup(ident); // Copy the string
4140 4321 *_ptr = _curchar; // and replace Nil with original character
4141 4322 }
4142 4323
4143 4324 return ident;
4144 4325 }
4145 4326
4146 4327 //----------------------get_ident_or_literal_constant--------------------------
4147 4328 // Looks for an identifier in the buffer, or a parenthesized expression.
4148 4329 char *ADLParser::get_ident_or_literal_constant(const char* description) {
4149 4330 char* param = NULL;
4150 4331 skipws();
4151 4332 if (_curchar == '(') {
4152 4333 // Grab a constant expression.
4153 4334 param = get_paren_expr(description);
4154 4335 if (param[0] != '(') {
4155 4336 char* buf = (char*) malloc(strlen(param) + 3);
4156 4337 sprintf(buf, "(%s)", param);
4157 4338 param = buf;
4158 4339 }
4159 4340 assert(is_literal_constant(param),
4160 4341 "expr must be recognizable as a constant");
4161 4342 } else {
4162 4343 param = get_ident();
4163 4344 }
4164 4345 return param;
4165 4346 }
4166 4347
4167 4348 //------------------------------get_rep_var_ident-----------------------------
4168 4349 // Do NOT duplicate,
4169 4350 // Leave nil terminator in buffer
4170 4351 // Preserve initial '$'(s) in string
4171 4352 char *ADLParser::get_rep_var_ident(void) {
4172 4353 // Remember starting point
4173 4354 char *rep_var = _ptr;
4174 4355
4175 4356 // Check for replacement variable indicator '$' and pass if present
4176 4357 if ( _curchar == '$' ) {
4177 4358 next_char();
4178 4359 }
4179 4360 // Check for a subfield indicator, a second '$', and pass if present
4180 4361 if ( _curchar == '$' ) {
4181 4362 next_char();
4182 4363 }
4183 4364
4184 4365 // Check for a control indicator, a third '$':
4185 4366 if ( _curchar == '$' ) {
4186 4367 next_char();
4187 4368 }
4188 4369
4189 4370 // Check for more than three '$'s in sequence, SYNERR
4190 4371 if( _curchar == '$' ) {
4191 4372 parse_err(SYNERR, "Replacement variables and field specifiers can not start with '$$$$'");
4192 4373 next_char();
4193 4374 return NULL;
4194 4375 }
4195 4376
4196 4377 // Nil terminate the variable name following the '$'
4197 4378 char *rep_var_name = get_ident();
4198 4379 assert( rep_var_name != NULL,
4199 4380 "Missing identifier after replacement variable indicator '$'");
4200 4381
4201 4382 return rep_var;
4202 4383 }
4203 4384
4204 4385
4205 4386
4206 4387 //------------------------------get_rep_var_ident_dup-------------------------
4207 4388 // Return the next replacement variable identifier, skipping first '$'
4208 4389 // given a pointer into a line of the buffer.
4209 4390 // Null terminates string, still inside the file buffer,
4210 4391 // Returns a pointer to a copy of the string, or NULL on failure
4211 4392 char *ADLParser::get_rep_var_ident_dup(void) {
4212 4393 if( _curchar != '$' ) return NULL;
4213 4394
4214 4395 next_char(); // Move past the '$'
4215 4396 char *rep_var = _ptr; // Remember starting point
4216 4397
4217 4398 // Check for a subfield indicator, a second '$':
4218 4399 if ( _curchar == '$' ) {
4219 4400 next_char();
4220 4401 }
4221 4402
4222 4403 // Check for a control indicator, a third '$':
4223 4404 if ( _curchar == '$' ) {
4224 4405 next_char();
4225 4406 }
4226 4407
4227 4408 // Check for more than three '$'s in sequence, SYNERR
4228 4409 if( _curchar == '$' ) {
4229 4410 parse_err(SYNERR, "Replacement variables and field specifiers can not start with '$$$$'");
4230 4411 next_char();
4231 4412 return NULL;
4232 4413 }
4233 4414
4234 4415 // Nil terminate the variable name following the '$'
4235 4416 char *rep_var_name = get_ident();
4236 4417 assert( rep_var_name != NULL,
4237 4418 "Missing identifier after replacement variable indicator '$'");
4238 4419 rep_var = strdup(rep_var); // Copy the string
4239 4420 *_ptr = _curchar; // and replace Nil with original character
4240 4421
4241 4422 return rep_var;
4242 4423 }
4243 4424
4244 4425
4245 4426 //------------------------------get_unique_ident------------------------------
4246 4427 // Looks for an identifier in the buffer, terminates it with a NULL,
4247 4428 // and checks that it is unique
4248 4429 char *ADLParser::get_unique_ident(FormDict& dict, const char* nameDescription){
4249 4430 char* ident = get_ident();
4250 4431
4251 4432 if (ident == NULL) {
4252 4433 parse_err(SYNERR, "missing %s identifier at %c\n", nameDescription, _curchar);
4253 4434 }
4254 4435 else {
4255 4436 if (dict[ident] != NULL) {
4256 4437 parse_err(SYNERR, "duplicate name %s for %s\n", ident, nameDescription);
4257 4438 ident = NULL;
4258 4439 }
4259 4440 }
4260 4441
4261 4442 return ident;
4262 4443 }
4263 4444
4264 4445
4265 4446 //------------------------------get_int----------------------------------------
4266 4447 // Looks for a character string integer in the buffer, and turns it into an int
4267 4448 // invokes a parse_err if the next token is not an integer.
4268 4449 // This routine does not leave the integer null-terminated.
4269 4450 int ADLParser::get_int(void) {
4270 4451 register char c;
4271 4452 char *start; // Pointer to start of token
4272 4453 char *end; // Pointer to end of token
4273 4454 int result; // Storage for integer result
4274 4455
4275 4456 if( _curline == NULL ) // Return NULL at EOF.
4276 4457 return 0;
4277 4458
4278 4459 skipws(); // Skip whitespace before identifier
4279 4460 start = end = _ptr; // Start points at first character
4280 4461 c = *end; // Grab character to test
4281 4462 while ((c >= '0') && (c <= '9')
4282 4463 || ((c == '-') && (end == start))) {
4283 4464 end++; // Increment end pointer
4284 4465 c = *end; // Grab character to test
4285 4466 }
4286 4467 if (start == end) { // We popped out on the first try
4287 4468 parse_err(SYNERR, "integer expected at %c\n", c);
4288 4469 result = 0;
4289 4470 }
4290 4471 else {
4291 4472 _curchar = c; // Save the first character of next token
4292 4473 *end = '\0'; // NULL terminate the string in place
4293 4474 result = atoi(start); // Convert the string to an integer
4294 4475 *end = _curchar; // Restore buffer to original condition
4295 4476 }
4296 4477
4297 4478 // Reset _ptr to next char after token
4298 4479 _ptr = end;
4299 4480
4300 4481 return result; // integer
4301 4482 }
4302 4483
4303 4484
4304 4485 //------------------------------get_relation_dup------------------------------
4305 4486 // Looks for a relational operator in the buffer
4306 4487 // invokes a parse_err if the next token is not a relation
4307 4488 // This routine creates a duplicate of the string in the buffer.
4308 4489 char *ADLParser::get_relation_dup(void) {
4309 4490 char *result = NULL; // relational operator being returned
4310 4491
4311 4492 if( _curline == NULL ) // Return NULL at EOF.
4312 4493 return NULL;
4313 4494
4314 4495 skipws(); // Skip whitespace before relation
4315 4496 char *start = _ptr; // Store start of relational operator
4316 4497 char first = *_ptr; // the first character
4317 4498 if( (first == '=') || (first == '!') || (first == '<') || (first == '>') ) {
4318 4499 next_char();
4319 4500 char second = *_ptr; // the second character
4320 4501 if( (second == '=') ) {
4321 4502 next_char();
4322 4503 char tmp = *_ptr;
4323 4504 *_ptr = '\0'; // NULL terminate
4324 4505 result = strdup(start); // Duplicate the string
4325 4506 *_ptr = tmp; // restore buffer
4326 4507 } else {
4327 4508 parse_err(SYNERR, "relational operator expected at %s\n", _ptr);
4328 4509 }
4329 4510 } else {
4330 4511 parse_err(SYNERR, "relational operator expected at %s\n", _ptr);
4331 4512 }
4332 4513
4333 4514 return result;
4334 4515 }
4335 4516
4336 4517
4337 4518
4338 4519 //------------------------------get_oplist-------------------------------------
4339 4520 // Looks for identifier pairs where first must be the name of an operand, and
4340 4521 // second must be a name unique in the scope of this instruction. Stores the
4341 4522 // names with a pointer to the OpClassForm of their type in a local name table.
4342 4523 void ADLParser::get_oplist(NameList ¶meters, FormDict &operands) {
4343 4524 OpClassForm *opclass = NULL;
4344 4525 char *ident = NULL;
4345 4526
4346 4527 do {
4347 4528 next_char(); // skip open paren & comma characters
4348 4529 skipws();
4349 4530 if (_curchar == ')') break;
4350 4531
4351 4532 // Get operand type, and check it against global name table
4352 4533 ident = get_ident();
4353 4534 if (ident == NULL) {
4354 4535 parse_err(SYNERR, "optype identifier expected at %c\n", _curchar);
4355 4536 return;
4356 4537 }
4357 4538 else {
4358 4539 const Form *form = _globalNames[ident];
4359 4540 if( form == NULL ) {
4360 4541 parse_err(SYNERR, "undefined operand type %s\n", ident);
4361 4542 return;
4362 4543 }
4363 4544
4364 4545 // Check for valid operand type
4365 4546 OpClassForm *opc = form->is_opclass();
4366 4547 OperandForm *oper = form->is_operand();
4367 4548 if((oper == NULL) && (opc == NULL)) {
4368 4549 parse_err(SYNERR, "identifier %s not operand type\n", ident);
4369 4550 return;
4370 4551 }
4371 4552 opclass = opc;
4372 4553 }
4373 4554 // Debugging Stuff
4374 4555 if (_AD._adl_debug > 1) fprintf(stderr, "\tOperand Type: %s\t", ident);
4375 4556
4376 4557 // Get name of operand and add it to local name table
4377 4558 if( (ident = get_unique_ident(operands, "operand")) == NULL) {
4378 4559 return;
4379 4560 }
4380 4561 // Parameter names must not be global names.
4381 4562 if( _globalNames[ident] != NULL ) {
4382 4563 parse_err(SYNERR, "Reuse of global name %s as operand.\n",ident);
4383 4564 return;
4384 4565 }
4385 4566 operands.Insert(ident, opclass);
4386 4567 parameters.addName(ident);
4387 4568
4388 4569 // Debugging Stuff
4389 4570 if (_AD._adl_debug > 1) fprintf(stderr, "\tOperand Name: %s\n", ident);
4390 4571 skipws();
4391 4572 } while(_curchar == ',');
4392 4573
4393 4574 if (_curchar != ')') parse_err(SYNERR, "missing ')'\n");
4394 4575 else {
4395 4576 next_char(); // set current character position past the close paren
4396 4577 }
4397 4578 }
4398 4579
4399 4580
4400 4581 //------------------------------get_effectlist---------------------------------
4401 4582 // Looks for identifier pairs where first must be the name of a pre-defined,
4402 4583 // effect, and the second must be the name of an operand defined in the
4403 4584 // operand list of this instruction. Stores the names with a pointer to the
4404 4585 // effect form in a local effects table.
4405 4586 void ADLParser::get_effectlist(FormDict &effects, FormDict &operands) {
4406 4587 OperandForm *opForm;
4407 4588 Effect *eForm;
4408 4589 char *ident;
4409 4590
4410 4591 do {
4411 4592 next_char(); // skip open paren & comma characters
4412 4593 skipws();
4413 4594 if (_curchar == ')') break;
4414 4595
4415 4596 // Get effect type, and check it against global name table
4416 4597 ident = get_ident();
4417 4598 if (ident == NULL) {
4418 4599 parse_err(SYNERR, "effect type identifier expected at %c\n", _curchar);
4419 4600 return;
4420 4601 }
4421 4602 else {
4422 4603 // Check for valid effect type
4423 4604 const Form *form = _globalNames[ident];
4424 4605 if( form == NULL ) {
4425 4606 parse_err(SYNERR, "undefined effect type %s\n", ident);
4426 4607 return;
4427 4608 }
4428 4609 else {
4429 4610 if( (eForm = form->is_effect()) == NULL) {
4430 4611 parse_err(SYNERR, "identifier %s not effect type\n", ident);
4431 4612 return;
4432 4613 }
4433 4614 }
4434 4615 }
4435 4616 // Debugging Stuff
4436 4617 if (_AD._adl_debug > 1) fprintf(stderr, "\tEffect Type: %s\t", ident);
4437 4618 skipws();
4438 4619 // Get name of operand and check that it is in the local name table
4439 4620 if( (ident = get_unique_ident(effects, "effect")) == NULL) {
4440 4621 parse_err(SYNERR, "missing operand identifier in effect list\n");
4441 4622 return;
4442 4623 }
4443 4624 const Form *form = operands[ident];
4444 4625 opForm = form ? form->is_operand() : NULL;
4445 4626 if( opForm == NULL ) {
4446 4627 if( form && form->is_opclass() ) {
4447 4628 const char* cname = form->is_opclass()->_ident;
4448 4629 parse_err(SYNERR, "operand classes are illegal in effect lists (found %s %s)\n", cname, ident);
4449 4630 } else {
4450 4631 parse_err(SYNERR, "undefined operand %s in effect list\n", ident);
4451 4632 }
4452 4633 return;
4453 4634 }
4454 4635 // Add the pair to the effects table
4455 4636 effects.Insert(ident, eForm);
4456 4637 // Debugging Stuff
4457 4638 if (_AD._adl_debug > 1) fprintf(stderr, "\tOperand Name: %s\n", ident);
4458 4639 skipws();
4459 4640 } while(_curchar == ',');
4460 4641
4461 4642 if (_curchar != ')') parse_err(SYNERR, "missing ')'\n");
4462 4643 else {
4463 4644 next_char(); // set current character position past the close paren
4464 4645 }
4465 4646 }
4466 4647
4467 4648
4468 4649 //-------------------------------preproc_line----------------------------------
4469 4650 // A "#line" keyword has been seen, so parse the rest of the line.
4470 4651 void ADLParser::preproc_line(void) {
4471 4652 int line = get_int();
4472 4653 skipws_no_preproc();
4473 4654 const char* file = NULL;
4474 4655 if (_curchar == '"') {
4475 4656 next_char(); // Move past the initial '"'
4476 4657 file = _ptr;
4477 4658 while (true) {
4478 4659 if (_curchar == '\n') {
4479 4660 parse_err(SYNERR, "missing '\"' at end of #line directive");
4480 4661 return;
4481 4662 }
4482 4663 if (_curchar == '"') {
4483 4664 *_ptr = '\0'; // Terminate the string
4484 4665 next_char();
4485 4666 skipws_no_preproc();
4486 4667 break;
4487 4668 }
4488 4669 next_char();
4489 4670 }
4490 4671 }
4491 4672 ensure_end_of_line();
4492 4673 if (file != NULL)
4493 4674 _AD._ADL_file._name = file;
4494 4675 _buf.set_linenum(line);
4495 4676 }
4496 4677
4497 4678 //------------------------------preproc_define---------------------------------
4498 4679 // A "#define" keyword has been seen, so parse the rest of the line.
4499 4680 void ADLParser::preproc_define(void) {
4500 4681 char* flag = get_ident_no_preproc();
4501 4682 skipws_no_preproc();
4502 4683 // only #define x y is supported for now
4503 4684 char* def = get_ident_no_preproc();
4504 4685 _AD.set_preproc_def(flag, def);
4505 4686 skipws_no_preproc();
4506 4687 if (_curchar != '\n') {
4507 4688 parse_err(SYNERR, "non-identifier in preprocessor definition\n");
4508 4689 }
4509 4690 }
4510 4691
4511 4692 //------------------------------preproc_undef----------------------------------
4512 4693 // An "#undef" keyword has been seen, so parse the rest of the line.
4513 4694 void ADLParser::preproc_undef(void) {
4514 4695 char* flag = get_ident_no_preproc();
4515 4696 skipws_no_preproc();
4516 4697 ensure_end_of_line();
4517 4698 _AD.set_preproc_def(flag, NULL);
4518 4699 }
4519 4700
4520 4701
4521 4702
4522 4703 //------------------------------parse_err--------------------------------------
4523 4704 // Issue a parser error message, and skip to the end of the current line
4524 4705 void ADLParser::parse_err(int flag, const char *fmt, ...) {
4525 4706 va_list args;
4526 4707
4527 4708 va_start(args, fmt);
4528 4709 if (flag == 1)
4529 4710 _AD._syntax_errs += _AD.emit_msg(0, flag, linenum(), fmt, args);
4530 4711 else if (flag == 2)
4531 4712 _AD._semantic_errs += _AD.emit_msg(0, flag, linenum(), fmt, args);
4532 4713 else
4533 4714 _AD._warnings += _AD.emit_msg(0, flag, linenum(), fmt, args);
4534 4715
4535 4716 int error_char = _curchar;
4536 4717 char* error_ptr = _ptr+1;
4537 4718 for(;*_ptr != '\n'; _ptr++) ; // Skip to the end of the current line
4538 4719 _curchar = '\n';
4539 4720 va_end(args);
4540 4721 _AD._no_output = 1;
4541 4722
4542 4723 if (flag == 1) {
4543 4724 char* error_tail = strchr(error_ptr, '\n');
4544 4725 char tem = *error_ptr;
4545 4726 error_ptr[-1] = '\0';
4546 4727 char* error_head = error_ptr-1;
4547 4728 while (error_head > _curline && *error_head) --error_head;
4548 4729 if (error_tail) *error_tail = '\0';
4549 4730 fprintf(stderr, "Error Context: %s>>>%c<<<%s\n",
4550 4731 error_head, error_char, error_ptr);
4551 4732 if (error_tail) *error_tail = '\n';
4552 4733 error_ptr[-1] = tem;
4553 4734 }
4554 4735 }
4555 4736
4556 4737 //---------------------------ensure_start_of_line------------------------------
4557 4738 // A preprocessor directive has been encountered. Be sure it has fallen at
4558 4739 // the beginning of a line, or else report an error.
4559 4740 void ADLParser::ensure_start_of_line(void) {
4560 4741 if (_curchar == '\n') { next_line(); return; }
4561 4742 assert( _ptr >= _curline && _ptr < _curline+strlen(_curline),
4562 4743 "Must be able to find which line we are in" );
4563 4744
4564 4745 for (char *s = _curline; s < _ptr; s++) {
4565 4746 if (*s > ' ') {
4566 4747 parse_err(SYNERR, "'%c' must be at beginning of line\n", _curchar);
4567 4748 break;
4568 4749 }
4569 4750 }
4570 4751 }
4571 4752
4572 4753 //---------------------------ensure_end_of_line--------------------------------
4573 4754 // A preprocessor directive has been parsed. Be sure there is no trailing
4574 4755 // garbage at the end of this line. Set the scan point to the beginning of
4575 4756 // the next line.
4576 4757 void ADLParser::ensure_end_of_line(void) {
4577 4758 skipws_no_preproc();
4578 4759 if (_curchar != '\n' && _curchar != '\0') {
4579 4760 parse_err(SYNERR, "garbage char '%c' at end of line\n", _curchar);
4580 4761 } else {
4581 4762 next_char_or_line();
4582 4763 }
4583 4764 }
4584 4765
4585 4766 //---------------------------handle_preproc------------------------------------
4586 4767 // The '#' character introducing a preprocessor directive has been found.
4587 4768 // Parse the whole directive name (e.g., #define, #endif) and take appropriate
4588 4769 // action. If we are in an "untaken" span of text, simply keep track of
4589 4770 // #ifdef nesting structure, so we can find out when to start taking text
4590 4771 // again. (In this state, we "sort of support" C's #if directives, enough
4591 4772 // to disregard their associated #else and #endif lines.) If we are in a
4592 4773 // "taken" span of text, there are two cases: "#define" and "#undef"
4593 4774 // directives are preserved and passed up to the caller, which eventually
4594 4775 // passes control to the top-level parser loop, which handles #define and
4595 4776 // #undef directly. (This prevents these directives from occurring in
4596 4777 // arbitrary positions in the AD file--we require better structure than C.)
4597 4778 // In the other case, and #ifdef, #ifndef, #else, or #endif is silently
4598 4779 // processed as whitespace, with the "taken" state of the text correctly
4599 4780 // updated. This routine returns "false" exactly in the case of a "taken"
4600 4781 // #define or #undef, which tells the caller that a preprocessor token
4601 4782 // has appeared which must be handled explicitly by the parse loop.
4602 4783 bool ADLParser::handle_preproc_token() {
4603 4784 assert(*_ptr == '#', "must be at start of preproc");
4604 4785 ensure_start_of_line();
4605 4786 next_char();
4606 4787 skipws_no_preproc();
4607 4788 char* start_ident = _ptr;
4608 4789 char* ident = (_curchar == '\n') ? NULL : get_ident_no_preproc();
4609 4790 if (ident == NULL) {
4610 4791 parse_err(SYNERR, "expected preprocessor command, got end of line\n");
4611 4792 } else if (!strcmp(ident, "ifdef") ||
4612 4793 !strcmp(ident, "ifndef")) {
4613 4794 char* flag = get_ident_no_preproc();
4614 4795 ensure_end_of_line();
4615 4796 // Test the identifier only if we are already in taken code:
4616 4797 bool flag_def = preproc_taken() && (_AD.get_preproc_def(flag) != NULL);
4617 4798 bool now_taken = !strcmp(ident, "ifdef") ? flag_def : !flag_def;
4618 4799 begin_if_def(now_taken);
4619 4800 } else if (!strcmp(ident, "if")) {
4620 4801 if (preproc_taken())
4621 4802 parse_err(SYNERR, "unimplemented: #%s %s", ident, _ptr+1);
4622 4803 next_line();
4623 4804 // Intelligently skip this nested C preprocessor directive:
4624 4805 begin_if_def(true);
4625 4806 } else if (!strcmp(ident, "else")) {
4626 4807 ensure_end_of_line();
4627 4808 invert_if_def();
4628 4809 } else if (!strcmp(ident, "endif")) {
4629 4810 ensure_end_of_line();
4630 4811 end_if_def();
4631 4812 } else if (preproc_taken()) {
4632 4813 // pass this token up to the main parser as "#define" or "#undef"
4633 4814 _ptr = start_ident;
4634 4815 _curchar = *--_ptr;
4635 4816 if( _curchar != '#' ) {
4636 4817 parse_err(SYNERR, "no space allowed after # in #define or #undef");
4637 4818 assert(_curchar == '#', "no space allowed after # in #define or #undef");
4638 4819 }
4639 4820 return false;
4640 4821 }
4641 4822 return true;
4642 4823 }
4643 4824
4644 4825 //---------------------------skipws_common-------------------------------------
4645 4826 // Skip whitespace, including comments and newlines, while keeping an accurate
4646 4827 // line count.
4647 4828 // Maybe handle certain preprocessor constructs: #ifdef, #ifndef, #else, #endif
4648 4829 void ADLParser::skipws_common(bool do_preproc) {
4649 4830 char *start = _ptr;
4650 4831 char *next = _ptr + 1;
4651 4832
4652 4833 if (*_ptr == '\0') {
4653 4834 // Check for string terminator
4654 4835 if (_curchar > ' ') return;
4655 4836 if (_curchar == '\n') {
4656 4837 if (!do_preproc) return; // let caller handle the newline
4657 4838 next_line();
4658 4839 _ptr = _curline; next = _ptr + 1;
4659 4840 }
4660 4841 else if (_curchar == '#' ||
4661 4842 (_curchar == '/' && (*next == '/' || *next == '*'))) {
4662 4843 parse_err(SYNERR, "unimplemented: comment token in a funny place");
4663 4844 }
4664 4845 }
4665 4846 while(_curline != NULL) { // Check for end of file
4666 4847 if (*_ptr == '\n') { // keep proper track of new lines
4667 4848 if (!do_preproc) break; // let caller handle the newline
4668 4849 next_line();
4669 4850 _ptr = _curline; next = _ptr + 1;
4670 4851 }
4671 4852 else if ((*_ptr == '/') && (*next == '/')) // C++ comment
4672 4853 do { _ptr++; next++; } while(*_ptr != '\n'); // So go to end of line
4673 4854 else if ((*_ptr == '/') && (*next == '*')) { // C comment
4674 4855 _ptr++; next++;
4675 4856 do {
4676 4857 _ptr++; next++;
4677 4858 if (*_ptr == '\n') { // keep proper track of new lines
4678 4859 next_line(); // skip newlines within comments
4679 4860 if (_curline == NULL) { // check for end of file
4680 4861 parse_err(SYNERR, "end-of-file detected inside comment\n");
4681 4862 break;
4682 4863 }
4683 4864 _ptr = _curline; next = _ptr + 1;
4684 4865 }
4685 4866 } while(!((*_ptr == '*') && (*next == '/'))); // Go to end of comment
4686 4867 _ptr = ++next; next++; // increment _ptr past comment end
4687 4868 }
4688 4869 else if (do_preproc && *_ptr == '#') {
4689 4870 // Note that this calls skipws_common(false) recursively!
4690 4871 bool preproc_handled = handle_preproc_token();
4691 4872 if (!preproc_handled) {
4692 4873 if (preproc_taken()) {
4693 4874 return; // short circuit
4694 4875 }
4695 4876 ++_ptr; // skip the preprocessor character
4696 4877 }
4697 4878 next = _ptr+1;
4698 4879 } else if(*_ptr > ' ' && !(do_preproc && !preproc_taken())) {
4699 4880 break;
4700 4881 }
4701 4882 else if (*_ptr == '"' || *_ptr == '\'') {
4702 4883 assert(do_preproc, "only skip strings if doing preproc");
4703 4884 // skip untaken quoted string
4704 4885 int qchar = *_ptr;
4705 4886 while (true) {
4706 4887 ++_ptr;
4707 4888 if (*_ptr == qchar) { ++_ptr; break; }
4708 4889 if (*_ptr == '\\') ++_ptr;
4709 4890 if (*_ptr == '\n' || *_ptr == '\0') {
4710 4891 parse_err(SYNERR, "newline in string");
4711 4892 break;
4712 4893 }
4713 4894 }
4714 4895 next = _ptr + 1;
4715 4896 }
4716 4897 else { ++_ptr; ++next; }
4717 4898 }
4718 4899 if( _curline != NULL ) // at end of file _curchar isn't valid
4719 4900 _curchar = *_ptr; // reset _curchar to maintain invariant
4720 4901 }
4721 4902
4722 4903 //---------------------------cur_char-----------------------------------------
4723 4904 char ADLParser::cur_char() {
4724 4905 return (_curchar);
4725 4906 }
4726 4907
4727 4908 //---------------------------next_char-----------------------------------------
4728 4909 void ADLParser::next_char() {
4729 4910 if (_curchar == '\n') parse_err(WARN, "must call next_line!");
4730 4911 _curchar = *++_ptr;
4731 4912 // if ( _curchar == '\n' ) {
4732 4913 // next_line();
4733 4914 // }
4734 4915 }
4735 4916
4736 4917 //---------------------------next_char_or_line---------------------------------
4737 4918 void ADLParser::next_char_or_line() {
4738 4919 if ( _curchar != '\n' ) {
4739 4920 _curchar = *++_ptr;
4740 4921 } else {
4741 4922 next_line();
4742 4923 _ptr = _curline;
4743 4924 _curchar = *_ptr; // maintain invariant
4744 4925 }
4745 4926 }
4746 4927
4747 4928 //---------------------------next_line-----------------------------------------
4748 4929 void ADLParser::next_line() {
4749 4930 _curline = _buf.get_line();
4750 4931 _curchar = ' ';
4751 4932 }
4752 4933
4753 4934 //------------------------get_line_string--------------------------------------
4754 4935 // Prepended location descriptor, for debugging.
4755 4936 // Must return a malloced string (that can be freed if desired).
4756 4937 char* ADLParser::get_line_string(int linenum) {
4757 4938 const char* file = _AD._ADL_file._name;
4758 4939 int line = linenum ? linenum : this->linenum();
4759 4940 char* location = (char *)malloc(strlen(file) + 100);
4760 4941 sprintf(location, "\n#line %d \"%s\"\n", line, file);
4761 4942 return location;
4762 4943 }
4763 4944
4764 4945 //-------------------------is_literal_constant---------------------------------
4765 4946 bool ADLParser::is_literal_constant(const char *param) {
4766 4947 if (param[0] == 0) return false; // null string
4767 4948 if (param[0] == '(') return true; // parenthesized expression
4768 4949 if (param[0] == '0' && (param[1] == 'x' || param[1] == 'X')) {
4769 4950 // Make sure it's a hex constant.
4770 4951 int i = 2;
4771 4952 do {
4772 4953 if( !ADLParser::is_hex_digit(*(param+i)) ) return false;
4773 4954 ++i;
4774 4955 } while( *(param+i) != 0 );
4775 4956 return true;
4776 4957 }
4777 4958 return false;
4778 4959 }
4779 4960
4780 4961 //---------------------------is_hex_digit--------------------------------------
4781 4962 bool ADLParser::is_hex_digit(char digit) {
4782 4963 return ((digit >= '0') && (digit <= '9'))
4783 4964 ||((digit >= 'a') && (digit <= 'f'))
4784 4965 ||((digit >= 'A') && (digit <= 'F'));
4785 4966 }
4786 4967
4787 4968 //---------------------------is_int_token--------------------------------------
4788 4969 bool ADLParser::is_int_token(const char* token, int& intval) {
4789 4970 const char* cp = token;
4790 4971 while (*cp != '\0' && *cp <= ' ') cp++;
4791 4972 if (*cp == '-') cp++;
4792 4973 int ndigit = 0;
4793 4974 while (*cp >= '0' && *cp <= '9') { cp++; ndigit++; }
4794 4975 while (*cp != '\0' && *cp <= ' ') cp++;
4795 4976 if (ndigit == 0 || *cp != '\0') {
4796 4977 return false;
4797 4978 }
4798 4979 intval = atoi(token);
4799 4980 return true;
4800 4981 }
4801 4982
4802 4983 static const char* skip_expr_ws(const char* str) {
4803 4984 const char * cp = str;
4804 4985 while (cp[0]) {
4805 4986 if (cp[0] <= ' ') {
4806 4987 ++cp;
4807 4988 } else if (cp[0] == '#') {
4808 4989 ++cp;
4809 4990 while (cp[0] == ' ') ++cp;
4810 4991 assert(0 == strncmp(cp, "line", 4), "must be a #line directive");
4811 4992 const char* eol = strchr(cp, '\n');
4812 4993 assert(eol != NULL, "must find end of line");
4813 4994 if (eol == NULL) eol = cp + strlen(cp);
4814 4995 cp = eol;
4815 4996 } else {
4816 4997 break;
4817 4998 }
4818 4999 }
4819 5000 return cp;
4820 5001 }
4821 5002
4822 5003 //-----------------------equivalent_expressions--------------------------------
4823 5004 bool ADLParser::equivalent_expressions(const char* str1, const char* str2) {
4824 5005 if (str1 == str2)
4825 5006 return true;
4826 5007 else if (str1 == NULL || str2 == NULL)
4827 5008 return false;
4828 5009 const char* cp1 = str1;
4829 5010 const char* cp2 = str2;
4830 5011 char in_quote = '\0';
4831 5012 while (cp1[0] && cp2[0]) {
4832 5013 if (!in_quote) {
4833 5014 // skip spaces and/or cpp directives
4834 5015 const char* cp1a = skip_expr_ws(cp1);
4835 5016 const char* cp2a = skip_expr_ws(cp2);
4836 5017 if (cp1a > cp1 && cp2a > cp2) {
4837 5018 cp1 = cp1a; cp2 = cp2a;
4838 5019 continue;
4839 5020 }
4840 5021 if (cp1a > cp1 || cp2a > cp2) break; // fail
4841 5022 }
4842 5023 // match one non-space char
4843 5024 if (cp1[0] != cp2[0]) break; // fail
4844 5025 char ch = cp1[0];
4845 5026 cp1++; cp2++;
4846 5027 // watch for quotes
4847 5028 if (in_quote && ch == '\\') {
4848 5029 if (cp1[0] != cp2[0]) break; // fail
4849 5030 if (!cp1[0]) break;
4850 5031 cp1++; cp2++;
4851 5032 }
4852 5033 if (in_quote && ch == in_quote) {
4853 5034 in_quote = '\0';
4854 5035 } else if (!in_quote && (ch == '"' || ch == '\'')) {
4855 5036 in_quote = ch;
4856 5037 }
4857 5038 }
4858 5039 return (!cp1[0] && !cp2[0]);
4859 5040 }
4860 5041
4861 5042
4862 5043 //-------------------------------trim------------------------------------------
4863 5044 void ADLParser::trim(char* &token) {
4864 5045 while (*token <= ' ') token++;
4865 5046 char* end = token + strlen(token);
4866 5047 while (end > token && *(end-1) <= ' ') --end;
4867 5048 *end = '\0';
4868 5049 }
↓ open down ↓ |
1884 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX