source: src/CodeGen/CodeGenerator.cc@ a1d6d80

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

fix CodeGenerator to output unresolved intrinsic constructor/destructor calls

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