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