source: src/CodeGen/CodeGenerator.cc@ 9f10c4b8

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 9f10c4b8 was 9236060, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Merge branch 'master' into references

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