source: src/CodeGen/CodeGenerator.cc@ 63be52cd

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 63be52cd was a5f0529, checked in by Andrew Beach <ajbeach@…>, 8 years ago

Virtual casts have been added. They still require a lot of hand coded support to work but for simple cases it should be enough.

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