Changes in src/CodeGen/CodeGenerator.cc [f9cebb5:8688ce1]
- File:
-
- 1 edited
-
src/CodeGen/CodeGenerator.cc (modified) (45 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
rf9cebb5 r8688ce1 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 11:16:21 2016 13 // Update Count : 351 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 152 150 handleStorageClass( objectDecl ); 153 151 output << genType( objectDecl->get_type(), mangleName( objectDecl ) ); … … 164 162 } 165 163 166 void CodeGenerator::handleAggregate( AggregateDecl * aggDecl ) {164 void CodeGenerator::handleAggregate( AggregateDecl * aggDecl ) { 167 165 if ( aggDecl->get_name() != "" ) 168 166 output << aggDecl->get_name(); 169 167 170 std::list< Declaration * > & memb = aggDecl->get_members();168 std::list< Declaration * > & memb = aggDecl->get_members(); 171 169 if ( ! memb.empty() ) { 172 170 // if ( aggDecl->has_body() ) { 173 // std::list< Declaration * > & memb = aggDecl->get_members();171 // std::list< Declaration * > & memb = aggDecl->get_members(); 174 172 output << " {" << endl; 175 173 … … 187 185 } 188 186 189 void CodeGenerator::visit( StructDecl * structDecl ) {187 void CodeGenerator::visit( StructDecl * structDecl ) { 190 188 extension( structDecl ); 191 189 output << "struct "; … … 193 191 } 194 192 195 void CodeGenerator::visit( UnionDecl * unionDecl ) {193 void CodeGenerator::visit( UnionDecl * unionDecl ) { 196 194 extension( unionDecl ); 197 195 output << "union "; … … 199 197 } 200 198 201 void CodeGenerator::visit( EnumDecl * enumDecl ) {199 void CodeGenerator::visit( EnumDecl * enumDecl ) { 202 200 extension( enumDecl ); 203 201 output << "enum "; … … 213 211 cur_indent += CodeGenerator::tabsize; 214 212 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { 215 ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );213 ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i ); 216 214 assert( obj ); 217 215 output << indent << mangleName( obj ); … … 229 227 } 230 228 231 void CodeGenerator::visit( TraitDecl * traitDecl ) {}232 233 void CodeGenerator::visit( TypedefDecl * typeDecl ) {229 void CodeGenerator::visit( TraitDecl * traitDecl ) {} 230 231 void CodeGenerator::visit( TypedefDecl * typeDecl ) { 234 232 assert( false && "Typedefs are removed and substituted in earlier passes." ); 235 233 //output << "typedef "; … … 237 235 } 238 236 239 void CodeGenerator::visit( TypeDecl * typeDecl ) {237 void CodeGenerator::visit( TypeDecl * typeDecl ) { 240 238 // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes, 241 239 // still to be done … … 265 263 } 266 264 267 void CodeGenerator::visit( SingleInit * init ) {265 void CodeGenerator::visit( SingleInit * init ) { 268 266 printDesignators( init->get_designators() ); 269 267 init->get_value()->accept( *this ); 270 268 } 271 269 272 void CodeGenerator::visit( ListInit * init ) {270 void CodeGenerator::visit( ListInit * init ) { 273 271 printDesignators( init->get_designators() ); 274 272 output << "{ "; 275 if ( init->begin () == init->end() ) {273 if ( init->begin_initializers() == init->end_initializers() ) { 276 274 // illegal to leave initializer list empty for scalar initializers, but always legal to have 0 277 275 output << "0"; 278 276 } else { 279 genCommaList( init->begin (), init->end() );280 } 277 genCommaList( init->begin_initializers(), init->end_initializers() ); 278 } // if 281 279 output << " }"; 282 280 } 283 281 284 void CodeGenerator::visit( Constant * constant ) {282 void CodeGenerator::visit( Constant * constant ) { 285 283 output << constant->get_value() ; 286 284 } 287 285 288 286 //*** Expressions 289 void CodeGenerator::visit( ApplicationExpr * applicationExpr ) {287 void CodeGenerator::visit( ApplicationExpr * applicationExpr ) { 290 288 extension( applicationExpr ); 291 if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {289 if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) { 292 290 OperatorInfo opInfo; 293 291 if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) { … … 301 299 { 302 300 assert( arg != applicationExpr->get_args().end() ); 303 if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {301 if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) { 304 302 // remove & from first assignment/ctor argument 305 303 *arg = addrExpr->get_arg(); 306 304 } else { 307 305 // no address-of operator, so must be a pointer - add dereference 308 UntypedExpr * newExpr = new UntypedExpr( new NameExpr( "*?" ) );306 UntypedExpr * newExpr = new UntypedExpr( new NameExpr( "*?" ) ); 309 307 newExpr->get_args().push_back( *arg ); 310 308 assert( (*arg)->get_results().size() == 1 ); … … 354 352 // no constructors with 0 or more than 2 parameters 355 353 assert( false ); 356 } 354 } // if 357 355 break; 358 356 … … 403 401 } 404 402 405 void CodeGenerator::visit( UntypedExpr * untypedExpr ) {403 void CodeGenerator::visit( UntypedExpr * untypedExpr ) { 406 404 extension( untypedExpr ); 407 if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {405 if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) { 408 406 OperatorInfo opInfo; 409 407 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) { … … 474 472 } // switch 475 473 } else { 476 if ( nameExpr->get_name() == " Range" ) { // case V1 ... V2 or case V1~V2474 if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2 477 475 assert( untypedExpr->get_args().size() == 2 ); 478 476 (*untypedExpr->get_args().begin())->accept( *this ); … … 494 492 } 495 493 496 void CodeGenerator::visit( NameExpr * nameExpr ) {494 void CodeGenerator::visit( NameExpr * nameExpr ) { 497 495 extension( nameExpr ); 498 496 OperatorInfo opInfo; … … 505 503 } 506 504 507 void CodeGenerator::visit( AddressExpr * addressExpr ) {505 void CodeGenerator::visit( AddressExpr * addressExpr ) { 508 506 extension( addressExpr ); 509 507 output << "(&"; 510 508 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address 511 if ( VariableExpr * variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {509 if ( VariableExpr * variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) { 512 510 output << mangleName( variableExpr->get_var() ); 513 511 } else { … … 517 515 } 518 516 519 void CodeGenerator::visit( CastExpr * castExpr ) {517 void CodeGenerator::visit( CastExpr * castExpr ) { 520 518 extension( castExpr ); 521 519 output << "("; … … 535 533 } 536 534 537 void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {535 void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) { 538 536 assert( false ); 539 537 } 540 538 541 void CodeGenerator::visit( MemberExpr * memberExpr ) {539 void CodeGenerator::visit( MemberExpr * memberExpr ) { 542 540 extension( memberExpr ); 543 541 memberExpr->get_aggregate()->accept( *this ); … … 545 543 } 546 544 547 void CodeGenerator::visit( VariableExpr * variableExpr ) {545 void CodeGenerator::visit( VariableExpr * variableExpr ) { 548 546 extension( variableExpr ); 549 547 OperatorInfo opInfo; … … 555 553 } 556 554 557 void CodeGenerator::visit( ConstantExpr * constantExpr ) {555 void CodeGenerator::visit( ConstantExpr * constantExpr ) { 558 556 assert( constantExpr->get_constant() ); 559 557 extension( constantExpr ); … … 561 559 } 562 560 563 void CodeGenerator::visit( SizeofExpr * sizeofExpr ) {561 void CodeGenerator::visit( SizeofExpr * sizeofExpr ) { 564 562 extension( sizeofExpr ); 565 563 output << "sizeof("; … … 572 570 } 573 571 574 void CodeGenerator::visit( AlignofExpr * alignofExpr ) {572 void CodeGenerator::visit( AlignofExpr * alignofExpr ) { 575 573 // use GCC extension to avoid bumping std to C11 576 574 extension( alignofExpr ); … … 584 582 } 585 583 586 void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) {584 void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) { 587 585 assert( false && "UntypedOffsetofExpr should not reach code generation." ); 588 586 } 589 587 590 void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) {588 void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) { 591 589 // use GCC builtin 592 590 output << "__builtin_offsetof("; … … 596 594 } 597 595 598 void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) {596 void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) { 599 597 assert( false && "OffsetPackExpr should not reach code generation." ); 600 598 } 601 599 602 void CodeGenerator::visit( LogicalExpr * logicalExpr ) {600 void CodeGenerator::visit( LogicalExpr * logicalExpr ) { 603 601 extension( logicalExpr ); 604 602 output << "("; … … 613 611 } 614 612 615 void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) {613 void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) { 616 614 extension( conditionalExpr ); 617 615 output << "("; … … 624 622 } 625 623 626 void CodeGenerator::visit( CommaExpr * commaExpr ) {624 void CodeGenerator::visit( CommaExpr * commaExpr ) { 627 625 extension( commaExpr ); 628 626 output << "("; … … 633 631 } 634 632 635 void CodeGenerator::visit( TupleExpr * tupleExpr ) {}636 637 void CodeGenerator::visit( TypeExpr * typeExpr ) {}638 639 void CodeGenerator::visit( AsmExpr * asmExpr ) {633 void CodeGenerator::visit( TupleExpr * tupleExpr ) {} 634 635 void CodeGenerator::visit( TypeExpr * typeExpr ) {} 636 637 void CodeGenerator::visit( AsmExpr * asmExpr ) { 640 638 if ( asmExpr->get_inout() ) { 641 639 output << "[ "; … … 650 648 651 649 //*** Statements 652 void CodeGenerator::visit( CompoundStmt * compoundStmt ) {650 void CodeGenerator::visit( CompoundStmt * compoundStmt ) { 653 651 std::list<Statement*> ks = compoundStmt->get_kids(); 654 652 output << "{" << endl; … … 664 662 output << endl; 665 663 } // if 666 } 664 } // for 667 665 cur_indent -= CodeGenerator::tabsize; 668 666 … … 670 668 } 671 669 672 void CodeGenerator::visit( ExprStmt * exprStmt ) {670 void CodeGenerator::visit( ExprStmt * exprStmt ) { 673 671 assert( exprStmt ); 674 672 // cast the top-level expression to void to reduce gcc warnings. … … 678 676 } 679 677 680 void CodeGenerator::visit( AsmStmt * asmStmt ) {678 void CodeGenerator::visit( AsmStmt * asmStmt ) { 681 679 output << "asm "; 682 680 if ( asmStmt->get_voltile() ) output << "volatile "; … … 701 699 } 702 700 703 void CodeGenerator::visit( IfStmt * ifStmt ) {701 void CodeGenerator::visit( IfStmt * ifStmt ) { 704 702 output << "if ( "; 705 703 ifStmt->get_condition()->accept( *this ); … … 714 712 } 715 713 716 void CodeGenerator::visit( SwitchStmt * switchStmt ) {714 void CodeGenerator::visit( SwitchStmt * switchStmt ) { 717 715 output << "switch ( " ; 718 716 switchStmt->get_condition()->accept( *this ); … … 721 719 output << "{" << std::endl; 722 720 cur_indent += CodeGenerator::tabsize; 723 724 acceptAll( switchStmt->get_branches(), *this ); 725 721 acceptAll( switchStmt->get_statements(), *this ); 726 722 cur_indent -= CodeGenerator::tabsize; 727 728 723 output << indent << "}"; 729 724 } 730 725 731 void CodeGenerator::visit( CaseStmt * caseStmt ) {726 void CodeGenerator::visit( CaseStmt * caseStmt ) { 732 727 output << indent; 733 728 if ( caseStmt->isDefault()) { … … 750 745 } 751 746 752 void CodeGenerator::visit( BranchStmt * branchStmt ) {747 void CodeGenerator::visit( BranchStmt * branchStmt ) { 753 748 switch ( branchStmt->get_type()) { 754 749 case BranchStmt::Goto: … … 773 768 774 769 775 void CodeGenerator::visit( ReturnStmt * returnStmt ) {770 void CodeGenerator::visit( ReturnStmt * returnStmt ) { 776 771 output << "return "; 777 772 maybeAccept( returnStmt->get_expr(), *this ); … … 779 774 } 780 775 781 void CodeGenerator::visit( WhileStmt * whileStmt ) {776 void CodeGenerator::visit( WhileStmt * whileStmt ) { 782 777 if ( whileStmt->get_isDoWhile() ) { 783 778 output << "do" ; … … 801 796 } 802 797 803 void CodeGenerator::visit( ForStmt * forStmt ) {798 void CodeGenerator::visit( ForStmt * forStmt ) { 804 799 // initialization is always hoisted, so don't bother doing anything with that 805 800 output << "for (;"; … … 823 818 } 824 819 825 void CodeGenerator::visit( NullStmt * nullStmt ) {820 void CodeGenerator::visit( NullStmt * nullStmt ) { 826 821 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() ); 827 822 output << "/* null statement */ ;"; 828 823 } 829 824 830 void CodeGenerator::visit( DeclStmt * declStmt ) {825 void CodeGenerator::visit( DeclStmt * declStmt ) { 831 826 declStmt->get_decl()->accept( *this ); 832 827 … … 836 831 } 837 832 838 void CodeGenerator::handleStorageClass( Declaration * decl ) {833 void CodeGenerator::handleStorageClass( Declaration * decl ) { 839 834 switch ( decl->get_storageClass() ) { 840 835 case DeclarationNode::Extern:
Note:
See TracChangeset
for help on using the changeset viewer.