source: src/CodeGen/CodeGenerator.cc@ 65cec25

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 65cec25 was 0a81c3f, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Remove several missing-reference related hacks

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