source: src/CodeGen/CodeGenerator.cc@ e45215c

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

basic designator codegen

  • Property mode set to 100644
File size: 18.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
[145f1fc]11// Last Modified By : Rob Schluntz
12// Last Modified On : Wed Jul 15 14:47:42 2015
13// Update Count : 177
[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
[cda48b6]54 CodeGenerator::CodeGenerator( std::ostream &os ) : indent(*this), cur_indent( 0 ), insideFunction( false ), output( os ) { }
55
56 CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indentation, bool infunp )
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 )
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() ) {
93 functionDecl->get_statements()->accept(*this );
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;
[51587aa]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 << " = ";
[51587aa]161 obj->get_init()->accept(*this );
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
[e45215c]188 void CodeGenerator::printDesignators( std::list< Expression * > & designators ) {
189 typedef std::list< Expression * > DesignatorList;
190 if ( designators.size() == 0 ) return;
191 for ( DesignatorList::iterator iter = designators.begin(); iter != designators.end(); ++iter ) {
192 // if expression is a name, then initializing aggregate member, if constant expression then array
193 if ( NameExpr * nm = dynamic_cast< NameExpr * >( *iter ) ) {
194 if ( nm->get_name() == "0" || nm->get_name() == "1" ) {
195 // except if name is 0 or 1...
196 output << "[";
197 nm->accept( *this );
198 output << "]";
199 } else {
200 output << ".";
201 nm->accept( *this );
202 }
203 } else if ( dynamic_cast< ConstantExpr * >( *iter ) ) {
204 output << "[";
205 (*iter)->accept( *this );
206 output << "]";
207 } else {
208 assert(0);
209 }
210 }
211 output << " = ";
212 }
213
[6c4ff37]214 void CodeGenerator::visit( SingleInit *init ) {
[e45215c]215 printDesignators( init->get_designators() );
[51587aa]216 init->get_value()->accept( *this );
217 }
218
[6c4ff37]219 void CodeGenerator::visit( ListInit *init ) {
[e45215c]220 printDesignators( init->get_designators() );
[6c4ff37]221 output << "{ ";
[51587aa]222 genCommaList( init->begin_initializers(), init->end_initializers() );
[6c4ff37]223 output << " }";
[51587aa]224 }
225
[6c4ff37]226 void CodeGenerator::visit( Constant *constant ) {
227 output << constant->get_value() ;
[51587aa]228 }
229
230 //*** Expressions
[6c4ff37]231 void CodeGenerator::visit( ApplicationExpr *applicationExpr ) {
[51587aa]232 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
233 OperatorInfo opInfo;
234 if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
235 std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
236 switch ( opInfo.type ) {
237 case OT_PREFIXASSIGN:
238 case OT_POSTFIXASSIGN:
239 case OT_INFIXASSIGN:
240 {
241 assert( arg != applicationExpr->get_args().end() );
242 if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
243
244 *arg = addrExpr->get_arg();
245 } else {
246 UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
247 newExpr->get_args().push_back( *arg );
248 *arg = newExpr;
249 } // if
250 break;
251 }
252
253 default:
254 // do nothing
255 ;
256 }
257
258 switch ( opInfo.type ) {
259 case OT_INDEX:
260 assert( applicationExpr->get_args().size() == 2 );
261 (*arg++)->accept( *this );
[6c4ff37]262 output << "[";
[51587aa]263 (*arg)->accept( *this );
[6c4ff37]264 output << "]";
[51587aa]265 break;
266
267 case OT_CALL:
268 // there are no intrinsic definitions of the function call operator
269 assert( false );
270 break;
271
272 case OT_PREFIX:
273 case OT_PREFIXASSIGN:
274 assert( applicationExpr->get_args().size() == 1 );
[6c4ff37]275 output << "(";
276 output << opInfo.symbol;
[51587aa]277 (*arg)->accept( *this );
[6c4ff37]278 output << ")";
[51587aa]279 break;
280
281 case OT_POSTFIX:
282 case OT_POSTFIXASSIGN:
283 assert( applicationExpr->get_args().size() == 1 );
284 (*arg)->accept( *this );
[6c4ff37]285 output << opInfo.symbol;
[51587aa]286 break;
287
288 case OT_INFIX:
289 case OT_INFIXASSIGN:
290 assert( applicationExpr->get_args().size() == 2 );
[6c4ff37]291 output << "(";
[51587aa]292 (*arg++)->accept( *this );
[6c4ff37]293 output << opInfo.symbol;
[51587aa]294 (*arg)->accept( *this );
[6c4ff37]295 output << ")";
[51587aa]296 break;
297
298 case OT_CONSTANT:
[721f17a]299 case OT_LABELADDRESS:
300 // there are no intrinsic definitions of 0/1 or label addresses as functions
[51587aa]301 assert( false );
302 }
[17cd4eb]303 } else {
[51587aa]304 varExpr->accept( *this );
[6c4ff37]305 output << "(";
[51587aa]306 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
[6c4ff37]307 output << ")";
[17cd4eb]308 } // if
[51587aa]309 } else {
310 applicationExpr->get_function()->accept( *this );
[6c4ff37]311 output << "(";
[51587aa]312 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
[6c4ff37]313 output << ")";
[51587aa]314 } // if
315 }
[51b73452]316
[6c4ff37]317 void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
[51587aa]318 if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
319 OperatorInfo opInfo;
320 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
321 std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
322 switch ( opInfo.type ) {
323 case OT_INDEX:
324 assert( untypedExpr->get_args().size() == 2 );
325 (*arg++)->accept( *this );
[6c4ff37]326 output << "[";
[51587aa]327 (*arg)->accept( *this );
[6c4ff37]328 output << "]";
[51587aa]329 break;
330
331 case OT_CALL:
332 assert( false );
333 break;
334
335 case OT_PREFIX:
336 case OT_PREFIXASSIGN:
[de62360d]337 case OT_LABELADDRESS:
[51587aa]338 assert( untypedExpr->get_args().size() == 1 );
[6c4ff37]339 output << "(";
340 output << opInfo.symbol;
[51587aa]341 (*arg)->accept( *this );
[6c4ff37]342 output << ")";
[51587aa]343 break;
344
345 case OT_POSTFIX:
346 case OT_POSTFIXASSIGN:
347 assert( untypedExpr->get_args().size() == 1 );
348 (*arg)->accept( *this );
[6c4ff37]349 output << opInfo.symbol;
[51587aa]350 break;
[51b73452]351
[51587aa]352 case OT_INFIX:
353 case OT_INFIXASSIGN:
354 assert( untypedExpr->get_args().size() == 2 );
[6c4ff37]355 output << "(";
[51587aa]356 (*arg++)->accept( *this );
[6c4ff37]357 output << opInfo.symbol;
[51587aa]358 (*arg)->accept( *this );
[6c4ff37]359 output << ")";
[51587aa]360 break;
[de62360d]361
[51587aa]362 case OT_CONSTANT:
363 // there are no intrinsic definitions of 0 or 1 as functions
364 assert( false );
365 }
366 } else {
367 nameExpr->accept( *this );
[6c4ff37]368 output << "(";
[51587aa]369 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
[6c4ff37]370 output << ")";
[51587aa]371 } // if
372 } else {
373 untypedExpr->get_function()->accept( *this );
[6c4ff37]374 output << "(";
[51587aa]375 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
[6c4ff37]376 output << ")";
[51587aa]377 } // if
378 }
[51b73452]379
[6c4ff37]380 void CodeGenerator::visit( NameExpr *nameExpr ) {
[51587aa]381 OperatorInfo opInfo;
382 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
383 assert( opInfo.type == OT_CONSTANT );
[6c4ff37]384 output << opInfo.symbol;
[51587aa]385 } else {
[6c4ff37]386 output << nameExpr->get_name();
[51587aa]387 } // if
388 }
[51b73452]389
[6c4ff37]390 void CodeGenerator::visit( AddressExpr *addressExpr ) {
391 output << "(&";
[51587aa]392 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
393 if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
[6c4ff37]394 output << mangleName( variableExpr->get_var() );
[51587aa]395 } else {
396 addressExpr->get_arg()->accept( *this );
397 } // if
[6c4ff37]398 output << ")";
[51587aa]399 }
400
[6c4ff37]401 void CodeGenerator::visit( CastExpr *castExpr ) {
402 output << "((";
[51587aa]403 if ( castExpr->get_results().empty() ) {
[6c4ff37]404 output << "void" ;
[51587aa]405 } else {
[6c4ff37]406 output << genType( castExpr->get_results().front(), "" );
[51587aa]407 } // if
[6c4ff37]408 output << ")";
[51587aa]409 castExpr->get_arg()->accept( *this );
[6c4ff37]410 output << ")";
[51587aa]411 }
[51b73452]412
[6c4ff37]413 void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
[51587aa]414 assert( false );
415 }
[51b73452]416
[6c4ff37]417 void CodeGenerator::visit( MemberExpr *memberExpr ) {
[51587aa]418 memberExpr->get_aggregate()->accept( *this );
[6c4ff37]419 output << "." << mangleName( memberExpr->get_member() );
[51587aa]420 }
[51b73452]421
[6c4ff37]422 void CodeGenerator::visit( VariableExpr *variableExpr ) {
[51587aa]423 OperatorInfo opInfo;
424 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
[6c4ff37]425 output << opInfo.symbol;
[51587aa]426 } else {
[6c4ff37]427 output << mangleName( variableExpr->get_var() );
[51587aa]428 } // if
429 }
[51b73452]430
[6c4ff37]431 void CodeGenerator::visit( ConstantExpr *constantExpr ) {
[51587aa]432 assert( constantExpr->get_constant() );
433 constantExpr->get_constant()->accept( *this );
434 }
[51b73452]435
[6c4ff37]436 void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
437 output << "sizeof(";
[51587aa]438 if ( sizeofExpr->get_isType() ) {
[6c4ff37]439 output << genType( sizeofExpr->get_type(), "" );
[51587aa]440 } else {
441 sizeofExpr->get_expr()->accept( *this );
442 } // if
[6c4ff37]443 output << ")";
[51587aa]444 }
[51b73452]445
[6c4ff37]446 void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
447 output << "(";
[51587aa]448 logicalExpr->get_arg1()->accept( *this );
449 if ( logicalExpr->get_isAnd() ) {
[6c4ff37]450 output << " && ";
[51587aa]451 } else {
[6c4ff37]452 output << " || ";
[51587aa]453 } // if
454 logicalExpr->get_arg2()->accept( *this );
[6c4ff37]455 output << ")";
[51587aa]456 }
[51b73452]457
[6c4ff37]458 void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
459 output << "(";
[51587aa]460 conditionalExpr->get_arg1()->accept( *this );
[6c4ff37]461 output << " ? ";
[51587aa]462 conditionalExpr->get_arg2()->accept( *this );
[6c4ff37]463 output << " : ";
[51587aa]464 conditionalExpr->get_arg3()->accept( *this );
[6c4ff37]465 output << ")";
[51587aa]466 }
[51b73452]467
[6c4ff37]468 void CodeGenerator::visit( CommaExpr *commaExpr ) {
469 output << "(";
[51587aa]470 commaExpr->get_arg1()->accept( *this );
[6c4ff37]471 output << " , ";
[51587aa]472 commaExpr->get_arg2()->accept( *this );
[6c4ff37]473 output << ")";
[51587aa]474 }
[51b73452]475
[6c4ff37]476 void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
[51b73452]477
[6c4ff37]478 void CodeGenerator::visit( TypeExpr *typeExpr ) {}
[2b6c1e0]479
[51587aa]480 //*** Statements
[6c4ff37]481 void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
[51587aa]482 std::list<Statement*> ks = compoundStmt->get_kids();
[2b6c1e0]483 output << "{" << endl;
[51587aa]484
[2b6c1e0]485 cur_indent += CodeGenerator::tabsize;
[51587aa]486
487 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++) {
[cda48b6]488 output << indent << printLabels( (*i)->get_labels() );
[51587aa]489 (*i)->accept(*this );
[2b6c1e0]490
[6c4ff37]491 output << endl;
[2b6c1e0]492 if ( wantSpacing( *i ) ) {
493 output << endl;
494 }
[51587aa]495 }
[6c4ff37]496 cur_indent -= CodeGenerator::tabsize;
[51587aa]497
[cda48b6]498 output << indent << "}";
[51587aa]499 }
500
[6c4ff37]501 void CodeGenerator::visit( ExprStmt *exprStmt ) {
502 // I don't see why this check is necessary.
503 // If this starts to cause problems then put it back in,
504 // with an explanation
505 assert( exprStmt );
506
507 // if ( exprStmt != 0 ) {
508 exprStmt->get_expr()->accept( *this );
509 output << ";" ;
510 // } // if
[51587aa]511 }
512
[6c4ff37]513 void CodeGenerator::visit( IfStmt *ifStmt ) {
514 output << "if (";
[51587aa]515 ifStmt->get_condition()->accept(*this );
[2b6c1e0]516 output << ") ";
[51587aa]517
518 ifStmt->get_thenPart()->accept(*this );
519
520 if ( ifStmt->get_elsePart() != 0) {
[2b6c1e0]521 output << " else ";
[51587aa]522 ifStmt->get_elsePart()->accept(*this );
523 } // if
524 }
525
[6c4ff37]526 void CodeGenerator::visit( SwitchStmt *switchStmt ) {
527 output << "switch (" ;
[51587aa]528 switchStmt->get_condition()->accept(*this );
[2b6c1e0]529 output << ") ";
[6c4ff37]530
[2b6c1e0]531 output << "{" << std::endl;
[6c4ff37]532 cur_indent += CodeGenerator::tabsize;
[51587aa]533
[6c4ff37]534 acceptAll( switchStmt->get_branches(), *this );
[51587aa]535
[6c4ff37]536 cur_indent -= CodeGenerator::tabsize;
[51587aa]537
[cda48b6]538 output << indent << "}";
[51587aa]539 }
540
[6c4ff37]541 void CodeGenerator::visit( CaseStmt *caseStmt ) {
[cda48b6]542 output << indent;
[eb3261f]543 if ( caseStmt->isDefault()) {
[2b6c1e0]544 output << "default";
[eb3261f]545 } else {
[2b6c1e0]546 output << "case ";
[51587aa]547 caseStmt->get_condition()->accept(*this );
[17cd4eb]548 } // if
[6c4ff37]549 output << ":\n";
550
[51587aa]551 std::list<Statement *> sts = caseStmt->get_statements();
552
[6c4ff37]553 cur_indent += CodeGenerator::tabsize;
[51587aa]554 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
[cda48b6]555 output << indent << printLabels( (*i)->get_labels() ) ;
[51587aa]556 (*i)->accept(*this );
[6c4ff37]557 output << endl;
[51587aa]558 }
[6c4ff37]559 cur_indent -= CodeGenerator::tabsize;
[51587aa]560 }
561
[6c4ff37]562 void CodeGenerator::visit( BranchStmt *branchStmt ) {
[51587aa]563 switch ( branchStmt->get_type()) {
564 case BranchStmt::Goto:
565 if ( ! branchStmt->get_target().empty() )
[6c4ff37]566 output << "goto " << branchStmt->get_target();
[51587aa]567 else {
568 if ( branchStmt->get_computedTarget() != 0 ) {
[6c4ff37]569 output << "goto *";
[51587aa]570 branchStmt->get_computedTarget()->accept( *this );
571 } // if
572 } // if
573 break;
574 case BranchStmt::Break:
[6c4ff37]575 output << "break";
[51587aa]576 break;
577 case BranchStmt::Continue:
[6c4ff37]578 output << "continue";
[51587aa]579 break;
580 }
[2b6c1e0]581 output << ";";
[51587aa]582 }
583
584
[6c4ff37]585 void CodeGenerator::visit( ReturnStmt *returnStmt ) {
586 output << "return ";
[51587aa]587
588 // xxx -- check for null expression;
589 if ( returnStmt->get_expr() ) {
590 returnStmt->get_expr()->accept( *this );
591 } // if
[6c4ff37]592 output << ";";
[51587aa]593 }
594
[6c4ff37]595 void CodeGenerator::visit( WhileStmt *whileStmt ) {
[51587aa]596 if ( whileStmt->get_isDoWhile() )
[6c4ff37]597 output << "do" ;
[51587aa]598 else {
[6c4ff37]599 output << "while (" ;
[51587aa]600 whileStmt->get_condition()->accept(*this );
[6c4ff37]601 output << ")";
[51587aa]602 } // if
[2b6c1e0]603 output << " ";
[51587aa]604
[2b6c1e0]605 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
[51587aa]606 whileStmt->get_body()->accept( *this );
607
[cda48b6]608 output << indent;
[51587aa]609
610 if ( whileStmt->get_isDoWhile() ) {
[6c4ff37]611 output << " while (" ;
[51587aa]612 whileStmt->get_condition()->accept(*this );
[6c4ff37]613 output << ");";
[51587aa]614 } // if
615 }
616
[6c4ff37]617 void CodeGenerator::visit( ForStmt *forStmt ) {
[145f1fc]618 // initialization is always hoisted, so don't
619 // bother doing anything with that
620 output << "for (;";
[51587aa]621
622 if ( forStmt->get_condition() != 0 )
623 forStmt->get_condition()->accept( *this );
[6c4ff37]624 output << ";";
[51587aa]625
626 if ( forStmt->get_increment() != 0 )
627 forStmt->get_increment()->accept( *this );
[2b6c1e0]628 output << ") ";
[51587aa]629
630 if ( forStmt->get_body() != 0 ) {
[2b6c1e0]631 output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
[51587aa]632 forStmt->get_body()->accept( *this );
633 } // if
634 }
635
[6c4ff37]636 void CodeGenerator::visit( NullStmt *nullStmt ) {
[cda48b6]637 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
[6c4ff37]638 output << "/* null statement */ ;";
[51587aa]639 }
640
[6c4ff37]641 void CodeGenerator::visit( DeclStmt *declStmt ) {
[51587aa]642 declStmt->get_decl()->accept( *this );
643
644 if ( doSemicolon( declStmt->get_decl() ) ) {
[6c4ff37]645 output << ";";
[51587aa]646 } // if
647 }
648
[6c4ff37]649 std::string CodeGenerator::printLabels( std::list< Label > &l ) {
[51587aa]650 std::string str( "" );
[6c4ff37]651 l.unique(); // assumes a sorted list. Why not use set?
[51587aa]652
653 for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ )
654 str += *i + ": ";
655
656 return str;
657 }
658
[6c4ff37]659 void CodeGenerator::handleStorageClass( Declaration *decl ) {
[51587aa]660 switch ( decl->get_storageClass() ) {
[68cd1ce]661 case DeclarationNode::Extern:
[6c4ff37]662 output << "extern ";
[51587aa]663 break;
[68cd1ce]664 case DeclarationNode::Static:
[6c4ff37]665 output << "static ";
[51587aa]666 break;
[68cd1ce]667 case DeclarationNode::Auto:
[51587aa]668 // silently drop storage class
669 break;
[68cd1ce]670 case DeclarationNode::Register:
[6c4ff37]671 output << "register ";
[51587aa]672 break;
[68cd1ce]673 case DeclarationNode::Inline:
[de62360d]674 output << "inline ";
[f38c8d9]675 break;
[68cd1ce]676 case DeclarationNode::Fortran:
677 output << "fortran ";
678 break;
679 case DeclarationNode::Noreturn:
680 output << "_Noreturn ";
681 break;
682 case DeclarationNode::Threadlocal:
683 output << "_Thread_local ";
684 break;
685 case DeclarationNode::NoStorageClass:
[f38c8d9]686 break;
[843054c2]687 } // switch
[51587aa]688 }
[51b73452]689} // namespace CodeGen
[51587aa]690
691// Local Variables: //
692// tab-width: 4 //
693// mode: c++ //
694// compile-command: "make install" //
695// End: //
Note: See TracBrowser for help on using the repository browser.