Changes in src/CodeGen/CodeGenerator.cc [d3b7937:e551c69]
- File:
-
- 1 edited
-
src/CodeGen/CodeGenerator.cc (modified) (35 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
rd3b7937 re551c69 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // CodeGenerator.cc -- 7 // CodeGenerator.cc -- 8 8 // 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 : Mon Jan 25 21:22:00 201613 // Update Count : 2 4211 // Last Modified By : Rob Schluntz 12 // Last Modified On : Thu Sep 17 15:24:08 2015 13 // Update Count : 231 14 14 // 15 15 … … 26 26 #include "SynTree/Statement.h" 27 27 28 #include " Common/utility.h"29 #include " Common/UnimplementedError.h"28 #include "utility.h" 29 #include "UnimplementedError.h" 30 30 31 31 #include "CodeGenerator.h" … … 98 98 handleStorageClass( objectDecl ); 99 99 output << genType( objectDecl->get_type(), mangleName( objectDecl ) ); 100 100 101 101 if ( objectDecl->get_init() ) { 102 102 output << " = "; … … 112 112 if ( aggDecl->get_name() != "" ) 113 113 output << aggDecl->get_name(); 114 114 115 115 std::list< Declaration * > &memb = aggDecl->get_members(); 116 116 … … 118 118 output << " {" << endl; 119 119 120 cur_indent += CodeGenerator::tabsize; 120 cur_indent += CodeGenerator::tabsize; 121 121 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { 122 output << indent; 122 output << indent; 123 123 (*i)->accept( *this ); 124 124 output << ";" << endl; 125 125 } 126 126 127 cur_indent -= CodeGenerator::tabsize; 127 cur_indent -= CodeGenerator::tabsize; 128 128 129 129 output << indent << "}"; … … 140 140 handleAggregate( aggregateDecl ); 141 141 } 142 142 143 143 void CodeGenerator::visit( EnumDecl *aggDecl ) { 144 144 output << "enum "; … … 146 146 if ( aggDecl->get_name() != "" ) 147 147 output << aggDecl->get_name(); 148 148 149 149 std::list< Declaration* > &memb = aggDecl->get_members(); 150 150 … … 152 152 output << " {" << endl; 153 153 154 cur_indent += CodeGenerator::tabsize; 154 cur_indent += CodeGenerator::tabsize; 155 155 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { 156 156 ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i ); 157 157 assert( obj ); 158 output << indent << mangleName( obj ); 158 output << indent << mangleName( obj ); 159 159 if ( obj->get_init() ) { 160 160 output << " = "; … … 164 164 } // for 165 165 166 cur_indent -= CodeGenerator::tabsize; 166 cur_indent -= CodeGenerator::tabsize; 167 167 168 168 output << indent << "}"; 169 169 } // if 170 170 } 171 171 172 172 void CodeGenerator::visit( ContextDecl *aggregateDecl ) {} 173 173 174 174 void CodeGenerator::visit( TypedefDecl *typeDecl ) { 175 175 output << "typedef "; 176 176 output << genType( typeDecl->get_base(), typeDecl->get_name() ); 177 177 } 178 178 179 179 void CodeGenerator::visit( TypeDecl *typeDecl ) { 180 180 // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes, … … 216 216 } 217 217 218 void CodeGenerator::visit( Constant *constant ) { 218 void CodeGenerator::visit( Constant *constant ) { 219 219 output << constant->get_value() ; 220 220 } … … 233 233 assert( arg != applicationExpr->get_args().end() ); 234 234 if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) { 235 235 236 236 *arg = addrExpr->get_arg(); 237 237 } else { … … 242 242 break; 243 243 } 244 244 245 245 default: 246 246 // do nothing 247 247 ; 248 248 } 249 249 250 250 switch ( opInfo.type ) { 251 251 case OT_INDEX: … … 256 256 output << "]"; 257 257 break; 258 258 259 259 case OT_CALL: 260 260 // there are no intrinsic definitions of the function call operator 261 261 assert( false ); 262 262 break; 263 263 264 264 case OT_PREFIX: 265 265 case OT_PREFIXASSIGN: … … 270 270 output << ")"; 271 271 break; 272 272 273 273 case OT_POSTFIX: 274 274 case OT_POSTFIXASSIGN: … … 287 287 output << ")"; 288 288 break; 289 289 290 290 case OT_CONSTANT: 291 291 case OT_LABELADDRESS: … … 306 306 } // if 307 307 } 308 308 309 309 void CodeGenerator::visit( UntypedExpr *untypedExpr ) { 310 310 if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) { … … 320 320 output << "]"; 321 321 break; 322 322 323 323 case OT_CALL: 324 324 assert( false ); 325 325 break; 326 326 327 327 case OT_PREFIX: 328 328 case OT_PREFIXASSIGN: … … 334 334 output << ")"; 335 335 break; 336 336 337 337 case OT_POSTFIX: 338 338 case OT_POSTFIXASSIGN: … … 341 341 output << opInfo.symbol; 342 342 break; 343 343 344 344 case OT_INFIX: 345 345 case OT_INFIXASSIGN: … … 351 351 output << ")"; 352 352 break; 353 353 354 354 case OT_CONSTANT: 355 355 // there are no intrinsic definitions of 0 or 1 as functions … … 369 369 } // if 370 370 } 371 371 372 372 void CodeGenerator::visit( NameExpr *nameExpr ) { 373 373 OperatorInfo opInfo; … … 379 379 } // if 380 380 } 381 381 382 382 void CodeGenerator::visit( AddressExpr *addressExpr ) { 383 383 output << "(&"; … … 392 392 393 393 void CodeGenerator::visit( CastExpr *castExpr ) { 394 output << "("; 395 if ( castExpr->get_results().empty() ) { 396 output << "(void)" ; 397 } else if ( ! castExpr->get_results().front()->get_isLvalue() ) { 398 // at least one result type of cast, but not an lvalue 399 output << "("; 400 output << genType( castExpr->get_results().front(), "" ); 394 // if the cast is to an lvalue type, then the cast 395 // should be dropped, since the result of a cast is 396 // never an lvalue in C 397 if ( castExpr->get_results().front()->get_isLvalue() ) { 398 castExpr->get_arg()->accept( *this ); 399 } else { 400 output << "(("; 401 if ( castExpr->get_results().empty() ) { 402 output << "void" ; 403 } else { 404 output << genType( castExpr->get_results().front(), "" ); 405 } // if 401 406 output << ")"; 402 } else { 403 // otherwise, the cast is to an lvalue type, so the cast 404 // should be dropped, since the result of a cast is 405 // never an lvalue in C 407 castExpr->get_arg()->accept( *this ); 408 output << ")"; 406 409 } 407 castExpr->get_arg()->accept( *this ); 408 output << ")"; 409 } 410 410 } 411 411 412 void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) { 412 413 assert( false ); 413 414 } 414 415 415 416 void CodeGenerator::visit( MemberExpr *memberExpr ) { 416 417 memberExpr->get_aggregate()->accept( *this ); 417 418 output << "." << mangleName( memberExpr->get_member() ); 418 419 } 419 420 420 421 void CodeGenerator::visit( VariableExpr *variableExpr ) { 421 422 OperatorInfo opInfo; … … 426 427 } // if 427 428 } 428 429 429 430 void CodeGenerator::visit( ConstantExpr *constantExpr ) { 430 431 assert( constantExpr->get_constant() ); 431 432 constantExpr->get_constant()->accept( *this ); 432 433 } 433 434 434 435 void CodeGenerator::visit( SizeofExpr *sizeofExpr ) { 435 436 output << "sizeof("; … … 442 443 } 443 444 444 void CodeGenerator::visit( AlignofExpr * sizeofExpr ) {445 void CodeGenerator::visit( AlignofExpr *alignofExpr ) { 445 446 // use GCC extension to avoid bumping std to C11 446 447 output << "__alignof__("; 447 if ( sizeofExpr->get_isType() ) {448 output << genType( sizeofExpr->get_type(), "" );449 } else { 450 sizeofExpr->get_expr()->accept( *this );448 if ( alignofExpr->get_isType() ) { 449 output << genType( alignofExpr->get_type(), "" ); 450 } else { 451 alignofExpr->get_expr()->accept( *this ); 451 452 } // if 452 453 output << ")"; 453 454 } 454 455 456 void CodeGenerator::visit( OffsetofExpr *offsetofExpr ) { 457 // use GCC builtin 458 output << "__builtin_offsetof("; 459 output << genType( offsetofExpr->get_type(), "" ); 460 output << ", " << mangleName( offsetofExpr->get_member() ); 461 output << ")"; 462 } 463 455 464 void CodeGenerator::visit( LogicalExpr *logicalExpr ) { 456 465 output << "("; … … 464 473 output << ")"; 465 474 } 466 475 467 476 void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) { 468 477 output << "("; … … 474 483 output << ")"; 475 484 } 476 485 477 486 void CodeGenerator::visit( CommaExpr *commaExpr ) { 478 487 output << "("; … … 482 491 output << ")"; 483 492 } 484 493 485 494 void CodeGenerator::visit( TupleExpr *tupleExpr ) {} 486 495 487 496 void CodeGenerator::visit( TypeExpr *typeExpr ) {} 488 497 … … 515 524 } 516 525 } 517 cur_indent -= CodeGenerator::tabsize; 526 cur_indent -= CodeGenerator::tabsize; 518 527 519 528 output << indent << "}"; … … 521 530 522 531 void CodeGenerator::visit( ExprStmt *exprStmt ) { 523 // I don't see why this check is necessary. 524 // If this starts to cause problems then put it back in, 532 // I don't see why this check is necessary. 533 // If this starts to cause problems then put it back in, 525 534 // with an explanation 526 535 assert( exprStmt ); … … 572 581 switchStmt->get_condition()->accept( *this ); 573 582 output << " ) "; 574 583 575 584 output << "{" << std::endl; 576 585 cur_indent += CodeGenerator::tabsize; … … 592 601 } // if 593 602 output << ":\n"; 594 603 595 604 std::list<Statement *> sts = caseStmt->get_statements(); 596 605 … … 609 618 if ( ! branchStmt->get_target().empty() ) 610 619 output << "goto " << branchStmt->get_target(); 611 else { 620 else { 612 621 if ( branchStmt->get_computedTarget() != 0 ) { 613 622 output << "goto *"; … … 660 669 661 670 void CodeGenerator::visit( ForStmt *forStmt ) { 662 // initialization is always hoisted, so don't 663 // bother doing anything with that 671 // initialization is always hoisted, so don't 672 // bother doing anything with that 664 673 output << "for (;"; 665 674 … … 685 694 void CodeGenerator::visit( DeclStmt *declStmt ) { 686 695 declStmt->get_decl()->accept( *this ); 687 696 688 697 if ( doSemicolon( declStmt->get_decl() ) ) { 689 698 output << ";";
Note:
See TracChangeset
for help on using the changeset viewer.