source: src/CodeGen/CodeGenerator.cc@ 7528ba1

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 with_gc
Last change on this file since 7528ba1 was d63eeb0, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

Merge branch 'master' into ctor

Conflicts:

src/CodeGen/CodeGenerator.cc
src/GenPoly/Box.cc
src/Makefile.in
src/Parser/ParseNode.h
src/Parser/parser.cc
src/Parser/parser.yy
src/SymTab/Validate.cc
src/SynTree/Initializer.h
src/SynTree/ObjectDecl.cc
src/SynTree/Visitor.h
src/main.cc

  • Property mode set to 100644
File size: 21.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//
[71f4e4f]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
[d63eeb0]12// Last Modified On : Tue Feb 09 13:24:40 2016
13// Update Count : 255
[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
[d3b7937]28#include "Common/utility.h"
29#include "Common/UnimplementedError.h"
[51b73452]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 ) );
[71f4e4f]100
[51587aa]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();
[71f4e4f]114
[51587aa]115 std::list< Declaration * > &memb = aggDecl->get_members();
116
117 if ( ! memb.empty() ) {
[94b4364]118 output << " {" << endl;
[51587aa]119
[71f4e4f]120 cur_indent += CodeGenerator::tabsize;
[51587aa]121 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
[71f4e4f]122 output << indent;
[7f5566b]123 (*i)->accept( *this );
[6c4ff37]124 output << ";" << endl;
[51587aa]125 }
126
[71f4e4f]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 }
[71f4e4f]142
[6c4ff37]143 void CodeGenerator::visit( EnumDecl *aggDecl ) {
144 output << "enum ";
[51587aa]145
146 if ( aggDecl->get_name() != "" )
[6c4ff37]147 output << aggDecl->get_name();
[71f4e4f]148
[51587aa]149 std::list< Declaration* > &memb = aggDecl->get_members();
150
151 if ( ! memb.empty() ) {
[cda48b6]152 output << " {" << endl;
[51587aa]153
[71f4e4f]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 );
[71f4e4f]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
[71f4e4f]166 cur_indent -= CodeGenerator::tabsize;
[51587aa]167
[cda48b6]168 output << indent << "}";
[51587aa]169 } // if
170 }
[71f4e4f]171
[6c4ff37]172 void CodeGenerator::visit( ContextDecl *aggregateDecl ) {}
[71f4e4f]173
[6c4ff37]174 void CodeGenerator::visit( TypedefDecl *typeDecl ) {
175 output << "typedef ";
176 output << genType( typeDecl->get_base(), typeDecl->get_name() );
[51587aa]177 }
[71f4e4f]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 ) {
[8a4da06]192 if ( dynamic_cast< NameExpr * >( *iter ) ) {
[f32c7f4]193 // if expression is a name, then initializing aggregate member
194 output << ".";
195 (*iter)->accept( *this );
196 } else {
197 // if not a simple name, it has to be a constant expression, i.e. an array designator
[e45215c]198 output << "[";
199 (*iter)->accept( *this );
200 output << "]";
201 }
202 }
203 output << " = ";
204 }
205
[6c4ff37]206 void CodeGenerator::visit( SingleInit *init ) {
[e45215c]207 printDesignators( init->get_designators() );
[51587aa]208 init->get_value()->accept( *this );
209 }
210
[6c4ff37]211 void CodeGenerator::visit( ListInit *init ) {
[e45215c]212 printDesignators( init->get_designators() );
[6c4ff37]213 output << "{ ";
[51587aa]214 genCommaList( init->begin_initializers(), init->end_initializers() );
[6c4ff37]215 output << " }";
[51587aa]216 }
217
[71f4e4f]218 void CodeGenerator::visit( Constant *constant ) {
[6c4ff37]219 output << constant->get_value() ;
[51587aa]220 }
221
222 //*** Expressions
[6c4ff37]223 void CodeGenerator::visit( ApplicationExpr *applicationExpr ) {
[51587aa]224 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
225 OperatorInfo opInfo;
226 if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
227 std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
228 switch ( opInfo.type ) {
229 case OT_PREFIXASSIGN:
230 case OT_POSTFIXASSIGN:
231 case OT_INFIXASSIGN:
232 {
233 assert( arg != applicationExpr->get_args().end() );
234 if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
[71f4e4f]235
[51587aa]236 *arg = addrExpr->get_arg();
237 } else {
238 UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
239 newExpr->get_args().push_back( *arg );
240 *arg = newExpr;
241 } // if
242 break;
243 }
[71f4e4f]244
[51587aa]245 default:
246 // do nothing
247 ;
248 }
[71f4e4f]249
[51587aa]250 switch ( opInfo.type ) {
251 case OT_INDEX:
252 assert( applicationExpr->get_args().size() == 2 );
253 (*arg++)->accept( *this );
[6c4ff37]254 output << "[";
[51587aa]255 (*arg)->accept( *this );
[6c4ff37]256 output << "]";
[51587aa]257 break;
[71f4e4f]258
[51587aa]259 case OT_CALL:
[2794fff]260 // there are no intrinsic definitions of the function call operator or constructors or destructors
[51587aa]261 assert( false );
262 break;
[71f4e4f]263
[f1e012b]264 case OT_CTOR:
265 // intrinsic constructors should never be called directly - they should be transformed back into Initializer nodes
266 assert(false);
267 break;
268
269 case OT_DTOR:
270 // intrinsic destructors do nothing - don't generate any code
271 output << " // " << dynamic_cast<VariableExpr*>(applicationExpr->get_function())->get_var()->get_name() << endl;
272 break;
273
[51587aa]274 case OT_PREFIX:
275 case OT_PREFIXASSIGN:
276 assert( applicationExpr->get_args().size() == 1 );
[6c4ff37]277 output << "(";
278 output << opInfo.symbol;
[51587aa]279 (*arg)->accept( *this );
[6c4ff37]280 output << ")";
[51587aa]281 break;
[71f4e4f]282
[51587aa]283 case OT_POSTFIX:
284 case OT_POSTFIXASSIGN:
285 assert( applicationExpr->get_args().size() == 1 );
286 (*arg)->accept( *this );
[6c4ff37]287 output << opInfo.symbol;
[51587aa]288 break;
289
[f1e012b]290
[51587aa]291 case OT_INFIX:
292 case OT_INFIXASSIGN:
293 assert( applicationExpr->get_args().size() == 2 );
[6c4ff37]294 output << "(";
[51587aa]295 (*arg++)->accept( *this );
[6c4ff37]296 output << opInfo.symbol;
[51587aa]297 (*arg)->accept( *this );
[6c4ff37]298 output << ")";
[51587aa]299 break;
[71f4e4f]300
[51587aa]301 case OT_CONSTANT:
[721f17a]302 case OT_LABELADDRESS:
303 // there are no intrinsic definitions of 0/1 or label addresses as functions
[51587aa]304 assert( false );
305 }
[17cd4eb]306 } else {
[51587aa]307 varExpr->accept( *this );
[6c4ff37]308 output << "(";
[51587aa]309 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
[6c4ff37]310 output << ")";
[17cd4eb]311 } // if
[51587aa]312 } else {
313 applicationExpr->get_function()->accept( *this );
[6c4ff37]314 output << "(";
[51587aa]315 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
[6c4ff37]316 output << ")";
[51587aa]317 } // if
318 }
[71f4e4f]319
[6c4ff37]320 void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
[51587aa]321 if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
322 OperatorInfo opInfo;
323 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
324 std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
325 switch ( opInfo.type ) {
326 case OT_INDEX:
327 assert( untypedExpr->get_args().size() == 2 );
328 (*arg++)->accept( *this );
[6c4ff37]329 output << "[";
[51587aa]330 (*arg)->accept( *this );
[6c4ff37]331 output << "]";
[51587aa]332 break;
[71f4e4f]333
[51587aa]334 case OT_CALL:
[f1e012b]335 assert( false );
336
[1840193]337 case OT_CTOR:
338 case OT_DTOR:
[f1e012b]339 // intrinsic constructors should never be called
340 // intrinsic destructors do nothing
[51587aa]341 break;
[71f4e4f]342
[51587aa]343 case OT_PREFIX:
344 case OT_PREFIXASSIGN:
[de62360d]345 case OT_LABELADDRESS:
[51587aa]346 assert( untypedExpr->get_args().size() == 1 );
[6c4ff37]347 output << "(";
348 output << opInfo.symbol;
[51587aa]349 (*arg)->accept( *this );
[6c4ff37]350 output << ")";
[51587aa]351 break;
[71f4e4f]352
[51587aa]353 case OT_POSTFIX:
354 case OT_POSTFIXASSIGN:
355 assert( untypedExpr->get_args().size() == 1 );
356 (*arg)->accept( *this );
[6c4ff37]357 output << opInfo.symbol;
[51587aa]358 break;
[71f4e4f]359
[51587aa]360 case OT_INFIX:
361 case OT_INFIXASSIGN:
362 assert( untypedExpr->get_args().size() == 2 );
[6c4ff37]363 output << "(";
[51587aa]364 (*arg++)->accept( *this );
[6c4ff37]365 output << opInfo.symbol;
[51587aa]366 (*arg)->accept( *this );
[6c4ff37]367 output << ")";
[51587aa]368 break;
[71f4e4f]369
[51587aa]370 case OT_CONSTANT:
371 // there are no intrinsic definitions of 0 or 1 as functions
372 assert( false );
373 }
374 } else {
375 nameExpr->accept( *this );
[6c4ff37]376 output << "(";
[51587aa]377 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
[6c4ff37]378 output << ")";
[51587aa]379 } // if
380 } else {
381 untypedExpr->get_function()->accept( *this );
[6c4ff37]382 output << "(";
[51587aa]383 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
[6c4ff37]384 output << ")";
[51587aa]385 } // if
386 }
[71f4e4f]387
[6c4ff37]388 void CodeGenerator::visit( NameExpr *nameExpr ) {
[51587aa]389 OperatorInfo opInfo;
390 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
391 assert( opInfo.type == OT_CONSTANT );
[6c4ff37]392 output << opInfo.symbol;
[51587aa]393 } else {
[6c4ff37]394 output << nameExpr->get_name();
[51587aa]395 } // if
396 }
[71f4e4f]397
[6c4ff37]398 void CodeGenerator::visit( AddressExpr *addressExpr ) {
399 output << "(&";
[51587aa]400 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
401 if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
[6c4ff37]402 output << mangleName( variableExpr->get_var() );
[51587aa]403 } else {
404 addressExpr->get_arg()->accept( *this );
405 } // if
[6c4ff37]406 output << ")";
[51587aa]407 }
408
[6c4ff37]409 void CodeGenerator::visit( CastExpr *castExpr ) {
[803deb1]410 output << "(";
411 if ( castExpr->get_results().empty() ) {
412 output << "(void)" ;
413 } else if ( ! castExpr->get_results().front()->get_isLvalue() ) {
414 // at least one result type of cast, but not an lvalue
415 output << "(";
416 output << genType( castExpr->get_results().front(), "" );
[71f4e4f]417 output << ")";
[803deb1]418 } else {
419 // otherwise, the cast is to an lvalue type, so the cast
420 // should be dropped, since the result of a cast is
421 // never an lvalue in C
[76b48f1]422 }
[803deb1]423 castExpr->get_arg()->accept( *this );
424 output << ")";
[51587aa]425 }
[71f4e4f]426
[6c4ff37]427 void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
[51587aa]428 assert( false );
429 }
[71f4e4f]430
[6c4ff37]431 void CodeGenerator::visit( MemberExpr *memberExpr ) {
[51587aa]432 memberExpr->get_aggregate()->accept( *this );
[6c4ff37]433 output << "." << mangleName( memberExpr->get_member() );
[51587aa]434 }
[71f4e4f]435
[6c4ff37]436 void CodeGenerator::visit( VariableExpr *variableExpr ) {
[51587aa]437 OperatorInfo opInfo;
438 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
[6c4ff37]439 output << opInfo.symbol;
[51587aa]440 } else {
[6c4ff37]441 output << mangleName( variableExpr->get_var() );
[51587aa]442 } // if
443 }
[71f4e4f]444
[6c4ff37]445 void CodeGenerator::visit( ConstantExpr *constantExpr ) {
[51587aa]446 assert( constantExpr->get_constant() );
447 constantExpr->get_constant()->accept( *this );
448 }
[71f4e4f]449
[6c4ff37]450 void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
451 output << "sizeof(";
[51587aa]452 if ( sizeofExpr->get_isType() ) {
[6c4ff37]453 output << genType( sizeofExpr->get_type(), "" );
[51587aa]454 } else {
455 sizeofExpr->get_expr()->accept( *this );
456 } // if
[6c4ff37]457 output << ")";
[51587aa]458 }
[47534159]459
[25a054f]460 void CodeGenerator::visit( AlignofExpr *alignofExpr ) {
[47534159]461 // use GCC extension to avoid bumping std to C11
462 output << "__alignof__(";
[25a054f]463 if ( alignofExpr->get_isType() ) {
464 output << genType( alignofExpr->get_type(), "" );
[47534159]465 } else {
[25a054f]466 alignofExpr->get_expr()->accept( *this );
[47534159]467 } // if
468 output << ")";
469 }
[71f4e4f]470
[2a4b088]471 void CodeGenerator::visit( UntypedOffsetofExpr *offsetofExpr ) {
472 assert( false );
473 }
474
[25a054f]475 void CodeGenerator::visit( OffsetofExpr *offsetofExpr ) {
476 // use GCC builtin
477 output << "__builtin_offsetof(";
478 output << genType( offsetofExpr->get_type(), "" );
[e551c69]479 output << ", " << mangleName( offsetofExpr->get_member() );
[25a054f]480 output << ")";
481 }
[d63eeb0]482
[6c4ff37]483 void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
484 output << "(";
[51587aa]485 logicalExpr->get_arg1()->accept( *this );
486 if ( logicalExpr->get_isAnd() ) {
[6c4ff37]487 output << " && ";
[51587aa]488 } else {
[6c4ff37]489 output << " || ";
[51587aa]490 } // if
491 logicalExpr->get_arg2()->accept( *this );
[6c4ff37]492 output << ")";
[51587aa]493 }
[71f4e4f]494
[6c4ff37]495 void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
496 output << "(";
[51587aa]497 conditionalExpr->get_arg1()->accept( *this );
[6c4ff37]498 output << " ? ";
[51587aa]499 conditionalExpr->get_arg2()->accept( *this );
[6c4ff37]500 output << " : ";
[51587aa]501 conditionalExpr->get_arg3()->accept( *this );
[6c4ff37]502 output << ")";
[51587aa]503 }
[71f4e4f]504
[6c4ff37]505 void CodeGenerator::visit( CommaExpr *commaExpr ) {
506 output << "(";
[51587aa]507 commaExpr->get_arg1()->accept( *this );
[6c4ff37]508 output << " , ";
[51587aa]509 commaExpr->get_arg2()->accept( *this );
[6c4ff37]510 output << ")";
[51587aa]511 }
[71f4e4f]512
[6c4ff37]513 void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
[71f4e4f]514
[6c4ff37]515 void CodeGenerator::visit( TypeExpr *typeExpr ) {}
[2b6c1e0]516
[7f5566b]517 void CodeGenerator::visit( AsmExpr *asmExpr ) {
518 if ( asmExpr->get_inout() ) {
519 output << "[ ";
520 asmExpr->get_inout()->accept( *this );
521 output << " ] ";
522 } // if
523 asmExpr->get_constraint()->accept( *this );
524 output << " ( ";
525 asmExpr->get_operand()->accept( *this );
526 output << " )";
527 }
528
[51587aa]529 //*** Statements
[6c4ff37]530 void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
[51587aa]531 std::list<Statement*> ks = compoundStmt->get_kids();
[2b6c1e0]532 output << "{" << endl;
[51587aa]533
[2b6c1e0]534 cur_indent += CodeGenerator::tabsize;
[51587aa]535
[7f5566b]536 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) {
[cda48b6]537 output << indent << printLabels( (*i)->get_labels() );
[7f5566b]538 (*i)->accept( *this );
[2b6c1e0]539
[6c4ff37]540 output << endl;
[2b6c1e0]541 if ( wantSpacing( *i ) ) {
542 output << endl;
543 }
[51587aa]544 }
[71f4e4f]545 cur_indent -= CodeGenerator::tabsize;
[51587aa]546
[cda48b6]547 output << indent << "}";
[51587aa]548 }
549
[6c4ff37]550 void CodeGenerator::visit( ExprStmt *exprStmt ) {
[71f4e4f]551 // I don't see why this check is necessary.
552 // If this starts to cause problems then put it back in,
[6c4ff37]553 // with an explanation
554 assert( exprStmt );
555
556 // if ( exprStmt != 0 ) {
557 exprStmt->get_expr()->accept( *this );
558 output << ";" ;
559 // } // if
[51587aa]560 }
561
[7f5566b]562 void CodeGenerator::visit( AsmStmt *asmStmt ) {
563 output << "asm ";
564 if ( asmStmt->get_voltile() ) output << "volatile ";
565 if ( ! asmStmt->get_gotolabels().empty() ) output << "goto ";
566 output << "( ";
567 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
568 output << " : ";
569 genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
570 output << " : ";
571 genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
572 output << " : ";
573 genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
574 if ( ! asmStmt->get_gotolabels().empty() ) {
575 output << " : ";
576 for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
577 output << *begin++;
578 if ( begin == asmStmt->get_gotolabels().end() ) break;
579 output << ", ";
580 } // for
581 } // if
582 output << " );" ;
583 }
584
[6c4ff37]585 void CodeGenerator::visit( IfStmt *ifStmt ) {
[7f5566b]586 output << "if ( ";
587 ifStmt->get_condition()->accept( *this );
588 output << " ) ";
[51587aa]589
[7f5566b]590 ifStmt->get_thenPart()->accept( *this );
[51587aa]591
592 if ( ifStmt->get_elsePart() != 0) {
[2b6c1e0]593 output << " else ";
[7f5566b]594 ifStmt->get_elsePart()->accept( *this );
[51587aa]595 } // if
596 }
597
[6c4ff37]598 void CodeGenerator::visit( SwitchStmt *switchStmt ) {
[7f5566b]599 output << "switch ( " ;
600 switchStmt->get_condition()->accept( *this );
601 output << " ) ";
[71f4e4f]602
[2b6c1e0]603 output << "{" << std::endl;
[6c4ff37]604 cur_indent += CodeGenerator::tabsize;
[51587aa]605
[6c4ff37]606 acceptAll( switchStmt->get_branches(), *this );
[51587aa]607
[6c4ff37]608 cur_indent -= CodeGenerator::tabsize;
[51587aa]609
[cda48b6]610 output << indent << "}";
[51587aa]611 }
612
[6c4ff37]613 void CodeGenerator::visit( CaseStmt *caseStmt ) {
[cda48b6]614 output << indent;
[eb3261f]615 if ( caseStmt->isDefault()) {
[2b6c1e0]616 output << "default";
[eb3261f]617 } else {
[2b6c1e0]618 output << "case ";
[7f5566b]619 caseStmt->get_condition()->accept( *this );
[17cd4eb]620 } // if
[6c4ff37]621 output << ":\n";
[71f4e4f]622
[51587aa]623 std::list<Statement *> sts = caseStmt->get_statements();
624
[6c4ff37]625 cur_indent += CodeGenerator::tabsize;
[51587aa]626 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
[cda48b6]627 output << indent << printLabels( (*i)->get_labels() ) ;
[7f5566b]628 (*i)->accept( *this );
[6c4ff37]629 output << endl;
[51587aa]630 }
[6c4ff37]631 cur_indent -= CodeGenerator::tabsize;
[51587aa]632 }
633
[6c4ff37]634 void CodeGenerator::visit( BranchStmt *branchStmt ) {
[51587aa]635 switch ( branchStmt->get_type()) {
636 case BranchStmt::Goto:
637 if ( ! branchStmt->get_target().empty() )
[6c4ff37]638 output << "goto " << branchStmt->get_target();
[71f4e4f]639 else {
[51587aa]640 if ( branchStmt->get_computedTarget() != 0 ) {
[6c4ff37]641 output << "goto *";
[51587aa]642 branchStmt->get_computedTarget()->accept( *this );
643 } // if
644 } // if
645 break;
646 case BranchStmt::Break:
[6c4ff37]647 output << "break";
[51587aa]648 break;
649 case BranchStmt::Continue:
[6c4ff37]650 output << "continue";
[51587aa]651 break;
652 }
[2b6c1e0]653 output << ";";
[51587aa]654 }
655
656
[6c4ff37]657 void CodeGenerator::visit( ReturnStmt *returnStmt ) {
658 output << "return ";
[51587aa]659
660 // xxx -- check for null expression;
661 if ( returnStmt->get_expr() ) {
662 returnStmt->get_expr()->accept( *this );
663 } // if
[6c4ff37]664 output << ";";
[51587aa]665 }
666
[6c4ff37]667 void CodeGenerator::visit( WhileStmt *whileStmt ) {
[51587aa]668 if ( whileStmt->get_isDoWhile() )
[6c4ff37]669 output << "do" ;
[51587aa]670 else {
[6c4ff37]671 output << "while (" ;
[7f5566b]672 whileStmt->get_condition()->accept( *this );
[6c4ff37]673 output << ")";
[51587aa]674 } // if
[2b6c1e0]675 output << " ";
[51587aa]676
[2b6c1e0]677 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
[51587aa]678 whileStmt->get_body()->accept( *this );
679
[cda48b6]680 output << indent;
[51587aa]681
682 if ( whileStmt->get_isDoWhile() ) {
[6c4ff37]683 output << " while (" ;
[7f5566b]684 whileStmt->get_condition()->accept( *this );
[6c4ff37]685 output << ");";
[51587aa]686 } // if
687 }
688
[6c4ff37]689 void CodeGenerator::visit( ForStmt *forStmt ) {
[71f4e4f]690 // initialization is always hoisted, so don't
691 // bother doing anything with that
[145f1fc]692 output << "for (;";
[51587aa]693
694 if ( forStmt->get_condition() != 0 )
695 forStmt->get_condition()->accept( *this );
[6c4ff37]696 output << ";";
[51587aa]697
698 if ( forStmt->get_increment() != 0 )
699 forStmt->get_increment()->accept( *this );
[2b6c1e0]700 output << ") ";
[51587aa]701
702 if ( forStmt->get_body() != 0 ) {
[2b6c1e0]703 output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
[51587aa]704 forStmt->get_body()->accept( *this );
705 } // if
706 }
707
[6c4ff37]708 void CodeGenerator::visit( NullStmt *nullStmt ) {
[cda48b6]709 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
[6c4ff37]710 output << "/* null statement */ ;";
[51587aa]711 }
712
[6c4ff37]713 void CodeGenerator::visit( DeclStmt *declStmt ) {
[51587aa]714 declStmt->get_decl()->accept( *this );
[71f4e4f]715
[51587aa]716 if ( doSemicolon( declStmt->get_decl() ) ) {
[6c4ff37]717 output << ";";
[51587aa]718 } // if
719 }
720
[6c4ff37]721 std::string CodeGenerator::printLabels( std::list< Label > &l ) {
[51587aa]722 std::string str( "" );
[6c4ff37]723 l.unique(); // assumes a sorted list. Why not use set?
[51587aa]724
725 for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ )
726 str += *i + ": ";
727
728 return str;
729 }
730
[6c4ff37]731 void CodeGenerator::handleStorageClass( Declaration *decl ) {
[51587aa]732 switch ( decl->get_storageClass() ) {
[68cd1ce]733 case DeclarationNode::Extern:
[6c4ff37]734 output << "extern ";
[51587aa]735 break;
[68cd1ce]736 case DeclarationNode::Static:
[6c4ff37]737 output << "static ";
[51587aa]738 break;
[68cd1ce]739 case DeclarationNode::Auto:
[51587aa]740 // silently drop storage class
741 break;
[68cd1ce]742 case DeclarationNode::Register:
[6c4ff37]743 output << "register ";
[51587aa]744 break;
[68cd1ce]745 case DeclarationNode::Inline:
[de62360d]746 output << "inline ";
[f38c8d9]747 break;
[68cd1ce]748 case DeclarationNode::Fortran:
749 output << "fortran ";
750 break;
751 case DeclarationNode::Noreturn:
752 output << "_Noreturn ";
753 break;
754 case DeclarationNode::Threadlocal:
755 output << "_Thread_local ";
756 break;
757 case DeclarationNode::NoStorageClass:
[f38c8d9]758 break;
[843054c2]759 } // switch
[51587aa]760 }
[51b73452]761} // namespace CodeGen
[51587aa]762
763// Local Variables: //
764// tab-width: 4 //
765// mode: c++ //
766// compile-command: "make install" //
767// End: //
Note: See TracBrowser for help on using the repository browser.