Changes in src/CodeGen/CodeGenerator.cc [8688ce1:f9cebb5]
- File:
-
- 1 edited
-
src/CodeGen/CodeGenerator.cc (modified) (45 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
r8688ce1 rf9cebb5 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Thu Aug 4 11:16:21201613 // Update Count : 3 5111 // Last Modified By : 12 // Last Modified On : Sun Jul 31 08:42:18 2016 13 // Update Count : 345 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 } // for81 } 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 } // if114 } 115 115 output << ","; 116 } // for116 } 117 117 output << ")) "; 118 } // if118 } 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 } // if279 genCommaList( init->begin(), init->end() ); 280 } 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 } // if356 } 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() == " ..." ) { // case V1 ... V2 or case V1~V2476 if ( nameExpr->get_name() == "Range" ) { // 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( NameExpr *nameExpr ) { 495 497 extension( nameExpr ); 496 498 OperatorInfo opInfo; … … 503 505 } 504 506 505 void CodeGenerator::visit( AddressExpr * addressExpr ) {507 void CodeGenerator::visit( AddressExpr *addressExpr ) { 506 508 extension( addressExpr ); 507 509 output << "(&"; 508 510 // 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() ) ) {511 if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) { 510 512 output << mangleName( variableExpr->get_var() ); 511 513 } else { … … 515 517 } 516 518 517 void CodeGenerator::visit( CastExpr * castExpr ) {519 void CodeGenerator::visit( CastExpr *castExpr ) { 518 520 extension( castExpr ); 519 521 output << "("; … … 533 535 } 534 536 535 void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {537 void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) { 536 538 assert( false ); 537 539 } 538 540 539 void CodeGenerator::visit( MemberExpr * memberExpr ) {541 void CodeGenerator::visit( MemberExpr *memberExpr ) { 540 542 extension( memberExpr ); 541 543 memberExpr->get_aggregate()->accept( *this ); … … 543 545 } 544 546 545 void CodeGenerator::visit( VariableExpr * variableExpr ) {547 void CodeGenerator::visit( VariableExpr *variableExpr ) { 546 548 extension( variableExpr ); 547 549 OperatorInfo opInfo; … … 553 555 } 554 556 555 void CodeGenerator::visit( ConstantExpr * constantExpr ) {557 void CodeGenerator::visit( ConstantExpr *constantExpr ) { 556 558 assert( constantExpr->get_constant() ); 557 559 extension( constantExpr ); … … 559 561 } 560 562 561 void CodeGenerator::visit( SizeofExpr * sizeofExpr ) {563 void CodeGenerator::visit( SizeofExpr *sizeofExpr ) { 562 564 extension( sizeofExpr ); 563 565 output << "sizeof("; … … 570 572 } 571 573 572 void CodeGenerator::visit( AlignofExpr * alignofExpr ) {574 void CodeGenerator::visit( AlignofExpr *alignofExpr ) { 573 575 // use GCC extension to avoid bumping std to C11 574 576 extension( alignofExpr ); … … 582 584 } 583 585 584 void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) {586 void CodeGenerator::visit( UntypedOffsetofExpr *offsetofExpr ) { 585 587 assert( false && "UntypedOffsetofExpr should not reach code generation." ); 586 588 } 587 589 588 void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) {590 void CodeGenerator::visit( OffsetofExpr *offsetofExpr ) { 589 591 // use GCC builtin 590 592 output << "__builtin_offsetof("; … … 594 596 } 595 597 596 void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) {598 void CodeGenerator::visit( OffsetPackExpr *offsetPackExpr ) { 597 599 assert( false && "OffsetPackExpr should not reach code generation." ); 598 600 } 599 601 600 void CodeGenerator::visit( LogicalExpr * logicalExpr ) {602 void CodeGenerator::visit( LogicalExpr *logicalExpr ) { 601 603 extension( logicalExpr ); 602 604 output << "("; … … 611 613 } 612 614 613 void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) {615 void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) { 614 616 extension( conditionalExpr ); 615 617 output << "("; … … 622 624 } 623 625 624 void CodeGenerator::visit( CommaExpr * commaExpr ) {626 void CodeGenerator::visit( CommaExpr *commaExpr ) { 625 627 extension( commaExpr ); 626 628 output << "("; … … 631 633 } 632 634 633 void CodeGenerator::visit( TupleExpr * tupleExpr ) {}634 635 void CodeGenerator::visit( TypeExpr * typeExpr ) {}636 637 void CodeGenerator::visit( AsmExpr * asmExpr ) {635 void CodeGenerator::visit( TupleExpr *tupleExpr ) {} 636 637 void CodeGenerator::visit( TypeExpr *typeExpr ) {} 638 639 void CodeGenerator::visit( AsmExpr *asmExpr ) { 638 640 if ( asmExpr->get_inout() ) { 639 641 output << "[ "; … … 648 650 649 651 //*** Statements 650 void CodeGenerator::visit( CompoundStmt * compoundStmt ) {652 void CodeGenerator::visit( CompoundStmt *compoundStmt ) { 651 653 std::list<Statement*> ks = compoundStmt->get_kids(); 652 654 output << "{" << endl; … … 662 664 output << endl; 663 665 } // if 664 } // for666 } 665 667 cur_indent -= CodeGenerator::tabsize; 666 668 … … 668 670 } 669 671 670 void CodeGenerator::visit( ExprStmt * exprStmt ) {672 void CodeGenerator::visit( ExprStmt *exprStmt ) { 671 673 assert( exprStmt ); 672 674 // cast the top-level expression to void to reduce gcc warnings. … … 676 678 } 677 679 678 void CodeGenerator::visit( AsmStmt * asmStmt ) {680 void CodeGenerator::visit( AsmStmt *asmStmt ) { 679 681 output << "asm "; 680 682 if ( asmStmt->get_voltile() ) output << "volatile "; … … 699 701 } 700 702 701 void CodeGenerator::visit( IfStmt * ifStmt ) {703 void CodeGenerator::visit( IfStmt *ifStmt ) { 702 704 output << "if ( "; 703 705 ifStmt->get_condition()->accept( *this ); … … 712 714 } 713 715 714 void CodeGenerator::visit( SwitchStmt * switchStmt ) {716 void CodeGenerator::visit( SwitchStmt *switchStmt ) { 715 717 output << "switch ( " ; 716 718 switchStmt->get_condition()->accept( *this ); … … 719 721 output << "{" << std::endl; 720 722 cur_indent += CodeGenerator::tabsize; 721 acceptAll( switchStmt->get_statements(), *this ); 723 724 acceptAll( switchStmt->get_branches(), *this ); 725 722 726 cur_indent -= CodeGenerator::tabsize; 727 723 728 output << indent << "}"; 724 729 } 725 730 726 void CodeGenerator::visit( CaseStmt * caseStmt ) {731 void CodeGenerator::visit( CaseStmt *caseStmt ) { 727 732 output << indent; 728 733 if ( caseStmt->isDefault()) { … … 745 750 } 746 751 747 void CodeGenerator::visit( BranchStmt * branchStmt ) {752 void CodeGenerator::visit( BranchStmt *branchStmt ) { 748 753 switch ( branchStmt->get_type()) { 749 754 case BranchStmt::Goto: … … 768 773 769 774 770 void CodeGenerator::visit( ReturnStmt * returnStmt ) {775 void CodeGenerator::visit( ReturnStmt *returnStmt ) { 771 776 output << "return "; 772 777 maybeAccept( returnStmt->get_expr(), *this ); … … 774 779 } 775 780 776 void CodeGenerator::visit( WhileStmt * whileStmt ) {781 void CodeGenerator::visit( WhileStmt *whileStmt ) { 777 782 if ( whileStmt->get_isDoWhile() ) { 778 783 output << "do" ; … … 796 801 } 797 802 798 void CodeGenerator::visit( ForStmt * forStmt ) {803 void CodeGenerator::visit( ForStmt *forStmt ) { 799 804 // initialization is always hoisted, so don't bother doing anything with that 800 805 output << "for (;"; … … 818 823 } 819 824 820 void CodeGenerator::visit( NullStmt * nullStmt ) {825 void CodeGenerator::visit( NullStmt *nullStmt ) { 821 826 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() ); 822 827 output << "/* null statement */ ;"; 823 828 } 824 829 825 void CodeGenerator::visit( DeclStmt * declStmt ) {830 void CodeGenerator::visit( DeclStmt *declStmt ) { 826 831 declStmt->get_decl()->accept( *this ); 827 832 … … 831 836 } 832 837 833 void CodeGenerator::handleStorageClass( Declaration * decl ) {838 void CodeGenerator::handleStorageClass( Declaration *decl ) { 834 839 switch ( decl->get_storageClass() ) { 835 840 case DeclarationNode::Extern:
Note:
See TracChangeset
for help on using the changeset viewer.