src/share/vm/adlc/adlparse.cpp

Wed, 14 Sep 2011 09:22:51 +0200

author
roland
date
Wed, 14 Sep 2011 09:22:51 +0200
changeset 3316
f03a3c8bd5e5
parent 3310
6729bbc1fcd6
child 3317
db2e64ca2d5a
permissions
-rw-r--r--

7077312: Provide a CALL effect for instruct declaration in the ad file
Summary: abstracted way to declare that the MachNode has the effect of a call (kills caller save registers, preserves callee save registers)
Reviewed-by: twisti, never

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

mercurial