source: src/CodeGen/CodeGenerator.cc@ fc56cdbf

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

Add codegen for TupleAssignExpr, UniqueExpr and remove lvalue cast special case

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