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

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 3f27b9a was 0720e049, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Merge branch 'master' of plg.uwaterloo.ca:/u/cforall/software/cfa/cfa-cc

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