src/share/vm/adlc/adlparse.cpp

Mon, 07 Jul 2014 10:12:40 +0200

author
stefank
date
Mon, 07 Jul 2014 10:12:40 +0200
changeset 6992
2c6ef90f030a
parent 6503
a9becfeecd1b
child 6876
710a3c8b516e
child 7853
a1642365d69f
permissions
-rw-r--r--

8049421: G1 Class Unloading after completing a concurrent mark cycle
Reviewed-by: tschatzl, ehelin, brutisso, coleenp, roland, iveresov
Contributed-by: stefan.karlsson@oracle.com, mikael.gerdin@oracle.com

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

mercurial