source: src/CodeGen/CodeGenerator.cc@ a8541d9

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 a8541d9 was cda48b6, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

more codegen reformatting

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