source: src/CodeGen/CodeGenerator.cc@ dc2e7e0

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

output intrinsic constructor expressions as void expression or C assignment, don't generate field constructor which takes unnamed bitfield as a parameter

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