Changes in src/CodeGen/CodeGenerator.cc [a0c7dc36:d48e529]
- File:
-
- 1 edited
-
src/CodeGen/CodeGenerator.cc (modified) (62 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
ra0c7dc36 rd48e529 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Andrew Beach12 // Last Modified On : Fri Aug 18 15:34:00201713 // Update Count : 4 8811 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun Sep 3 20:42:52 2017 13 // Update Count : 490 14 14 // 15 15 #include "CodeGenerator.h" … … 59 59 60 60 void CodeGenerator::asmName( DeclarationWithType * decl ) { 61 if ( ConstantExpr * asmName = d ecl->get_asmName() ) {61 if ( ConstantExpr * asmName = dynamic_cast<ConstantExpr *>(decl->get_asmName()) ) { 62 62 output << " asm ( " << asmName->get_constant()->get_value() << " )"; 63 63 } // if … … 89 89 } else if ( currentLocation.followedBy( to, 1 ) ) { 90 90 output << "\n" << indent; 91 currentLocation. linenumber+= 1;91 currentLocation.first_line += 1; 92 92 } else if ( currentLocation.followedBy( to, 2 ) ) { 93 93 output << "\n\n" << indent; 94 currentLocation. linenumber+= 2;95 } else { 96 output << "\n# " << to. linenumber<< " \"" << to.filename94 currentLocation.first_line += 2; 95 } else { 96 output << "\n# " << to.first_line << " \"" << to.filename 97 97 << "\"\n" << indent; 98 98 currentLocation = to; … … 139 139 } // CodeGenerator::genAttributes 140 140 141 // *** BaseSyntaxNode 142 void CodeGenerator::previsit( BaseSyntaxNode * ) { 143 // turn off automatic recursion for all nodes, to allow each visitor to 144 // precisely control the order in which its children are visited. 145 visit_children = false; 146 } 147 148 // *** BaseSyntaxNode 149 void CodeGenerator::postvisit( BaseSyntaxNode * node ) { 150 std::stringstream ss; 151 node->print( ss ); 152 assertf( false, "Unhandled node reached in CodeGenerator: %s", ss.str().c_str() ); 153 } 141 154 142 155 // *** Declarations 143 void CodeGenerator:: visit( FunctionDecl * functionDecl ) {156 void CodeGenerator::postvisit( FunctionDecl * functionDecl ) { 144 157 extension( functionDecl ); 145 158 genAttributes( functionDecl->get_attributes() ); … … 152 165 asmName( functionDecl ); 153 166 154 // acceptAll( functionDecl->get_oldDecls(), * this);167 // acceptAll( functionDecl->get_oldDecls(), *visitor ); 155 168 if ( functionDecl->get_statements() ) { 156 functionDecl->get_statements()->accept( * this);157 } // if 158 } 159 160 void CodeGenerator:: visit( ObjectDecl * objectDecl ) {169 functionDecl->get_statements()->accept( *visitor ); 170 } // if 171 } 172 173 void CodeGenerator::postvisit( ObjectDecl * objectDecl ) { 161 174 if (objectDecl->get_name().empty() && genC ) { 162 175 // only generate an anonymous name when generating C code, otherwise it clutters the output too much … … 175 188 if ( objectDecl->get_init() ) { 176 189 output << " = "; 177 objectDecl->get_init()->accept( * this);190 objectDecl->get_init()->accept( *visitor ); 178 191 } // if 179 192 180 193 if ( objectDecl->get_bitfieldWidth() ) { 181 194 output << ":"; 182 objectDecl->get_bitfieldWidth()->accept( * this);195 objectDecl->get_bitfieldWidth()->accept( *visitor ); 183 196 } // if 184 197 } … … 205 218 updateLocation( *i ); 206 219 output << indent; 207 (*i)->accept( * this);220 (*i)->accept( *visitor ); 208 221 output << ";" << endl; 209 222 } // for … … 215 228 } 216 229 217 void CodeGenerator:: visit( StructDecl * structDecl ) {230 void CodeGenerator::postvisit( StructDecl * structDecl ) { 218 231 extension( structDecl ); 219 232 handleAggregate( structDecl, "struct " ); 220 233 } 221 234 222 void CodeGenerator:: visit( UnionDecl * unionDecl ) {235 void CodeGenerator::postvisit( UnionDecl * unionDecl ) { 223 236 extension( unionDecl ); 224 237 handleAggregate( unionDecl, "union " ); 225 238 } 226 239 227 void CodeGenerator:: visit( EnumDecl * enumDecl ) {240 void CodeGenerator::postvisit( EnumDecl * enumDecl ) { 228 241 extension( enumDecl ); 229 242 updateLocation( enumDecl ); … … 246 259 if ( obj->get_init() ) { 247 260 output << " = "; 248 obj->get_init()->accept( * this);261 obj->get_init()->accept( *visitor ); 249 262 } // if 250 263 output << "," << endl; … … 257 270 } 258 271 259 void CodeGenerator:: visit( TraitDecl * traitDecl ) {272 void CodeGenerator::postvisit( TraitDecl * traitDecl ) { 260 273 assertf( ! genC, "TraitDecls should not reach code generation." ); 261 274 extension( traitDecl ); … … 263 276 } 264 277 265 void CodeGenerator:: visit( TypedefDecl * typeDecl ) {278 void CodeGenerator::postvisit( TypedefDecl * typeDecl ) { 266 279 assertf( ! genC, "Typedefs are removed and substituted in earlier passes." ); 267 280 updateLocation( typeDecl ); … … 270 283 } 271 284 272 void CodeGenerator:: visit( TypeDecl * typeDecl ) {285 void CodeGenerator::postvisit( TypeDecl * typeDecl ) { 273 286 assertf( ! genC, "TypeDecls should not reach code generation." ); 274 287 output << typeDecl->genTypeString() << " " << typeDecl->get_name(); … … 283 296 } 284 297 285 void CodeGenerator:: visit( Designation * designation ) {298 void CodeGenerator::postvisit( Designation * designation ) { 286 299 std::list< Expression * > designators = designation->get_designators(); 287 300 if ( designators.size() == 0 ) return; … … 290 303 // if expression is a NameExpr or VariableExpr, then initializing aggregate member 291 304 output << "."; 292 des->accept( * this);305 des->accept( *visitor ); 293 306 } else { 294 307 // otherwise, it has to be a ConstantExpr or CastExpr, initializing array eleemnt 295 308 output << "["; 296 des->accept( * this);309 des->accept( *visitor ); 297 310 output << "]"; 298 311 } // if … … 301 314 } 302 315 303 void CodeGenerator:: visit( SingleInit * init ) {304 init->get_value()->accept( * this);305 } 306 307 void CodeGenerator:: visit( ListInit * init ) {316 void CodeGenerator::postvisit( SingleInit * init ) { 317 init->get_value()->accept( *visitor ); 318 } 319 320 void CodeGenerator::postvisit( ListInit * init ) { 308 321 auto initBegin = init->begin(); 309 322 auto initEnd = init->end(); … … 313 326 output << "{ "; 314 327 for ( ; initBegin != initEnd && desigBegin != desigEnd; ) { 315 (*desigBegin)->accept( * this);316 (*initBegin)->accept( * this);328 (*desigBegin)->accept( *visitor ); 329 (*initBegin)->accept( *visitor ); 317 330 ++initBegin, ++desigBegin; 318 331 if ( initBegin != initEnd ) { … … 324 337 } 325 338 326 void CodeGenerator:: visit( __attribute__((unused)) ConstructorInit * init ){339 void CodeGenerator::postvisit( __attribute__((unused)) ConstructorInit * init ){ 327 340 assertf( ! genC, "ConstructorInit nodes should not reach code generation." ); 328 341 // pseudo-output for constructor/destructor pairs 329 342 output << "<ctorinit>{" << std::endl << ++indent << "ctor: "; 330 maybeAccept( init->get_ctor(), * this);343 maybeAccept( init->get_ctor(), *visitor ); 331 344 output << ", " << std::endl << indent << "dtor: "; 332 maybeAccept( init->get_dtor(), * this);345 maybeAccept( init->get_dtor(), *visitor ); 333 346 output << std::endl << --indent << "}"; 334 347 } 335 348 336 void CodeGenerator:: visit( Constant * constant ) {349 void CodeGenerator::postvisit( Constant * constant ) { 337 350 output << constant->get_value() ; 338 351 } 339 352 340 353 // *** Expressions 341 void CodeGenerator:: visit( ApplicationExpr * applicationExpr ) {354 void CodeGenerator::postvisit( ApplicationExpr * applicationExpr ) { 342 355 extension( applicationExpr ); 343 356 if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) { … … 348 361 case OT_INDEX: 349 362 assert( applicationExpr->get_args().size() == 2 ); 350 (*arg++)->accept( * this);363 (*arg++)->accept( *visitor ); 351 364 output << "["; 352 (*arg)->accept( * this);365 (*arg)->accept( *visitor ); 353 366 output << "]"; 354 367 break; … … 365 378 // effects, so must still output this expression 366 379 output << "("; 367 (*arg++)->accept( * this);380 (*arg++)->accept( *visitor ); 368 381 output << ") /* " << opInfo.inputName << " */"; 369 382 } else if ( applicationExpr->get_args().size() == 2 ) { 370 383 // intrinsic two parameter constructors are essentially bitwise assignment 371 384 output << "("; 372 (*arg++)->accept( * this);385 (*arg++)->accept( *visitor ); 373 386 output << opInfo.symbol; 374 (*arg)->accept( * this);387 (*arg)->accept( *visitor ); 375 388 output << ") /* " << opInfo.inputName << " */"; 376 389 } else { … … 385 398 output << "("; 386 399 output << opInfo.symbol; 387 (*arg)->accept( * this);400 (*arg)->accept( *visitor ); 388 401 output << ")"; 389 402 break; … … 392 405 case OT_POSTFIXASSIGN: 393 406 assert( applicationExpr->get_args().size() == 1 ); 394 (*arg)->accept( * this);407 (*arg)->accept( *visitor ); 395 408 output << opInfo.symbol; 396 409 break; … … 401 414 assert( applicationExpr->get_args().size() == 2 ); 402 415 output << "("; 403 (*arg++)->accept( * this);416 (*arg++)->accept( *visitor ); 404 417 output << opInfo.symbol; 405 (*arg)->accept( * this);418 (*arg)->accept( *visitor ); 406 419 output << ")"; 407 420 break; … … 413 426 } // switch 414 427 } else { 415 varExpr->accept( * this);428 varExpr->accept( *visitor ); 416 429 output << "("; 417 430 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); … … 419 432 } // if 420 433 } else { 421 applicationExpr->get_function()->accept( * this);434 applicationExpr->get_function()->accept( *visitor ); 422 435 output << "("; 423 436 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); … … 426 439 } 427 440 428 void CodeGenerator:: visit( UntypedExpr * untypedExpr ) {441 void CodeGenerator::postvisit( UntypedExpr * untypedExpr ) { 429 442 extension( untypedExpr ); 430 443 if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) { … … 435 448 case OT_INDEX: 436 449 assert( untypedExpr->get_args().size() == 2 ); 437 (*arg++)->accept( * this);450 (*arg++)->accept( *visitor ); 438 451 output << "["; 439 (*arg)->accept( * this);452 (*arg)->accept( *visitor ); 440 453 output << "]"; 441 454 break; … … 450 463 // effects, so must still output this expression 451 464 output << "("; 452 (*arg++)->accept( * this);465 (*arg++)->accept( *visitor ); 453 466 output << ") /* " << opInfo.inputName << " */"; 454 467 } else if ( untypedExpr->get_args().size() == 2 ) { 455 468 // intrinsic two parameter constructors are essentially bitwise assignment 456 469 output << "("; 457 (*arg++)->accept( * this);470 (*arg++)->accept( *visitor ); 458 471 output << opInfo.symbol; 459 (*arg)->accept( * this);472 (*arg)->accept( *visitor ); 460 473 output << ") /* " << opInfo.inputName << " */"; 461 474 } else { 462 475 // no constructors with 0 or more than 2 parameters 463 assert( false ); 476 assertf( ! genC, "UntypedExpr constructor/destructor with 0 or more than 2 parameters." ); 477 output << "("; 478 (*arg++)->accept( *visitor ); 479 output << opInfo.symbol << "{ "; 480 genCommaList( arg, untypedExpr->get_args().end() ); 481 output << "}) /* " << opInfo.inputName << " */"; 464 482 } // if 465 483 break; … … 471 489 output << "("; 472 490 output << opInfo.symbol; 473 (*arg)->accept( * this);491 (*arg)->accept( *visitor ); 474 492 output << ")"; 475 493 break; … … 478 496 case OT_POSTFIXASSIGN: 479 497 assert( untypedExpr->get_args().size() == 1 ); 480 (*arg)->accept( * this);498 (*arg)->accept( *visitor ); 481 499 output << opInfo.symbol; 482 500 break; … … 486 504 assert( untypedExpr->get_args().size() == 2 ); 487 505 output << "("; 488 (*arg++)->accept( * this);506 (*arg++)->accept( *visitor ); 489 507 output << opInfo.symbol; 490 (*arg)->accept( * this);508 (*arg)->accept( *visitor ); 491 509 output << ")"; 492 510 break; … … 499 517 if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2 500 518 assert( untypedExpr->get_args().size() == 2 ); 501 (*untypedExpr->get_args().begin())->accept( * this);519 (*untypedExpr->get_args().begin())->accept( *visitor ); 502 520 output << " ... "; 503 (*--untypedExpr->get_args().end())->accept( * this);521 (*--untypedExpr->get_args().end())->accept( *visitor ); 504 522 } else { // builtin routines 505 nameExpr->accept( * this);523 nameExpr->accept( *visitor ); 506 524 output << "("; 507 525 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); … … 510 528 } // if 511 529 } else { 512 untypedExpr->get_function()->accept( * this);530 untypedExpr->get_function()->accept( *visitor ); 513 531 output << "("; 514 532 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); … … 517 535 } 518 536 519 void CodeGenerator:: visit( RangeExpr * rangeExpr ) {520 rangeExpr->get_low()->accept( * this);537 void CodeGenerator::postvisit( RangeExpr * rangeExpr ) { 538 rangeExpr->get_low()->accept( *visitor ); 521 539 output << " ... "; 522 rangeExpr->get_high()->accept( * this);523 } 524 525 void CodeGenerator:: visit( NameExpr * nameExpr ) {540 rangeExpr->get_high()->accept( *visitor ); 541 } 542 543 void CodeGenerator::postvisit( NameExpr * nameExpr ) { 526 544 extension( nameExpr ); 527 545 OperatorInfo opInfo; … … 534 552 } 535 553 536 void CodeGenerator:: visit( AddressExpr * addressExpr ) {554 void CodeGenerator::postvisit( AddressExpr * addressExpr ) { 537 555 extension( addressExpr ); 538 556 output << "(&"; 539 addressExpr->arg->accept( * this);557 addressExpr->arg->accept( *visitor ); 540 558 output << ")"; 541 559 } 542 560 543 void CodeGenerator:: visit( LabelAddressExpr *addressExpr ) {561 void CodeGenerator::postvisit( LabelAddressExpr *addressExpr ) { 544 562 extension( addressExpr ); 545 563 output << "(&&" << addressExpr->arg << ")"; 546 564 } 547 565 548 void CodeGenerator:: visit( CastExpr * castExpr ) {566 void CodeGenerator::postvisit( CastExpr * castExpr ) { 549 567 extension( castExpr ); 550 568 output << "("; … … 559 577 output << ")"; 560 578 } // if 561 castExpr->get_arg()->accept( * this);579 castExpr->get_arg()->accept( *visitor ); 562 580 output << ")"; 563 581 } 564 582 565 void CodeGenerator:: visit( VirtualCastExpr * castExpr ) {583 void CodeGenerator::postvisit( VirtualCastExpr * castExpr ) { 566 584 assertf( ! genC, "VirtualCastExpr should not reach code generation." ); 567 585 extension( castExpr ); 568 586 output << "(virtual "; 569 castExpr->get_arg()->accept( * this);587 castExpr->get_arg()->accept( *visitor ); 570 588 output << ")"; 571 589 } 572 590 573 void CodeGenerator:: visit( UntypedMemberExpr * memberExpr ) {591 void CodeGenerator::postvisit( UntypedMemberExpr * memberExpr ) { 574 592 assertf( ! genC, "UntypedMemberExpr should not reach code generation." ); 575 593 extension( memberExpr ); 576 memberExpr->get_aggregate()->accept( * this);594 memberExpr->get_aggregate()->accept( *visitor ); 577 595 output << "."; 578 memberExpr->get_member()->accept( * this);579 } 580 581 void CodeGenerator:: visit( MemberExpr * memberExpr ) {596 memberExpr->get_member()->accept( *visitor ); 597 } 598 599 void CodeGenerator::postvisit( MemberExpr * memberExpr ) { 582 600 extension( memberExpr ); 583 memberExpr->get_aggregate()->accept( * this);601 memberExpr->get_aggregate()->accept( *visitor ); 584 602 output << "." << mangleName( memberExpr->get_member() ); 585 603 } 586 604 587 void CodeGenerator:: visit( VariableExpr * variableExpr ) {605 void CodeGenerator::postvisit( VariableExpr * variableExpr ) { 588 606 extension( variableExpr ); 589 607 OperatorInfo opInfo; … … 595 613 } 596 614 597 void CodeGenerator:: visit( ConstantExpr * constantExpr ) {615 void CodeGenerator::postvisit( ConstantExpr * constantExpr ) { 598 616 assert( constantExpr->get_constant() ); 599 617 extension( constantExpr ); 600 constantExpr->get_constant()->accept( * this);601 } 602 603 void CodeGenerator:: visit( SizeofExpr * sizeofExpr ) {618 constantExpr->get_constant()->accept( *visitor ); 619 } 620 621 void CodeGenerator::postvisit( SizeofExpr * sizeofExpr ) { 604 622 extension( sizeofExpr ); 605 623 output << "sizeof("; … … 607 625 output << genType( sizeofExpr->get_type(), "", pretty, genC ); 608 626 } else { 609 sizeofExpr->get_expr()->accept( * this);627 sizeofExpr->get_expr()->accept( *visitor ); 610 628 } // if 611 629 output << ")"; 612 630 } 613 631 614 void CodeGenerator:: visit( AlignofExpr * alignofExpr ) {632 void CodeGenerator::postvisit( AlignofExpr * alignofExpr ) { 615 633 // use GCC extension to avoid bumping std to C11 616 634 extension( alignofExpr ); … … 619 637 output << genType( alignofExpr->get_type(), "", pretty, genC ); 620 638 } else { 621 alignofExpr->get_expr()->accept( * this);639 alignofExpr->get_expr()->accept( *visitor ); 622 640 } // if 623 641 output << ")"; 624 642 } 625 643 626 void CodeGenerator:: visit( UntypedOffsetofExpr * offsetofExpr ) {644 void CodeGenerator::postvisit( UntypedOffsetofExpr * offsetofExpr ) { 627 645 assertf( ! genC, "UntypedOffsetofExpr should not reach code generation." ); 628 646 output << "offsetof("; … … 632 650 } 633 651 634 void CodeGenerator:: visit( OffsetofExpr * offsetofExpr ) {652 void CodeGenerator::postvisit( OffsetofExpr * offsetofExpr ) { 635 653 // use GCC builtin 636 654 output << "__builtin_offsetof("; … … 640 658 } 641 659 642 void CodeGenerator:: visit( OffsetPackExpr * offsetPackExpr ) {660 void CodeGenerator::postvisit( OffsetPackExpr * offsetPackExpr ) { 643 661 assertf( ! genC, "OffsetPackExpr should not reach code generation." ); 644 662 output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", pretty, genC ) << ")"; 645 663 } 646 664 647 void CodeGenerator:: visit( LogicalExpr * logicalExpr ) {665 void CodeGenerator::postvisit( LogicalExpr * logicalExpr ) { 648 666 extension( logicalExpr ); 649 667 output << "("; 650 logicalExpr->get_arg1()->accept( * this);668 logicalExpr->get_arg1()->accept( *visitor ); 651 669 if ( logicalExpr->get_isAnd() ) { 652 670 output << " && "; … … 654 672 output << " || "; 655 673 } // if 656 logicalExpr->get_arg2()->accept( * this);674 logicalExpr->get_arg2()->accept( *visitor ); 657 675 output << ")"; 658 676 } 659 677 660 void CodeGenerator:: visit( ConditionalExpr * conditionalExpr ) {678 void CodeGenerator::postvisit( ConditionalExpr * conditionalExpr ) { 661 679 extension( conditionalExpr ); 662 680 output << "("; 663 conditionalExpr->get_arg1()->accept( * this);681 conditionalExpr->get_arg1()->accept( *visitor ); 664 682 output << " ? "; 665 conditionalExpr->get_arg2()->accept( * this);683 conditionalExpr->get_arg2()->accept( *visitor ); 666 684 output << " : "; 667 conditionalExpr->get_arg3()->accept( * this);685 conditionalExpr->get_arg3()->accept( *visitor ); 668 686 output << ")"; 669 687 } 670 688 671 void CodeGenerator:: visit( CommaExpr * commaExpr ) {689 void CodeGenerator::postvisit( CommaExpr * commaExpr ) { 672 690 extension( commaExpr ); 673 691 output << "("; … … 676 694 commaExpr->set_arg1( new CastExpr( commaExpr->get_arg1() ) ); 677 695 } 678 commaExpr->get_arg1()->accept( * this);696 commaExpr->get_arg1()->accept( *visitor ); 679 697 output << " , "; 680 commaExpr->get_arg2()->accept( * this);698 commaExpr->get_arg2()->accept( *visitor ); 681 699 output << ")"; 682 700 } 683 701 684 void CodeGenerator:: visit( TupleAssignExpr * tupleExpr ) {702 void CodeGenerator::postvisit( TupleAssignExpr * tupleExpr ) { 685 703 assertf( ! genC, "TupleAssignExpr should not reach code generation." ); 686 tupleExpr->stmtExpr->accept( * this);687 } 688 689 void CodeGenerator:: visit( UntypedTupleExpr * tupleExpr ) {704 tupleExpr->stmtExpr->accept( *visitor ); 705 } 706 707 void CodeGenerator::postvisit( UntypedTupleExpr * tupleExpr ) { 690 708 assertf( ! genC, "UntypedTupleExpr should not reach code generation." ); 691 709 extension( tupleExpr ); … … 695 713 } 696 714 697 void CodeGenerator:: visit( TupleExpr * tupleExpr ) {715 void CodeGenerator::postvisit( TupleExpr * tupleExpr ) { 698 716 assertf( ! genC, "TupleExpr should not reach code generation." ); 699 717 extension( tupleExpr ); … … 703 721 } 704 722 705 void CodeGenerator:: visit( TupleIndexExpr * tupleExpr ) {723 void CodeGenerator::postvisit( TupleIndexExpr * tupleExpr ) { 706 724 assertf( ! genC, "TupleIndexExpr should not reach code generation." ); 707 725 extension( tupleExpr ); 708 tupleExpr->get_tuple()->accept( * this);726 tupleExpr->get_tuple()->accept( *visitor ); 709 727 output << "." << tupleExpr->get_index(); 710 728 } 711 729 712 void CodeGenerator:: visit( TypeExpr * typeExpr ) {730 void CodeGenerator::postvisit( TypeExpr * typeExpr ) { 713 731 // if ( genC ) std::cerr << "typeexpr still exists: " << typeExpr << std::endl; 714 732 // assertf( ! genC, "TypeExpr should not reach code generation." ); … … 718 736 } 719 737 720 void CodeGenerator:: visit( AsmExpr * asmExpr ) {738 void CodeGenerator::postvisit( AsmExpr * asmExpr ) { 721 739 if ( asmExpr->get_inout() ) { 722 740 output << "[ "; 723 asmExpr->get_inout()->accept( * this);741 asmExpr->get_inout()->accept( *visitor ); 724 742 output << " ] "; 725 743 } // if 726 asmExpr->get_constraint()->accept( * this);744 asmExpr->get_constraint()->accept( *visitor ); 727 745 output << " ( "; 728 asmExpr->get_operand()->accept( * this);746 asmExpr->get_operand()->accept( *visitor ); 729 747 output << " )"; 730 748 } 731 749 732 void CodeGenerator:: visit( CompoundLiteralExpr *compLitExpr ) {750 void CodeGenerator::postvisit( CompoundLiteralExpr *compLitExpr ) { 733 751 assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) ); 734 752 output << "(" << genType( compLitExpr->get_result(), "", pretty, genC ) << ")"; 735 compLitExpr->get_initializer()->accept( * this);736 } 737 738 void CodeGenerator:: visit( UniqueExpr * unqExpr ) {753 compLitExpr->get_initializer()->accept( *visitor ); 754 } 755 756 void CodeGenerator::postvisit( UniqueExpr * unqExpr ) { 739 757 assertf( ! genC, "Unique expressions should not reach code generation." ); 740 758 output << "unq<" << unqExpr->get_id() << ">{ "; 741 unqExpr->get_expr()->accept( * this);759 unqExpr->get_expr()->accept( *visitor ); 742 760 output << " }"; 743 761 } 744 762 745 void CodeGenerator:: visit( StmtExpr * stmtExpr ) {763 void CodeGenerator::postvisit( StmtExpr * stmtExpr ) { 746 764 std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids(); 747 765 updateLocation( stmtExpr ); … … 757 775 // cannot cast to void, otherwise the expression statement has no value 758 776 if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) { 759 exprStmt->get_expr()->accept( * this);777 exprStmt->get_expr()->accept( *visitor ); 760 778 output << ";" << endl; 761 779 ++i; … … 763 781 } 764 782 } 765 stmt->accept( * this);783 stmt->accept( *visitor ); 766 784 output << endl; 767 785 if ( wantSpacing( stmt ) ) { … … 774 792 } 775 793 794 void CodeGenerator::postvisit( ConstructorExpr * expr ) { 795 assertf( ! genC, "Unique expressions should not reach code generation." ); 796 expr->callExpr->accept( *visitor ); 797 } 798 776 799 // *** Statements 777 void CodeGenerator:: visit( CompoundStmt * compoundStmt ) {800 void CodeGenerator::postvisit( CompoundStmt * compoundStmt ) { 778 801 std::list<Statement*> ks = compoundStmt->get_kids(); 779 802 output << "{" << endl; … … 783 806 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) { 784 807 output << indent << printLabels( (*i)->get_labels() ); 785 (*i)->accept( * this);808 (*i)->accept( *visitor ); 786 809 787 810 output << endl; … … 795 818 } 796 819 797 void CodeGenerator:: visit( ExprStmt * exprStmt ) {820 void CodeGenerator::postvisit( ExprStmt * exprStmt ) { 798 821 assert( exprStmt ); 799 822 if ( genC ) { … … 801 824 exprStmt->set_expr( new CastExpr( exprStmt->get_expr() ) ); 802 825 } 803 exprStmt->get_expr()->accept( * this);826 exprStmt->get_expr()->accept( *visitor ); 804 827 output << ";"; 805 828 } 806 829 807 void CodeGenerator:: visit( AsmStmt * asmStmt ) {830 void CodeGenerator::postvisit( AsmStmt * asmStmt ) { 808 831 output << "asm "; 809 832 if ( asmStmt->get_voltile() ) output << "volatile "; 810 833 if ( ! asmStmt->get_gotolabels().empty() ) output << "goto "; 811 834 output << "( "; 812 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( * this);835 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *visitor ); 813 836 output << " : "; 814 837 genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() ); … … 828 851 } 829 852 830 void CodeGenerator:: visit( AsmDecl * asmDecl ) {853 void CodeGenerator::postvisit( AsmDecl * asmDecl ) { 831 854 output << "asm "; 832 855 AsmStmt * asmStmt = asmDecl->get_stmt(); 833 856 output << "( "; 834 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( * this);857 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *visitor ); 835 858 output << " )" ; 836 859 } 837 860 838 void CodeGenerator:: visit( IfStmt * ifStmt ) {861 void CodeGenerator::postvisit( IfStmt * ifStmt ) { 839 862 updateLocation( ifStmt ); 840 863 output << "if ( "; 841 ifStmt->get_condition()->accept( * this);864 ifStmt->get_condition()->accept( *visitor ); 842 865 output << " ) "; 843 866 844 ifStmt->get_thenPart()->accept( * this);867 ifStmt->get_thenPart()->accept( *visitor ); 845 868 846 869 if ( ifStmt->get_elsePart() != 0) { 847 870 output << " else "; 848 ifStmt->get_elsePart()->accept( * this);849 } // if 850 } 851 852 void CodeGenerator:: visit( SwitchStmt * switchStmt ) {871 ifStmt->get_elsePart()->accept( *visitor ); 872 } // if 873 } 874 875 void CodeGenerator::postvisit( SwitchStmt * switchStmt ) { 853 876 updateLocation( switchStmt ); 854 877 output << "switch ( " ; 855 switchStmt->get_condition()->accept( * this);878 switchStmt->get_condition()->accept( *visitor ); 856 879 output << " ) "; 857 880 858 881 output << "{" << std::endl; 859 882 ++indent; 860 acceptAll( switchStmt->get_statements(), * this);883 acceptAll( switchStmt->get_statements(), *visitor ); 861 884 --indent; 862 885 output << indent << "}"; 863 886 } 864 887 865 void CodeGenerator:: visit( CaseStmt * caseStmt ) {888 void CodeGenerator::postvisit( CaseStmt * caseStmt ) { 866 889 updateLocation( caseStmt ); 867 890 if ( caseStmt->isDefault()) { … … 869 892 } else { 870 893 output << "case "; 871 caseStmt->get_condition()->accept( * this);894 caseStmt->get_condition()->accept( *visitor ); 872 895 } // if 873 896 output << ":\n"; … … 878 901 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) { 879 902 output << indent << printLabels( (*i)->get_labels() ) ; 880 (*i)->accept( * this);903 (*i)->accept( *visitor ); 881 904 output << endl; 882 905 } // for … … 884 907 } 885 908 886 void CodeGenerator:: visit( BranchStmt * branchStmt ) {909 void CodeGenerator::postvisit( BranchStmt * branchStmt ) { 887 910 switch ( branchStmt->get_type()) { 888 911 case BranchStmt::Goto: … … 892 915 if ( branchStmt->get_computedTarget() != 0 ) { 893 916 output << "goto *"; 894 branchStmt->get_computedTarget()->accept( * this);917 branchStmt->get_computedTarget()->accept( *visitor ); 895 918 } // if 896 919 } // if … … 906 929 } 907 930 908 void CodeGenerator:: visit( ReturnStmt * returnStmt ) {931 void CodeGenerator::postvisit( ReturnStmt * returnStmt ) { 909 932 output << "return "; 910 maybeAccept( returnStmt->get_expr(), * this);933 maybeAccept( returnStmt->get_expr(), *visitor ); 911 934 output << ";"; 912 935 } 913 936 914 void CodeGenerator:: visit( ThrowStmt * throwStmt ) {937 void CodeGenerator::postvisit( ThrowStmt * throwStmt ) { 915 938 assertf( ! genC, "Throw statements should not reach code generation." ); 916 939 … … 919 942 if (throwStmt->get_expr()) { 920 943 output << " "; 921 throwStmt->get_expr()->accept( * this);944 throwStmt->get_expr()->accept( *visitor ); 922 945 } 923 946 if (throwStmt->get_target()) { 924 947 output << " _At "; 925 throwStmt->get_target()->accept( * this);948 throwStmt->get_target()->accept( *visitor ); 926 949 } 927 950 output << ";"; 928 951 } 929 952 930 void CodeGenerator:: visit( WhileStmt * whileStmt ) {953 void CodeGenerator::postvisit( WhileStmt * whileStmt ) { 931 954 if ( whileStmt->get_isDoWhile() ) { 932 955 output << "do" ; 933 956 } else { 934 957 output << "while (" ; 935 whileStmt->get_condition()->accept( * this);958 whileStmt->get_condition()->accept( *visitor ); 936 959 output << ")"; 937 960 } // if … … 939 962 940 963 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() ); 941 whileStmt->get_body()->accept( * this);964 whileStmt->get_body()->accept( *visitor ); 942 965 943 966 output << indent; … … 945 968 if ( whileStmt->get_isDoWhile() ) { 946 969 output << " while (" ; 947 whileStmt->get_condition()->accept( * this);970 whileStmt->get_condition()->accept( *visitor ); 948 971 output << ");"; 949 972 } // if 950 973 } 951 974 952 void CodeGenerator:: visit( ForStmt * forStmt ) {975 void CodeGenerator::postvisit( ForStmt * forStmt ) { 953 976 // initialization is always hoisted, so don't bother doing anything with that 954 977 output << "for (;"; 955 978 956 979 if ( forStmt->get_condition() != 0 ) { 957 forStmt->get_condition()->accept( * this);980 forStmt->get_condition()->accept( *visitor ); 958 981 } // if 959 982 output << ";"; … … 962 985 // cast the top-level expression to void to reduce gcc warnings. 963 986 Expression * expr = new CastExpr( forStmt->get_increment() ); 964 expr->accept( * this);987 expr->accept( *visitor ); 965 988 } // if 966 989 output << ") "; … … 968 991 if ( forStmt->get_body() != 0 ) { 969 992 output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() ); 970 forStmt->get_body()->accept( * this);971 } // if 972 } 973 974 void CodeGenerator:: visit( __attribute__((unused)) NullStmt * nullStmt ) {993 forStmt->get_body()->accept( *visitor ); 994 } // if 995 } 996 997 void CodeGenerator::postvisit( __attribute__((unused)) NullStmt * nullStmt ) { 975 998 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() ); 976 999 output << "/* null statement */ ;"; 977 1000 } 978 1001 979 void CodeGenerator:: visit( DeclStmt * declStmt ) {980 declStmt->get_decl()->accept( * this);1002 void CodeGenerator::postvisit( DeclStmt * declStmt ) { 1003 declStmt->get_decl()->accept( *visitor ); 981 1004 982 1005 if ( doSemicolon( declStmt->get_decl() ) ) { 983 1006 output << ";"; 984 1007 } // if 1008 } 1009 1010 void CodeGenerator::postvisit( ImplicitCtorDtorStmt * stmt ) { 1011 assertf( ! genC, "ImplicitCtorDtorStmts should not reach code generation." ); 1012 stmt->callStmt->accept( *visitor ); 985 1013 } 986 1014
Note:
See TracChangeset
for help on using the changeset viewer.