Changes in src/CodeGen/CodeGenerator.cc [03e5d14:7baed7d]
- File:
-
- 1 edited
-
src/CodeGen/CodeGenerator.cc (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
r03e5d14 r7baed7d 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 5:40:35201613 // Update Count : 2 4312 // 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; … … 67 70 string mangleName( DeclarationWithType *decl ) { 68 71 if ( decl->get_mangleName() != "" ) { 69 return decl->get_mangleName(); 72 // need to incorporate scope level in order to differentiate names for destructors 73 return decl->get_scopedMangleName(); 70 74 } else { 71 75 return decl->get_name(); 72 76 } // if 73 77 } 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 74 94 75 95 //*** Declarations 76 96 void CodeGenerator::visit( FunctionDecl *functionDecl ) { 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 } 97 genAttributes( functionDecl->get_attributes() ); 98 97 99 handleStorageClass( functionDecl ); 98 100 if ( functionDecl->get_isInline() ) { … … 233 235 printDesignators( init->get_designators() ); 234 236 output << "{ "; 235 genCommaList( init->begin_initializers(), init->end_initializers() ); 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 } 236 244 output << " }"; 237 245 } … … 251 259 case OT_POSTFIXASSIGN: 252 260 case OT_INFIXASSIGN: 261 case OT_CTOR: 262 case OT_DTOR: 253 263 { 254 264 assert( arg != applicationExpr->get_args().end() ); 255 265 if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) { 256 266 // remove & from first assignment/ctor argument 257 267 *arg = addrExpr->get_arg(); 258 268 } else { 269 // no address-of operator, so must be a pointer - add dereference 259 270 UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) ); 260 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 ); 261 276 *arg = newExpr; 262 277 } // if … … 283 298 break; 284 299 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 destructor 304 // may contain side effects, so must still output this expression 305 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 assignment 310 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 parameters 317 assert( false ); 318 } 319 break; 320 285 321 case OT_PREFIX: 286 322 case OT_PREFIXASSIGN: … … 298 334 output << opInfo.symbol; 299 335 break; 336 300 337 301 338 case OT_INFIX: … … 344 381 case OT_CALL: 345 382 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 destructor 389 // may contain side effects, so must still output this expression 390 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 assignment 395 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 parameters 402 assert( false ); 403 } 346 404 break; 347 405 … … 558 616 559 617 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 explanation563 618 assert( exprStmt ); 564 565 // if ( exprStmt != 0 ) { 566 exprStmt->get_expr()->accept( *this ); 567 output << ";" ; 568 // } // 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 << ";"; 569 623 } 570 624 … … 675 729 676 730 void CodeGenerator::visit( WhileStmt *whileStmt ) { 677 if ( whileStmt->get_isDoWhile() ) 731 if ( whileStmt->get_isDoWhile() ) { 678 732 output << "do" ; 679 else {733 } else { 680 734 output << "while (" ; 681 735 whileStmt->get_condition()->accept( *this ); … … 701 755 output << "for (;"; 702 756 703 if ( forStmt->get_condition() != 0 ) 757 if ( forStmt->get_condition() != 0 ) { 704 758 forStmt->get_condition()->accept( *this ); 759 } 705 760 output << ";"; 706 761 707 if ( forStmt->get_increment() != 0 ) 708 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 } 709 767 output << ") "; 710 768
Note:
See TracChangeset
for help on using the changeset viewer.