source: src/CodeGen/CodeGenerator.cc@ e04ef3a

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 e04ef3a was e04ef3a, checked in by Peter A. Buhr <pabuhr@…>, 10 years ago

add gcc extension, first attempt, not done yet

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