source: src/CodeGen/CodeGenerator.cc@ 70d826cd

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 70d826cd was a0c7dc36, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Convert PolyGenericCalculator to PassVisitor and generate size/align variables for opaque type declarations

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