source: src/CodeGen/CodeGenerator.cc@ ec55ed5

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 ec55ed5 was 8135d4c, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Merge branch 'master' into references

  • Property mode set to 100644
File size: 30.9 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
[29cf9c8]197 output << kind;
[51587aa]198 if ( aggDecl->get_name() != "" )
[6c4ff37]199 output << aggDecl->get_name();
[71f4e4f]200
[2c57025]201 if ( aggDecl->has_body() ) {
202 std::list< Declaration * > & memb = aggDecl->get_members();
[94b4364]203 output << " {" << endl;
[51587aa]204
[f7cb0bc]205 ++indent;
[3778cb2]206 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) {
[8bafacc]207 updateLocation( *i );
[7f5566b]208 (*i)->accept( *this );
[6c4ff37]209 output << ";" << endl;
[3778cb2]210 } // for
[51587aa]211
[f7cb0bc]212 --indent;
[51587aa]213
[cda48b6]214 output << indent << "}";
[17cd4eb]215 } // if
[51587aa]216 }
[17cd4eb]217
[8688ce1]218 void CodeGenerator::visit( StructDecl * structDecl ) {
[8e9cbb2]219 extension( structDecl );
[5f642e38]220 handleAggregate( structDecl, "struct " );
[51587aa]221 }
[17cd4eb]222
[8688ce1]223 void CodeGenerator::visit( UnionDecl * unionDecl ) {
[8e9cbb2]224 extension( unionDecl );
[5f642e38]225 handleAggregate( unionDecl, "union " );
[51587aa]226 }
[71f4e4f]227
[8688ce1]228 void CodeGenerator::visit( EnumDecl * enumDecl ) {
[8e9cbb2]229 extension( enumDecl );
[8bafacc]230 updateLocation( enumDecl );
[6c4ff37]231 output << "enum ";
[c0aa336]232 genAttributes( enumDecl->get_attributes() );
[51587aa]233
[8e9cbb2]234 if ( enumDecl->get_name() != "" )
235 output << enumDecl->get_name();
[71f4e4f]236
[8e9cbb2]237 std::list< Declaration* > &memb = enumDecl->get_members();
[51587aa]238
239 if ( ! memb.empty() ) {
[cda48b6]240 output << " {" << endl;
[51587aa]241
[f7cb0bc]242 ++indent;
[51587aa]243 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
[8688ce1]244 ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
[51587aa]245 assert( obj );
[8bafacc]246 updateLocation( obj );
247 output << mangleName( obj );
[51587aa]248 if ( obj->get_init() ) {
[6c4ff37]249 output << " = ";
[7f5566b]250 obj->get_init()->accept( *this );
[51587aa]251 } // if
[6c4ff37]252 output << "," << endl;
[51587aa]253 } // for
254
[f7cb0bc]255 --indent;
[51587aa]256
[cda48b6]257 output << indent << "}";
[51587aa]258 } // if
259 }
[71f4e4f]260
[7e003011]261 void CodeGenerator::visit( __attribute__((unused)) TraitDecl * traitDecl ) {}
[71f4e4f]262
[8688ce1]263 void CodeGenerator::visit( TypedefDecl * typeDecl ) {
[e39241b]264 assertf( ! genC, "Typedefs are removed and substituted in earlier passes." );
[8bafacc]265 updateLocation( typeDecl );
[e39241b]266 output << "typedef ";
267 output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl;
[51587aa]268 }
[71f4e4f]269
[8688ce1]270 void CodeGenerator::visit( TypeDecl * typeDecl ) {
[e39241b]271 if ( genC ) {
272 // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
273 // still to be done
274 extension( typeDecl );
275 output << "extern unsigned long " << typeDecl->get_name();
276 if ( typeDecl->get_base() ) {
277 output << " = sizeof( " << genType( typeDecl->get_base(), "", pretty, genC ) << " )";
278 } // if
279 } else {
[bdd0755]280 output << typeDecl->genTypeString() << " " << typeDecl->get_name();
281 if ( typeDecl->get_kind() != TypeDecl::Any && typeDecl->get_sized() ) {
282 output << " | sized(" << typeDecl->get_name() << ")";
283 }
[e39241b]284 if ( ! typeDecl->get_assertions().empty() ) {
285 output << " | { ";
286 genCommaList( typeDecl->get_assertions().begin(), typeDecl->get_assertions().end() );
287 output << " }";
288 }
289 }
[51587aa]290 }
291
[e4d829b]292 void CodeGenerator::visit( Designation * designation ) {
293 std::list< Expression * > designators = designation->get_designators();
[e45215c]294 if ( designators.size() == 0 ) return;
[e4d829b]295 for ( Expression * des : designators ) {
[62423350]296 if ( dynamic_cast< NameExpr * >( des ) || dynamic_cast< VariableExpr * >( des ) ) {
297 // if expression is a NameExpr or VariableExpr, then initializing aggregate member
298 output << ".";
[e4d829b]299 des->accept( *this );
300 } else {
[62423350]301 // otherwise, it has to be a ConstantExpr or CastExpr, initializing array eleemnt
302 output << "[";
[e4d829b]303 des->accept( *this );
[62423350]304 output << "]";
[3778cb2]305 } // if
306 } // for
[e45215c]307 output << " = ";
308 }
309
[8688ce1]310 void CodeGenerator::visit( SingleInit * init ) {
[51587aa]311 init->get_value()->accept( *this );
312 }
313
[8688ce1]314 void CodeGenerator::visit( ListInit * init ) {
[e4d829b]315 auto initBegin = init->begin();
316 auto initEnd = init->end();
317 auto desigBegin = init->get_designations().begin();
318 auto desigEnd = init->get_designations().end();
319
[6c4ff37]320 output << "{ ";
[e4d829b]321 for ( ; initBegin != initEnd && desigBegin != desigEnd; ) {
322 (*desigBegin)->accept( *this );
323 (*initBegin)->accept( *this );
324 ++initBegin, ++desigBegin;
325 if ( initBegin != initEnd ) {
326 output << ", ";
327 }
328 }
[6c4ff37]329 output << " }";
[e4d829b]330 assertf( initBegin == initEnd && desigBegin == desigEnd, "Initializers and designators not the same length. %s", toString( init ).c_str() );
[51587aa]331 }
332
[7e003011]333 void CodeGenerator::visit( __attribute__((unused)) ConstructorInit * init ){
[e39241b]334 assertf( ! genC, "ConstructorInit nodes should not reach code generation." );
[e6cee92]335 // pseudo-output for constructor/destructor pairs
336 output << "<ctorinit>{" << std::endl << ++indent << "ctor: ";
337 maybeAccept( init->get_ctor(), *this );
338 output << ", " << std::endl << indent << "dtor: ";
339 maybeAccept( init->get_dtor(), *this );
340 output << std::endl << --indent << "}";
[fc638d2]341 }
342
[8688ce1]343 void CodeGenerator::visit( Constant * constant ) {
[6c4ff37]344 output << constant->get_value() ;
[51587aa]345 }
346
[4810867]347 // *** Expressions
[8688ce1]348 void CodeGenerator::visit( ApplicationExpr * applicationExpr ) {
[e04ef3a]349 extension( applicationExpr );
[8688ce1]350 if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
[51587aa]351 OperatorInfo opInfo;
352 if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
353 std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
354 switch ( opInfo.type ) {
355 case OT_INDEX:
356 assert( applicationExpr->get_args().size() == 2 );
357 (*arg++)->accept( *this );
[6c4ff37]358 output << "[";
[51587aa]359 (*arg)->accept( *this );
[6c4ff37]360 output << "]";
[51587aa]361 break;
[71f4e4f]362
[51587aa]363 case OT_CALL:
[356189a]364 // there are no intrinsic definitions of the function call operator
[51587aa]365 assert( false );
366 break;
[71f4e4f]367
[f1e012b]368 case OT_CTOR:
[c2ce2350]369 case OT_DTOR:
[356189a]370 if ( applicationExpr->get_args().size() == 1 ) {
[8e9cbb2]371 // the expression fed into a single parameter constructor or destructor may contain side
372 // effects, so must still output this expression
[64071c2]373 output << "(";
[356189a]374 (*arg++)->accept( *this );
[64071c2]375 output << ") /* " << opInfo.inputName << " */";
[356189a]376 } else if ( applicationExpr->get_args().size() == 2 ) {
[c2ce2350]377 // intrinsic two parameter constructors are essentially bitwise assignment
[356189a]378 output << "(";
379 (*arg++)->accept( *this );
380 output << opInfo.symbol;
381 (*arg)->accept( *this );
[c2ce2350]382 output << ") /* " << opInfo.inputName << " */";
[356189a]383 } else {
[c2ce2350]384 // no constructors with 0 or more than 2 parameters
[356189a]385 assert( false );
[8688ce1]386 } // if
[356189a]387 break;
[f1e012b]388
[51587aa]389 case OT_PREFIX:
390 case OT_PREFIXASSIGN:
391 assert( applicationExpr->get_args().size() == 1 );
[6c4ff37]392 output << "(";
393 output << opInfo.symbol;
[51587aa]394 (*arg)->accept( *this );
[6c4ff37]395 output << ")";
[51587aa]396 break;
[71f4e4f]397
[51587aa]398 case OT_POSTFIX:
399 case OT_POSTFIXASSIGN:
400 assert( applicationExpr->get_args().size() == 1 );
401 (*arg)->accept( *this );
[6c4ff37]402 output << opInfo.symbol;
[51587aa]403 break;
404
[f1e012b]405
[51587aa]406 case OT_INFIX:
407 case OT_INFIXASSIGN:
408 assert( applicationExpr->get_args().size() == 2 );
[6c4ff37]409 output << "(";
[51587aa]410 (*arg++)->accept( *this );
[6c4ff37]411 output << opInfo.symbol;
[51587aa]412 (*arg)->accept( *this );
[6c4ff37]413 output << ")";
[51587aa]414 break;
[71f4e4f]415
[51587aa]416 case OT_CONSTANT:
[721f17a]417 case OT_LABELADDRESS:
418 // there are no intrinsic definitions of 0/1 or label addresses as functions
[51587aa]419 assert( false );
[3778cb2]420 } // switch
[17cd4eb]421 } else {
[51587aa]422 varExpr->accept( *this );
[6c4ff37]423 output << "(";
[51587aa]424 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
[6c4ff37]425 output << ")";
[17cd4eb]426 } // if
[51587aa]427 } else {
428 applicationExpr->get_function()->accept( *this );
[6c4ff37]429 output << "(";
[51587aa]430 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
[6c4ff37]431 output << ")";
[51587aa]432 } // if
433 }
[71f4e4f]434
[8688ce1]435 void CodeGenerator::visit( UntypedExpr * untypedExpr ) {
[e04ef3a]436 extension( untypedExpr );
[8688ce1]437 if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
[51587aa]438 OperatorInfo opInfo;
439 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
440 std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
441 switch ( opInfo.type ) {
442 case OT_INDEX:
443 assert( untypedExpr->get_args().size() == 2 );
444 (*arg++)->accept( *this );
[6c4ff37]445 output << "[";
[51587aa]446 (*arg)->accept( *this );
[6c4ff37]447 output << "]";
[51587aa]448 break;
[71f4e4f]449
[51587aa]450 case OT_CALL:
[f1e012b]451 assert( false );
452
[c2ce2350]453 case OT_CTOR:
454 case OT_DTOR:
455 if ( untypedExpr->get_args().size() == 1 ) {
[8e9cbb2]456 // the expression fed into a single parameter constructor or destructor may contain side
457 // effects, so must still output this expression
[64071c2]458 output << "(";
[c2ce2350]459 (*arg++)->accept( *this );
[64071c2]460 output << ") /* " << opInfo.inputName << " */";
[c2ce2350]461 } else if ( untypedExpr->get_args().size() == 2 ) {
462 // intrinsic two parameter constructors are essentially bitwise assignment
463 output << "(";
464 (*arg++)->accept( *this );
465 output << opInfo.symbol;
466 (*arg)->accept( *this );
467 output << ") /* " << opInfo.inputName << " */";
468 } else {
469 // no constructors with 0 or more than 2 parameters
470 assert( false );
[3778cb2]471 } // if
[51587aa]472 break;
[71f4e4f]473
[51587aa]474 case OT_PREFIX:
475 case OT_PREFIXASSIGN:
[de62360d]476 case OT_LABELADDRESS:
[51587aa]477 assert( untypedExpr->get_args().size() == 1 );
[6c4ff37]478 output << "(";
479 output << opInfo.symbol;
[51587aa]480 (*arg)->accept( *this );
[6c4ff37]481 output << ")";
[51587aa]482 break;
[71f4e4f]483
[51587aa]484 case OT_POSTFIX:
485 case OT_POSTFIXASSIGN:
486 assert( untypedExpr->get_args().size() == 1 );
487 (*arg)->accept( *this );
[6c4ff37]488 output << opInfo.symbol;
[51587aa]489 break;
[71f4e4f]490
[51587aa]491 case OT_INFIX:
492 case OT_INFIXASSIGN:
493 assert( untypedExpr->get_args().size() == 2 );
[6c4ff37]494 output << "(";
[51587aa]495 (*arg++)->accept( *this );
[6c4ff37]496 output << opInfo.symbol;
[51587aa]497 (*arg)->accept( *this );
[6c4ff37]498 output << ")";
[51587aa]499 break;
[71f4e4f]500
[51587aa]501 case OT_CONSTANT:
502 // there are no intrinsic definitions of 0 or 1 as functions
503 assert( false );
[3778cb2]504 } // switch
[51587aa]505 } else {
[8688ce1]506 if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2
[ac911f4]507 assert( untypedExpr->get_args().size() == 2 );
508 (*untypedExpr->get_args().begin())->accept( *this );
509 output << " ... ";
510 (*--untypedExpr->get_args().end())->accept( *this );
[057b34f]511 } else { // builtin routines
512 nameExpr->accept( *this );
513 output << "(";
514 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
515 output << ")";
[66d12f7]516 } // if
[51587aa]517 } // if
518 } else {
519 untypedExpr->get_function()->accept( *this );
[6c4ff37]520 output << "(";
[51587aa]521 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
[6c4ff37]522 output << ")";
[51587aa]523 } // if
524 }
[71f4e4f]525
[064e3ff]526 void CodeGenerator::visit( RangeExpr * rangeExpr ) {
527 rangeExpr->get_low()->accept( *this );
528 output << " ... ";
529 rangeExpr->get_high()->accept( *this );
530 }
531
[8688ce1]532 void CodeGenerator::visit( NameExpr * nameExpr ) {
[e04ef3a]533 extension( nameExpr );
[51587aa]534 OperatorInfo opInfo;
535 if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
536 assert( opInfo.type == OT_CONSTANT );
[6c4ff37]537 output << opInfo.symbol;
[51587aa]538 } else {
[6c4ff37]539 output << nameExpr->get_name();
[51587aa]540 } // if
541 }
[71f4e4f]542
[8688ce1]543 void CodeGenerator::visit( AddressExpr * addressExpr ) {
[e04ef3a]544 extension( addressExpr );
[6c4ff37]545 output << "(&";
[51587aa]546 // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
[8688ce1]547 if ( VariableExpr * variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
[6c4ff37]548 output << mangleName( variableExpr->get_var() );
[51587aa]549 } else {
550 addressExpr->get_arg()->accept( *this );
551 } // if
[6c4ff37]552 output << ")";
[51587aa]553 }
554
[8688ce1]555 void CodeGenerator::visit( CastExpr * castExpr ) {
[e04ef3a]556 extension( castExpr );
[803deb1]557 output << "(";
[906e24d]558 if ( castExpr->get_result()->isVoid() ) {
[803deb1]559 output << "(void)" ;
[d104b02]560 } else {
561 // at least one result type of cast.
562 // Note: previously, lvalue casts were skipped. Since it's now impossible for the user to write
563 // an lvalue cast, this has been taken out.
[803deb1]564 output << "(";
[e39241b]565 output << genType( castExpr->get_result(), "", pretty, genC );
[71f4e4f]566 output << ")";
[3778cb2]567 } // if
[803deb1]568 castExpr->get_arg()->accept( *this );
569 output << ")";
[51587aa]570 }
[71f4e4f]571
[a5f0529]572 void CodeGenerator::visit( VirtualCastExpr * castExpr ) {
573 assertf( ! genC, "VirtualCastExpr should not reach code generation." );
574 extension( castExpr );
575 output << "(virtual ";
576 castExpr->get_arg()->accept( *this );
577 output << ")";
578 }
579
[8688ce1]580 void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {
[e39241b]581 assertf( ! genC, "UntypedMemberExpr should not reach code generation." );
582 extension( memberExpr );
583 memberExpr->get_aggregate()->accept( *this );
[5f642e38]584 output << ".";
585 memberExpr->get_member()->accept( *this );
[51587aa]586 }
[71f4e4f]587
[8688ce1]588 void CodeGenerator::visit( MemberExpr * memberExpr ) {
[e04ef3a]589 extension( memberExpr );
[51587aa]590 memberExpr->get_aggregate()->accept( *this );
[6c4ff37]591 output << "." << mangleName( memberExpr->get_member() );
[51587aa]592 }
[71f4e4f]593
[8688ce1]594 void CodeGenerator::visit( VariableExpr * variableExpr ) {
[e04ef3a]595 extension( variableExpr );
[51587aa]596 OperatorInfo opInfo;
597 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
[6c4ff37]598 output << opInfo.symbol;
[51587aa]599 } else {
[6c4ff37]600 output << mangleName( variableExpr->get_var() );
[51587aa]601 } // if
602 }
[71f4e4f]603
[8688ce1]604 void CodeGenerator::visit( ConstantExpr * constantExpr ) {
[51587aa]605 assert( constantExpr->get_constant() );
[e04ef3a]606 extension( constantExpr );
[51587aa]607 constantExpr->get_constant()->accept( *this );
608 }
[71f4e4f]609
[8688ce1]610 void CodeGenerator::visit( SizeofExpr * sizeofExpr ) {
[e04ef3a]611 extension( sizeofExpr );
[6c4ff37]612 output << "sizeof(";
[51587aa]613 if ( sizeofExpr->get_isType() ) {
[e39241b]614 output << genType( sizeofExpr->get_type(), "", pretty, genC );
[51587aa]615 } else {
616 sizeofExpr->get_expr()->accept( *this );
617 } // if
[6c4ff37]618 output << ")";
[51587aa]619 }
[47534159]620
[8688ce1]621 void CodeGenerator::visit( AlignofExpr * alignofExpr ) {
[47534159]622 // use GCC extension to avoid bumping std to C11
[8e9cbb2]623 extension( alignofExpr );
[47534159]624 output << "__alignof__(";
[25a054f]625 if ( alignofExpr->get_isType() ) {
[e39241b]626 output << genType( alignofExpr->get_type(), "", pretty, genC );
[47534159]627 } else {
[25a054f]628 alignofExpr->get_expr()->accept( *this );
[47534159]629 } // if
630 output << ")";
631 }
[71f4e4f]632
[8688ce1]633 void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) {
[e39241b]634 assertf( ! genC, "UntypedOffsetofExpr should not reach code generation." );
635 output << "offsetof(";
636 output << genType( offsetofExpr->get_type(), "", pretty, genC );
637 output << ", " << offsetofExpr->get_member();
638 output << ")";
[2a4b088]639 }
640
[8688ce1]641 void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) {
[25a054f]642 // use GCC builtin
643 output << "__builtin_offsetof(";
[e39241b]644 output << genType( offsetofExpr->get_type(), "", pretty, genC );
[e551c69]645 output << ", " << mangleName( offsetofExpr->get_member() );
[25a054f]646 output << ")";
647 }
[d63eeb0]648
[8688ce1]649 void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) {
[e39241b]650 assertf( ! genC, "OffsetPackExpr should not reach code generation." );
651 output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", pretty, genC ) << ")";
[afc1045]652 }
[70a06f6]653
[8688ce1]654 void CodeGenerator::visit( LogicalExpr * logicalExpr ) {
[e04ef3a]655 extension( logicalExpr );
[6c4ff37]656 output << "(";
[51587aa]657 logicalExpr->get_arg1()->accept( *this );
658 if ( logicalExpr->get_isAnd() ) {
[6c4ff37]659 output << " && ";
[51587aa]660 } else {
[6c4ff37]661 output << " || ";
[51587aa]662 } // if
663 logicalExpr->get_arg2()->accept( *this );
[6c4ff37]664 output << ")";
[51587aa]665 }
[71f4e4f]666
[8688ce1]667 void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) {
[e04ef3a]668 extension( conditionalExpr );
[6c4ff37]669 output << "(";
[51587aa]670 conditionalExpr->get_arg1()->accept( *this );
[6c4ff37]671 output << " ? ";
[51587aa]672 conditionalExpr->get_arg2()->accept( *this );
[6c4ff37]673 output << " : ";
[51587aa]674 conditionalExpr->get_arg3()->accept( *this );
[6c4ff37]675 output << ")";
[51587aa]676 }
[71f4e4f]677
[8688ce1]678 void CodeGenerator::visit( CommaExpr * commaExpr ) {
[e04ef3a]679 extension( commaExpr );
[6c4ff37]680 output << "(";
[8a6cf7e]681 if ( genC ) {
682 // arg1 of a CommaExpr is never used, so it can be safely cast to void to reduce gcc warnings.
683 commaExpr->set_arg1( new CastExpr( commaExpr->get_arg1() ) );
684 }
[51587aa]685 commaExpr->get_arg1()->accept( *this );
[6c4ff37]686 output << " , ";
[51587aa]687 commaExpr->get_arg2()->accept( *this );
[6c4ff37]688 output << ")";
[51587aa]689 }
[71f4e4f]690
[d104b02]691 void CodeGenerator::visit( TupleAssignExpr * tupleExpr ) {
692 assertf( ! genC, "TupleAssignExpr should not reach code generation." );
693 tupleExpr->stmtExpr->accept( *this );
694 }
695
[e39241b]696 void CodeGenerator::visit( UntypedTupleExpr * tupleExpr ) {
697 assertf( ! genC, "UntypedTupleExpr should not reach code generation." );
[f975c65]698 extension( tupleExpr );
[e39241b]699 output << "[";
700 genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() );
701 output << "]";
702 }
[907eccb]703
[e39241b]704 void CodeGenerator::visit( TupleExpr * tupleExpr ) {
705 assertf( ! genC, "TupleExpr should not reach code generation." );
[f975c65]706 extension( tupleExpr );
[e39241b]707 output << "[";
708 genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() );
709 output << "]";
710 }
[71f4e4f]711
[f975c65]712 void CodeGenerator::visit( TupleIndexExpr * tupleExpr ) {
713 assertf( ! genC, "TupleIndexExpr should not reach code generation." );
714 extension( tupleExpr );
715 tupleExpr->get_tuple()->accept( *this );
716 output << "." << tupleExpr->get_index();
717 }
718
[e39241b]719 void CodeGenerator::visit( TypeExpr * typeExpr ) {
[e4d829b]720 // if ( genC ) std::cerr << "typeexpr still exists: " << typeExpr << std::endl;
721 // assertf( ! genC, "TypeExpr should not reach code generation." );
722 if ( ! genC ) {
723 output<< genType( typeExpr->get_type(), "", pretty, genC );
724 }
[e39241b]725 }
[2b6c1e0]726
[8688ce1]727 void CodeGenerator::visit( AsmExpr * asmExpr ) {
[7f5566b]728 if ( asmExpr->get_inout() ) {
729 output << "[ ";
730 asmExpr->get_inout()->accept( *this );
731 output << " ] ";
732 } // if
733 asmExpr->get_constraint()->accept( *this );
734 output << " ( ";
735 asmExpr->get_operand()->accept( *this );
736 output << " )";
737 }
738
[3c13c03]739 void CodeGenerator::visit( CompoundLiteralExpr *compLitExpr ) {
[fbcde64]740 assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) );
[e39241b]741 output << "(" << genType( compLitExpr->get_result(), "", pretty, genC ) << ")";
[3c13c03]742 compLitExpr->get_initializer()->accept( *this );
743 }
744
[d104b02]745 void CodeGenerator::visit( UniqueExpr * unqExpr ) {
746 assertf( ! genC, "Unique expressions should not reach code generation." );
747 output << "unq<" << unqExpr->get_id() << ">{ ";
748 unqExpr->get_expr()->accept( *this );
749 output << " }";
750 }
751
[6eb8948]752 void CodeGenerator::visit( StmtExpr * stmtExpr ) {
[3c13c03]753 std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids();
[8bafacc]754 updateLocation( stmtExpr );
755 output << "({" << std::endl;
[f7cb0bc]756 ++indent;
[3c13c03]757 unsigned int numStmts = stmts.size();
758 unsigned int i = 0;
759 for ( Statement * stmt : stmts ) {
[8bafacc]760 updateLocation( stmt );
[8a6cf7e]761 output << printLabels( stmt->get_labels() );
[3c13c03]762 if ( i+1 == numStmts ) {
763 // last statement in a statement expression needs to be handled specially -
764 // cannot cast to void, otherwise the expression statement has no value
765 if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) {
766 exprStmt->get_expr()->accept( *this );
767 output << ";" << endl;
768 ++i;
769 break;
770 }
771 }
772 stmt->accept( *this );
773 output << endl;
774 if ( wantSpacing( stmt ) ) {
775 output << endl;
776 } // if
777 ++i;
778 }
[f7cb0bc]779 --indent;
[3c13c03]780 output << indent << "})";
[6eb8948]781 }
782
[4810867]783 // *** Statements
[8688ce1]784 void CodeGenerator::visit( CompoundStmt * compoundStmt ) {
[51587aa]785 std::list<Statement*> ks = compoundStmt->get_kids();
[2b6c1e0]786 output << "{" << endl;
[51587aa]787
[f7cb0bc]788 ++indent;
[51587aa]789
[7f5566b]790 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) {
[cda48b6]791 output << indent << printLabels( (*i)->get_labels() );
[7f5566b]792 (*i)->accept( *this );
[2b6c1e0]793
[6c4ff37]794 output << endl;
[2b6c1e0]795 if ( wantSpacing( *i ) ) {
796 output << endl;
[3778cb2]797 } // if
[8688ce1]798 } // for
[f7cb0bc]799 --indent;
[51587aa]800
[cda48b6]801 output << indent << "}";
[51587aa]802 }
803
[8688ce1]804 void CodeGenerator::visit( ExprStmt * exprStmt ) {
[6c4ff37]805 assert( exprStmt );
[262f085f]806 if ( genC ) {
807 // cast the top-level expression to void to reduce gcc warnings.
[8a6cf7e]808 exprStmt->set_expr( new CastExpr( exprStmt->get_expr() ) );
[262f085f]809 }
[8a6cf7e]810 exprStmt->get_expr()->accept( *this );
[321a2481]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 ) {
[8bafacc]846 updateLocation( 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 ) {
[8bafacc]860 updateLocation( switchStmt );
[7f5566b]861 output << "switch ( " ;
862 switchStmt->get_condition()->accept( *this );
863 output << " ) ";
[71f4e4f]864
[2b6c1e0]865 output << "{" << std::endl;
[f7cb0bc]866 ++indent;
[8688ce1]867 acceptAll( switchStmt->get_statements(), *this );
[f7cb0bc]868 --indent;
[cda48b6]869 output << indent << "}";
[51587aa]870 }
871
[8688ce1]872 void CodeGenerator::visit( CaseStmt * caseStmt ) {
[8bafacc]873 updateLocation( caseStmt );
[eb3261f]874 if ( caseStmt->isDefault()) {
[2b6c1e0]875 output << "default";
[eb3261f]876 } else {
[2b6c1e0]877 output << "case ";
[7f5566b]878 caseStmt->get_condition()->accept( *this );
[17cd4eb]879 } // if
[6c4ff37]880 output << ":\n";
[71f4e4f]881
[51587aa]882 std::list<Statement *> sts = caseStmt->get_statements();
883
[f7cb0bc]884 ++indent;
[51587aa]885 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
[cda48b6]886 output << indent << printLabels( (*i)->get_labels() ) ;
[7f5566b]887 (*i)->accept( *this );
[6c4ff37]888 output << endl;
[3778cb2]889 } // for
[f7cb0bc]890 --indent;
[51587aa]891 }
892
[8688ce1]893 void CodeGenerator::visit( BranchStmt * branchStmt ) {
[51587aa]894 switch ( branchStmt->get_type()) {
895 case BranchStmt::Goto:
896 if ( ! branchStmt->get_target().empty() )
[6c4ff37]897 output << "goto " << branchStmt->get_target();
[71f4e4f]898 else {
[51587aa]899 if ( branchStmt->get_computedTarget() != 0 ) {
[6c4ff37]900 output << "goto *";
[51587aa]901 branchStmt->get_computedTarget()->accept( *this );
902 } // if
903 } // if
904 break;
905 case BranchStmt::Break:
[6c4ff37]906 output << "break";
[51587aa]907 break;
908 case BranchStmt::Continue:
[6c4ff37]909 output << "continue";
[51587aa]910 break;
[3778cb2]911 } // switch
[2b6c1e0]912 output << ";";
[51587aa]913 }
914
[8688ce1]915 void CodeGenerator::visit( ReturnStmt * returnStmt ) {
[6c4ff37]916 output << "return ";
[4b2589a]917 maybeAccept( returnStmt->get_expr(), *this );
[6c4ff37]918 output << ";";
[51587aa]919 }
920
[daf1af8]921 void CodeGenerator::visit( ThrowStmt * throwStmt ) {
922 assertf( ! genC, "Throw statements should not reach code generation." );
923
924 output << ((throwStmt->get_kind() == ThrowStmt::Terminate) ?
925 "throw" : "throwResume");
926 if (throwStmt->get_expr()) {
927 output << " ";
928 throwStmt->get_expr()->accept( *this );
929 }
930 if (throwStmt->get_target()) {
931 output << " _At ";
932 throwStmt->get_target()->accept( *this );
933 }
934 output << ";";
935 }
936
[8688ce1]937 void CodeGenerator::visit( WhileStmt * whileStmt ) {
[321a2481]938 if ( whileStmt->get_isDoWhile() ) {
[6c4ff37]939 output << "do" ;
[321a2481]940 } else {
[6c4ff37]941 output << "while (" ;
[7f5566b]942 whileStmt->get_condition()->accept( *this );
[6c4ff37]943 output << ")";
[51587aa]944 } // if
[2b6c1e0]945 output << " ";
[51587aa]946
[2b6c1e0]947 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
[51587aa]948 whileStmt->get_body()->accept( *this );
949
[cda48b6]950 output << indent;
[51587aa]951
952 if ( whileStmt->get_isDoWhile() ) {
[6c4ff37]953 output << " while (" ;
[7f5566b]954 whileStmt->get_condition()->accept( *this );
[6c4ff37]955 output << ");";
[51587aa]956 } // if
957 }
958
[8688ce1]959 void CodeGenerator::visit( ForStmt * forStmt ) {
[8e9cbb2]960 // initialization is always hoisted, so don't bother doing anything with that
[145f1fc]961 output << "for (;";
[51587aa]962
[321a2481]963 if ( forStmt->get_condition() != 0 ) {
[51587aa]964 forStmt->get_condition()->accept( *this );
[3778cb2]965 } // if
[6c4ff37]966 output << ";";
[51587aa]967
[321a2481]968 if ( forStmt->get_increment() != 0 ) {
969 // cast the top-level expression to void to reduce gcc warnings.
970 Expression * expr = new CastExpr( forStmt->get_increment() );
971 expr->accept( *this );
[3778cb2]972 } // if
[2b6c1e0]973 output << ") ";
[51587aa]974
975 if ( forStmt->get_body() != 0 ) {
[2b6c1e0]976 output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
[51587aa]977 forStmt->get_body()->accept( *this );
978 } // if
979 }
980
[7e003011]981 void CodeGenerator::visit( __attribute__((unused)) NullStmt * nullStmt ) {
[cda48b6]982 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
[6c4ff37]983 output << "/* null statement */ ;";
[51587aa]984 }
985
[8688ce1]986 void CodeGenerator::visit( DeclStmt * declStmt ) {
[51587aa]987 declStmt->get_decl()->accept( *this );
[71f4e4f]988
[51587aa]989 if ( doSemicolon( declStmt->get_decl() ) ) {
[6c4ff37]990 output << ";";
[51587aa]991 } // if
992 }
993
[dd020c0]994 void CodeGenerator::handleStorageClass( DeclarationWithType * decl ) {
[fb04321]995 if ( decl->get_storageClasses().any() ) {
[6e8bd43]996 decl->get_storageClasses().print( output );
[a7c90d4]997 } // if
[dd020c0]998 } // CodeGenerator::handleStorageClass
[9facf3b]999
1000 std::string genName( DeclarationWithType * decl ) {
1001 CodeGen::OperatorInfo opInfo;
1002 if ( operatorLookup( decl->get_name(), opInfo ) ) {
1003 return opInfo.outputName;
1004 } else {
1005 return decl->get_name();
1006 } // if
1007 }
[51b73452]1008} // namespace CodeGen
[51587aa]1009
1010// Local Variables: //
1011// tab-width: 4 //
1012// mode: c++ //
1013// compile-command: "make install" //
1014// End: //
Note: See TracBrowser for help on using the repository browser.