source: src/CodeGen/CodeGenerator.cc@ f678663e

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 f678663e was 7f5566b, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

asm statement, memory leaks

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