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