Changeset 3d5701e for src/CodeGen/CodeGenerator.cc
- Timestamp:
- Feb 25, 2020, 1:17:33 PM (6 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- 7dc2e015
- Parents:
- 9fb8f01 (diff), dd9e1ca (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
-
src/CodeGen/CodeGenerator.cc (modified) (27 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
r9fb8f01 r3d5701e 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Andrew Beach12 // Last Modified On : Thr May 2 10:47:00 201913 // Update Count : 49711 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun Feb 16 08:32:48 2020 13 // Update Count : 532 14 14 // 15 15 #include "CodeGenerator.h" … … 23 23 #include "InitTweak/InitTweak.h" // for getPointerBase 24 24 #include "OperatorTable.h" // for OperatorInfo, operatorLookup 25 #include " Parser/LinkageSpec.h"// for Spec, Intrinsic25 #include "SynTree/LinkageSpec.h" // for Spec, Intrinsic 26 26 #include "SynTree/Attribute.h" // for Attribute 27 27 #include "SynTree/BaseSyntaxNode.h" // for BaseSyntaxNode … … 39 39 int CodeGenerator::tabsize = 4; 40 40 41 // the kinds of statements that would ideally be followed by whitespace41 // The kinds of statements that would ideally be followed by whitespace. 42 42 bool wantSpacing( Statement * stmt) { 43 43 return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) || … … 78 78 } 79 79 80 /* Using updateLocation at the beginning of a node and endl 81 * within a node should become the method of formating. 82 */ 80 // Using updateLocation at the beginning of a node and endl within a node should become the method of formating. 83 81 void CodeGenerator::updateLocation( CodeLocation const & to ) { 84 82 // skip if linemarks shouldn't appear or if codelocation is unset … … 95 93 } else { 96 94 output << "\n# " << to.first_line << " \"" << to.filename 97 << "\"\n" << indent;95 << "\"\n" << indent; 98 96 currentLocation = to; 99 97 } … … 131 129 132 130 void CodeGenerator::genAttributes( list< Attribute * > & attributes ) { 133 if ( attributes.empty() ) return;131 if ( attributes.empty() ) return; 134 132 output << "__attribute__ (("; 135 133 for ( list< Attribute * >::iterator attr( attributes.begin() );; ) { … … 140 138 output << ")"; 141 139 } // if 142 if ( ++attr == attributes.end() ) break;140 if ( ++attr == attributes.end() ) break; 143 141 output << ","; // separator 144 142 } // for … … 165 163 previsit( (BaseSyntaxNode *)node ); 166 164 GuardAction( [this, node](){ 167 if ( options.printExprTypes && node->result ) {168 output << " /* " << genType( node->result, "", options ) << " */ ";169 }170 } );165 if ( options.printExprTypes && node->result ) { 166 output << " /* " << genType( node->result, "", options ) << " */ "; 167 } 168 } ); 171 169 } 172 170 … … 198 196 // deleted decls should never be used, so don't print them 199 197 if ( objectDecl->isDeleted && options.genC ) return; 200 if (objectDecl->get_name().empty() && options.genC ) { 198 199 // gcc allows an empty declarator (no name) for bit-fields and C states: 6.7.2.1 Structure and union specifiers, 200 // point 4, page 113: If the (bit field) value is zero, the declaration shall have no declarator. For anything 201 // else, the anonymous name refers to the anonymous object for plan9 inheritance. 202 if ( objectDecl->get_name().empty() && options.genC && ! objectDecl->get_bitfieldWidth() ) { 201 203 // only generate an anonymous name when generating C code, otherwise it clutters the output too much 202 204 static UniqueName name = { "__anonymous_object" }; 203 205 objectDecl->set_name( name.newName() ); 204 // Stops unused parameter warnings.205 if ( options.anonymousUnused ) {206 objectDecl->attributes.push_back( new Attribute( "unused" ) );207 }206 // Stops unused parameter warnings. 207 if ( options.anonymousUnused ) { 208 objectDecl->attributes.push_back( new Attribute( "unused" ) ); 209 } 208 210 } 209 211 … … 395 397 extension( applicationExpr ); 396 398 if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) { 397 OperatorInfoopInfo;398 if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo) ) {399 const OperatorInfo * opInfo; 400 if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && ( opInfo = operatorLookup( varExpr->get_var()->get_name() ) ) ) { 399 401 std::list< Expression* >::iterator arg = applicationExpr->get_args().begin(); 400 switch ( opInfo .type ) {402 switch ( opInfo->type ) { 401 403 case OT_INDEX: 402 404 assert( applicationExpr->get_args().size() == 2 ); … … 419 421 output << "("; 420 422 (*arg++)->accept( *visitor ); 421 output << ") /* " << opInfo .inputName << " */";423 output << ") /* " << opInfo->inputName << " */"; 422 424 } else if ( applicationExpr->get_args().size() == 2 ) { 423 425 // intrinsic two parameter constructors are essentially bitwise assignment 424 426 output << "("; 425 427 (*arg++)->accept( *visitor ); 426 output << opInfo .symbol;428 output << opInfo->symbol; 427 429 (*arg)->accept( *visitor ); 428 output << ") /* " << opInfo .inputName << " */";430 output << ") /* " << opInfo->inputName << " */"; 429 431 } else { 430 432 // no constructors with 0 or more than 2 parameters … … 437 439 assert( applicationExpr->get_args().size() == 1 ); 438 440 output << "("; 439 output << opInfo .symbol;441 output << opInfo->symbol; 440 442 (*arg)->accept( *visitor ); 441 443 output << ")"; … … 446 448 assert( applicationExpr->get_args().size() == 1 ); 447 449 (*arg)->accept( *visitor ); 448 output << opInfo .symbol;450 output << opInfo->symbol; 449 451 break; 450 452 … … 455 457 output << "("; 456 458 (*arg++)->accept( *visitor ); 457 output << opInfo .symbol;459 output << opInfo->symbol; 458 460 (*arg)->accept( *visitor ); 459 461 output << ")"; … … 482 484 extension( untypedExpr ); 483 485 if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->function ) ) { 484 OperatorInfo opInfo;485 if ( op eratorLookup( nameExpr->name, opInfo )) {486 const OperatorInfo * opInfo = operatorLookup( nameExpr->name ); 487 if ( opInfo ) { 486 488 std::list< Expression* >::iterator arg = untypedExpr->args.begin(); 487 switch ( opInfo .type ) {489 switch ( opInfo->type ) { 488 490 case OT_INDEX: 489 491 assert( untypedExpr->args.size() == 2 ); … … 504 506 output << "("; 505 507 (*arg++)->accept( *visitor ); 506 output << ") /* " << opInfo .inputName << " */";508 output << ") /* " << opInfo->inputName << " */"; 507 509 } else if ( untypedExpr->get_args().size() == 2 ) { 508 510 // intrinsic two parameter constructors are essentially bitwise assignment 509 511 output << "("; 510 512 (*arg++)->accept( *visitor ); 511 output << opInfo .symbol;513 output << opInfo->symbol; 512 514 (*arg)->accept( *visitor ); 513 output << ") /* " << opInfo .inputName << " */";515 output << ") /* " << opInfo->inputName << " */"; 514 516 } else { 515 517 // no constructors with 0 or more than 2 parameters … … 517 519 output << "("; 518 520 (*arg++)->accept( *visitor ); 519 output << opInfo .symbol << "{ ";521 output << opInfo->symbol << "{ "; 520 522 genCommaList( arg, untypedExpr->args.end() ); 521 output << "}) /* " << opInfo .inputName << " */";523 output << "}) /* " << opInfo->inputName << " */"; 522 524 } // if 523 525 break; … … 528 530 assert( untypedExpr->args.size() == 1 ); 529 531 output << "("; 530 output << opInfo .symbol;532 output << opInfo->symbol; 531 533 (*arg)->accept( *visitor ); 532 534 output << ")"; … … 537 539 assert( untypedExpr->args.size() == 1 ); 538 540 (*arg)->accept( *visitor ); 539 output << opInfo .symbol;541 output << opInfo->symbol; 540 542 break; 541 543 … … 545 547 output << "("; 546 548 (*arg++)->accept( *visitor ); 547 output << opInfo .symbol;549 output << opInfo->symbol; 548 550 (*arg)->accept( *visitor ); 549 551 output << ")"; … … 577 579 void CodeGenerator::postvisit( NameExpr * nameExpr ) { 578 580 extension( nameExpr ); 579 OperatorInfo opInfo;580 if ( op eratorLookup( nameExpr->name, opInfo )) {581 if ( opInfo .type == OT_CONSTANT ) {582 output << opInfo .symbol;581 const OperatorInfo * opInfo = operatorLookup( nameExpr->name ); 582 if ( opInfo ) { 583 if ( opInfo->type == OT_CONSTANT ) { 584 output << opInfo->symbol; 583 585 } else { 584 output << opInfo .outputName;586 output << opInfo->outputName; 585 587 } 586 588 } else { … … 650 652 void CodeGenerator::postvisit( VariableExpr * variableExpr ) { 651 653 extension( variableExpr ); 652 OperatorInfoopInfo;653 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {654 output << opInfo .symbol;654 const OperatorInfo * opInfo; 655 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && (opInfo = operatorLookup( variableExpr->get_var()->get_name() )) && opInfo->type == OT_CONSTANT ) { 656 output << opInfo->symbol; 655 657 } else { 656 658 output << mangleName( variableExpr->get_var() ); … … 782 784 783 785 void CodeGenerator::postvisit( AsmExpr * asmExpr ) { 784 if ( asmExpr->get_inout() ) {786 if ( !asmExpr->inout.empty() ) { 785 787 output << "[ "; 786 asmExpr->get_inout()->accept( *visitor );788 output << asmExpr->inout; 787 789 output << " ] "; 788 790 } // if 789 asmExpr-> get_constraint()->accept( *visitor );791 asmExpr->constraint->accept( *visitor ); 790 792 output << " ( "; 791 asmExpr-> get_operand()->accept( *visitor );793 asmExpr->operand->accept( *visitor ); 792 794 output << " )"; 793 795 } … … 1007 1009 case BranchStmt::FallThroughDefault: 1008 1010 assertf( ! options.genC, "fallthru should not reach code generation." ); 1009 output << "fallthru";1011 output << "fallthru"; 1010 1012 break; 1011 1013 } // switch … … 1031 1033 1032 1034 output << ((throwStmt->get_kind() == ThrowStmt::Terminate) ? 1033 "throw" : "throwResume");1035 "throw" : "throwResume"); 1034 1036 if (throwStmt->get_expr()) { 1035 1037 output << " "; … … 1046 1048 1047 1049 output << ((stmt->get_kind() == CatchStmt::Terminate) ? 1048 "catch" : "catchResume");1050 "catch" : "catchResume"); 1049 1051 output << "( "; 1050 1052 stmt->decl->accept( *visitor ); … … 1183 1185 1184 1186 std::string genName( DeclarationWithType * decl ) { 1185 CodeGen::OperatorInfo opInfo;1186 if ( op eratorLookup( decl->get_name(), opInfo )) {1187 return opInfo .outputName;1187 const OperatorInfo * opInfo = operatorLookup( decl->get_name() ); 1188 if ( opInfo ) { 1189 return opInfo->outputName; 1188 1190 } else { 1189 1191 return decl->get_name();
Note:
See TracChangeset
for help on using the changeset viewer.