source: src/CodeGen/CodeGenerator.cc@ de62360d

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new string with_gc
Last change on this file since de62360d was de62360d, checked in by Peter A. Buhr <pabuhr@…>, 11 years ago

fix computed goto, fixed -std=, implicit typedefs for enum and aggregates, add _Noreturn _Thread_local

  • Property mode set to 100644
File size: 17.7 KB
RevLine 
[51587aa]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[6c4ff37]7// CodeGenerator.cc --
[51587aa]8//
9// Author : Richard C. Bilson
10// Created On : Mon May 18 07:44:20 2015
[68cd1ce]11// Last Modified By : Peter A. Buhr
[de62360d]12// Last Modified On : Tue Jun 23 16:16:57 2015
13// Update Count : 133
[51587aa]14//
15
[51b73452]16#include <algorithm>
17#include <iostream>
18#include <cassert>
19#include <list>
20
[68cd1ce]21#include "Parser/ParseNode.h"
22
[51b73452]23#include "SynTree/Type.h"
24#include "SynTree/Expression.h"
25#include "SynTree/Initializer.h"
[68cd1ce]26#include "SynTree/Statement.h"
[51b73452]27
28#include "utility.h"
29#include "UnimplementedError.h"
30
[a61fea9a]31#include "CodeGenerator.h"
[51b73452]32#include "OperatorTable.h"
33#include "GenType.h"
34
35using namespace std;
36
37namespace CodeGen {
[6c4ff37]38 int CodeGenerator::tabsize = 4;
[51587aa]39
[2b6c1e0]40 // the kinds of statements that would ideally be separated by more whitespace
41 bool wantSpacing( Statement * stmt) {
42 return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) ||
43 dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * > ( stmt ) || dynamic_cast< SwitchStmt *>( stmt );
44 }
45
[6c4ff37]46 CodeGenerator::CodeGenerator( std::ostream &os ) : cur_indent( 0 ), insideFunction( false ), output( os ) { }
[51587aa]47
[6c4ff37]48 CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indent, bool infunp )
49 : cur_indent( indent ), insideFunction( infunp ), output( os ) {
50 //output << std::string( init );
[51587aa]51 }
52
[6c4ff37]53 CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indent, bool infunp )
54 : cur_indent( indent ), insideFunction( infunp ), output( os ) {
55 //output << std::string( init );
[51587aa]56 }
57
58 string mangleName( DeclarationWithType *decl ) {
59 if ( decl->get_mangleName() != "" ) {
60 return decl->get_mangleName();
61 } else {
62 return decl->get_name();
63 } // if
64 }
[51b73452]65
[51587aa]66 //*** Declarations
[6c4ff37]67 void CodeGenerator::visit( FunctionDecl *functionDecl ) {
[51587aa]68 handleStorageClass( functionDecl );
[f38c8d9]69 if ( functionDecl->get_isInline() ) {
[6c4ff37]70 output << "inline ";
[f38c8d9]71 } // if
[de62360d]72 if ( functionDecl->get_isNoreturn() ) {
73 output << "_Noreturn ";
74 } // if
[6c4ff37]75 output << genType( functionDecl->get_functionType(), mangleName( functionDecl ) );
[51587aa]76
77 // how to get this to the Functype?
78 std::list< Declaration * > olds = functionDecl->get_oldDecls();
79 if ( ! olds.empty() ) {
[6c4ff37]80 output << " /* function has old declaration */";
[51587aa]81 } // if
82
83 // acceptAll( functionDecl->get_oldDecls(), *this );
84 if ( functionDecl->get_statements() ) {
85 functionDecl->get_statements()->accept(*this );
86 } // if
87 }
88
[6c4ff37]89 void CodeGenerator::visit( ObjectDecl *objectDecl ) {
[51587aa]90 handleStorageClass( objectDecl );
[6c4ff37]91 output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
[51587aa]92
93 if ( objectDecl->get_init() ) {
[6c4ff37]94 output << " = ";
[51587aa]95 objectDecl->get_init()->accept( *this );
96 } // if
97 if ( objectDecl->get_bitfieldWidth() ) {
[6c4ff37]98 output << ":";
[51587aa]99 objectDecl->get_bitfieldWidth()->accept( *this );
100 } // if
101 }
102
[6c4ff37]103 void CodeGenerator::handleAggregate( AggregateDecl *aggDecl ) {
[51587aa]104 if ( aggDecl->get_name() != "" )
[6c4ff37]105 output << aggDecl->get_name();
[51587aa]106
107 std::list< Declaration * > &memb = aggDecl->get_members();
108
109 if ( ! memb.empty() ) {
[6c4ff37]110 output << endl << string( cur_indent, ' ' ) << "{" << endl;
[51587aa]111
[6c4ff37]112 cur_indent += CodeGenerator::tabsize;
[51587aa]113 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
[6c4ff37]114 output << string( cur_indent, ' ' );
[51587aa]115 (*i)->accept(*this );
[6c4ff37]116 output << ";" << endl;
[51587aa]117 }
118
[6c4ff37]119 cur_indent -= CodeGenerator::tabsize;
[51587aa]120
[6c4ff37]121 output << string( cur_indent, ' ' ) << "}";
[17cd4eb]122 } // if
[51587aa]123 }
[17cd4eb]124
[6c4ff37]125 void CodeGenerator::visit( StructDecl *structDecl ) {
126 output << "struct ";
[51587aa]127 handleAggregate( structDecl );
128 }
[17cd4eb]129
[6c4ff37]130 void CodeGenerator::visit( UnionDecl *aggregateDecl ) {
131 output << "union ";
[51587aa]132 handleAggregate( aggregateDecl );
133 }
[51b73452]134
[6c4ff37]135 void CodeGenerator::visit( EnumDecl *aggDecl ) {
136 output << "enum ";
[51587aa]137
138 if ( aggDecl->get_name() != "" )
[6c4ff37]139 output << aggDecl->get_name();
[51587aa]140
141 std::list< Declaration* > &memb = aggDecl->get_members();
142
143 if ( ! memb.empty() ) {
[6c4ff37]144 output << endl << "{" << endl;
[51587aa]145
[6c4ff37]146 cur_indent += CodeGenerator::tabsize;
[51587aa]147 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
148 ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
149 assert( obj );
[6c4ff37]150 output << string( cur_indent, ' ' ) << mangleName( obj );
[51587aa]151 if ( obj->get_init() ) {
[6c4ff37]152 output << " = ";
[51587aa]153 obj->get_init()->accept(*this );
154 } // if
[6c4ff37]155 output << "," << endl;
[51587aa]156 } // for
157
[6c4ff37]158 cur_indent -= CodeGenerator::tabsize;
[51587aa]159
[6c4ff37]160 output << "}" << endl;
[51587aa]161 } // if
162 }
[51b73452]163
[6c4ff37]164 void CodeGenerator::visit( ContextDecl *aggregateDecl ) {}
[51587aa]165
[6c4ff37]166 void CodeGenerator::visit( TypedefDecl *typeDecl ) {
167 output << "typedef ";
168 output << genType( typeDecl->get_base(), typeDecl->get_name() );
[51587aa]169 }
[51b73452]170
[6c4ff37]171 void CodeGenerator::visit( TypeDecl *typeDecl ) {
[51587aa]172 // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
173 // still to be done
[6c4ff37]174 output << "extern unsigned long " << typeDecl->get_name();
[51587aa]175 if ( typeDecl->get_base() ) {
[6c4ff37]176 output << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )";
[51587aa]177 } // if
178 }
179
[6c4ff37]180 void CodeGenerator::visit( SingleInit *init ) {
[51587aa]181 init->get_value()->accept( *this );
182 }
183
[6c4ff37]184 void CodeGenerator::visit( ListInit *init ) {
185 output << "{ ";
[51587aa]186 genCommaList( init->begin_initializers(), init->end_initializers() );
[6c4ff37]187 output << " }";
[51587aa]188 }
189
[6c4ff37]190 void CodeGenerator::visit( Constant *constant ) {
191 output << constant->get_value() ;
[51587aa]192 }
193
194 //*** Expressions
[6c4ff37]195 void CodeGenerator::visit( ApplicationExpr *applicationExpr ) {
[51587aa]196 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
197 OperatorInfo opInfo;
198 if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
199 std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
200 switch ( opInfo.type ) {
201 case OT_PREFIXASSIGN:
202 case OT_POSTFIXASSIGN:
203 case OT_INFIXASSIGN:
204 {
205 assert( arg != applicationExpr->get_args().end() );
206 if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
207
208 *arg = addrExpr->get_arg();
209 } else {
210 UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
211 newExpr->get_args().push_back( *arg );
212 *arg = newExpr;
213 } // if
214 break;
215 }
216
217 default:
218 // do nothing
219 ;
220 }
221
222 switch ( opInfo.type ) {
223 case OT_INDEX:
224 assert( applicationExpr->get_args().size() == 2 );
225 (*arg++)->accept( *this );
[6c4ff37]226 output << "[";
[51587aa]227 (*arg)->accept( *this );
[6c4ff37]228 output << "]";
[51587aa]229 break;
230
231 case OT_CALL:
232 // there are no intrinsic definitions of the function call operator
233 assert( false );
234 break;
235
236 case OT_PREFIX:
237 case OT_PREFIXASSIGN:
238 assert( applicationExpr->get_args().size() == 1 );
[6c4ff37]239 output << "(";
240 output << opInfo.symbol;
[51587aa]241 (*arg)->accept( *this );
[6c4ff37]242 output << ")";
[51587aa]243 break;
244
245 case OT_POSTFIX:
246 case OT_POSTFIXASSIGN:
247 assert( applicationExpr->get_args().size() == 1 );
248 (*arg)->accept( *this );
[6c4ff37]249 output << opInfo.symbol;
[51587aa]250 break;
251
252 case OT_INFIX:
253 case OT_INFIXASSIGN:
254 assert( applicationExpr->get_args().size() == 2 );
[6c4ff37]255 output << "(";
[51587aa]256 (*arg++)->accept( *this );
[6c4ff37]257 output << opInfo.symbol;
[51587aa]258 (*arg)->accept( *this );
[6c4ff37]259 output << ")";
[51587aa]260 break;
261
262 case OT_CONSTANT:
263 // there are no intrinsic definitions of 0 or 1 as functions
264 assert( false );
265 }
[17cd4eb]266 } else {
[51587aa]267 varExpr->accept( *this );
[6c4ff37]268 output << "(";
[51587aa]269 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
[6c4ff37]270 output << ")";
[17cd4eb]271 } // if
[51587aa]272 } else {
273 applicationExpr->get_function()->accept( *this );
[6c4ff37]274 output << "(";
[51587aa]275 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
[6c4ff37]276 output << ")";
[51587aa]277 } // if
278 }
[51b73452]279
[6c4ff37]280 void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
[51587aa]281 if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
282 OperatorInfo opInfo;
283 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
284 std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
285 switch ( opInfo.type ) {
286 case OT_INDEX:
287 assert( untypedExpr->get_args().size() == 2 );
288 (*arg++)->accept( *this );
[6c4ff37]289 output << "[";
[51587aa]290 (*arg)->accept( *this );
[6c4ff37]291 output << "]";
[51587aa]292 break;
293
294 case OT_CALL:
295 assert( false );
296 break;
297
298 case OT_PREFIX:
299 case OT_PREFIXASSIGN:
[de62360d]300 case OT_LABELADDRESS:
[51587aa]301 assert( untypedExpr->get_args().size() == 1 );
[6c4ff37]302 output << "(";
303 output << opInfo.symbol;
[51587aa]304 (*arg)->accept( *this );
[6c4ff37]305 output << ")";
[51587aa]306 break;
307
308 case OT_POSTFIX:
309 case OT_POSTFIXASSIGN:
310 assert( untypedExpr->get_args().size() == 1 );
311 (*arg)->accept( *this );
[6c4ff37]312 output << opInfo.symbol;
[51587aa]313 break;
[51b73452]314
[51587aa]315 case OT_INFIX:
316 case OT_INFIXASSIGN:
317 assert( untypedExpr->get_args().size() == 2 );
[6c4ff37]318 output << "(";
[51587aa]319 (*arg++)->accept( *this );
[6c4ff37]320 output << opInfo.symbol;
[51587aa]321 (*arg)->accept( *this );
[6c4ff37]322 output << ")";
[51587aa]323 break;
[de62360d]324
[51587aa]325 case OT_CONSTANT:
326 // there are no intrinsic definitions of 0 or 1 as functions
327 assert( false );
328 }
329 } else {
330 nameExpr->accept( *this );
[6c4ff37]331 output << "(";
[51587aa]332 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
[6c4ff37]333 output << ")";
[51587aa]334 } // if
335 } else {
336 untypedExpr->get_function()->accept( *this );
[6c4ff37]337 output << "(";
[51587aa]338 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
[6c4ff37]339 output << ")";
[51587aa]340 } // if
341 }
[51b73452]342
[6c4ff37]343 void CodeGenerator::visit( NameExpr *nameExpr ) {
[51587aa]344 OperatorInfo opInfo;
345 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
346 assert( opInfo.type == OT_CONSTANT );
[6c4ff37]347 output << opInfo.symbol;
[51587aa]348 } else {
[6c4ff37]349 output << nameExpr->get_name();
[51587aa]350 } // if
351 }
[51b73452]352
[6c4ff37]353 void CodeGenerator::visit( AddressExpr *addressExpr ) {
354 output << "(&";
[51587aa]355 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
356 if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
[6c4ff37]357 output << mangleName( variableExpr->get_var() );
[51587aa]358 } else {
359 addressExpr->get_arg()->accept( *this );
360 } // if
[6c4ff37]361 output << ")";
[51587aa]362 }
363
[6c4ff37]364 void CodeGenerator::visit( CastExpr *castExpr ) {
365 output << "((";
[51587aa]366 if ( castExpr->get_results().empty() ) {
[6c4ff37]367 output << "void" ;
[51587aa]368 } else {
[6c4ff37]369 output << genType( castExpr->get_results().front(), "" );
[51587aa]370 } // if
[6c4ff37]371 output << ")";
[51587aa]372 castExpr->get_arg()->accept( *this );
[6c4ff37]373 output << ")";
[51587aa]374 }
[51b73452]375
[6c4ff37]376 void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
[51587aa]377 assert( false );
378 }
[51b73452]379
[6c4ff37]380 void CodeGenerator::visit( MemberExpr *memberExpr ) {
[51587aa]381 memberExpr->get_aggregate()->accept( *this );
[6c4ff37]382 output << "." << mangleName( memberExpr->get_member() );
[51587aa]383 }
[51b73452]384
[6c4ff37]385 void CodeGenerator::visit( VariableExpr *variableExpr ) {
[51587aa]386 OperatorInfo opInfo;
387 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
[6c4ff37]388 output << opInfo.symbol;
[51587aa]389 } else {
[6c4ff37]390 output << mangleName( variableExpr->get_var() );
[51587aa]391 } // if
392 }
[51b73452]393
[6c4ff37]394 void CodeGenerator::visit( ConstantExpr *constantExpr ) {
[51587aa]395 assert( constantExpr->get_constant() );
396 constantExpr->get_constant()->accept( *this );
397 }
[51b73452]398
[6c4ff37]399 void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
400 output << "sizeof(";
[51587aa]401 if ( sizeofExpr->get_isType() ) {
[6c4ff37]402 output << genType( sizeofExpr->get_type(), "" );
[51587aa]403 } else {
404 sizeofExpr->get_expr()->accept( *this );
405 } // if
[6c4ff37]406 output << ")";
[51587aa]407 }
[51b73452]408
[6c4ff37]409 void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
410 output << "(";
[51587aa]411 logicalExpr->get_arg1()->accept( *this );
412 if ( logicalExpr->get_isAnd() ) {
[6c4ff37]413 output << " && ";
[51587aa]414 } else {
[6c4ff37]415 output << " || ";
[51587aa]416 } // if
417 logicalExpr->get_arg2()->accept( *this );
[6c4ff37]418 output << ")";
[51587aa]419 }
[51b73452]420
[6c4ff37]421 void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
422 output << "(";
[51587aa]423 conditionalExpr->get_arg1()->accept( *this );
[6c4ff37]424 output << " ? ";
[51587aa]425 conditionalExpr->get_arg2()->accept( *this );
[6c4ff37]426 output << " : ";
[51587aa]427 conditionalExpr->get_arg3()->accept( *this );
[6c4ff37]428 output << ")";
[51587aa]429 }
[51b73452]430
[6c4ff37]431 void CodeGenerator::visit( CommaExpr *commaExpr ) {
432 output << "(";
[51587aa]433 commaExpr->get_arg1()->accept( *this );
[6c4ff37]434 output << " , ";
[51587aa]435 commaExpr->get_arg2()->accept( *this );
[6c4ff37]436 output << ")";
[51587aa]437 }
[51b73452]438
[6c4ff37]439 void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
[51b73452]440
[6c4ff37]441 void CodeGenerator::visit( TypeExpr *typeExpr ) {}
[2b6c1e0]442
[51587aa]443 //*** Statements
[6c4ff37]444 void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
[51587aa]445 std::list<Statement*> ks = compoundStmt->get_kids();
[2b6c1e0]446 output << "{" << endl;
[51587aa]447
[2b6c1e0]448 cur_indent += CodeGenerator::tabsize;
[51587aa]449
450 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++) {
[2b6c1e0]451 output << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() );
[51587aa]452 (*i)->accept(*this );
[2b6c1e0]453
[6c4ff37]454 output << endl;
[2b6c1e0]455 if ( wantSpacing( *i ) ) {
456 output << endl;
457 }
[51587aa]458 }
[6c4ff37]459 cur_indent -= CodeGenerator::tabsize;
[51587aa]460
[2b6c1e0]461 output << string( cur_indent, ' ' ) << "}";
[51587aa]462 }
463
[6c4ff37]464 void CodeGenerator::visit( ExprStmt *exprStmt ) {
465 // I don't see why this check is necessary.
466 // If this starts to cause problems then put it back in,
467 // with an explanation
468 assert( exprStmt );
469
470 // if ( exprStmt != 0 ) {
471 exprStmt->get_expr()->accept( *this );
472 output << ";" ;
473 // } // if
[51587aa]474 }
475
[6c4ff37]476 void CodeGenerator::visit( IfStmt *ifStmt ) {
477 output << "if (";
[51587aa]478 ifStmt->get_condition()->accept(*this );
[2b6c1e0]479 output << ") ";
[51587aa]480
481 ifStmt->get_thenPart()->accept(*this );
482
483 if ( ifStmt->get_elsePart() != 0) {
[2b6c1e0]484 output << " else ";
[51587aa]485 ifStmt->get_elsePart()->accept(*this );
486 } // if
487 }
488
[6c4ff37]489 void CodeGenerator::visit( SwitchStmt *switchStmt ) {
490 output << "switch (" ;
[51587aa]491 switchStmt->get_condition()->accept(*this );
[2b6c1e0]492 output << ") ";
[6c4ff37]493
[2b6c1e0]494 output << "{" << std::endl;
[6c4ff37]495 cur_indent += CodeGenerator::tabsize;
[51587aa]496
[6c4ff37]497 acceptAll( switchStmt->get_branches(), *this );
[51587aa]498
[6c4ff37]499 cur_indent -= CodeGenerator::tabsize;
[51587aa]500
[2b6c1e0]501 output << string( cur_indent, ' ' ) << "}";
[51587aa]502 }
503
[6c4ff37]504 void CodeGenerator::visit( CaseStmt *caseStmt ) {
505 output << string( cur_indent, ' ' );
[eb3261f]506 if ( caseStmt->isDefault()) {
[2b6c1e0]507 output << "default";
[eb3261f]508 } else {
[2b6c1e0]509 output << "case ";
[51587aa]510 caseStmt->get_condition()->accept(*this );
[17cd4eb]511 } // if
[6c4ff37]512 output << ":\n";
513
[51587aa]514 std::list<Statement *> sts = caseStmt->get_statements();
515
[6c4ff37]516 cur_indent += CodeGenerator::tabsize;
[51587aa]517 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
[eb3261f]518 output << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() ) ;
[51587aa]519 (*i)->accept(*this );
[6c4ff37]520 output << endl;
[51587aa]521 }
[6c4ff37]522 cur_indent -= CodeGenerator::tabsize;
[51587aa]523 }
524
[6c4ff37]525 void CodeGenerator::visit( BranchStmt *branchStmt ) {
[51587aa]526 switch ( branchStmt->get_type()) {
527 case BranchStmt::Goto:
528 if ( ! branchStmt->get_target().empty() )
[6c4ff37]529 output << "goto " << branchStmt->get_target();
[51587aa]530 else {
531 if ( branchStmt->get_computedTarget() != 0 ) {
[6c4ff37]532 output << "goto *";
[51587aa]533 branchStmt->get_computedTarget()->accept( *this );
534 } // if
535 } // if
536 break;
537 case BranchStmt::Break:
[6c4ff37]538 output << "break";
[51587aa]539 break;
540 case BranchStmt::Continue:
[6c4ff37]541 output << "continue";
[51587aa]542 break;
543 }
[2b6c1e0]544 output << ";";
[51587aa]545 }
546
547
[6c4ff37]548 void CodeGenerator::visit( ReturnStmt *returnStmt ) {
549 output << "return ";
[51587aa]550
551 // xxx -- check for null expression;
552 if ( returnStmt->get_expr() ) {
553 returnStmt->get_expr()->accept( *this );
554 } // if
[6c4ff37]555 output << ";";
[51587aa]556 }
557
[6c4ff37]558 void CodeGenerator::visit( WhileStmt *whileStmt ) {
[51587aa]559 if ( whileStmt->get_isDoWhile() )
[6c4ff37]560 output << "do" ;
[51587aa]561 else {
[6c4ff37]562 output << "while (" ;
[51587aa]563 whileStmt->get_condition()->accept(*this );
[6c4ff37]564 output << ")";
[51587aa]565 } // if
[2b6c1e0]566 output << " ";
[51587aa]567
[2b6c1e0]568 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
[51587aa]569 whileStmt->get_body()->accept( *this );
570
[eb3261f]571 output << string( cur_indent, ' ' );
[51587aa]572
573 if ( whileStmt->get_isDoWhile() ) {
[6c4ff37]574 output << " while (" ;
[51587aa]575 whileStmt->get_condition()->accept(*this );
[6c4ff37]576 output << ");";
[51587aa]577 } // if
578 }
579
[6c4ff37]580 void CodeGenerator::visit( ForStmt *forStmt ) {
581 output << "for (";
[51587aa]582
583 if ( forStmt->get_initialization() != 0 )
584 forStmt->get_initialization()->accept( *this );
585 else
[6c4ff37]586 output << ";";
587
[51587aa]588 if ( forStmt->get_condition() != 0 )
589 forStmt->get_condition()->accept( *this );
[6c4ff37]590 output << ";";
[51587aa]591
592 if ( forStmt->get_increment() != 0 )
593 forStmt->get_increment()->accept( *this );
[2b6c1e0]594 output << ") ";
[51587aa]595
596 if ( forStmt->get_body() != 0 ) {
[2b6c1e0]597 output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
[51587aa]598 forStmt->get_body()->accept( *this );
599 } // if
600 }
601
[6c4ff37]602 void CodeGenerator::visit( NullStmt *nullStmt ) {
[eb3261f]603 //output << string( cur_indent, ' ' ) << CodeGenerator::printLabels( nullStmt->get_labels() );
[6c4ff37]604 output << "/* null statement */ ;";
[51587aa]605 }
606
[6c4ff37]607 void CodeGenerator::visit( DeclStmt *declStmt ) {
[51587aa]608 declStmt->get_decl()->accept( *this );
609
610 if ( doSemicolon( declStmt->get_decl() ) ) {
[6c4ff37]611 output << ";";
[51587aa]612 } // if
613 }
614
[6c4ff37]615 std::string CodeGenerator::printLabels( std::list< Label > &l ) {
[51587aa]616 std::string str( "" );
[6c4ff37]617 l.unique(); // assumes a sorted list. Why not use set?
[51587aa]618
619 for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ )
620 str += *i + ": ";
621
622 return str;
623 }
624
[6c4ff37]625 void CodeGenerator::handleStorageClass( Declaration *decl ) {
[51587aa]626 switch ( decl->get_storageClass() ) {
[68cd1ce]627 case DeclarationNode::Extern:
[6c4ff37]628 output << "extern ";
[51587aa]629 break;
[68cd1ce]630 case DeclarationNode::Static:
[6c4ff37]631 output << "static ";
[51587aa]632 break;
[68cd1ce]633 case DeclarationNode::Auto:
[51587aa]634 // silently drop storage class
635 break;
[68cd1ce]636 case DeclarationNode::Register:
[6c4ff37]637 output << "register ";
[51587aa]638 break;
[68cd1ce]639 case DeclarationNode::Inline:
[de62360d]640 output << "inline ";
[f38c8d9]641 break;
[68cd1ce]642 case DeclarationNode::Fortran:
643 output << "fortran ";
644 break;
645 case DeclarationNode::Noreturn:
646 output << "_Noreturn ";
647 break;
648 case DeclarationNode::Threadlocal:
649 output << "_Thread_local ";
650 break;
651 case DeclarationNode::NoStorageClass:
[f38c8d9]652 break;
[843054c2]653 } // switch
[51587aa]654 }
[51b73452]655} // namespace CodeGen
[51587aa]656
657// Local Variables: //
658// tab-width: 4 //
659// mode: c++ //
660// compile-command: "make install" //
661// End: //
Note: See TracBrowser for help on using the repository browser.