source: src/CodeGen/CodeGenerator.cc@ b826e6b

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox 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 b826e6b was bf2438c, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

Cleaned-up some headers using a tool called 'include-what-you-use'

  • Property mode set to 100644
File size: 31.5 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
[6e300d9]11// Last Modified By : Andrew Beach
[daf1af8]12// Last Modified On : Thu Jun 8 16:00:00 2017
13// Update Count : 485
[51587aa]14//
15
[bf2438c]16#include <cassert> // for assert, assertf
17#include <list> // for _List_iterator, list, list<>::it...
[51b73452]18
[a61fea9a]19#include "CodeGenerator.h"
[bf2438c]20#include "Common/SemanticError.h" // for SemanticError
21#include "Common/UniqueName.h" // for UniqueName
22#include "Common/utility.h" // for CodeLocation, toString
23#include "GenType.h" // for genType
24#include "InitTweak/InitTweak.h" // for getPointerBase
25#include "OperatorTable.h" // for OperatorInfo, operatorLookup
26#include "Parser/LinkageSpec.h" // for Spec, Intrinsic
27#include "SynTree/Attribute.h" // for Attribute
28#include "SynTree/BaseSyntaxNode.h" // for BaseSyntaxNode
29#include "SynTree/Constant.h" // for Constant
30#include "SynTree/Declaration.h" // for DeclarationWithType, TypeDecl
31#include "SynTree/Expression.h" // for Expression, UntypedExpr, Applica...
32#include "SynTree/Initializer.h" // for Initializer, ListInit, Designation
33#include "SynTree/Label.h" // for Label, operator<<
34#include "SynTree/Statement.h" // for Statement, AsmStmt, BranchStmt
35#include "SynTree/Type.h" // for Type, Type::StorageClasses, Func...
[10a7775]36
[51b73452]37using namespace std;
38
39namespace CodeGen {
[6c4ff37]40 int CodeGenerator::tabsize = 4;
[51587aa]41
[145f1fc]42 // the kinds of statements that would ideally be followed by whitespace
[2b6c1e0]43 bool wantSpacing( Statement * stmt) {
44 return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) ||
[08061589]45 dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * >( stmt ) || dynamic_cast< SwitchStmt *>( stmt );
[2b6c1e0]46 }
47
[8688ce1]48 void CodeGenerator::extension( Expression * expr ) {
[8e9cbb2]49 if ( expr->get_extension() ) {
50 output << "__extension__ ";
51 } // if
52 } // extension
53
[8688ce1]54 void CodeGenerator::extension( Declaration * decl ) {
[8e9cbb2]55 if ( decl->get_extension() ) {
56 output << "__extension__ ";
57 } // if
58 } // extension
59
[58dd019]60 void CodeGenerator::asmName( DeclarationWithType * decl ) {
61 if ( ConstantExpr * asmName = decl->get_asmName() ) {
62 output << " asm ( " << asmName->get_constant()->get_value() << " )";
63 } // if
64 } // extension
65
[888cbe4]66 ostream & CodeGenerator::Indenter::operator()( ostream & output ) const {
[cda48b6]67 return output << string( cg.cur_indent, ' ' );
68 }
69
[888cbe4]70 ostream & operator<<( ostream & output, const CodeGenerator::Indenter &indent ) {
[cda48b6]71 return indent( output );
72 }
[51587aa]73
[888cbe4]74 CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) {
75 labels = &l;
76 return *this;
77 }
78
[8688ce1]79 ostream & operator<<( ostream & output, CodeGenerator::LabelPrinter & printLabels ) {
[888cbe4]80 std::list< Label > & labs = *printLabels.labels;
81 // l.unique(); // assumes a sorted list. Why not use set? Does order matter?
82 for ( Label & l : labs ) {
83 output << l.get_name() + ": ";
84 printLabels.cg.genAttributes( l.get_attributes() );
[8688ce1]85 } // for
[888cbe4]86 return output;
87 }
88
[c850687]89 CodeGenerator::LineMarker::LineMarker(
90 CodeLocation const & loc, bool toPrint) :
91 loc(loc), toPrint(toPrint)
92 {}
93
94 CodeGenerator::LineMarker CodeGenerator::lineDirective(
95 BaseSyntaxNode const * node) {
96 return LineMarker(node->location, lineMarks);
97 }
98
99 std::ostream & operator<<(std::ostream & out,
100 CodeGenerator::LineMarker const & marker) {
101 if (marker.toPrint && marker.loc.isSet()) {
102 return out << "\n# " << marker.loc.linenumber << " \""
103 << marker.loc.filename << "\"\n";
104 } else if (marker.toPrint) {
105 return out << "\n/* Missing CodeLocation */\n";
106 } else {
107 return out;
108 }
109 }
110
111 CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {}
[cda48b6]112
[486341f]113 string CodeGenerator::mangleName( DeclarationWithType * decl ) {
[35b1bf4]114 if ( pretty ) return decl->get_name();
[51587aa]115 if ( decl->get_mangleName() != "" ) {
[f326f99]116 // need to incorporate scope level in order to differentiate names for destructors
117 return decl->get_scopedMangleName();
[51587aa]118 } else {
119 return decl->get_name();
120 } // if
121 }
[94b4364]122
[44a81853]123 void CodeGenerator::genAttributes( list< Attribute * > & attributes ) {
124 if ( attributes.empty() ) return;
125 output << "__attribute__ ((";
126 for ( list< Attribute * >::iterator attr( attributes.begin() );; ) {
127 output << (*attr)->get_name();
128 if ( ! (*attr)->get_parameters().empty() ) {
129 output << "(";
130 genCommaList( (*attr)->get_parameters().begin(), (*attr)->get_parameters().end() );
131 output << ")";
132 } // if
133 if ( ++attr == attributes.end() ) break;
134 output << ","; // separator
135 } // for
136 output << ")) ";
137 } // CodeGenerator::genAttributes
[7baed7d]138
139
[4810867]140 // *** Declarations
[8688ce1]141 void CodeGenerator::visit( FunctionDecl * functionDecl ) {
[8e9cbb2]142 extension( functionDecl );
[7baed7d]143 genAttributes( functionDecl->get_attributes() );
144
[51587aa]145 handleStorageClass( functionDecl );
[6e8bd43]146 functionDecl->get_funcSpec().print( output );
[dd020c0]147
[e39241b]148 output << genType( functionDecl->get_functionType(), mangleName( functionDecl ), pretty, genC );
[51587aa]149
[58dd019]150 asmName( functionDecl );
151
[51587aa]152 // acceptAll( functionDecl->get_oldDecls(), *this );
153 if ( functionDecl->get_statements() ) {
[7f5566b]154 functionDecl->get_statements()->accept( *this );
[51587aa]155 } // if
156 }
157
[8688ce1]158 void CodeGenerator::visit( ObjectDecl * objectDecl ) {
[e39241b]159 if (objectDecl->get_name().empty() && genC ) {
160 // only generate an anonymous name when generating C code, otherwise it clutters the output too much
[d9c8a59]161 static UniqueName name = { "__anonymous_object" };
162 objectDecl->set_name( name.newName() );
163 }
164
[8e9cbb2]165 extension( objectDecl );
[f9cebb5]166 genAttributes( objectDecl->get_attributes() );
167
[51587aa]168 handleStorageClass( objectDecl );
[e39241b]169 output << genType( objectDecl->get_type(), mangleName( objectDecl ), pretty, genC );
[71f4e4f]170
[58dd019]171 asmName( objectDecl );
172
[51587aa]173 if ( objectDecl->get_init() ) {
[6c4ff37]174 output << " = ";
[51587aa]175 objectDecl->get_init()->accept( *this );
176 } // if
[3778cb2]177
[51587aa]178 if ( objectDecl->get_bitfieldWidth() ) {
[6c4ff37]179 output << ":";
[51587aa]180 objectDecl->get_bitfieldWidth()->accept( *this );
181 } // if
182 }
183
[5f642e38]184 void CodeGenerator::handleAggregate( AggregateDecl * aggDecl, const std::string & kind ) {
[c0aa336]185 genAttributes( aggDecl->get_attributes() );
[35b1bf4]186
[e39241b]187 if( ! aggDecl->get_parameters().empty() && ! genC ) {
188 // assertf( ! genC, "Aggregate type parameters should not reach code generation." );
189 output << "forall(";
190 genCommaList( aggDecl->get_parameters().begin(), aggDecl->get_parameters().end() );
191 output << ")" << endl;
192 }
193
[29cf9c8]194 output << kind;
[51587aa]195 if ( aggDecl->get_name() != "" )
[6c4ff37]196 output << aggDecl->get_name();
[71f4e4f]197
[2c57025]198 if ( aggDecl->has_body() ) {
199 std::list< Declaration * > & memb = aggDecl->get_members();
[94b4364]200 output << " {" << endl;
[51587aa]201
[71f4e4f]202 cur_indent += CodeGenerator::tabsize;
[3778cb2]203 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) {
[4810867]204 output << lineDirective( *i ) << indent;
[7f5566b]205 (*i)->accept( *this );
[6c4ff37]206 output << ";" << endl;
[3778cb2]207 } // for
[51587aa]208
[71f4e4f]209 cur_indent -= CodeGenerator::tabsize;
[51587aa]210
[cda48b6]211 output << indent << "}";
[17cd4eb]212 } // if
[51587aa]213 }
[17cd4eb]214
[8688ce1]215 void CodeGenerator::visit( StructDecl * structDecl ) {
[8e9cbb2]216 extension( structDecl );
[5f642e38]217 handleAggregate( structDecl, "struct " );
[51587aa]218 }
[17cd4eb]219
[8688ce1]220 void CodeGenerator::visit( UnionDecl * unionDecl ) {
[8e9cbb2]221 extension( unionDecl );
[5f642e38]222 handleAggregate( unionDecl, "union " );
[51587aa]223 }
[71f4e4f]224
[8688ce1]225 void CodeGenerator::visit( EnumDecl * enumDecl ) {
[8e9cbb2]226 extension( enumDecl );
[4810867]227 output << lineDirective ( enumDecl );
[6c4ff37]228 output << "enum ";
[c0aa336]229 genAttributes( enumDecl->get_attributes() );
[51587aa]230
[8e9cbb2]231 if ( enumDecl->get_name() != "" )
232 output << enumDecl->get_name();
[71f4e4f]233
[8e9cbb2]234 std::list< Declaration* > &memb = enumDecl->get_members();
[51587aa]235
236 if ( ! memb.empty() ) {
[cda48b6]237 output << " {" << endl;
[51587aa]238
[71f4e4f]239 cur_indent += CodeGenerator::tabsize;
[51587aa]240 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
[8688ce1]241 ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
[51587aa]242 assert( obj );
[4810867]243 output << lineDirective( obj ) << indent << mangleName( obj );
[51587aa]244 if ( obj->get_init() ) {
[6c4ff37]245 output << " = ";
[7f5566b]246 obj->get_init()->accept( *this );
[51587aa]247 } // if
[6c4ff37]248 output << "," << endl;
[51587aa]249 } // for
250
[71f4e4f]251 cur_indent -= CodeGenerator::tabsize;
[51587aa]252
[cda48b6]253 output << indent << "}";
[51587aa]254 } // if
255 }
[71f4e4f]256
[7e003011]257 void CodeGenerator::visit( __attribute__((unused)) TraitDecl * traitDecl ) {}
[71f4e4f]258
[8688ce1]259 void CodeGenerator::visit( TypedefDecl * typeDecl ) {
[e39241b]260 assertf( ! genC, "Typedefs are removed and substituted in earlier passes." );
[4810867]261 output << lineDirective( typeDecl );
[e39241b]262 output << "typedef ";
263 output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl;
[51587aa]264 }
[71f4e4f]265
[8688ce1]266 void CodeGenerator::visit( TypeDecl * typeDecl ) {
[e39241b]267 if ( genC ) {
268 // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
269 // still to be done
270 extension( typeDecl );
271 output << "extern unsigned long " << typeDecl->get_name();
272 if ( typeDecl->get_base() ) {
273 output << " = sizeof( " << genType( typeDecl->get_base(), "", pretty, genC ) << " )";
274 } // if
275 } else {
[bdd0755]276 output << typeDecl->genTypeString() << " " << typeDecl->get_name();
277 if ( typeDecl->get_kind() != TypeDecl::Any && typeDecl->get_sized() ) {
278 output << " | sized(" << typeDecl->get_name() << ")";
279 }
[e39241b]280 if ( ! typeDecl->get_assertions().empty() ) {
281 output << " | { ";
282 genCommaList( typeDecl->get_assertions().begin(), typeDecl->get_assertions().end() );
283 output << " }";
284 }
285 }
[51587aa]286 }
287
[e4d829b]288 void CodeGenerator::visit( Designation * designation ) {
289 std::list< Expression * > designators = designation->get_designators();
[e45215c]290 if ( designators.size() == 0 ) return;
[e4d829b]291 for ( Expression * des : designators ) {
[62423350]292 if ( dynamic_cast< NameExpr * >( des ) || dynamic_cast< VariableExpr * >( des ) ) {
293 // if expression is a NameExpr or VariableExpr, then initializing aggregate member
294 output << ".";
[e4d829b]295 des->accept( *this );
296 } else {
[62423350]297 // otherwise, it has to be a ConstantExpr or CastExpr, initializing array eleemnt
298 output << "[";
[e4d829b]299 des->accept( *this );
[62423350]300 output << "]";
[3778cb2]301 } // if
302 } // for
[e45215c]303 output << " = ";
304 }
305
[8688ce1]306 void CodeGenerator::visit( SingleInit * init ) {
[51587aa]307 init->get_value()->accept( *this );
308 }
309
[8688ce1]310 void CodeGenerator::visit( ListInit * init ) {
[e4d829b]311 auto initBegin = init->begin();
312 auto initEnd = init->end();
313 auto desigBegin = init->get_designations().begin();
314 auto desigEnd = init->get_designations().end();
315
[6c4ff37]316 output << "{ ";
[e4d829b]317 for ( ; initBegin != initEnd && desigBegin != desigEnd; ) {
318 (*desigBegin)->accept( *this );
319 (*initBegin)->accept( *this );
320 ++initBegin, ++desigBegin;
321 if ( initBegin != initEnd ) {
322 output << ", ";
323 }
324 }
[6c4ff37]325 output << " }";
[e4d829b]326 assertf( initBegin == initEnd && desigBegin == desigEnd, "Initializers and designators not the same length. %s", toString( init ).c_str() );
[51587aa]327 }
328
[7e003011]329 void CodeGenerator::visit( __attribute__((unused)) ConstructorInit * init ){
[e39241b]330 assertf( ! genC, "ConstructorInit nodes should not reach code generation." );
331 // xxx - generate something reasonable for constructor/destructor pairs
332 output << "<ctorinit>";
[fc638d2]333 }
334
[8688ce1]335 void CodeGenerator::visit( Constant * constant ) {
[6c4ff37]336 output << constant->get_value() ;
[51587aa]337 }
338
[4810867]339 // *** Expressions
[8688ce1]340 void CodeGenerator::visit( ApplicationExpr * applicationExpr ) {
[e04ef3a]341 extension( applicationExpr );
[8688ce1]342 if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
[51587aa]343 OperatorInfo opInfo;
344 if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
345 std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
346 switch ( opInfo.type ) {
347 case OT_PREFIXASSIGN:
348 case OT_POSTFIXASSIGN:
349 case OT_INFIXASSIGN:
[356189a]350 case OT_CTOR:
[c2ce2350]351 case OT_DTOR:
[51587aa]352 {
353 assert( arg != applicationExpr->get_args().end() );
[8688ce1]354 if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
[356189a]355 // remove & from first assignment/ctor argument
[51587aa]356 *arg = addrExpr->get_arg();
357 } else {
[356189a]358 // no address-of operator, so must be a pointer - add dereference
[066d77a]359 // NOTE: if the assertion starts to trigger, check that the application expr isn't being shared.
360 // Since its arguments are modified here, this assertion most commonly triggers when the application
361 // is visited multiple times.
[8688ce1]362 UntypedExpr * newExpr = new UntypedExpr( new NameExpr( "*?" ) );
[51587aa]363 newExpr->get_args().push_back( *arg );
[906e24d]364 Type * type = InitTweak::getPointerBase( (*arg)->get_result() );
[066d77a]365 assertf( type, "First argument to a derefence must be a pointer. Ensure that expressions are not being shared." );
[906e24d]366 newExpr->set_result( type->clone() );
[51587aa]367 *arg = newExpr;
368 } // if
369 break;
370 }
[71f4e4f]371
[51587aa]372 default:
373 // do nothing
374 ;
[3778cb2]375 } // switch
[71f4e4f]376
[51587aa]377 switch ( opInfo.type ) {
378 case OT_INDEX:
379 assert( applicationExpr->get_args().size() == 2 );
380 (*arg++)->accept( *this );
[6c4ff37]381 output << "[";
[51587aa]382 (*arg)->accept( *this );
[6c4ff37]383 output << "]";
[51587aa]384 break;
[71f4e4f]385
[51587aa]386 case OT_CALL:
[356189a]387 // there are no intrinsic definitions of the function call operator
[51587aa]388 assert( false );
389 break;
[71f4e4f]390
[f1e012b]391 case OT_CTOR:
[c2ce2350]392 case OT_DTOR:
[356189a]393 if ( applicationExpr->get_args().size() == 1 ) {
[8e9cbb2]394 // the expression fed into a single parameter constructor or destructor may contain side
395 // effects, so must still output this expression
[64071c2]396 output << "(";
[356189a]397 (*arg++)->accept( *this );
[64071c2]398 output << ") /* " << opInfo.inputName << " */";
[356189a]399 } else if ( applicationExpr->get_args().size() == 2 ) {
[c2ce2350]400 // intrinsic two parameter constructors are essentially bitwise assignment
[356189a]401 output << "(";
402 (*arg++)->accept( *this );
403 output << opInfo.symbol;
404 (*arg)->accept( *this );
[c2ce2350]405 output << ") /* " << opInfo.inputName << " */";
[356189a]406 } else {
[c2ce2350]407 // no constructors with 0 or more than 2 parameters
[356189a]408 assert( false );
[8688ce1]409 } // if
[356189a]410 break;
[f1e012b]411
[51587aa]412 case OT_PREFIX:
413 case OT_PREFIXASSIGN:
414 assert( applicationExpr->get_args().size() == 1 );
[6c4ff37]415 output << "(";
416 output << opInfo.symbol;
[51587aa]417 (*arg)->accept( *this );
[6c4ff37]418 output << ")";
[51587aa]419 break;
[71f4e4f]420
[51587aa]421 case OT_POSTFIX:
422 case OT_POSTFIXASSIGN:
423 assert( applicationExpr->get_args().size() == 1 );
424 (*arg)->accept( *this );
[6c4ff37]425 output << opInfo.symbol;
[51587aa]426 break;
427
[f1e012b]428
[51587aa]429 case OT_INFIX:
430 case OT_INFIXASSIGN:
431 assert( applicationExpr->get_args().size() == 2 );
[6c4ff37]432 output << "(";
[51587aa]433 (*arg++)->accept( *this );
[6c4ff37]434 output << opInfo.symbol;
[51587aa]435 (*arg)->accept( *this );
[6c4ff37]436 output << ")";
[51587aa]437 break;
[71f4e4f]438
[51587aa]439 case OT_CONSTANT:
[721f17a]440 case OT_LABELADDRESS:
441 // there are no intrinsic definitions of 0/1 or label addresses as functions
[51587aa]442 assert( false );
[3778cb2]443 } // switch
[17cd4eb]444 } else {
[51587aa]445 varExpr->accept( *this );
[6c4ff37]446 output << "(";
[51587aa]447 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
[6c4ff37]448 output << ")";
[17cd4eb]449 } // if
[51587aa]450 } else {
451 applicationExpr->get_function()->accept( *this );
[6c4ff37]452 output << "(";
[51587aa]453 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
[6c4ff37]454 output << ")";
[51587aa]455 } // if
456 }
[71f4e4f]457
[8688ce1]458 void CodeGenerator::visit( UntypedExpr * untypedExpr ) {
[e04ef3a]459 extension( untypedExpr );
[8688ce1]460 if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
[51587aa]461 OperatorInfo opInfo;
462 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
463 std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
464 switch ( opInfo.type ) {
465 case OT_INDEX:
466 assert( untypedExpr->get_args().size() == 2 );
467 (*arg++)->accept( *this );
[6c4ff37]468 output << "[";
[51587aa]469 (*arg)->accept( *this );
[6c4ff37]470 output << "]";
[51587aa]471 break;
[71f4e4f]472
[51587aa]473 case OT_CALL:
[f1e012b]474 assert( false );
475
[c2ce2350]476 case OT_CTOR:
477 case OT_DTOR:
478 if ( untypedExpr->get_args().size() == 1 ) {
[8e9cbb2]479 // the expression fed into a single parameter constructor or destructor may contain side
480 // effects, so must still output this expression
[64071c2]481 output << "(";
[c2ce2350]482 (*arg++)->accept( *this );
[64071c2]483 output << ") /* " << opInfo.inputName << " */";
[c2ce2350]484 } else if ( untypedExpr->get_args().size() == 2 ) {
485 // intrinsic two parameter constructors are essentially bitwise assignment
486 output << "(";
487 (*arg++)->accept( *this );
488 output << opInfo.symbol;
489 (*arg)->accept( *this );
490 output << ") /* " << opInfo.inputName << " */";
491 } else {
492 // no constructors with 0 or more than 2 parameters
493 assert( false );
[3778cb2]494 } // if
[51587aa]495 break;
[71f4e4f]496
[51587aa]497 case OT_PREFIX:
498 case OT_PREFIXASSIGN:
[de62360d]499 case OT_LABELADDRESS:
[51587aa]500 assert( untypedExpr->get_args().size() == 1 );
[6c4ff37]501 output << "(";
502 output << opInfo.symbol;
[51587aa]503 (*arg)->accept( *this );
[6c4ff37]504 output << ")";
[51587aa]505 break;
[71f4e4f]506
[51587aa]507 case OT_POSTFIX:
508 case OT_POSTFIXASSIGN:
509 assert( untypedExpr->get_args().size() == 1 );
510 (*arg)->accept( *this );
[6c4ff37]511 output << opInfo.symbol;
[51587aa]512 break;
[71f4e4f]513
[51587aa]514 case OT_INFIX:
515 case OT_INFIXASSIGN:
516 assert( untypedExpr->get_args().size() == 2 );
[6c4ff37]517 output << "(";
[51587aa]518 (*arg++)->accept( *this );
[6c4ff37]519 output << opInfo.symbol;
[51587aa]520 (*arg)->accept( *this );
[6c4ff37]521 output << ")";
[51587aa]522 break;
[71f4e4f]523
[51587aa]524 case OT_CONSTANT:
525 // there are no intrinsic definitions of 0 or 1 as functions
526 assert( false );
[3778cb2]527 } // switch
[51587aa]528 } else {
[8688ce1]529 if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2
[ac911f4]530 assert( untypedExpr->get_args().size() == 2 );
531 (*untypedExpr->get_args().begin())->accept( *this );
532 output << " ... ";
533 (*--untypedExpr->get_args().end())->accept( *this );
[057b34f]534 } else { // builtin routines
535 nameExpr->accept( *this );
536 output << "(";
537 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
538 output << ")";
[66d12f7]539 } // if
[51587aa]540 } // if
541 } else {
542 untypedExpr->get_function()->accept( *this );
[6c4ff37]543 output << "(";
[51587aa]544 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
[6c4ff37]545 output << ")";
[51587aa]546 } // if
547 }
[71f4e4f]548
[064e3ff]549 void CodeGenerator::visit( RangeExpr * rangeExpr ) {
550 rangeExpr->get_low()->accept( *this );
551 output << " ... ";
552 rangeExpr->get_high()->accept( *this );
553 }
554
[8688ce1]555 void CodeGenerator::visit( NameExpr * nameExpr ) {
[e04ef3a]556 extension( nameExpr );
[51587aa]557 OperatorInfo opInfo;
558 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
559 assert( opInfo.type == OT_CONSTANT );
[6c4ff37]560 output << opInfo.symbol;
[51587aa]561 } else {
[6c4ff37]562 output << nameExpr->get_name();
[51587aa]563 } // if
564 }
[71f4e4f]565
[8688ce1]566 void CodeGenerator::visit( AddressExpr * addressExpr ) {
[e04ef3a]567 extension( addressExpr );
[6c4ff37]568 output << "(&";
[51587aa]569 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
[8688ce1]570 if ( VariableExpr * variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
[6c4ff37]571 output << mangleName( variableExpr->get_var() );
[51587aa]572 } else {
573 addressExpr->get_arg()->accept( *this );
574 } // if
[6c4ff37]575 output << ")";
[51587aa]576 }
577
[8688ce1]578 void CodeGenerator::visit( CastExpr * castExpr ) {
[e04ef3a]579 extension( castExpr );
[803deb1]580 output << "(";
[906e24d]581 if ( castExpr->get_result()->isVoid() ) {
[803deb1]582 output << "(void)" ;
[615a096]583 } else if ( ! castExpr->get_result()->get_lvalue() ) {
[803deb1]584 // at least one result type of cast, but not an lvalue
585 output << "(";
[e39241b]586 output << genType( castExpr->get_result(), "", pretty, genC );
[71f4e4f]587 output << ")";
[803deb1]588 } else {
[8e9cbb2]589 // otherwise, the cast is to an lvalue type, so the cast should be dropped, since the result of a cast is
[803deb1]590 // never an lvalue in C
[3778cb2]591 } // if
[803deb1]592 castExpr->get_arg()->accept( *this );
593 output << ")";
[51587aa]594 }
[71f4e4f]595
[8688ce1]596 void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {
[e39241b]597 assertf( ! genC, "UntypedMemberExpr should not reach code generation." );
598 extension( memberExpr );
599 memberExpr->get_aggregate()->accept( *this );
[5f642e38]600 output << ".";
601 memberExpr->get_member()->accept( *this );
[51587aa]602 }
[71f4e4f]603
[8688ce1]604 void CodeGenerator::visit( MemberExpr * memberExpr ) {
[e04ef3a]605 extension( memberExpr );
[51587aa]606 memberExpr->get_aggregate()->accept( *this );
[6c4ff37]607 output << "." << mangleName( memberExpr->get_member() );
[51587aa]608 }
[71f4e4f]609
[8688ce1]610 void CodeGenerator::visit( VariableExpr * variableExpr ) {
[e04ef3a]611 extension( variableExpr );
[51587aa]612 OperatorInfo opInfo;
613 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
[6c4ff37]614 output << opInfo.symbol;
[51587aa]615 } else {
[6c4ff37]616 output << mangleName( variableExpr->get_var() );
[51587aa]617 } // if
618 }
[71f4e4f]619
[8688ce1]620 void CodeGenerator::visit( ConstantExpr * constantExpr ) {
[51587aa]621 assert( constantExpr->get_constant() );
[e04ef3a]622 extension( constantExpr );
[51587aa]623 constantExpr->get_constant()->accept( *this );
624 }
[71f4e4f]625
[8688ce1]626 void CodeGenerator::visit( SizeofExpr * sizeofExpr ) {
[e04ef3a]627 extension( sizeofExpr );
[6c4ff37]628 output << "sizeof(";
[51587aa]629 if ( sizeofExpr->get_isType() ) {
[e39241b]630 output << genType( sizeofExpr->get_type(), "", pretty, genC );
[51587aa]631 } else {
632 sizeofExpr->get_expr()->accept( *this );
633 } // if
[6c4ff37]634 output << ")";
[51587aa]635 }
[47534159]636
[8688ce1]637 void CodeGenerator::visit( AlignofExpr * alignofExpr ) {
[47534159]638 // use GCC extension to avoid bumping std to C11
[8e9cbb2]639 extension( alignofExpr );
[47534159]640 output << "__alignof__(";
[25a054f]641 if ( alignofExpr->get_isType() ) {
[e39241b]642 output << genType( alignofExpr->get_type(), "", pretty, genC );
[47534159]643 } else {
[25a054f]644 alignofExpr->get_expr()->accept( *this );
[47534159]645 } // if
646 output << ")";
647 }
[71f4e4f]648
[8688ce1]649 void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) {
[e39241b]650 assertf( ! genC, "UntypedOffsetofExpr should not reach code generation." );
651 output << "offsetof(";
652 output << genType( offsetofExpr->get_type(), "", pretty, genC );
653 output << ", " << offsetofExpr->get_member();
654 output << ")";
[2a4b088]655 }
656
[8688ce1]657 void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) {
[25a054f]658 // use GCC builtin
659 output << "__builtin_offsetof(";
[e39241b]660 output << genType( offsetofExpr->get_type(), "", pretty, genC );
[e551c69]661 output << ", " << mangleName( offsetofExpr->get_member() );
[25a054f]662 output << ")";
663 }
[d63eeb0]664
[8688ce1]665 void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) {
[e39241b]666 assertf( ! genC, "OffsetPackExpr should not reach code generation." );
667 output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", pretty, genC ) << ")";
[afc1045]668 }
[70a06f6]669
[8688ce1]670 void CodeGenerator::visit( LogicalExpr * logicalExpr ) {
[e04ef3a]671 extension( logicalExpr );
[6c4ff37]672 output << "(";
[51587aa]673 logicalExpr->get_arg1()->accept( *this );
674 if ( logicalExpr->get_isAnd() ) {
[6c4ff37]675 output << " && ";
[51587aa]676 } else {
[6c4ff37]677 output << " || ";
[51587aa]678 } // if
679 logicalExpr->get_arg2()->accept( *this );
[6c4ff37]680 output << ")";
[51587aa]681 }
[71f4e4f]682
[8688ce1]683 void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) {
[e04ef3a]684 extension( conditionalExpr );
[6c4ff37]685 output << "(";
[51587aa]686 conditionalExpr->get_arg1()->accept( *this );
[6c4ff37]687 output << " ? ";
[51587aa]688 conditionalExpr->get_arg2()->accept( *this );
[6c4ff37]689 output << " : ";
[51587aa]690 conditionalExpr->get_arg3()->accept( *this );
[6c4ff37]691 output << ")";
[51587aa]692 }
[71f4e4f]693
[8688ce1]694 void CodeGenerator::visit( CommaExpr * commaExpr ) {
[e04ef3a]695 extension( commaExpr );
[6c4ff37]696 output << "(";
[51587aa]697 commaExpr->get_arg1()->accept( *this );
[6c4ff37]698 output << " , ";
[51587aa]699 commaExpr->get_arg2()->accept( *this );
[6c4ff37]700 output << ")";
[51587aa]701 }
[71f4e4f]702
[e39241b]703 void CodeGenerator::visit( UntypedTupleExpr * tupleExpr ) {
704 assertf( ! genC, "UntypedTupleExpr should not reach code generation." );
[f975c65]705 extension( tupleExpr );
[e39241b]706 output << "[";
707 genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() );
708 output << "]";
709 }
[907eccb]710
[e39241b]711 void CodeGenerator::visit( TupleExpr * tupleExpr ) {
712 assertf( ! genC, "TupleExpr should not reach code generation." );
[f975c65]713 extension( tupleExpr );
[e39241b]714 output << "[";
715 genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() );
716 output << "]";
717 }
[71f4e4f]718
[f975c65]719 void CodeGenerator::visit( TupleIndexExpr * tupleExpr ) {
720 assertf( ! genC, "TupleIndexExpr should not reach code generation." );
721 extension( tupleExpr );
722 tupleExpr->get_tuple()->accept( *this );
723 output << "." << tupleExpr->get_index();
724 }
725
[e39241b]726 void CodeGenerator::visit( TypeExpr * typeExpr ) {
[e4d829b]727 // if ( genC ) std::cerr << "typeexpr still exists: " << typeExpr << std::endl;
728 // assertf( ! genC, "TypeExpr should not reach code generation." );
729 if ( ! genC ) {
730 output<< genType( typeExpr->get_type(), "", pretty, genC );
731 }
[e39241b]732 }
[2b6c1e0]733
[8688ce1]734 void CodeGenerator::visit( AsmExpr * asmExpr ) {
[7f5566b]735 if ( asmExpr->get_inout() ) {
736 output << "[ ";
737 asmExpr->get_inout()->accept( *this );
738 output << " ] ";
739 } // if
740 asmExpr->get_constraint()->accept( *this );
741 output << " ( ";
742 asmExpr->get_operand()->accept( *this );
743 output << " )";
744 }
745
[3c13c03]746 void CodeGenerator::visit( CompoundLiteralExpr *compLitExpr ) {
[fbcde64]747 assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) );
[e39241b]748 output << "(" << genType( compLitExpr->get_result(), "", pretty, genC ) << ")";
[3c13c03]749 compLitExpr->get_initializer()->accept( *this );
750 }
751
[6eb8948]752 void CodeGenerator::visit( StmtExpr * stmtExpr ) {
[3c13c03]753 std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids();
[4810867]754 output << lineDirective( stmtExpr) << "({" << std::endl;
[3c13c03]755 cur_indent += CodeGenerator::tabsize;
756 unsigned int numStmts = stmts.size();
757 unsigned int i = 0;
758 for ( Statement * stmt : stmts ) {
[4810867]759 output << lineDirective( stmt ) << indent;
760 output << printLabels( stmt->get_labels() );
[3c13c03]761 if ( i+1 == numStmts ) {
762 // last statement in a statement expression needs to be handled specially -
763 // cannot cast to void, otherwise the expression statement has no value
764 if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) {
765 exprStmt->get_expr()->accept( *this );
766 output << ";" << endl;
767 ++i;
768 break;
769 }
770 }
771 stmt->accept( *this );
772 output << endl;
773 if ( wantSpacing( stmt ) ) {
774 output << endl;
775 } // if
776 ++i;
777 }
778 cur_indent -= CodeGenerator::tabsize;
779 output << indent << "})";
[6eb8948]780 }
781
[4810867]782 // *** Statements
[8688ce1]783 void CodeGenerator::visit( CompoundStmt * compoundStmt ) {
[51587aa]784 std::list<Statement*> ks = compoundStmt->get_kids();
[2b6c1e0]785 output << "{" << endl;
[51587aa]786
[2b6c1e0]787 cur_indent += CodeGenerator::tabsize;
[51587aa]788
[7f5566b]789 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) {
[cda48b6]790 output << indent << printLabels( (*i)->get_labels() );
[7f5566b]791 (*i)->accept( *this );
[2b6c1e0]792
[6c4ff37]793 output << endl;
[2b6c1e0]794 if ( wantSpacing( *i ) ) {
795 output << endl;
[3778cb2]796 } // if
[8688ce1]797 } // for
[71f4e4f]798 cur_indent -= CodeGenerator::tabsize;
[51587aa]799
[cda48b6]800 output << indent << "}";
[51587aa]801 }
802
[8688ce1]803 void CodeGenerator::visit( ExprStmt * exprStmt ) {
[6c4ff37]804 assert( exprStmt );
[262f085f]805 Expression * expr = exprStmt->get_expr();
806 if ( genC ) {
807 // cast the top-level expression to void to reduce gcc warnings.
808 expr = new CastExpr( expr );
809 }
[321a2481]810 expr->accept( *this );
811 output << ";";
[51587aa]812 }
813
[8688ce1]814 void CodeGenerator::visit( AsmStmt * asmStmt ) {
[7f5566b]815 output << "asm ";
816 if ( asmStmt->get_voltile() ) output << "volatile ";
817 if ( ! asmStmt->get_gotolabels().empty() ) output << "goto ";
818 output << "( ";
819 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
820 output << " : ";
821 genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
822 output << " : ";
823 genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
824 output << " : ";
825 genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
826 if ( ! asmStmt->get_gotolabels().empty() ) {
827 output << " : ";
828 for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
829 output << *begin++;
830 if ( begin == asmStmt->get_gotolabels().end() ) break;
831 output << ", ";
832 } // for
833 } // if
834 output << " );" ;
835 }
836
[e994912]837 void CodeGenerator::visit( AsmDecl * asmDecl ) {
838 output << "asm ";
839 AsmStmt * asmStmt = asmDecl->get_stmt();
840 output << "( ";
841 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
842 output << " )" ;
843 }
844
[8688ce1]845 void CodeGenerator::visit( IfStmt * ifStmt ) {
[4810867]846 output << lineDirective( ifStmt );
[7f5566b]847 output << "if ( ";
848 ifStmt->get_condition()->accept( *this );
849 output << " ) ";
[51587aa]850
[7f5566b]851 ifStmt->get_thenPart()->accept( *this );
[51587aa]852
853 if ( ifStmt->get_elsePart() != 0) {
[2b6c1e0]854 output << " else ";
[7f5566b]855 ifStmt->get_elsePart()->accept( *this );
[51587aa]856 } // if
857 }
858
[8688ce1]859 void CodeGenerator::visit( SwitchStmt * switchStmt ) {
[4810867]860 output << lineDirective( switchStmt );
[7f5566b]861 output << "switch ( " ;
862 switchStmt->get_condition()->accept( *this );
863 output << " ) ";
[71f4e4f]864
[2b6c1e0]865 output << "{" << std::endl;
[6c4ff37]866 cur_indent += CodeGenerator::tabsize;
[8688ce1]867 acceptAll( switchStmt->get_statements(), *this );
[6c4ff37]868 cur_indent -= CodeGenerator::tabsize;
[cda48b6]869 output << indent << "}";
[51587aa]870 }
871
[8688ce1]872 void CodeGenerator::visit( CaseStmt * caseStmt ) {
[4810867]873 output << lineDirective( caseStmt );
[cda48b6]874 output << indent;
[eb3261f]875 if ( caseStmt->isDefault()) {
[2b6c1e0]876 output << "default";
[eb3261f]877 } else {
[2b6c1e0]878 output << "case ";
[7f5566b]879 caseStmt->get_condition()->accept( *this );
[17cd4eb]880 } // if
[6c4ff37]881 output << ":\n";
[71f4e4f]882
[51587aa]883 std::list<Statement *> sts = caseStmt->get_statements();
884
[6c4ff37]885 cur_indent += CodeGenerator::tabsize;
[51587aa]886 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
[cda48b6]887 output << indent << printLabels( (*i)->get_labels() ) ;
[7f5566b]888 (*i)->accept( *this );
[6c4ff37]889 output << endl;
[3778cb2]890 } // for
[6c4ff37]891 cur_indent -= CodeGenerator::tabsize;
[51587aa]892 }
893
[8688ce1]894 void CodeGenerator::visit( BranchStmt * branchStmt ) {
[51587aa]895 switch ( branchStmt->get_type()) {
896 case BranchStmt::Goto:
897 if ( ! branchStmt->get_target().empty() )
[6c4ff37]898 output << "goto " << branchStmt->get_target();
[71f4e4f]899 else {
[51587aa]900 if ( branchStmt->get_computedTarget() != 0 ) {
[6c4ff37]901 output << "goto *";
[51587aa]902 branchStmt->get_computedTarget()->accept( *this );
903 } // if
904 } // if
905 break;
906 case BranchStmt::Break:
[6c4ff37]907 output << "break";
[51587aa]908 break;
909 case BranchStmt::Continue:
[6c4ff37]910 output << "continue";
[51587aa]911 break;
[3778cb2]912 } // switch
[2b6c1e0]913 output << ";";
[51587aa]914 }
915
[8688ce1]916 void CodeGenerator::visit( ReturnStmt * returnStmt ) {
[6c4ff37]917 output << "return ";
[4b2589a]918 maybeAccept( returnStmt->get_expr(), *this );
[6c4ff37]919 output << ";";
[51587aa]920 }
921
[daf1af8]922 void CodeGenerator::visit( ThrowStmt * throwStmt ) {
923 assertf( ! genC, "Throw statements should not reach code generation." );
924
925 output << ((throwStmt->get_kind() == ThrowStmt::Terminate) ?
926 "throw" : "throwResume");
927 if (throwStmt->get_expr()) {
928 output << " ";
929 throwStmt->get_expr()->accept( *this );
930 }
931 if (throwStmt->get_target()) {
932 output << " _At ";
933 throwStmt->get_target()->accept( *this );
934 }
935 output << ";";
936 }
937
[8688ce1]938 void CodeGenerator::visit( WhileStmt * whileStmt ) {
[321a2481]939 if ( whileStmt->get_isDoWhile() ) {
[6c4ff37]940 output << "do" ;
[321a2481]941 } else {
[6c4ff37]942 output << "while (" ;
[7f5566b]943 whileStmt->get_condition()->accept( *this );
[6c4ff37]944 output << ")";
[51587aa]945 } // if
[2b6c1e0]946 output << " ";
[51587aa]947
[2b6c1e0]948 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
[51587aa]949 whileStmt->get_body()->accept( *this );
950
[cda48b6]951 output << indent;
[51587aa]952
953 if ( whileStmt->get_isDoWhile() ) {
[6c4ff37]954 output << " while (" ;
[7f5566b]955 whileStmt->get_condition()->accept( *this );
[6c4ff37]956 output << ");";
[51587aa]957 } // if
958 }
959
[8688ce1]960 void CodeGenerator::visit( ForStmt * forStmt ) {
[8e9cbb2]961 // initialization is always hoisted, so don't bother doing anything with that
[145f1fc]962 output << "for (;";
[51587aa]963
[321a2481]964 if ( forStmt->get_condition() != 0 ) {
[51587aa]965 forStmt->get_condition()->accept( *this );
[3778cb2]966 } // if
[6c4ff37]967 output << ";";
[51587aa]968
[321a2481]969 if ( forStmt->get_increment() != 0 ) {
970 // cast the top-level expression to void to reduce gcc warnings.
971 Expression * expr = new CastExpr( forStmt->get_increment() );
972 expr->accept( *this );
[3778cb2]973 } // if
[2b6c1e0]974 output << ") ";
[51587aa]975
976 if ( forStmt->get_body() != 0 ) {
[2b6c1e0]977 output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
[51587aa]978 forStmt->get_body()->accept( *this );
979 } // if
980 }
981
[7e003011]982 void CodeGenerator::visit( __attribute__((unused)) NullStmt * nullStmt ) {
[cda48b6]983 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
[6c4ff37]984 output << "/* null statement */ ;";
[51587aa]985 }
986
[8688ce1]987 void CodeGenerator::visit( DeclStmt * declStmt ) {
[51587aa]988 declStmt->get_decl()->accept( *this );
[71f4e4f]989
[51587aa]990 if ( doSemicolon( declStmt->get_decl() ) ) {
[6c4ff37]991 output << ";";
[51587aa]992 } // if
993 }
994
[dd020c0]995 void CodeGenerator::handleStorageClass( DeclarationWithType * decl ) {
[fb04321]996 if ( decl->get_storageClasses().any() ) {
[6e8bd43]997 decl->get_storageClasses().print( output );
[a7c90d4]998 } // if
[dd020c0]999 } // CodeGenerator::handleStorageClass
[9facf3b]1000
1001 std::string genName( DeclarationWithType * decl ) {
1002 CodeGen::OperatorInfo opInfo;
1003 if ( operatorLookup( decl->get_name(), opInfo ) ) {
1004 return opInfo.outputName;
1005 } else {
1006 return decl->get_name();
1007 } // if
1008 }
[51b73452]1009} // namespace CodeGen
[51587aa]1010
1011// Local Variables: //
1012// tab-width: 4 //
1013// mode: c++ //
1014// compile-command: "make install" //
1015// End: //
Note: See TracBrowser for help on using the repository browser.