source: src/CodeGen/CodeGenerator.cc@ db4ecc5

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

add integer suffix to mangled names so that shadowed variables are accessable (needed for destructors to work correctly)

  • Property mode set to 100644
File size: 21.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//
[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
[f326f99]12// Last Modified On : Mon Apr 11 15:30:52 2016
[d63eeb0]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
[e8032b0]23#include "SynTree/Declaration.h"
[51b73452]24#include "SynTree/Expression.h"
25#include "SynTree/Initializer.h"
[68cd1ce]26#include "SynTree/Statement.h"
[e8032b0]27#include "SynTree/Type.h"
[51b73452]28
[d3b7937]29#include "Common/utility.h"
30#include "Common/UnimplementedError.h"
[51b73452]31
[a61fea9a]32#include "CodeGenerator.h"
[51b73452]33#include "OperatorTable.h"
34#include "GenType.h"
35
36using namespace std;
37
38namespace CodeGen {
[6c4ff37]39 int CodeGenerator::tabsize = 4;
[51587aa]40
[145f1fc]41 // the kinds of statements that would ideally be followed by whitespace
[2b6c1e0]42 bool wantSpacing( Statement * stmt) {
43 return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) ||
44 dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * > ( stmt ) || dynamic_cast< SwitchStmt *>( stmt );
45 }
46
[cda48b6]47 ostream & CodeGenerator::Indenter::operator()( ostream & output ) {
48 return output << string( cg.cur_indent, ' ' );
49 }
50
51 ostream & operator<<( ostream & output, CodeGenerator::Indenter &indent ) {
52 return indent( output );
53 }
[51587aa]54
[7f5566b]55 CodeGenerator::CodeGenerator( std::ostream &os ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ) { }
[cda48b6]56
57 CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indentation, bool infunp )
[7f5566b]58 : indent( *this), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
[6c4ff37]59 //output << std::string( init );
[51587aa]60 }
61
[cda48b6]62 CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indentation, bool infunp )
[7f5566b]63 : indent( *this ), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
[6c4ff37]64 //output << std::string( init );
[51587aa]65 }
66
67 string mangleName( DeclarationWithType *decl ) {
68 if ( decl->get_mangleName() != "" ) {
[f326f99]69 // need to incorporate scope level in order to differentiate names for destructors
70 return decl->get_scopedMangleName();
[51587aa]71 } else {
72 return decl->get_name();
73 } // if
74 }
[94b4364]75
[51587aa]76 //*** Declarations
[6c4ff37]77 void CodeGenerator::visit( FunctionDecl *functionDecl ) {
[51587aa]78 handleStorageClass( functionDecl );
[f38c8d9]79 if ( functionDecl->get_isInline() ) {
[6c4ff37]80 output << "inline ";
[f38c8d9]81 } // if
[de62360d]82 if ( functionDecl->get_isNoreturn() ) {
83 output << "_Noreturn ";
84 } // if
[6c4ff37]85 output << genType( functionDecl->get_functionType(), mangleName( functionDecl ) );
[51587aa]86
87 // how to get this to the Functype?
88 std::list< Declaration * > olds = functionDecl->get_oldDecls();
89 if ( ! olds.empty() ) {
[6c4ff37]90 output << " /* function has old declaration */";
[51587aa]91 } // if
92
93 // acceptAll( functionDecl->get_oldDecls(), *this );
94 if ( functionDecl->get_statements() ) {
[7f5566b]95 functionDecl->get_statements()->accept( *this );
[51587aa]96 } // if
97 }
98
[6c4ff37]99 void CodeGenerator::visit( ObjectDecl *objectDecl ) {
[51587aa]100 handleStorageClass( objectDecl );
[6c4ff37]101 output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
[71f4e4f]102
[51587aa]103 if ( objectDecl->get_init() ) {
[6c4ff37]104 output << " = ";
[51587aa]105 objectDecl->get_init()->accept( *this );
106 } // if
107 if ( objectDecl->get_bitfieldWidth() ) {
[6c4ff37]108 output << ":";
[51587aa]109 objectDecl->get_bitfieldWidth()->accept( *this );
110 } // if
111 }
112
[6c4ff37]113 void CodeGenerator::handleAggregate( AggregateDecl *aggDecl ) {
[51587aa]114 if ( aggDecl->get_name() != "" )
[6c4ff37]115 output << aggDecl->get_name();
[71f4e4f]116
[51587aa]117 std::list< Declaration * > &memb = aggDecl->get_members();
118
119 if ( ! memb.empty() ) {
[94b4364]120 output << " {" << endl;
[51587aa]121
[71f4e4f]122 cur_indent += CodeGenerator::tabsize;
[51587aa]123 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
[71f4e4f]124 output << indent;
[7f5566b]125 (*i)->accept( *this );
[6c4ff37]126 output << ";" << endl;
[51587aa]127 }
128
[71f4e4f]129 cur_indent -= CodeGenerator::tabsize;
[51587aa]130
[cda48b6]131 output << indent << "}";
[17cd4eb]132 } // if
[51587aa]133 }
[17cd4eb]134
[6c4ff37]135 void CodeGenerator::visit( StructDecl *structDecl ) {
136 output << "struct ";
[51587aa]137 handleAggregate( structDecl );
138 }
[17cd4eb]139
[6c4ff37]140 void CodeGenerator::visit( UnionDecl *aggregateDecl ) {
141 output << "union ";
[51587aa]142 handleAggregate( aggregateDecl );
143 }
[71f4e4f]144
[6c4ff37]145 void CodeGenerator::visit( EnumDecl *aggDecl ) {
146 output << "enum ";
[51587aa]147
148 if ( aggDecl->get_name() != "" )
[6c4ff37]149 output << aggDecl->get_name();
[71f4e4f]150
[51587aa]151 std::list< Declaration* > &memb = aggDecl->get_members();
152
153 if ( ! memb.empty() ) {
[cda48b6]154 output << " {" << endl;
[51587aa]155
[71f4e4f]156 cur_indent += CodeGenerator::tabsize;
[51587aa]157 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
158 ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
159 assert( obj );
[71f4e4f]160 output << indent << mangleName( obj );
[51587aa]161 if ( obj->get_init() ) {
[6c4ff37]162 output << " = ";
[7f5566b]163 obj->get_init()->accept( *this );
[51587aa]164 } // if
[6c4ff37]165 output << "," << endl;
[51587aa]166 } // for
167
[71f4e4f]168 cur_indent -= CodeGenerator::tabsize;
[51587aa]169
[cda48b6]170 output << indent << "}";
[51587aa]171 } // if
172 }
[71f4e4f]173
[4040425]174 void CodeGenerator::visit( TraitDecl *aggregateDecl ) {}
[71f4e4f]175
[6c4ff37]176 void CodeGenerator::visit( TypedefDecl *typeDecl ) {
177 output << "typedef ";
178 output << genType( typeDecl->get_base(), typeDecl->get_name() );
[51587aa]179 }
[71f4e4f]180
[6c4ff37]181 void CodeGenerator::visit( TypeDecl *typeDecl ) {
[51587aa]182 // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
183 // still to be done
[6c4ff37]184 output << "extern unsigned long " << typeDecl->get_name();
[51587aa]185 if ( typeDecl->get_base() ) {
[6c4ff37]186 output << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )";
[51587aa]187 } // if
188 }
189
[e45215c]190 void CodeGenerator::printDesignators( std::list< Expression * > & designators ) {
191 typedef std::list< Expression * > DesignatorList;
192 if ( designators.size() == 0 ) return;
193 for ( DesignatorList::iterator iter = designators.begin(); iter != designators.end(); ++iter ) {
[8a4da06]194 if ( dynamic_cast< NameExpr * >( *iter ) ) {
[f32c7f4]195 // if expression is a name, then initializing aggregate member
196 output << ".";
197 (*iter)->accept( *this );
198 } else {
199 // if not a simple name, it has to be a constant expression, i.e. an array designator
[e45215c]200 output << "[";
201 (*iter)->accept( *this );
202 output << "]";
203 }
204 }
205 output << " = ";
206 }
207
[6c4ff37]208 void CodeGenerator::visit( SingleInit *init ) {
[e45215c]209 printDesignators( init->get_designators() );
[51587aa]210 init->get_value()->accept( *this );
211 }
212
[6c4ff37]213 void CodeGenerator::visit( ListInit *init ) {
[e45215c]214 printDesignators( init->get_designators() );
[6c4ff37]215 output << "{ ";
[5b40f30]216 if ( init->begin_initializers() == init->end_initializers() ) {
217 // illegal to leave initializer list empty for scalar initializers,
218 // but always legal to have 0
219 output << "0";
220 } else {
221 genCommaList( init->begin_initializers(), init->end_initializers() );
222 }
[6c4ff37]223 output << " }";
[51587aa]224 }
225
[71f4e4f]226 void CodeGenerator::visit( Constant *constant ) {
[6c4ff37]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 ) ) {
[71f4e4f]243
[51587aa]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 }
[71f4e4f]252
[51587aa]253 default:
254 // do nothing
255 ;
256 }
[71f4e4f]257
[51587aa]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;
[71f4e4f]266
[51587aa]267 case OT_CALL:
[2794fff]268 // there are no intrinsic definitions of the function call operator or constructors or destructors
[51587aa]269 assert( false );
270 break;
[71f4e4f]271
[f1e012b]272 case OT_CTOR:
[a9a259c]273 // it's just an optimization to disallow this, so for now let it through
274 // since it makes autogenerating constructors a lot easier
275 varExpr->accept( *this );
276 output << "(";
277 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
278 output << ")";
279
[f1e012b]280 // intrinsic constructors should never be called directly - they should be transformed back into Initializer nodes
[a9a259c]281 // assert(false);
[f1e012b]282 break;
283
284 case OT_DTOR:
285 // intrinsic destructors do nothing - don't generate any code
[5b2f5bb]286 output << " /* " << dynamic_cast<VariableExpr*>(applicationExpr->get_function())->get_var()->get_name() << " */";
[f1e012b]287 break;
288
[51587aa]289 case OT_PREFIX:
290 case OT_PREFIXASSIGN:
291 assert( applicationExpr->get_args().size() == 1 );
[6c4ff37]292 output << "(";
293 output << opInfo.symbol;
[51587aa]294 (*arg)->accept( *this );
[6c4ff37]295 output << ")";
[51587aa]296 break;
[71f4e4f]297
[51587aa]298 case OT_POSTFIX:
299 case OT_POSTFIXASSIGN:
300 assert( applicationExpr->get_args().size() == 1 );
301 (*arg)->accept( *this );
[6c4ff37]302 output << opInfo.symbol;
[51587aa]303 break;
304
[f1e012b]305
[51587aa]306 case OT_INFIX:
307 case OT_INFIXASSIGN:
308 assert( applicationExpr->get_args().size() == 2 );
[6c4ff37]309 output << "(";
[51587aa]310 (*arg++)->accept( *this );
[6c4ff37]311 output << opInfo.symbol;
[51587aa]312 (*arg)->accept( *this );
[6c4ff37]313 output << ")";
[51587aa]314 break;
[71f4e4f]315
[51587aa]316 case OT_CONSTANT:
[721f17a]317 case OT_LABELADDRESS:
318 // there are no intrinsic definitions of 0/1 or label addresses as functions
[51587aa]319 assert( false );
320 }
[17cd4eb]321 } else {
[51587aa]322 varExpr->accept( *this );
[6c4ff37]323 output << "(";
[51587aa]324 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
[6c4ff37]325 output << ")";
[17cd4eb]326 } // if
[51587aa]327 } else {
328 applicationExpr->get_function()->accept( *this );
[6c4ff37]329 output << "(";
[51587aa]330 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
[6c4ff37]331 output << ")";
[51587aa]332 } // if
333 }
[71f4e4f]334
[6c4ff37]335 void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
[51587aa]336 if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
337 OperatorInfo opInfo;
338 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
339 std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
340 switch ( opInfo.type ) {
341 case OT_INDEX:
342 assert( untypedExpr->get_args().size() == 2 );
343 (*arg++)->accept( *this );
[6c4ff37]344 output << "[";
[51587aa]345 (*arg)->accept( *this );
[6c4ff37]346 output << "]";
[51587aa]347 break;
[71f4e4f]348
[51587aa]349 case OT_CALL:
[f1e012b]350 assert( false );
351
[1840193]352 case OT_CTOR:
353 case OT_DTOR:
[f1e012b]354 // intrinsic constructors should never be called
355 // intrinsic destructors do nothing
[51587aa]356 break;
[71f4e4f]357
[51587aa]358 case OT_PREFIX:
359 case OT_PREFIXASSIGN:
[de62360d]360 case OT_LABELADDRESS:
[51587aa]361 assert( untypedExpr->get_args().size() == 1 );
[6c4ff37]362 output << "(";
363 output << opInfo.symbol;
[51587aa]364 (*arg)->accept( *this );
[6c4ff37]365 output << ")";
[51587aa]366 break;
[71f4e4f]367
[51587aa]368 case OT_POSTFIX:
369 case OT_POSTFIXASSIGN:
370 assert( untypedExpr->get_args().size() == 1 );
371 (*arg)->accept( *this );
[6c4ff37]372 output << opInfo.symbol;
[51587aa]373 break;
[71f4e4f]374
[51587aa]375 case OT_INFIX:
376 case OT_INFIXASSIGN:
377 assert( untypedExpr->get_args().size() == 2 );
[6c4ff37]378 output << "(";
[51587aa]379 (*arg++)->accept( *this );
[6c4ff37]380 output << opInfo.symbol;
[51587aa]381 (*arg)->accept( *this );
[6c4ff37]382 output << ")";
[51587aa]383 break;
[71f4e4f]384
[51587aa]385 case OT_CONSTANT:
386 // there are no intrinsic definitions of 0 or 1 as functions
387 assert( false );
388 }
389 } else {
390 nameExpr->accept( *this );
[6c4ff37]391 output << "(";
[51587aa]392 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
[6c4ff37]393 output << ")";
[51587aa]394 } // if
395 } else {
396 untypedExpr->get_function()->accept( *this );
[6c4ff37]397 output << "(";
[51587aa]398 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
[6c4ff37]399 output << ")";
[51587aa]400 } // if
401 }
[71f4e4f]402
[6c4ff37]403 void CodeGenerator::visit( NameExpr *nameExpr ) {
[51587aa]404 OperatorInfo opInfo;
405 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
406 assert( opInfo.type == OT_CONSTANT );
[6c4ff37]407 output << opInfo.symbol;
[51587aa]408 } else {
[6c4ff37]409 output << nameExpr->get_name();
[51587aa]410 } // if
411 }
[71f4e4f]412
[6c4ff37]413 void CodeGenerator::visit( AddressExpr *addressExpr ) {
414 output << "(&";
[51587aa]415 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
416 if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
[6c4ff37]417 output << mangleName( variableExpr->get_var() );
[51587aa]418 } else {
419 addressExpr->get_arg()->accept( *this );
420 } // if
[6c4ff37]421 output << ")";
[51587aa]422 }
423
[6c4ff37]424 void CodeGenerator::visit( CastExpr *castExpr ) {
[803deb1]425 output << "(";
426 if ( castExpr->get_results().empty() ) {
427 output << "(void)" ;
428 } else if ( ! castExpr->get_results().front()->get_isLvalue() ) {
429 // at least one result type of cast, but not an lvalue
430 output << "(";
431 output << genType( castExpr->get_results().front(), "" );
[71f4e4f]432 output << ")";
[803deb1]433 } else {
434 // otherwise, the cast is to an lvalue type, so the cast
435 // should be dropped, since the result of a cast is
436 // never an lvalue in C
[76b48f1]437 }
[803deb1]438 castExpr->get_arg()->accept( *this );
439 output << ")";
[51587aa]440 }
[71f4e4f]441
[6c4ff37]442 void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
[51587aa]443 assert( false );
444 }
[71f4e4f]445
[6c4ff37]446 void CodeGenerator::visit( MemberExpr *memberExpr ) {
[51587aa]447 memberExpr->get_aggregate()->accept( *this );
[6c4ff37]448 output << "." << mangleName( memberExpr->get_member() );
[51587aa]449 }
[71f4e4f]450
[6c4ff37]451 void CodeGenerator::visit( VariableExpr *variableExpr ) {
[51587aa]452 OperatorInfo opInfo;
453 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
[6c4ff37]454 output << opInfo.symbol;
[51587aa]455 } else {
[6c4ff37]456 output << mangleName( variableExpr->get_var() );
[51587aa]457 } // if
458 }
[71f4e4f]459
[6c4ff37]460 void CodeGenerator::visit( ConstantExpr *constantExpr ) {
[51587aa]461 assert( constantExpr->get_constant() );
462 constantExpr->get_constant()->accept( *this );
463 }
[71f4e4f]464
[6c4ff37]465 void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
466 output << "sizeof(";
[51587aa]467 if ( sizeofExpr->get_isType() ) {
[6c4ff37]468 output << genType( sizeofExpr->get_type(), "" );
[51587aa]469 } else {
470 sizeofExpr->get_expr()->accept( *this );
471 } // if
[6c4ff37]472 output << ")";
[51587aa]473 }
[47534159]474
[25a054f]475 void CodeGenerator::visit( AlignofExpr *alignofExpr ) {
[47534159]476 // use GCC extension to avoid bumping std to C11
477 output << "__alignof__(";
[25a054f]478 if ( alignofExpr->get_isType() ) {
479 output << genType( alignofExpr->get_type(), "" );
[47534159]480 } else {
[25a054f]481 alignofExpr->get_expr()->accept( *this );
[47534159]482 } // if
483 output << ")";
484 }
[71f4e4f]485
[2a4b088]486 void CodeGenerator::visit( UntypedOffsetofExpr *offsetofExpr ) {
487 assert( false );
488 }
489
[25a054f]490 void CodeGenerator::visit( OffsetofExpr *offsetofExpr ) {
491 // use GCC builtin
492 output << "__builtin_offsetof(";
493 output << genType( offsetofExpr->get_type(), "" );
[e551c69]494 output << ", " << mangleName( offsetofExpr->get_member() );
[25a054f]495 output << ")";
496 }
[d63eeb0]497
[6c4ff37]498 void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
499 output << "(";
[51587aa]500 logicalExpr->get_arg1()->accept( *this );
501 if ( logicalExpr->get_isAnd() ) {
[6c4ff37]502 output << " && ";
[51587aa]503 } else {
[6c4ff37]504 output << " || ";
[51587aa]505 } // if
506 logicalExpr->get_arg2()->accept( *this );
[6c4ff37]507 output << ")";
[51587aa]508 }
[71f4e4f]509
[6c4ff37]510 void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
511 output << "(";
[51587aa]512 conditionalExpr->get_arg1()->accept( *this );
[6c4ff37]513 output << " ? ";
[51587aa]514 conditionalExpr->get_arg2()->accept( *this );
[6c4ff37]515 output << " : ";
[51587aa]516 conditionalExpr->get_arg3()->accept( *this );
[6c4ff37]517 output << ")";
[51587aa]518 }
[71f4e4f]519
[6c4ff37]520 void CodeGenerator::visit( CommaExpr *commaExpr ) {
521 output << "(";
[51587aa]522 commaExpr->get_arg1()->accept( *this );
[6c4ff37]523 output << " , ";
[51587aa]524 commaExpr->get_arg2()->accept( *this );
[6c4ff37]525 output << ")";
[51587aa]526 }
[71f4e4f]527
[6c4ff37]528 void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
[71f4e4f]529
[6c4ff37]530 void CodeGenerator::visit( TypeExpr *typeExpr ) {}
[2b6c1e0]531
[7f5566b]532 void CodeGenerator::visit( AsmExpr *asmExpr ) {
533 if ( asmExpr->get_inout() ) {
534 output << "[ ";
535 asmExpr->get_inout()->accept( *this );
536 output << " ] ";
537 } // if
538 asmExpr->get_constraint()->accept( *this );
539 output << " ( ";
540 asmExpr->get_operand()->accept( *this );
541 output << " )";
542 }
543
[51587aa]544 //*** Statements
[6c4ff37]545 void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
[51587aa]546 std::list<Statement*> ks = compoundStmt->get_kids();
[2b6c1e0]547 output << "{" << endl;
[51587aa]548
[2b6c1e0]549 cur_indent += CodeGenerator::tabsize;
[51587aa]550
[7f5566b]551 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) {
[cda48b6]552 output << indent << printLabels( (*i)->get_labels() );
[7f5566b]553 (*i)->accept( *this );
[2b6c1e0]554
[6c4ff37]555 output << endl;
[2b6c1e0]556 if ( wantSpacing( *i ) ) {
557 output << endl;
558 }
[51587aa]559 }
[71f4e4f]560 cur_indent -= CodeGenerator::tabsize;
[51587aa]561
[cda48b6]562 output << indent << "}";
[51587aa]563 }
564
[6c4ff37]565 void CodeGenerator::visit( ExprStmt *exprStmt ) {
[71f4e4f]566 // I don't see why this check is necessary.
567 // If this starts to cause problems then put it back in,
[6c4ff37]568 // with an explanation
569 assert( exprStmt );
570
571 // if ( exprStmt != 0 ) {
572 exprStmt->get_expr()->accept( *this );
573 output << ";" ;
574 // } // if
[51587aa]575 }
576
[7f5566b]577 void CodeGenerator::visit( AsmStmt *asmStmt ) {
578 output << "asm ";
579 if ( asmStmt->get_voltile() ) output << "volatile ";
580 if ( ! asmStmt->get_gotolabels().empty() ) output << "goto ";
581 output << "( ";
582 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
583 output << " : ";
584 genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
585 output << " : ";
586 genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
587 output << " : ";
588 genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
589 if ( ! asmStmt->get_gotolabels().empty() ) {
590 output << " : ";
591 for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
592 output << *begin++;
593 if ( begin == asmStmt->get_gotolabels().end() ) break;
594 output << ", ";
595 } // for
596 } // if
597 output << " );" ;
598 }
599
[6c4ff37]600 void CodeGenerator::visit( IfStmt *ifStmt ) {
[7f5566b]601 output << "if ( ";
602 ifStmt->get_condition()->accept( *this );
603 output << " ) ";
[51587aa]604
[7f5566b]605 ifStmt->get_thenPart()->accept( *this );
[51587aa]606
607 if ( ifStmt->get_elsePart() != 0) {
[2b6c1e0]608 output << " else ";
[7f5566b]609 ifStmt->get_elsePart()->accept( *this );
[51587aa]610 } // if
611 }
612
[6c4ff37]613 void CodeGenerator::visit( SwitchStmt *switchStmt ) {
[7f5566b]614 output << "switch ( " ;
615 switchStmt->get_condition()->accept( *this );
616 output << " ) ";
[71f4e4f]617
[2b6c1e0]618 output << "{" << std::endl;
[6c4ff37]619 cur_indent += CodeGenerator::tabsize;
[51587aa]620
[6c4ff37]621 acceptAll( switchStmt->get_branches(), *this );
[51587aa]622
[6c4ff37]623 cur_indent -= CodeGenerator::tabsize;
[51587aa]624
[cda48b6]625 output << indent << "}";
[51587aa]626 }
627
[6c4ff37]628 void CodeGenerator::visit( CaseStmt *caseStmt ) {
[cda48b6]629 output << indent;
[eb3261f]630 if ( caseStmt->isDefault()) {
[2b6c1e0]631 output << "default";
[eb3261f]632 } else {
[2b6c1e0]633 output << "case ";
[7f5566b]634 caseStmt->get_condition()->accept( *this );
[17cd4eb]635 } // if
[6c4ff37]636 output << ":\n";
[71f4e4f]637
[51587aa]638 std::list<Statement *> sts = caseStmt->get_statements();
639
[6c4ff37]640 cur_indent += CodeGenerator::tabsize;
[51587aa]641 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
[cda48b6]642 output << indent << printLabels( (*i)->get_labels() ) ;
[7f5566b]643 (*i)->accept( *this );
[6c4ff37]644 output << endl;
[51587aa]645 }
[6c4ff37]646 cur_indent -= CodeGenerator::tabsize;
[51587aa]647 }
648
[6c4ff37]649 void CodeGenerator::visit( BranchStmt *branchStmt ) {
[51587aa]650 switch ( branchStmt->get_type()) {
651 case BranchStmt::Goto:
652 if ( ! branchStmt->get_target().empty() )
[6c4ff37]653 output << "goto " << branchStmt->get_target();
[71f4e4f]654 else {
[51587aa]655 if ( branchStmt->get_computedTarget() != 0 ) {
[6c4ff37]656 output << "goto *";
[51587aa]657 branchStmt->get_computedTarget()->accept( *this );
658 } // if
659 } // if
660 break;
661 case BranchStmt::Break:
[6c4ff37]662 output << "break";
[51587aa]663 break;
664 case BranchStmt::Continue:
[6c4ff37]665 output << "continue";
[51587aa]666 break;
667 }
[2b6c1e0]668 output << ";";
[51587aa]669 }
670
671
[6c4ff37]672 void CodeGenerator::visit( ReturnStmt *returnStmt ) {
673 output << "return ";
[51587aa]674
675 // xxx -- check for null expression;
676 if ( returnStmt->get_expr() ) {
677 returnStmt->get_expr()->accept( *this );
678 } // if
[6c4ff37]679 output << ";";
[51587aa]680 }
681
[6c4ff37]682 void CodeGenerator::visit( WhileStmt *whileStmt ) {
[51587aa]683 if ( whileStmt->get_isDoWhile() )
[6c4ff37]684 output << "do" ;
[51587aa]685 else {
[6c4ff37]686 output << "while (" ;
[7f5566b]687 whileStmt->get_condition()->accept( *this );
[6c4ff37]688 output << ")";
[51587aa]689 } // if
[2b6c1e0]690 output << " ";
[51587aa]691
[2b6c1e0]692 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
[51587aa]693 whileStmt->get_body()->accept( *this );
694
[cda48b6]695 output << indent;
[51587aa]696
697 if ( whileStmt->get_isDoWhile() ) {
[6c4ff37]698 output << " while (" ;
[7f5566b]699 whileStmt->get_condition()->accept( *this );
[6c4ff37]700 output << ");";
[51587aa]701 } // if
702 }
703
[6c4ff37]704 void CodeGenerator::visit( ForStmt *forStmt ) {
[71f4e4f]705 // initialization is always hoisted, so don't
706 // bother doing anything with that
[145f1fc]707 output << "for (;";
[51587aa]708
709 if ( forStmt->get_condition() != 0 )
710 forStmt->get_condition()->accept( *this );
[6c4ff37]711 output << ";";
[51587aa]712
713 if ( forStmt->get_increment() != 0 )
714 forStmt->get_increment()->accept( *this );
[2b6c1e0]715 output << ") ";
[51587aa]716
717 if ( forStmt->get_body() != 0 ) {
[2b6c1e0]718 output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
[51587aa]719 forStmt->get_body()->accept( *this );
720 } // if
721 }
722
[6c4ff37]723 void CodeGenerator::visit( NullStmt *nullStmt ) {
[cda48b6]724 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
[6c4ff37]725 output << "/* null statement */ ;";
[51587aa]726 }
727
[6c4ff37]728 void CodeGenerator::visit( DeclStmt *declStmt ) {
[51587aa]729 declStmt->get_decl()->accept( *this );
[71f4e4f]730
[51587aa]731 if ( doSemicolon( declStmt->get_decl() ) ) {
[6c4ff37]732 output << ";";
[51587aa]733 } // if
734 }
735
[6c4ff37]736 std::string CodeGenerator::printLabels( std::list< Label > &l ) {
[51587aa]737 std::string str( "" );
[6c4ff37]738 l.unique(); // assumes a sorted list. Why not use set?
[51587aa]739
740 for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ )
741 str += *i + ": ";
742
743 return str;
744 }
745
[6c4ff37]746 void CodeGenerator::handleStorageClass( Declaration *decl ) {
[51587aa]747 switch ( decl->get_storageClass() ) {
[68cd1ce]748 case DeclarationNode::Extern:
[6c4ff37]749 output << "extern ";
[51587aa]750 break;
[68cd1ce]751 case DeclarationNode::Static:
[6c4ff37]752 output << "static ";
[51587aa]753 break;
[68cd1ce]754 case DeclarationNode::Auto:
[51587aa]755 // silently drop storage class
756 break;
[68cd1ce]757 case DeclarationNode::Register:
[6c4ff37]758 output << "register ";
[51587aa]759 break;
[68cd1ce]760 case DeclarationNode::Inline:
[de62360d]761 output << "inline ";
[f38c8d9]762 break;
[68cd1ce]763 case DeclarationNode::Fortran:
764 output << "fortran ";
765 break;
766 case DeclarationNode::Noreturn:
767 output << "_Noreturn ";
768 break;
769 case DeclarationNode::Threadlocal:
770 output << "_Thread_local ";
771 break;
772 case DeclarationNode::NoStorageClass:
[f38c8d9]773 break;
[843054c2]774 } // switch
[51587aa]775 }
[51b73452]776} // namespace CodeGen
[51587aa]777
778// Local Variables: //
779// tab-width: 4 //
780// mode: c++ //
781// compile-command: "make install" //
782// End: //
Note: See TracBrowser for help on using the repository browser.