Changeset 6c4ff37 for src/CodeGen
- Timestamp:
- Jun 2, 2015, 11:51:22 AM (9 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 2b6c1e0
- Parents:
- 6db50d5
- Location:
- src/CodeGen
- Files:
-
- 3 edited
- 2 moved
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/CodeGenerator.cc
r6db50d5 r6c4ff37 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // CodeGenerator 2.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 : Sun May 24 20:43:16 201513 // Update Count : 1111 // Last Modified By : Rob Schluntz 12 // Last Modified On : Tue Jun 02 11:45:06 2015 13 // Update Count : 65 14 14 // 15 15 … … 28 28 #include "UnimplementedError.h" 29 29 30 #include "CodeGenerator 2.h"30 #include "CodeGenerator.h" 31 31 #include "OperatorTable.h" 32 32 #include "GenType.h" … … 35 35 36 36 namespace CodeGen { 37 int CodeGenerator 2::tabsize = 4;38 39 CodeGenerator 2::CodeGenerator2( std::ostream &os ) : cur_indent( 0 ), insideFunction( false ), before( os ), after() { }40 41 CodeGenerator 2::CodeGenerator2( std::ostream &os, std::string init, int indent, bool infunp )42 : cur_indent( indent ), insideFunction( infunp ), before( os ) {43 // before<< std::string( init );44 } 45 46 CodeGenerator 2::CodeGenerator2( std::ostream &os, char *init, int indent, bool infunp )47 : cur_indent( indent ), insideFunction( infunp ), before( os ) {48 // before<< std::string( init );37 int CodeGenerator::tabsize = 4; 38 39 CodeGenerator::CodeGenerator( std::ostream &os ) : cur_indent( 0 ), insideFunction( false ), output( os ) { } 40 41 CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indent, bool infunp ) 42 : cur_indent( indent ), insideFunction( infunp ), output( os ) { 43 //output << std::string( init ); 44 } 45 46 CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indent, bool infunp ) 47 : cur_indent( indent ), insideFunction( infunp ), output( os ) { 48 //output << std::string( init ); 49 49 } 50 50 … … 58 58 59 59 //*** Declarations 60 void CodeGenerator 2::visit( FunctionDecl *functionDecl ) {60 void CodeGenerator::visit( FunctionDecl *functionDecl ) { 61 61 handleStorageClass( functionDecl ); 62 62 if ( functionDecl->get_isInline() ) { 63 before<< "inline ";64 } // if 65 before<< genType( functionDecl->get_functionType(), mangleName( functionDecl ) );63 output << "inline "; 64 } // if 65 output << genType( functionDecl->get_functionType(), mangleName( functionDecl ) ); 66 66 67 67 // how to get this to the Functype? 68 68 std::list< Declaration * > olds = functionDecl->get_oldDecls(); 69 69 if ( ! olds.empty() ) { 70 before<< " /* function has old declaration */";70 output << " /* function has old declaration */"; 71 71 } // if 72 72 … … 77 77 } 78 78 79 void CodeGenerator 2::visit( ObjectDecl *objectDecl ) {79 void CodeGenerator::visit( ObjectDecl *objectDecl ) { 80 80 handleStorageClass( objectDecl ); 81 before<< genType( objectDecl->get_type(), mangleName( objectDecl ) );81 output << genType( objectDecl->get_type(), mangleName( objectDecl ) ); 82 82 83 83 if ( objectDecl->get_init() ) { 84 before<< " = ";84 output << " = "; 85 85 objectDecl->get_init()->accept( *this ); 86 86 } // if 87 87 if ( objectDecl->get_bitfieldWidth() ) { 88 before<< ":";88 output << ":"; 89 89 objectDecl->get_bitfieldWidth()->accept( *this ); 90 90 } // if 91 91 } 92 92 93 void CodeGenerator 2::handleAggregate( AggregateDecl *aggDecl ) {93 void CodeGenerator::handleAggregate( AggregateDecl *aggDecl ) { 94 94 if ( aggDecl->get_name() != "" ) 95 before<< aggDecl->get_name();95 output << aggDecl->get_name(); 96 96 97 97 std::list< Declaration * > &memb = aggDecl->get_members(); 98 98 99 99 if ( ! memb.empty() ) { 100 before<< endl << string( cur_indent, ' ' ) << "{" << endl;101 102 cur_indent += CodeGenerator 2::tabsize;100 output << endl << string( cur_indent, ' ' ) << "{" << endl; 101 102 cur_indent += CodeGenerator::tabsize; 103 103 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { 104 before<< string( cur_indent, ' ' );104 output << string( cur_indent, ' ' ); 105 105 (*i)->accept(*this ); 106 before<< ";" << endl;106 output << ";" << endl; 107 107 } 108 108 109 cur_indent -= CodeGenerator 2::tabsize;110 111 before<< string( cur_indent, ' ' ) << "}";112 } // if 113 } 114 115 void CodeGenerator 2::visit( StructDecl *structDecl ) {116 before<< "struct ";109 cur_indent -= CodeGenerator::tabsize; 110 111 output << string( cur_indent, ' ' ) << "}"; 112 } // if 113 } 114 115 void CodeGenerator::visit( StructDecl *structDecl ) { 116 output << "struct "; 117 117 handleAggregate( structDecl ); 118 118 } 119 119 120 void CodeGenerator 2::visit( UnionDecl *aggregateDecl ) {121 before<< "union ";120 void CodeGenerator::visit( UnionDecl *aggregateDecl ) { 121 output << "union "; 122 122 handleAggregate( aggregateDecl ); 123 123 } 124 124 125 void CodeGenerator 2::visit( EnumDecl *aggDecl ) {126 before<< "enum ";125 void CodeGenerator::visit( EnumDecl *aggDecl ) { 126 output << "enum "; 127 127 128 128 if ( aggDecl->get_name() != "" ) 129 before<< aggDecl->get_name();129 output << aggDecl->get_name(); 130 130 131 131 std::list< Declaration* > &memb = aggDecl->get_members(); 132 132 133 133 if ( ! memb.empty() ) { 134 before<< endl << "{" << endl;135 136 cur_indent += CodeGenerator 2::tabsize;134 output << endl << "{" << endl; 135 136 cur_indent += CodeGenerator::tabsize; 137 137 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) { 138 138 ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i ); 139 139 assert( obj ); 140 before<< string( cur_indent, ' ' ) << mangleName( obj );140 output << string( cur_indent, ' ' ) << mangleName( obj ); 141 141 if ( obj->get_init() ) { 142 before<< " = ";142 output << " = "; 143 143 obj->get_init()->accept(*this ); 144 144 } // if 145 before<< "," << endl;145 output << "," << endl; 146 146 } // for 147 147 148 cur_indent -= CodeGenerator 2::tabsize;149 150 before<< "}" << endl;151 } // if 152 } 153 154 void CodeGenerator 2::visit( ContextDecl *aggregateDecl ) {}155 156 void CodeGenerator 2::visit( TypedefDecl *typeDecl ) {157 before<< "typedef ";158 before<< genType( typeDecl->get_base(), typeDecl->get_name() );159 } 160 161 void CodeGenerator 2::visit( TypeDecl *typeDecl ) {148 cur_indent -= CodeGenerator::tabsize; 149 150 output << "}" << endl; 151 } // if 152 } 153 154 void CodeGenerator::visit( ContextDecl *aggregateDecl ) {} 155 156 void CodeGenerator::visit( TypedefDecl *typeDecl ) { 157 output << "typedef "; 158 output << genType( typeDecl->get_base(), typeDecl->get_name() ); 159 } 160 161 void CodeGenerator::visit( TypeDecl *typeDecl ) { 162 162 // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes, 163 163 // still to be done 164 before<< "extern unsigned long " << typeDecl->get_name();164 output << "extern unsigned long " << typeDecl->get_name(); 165 165 if ( typeDecl->get_base() ) { 166 before<< " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )";167 } // if 168 } 169 170 void CodeGenerator 2::visit( SingleInit *init ) {166 output << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )"; 167 } // if 168 } 169 170 void CodeGenerator::visit( SingleInit *init ) { 171 171 init->get_value()->accept( *this ); 172 172 } 173 173 174 void CodeGenerator 2::visit( ListInit *init ) {175 before<< "{ ";174 void CodeGenerator::visit( ListInit *init ) { 175 output << "{ "; 176 176 genCommaList( init->begin_initializers(), init->end_initializers() ); 177 before<< " }";178 } 179 180 void CodeGenerator 2::visit( Constant *constant ) {181 before<< constant->get_value() ;177 output << " }"; 178 } 179 180 void CodeGenerator::visit( Constant *constant ) { 181 output << constant->get_value() ; 182 182 } 183 183 184 184 //*** Expressions 185 void CodeGenerator 2::visit( ApplicationExpr *applicationExpr ) {185 void CodeGenerator::visit( ApplicationExpr *applicationExpr ) { 186 186 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) { 187 187 OperatorInfo opInfo; … … 214 214 assert( applicationExpr->get_args().size() == 2 ); 215 215 (*arg++)->accept( *this ); 216 before<< "[";217 (*arg)->accept( *this ); 218 before<< "]";216 output << "["; 217 (*arg)->accept( *this ); 218 output << "]"; 219 219 break; 220 220 … … 227 227 case OT_PREFIXASSIGN: 228 228 assert( applicationExpr->get_args().size() == 1 ); 229 before<< "(";230 before<< opInfo.symbol;231 (*arg)->accept( *this ); 232 before<< ")";229 output << "("; 230 output << opInfo.symbol; 231 (*arg)->accept( *this ); 232 output << ")"; 233 233 break; 234 234 … … 237 237 assert( applicationExpr->get_args().size() == 1 ); 238 238 (*arg)->accept( *this ); 239 before<< opInfo.symbol;239 output << opInfo.symbol; 240 240 break; 241 241 … … 243 243 case OT_INFIXASSIGN: 244 244 assert( applicationExpr->get_args().size() == 2 ); 245 before<< "(";245 output << "("; 246 246 (*arg++)->accept( *this ); 247 before<< opInfo.symbol;248 (*arg)->accept( *this ); 249 before<< ")";247 output << opInfo.symbol; 248 (*arg)->accept( *this ); 249 output << ")"; 250 250 break; 251 251 … … 256 256 } else { 257 257 varExpr->accept( *this ); 258 before<< "(";258 output << "("; 259 259 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); 260 before<< ")";260 output << ")"; 261 261 } // if 262 262 } else { 263 263 applicationExpr->get_function()->accept( *this ); 264 before<< "(";264 output << "("; 265 265 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() ); 266 before<< ")";267 } // if 268 } 269 270 void CodeGenerator 2::visit( UntypedExpr *untypedExpr ) {266 output << ")"; 267 } // if 268 } 269 270 void CodeGenerator::visit( UntypedExpr *untypedExpr ) { 271 271 if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) { 272 272 OperatorInfo opInfo; … … 277 277 assert( untypedExpr->get_args().size() == 2 ); 278 278 (*arg++)->accept( *this ); 279 before<< "[";280 (*arg)->accept( *this ); 281 before<< "]";279 output << "["; 280 (*arg)->accept( *this ); 281 output << "]"; 282 282 break; 283 283 … … 289 289 case OT_PREFIXASSIGN: 290 290 assert( untypedExpr->get_args().size() == 1 ); 291 before<< "(";292 before<< opInfo.symbol;293 (*arg)->accept( *this ); 294 before<< ")";291 output << "("; 292 output << opInfo.symbol; 293 (*arg)->accept( *this ); 294 output << ")"; 295 295 break; 296 296 … … 299 299 assert( untypedExpr->get_args().size() == 1 ); 300 300 (*arg)->accept( *this ); 301 before<< opInfo.symbol;301 output << opInfo.symbol; 302 302 break; 303 303 … … 305 305 case OT_INFIXASSIGN: 306 306 assert( untypedExpr->get_args().size() == 2 ); 307 before<< "(";307 output << "("; 308 308 (*arg++)->accept( *this ); 309 before<< opInfo.symbol;310 (*arg)->accept( *this ); 311 before<< ")";309 output << opInfo.symbol; 310 (*arg)->accept( *this ); 311 output << ")"; 312 312 break; 313 313 … … 318 318 } else { 319 319 nameExpr->accept( *this ); 320 before<< "(";320 output << "("; 321 321 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); 322 before<< ")";322 output << ")"; 323 323 } // if 324 324 } else { 325 325 untypedExpr->get_function()->accept( *this ); 326 before<< "(";326 output << "("; 327 327 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() ); 328 before<< ")";329 } // if 330 } 331 332 void CodeGenerator 2::visit( NameExpr *nameExpr ) {328 output << ")"; 329 } // if 330 } 331 332 void CodeGenerator::visit( NameExpr *nameExpr ) { 333 333 OperatorInfo opInfo; 334 334 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) { 335 335 assert( opInfo.type == OT_CONSTANT ); 336 before<< opInfo.symbol;337 } else { 338 before<< nameExpr->get_name();339 } // if 340 } 341 342 void CodeGenerator 2::visit( AddressExpr *addressExpr ) {343 before<< "(&";336 output << opInfo.symbol; 337 } else { 338 output << nameExpr->get_name(); 339 } // if 340 } 341 342 void CodeGenerator::visit( AddressExpr *addressExpr ) { 343 output << "(&"; 344 344 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address 345 345 if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) { 346 before<< mangleName( variableExpr->get_var() );346 output << mangleName( variableExpr->get_var() ); 347 347 } else { 348 348 addressExpr->get_arg()->accept( *this ); 349 349 } // if 350 before<< ")";351 } 352 353 void CodeGenerator 2::visit( CastExpr *castExpr ) {354 before<< "((";350 output << ")"; 351 } 352 353 void CodeGenerator::visit( CastExpr *castExpr ) { 354 output << "(("; 355 355 if ( castExpr->get_results().empty() ) { 356 before<< "void" ;357 } else { 358 before<< genType( castExpr->get_results().front(), "" );359 } // if 360 before<< ")";356 output << "void" ; 357 } else { 358 output << genType( castExpr->get_results().front(), "" ); 359 } // if 360 output << ")"; 361 361 castExpr->get_arg()->accept( *this ); 362 before<< ")";363 } 364 365 void CodeGenerator 2::visit( UntypedMemberExpr *memberExpr ) {362 output << ")"; 363 } 364 365 void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) { 366 366 assert( false ); 367 367 } 368 368 369 void CodeGenerator 2::visit( MemberExpr *memberExpr ) {369 void CodeGenerator::visit( MemberExpr *memberExpr ) { 370 370 memberExpr->get_aggregate()->accept( *this ); 371 before<< "." << mangleName( memberExpr->get_member() );372 } 373 374 void CodeGenerator 2::visit( VariableExpr *variableExpr ) {371 output << "." << mangleName( memberExpr->get_member() ); 372 } 373 374 void CodeGenerator::visit( VariableExpr *variableExpr ) { 375 375 OperatorInfo opInfo; 376 376 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) { 377 before<< opInfo.symbol;378 } else { 379 before<< mangleName( variableExpr->get_var() );380 } // if 381 } 382 383 void CodeGenerator 2::visit( ConstantExpr *constantExpr ) {377 output << opInfo.symbol; 378 } else { 379 output << mangleName( variableExpr->get_var() ); 380 } // if 381 } 382 383 void CodeGenerator::visit( ConstantExpr *constantExpr ) { 384 384 assert( constantExpr->get_constant() ); 385 385 constantExpr->get_constant()->accept( *this ); 386 386 } 387 387 388 void CodeGenerator 2::visit( SizeofExpr *sizeofExpr ) {389 before<< "sizeof(";388 void CodeGenerator::visit( SizeofExpr *sizeofExpr ) { 389 output << "sizeof("; 390 390 if ( sizeofExpr->get_isType() ) { 391 before<< genType( sizeofExpr->get_type(), "" );391 output << genType( sizeofExpr->get_type(), "" ); 392 392 } else { 393 393 sizeofExpr->get_expr()->accept( *this ); 394 394 } // if 395 before<< ")";396 } 397 398 void CodeGenerator 2::visit( LogicalExpr *logicalExpr ) {399 before<< "(";395 output << ")"; 396 } 397 398 void CodeGenerator::visit( LogicalExpr *logicalExpr ) { 399 output << "("; 400 400 logicalExpr->get_arg1()->accept( *this ); 401 401 if ( logicalExpr->get_isAnd() ) { 402 before<< " && ";403 } else { 404 before<< " || ";402 output << " && "; 403 } else { 404 output << " || "; 405 405 } // if 406 406 logicalExpr->get_arg2()->accept( *this ); 407 before<< ")";408 } 409 410 void CodeGenerator 2::visit( ConditionalExpr *conditionalExpr ) {411 before<< "(";407 output << ")"; 408 } 409 410 void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) { 411 output << "("; 412 412 conditionalExpr->get_arg1()->accept( *this ); 413 before<< " ? ";413 output << " ? "; 414 414 conditionalExpr->get_arg2()->accept( *this ); 415 before<< " : ";415 output << " : "; 416 416 conditionalExpr->get_arg3()->accept( *this ); 417 before<< ")";418 } 419 420 void CodeGenerator 2::visit( CommaExpr *commaExpr ) {421 before<< "(";417 output << ")"; 418 } 419 420 void CodeGenerator::visit( CommaExpr *commaExpr ) { 421 output << "("; 422 422 commaExpr->get_arg1()->accept( *this ); 423 before<< " , ";423 output << " , "; 424 424 commaExpr->get_arg2()->accept( *this ); 425 before<< ")";426 } 427 428 void CodeGenerator 2::visit( TupleExpr *tupleExpr ) {}429 430 void CodeGenerator 2::visit( TypeExpr *typeExpr ) {}425 output << ")"; 426 } 427 428 void CodeGenerator::visit( TupleExpr *tupleExpr ) {} 429 430 void CodeGenerator::visit( TypeExpr *typeExpr ) {} 431 431 432 432 433 433 //*** Statements 434 void CodeGenerator 2::visit( CompoundStmt *compoundStmt ) {434 void CodeGenerator::visit( CompoundStmt *compoundStmt ) { 435 435 std::list<Statement*> ks = compoundStmt->get_kids(); 436 436 437 before<< endl << string( cur_indent, ' ' ) << "{" << endl;438 439 cur_indent += CodeGenerator 2::tabsize;437 output << endl << string( cur_indent, ' ' ) << "{" << endl; 438 439 cur_indent += CodeGenerator::tabsize; 440 440 441 441 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++) { 442 before<< string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() ) ;442 output << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() ) ; 443 443 (*i)->accept(*this ); 444 shift_left(); 445 before << endl; 444 output << endl; 446 445 } 447 cur_indent -= CodeGenerator2::tabsize; 448 449 before << string( cur_indent, ' ' ) << "}" << endl; 450 } 451 452 void CodeGenerator2::visit( ExprStmt *exprStmt ) { 453 if ( exprStmt != 0 ) { 454 exprStmt->get_expr()->accept( *this ); 455 shift_left(); 456 before << ";" ; 457 } // if 458 } 459 460 void CodeGenerator2::visit( IfStmt *ifStmt ) { 461 before << "if ("; 446 cur_indent -= CodeGenerator::tabsize; 447 448 output << string( cur_indent, ' ' ) << "}" << endl; 449 } 450 451 void CodeGenerator::visit( ExprStmt *exprStmt ) { 452 // I don't see why this check is necessary. 453 // If this starts to cause problems then put it back in, 454 // with an explanation 455 assert( exprStmt ); 456 457 // if ( exprStmt != 0 ) { 458 exprStmt->get_expr()->accept( *this ); 459 output << ";" ; 460 // } // if 461 } 462 463 void CodeGenerator::visit( IfStmt *ifStmt ) { 464 output << "if ("; 462 465 ifStmt->get_condition()->accept(*this ); 463 after += ")\n"; 464 shift_left(); 465 466 cur_indent += CodeGenerator2::tabsize; 467 before << string( cur_indent, ' ' ); 466 output << ")\n"; 467 468 cur_indent += CodeGenerator::tabsize; 469 output << string( cur_indent, ' ' ); 468 470 ifStmt->get_thenPart()->accept(*this ); 469 cur_indent -= CodeGenerator 2::tabsize;470 shift_left(); before<< endl;471 cur_indent -= CodeGenerator::tabsize; 472 output << endl; 471 473 472 474 if ( ifStmt->get_elsePart() != 0) { 473 before<< string( cur_indent, ' ' ) << " else " << endl ;474 475 cur_indent += CodeGenerator 2::tabsize;475 output << string( cur_indent, ' ' ) << " else " << endl ; 476 477 cur_indent += CodeGenerator::tabsize; 476 478 ifStmt->get_elsePart()->accept(*this ); 477 cur_indent -= CodeGenerator 2::tabsize;478 } // if 479 } 480 481 void CodeGenerator 2::visit( SwitchStmt *switchStmt ) {482 // before << /* "\r" << */ string( cur_indent, ' ' ) << CodeGenerator2::printLabels( switchStmt->get_labels() )483 before<< "switch (" ;479 cur_indent -= CodeGenerator::tabsize; 480 } // if 481 } 482 483 void CodeGenerator::visit( SwitchStmt *switchStmt ) { 484 //output << /* "\r" << */ string( cur_indent, ' ' ) << CodeGenerator::printLabels( switchStmt->get_labels() ) 485 output << "switch (" ; 484 486 switchStmt->get_condition()->accept(*this ); 485 after += ")\n"; 486 shift_left(); 487 488 before << string( cur_indent, ' ' ) << "{" << std::endl; 489 cur_indent += CodeGenerator2::tabsize; 490 491 std::list< Statement * > stmts = switchStmt->get_branches(); 492 bool lastBreak = false; 493 494 // horrible, horrible hack 495 if ( dynamic_cast<BranchStmt *>( stmts.back() ) != 0 ) { 496 lastBreak = true; 497 stmts.pop_back(); 498 } // if 499 acceptAll( stmts, *this ); 500 if ( lastBreak ) { 501 Statement *st = switchStmt->get_branches().back(); 502 before << CodeGenerator2::printLabels( st->get_labels()); 503 st->accept( *this ); 504 } // if 505 506 cur_indent -= CodeGenerator2::tabsize; 507 508 before << /* "\r" << */ string( cur_indent, ' ' ) << "}" << endl ; 509 } 510 511 void CodeGenerator2::visit( CaseStmt *caseStmt ) { 512 before << string( cur_indent, ' ' ); 487 output << ")\n"; 488 489 output << string( cur_indent, ' ' ) << "{" << std::endl; 490 cur_indent += CodeGenerator::tabsize; 491 492 acceptAll( switchStmt->get_branches(), *this ); 493 494 cur_indent -= CodeGenerator::tabsize; 495 496 output << /* "\r" << */ string( cur_indent, ' ' ) << "}" << endl; 497 } 498 499 void CodeGenerator::visit( CaseStmt *caseStmt ) { 500 output << string( cur_indent, ' ' ); 513 501 if ( caseStmt->isDefault()) 514 before << "default" ;502 output << "default" ; 515 503 else { 516 before<< "case " ;504 output << "case " ; 517 505 caseStmt->get_condition()->accept(*this ); 518 506 } // if 519 after += ":\n"; 520 shift_left(); 521 507 output << ":\n"; 508 522 509 std::list<Statement *> sts = caseStmt->get_statements(); 523 510 524 cur_indent += CodeGenerator 2::tabsize;511 cur_indent += CodeGenerator::tabsize; 525 512 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) { 526 before<< /* "\r" << */ string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() ) ;513 output << /* "\r" << */ string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() ) ; 527 514 (*i)->accept(*this ); 528 shift_left(); 529 before << ";" << endl; 515 output << endl; 530 516 } 531 cur_indent -= CodeGenerator2::tabsize; 532 } 533 534 void CodeGenerator2::visit( BranchStmt *branchStmt ) { 517 cur_indent -= CodeGenerator::tabsize; 518 } 519 520 void CodeGenerator::visit( BranchStmt *branchStmt ) { 521 output << "\r" << string( cur_indent, ' ' ); 522 output << CodeGenerator::printLabels( branchStmt->get_labels()); 523 535 524 switch ( branchStmt->get_type()) { 536 525 case BranchStmt::Goto: 537 526 if ( ! branchStmt->get_target().empty() ) 538 before<< "goto " << branchStmt->get_target();527 output << "goto " << branchStmt->get_target(); 539 528 else { 540 529 if ( branchStmt->get_computedTarget() != 0 ) { 541 before<< "goto *";530 output << "goto *"; 542 531 branchStmt->get_computedTarget()->accept( *this ); 543 532 } // if … … 545 534 break; 546 535 case BranchStmt::Break: 547 before<< "break";536 output << "break"; 548 537 break; 549 538 case BranchStmt::Continue: 550 before<< "continue";539 output << "continue"; 551 540 break; 552 541 } 553 before << ";";554 } 555 556 557 void CodeGenerator 2::visit( ReturnStmt *returnStmt ) {558 before<< "return ";542 output << ";" << endl; 543 } 544 545 546 void CodeGenerator::visit( ReturnStmt *returnStmt ) { 547 output << "return "; 559 548 560 549 // xxx -- check for null expression; … … 562 551 returnStmt->get_expr()->accept( *this ); 563 552 } // if 564 after +=";";565 } 566 567 void CodeGenerator 2::visit( WhileStmt *whileStmt ) {553 output << ";"; 554 } 555 556 void CodeGenerator::visit( WhileStmt *whileStmt ) { 568 557 if ( whileStmt->get_isDoWhile() ) 569 before<< "do" ;558 output << "do" ; 570 559 else { 571 before<< "while (" ;560 output << "while (" ; 572 561 whileStmt->get_condition()->accept(*this ); 573 after +=")";574 } // if 575 after += "{\n";576 shift_left(); 577 562 output << ")"; 563 } // if 564 output << "\n"; 565 566 output << string( cur_indent, ' ' ) << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() ); 578 567 whileStmt->get_body()->accept( *this ); 579 568 580 before << /* "\r" << */ string( cur_indent, ' ' ) << "}";569 output << /* "\r" << */ string( cur_indent, ' ' ); 581 570 582 571 if ( whileStmt->get_isDoWhile() ) { 583 before<< " while (" ;572 output << " while (" ; 584 573 whileStmt->get_condition()->accept(*this ); 585 after +=");";586 } // if 587 588 after +="\n";589 } 590 591 void CodeGenerator 2::visit( ForStmt *forStmt ) {592 before<< "for (";574 output << ");"; 575 } // if 576 577 output << "\n"; 578 } 579 580 void CodeGenerator::visit( ForStmt *forStmt ) { 581 output << "for ("; 593 582 594 583 if ( forStmt->get_initialization() != 0 ) 595 584 forStmt->get_initialization()->accept( *this ); 596 585 else 597 before << ";"; 598 shift_left(); 599 586 output << ";"; 587 600 588 if ( forStmt->get_condition() != 0 ) 601 589 forStmt->get_condition()->accept( *this ); 602 shift_left(); before<< ";";590 output << ";"; 603 591 604 592 if ( forStmt->get_increment() != 0 ) 605 593 forStmt->get_increment()->accept( *this ); 606 shift_left(); before<< ")" << endl;594 output << ")" << endl; 607 595 608 596 if ( forStmt->get_body() != 0 ) { 609 cur_indent += CodeGenerator2::tabsize; 610 before << string( cur_indent, ' ' ) << CodeGenerator2::printLabels( forStmt->get_body()->get_labels() ); 597 output << string( cur_indent, ' ' ) << CodeGenerator::printLabels( forStmt->get_body()->get_labels() ); 611 598 forStmt->get_body()->accept( *this ); 612 cur_indent -= CodeGenerator2::tabsize; 613 } // if 614 } 615 616 void CodeGenerator2::visit( NullStmt *nullStmt ) { 617 //before << /* "\r" << */ string( cur_indent, ' ' ) << CodeGenerator2::printLabels( nullStmt->get_labels() ); 618 before << "/* null statement */ ;"; 619 } 620 621 void CodeGenerator2::visit( DeclStmt *declStmt ) { 599 } // if 600 } 601 602 void CodeGenerator::visit( NullStmt *nullStmt ) { 603 //output << /* "\r" << */ string( cur_indent, ' ' ) << CodeGenerator::printLabels( nullStmt->get_labels() ); 604 output << "/* null statement */ ;"; 605 } 606 607 void CodeGenerator::visit( DeclStmt *declStmt ) { 622 608 declStmt->get_decl()->accept( *this ); 623 609 624 610 if ( doSemicolon( declStmt->get_decl() ) ) { 625 after += ";"; 626 } // if 627 shift_left(); 628 } 629 630 std::string CodeGenerator2::printLabels( std::list< Label > &l ) { 611 output << ";"; 612 } // if 613 } 614 615 std::string CodeGenerator::printLabels( std::list< Label > &l ) { 631 616 std::string str( "" ); 632 l.unique(); 617 l.unique(); // assumes a sorted list. Why not use set? 633 618 634 619 for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ ) … … 638 623 } 639 624 640 void CodeGenerator2::shift_left() { 641 before << after; 642 after = ""; 643 } 644 645 void CodeGenerator2::handleStorageClass( Declaration *decl ) { 625 void CodeGenerator::handleStorageClass( Declaration *decl ) { 646 626 switch ( decl->get_storageClass() ) { 647 627 case Declaration::NoStorageClass: 648 628 break; 649 629 case Declaration::Extern: 650 before<< "extern ";630 output << "extern "; 651 631 break; 652 632 case Declaration::Static: 653 before<< "static ";633 output << "static "; 654 634 break; 655 635 case Declaration::Auto: … … 657 637 break; 658 638 case Declaration::Register: 659 before<< "register ";639 output << "register "; 660 640 break; 661 641 case Declaration::Inline: -
src/CodeGen/CodeGenerator.h
r6db50d5 r6c4ff37 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // CodeGenerator 2.h --7 // CodeGenerator.h -- 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 : Mon May 18 23:35:37201513 // Update Count : 211 // Last Modified By : Rob Schluntz 12 // Last Modified On : Tue Jun 02 11:45:20 2015 13 // Update Count : 9 14 14 // 15 15 … … 25 25 26 26 namespace CodeGen { 27 class CodeGenerator 2: public Visitor {27 class CodeGenerator : public Visitor { 28 28 public: 29 29 static int tabsize; 30 30 31 CodeGenerator 2( std::ostream &os );32 CodeGenerator 2( std::ostream &os, std::string, int indent = 0, bool infun = false );33 CodeGenerator 2( std::ostream &os, char *, int indent = 0, bool infun = false );31 CodeGenerator( std::ostream &os ); 32 CodeGenerator( std::ostream &os, std::string, int indent = 0, bool infun = false ); 33 CodeGenerator( std::ostream &os, char *, int indent = 0, bool infun = false ); 34 34 35 CodeGenerator 2( CodeGenerator2& );35 CodeGenerator( CodeGenerator & ); 36 36 37 37 //*** Declaration … … 82 82 virtual void visit( DeclStmt * ); 83 83 84 std::string get_string( void );85 void add_string_left( std::string s ) { before << s; }86 void shift_left();87 84 template< class Iterator > void genCommaList( Iterator begin, Iterator end ); 88 85 private: 89 86 int cur_indent; 90 87 bool insideFunction; 91 std::ostream &before; 92 std::string after; 88 std::ostream &output; 93 89 94 90 static std::string printLabels ( std::list < Label > & ); … … 100 96 101 97 template< class Iterator > 102 void CodeGenerator 2::genCommaList( Iterator begin, Iterator end ) {98 void CodeGenerator::genCommaList( Iterator begin, Iterator end ) { 103 99 if ( begin == end ) return; 104 100 … … 106 102 (*begin++)->accept( *this ); 107 103 if ( begin == end ) return; 108 before<< ", ";104 output << ", "; 109 105 } // for 110 106 } -
src/CodeGen/GenType.cc
r6db50d5 r6c4ff37 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 : Mon May 18 23:38:22201513 // Update Count : 211 // Last Modified By : Rob Schluntz 12 // Last Modified On : Tue Jun 02 11:07:25 2015 13 // Update Count : 3 14 14 // 15 15 … … 18 18 19 19 #include "GenType.h" 20 #include "CodeGenerator 2.h"20 #include "CodeGenerator.h" 21 21 #include "SynTree/Visitor.h" 22 22 #include "SynTree/Type.h" … … 97 97 } // if 98 98 if ( dimension != 0 ) { 99 CodeGenerator 2cg( os );99 CodeGenerator cg( os ); 100 100 dimension->accept( cg ); 101 101 } // if … … 148 148 } // if 149 149 } else { 150 CodeGenerator 2cg( os );150 CodeGenerator cg( os ); 151 151 os << "(" ; 152 152 -
src/CodeGen/Generate.cc
r6db50d5 r6c4ff37 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 : Mon May 18 23:39:24 201513 // Update Count : 111 // Last Modified By : Rob Schluntz 12 // Last Modified On : Tue Jun 02 11:08:44 2015 13 // Update Count : 4 14 14 // 15 15 … … 22 22 #include "SynTree/Declaration.h" 23 23 24 #include "CodeGenerator 2.h"24 #include "CodeGenerator.h" 25 25 26 26 using namespace std; … … 28 28 namespace CodeGen { 29 29 void generate( std::list< Declaration* > translationUnit, std::ostream &os, bool doIntrinsics ) { 30 CodeGen::CodeGenerator 2cgv( os );30 CodeGen::CodeGenerator cgv( os ); 31 31 32 32 for ( std::list<Declaration *>::iterator i = translationUnit.begin(); i != translationUnit.end(); i++ ) { 33 33 if ( LinkageSpec::isGeneratable( (*i)->get_linkage() ) && (doIntrinsics || ! LinkageSpec::isBuiltin( (*i)->get_linkage() ) ) ) { 34 34 (*i)->accept(cgv); 35 cgv.shift_left();36 35 if ( doSemicolon( *i ) ) { 37 36 os << ";"; -
src/CodeGen/module.mk
r6db50d5 r6c4ff37 1 1 SRC += CodeGen/Generate.cc \ 2 CodeGen/CodeGenerator 2.cc \2 CodeGen/CodeGenerator.cc \ 3 3 CodeGen/GenType.cc \ 4 4 CodeGen/FixNames.cc \
Note: See TracChangeset
for help on using the changeset viewer.