src/share/vm/adlc/adlparse.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6503
a9becfeecd1b
parent 0
f90c822e73f8
child 7994
04ff2f6cd0eb
permissions
-rw-r--r--

merge

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

mercurial