Changes in src/CodeGen/CodeGenerator.cc [e04ef3a:7baed7d]
- File:
-
- 1 edited
-
src/CodeGen/CodeGenerator.cc (modified) (24 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
re04ef3a r7baed7d 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 Jun 9 13:21:00 201613 // Update Count : 25 611 // Last Modified By : Rob Schluntz 12 // Last Modified On : Fri May 06 16:01:00 2016 13 // Update Count : 255 14 14 // 15 15 … … 26 26 #include "SynTree/Statement.h" 27 27 #include "SynTree/Type.h" 28 #include "SynTree/Attribute.h" 28 29 29 30 #include "Common/utility.h" … … 33 34 #include "OperatorTable.h" 34 35 #include "GenType.h" 36 37 #include "InitTweak/InitTweak.h" 35 38 36 39 using namespace std; … … 74 77 } 75 78 79 void CodeGenerator::genAttributes( std::list< Attribute * > & attributes ) { 80 if ( ! attributes.empty() ) { 81 output << "__attribute__ (("; 82 for ( Attribute *& attr : attributes ) { 83 if ( ! attr->empty() ) { 84 output << attr->get_name() << "("; 85 genCommaList( attr->get_parameters().begin(), attr->get_parameters().end() ); 86 output << ")"; 87 } 88 output << ","; 89 } 90 output << ")) "; 91 } 92 } 93 94 76 95 //*** Declarations 77 96 void CodeGenerator::visit( FunctionDecl *functionDecl ) { 78 // generalize this 79 FunctionDecl::Attribute attr = functionDecl->get_attribute(); 80 switch ( attr.type ) { 81 case FunctionDecl::Attribute::Constructor: 82 output << "__attribute__ ((constructor"; 83 if ( attr.priority != FunctionDecl::Attribute::Default ) { 84 output << "(" << attr.priority << ")"; 85 } 86 output << ")) "; 87 break; 88 case FunctionDecl::Attribute::Destructor: 89 output << "__attribute__ ((destructor"; 90 if ( attr.priority != FunctionDecl::Attribute::Default ) { 91 output << "(" << attr.priority << ")"; 92 } 93 output << ")) "; 94 break; 95 default: 96 break; 97 } 97 genAttributes( functionDecl->get_attributes() ); 98 98 99 handleStorageClass( functionDecl ); 99 100 if ( functionDecl->get_isInline() ) { … … 250 251 //*** Expressions 251 252 void CodeGenerator::visit( ApplicationExpr *applicationExpr ) { 252 extension( applicationExpr );253 253 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) { 254 254 OperatorInfo opInfo; … … 270 270 UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) ); 271 271 newExpr->get_args().push_back( *arg ); 272 assert( (*arg)->get_results().size() == 1 ); 273 Type * type = InitTweak::getPointerBase( (*arg)->get_results().front() ); 274 assert( type ); 275 newExpr->get_results().push_back( type ); 272 276 *arg = newExpr; 273 277 } // if … … 298 302 if ( applicationExpr->get_args().size() == 1 ) { 299 303 // the expression fed into a single parameter constructor or destructor 300 // may contain side effects - output as a voidexpression301 output << "( (void)(";304 // may contain side effects, so must still output this expression 305 output << "("; 302 306 (*arg++)->accept( *this ); 303 output << ") )/* " << opInfo.inputName << " */";307 output << ") /* " << opInfo.inputName << " */"; 304 308 } else if ( applicationExpr->get_args().size() == 2 ) { 305 309 // intrinsic two parameter constructors are essentially bitwise assignment … … 362 366 363 367 void CodeGenerator::visit( UntypedExpr *untypedExpr ) { 364 extension( untypedExpr );365 368 if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) { 366 369 OperatorInfo opInfo; … … 384 387 if ( untypedExpr->get_args().size() == 1 ) { 385 388 // the expression fed into a single parameter constructor or destructor 386 // may contain side effects - output as a voidexpression387 output << "( (void)(";389 // may contain side effects, so must still output this expression 390 output << "("; 388 391 (*arg++)->accept( *this ); 389 output << ") )/* " << opInfo.inputName << " */";392 output << ") /* " << opInfo.inputName << " */"; 390 393 } else if ( untypedExpr->get_args().size() == 2 ) { 391 394 // intrinsic two parameter constructors are essentially bitwise assignment … … 447 450 448 451 void CodeGenerator::visit( NameExpr *nameExpr ) { 449 extension( nameExpr );450 452 OperatorInfo opInfo; 451 453 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) { … … 458 460 459 461 void CodeGenerator::visit( AddressExpr *addressExpr ) { 460 extension( addressExpr );461 462 output << "(&"; 462 463 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address … … 470 471 471 472 void CodeGenerator::visit( CastExpr *castExpr ) { 472 extension( castExpr );473 473 output << "("; 474 474 if ( castExpr->get_results().empty() ) { … … 493 493 494 494 void CodeGenerator::visit( MemberExpr *memberExpr ) { 495 extension( memberExpr );496 495 memberExpr->get_aggregate()->accept( *this ); 497 496 output << "." << mangleName( memberExpr->get_member() ); … … 499 498 500 499 void CodeGenerator::visit( VariableExpr *variableExpr ) { 501 extension( variableExpr );502 500 OperatorInfo opInfo; 503 501 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) { … … 510 508 void CodeGenerator::visit( ConstantExpr *constantExpr ) { 511 509 assert( constantExpr->get_constant() ); 512 extension( constantExpr );513 510 constantExpr->get_constant()->accept( *this ); 514 511 } 515 512 516 513 void CodeGenerator::visit( SizeofExpr *sizeofExpr ) { 517 extension( sizeofExpr );518 514 output << "sizeof("; 519 515 if ( sizeofExpr->get_isType() ) { … … 526 522 527 523 void CodeGenerator::visit( AlignofExpr *alignofExpr ) { 528 extension( alignofExpr );529 524 // use GCC extension to avoid bumping std to C11 530 525 output << "__alignof__("; … … 542 537 543 538 void CodeGenerator::visit( OffsetofExpr *offsetofExpr ) { 544 extension( offsetofExpr );545 539 // use GCC builtin 546 540 output << "__builtin_offsetof("; … … 555 549 556 550 void CodeGenerator::visit( LogicalExpr *logicalExpr ) { 557 extension( logicalExpr );558 551 output << "("; 559 552 logicalExpr->get_arg1()->accept( *this ); … … 568 561 569 562 void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) { 570 extension( conditionalExpr );571 563 output << "("; 572 564 conditionalExpr->get_arg1()->accept( *this ); … … 579 571 580 572 void CodeGenerator::visit( CommaExpr *commaExpr ) { 581 extension( commaExpr );582 573 output << "("; 583 574 commaExpr->get_arg1()->accept( *this ); … … 592 583 593 584 void CodeGenerator::visit( AsmExpr *asmExpr ) { 594 extension( asmExpr );595 585 if ( asmExpr->get_inout() ) { 596 586 output << "[ "; … … 626 616 627 617 void CodeGenerator::visit( ExprStmt *exprStmt ) { 628 // I don't see why this check is necessary.629 // If this starts to cause problems then put it back in,630 // with an explanation631 618 assert( exprStmt ); 632 633 // if ( exprStmt != 0 ) { 634 exprStmt->get_expr()->accept( *this ); 635 output << ";" ; 636 // } // if 619 // cast the top-level expression to void to reduce gcc warnings. 620 Expression * expr = new CastExpr( exprStmt->get_expr() ); 621 expr->accept( *this ); 622 output << ";"; 637 623 } 638 624 … … 743 729 744 730 void CodeGenerator::visit( WhileStmt *whileStmt ) { 745 if ( whileStmt->get_isDoWhile() ) 731 if ( whileStmt->get_isDoWhile() ) { 746 732 output << "do" ; 747 else {733 } else { 748 734 output << "while (" ; 749 735 whileStmt->get_condition()->accept( *this ); … … 769 755 output << "for (;"; 770 756 771 if ( forStmt->get_condition() != 0 ) 757 if ( forStmt->get_condition() != 0 ) { 772 758 forStmt->get_condition()->accept( *this ); 759 } 773 760 output << ";"; 774 761 775 if ( forStmt->get_increment() != 0 ) 776 forStmt->get_increment()->accept( *this ); 762 if ( forStmt->get_increment() != 0 ) { 763 // cast the top-level expression to void to reduce gcc warnings. 764 Expression * expr = new CastExpr( forStmt->get_increment() ); 765 expr->accept( *this ); 766 } 777 767 output << ") "; 778 768
Note:
See TracChangeset
for help on using the changeset viewer.