Changeset 42a36d9 for src/CodeGen/CodeGenerator.cc
- Timestamp:
- May 1, 2019, 3:32:10 PM (5 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 5157ba7
- Parents:
- 3908e5d
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
r3908e5d r42a36d9 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 : Sat May 5 09:08:32 201813 // Update Count : 49 411 // Last Modified By : Andrew Beach 12 // Last Modified On : Wed May 1 10:06:00 2019 13 // Update Count : 495 14 14 // 15 15 #include "CodeGenerator.h" … … 83 83 void CodeGenerator::updateLocation( CodeLocation const & to ) { 84 84 // skip if linemarks shouldn't appear or if codelocation is unset 85 if ( ! lineMarks || to.isUnset() ) return;85 if ( !options.lineMarks || to.isUnset() ) return; 86 86 87 87 if ( currentLocation.followedBy( to, 0 ) ) { … … 116 116 } 117 117 118 CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks, bool printExprTypes ) : indent( CodeGenerator::tabsize ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ), printExprTypes( printExprTypes ), endl( *this ) {} 118 CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks, bool printExprTypes ) : indent( CodeGenerator::tabsize ), output( os ), printLabels( *this ), options( pretty, genC, lineMarks, printExprTypes ), endl( *this ) {} 119 CodeGenerator::CodeGenerator( std::ostream & os, const Options &options ) : indent( CodeGenerator::tabsize ), output( os ), printLabels( *this ), options(options), endl( *this ) {} 119 120 120 121 string CodeGenerator::mangleName( DeclarationWithType * decl ) { 121 122 // GCC builtins should always be printed unmangled 122 if ( pretty || decl->linkage.is_gcc_builtin ) return decl->name;123 if ( options.pretty || decl->linkage.is_gcc_builtin ) return decl->name; 123 124 if ( decl->mangleName != "" ) { 124 125 // need to incorporate scope level in order to differentiate names for destructors … … 164 165 previsit( (BaseSyntaxNode *)node ); 165 166 GuardAction( [this, node](){ 166 if ( printExprTypes && node->result ) {167 output << " /* " << genType( node->result, "", pretty, genC) << " */ ";167 if ( options.printExprTypes && node->result ) { 168 output << " /* " << genType( node->result, "", options ) << " */ "; 168 169 } 169 170 } ); … … 173 174 void CodeGenerator::postvisit( FunctionDecl * functionDecl ) { 174 175 // deleted decls should never be used, so don't print them 175 if ( functionDecl->isDeleted && genC ) return;176 if ( functionDecl->isDeleted && options.genC ) return; 176 177 extension( functionDecl ); 177 178 genAttributes( functionDecl->get_attributes() ); … … 180 181 functionDecl->get_funcSpec().print( output ); 181 182 182 output << genType( functionDecl->get_functionType(), mangleName( functionDecl ), pretty,genC );183 output << genType( functionDecl->get_functionType(), mangleName( functionDecl ), options.pretty, options.genC ); 183 184 184 185 asmName( functionDecl ); … … 194 195 void CodeGenerator::postvisit( ObjectDecl * objectDecl ) { 195 196 // deleted decls should never be used, so don't print them 196 if ( objectDecl->isDeleted && genC ) return;197 if (objectDecl->get_name().empty() && genC ) {197 if ( objectDecl->isDeleted && options.genC ) return; 198 if (objectDecl->get_name().empty() && options.genC ) { 198 199 // only generate an anonymous name when generating C code, otherwise it clutters the output too much 199 200 static UniqueName name = { "__anonymous_object" }; … … 205 206 206 207 handleStorageClass( objectDecl ); 207 output << genType( objectDecl->get_type(), mangleName( objectDecl ), pretty,genC );208 output << genType( objectDecl->get_type(), mangleName( objectDecl ), options.pretty, options.genC ); 208 209 209 210 asmName( objectDecl ); … … 224 225 225 226 void CodeGenerator::handleAggregate( AggregateDecl * aggDecl, const std::string & kind ) { 226 if( ! aggDecl->parameters.empty() && ! genC ) {227 if( ! aggDecl->parameters.empty() && ! options.genC ) { 227 228 // assertf( ! genC, "Aggregate type parameters should not reach code generation." ); 228 229 output << "forall("; … … 294 295 295 296 void CodeGenerator::postvisit( TraitDecl * traitDecl ) { 296 assertf( ! genC, "TraitDecls should not reach code generation." );297 assertf( ! options.genC, "TraitDecls should not reach code generation." ); 297 298 extension( traitDecl ); 298 299 handleAggregate( traitDecl, "trait " ); … … 300 301 301 302 void CodeGenerator::postvisit( TypedefDecl * typeDecl ) { 302 assertf( ! genC, "Typedefs are removed and substituted in earlier passes." );303 assertf( ! options.genC, "Typedefs are removed and substituted in earlier passes." ); 303 304 output << "typedef "; 304 output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC) << endl;305 output << genType( typeDecl->get_base(), typeDecl->get_name(), options ) << endl; 305 306 } 306 307 307 308 void CodeGenerator::postvisit( TypeDecl * typeDecl ) { 308 assertf( ! genC, "TypeDecls should not reach code generation." );309 assertf( ! options.genC, "TypeDecls should not reach code generation." ); 309 310 output << typeDecl->genTypeString() << " " << typeDecl->name; 310 311 if ( typeDecl->sized ) { … … 371 372 372 373 void CodeGenerator::postvisit( ConstructorInit * init ){ 373 assertf( ! genC, "ConstructorInit nodes should not reach code generation." );374 assertf( ! options.genC, "ConstructorInit nodes should not reach code generation." ); 374 375 // pseudo-output for constructor/destructor pairs 375 376 output << "<ctorinit>{" << endl << ++indent << "ctor: "; … … 507 508 } else { 508 509 // no constructors with 0 or more than 2 parameters 509 assertf( ! genC, "UntypedExpr constructor/destructor with 0 or more than 2 parameters." );510 assertf( ! options.genC, "UntypedExpr constructor/destructor with 0 or more than 2 parameters." ); 510 511 output << "("; 511 512 (*arg++)->accept( *visitor ); … … 604 605 // an lvalue cast, this has been taken out. 605 606 output << "("; 606 output << genType( castExpr->get_result(), "", pretty, genC);607 output << genType( castExpr->get_result(), "", options ); 607 608 output << ")"; 608 609 } // if … … 612 613 613 614 void CodeGenerator::postvisit( KeywordCastExpr * castExpr ) { 614 assertf( ! genC, "KeywordCast should not reach code generation." );615 assertf( ! options.genC, "KeywordCast should not reach code generation." ); 615 616 extension( castExpr ); 616 617 output << "((" << castExpr->targetString() << " &)"; … … 620 621 621 622 void CodeGenerator::postvisit( VirtualCastExpr * castExpr ) { 622 assertf( ! genC, "VirtualCastExpr should not reach code generation." );623 assertf( ! options.genC, "VirtualCastExpr should not reach code generation." ); 623 624 extension( castExpr ); 624 625 output << "(virtual "; … … 628 629 629 630 void CodeGenerator::postvisit( UntypedMemberExpr * memberExpr ) { 630 assertf( ! genC, "UntypedMemberExpr should not reach code generation." );631 assertf( ! options.genC, "UntypedMemberExpr should not reach code generation." ); 631 632 extension( memberExpr ); 632 633 memberExpr->get_aggregate()->accept( *visitor ); … … 661 662 output << "sizeof("; 662 663 if ( sizeofExpr->get_isType() ) { 663 output << genType( sizeofExpr->get_type(), "", pretty, genC);664 output << genType( sizeofExpr->get_type(), "", options ); 664 665 } else { 665 666 sizeofExpr->get_expr()->accept( *visitor ); … … 673 674 output << "__alignof__("; 674 675 if ( alignofExpr->get_isType() ) { 675 output << genType( alignofExpr->get_type(), "", pretty, genC);676 output << genType( alignofExpr->get_type(), "", options ); 676 677 } else { 677 678 alignofExpr->get_expr()->accept( *visitor ); … … 681 682 682 683 void CodeGenerator::postvisit( UntypedOffsetofExpr * offsetofExpr ) { 683 assertf( ! genC, "UntypedOffsetofExpr should not reach code generation." );684 assertf( ! options.genC, "UntypedOffsetofExpr should not reach code generation." ); 684 685 output << "offsetof("; 685 output << genType( offsetofExpr->get_type(), "", pretty, genC);686 output << genType( offsetofExpr->get_type(), "", options ); 686 687 output << ", " << offsetofExpr->get_member(); 687 688 output << ")"; … … 691 692 // use GCC builtin 692 693 output << "__builtin_offsetof("; 693 output << genType( offsetofExpr->get_type(), "", pretty, genC);694 output << genType( offsetofExpr->get_type(), "", options ); 694 695 output << ", " << mangleName( offsetofExpr->get_member() ); 695 696 output << ")"; … … 697 698 698 699 void CodeGenerator::postvisit( OffsetPackExpr * offsetPackExpr ) { 699 assertf( ! genC, "OffsetPackExpr should not reach code generation." );700 output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", pretty, genC) << ")";700 assertf( ! options.genC, "OffsetPackExpr should not reach code generation." ); 701 output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", options ) << ")"; 701 702 } 702 703 … … 728 729 extension( commaExpr ); 729 730 output << "("; 730 if ( genC ) {731 if ( options.genC ) { 731 732 // arg1 of a CommaExpr is never used, so it can be safely cast to void to reduce gcc warnings. 732 733 commaExpr->set_arg1( new CastExpr( commaExpr->get_arg1() ) ); … … 739 740 740 741 void CodeGenerator::postvisit( TupleAssignExpr * tupleExpr ) { 741 assertf( ! genC, "TupleAssignExpr should not reach code generation." );742 assertf( ! options.genC, "TupleAssignExpr should not reach code generation." ); 742 743 tupleExpr->stmtExpr->accept( *visitor ); 743 744 } 744 745 745 746 void CodeGenerator::postvisit( UntypedTupleExpr * tupleExpr ) { 746 assertf( ! genC, "UntypedTupleExpr should not reach code generation." );747 assertf( ! options.genC, "UntypedTupleExpr should not reach code generation." ); 747 748 extension( tupleExpr ); 748 749 output << "["; … … 752 753 753 754 void CodeGenerator::postvisit( TupleExpr * tupleExpr ) { 754 assertf( ! genC, "TupleExpr should not reach code generation." );755 assertf( ! options.genC, "TupleExpr should not reach code generation." ); 755 756 extension( tupleExpr ); 756 757 output << "["; … … 760 761 761 762 void CodeGenerator::postvisit( TupleIndexExpr * tupleExpr ) { 762 assertf( ! genC, "TupleIndexExpr should not reach code generation." );763 assertf( ! options.genC, "TupleIndexExpr should not reach code generation." ); 763 764 extension( tupleExpr ); 764 765 tupleExpr->get_tuple()->accept( *visitor ); … … 767 768 768 769 void CodeGenerator::postvisit( TypeExpr * typeExpr ) { 769 // if ( genC ) std::cerr << "typeexpr still exists: " << typeExpr << std::endl;770 // assertf( ! genC, "TypeExpr should not reach code generation." );771 if ( ! genC ) {772 output << genType( typeExpr->get_type(), "", pretty, genC);770 // if ( options.genC ) std::cerr << "typeexpr still exists: " << typeExpr << std::endl; 771 // assertf( ! options.genC, "TypeExpr should not reach code generation." ); 772 if ( ! options.genC ) { 773 output << genType( typeExpr->get_type(), "", options ); 773 774 } 774 775 } … … 788 789 void CodeGenerator::postvisit( CompoundLiteralExpr *compLitExpr ) { 789 790 assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) ); 790 output << "(" << genType( compLitExpr->get_result(), "", pretty, genC) << ")";791 output << "(" << genType( compLitExpr->get_result(), "", options ) << ")"; 791 792 compLitExpr->get_initializer()->accept( *visitor ); 792 793 } 793 794 794 795 void CodeGenerator::postvisit( UniqueExpr * unqExpr ) { 795 assertf( ! genC, "Unique expressions should not reach code generation." );796 assertf( ! options.genC, "Unique expressions should not reach code generation." ); 796 797 output << "unq<" << unqExpr->get_id() << ">{ "; 797 798 unqExpr->get_expr()->accept( *visitor ); … … 829 830 830 831 void CodeGenerator::postvisit( ConstructorExpr * expr ) { 831 assertf( ! genC, "Unique expressions should not reach code generation." );832 assertf( ! options.genC, "Unique expressions should not reach code generation." ); 832 833 expr->callExpr->accept( *visitor ); 833 834 } 834 835 835 836 void CodeGenerator::postvisit( DeletedExpr * expr ) { 836 assertf( ! genC, "Deleted expressions should not reach code generation." );837 assertf( ! options.genC, "Deleted expressions should not reach code generation." ); 837 838 expr->expr->accept( *visitor ); 838 839 } 839 840 840 841 void CodeGenerator::postvisit( DefaultArgExpr * arg ) { 841 assertf( ! genC, "Default argument expressions should not reach code generation." );842 assertf( ! options.genC, "Default argument expressions should not reach code generation." ); 842 843 arg->expr->accept( *visitor ); 843 844 } 844 845 845 846 void CodeGenerator::postvisit( GenericExpr * expr ) { 846 assertf( ! genC, "C11 _Generic expressions should not reach code generation." );847 assertf( ! options.genC, "C11 _Generic expressions should not reach code generation." ); 847 848 output << "_Generic("; 848 849 expr->control->accept( *visitor ); … … 854 855 output << "default: "; 855 856 } else { 856 output << genType( assoc.type, "", pretty, genC) << ": ";857 output << genType( assoc.type, "", options ) << ": "; 857 858 } 858 859 assoc.expr->accept( *visitor ); … … 889 890 void CodeGenerator::postvisit( ExprStmt * exprStmt ) { 890 891 assert( exprStmt ); 891 if ( genC ) {892 if ( options.genC ) { 892 893 // cast the top-level expression to void to reduce gcc warnings. 893 894 exprStmt->set_expr( new CastExpr( exprStmt->get_expr() ) ); … … 999 1000 case BranchStmt::FallThrough: 1000 1001 case BranchStmt::FallThroughDefault: 1001 assertf( ! genC, "fallthru should not reach code generation." );1002 assertf( ! options.genC, "fallthru should not reach code generation." ); 1002 1003 output << "fallthru"; 1003 1004 break; 1004 1005 } // switch 1005 1006 // print branch target for labelled break/continue/fallthru in debug mode 1006 if ( ! genC && branchStmt->get_type() != BranchStmt::Goto ) {1007 if ( ! options.genC && branchStmt->get_type() != BranchStmt::Goto ) { 1007 1008 if ( ! branchStmt->get_target().empty() ) { 1008 1009 output << " " << branchStmt->get_target(); … … 1021 1022 1022 1023 void CodeGenerator::postvisit( ThrowStmt * throwStmt ) { 1023 assertf( ! genC, "Throw statements should not reach code generation." );1024 assertf( ! options.genC, "Throw statements should not reach code generation." ); 1024 1025 1025 1026 output << ((throwStmt->get_kind() == ThrowStmt::Terminate) ? … … 1036 1037 } 1037 1038 void CodeGenerator::postvisit( CatchStmt * stmt ) { 1038 assertf( ! genC, "Catch statements should not reach code generation." );1039 assertf( ! options.genC, "Catch statements should not reach code generation." ); 1039 1040 1040 1041 output << ((stmt->get_kind() == CatchStmt::Terminate) ? … … 1053 1054 1054 1055 void CodeGenerator::postvisit( WaitForStmt * stmt ) { 1055 assertf( ! genC, "Waitfor statements should not reach code generation." );1056 assertf( ! options.genC, "Waitfor statements should not reach code generation." ); 1056 1057 1057 1058 bool first = true; … … 1099 1100 1100 1101 void CodeGenerator::postvisit( WithStmt * with ) { 1101 if ( ! genC ) {1102 if ( ! options.genC ) { 1102 1103 output << "with ( "; 1103 1104 genCommaList( with->exprs.begin(), with->exprs.end() ); … … 1165 1166 1166 1167 void CodeGenerator::postvisit( ImplicitCtorDtorStmt * stmt ) { 1167 assertf( ! genC, "ImplicitCtorDtorStmts should not reach code generation." );1168 assertf( ! options.genC, "ImplicitCtorDtorStmts should not reach code generation." ); 1168 1169 stmt->callStmt->accept( *visitor ); 1169 1170 }
Note: See TracChangeset
for help on using the changeset viewer.