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