Changes in src/CodeGen/CodeGenerator.cc [7baed7d:03e5d14]
- File:
-
- 1 edited
-
src/CodeGen/CodeGenerator.cc (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
r7baed7d r03e5d14 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Rob Schluntz 12 // Last Modified On : Fri May 06 1 6:01:00201613 // Update Count : 2 5512 // Last Modified On : Fri May 06 15:40:35 2016 13 // Update Count : 243 14 14 // 15 15 … … 26 26 #include "SynTree/Statement.h" 27 27 #include "SynTree/Type.h" 28 #include "SynTree/Attribute.h"29 28 30 29 #include "Common/utility.h" … … 34 33 #include "OperatorTable.h" 35 34 #include "GenType.h" 36 37 #include "InitTweak/InitTweak.h"38 35 39 36 using namespace std; … … 70 67 string mangleName( DeclarationWithType *decl ) { 71 68 if ( decl->get_mangleName() != "" ) { 72 // need to incorporate scope level in order to differentiate names for destructors 73 return decl->get_scopedMangleName(); 69 return decl->get_mangleName(); 74 70 } else { 75 71 return decl->get_name(); 76 72 } // if 77 73 } 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 74 95 75 //*** Declarations 96 76 void CodeGenerator::visit( FunctionDecl *functionDecl ) { 97 genAttributes( functionDecl->get_attributes() ); 98 77 // generalize this 78 FunctionDecl::Attribute attr = functionDecl->get_attribute(); 79 switch ( attr.type ) { 80 case FunctionDecl::Attribute::Constructor: 81 output << "__attribute__ ((constructor"; 82 if ( attr.priority != FunctionDecl::Attribute::Default ) { 83 output << "(" << attr.priority << ")"; 84 } 85 output << ")) "; 86 break; 87 case FunctionDecl::Attribute::Destructor: 88 output << "__attribute__ ((destructor"; 89 if ( attr.priority != FunctionDecl::Attribute::Default ) { 90 output << "(" << attr.priority << ")"; 91 } 92 output << ")) "; 93 break; 94 default: 95 break; 96 } 99 97 handleStorageClass( functionDecl ); 100 98 if ( functionDecl->get_isInline() ) { … … 235 233 printDesignators( init->get_designators() ); 236 234 output << "{ "; 237 if ( init->begin_initializers() == init->end_initializers() ) { 238 // illegal to leave initializer list empty for scalar initializers, 239 // but always legal to have 0 240 output << "0"; 241 } else { 242 genCommaList( init->begin_initializers(), init->end_initializers() ); 243 } 235 genCommaList( init->begin_initializers(), init->end_initializers() ); 244 236 output << " }"; 245 237 } … … 259 251 case OT_POSTFIXASSIGN: 260 252 case OT_INFIXASSIGN: 261 case OT_CTOR:262 case OT_DTOR:263 253 { 264 254 assert( arg != applicationExpr->get_args().end() ); 265 255 if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) { 266 // remove & from first assignment/ctor argument 256 267 257 *arg = addrExpr->get_arg(); 268 258 } else { 269 // no address-of operator, so must be a pointer - add dereference270 259 UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) ); 271 260 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 );276 261 *arg = newExpr; 277 262 } // if … … 298 283 break; 299 284 300 case OT_CTOR:301 case OT_DTOR:302 if ( applicationExpr->get_args().size() == 1 ) {303 // the expression fed into a single parameter constructor or destructor304 // may contain side effects, so must still output this expression305 output << "(";306 (*arg++)->accept( *this );307 output << ") /* " << opInfo.inputName << " */";308 } else if ( applicationExpr->get_args().size() == 2 ) {309 // intrinsic two parameter constructors are essentially bitwise assignment310 output << "(";311 (*arg++)->accept( *this );312 output << opInfo.symbol;313 (*arg)->accept( *this );314 output << ") /* " << opInfo.inputName << " */";315 } else {316 // no constructors with 0 or more than 2 parameters317 assert( false );318 }319 break;320 321 285 case OT_PREFIX: 322 286 case OT_PREFIXASSIGN: … … 334 298 output << opInfo.symbol; 335 299 break; 336 337 300 338 301 case OT_INFIX: … … 381 344 case OT_CALL: 382 345 assert( false ); 383 384 385 case OT_CTOR:386 case OT_DTOR:387 if ( untypedExpr->get_args().size() == 1 ) {388 // the expression fed into a single parameter constructor or destructor389 // may contain side effects, so must still output this expression390 output << "(";391 (*arg++)->accept( *this );392 output << ") /* " << opInfo.inputName << " */";393 } else if ( untypedExpr->get_args().size() == 2 ) {394 // intrinsic two parameter constructors are essentially bitwise assignment395 output << "(";396 (*arg++)->accept( *this );397 output << opInfo.symbol;398 (*arg)->accept( *this );399 output << ") /* " << opInfo.inputName << " */";400 } else {401 // no constructors with 0 or more than 2 parameters402 assert( false );403 }404 346 break; 405 347 … … 616 558 617 559 void CodeGenerator::visit( ExprStmt *exprStmt ) { 560 // I don't see why this check is necessary. 561 // If this starts to cause problems then put it back in, 562 // with an explanation 618 563 assert( exprStmt ); 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 << ";"; 564 565 // if ( exprStmt != 0 ) { 566 exprStmt->get_expr()->accept( *this ); 567 output << ";" ; 568 // } // if 623 569 } 624 570 … … 729 675 730 676 void CodeGenerator::visit( WhileStmt *whileStmt ) { 731 if ( whileStmt->get_isDoWhile() ) {677 if ( whileStmt->get_isDoWhile() ) 732 678 output << "do" ; 733 }else {679 else { 734 680 output << "while (" ; 735 681 whileStmt->get_condition()->accept( *this ); … … 755 701 output << "for (;"; 756 702 757 if ( forStmt->get_condition() != 0 ) {703 if ( forStmt->get_condition() != 0 ) 758 704 forStmt->get_condition()->accept( *this ); 759 }760 705 output << ";"; 761 706 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 } 707 if ( forStmt->get_increment() != 0 ) 708 forStmt->get_increment()->accept( *this ); 767 709 output << ") "; 768 710
Note:
See TracChangeset
for help on using the changeset viewer.