Changes in src/CodeGen/CodeGenerator.cc [afc1045:4b2589a]
- File:
-
- 1 edited
-
src/CodeGen/CodeGenerator.cc (modified) (43 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
rafc1045 r4b2589a 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 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Wed Mar 2 17:32:16201613 // Update Count : 2 4312 // Last Modified On : Thu Jun 9 13:21:00 2016 13 // Update Count : 256 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; … … 45 48 } 46 49 47 ostream & CodeGenerator::Indenter::operator()( ostream & output ) {50 ostream & CodeGenerator::Indenter::operator()( ostream & output ) const { 48 51 return output << string( cg.cur_indent, ' ' ); 49 52 } 50 53 51 ostream & operator<<( ostream & output, CodeGenerator::Indenter &indent ) {54 ostream & operator<<( ostream & output, const CodeGenerator::Indenter &indent ) { 52 55 return indent( output ); 53 56 } 54 57 55 CodeGenerator::CodeGenerator( std::ostream &os ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ) { } 58 CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) { 59 labels = &l; 60 return *this; 61 } 62 63 ostream & operator<<( ostream & output, CodeGenerator::LabelPrinter &printLabels ) { 64 std::list< Label > & labs = *printLabels.labels; 65 // l.unique(); // assumes a sorted list. Why not use set? Does order matter? 66 for ( Label & l : labs ) { 67 output << l.get_name() + ": "; 68 printLabels.cg.genAttributes( l.get_attributes() ); 69 } 70 return output; 71 } 72 73 CodeGenerator::CodeGenerator( std::ostream &os ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ), printLabels( *this ) { } 56 74 57 75 CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indentation, bool infunp ) 58 : indent( *this), cur_indent( indentation ), insideFunction( infunp ), output( os ) {76 : indent( *this), cur_indent( indentation ), insideFunction( infunp ), output( os ), printLabels( *this ) { 59 77 //output << std::string( init ); 60 78 } 61 79 62 80 CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indentation, bool infunp ) 63 : indent( *this ), cur_indent( indentation ), insideFunction( infunp ), output( os ) {81 : indent( *this ), cur_indent( indentation ), insideFunction( infunp ), output( os ), printLabels( *this ) { 64 82 //output << std::string( init ); 65 83 } … … 67 85 string mangleName( DeclarationWithType *decl ) { 68 86 if ( decl->get_mangleName() != "" ) { 69 return decl->get_mangleName(); 87 // need to incorporate scope level in order to differentiate names for destructors 88 return decl->get_scopedMangleName(); 70 89 } else { 71 90 return decl->get_name(); 72 91 } // if 73 92 } 93 94 void CodeGenerator::genAttributes( std::list< Attribute * > & attributes ) { 95 if ( ! attributes.empty() ) { 96 output << "__attribute__ (("; 97 for ( Attribute *& attr : attributes ) { 98 if ( ! attr->empty() ) { 99 output << attr->get_name() << "("; 100 genCommaList( attr->get_parameters().begin(), attr->get_parameters().end() ); 101 output << ")"; 102 } 103 output << ","; 104 } 105 output << ")) "; 106 } 107 } 108 74 109 75 110 //*** Declarations 76 111 void CodeGenerator::visit( FunctionDecl *functionDecl ) { 112 genAttributes( functionDecl->get_attributes() ); 113 77 114 handleStorageClass( functionDecl ); 78 115 if ( functionDecl->get_isInline() ) { … … 99 136 handleStorageClass( objectDecl ); 100 137 output << genType( objectDecl->get_type(), mangleName( objectDecl ) ); 101 138 102 139 if ( objectDecl->get_init() ) { 103 140 output << " = "; … … 113 150 if ( aggDecl->get_name() != "" ) 114 151 output << aggDecl->get_name(); 115 152 116 153 std::list< Declaration * > &memb = aggDecl->get_members(); 117 154 … … 119 156 output << " {" << endl; 120 157 121 cur_indent += CodeGenerator::tabsize; 158 cur_indent += CodeGenerator::tabsize; 122 159 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { 123 output << indent; 160 output << indent; 124 161 (*i)->accept( *this ); 125 162 output << ";" << endl; 126 163 } 127 164 128 cur_indent -= CodeGenerator::tabsize; 165 cur_indent -= CodeGenerator::tabsize; 129 166 130 167 output << indent << "}"; … … 141 178 handleAggregate( aggregateDecl ); 142 179 } 143 180 144 181 void CodeGenerator::visit( EnumDecl *aggDecl ) { 145 182 output << "enum "; … … 147 184 if ( aggDecl->get_name() != "" ) 148 185 output << aggDecl->get_name(); 149 186 150 187 std::list< Declaration* > &memb = aggDecl->get_members(); 151 188 … … 153 190 output << " {" << endl; 154 191 155 cur_indent += CodeGenerator::tabsize; 192 cur_indent += CodeGenerator::tabsize; 156 193 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { 157 194 ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i ); 158 195 assert( obj ); 159 output << indent << mangleName( obj ); 196 output << indent << mangleName( obj ); 160 197 if ( obj->get_init() ) { 161 198 output << " = "; … … 165 202 } // for 166 203 167 cur_indent -= CodeGenerator::tabsize; 204 cur_indent -= CodeGenerator::tabsize; 168 205 169 206 output << indent << "}"; 170 207 } // if 171 208 } 172 209 173 210 void CodeGenerator::visit( TraitDecl *aggregateDecl ) {} 174 211 175 212 void CodeGenerator::visit( TypedefDecl *typeDecl ) { 176 213 output << "typedef "; 177 214 output << genType( typeDecl->get_base(), typeDecl->get_name() ); 178 215 } 179 216 180 217 void CodeGenerator::visit( TypeDecl *typeDecl ) { 181 218 // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes, … … 213 250 printDesignators( init->get_designators() ); 214 251 output << "{ "; 215 genCommaList( init->begin_initializers(), init->end_initializers() ); 252 if ( init->begin_initializers() == init->end_initializers() ) { 253 // illegal to leave initializer list empty for scalar initializers, 254 // but always legal to have 0 255 output << "0"; 256 } else { 257 genCommaList( init->begin_initializers(), init->end_initializers() ); 258 } 216 259 output << " }"; 217 260 } 218 261 219 void CodeGenerator::visit( Constant *constant ) { 262 void CodeGenerator::visit( Constant *constant ) { 220 263 output << constant->get_value() ; 221 264 } … … 223 266 //*** Expressions 224 267 void CodeGenerator::visit( ApplicationExpr *applicationExpr ) { 268 extension( applicationExpr ); 225 269 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) { 226 270 OperatorInfo opInfo; … … 231 275 case OT_POSTFIXASSIGN: 232 276 case OT_INFIXASSIGN: 277 case OT_CTOR: 278 case OT_DTOR: 233 279 { 234 280 assert( arg != applicationExpr->get_args().end() ); 235 281 if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) { 236 282 // remove & from first assignment/ctor argument 237 283 *arg = addrExpr->get_arg(); 238 284 } else { 285 // no address-of operator, so must be a pointer - add dereference 239 286 UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) ); 240 287 newExpr->get_args().push_back( *arg ); 288 assert( (*arg)->get_results().size() == 1 ); 289 Type * type = InitTweak::getPointerBase( (*arg)->get_results().front() ); 290 assert( type ); 291 newExpr->get_results().push_back( type ); 241 292 *arg = newExpr; 242 293 } // if 243 294 break; 244 295 } 245 296 246 297 default: 247 298 // do nothing 248 299 ; 249 300 } 250 301 251 302 switch ( opInfo.type ) { 252 303 case OT_INDEX: … … 257 308 output << "]"; 258 309 break; 259 310 260 311 case OT_CALL: 261 312 // there are no intrinsic definitions of the function call operator 262 313 assert( false ); 263 314 break; 264 315 316 case OT_CTOR: 317 case OT_DTOR: 318 if ( applicationExpr->get_args().size() == 1 ) { 319 // the expression fed into a single parameter constructor or destructor 320 // may contain side effects, so must still output this expression 321 output << "("; 322 (*arg++)->accept( *this ); 323 output << ") /* " << opInfo.inputName << " */"; 324 } else if ( applicationExpr->get_args().size() == 2 ) { 325 // intrinsic two parameter constructors are essentially bitwise assignment 326 output << "("; 327 (*arg++)->accept( *this ); 328 output << opInfo.symbol; 329 (*arg)->accept( *this ); 330 output << ") /* " << opInfo.inputName << " */"; 331 } else { 332 // no constructors with 0 or more than 2 parameters 333 assert( false ); 334 } 335 break; 336 265 337 case OT_PREFIX: 266 338 case OT_PREFIXASSIGN: … … 271 343 output << ")"; 272 344 break; 273 345 274 346 case OT_POSTFIX: 275 347 case OT_POSTFIXASSIGN: … … 278 350 output << opInfo.symbol; 279 351 break; 352 280 353 281 354 case OT_INFIX: … … 288 361 output << ")"; 289 362 break; 290 363 291 364 case OT_CONSTANT: 292 365 case OT_LABELADDRESS: … … 307 380 } // if 308 381 } 309 382 310 383 void CodeGenerator::visit( UntypedExpr *untypedExpr ) { 384 extension( untypedExpr ); 311 385 if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) { 312 386 OperatorInfo opInfo; … … 321 395 output << "]"; 322 396 break; 323 397 324 398 case OT_CALL: 325 399 assert( false ); 326 break; 327 400 401 402 case OT_CTOR: 403 case OT_DTOR: 404 if ( untypedExpr->get_args().size() == 1 ) { 405 // the expression fed into a single parameter constructor or destructor 406 // may contain side effects, so must still output this expression 407 output << "("; 408 (*arg++)->accept( *this ); 409 output << ") /* " << opInfo.inputName << " */"; 410 } else if ( untypedExpr->get_args().size() == 2 ) { 411 // intrinsic two parameter constructors are essentially bitwise assignment 412 output << "("; 413 (*arg++)->accept( *this ); 414 output << opInfo.symbol; 415 (*arg)->accept( *this ); 416 output << ") /* " << opInfo.inputName << " */"; 417 } else { 418 // no constructors with 0 or more than 2 parameters 419 assert( false ); 420 } 421 break; 422 328 423 case OT_PREFIX: 329 424 case OT_PREFIXASSIGN: … … 335 430 output << ")"; 336 431 break; 337 432 338 433 case OT_POSTFIX: 339 434 case OT_POSTFIXASSIGN: … … 342 437 output << opInfo.symbol; 343 438 break; 344 439 345 440 case OT_INFIX: 346 441 case OT_INFIXASSIGN: … … 352 447 output << ")"; 353 448 break; 354 449 355 450 case OT_CONSTANT: 356 451 // there are no intrinsic definitions of 0 or 1 as functions … … 370 465 } // if 371 466 } 372 467 373 468 void CodeGenerator::visit( NameExpr *nameExpr ) { 469 extension( nameExpr ); 374 470 OperatorInfo opInfo; 375 471 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) { … … 380 476 } // if 381 477 } 382 478 383 479 void CodeGenerator::visit( AddressExpr *addressExpr ) { 480 extension( addressExpr ); 384 481 output << "(&"; 385 482 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address … … 393 490 394 491 void CodeGenerator::visit( CastExpr *castExpr ) { 492 extension( castExpr ); 395 493 output << "("; 396 494 if ( castExpr->get_results().empty() ) { … … 409 507 output << ")"; 410 508 } 411 509 412 510 void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) { 413 511 assert( false ); 414 512 } 415 513 416 514 void CodeGenerator::visit( MemberExpr *memberExpr ) { 515 extension( memberExpr ); 417 516 memberExpr->get_aggregate()->accept( *this ); 418 517 output << "." << mangleName( memberExpr->get_member() ); 419 518 } 420 519 421 520 void CodeGenerator::visit( VariableExpr *variableExpr ) { 521 extension( variableExpr ); 422 522 OperatorInfo opInfo; 423 523 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) { … … 427 527 } // if 428 528 } 429 529 430 530 void CodeGenerator::visit( ConstantExpr *constantExpr ) { 431 531 assert( constantExpr->get_constant() ); 532 extension( constantExpr ); 432 533 constantExpr->get_constant()->accept( *this ); 433 534 } 434 535 435 536 void CodeGenerator::visit( SizeofExpr *sizeofExpr ) { 537 extension( sizeofExpr ); 436 538 output << "sizeof("; 437 539 if ( sizeofExpr->get_isType() ) { … … 444 546 445 547 void CodeGenerator::visit( AlignofExpr *alignofExpr ) { 548 extension( alignofExpr ); 446 549 // use GCC extension to avoid bumping std to C11 447 550 output << "__alignof__("; … … 459 562 460 563 void CodeGenerator::visit( OffsetofExpr *offsetofExpr ) { 564 extension( offsetofExpr ); 461 565 // use GCC builtin 462 566 output << "__builtin_offsetof("; … … 469 573 assert( false && "OffsetPackExpr should not reach code generation" ); 470 574 } 471 575 472 576 void CodeGenerator::visit( LogicalExpr *logicalExpr ) { 577 extension( logicalExpr ); 473 578 output << "("; 474 579 logicalExpr->get_arg1()->accept( *this ); … … 481 586 output << ")"; 482 587 } 483 588 484 589 void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) { 590 extension( conditionalExpr ); 485 591 output << "("; 486 592 conditionalExpr->get_arg1()->accept( *this ); … … 491 597 output << ")"; 492 598 } 493 599 494 600 void CodeGenerator::visit( CommaExpr *commaExpr ) { 601 extension( commaExpr ); 495 602 output << "("; 496 603 commaExpr->get_arg1()->accept( *this ); … … 499 606 output << ")"; 500 607 } 501 608 502 609 void CodeGenerator::visit( TupleExpr *tupleExpr ) {} 503 610 504 611 void CodeGenerator::visit( TypeExpr *typeExpr ) {} 505 612 506 613 void CodeGenerator::visit( AsmExpr *asmExpr ) { 614 extension( asmExpr ); 507 615 if ( asmExpr->get_inout() ) { 508 616 output << "[ "; … … 532 640 } 533 641 } 534 cur_indent -= CodeGenerator::tabsize; 642 cur_indent -= CodeGenerator::tabsize; 535 643 536 644 output << indent << "}"; … … 538 646 539 647 void CodeGenerator::visit( ExprStmt *exprStmt ) { 540 // I don't see why this check is necessary.541 // If this starts to cause problems then put it back in,542 // with an explanation543 648 assert( exprStmt ); 544 545 // if ( exprStmt != 0 ) { 546 exprStmt->get_expr()->accept( *this ); 547 output << ";" ; 548 // } // if 649 // cast the top-level expression to void to reduce gcc warnings. 650 Expression * expr = new CastExpr( exprStmt->get_expr() ); 651 expr->accept( *this ); 652 output << ";"; 549 653 } 550 654 … … 589 693 switchStmt->get_condition()->accept( *this ); 590 694 output << " ) "; 591 695 592 696 output << "{" << std::endl; 593 697 cur_indent += CodeGenerator::tabsize; … … 609 713 } // if 610 714 output << ":\n"; 611 715 612 716 std::list<Statement *> sts = caseStmt->get_statements(); 613 717 … … 626 730 if ( ! branchStmt->get_target().empty() ) 627 731 output << "goto " << branchStmt->get_target(); 628 else { 732 else { 629 733 if ( branchStmt->get_computedTarget() != 0 ) { 630 734 output << "goto *"; … … 646 750 void CodeGenerator::visit( ReturnStmt *returnStmt ) { 647 751 output << "return "; 648 649 // xxx -- check for null expression; 650 if ( returnStmt->get_expr() ) { 651 returnStmt->get_expr()->accept( *this ); 652 } // if 752 maybeAccept( returnStmt->get_expr(), *this ); 653 753 output << ";"; 654 754 } 655 755 656 756 void CodeGenerator::visit( WhileStmt *whileStmt ) { 657 if ( whileStmt->get_isDoWhile() ) 757 if ( whileStmt->get_isDoWhile() ) { 658 758 output << "do" ; 659 else {759 } else { 660 760 output << "while (" ; 661 761 whileStmt->get_condition()->accept( *this ); … … 677 777 678 778 void CodeGenerator::visit( ForStmt *forStmt ) { 679 // initialization is always hoisted, so don't 680 // bother doing anything with that 779 // initialization is always hoisted, so don't 780 // bother doing anything with that 681 781 output << "for (;"; 682 782 683 if ( forStmt->get_condition() != 0 ) 783 if ( forStmt->get_condition() != 0 ) { 684 784 forStmt->get_condition()->accept( *this ); 785 } 685 786 output << ";"; 686 787 687 if ( forStmt->get_increment() != 0 ) 688 forStmt->get_increment()->accept( *this ); 788 if ( forStmt->get_increment() != 0 ) { 789 // cast the top-level expression to void to reduce gcc warnings. 790 Expression * expr = new CastExpr( forStmt->get_increment() ); 791 expr->accept( *this ); 792 } 689 793 output << ") "; 690 794 … … 702 806 void CodeGenerator::visit( DeclStmt *declStmt ) { 703 807 declStmt->get_decl()->accept( *this ); 704 808 705 809 if ( doSemicolon( declStmt->get_decl() ) ) { 706 810 output << ";"; 707 811 } // if 708 }709 710 std::string CodeGenerator::printLabels( std::list< Label > &l ) {711 std::string str( "" );712 l.unique(); // assumes a sorted list. Why not use set?713 714 for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ )715 str += *i + ": ";716 717 return str;718 812 } 719 813
Note:
See TracChangeset
for help on using the changeset viewer.