Changes in src/CodeGen/CodeGenerator.cc [e8032b0:5b2f5bb]
- File:
-
- 1 edited
-
src/CodeGen/CodeGenerator.cc (modified) (36 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
re8032b0 r5b2f5bb 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 : Wed Mar 30 14:39:30 2016 13 // Update Count : 255 14 14 // 15 15 … … 21 21 #include "Parser/ParseNode.h" 22 22 23 #include "SynTree/ Declaration.h"23 #include "SynTree/Type.h" 24 24 #include "SynTree/Expression.h" 25 25 #include "SynTree/Initializer.h" 26 26 #include "SynTree/Statement.h" 27 #include "SynTree/Type.h"28 27 29 28 #include "Common/utility.h" … … 99 98 handleStorageClass( objectDecl ); 100 99 output << genType( objectDecl->get_type(), mangleName( objectDecl ) ); 101 100 102 101 if ( objectDecl->get_init() ) { 103 102 output << " = "; … … 113 112 if ( aggDecl->get_name() != "" ) 114 113 output << aggDecl->get_name(); 115 114 116 115 std::list< Declaration * > &memb = aggDecl->get_members(); 117 116 … … 119 118 output << " {" << endl; 120 119 121 cur_indent += CodeGenerator::tabsize; 120 cur_indent += CodeGenerator::tabsize; 122 121 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { 123 output << indent; 122 output << indent; 124 123 (*i)->accept( *this ); 125 124 output << ";" << endl; 126 125 } 127 126 128 cur_indent -= CodeGenerator::tabsize; 127 cur_indent -= CodeGenerator::tabsize; 129 128 130 129 output << indent << "}"; … … 141 140 handleAggregate( aggregateDecl ); 142 141 } 143 142 144 143 void CodeGenerator::visit( EnumDecl *aggDecl ) { 145 144 output << "enum "; … … 147 146 if ( aggDecl->get_name() != "" ) 148 147 output << aggDecl->get_name(); 149 148 150 149 std::list< Declaration* > &memb = aggDecl->get_members(); 151 150 … … 153 152 output << " {" << endl; 154 153 155 cur_indent += CodeGenerator::tabsize; 154 cur_indent += CodeGenerator::tabsize; 156 155 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { 157 156 ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i ); 158 157 assert( obj ); 159 output << indent << mangleName( obj ); 158 output << indent << mangleName( obj ); 160 159 if ( obj->get_init() ) { 161 160 output << " = "; … … 165 164 } // for 166 165 167 cur_indent -= CodeGenerator::tabsize; 166 cur_indent -= CodeGenerator::tabsize; 168 167 169 168 output << indent << "}"; 170 169 } // if 171 170 } 172 173 void CodeGenerator::visit( TraitDecl *aggregateDecl ) {}174 171 172 void CodeGenerator::visit( ContextDecl *aggregateDecl ) {} 173 175 174 void CodeGenerator::visit( TypedefDecl *typeDecl ) { 176 175 output << "typedef "; 177 176 output << genType( typeDecl->get_base(), typeDecl->get_name() ); 178 177 } 179 178 180 179 void CodeGenerator::visit( TypeDecl *typeDecl ) { 181 180 // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes, … … 213 212 printDesignators( init->get_designators() ); 214 213 output << "{ "; 215 genCommaList( init->begin_initializers(), init->end_initializers() ); 214 if ( init->begin_initializers() == init->end_initializers() ) { 215 // illegal to leave initializer list empty for scalar initializers, 216 // but always legal to have 0 217 output << "0"; 218 } else { 219 genCommaList( init->begin_initializers(), init->end_initializers() ); 220 } 216 221 output << " }"; 217 222 } 218 223 219 void CodeGenerator::visit( Constant *constant ) { 224 void CodeGenerator::visit( Constant *constant ) { 220 225 output << constant->get_value() ; 221 226 } … … 234 239 assert( arg != applicationExpr->get_args().end() ); 235 240 if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) { 236 241 237 242 *arg = addrExpr->get_arg(); 238 243 } else { … … 243 248 break; 244 249 } 245 250 246 251 default: 247 252 // do nothing 248 253 ; 249 254 } 250 255 251 256 switch ( opInfo.type ) { 252 257 case OT_INDEX: … … 257 262 output << "]"; 258 263 break; 259 264 260 265 case OT_CALL: 261 // there are no intrinsic definitions of the function call operator 266 // there are no intrinsic definitions of the function call operator or constructors or destructors 262 267 assert( false ); 263 268 break; 264 269 270 case OT_CTOR: 271 // it's just an optimization to disallow this, so for now let it through 272 // since it makes autogenerating constructors a lot easier 273 varExpr->accept( *this ); 274 output << "("; 275 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); 276 output << ")"; 277 278 // intrinsic constructors should never be called directly - they should be transformed back into Initializer nodes 279 // assert(false); 280 break; 281 282 case OT_DTOR: 283 // intrinsic destructors do nothing - don't generate any code 284 output << " /* " << dynamic_cast<VariableExpr*>(applicationExpr->get_function())->get_var()->get_name() << " */"; 285 break; 286 265 287 case OT_PREFIX: 266 288 case OT_PREFIXASSIGN: … … 271 293 output << ")"; 272 294 break; 273 295 274 296 case OT_POSTFIX: 275 297 case OT_POSTFIXASSIGN: … … 278 300 output << opInfo.symbol; 279 301 break; 302 280 303 281 304 case OT_INFIX: … … 288 311 output << ")"; 289 312 break; 290 313 291 314 case OT_CONSTANT: 292 315 case OT_LABELADDRESS: … … 307 330 } // if 308 331 } 309 332 310 333 void CodeGenerator::visit( UntypedExpr *untypedExpr ) { 311 334 if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) { … … 321 344 output << "]"; 322 345 break; 323 346 324 347 case OT_CALL: 325 348 assert( false ); 326 break; 327 349 350 case OT_CTOR: 351 case OT_DTOR: 352 // intrinsic constructors should never be called 353 // intrinsic destructors do nothing 354 break; 355 328 356 case OT_PREFIX: 329 357 case OT_PREFIXASSIGN: … … 335 363 output << ")"; 336 364 break; 337 365 338 366 case OT_POSTFIX: 339 367 case OT_POSTFIXASSIGN: … … 342 370 output << opInfo.symbol; 343 371 break; 344 372 345 373 case OT_INFIX: 346 374 case OT_INFIXASSIGN: … … 352 380 output << ")"; 353 381 break; 354 382 355 383 case OT_CONSTANT: 356 384 // there are no intrinsic definitions of 0 or 1 as functions … … 370 398 } // if 371 399 } 372 400 373 401 void CodeGenerator::visit( NameExpr *nameExpr ) { 374 402 OperatorInfo opInfo; … … 380 408 } // if 381 409 } 382 410 383 411 void CodeGenerator::visit( AddressExpr *addressExpr ) { 384 412 output << "(&"; … … 409 437 output << ")"; 410 438 } 411 439 412 440 void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) { 413 441 assert( false ); 414 442 } 415 443 416 444 void CodeGenerator::visit( MemberExpr *memberExpr ) { 417 445 memberExpr->get_aggregate()->accept( *this ); 418 446 output << "." << mangleName( memberExpr->get_member() ); 419 447 } 420 448 421 449 void CodeGenerator::visit( VariableExpr *variableExpr ) { 422 450 OperatorInfo opInfo; … … 427 455 } // if 428 456 } 429 457 430 458 void CodeGenerator::visit( ConstantExpr *constantExpr ) { 431 459 assert( constantExpr->get_constant() ); 432 460 constantExpr->get_constant()->accept( *this ); 433 461 } 434 462 435 463 void CodeGenerator::visit( SizeofExpr *sizeofExpr ) { 436 464 output << "sizeof("; … … 465 493 output << ")"; 466 494 } 467 495 468 496 void CodeGenerator::visit( LogicalExpr *logicalExpr ) { 469 497 output << "("; … … 477 505 output << ")"; 478 506 } 479 507 480 508 void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) { 481 509 output << "("; … … 487 515 output << ")"; 488 516 } 489 517 490 518 void CodeGenerator::visit( CommaExpr *commaExpr ) { 491 519 output << "("; … … 495 523 output << ")"; 496 524 } 497 525 498 526 void CodeGenerator::visit( TupleExpr *tupleExpr ) {} 499 527 500 528 void CodeGenerator::visit( TypeExpr *typeExpr ) {} 501 529 … … 528 556 } 529 557 } 530 cur_indent -= CodeGenerator::tabsize; 558 cur_indent -= CodeGenerator::tabsize; 531 559 532 560 output << indent << "}"; … … 534 562 535 563 void CodeGenerator::visit( ExprStmt *exprStmt ) { 536 // I don't see why this check is necessary. 537 // If this starts to cause problems then put it back in, 564 // I don't see why this check is necessary. 565 // If this starts to cause problems then put it back in, 538 566 // with an explanation 539 567 assert( exprStmt ); … … 585 613 switchStmt->get_condition()->accept( *this ); 586 614 output << " ) "; 587 615 588 616 output << "{" << std::endl; 589 617 cur_indent += CodeGenerator::tabsize; … … 605 633 } // if 606 634 output << ":\n"; 607 635 608 636 std::list<Statement *> sts = caseStmt->get_statements(); 609 637 … … 622 650 if ( ! branchStmt->get_target().empty() ) 623 651 output << "goto " << branchStmt->get_target(); 624 else { 652 else { 625 653 if ( branchStmt->get_computedTarget() != 0 ) { 626 654 output << "goto *"; … … 673 701 674 702 void CodeGenerator::visit( ForStmt *forStmt ) { 675 // initialization is always hoisted, so don't 676 // bother doing anything with that 703 // initialization is always hoisted, so don't 704 // bother doing anything with that 677 705 output << "for (;"; 678 706 … … 698 726 void CodeGenerator::visit( DeclStmt *declStmt ) { 699 727 declStmt->get_decl()->accept( *this ); 700 728 701 729 if ( doSemicolon( declStmt->get_decl() ) ) { 702 730 output << ";";
Note:
See TracChangeset
for help on using the changeset viewer.