source: src/CodeGen/CodeGenerator.cc@ 78a0b88

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 78a0b88 was a984e65, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Remove TraitDecl prior to CodeGen, add debug codegen for TraitDecl

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