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