jdk

Форк
0
/
adlparse.cpp 
5429 строк · 179.1 Кб
1
/*
2
 * Copyright (c) 1997, 2024, Oracle and/or its affiliates. All rights reserved.
3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
 *
5
 * This code is free software; you can redistribute it and/or modify it
6
 * under the terms of the GNU General Public License version 2 only, as
7
 * published by the Free Software Foundation.
8
 *
9
 * This code is distributed in the hope that it will be useful, but WITHOUT
10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12
 * version 2 for more details (a copy is included in the LICENSE file that
13
 * accompanied this code).
14
 *
15
 * You should have received a copy of the GNU General Public License version
16
 * 2 along with this work; if not, write to the Free Software Foundation,
17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
 *
19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
 * or visit www.oracle.com if you need additional information or have any
21
 * questions.
22
 *
23
 */
24

25
// ADLPARSE.CPP - Architecture Description Language Parser
26
// Authors: Chris Vick and Mike Paleczny
27
#include "adlc.hpp"
28

29
//----------------------------ADLParser----------------------------------------
30
// Create a new ADL parser
31
ADLParser::ADLParser(FileBuff& buffer, ArchDesc& archDesc)
32
  : _buf(buffer), _AD(archDesc),
33
    _globalNames(archDesc.globalNames()) {
34
  _AD._syntax_errs = _AD._semantic_errs = 0; // No errors so far this file
35
  _AD._warnings    = 0;                      // No warnings either
36
  _curline         = _ptr = nullptr;            // No pointers into buffer yet
37

38
  _preproc_depth = 0;
39
  _preproc_not_taken = 0;
40

41
  // Delimit command-line definitions from in-file definitions:
42
  _AD._preproc_list.add_signal();
43
}
44

45
//------------------------------~ADLParser-------------------------------------
46
// Delete an ADL parser.
47
ADLParser::~ADLParser() {
48
  if (!_AD._quiet_mode)
49
    fprintf(stderr,"---------------------------- Errors and Warnings ----------------------------\n");
50
#ifndef ASSERT
51
  if (!_AD._quiet_mode) {
52
    fprintf(stderr, "**************************************************************\n");
53
    fprintf(stderr, "***** WARNING: ASSERT is undefined, assertions disabled. *****\n");
54
    fprintf(stderr, "**************************************************************\n");
55
  }
56
#endif
57
  if( _AD._syntax_errs + _AD._semantic_errs + _AD._warnings == 0 ) {
58
    if (!_AD._quiet_mode)
59
      fprintf(stderr,"No errors or warnings to report from phase-1 parse.\n" );
60
  }
61
  else {
62
    if( _AD._syntax_errs ) {      // Any syntax errors?
63
      fprintf(stderr,"%s:  Found %d syntax error", _buf._fp->_name, _AD._syntax_errs);
64
      if( _AD._syntax_errs > 1 ) fprintf(stderr,"s.\n\n");
65
      else fprintf(stderr,".\n\n");
66
    }
67
    if( _AD._semantic_errs ) {    // Any semantic errors?
68
      fprintf(stderr,"%s:  Found %d semantic error", _buf._fp->_name, _AD._semantic_errs);
69
      if( _AD._semantic_errs > 1 ) fprintf(stderr,"s.\n\n");
70
      else fprintf(stderr,".\n\n");
71
    }
72
    if( _AD._warnings ) {         // Any warnings?
73
      fprintf(stderr,"%s:  Found %d warning", _buf._fp->_name, _AD._warnings);
74
      if( _AD._warnings > 1 ) fprintf(stderr,"s.\n\n");
75
      else fprintf(stderr,".\n\n");
76
    }
77
  }
78
  if (!_AD._quiet_mode)
79
    fprintf(stderr,"-----------------------------------------------------------------------------\n");
80
  _AD._TotalLines += linenum()-1;     // -1 for overshoot in "nextline" routine
81

82
  // Write out information we have stored
83
  // // UNIXism == fsync(stderr);
84
}
85

86
//------------------------------parse------------------------------------------
87
// Each top-level keyword should appear as the first non-whitespace on a line.
88
//
89
void ADLParser::parse() {
90
  char *ident;
91

92
  // Iterate over the lines in the file buffer parsing Level 1 objects
93
  for( next_line(); _curline != nullptr; next_line()) {
94
    _ptr = _curline;             // Reset ptr to start of new line
95
    skipws();                    // Skip any leading whitespace
96
    ident = get_ident();         // Get first token
97
    if (ident == nullptr) {         // Empty line
98
      continue;                  // Get the next line
99
    }
100
         if (!strcmp(ident, "instruct"))   instr_parse();
101
    else if (!strcmp(ident, "operand"))    oper_parse();
102
    else if (!strcmp(ident, "opclass"))    opclass_parse();
103
    else if (!strcmp(ident, "ins_attrib")) ins_attr_parse();
104
    else if (!strcmp(ident, "op_attrib"))  op_attr_parse();
105
    else if (!strcmp(ident, "source"))     source_parse();
106
    else if (!strcmp(ident, "source_hpp")) source_hpp_parse();
107
    else if (!strcmp(ident, "register"))   reg_parse();
108
    else if (!strcmp(ident, "frame"))      frame_parse();
109
    else if (!strcmp(ident, "encode"))     encode_parse();
110
    else if (!strcmp(ident, "pipeline"))   pipe_parse();
111
    else if (!strcmp(ident, "definitions")) definitions_parse();
112
    else if (!strcmp(ident, "peephole"))   peep_parse();
113
    else if (!strcmp(ident, "#line"))      preproc_line();
114
    else if (!strcmp(ident, "#define"))    preproc_define();
115
    else if (!strcmp(ident, "#undef"))     preproc_undef();
116
    else {
117
      parse_err(SYNERR, "expected one of - instruct, operand, ins_attrib, op_attrib, source, register, pipeline, encode\n     Found %s",ident);
118
    }
119
  }
120
  // Add reg_class spill_regs after parsing.
121
  RegisterForm *regBlock = _AD.get_registers();
122
  if (regBlock == nullptr) {
123
    parse_err(SEMERR, "Did not declare 'register' definitions");
124
  }
125
  regBlock->addSpillRegClass();
126
  regBlock->addDynamicRegClass();
127

128
  // Done with parsing, check consistency.
129

130
  if (_preproc_depth != 0) {
131
    parse_err(SYNERR, "End of file inside #ifdef");
132
  }
133

134
  // AttributeForms ins_cost and op_cost must be defined for default behaviour
135
  if (_globalNames[AttributeForm::_ins_cost] == nullptr) {
136
    parse_err(SEMERR, "Did not declare 'ins_cost' attribute");
137
  }
138
  if (_globalNames[AttributeForm::_op_cost] == nullptr) {
139
    parse_err(SEMERR, "Did not declare 'op_cost' attribute");
140
  }
141
}
142

143
// ******************** Private Level 1 Parse Functions ********************
144
//------------------------------instr_parse------------------------------------
145
// Parse the contents of an instruction definition, build the InstructForm to
146
// represent that instruction, and add it to the InstructForm list.
147
void ADLParser::instr_parse(void) {
148
  char          *ident;
149
  InstructForm  *instr;
150
  MatchRule     *rule;
151
  int            match_rules_cnt = 0;
152

153
  // First get the name of the instruction
154
  if( (ident = get_unique_ident(_globalNames,"instruction")) == nullptr )
155
    return;
156
  instr = new InstructForm(ident); // Create new instruction form
157
  instr->_linenum = linenum();
158
  _globalNames.Insert(ident, instr); // Add name to the name table
159
  // Debugging Stuff
160
  if (_AD._adl_debug > 1)
161
    fprintf(stderr,"Parsing Instruction Form %s\n", ident);
162

163
  // Then get the operands
164
  skipws();
165
  if (_curchar != '(') {
166
    parse_err(SYNERR, "missing '(' in instruct definition\n");
167
  }
168
  // Parse the operand list
169
  else get_oplist(instr->_parameters, instr->_localNames);
170
  skipws();                        // Skip leading whitespace
171
  // Check for block delimiter
172
  if ( (_curchar != '%')
173
       || ( next_char(),  (_curchar != '{')) ) {
174
    parse_err(SYNERR, "missing '%%{' in instruction definition\n");
175
    return;
176
  }
177
  next_char();                     // Maintain the invariant
178
  do {
179
    ident = get_ident();           // Grab next identifier
180
    if (ident == nullptr) {
181
      parse_err(SYNERR, "keyword identifier expected at %c\n", _curchar);
182
      continue;
183
    }
184
    if      (!strcmp(ident, "predicate")) instr->_predicate = pred_parse();
185
    else if      (!strcmp(ident, "match")) {
186
      // Allow one instruction have several match rules.
187
      rule = instr->_matrule;
188
      if (rule == nullptr) {
189
        // This is first match rule encountered
190
        rule = match_parse(instr->_localNames);
191
        if (rule) {
192
          instr->_matrule = rule;
193
          // Special case the treatment of Control instructions.
194
          if( instr->is_ideal_control() ) {
195
            // Control instructions return a special result, 'Universe'
196
            rule->_result = "Universe";
197
          }
198
          // Check for commutative operations with tree operands.
199
          matchrule_clone_and_swap(rule, instr->_ident, match_rules_cnt);
200
        }
201
      } else {
202
        // Find the end of the match rule list
203
        while (rule->_next != nullptr)
204
          rule = rule->_next;
205
        // Add the new match rule to the list
206
        rule->_next = match_parse(instr->_localNames);
207
        if (rule->_next) {
208
          rule = rule->_next;
209
          if( instr->is_ideal_control() ) {
210
            parse_err(SYNERR, "unique match rule expected for %s\n", rule->_name);
211
            return;
212
          }
213
          assert(match_rules_cnt < 100," too many match rule clones");
214
          const size_t buf_size = strlen(instr->_ident) + 4;
215
          char* buf = (char*) AdlAllocateHeap(buf_size);
216
          snprintf_checked(buf, buf_size, "%s_%d", instr->_ident, match_rules_cnt++);
217
          rule->_result = buf;
218
          // Check for commutative operations with tree operands.
219
          matchrule_clone_and_swap(rule, instr->_ident, match_rules_cnt);
220
        }
221
      }
222
    }
223
    else if (!strcmp(ident, "encode"))  {
224
      parse_err(SYNERR, "Instructions specify ins_encode, not encode\n");
225
    }
226
    else if (!strcmp(ident, "ins_encode"))       ins_encode_parse(*instr);
227
    // Parse late expand keyword.
228
    else if (!strcmp(ident, "postalloc_expand")) postalloc_expand_parse(*instr);
229
    else if (!strcmp(ident, "opcode"))           instr->_opcode    = opcode_parse(instr);
230
    else if (!strcmp(ident, "size"))             instr->_size      = size_parse(instr);
231
    else if (!strcmp(ident, "effect"))           effect_parse(instr);
232
    else if (!strcmp(ident, "flag"))             instr->_flag      = flag_parse(instr);
233
    else if (!strcmp(ident, "expand"))           instr->_exprule   = expand_parse(instr);
234
    else if (!strcmp(ident, "rewrite"))          instr->_rewrule   = rewrite_parse();
235
    else if (!strcmp(ident, "constraint")) {
236
      parse_err(SYNERR, "Instructions do not specify a constraint\n");
237
    }
238
    else if (!strcmp(ident, "construct")) {
239
      parse_err(SYNERR, "Instructions do not specify a construct\n");
240
    }
241
    else if (!strcmp(ident, "format"))           instr->_format    = format_parse();
242
    else if (!strcmp(ident, "interface")) {
243
      parse_err(SYNERR, "Instructions do not specify an interface\n");
244
    }
245
    else if (!strcmp(ident, "ins_pipe"))        ins_pipe_parse(*instr);
246
    else {  // Done with statically defined parts of instruction definition
247
      // Check identifier to see if it is the name of an attribute
248
      const Form    *form = _globalNames[ident];
249
      AttributeForm *attr = form ? form->is_attribute() : nullptr;
250
      if (attr && (attr->_atype == INS_ATTR)) {
251
        // Insert the new attribute into the linked list.
252
        Attribute *temp = attr_parse(ident);
253
        temp->_next = instr->_attribs;
254
        instr->_attribs = temp;
255
      } else {
256
        parse_err(SYNERR, "expected one of:\n predicate, match, encode, or the name of"
257
                  " an instruction attribute at %s\n", ident);
258
      }
259
    }
260
    skipws();
261
  } while(_curchar != '%');
262
  next_char();
263
  if (_curchar != '}') {
264
    parse_err(SYNERR, "missing '%%}' in instruction definition\n");
265
    return;
266
  }
267
  // Check for "Set" form of chain rule
268
  adjust_set_rule(instr);
269
  if (_AD._pipeline) {
270
    // No pipe required for late expand.
271
    if (instr->expands() || instr->postalloc_expands()) {
272
      if (instr->_ins_pipe) {
273
        parse_err(WARN, "ins_pipe and expand rule both specified for instruction \"%s\";"
274
                  " ins_pipe will be unused\n", instr->_ident);
275
      }
276
    } else {
277
      if (!instr->_ins_pipe) {
278
        parse_err(WARN, "No ins_pipe specified for instruction \"%s\"\n", instr->_ident);
279
      }
280
    }
281
  }
282
  // Add instruction to tail of instruction list
283
  _AD.addForm(instr);
284

285
  // Create instruction form for each additional match rule
286
  rule = instr->_matrule;
287
  if (rule != nullptr) {
288
    rule = rule->_next;
289
    while (rule != nullptr) {
290
      ident = (char*)rule->_result;
291
      InstructForm *clone = new InstructForm(ident, instr, rule); // Create new instruction form
292
      _globalNames.Insert(ident, clone); // Add name to the name table
293
      // Debugging Stuff
294
      if (_AD._adl_debug > 1)
295
        fprintf(stderr,"Parsing Instruction Form %s\n", ident);
296
      // Check for "Set" form of chain rule
297
      adjust_set_rule(clone);
298
      // Add instruction to tail of instruction list
299
      _AD.addForm(clone);
300
      rule = rule->_next;
301
      clone->_matrule->_next = nullptr; // One match rule per clone
302
    }
303
  }
304
}
305

306
//------------------------------matchrule_clone_and_swap-----------------------
307
// Check for commutative operations with subtree operands,
308
// create clones and swap operands.
309
void ADLParser::matchrule_clone_and_swap(MatchRule* rule, const char* instr_ident, int& match_rules_cnt) {
310
  // Check for commutative operations with tree operands.
311
  int count = 0;
312
  rule->count_commutative_op(count);
313
  if (count > 0) {
314
    // Clone match rule and swap commutative operation's operands.
315
    rule->matchrule_swap_commutative_op(instr_ident, count, match_rules_cnt);
316
  }
317
}
318

319
//------------------------------adjust_set_rule--------------------------------
320
// Check for "Set" form of chain rule
321
void ADLParser::adjust_set_rule(InstructForm *instr) {
322
  if (instr->_matrule == nullptr || instr->_matrule->_rChild == nullptr) return;
323
  const char *rch = instr->_matrule->_rChild->_opType;
324
  const Form *frm = _globalNames[rch];
325
  if( (! strcmp(instr->_matrule->_opType,"Set")) &&
326
      frm && frm->is_operand() && (! frm->ideal_only()) ) {
327
    // Previous implementation, which missed leaP*, but worked for loadCon*
328
    unsigned    position = 0;
329
    const char *result   = nullptr;
330
    const char *name     = nullptr;
331
    const char *optype   = nullptr;
332
    MatchNode  *right    = instr->_matrule->_rChild;
333
    if (right->base_operand(position, _globalNames, result, name, optype)) {
334
      position = 1;
335
      const char *result2  = nullptr;
336
      const char *name2    = nullptr;
337
      const char *optype2  = nullptr;
338
      // Can not have additional base operands in right side of match!
339
      if ( ! right->base_operand( position, _globalNames, result2, name2, optype2) ) {
340
        if (instr->_predicate != nullptr)
341
          parse_err(SYNERR, "ADLC does not support instruction chain rules with predicates");
342
        // Chain from input  _ideal_operand_type_,
343
        // Needed for shared roots of match-trees
344
        ChainList *lst = (ChainList *)_AD._chainRules[optype];
345
        if (lst == nullptr) {
346
          lst = new ChainList();
347
          _AD._chainRules.Insert(optype, lst);
348
        }
349
        if (!lst->search(instr->_matrule->_lChild->_opType)) {
350
          const char *cost = instr->cost();
351
          if (cost == nullptr) {
352
            cost = ((AttributeForm*)_globalNames[AttributeForm::_ins_cost])->_attrdef;
353
          }
354
          // The ADLC does not support chaining from the ideal operand type
355
          // of a predicated user-defined operand
356
          if( frm->is_operand() == nullptr || frm->is_operand()->_predicate == nullptr ) {
357
            lst->insert(instr->_matrule->_lChild->_opType,cost,instr->_ident);
358
          }
359
        }
360
        // Chain from input  _user_defined_operand_type_,
361
        lst = (ChainList *)_AD._chainRules[result];
362
        if (lst == nullptr) {
363
          lst = new ChainList();
364
          _AD._chainRules.Insert(result, lst);
365
        }
366
        if (!lst->search(instr->_matrule->_lChild->_opType)) {
367
          const char *cost = instr->cost();
368
          if (cost == nullptr) {
369
            cost = ((AttributeForm*)_globalNames[AttributeForm::_ins_cost])->_attrdef;
370
          }
371
          // It is safe to chain from the top-level user-defined operand even
372
          // if it has a predicate, since the predicate is checked before
373
          // the user-defined type is available.
374
          lst->insert(instr->_matrule->_lChild->_opType,cost,instr->_ident);
375
        }
376
      } else {
377
        // May have instruction chain rule if root of right-tree is an ideal
378
        OperandForm *rightOp = _globalNames[right->_opType]->is_operand();
379
        if( rightOp ) {
380
          const Form *rightRoot = _globalNames[rightOp->_matrule->_opType];
381
          if( rightRoot && rightRoot->ideal_only() ) {
382
            const char *chain_op = nullptr;
383
            if( rightRoot->is_instruction() )
384
              chain_op = rightOp->_ident;
385
            if( chain_op ) {
386
              // Look-up the operation in chain rule table
387
              ChainList *lst = (ChainList *)_AD._chainRules[chain_op];
388
              if (lst == nullptr) {
389
                lst = new ChainList();
390
                _AD._chainRules.Insert(chain_op, lst);
391
              }
392
              // if (!lst->search(instr->_matrule->_lChild->_opType)) {
393
              const char *cost = instr->cost();
394
              if (cost == nullptr) {
395
                cost = ((AttributeForm*)_globalNames[AttributeForm::_ins_cost])->_attrdef;
396
              }
397
              // This chains from a top-level operand whose predicate, if any,
398
              // has been checked.
399
              lst->insert(instr->_matrule->_lChild->_opType,cost,instr->_ident);
400
              // }
401
            }
402
          }
403
        }
404
      } // end chain rule from right-tree's ideal root
405
    }
406
  }
407
}
408

409

410
//------------------------------oper_parse-------------------------------------
411
void ADLParser::oper_parse(void) {
412
  char          *ident;
413
  OperandForm   *oper;
414
  AttributeForm *attr;
415
  MatchRule     *rule;
416

417
  // First get the name of the operand
418
  skipws();
419
  if( (ident = get_unique_ident(_globalNames,"operand")) == nullptr )
420
    return;
421
  oper = new OperandForm(ident);        // Create new operand form
422
  oper->_linenum = linenum();
423
  _globalNames.Insert(ident, oper); // Add name to the name table
424

425
  // Debugging Stuff
426
  if (_AD._adl_debug > 1) fprintf(stderr,"Parsing Operand Form %s\n", ident);
427

428
  // Get the component operands
429
  skipws();
430
  if (_curchar != '(') {
431
    parse_err(SYNERR, "missing '(' in operand definition\n");
432
    return;
433
  }
434
  else get_oplist(oper->_parameters, oper->_localNames); // Parse the component operand list
435
  skipws();
436
  // Check for block delimiter
437
  if ((_curchar != '%') || (*(_ptr+1) != '{')) { // If not open block
438
    parse_err(SYNERR, "missing '%%{' in operand definition\n");
439
    return;
440
  }
441
  next_char(); next_char();        // Skip over "%{" symbol
442
  do {
443
    ident = get_ident();           // Grab next identifier
444
    if (ident == nullptr) {
445
      parse_err(SYNERR, "keyword identifier expected at %c\n", _curchar);
446
      continue;
447
    }
448
    if      (!strcmp(ident, "predicate")) oper->_predicate = pred_parse();
449
    else if (!strcmp(ident, "match"))     {
450
      // Find the end of the match rule list
451
      rule = oper->_matrule;
452
      if (rule) {
453
        while (rule->_next) rule = rule->_next;
454
        // Add the new match rule to the list
455
        rule->_next = match_parse(oper->_localNames);
456
        if (rule->_next) {
457
          rule->_next->_result = oper->_ident;
458
        }
459
      }
460
      else {
461
        // This is first match rule encountered
462
        oper->_matrule = match_parse(oper->_localNames);
463
        if (oper->_matrule) {
464
          oper->_matrule->_result = oper->_ident;
465
        }
466
      }
467
    }
468
    else if (!strcmp(ident, "encode"))    oper->_interface = interface_parse();
469
    else if (!strcmp(ident, "ins_encode")) {
470
      parse_err(SYNERR, "Operands specify 'encode', not 'ins_encode'\n");
471
    }
472
    else if (!strcmp(ident, "opcode"))    {
473
      parse_err(SYNERR, "Operands do not specify an opcode\n");
474
    }
475
    else if (!strcmp(ident, "effect"))    {
476
      parse_err(SYNERR, "Operands do not specify an effect\n");
477
    }
478
    else if (!strcmp(ident, "expand"))    {
479
      parse_err(SYNERR, "Operands do not specify an expand\n");
480
    }
481
    else if (!strcmp(ident, "rewrite"))   {
482
      parse_err(SYNERR, "Operands do not specify a rewrite\n");
483
    }
484
    else if (!strcmp(ident, "constraint"))oper->_constraint= constraint_parse();
485
    else if (!strcmp(ident, "construct")) oper->_construct = construct_parse();
486
    else if (!strcmp(ident, "format"))    oper->_format    = format_parse();
487
    else if (!strcmp(ident, "interface")) oper->_interface = interface_parse();
488
    // Check identifier to see if it is the name of an attribute
489
    else if (((attr = _globalNames[ident]->is_attribute()) != nullptr) &&
490
             (attr->_atype == OP_ATTR))   oper->_attribs   = attr_parse(ident);
491
    else {
492
      parse_err(SYNERR, "expected one of - constraint, predicate, match, encode, format, construct, or the name of a defined operand attribute at %s\n", ident);
493
    }
494
    skipws();
495
  } while(_curchar != '%');
496
  next_char();
497
  if (_curchar != '}') {
498
    parse_err(SYNERR, "missing '%%}' in operand definition\n");
499
    return;
500
  }
501
  // Add operand to tail of operand list
502
  _AD.addForm(oper);
503
}
504

505
//------------------------------opclass_parse----------------------------------
506
// Operand Classes are a block with a comma delimited list of operand names
507
void ADLParser::opclass_parse(void) {
508
  char          *ident;
509
  OpClassForm   *opc;
510
  OperandForm   *opForm;
511

512
  // First get the name of the operand class
513
  skipws();
514
  if( (ident = get_unique_ident(_globalNames,"opclass")) == nullptr )
515
    return;
516
  opc = new OpClassForm(ident);             // Create new operand class form
517
  _globalNames.Insert(ident, opc);  // Add name to the name table
518

519
  // Debugging Stuff
520
  if (_AD._adl_debug > 1)
521
    fprintf(stderr,"Parsing Operand Class Form %s\n", ident);
522

523
  // Get the list of operands
524
  skipws();
525
  if (_curchar != '(') {
526
    parse_err(SYNERR, "missing '(' in operand definition\n");
527
    return;
528
  }
529
  do {
530
    next_char();                            // Skip past open paren or comma
531
    ident = get_ident();                    // Grab next identifier
532
    if (ident == nullptr) {
533
      parse_err(SYNERR, "keyword identifier expected at %c\n", _curchar);
534
      continue;
535
    }
536
    // Check identifier to see if it is the name of an operand
537
    const Form *form = _globalNames[ident];
538
    opForm     = form ? form->is_operand() : nullptr;
539
    if ( opForm ) {
540
      opc->_oplst.addName(ident);           // Add operand to opclass list
541
      opForm->_classes.addName(opc->_ident);// Add opclass to operand list
542
    }
543
    else {
544
      parse_err(SYNERR, "expected name of a defined operand at %s\n", ident);
545
    }
546
    skipws();                               // skip trailing whitespace
547
  } while (_curchar == ',');                // Check for the comma
548
  // Check for closing ')'
549
  if (_curchar != ')') {
550
    parse_err(SYNERR, "missing ')' or ',' in opclass definition\n");
551
    return;
552
  }
553
  next_char();                              // Consume the ')'
554
  skipws();
555
  // Check for closing ';'
556
  if (_curchar != ';') {
557
    parse_err(SYNERR, "missing ';' in opclass definition\n");
558
    return;
559
  }
560
  next_char();                             // Consume the ';'
561
  // Add operand to tail of operand list
562
  _AD.addForm(opc);
563
}
564

565
//------------------------------ins_attr_parse---------------------------------
566
void ADLParser::ins_attr_parse(void) {
567
  char          *ident;
568
  char          *aexpr;
569
  AttributeForm *attrib;
570

571
  // get name for the instruction attribute
572
  skipws();                      // Skip leading whitespace
573
  if( (ident = get_unique_ident(_globalNames,"inst_attrib")) == nullptr )
574
    return;
575
  // Debugging Stuff
576
  if (_AD._adl_debug > 1) fprintf(stderr,"Parsing Ins_Attribute Form %s\n", ident);
577

578
  // Get default value of the instruction attribute
579
  skipws();                      // Skip whitespace
580
  if ((aexpr = get_paren_expr("attribute default expression string")) == nullptr) {
581
    parse_err(SYNERR, "missing '(' in ins_attrib definition\n");
582
    return;
583
  }
584
  // Debug Stuff
585
  if (_AD._adl_debug > 1) fprintf(stderr,"Attribute Expression: %s\n", aexpr);
586

587
  // Check for terminator
588
  if (_curchar != ';') {
589
    parse_err(SYNERR, "missing ';' in ins_attrib definition\n");
590
    return;
591
  }
592
  next_char();                    // Advance past the ';'
593

594
  // Construct the attribute, record global name, and store in ArchDesc
595
  attrib = new AttributeForm(ident, INS_ATTR, aexpr);
596
  _globalNames.Insert(ident, attrib);  // Add name to the name table
597
  _AD.addForm(attrib);
598
}
599

600
//------------------------------op_attr_parse----------------------------------
601
void ADLParser::op_attr_parse(void) {
602
  char          *ident;
603
  char          *aexpr;
604
  AttributeForm *attrib;
605

606
  // get name for the operand attribute
607
  skipws();                      // Skip leading whitespace
608
  if( (ident = get_unique_ident(_globalNames,"op_attrib")) == nullptr )
609
    return;
610
  // Debugging Stuff
611
  if (_AD._adl_debug > 1) fprintf(stderr,"Parsing Op_Attribute Form %s\n", ident);
612

613
  // Get default value of the instruction attribute
614
  skipws();                      // Skip whitespace
615
  if ((aexpr = get_paren_expr("attribute default expression string")) == nullptr) {
616
    parse_err(SYNERR, "missing '(' in op_attrib definition\n");
617
    return;
618
  }
619
  // Debug Stuff
620
  if (_AD._adl_debug > 1) fprintf(stderr,"Attribute Expression: %s\n", aexpr);
621

622
  // Check for terminator
623
  if (_curchar != ';') {
624
    parse_err(SYNERR, "missing ';' in op_attrib definition\n");
625
    return;
626
  }
627
  next_char();                    // Advance past the ';'
628

629
  // Construct the attribute, record global name, and store in ArchDesc
630
  attrib = new AttributeForm(ident, OP_ATTR, aexpr);
631
  _globalNames.Insert(ident, attrib);
632
  _AD.addForm(attrib);
633
}
634

635
//------------------------------definitions_parse-----------------------------------
636
void ADLParser::definitions_parse(void) {
637
  skipws();                       // Skip leading whitespace
638
  if (_curchar == '%' && *(_ptr+1) == '{') {
639
    next_char(); next_char();     // Skip "%{"
640
    skipws();
641
    while (_curchar != '%' && *(_ptr+1) != '}') {
642
      // Process each definition until finding closing string "%}"
643
      char *token = get_ident();
644
      if (token == nullptr) {
645
        parse_err(SYNERR, "missing identifier inside definitions block.\n");
646
        return;
647
      }
648
      if (strcmp(token,"int_def")==0)     { int_def_parse(); }
649
      // if (strcmp(token,"str_def")==0)   { str_def_parse(); }
650
      skipws();
651
    }
652
  }
653
  else {
654
    parse_err(SYNERR, "Missing %%{ ... %%} block after definitions keyword.\n");
655
    return;
656
  }
657
}
658

659
//------------------------------int_def_parse----------------------------------
660
// Parse Example:
661
// int_def    MEMORY_REF_COST      (         200,  DEFAULT_COST * 2);
662
// <keyword>  <name>               ( <int_value>,   <description>  );
663
//
664
void ADLParser::int_def_parse(void) {
665
  char *name        = nullptr;         // Name of definition
666
  char *value       = nullptr;         // its value,
667
  int   int_value   = -1;           // positive values only
668
  char *description = nullptr;         // textual description
669

670
  // Get definition name
671
  skipws();                      // Skip whitespace
672
  name = get_ident();
673
  if (name == nullptr) {
674
    parse_err(SYNERR, "missing definition name after int_def\n");
675
    return;
676
  }
677

678
  // Check for value of int_def dname( integer_value [, string_expression ] )
679
  skipws();
680
  if (_curchar == '(') {
681

682
    // Parse the integer value.
683
    next_char();
684
    value = get_ident();
685
    if (value == nullptr) {
686
      parse_err(SYNERR, "missing value in int_def\n");
687
      return;
688
    }
689
    if( !is_int_token(value, int_value) ) {
690
      parse_err(SYNERR, "value in int_def is not recognized as integer\n");
691
      return;
692
    }
693
    skipws();
694

695
    // Check for description
696
    if (_curchar == ',') {
697
      next_char();   // skip ','
698

699
      description = get_expr("int_def description", ")");
700
      if (description == nullptr) {
701
        parse_err(SYNERR, "invalid or missing description in int_def\n");
702
        return;
703
      }
704
      trim(description);
705
    }
706

707
    if (_curchar != ')') {
708
      parse_err(SYNERR, "missing ')' in register definition statement\n");
709
      return;
710
    }
711
    next_char();
712
  }
713

714
  // Check for closing ';'
715
  skipws();
716
  if (_curchar != ';') {
717
    parse_err(SYNERR, "missing ';' after int_def\n");
718
    return;
719
  }
720
  next_char();                   // move past ';'
721

722
  // Debug Stuff
723
  if (_AD._adl_debug > 1) {
724
    fprintf(stderr,"int_def: %s ( %s, %s )\n", name,
725
            (value), (description ? description : ""));
726
  }
727

728
  // Record new definition.
729
  Expr *expr     = new Expr(name, description, int_value, int_value);
730
  const Expr *old_expr = _AD.globalDefs().define(name, expr);
731
  if (old_expr != nullptr) {
732
    parse_err(SYNERR, "Duplicate definition\n");
733
    return;
734
  }
735

736
  return;
737
}
738

739

740
//------------------------------source_parse-----------------------------------
741
void ADLParser::source_parse(void) {
742
  SourceForm *source;             // Encode class for instruction/operand
743
  char   *rule = nullptr;            // String representation of encode rule
744

745
  skipws();                       // Skip leading whitespace
746
  if ( (rule = find_cpp_block("source block")) == nullptr ) {
747
    parse_err(SYNERR, "incorrect or missing block for 'source'.\n");
748
    return;
749
  }
750
  // Debug Stuff
751
  if (_AD._adl_debug > 1) fprintf(stderr,"Source Form: %s\n", rule);
752

753
  source = new SourceForm(rule);    // Build new Source object
754
  _AD.addForm(source);
755
  // skipws();
756
}
757

758
//------------------------------source_hpp_parse-------------------------------
759
// Parse a source_hpp %{ ... %} block.
760
// The code gets stuck into the ad_<arch>.hpp file.
761
// If the source_hpp block appears before the register block in the AD
762
// file, it goes up at the very top of the ad_<arch>.hpp file, so that
763
// it can be used by register encodings, etc.  Otherwise, it goes towards
764
// the bottom, where it's useful as a global definition to *.cpp files.
765
void ADLParser::source_hpp_parse(void) {
766
  char   *rule = nullptr;            // String representation of encode rule
767

768
  skipws();                       // Skip leading whitespace
769
  if ( (rule = find_cpp_block("source_hpp block")) == nullptr ) {
770
    parse_err(SYNERR, "incorrect or missing block for 'source_hpp'.\n");
771
    return;
772
  }
773
  // Debug Stuff
774
  if (_AD._adl_debug > 1) fprintf(stderr,"Header Form: %s\n", rule);
775

776
  if (_AD.get_registers() == nullptr) {
777
    // Very early in the file, before reg_defs, we collect pre-headers.
778
    PreHeaderForm* pre_header = new PreHeaderForm(rule);
779
    _AD.addForm(pre_header);
780
  } else {
781
    // Normally, we collect header info, placed at the bottom of the hpp file.
782
    HeaderForm* header = new HeaderForm(rule);
783
    _AD.addForm(header);
784
  }
785
}
786

787
//------------------------------reg_parse--------------------------------------
788
void ADLParser::reg_parse(void) {
789
  RegisterForm *regBlock = _AD.get_registers(); // Information about registers encoding
790
  if (regBlock == nullptr) {
791
    // Create the RegisterForm for the architecture description.
792
    regBlock = new RegisterForm();    // Build new Source object
793
    _AD.addForm(regBlock);
794
  }
795

796
  skipws();                       // Skip leading whitespace
797
  if (_curchar == '%' && *(_ptr+1) == '{') {
798
    next_char(); next_char();     // Skip "%{"
799
    skipws();
800
    while (_curchar != '%' && *(_ptr+1) != '}') {
801
      char *token = get_ident();
802
      if (token == nullptr) {
803
        parse_err(SYNERR, "missing identifier inside register block.\n");
804
        return;
805
      }
806
      if (strcmp(token,"reg_def")==0)          { reg_def_parse(); }
807
      else if (strcmp(token,"reg_class")==0)   { reg_class_parse(); }
808
      else if (strcmp(token, "reg_class_dynamic") == 0) { reg_class_dynamic_parse(); }
809
      else if (strcmp(token,"alloc_class")==0) { alloc_class_parse(); }
810
      else if (strcmp(token,"#define")==0)     { preproc_define(); }
811
      else { parse_err(SYNERR, "bad token %s inside register block.\n", token); break; }
812
      skipws();
813
    }
814
  }
815
  else {
816
    parse_err(SYNERR, "Missing %c{ ... %c} block after register keyword.\n",'%','%');
817
    return;
818
  }
819
}
820

821
//------------------------------encode_parse-----------------------------------
822
void ADLParser::encode_parse(void) {
823
  EncodeForm *encBlock;         // Information about instruction/operand encoding
824

825
  _AD.getForm(&encBlock);
826
  if ( encBlock == nullptr) {
827
    // Create the EncodeForm for the architecture description.
828
    encBlock = new EncodeForm();    // Build new Source object
829
    _AD.addForm(encBlock);
830
  }
831

832
  skipws();                       // Skip leading whitespace
833
  if (_curchar == '%' && *(_ptr+1) == '{') {
834
    next_char(); next_char();     // Skip "%{"
835
    skipws();
836
    while (_curchar != '%' && *(_ptr+1) != '}') {
837
      char *token = get_ident();
838
      if (token == nullptr) {
839
            parse_err(SYNERR, "missing identifier inside encoding block.\n");
840
            return;
841
      }
842
      if (strcmp(token,"enc_class")==0)   { enc_class_parse(); }
843
      skipws();
844
    }
845
  }
846
  else {
847
    parse_err(SYNERR, "Missing %c{ ... %c} block after encode keyword.\n",'%','%');
848
    return;
849
  }
850
}
851

852
//------------------------------enc_class_parse--------------------------------
853
void ADLParser::enc_class_parse(void) {
854
  char       *ec_name;           // Name of encoding class being defined
855

856
  // Get encoding class name
857
  skipws();                      // Skip whitespace
858
  ec_name = get_ident();
859
  if (ec_name == nullptr) {
860
    parse_err(SYNERR, "missing encoding class name after encode.\n");
861
    return;
862
  }
863

864
  EncClass  *encoding = _AD._encode->add_EncClass(ec_name);
865
  encoding->_linenum = linenum();
866

867
  skipws();                      // Skip leading whitespace
868
  // Check for optional parameter list
869
  if (_curchar == '(') {
870
    do {
871
      char *pType = nullptr;     // parameter type
872
      char *pName = nullptr;     // parameter name
873

874
      next_char();               // skip open paren & comma characters
875
      skipws();
876
      if (_curchar == ')') break;
877

878
      // Get parameter type
879
      pType = get_ident();
880
      if (pType == nullptr) {
881
        parse_err(SYNERR, "parameter type expected at %c\n", _curchar);
882
        return;
883
      }
884

885
      skipws();
886
      // Get parameter name
887
      pName = get_ident();
888
      if (pName == nullptr) {
889
        parse_err(SYNERR, "parameter name expected at %c\n", _curchar);
890
        return;
891
      }
892

893
      // Record parameter type and name
894
      encoding->add_parameter( pType, pName );
895

896
      skipws();
897
    } while(_curchar == ',');
898

899
    if (_curchar != ')') parse_err(SYNERR, "missing ')'\n");
900
    else {
901
      next_char();                  // Skip ')'
902
    }
903
  } // Done with parameter list
904

905
  skipws();
906
  // Check for block starting delimiters
907
  if ((_curchar != '%') || (*(_ptr+1) != '{')) { // If not open block
908
    parse_err(SYNERR, "missing '%c{' in enc_class definition\n", '%');
909
    return;
910
  }
911
  next_char();                      // Skip '%'
912
  next_char();                      // Skip '{'
913

914
  enc_class_parse_block(encoding, ec_name);
915
}
916

917

918
void ADLParser::enc_class_parse_block(EncClass* encoding, char* ec_name) {
919
  skipws_no_preproc();              // Skip leading whitespace
920
  // Prepend location descriptor, for debugging; cf. ADLParser::find_cpp_block
921
  if (_AD._adlocation_debug) {
922
    encoding->add_code(get_line_string());
923
  }
924

925
  // Collect the parts of the encode description
926
  // (1) strings that are passed through to output
927
  // (2) replacement/substitution variable, preceded by a '$'
928
  while ( (_curchar != '%') && (*(_ptr+1) != '}') ) {
929

930
    // (1)
931
    // Check if there is a string to pass through to output
932
    char *start = _ptr;       // Record start of the next string
933
    while ((_curchar != '$') && ((_curchar != '%') || (*(_ptr+1) != '}')) ) {
934
      // If at the start of a comment, skip past it
935
      if( (_curchar == '/') && ((*(_ptr+1) == '/') || (*(_ptr+1) == '*')) ) {
936
        skipws_no_preproc();
937
      } else {
938
        // ELSE advance to the next character, or start of the next line
939
        next_char_or_line();
940
      }
941
    }
942
    // If a string was found, terminate it and record in EncClass
943
    if ( start != _ptr ) {
944
      *_ptr  = '\0';          // Terminate the string
945
      encoding->add_code(start);
946
    }
947

948
    // (2)
949
    // If we are at a replacement variable,
950
    // copy it and record in EncClass
951
    if (_curchar == '$') {
952
      // Found replacement Variable
953
      char* rep_var = get_rep_var_ident_dup();
954
      // Add flag to _strings list indicating we should check _rep_vars
955
      encoding->add_rep_var(rep_var);
956
    }
957
  } // end while part of format description
958
  next_char();                      // Skip '%'
959
  next_char();                      // Skip '}'
960

961
  skipws();
962

963
  if (_AD._adlocation_debug) {
964
    encoding->add_code(end_line_marker());
965
  }
966

967
  // Debug Stuff
968
  if (_AD._adl_debug > 1) fprintf(stderr,"EncodingClass Form: %s\n", ec_name);
969
}
970

971
//------------------------------frame_parse-----------------------------------
972
void ADLParser::frame_parse(void) {
973
  FrameForm  *frame;              // Information about stack-frame layout
974
  char       *desc = nullptr;     // String representation of frame
975

976
  skipws();                       // Skip leading whitespace
977

978
  frame = new FrameForm();        // Build new Frame object
979
  // Check for open block sequence
980
  skipws();                       // Skip leading whitespace
981
  if (_curchar == '%' && *(_ptr+1) == '{') {
982
    next_char(); next_char();     // Skip "%{"
983
    skipws();
984
    while (_curchar != '%' && *(_ptr+1) != '}') {
985
      char *token = get_ident();
986
      if (token == nullptr) {
987
            parse_err(SYNERR, "missing identifier inside frame block.\n");
988
            return;
989
      }
990
      if (strcmp(token,"sync_stack_slots")==0) {
991
        sync_stack_slots_parse(frame);
992
      }
993
      if (strcmp(token,"frame_pointer")==0) {
994
        frame_pointer_parse(frame, false);
995
      }
996
      if (strcmp(token,"interpreter_frame_pointer")==0) {
997
        interpreter_frame_pointer_parse(frame, false);
998
      }
999
      if (strcmp(token,"inline_cache_reg")==0) {
1000
        inline_cache_parse(frame, false);
1001
      }
1002
      if (strcmp(token,"compiler_method_oop_reg")==0) {
1003
        parse_err(WARN, "Using obsolete Token, compiler_method_oop_reg");
1004
        skipws();
1005
      }
1006
      if (strcmp(token,"interpreter_method_oop_reg")==0) {
1007
        parse_err(WARN, "Using obsolete Token, interpreter_method_oop_reg");
1008
        skipws();
1009
      }
1010
      if (strcmp(token,"interpreter_method_reg")==0) {
1011
        parse_err(WARN, "Using obsolete Token, interpreter_method_reg");
1012
        skipws();
1013
      }
1014
      if (strcmp(token,"cisc_spilling_operand_name")==0) {
1015
        cisc_spilling_operand_name_parse(frame, false);
1016
      }
1017
      if (strcmp(token,"stack_alignment")==0) {
1018
        stack_alignment_parse(frame);
1019
      }
1020
      if (strcmp(token,"return_addr")==0) {
1021
        return_addr_parse(frame, false);
1022
      }
1023
      if (strcmp(token,"in_preserve_stack_slots")==0) {
1024
        parse_err(WARN, "Using obsolete token, in_preserve_stack_slots");
1025
        skipws();
1026
      }
1027
      if (strcmp(token,"out_preserve_stack_slots")==0) {
1028
        parse_err(WARN, "Using obsolete token, out_preserve_stack_slots");
1029
        skipws();
1030
      }
1031
      if (strcmp(token,"varargs_C_out_slots_killed")==0) {
1032
        frame->_varargs_C_out_slots_killed = parse_one_arg("varargs C out slots killed");
1033
      }
1034
      if (strcmp(token,"calling_convention")==0) {
1035
        parse_err(WARN, "Using obsolete token, calling_convention");
1036
        skipws();
1037
      }
1038
      if (strcmp(token,"return_value")==0) {
1039
        frame->_return_value = return_value_parse();
1040
      }
1041
      if (strcmp(token,"c_frame_pointer")==0) {
1042
        frame_pointer_parse(frame, true);
1043
      }
1044
      if (strcmp(token,"c_return_addr")==0) {
1045
        return_addr_parse(frame, true);
1046
      }
1047
      if (strcmp(token,"c_calling_convention")==0) {
1048
        parse_err(WARN, "Using obsolete token, c_calling_convention");
1049
        skipws();
1050
      }
1051
      if (strcmp(token,"c_return_value")==0) {
1052
        frame->_c_return_value = return_value_parse();
1053
      }
1054

1055
      skipws();
1056
    }
1057
  }
1058
  else {
1059
    parse_err(SYNERR, "Missing %c{ ... %c} block after encode keyword.\n",'%','%');
1060
    return;
1061
  }
1062
  // All Java versions are required, native versions are optional
1063
  if(frame->_frame_pointer == nullptr) {
1064
    parse_err(SYNERR, "missing frame pointer definition in frame section.\n");
1065
    return;
1066
  }
1067
  // !!!!! !!!!!
1068
  // if(frame->_interpreter_frame_ptr_reg == nullptr) {
1069
  //   parse_err(SYNERR, "missing interpreter frame pointer definition in frame section.\n");
1070
  //   return;
1071
  // }
1072
  if(frame->_alignment == nullptr) {
1073
    parse_err(SYNERR, "missing alignment definition in frame section.\n");
1074
    return;
1075
  }
1076
  if(frame->_return_addr == nullptr) {
1077
    parse_err(SYNERR, "missing return address location in frame section.\n");
1078
    return;
1079
  }
1080
  if(frame->_varargs_C_out_slots_killed == nullptr) {
1081
    parse_err(SYNERR, "missing varargs C out slots killed definition in frame section.\n");
1082
    return;
1083
  }
1084
  if(frame->_return_value == nullptr) {
1085
    parse_err(SYNERR, "missing return value definition in frame section.\n");
1086
    return;
1087
  }
1088
  // Fill natives in identically with the Java versions if not present.
1089
  if(frame->_c_frame_pointer == nullptr) {
1090
    frame->_c_frame_pointer = frame->_frame_pointer;
1091
  }
1092
  if(frame->_c_return_addr == nullptr) {
1093
    frame->_c_return_addr = frame->_return_addr;
1094
    frame->_c_return_addr_loc = frame->_return_addr_loc;
1095
  }
1096
  if(frame->_c_return_value == nullptr) {
1097
    frame->_c_return_value = frame->_return_value;
1098
  }
1099

1100
  // Debug Stuff
1101
  if (_AD._adl_debug > 1) fprintf(stderr,"Frame Form: %s\n", desc);
1102

1103
  // Create the EncodeForm for the architecture description.
1104
  _AD.addForm(frame);
1105
  // skipws();
1106
}
1107

1108
//------------------------------sync_stack_slots_parse-------------------------
1109
void ADLParser::sync_stack_slots_parse(FrameForm *frame) {
1110
    // Assign value into frame form
1111
    frame->_sync_stack_slots = parse_one_arg("sync stack slots entry");
1112
}
1113

1114
//------------------------------frame_pointer_parse----------------------------
1115
void ADLParser::frame_pointer_parse(FrameForm *frame, bool native) {
1116
  char *frame_pointer = parse_one_arg("frame pointer entry");
1117
  // Assign value into frame form
1118
  if (native) { frame->_c_frame_pointer = frame_pointer; }
1119
  else        { frame->_frame_pointer   = frame_pointer; }
1120
}
1121

1122
//------------------------------interpreter_frame_pointer_parse----------------------------
1123
void ADLParser::interpreter_frame_pointer_parse(FrameForm *frame, bool native) {
1124
  frame->_interpreter_frame_pointer_reg = parse_one_arg("interpreter frame pointer entry");
1125
}
1126

1127
//------------------------------inline_cache_parse-----------------------------
1128
void ADLParser::inline_cache_parse(FrameForm *frame, bool native) {
1129
  frame->_inline_cache_reg = parse_one_arg("inline cache reg entry");
1130
}
1131

1132
//------------------------------cisc_spilling_operand_parse---------------------
1133
void ADLParser::cisc_spilling_operand_name_parse(FrameForm *frame, bool native) {
1134
  frame->_cisc_spilling_operand_name = parse_one_arg("cisc spilling operand name");
1135
}
1136

1137
//------------------------------stack_alignment_parse--------------------------
1138
void ADLParser::stack_alignment_parse(FrameForm *frame) {
1139
  char *alignment = parse_one_arg("stack alignment entry");
1140
  // Assign value into frame
1141
  frame->_alignment   = alignment;
1142
}
1143

1144
//------------------------------parse_one_arg-------------------------------
1145
char *ADLParser::parse_one_arg(const char *description) {
1146
  char *token = nullptr;
1147
  if(_curchar == '(') {
1148
    next_char();
1149
    skipws();
1150
    token = get_expr(description, ")");
1151
    if (token == nullptr) {
1152
      parse_err(SYNERR, "missing value inside %s.\n", description);
1153
      return nullptr;
1154
    }
1155
    next_char();           // skip the close paren
1156
    if(_curchar != ';') {  // check for semi-colon
1157
      parse_err(SYNERR, "missing %c in %s.\n", ';', description);
1158
      return nullptr;
1159
    }
1160
    next_char();           // skip the semi-colon
1161
  }
1162
  else {
1163
    parse_err(SYNERR, "Missing %c in %s.\n", '(', description);
1164
    return nullptr;
1165
  }
1166

1167
  trim(token);
1168
  return token;
1169
}
1170

1171
//------------------------------return_addr_parse------------------------------
1172
void ADLParser::return_addr_parse(FrameForm *frame, bool native) {
1173
  bool in_register  = true;
1174
  if(_curchar == '(') {
1175
    next_char();
1176
    skipws();
1177
    char *token = get_ident();
1178
    if (token == nullptr) {
1179
      parse_err(SYNERR, "missing value inside return address entry.\n");
1180
      return;
1181
    }
1182
    // check for valid values for stack/register
1183
    if (strcmp(token, "REG") == 0) {
1184
      in_register = true;
1185
    }
1186
    else if (strcmp(token, "STACK") == 0) {
1187
      in_register = false;
1188
    }
1189
    else {
1190
      parse_err(SYNERR, "invalid value inside return_address entry.\n");
1191
      return;
1192
    }
1193
    if (native) { frame->_c_return_addr_loc = in_register; }
1194
    else        { frame->_return_addr_loc   = in_register; }
1195

1196
    // Parse expression that specifies register or stack position
1197
    skipws();
1198
    char *token2 = get_expr("return address entry", ")");
1199
    if (token2 == nullptr) {
1200
      parse_err(SYNERR, "missing value inside return address entry.\n");
1201
      return;
1202
    }
1203
    next_char();           // skip the close paren
1204
    if (native) { frame->_c_return_addr = token2; }
1205
    else        { frame->_return_addr   = token2; }
1206

1207
    if(_curchar != ';') {  // check for semi-colon
1208
      parse_err(SYNERR, "missing %c in return address entry.\n", ';');
1209
      return;
1210
    }
1211
    next_char();           // skip the semi-colon
1212
  }
1213
  else {
1214
    parse_err(SYNERR, "Missing %c in return_address entry.\n", '(');
1215
  }
1216
}
1217

1218
//------------------------------return_value_parse-----------------------------
1219
char *ADLParser::return_value_parse() {
1220
  char   *desc = nullptr;       // String representation of return_value
1221

1222
  skipws();                     // Skip leading whitespace
1223
  if ( (desc = find_cpp_block("return value block")) == nullptr ) {
1224
    parse_err(SYNERR, "incorrect or missing block for 'return_value'.\n");
1225
  }
1226
  return desc;
1227
}
1228

1229
//------------------------------ins_pipe_parse---------------------------------
1230
void ADLParser::ins_pipe_parse(InstructForm &instr) {
1231
  char * ident;
1232

1233
  skipws();
1234
  if ( _curchar != '(' ) {       // Check for delimiter
1235
    parse_err(SYNERR, "missing \"(\" in ins_pipe definition\n");
1236
    return;
1237
  }
1238

1239
  next_char();
1240
  ident = get_ident();           // Grab next identifier
1241

1242
  if (ident == nullptr) {
1243
    parse_err(SYNERR, "keyword identifier expected at %c\n", _curchar);
1244
    return;
1245
  }
1246

1247
  skipws();
1248
  if ( _curchar != ')' ) {       // Check for delimiter
1249
    parse_err(SYNERR, "missing \")\" in ins_pipe definition\n");
1250
    return;
1251
  }
1252

1253
  next_char();                   // skip the close paren
1254
  if(_curchar != ';') {          // check for semi-colon
1255
    parse_err(SYNERR, "missing %c in return value entry.\n", ';');
1256
    return;
1257
  }
1258
  next_char();                   // skip the semi-colon
1259

1260
  // Check ident for validity
1261
  if (_AD._pipeline && !_AD._pipeline->_classlist.search(ident)) {
1262
    parse_err(SYNERR, "\"%s\" is not a valid pipeline class\n", ident);
1263
    return;
1264
  }
1265

1266
  // Add this instruction to the list in the pipeline class
1267
  _AD._pipeline->_classdict[ident]->is_pipeclass()->_instructs.addName(instr._ident);
1268

1269
  // Set the name of the pipeline class in the instruction
1270
  instr._ins_pipe = ident;
1271
  return;
1272
}
1273

1274
//------------------------------pipe_parse-------------------------------------
1275
void ADLParser::pipe_parse(void) {
1276
  PipelineForm *pipeline;         // Encode class for instruction/operand
1277
  char * ident;
1278

1279
  pipeline = new PipelineForm();  // Build new Source object
1280
  _AD.addForm(pipeline);
1281

1282
  skipws();                       // Skip leading whitespace
1283
  // Check for block delimiter
1284
  if ( (_curchar != '%')
1285
       || ( next_char(),  (_curchar != '{')) ) {
1286
    parse_err(SYNERR, "missing '%%{' in pipeline definition\n");
1287
    return;
1288
  }
1289
  next_char();                     // Maintain the invariant
1290
  do {
1291
    ident = get_ident();           // Grab next identifier
1292
    if (ident == nullptr) {
1293
      parse_err(SYNERR, "keyword identifier expected at %c\n", _curchar);
1294
      continue;
1295
    }
1296
    if      (!strcmp(ident, "resources" )) resource_parse(*pipeline);
1297
    else if (!strcmp(ident, "pipe_desc" )) pipe_desc_parse(*pipeline);
1298
    else if (!strcmp(ident, "pipe_class")) pipe_class_parse(*pipeline);
1299
    else if (!strcmp(ident, "define")) {
1300
      skipws();
1301
      if ( (_curchar != '%')
1302
           || ( next_char(),  (_curchar != '{')) ) {
1303
        parse_err(SYNERR, "expected '%%{'\n");
1304
        return;
1305
      }
1306
      next_char(); skipws();
1307

1308
      char *node_class = get_ident();
1309
      if (node_class == nullptr) {
1310
        parse_err(SYNERR, "expected identifier, found \"%c\"\n", _curchar);
1311
        return;
1312
      }
1313

1314
      skipws();
1315
      if (_curchar != ',' && _curchar != '=') {
1316
        parse_err(SYNERR, "expected `=`, found '%c'\n", _curchar);
1317
        break;
1318
      }
1319
      next_char(); skipws();
1320

1321
      char *pipe_class = get_ident();
1322
      if (pipe_class == nullptr) {
1323
        parse_err(SYNERR, "expected identifier, found \"%c\"\n", _curchar);
1324
        return;
1325
      }
1326
      if (_curchar != ';' ) {
1327
        parse_err(SYNERR, "expected `;`, found '%c'\n", _curchar);
1328
        break;
1329
      }
1330
      next_char();              // Skip over semi-colon
1331

1332
      skipws();
1333
      if ( (_curchar != '%')
1334
           || ( next_char(),  (_curchar != '}')) ) {
1335
        parse_err(SYNERR, "expected '%%}', found \"%c\"\n", _curchar);
1336
      }
1337
      next_char();
1338

1339
      // Check ident for validity
1340
      if (_AD._pipeline && !_AD._pipeline->_classlist.search(pipe_class)) {
1341
        parse_err(SYNERR, "\"%s\" is not a valid pipeline class\n", pipe_class);
1342
        return;
1343
      }
1344

1345
      // Add this machine node to the list in the pipeline class
1346
      _AD._pipeline->_classdict[pipe_class]->is_pipeclass()->_instructs.addName(node_class);
1347

1348
      MachNodeForm *machnode = new MachNodeForm(node_class); // Create new machnode form
1349
      machnode->_machnode_pipe = pipe_class;
1350

1351
      _AD.addForm(machnode);
1352
    }
1353
    else if (!strcmp(ident, "attributes")) {
1354
      bool vsi_seen = false;
1355

1356
      skipws();
1357
      if ( (_curchar != '%')
1358
           || ( next_char(),  (_curchar != '{')) ) {
1359
        parse_err(SYNERR, "expected '%%{'\n");
1360
        return;
1361
      }
1362
      next_char(); skipws();
1363

1364
      while (_curchar != '%') {
1365
        ident = get_ident();
1366
        if (ident == nullptr)
1367
          break;
1368

1369
        if (!strcmp(ident, "variable_size_instructions")) {
1370
          skipws();
1371
          if (_curchar == ';') {
1372
            next_char(); skipws();
1373
          }
1374

1375
          pipeline->_variableSizeInstrs = true;
1376
          vsi_seen = true;
1377
          continue;
1378
        }
1379

1380
        if (!strcmp(ident, "fixed_size_instructions")) {
1381
          skipws();
1382
          if (_curchar == ';') {
1383
            next_char(); skipws();
1384
          }
1385

1386
          pipeline->_variableSizeInstrs = false;
1387
          vsi_seen = true;
1388
          continue;
1389
        }
1390

1391
        if (!strcmp(ident, "branch_has_delay_slot")) {
1392
          skipws();
1393
          if (_curchar == ';') {
1394
            next_char(); skipws();
1395
          }
1396

1397
          pipeline->_branchHasDelaySlot = true;
1398
          continue;
1399
        }
1400

1401
        if (!strcmp(ident, "max_instructions_per_bundle")) {
1402
          skipws();
1403
          if (_curchar != '=') {
1404
            parse_err(SYNERR, "expected `=`\n");
1405
            break;
1406
            }
1407

1408
          next_char(); skipws();
1409
          pipeline->_maxInstrsPerBundle = get_int();
1410
          skipws();
1411

1412
          if (_curchar == ';') {
1413
            next_char(); skipws();
1414
          }
1415

1416
          continue;
1417
        }
1418

1419
        if (!strcmp(ident, "max_bundles_per_cycle")) {
1420
          skipws();
1421
          if (_curchar != '=') {
1422
            parse_err(SYNERR, "expected `=`\n");
1423
            break;
1424
            }
1425

1426
          next_char(); skipws();
1427
          pipeline->_maxBundlesPerCycle = get_int();
1428
          skipws();
1429

1430
          if (_curchar == ';') {
1431
            next_char(); skipws();
1432
          }
1433

1434
          continue;
1435
        }
1436

1437
        if (!strcmp(ident, "instruction_unit_size")) {
1438
          skipws();
1439
          if (_curchar != '=') {
1440
            parse_err(SYNERR, "expected `=`, found '%c'\n", _curchar);
1441
            break;
1442
            }
1443

1444
          next_char(); skipws();
1445
          pipeline->_instrUnitSize = get_int();
1446
          skipws();
1447

1448
          if (_curchar == ';') {
1449
            next_char(); skipws();
1450
          }
1451

1452
          continue;
1453
        }
1454

1455
        if (!strcmp(ident, "bundle_unit_size")) {
1456
          skipws();
1457
          if (_curchar != '=') {
1458
            parse_err(SYNERR, "expected `=`, found '%c'\n", _curchar);
1459
            break;
1460
            }
1461

1462
          next_char(); skipws();
1463
          pipeline->_bundleUnitSize = get_int();
1464
          skipws();
1465

1466
          if (_curchar == ';') {
1467
            next_char(); skipws();
1468
          }
1469

1470
          continue;
1471
        }
1472

1473
        if (!strcmp(ident, "instruction_fetch_unit_size")) {
1474
          skipws();
1475
          if (_curchar != '=') {
1476
            parse_err(SYNERR, "expected `=`, found '%c'\n", _curchar);
1477
            break;
1478
            }
1479

1480
          next_char(); skipws();
1481
          pipeline->_instrFetchUnitSize = get_int();
1482
          skipws();
1483

1484
          if (_curchar == ';') {
1485
            next_char(); skipws();
1486
          }
1487

1488
          continue;
1489
        }
1490

1491
        if (!strcmp(ident, "instruction_fetch_units")) {
1492
          skipws();
1493
          if (_curchar != '=') {
1494
            parse_err(SYNERR, "expected `=`, found '%c'\n", _curchar);
1495
            break;
1496
            }
1497

1498
          next_char(); skipws();
1499
          pipeline->_instrFetchUnits = get_int();
1500
          skipws();
1501

1502
          if (_curchar == ';') {
1503
            next_char(); skipws();
1504
          }
1505

1506
          continue;
1507
        }
1508

1509
        if (!strcmp(ident, "nops")) {
1510
          skipws();
1511
          if (_curchar != '(') {
1512
            parse_err(SYNERR, "expected `(`, found '%c'\n", _curchar);
1513
            break;
1514
            }
1515

1516
          next_char(); skipws();
1517

1518
          while (_curchar != ')') {
1519
            ident = get_ident();
1520
            if (ident == nullptr) {
1521
              parse_err(SYNERR, "expected identifier for nop instruction, found '%c'\n", _curchar);
1522
              break;
1523
            }
1524

1525
            pipeline->_noplist.addName(ident);
1526
            pipeline->_nopcnt++;
1527
            skipws();
1528

1529
            if (_curchar == ',') {
1530
              next_char(); skipws();
1531
            }
1532
          }
1533

1534
          next_char(); skipws();
1535

1536
          if (_curchar == ';') {
1537
            next_char(); skipws();
1538
          }
1539

1540
          continue;
1541
        }
1542

1543
        parse_err(SYNERR, "unknown specifier \"%s\"\n", ident);
1544
      }
1545

1546
      if ( (_curchar != '%')
1547
           || ( next_char(),  (_curchar != '}')) ) {
1548
        parse_err(SYNERR, "expected '%%}', found \"%c\"\n", _curchar);
1549
      }
1550
      next_char(); skipws();
1551

1552
      if (pipeline->_maxInstrsPerBundle == 0)
1553
        parse_err(SYNERR, "\"max_instructions_per_bundle\" unspecified\n");
1554
      if (pipeline->_instrUnitSize == 0 && pipeline->_bundleUnitSize == 0)
1555
        parse_err(SYNERR, "\"instruction_unit_size\" and \"bundle_unit_size\" unspecified\n");
1556
      if (pipeline->_instrFetchUnitSize == 0)
1557
        parse_err(SYNERR, "\"instruction_fetch_unit_size\" unspecified\n");
1558
      if (pipeline->_instrFetchUnits == 0)
1559
        parse_err(SYNERR, "\"instruction_fetch_units\" unspecified\n");
1560
      if (!vsi_seen)
1561
        parse_err(SYNERR, "\"variable_size_instruction\" or \"fixed_size_instruction\" unspecified\n");
1562
    }
1563
    else {  // Done with statically defined parts of instruction definition
1564
      parse_err(SYNERR, "expected one of \"resources\", \"pipe_desc\", \"pipe_class\", found \"%s\"\n", ident);
1565
      return;
1566
    }
1567
    skipws();
1568
    if (_curchar == ';')
1569
      skipws();
1570
  } while(_curchar != '%');
1571

1572
  next_char();
1573
  if (_curchar != '}') {
1574
    parse_err(SYNERR, "missing \"%%}\" in pipeline definition\n");
1575
    return;
1576
  }
1577

1578
  next_char();
1579
}
1580

1581
//------------------------------resource_parse----------------------------
1582
void ADLParser::resource_parse(PipelineForm &pipeline) {
1583
  ResourceForm *resource;
1584
  char * ident;
1585
  char * expr;
1586
  unsigned mask;
1587
  pipeline._rescount = 0;
1588

1589
  skipws();                       // Skip leading whitespace
1590

1591
  if (_curchar != '(') {
1592
    parse_err(SYNERR, "missing \"(\" in resource definition\n");
1593
    return;
1594
  }
1595

1596
  do {
1597
    next_char();                   // Skip "(" or ","
1598
    ident = get_ident();           // Grab next identifier
1599

1600
    if (_AD._adl_debug > 1) {
1601
      if (ident != nullptr) {
1602
        fprintf(stderr, "resource_parse: identifier: %s\n", ident);
1603
      }
1604
    }
1605

1606
    if (ident == nullptr) {
1607
      parse_err(SYNERR, "keyword identifier expected at \"%c\"\n", _curchar);
1608
      return;
1609
    }
1610
    skipws();
1611

1612
    if (_curchar != '=') {
1613
      mask = (1 << pipeline._rescount++);
1614
    }
1615
    else {
1616
      next_char(); skipws();
1617
      expr = get_ident();          // Grab next identifier
1618
      if (expr == nullptr) {
1619
        parse_err(SYNERR, "keyword identifier expected at \"%c\"\n", _curchar);
1620
        return;
1621
      }
1622
      resource = (ResourceForm *) pipeline._resdict[expr];
1623
      if (resource == nullptr) {
1624
        parse_err(SYNERR, "resource \"%s\" is not defined\n", expr);
1625
        return;
1626
      }
1627
      mask = resource->mask();
1628

1629
      skipws();
1630
      while (_curchar == '|') {
1631
        next_char(); skipws();
1632

1633
        expr = get_ident();          // Grab next identifier
1634
        if (expr == nullptr) {
1635
          parse_err(SYNERR, "keyword identifier expected at \"%c\"\n", _curchar);
1636
          return;
1637
        }
1638

1639
        resource = (ResourceForm *) pipeline._resdict[expr];   // Look up the value
1640
        if (resource == nullptr) {
1641
          parse_err(SYNERR, "resource \"%s\" is not defined\n", expr);
1642
          return;
1643
        }
1644

1645
        mask |= resource->mask();
1646
        skipws();
1647
      }
1648
    }
1649

1650
    resource = new ResourceForm(mask);
1651

1652
    pipeline._resdict.Insert(ident, resource);
1653
    pipeline._reslist.addName(ident);
1654
  } while (_curchar == ',');
1655

1656
  if (_curchar != ')') {
1657
      parse_err(SYNERR, "\")\" expected at \"%c\"\n", _curchar);
1658
      return;
1659
  }
1660

1661
  next_char();                 // Skip ")"
1662
  if (_curchar == ';')
1663
    next_char();               // Skip ";"
1664
}
1665

1666
//------------------------------resource_parse----------------------------
1667
void ADLParser::pipe_desc_parse(PipelineForm &pipeline) {
1668
  char * ident;
1669

1670
  skipws();                       // Skip leading whitespace
1671

1672
  if (_curchar != '(') {
1673
    parse_err(SYNERR, "missing \"(\" in pipe_desc definition\n");
1674
    return;
1675
  }
1676

1677
  do {
1678
    next_char();                   // Skip "(" or ","
1679
    ident = get_ident();           // Grab next identifier
1680
    if (ident == nullptr) {
1681
      parse_err(SYNERR, "keyword identifier expected at \"%c\"\n", _curchar);
1682
      return;
1683
    }
1684

1685
    // Add the name to the list
1686
    pipeline._stages.addName(ident);
1687
    pipeline._stagecnt++;
1688

1689
    skipws();
1690
  } while (_curchar == ',');
1691

1692
  if (_curchar != ')') {
1693
      parse_err(SYNERR, "\")\" expected at \"%c\"\n", _curchar);
1694
      return;
1695
  }
1696

1697
  next_char();                     // Skip ")"
1698
  if (_curchar == ';')
1699
    next_char();                   // Skip ";"
1700
}
1701

1702
//------------------------------pipe_class_parse--------------------------
1703
void ADLParser::pipe_class_parse(PipelineForm &pipeline) {
1704
  PipeClassForm *pipe_class;
1705
  char * ident;
1706
  char * stage;
1707
  char * read_or_write;
1708
  int is_write;
1709
  int is_read;
1710
  OperandForm  *oper;
1711

1712
  skipws();                       // Skip leading whitespace
1713

1714
  ident = get_ident();            // Grab next identifier
1715

1716
  if (ident == nullptr) {
1717
    parse_err(SYNERR, "keyword identifier expected at \"%c\"\n", _curchar);
1718
    return;
1719
  }
1720

1721
  // Create a record for the pipe_class
1722
  pipe_class = new PipeClassForm(ident, ++pipeline._classcnt);
1723
  pipeline._classdict.Insert(ident, pipe_class);
1724
  pipeline._classlist.addName(ident);
1725

1726
  // Then get the operands
1727
  skipws();
1728
  if (_curchar != '(') {
1729
    parse_err(SYNERR, "missing \"(\" in pipe_class definition\n");
1730
  }
1731
  // Parse the operand list
1732
  else get_oplist(pipe_class->_parameters, pipe_class->_localNames);
1733
  skipws();                        // Skip leading whitespace
1734
  // Check for block delimiter
1735
  if ( (_curchar != '%')
1736
       || ( next_char(),  (_curchar != '{')) ) {
1737
    parse_err(SYNERR, "missing \"%%{\" in pipe_class definition\n");
1738
    return;
1739
  }
1740
  next_char();
1741

1742
  do {
1743
    ident = get_ident();           // Grab next identifier
1744
    if (ident == nullptr) {
1745
      parse_err(SYNERR, "keyword identifier expected at \"%c\"\n", _curchar);
1746
      continue;
1747
    }
1748
    skipws();
1749

1750
    if (!strcmp(ident, "fixed_latency")) {
1751
      skipws();
1752
      if (_curchar != '(') {
1753
        parse_err(SYNERR, "missing \"(\" in latency definition\n");
1754
        return;
1755
      }
1756
      next_char(); skipws();
1757
      if( !isdigit(_curchar) ) {
1758
        parse_err(SYNERR, "number expected for \"%c\" in latency definition\n", _curchar);
1759
        return;
1760
      }
1761
      int fixed_latency = get_int();
1762
      skipws();
1763
      if (_curchar != ')') {
1764
        parse_err(SYNERR, "missing \")\" in latency definition\n");
1765
        return;
1766
      }
1767
      next_char(); skipws();
1768
      if (_curchar != ';') {
1769
        parse_err(SYNERR, "missing \";\" in latency definition\n");
1770
        return;
1771
      }
1772

1773
      pipe_class->setFixedLatency(fixed_latency);
1774
      next_char(); skipws();
1775
      continue;
1776
    }
1777

1778
    if (!strcmp(ident, "zero_instructions") ||
1779
        !strcmp(ident, "no_instructions")) {
1780
      skipws();
1781
      if (_curchar != ';') {
1782
        parse_err(SYNERR, "missing \";\" in latency definition\n");
1783
        return;
1784
      }
1785

1786
      pipe_class->setInstructionCount(0);
1787
      next_char(); skipws();
1788
      continue;
1789
    }
1790

1791
    if (!strcmp(ident, "one_instruction_with_delay_slot") ||
1792
        !strcmp(ident, "single_instruction_with_delay_slot")) {
1793
      skipws();
1794
      if (_curchar != ';') {
1795
        parse_err(SYNERR, "missing \";\" in latency definition\n");
1796
        return;
1797
      }
1798

1799
      pipe_class->setInstructionCount(1);
1800
      pipe_class->setBranchDelay(true);
1801
      next_char(); skipws();
1802
      continue;
1803
    }
1804

1805
    if (!strcmp(ident, "one_instruction") ||
1806
        !strcmp(ident, "single_instruction")) {
1807
      skipws();
1808
      if (_curchar != ';') {
1809
        parse_err(SYNERR, "missing \";\" in latency definition\n");
1810
        return;
1811
      }
1812

1813
      pipe_class->setInstructionCount(1);
1814
      next_char(); skipws();
1815
      continue;
1816
    }
1817

1818
    if (!strcmp(ident, "instructions_in_first_bundle") ||
1819
        !strcmp(ident, "instruction_count")) {
1820
      skipws();
1821

1822
      int number_of_instructions = 1;
1823

1824
      if (_curchar != '(') {
1825
        parse_err(SYNERR, "\"(\" expected at \"%c\"\n", _curchar);
1826
        continue;
1827
      }
1828

1829
      next_char(); skipws();
1830
      number_of_instructions = get_int();
1831

1832
      skipws();
1833
      if (_curchar != ')') {
1834
        parse_err(SYNERR, "\")\" expected at \"%c\"\n", _curchar);
1835
        continue;
1836
      }
1837

1838
      next_char(); skipws();
1839
      if (_curchar != ';') {
1840
        parse_err(SYNERR, "missing \";\" in latency definition\n");
1841
        return;
1842
      }
1843

1844
      pipe_class->setInstructionCount(number_of_instructions);
1845
      next_char(); skipws();
1846
      continue;
1847
    }
1848

1849
    if (!strcmp(ident, "multiple_bundles")) {
1850
      skipws();
1851
      if (_curchar != ';') {
1852
        parse_err(SYNERR, "missing \";\" after multiple bundles\n");
1853
        return;
1854
      }
1855

1856
      pipe_class->setMultipleBundles(true);
1857
      next_char(); skipws();
1858
      continue;
1859
    }
1860

1861
    if (!strcmp(ident, "has_delay_slot")) {
1862
      skipws();
1863
      if (_curchar != ';') {
1864
        parse_err(SYNERR, "missing \";\" after \"has_delay_slot\"\n");
1865
        return;
1866
      }
1867

1868
      pipe_class->setBranchDelay(true);
1869
      next_char(); skipws();
1870
      continue;
1871
    }
1872

1873
    if (!strcmp(ident, "force_serialization")) {
1874
      skipws();
1875
      if (_curchar != ';') {
1876
        parse_err(SYNERR, "missing \";\" after \"force_serialization\"\n");
1877
        return;
1878
      }
1879

1880
      pipe_class->setForceSerialization(true);
1881
      next_char(); skipws();
1882
      continue;
1883
    }
1884

1885
    if (!strcmp(ident, "may_have_no_code")) {
1886
      skipws();
1887
      if (_curchar != ';') {
1888
        parse_err(SYNERR, "missing \";\" after \"may_have_no_code\"\n");
1889
        return;
1890
      }
1891

1892
      pipe_class->setMayHaveNoCode(true);
1893
      next_char(); skipws();
1894
      continue;
1895
    }
1896

1897
    const Form *parm = pipe_class->_localNames[ident];
1898
    if (parm != nullptr) {
1899
      oper = parm->is_operand();
1900
      if (oper == nullptr && !parm->is_opclass()) {
1901
        parse_err(SYNERR, "operand name expected at %s\n", ident);
1902
        continue;
1903
      }
1904

1905
      if (_curchar != ':') {
1906
        parse_err(SYNERR, "\":\" expected at \"%c\"\n", _curchar);
1907
        continue;
1908
      }
1909
      next_char(); skipws();
1910
      stage = get_ident();
1911
      if (stage == nullptr) {
1912
        parse_err(SYNERR, "pipeline stage identifier expected at \"%c\"\n", _curchar);
1913
        continue;
1914
      }
1915

1916
      skipws();
1917
      if (_curchar != '(') {
1918
        parse_err(SYNERR, "\"(\" expected at \"%c\"\n", _curchar);
1919
        continue;
1920
      }
1921

1922
      next_char();
1923
      read_or_write = get_ident();
1924
      if (read_or_write == nullptr) {
1925
        parse_err(SYNERR, "\"read\" or \"write\" expected at \"%c\"\n", _curchar);
1926
        continue;
1927
      }
1928

1929
      is_read  = strcmp(read_or_write, "read")   == 0;
1930
      is_write = strcmp(read_or_write, "write")  == 0;
1931
      if (!is_read && !is_write) {
1932
        parse_err(SYNERR, "\"read\" or \"write\" expected at \"%c\"\n", _curchar);
1933
        continue;
1934
      }
1935

1936
      skipws();
1937
      if (_curchar != ')') {
1938
        parse_err(SYNERR, "\")\" expected at \"%c\"\n", _curchar);
1939
        continue;
1940
      }
1941

1942
      next_char(); skipws();
1943
      int more_instrs = 0;
1944
      if (_curchar == '+') {
1945
          next_char(); skipws();
1946
          if (_curchar < '0' || _curchar > '9') {
1947
            parse_err(SYNERR, "<number> expected at \"%c\"\n", _curchar);
1948
            continue;
1949
          }
1950
          while (_curchar >= '0' && _curchar <= '9') {
1951
            more_instrs *= 10;
1952
            more_instrs += _curchar - '0';
1953
            next_char();
1954
          }
1955
          skipws();
1956
      }
1957

1958
      PipeClassOperandForm *pipe_operand = new PipeClassOperandForm(stage, is_write, more_instrs);
1959
      pipe_class->_localUsage.Insert(ident, pipe_operand);
1960

1961
      if (_curchar == '%')
1962
          continue;
1963

1964
      if (_curchar != ';') {
1965
        parse_err(SYNERR, "\";\" expected at \"%c\"\n", _curchar);
1966
        continue;
1967
      }
1968
      next_char(); skipws();
1969
      continue;
1970
    }
1971

1972
    // Scan for Resource Specifier
1973
    const Form *res = pipeline._resdict[ident];
1974
    if (res != nullptr) {
1975
      int cyclecnt = 1;
1976
      if (_curchar != ':') {
1977
        parse_err(SYNERR, "\":\" expected at \"%c\"\n", _curchar);
1978
        continue;
1979
      }
1980
      next_char(); skipws();
1981
      stage = get_ident();
1982
      if (stage == nullptr) {
1983
        parse_err(SYNERR, "pipeline stage identifier expected at \"%c\"\n", _curchar);
1984
        continue;
1985
      }
1986

1987
      skipws();
1988
      if (_curchar == '(') {
1989
        next_char();
1990
        cyclecnt = get_int();
1991

1992
        skipws();
1993
        if (_curchar != ')') {
1994
          parse_err(SYNERR, "\")\" expected at \"%c\"\n", _curchar);
1995
          continue;
1996
        }
1997

1998
        next_char(); skipws();
1999
      }
2000

2001
      PipeClassResourceForm *resource = new PipeClassResourceForm(ident, stage, cyclecnt);
2002
      int stagenum = pipeline._stages.index(stage);
2003
      if (pipeline._maxcycleused < (stagenum+cyclecnt))
2004
        pipeline._maxcycleused = (stagenum+cyclecnt);
2005
      pipe_class->_resUsage.addForm(resource);
2006

2007
      if (_curchar == '%')
2008
          continue;
2009

2010
      if (_curchar != ';') {
2011
        parse_err(SYNERR, "\";\" expected at \"%c\"\n", _curchar);
2012
        continue;
2013
      }
2014
      next_char(); skipws();
2015
      continue;
2016
    }
2017

2018
    parse_err(SYNERR, "resource expected at \"%s\"\n", ident);
2019
    return;
2020
  } while(_curchar != '%');
2021

2022
  next_char();
2023
  if (_curchar != '}') {
2024
    parse_err(SYNERR, "missing \"%%}\" in pipe_class definition\n");
2025
    return;
2026
  }
2027

2028
  next_char();
2029
}
2030

2031
//------------------------------peep_parse-------------------------------------
2032
void ADLParser::peep_parse(void) {
2033
  Peephole  *peep;                // Pointer to current peephole rule form
2034
  char      *desc = nullptr;      // String representation of rule
2035

2036
  skipws();                       // Skip leading whitespace
2037

2038
  peep = new Peephole();          // Build new Peephole object
2039
  // Check for open block sequence
2040
  skipws();                       // Skip leading whitespace
2041
  if (_curchar == '%' && *(_ptr+1) == '{') {
2042
    next_char(); next_char();     // Skip "%{"
2043
    skipws();
2044
    while (_curchar != '%' && *(_ptr+1) != '}') {
2045
      char *token = get_ident();
2046
      if (token == nullptr) {
2047
        parse_err(SYNERR, "missing identifier inside peephole rule.\n");
2048
        return;
2049
      }
2050
      // check for legal subsections of peephole rule
2051
      if (strcmp(token,"peeppredicate")==0) {
2052
        peep_predicate_parse(*peep); }
2053
      else if (strcmp(token,"peepmatch")==0) {
2054
        peep_match_parse(*peep); }
2055
      else if (strcmp(token, "peepprocedure")==0) {
2056
        peep_procedure_parse(*peep); }
2057
      else if (strcmp(token,"peepconstraint")==0) {
2058
        peep_constraint_parse(*peep); }
2059
      else if (strcmp(token,"peepreplace")==0) {
2060
        peep_replace_parse(*peep); }
2061
      else {
2062
        parse_err(SYNERR,
2063
            "expected peeppreddicate, peepmatch, peepprocedure, peepconstraint, peepreplace, received %s.\n",
2064
            token);
2065
      }
2066
      skipws();
2067
    }
2068
  }
2069
  else {
2070
    parse_err(SYNERR, "Missing %%{ ... %%} block after peephole keyword.\n");
2071
    return;
2072
  }
2073
  next_char();                    // Skip past '%'
2074
  next_char();                    // Skip past '}'
2075
}
2076

2077
// ******************** Private Level 2 Parse Functions ********************
2078
//------------------------------constraint_parse------------------------------
2079
Constraint *ADLParser::constraint_parse(void) {
2080
  char *func;
2081
  char *arg;
2082

2083
  // Check for constraint expression
2084
  skipws();
2085
  if (_curchar != '(') {
2086
    parse_err(SYNERR, "missing constraint expression, (...)\n");
2087
    return nullptr;
2088
  }
2089
  next_char();                    // Skip past '('
2090

2091
  // Get constraint function
2092
  skipws();
2093
  func = get_ident();
2094
  if (func == nullptr) {
2095
    parse_err(SYNERR, "missing function in constraint expression.\n");
2096
    return nullptr;
2097
  }
2098
  if (strcmp(func,"ALLOC_IN_RC")==0
2099
      || strcmp(func,"IS_R_CLASS")==0) {
2100
    // Check for '(' before argument
2101
    skipws();
2102
    if (_curchar != '(') {
2103
      parse_err(SYNERR, "missing '(' for constraint function's argument.\n");
2104
      return nullptr;
2105
    }
2106
    next_char();
2107

2108
    // Get it's argument
2109
    skipws();
2110
    arg = get_ident();
2111
    if (arg == nullptr) {
2112
      parse_err(SYNERR, "missing argument for constraint function %s\n",func);
2113
      return nullptr;
2114
    }
2115
    // Check for ')' after argument
2116
    skipws();
2117
    if (_curchar != ')') {
2118
      parse_err(SYNERR, "missing ')' after constraint function argument %s\n",arg);
2119
      return nullptr;
2120
    }
2121
    next_char();
2122
  } else {
2123
    parse_err(SYNERR, "Invalid constraint function %s\n",func);
2124
    return nullptr;
2125
  }
2126

2127
  // Check for closing paren and ';'
2128
  skipws();
2129
  if (_curchar != ')') {
2130
    parse_err(SYNERR, "Missing ')' for constraint function %s\n",func);
2131
    return nullptr;
2132
  }
2133
  next_char();
2134
  skipws();
2135
  if (_curchar != ';') {
2136
    parse_err(SYNERR, "Missing ';' after constraint.\n");
2137
    return nullptr;
2138
  }
2139
  next_char();
2140

2141
  // Create new "Constraint"
2142
  Constraint *constraint = new Constraint(func,arg);
2143
  return constraint;
2144
}
2145

2146
//------------------------------constr_parse-----------------------------------
2147
ConstructRule *ADLParser::construct_parse(void) {
2148
  return nullptr;
2149
}
2150

2151

2152
//------------------------------reg_def_parse----------------------------------
2153
void ADLParser::reg_def_parse(void) {
2154
  char *rname;                   // Name of register being defined
2155

2156
  // Get register name
2157
  skipws();                      // Skip whitespace
2158
  rname = get_ident();
2159
  if (rname == nullptr) {
2160
    parse_err(SYNERR, "missing register name after reg_def\n");
2161
    return;
2162
  }
2163

2164
  // Check for definition of register calling convention (save on call, ...),
2165
  // register save type, and register encoding value.
2166
  skipws();
2167
  char *callconv  = nullptr;
2168
  char *c_conv    = nullptr;
2169
  char *idealtype = nullptr;
2170
  char *encoding  = nullptr;
2171
  char *concrete = nullptr;
2172
  if (_curchar == '(') {
2173
    next_char();
2174
    callconv = get_ident();
2175
    // Parse the internal calling convention, must be NS, SOC, SOE, or AS.
2176
    if (callconv == nullptr) {
2177
      parse_err(SYNERR, "missing register calling convention value\n");
2178
      return;
2179
    }
2180
    if(strcmp(callconv, "SOC") && strcmp(callconv,"SOE") &&
2181
       strcmp(callconv, "NS") && strcmp(callconv, "AS")) {
2182
      parse_err(SYNERR, "invalid value for register calling convention\n");
2183
    }
2184
    skipws();
2185
    if (_curchar != ',') {
2186
      parse_err(SYNERR, "missing comma in register definition statement\n");
2187
      return;
2188
    }
2189
    next_char();
2190

2191
    // Parse the native calling convention, must be NS, SOC, SOE, AS
2192
    c_conv = get_ident();
2193
    if (c_conv == nullptr) {
2194
      parse_err(SYNERR, "missing register native calling convention value\n");
2195
      return;
2196
    }
2197
    if(strcmp(c_conv, "SOC") && strcmp(c_conv,"SOE") &&
2198
       strcmp(c_conv, "NS") && strcmp(c_conv, "AS")) {
2199
      parse_err(SYNERR, "invalid value for register calling convention\n");
2200
    }
2201
    skipws();
2202
    if (_curchar != ',') {
2203
      parse_err(SYNERR, "missing comma in register definition statement\n");
2204
      return;
2205
    }
2206
    next_char();
2207
    skipws();
2208

2209
    // Parse the ideal save type
2210
    idealtype = get_ident();
2211
    if (idealtype == nullptr) {
2212
      parse_err(SYNERR, "missing register save type value\n");
2213
      return;
2214
    }
2215
    skipws();
2216
    if (_curchar != ',') {
2217
      parse_err(SYNERR, "missing comma in register definition statement\n");
2218
      return;
2219
    }
2220
    next_char();
2221
    skipws();
2222

2223
    // Parse the encoding value
2224
    encoding = get_expr("encoding", ",");
2225
    if (encoding == nullptr) {
2226
      parse_err(SYNERR, "missing register encoding value\n");
2227
      return;
2228
    }
2229
    trim(encoding);
2230
    if (_curchar != ',') {
2231
      parse_err(SYNERR, "missing comma in register definition statement\n");
2232
      return;
2233
    }
2234
    next_char();
2235
    skipws();
2236
    // Parse the concrete name type
2237
    // concrete = get_ident();
2238
    concrete = get_expr("concrete", ")");
2239
    if (concrete == nullptr) {
2240
      parse_err(SYNERR, "missing vm register name value\n");
2241
      return;
2242
    }
2243

2244
    if (_curchar != ')') {
2245
      parse_err(SYNERR, "missing ')' in register definition statement\n");
2246
      return;
2247
    }
2248
    next_char();
2249
  }
2250

2251
  // Check for closing ';'
2252
  skipws();
2253
  if (_curchar != ';') {
2254
    parse_err(SYNERR, "missing ';' after reg_def\n");
2255
    return;
2256
  }
2257
  next_char();                   // move past ';'
2258

2259
  // Debug Stuff
2260
  if (_AD._adl_debug > 1) {
2261
    fprintf(stderr,"Register Definition: %s ( %s, %s %s )\n", rname,
2262
            (callconv ? callconv : ""), (c_conv ? c_conv : ""), concrete);
2263
  }
2264

2265
  // Record new register definition.
2266
  _AD._register->addRegDef(rname, callconv, c_conv, idealtype, encoding, concrete);
2267
  return;
2268
}
2269

2270
//------------------------------reg_class_parse--------------------------------
2271
void ADLParser::reg_class_parse(void) {
2272
  char *cname;                    // Name of register class being defined
2273

2274
  // Get register class name
2275
  skipws();                       // Skip leading whitespace
2276
  cname = get_ident();
2277
  if (cname == nullptr) {
2278
    parse_err(SYNERR, "missing register class name after 'reg_class'\n");
2279
    return;
2280
  }
2281
  // Debug Stuff
2282
  if (_AD._adl_debug >1) fprintf(stderr,"Register Class: %s\n", cname);
2283

2284
  skipws();
2285
  if (_curchar == '(') {
2286
    // A register list is defined for the register class.
2287
    // Collect registers into a generic RegClass register class.
2288
    RegClass* reg_class = _AD._register->addRegClass<RegClass>(cname);
2289

2290
    next_char();                  // Skip '('
2291
    skipws();
2292
    while (_curchar != ')') {
2293
      char *rname = get_ident();
2294
      if (rname==nullptr) {
2295
        parse_err(SYNERR, "missing identifier inside reg_class list.\n");
2296
        return;
2297
      }
2298
      RegDef *regDef = _AD._register->getRegDef(rname);
2299
      if (!regDef) {
2300
        parse_err(SEMERR, "unknown identifier %s inside reg_class list.\n", rname);
2301
      } else {
2302
        reg_class->addReg(regDef); // add regDef to regClass
2303
      }
2304

2305
      // Check for ',' and position to next token.
2306
      skipws();
2307
      if (_curchar == ',') {
2308
        next_char();              // Skip trailing ','
2309
        skipws();
2310
      }
2311
    }
2312
    next_char();                  // Skip closing ')'
2313
  } else if (_curchar == '%') {
2314
    // A code snippet is defined for the register class.
2315
    // Collect the code snippet into a CodeSnippetRegClass register class.
2316
    CodeSnippetRegClass* reg_class = _AD._register->addRegClass<CodeSnippetRegClass>(cname);
2317
    char *code = find_cpp_block("reg class");
2318
    if (code == nullptr) {
2319
      parse_err(SYNERR, "missing code declaration for reg class.\n");
2320
      return;
2321
    }
2322
    reg_class->set_code_snippet(code);
2323
    return;
2324
  }
2325

2326
  // Check for terminating ';'
2327
  skipws();
2328
  if (_curchar != ';') {
2329
    parse_err(SYNERR, "missing ';' at end of reg_class definition.\n");
2330
    return;
2331
  }
2332
  next_char();                    // Skip trailing ';'
2333

2334
  // Check RegClass size, must be <= 32 registers in class.
2335

2336
  return;
2337
}
2338

2339
//------------------------------reg_class_dynamic_parse------------------------
2340
void ADLParser::reg_class_dynamic_parse(void) {
2341
  char *cname; // Name of dynamic register class being defined
2342

2343
  // Get register class name
2344
  skipws();
2345
  cname = get_ident();
2346
  if (cname == nullptr) {
2347
    parse_err(SYNERR, "missing dynamic register class name after 'reg_class_dynamic'\n");
2348
    return;
2349
  }
2350

2351
  if (_AD._adl_debug > 1) {
2352
    fprintf(stdout, "Dynamic Register Class: %s\n", cname);
2353
  }
2354

2355
  skipws();
2356
  if (_curchar != '(') {
2357
    parse_err(SYNERR, "missing '(' at the beginning of reg_class_dynamic definition\n");
2358
    return;
2359
  }
2360
  next_char();
2361
  skipws();
2362

2363
  // Collect two register classes and the C++ code representing the condition code used to
2364
  // select between the two classes into a ConditionalRegClass register class.
2365
  ConditionalRegClass* reg_class = _AD._register->addRegClass<ConditionalRegClass>(cname);
2366
  int i;
2367
  for (i = 0; i < 2; i++) {
2368
    char* name = get_ident();
2369
    if (name == nullptr) {
2370
      parse_err(SYNERR, "missing class identifier inside reg_class_dynamic list.\n");
2371
      return;
2372
    }
2373
    RegClass* rc = _AD._register->getRegClass(name);
2374
    if (rc == nullptr) {
2375
      parse_err(SEMERR, "unknown identifier %s inside reg_class_dynamic list.\n", name);
2376
    } else {
2377
      reg_class->set_rclass_at_index(i, rc);
2378
    }
2379

2380
    skipws();
2381
    if (_curchar == ',') {
2382
      next_char();
2383
      skipws();
2384
    } else {
2385
      parse_err(SYNERR, "missing separator ',' inside reg_class_dynamic list.\n");
2386
    }
2387
  }
2388

2389
  // Collect the condition code.
2390
  skipws();
2391
  if (_curchar == '%') {
2392
    char* code = find_cpp_block("reg class dynamic");
2393
    if (code == nullptr) {
2394
       parse_err(SYNERR, "missing code declaration for reg_class_dynamic.\n");
2395
       return;
2396
    }
2397
    reg_class->set_condition_code(code);
2398
  } else {
2399
    parse_err(SYNERR, "missing %% at the beginning of code block in reg_class_dynamic definition\n");
2400
    return;
2401
  }
2402

2403
  skipws();
2404
  if (_curchar != ')') {
2405
    parse_err(SYNERR, "missing ')' at the end of reg_class_dynamic definition\n");
2406
    return;
2407
  }
2408
  next_char();
2409

2410
  skipws();
2411
  if (_curchar != ';') {
2412
    parse_err(SYNERR, "missing ';' at the end of reg_class_dynamic definition.\n");
2413
    return;
2414
  }
2415
  next_char();                    // Skip trailing ';'
2416

2417
  return;
2418
}
2419

2420
//------------------------------alloc_class_parse------------------------------
2421
void ADLParser::alloc_class_parse(void) {
2422
  char *name;                     // Name of allocation class being defined
2423

2424
  // Get allocation class name
2425
  skipws();                       // Skip leading whitespace
2426
  name = get_ident();
2427
  if (name == nullptr) {
2428
    parse_err(SYNERR, "missing allocation class name after 'reg_class'\n");
2429
    return;
2430
  }
2431
  // Debug Stuff
2432
  if (_AD._adl_debug >1) fprintf(stderr,"Allocation Class: %s\n", name);
2433

2434
  AllocClass *alloc_class = _AD._register->addAllocClass(name);
2435

2436
  // Collect registers in class
2437
  skipws();
2438
  if (_curchar == '(') {
2439
    next_char();                  // Skip '('
2440
    skipws();
2441
    while (_curchar != ')') {
2442
      char *rname = get_ident();
2443
      if (rname==nullptr) {
2444
        parse_err(SYNERR, "missing identifier inside reg_class list.\n");
2445
        return;
2446
      }
2447
      // Check if name is a RegDef
2448
      RegDef *regDef = _AD._register->getRegDef(rname);
2449
      if (regDef) {
2450
        alloc_class->addReg(regDef);   // add regDef to allocClass
2451
      } else {
2452

2453
        // name must be a RegDef or a RegClass
2454
        parse_err(SYNERR, "name %s should be a previously defined reg_def.\n", rname);
2455
        return;
2456
      }
2457

2458
      // Check for ',' and position to next token.
2459
      skipws();
2460
      if (_curchar == ',') {
2461
        next_char();              // Skip trailing ','
2462
        skipws();
2463
      }
2464
    }
2465
    next_char();                  // Skip closing ')'
2466
  }
2467

2468
  // Check for terminating ';'
2469
  skipws();
2470
  if (_curchar != ';') {
2471
    parse_err(SYNERR, "missing ';' at end of reg_class definition.\n");
2472
    return;
2473
  }
2474
  next_char();                    // Skip trailing ';'
2475

2476
  return;
2477
}
2478

2479
//------------------------------peep_match_child_parse-------------------------
2480
InstructForm *ADLParser::peep_match_child_parse(PeepMatch &match, int parent, int &position, int input){
2481
  char      *token  = nullptr;
2482
  int        lparen = 0;          // keep track of parenthesis nesting depth
2483
  int        rparen = 0;          // position of instruction at this depth
2484
  InstructForm *inst_seen  = nullptr;
2485

2486
  // Walk the match tree,
2487
  // Record <parent, position, instruction name, input position>
2488
  while ( lparen >= rparen ) {
2489
    skipws();
2490
    // Left paren signals start of an input, collect with recursive call
2491
    if (_curchar == '(') {
2492
      ++lparen;
2493
      next_char();
2494
      ( void ) peep_match_child_parse(match, parent, position, rparen);
2495
    }
2496
    // Right paren signals end of an input, may be more
2497
    else if (_curchar == ')') {
2498
      ++rparen;
2499
      if( rparen == lparen ) { // IF rparen matches an lparen I've seen
2500
        next_char();           //    move past ')'
2501
      } else {                 // ELSE leave ')' for parent
2502
        assert( rparen == lparen + 1, "Should only see one extra ')'");
2503
        // if an instruction was not specified for this paren-pair
2504
        if( ! inst_seen ) {   // record signal entry
2505
          match.add_instruction( parent, position, NameList::_signal, input );
2506
          ++position;
2507
        }
2508
        // ++input;   // TEMPORARY
2509
        return inst_seen;
2510
      }
2511
    }
2512
    // if no parens, then check for instruction name
2513
    // This instruction is the parent of a sub-tree
2514
    else if ((token = get_ident_dup()) != nullptr) {
2515
      const Form *form = _AD._globalNames[token];
2516
      if (form) {
2517
        InstructForm *inst = form->is_instruction();
2518
        // Record the first instruction at this level
2519
        if( inst_seen == nullptr ) {
2520
          inst_seen = inst;
2521
        }
2522
        if (inst) {
2523
          match.add_instruction( parent, position, token, input );
2524
          parent = position;
2525
          ++position;
2526
        } else {
2527
          parse_err(SYNERR, "instruction name expected at identifier %s.\n",
2528
                    token);
2529
          return inst_seen;
2530
        }
2531
      }
2532
      else {
2533
        parse_err(SYNERR, "missing identifier in peepmatch rule.\n");
2534
        return nullptr;
2535
      }
2536
    }
2537
    else {
2538
      parse_err(SYNERR, "missing identifier in peepmatch rule.\n");
2539
      return nullptr;
2540
    }
2541

2542
  } // end while
2543

2544
  assert( false, "ShouldNotReachHere();");
2545
  return nullptr;
2546
}
2547

2548
//---------------------------peep-predicate-parse------------------------------
2549
// Syntax for a peeppredicate rule
2550
//
2551
// peeppredicate ( predicate );
2552
//
2553
void ADLParser::peep_predicate_parse(Peephole& peep) {
2554

2555
  skipws();
2556
  char* rule = nullptr;
2557
  if ( (rule = get_paren_expr("pred expression", true)) == nullptr ) {
2558
    parse_err(SYNERR, "incorrect or missing expression for 'peeppredicate'\n");
2559
    return;
2560
  }
2561
  if (_curchar != ';') {
2562
    parse_err(SYNERR, "missing ';' in peeppredicate definition\n");
2563
    return;
2564
  }
2565
  next_char();   // skip ';'
2566
  skipws();
2567

2568
  // Construct PeepPredicate
2569
  PeepPredicate* predicate = new PeepPredicate(rule);
2570
  peep.add_predicate(predicate);
2571
}
2572

2573
//------------------------------peep_match_parse-------------------------------
2574
// Syntax for a peepmatch rule
2575
//
2576
// peepmatch ( root_instr_name [(instruction subtree)] [,(instruction subtree)]* );
2577
//
2578
void ADLParser::peep_match_parse(Peephole &peep) {
2579

2580
  skipws();
2581
  // Check the structure of the rule
2582
  // Check for open paren
2583
  if (_curchar != '(') {
2584
    parse_err(SYNERR, "missing '(' at start of peepmatch rule.\n");
2585
    return;
2586
  }
2587
  next_char();   // skip '('
2588

2589
  // Construct PeepMatch and parse the peepmatch rule.
2590
  PeepMatch *match = new PeepMatch(_ptr);
2591
  int  parent   = -1;                   // parent of root
2592
  int  position = 0;                    // zero-based positions
2593
  int  input    = 0;                    // input position in parent's operands
2594
  InstructForm *root= peep_match_child_parse( *match, parent, position, input);
2595
  if( root == nullptr ) {
2596
    parse_err(SYNERR, "missing instruction-name at start of peepmatch.\n");
2597
    return;
2598
  }
2599

2600
  if( _curchar != ')' ) {
2601
    parse_err(SYNERR, "missing ')' at end of peepmatch.\n");
2602
    return;
2603
  }
2604
  next_char();   // skip ')'
2605

2606
  // Check for closing semicolon
2607
  skipws();
2608
  if( _curchar != ';' ) {
2609
    parse_err(SYNERR, "missing ';' at end of peepmatch.\n");
2610
    return;
2611
  }
2612
  next_char();   // skip ';'
2613

2614
  // Store match into peep, and store peep into instruction
2615
  peep.add_match(match);
2616
  root->append_peephole(&peep);
2617
}
2618

2619
//---------------------------peep-procedure-parse------------------------------
2620
// Syntax for a peepprocedure rule
2621
//
2622
// peeppredicate ( function_name );
2623
//
2624
void ADLParser::peep_procedure_parse(Peephole& peep) {
2625

2626
  skipws();
2627
  // Check for open paren
2628
  if (_curchar != '(') {
2629
    parse_err(SYNERR, "missing '(' at start of peepprocedure rule.\n");
2630
    return;
2631
  }
2632
  next_char();   // skip '('
2633
  skipws();
2634

2635
  char* name = nullptr;
2636
  if ( (name = get_ident_dup()) == nullptr ) {
2637
    parse_err(SYNERR, "incorrect or missing expression for 'peepprocedure'\n");
2638
    return;
2639
  }
2640

2641
  skipws();
2642
  if (_curchar != ')') {
2643
    parse_err(SYNERR, "peepprocedure should contain a single identifier only\n");
2644
    return;
2645
  }
2646
  next_char();   // skip ')'
2647
  if (_curchar != ';') {
2648
    parse_err(SYNERR, "missing ';' in peepprocedure definition\n");
2649
    return;
2650
  }
2651
  next_char();   // skip ';'
2652
  skipws();
2653

2654
  // Construct PeepProcedure
2655
  PeepProcedure* procedure = new PeepProcedure(name);
2656
  peep.add_procedure(procedure);
2657
}
2658

2659
//------------------------------peep_constraint_parse--------------------------
2660
// Syntax for a peepconstraint rule
2661
// A parenthesized list of relations between operands in peepmatch subtree
2662
//
2663
// peepconstraint %{
2664
// (instruction_number.operand_name
2665
//     relational_op
2666
//  instruction_number.operand_name OR register_name
2667
//  [, ...] );
2668
//
2669
// // instruction numbers are zero-based using topological order in peepmatch
2670
//
2671
void ADLParser::peep_constraint_parse(Peephole &peep) {
2672

2673
  skipws();
2674
  // Check the structure of the rule
2675
  // Check for open paren
2676
  if (_curchar != '(') {
2677
    parse_err(SYNERR, "missing '(' at start of peepconstraint rule.\n");
2678
    return;
2679
  }
2680
  else {
2681
    next_char();                  // Skip '('
2682
  }
2683

2684
  // Check for a constraint
2685
  skipws();
2686
  while( _curchar != ')' ) {
2687
    // Get information on the left instruction and its operand
2688
    // left-instructions's number
2689
    int left_inst = get_int();
2690
    // Left-instruction's operand
2691
    skipws();
2692
    if( _curchar != '.' ) {
2693
      parse_err(SYNERR, "missing '.' in peepconstraint after instruction number.\n");
2694
      return;
2695
    }
2696
    next_char();                  // Skip '.'
2697
    char *left_op = get_ident_dup();
2698

2699
    skipws();
2700
    // Collect relational operator
2701
    char *relation = get_relation_dup();
2702

2703
    skipws();
2704
    // Get information on the right instruction and its operand
2705
    int right_inst;        // Right-instruction's number
2706
    if( isdigit(_curchar) ) {
2707
      right_inst = get_int();
2708
      // Right-instruction's operand
2709
      skipws();
2710
      if( _curchar != '.' ) {
2711
        parse_err(SYNERR, "missing '.' in peepconstraint after instruction number.\n");
2712
        return;
2713
      }
2714
      next_char();              // Skip '.'
2715
    } else {
2716
      right_inst = -1;          // Flag as being a register constraint
2717
    }
2718

2719
    char *right_op = get_ident_dup();
2720

2721
    // Construct the next PeepConstraint
2722
    PeepConstraint *constraint = new PeepConstraint( left_inst, left_op,
2723
                                                     relation,
2724
                                                     right_inst, right_op );
2725
    // And append it to the list for this peephole rule
2726
    peep.append_constraint( constraint );
2727

2728
    // Check for another constraint, or end of rule
2729
    skipws();
2730
    if( _curchar == ',' ) {
2731
      next_char();                // Skip ','
2732
      skipws();
2733
    }
2734
    else if( _curchar != ')' ) {
2735
      parse_err(SYNERR, "expected ',' or ')' after peephole constraint.\n");
2736
      return;
2737
    }
2738
  } // end while( processing constraints )
2739
  next_char();                    // Skip ')'
2740

2741
  // Check for terminating ';'
2742
  skipws();
2743
  if (_curchar != ';') {
2744
    parse_err(SYNERR, "missing ';' at end of peepconstraint.\n");
2745
    return;
2746
  }
2747
  next_char();                    // Skip trailing ';'
2748
}
2749

2750

2751
//------------------------------peep_replace_parse-----------------------------
2752
// Syntax for a peepreplace rule
2753
// root instruction name followed by a
2754
// parenthesized list of whitespace separated instruction.operand specifiers
2755
//
2756
// peepreplace ( instr_name  ( [instruction_number.operand_name]* ) );
2757
//
2758
//
2759
void ADLParser::peep_replace_parse(Peephole &peep) {
2760
  int          lparen = 0;        // keep track of parenthesis nesting depth
2761
  int          rparen = 0;        // keep track of parenthesis nesting depth
2762
  int          icount = 0;        // count of instructions in rule for naming
2763
  char        *str    = nullptr;
2764
  char        *token  = nullptr;
2765

2766
  skipws();
2767
  // Check for open paren
2768
  if (_curchar != '(') {
2769
    parse_err(SYNERR, "missing '(' at start of peepreplace rule.\n");
2770
    return;
2771
  }
2772
  else {
2773
    lparen++;
2774
    next_char();
2775
  }
2776

2777
  // Check for root instruction
2778
  char       *inst = get_ident_dup();
2779
  const Form *form = _AD._globalNames[inst];
2780
  if( form == nullptr || form->is_instruction() == nullptr ) {
2781
    parse_err(SYNERR, "Instruction name expected at start of peepreplace.\n");
2782
    return;
2783
  }
2784

2785
  // Store string representation of rule into replace
2786
  PeepReplace *replace = new PeepReplace(str);
2787
  replace->add_instruction( inst );
2788

2789
  skipws();
2790
  // Start of root's operand-list
2791
  if (_curchar != '(') {
2792
    parse_err(SYNERR, "missing '(' at peepreplace root's operand-list.\n");
2793
    return;
2794
  }
2795
  else {
2796
    lparen++;
2797
    next_char();
2798
  }
2799

2800
  skipws();
2801
  // Get the list of operands
2802
  while( _curchar != ')' ) {
2803
    // Get information on an instruction and its operand
2804
    // instructions's number
2805
    int   inst_num = get_int();
2806
    // Left-instruction's operand
2807
    skipws();
2808
    if( _curchar != '.' ) {
2809
      parse_err(SYNERR, "missing '.' in peepreplace after instruction number.\n");
2810
      return;
2811
    }
2812
    next_char();                  // Skip '.'
2813
    char *inst_op = get_ident_dup();
2814
    if( inst_op == nullptr ) {
2815
      parse_err(SYNERR, "missing operand identifier in peepreplace.\n");
2816
      return;
2817
    }
2818

2819
    // Record this operand's position in peepmatch
2820
    replace->add_operand( inst_num, inst_op );
2821
    skipws();
2822
  }
2823

2824
  // Check for the end of operands list
2825
  skipws();
2826
  assert( _curchar == ')', "While loop should have advanced to ')'.");
2827
  next_char();  // Skip ')'
2828

2829
  skipws();
2830
  // Check for end of peepreplace
2831
  if( _curchar != ')' ) {
2832
    parse_err(SYNERR, "missing ')' at end of peepmatch.\n");
2833
    parse_err(SYNERR, "Support one replacement instruction.\n");
2834
    return;
2835
  }
2836
  next_char(); // Skip ')'
2837

2838
  // Check for closing semicolon
2839
  skipws();
2840
  if( _curchar != ';' ) {
2841
    parse_err(SYNERR, "missing ';' at end of peepreplace.\n");
2842
    return;
2843
  }
2844
  next_char();   // skip ';'
2845

2846
  // Store replace into peep
2847
  peep.add_replace( replace );
2848
}
2849

2850
//------------------------------pred_parse-------------------------------------
2851
Predicate *ADLParser::pred_parse(void) {
2852
  Predicate *predicate;           // Predicate class for operand
2853
  char      *rule = nullptr;         // String representation of predicate
2854

2855
  skipws();                       // Skip leading whitespace
2856
  int line = linenum();
2857
  if ( (rule = get_paren_expr("pred expression", true)) == nullptr ) {
2858
    parse_err(SYNERR, "incorrect or missing expression for 'predicate'\n");
2859
    return nullptr;
2860
  }
2861
  // Debug Stuff
2862
  if (_AD._adl_debug > 1) fprintf(stderr,"Predicate: %s\n", rule);
2863
  if (_curchar != ';') {
2864
    parse_err(SYNERR, "missing ';' in predicate definition\n");
2865
    return nullptr;
2866
  }
2867
  next_char();                     // Point after the terminator
2868

2869
  predicate = new Predicate(rule); // Build new predicate object
2870
  skipws();
2871
  return predicate;
2872
}
2873

2874

2875
//------------------------------ins_encode_parse_block-------------------------
2876
// Parse the block form of ins_encode.  See ins_encode_parse for more details
2877
void ADLParser::ins_encode_parse_block(InstructForm& inst) {
2878
  // Create a new encoding name based on the name of the instruction
2879
  // definition, which should be unique.
2880
  const char* prefix = "__ins_encode_";
2881
  const size_t ec_name_size = strlen(inst._ident) + strlen(prefix) + 1;
2882
  char* ec_name = (char*) AdlAllocateHeap(ec_name_size);
2883
  snprintf_checked(ec_name, ec_name_size, "%s%s", prefix, inst._ident);
2884

2885
  assert(_AD._encode->encClass(ec_name) == nullptr, "shouldn't already exist");
2886
  EncClass* encoding = _AD._encode->add_EncClass(ec_name);
2887
  encoding->_linenum = linenum();
2888

2889
  // synthesize the arguments list for the enc_class from the
2890
  // arguments to the instruct definition.
2891
  const char* param = nullptr;
2892
  inst._parameters.reset();
2893
  while ((param = inst._parameters.iter()) != nullptr) {
2894
    OpClassForm* opForm = inst._localNames[param]->is_opclass();
2895
    assert(opForm != nullptr, "sanity");
2896
    encoding->add_parameter(opForm->_ident, param);
2897
  }
2898

2899
  // Parse the following %{ }% block
2900
  ins_encode_parse_block_impl(inst, encoding, ec_name);
2901

2902
  // Build an encoding rule which invokes the encoding rule we just
2903
  // created, passing all arguments that we received.
2904
  InsEncode*   encrule = new InsEncode(); // Encode class for instruction
2905
  NameAndList* params  = encrule->add_encode(ec_name);
2906
  inst._parameters.reset();
2907
  while ((param = inst._parameters.iter()) != nullptr) {
2908
    params->add_entry(param);
2909
  }
2910

2911
  // Check for duplicate ins_encode sections after parsing the block
2912
  // so that parsing can continue and find any other errors.
2913
  if (inst._insencode != nullptr) {
2914
    parse_err(SYNERR, "Multiple ins_encode sections defined\n");
2915
    return;
2916
  }
2917

2918
  // Set encode class of this instruction.
2919
  inst._insencode = encrule;
2920
}
2921

2922

2923
void ADLParser::ins_encode_parse_block_impl(InstructForm& inst, EncClass* encoding, char* ec_name) {
2924
  skipws_no_preproc();              // Skip leading whitespace
2925
  // Prepend location descriptor, for debugging; cf. ADLParser::find_cpp_block
2926
  if (_AD._adlocation_debug) {
2927
    encoding->add_code(get_line_string());
2928
  }
2929

2930
  // Collect the parts of the encode description
2931
  // (1) strings that are passed through to output
2932
  // (2) replacement/substitution variable, preceded by a '$'
2933
  while ((_curchar != '%') && (*(_ptr+1) != '}')) {
2934

2935
    // (1)
2936
    // Check if there is a string to pass through to output
2937
    char *start = _ptr;       // Record start of the next string
2938
    while ((_curchar != '$') && ((_curchar != '%') || (*(_ptr+1) != '}')) ) {
2939
      // If at the start of a comment, skip past it
2940
      if( (_curchar == '/') && ((*(_ptr+1) == '/') || (*(_ptr+1) == '*')) ) {
2941
        skipws_no_preproc();
2942
      } else {
2943
        // ELSE advance to the next character, or start of the next line
2944
        next_char_or_line();
2945
      }
2946
    }
2947
    // If a string was found, terminate it and record in EncClass
2948
    if (start != _ptr) {
2949
      *_ptr = '\0';          // Terminate the string
2950
      encoding->add_code(start);
2951
    }
2952

2953
    // (2)
2954
    // If we are at a replacement variable,
2955
    // copy it and record in EncClass
2956
    if (_curchar == '$') {
2957
      // Found replacement Variable
2958
      char* rep_var = get_rep_var_ident_dup();
2959

2960
      // Add flag to _strings list indicating we should check _rep_vars
2961
      encoding->add_rep_var(rep_var);
2962

2963
      skipws();
2964

2965
      // Check if this instruct is a MachConstantNode.
2966
      if (strcmp(rep_var, "constanttablebase") == 0) {
2967
        // This instruct is a MachConstantNode.
2968
        inst.set_needs_constant_base(true);
2969
        if (strncmp("MachCall", inst.mach_base_class(_globalNames), strlen("MachCall")) != 0 ) {
2970
          inst.set_is_mach_constant(true);
2971
        }
2972

2973
        if (_curchar == '(')  {
2974
          parse_err(SYNERR, "constanttablebase in instruct %s cannot have an argument "
2975
                            "(only constantaddress and constantoffset)", ec_name);
2976
          return;
2977
        }
2978
      }
2979
      else if ((strcmp(rep_var, "constantaddress")   == 0) ||
2980
               (strcmp(rep_var, "constantoffset")    == 0)) {
2981
        // This instruct is a MachConstantNode.
2982
        inst.set_is_mach_constant(true);
2983

2984
        // If the constant keyword has an argument, parse it.
2985
        if (_curchar == '(')  constant_parse(inst);
2986
      }
2987
    }
2988
  } // end while part of format description
2989
  next_char();                      // Skip '%'
2990
  next_char();                      // Skip '}'
2991

2992
  skipws();
2993

2994
  if (_AD._adlocation_debug) {
2995
    encoding->add_code(end_line_marker());
2996
  }
2997

2998
  // Debug Stuff
2999
  if (_AD._adl_debug > 1)  fprintf(stderr, "EncodingClass Form: %s\n", ec_name);
3000
}
3001

3002

3003
//------------------------------ins_encode_parse-------------------------------
3004
// Encode rules have the form
3005
//   ins_encode( encode_class_name(parameter_list), ... );
3006
//
3007
// The "encode_class_name" must be defined in the encode section
3008
// The parameter list contains $names that are locals.
3009
//
3010
// Alternatively it can be written like this:
3011
//
3012
//   ins_encode %{
3013
//      ... // body
3014
//   %}
3015
//
3016
// which synthesizes a new encoding class taking the same arguments as
3017
// the InstructForm, and automatically prefixes the definition with:
3018
//
3019
//    C2_MacroAssembler masm(&cbuf);\n");
3020
//
3021
//  making it more compact to take advantage of the C2_MacroAssembler and
3022
//  placing the assembly closer to it's use by instructions.
3023
void ADLParser::ins_encode_parse(InstructForm& inst) {
3024

3025
  // Parse encode class name
3026
  skipws();                        // Skip whitespace
3027
  if (_curchar != '(') {
3028
    // Check for ins_encode %{ form
3029
    if ((_curchar == '%') && (*(_ptr+1) == '{')) {
3030
      next_char();                      // Skip '%'
3031
      next_char();                      // Skip '{'
3032

3033
      // Parse the block form of ins_encode
3034
      ins_encode_parse_block(inst);
3035
      return;
3036
    }
3037

3038
    parse_err(SYNERR, "missing '%%{' or '(' in ins_encode definition\n");
3039
    return;
3040
  }
3041
  next_char();                     // move past '('
3042
  skipws();
3043

3044
  InsEncode *encrule  = new InsEncode(); // Encode class for instruction
3045
  encrule->_linenum = linenum();
3046
  char      *ec_name  = nullptr;      // String representation of encode rule
3047
  // identifier is optional.
3048
  while (_curchar != ')') {
3049
    ec_name = get_ident();
3050
    if (ec_name == nullptr) {
3051
      parse_err(SYNERR, "Invalid encode class name after 'ins_encode('.\n");
3052
      return;
3053
    }
3054
    // Check that encoding is defined in the encode section
3055
    EncClass *encode_class = _AD._encode->encClass(ec_name);
3056
    if (encode_class == nullptr) {
3057
      // Like to defer checking these till later...
3058
      // parse_err(WARN, "Using an undefined encode class '%s' in 'ins_encode'.\n", ec_name);
3059
    }
3060

3061
    // Get list for encode method's parameters
3062
    NameAndList *params = encrule->add_encode(ec_name);
3063

3064
    // Parse the parameters to this encode method.
3065
    skipws();
3066
    if ( _curchar == '(' ) {
3067
      next_char();                 // move past '(' for parameters
3068

3069
      // Parse the encode method's parameters
3070
      while (_curchar != ')') {
3071
        char *param = get_ident_or_literal_constant("encoding operand");
3072
        if ( param != nullptr ) {
3073

3074
          // Check if this instruct is a MachConstantNode.
3075
          if (strcmp(param, "constanttablebase") == 0) {
3076
            // This instruct is a MachConstantNode.
3077
            inst.set_needs_constant_base(true);
3078
            if (strncmp("MachCall", inst.mach_base_class(_globalNames), strlen("MachCall")) != 0 ) {
3079
              inst.set_is_mach_constant(true);
3080
            }
3081

3082
            if (_curchar == '(')  {
3083
              parse_err(SYNERR, "constanttablebase in instruct %s cannot have an argument "
3084
                        "(only constantaddress and constantoffset)", ec_name);
3085
              return;
3086
            }
3087
          } else {
3088
            // Found a parameter:
3089
            // Check it is a local name, add it to the list, then check for more
3090
            // New: allow hex constants as parameters to an encode method.
3091
            // New: allow parenthesized expressions as parameters.
3092
            // New: allow "primary", "secondary", "tertiary" as parameters.
3093
            // New: allow user-defined register name as parameter
3094
            if ( (inst._localNames[param] == nullptr) &&
3095
                 !ADLParser::is_literal_constant(param) &&
3096
                 (Opcode::as_opcode_type(param) == Opcode::NOT_AN_OPCODE) &&
3097
                 ((_AD._register == nullptr ) || (_AD._register->getRegDef(param) == nullptr)) ) {
3098
              parse_err(SYNERR, "Using non-locally defined parameter %s for encoding %s.\n", param, ec_name);
3099
              return;
3100
            }
3101
          }
3102
          params->add_entry(param);
3103

3104
          skipws();
3105
          if (_curchar == ',' ) {
3106
            // More parameters to come
3107
            next_char();           // move past ',' between parameters
3108
            skipws();              // Skip to next parameter
3109
          }
3110
          else if (_curchar == ')') {
3111
            // Done with parameter list
3112
          }
3113
          else {
3114
            // Only ',' or ')' are valid after a parameter name
3115
            parse_err(SYNERR, "expected ',' or ')' after parameter %s.\n",
3116
                      ec_name);
3117
            return;
3118
          }
3119

3120
        } else {
3121
          skipws();
3122
          // Did not find a parameter
3123
          if (_curchar == ',') {
3124
            parse_err(SYNERR, "Expected encode parameter before ',' in encoding %s.\n", ec_name);
3125
            return;
3126
          }
3127
          if (_curchar != ')') {
3128
            parse_err(SYNERR, "Expected ')' after encode parameters.\n");
3129
            return;
3130
          }
3131
        }
3132
      } // WHILE loop collecting parameters
3133
      next_char();                   // move past ')' at end of parameters
3134
    } // done with parameter list for encoding
3135

3136
    // Check for ',' or ')' after encoding
3137
    skipws();                      // move to character after parameters
3138
    if ( _curchar == ',' ) {
3139
      // Found a ','
3140
      next_char();                 // move past ',' between encode methods
3141
      skipws();
3142
    }
3143
    else if ( _curchar != ')' ) {
3144
      // If not a ',' then only a ')' is allowed
3145
      parse_err(SYNERR, "Expected ')' after encoding %s.\n", ec_name);
3146
      return;
3147
    }
3148

3149
    // Check for ',' separating parameters
3150
    // if ( _curchar != ',' && _curchar != ')' ) {
3151
    //   parse_err(SYNERR, "expected ',' or ')' after encode method inside ins_encode.\n");
3152
    //   return nullptr;
3153
    // }
3154

3155
  } // done parsing ins_encode methods and their parameters
3156
  if (_curchar != ')') {
3157
    parse_err(SYNERR, "Missing ')' at end of ins_encode description.\n");
3158
    return;
3159
  }
3160
  next_char();                     // move past ')'
3161
  skipws();                        // Skip leading whitespace
3162

3163
  if ( _curchar != ';' ) {
3164
    parse_err(SYNERR, "Missing ';' at end of ins_encode.\n");
3165
    return;
3166
  }
3167
  next_char();                     // move past ';'
3168
  skipws();                        // be friendly to oper_parse()
3169

3170
  // Check for duplicate ins_encode sections after parsing the block
3171
  // so that parsing can continue and find any other errors.
3172
  if (inst._insencode != nullptr) {
3173
    parse_err(SYNERR, "Multiple ins_encode sections defined\n");
3174
    return;
3175
  }
3176

3177
  // Debug Stuff
3178
  if (_AD._adl_debug > 1) fprintf(stderr,"Instruction Encode: %s\n", ec_name);
3179

3180
  // Set encode class of this instruction.
3181
  inst._insencode = encrule;
3182
}
3183

3184
//------------------------------postalloc_expand_parse---------------------------
3185
// Encode rules have the form
3186
//   postalloc_expand( encode_class_name(parameter_list) );
3187
//
3188
// The "encode_class_name" must be defined in the encode section.
3189
// The parameter list contains $names that are locals.
3190
//
3191
// This is just a copy of ins_encode_parse without the loop.
3192
void ADLParser::postalloc_expand_parse(InstructForm& inst) {
3193
  inst._is_postalloc_expand = true;
3194

3195
  // Parse encode class name.
3196
  skipws();                        // Skip whitespace.
3197
  if (_curchar != '(') {
3198
    // Check for postalloc_expand %{ form
3199
    if ((_curchar == '%') && (*(_ptr+1) == '{')) {
3200
      next_char();                      // Skip '%'
3201
      next_char();                      // Skip '{'
3202

3203
      // Parse the block form of postalloc_expand
3204
      ins_encode_parse_block(inst);
3205
      return;
3206
    }
3207

3208
    parse_err(SYNERR, "missing '(' in postalloc_expand definition\n");
3209
    return;
3210
  }
3211
  next_char();                     // Move past '('.
3212
  skipws();
3213

3214
  InsEncode *encrule = new InsEncode(); // Encode class for instruction.
3215
  encrule->_linenum = linenum();
3216
  char      *ec_name = nullptr;       // String representation of encode rule.
3217
  // identifier is optional.
3218
  if (_curchar != ')') {
3219
    ec_name = get_ident();
3220
    if (ec_name == nullptr) {
3221
      parse_err(SYNERR, "Invalid postalloc_expand class name after 'postalloc_expand('.\n");
3222
      return;
3223
    }
3224
    // Check that encoding is defined in the encode section.
3225
    EncClass *encode_class = _AD._encode->encClass(ec_name);
3226

3227
    // Get list for encode method's parameters
3228
    NameAndList *params = encrule->add_encode(ec_name);
3229

3230
    // Parse the parameters to this encode method.
3231
    skipws();
3232
    if (_curchar == '(') {
3233
      next_char();                 // Move past '(' for parameters.
3234

3235
      // Parse the encode method's parameters.
3236
      while (_curchar != ')') {
3237
        char *param = get_ident_or_literal_constant("encoding operand");
3238
        if (param != nullptr) {
3239
          // Found a parameter:
3240

3241
          // First check for constant table support.
3242

3243
          // Check if this instruct is a MachConstantNode.
3244
          if (strcmp(param, "constanttablebase") == 0) {
3245
            // This instruct is a MachConstantNode.
3246
            inst.set_needs_constant_base(true);
3247
            if (strncmp("MachCall", inst.mach_base_class(_globalNames), strlen("MachCall")) != 0 ) {
3248
              inst.set_is_mach_constant(true);
3249
            }
3250

3251
            if (_curchar == '(') {
3252
              parse_err(SYNERR, "constanttablebase in instruct %s cannot have an argument "
3253
                        "(only constantaddress and constantoffset)", ec_name);
3254
              return;
3255
            }
3256
          }
3257
          else if ((strcmp(param, "constantaddress") == 0) ||
3258
                   (strcmp(param, "constantoffset")  == 0))  {
3259
            // This instruct is a MachConstantNode.
3260
            inst.set_is_mach_constant(true);
3261

3262
            // If the constant keyword has an argument, parse it.
3263
            if (_curchar == '(') constant_parse(inst);
3264
          }
3265

3266
          // Else check it is a local name, add it to the list, then check for more.
3267
          // New: allow hex constants as parameters to an encode method.
3268
          // New: allow parenthesized expressions as parameters.
3269
          // New: allow "primary", "secondary", "tertiary" as parameters.
3270
          // New: allow user-defined register name as parameter.
3271
          else if ((inst._localNames[param] == nullptr) &&
3272
                   !ADLParser::is_literal_constant(param) &&
3273
                   (Opcode::as_opcode_type(param) == Opcode::NOT_AN_OPCODE) &&
3274
                   ((_AD._register == nullptr) || (_AD._register->getRegDef(param) == nullptr))) {
3275
            parse_err(SYNERR, "Using non-locally defined parameter %s for encoding %s.\n", param, ec_name);
3276
            return;
3277
          }
3278
          params->add_entry(param);
3279

3280
          skipws();
3281
          if (_curchar == ',') {
3282
            // More parameters to come.
3283
            next_char();           // Move past ',' between parameters.
3284
            skipws();              // Skip to next parameter.
3285
          } else if (_curchar == ')') {
3286
            // Done with parameter list
3287
          } else {
3288
            // Only ',' or ')' are valid after a parameter name.
3289
            parse_err(SYNERR, "expected ',' or ')' after parameter %s.\n", ec_name);
3290
            return;
3291
          }
3292

3293
        } else {
3294
          skipws();
3295
          // Did not find a parameter.
3296
          if (_curchar == ',') {
3297
            parse_err(SYNERR, "Expected encode parameter before ',' in postalloc_expand %s.\n", ec_name);
3298
            return;
3299
          }
3300
          if (_curchar != ')') {
3301
            parse_err(SYNERR, "Expected ')' after postalloc_expand parameters.\n");
3302
            return;
3303
          }
3304
        }
3305
      } // WHILE loop collecting parameters.
3306
      next_char();                 // Move past ')' at end of parameters.
3307
    } // Done with parameter list for encoding.
3308

3309
    // Check for ',' or ')' after encoding.
3310
    skipws();                      // Move to character after parameters.
3311
    if (_curchar != ')') {
3312
      // Only a ')' is allowed.
3313
      parse_err(SYNERR, "Expected ')' after postalloc_expand %s.\n", ec_name);
3314
      return;
3315
    }
3316
  } // Done parsing postalloc_expand method and their parameters.
3317
  if (_curchar != ')') {
3318
    parse_err(SYNERR, "Missing ')' at end of postalloc_expand description.\n");
3319
    return;
3320
  }
3321
  next_char();                     // Move past ')'.
3322
  skipws();                        // Skip leading whitespace.
3323

3324
  if (_curchar != ';') {
3325
    parse_err(SYNERR, "Missing ';' at end of postalloc_expand.\n");
3326
    return;
3327
  }
3328
  next_char();                     // Move past ';'.
3329
  skipws();                        // Be friendly to oper_parse().
3330

3331
  // Debug Stuff.
3332
  if (_AD._adl_debug > 1) fprintf(stderr, "Instruction postalloc_expand: %s\n", ec_name);
3333

3334
  // Set encode class of this instruction.
3335
  inst._insencode = encrule;
3336
}
3337

3338

3339
//------------------------------constant_parse---------------------------------
3340
// Parse a constant expression.
3341
void ADLParser::constant_parse(InstructForm& inst) {
3342
  // Create a new encoding name based on the name of the instruction
3343
  // definition, which should be unique.
3344
  const char* prefix = "__constant_";
3345
  const size_t ec_name_size = strlen(inst._ident) + strlen(prefix) + 1;
3346
  char* ec_name = (char*) AdlAllocateHeap(ec_name_size);
3347
  snprintf_checked(ec_name, ec_name_size, "%s%s", prefix, inst._ident);
3348

3349
  assert(_AD._encode->encClass(ec_name) == nullptr, "shouldn't already exist");
3350
  EncClass* encoding = _AD._encode->add_EncClass(ec_name);
3351
  encoding->_linenum = linenum();
3352

3353
  // synthesize the arguments list for the enc_class from the
3354
  // arguments to the instruct definition.
3355
  const char* param = nullptr;
3356
  inst._parameters.reset();
3357
  while ((param = inst._parameters.iter()) != nullptr) {
3358
    OpClassForm* opForm = inst._localNames[param]->is_opclass();
3359
    assert(opForm != nullptr, "sanity");
3360
    encoding->add_parameter(opForm->_ident, param);
3361
  }
3362

3363
  // Parse the following ( ) expression.
3364
  constant_parse_expression(encoding, ec_name);
3365

3366
  // Build an encoding rule which invokes the encoding rule we just
3367
  // created, passing all arguments that we received.
3368
  InsEncode*   encrule = new InsEncode(); // Encode class for instruction
3369
  NameAndList* params  = encrule->add_encode(ec_name);
3370
  inst._parameters.reset();
3371
  while ((param = inst._parameters.iter()) != nullptr) {
3372
    params->add_entry(param);
3373
  }
3374

3375
  // Set encode class of this instruction.
3376
  inst._constant = encrule;
3377
}
3378

3379

3380
//------------------------------constant_parse_expression----------------------
3381
void ADLParser::constant_parse_expression(EncClass* encoding, char* ec_name) {
3382
  skipws();
3383

3384
  // Prepend location descriptor, for debugging; cf. ADLParser::find_cpp_block
3385
  if (_AD._adlocation_debug) {
3386
    encoding->add_code(get_line_string());
3387
  }
3388

3389
  // Start code line.
3390
  encoding->add_code("    _constant = C->output()->constant_table().add");
3391

3392
  // Parse everything in ( ) expression.
3393
  encoding->add_code("(this, ");
3394
  next_char();  // Skip '('
3395
  int parens_depth = 1;
3396

3397
  // Collect the parts of the constant expression.
3398
  // (1) strings that are passed through to output
3399
  // (2) replacement/substitution variable, preceded by a '$'
3400
  while (parens_depth > 0) {
3401
    if (_curchar == '(') {
3402
      parens_depth++;
3403
      encoding->add_code("(");
3404
      next_char_or_line();
3405
    }
3406
    else if (_curchar == ')') {
3407
      parens_depth--;
3408
      if (parens_depth > 0)
3409
        encoding->add_code(")");
3410
      next_char_or_line();
3411
    }
3412
    else {
3413
      // (1)
3414
      // Check if there is a string to pass through to output
3415
      char *start = _ptr;  // Record start of the next string
3416
      while ((_curchar != '$') && (_curchar != '(') && (_curchar != ')')) {
3417
        next_char_or_line();
3418
      }
3419
      // If a string was found, terminate it and record in EncClass
3420
      if (start != _ptr) {
3421
        *_ptr = '\0';  // Terminate the string
3422
        encoding->add_code(start);
3423
      }
3424

3425
      // (2)
3426
      // If we are at a replacement variable, copy it and record in EncClass.
3427
      if (_curchar == '$') {
3428
        // Found replacement Variable
3429
        char* rep_var = get_rep_var_ident_dup();
3430
        encoding->add_rep_var(rep_var);
3431
      }
3432
    }
3433
  }
3434

3435
  // Finish code line.
3436
  encoding->add_code(");");
3437

3438
  if (_AD._adlocation_debug) {
3439
    encoding->add_code(end_line_marker());
3440
  }
3441

3442
  // Debug Stuff
3443
  if (_AD._adl_debug > 1)  fprintf(stderr, "EncodingClass Form: %s\n", ec_name);
3444
}
3445

3446

3447
//------------------------------size_parse-----------------------------------
3448
// Parse a 'size(<expr>)' attribute which specifies the size of the
3449
// emitted instructions in bytes. <expr> can be a C++ expression,
3450
// e.g. a constant.
3451
char* ADLParser::size_parse(InstructForm *instr) {
3452
  char* sizeOfInstr = nullptr;
3453

3454
  // Get value of the instruction's size
3455
  skipws();
3456

3457
  // Parse size
3458
  sizeOfInstr = get_paren_expr("size expression");
3459
  if (sizeOfInstr == nullptr) {
3460
     parse_err(SYNERR, "size of opcode expected at %c\n", _curchar);
3461
     return nullptr;
3462
  }
3463

3464
  skipws();
3465

3466
  // Check for terminator
3467
  if (_curchar != ';') {
3468
    parse_err(SYNERR, "missing ';' in ins_attrib definition\n");
3469
    return nullptr;
3470
  }
3471
  next_char();                     // Advance past the ';'
3472
  skipws();                        // necessary for instr_parse()
3473

3474
  // Debug Stuff
3475
  if (_AD._adl_debug > 1) {
3476
    if (sizeOfInstr != nullptr) {
3477
      fprintf(stderr,"size of opcode: %s\n", sizeOfInstr);
3478
    }
3479
  }
3480

3481
  return sizeOfInstr;
3482
}
3483

3484

3485
//------------------------------opcode_parse-----------------------------------
3486
Opcode * ADLParser::opcode_parse(InstructForm *instr) {
3487
  char *primary   = nullptr;
3488
  char *secondary = nullptr;
3489
  char *tertiary  = nullptr;
3490

3491
  char   *val    = nullptr;
3492
  Opcode *opcode = nullptr;
3493

3494
  // Get value of the instruction's opcode
3495
  skipws();
3496
  if (_curchar != '(') {         // Check for parenthesized operand list
3497
    parse_err(SYNERR, "missing '(' in expand instruction declaration\n");
3498
    return nullptr;
3499
  }
3500
  next_char();                   // skip open paren
3501
  skipws();
3502
  if (_curchar != ')') {
3503
    // Parse primary, secondary, and tertiary opcodes, if provided.
3504
    if ( (primary = get_ident_or_literal_constant("primary opcode")) == nullptr ) {
3505
          parse_err(SYNERR, "primary hex opcode expected at %c\n", _curchar);
3506
        return nullptr;
3507
    }
3508
    skipws();
3509
    if (_curchar == ',') {
3510
      next_char();
3511
      skipws();
3512
      // Parse secondary opcode
3513
      if ( (secondary = get_ident_or_literal_constant("secondary opcode")) == nullptr ) {
3514
        parse_err(SYNERR, "secondary hex opcode expected at %c\n", _curchar);
3515
        return nullptr;
3516
      }
3517
      skipws();
3518
      if (_curchar == ',') {
3519
        next_char();
3520
        skipws();
3521
        // Parse tertiary opcode
3522
        if ( (tertiary = get_ident_or_literal_constant("tertiary opcode")) == nullptr ) {
3523
          parse_err(SYNERR,"tertiary hex opcode expected at %c\n", _curchar);
3524
          return nullptr;
3525
        }
3526
        skipws();
3527
      }
3528
    }
3529
    skipws();
3530
    if (_curchar != ')') {
3531
      parse_err(SYNERR, "Missing ')' in opcode description\n");
3532
      return nullptr;
3533
    }
3534
  }
3535
  next_char();                     // Skip ')'
3536
  skipws();
3537
  // Check for terminator
3538
  if (_curchar != ';') {
3539
    parse_err(SYNERR, "missing ';' in ins_attrib definition\n");
3540
    return nullptr;
3541
  }
3542
  next_char();                     // Advance past the ';'
3543
  skipws();                        // necessary for instr_parse()
3544

3545
  // Debug Stuff
3546
  if (_AD._adl_debug > 1) {
3547
    if (primary   != nullptr) fprintf(stderr,"primary   opcode: %s\n", primary);
3548
    if (secondary != nullptr) fprintf(stderr,"secondary opcode: %s\n", secondary);
3549
    if (tertiary  != nullptr) fprintf(stderr,"tertiary  opcode: %s\n", tertiary);
3550
  }
3551

3552
  // Generate new object and return
3553
  opcode = new Opcode(primary, secondary, tertiary);
3554
  return opcode;
3555
}
3556

3557

3558
//------------------------------interface_parse--------------------------------
3559
Interface *ADLParser::interface_parse(void) {
3560
  char *iface_name  = nullptr;      // Name of interface class being used
3561
  char *iface_code  = nullptr;      // Describe components of this class
3562

3563
  // Get interface class name
3564
  skipws();                       // Skip whitespace
3565
  if (_curchar != '(') {
3566
    parse_err(SYNERR, "Missing '(' at start of interface description.\n");
3567
    return nullptr;
3568
  }
3569
  next_char();                    // move past '('
3570
  skipws();
3571
  iface_name = get_ident();
3572
  if (iface_name == nullptr) {
3573
    parse_err(SYNERR, "missing interface name after 'interface'.\n");
3574
    return nullptr;
3575
  }
3576
  skipws();
3577
  if (_curchar != ')') {
3578
    parse_err(SYNERR, "Missing ')' after name of interface.\n");
3579
    return nullptr;
3580
  }
3581
  next_char();                    // move past ')'
3582

3583
  // Get details of the interface,
3584
  // for the type of interface indicated by iface_name.
3585
  Interface *inter = nullptr;
3586
  skipws();
3587
  if ( _curchar != ';' ) {
3588
    if ( strcmp(iface_name,"MEMORY_INTER") == 0 ) {
3589
      inter = mem_interface_parse();
3590
    }
3591
    else if ( strcmp(iface_name,"COND_INTER") == 0 ) {
3592
      inter = cond_interface_parse();
3593
    }
3594
    // The parse routines consume the "%}"
3595

3596
    // Check for probable extra ';' after defining block.
3597
    if ( _curchar == ';' ) {
3598
      parse_err(SYNERR, "Extra ';' after defining interface block.\n");
3599
      next_char();                // Skip ';'
3600
      return nullptr;
3601
    }
3602
  } else {
3603
    next_char();                  // move past ';'
3604

3605
    // Create appropriate interface object
3606
    if ( strcmp(iface_name,"REG_INTER") == 0 ) {
3607
      inter = new RegInterface();
3608
    }
3609
    else if ( strcmp(iface_name,"CONST_INTER") == 0 ) {
3610
      inter = new ConstInterface();
3611
    }
3612
  }
3613
  skipws();                       // be friendly to oper_parse()
3614
  // Debug Stuff
3615
  if (_AD._adl_debug > 1) fprintf(stderr,"Interface Form: %s\n", iface_name);
3616

3617
  // Create appropriate interface object and return.
3618
  return inter;
3619
}
3620

3621

3622
//------------------------------mem_interface_parse----------------------------
3623
Interface *ADLParser::mem_interface_parse(void) {
3624
  // Fields for MemInterface
3625
  char *base        = nullptr;
3626
  char *index       = nullptr;
3627
  char *scale       = nullptr;
3628
  char *disp        = nullptr;
3629

3630
  if (_curchar != '%') {
3631
    parse_err(SYNERR, "Missing '%%{' for 'interface' block.\n");
3632
    return nullptr;
3633
  }
3634
  next_char();                  // Skip '%'
3635
  if (_curchar != '{') {
3636
    parse_err(SYNERR, "Missing '%%{' for 'interface' block.\n");
3637
    return nullptr;
3638
  }
3639
  next_char();                  // Skip '{'
3640
  skipws();
3641
  do {
3642
    char *field = get_ident();
3643
    if (field == nullptr) {
3644
      parse_err(SYNERR, "Expected keyword, base|index|scale|disp,  or '%%}' ending interface.\n");
3645
      return nullptr;
3646
    }
3647
    if ( strcmp(field,"base") == 0 ) {
3648
      base  = interface_field_parse();
3649
    }
3650
    else if ( strcmp(field,"index") == 0 ) {
3651
      index = interface_field_parse();
3652
    }
3653
    else if ( strcmp(field,"scale") == 0 ) {
3654
      scale = interface_field_parse();
3655
    }
3656
    else if ( strcmp(field,"disp") == 0 ) {
3657
      disp  = interface_field_parse();
3658
    }
3659
    else {
3660
      parse_err(SYNERR, "Expected keyword, base|index|scale|disp,  or '%%}' ending interface.\n");
3661
      return nullptr;
3662
    }
3663
  } while( _curchar != '%' );
3664
  next_char();                  // Skip '%'
3665
  if ( _curchar != '}' ) {
3666
    parse_err(SYNERR, "Missing '%%}' for 'interface' block.\n");
3667
    return nullptr;
3668
  }
3669
  next_char();                  // Skip '}'
3670

3671
  // Construct desired object and return
3672
  Interface *inter = new MemInterface(base, index, scale, disp);
3673
  return inter;
3674
}
3675

3676

3677
//------------------------------cond_interface_parse---------------------------
3678
Interface *ADLParser::cond_interface_parse(void) {
3679
  char *equal;
3680
  char *not_equal;
3681
  char *less;
3682
  char *greater_equal;
3683
  char *less_equal;
3684
  char *greater;
3685
  char *overflow;
3686
  char *no_overflow;
3687
  const char *equal_format = "eq";
3688
  const char *not_equal_format = "ne";
3689
  const char *less_format = "lt";
3690
  const char *greater_equal_format = "ge";
3691
  const char *less_equal_format = "le";
3692
  const char *greater_format = "gt";
3693
  const char *overflow_format = "o";
3694
  const char *no_overflow_format = "no";
3695

3696
  if (_curchar != '%') {
3697
    parse_err(SYNERR, "Missing '%%{' for 'cond_interface' block.\n");
3698
    return nullptr;
3699
  }
3700
  next_char();                  // Skip '%'
3701
  if (_curchar != '{') {
3702
    parse_err(SYNERR, "Missing '%%{' for 'cond_interface' block.\n");
3703
    return nullptr;
3704
  }
3705
  next_char();                  // Skip '{'
3706
  skipws();
3707
  do {
3708
    char *field = get_ident();
3709
    if (field == nullptr) {
3710
      parse_err(SYNERR, "Expected keyword, base|index|scale|disp,  or '%%}' ending interface.\n");
3711
      return nullptr;
3712
    }
3713
    if ( strcmp(field,"equal") == 0 ) {
3714
      equal  = interface_field_parse(&equal_format);
3715
    }
3716
    else if ( strcmp(field,"not_equal") == 0 ) {
3717
      not_equal = interface_field_parse(&not_equal_format);
3718
    }
3719
    else if ( strcmp(field,"less") == 0 ) {
3720
      less = interface_field_parse(&less_format);
3721
    }
3722
    else if ( strcmp(field,"greater_equal") == 0 ) {
3723
      greater_equal  = interface_field_parse(&greater_equal_format);
3724
    }
3725
    else if ( strcmp(field,"less_equal") == 0 ) {
3726
      less_equal = interface_field_parse(&less_equal_format);
3727
    }
3728
    else if ( strcmp(field,"greater") == 0 ) {
3729
      greater = interface_field_parse(&greater_format);
3730
    }
3731
    else if ( strcmp(field,"overflow") == 0 ) {
3732
      overflow = interface_field_parse(&overflow_format);
3733
    }
3734
    else if ( strcmp(field,"no_overflow") == 0 ) {
3735
      no_overflow = interface_field_parse(&no_overflow_format);
3736
    }
3737
    else {
3738
      parse_err(SYNERR, "Expected keyword, base|index|scale|disp,  or '%%}' ending interface.\n");
3739
      return nullptr;
3740
    }
3741
  } while( _curchar != '%' );
3742
  next_char();                  // Skip '%'
3743
  if ( _curchar != '}' ) {
3744
    parse_err(SYNERR, "Missing '%%}' for 'interface' block.\n");
3745
    return nullptr;
3746
  }
3747
  next_char();                  // Skip '}'
3748

3749
  // Construct desired object and return
3750
  Interface *inter = new CondInterface(equal,         equal_format,
3751
                                       not_equal,     not_equal_format,
3752
                                       less,          less_format,
3753
                                       greater_equal, greater_equal_format,
3754
                                       less_equal,    less_equal_format,
3755
                                       greater,       greater_format,
3756
                                       overflow,      overflow_format,
3757
                                       no_overflow,   no_overflow_format);
3758
  return inter;
3759
}
3760

3761

3762
//------------------------------interface_field_parse--------------------------
3763
char *ADLParser::interface_field_parse(const char ** format) {
3764
  char *iface_field = nullptr;
3765

3766
  // Get interface field
3767
  skipws();                      // Skip whitespace
3768
  if (_curchar != '(') {
3769
    parse_err(SYNERR, "Missing '(' at start of interface field.\n");
3770
    return nullptr;
3771
  }
3772
  next_char();                   // move past '('
3773
  skipws();
3774
  if ( _curchar != '0' && _curchar != '$' ) {
3775
    parse_err(SYNERR, "missing or invalid interface field contents.\n");
3776
    return nullptr;
3777
  }
3778
  iface_field = get_rep_var_ident();
3779
  if (iface_field == nullptr) {
3780
    parse_err(SYNERR, "missing or invalid interface field contents.\n");
3781
    return nullptr;
3782
  }
3783
  skipws();
3784
  if (format != nullptr && _curchar == ',') {
3785
    next_char();
3786
    skipws();
3787
    if (_curchar != '"') {
3788
      parse_err(SYNERR, "Missing '\"' in field format .\n");
3789
      return nullptr;
3790
    }
3791
    next_char();
3792
    char *start = _ptr;       // Record start of the next string
3793
    while ((_curchar != '"') && (_curchar != '%') && (_curchar != '\n')) {
3794
      if (_curchar == '\\')  next_char();  // superquote
3795
      if (_curchar == '\n')  parse_err(SYNERR, "newline in string");  // unimplemented!
3796
      next_char();
3797
    }
3798
    if (_curchar != '"') {
3799
      parse_err(SYNERR, "Missing '\"' at end of field format .\n");
3800
      return nullptr;
3801
    }
3802
    // If a string was found, terminate it and record in FormatRule
3803
    if ( start != _ptr ) {
3804
      *_ptr  = '\0';          // Terminate the string
3805
      *format = start;
3806
    }
3807
    next_char();
3808
    skipws();
3809
  }
3810
  if (_curchar != ')') {
3811
    parse_err(SYNERR, "Missing ')' after interface field.\n");
3812
    return nullptr;
3813
  }
3814
  next_char();                   // move past ')'
3815
  skipws();
3816
  if ( _curchar != ';' ) {
3817
    parse_err(SYNERR, "Missing ';' at end of interface field.\n");
3818
    return nullptr;
3819
  }
3820
  next_char();                    // move past ';'
3821
  skipws();                       // be friendly to interface_parse()
3822

3823
  return iface_field;
3824
}
3825

3826

3827
//------------------------------match_parse------------------------------------
3828
MatchRule *ADLParser::match_parse(FormDict &operands) {
3829
  MatchRule *match;               // Match Rule class for instruction/operand
3830
  char      *cnstr = nullptr;     // Code for constructor
3831
  int        depth = 0;           // Counter for matching parentheses
3832
  int        numleaves = 0;       // Counter for number of leaves in rule
3833

3834
  // Parse the match rule tree
3835
  MatchNode *mnode = matchNode_parse(operands, depth, numleaves, true);
3836

3837
  // Either there is a block with a constructor, or a ';' here
3838
  skipws();                       // Skip whitespace
3839
  if ( _curchar == ';' ) {        // Semicolon is valid terminator
3840
    cnstr = nullptr;              // no constructor for this form
3841
    next_char();                  // Move past the ';', replaced with '\0'
3842
  }
3843
  else if ((cnstr = find_cpp_block("match constructor")) == nullptr ) {
3844
    parse_err(SYNERR, "invalid construction of match rule\n"
3845
              "Missing ';' or invalid '%%{' and '%%}' constructor\n");
3846
    return nullptr;               // No MatchRule to return
3847
  }
3848
  if (_AD._adl_debug > 1)
3849
    if (cnstr) fprintf(stderr,"Match Constructor: %s\n", cnstr);
3850
  // Build new MatchRule object
3851
  match = new MatchRule(_AD, mnode, depth, cnstr, numleaves);
3852
  skipws();                       // Skip any trailing whitespace
3853
  return match;                   // Return MatchRule object
3854
}
3855

3856
//------------------------------format_parse-----------------------------------
3857
FormatRule* ADLParser::format_parse(void) {
3858
  char       *desc   = nullptr;
3859
  FormatRule *format = (new FormatRule(desc));
3860

3861
  // Without expression form, MUST have a code block;
3862
  skipws();                       // Skip whitespace
3863
  if ( _curchar == ';' ) {        // Semicolon is valid terminator
3864
    desc  = nullptr;              // no constructor for this form
3865
    next_char();                  // Move past the ';', replaced with '\0'
3866
  }
3867
  else if ( _curchar == '%' && *(_ptr+1) == '{') {
3868
    next_char();                  // Move past the '%'
3869
    next_char();                  // Move past the '{'
3870

3871
    skipws();
3872
    if (_curchar == '$') {
3873
      char* ident = get_rep_var_ident();
3874
      if (strcmp(ident, "$$template") == 0) return template_parse();
3875
      parse_err(SYNERR, "Unknown \"%s\" directive in format", ident);
3876
      return nullptr;
3877
    }
3878
    // Check for the opening '"' inside the format description
3879
    if ( _curchar == '"' ) {
3880
      next_char();              // Move past the initial '"'
3881
      if( _curchar == '"' ) {   // Handle empty format string case
3882
        *_ptr = '\0';           // Terminate empty string
3883
        format->_strings.addName(_ptr);
3884
      }
3885

3886
      // Collect the parts of the format description
3887
      // (1) strings that are passed through to tty->print
3888
      // (2) replacement/substitution variable, preceded by a '$'
3889
      // (3) multi-token ANSIY C style strings
3890
      while ( true ) {
3891
        if ( _curchar == '%' || _curchar == '\n' ) {
3892
          if ( _curchar != '"' ) {
3893
            parse_err(SYNERR, "missing '\"' at end of format block");
3894
            return nullptr;
3895
          }
3896
        }
3897

3898
        // (1)
3899
        // Check if there is a string to pass through to output
3900
        char *start = _ptr;       // Record start of the next string
3901
        while ((_curchar != '$') && (_curchar != '"') && (_curchar != '%') && (_curchar != '\n')) {
3902
          if (_curchar == '\\') {
3903
            next_char();  // superquote
3904
            if ((_curchar == '$') || (_curchar == '%'))
3905
              // hack to avoid % escapes and warnings about undefined \ escapes
3906
              *(_ptr-1) = _curchar;
3907
          }
3908
          if (_curchar == '\n')  parse_err(SYNERR, "newline in string");  // unimplemented!
3909
          next_char();
3910
        }
3911
        // If a string was found, terminate it and record in FormatRule
3912
        if ( start != _ptr ) {
3913
          *_ptr  = '\0';          // Terminate the string
3914
          format->_strings.addName(start);
3915
        }
3916

3917
        // (2)
3918
        // If we are at a replacement variable,
3919
        // copy it and record in FormatRule
3920
        if ( _curchar == '$' ) {
3921
          next_char();          // Move past the '$'
3922
          char* rep_var = get_ident(); // Nil terminate the variable name
3923
          rep_var = strdup(rep_var);// Copy the string
3924
          *_ptr   = _curchar;     // and replace Nil with original character
3925
          format->_rep_vars.addName(rep_var);
3926
          // Add flag to _strings list indicating we should check _rep_vars
3927
          format->_strings.addName(NameList::_signal);
3928
        }
3929

3930
        // (3)
3931
        // Allow very long strings to be broken up,
3932
        // using the ANSI C syntax "foo\n" <newline> "bar"
3933
        if ( _curchar == '"') {
3934
          next_char();           // Move past the '"'
3935
          skipws();              // Skip white space before next string token
3936
          if ( _curchar != '"') {
3937
            break;
3938
          } else {
3939
            // Found one.  Skip both " and the whitespace in between.
3940
            next_char();
3941
          }
3942
        }
3943
      } // end while part of format description
3944

3945
      // Check for closing '"' and '%}' in format description
3946
      skipws();                   // Move to closing '%}'
3947
      if ( _curchar != '%' ) {
3948
        parse_err(SYNERR, "non-blank characters between closing '\"' and '%%' in format");
3949
        return nullptr;
3950
      }
3951
    } // Done with format description inside
3952

3953
    skipws();
3954
    // Past format description, at '%'
3955
    if ( _curchar != '%' || *(_ptr+1) != '}' ) {
3956
      parse_err(SYNERR, "missing '%%}' at end of format block");
3957
      return nullptr;
3958
    }
3959
    next_char();                  // Move past the '%'
3960
    next_char();                  // Move past the '}'
3961
  }
3962
  else {  // parameter list alone must terminate with a ';'
3963
    parse_err(SYNERR, "missing ';' after Format expression");
3964
    return nullptr;
3965
  }
3966
  // Debug Stuff
3967
  if (_AD._adl_debug > 1) fprintf(stderr,"Format Rule: %s\n", desc);
3968

3969
  skipws();
3970
  return format;
3971
}
3972

3973

3974
//------------------------------template_parse-----------------------------------
3975
FormatRule* ADLParser::template_parse(void) {
3976
  char       *desc   = nullptr;
3977
  FormatRule *format = (new FormatRule(desc));
3978

3979
  skipws();
3980
  while ( (_curchar != '%') && (*(_ptr+1) != '}') ) {
3981

3982
    // (1)
3983
    // Check if there is a string to pass through to output
3984
    {
3985
      char *start = _ptr;       // Record start of the next string
3986
      while ((_curchar != '$') && ((_curchar != '%') || (*(_ptr+1) != '}')) ) {
3987
        // If at the start of a comment, skip past it
3988
        if( (_curchar == '/') && ((*(_ptr+1) == '/') || (*(_ptr+1) == '*')) ) {
3989
          skipws_no_preproc();
3990
        } else {
3991
          // ELSE advance to the next character, or start of the next line
3992
          next_char_or_line();
3993
        }
3994
      }
3995
      // If a string was found, terminate it and record in EncClass
3996
      if ( start != _ptr ) {
3997
        *_ptr  = '\0';          // Terminate the string
3998
        // Add flag to _strings list indicating we should check _rep_vars
3999
        format->_strings.addName(NameList::_signal2);
4000
        format->_strings.addName(start);
4001
      }
4002
    }
4003

4004
    // (2)
4005
    // If we are at a replacement variable,
4006
    // copy it and record in EncClass
4007
    if ( _curchar == '$' ) {
4008
      // Found replacement Variable
4009
      char *rep_var = get_rep_var_ident_dup();
4010
      if (strcmp(rep_var, "$emit") == 0) {
4011
        // switch to normal format parsing
4012
        next_char();
4013
        next_char();
4014
        skipws();
4015
        // Check for the opening '"' inside the format description
4016
        if ( _curchar == '"' ) {
4017
          next_char();              // Move past the initial '"'
4018
          if( _curchar == '"' ) {   // Handle empty format string case
4019
            *_ptr = '\0';           // Terminate empty string
4020
            format->_strings.addName(_ptr);
4021
          }
4022

4023
          // Collect the parts of the format description
4024
          // (1) strings that are passed through to tty->print
4025
          // (2) replacement/substitution variable, preceded by a '$'
4026
          // (3) multi-token ANSIY C style strings
4027
          while ( true ) {
4028
            if ( _curchar == '%' || _curchar == '\n' ) {
4029
              parse_err(SYNERR, "missing '\"' at end of format block");
4030
              return nullptr;
4031
            }
4032

4033
            // (1)
4034
            // Check if there is a string to pass through to output
4035
            char *start = _ptr;       // Record start of the next string
4036
            while ((_curchar != '$') && (_curchar != '"') && (_curchar != '%') && (_curchar != '\n')) {
4037
              if (_curchar == '\\')  next_char();  // superquote
4038
              if (_curchar == '\n')  parse_err(SYNERR, "newline in string");  // unimplemented!
4039
              next_char();
4040
            }
4041
            // If a string was found, terminate it and record in FormatRule
4042
            if ( start != _ptr ) {
4043
              *_ptr  = '\0';          // Terminate the string
4044
              format->_strings.addName(start);
4045
            }
4046

4047
            // (2)
4048
            // If we are at a replacement variable,
4049
            // copy it and record in FormatRule
4050
            if ( _curchar == '$' ) {
4051
              next_char();          // Move past the '$'
4052
              char* next_rep_var = get_ident(); // Nil terminate the variable name
4053
              next_rep_var = strdup(next_rep_var);// Copy the string
4054
              *_ptr   = _curchar;     // and replace Nil with original character
4055
              format->_rep_vars.addName(next_rep_var);
4056
              // Add flag to _strings list indicating we should check _rep_vars
4057
              format->_strings.addName(NameList::_signal);
4058
            }
4059

4060
            // (3)
4061
            // Allow very long strings to be broken up,
4062
            // using the ANSI C syntax "foo\n" <newline> "bar"
4063
            if ( _curchar == '"') {
4064
              next_char();           // Move past the '"'
4065
              skipws();              // Skip white space before next string token
4066
              if ( _curchar != '"') {
4067
                break;
4068
              } else {
4069
                // Found one.  Skip both " and the whitespace in between.
4070
                next_char();
4071
              }
4072
            }
4073
          } // end while part of format description
4074
        }
4075
      } else {
4076
        // Add flag to _strings list indicating we should check _rep_vars
4077
        format->_rep_vars.addName(rep_var);
4078
        // Add flag to _strings list indicating we should check _rep_vars
4079
        format->_strings.addName(NameList::_signal3);
4080
      }
4081
    } // end while part of format description
4082
  }
4083

4084
  skipws();
4085
  // Past format description, at '%'
4086
  if ( _curchar != '%' || *(_ptr+1) != '}' ) {
4087
    parse_err(SYNERR, "missing '%%}' at end of format block");
4088
    return nullptr;
4089
  }
4090
  next_char();                  // Move past the '%'
4091
  next_char();                  // Move past the '}'
4092

4093
  // Debug Stuff
4094
  if (_AD._adl_debug > 1) fprintf(stderr,"Format Rule: %s\n", desc);
4095

4096
  skipws();
4097
  return format;
4098
}
4099

4100

4101
//------------------------------effect_parse-----------------------------------
4102
void ADLParser::effect_parse(InstructForm *instr) {
4103
  char* desc   = nullptr;
4104

4105
  skipws();                      // Skip whitespace
4106
  if (_curchar != '(') {
4107
    parse_err(SYNERR, "missing '(' in effect definition\n");
4108
    return;
4109
  }
4110
  // Get list of effect-operand pairs and insert into dictionary
4111
  else get_effectlist(instr->_effects, instr->_localNames, instr->_has_call);
4112

4113
  // Debug Stuff
4114
  if (_AD._adl_debug > 1) fprintf(stderr,"Effect description: %s\n", desc);
4115
  if (_curchar != ';') {
4116
    parse_err(SYNERR, "missing ';' in Effect definition\n");
4117
  }
4118
  next_char();                  // Skip ';'
4119

4120
}
4121

4122
//-------------------------------flag_parse------------------------------------
4123
Flag* ADLParser::flag_parse(InstructForm *instr) {
4124
  char* ident = nullptr;
4125
  Flag* result = nullptr;
4126

4127
  skipws();                      // Skip whitespace
4128
  if (_curchar != '(') {
4129
    parse_err(SYNERR, "missing '(' in flag definition\n");
4130
    return nullptr;
4131
  }
4132
  do {
4133
    next_char();
4134
    skipws();
4135
    if (_curchar == ')') break;
4136

4137
    ident = get_ident();
4138
    if (ident == nullptr) {
4139
      parse_err(SYNERR, "flag name expected at %c\n", _curchar);
4140
      return nullptr;
4141
    }
4142
    Flag* newflag = new Flag(ident);
4143
    if (result == nullptr) result = newflag;
4144
    else result->append_flag(newflag);
4145
    if (_AD._adl_debug > 1) fprintf(stderr, "\tFlag Name: %s\n", ident);
4146
    skipws();
4147
  } while (_curchar == ',');
4148
  if (_curchar != ')') parse_err(SYNERR, "missing ')'\n");
4149
  else {
4150
    next_char();  // set current character position past the close paren
4151
  }
4152

4153
  // Debug Stuff
4154
  if (_curchar != ';') {
4155
    parse_err(SYNERR, "missing ';' in Flag definition\n");
4156
  }
4157
  // Skip ';'
4158
  next_char();
4159
  return result;
4160
}
4161

4162
//------------------------------expand_parse-----------------------------------
4163
ExpandRule* ADLParser::expand_parse(InstructForm *instr) {
4164
  char         *ident, *ident2;
4165
  NameAndList  *instr_and_operands = nullptr;
4166
  ExpandRule   *exp = new ExpandRule();
4167

4168
  // Expand is a block containing an ordered list of operands with initializers,
4169
  // or instructions, each of which has an ordered list of operands.
4170
  // Check for block delimiter
4171
  skipws();                        // Skip leading whitespace
4172
  if ((_curchar != '%')
4173
      || (next_char(), (_curchar != '{')) ) { // If not open block
4174
    parse_err(SYNERR, "missing '%%{' in expand definition\n");
4175
    return(nullptr);
4176
  }
4177
  next_char();                     // Maintain the invariant
4178
  do {
4179
    ident = get_ident();           // Grab next identifier
4180
    if (ident == nullptr) {
4181
      parse_err(SYNERR, "identifier expected at %c\n", _curchar);
4182
      continue;
4183
    }
4184

4185
    // Check whether we should parse an instruction or operand.
4186
    const Form *form = _globalNames[ident];
4187
    bool parse_oper = false;
4188
    bool parse_ins  = false;
4189
    if (form == nullptr) {
4190
      skipws();
4191
      // Check whether this looks like an instruction specification.  If so,
4192
      // just parse the instruction.  The declaration of the instruction is
4193
      // not needed here.
4194
      if (_curchar == '(') parse_ins = true;
4195
    } else if (form->is_instruction()) {
4196
      parse_ins = true;
4197
    } else if (form->is_operand()) {
4198
      parse_oper = true;
4199
    } else {
4200
      parse_err(SYNERR, "instruction/operand name expected at %s\n", ident);
4201
      continue;
4202
    }
4203

4204
    if (parse_oper) {
4205
      // This is a new operand
4206
      OperandForm *oper = form->is_operand();
4207
      if (oper == nullptr) {
4208
        parse_err(SYNERR, "instruction/operand name expected at %s\n", ident);
4209
        continue;
4210
      }
4211
      // Throw the operand on the _newopers list
4212
      skipws();
4213
      ident = get_unique_ident(instr->_localNames,"Operand");
4214
      if (ident == nullptr) {
4215
        parse_err(SYNERR, "identifier expected at %c\n", _curchar);
4216
        continue;
4217
      }
4218
      exp->_newopers.addName(ident);
4219
      // Add new operand to LocalNames
4220
      instr->_localNames.Insert(ident, oper);
4221
      // Grab any constructor code and save as a string
4222
      char *c = nullptr;
4223
      skipws();
4224
      if (_curchar == '%') { // Need a constructor for the operand
4225
        c = find_cpp_block("Operand Constructor");
4226
        if (c == nullptr) {
4227
          parse_err(SYNERR, "Invalid code block for operand constructor\n");
4228
          continue;
4229
        }
4230
        // Add constructor to _newopconst Dict
4231
        exp->_newopconst.Insert(ident, c);
4232
      }
4233
      else if (_curchar != ';') { // If no constructor, need a ;
4234
        parse_err(SYNERR, "Missing ; in expand rule operand declaration\n");
4235
        continue;
4236
      }
4237
      else next_char(); // Skip the ;
4238
      skipws();
4239
    }
4240
    else {
4241
      assert(parse_ins, "sanity");
4242
      // Add instruction to list
4243
      instr_and_operands = new NameAndList(ident);
4244
      // Grab operands, build nameList of them, and then put into dictionary
4245
      skipws();
4246
      if (_curchar != '(') {         // Check for parenthesized operand list
4247
        parse_err(SYNERR, "missing '(' in expand instruction declaration\n");
4248
        continue;
4249
      }
4250
      do {
4251
        next_char();                 // skip open paren & comma characters
4252
        skipws();
4253
        if (_curchar == ')') break;
4254
        ident2 = get_ident();
4255
        skipws();
4256
        if (ident2 == nullptr) {
4257
          parse_err(SYNERR, "identifier expected at %c\n", _curchar);
4258
          continue;
4259
        }                            // Check that you have a valid operand
4260
        const Form *form2 = instr->_localNames[ident2];
4261
        if (!form2) {
4262
          parse_err(SYNERR, "operand name expected at %s\n", ident2);
4263
          continue;
4264
        }
4265
        OperandForm *oper = form2->is_operand();
4266
        if (oper == nullptr && !form2->is_opclass()) {
4267
          parse_err(SYNERR, "operand name expected at %s\n", ident2);
4268
          continue;
4269
        }                            // Add operand to list
4270
        instr_and_operands->add_entry(ident2);
4271
      } while(_curchar == ',');
4272
      if (_curchar != ')') {
4273
        parse_err(SYNERR, "missing ')'in expand instruction declaration\n");
4274
        continue;
4275
      }
4276
      next_char();
4277
      if (_curchar != ';') {
4278
        parse_err(SYNERR, "missing ';'in expand instruction declaration\n");
4279
        continue;
4280
      }
4281
      next_char();
4282

4283
      // Record both instruction name and its operand list
4284
      exp->add_instruction(instr_and_operands);
4285

4286
      skipws();
4287
    }
4288

4289
  } while(_curchar != '%');
4290
  next_char();
4291
  if (_curchar != '}') {
4292
    parse_err(SYNERR, "missing '%%}' in expand rule definition\n");
4293
    return(nullptr);
4294
  }
4295
  next_char();
4296

4297
  // Debug Stuff
4298
  if (_AD._adl_debug > 1) fprintf(stderr,"Expand Rule:\n");
4299

4300
  skipws();
4301
  return (exp);
4302
}
4303

4304
//------------------------------rewrite_parse----------------------------------
4305
RewriteRule* ADLParser::rewrite_parse(void) {
4306
  char* params = nullptr;
4307
  char* desc   = nullptr;
4308

4309

4310
  // This feature targeted for second generation description language.
4311

4312
  skipws();                      // Skip whitespace
4313
  // Get parameters for rewrite
4314
  if ((params = get_paren_expr("rewrite parameters")) == nullptr) {
4315
    parse_err(SYNERR, "missing '(' in rewrite rule\n");
4316
    return nullptr;
4317
  }
4318
  // Debug Stuff
4319
  if (_AD._adl_debug > 1) fprintf(stderr,"Rewrite parameters: %s\n", params);
4320

4321
  // For now, grab entire block;
4322
  skipws();
4323
  if ( (desc = find_cpp_block("rewrite block")) == nullptr ) {
4324
    parse_err(SYNERR, "incorrect or missing block for 'rewrite'.\n");
4325
    return nullptr;
4326
  }
4327
  // Debug Stuff
4328
  if (_AD._adl_debug > 1) fprintf(stderr,"Rewrite Rule: %s\n", desc);
4329

4330
  skipws();
4331
  return (new RewriteRule(params,desc));
4332
}
4333

4334
//------------------------------attr_parse-------------------------------------
4335
Attribute *ADLParser::attr_parse(char* ident) {
4336
  Attribute *attrib;              // Attribute class
4337
  char      *cost = nullptr;      // String representation of cost attribute
4338

4339
  skipws();                       // Skip leading whitespace
4340
  if ( (cost = get_paren_expr("attribute")) == nullptr ) {
4341
    parse_err(SYNERR, "incorrect or missing expression for 'attribute'\n");
4342
    return nullptr;
4343
  }
4344
  // Debug Stuff
4345
  if (_AD._adl_debug > 1) fprintf(stderr,"Attribute: %s\n", cost);
4346
  if (_curchar != ';') {
4347
    parse_err(SYNERR, "missing ';' in attribute definition\n");
4348
    return nullptr;
4349
  }
4350
  next_char();                   // Point after the terminator
4351

4352
  skipws();
4353
  attrib = new Attribute(ident,cost,INS_ATTR); // Build new predicate object
4354
  return attrib;
4355
}
4356

4357

4358
//------------------------------matchNode_parse--------------------------------
4359
MatchNode *ADLParser::matchNode_parse(FormDict &operands, int &depth, int &numleaves, bool atroot) {
4360
  // Count depth of parenthesis nesting for both left and right children
4361
  int   lParens = depth;
4362
  int   rParens = depth;
4363

4364
  // MatchNode objects for left, right, and root of subtree.
4365
  MatchNode *lChild = nullptr;
4366
  MatchNode *rChild = nullptr;
4367
  char      *token;               // Identifier which may be opcode or operand
4368

4369
  // Match expression starts with a '('
4370
  if (cur_char() != '(')
4371
    return nullptr;
4372

4373
  next_char();                    // advance past '('
4374

4375
  // Parse the opcode
4376
  token = get_ident();            // Get identifier, opcode
4377
  if (token == nullptr) {
4378
    parse_err(SYNERR, "missing opcode in match expression\n");
4379
    return nullptr;
4380
  }
4381

4382
  // Take note if we see one of a few special operations - those that are
4383
  // treated differently on different architectures in the sense that on
4384
  // one architecture there is a match rule and on another there isn't (so
4385
  // a call will eventually be generated).
4386

4387
  for (int i = _last_machine_leaf + 1; i < _last_opcode; i++) {
4388
    if (strcmp(token, NodeClassNames[i]) == 0) {
4389
      _AD.has_match_rule(i, true);
4390
    }
4391
  }
4392

4393
  // Lookup the root value in the operands dict to perform substitution
4394
  const char  *result    = nullptr;  // Result type will be filled in later
4395
  const char  *name      = token;    // local name associated with this node
4396
  const char  *operation = token;    // remember valid operation for later
4397
  const Form  *form      = operands[token];
4398
  OpClassForm *opcForm = form ? form->is_opclass() : nullptr;
4399
  if (opcForm != nullptr) {
4400
    // If this token is an entry in the local names table, record its type
4401
    if (!opcForm->ideal_only()) {
4402
      operation = opcForm->_ident;
4403
      result = operation;         // Operands result in their own type
4404
    }
4405
    // Otherwise it is an ideal type, and so, has no local name
4406
    else                        name = nullptr;
4407
  }
4408

4409
  // Parse the operands
4410
  skipws();
4411
  if (cur_char() != ')') {
4412

4413
    // Parse the left child
4414
    if (strcmp(operation,"Set"))
4415
      lChild = matchChild_parse(operands, lParens, numleaves, false);
4416
    else
4417
      lChild = matchChild_parse(operands, lParens, numleaves, true);
4418

4419
    skipws();
4420
    if (cur_char() != ')' ) {
4421
      if(strcmp(operation, "Set"))
4422
        rChild = matchChild_parse(operands,rParens,numleaves,false);
4423
      else
4424
        rChild = matchChild_parse(operands,rParens,numleaves,true);
4425
    }
4426
  }
4427

4428
  // Check for required ')'
4429
  skipws();
4430
  if (cur_char() != ')') {
4431
    parse_err(SYNERR, "missing ')' in match expression\n");
4432
    return nullptr;
4433
  }
4434
  next_char();                    // skip the ')'
4435

4436
  MatchNode* mroot = new MatchNode(_AD,result,name,operation,lChild,rChild);
4437

4438
  // If not the root, reduce this subtree to an internal operand
4439
  if (!atroot) {
4440
    mroot->build_internalop();
4441
  }
4442
  // depth is greater of left and right paths.
4443
  depth = (lParens > rParens) ? lParens : rParens;
4444

4445
  return mroot;
4446
}
4447

4448

4449
//------------------------------matchChild_parse-------------------------------
4450
MatchNode *ADLParser::matchChild_parse(FormDict &operands, int &parens, int &numleaves, bool atroot) {
4451
  MatchNode  *child  = nullptr;
4452
  const char *result = nullptr;
4453
  const char *token  = nullptr;
4454
  const char *opType = nullptr;
4455

4456
  if (cur_char() == '(') {         // child is an operation
4457
    ++parens;
4458
    child = matchNode_parse(operands, parens, numleaves, atroot);
4459
  }
4460
  else {                           // child is an operand
4461
    token = get_ident();
4462
    const Form  *form    = operands[token];
4463
    OpClassForm *opcForm = form ? form->is_opclass() : nullptr;
4464
    if (opcForm != nullptr) {
4465
      opType = opcForm->_ident;
4466
      result = opcForm->_ident;    // an operand's result matches its type
4467
    } else {
4468
      parse_err(SYNERR, "undefined operand %s in match rule\n", token);
4469
      return nullptr;
4470
    }
4471

4472
    if (opType == nullptr) {
4473
      parse_err(SYNERR, "missing type for argument '%s'\n", token);
4474
    }
4475

4476
    child = new MatchNode(_AD, result, token, opType);
4477
    ++numleaves;
4478
  }
4479

4480
  return child;
4481
}
4482

4483

4484

4485
// ******************** Private Utility Functions *************************
4486

4487

4488
char* ADLParser::find_cpp_block(const char* description) {
4489
  char *next;                     // Pointer for finding block delimiters
4490
  char* cppBlock = nullptr;       // Beginning of C++ code block
4491

4492
  if (_curchar == '%') {          // Encoding is a C++ expression
4493
    next_char();
4494
    if (_curchar != '{') {
4495
      parse_err(SYNERR, "missing '{' in %s \n", description);
4496
      return nullptr;
4497
    }
4498
    next_char();                  // Skip block delimiter
4499
    skipws_no_preproc();          // Skip leading whitespace
4500
    cppBlock = _ptr;              // Point to start of expression
4501
    int line = linenum();
4502
    next = _ptr + 1;
4503
    while(((_curchar != '%') || (*next != '}')) && (_curchar != '\0')) {
4504
      next_char_or_line();
4505
      next = _ptr+1;              // Maintain the next pointer
4506
    }                             // Grab string
4507
    if (_curchar == '\0') {
4508
      parse_err(SYNERR, "invalid termination of %s \n", description);
4509
      return nullptr;
4510
    }
4511
    *_ptr = '\0';                 // Terminate string
4512
    _ptr += 2;                    // Skip block delimiter
4513
    _curchar = *_ptr;             // Maintain invariant
4514

4515
    // Prepend location descriptor, for debugging.
4516
    if (_AD._adlocation_debug) {
4517
      char* location = get_line_string(line);
4518
      char* end_loc  = end_line_marker();
4519
      char* result = (char *)AdlAllocateHeap(strlen(location) + strlen(cppBlock) + strlen(end_loc) + 1);
4520
      strcpy(result, location);
4521
      strcat(result, cppBlock);
4522
      strcat(result, end_loc);
4523
      cppBlock = result;
4524
      free(location);
4525
    }
4526
  }
4527

4528
  return cppBlock;
4529
}
4530

4531
// Move to the closing token of the expression we are currently at,
4532
// as defined by stop_chars.  Match parens and quotes.
4533
char* ADLParser::get_expr(const char *desc, const char *stop_chars) {
4534
  char* expr = nullptr;
4535
  int   paren = 0;
4536

4537
  expr = _ptr;
4538
  while (paren > 0 || !strchr(stop_chars, _curchar)) {
4539
    if (_curchar == '(') {        // Down level of nesting
4540
      paren++;                    // Bump the parenthesis counter
4541
      next_char();                // maintain the invariant
4542
    }
4543
    else if (_curchar == ')') {   // Up one level of nesting
4544
      if (paren == 0) {
4545
        // Paren underflow:  We didn't encounter the required stop-char.
4546
        parse_err(SYNERR, "too many )'s, did not find %s after %s\n",
4547
                  stop_chars, desc);
4548
        return nullptr;
4549
      }
4550
      paren--;                    // Drop the parenthesis counter
4551
      next_char();                // Maintain the invariant
4552
    }
4553
    else if (_curchar == '"' || _curchar == '\'') {
4554
      int qchar = _curchar;
4555
      while (true) {
4556
        next_char();
4557
        if (_curchar == qchar) { next_char(); break; }
4558
        if (_curchar == '\\')  next_char();  // superquote
4559
        if (_curchar == '\n' || _curchar == '\0') {
4560
          parse_err(SYNERR, "newline in string in %s\n", desc);
4561
          return nullptr;
4562
        }
4563
      }
4564
    }
4565
    else if (_curchar == '%' && (_ptr[1] == '{' || _ptr[1] == '}')) {
4566
      // Make sure we do not stray into the next ADLC-level form.
4567
      parse_err(SYNERR, "unexpected %%%c in %s\n", _ptr[1], desc);
4568
      return nullptr;
4569
    }
4570
    else if (_curchar == '\0') {
4571
      parse_err(SYNERR, "unexpected EOF in %s\n", desc);
4572
      return nullptr;
4573
    }
4574
    else {
4575
      // Always walk over whitespace, comments, preprocessor directives, etc.
4576
      char* pre_skip_ptr = _ptr;
4577
      skipws();
4578
      // If the parser declined to make progress on whitespace,
4579
      // skip the next character, which is therefore NOT whitespace.
4580
      if (pre_skip_ptr == _ptr) {
4581
        next_char();
4582
      } else if (pre_skip_ptr+strlen(pre_skip_ptr) != _ptr+strlen(_ptr)) {
4583
        parse_err(SYNERR, "unimplemented: preprocessor must not elide subexpression in %s", desc);
4584
      }
4585
    }
4586
  }
4587

4588
  assert(strchr(stop_chars, _curchar), "non-null return must be at stop-char");
4589
  *_ptr = '\0';               // Replace ')' or other stop-char with '\0'
4590
  return expr;
4591
}
4592

4593
// Helper function around get_expr
4594
// Sets _curchar to '(' so that get_paren_expr will search for a matching ')'
4595
char *ADLParser::get_paren_expr(const char *description, bool include_location) {
4596
  int line = linenum();
4597
  if (_curchar != '(')            // Escape if not valid starting position
4598
    return nullptr;
4599
  next_char();                    // Skip the required initial paren.
4600
  char *token2 = get_expr(description, ")");
4601
  if (_curchar == ')')
4602
    next_char();                  // Skip required final paren.
4603
  int junk = 0;
4604
  if (include_location && _AD._adlocation_debug && !is_int_token(token2, junk)) {
4605
    // Prepend location descriptor, for debugging.
4606
    char* location = get_line_string(line);
4607
    char* end_loc  = end_line_marker();
4608
    char* result = (char *)AdlAllocateHeap(strlen(location) + strlen(token2) + strlen(end_loc) + 1);
4609
    strcpy(result, location);
4610
    strcat(result, token2);
4611
    strcat(result, end_loc);
4612
    token2 = result;
4613
    free(location);
4614
  }
4615
  return token2;
4616
}
4617

4618
//------------------------------get_ident_common-------------------------------
4619
// Looks for an identifier in the buffer, and turns it into a null terminated
4620
// string(still inside the file buffer).  Returns a pointer to the string or
4621
// null if some other token is found instead.
4622
char *ADLParser::get_ident_common(bool do_preproc) {
4623
  char c;
4624
  char *start;                    // Pointer to start of token
4625
  char *end;                      // Pointer to end of token
4626

4627
  if (_curline == nullptr) {       // Return null at EOF.
4628
    return nullptr;
4629
  }
4630

4631
  skipws_common(do_preproc);      // Skip whitespace before identifier
4632
  start = end = _ptr;             // Start points at first character
4633
  end--;                          // unwind end by one to prepare for loop
4634
  do {
4635
    end++;                        // Increment end pointer
4636
    c = *end;                     // Grab character to test
4637
  } while ( ((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z'))
4638
            || ((c >= '0') && (c <= '9'))
4639
            || ((c == '_')) || ((c == ':')) || ((c == '#')) );
4640
  if (start == end) {             // We popped out on the first try
4641
    // It can occur that `start' contains the rest of the input file.
4642
    // In this case the output should be truncated.
4643
    if (strlen(start) > 24) {
4644
      char buf[32];
4645
      strncpy(buf, start, 20);
4646
      buf[20] = '\0';
4647
      strcat(buf, "[...]");
4648
      parse_err(SYNERR, "Identifier expected, but found '%s'.", buf);
4649
    } else {
4650
      parse_err(SYNERR, "Identifier expected, but found '%s'.", start);
4651
    }
4652
    start = nullptr;
4653
  }
4654
  else {
4655
    _curchar = c;                 // Save the first character of next token
4656
    *end = '\0';                  // null terminate the string in place
4657
  }
4658
  _ptr = end;                     // Reset _ptr to point to next char after token
4659

4660
  // Make sure we do not try to use #defined identifiers.  If start is
4661
  // nullptr an error was already reported.
4662
  if (do_preproc && start != nullptr) {
4663
    const char* def = _AD.get_preproc_def(start);
4664
    if (def != nullptr && strcmp(def, start)) {
4665
      const char* def1 = def;
4666
      const char* def2 = _AD.get_preproc_def(def1);
4667
      // implement up to 2 levels of #define
4668
      if (def2 != nullptr && strcmp(def2, def1)) {
4669
        def = def2;
4670
        const char* def3 = _AD.get_preproc_def(def2);
4671
        if (def3 != nullptr && strcmp(def3, def2) && strcmp(def3, def1)) {
4672
          parse_err(SYNERR, "unimplemented: using %s defined as %s => %s => %s",
4673
                    start, def1, def2, def3);
4674
        }
4675
      }
4676
      start = strdup(def);
4677
    }
4678
  }
4679

4680
  return start;                   // Pointer to token in filebuf
4681
}
4682

4683
//------------------------------get_ident_dup----------------------------------
4684
// Looks for an identifier in the buffer, and returns a duplicate
4685
// or null if some other token is found instead.
4686
char *ADLParser::get_ident_dup(void) {
4687
  char *ident = get_ident();
4688

4689
  // Duplicate an identifier before returning and restore string.
4690
  if( ident != nullptr ) {
4691
    ident = strdup(ident);  // Copy the string
4692
    *_ptr   = _curchar;         // and replace Nil with original character
4693
  }
4694

4695
  return ident;
4696
}
4697

4698
//----------------------get_ident_or_literal_constant--------------------------
4699
// Looks for an identifier in the buffer, or a parenthesized expression.
4700
char *ADLParser::get_ident_or_literal_constant(const char* description) {
4701
  char* param = nullptr;
4702
  skipws();
4703
  if (_curchar == '(') {
4704
    // Grab a constant expression.
4705
    param = get_paren_expr(description);
4706
    if (param[0] != '(') {
4707
      const size_t buf_size = strlen(param) + 3;
4708
      char* buf = (char*) AdlAllocateHeap(buf_size);
4709
      snprintf_checked(buf, buf_size, "(%s)", param);
4710
      param = buf;
4711
    }
4712
    assert(is_literal_constant(param),
4713
           "expr must be recognizable as a constant");
4714
  } else {
4715
    param = get_ident();
4716
  }
4717
  return param;
4718
}
4719

4720
//------------------------------get_rep_var_ident-----------------------------
4721
// Do NOT duplicate,
4722
// Leave nil terminator in buffer
4723
// Preserve initial '$'(s) in string
4724
char *ADLParser::get_rep_var_ident(void) {
4725
  // Remember starting point
4726
  char *rep_var = _ptr;
4727

4728
  // Check for replacement variable indicator '$' and pass if present
4729
  if ( _curchar == '$' ) {
4730
    next_char();
4731
  }
4732
  // Check for a subfield indicator, a second '$', and pass if present
4733
  if ( _curchar == '$' ) {
4734
    next_char();
4735
  }
4736

4737
  // Check for a control indicator, a third '$':
4738
  if ( _curchar == '$' ) {
4739
    next_char();
4740
  }
4741

4742
  // Check for more than three '$'s in sequence, SYNERR
4743
  if( _curchar == '$' ) {
4744
    parse_err(SYNERR, "Replacement variables and field specifiers can not start with '$$$$'");
4745
    next_char();
4746
    return nullptr;
4747
  }
4748

4749
  // Nil terminate the variable name following the '$'
4750
  char *rep_var_name = get_ident();
4751
  assert( rep_var_name != nullptr,
4752
          "Missing identifier after replacement variable indicator '$'");
4753

4754
  return rep_var;
4755
}
4756

4757

4758

4759
//------------------------------get_rep_var_ident_dup-------------------------
4760
// Return the next replacement variable identifier, skipping first '$'
4761
// given a pointer into a line of the buffer.
4762
// Null terminates string, still inside the file buffer,
4763
// Returns a pointer to a copy of the string, or null on failure
4764
char *ADLParser::get_rep_var_ident_dup(void) {
4765
  if( _curchar != '$' ) return nullptr;
4766

4767
  next_char();                // Move past the '$'
4768
  char *rep_var = _ptr;       // Remember starting point
4769

4770
  // Check for a subfield indicator, a second '$':
4771
  if ( _curchar == '$' ) {
4772
    next_char();
4773
  }
4774

4775
  // Check for a control indicator, a third '$':
4776
  if ( _curchar == '$' ) {
4777
    next_char();
4778
  }
4779

4780
  // Check for more than three '$'s in sequence, SYNERR
4781
  if( _curchar == '$' ) {
4782
    parse_err(SYNERR, "Replacement variables and field specifiers can not start with '$$$$'");
4783
    next_char();
4784
    return nullptr;
4785
  }
4786

4787
  // Nil terminate the variable name following the '$'
4788
  char *rep_var_name = get_ident();
4789
  assert( rep_var_name != nullptr,
4790
          "Missing identifier after replacement variable indicator '$'");
4791
  rep_var = strdup(rep_var);  // Copy the string
4792
  *_ptr   = _curchar;         // and replace Nil with original character
4793

4794
  return rep_var;
4795
}
4796

4797

4798
//------------------------------get_unique_ident------------------------------
4799
// Looks for an identifier in the buffer, terminates it with a null,
4800
// and checks that it is unique
4801
char *ADLParser::get_unique_ident(FormDict& dict, const char* nameDescription){
4802
  char* ident = get_ident();
4803

4804
  if (ident == nullptr) {
4805
    parse_err(SYNERR, "missing %s identifier at %c\n", nameDescription, _curchar);
4806
  }
4807
  else {
4808
    if (dict[ident] != nullptr) {
4809
      parse_err(SYNERR, "duplicate name %s for %s\n", ident, nameDescription);
4810
      ident = nullptr;
4811
    }
4812
  }
4813

4814
  return ident;
4815
}
4816

4817

4818
//------------------------------get_int----------------------------------------
4819
// Looks for a character string integer in the buffer, and turns it into an int
4820
// invokes a parse_err if the next token is not an integer.
4821
// This routine does not leave the integer null-terminated.
4822
int ADLParser::get_int(void) {
4823
  char          c;
4824
  char         *start;            // Pointer to start of token
4825
  char         *end;              // Pointer to end of token
4826
  int           result;           // Storage for integer result
4827

4828
  if (_curline == nullptr) {       // Return null at EOF.
4829
    return 0;
4830
  }
4831

4832
  skipws();                       // Skip whitespace before identifier
4833
  start = end = _ptr;             // Start points at first character
4834
  c = *end;                       // Grab character to test
4835
  while ((c >= '0' && c <= '9') || (c == '-' && end == start)) {
4836
    end++;                        // Increment end pointer
4837
    c = *end;                     // Grab character to test
4838
  }
4839
  if (start == end) {             // We popped out on the first try
4840
    parse_err(SYNERR, "integer expected at %c\n", c);
4841
    result = 0;
4842
  }
4843
  else {
4844
    _curchar = c;                 // Save the first character of next token
4845
    *end = '\0';                  // null terminate the string in place
4846
    result = atoi(start);         // Convert the string to an integer
4847
    *end = _curchar;              // Restore buffer to original condition
4848
  }
4849

4850
  // Reset _ptr to next char after token
4851
  _ptr = end;
4852

4853
  return result;                   // integer
4854
}
4855

4856

4857
//------------------------------get_relation_dup------------------------------
4858
// Looks for a relational operator in the buffer
4859
// invokes a parse_err if the next token is not a relation
4860
// This routine creates a duplicate of the string in the buffer.
4861
char *ADLParser::get_relation_dup(void) {
4862
  char         *result = nullptr; // relational operator being returned
4863

4864
  if (_curline == nullptr) {      // Return null at EOF.
4865
    return  nullptr;
4866
  }
4867

4868
  skipws();                       // Skip whitespace before relation
4869
  char *start = _ptr;             // Store start of relational operator
4870
  char first  = *_ptr;            // the first character
4871
  if( (first == '=') || (first == '!') || (first == '<') || (first == '>') ) {
4872
    next_char();
4873
    char second = *_ptr;          // the second character
4874
    if( second == '=' ) {
4875
      next_char();
4876
      char tmp  = *_ptr;
4877
      *_ptr = '\0';               // null terminate
4878
      result = strdup(start);     // Duplicate the string
4879
      *_ptr = tmp;                // restore buffer
4880
    } else {
4881
      parse_err(SYNERR, "relational operator expected at %s\n", _ptr);
4882
    }
4883
  } else {
4884
    parse_err(SYNERR, "relational operator expected at %s\n", _ptr);
4885
  }
4886

4887
  return result;
4888
}
4889

4890

4891

4892
//------------------------------get_oplist-------------------------------------
4893
// Looks for identifier pairs where first must be the name of an operand, and
4894
// second must be a name unique in the scope of this instruction.  Stores the
4895
// names with a pointer to the OpClassForm of their type in a local name table.
4896
void ADLParser::get_oplist(NameList &parameters, FormDict &operands) {
4897
  OpClassForm *opclass = nullptr;
4898
  char        *ident   = nullptr;
4899

4900
  do {
4901
    next_char();             // skip open paren & comma characters
4902
    skipws();
4903
    if (_curchar == ')') break;
4904

4905
    // Get operand type, and check it against global name table
4906
    ident = get_ident();
4907
    if (ident == nullptr) {
4908
      parse_err(SYNERR, "optype identifier expected at %c\n", _curchar);
4909
      return;
4910
    }
4911
    else {
4912
      const Form  *form = _globalNames[ident];
4913
      if( form == nullptr ) {
4914
        parse_err(SYNERR, "undefined operand type %s\n", ident);
4915
        return;
4916
      }
4917

4918
      // Check for valid operand type
4919
      OpClassForm *opc  = form->is_opclass();
4920
      OperandForm *oper = form->is_operand();
4921
      if((oper == nullptr) && (opc == nullptr)) {
4922
        parse_err(SYNERR, "identifier %s not operand type\n", ident);
4923
        return;
4924
      }
4925
      opclass = opc;
4926
    }
4927
    // Debugging Stuff
4928
    if (_AD._adl_debug > 1) fprintf(stderr, "\tOperand Type: %s\t", ident);
4929

4930
    // Get name of operand and add it to local name table
4931
    if( (ident = get_unique_ident(operands, "operand")) == nullptr) {
4932
      return;
4933
    }
4934
    // Parameter names must not be global names.
4935
    if( _globalNames[ident] != nullptr ) {
4936
         parse_err(SYNERR, "Reuse of global name %s as operand.\n",ident);
4937
         return;
4938
    }
4939
    operands.Insert(ident, opclass);
4940
    parameters.addName(ident);
4941

4942
    // Debugging Stuff
4943
    if (_AD._adl_debug > 1) fprintf(stderr, "\tOperand Name: %s\n", ident);
4944
    skipws();
4945
  } while(_curchar == ',');
4946

4947
  if (_curchar != ')') parse_err(SYNERR, "missing ')'\n");
4948
  else {
4949
    next_char();  // set current character position past the close paren
4950
  }
4951
}
4952

4953

4954
//------------------------------get_effectlist---------------------------------
4955
// Looks for identifier pairs where first must be the name of a pre-defined,
4956
// effect, and the second must be the name of an operand defined in the
4957
// operand list of this instruction.  Stores the names with a pointer to the
4958
// effect form in a local effects table.
4959
void ADLParser::get_effectlist(FormDict &effects, FormDict &operands, bool& has_call) {
4960
  OperandForm *opForm;
4961
  Effect      *eForm;
4962
  char        *ident;
4963

4964
  do {
4965
    next_char();             // skip open paren & comma characters
4966
    skipws();
4967
    if (_curchar == ')') break;
4968

4969
    // Get effect type, and check it against global name table
4970
    ident = get_ident();
4971
    if (ident == nullptr) {
4972
      parse_err(SYNERR, "effect type identifier expected at %c\n", _curchar);
4973
      return;
4974
    }
4975
    else {
4976
      // Check for valid effect type
4977
      const Form *form = _globalNames[ident];
4978
      if( form == nullptr ) {
4979
        parse_err(SYNERR, "undefined effect type %s\n", ident);
4980
        return;
4981
      }
4982
      else {
4983
        if( (eForm = form->is_effect()) == nullptr) {
4984
          parse_err(SYNERR, "identifier %s not effect type\n", ident);
4985
          return;
4986
        }
4987
      }
4988
    }
4989
      // Debugging Stuff
4990
    if (_AD._adl_debug > 1) fprintf(stderr, "\tEffect Type: %s\t", ident);
4991
    skipws();
4992
    if (eForm->is(Component::CALL)) {
4993
      if (_AD._adl_debug > 1) fprintf(stderr, "\n");
4994
      has_call = true;
4995
    } else {
4996
      // Get name of operand and check that it is in the local name table
4997
      if( (ident = get_unique_ident(effects, "effect")) == nullptr) {
4998
        parse_err(SYNERR, "missing operand identifier in effect list\n");
4999
        return;
5000
      }
5001
      const Form *form = operands[ident];
5002
      opForm = form ? form->is_operand() : nullptr;
5003
      if( opForm == nullptr ) {
5004
        if( form && form->is_opclass() ) {
5005
          const char* cname = form->is_opclass()->_ident;
5006
          parse_err(SYNERR, "operand classes are illegal in effect lists (found %s %s)\n", cname, ident);
5007
        } else {
5008
          parse_err(SYNERR, "undefined operand %s in effect list\n", ident);
5009
        }
5010
        return;
5011
      }
5012
      // Add the pair to the effects table
5013
      effects.Insert(ident, eForm);
5014
      // Debugging Stuff
5015
      if (_AD._adl_debug > 1) fprintf(stderr, "\tOperand Name: %s\n", ident);
5016
    }
5017
    skipws();
5018
  } while(_curchar == ',');
5019

5020
  if (_curchar != ')') parse_err(SYNERR, "missing ')'\n");
5021
  else {
5022
    next_char();  // set current character position past the close paren
5023
  }
5024
}
5025

5026

5027
//-------------------------------preproc_line----------------------------------
5028
// A "#line" keyword has been seen, so parse the rest of the line.
5029
void ADLParser::preproc_line(void) {
5030
  int line = get_int();
5031
  skipws_no_preproc();
5032
  const char* file = nullptr;
5033
  if (_curchar == '"') {
5034
    next_char();              // Move past the initial '"'
5035
    file = _ptr;
5036
    while (true) {
5037
      if (_curchar == '\n') {
5038
        parse_err(SYNERR, "missing '\"' at end of #line directive");
5039
        return;
5040
      }
5041
      if (_curchar == '"') {
5042
        *_ptr  = '\0';          // Terminate the string
5043
        next_char();
5044
        skipws_no_preproc();
5045
        break;
5046
      }
5047
      next_char();
5048
    }
5049
  }
5050
  ensure_end_of_line();
5051
  if (file != nullptr)
5052
    _AD._ADL_file._name = file;
5053
  _buf.set_linenum(line);
5054
}
5055

5056
//------------------------------preproc_define---------------------------------
5057
// A "#define" keyword has been seen, so parse the rest of the line.
5058
void ADLParser::preproc_define(void) {
5059
  char* flag = get_ident_no_preproc();
5060
  skipws_no_preproc();
5061
  // only #define x y is supported for now
5062
  char* def = get_ident_no_preproc();
5063
  _AD.set_preproc_def(flag, def);
5064
  skipws_no_preproc();
5065
  if (_curchar != '\n') {
5066
    parse_err(SYNERR, "non-identifier in preprocessor definition\n");
5067
  }
5068
}
5069

5070
//------------------------------preproc_undef----------------------------------
5071
// An "#undef" keyword has been seen, so parse the rest of the line.
5072
void ADLParser::preproc_undef(void) {
5073
  char* flag = get_ident_no_preproc();
5074
  skipws_no_preproc();
5075
  ensure_end_of_line();
5076
  _AD.set_preproc_def(flag, nullptr);
5077
}
5078

5079

5080

5081
//------------------------------parse_err--------------------------------------
5082
// Issue a parser error message, and skip to the end of the current line
5083
void ADLParser::parse_err(int flag, const char *fmt, ...) {
5084
  va_list args;
5085

5086
  va_start(args, fmt);
5087
  if (flag == 1)
5088
    _AD._syntax_errs += _AD.emit_msg(0, flag, linenum(), fmt, args);
5089
  else if (flag == 2)
5090
    _AD._semantic_errs += _AD.emit_msg(0, flag, linenum(), fmt, args);
5091
  else
5092
    _AD._warnings += _AD.emit_msg(0, flag, linenum(), fmt, args);
5093

5094
  int error_char = _curchar;
5095
  char* error_ptr = _ptr+1;
5096
  for(;*_ptr != '\n'; _ptr++) ; // Skip to the end of the current line
5097
  _curchar = '\n';
5098
  va_end(args);
5099
  _AD._no_output = 1;
5100

5101
  if (flag == 1) {
5102
    char* error_tail = strchr(error_ptr, '\n');
5103
    char tem = *error_ptr;
5104
    error_ptr[-1] = '\0';
5105
    char* error_head = error_ptr-1;
5106
    while (error_head > _curline && *error_head)  --error_head;
5107
    if (error_tail)  *error_tail = '\0';
5108
    fprintf(stderr, "Error Context:  %s>>>%c<<<%s\n",
5109
            error_head, error_char, error_ptr);
5110
    if (error_tail)  *error_tail = '\n';
5111
    error_ptr[-1] = tem;
5112
  }
5113
}
5114

5115
//---------------------------ensure_start_of_line------------------------------
5116
// A preprocessor directive has been encountered.  Be sure it has fallen at
5117
// the beginning of a line, or else report an error.
5118
void ADLParser::ensure_start_of_line(void) {
5119
  if (_curchar == '\n') { next_line(); return; }
5120
  assert( _ptr >= _curline && _ptr < _curline+strlen(_curline),
5121
          "Must be able to find which line we are in" );
5122

5123
  for (char *s = _curline; s < _ptr; s++) {
5124
    if (*s > ' ') {
5125
      parse_err(SYNERR, "'%c' must be at beginning of line\n", _curchar);
5126
      break;
5127
    }
5128
  }
5129
}
5130

5131
//---------------------------ensure_end_of_line--------------------------------
5132
// A preprocessor directive has been parsed.  Be sure there is no trailing
5133
// garbage at the end of this line.  Set the scan point to the beginning of
5134
// the next line.
5135
void ADLParser::ensure_end_of_line(void) {
5136
  skipws_no_preproc();
5137
  if (_curchar != '\n' && _curchar != '\0') {
5138
    parse_err(SYNERR, "garbage char '%c' at end of line\n", _curchar);
5139
  } else {
5140
    next_char_or_line();
5141
  }
5142
}
5143

5144
//---------------------------handle_preproc------------------------------------
5145
// The '#' character introducing a preprocessor directive has been found.
5146
// Parse the whole directive name (e.g., #define, #endif) and take appropriate
5147
// action.  If we are in an "untaken" span of text, simply keep track of
5148
// #ifdef nesting structure, so we can find out when to start taking text
5149
// again.  (In this state, we "sort of support" C's #if directives, enough
5150
// to disregard their associated #else and #endif lines.)  If we are in a
5151
// "taken" span of text, there are two cases:  "#define" and "#undef"
5152
// directives are preserved and passed up to the caller, which eventually
5153
// passes control to the top-level parser loop, which handles #define and
5154
// #undef directly.  (This prevents these directives from occurring in
5155
// arbitrary positions in the AD file--we require better structure than C.)
5156
// In the other case, and #ifdef, #ifndef, #else, or #endif is silently
5157
// processed as whitespace, with the "taken" state of the text correctly
5158
// updated.  This routine returns "false" exactly in the case of a "taken"
5159
// #define or #undef, which tells the caller that a preprocessor token
5160
// has appeared which must be handled explicitly by the parse loop.
5161
bool ADLParser::handle_preproc_token() {
5162
  assert(*_ptr == '#', "must be at start of preproc");
5163
  ensure_start_of_line();
5164
  next_char();
5165
  skipws_no_preproc();
5166
  char* start_ident = _ptr;
5167
  char* ident = (_curchar == '\n') ? nullptr : get_ident_no_preproc();
5168
  if (ident == nullptr) {
5169
    parse_err(SYNERR, "expected preprocessor command, got end of line\n");
5170
  } else if (!strcmp(ident, "ifdef") ||
5171
             !strcmp(ident, "ifndef")) {
5172
    char* flag = get_ident_no_preproc();
5173
    ensure_end_of_line();
5174
    // Test the identifier only if we are already in taken code:
5175
    bool flag_def  = preproc_taken() && (_AD.get_preproc_def(flag) != nullptr);
5176
    bool now_taken = !strcmp(ident, "ifdef") ? flag_def : !flag_def;
5177
    begin_if_def(now_taken);
5178
  } else if (!strcmp(ident, "if")) {
5179
    if (preproc_taken())
5180
      parse_err(SYNERR, "unimplemented: #%s %s", ident, _ptr+1);
5181
    next_line();
5182
    // Intelligently skip this nested C preprocessor directive:
5183
    begin_if_def(true);
5184
  } else if (!strcmp(ident, "else")) {
5185
    ensure_end_of_line();
5186
    invert_if_def();
5187
  } else if (!strcmp(ident, "endif")) {
5188
    ensure_end_of_line();
5189
    end_if_def();
5190
  } else if (preproc_taken()) {
5191
    // pass this token up to the main parser as "#define" or "#undef"
5192
    _ptr = start_ident;
5193
    _curchar = *--_ptr;
5194
    if( _curchar != '#' ) {
5195
      parse_err(SYNERR, "no space allowed after # in #define or #undef");
5196
      assert(_curchar == '#', "no space allowed after # in #define or #undef");
5197
    }
5198
    return false;
5199
  }
5200
  return true;
5201
}
5202

5203
//---------------------------skipws_common-------------------------------------
5204
// Skip whitespace, including comments and newlines, while keeping an accurate
5205
// line count.
5206
// Maybe handle certain preprocessor constructs: #ifdef, #ifndef, #else, #endif
5207
void ADLParser::skipws_common(bool do_preproc) {
5208
  char *start = _ptr;
5209
  char *next = _ptr + 1;
5210

5211
  if (*_ptr == '\0') {
5212
    // Check for string terminator
5213
    if (_curchar > ' ')  return;
5214
    if (_curchar == '\n') {
5215
      if (!do_preproc)  return;            // let caller handle the newline
5216
      next_line();
5217
      _ptr = _curline; next = _ptr + 1;
5218
    }
5219
    else if (_curchar == '#' ||
5220
        (_curchar == '/' && (*next == '/' || *next == '*'))) {
5221
      parse_err(SYNERR, "unimplemented: comment token in a funny place");
5222
    }
5223
  }
5224
  while(_curline != nullptr) {             // Check for end of file
5225
    if (*_ptr == '\n') {                   // keep proper track of new lines
5226
      if (!do_preproc)  break;             // let caller handle the newline
5227
      next_line();
5228
      _ptr = _curline; if (_ptr != nullptr) next = _ptr + 1;
5229
    }
5230
    else if ((*_ptr == '/') && (*next == '/'))      // C++ comment
5231
      do { _ptr++; next++; } while(*_ptr != '\n');  // So go to end of line
5232
    else if ((*_ptr == '/') && (*next == '*')) {    // C comment
5233
      _ptr++; next++;
5234
      do {
5235
        _ptr++; next++;
5236
        if (*_ptr == '\n') {               // keep proper track of new lines
5237
          next_line();                     // skip newlines within comments
5238
          if (_curline == nullptr) {       // check for end of file
5239
            parse_err(SYNERR, "end-of-file detected inside comment\n");
5240
            break;
5241
          }
5242
          _ptr = _curline; next = _ptr + 1;
5243
        }
5244
      } while(!((*_ptr == '*') && (*next == '/'))); // Go to end of comment
5245
      _ptr = ++next; next++;               // increment _ptr past comment end
5246
    }
5247
    else if (do_preproc && *_ptr == '#') {
5248
      // Note that this calls skipws_common(false) recursively!
5249
      bool preproc_handled = handle_preproc_token();
5250
      if (!preproc_handled) {
5251
        if (preproc_taken()) {
5252
          return;  // short circuit
5253
        }
5254
        ++_ptr;    // skip the preprocessor character
5255
      }
5256
      next = _ptr+1;
5257
    } else if(*_ptr > ' ' && !(do_preproc && !preproc_taken())) {
5258
      break;
5259
    }
5260
    else if (*_ptr == '"' || *_ptr == '\'') {
5261
      assert(do_preproc, "only skip strings if doing preproc");
5262
      // skip untaken quoted string
5263
      int qchar = *_ptr;
5264
      while (true) {
5265
        ++_ptr;
5266
        if (*_ptr == qchar) { ++_ptr; break; }
5267
        if (*_ptr == '\\')  ++_ptr;
5268
        if (*_ptr == '\n' || *_ptr == '\0') {
5269
          parse_err(SYNERR, "newline in string");
5270
          break;
5271
        }
5272
      }
5273
      next = _ptr + 1;
5274
    }
5275
    else { ++_ptr; ++next; }
5276
  }
5277
  if (_curline != nullptr) {        // at end of file _curchar isn't valid
5278
    _curchar = *_ptr;               // reset _curchar to maintain invariant
5279
  }
5280
}
5281

5282
//---------------------------cur_char-----------------------------------------
5283
char ADLParser::cur_char() {
5284
  return (_curchar);
5285
}
5286

5287
//---------------------------next_char-----------------------------------------
5288
void ADLParser::next_char() {
5289
  if (_curchar == '\n')  parse_err(WARN, "must call next_line!");
5290
  _curchar = *++_ptr;
5291
  // if ( _curchar == '\n' ) {
5292
  //   next_line();
5293
  // }
5294
}
5295

5296
//---------------------------next_char_or_line---------------------------------
5297
void ADLParser::next_char_or_line() {
5298
  if ( _curchar != '\n' ) {
5299
    _curchar = *++_ptr;
5300
  } else {
5301
    next_line();
5302
    _ptr = _curline;
5303
    _curchar = *_ptr;  // maintain invariant
5304
  }
5305
}
5306

5307
//---------------------------next_line-----------------------------------------
5308
void ADLParser::next_line() {
5309
  _curline = _buf.get_line();
5310
  _curchar = ' ';
5311
}
5312

5313
//------------------------get_line_string--------------------------------------
5314
// Prepended location descriptor, for debugging.
5315
// Must return a malloced string (that can be freed if desired).
5316
char* ADLParser::get_line_string(int linenum) {
5317
  const char* file = _AD._ADL_file._name;
5318
  int         line = linenum ? linenum : this->linenum();
5319
  const size_t location_size = strlen(file) + 100;
5320
  char* location = (char *)AdlAllocateHeap(location_size);
5321
  snprintf_checked(location, location_size, "\n#line %d \"%s\"\n", line, file);
5322
  return location;
5323
}
5324

5325
//-------------------------is_literal_constant---------------------------------
5326
bool ADLParser::is_literal_constant(const char *param) {
5327
  if (param[0] == 0)     return false;  // null string
5328
  if (param[0] == '(')   return true;   // parenthesized expression
5329
  if (param[0] == '0' && (param[1] == 'x' || param[1] == 'X')) {
5330
    // Make sure it's a hex constant.
5331
    int i = 2;
5332
    do {
5333
      if( !ADLParser::is_hex_digit(*(param+i)) )  return false;
5334
      ++i;
5335
    } while( *(param+i) != 0 );
5336
    return true;
5337
  }
5338
  return false;
5339
}
5340

5341
//---------------------------is_hex_digit--------------------------------------
5342
bool ADLParser::is_hex_digit(char digit) {
5343
  return ((digit >= '0') && (digit <= '9'))
5344
       ||((digit >= 'a') && (digit <= 'f'))
5345
       ||((digit >= 'A') && (digit <= 'F'));
5346
}
5347

5348
//---------------------------is_int_token--------------------------------------
5349
bool ADLParser::is_int_token(const char* token, int& intval) {
5350
  const char* cp = token;
5351
  while (*cp != '\0' && *cp <= ' ')  cp++;
5352
  if (*cp == '-')  cp++;
5353
  int ndigit = 0;
5354
  while (*cp >= '0' && *cp <= '9')  { cp++; ndigit++; }
5355
  while (*cp != '\0' && *cp <= ' ')  cp++;
5356
  if (ndigit == 0 || *cp != '\0') {
5357
    return false;
5358
  }
5359
  intval = atoi(token);
5360
  return true;
5361
}
5362

5363
static const char* skip_expr_ws(const char* str) {
5364
  const char * cp = str;
5365
  while (cp[0]) {
5366
    if (cp[0] <= ' ') {
5367
      ++cp;
5368
    } else if (cp[0] == '#') {
5369
      ++cp;
5370
      while (cp[0] == ' ')  ++cp;
5371
      assert(0 == strncmp(cp, "line", 4), "must be a #line directive");
5372
      const char* eol = strchr(cp, '\n');
5373
      assert(eol != nullptr, "must find end of line");
5374
      if (eol == nullptr)  eol = cp + strlen(cp);
5375
      cp = eol;
5376
    } else {
5377
      break;
5378
    }
5379
  }
5380
  return cp;
5381
}
5382

5383
//-----------------------equivalent_expressions--------------------------------
5384
bool ADLParser::equivalent_expressions(const char* str1, const char* str2) {
5385
  if (str1 == str2)
5386
    return true;
5387
  else if (str1 == nullptr || str2 == nullptr)
5388
    return false;
5389
  const char* cp1 = str1;
5390
  const char* cp2 = str2;
5391
  char in_quote = '\0';
5392
  while (cp1[0] && cp2[0]) {
5393
    if (!in_quote) {
5394
      // skip spaces and/or cpp directives
5395
      const char* cp1a = skip_expr_ws(cp1);
5396
      const char* cp2a = skip_expr_ws(cp2);
5397
      if (cp1a > cp1 && cp2a > cp2) {
5398
        cp1 = cp1a; cp2 = cp2a;
5399
        continue;
5400
      }
5401
      if (cp1a > cp1 || cp2a > cp2)  break; // fail
5402
    }
5403
    // match one non-space char
5404
    if (cp1[0] != cp2[0])  break; // fail
5405
    char ch = cp1[0];
5406
    cp1++; cp2++;
5407
    // watch for quotes
5408
    if (in_quote && ch == '\\') {
5409
      if (cp1[0] != cp2[0])  break; // fail
5410
      if (!cp1[0])  break;
5411
      cp1++; cp2++;
5412
    }
5413
    if (in_quote && ch == in_quote) {
5414
      in_quote = '\0';
5415
    } else if (!in_quote && (ch == '"' || ch == '\'')) {
5416
      in_quote = ch;
5417
    }
5418
  }
5419
  return (!cp1[0] && !cp2[0]);
5420
}
5421

5422

5423
//-------------------------------trim------------------------------------------
5424
void ADLParser::trim(char* &token) {
5425
  while (*token <= ' ')  token++;
5426
  char* end = token + strlen(token);
5427
  while (end > token && *(end-1) <= ' ')  --end;
5428
  *end = '\0';
5429
}
5430

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.