Changeset c331406 for src/CodeGen/CodeGenerator.cc
- Timestamp:
- Aug 5, 2016, 11:03:04 AM (8 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- a2f920f
- Parents:
- 5070fe4 (diff), 1b0020a (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
r5070fe4 rc331406 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : 12 // Last Modified On : Sun Jul 31 08:42:18201613 // Update Count : 3 4511 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Thu Aug 4 13:35:30 2016 13 // Update Count : 352 14 14 // 15 15 … … 48 48 } 49 49 50 void CodeGenerator::extension( Expression * expr ) {50 void CodeGenerator::extension( Expression * expr ) { 51 51 if ( expr->get_extension() ) { 52 52 output << "__extension__ "; … … 54 54 } // extension 55 55 56 void CodeGenerator::extension( Declaration * decl ) {56 void CodeGenerator::extension( Declaration * decl ) { 57 57 if ( decl->get_extension() ) { 58 58 output << "__extension__ "; … … 73 73 } 74 74 75 ostream & operator<<( ostream & output, CodeGenerator::LabelPrinter & printLabels ) {75 ostream & operator<<( ostream & output, CodeGenerator::LabelPrinter & printLabels ) { 76 76 std::list< Label > & labs = *printLabels.labels; 77 77 // l.unique(); // assumes a sorted list. Why not use set? Does order matter? … … 79 79 output << l.get_name() + ": "; 80 80 printLabels.cg.genAttributes( l.get_attributes() ); 81 } 81 } // for 82 82 return output; 83 83 } 84 84 85 CodeGenerator::CodeGenerator( std::ostream & os ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ), printLabels( *this ) {}86 87 CodeGenerator::CodeGenerator( std::ostream & os, std::string init, int indentation, bool infunp )85 CodeGenerator::CodeGenerator( std::ostream & os ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ), printLabels( *this ) {} 86 87 CodeGenerator::CodeGenerator( std::ostream & os, std::string init, int indentation, bool infunp ) 88 88 : indent( *this), cur_indent( indentation ), insideFunction( infunp ), output( os ), printLabels( *this ) { 89 89 //output << std::string( init ); 90 90 } 91 91 92 CodeGenerator::CodeGenerator( std::ostream & os, char *init, int indentation, bool infunp )92 CodeGenerator::CodeGenerator( std::ostream & os, char * init, int indentation, bool infunp ) 93 93 : indent( *this ), cur_indent( indentation ), insideFunction( infunp ), output( os ), printLabels( *this ) { 94 94 //output << std::string( init ); 95 95 } 96 96 97 string mangleName( DeclarationWithType * decl ) {97 string mangleName( DeclarationWithType * decl ) { 98 98 if ( decl->get_mangleName() != "" ) { 99 99 // need to incorporate scope level in order to differentiate names for destructors … … 112 112 genCommaList( attr->get_parameters().begin(), attr->get_parameters().end() ); 113 113 output << ")"; 114 } 114 } // if 115 115 output << ","; 116 } 116 } // for 117 117 output << ")) "; 118 } 118 } // if 119 119 } 120 120 121 121 122 122 //*** Declarations 123 void CodeGenerator::visit( FunctionDecl * functionDecl ) {123 void CodeGenerator::visit( FunctionDecl * functionDecl ) { 124 124 extension( functionDecl ); 125 125 genAttributes( functionDecl->get_attributes() ); … … 146 146 } 147 147 148 void CodeGenerator::visit( ObjectDecl * objectDecl ) {148 void CodeGenerator::visit( ObjectDecl * objectDecl ) { 149 149 extension( objectDecl ); 150 genAttributes( objectDecl->get_attributes() ); 151 150 152 handleStorageClass( objectDecl ); 151 153 output << genType( objectDecl->get_type(), mangleName( objectDecl ) ); … … 162 164 } 163 165 164 void CodeGenerator::handleAggregate( AggregateDecl * aggDecl ) {166 void CodeGenerator::handleAggregate( AggregateDecl * aggDecl ) { 165 167 if ( aggDecl->get_name() != "" ) 166 168 output << aggDecl->get_name(); 167 169 168 std::list< Declaration * > & memb = aggDecl->get_members();170 std::list< Declaration * > & memb = aggDecl->get_members(); 169 171 if ( ! memb.empty() ) { 170 172 // if ( aggDecl->has_body() ) { 171 // std::list< Declaration * > & memb = aggDecl->get_members();173 // std::list< Declaration * > & memb = aggDecl->get_members(); 172 174 output << " {" << endl; 173 175 … … 185 187 } 186 188 187 void CodeGenerator::visit( StructDecl * structDecl ) {189 void CodeGenerator::visit( StructDecl * structDecl ) { 188 190 extension( structDecl ); 189 191 output << "struct "; … … 191 193 } 192 194 193 void CodeGenerator::visit( UnionDecl * unionDecl ) {195 void CodeGenerator::visit( UnionDecl * unionDecl ) { 194 196 extension( unionDecl ); 195 197 output << "union "; … … 197 199 } 198 200 199 void CodeGenerator::visit( EnumDecl * enumDecl ) {201 void CodeGenerator::visit( EnumDecl * enumDecl ) { 200 202 extension( enumDecl ); 201 203 output << "enum "; … … 211 213 cur_indent += CodeGenerator::tabsize; 212 214 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { 213 ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );215 ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i ); 214 216 assert( obj ); 215 217 output << indent << mangleName( obj ); … … 227 229 } 228 230 229 void CodeGenerator::visit( TraitDecl * traitDecl ) {}230 231 void CodeGenerator::visit( TypedefDecl * typeDecl ) {231 void CodeGenerator::visit( TraitDecl * traitDecl ) {} 232 233 void CodeGenerator::visit( TypedefDecl * typeDecl ) { 232 234 assert( false && "Typedefs are removed and substituted in earlier passes." ); 233 235 //output << "typedef "; … … 235 237 } 236 238 237 void CodeGenerator::visit( TypeDecl * typeDecl ) {239 void CodeGenerator::visit( TypeDecl * typeDecl ) { 238 240 // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes, 239 241 // still to be done … … 263 265 } 264 266 265 void CodeGenerator::visit( SingleInit * init ) {267 void CodeGenerator::visit( SingleInit * init ) { 266 268 printDesignators( init->get_designators() ); 267 269 init->get_value()->accept( *this ); 268 270 } 269 271 270 void CodeGenerator::visit( ListInit * init ) {272 void CodeGenerator::visit( ListInit * init ) { 271 273 printDesignators( init->get_designators() ); 272 274 output << "{ "; 273 if ( init->begin _initializers() == init->end_initializers() ) {275 if ( init->begin() == init->end() ) { 274 276 // illegal to leave initializer list empty for scalar initializers, but always legal to have 0 275 277 output << "0"; 276 278 } else { 277 genCommaList( init->begin _initializers(), init->end_initializers() );278 } 279 genCommaList( init->begin(), init->end() ); 280 } // if 279 281 output << " }"; 280 282 } 281 283 282 void CodeGenerator::visit( Constant * constant ) {284 void CodeGenerator::visit( Constant * constant ) { 283 285 output << constant->get_value() ; 284 286 } 285 287 286 288 //*** Expressions 287 void CodeGenerator::visit( ApplicationExpr * applicationExpr ) {289 void CodeGenerator::visit( ApplicationExpr * applicationExpr ) { 288 290 extension( applicationExpr ); 289 if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {291 if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) { 290 292 OperatorInfo opInfo; 291 293 if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) { … … 299 301 { 300 302 assert( arg != applicationExpr->get_args().end() ); 301 if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {303 if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) { 302 304 // remove & from first assignment/ctor argument 303 305 *arg = addrExpr->get_arg(); 304 306 } else { 305 307 // no address-of operator, so must be a pointer - add dereference 306 UntypedExpr * newExpr = new UntypedExpr( new NameExpr( "*?" ) );308 UntypedExpr * newExpr = new UntypedExpr( new NameExpr( "*?" ) ); 307 309 newExpr->get_args().push_back( *arg ); 308 310 assert( (*arg)->get_results().size() == 1 ); … … 352 354 // no constructors with 0 or more than 2 parameters 353 355 assert( false ); 354 } 356 } // if 355 357 break; 356 358 … … 401 403 } 402 404 403 void CodeGenerator::visit( UntypedExpr * untypedExpr ) {405 void CodeGenerator::visit( UntypedExpr * untypedExpr ) { 404 406 extension( untypedExpr ); 405 if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {407 if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) { 406 408 OperatorInfo opInfo; 407 409 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) { … … 472 474 } // switch 473 475 } else { 474 if ( nameExpr->get_name() == " Range" ) { // case V1 ... V2 or case V1~V2476 if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2 475 477 assert( untypedExpr->get_args().size() == 2 ); 476 478 (*untypedExpr->get_args().begin())->accept( *this ); … … 492 494 } 493 495 494 void CodeGenerator::visit( NameExpr *nameExpr ) { 496 void CodeGenerator::visit( RangeExpr * rangeExpr ) { 497 rangeExpr->get_low()->accept( *this ); 498 output << " ... "; 499 rangeExpr->get_high()->accept( *this ); 500 } 501 502 void CodeGenerator::visit( NameExpr * nameExpr ) { 495 503 extension( nameExpr ); 496 504 OperatorInfo opInfo; … … 503 511 } 504 512 505 void CodeGenerator::visit( AddressExpr * addressExpr ) {513 void CodeGenerator::visit( AddressExpr * addressExpr ) { 506 514 extension( addressExpr ); 507 515 output << "(&"; 508 516 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address 509 if ( VariableExpr * variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {517 if ( VariableExpr * variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) { 510 518 output << mangleName( variableExpr->get_var() ); 511 519 } else { … … 515 523 } 516 524 517 void CodeGenerator::visit( CastExpr * castExpr ) {525 void CodeGenerator::visit( CastExpr * castExpr ) { 518 526 extension( castExpr ); 519 527 output << "("; … … 533 541 } 534 542 535 void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {543 void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) { 536 544 assert( false ); 537 545 } 538 546 539 void CodeGenerator::visit( MemberExpr * memberExpr ) {547 void CodeGenerator::visit( MemberExpr * memberExpr ) { 540 548 extension( memberExpr ); 541 549 memberExpr->get_aggregate()->accept( *this ); … … 543 551 } 544 552 545 void CodeGenerator::visit( VariableExpr * variableExpr ) {553 void CodeGenerator::visit( VariableExpr * variableExpr ) { 546 554 extension( variableExpr ); 547 555 OperatorInfo opInfo; … … 553 561 } 554 562 555 void CodeGenerator::visit( ConstantExpr * constantExpr ) {563 void CodeGenerator::visit( ConstantExpr * constantExpr ) { 556 564 assert( constantExpr->get_constant() ); 557 565 extension( constantExpr ); … … 559 567 } 560 568 561 void CodeGenerator::visit( SizeofExpr * sizeofExpr ) {569 void CodeGenerator::visit( SizeofExpr * sizeofExpr ) { 562 570 extension( sizeofExpr ); 563 571 output << "sizeof("; … … 570 578 } 571 579 572 void CodeGenerator::visit( AlignofExpr * alignofExpr ) {580 void CodeGenerator::visit( AlignofExpr * alignofExpr ) { 573 581 // use GCC extension to avoid bumping std to C11 574 582 extension( alignofExpr ); … … 582 590 } 583 591 584 void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) {592 void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) { 585 593 assert( false && "UntypedOffsetofExpr should not reach code generation." ); 586 594 } 587 595 588 void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) {596 void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) { 589 597 // use GCC builtin 590 598 output << "__builtin_offsetof("; … … 594 602 } 595 603 596 void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) {604 void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) { 597 605 assert( false && "OffsetPackExpr should not reach code generation." ); 598 606 } 599 607 600 void CodeGenerator::visit( LogicalExpr * logicalExpr ) {608 void CodeGenerator::visit( LogicalExpr * logicalExpr ) { 601 609 extension( logicalExpr ); 602 610 output << "("; … … 611 619 } 612 620 613 void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) {621 void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) { 614 622 extension( conditionalExpr ); 615 623 output << "("; … … 622 630 } 623 631 624 void CodeGenerator::visit( CommaExpr * commaExpr ) {632 void CodeGenerator::visit( CommaExpr * commaExpr ) { 625 633 extension( commaExpr ); 626 634 output << "("; … … 631 639 } 632 640 633 void CodeGenerator::visit( TupleExpr * tupleExpr ) {}634 635 void CodeGenerator::visit( TypeExpr * typeExpr ) {}636 637 void CodeGenerator::visit( AsmExpr * asmExpr ) {641 void CodeGenerator::visit( TupleExpr * tupleExpr ) {} 642 643 void CodeGenerator::visit( TypeExpr * typeExpr ) {} 644 645 void CodeGenerator::visit( AsmExpr * asmExpr ) { 638 646 if ( asmExpr->get_inout() ) { 639 647 output << "[ "; … … 648 656 649 657 //*** Statements 650 void CodeGenerator::visit( CompoundStmt * compoundStmt ) {658 void CodeGenerator::visit( CompoundStmt * compoundStmt ) { 651 659 std::list<Statement*> ks = compoundStmt->get_kids(); 652 660 output << "{" << endl; … … 662 670 output << endl; 663 671 } // if 664 } 672 } // for 665 673 cur_indent -= CodeGenerator::tabsize; 666 674 … … 668 676 } 669 677 670 void CodeGenerator::visit( ExprStmt * exprStmt ) {678 void CodeGenerator::visit( ExprStmt * exprStmt ) { 671 679 assert( exprStmt ); 672 680 // cast the top-level expression to void to reduce gcc warnings. … … 676 684 } 677 685 678 void CodeGenerator::visit( AsmStmt * asmStmt ) {686 void CodeGenerator::visit( AsmStmt * asmStmt ) { 679 687 output << "asm "; 680 688 if ( asmStmt->get_voltile() ) output << "volatile "; … … 699 707 } 700 708 701 void CodeGenerator::visit( IfStmt * ifStmt ) {709 void CodeGenerator::visit( IfStmt * ifStmt ) { 702 710 output << "if ( "; 703 711 ifStmt->get_condition()->accept( *this ); … … 712 720 } 713 721 714 void CodeGenerator::visit( SwitchStmt * switchStmt ) {722 void CodeGenerator::visit( SwitchStmt * switchStmt ) { 715 723 output << "switch ( " ; 716 724 switchStmt->get_condition()->accept( *this ); … … 719 727 output << "{" << std::endl; 720 728 cur_indent += CodeGenerator::tabsize; 721 722 acceptAll( switchStmt->get_branches(), *this ); 723 729 acceptAll( switchStmt->get_statements(), *this ); 724 730 cur_indent -= CodeGenerator::tabsize; 725 726 731 output << indent << "}"; 727 732 } 728 733 729 void CodeGenerator::visit( CaseStmt * caseStmt ) {734 void CodeGenerator::visit( CaseStmt * caseStmt ) { 730 735 output << indent; 731 736 if ( caseStmt->isDefault()) { … … 748 753 } 749 754 750 void CodeGenerator::visit( BranchStmt * branchStmt ) {755 void CodeGenerator::visit( BranchStmt * branchStmt ) { 751 756 switch ( branchStmt->get_type()) { 752 757 case BranchStmt::Goto: … … 771 776 772 777 773 void CodeGenerator::visit( ReturnStmt * returnStmt ) {778 void CodeGenerator::visit( ReturnStmt * returnStmt ) { 774 779 output << "return "; 775 780 maybeAccept( returnStmt->get_expr(), *this ); … … 777 782 } 778 783 779 void CodeGenerator::visit( WhileStmt * whileStmt ) {784 void CodeGenerator::visit( WhileStmt * whileStmt ) { 780 785 if ( whileStmt->get_isDoWhile() ) { 781 786 output << "do" ; … … 799 804 } 800 805 801 void CodeGenerator::visit( ForStmt * forStmt ) {806 void CodeGenerator::visit( ForStmt * forStmt ) { 802 807 // initialization is always hoisted, so don't bother doing anything with that 803 808 output << "for (;"; … … 821 826 } 822 827 823 void CodeGenerator::visit( NullStmt * nullStmt ) {828 void CodeGenerator::visit( NullStmt * nullStmt ) { 824 829 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() ); 825 830 output << "/* null statement */ ;"; 826 831 } 827 832 828 void CodeGenerator::visit( DeclStmt * declStmt ) {833 void CodeGenerator::visit( DeclStmt * declStmt ) { 829 834 declStmt->get_decl()->accept( *this ); 830 835 … … 834 839 } 835 840 836 void CodeGenerator::handleStorageClass( Declaration * decl ) {841 void CodeGenerator::handleStorageClass( Declaration * decl ) { 837 842 switch ( decl->get_storageClass() ) { 838 843 case DeclarationNode::Extern:
Note: See TracChangeset
for help on using the changeset viewer.