Changes in src/CodeGen/CodeGenerator.cc [afc1045:356189a]
- File:
-
- 1 edited
-
src/CodeGen/CodeGenerator.cc (modified) (36 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
rafc1045 r356189a 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 : Wed Mar 2 17:32:16201613 // Update Count : 2 4311 // Last Modified By : Rob Schluntz 12 // Last Modified On : Thu Apr 14 17:10:21 2016 13 // Update Count : 255 14 14 // 15 15 … … 67 67 string mangleName( DeclarationWithType *decl ) { 68 68 if ( decl->get_mangleName() != "" ) { 69 return decl->get_mangleName(); 69 // need to incorporate scope level in order to differentiate names for destructors 70 return decl->get_scopedMangleName(); 70 71 } else { 71 72 return decl->get_name(); … … 99 100 handleStorageClass( objectDecl ); 100 101 output << genType( objectDecl->get_type(), mangleName( objectDecl ) ); 101 102 102 103 if ( objectDecl->get_init() ) { 103 104 output << " = "; … … 113 114 if ( aggDecl->get_name() != "" ) 114 115 output << aggDecl->get_name(); 115 116 116 117 std::list< Declaration * > &memb = aggDecl->get_members(); 117 118 … … 119 120 output << " {" << endl; 120 121 121 cur_indent += CodeGenerator::tabsize; 122 cur_indent += CodeGenerator::tabsize; 122 123 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { 123 output << indent; 124 output << indent; 124 125 (*i)->accept( *this ); 125 126 output << ";" << endl; 126 127 } 127 128 128 cur_indent -= CodeGenerator::tabsize; 129 cur_indent -= CodeGenerator::tabsize; 129 130 130 131 output << indent << "}"; … … 141 142 handleAggregate( aggregateDecl ); 142 143 } 143 144 144 145 void CodeGenerator::visit( EnumDecl *aggDecl ) { 145 146 output << "enum "; … … 147 148 if ( aggDecl->get_name() != "" ) 148 149 output << aggDecl->get_name(); 149 150 150 151 std::list< Declaration* > &memb = aggDecl->get_members(); 151 152 … … 153 154 output << " {" << endl; 154 155 155 cur_indent += CodeGenerator::tabsize; 156 cur_indent += CodeGenerator::tabsize; 156 157 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { 157 158 ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i ); 158 159 assert( obj ); 159 output << indent << mangleName( obj ); 160 output << indent << mangleName( obj ); 160 161 if ( obj->get_init() ) { 161 162 output << " = "; … … 165 166 } // for 166 167 167 cur_indent -= CodeGenerator::tabsize; 168 cur_indent -= CodeGenerator::tabsize; 168 169 169 170 output << indent << "}"; 170 171 } // if 171 172 } 172 173 173 174 void CodeGenerator::visit( TraitDecl *aggregateDecl ) {} 174 175 175 176 void CodeGenerator::visit( TypedefDecl *typeDecl ) { 176 177 output << "typedef "; 177 178 output << genType( typeDecl->get_base(), typeDecl->get_name() ); 178 179 } 179 180 180 181 void CodeGenerator::visit( TypeDecl *typeDecl ) { 181 182 // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes, … … 213 214 printDesignators( init->get_designators() ); 214 215 output << "{ "; 215 genCommaList( init->begin_initializers(), init->end_initializers() ); 216 if ( init->begin_initializers() == init->end_initializers() ) { 217 // illegal to leave initializer list empty for scalar initializers, 218 // but always legal to have 0 219 output << "0"; 220 } else { 221 genCommaList( init->begin_initializers(), init->end_initializers() ); 222 } 216 223 output << " }"; 217 224 } 218 225 219 void CodeGenerator::visit( Constant *constant ) { 226 void CodeGenerator::visit( Constant *constant ) { 220 227 output << constant->get_value() ; 221 228 } … … 231 238 case OT_POSTFIXASSIGN: 232 239 case OT_INFIXASSIGN: 240 case OT_CTOR: 233 241 { 234 242 assert( arg != applicationExpr->get_args().end() ); 235 243 if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) { 236 244 // remove & from first assignment/ctor argument 237 245 *arg = addrExpr->get_arg(); 238 246 } else { 247 // no address-of operator, so must be a pointer - add dereference 239 248 UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) ); 240 249 newExpr->get_args().push_back( *arg ); … … 243 252 break; 244 253 } 245 254 246 255 default: 247 256 // do nothing 248 257 ; 249 258 } 250 259 251 260 switch ( opInfo.type ) { 252 261 case OT_INDEX: … … 257 266 output << "]"; 258 267 break; 259 268 260 269 case OT_CALL: 261 270 // there are no intrinsic definitions of the function call operator 262 271 assert( false ); 263 272 break; 264 273 274 case OT_CTOR: 275 if ( applicationExpr->get_args().size() == 1 ) { 276 // the expression fed into a single parameter constructor may contain 277 // side effects - output as a void expression 278 output << "((void)("; 279 (*arg++)->accept( *this ); 280 output << ")) /* ?{} */"; 281 } else if ( applicationExpr->get_args().size() == 2 ) { 282 // intrinsic constructors are essentially bitwise assignment 283 output << "("; 284 (*arg++)->accept( *this ); 285 output << opInfo.symbol; 286 (*arg)->accept( *this ); 287 output << ") /* ?{} */"; 288 } else { 289 // not constructors with 0 or more than 2 parameters 290 assert( false ); 291 } 292 break; 293 294 case OT_DTOR: 295 // intrinsic destructors do nothing - don't generate any code 296 output << " /* " << dynamic_cast<VariableExpr*>(applicationExpr->get_function())->get_var()->get_name() << " */"; 297 break; 298 265 299 case OT_PREFIX: 266 300 case OT_PREFIXASSIGN: … … 271 305 output << ")"; 272 306 break; 273 307 274 308 case OT_POSTFIX: 275 309 case OT_POSTFIXASSIGN: … … 278 312 output << opInfo.symbol; 279 313 break; 314 280 315 281 316 case OT_INFIX: … … 288 323 output << ")"; 289 324 break; 290 325 291 326 case OT_CONSTANT: 292 327 case OT_LABELADDRESS: … … 307 342 } // if 308 343 } 309 344 310 345 void CodeGenerator::visit( UntypedExpr *untypedExpr ) { 311 346 if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) { … … 321 356 output << "]"; 322 357 break; 323 358 324 359 case OT_CALL: 325 360 assert( false ); 326 break; 327 361 362 case OT_CTOR: 363 case OT_DTOR: 364 // intrinsic constructors should never be called 365 // intrinsic destructors do nothing 366 break; 367 328 368 case OT_PREFIX: 329 369 case OT_PREFIXASSIGN: … … 335 375 output << ")"; 336 376 break; 337 377 338 378 case OT_POSTFIX: 339 379 case OT_POSTFIXASSIGN: … … 342 382 output << opInfo.symbol; 343 383 break; 344 384 345 385 case OT_INFIX: 346 386 case OT_INFIXASSIGN: … … 352 392 output << ")"; 353 393 break; 354 394 355 395 case OT_CONSTANT: 356 396 // there are no intrinsic definitions of 0 or 1 as functions … … 370 410 } // if 371 411 } 372 412 373 413 void CodeGenerator::visit( NameExpr *nameExpr ) { 374 414 OperatorInfo opInfo; … … 380 420 } // if 381 421 } 382 422 383 423 void CodeGenerator::visit( AddressExpr *addressExpr ) { 384 424 output << "(&"; … … 409 449 output << ")"; 410 450 } 411 451 412 452 void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) { 413 453 assert( false ); 414 454 } 415 455 416 456 void CodeGenerator::visit( MemberExpr *memberExpr ) { 417 457 memberExpr->get_aggregate()->accept( *this ); 418 458 output << "." << mangleName( memberExpr->get_member() ); 419 459 } 420 460 421 461 void CodeGenerator::visit( VariableExpr *variableExpr ) { 422 462 OperatorInfo opInfo; … … 427 467 } // if 428 468 } 429 469 430 470 void CodeGenerator::visit( ConstantExpr *constantExpr ) { 431 471 assert( constantExpr->get_constant() ); 432 472 constantExpr->get_constant()->accept( *this ); 433 473 } 434 474 435 475 void CodeGenerator::visit( SizeofExpr *sizeofExpr ) { 436 476 output << "sizeof("; … … 469 509 assert( false && "OffsetPackExpr should not reach code generation" ); 470 510 } 471 511 472 512 void CodeGenerator::visit( LogicalExpr *logicalExpr ) { 473 513 output << "("; … … 481 521 output << ")"; 482 522 } 483 523 484 524 void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) { 485 525 output << "("; … … 491 531 output << ")"; 492 532 } 493 533 494 534 void CodeGenerator::visit( CommaExpr *commaExpr ) { 495 535 output << "("; … … 499 539 output << ")"; 500 540 } 501 541 502 542 void CodeGenerator::visit( TupleExpr *tupleExpr ) {} 503 543 504 544 void CodeGenerator::visit( TypeExpr *typeExpr ) {} 505 545 … … 532 572 } 533 573 } 534 cur_indent -= CodeGenerator::tabsize; 574 cur_indent -= CodeGenerator::tabsize; 535 575 536 576 output << indent << "}"; … … 538 578 539 579 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, 580 // I don't see why this check is necessary. 581 // If this starts to cause problems then put it back in, 542 582 // with an explanation 543 583 assert( exprStmt ); … … 589 629 switchStmt->get_condition()->accept( *this ); 590 630 output << " ) "; 591 631 592 632 output << "{" << std::endl; 593 633 cur_indent += CodeGenerator::tabsize; … … 609 649 } // if 610 650 output << ":\n"; 611 651 612 652 std::list<Statement *> sts = caseStmt->get_statements(); 613 653 … … 626 666 if ( ! branchStmt->get_target().empty() ) 627 667 output << "goto " << branchStmt->get_target(); 628 else { 668 else { 629 669 if ( branchStmt->get_computedTarget() != 0 ) { 630 670 output << "goto *"; … … 677 717 678 718 void CodeGenerator::visit( ForStmt *forStmt ) { 679 // initialization is always hoisted, so don't 680 // bother doing anything with that 719 // initialization is always hoisted, so don't 720 // bother doing anything with that 681 721 output << "for (;"; 682 722 … … 702 742 void CodeGenerator::visit( DeclStmt *declStmt ) { 703 743 declStmt->get_decl()->accept( *this ); 704 744 705 745 if ( doSemicolon( declStmt->get_decl() ) ) { 706 746 output << ";";
Note:
See TracChangeset
for help on using the changeset viewer.