Changes in src/CodeGen/CodeGenerator.cc [c2ce2350:afc1045]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
rc2ce2350 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 : Wed Apr 27 11:59:36 201613 // 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 case OT_DTOR:242 233 { 243 234 assert( arg != applicationExpr->get_args().end() ); 244 235 if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) { 245 // remove & from first assignment/ctor argument236 246 237 *arg = addrExpr->get_arg(); 247 238 } else { 248 // no address-of operator, so must be a pointer - add dereference249 239 UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) ); 250 240 newExpr->get_args().push_back( *arg ); … … 253 243 break; 254 244 } 255 245 256 246 default: 257 247 // do nothing 258 248 ; 259 249 } 260 250 261 251 switch ( opInfo.type ) { 262 252 case OT_INDEX: … … 267 257 output << "]"; 268 258 break; 269 259 270 260 case OT_CALL: 271 261 // there are no intrinsic definitions of the function call operator 272 262 assert( false ); 273 263 break; 274 275 case OT_CTOR: 276 case OT_DTOR: 277 if ( applicationExpr->get_args().size() == 1 ) { 278 // the expression fed into a single parameter constructor or destructor 279 // may contain side effects - output as a void expression 280 output << "((void)("; 281 (*arg++)->accept( *this ); 282 output << ")) /* " << opInfo.inputName << " */"; 283 } else if ( applicationExpr->get_args().size() == 2 ) { 284 // intrinsic two parameter constructors are essentially bitwise assignment 285 output << "("; 286 (*arg++)->accept( *this ); 287 output << opInfo.symbol; 288 (*arg)->accept( *this ); 289 output << ") /* " << opInfo.inputName << " */"; 290 } else { 291 // no constructors with 0 or more than 2 parameters 292 assert( false ); 293 } 294 break; 295 264 296 265 case OT_PREFIX: 297 266 case OT_PREFIXASSIGN: … … 302 271 output << ")"; 303 272 break; 304 273 305 274 case OT_POSTFIX: 306 275 case OT_POSTFIXASSIGN: … … 309 278 output << opInfo.symbol; 310 279 break; 311 312 280 313 281 case OT_INFIX: … … 320 288 output << ")"; 321 289 break; 322 290 323 291 case OT_CONSTANT: 324 292 case OT_LABELADDRESS: … … 339 307 } // if 340 308 } 341 309 342 310 void CodeGenerator::visit( UntypedExpr *untypedExpr ) { 343 311 if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) { … … 353 321 output << "]"; 354 322 break; 355 323 356 324 case OT_CALL: 357 325 assert( false ); 358 359 360 case OT_CTOR: 361 case OT_DTOR: 362 if ( untypedExpr->get_args().size() == 1 ) { 363 // the expression fed into a single parameter constructor or destructor 364 // may contain side effects - output as a void expression 365 output << "((void)("; 366 (*arg++)->accept( *this ); 367 output << ")) /* " << opInfo.inputName << " */"; 368 } else if ( untypedExpr->get_args().size() == 2 ) { 369 // intrinsic two parameter constructors are essentially bitwise assignment 370 output << "("; 371 (*arg++)->accept( *this ); 372 output << opInfo.symbol; 373 (*arg)->accept( *this ); 374 output << ") /* " << opInfo.inputName << " */"; 375 } else { 376 // no constructors with 0 or more than 2 parameters 377 assert( false ); 378 } 379 break; 380 326 break; 327 381 328 case OT_PREFIX: 382 329 case OT_PREFIXASSIGN: … … 388 335 output << ")"; 389 336 break; 390 337 391 338 case OT_POSTFIX: 392 339 case OT_POSTFIXASSIGN: … … 395 342 output << opInfo.symbol; 396 343 break; 397 344 398 345 case OT_INFIX: 399 346 case OT_INFIXASSIGN: … … 405 352 output << ")"; 406 353 break; 407 354 408 355 case OT_CONSTANT: 409 356 // there are no intrinsic definitions of 0 or 1 as functions … … 423 370 } // if 424 371 } 425 372 426 373 void CodeGenerator::visit( NameExpr *nameExpr ) { 427 374 OperatorInfo opInfo; … … 433 380 } // if 434 381 } 435 382 436 383 void CodeGenerator::visit( AddressExpr *addressExpr ) { 437 384 output << "(&"; … … 462 409 output << ")"; 463 410 } 464 411 465 412 void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) { 466 413 assert( false ); 467 414 } 468 415 469 416 void CodeGenerator::visit( MemberExpr *memberExpr ) { 470 417 memberExpr->get_aggregate()->accept( *this ); 471 418 output << "." << mangleName( memberExpr->get_member() ); 472 419 } 473 420 474 421 void CodeGenerator::visit( VariableExpr *variableExpr ) { 475 422 OperatorInfo opInfo; … … 480 427 } // if 481 428 } 482 429 483 430 void CodeGenerator::visit( ConstantExpr *constantExpr ) { 484 431 assert( constantExpr->get_constant() ); 485 432 constantExpr->get_constant()->accept( *this ); 486 433 } 487 434 488 435 void CodeGenerator::visit( SizeofExpr *sizeofExpr ) { 489 436 output << "sizeof("; … … 522 469 assert( false && "OffsetPackExpr should not reach code generation" ); 523 470 } 524 471 525 472 void CodeGenerator::visit( LogicalExpr *logicalExpr ) { 526 473 output << "("; … … 534 481 output << ")"; 535 482 } 536 483 537 484 void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) { 538 485 output << "("; … … 544 491 output << ")"; 545 492 } 546 493 547 494 void CodeGenerator::visit( CommaExpr *commaExpr ) { 548 495 output << "("; … … 552 499 output << ")"; 553 500 } 554 501 555 502 void CodeGenerator::visit( TupleExpr *tupleExpr ) {} 556 503 557 504 void CodeGenerator::visit( TypeExpr *typeExpr ) {} 558 505 … … 585 532 } 586 533 } 587 cur_indent -= CodeGenerator::tabsize; 534 cur_indent -= CodeGenerator::tabsize; 588 535 589 536 output << indent << "}"; … … 591 538 592 539 void CodeGenerator::visit( ExprStmt *exprStmt ) { 593 // I don't see why this check is necessary. 594 // 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, 595 542 // with an explanation 596 543 assert( exprStmt ); … … 642 589 switchStmt->get_condition()->accept( *this ); 643 590 output << " ) "; 644 591 645 592 output << "{" << std::endl; 646 593 cur_indent += CodeGenerator::tabsize; … … 662 609 } // if 663 610 output << ":\n"; 664 611 665 612 std::list<Statement *> sts = caseStmt->get_statements(); 666 613 … … 679 626 if ( ! branchStmt->get_target().empty() ) 680 627 output << "goto " << branchStmt->get_target(); 681 else { 628 else { 682 629 if ( branchStmt->get_computedTarget() != 0 ) { 683 630 output << "goto *"; … … 730 677 731 678 void CodeGenerator::visit( ForStmt *forStmt ) { 732 // initialization is always hoisted, so don't 733 // bother doing anything with that 679 // initialization is always hoisted, so don't 680 // bother doing anything with that 734 681 output << "for (;"; 735 682 … … 755 702 void CodeGenerator::visit( DeclStmt *declStmt ) { 756 703 declStmt->get_decl()->accept( *this ); 757 704 758 705 if ( doSemicolon( declStmt->get_decl() ) ) { 759 706 output << ";";
Note: See TracChangeset
for help on using the changeset viewer.