Changes in src/CodeGen/CodeGenerator.cc [064e3ff:f9cebb5]
- File:
-
- 1 edited
-
src/CodeGen/CodeGenerator.cc (modified) (45 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
r064e3ff 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 13:35:30201613 // Update Count : 3 5211 // 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( RangeExpr * rangeExpr ) { 495 rangeExpr->get_low()->accept( *this ); 496 output << " ... "; 497 rangeExpr->get_high()->accept( *this ); 498 } 499 500 void CodeGenerator::visit( NameExpr * nameExpr ) { 496 void CodeGenerator::visit( NameExpr *nameExpr ) { 501 497 extension( nameExpr ); 502 498 OperatorInfo opInfo; … … 509 505 } 510 506 511 void CodeGenerator::visit( AddressExpr * addressExpr ) {507 void CodeGenerator::visit( AddressExpr *addressExpr ) { 512 508 extension( addressExpr ); 513 509 output << "(&"; 514 510 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address 515 if ( VariableExpr * variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {511 if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) { 516 512 output << mangleName( variableExpr->get_var() ); 517 513 } else { … … 521 517 } 522 518 523 void CodeGenerator::visit( CastExpr * castExpr ) {519 void CodeGenerator::visit( CastExpr *castExpr ) { 524 520 extension( castExpr ); 525 521 output << "("; … … 539 535 } 540 536 541 void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {537 void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) { 542 538 assert( false ); 543 539 } 544 540 545 void CodeGenerator::visit( MemberExpr * memberExpr ) {541 void CodeGenerator::visit( MemberExpr *memberExpr ) { 546 542 extension( memberExpr ); 547 543 memberExpr->get_aggregate()->accept( *this ); … … 549 545 } 550 546 551 void CodeGenerator::visit( VariableExpr * variableExpr ) {547 void CodeGenerator::visit( VariableExpr *variableExpr ) { 552 548 extension( variableExpr ); 553 549 OperatorInfo opInfo; … … 559 555 } 560 556 561 void CodeGenerator::visit( ConstantExpr * constantExpr ) {557 void CodeGenerator::visit( ConstantExpr *constantExpr ) { 562 558 assert( constantExpr->get_constant() ); 563 559 extension( constantExpr ); … … 565 561 } 566 562 567 void CodeGenerator::visit( SizeofExpr * sizeofExpr ) {563 void CodeGenerator::visit( SizeofExpr *sizeofExpr ) { 568 564 extension( sizeofExpr ); 569 565 output << "sizeof("; … … 576 572 } 577 573 578 void CodeGenerator::visit( AlignofExpr * alignofExpr ) {574 void CodeGenerator::visit( AlignofExpr *alignofExpr ) { 579 575 // use GCC extension to avoid bumping std to C11 580 576 extension( alignofExpr ); … … 588 584 } 589 585 590 void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) {586 void CodeGenerator::visit( UntypedOffsetofExpr *offsetofExpr ) { 591 587 assert( false && "UntypedOffsetofExpr should not reach code generation." ); 592 588 } 593 589 594 void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) {590 void CodeGenerator::visit( OffsetofExpr *offsetofExpr ) { 595 591 // use GCC builtin 596 592 output << "__builtin_offsetof("; … … 600 596 } 601 597 602 void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) {598 void CodeGenerator::visit( OffsetPackExpr *offsetPackExpr ) { 603 599 assert( false && "OffsetPackExpr should not reach code generation." ); 604 600 } 605 601 606 void CodeGenerator::visit( LogicalExpr * logicalExpr ) {602 void CodeGenerator::visit( LogicalExpr *logicalExpr ) { 607 603 extension( logicalExpr ); 608 604 output << "("; … … 617 613 } 618 614 619 void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) {615 void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) { 620 616 extension( conditionalExpr ); 621 617 output << "("; … … 628 624 } 629 625 630 void CodeGenerator::visit( CommaExpr * commaExpr ) {626 void CodeGenerator::visit( CommaExpr *commaExpr ) { 631 627 extension( commaExpr ); 632 628 output << "("; … … 637 633 } 638 634 639 void CodeGenerator::visit( TupleExpr * tupleExpr ) {}640 641 void CodeGenerator::visit( TypeExpr * typeExpr ) {}642 643 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 ) { 644 640 if ( asmExpr->get_inout() ) { 645 641 output << "[ "; … … 654 650 655 651 //*** Statements 656 void CodeGenerator::visit( CompoundStmt * compoundStmt ) {652 void CodeGenerator::visit( CompoundStmt *compoundStmt ) { 657 653 std::list<Statement*> ks = compoundStmt->get_kids(); 658 654 output << "{" << endl; … … 668 664 output << endl; 669 665 } // if 670 } // for666 } 671 667 cur_indent -= CodeGenerator::tabsize; 672 668 … … 674 670 } 675 671 676 void CodeGenerator::visit( ExprStmt * exprStmt ) {672 void CodeGenerator::visit( ExprStmt *exprStmt ) { 677 673 assert( exprStmt ); 678 674 // cast the top-level expression to void to reduce gcc warnings. … … 682 678 } 683 679 684 void CodeGenerator::visit( AsmStmt * asmStmt ) {680 void CodeGenerator::visit( AsmStmt *asmStmt ) { 685 681 output << "asm "; 686 682 if ( asmStmt->get_voltile() ) output << "volatile "; … … 705 701 } 706 702 707 void CodeGenerator::visit( IfStmt * ifStmt ) {703 void CodeGenerator::visit( IfStmt *ifStmt ) { 708 704 output << "if ( "; 709 705 ifStmt->get_condition()->accept( *this ); … … 718 714 } 719 715 720 void CodeGenerator::visit( SwitchStmt * switchStmt ) {716 void CodeGenerator::visit( SwitchStmt *switchStmt ) { 721 717 output << "switch ( " ; 722 718 switchStmt->get_condition()->accept( *this ); … … 725 721 output << "{" << std::endl; 726 722 cur_indent += CodeGenerator::tabsize; 727 acceptAll( switchStmt->get_statements(), *this ); 723 724 acceptAll( switchStmt->get_branches(), *this ); 725 728 726 cur_indent -= CodeGenerator::tabsize; 727 729 728 output << indent << "}"; 730 729 } 731 730 732 void CodeGenerator::visit( CaseStmt * caseStmt ) {731 void CodeGenerator::visit( CaseStmt *caseStmt ) { 733 732 output << indent; 734 733 if ( caseStmt->isDefault()) { … … 751 750 } 752 751 753 void CodeGenerator::visit( BranchStmt * branchStmt ) {752 void CodeGenerator::visit( BranchStmt *branchStmt ) { 754 753 switch ( branchStmt->get_type()) { 755 754 case BranchStmt::Goto: … … 774 773 775 774 776 void CodeGenerator::visit( ReturnStmt * returnStmt ) {775 void CodeGenerator::visit( ReturnStmt *returnStmt ) { 777 776 output << "return "; 778 777 maybeAccept( returnStmt->get_expr(), *this ); … … 780 779 } 781 780 782 void CodeGenerator::visit( WhileStmt * whileStmt ) {781 void CodeGenerator::visit( WhileStmt *whileStmt ) { 783 782 if ( whileStmt->get_isDoWhile() ) { 784 783 output << "do" ; … … 802 801 } 803 802 804 void CodeGenerator::visit( ForStmt * forStmt ) {803 void CodeGenerator::visit( ForStmt *forStmt ) { 805 804 // initialization is always hoisted, so don't bother doing anything with that 806 805 output << "for (;"; … … 824 823 } 825 824 826 void CodeGenerator::visit( NullStmt * nullStmt ) {825 void CodeGenerator::visit( NullStmt *nullStmt ) { 827 826 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() ); 828 827 output << "/* null statement */ ;"; 829 828 } 830 829 831 void CodeGenerator::visit( DeclStmt * declStmt ) {830 void CodeGenerator::visit( DeclStmt *declStmt ) { 832 831 declStmt->get_decl()->accept( *this ); 833 832 … … 837 836 } 838 837 839 void CodeGenerator::handleStorageClass( Declaration * decl ) {838 void CodeGenerator::handleStorageClass( Declaration *decl ) { 840 839 switch ( decl->get_storageClass() ) { 841 840 case DeclarationNode::Extern:
Note:
See TracChangeset
for help on using the changeset viewer.