Changeset e39241b for src/CodeGen/CodeGenerator.cc
- Timestamp:
- Apr 17, 2017, 5:43:01 PM (7 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
- Children:
- 7069652
- Parents:
- 4ae83a4b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
r4ae83a4b re39241b 89 89 } 90 90 91 CodeGenerator::CodeGenerator( std::ostream & os, bool pretty ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ), printLabels( *this ), pretty( pretty) {}91 CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ) {} 92 92 93 93 CodeGenerator::CodeGenerator( std::ostream & os, std::string init, int indentation, bool infunp ) … … 136 136 functionDecl->get_funcSpec().print( output ); 137 137 138 output << genType( functionDecl->get_functionType(), mangleName( functionDecl ), pretty );138 output << genType( functionDecl->get_functionType(), mangleName( functionDecl ), pretty, genC ); 139 139 140 140 asmName( functionDecl ); … … 147 147 148 148 void CodeGenerator::visit( ObjectDecl * objectDecl ) { 149 if (objectDecl->get_name().empty()) { 149 if (objectDecl->get_name().empty() && genC ) { 150 // only generate an anonymous name when generating C code, otherwise it clutters the output too much 150 151 static UniqueName name = { "__anonymous_object" }; 151 152 objectDecl->set_name( name.newName() ); … … 156 157 157 158 handleStorageClass( objectDecl ); 158 output << genType( objectDecl->get_type(), mangleName( objectDecl ), pretty );159 output << genType( objectDecl->get_type(), mangleName( objectDecl ), pretty, genC ); 159 160 160 161 asmName( objectDecl ); … … 174 175 genAttributes( aggDecl->get_attributes() ); 175 176 177 if( ! aggDecl->get_parameters().empty() && ! genC ) { 178 // assertf( ! genC, "Aggregate type parameters should not reach code generation." ); 179 output << "forall("; 180 genCommaList( aggDecl->get_parameters().begin(), aggDecl->get_parameters().end() ); 181 output << ")" << endl; 182 } 183 176 184 if ( aggDecl->get_name() != "" ) 177 185 output << aggDecl->get_name(); 178 186 179 // std::list< Declaration * > & memb = aggDecl->get_members();180 // if ( ! memb.empty() ) {181 187 if ( aggDecl->has_body() ) { 182 188 std::list< Declaration * > & memb = aggDecl->get_members(); … … 242 248 243 249 void CodeGenerator::visit( TypedefDecl * typeDecl ) { 244 assert ( false &&"Typedefs are removed and substituted in earlier passes." );245 //output << "typedef ";246 //output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty );250 assertf( ! genC, "Typedefs are removed and substituted in earlier passes." ); 251 output << "typedef "; 252 output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl; 247 253 } 248 254 249 255 void CodeGenerator::visit( TypeDecl * typeDecl ) { 250 // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes, 251 // still to be done 252 extension( typeDecl ); 253 output << "extern unsigned long " << typeDecl->get_name(); 254 if ( typeDecl->get_base() ) { 255 output << " = sizeof( " << genType( typeDecl->get_base(), "", pretty ) << " )"; 256 } // if 256 if ( genC ) { 257 // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes, 258 // still to be done 259 extension( typeDecl ); 260 output << "extern unsigned long " << typeDecl->get_name(); 261 if ( typeDecl->get_base() ) { 262 output << " = sizeof( " << genType( typeDecl->get_base(), "", pretty, genC ) << " )"; 263 } // if 264 } else { 265 output << typeDecl->typeString() << " " << typeDecl->get_name(); 266 if ( ! typeDecl->get_assertions().empty() ) { 267 output << " | { "; 268 genCommaList( typeDecl->get_assertions().begin(), typeDecl->get_assertions().end() ); 269 output << " }"; 270 } 271 } 257 272 } 258 273 … … 293 308 294 309 void CodeGenerator::visit( ConstructorInit * init ){ 295 assertf( false, "ConstructorInit nodes should not make it to CodeGen." ); 310 assertf( ! genC, "ConstructorInit nodes should not reach code generation." ); 311 // xxx - generate something reasonable for constructor/destructor pairs 312 output << "<ctorinit>"; 296 313 } 297 314 … … 547 564 // at least one result type of cast, but not an lvalue 548 565 output << "("; 549 output << genType( castExpr->get_result(), "", pretty );566 output << genType( castExpr->get_result(), "", pretty, genC ); 550 567 output << ")"; 551 568 } else { … … 558 575 559 576 void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) { 560 assert( false ); 577 assertf( ! genC, "UntypedMemberExpr should not reach code generation." ); 578 extension( memberExpr ); 579 memberExpr->get_aggregate()->accept( *this ); 580 output << "." << memberExpr->get_member(); 561 581 } 562 582 … … 587 607 output << "sizeof("; 588 608 if ( sizeofExpr->get_isType() ) { 589 output << genType( sizeofExpr->get_type(), "", pretty );609 output << genType( sizeofExpr->get_type(), "", pretty, genC ); 590 610 } else { 591 611 sizeofExpr->get_expr()->accept( *this ); … … 599 619 output << "__alignof__("; 600 620 if ( alignofExpr->get_isType() ) { 601 output << genType( alignofExpr->get_type(), "", pretty );621 output << genType( alignofExpr->get_type(), "", pretty, genC ); 602 622 } else { 603 623 alignofExpr->get_expr()->accept( *this ); … … 607 627 608 628 void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) { 609 assert( false && "UntypedOffsetofExpr should not reach code generation." ); 629 assertf( ! genC, "UntypedOffsetofExpr should not reach code generation." ); 630 output << "offsetof("; 631 output << genType( offsetofExpr->get_type(), "", pretty, genC ); 632 output << ", " << offsetofExpr->get_member(); 633 output << ")"; 610 634 } 611 635 … … 613 637 // use GCC builtin 614 638 output << "__builtin_offsetof("; 615 output << genType( offsetofExpr->get_type(), "", pretty );639 output << genType( offsetofExpr->get_type(), "", pretty, genC ); 616 640 output << ", " << mangleName( offsetofExpr->get_member() ); 617 641 output << ")"; … … 619 643 620 644 void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) { 621 assert( false && "OffsetPackExpr should not reach code generation." ); 645 assertf( ! genC, "OffsetPackExpr should not reach code generation." ); 646 output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", pretty, genC ) << ")"; 622 647 } 623 648 … … 655 680 } 656 681 657 void CodeGenerator::visit( UntypedTupleExpr * tupleExpr ) { assertf( false, "UntypedTupleExpr should not make it to Code Gen" ); } 658 659 void CodeGenerator::visit( TupleExpr * tupleExpr ) { assertf( false, "TupleExpr should not make it to Code Gen" ); } 660 661 void CodeGenerator::visit( TypeExpr * typeExpr ) {} 682 void CodeGenerator::visit( UntypedTupleExpr * tupleExpr ) { 683 assertf( ! genC, "UntypedTupleExpr should not reach code generation." ); 684 output << "["; 685 genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() ); 686 output << "]"; 687 } 688 689 void CodeGenerator::visit( TupleExpr * tupleExpr ) { 690 assertf( ! genC, "TupleExpr should not reach code generation." ); 691 output << "["; 692 genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() ); 693 output << "]"; 694 } 695 696 void CodeGenerator::visit( TypeExpr * typeExpr ) { 697 assertf( ! genC, "TypeExpr should not reach code generation." ); 698 output<< genType( typeExpr->get_type(), "", pretty, genC ); 699 } 662 700 663 701 void CodeGenerator::visit( AsmExpr * asmExpr ) { … … 675 713 void CodeGenerator::visit( CompoundLiteralExpr *compLitExpr ) { 676 714 assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) ); 677 output << "(" << genType( compLitExpr->get_result(), "", pretty ) << ")";715 output << "(" << genType( compLitExpr->get_result(), "", pretty, genC ) << ")"; 678 716 compLitExpr->get_initializer()->accept( *this ); 679 717 }
Note: See TracChangeset
for help on using the changeset viewer.