source: src/CodeGen/CodeGenerator.cc@ 3eab0ef6

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 3eab0ef6 was 0dd18fd, checked in by Andrew Beach <ajbeach@…>, 8 years ago

Removed the old LineMark system.

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