source: src/CodeGen/CodeGenerator.cc@ 6d267ca

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 6d267ca was e6cee92, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Fix TupleAssignment code for references

  • Property mode set to 100644
File size: 30.0 KB
RevLine 
[51587aa]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[71f4e4f]7// CodeGenerator.cc --
[51587aa]8//
9// Author : Richard C. Bilson
10// Created On : Mon May 18 07:44:20 2015
[6e300d9]11// Last Modified By : Andrew Beach
[daf1af8]12// Last Modified On : Thu Jun 8 16:00:00 2017
13// Update Count : 485
[51587aa]14//
15
[bf2438c]16#include <cassert> // for assert, assertf
17#include <list> // for _List_iterator, list, list<>::it...
[51b73452]18
[a61fea9a]19#include "CodeGenerator.h"
[bf2438c]20#include "Common/SemanticError.h" // for SemanticError
21#include "Common/UniqueName.h" // for UniqueName
22#include "Common/utility.h" // for CodeLocation, toString
23#include "GenType.h" // for genType
24#include "InitTweak/InitTweak.h" // for getPointerBase
25#include "OperatorTable.h" // for OperatorInfo, operatorLookup
26#include "Parser/LinkageSpec.h" // for Spec, Intrinsic
27#include "SynTree/Attribute.h" // for Attribute
28#include "SynTree/BaseSyntaxNode.h" // for BaseSyntaxNode
29#include "SynTree/Constant.h" // for Constant
30#include "SynTree/Declaration.h" // for DeclarationWithType, TypeDecl
31#include "SynTree/Expression.h" // for Expression, UntypedExpr, Applica...
32#include "SynTree/Initializer.h" // for Initializer, ListInit, Designation
33#include "SynTree/Label.h" // for Label, operator<<
34#include "SynTree/Statement.h" // for Statement, AsmStmt, BranchStmt
35#include "SynTree/Type.h" // for Type, Type::StorageClasses, Func...
[10a7775]36
[51b73452]37using namespace std;
38
39namespace CodeGen {
[6c4ff37]40 int CodeGenerator::tabsize = 4;
[51587aa]41
[145f1fc]42 // the kinds of statements that would ideally be followed by whitespace
[2b6c1e0]43 bool wantSpacing( Statement * stmt) {
44 return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) ||
[08061589]45 dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * >( stmt ) || dynamic_cast< SwitchStmt *>( stmt );
[2b6c1e0]46 }
47
[8688ce1]48 void CodeGenerator::extension( Expression * expr ) {
[8e9cbb2]49 if ( expr->get_extension() ) {
50 output << "__extension__ ";
51 } // if
52 } // extension
53
[8688ce1]54 void CodeGenerator::extension( Declaration * decl ) {
[8e9cbb2]55 if ( decl->get_extension() ) {
56 output << "__extension__ ";
57 } // if
58 } // extension
59
[58dd019]60 void CodeGenerator::asmName( DeclarationWithType * decl ) {
61 if ( ConstantExpr * asmName = decl->get_asmName() ) {
62 output << " asm ( " << asmName->get_constant()->get_value() << " )";
63 } // if
64 } // extension
65
[888cbe4]66 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
[8688ce1]562 void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {
[e39241b]563 assertf( ! genC, "UntypedMemberExpr should not reach code generation." );
564 extension( memberExpr );
565 memberExpr->get_aggregate()->accept( *this );
[5f642e38]566 output << ".";
567 memberExpr->get_member()->accept( *this );
[51587aa]568 }
[71f4e4f]569
[8688ce1]570 void CodeGenerator::visit( MemberExpr * memberExpr ) {
[e04ef3a]571 extension( memberExpr );
[51587aa]572 memberExpr->get_aggregate()->accept( *this );
[6c4ff37]573 output << "." << mangleName( memberExpr->get_member() );
[51587aa]574 }
[71f4e4f]575
[8688ce1]576 void CodeGenerator::visit( VariableExpr * variableExpr ) {
[e04ef3a]577 extension( variableExpr );
[51587aa]578 OperatorInfo opInfo;
579 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
[6c4ff37]580 output << opInfo.symbol;
[51587aa]581 } else {
[6c4ff37]582 output << mangleName( variableExpr->get_var() );
[51587aa]583 } // if
584 }
[71f4e4f]585
[8688ce1]586 void CodeGenerator::visit( ConstantExpr * constantExpr ) {
[51587aa]587 assert( constantExpr->get_constant() );
[e04ef3a]588 extension( constantExpr );
[51587aa]589 constantExpr->get_constant()->accept( *this );
590 }
[71f4e4f]591
[8688ce1]592 void CodeGenerator::visit( SizeofExpr * sizeofExpr ) {
[e04ef3a]593 extension( sizeofExpr );
[6c4ff37]594 output << "sizeof(";
[51587aa]595 if ( sizeofExpr->get_isType() ) {
[e39241b]596 output << genType( sizeofExpr->get_type(), "", pretty, genC );
[51587aa]597 } else {
598 sizeofExpr->get_expr()->accept( *this );
599 } // if
[6c4ff37]600 output << ")";
[51587aa]601 }
[47534159]602
[8688ce1]603 void CodeGenerator::visit( AlignofExpr * alignofExpr ) {
[47534159]604 // use GCC extension to avoid bumping std to C11
[8e9cbb2]605 extension( alignofExpr );
[47534159]606 output << "__alignof__(";
[25a054f]607 if ( alignofExpr->get_isType() ) {
[e39241b]608 output << genType( alignofExpr->get_type(), "", pretty, genC );
[47534159]609 } else {
[25a054f]610 alignofExpr->get_expr()->accept( *this );
[47534159]611 } // if
612 output << ")";
613 }
[71f4e4f]614
[8688ce1]615 void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) {
[e39241b]616 assertf( ! genC, "UntypedOffsetofExpr should not reach code generation." );
617 output << "offsetof(";
618 output << genType( offsetofExpr->get_type(), "", pretty, genC );
619 output << ", " << offsetofExpr->get_member();
620 output << ")";
[2a4b088]621 }
622
[8688ce1]623 void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) {
[25a054f]624 // use GCC builtin
625 output << "__builtin_offsetof(";
[e39241b]626 output << genType( offsetofExpr->get_type(), "", pretty, genC );
[e551c69]627 output << ", " << mangleName( offsetofExpr->get_member() );
[25a054f]628 output << ")";
629 }
[d63eeb0]630
[8688ce1]631 void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) {
[e39241b]632 assertf( ! genC, "OffsetPackExpr should not reach code generation." );
633 output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", pretty, genC ) << ")";
[afc1045]634 }
[70a06f6]635
[8688ce1]636 void CodeGenerator::visit( LogicalExpr * logicalExpr ) {
[e04ef3a]637 extension( logicalExpr );
[6c4ff37]638 output << "(";
[51587aa]639 logicalExpr->get_arg1()->accept( *this );
640 if ( logicalExpr->get_isAnd() ) {
[6c4ff37]641 output << " && ";
[51587aa]642 } else {
[6c4ff37]643 output << " || ";
[51587aa]644 } // if
645 logicalExpr->get_arg2()->accept( *this );
[6c4ff37]646 output << ")";
[51587aa]647 }
[71f4e4f]648
[8688ce1]649 void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) {
[e04ef3a]650 extension( conditionalExpr );
[6c4ff37]651 output << "(";
[51587aa]652 conditionalExpr->get_arg1()->accept( *this );
[6c4ff37]653 output << " ? ";
[51587aa]654 conditionalExpr->get_arg2()->accept( *this );
[6c4ff37]655 output << " : ";
[51587aa]656 conditionalExpr->get_arg3()->accept( *this );
[6c4ff37]657 output << ")";
[51587aa]658 }
[71f4e4f]659
[8688ce1]660 void CodeGenerator::visit( CommaExpr * commaExpr ) {
[e04ef3a]661 extension( commaExpr );
[6c4ff37]662 output << "(";
[51587aa]663 commaExpr->get_arg1()->accept( *this );
[6c4ff37]664 output << " , ";
[51587aa]665 commaExpr->get_arg2()->accept( *this );
[6c4ff37]666 output << ")";
[51587aa]667 }
[71f4e4f]668
[e39241b]669 void CodeGenerator::visit( UntypedTupleExpr * tupleExpr ) {
670 assertf( ! genC, "UntypedTupleExpr should not reach code generation." );
[f975c65]671 extension( tupleExpr );
[e39241b]672 output << "[";
673 genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() );
674 output << "]";
675 }
[907eccb]676
[e39241b]677 void CodeGenerator::visit( TupleExpr * tupleExpr ) {
678 assertf( ! genC, "TupleExpr should not reach code generation." );
[f975c65]679 extension( tupleExpr );
[e39241b]680 output << "[";
681 genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() );
682 output << "]";
683 }
[71f4e4f]684
[f975c65]685 void CodeGenerator::visit( TupleIndexExpr * tupleExpr ) {
686 assertf( ! genC, "TupleIndexExpr should not reach code generation." );
687 extension( tupleExpr );
688 tupleExpr->get_tuple()->accept( *this );
689 output << "." << tupleExpr->get_index();
690 }
691
[e39241b]692 void CodeGenerator::visit( TypeExpr * typeExpr ) {
[e4d829b]693 // if ( genC ) std::cerr << "typeexpr still exists: " << typeExpr << std::endl;
694 // assertf( ! genC, "TypeExpr should not reach code generation." );
695 if ( ! genC ) {
696 output<< genType( typeExpr->get_type(), "", pretty, genC );
697 }
[e39241b]698 }
[2b6c1e0]699
[8688ce1]700 void CodeGenerator::visit( AsmExpr * asmExpr ) {
[7f5566b]701 if ( asmExpr->get_inout() ) {
702 output << "[ ";
703 asmExpr->get_inout()->accept( *this );
704 output << " ] ";
705 } // if
706 asmExpr->get_constraint()->accept( *this );
707 output << " ( ";
708 asmExpr->get_operand()->accept( *this );
709 output << " )";
710 }
711
[3c13c03]712 void CodeGenerator::visit( CompoundLiteralExpr *compLitExpr ) {
[fbcde64]713 assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) );
[e39241b]714 output << "(" << genType( compLitExpr->get_result(), "", pretty, genC ) << ")";
[3c13c03]715 compLitExpr->get_initializer()->accept( *this );
716 }
717
[6eb8948]718 void CodeGenerator::visit( StmtExpr * stmtExpr ) {
[3c13c03]719 std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids();
[4810867]720 output << lineDirective( stmtExpr) << "({" << std::endl;
[f7cb0bc]721 ++indent;
[3c13c03]722 unsigned int numStmts = stmts.size();
723 unsigned int i = 0;
724 for ( Statement * stmt : stmts ) {
[4810867]725 output << lineDirective( stmt ) << indent;
726 output << printLabels( stmt->get_labels() );
[3c13c03]727 if ( i+1 == numStmts ) {
728 // last statement in a statement expression needs to be handled specially -
729 // cannot cast to void, otherwise the expression statement has no value
730 if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) {
731 exprStmt->get_expr()->accept( *this );
732 output << ";" << endl;
733 ++i;
734 break;
735 }
736 }
737 stmt->accept( *this );
738 output << endl;
739 if ( wantSpacing( stmt ) ) {
740 output << endl;
741 } // if
742 ++i;
743 }
[f7cb0bc]744 --indent;
[3c13c03]745 output << indent << "})";
[6eb8948]746 }
747
[4810867]748 // *** Statements
[8688ce1]749 void CodeGenerator::visit( CompoundStmt * compoundStmt ) {
[51587aa]750 std::list<Statement*> ks = compoundStmt->get_kids();
[2b6c1e0]751 output << "{" << endl;
[51587aa]752
[f7cb0bc]753 ++indent;
[51587aa]754
[7f5566b]755 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) {
[cda48b6]756 output << indent << printLabels( (*i)->get_labels() );
[7f5566b]757 (*i)->accept( *this );
[2b6c1e0]758
[6c4ff37]759 output << endl;
[2b6c1e0]760 if ( wantSpacing( *i ) ) {
761 output << endl;
[3778cb2]762 } // if
[8688ce1]763 } // for
[f7cb0bc]764 --indent;
[51587aa]765
[cda48b6]766 output << indent << "}";
[51587aa]767 }
768
[8688ce1]769 void CodeGenerator::visit( ExprStmt * exprStmt ) {
[6c4ff37]770 assert( exprStmt );
[262f085f]771 Expression * expr = exprStmt->get_expr();
772 if ( genC ) {
773 // cast the top-level expression to void to reduce gcc warnings.
774 expr = new CastExpr( expr );
775 }
[321a2481]776 expr->accept( *this );
777 output << ";";
[51587aa]778 }
779
[8688ce1]780 void CodeGenerator::visit( AsmStmt * asmStmt ) {
[7f5566b]781 output << "asm ";
782 if ( asmStmt->get_voltile() ) output << "volatile ";
783 if ( ! asmStmt->get_gotolabels().empty() ) output << "goto ";
784 output << "( ";
785 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
786 output << " : ";
787 genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
788 output << " : ";
789 genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
790 output << " : ";
791 genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
792 if ( ! asmStmt->get_gotolabels().empty() ) {
793 output << " : ";
794 for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
795 output << *begin++;
796 if ( begin == asmStmt->get_gotolabels().end() ) break;
797 output << ", ";
798 } // for
799 } // if
800 output << " );" ;
801 }
802
[e994912]803 void CodeGenerator::visit( AsmDecl * asmDecl ) {
804 output << "asm ";
805 AsmStmt * asmStmt = asmDecl->get_stmt();
806 output << "( ";
807 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
808 output << " )" ;
809 }
810
[8688ce1]811 void CodeGenerator::visit( IfStmt * ifStmt ) {
[4810867]812 output << lineDirective( ifStmt );
[7f5566b]813 output << "if ( ";
814 ifStmt->get_condition()->accept( *this );
815 output << " ) ";
[51587aa]816
[7f5566b]817 ifStmt->get_thenPart()->accept( *this );
[51587aa]818
819 if ( ifStmt->get_elsePart() != 0) {
[2b6c1e0]820 output << " else ";
[7f5566b]821 ifStmt->get_elsePart()->accept( *this );
[51587aa]822 } // if
823 }
824
[8688ce1]825 void CodeGenerator::visit( SwitchStmt * switchStmt ) {
[4810867]826 output << lineDirective( switchStmt );
[7f5566b]827 output << "switch ( " ;
828 switchStmt->get_condition()->accept( *this );
829 output << " ) ";
[71f4e4f]830
[2b6c1e0]831 output << "{" << std::endl;
[f7cb0bc]832 ++indent;
[8688ce1]833 acceptAll( switchStmt->get_statements(), *this );
[f7cb0bc]834 --indent;
[cda48b6]835 output << indent << "}";
[51587aa]836 }
837
[8688ce1]838 void CodeGenerator::visit( CaseStmt * caseStmt ) {
[4810867]839 output << lineDirective( caseStmt );
[cda48b6]840 output << indent;
[eb3261f]841 if ( caseStmt->isDefault()) {
[2b6c1e0]842 output << "default";
[eb3261f]843 } else {
[2b6c1e0]844 output << "case ";
[7f5566b]845 caseStmt->get_condition()->accept( *this );
[17cd4eb]846 } // if
[6c4ff37]847 output << ":\n";
[71f4e4f]848
[51587aa]849 std::list<Statement *> sts = caseStmt->get_statements();
850
[f7cb0bc]851 ++indent;
[51587aa]852 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
[cda48b6]853 output << indent << printLabels( (*i)->get_labels() ) ;
[7f5566b]854 (*i)->accept( *this );
[6c4ff37]855 output << endl;
[3778cb2]856 } // for
[f7cb0bc]857 --indent;
[51587aa]858 }
859
[8688ce1]860 void CodeGenerator::visit( BranchStmt * branchStmt ) {
[51587aa]861 switch ( branchStmt->get_type()) {
862 case BranchStmt::Goto:
863 if ( ! branchStmt->get_target().empty() )
[6c4ff37]864 output << "goto " << branchStmt->get_target();
[71f4e4f]865 else {
[51587aa]866 if ( branchStmt->get_computedTarget() != 0 ) {
[6c4ff37]867 output << "goto *";
[51587aa]868 branchStmt->get_computedTarget()->accept( *this );
869 } // if
870 } // if
871 break;
872 case BranchStmt::Break:
[6c4ff37]873 output << "break";
[51587aa]874 break;
875 case BranchStmt::Continue:
[6c4ff37]876 output << "continue";
[51587aa]877 break;
[3778cb2]878 } // switch
[2b6c1e0]879 output << ";";
[51587aa]880 }
881
[8688ce1]882 void CodeGenerator::visit( ReturnStmt * returnStmt ) {
[6c4ff37]883 output << "return ";
[4b2589a]884 maybeAccept( returnStmt->get_expr(), *this );
[6c4ff37]885 output << ";";
[51587aa]886 }
887
[daf1af8]888 void CodeGenerator::visit( ThrowStmt * throwStmt ) {
889 assertf( ! genC, "Throw statements should not reach code generation." );
890
891 output << ((throwStmt->get_kind() == ThrowStmt::Terminate) ?
892 "throw" : "throwResume");
893 if (throwStmt->get_expr()) {
894 output << " ";
895 throwStmt->get_expr()->accept( *this );
896 }
897 if (throwStmt->get_target()) {
898 output << " _At ";
899 throwStmt->get_target()->accept( *this );
900 }
901 output << ";";
902 }
903
[8688ce1]904 void CodeGenerator::visit( WhileStmt * whileStmt ) {
[321a2481]905 if ( whileStmt->get_isDoWhile() ) {
[6c4ff37]906 output << "do" ;
[321a2481]907 } else {
[6c4ff37]908 output << "while (" ;
[7f5566b]909 whileStmt->get_condition()->accept( *this );
[6c4ff37]910 output << ")";
[51587aa]911 } // if
[2b6c1e0]912 output << " ";
[51587aa]913
[2b6c1e0]914 output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
[51587aa]915 whileStmt->get_body()->accept( *this );
916
[cda48b6]917 output << indent;
[51587aa]918
919 if ( whileStmt->get_isDoWhile() ) {
[6c4ff37]920 output << " while (" ;
[7f5566b]921 whileStmt->get_condition()->accept( *this );
[6c4ff37]922 output << ");";
[51587aa]923 } // if
924 }
925
[8688ce1]926 void CodeGenerator::visit( ForStmt * forStmt ) {
[8e9cbb2]927 // initialization is always hoisted, so don't bother doing anything with that
[145f1fc]928 output << "for (;";
[51587aa]929
[321a2481]930 if ( forStmt->get_condition() != 0 ) {
[51587aa]931 forStmt->get_condition()->accept( *this );
[3778cb2]932 } // if
[6c4ff37]933 output << ";";
[51587aa]934
[321a2481]935 if ( forStmt->get_increment() != 0 ) {
936 // cast the top-level expression to void to reduce gcc warnings.
937 Expression * expr = new CastExpr( forStmt->get_increment() );
938 expr->accept( *this );
[3778cb2]939 } // if
[2b6c1e0]940 output << ") ";
[51587aa]941
942 if ( forStmt->get_body() != 0 ) {
[2b6c1e0]943 output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
[51587aa]944 forStmt->get_body()->accept( *this );
945 } // if
946 }
947
[7e003011]948 void CodeGenerator::visit( __attribute__((unused)) NullStmt * nullStmt ) {
[cda48b6]949 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
[6c4ff37]950 output << "/* null statement */ ;";
[51587aa]951 }
952
[8688ce1]953 void CodeGenerator::visit( DeclStmt * declStmt ) {
[51587aa]954 declStmt->get_decl()->accept( *this );
[71f4e4f]955
[51587aa]956 if ( doSemicolon( declStmt->get_decl() ) ) {
[6c4ff37]957 output << ";";
[51587aa]958 } // if
959 }
960
[dd020c0]961 void CodeGenerator::handleStorageClass( DeclarationWithType * decl ) {
[fb04321]962 if ( decl->get_storageClasses().any() ) {
[6e8bd43]963 decl->get_storageClasses().print( output );
[a7c90d4]964 } // if
[dd020c0]965 } // CodeGenerator::handleStorageClass
[9facf3b]966
967 std::string genName( DeclarationWithType * decl ) {
968 CodeGen::OperatorInfo opInfo;
969 if ( operatorLookup( decl->get_name(), opInfo ) ) {
970 return opInfo.outputName;
971 } else {
972 return decl->get_name();
973 } // if
974 }
[51b73452]975} // namespace CodeGen
[51587aa]976
977// Local Variables: //
978// tab-width: 4 //
979// mode: c++ //
980// compile-command: "make install" //
981// End: //
Note: See TracBrowser for help on using the repository browser.