source: src/CodeGen/CodeGenerator.cc@ fa2c005

ADT
Last change on this file since fa2c005 was fa2c005, checked in by JiadaL <j82liang@…>, 3 years ago

Finish Adt POC

  • Property mode set to 100644
File size: 40.4 KB
RevLine 
[51587aa]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[71f4e4f]7// CodeGenerator.cc --
[51587aa]8//
9// Author : Richard C. Bilson
10// Created On : Mon May 18 07:44:20 2015
[1931bb01]11// Last Modified By : Andrew Beach
12// Last Modified On : Wed Jun 29 14:34:00 2022
13// Update Count : 542
[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...
[9feb34b]19#include <sstream> // for stringstream
[51b73452]20
[1931bb01]21#include "AST/Decl.hpp" // for DeclWithType
[bf2438c]22#include "Common/UniqueName.h" // for UniqueName
23#include "GenType.h" // for genType
24#include "InitTweak/InitTweak.h" // for getPointerBase
25#include "OperatorTable.h" // for OperatorInfo, operatorLookup
[07de76b]26#include "SynTree/LinkageSpec.h" // for Spec, Intrinsic
[bf2438c]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
[60a8062]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 ) ||
[3b0bc16]45 dynamic_cast< WhileDoStmt * >( 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 ) {
[e612146c]61 if ( ConstantExpr * asmName = dynamic_cast<ConstantExpr *>(decl->get_asmName()) ) {
[58dd019]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
[60a8062]81 // Using updateLocation at the beginning of a node and endl within a node should become the method of formating.
[8bafacc]82 void CodeGenerator::updateLocation( CodeLocation const & to ) {
[d22e90f]83 // skip if linemarks shouldn't appear or if codelocation is unset
[42a36d9]84 if ( !options.lineMarks || to.isUnset() ) return;
[d22e90f]85
86 if ( currentLocation.followedBy( to, 0 ) ) {
[8bafacc]87 return;
88 } else if ( currentLocation.followedBy( to, 1 ) ) {
89 output << "\n" << indent;
[d48e529]90 currentLocation.first_line += 1;
[8bafacc]91 } else if ( currentLocation.followedBy( to, 2 ) ) {
92 output << "\n\n" << indent;
[d48e529]93 currentLocation.first_line += 2;
[8bafacc]94 } else {
[d48e529]95 output << "\n# " << to.first_line << " \"" << to.filename
[60a8062]96 << "\"\n" << indent;
[8bafacc]97 currentLocation = to;
98 }
99 output << std::flush;
100 }
[c850687]101
[8bafacc]102 void CodeGenerator::updateLocation( BaseSyntaxNode const * to ) {
103 updateLocation( to->location );
[c850687]104 }
105
[d22e90f]106 // replace endl
107 ostream & CodeGenerator::LineEnder::operator()( ostream & os ) const {
108 // if ( !cg.lineMarks ) {
109 // os << "\n" << cg.indent << std::flush;
110 // }
111 os << "\n" << std::flush;
112 cg.currentLocation.first_line++;
113 // os << "/* did endl; current loc is: " << cg.currentLocation.first_line << "*/";
114 return os;
[c850687]115 }
116
[4e5e6cc]117 CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks, bool printExprTypes ) : indent( 0, CodeGenerator::tabsize ), output( os ), printLabels( *this ), options( pretty, genC, lineMarks, printExprTypes ), endl( *this ) {}
118 CodeGenerator::CodeGenerator( std::ostream & os, const Options &options ) : indent( 0, CodeGenerator::tabsize ), output( os ), printLabels( *this ), options(options), endl( *this ) {}
[cda48b6]119
[486341f]120 string CodeGenerator::mangleName( DeclarationWithType * decl ) {
[13073be]121 // GCC builtins should always be printed unmangled
[42a36d9]122 if ( options.pretty || decl->linkage.is_gcc_builtin ) return decl->name;
[16ba4a6f]123 if ( LinkageSpec::isMangled(decl->linkage) && decl->mangleName != "" ) {
[f326f99]124 // need to incorporate scope level in order to differentiate names for destructors
125 return decl->get_scopedMangleName();
[51587aa]126 } else {
[13073be]127 return decl->name;
[51587aa]128 } // if
129 }
[94b4364]130
[44a81853]131 void CodeGenerator::genAttributes( list< Attribute * > & attributes ) {
[60a8062]132 if ( attributes.empty() ) return;
[44a81853]133 output << "__attribute__ ((";
134 for ( list< Attribute * >::iterator attr( attributes.begin() );; ) {
[85b2300]135 output << (*attr)->name;
136 if ( ! (*attr)->parameters.empty() ) {
[44a81853]137 output << "(";
[85b2300]138 genCommaList( (*attr)->parameters.begin(), (*attr)->parameters.end() );
[44a81853]139 output << ")";
140 } // if
[60a8062]141 if ( ++attr == attributes.end() ) break;
[44a81853]142 output << ","; // separator
143 } // for
144 output << ")) ";
145 } // CodeGenerator::genAttributes
[7baed7d]146
[9857e8d]147 // *** BaseSyntaxNode
[d22e90f]148 void CodeGenerator::previsit( BaseSyntaxNode * node ) {
[9857e8d]149 // turn off automatic recursion for all nodes, to allow each visitor to
150 // precisely control the order in which its children are visited.
151 visit_children = false;
[d22e90f]152 updateLocation( node );
[9857e8d]153 }
154
155 // *** BaseSyntaxNode
156 void CodeGenerator::postvisit( BaseSyntaxNode * node ) {
157 std::stringstream ss;
158 node->print( ss );
159 assertf( false, "Unhandled node reached in CodeGenerator: %s", ss.str().c_str() );
160 }
[7baed7d]161
[5f08961d]162 // *** Expression
163 void CodeGenerator::previsit( Expression * node ) {
164 previsit( (BaseSyntaxNode *)node );
165 GuardAction( [this, node](){
[60a8062]166 if ( options.printExprTypes && node->result ) {
167 output << " /* " << genType( node->result, "", options ) << " */ ";
168 }
169 } );
[5f08961d]170 }
171
[4810867]172 // *** Declarations
[9857e8d]173 void CodeGenerator::postvisit( FunctionDecl * functionDecl ) {
[3ed994e]174 // deleted decls should never be used, so don't print them
[42a36d9]175 if ( functionDecl->isDeleted && options.genC ) return;
[8e9cbb2]176 extension( functionDecl );
[7baed7d]177 genAttributes( functionDecl->get_attributes() );
178
[51587aa]179 handleStorageClass( functionDecl );
[6e8bd43]180 functionDecl->get_funcSpec().print( output );
[dd020c0]181
[da09ba1]182 Options subOptions = options;
[76f7fc7]183 subOptions.anonymousUnused = functionDecl->has_body();
[da09ba1]184 output << genType( functionDecl->get_functionType(), mangleName( functionDecl ), subOptions );
[51587aa]185
[58dd019]186 asmName( functionDecl );
187
[51587aa]188 if ( functionDecl->get_statements() ) {
[9857e8d]189 functionDecl->get_statements()->accept( *visitor );
[51587aa]190 } // if
[3ed994e]191 if ( functionDecl->isDeleted ) {
192 output << " = void";
193 }
[51587aa]194 }
195
[9857e8d]196 void CodeGenerator::postvisit( ObjectDecl * objectDecl ) {
[3ed994e]197 // deleted decls should never be used, so don't print them
[42a36d9]198 if ( objectDecl->isDeleted && options.genC ) return;
[de8d7fb1]199
200 // gcc allows an empty declarator (no name) for bit-fields and C states: 6.7.2.1 Structure and union specifiers,
201 // point 4, page 113: If the (bit field) value is zero, the declaration shall have no declarator. For anything
202 // else, the anonymous name refers to the anonymous object for plan9 inheritance.
203 if ( objectDecl->get_name().empty() && options.genC && ! objectDecl->get_bitfieldWidth() ) {
[e39241b]204 // only generate an anonymous name when generating C code, otherwise it clutters the output too much
[d9c8a59]205 static UniqueName name = { "__anonymous_object" };
206 objectDecl->set_name( name.newName() );
[de8d7fb1]207 // Stops unused parameter warnings.
208 if ( options.anonymousUnused ) {
209 objectDecl->attributes.push_back( new Attribute( "unused" ) );
210 }
[d9c8a59]211 }
212
[8e9cbb2]213 extension( objectDecl );
[f9cebb5]214 genAttributes( objectDecl->get_attributes() );
215
[51587aa]216 handleStorageClass( objectDecl );
[42a36d9]217 output << genType( objectDecl->get_type(), mangleName( objectDecl ), options.pretty, options.genC );
[71f4e4f]218
[58dd019]219 asmName( objectDecl );
220
[51587aa]221 if ( objectDecl->get_init() ) {
[6c4ff37]222 output << " = ";
[9857e8d]223 objectDecl->get_init()->accept( *visitor );
[51587aa]224 } // if
[3ed994e]225 if ( objectDecl->isDeleted ) {
226 output << " = void";
227 }
[3778cb2]228
[51587aa]229 if ( objectDecl->get_bitfieldWidth() ) {
[6c4ff37]230 output << ":";
[9857e8d]231 objectDecl->get_bitfieldWidth()->accept( *visitor );
[51587aa]232 } // if
233 }
234
[5f642e38]235 void CodeGenerator::handleAggregate( AggregateDecl * aggDecl, const std::string & kind ) {
[42a36d9]236 if( ! aggDecl->parameters.empty() && ! options.genC ) {
[e39241b]237 // assertf( ! genC, "Aggregate type parameters should not reach code generation." );
238 output << "forall(";
[0b3b2ae]239 genCommaList( aggDecl->parameters.begin(), aggDecl->parameters.end() );
[e39241b]240 output << ")" << endl;
[e6cee92]241 output << indent;
[e39241b]242 }
243
[593370c]244 output << kind;
[0b3b2ae]245 genAttributes( aggDecl->attributes );
246 output << aggDecl->name;
[71f4e4f]247
[2c57025]248 if ( aggDecl->has_body() ) {
[0b3b2ae]249 std::list< Declaration * > & memb = aggDecl->members;
[94b4364]250 output << " {" << endl;
[51587aa]251
[f7cb0bc]252 ++indent;
[3778cb2]253 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) {
[87e08e24]254 output << indent;
[9857e8d]255 (*i)->accept( *visitor );
[6c4ff37]256 output << ";" << endl;
[3778cb2]257 } // for
[51587aa]258
[f7cb0bc]259 --indent;
[51587aa]260
[cda48b6]261 output << indent << "}";
[17cd4eb]262 } // if
[51587aa]263 }
[17cd4eb]264
[9857e8d]265 void CodeGenerator::postvisit( StructDecl * structDecl ) {
[8e9cbb2]266 extension( structDecl );
[5f642e38]267 handleAggregate( structDecl, "struct " );
[51587aa]268 }
[17cd4eb]269
[9857e8d]270 void CodeGenerator::postvisit( UnionDecl * unionDecl ) {
[8e9cbb2]271 extension( unionDecl );
[5f642e38]272 handleAggregate( unionDecl, "union " );
[51587aa]273 }
[71f4e4f]274
[8bb86ce]275 template<typename pass_type>
276 inline void genEnumInitializer( PassVisitor<pass_type> * visitor, Type * baseType, std::ostream & output,
277 Initializer * init, long long * cur_val, Options options) {
278 auto baseTypeAsBasic = baseType ? dynamic_cast<BasicType *>( baseType ) : nullptr;
279 if ( init ) { // If value has an explicit initiazatior
280 output << " = ";
281 output << "(" << genType(baseType, "", options) << ")";
282 init->accept( *visitor );
283 if ( baseTypeAsBasic && baseTypeAsBasic->isInteger() ) { // if it is an integral type and initilizer offered,
284 // need to update the cur_val
285 Expression* expr = ((SingleInit *)(init))->value;
286 while ( auto temp = dynamic_cast<CastExpr *>(expr) ) { // unwrap introduced cast
287 expr = temp->arg;
288 }
289 *cur_val = ((ConstantExpr *)expr)->constant.get_ival()+1;
290 }
291 } else if ( baseTypeAsBasic && baseTypeAsBasic->isInteger() ) { // integral implicitly init to cur_val + 1
292 output << " = " << "(" << genType(baseType, "", options) << ")";
293 output << (*cur_val)++;
294 }
295 }
296
[f4e01f1]297 void CodeGenerator::handleData( EnumDecl * ) {
[561354f]298 assert(false);
[28f8f15]299 }
300
[9857e8d]301 void CodeGenerator::postvisit( EnumDecl * enumDecl ) {
[28f8f15]302 extension( enumDecl );
[8e9cbb2]303 std::list< Declaration* > &memb = enumDecl->get_members();
[d8e2a09]304 if (enumDecl->base && ! memb.empty()) {
[8bb86ce]305 long long cur_val = 0;
[51587aa]306 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
[8688ce1]307 ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
[51587aa]308 assert( obj );
[0bd46fd]309 output << "static ";
[9749d2fa]310 output << genType(enumDecl->base, mangleName( obj ), options);
[8bb86ce]311 genEnumInitializer( visitor, enumDecl->base, output, obj->get_init(), &cur_val, options);
[4390fb6]312 output << ";" << endl;
[51587aa]313 } // for
[4390fb6]314 } else {
315 output << "enum ";
316 genAttributes( enumDecl->get_attributes() );
317
318 output << enumDecl->get_name();
319
320 if ( ! memb.empty() ) {
321 output << " {" << endl;
322
323 ++indent;
324 for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++) {
325 ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
326 assert( obj );
327 output << indent << mangleName( obj );
328 if ( obj->get_init() ) {
329 output << " = ";
330 obj->get_init()->accept( *visitor );
331 } // if
332 output << "," << endl;
333 } // for
[f7cb0bc]334 --indent;
[cda48b6]335 output << indent << "}";
[4390fb6]336 } // if
[51587aa]337 } // if
338 }
[71f4e4f]339
[fa2c005]340 // Temporary fix, But visitor should be able to handle this
341 void CodeGenerator::postvisit( AdtDecl * adtDecl ) {
342 output << endl << "/* AdtDecl Code Section */" << endl;
343 postvisit( adtDecl->tag );
344 output << ";" << endl;
345 postvisit( adtDecl->data_union );
346 output << ";" << endl;
347 postvisit( adtDecl->tag_union );
[f4e01f1]348 }
349
[9857e8d]350 void CodeGenerator::postvisit( TraitDecl * traitDecl ) {
[42a36d9]351 assertf( ! options.genC, "TraitDecls should not reach code generation." );
[a984e65]352 extension( traitDecl );
353 handleAggregate( traitDecl, "trait " );
354 }
[71f4e4f]355
[9857e8d]356 void CodeGenerator::postvisit( TypedefDecl * typeDecl ) {
[42a36d9]357 assertf( ! options.genC, "Typedefs are removed and substituted in earlier passes." );
[e39241b]358 output << "typedef ";
[42a36d9]359 output << genType( typeDecl->get_base(), typeDecl->get_name(), options ) << endl;
[51587aa]360 }
[71f4e4f]361
[9857e8d]362 void CodeGenerator::postvisit( TypeDecl * typeDecl ) {
[42a36d9]363 assertf( ! options.genC, "TypeDecls should not reach code generation." );
[ab4bff5]364 output << typeDecl->genTypeString() << " " << typeDecl->name;
[f0ecf9b]365 if ( typeDecl->sized ) {
[ab4bff5]366 output << " | sized(" << typeDecl->name << ")";
[a0c7dc36]367 }
[ab4bff5]368 if ( ! typeDecl->assertions.empty() ) {
[a0c7dc36]369 output << " | { ";
[ab4bff5]370 for ( DeclarationWithType * assert : typeDecl->assertions ) {
371 assert->accept( *visitor );
372 output << "; ";
373 }
[a0c7dc36]374 output << " }";
[e39241b]375 }
[51587aa]376 }
377
[92fea32]378 void CodeGenerator::postvisit( StaticAssertDecl * assertDecl ) {
379 output << "_Static_assert(";
380 assertDecl->condition->accept( *visitor );
381 output << ", ";
382 assertDecl->message->accept( *visitor );
383 output << ")";
384 }
385
[9857e8d]386 void CodeGenerator::postvisit( Designation * designation ) {
[e4d829b]387 std::list< Expression * > designators = designation->get_designators();
[e45215c]388 if ( designators.size() == 0 ) return;
[e4d829b]389 for ( Expression * des : designators ) {
[62423350]390 if ( dynamic_cast< NameExpr * >( des ) || dynamic_cast< VariableExpr * >( des ) ) {
391 // if expression is a NameExpr or VariableExpr, then initializing aggregate member
392 output << ".";
[9857e8d]393 des->accept( *visitor );
[e4d829b]394 } else {
[3e54399]395 // otherwise, it has to be a ConstantExpr or CastExpr, initializing array element
[62423350]396 output << "[";
[9857e8d]397 des->accept( *visitor );
[62423350]398 output << "]";
[3778cb2]399 } // if
400 } // for
[3eb1653]401 output << " = ";
[e45215c]402 }
403
[9857e8d]404 void CodeGenerator::postvisit( SingleInit * init ) {
405 init->get_value()->accept( *visitor );
[51587aa]406 }
407
[9857e8d]408 void CodeGenerator::postvisit( ListInit * init ) {
[e4d829b]409 auto initBegin = init->begin();
410 auto initEnd = init->end();
411 auto desigBegin = init->get_designations().begin();
412 auto desigEnd = init->get_designations().end();
413
[6c4ff37]414 output << "{ ";
[e4d829b]415 for ( ; initBegin != initEnd && desigBegin != desigEnd; ) {
[9857e8d]416 (*desigBegin)->accept( *visitor );
417 (*initBegin)->accept( *visitor );
[e4d829b]418 ++initBegin, ++desigBegin;
419 if ( initBegin != initEnd ) {
420 output << ", ";
421 }
422 }
[6c4ff37]423 output << " }";
[e4d829b]424 assertf( initBegin == initEnd && desigBegin == desigEnd, "Initializers and designators not the same length. %s", toString( init ).c_str() );
[51587aa]425 }
426
[d22e90f]427 void CodeGenerator::postvisit( ConstructorInit * init ){
[42a36d9]428 assertf( ! options.genC, "ConstructorInit nodes should not reach code generation." );
[e6cee92]429 // pseudo-output for constructor/destructor pairs
[d22e90f]430 output << "<ctorinit>{" << endl << ++indent << "ctor: ";
[9857e8d]431 maybeAccept( init->get_ctor(), *visitor );
[d22e90f]432 output << ", " << endl << indent << "dtor: ";
[9857e8d]433 maybeAccept( init->get_dtor(), *visitor );
[d22e90f]434 output << endl << --indent << "}";
[fc638d2]435 }
436
[9857e8d]437 void CodeGenerator::postvisit( Constant * constant ) {
[d7312ac]438 output << constant->get_value();
[51587aa]439 }
440
[4810867]441 // *** Expressions
[9857e8d]442 void CodeGenerator::postvisit( ApplicationExpr * applicationExpr ) {
[e04ef3a]443 extension( applicationExpr );
[8688ce1]444 if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
[60a8062]445 const OperatorInfo * opInfo;
446 if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && ( opInfo = operatorLookup( varExpr->get_var()->get_name() ) ) ) {
[51587aa]447 std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
[60a8062]448 switch ( opInfo->type ) {
[51587aa]449 case OT_INDEX:
450 assert( applicationExpr->get_args().size() == 2 );
[9857e8d]451 (*arg++)->accept( *visitor );
[6c4ff37]452 output << "[";
[9857e8d]453 (*arg)->accept( *visitor );
[6c4ff37]454 output << "]";
[51587aa]455 break;
[71f4e4f]456
[51587aa]457 case OT_CALL:
[356189a]458 // there are no intrinsic definitions of the function call operator
[51587aa]459 assert( false );
460 break;
[71f4e4f]461
[f1e012b]462 case OT_CTOR:
[c2ce2350]463 case OT_DTOR:
[356189a]464 if ( applicationExpr->get_args().size() == 1 ) {
[8e9cbb2]465 // the expression fed into a single parameter constructor or destructor may contain side
466 // effects, so must still output this expression
[64071c2]467 output << "(";
[9857e8d]468 (*arg++)->accept( *visitor );
[60a8062]469 output << ") /* " << opInfo->inputName << " */";
[356189a]470 } else if ( applicationExpr->get_args().size() == 2 ) {
[c2ce2350]471 // intrinsic two parameter constructors are essentially bitwise assignment
[356189a]472 output << "(";
[9857e8d]473 (*arg++)->accept( *visitor );
[60a8062]474 output << opInfo->symbol;
[9857e8d]475 (*arg)->accept( *visitor );
[60a8062]476 output << ") /* " << opInfo->inputName << " */";
[356189a]477 } else {
[c2ce2350]478 // no constructors with 0 or more than 2 parameters
[356189a]479 assert( false );
[8688ce1]480 } // if
[356189a]481 break;
[f1e012b]482
[51587aa]483 case OT_PREFIX:
484 case OT_PREFIXASSIGN:
485 assert( applicationExpr->get_args().size() == 1 );
[6c4ff37]486 output << "(";
[60a8062]487 output << opInfo->symbol;
[9857e8d]488 (*arg)->accept( *visitor );
[6c4ff37]489 output << ")";
[51587aa]490 break;
[71f4e4f]491
[51587aa]492 case OT_POSTFIX:
493 case OT_POSTFIXASSIGN:
494 assert( applicationExpr->get_args().size() == 1 );
[9857e8d]495 (*arg)->accept( *visitor );
[60a8062]496 output << opInfo->symbol;
[51587aa]497 break;
498
[f1e012b]499
[51587aa]500 case OT_INFIX:
501 case OT_INFIXASSIGN:
502 assert( applicationExpr->get_args().size() == 2 );
[6c4ff37]503 output << "(";
[9857e8d]504 (*arg++)->accept( *visitor );
[60a8062]505 output << opInfo->symbol;
[9857e8d]506 (*arg)->accept( *visitor );
[6c4ff37]507 output << ")";
[51587aa]508 break;
[71f4e4f]509
[51587aa]510 case OT_CONSTANT:
[721f17a]511 case OT_LABELADDRESS:
512 // there are no intrinsic definitions of 0/1 or label addresses as functions
[51587aa]513 assert( false );
[3778cb2]514 } // switch
[9d55ff6]515 } else if( varExpr->get_var()->get_linkage() == LinkageSpec::BuiltinCFA && varExpr->get_var()->get_name() == "intptr" ) {
516 // THIS is a hack to make it a constant until a proper constexpr solution is created
517 output << "((void*)";
518 std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
519 (*arg++)->accept( *visitor );
520 output << ")";
[17cd4eb]521 } else {
[9857e8d]522 varExpr->accept( *visitor );
[6c4ff37]523 output << "(";
[51587aa]524 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
[6c4ff37]525 output << ")";
[17cd4eb]526 } // if
[51587aa]527 } else {
[9857e8d]528 applicationExpr->get_function()->accept( *visitor );
[6c4ff37]529 output << "(";
[51587aa]530 genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
[6c4ff37]531 output << ")";
[51587aa]532 } // if
533 }
[71f4e4f]534
[9857e8d]535 void CodeGenerator::postvisit( UntypedExpr * untypedExpr ) {
[e04ef3a]536 extension( untypedExpr );
[22bc276]537 if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->function ) ) {
[60a8062]538 const OperatorInfo * opInfo = operatorLookup( nameExpr->name );
539 if ( opInfo ) {
[22bc276]540 std::list< Expression* >::iterator arg = untypedExpr->args.begin();
[60a8062]541 switch ( opInfo->type ) {
[51587aa]542 case OT_INDEX:
[22bc276]543 assert( untypedExpr->args.size() == 2 );
[9857e8d]544 (*arg++)->accept( *visitor );
[6c4ff37]545 output << "[";
[9857e8d]546 (*arg)->accept( *visitor );
[6c4ff37]547 output << "]";
[51587aa]548 break;
[71f4e4f]549
[51587aa]550 case OT_CALL:
[f1e012b]551 assert( false );
552
[c2ce2350]553 case OT_CTOR:
554 case OT_DTOR:
[22bc276]555 if ( untypedExpr->args.size() == 1 ) {
[8e9cbb2]556 // the expression fed into a single parameter constructor or destructor may contain side
557 // effects, so must still output this expression
[64071c2]558 output << "(";
[9857e8d]559 (*arg++)->accept( *visitor );
[60a8062]560 output << ") /* " << opInfo->inputName << " */";
[c2ce2350]561 } else if ( untypedExpr->get_args().size() == 2 ) {
562 // intrinsic two parameter constructors are essentially bitwise assignment
563 output << "(";
[9857e8d]564 (*arg++)->accept( *visitor );
[60a8062]565 output << opInfo->symbol;
[9857e8d]566 (*arg)->accept( *visitor );
[60a8062]567 output << ") /* " << opInfo->inputName << " */";
[c2ce2350]568 } else {
569 // no constructors with 0 or more than 2 parameters
[42a36d9]570 assertf( ! options.genC, "UntypedExpr constructor/destructor with 0 or more than 2 parameters." );
[4e8949f]571 output << "(";
572 (*arg++)->accept( *visitor );
[60a8062]573 output << opInfo->symbol << "{ ";
[22bc276]574 genCommaList( arg, untypedExpr->args.end() );
[60a8062]575 output << "}) /* " << opInfo->inputName << " */";
[3778cb2]576 } // if
[51587aa]577 break;
[71f4e4f]578
[51587aa]579 case OT_PREFIX:
580 case OT_PREFIXASSIGN:
[de62360d]581 case OT_LABELADDRESS:
[22bc276]582 assert( untypedExpr->args.size() == 1 );
[6c4ff37]583 output << "(";
[60a8062]584 output << opInfo->symbol;
[9857e8d]585 (*arg)->accept( *visitor );
[6c4ff37]586 output << ")";
[51587aa]587 break;
[71f4e4f]588
[51587aa]589 case OT_POSTFIX:
590 case OT_POSTFIXASSIGN:
[22bc276]591 assert( untypedExpr->args.size() == 1 );
[9857e8d]592 (*arg)->accept( *visitor );
[60a8062]593 output << opInfo->symbol;
[51587aa]594 break;
[71f4e4f]595
[51587aa]596 case OT_INFIX:
597 case OT_INFIXASSIGN:
[22bc276]598 assert( untypedExpr->args.size() == 2 );
[6c4ff37]599 output << "(";
[9857e8d]600 (*arg++)->accept( *visitor );
[60a8062]601 output << opInfo->symbol;
[9857e8d]602 (*arg)->accept( *visitor );
[6c4ff37]603 output << ")";
[51587aa]604 break;
[71f4e4f]605
[51587aa]606 case OT_CONSTANT:
607 // there are no intrinsic definitions of 0 or 1 as functions
608 assert( false );
[3778cb2]609 } // switch
[51587aa]610 } else {
[22bc276]611 // builtin routines
612 nameExpr->accept( *visitor );
613 output << "(";
614 genCommaList( untypedExpr->args.begin(), untypedExpr->args.end() );
615 output << ")";
[51587aa]616 } // if
617 } else {
[22bc276]618 untypedExpr->function->accept( *visitor );
[6c4ff37]619 output << "(";
[22bc276]620 genCommaList( untypedExpr->args.begin(), untypedExpr->args.end() );
[6c4ff37]621 output << ")";
[51587aa]622 } // if
623 }
[71f4e4f]624
[9857e8d]625 void CodeGenerator::postvisit( RangeExpr * rangeExpr ) {
[22bc276]626 rangeExpr->low->accept( *visitor );
[064e3ff]627 output << " ... ";
[22bc276]628 rangeExpr->high->accept( *visitor );
[064e3ff]629 }
630
[9857e8d]631 void CodeGenerator::postvisit( NameExpr * nameExpr ) {
[e04ef3a]632 extension( nameExpr );
[60a8062]633 const OperatorInfo * opInfo = operatorLookup( nameExpr->name );
634 if ( opInfo ) {
635 if ( opInfo->type == OT_CONSTANT ) {
636 output << opInfo->symbol;
[5ea7a22]637 } else {
[60a8062]638 output << opInfo->outputName;
[5ea7a22]639 }
[51587aa]640 } else {
[6c4ff37]641 output << nameExpr->get_name();
[51587aa]642 } // if
643 }
[71f4e4f]644
[6e50a6b]645 void CodeGenerator::postvisit( DimensionExpr * dimensionExpr ) {
646 extension( dimensionExpr );
647 output << "/*non-type*/" << dimensionExpr->get_name();
648 }
649
[9857e8d]650 void CodeGenerator::postvisit( AddressExpr * addressExpr ) {
[e04ef3a]651 extension( addressExpr );
[6c4ff37]652 output << "(&";
[9857e8d]653 addressExpr->arg->accept( *visitor );
[6c4ff37]654 output << ")";
[51587aa]655 }
656
[9857e8d]657 void CodeGenerator::postvisit( LabelAddressExpr *addressExpr ) {
[5809461]658 extension( addressExpr );
659 output << "(&&" << addressExpr->arg << ")";
660 }
661
[9857e8d]662 void CodeGenerator::postvisit( CastExpr * castExpr ) {
[e04ef3a]663 extension( castExpr );
[803deb1]664 output << "(";
[906e24d]665 if ( castExpr->get_result()->isVoid() ) {
[d7312ac]666 output << "(void)";
[d104b02]667 } else {
668 // at least one result type of cast.
669 // Note: previously, lvalue casts were skipped. Since it's now impossible for the user to write
670 // an lvalue cast, this has been taken out.
[803deb1]671 output << "(";
[42a36d9]672 output << genType( castExpr->get_result(), "", options );
[71f4e4f]673 output << ")";
[3778cb2]674 } // if
[da9d79b]675 castExpr->arg->accept( *visitor );
676 output << ")";
677 }
678
679 void CodeGenerator::postvisit( KeywordCastExpr * castExpr ) {
[42a36d9]680 assertf( ! options.genC, "KeywordCast should not reach code generation." );
[da9d79b]681 extension( castExpr );
682 output << "((" << castExpr->targetString() << " &)";
683 castExpr->arg->accept( *visitor );
[803deb1]684 output << ")";
[51587aa]685 }
[71f4e4f]686
[9857e8d]687 void CodeGenerator::postvisit( VirtualCastExpr * castExpr ) {
[42a36d9]688 assertf( ! options.genC, "VirtualCastExpr should not reach code generation." );
[a5f0529]689 extension( castExpr );
690 output << "(virtual ";
[9857e8d]691 castExpr->get_arg()->accept( *visitor );
[a5f0529]692 output << ")";
693 }
694
[9857e8d]695 void CodeGenerator::postvisit( UntypedMemberExpr * memberExpr ) {
[42a36d9]696 assertf( ! options.genC, "UntypedMemberExpr should not reach code generation." );
[e39241b]697 extension( memberExpr );
[9857e8d]698 memberExpr->get_aggregate()->accept( *visitor );
[5f642e38]699 output << ".";
[9857e8d]700 memberExpr->get_member()->accept( *visitor );
[51587aa]701 }
[71f4e4f]702
[9857e8d]703 void CodeGenerator::postvisit( MemberExpr * memberExpr ) {
[e04ef3a]704 extension( memberExpr );
[9857e8d]705 memberExpr->get_aggregate()->accept( *visitor );
[6c4ff37]706 output << "." << mangleName( memberExpr->get_member() );
[51587aa]707 }
[71f4e4f]708
[9857e8d]709 void CodeGenerator::postvisit( VariableExpr * variableExpr ) {
[e04ef3a]710 extension( variableExpr );
[60a8062]711 const OperatorInfo * opInfo;
[b99fd56]712 if( dynamic_cast<ZeroType*>( variableExpr->get_var()->get_type() ) ) {
713 output << "0";
714 } else if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && (opInfo = operatorLookup( variableExpr->get_var()->get_name() )) && opInfo->type == OT_CONSTANT ) {
[60a8062]715 output << opInfo->symbol;
[51587aa]716 } else {
[6c4ff37]717 output << mangleName( variableExpr->get_var() );
[51587aa]718 } // if
719 }
[71f4e4f]720
[9857e8d]721 void CodeGenerator::postvisit( ConstantExpr * constantExpr ) {
[51587aa]722 assert( constantExpr->get_constant() );
[e04ef3a]723 extension( constantExpr );
[9857e8d]724 constantExpr->get_constant()->accept( *visitor );
[51587aa]725 }
[71f4e4f]726
[9857e8d]727 void CodeGenerator::postvisit( SizeofExpr * sizeofExpr ) {
[e04ef3a]728 extension( sizeofExpr );
[6c4ff37]729 output << "sizeof(";
[51587aa]730 if ( sizeofExpr->get_isType() ) {
[42a36d9]731 output << genType( sizeofExpr->get_type(), "", options );
[51587aa]732 } else {
[9857e8d]733 sizeofExpr->get_expr()->accept( *visitor );
[51587aa]734 } // if
[6c4ff37]735 output << ")";
[51587aa]736 }
[47534159]737
[9857e8d]738 void CodeGenerator::postvisit( AlignofExpr * alignofExpr ) {
[47534159]739 // use GCC extension to avoid bumping std to C11
[8e9cbb2]740 extension( alignofExpr );
[47534159]741 output << "__alignof__(";
[25a054f]742 if ( alignofExpr->get_isType() ) {
[42a36d9]743 output << genType( alignofExpr->get_type(), "", options );
[47534159]744 } else {
[9857e8d]745 alignofExpr->get_expr()->accept( *visitor );
[47534159]746 } // if
747 output << ")";
748 }
[71f4e4f]749
[9857e8d]750 void CodeGenerator::postvisit( UntypedOffsetofExpr * offsetofExpr ) {
[42a36d9]751 assertf( ! options.genC, "UntypedOffsetofExpr should not reach code generation." );
[e39241b]752 output << "offsetof(";
[42a36d9]753 output << genType( offsetofExpr->get_type(), "", options );
[e39241b]754 output << ", " << offsetofExpr->get_member();
755 output << ")";
[2a4b088]756 }
757
[9857e8d]758 void CodeGenerator::postvisit( OffsetofExpr * offsetofExpr ) {
[25a054f]759 // use GCC builtin
760 output << "__builtin_offsetof(";
[42a36d9]761 output << genType( offsetofExpr->get_type(), "", options );
[e551c69]762 output << ", " << mangleName( offsetofExpr->get_member() );
[25a054f]763 output << ")";
764 }
[d63eeb0]765
[9857e8d]766 void CodeGenerator::postvisit( OffsetPackExpr * offsetPackExpr ) {
[42a36d9]767 assertf( ! options.genC, "OffsetPackExpr should not reach code generation." );
768 output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", options ) << ")";
[afc1045]769 }
[70a06f6]770
[9857e8d]771 void CodeGenerator::postvisit( LogicalExpr * logicalExpr ) {
[e04ef3a]772 extension( logicalExpr );
[6c4ff37]773 output << "(";
[9857e8d]774 logicalExpr->get_arg1()->accept( *visitor );
[51587aa]775 if ( logicalExpr->get_isAnd() ) {
[6c4ff37]776 output << " && ";
[51587aa]777 } else {
[6c4ff37]778 output << " || ";
[51587aa]779 } // if
[9857e8d]780 logicalExpr->get_arg2()->accept( *visitor );
[6c4ff37]781 output << ")";
[51587aa]782 }
[71f4e4f]783
[9857e8d]784 void CodeGenerator::postvisit( ConditionalExpr * conditionalExpr ) {
[e04ef3a]785 extension( conditionalExpr );
[6c4ff37]786 output << "(";
[9857e8d]787 conditionalExpr->get_arg1()->accept( *visitor );
[6c4ff37]788 output << " ? ";
[9857e8d]789 conditionalExpr->get_arg2()->accept( *visitor );
[6c4ff37]790 output << " : ";
[9857e8d]791 conditionalExpr->get_arg3()->accept( *visitor );
[6c4ff37]792 output << ")";
[51587aa]793 }
[71f4e4f]794
[9857e8d]795 void CodeGenerator::postvisit( CommaExpr * commaExpr ) {
[e04ef3a]796 extension( commaExpr );
[6c4ff37]797 output << "(";
[42a36d9]798 if ( options.genC ) {
[8a6cf7e]799 // arg1 of a CommaExpr is never used, so it can be safely cast to void to reduce gcc warnings.
800 commaExpr->set_arg1( new CastExpr( commaExpr->get_arg1() ) );
801 }
[9857e8d]802 commaExpr->get_arg1()->accept( *visitor );
[6c4ff37]803 output << " , ";
[9857e8d]804 commaExpr->get_arg2()->accept( *visitor );
[6c4ff37]805 output << ")";
[51587aa]806 }
[71f4e4f]807
[9857e8d]808 void CodeGenerator::postvisit( TupleAssignExpr * tupleExpr ) {
[42a36d9]809 assertf( ! options.genC, "TupleAssignExpr should not reach code generation." );
[9857e8d]810 tupleExpr->stmtExpr->accept( *visitor );
[d104b02]811 }
812
[9857e8d]813 void CodeGenerator::postvisit( UntypedTupleExpr * tupleExpr ) {
[42a36d9]814 assertf( ! options.genC, "UntypedTupleExpr should not reach code generation." );
[f975c65]815 extension( tupleExpr );
[e39241b]816 output << "[";
817 genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() );
818 output << "]";
819 }
[907eccb]820
[9857e8d]821 void CodeGenerator::postvisit( TupleExpr * tupleExpr ) {
[42a36d9]822 assertf( ! options.genC, "TupleExpr should not reach code generation." );
[f975c65]823 extension( tupleExpr );
[e39241b]824 output << "[";
825 genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() );
826 output << "]";
827 }
[71f4e4f]828
[9857e8d]829 void CodeGenerator::postvisit( TupleIndexExpr * tupleExpr ) {
[42a36d9]830 assertf( ! options.genC, "TupleIndexExpr should not reach code generation." );
[f975c65]831 extension( tupleExpr );
[9857e8d]832 tupleExpr->get_tuple()->accept( *visitor );
[f975c65]833 output << "." << tupleExpr->get_index();
834 }
835
[9857e8d]836 void CodeGenerator::postvisit( TypeExpr * typeExpr ) {
[42a36d9]837 // if ( options.genC ) std::cerr << "typeexpr still exists: " << typeExpr << std::endl;
838 // assertf( ! options.genC, "TypeExpr should not reach code generation." );
839 if ( ! options.genC ) {
840 output << genType( typeExpr->get_type(), "", options );
[e4d829b]841 }
[e39241b]842 }
[2b6c1e0]843
[9857e8d]844 void CodeGenerator::postvisit( AsmExpr * asmExpr ) {
[665f432]845 if ( !asmExpr->inout.empty() ) {
[7f5566b]846 output << "[ ";
[665f432]847 output << asmExpr->inout;
[7f5566b]848 output << " ] ";
849 } // if
[665f432]850 asmExpr->constraint->accept( *visitor );
[7f5566b]851 output << " ( ";
[665f432]852 asmExpr->operand->accept( *visitor );
[7f5566b]853 output << " )";
854 }
855
[9857e8d]856 void CodeGenerator::postvisit( CompoundLiteralExpr *compLitExpr ) {
[fbcde64]857 assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) );
[42a36d9]858 output << "(" << genType( compLitExpr->get_result(), "", options ) << ")";
[9857e8d]859 compLitExpr->get_initializer()->accept( *visitor );
[3c13c03]860 }
861
[9857e8d]862 void CodeGenerator::postvisit( UniqueExpr * unqExpr ) {
[42a36d9]863 assertf( ! options.genC, "Unique expressions should not reach code generation." );
[d104b02]864 output << "unq<" << unqExpr->get_id() << ">{ ";
[9857e8d]865 unqExpr->get_expr()->accept( *visitor );
[d104b02]866 output << " }";
867 }
868
[9857e8d]869 void CodeGenerator::postvisit( StmtExpr * stmtExpr ) {
[44b4114]870 std::list< Statement * > & stmts = stmtExpr->statements->kids;
[d22e90f]871 output << "({" << endl;
[f7cb0bc]872 ++indent;
[3c13c03]873 unsigned int numStmts = stmts.size();
874 unsigned int i = 0;
875 for ( Statement * stmt : stmts ) {
[058f549]876 output << indent << printLabels( stmt->get_labels() );
[3c13c03]877 if ( i+1 == numStmts ) {
878 // last statement in a statement expression needs to be handled specially -
879 // cannot cast to void, otherwise the expression statement has no value
880 if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) {
[44b4114]881 exprStmt->expr->accept( *visitor );
[3c13c03]882 output << ";" << endl;
883 ++i;
884 break;
885 }
886 }
[9857e8d]887 stmt->accept( *visitor );
[3c13c03]888 output << endl;
889 if ( wantSpacing( stmt ) ) {
890 output << endl;
891 } // if
892 ++i;
893 }
[f7cb0bc]894 --indent;
[3c13c03]895 output << indent << "})";
[6eb8948]896 }
897
[4e8949f]898 void CodeGenerator::postvisit( ConstructorExpr * expr ) {
[42a36d9]899 assertf( ! options.genC, "Unique expressions should not reach code generation." );
[4e8949f]900 expr->callExpr->accept( *visitor );
901 }
902
[44b4114]903 void CodeGenerator::postvisit( DeletedExpr * expr ) {
[42a36d9]904 assertf( ! options.genC, "Deleted expressions should not reach code generation." );
[44b4114]905 expr->expr->accept( *visitor );
906 }
907
[0f79853]908 void CodeGenerator::postvisit( DefaultArgExpr * arg ) {
[42a36d9]909 assertf( ! options.genC, "Default argument expressions should not reach code generation." );
[0f79853]910 arg->expr->accept( *visitor );
911 }
912
[d807ca28]913 void CodeGenerator::postvisit( GenericExpr * expr ) {
[42a36d9]914 assertf( ! options.genC, "C11 _Generic expressions should not reach code generation." );
[d807ca28]915 output << "_Generic(";
916 expr->control->accept( *visitor );
917 output << ", ";
918 unsigned int numAssocs = expr->associations.size();
919 unsigned int i = 0;
920 for ( GenericExpr::Association & assoc : expr->associations ) {
921 if (assoc.isDefault) {
922 output << "default: ";
923 } else {
[42a36d9]924 output << genType( assoc.type, "", options ) << ": ";
[d807ca28]925 }
926 assoc.expr->accept( *visitor );
927 if ( i+1 != numAssocs ) {
928 output << ", ";
929 }
930 i++;
931 }
932 output << ")";
933 }
934
[4810867]935 // *** Statements
[9857e8d]936 void CodeGenerator::postvisit( CompoundStmt * compoundStmt ) {
[51587aa]937 std::list<Statement*> ks = compoundStmt->get_kids();
[2b6c1e0]938 output << "{" << endl;
[51587aa]939
[f7cb0bc]940 ++indent;
[51587aa]941
[7f5566b]942 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end(); i++ ) {
[cda48b6]943 output << indent << printLabels( (*i)->get_labels() );
[9857e8d]944 (*i)->accept( *visitor );
[2b6c1e0]945
[6c4ff37]946 output << endl;
[2b6c1e0]947 if ( wantSpacing( *i ) ) {
948 output << endl;
[3778cb2]949 } // if
[8688ce1]950 } // for
[f7cb0bc]951 --indent;
[51587aa]952
[cda48b6]953 output << indent << "}";
[51587aa]954 }
955
[9857e8d]956 void CodeGenerator::postvisit( ExprStmt * exprStmt ) {
[6c4ff37]957 assert( exprStmt );
[42a36d9]958 if ( options.genC ) {
[262f085f]959 // cast the top-level expression to void to reduce gcc warnings.
[8a6cf7e]960 exprStmt->set_expr( new CastExpr( exprStmt->get_expr() ) );
[262f085f]961 }
[9857e8d]962 exprStmt->get_expr()->accept( *visitor );
[3eb1653]963 output << ";";
[51587aa]964 }
965
[9857e8d]966 void CodeGenerator::postvisit( AsmStmt * asmStmt ) {
[7f5566b]967 output << "asm ";
968 if ( asmStmt->get_voltile() ) output << "volatile ";
969 if ( ! asmStmt->get_gotolabels().empty() ) output << "goto ";
970 output << "( ";
[9857e8d]971 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *visitor );
[7f5566b]972 output << " : ";
973 genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
974 output << " : ";
975 genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
976 output << " : ";
977 genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
978 if ( ! asmStmt->get_gotolabels().empty() ) {
979 output << " : ";
980 for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
981 output << *begin++;
982 if ( begin == asmStmt->get_gotolabels().end() ) break;
983 output << ", ";
984 } // for
985 } // if
[d7312ac]986 output << " );";
[7f5566b]987 }
988
[9857e8d]989 void CodeGenerator::postvisit( AsmDecl * asmDecl ) {
[e994912]990 output << "asm ";
991 AsmStmt * asmStmt = asmDecl->get_stmt();
992 output << "( ";
[9857e8d]993 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *visitor );
[d7312ac]994 output << " )";
[e994912]995 }
996
[2d019af]997 void CodeGenerator::postvisit( DirectiveDecl * directiveDecl ) {
998 output << endl << directiveDecl->get_stmt()->directive; // endl prevents spaces before directive
999 }
1000
[cc32d83]1001 void CodeGenerator::postvisit( DirectiveStmt * dirStmt ) {
[d7312ac]1002 output << endl << dirStmt->directive; // endl prevents spaces before directive
[cc32d83]1003 }
1004
[9857e8d]1005 void CodeGenerator::postvisit( IfStmt * ifStmt ) {
[7f5566b]1006 output << "if ( ";
[9857e8d]1007 ifStmt->get_condition()->accept( *visitor );
[7f5566b]1008 output << " ) ";
[51587aa]1009
[3b0bc16]1010 ifStmt->get_then()->accept( *visitor );
[51587aa]1011
[3b0bc16]1012 if ( ifStmt->get_else() != 0) {
[2b6c1e0]1013 output << " else ";
[3b0bc16]1014 ifStmt->get_else()->accept( *visitor );
[51587aa]1015 } // if
1016 }
1017
[9857e8d]1018 void CodeGenerator::postvisit( SwitchStmt * switchStmt ) {
[d7312ac]1019 output << "switch ( ";
[9857e8d]1020 switchStmt->get_condition()->accept( *visitor );
[7f5566b]1021 output << " ) ";
[71f4e4f]1022
[d22e90f]1023 output << "{" << endl;
[f7cb0bc]1024 ++indent;
[9857e8d]1025 acceptAll( switchStmt->get_statements(), *visitor );
[f7cb0bc]1026 --indent;
[cda48b6]1027 output << indent << "}";
[51587aa]1028 }
1029
[9857e8d]1030 void CodeGenerator::postvisit( CaseStmt * caseStmt ) {
[8bafacc]1031 updateLocation( caseStmt );
[1dcd9554]1032 output << indent;
[eb3261f]1033 if ( caseStmt->isDefault()) {
[2b6c1e0]1034 output << "default";
[eb3261f]1035 } else {
[2b6c1e0]1036 output << "case ";
[9857e8d]1037 caseStmt->get_condition()->accept( *visitor );
[17cd4eb]1038 } // if
[d22e90f]1039 output << ":" << endl;
[71f4e4f]1040
[51587aa]1041 std::list<Statement *> sts = caseStmt->get_statements();
1042
[f7cb0bc]1043 ++indent;
[51587aa]1044 for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end(); i++) {
[d7312ac]1045 output << indent << printLabels( (*i)->get_labels() ) ;
[9857e8d]1046 (*i)->accept( *visitor );
[6c4ff37]1047 output << endl;
[3778cb2]1048 } // for
[f7cb0bc]1049 --indent;
[51587aa]1050 }
1051
[9857e8d]1052 void CodeGenerator::postvisit( BranchStmt * branchStmt ) {
[51587aa]1053 switch ( branchStmt->get_type()) {
1054 case BranchStmt::Goto:
1055 if ( ! branchStmt->get_target().empty() )
[6c4ff37]1056 output << "goto " << branchStmt->get_target();
[71f4e4f]1057 else {
[51587aa]1058 if ( branchStmt->get_computedTarget() != 0 ) {
[6c4ff37]1059 output << "goto *";
[9857e8d]1060 branchStmt->get_computedTarget()->accept( *visitor );
[51587aa]1061 } // if
1062 } // if
1063 break;
1064 case BranchStmt::Break:
[6c4ff37]1065 output << "break";
[51587aa]1066 break;
1067 case BranchStmt::Continue:
[6c4ff37]1068 output << "continue";
[51587aa]1069 break;
[7c2a7b6]1070 case BranchStmt::FallThrough:
1071 case BranchStmt::FallThroughDefault:
[42a36d9]1072 assertf( ! options.genC, "fallthru should not reach code generation." );
[60a8062]1073 output << "fallthru";
[7c2a7b6]1074 break;
[6180274]1075 default: ; // prevent warning
[3778cb2]1076 } // switch
[7c2a7b6]1077 // print branch target for labelled break/continue/fallthru in debug mode
[42a36d9]1078 if ( ! options.genC && branchStmt->get_type() != BranchStmt::Goto ) {
[7c2a7b6]1079 if ( ! branchStmt->get_target().empty() ) {
1080 output << " " << branchStmt->get_target();
1081 } else if ( branchStmt->get_type() == BranchStmt::FallThrough ) {
1082 output << " default";
1083 }
1084 }
[2b6c1e0]1085 output << ";";
[51587aa]1086 }
1087
[9857e8d]1088 void CodeGenerator::postvisit( ReturnStmt * returnStmt ) {
[6c4ff37]1089 output << "return ";
[9857e8d]1090 maybeAccept( returnStmt->get_expr(), *visitor );
[6c4ff37]1091 output << ";";
[51587aa]1092 }
1093
[9857e8d]1094 void CodeGenerator::postvisit( ThrowStmt * throwStmt ) {
[42a36d9]1095 assertf( ! options.genC, "Throw statements should not reach code generation." );
[daf1af8]1096
1097 output << ((throwStmt->get_kind() == ThrowStmt::Terminate) ?
[60a8062]1098 "throw" : "throwResume");
[daf1af8]1099 if (throwStmt->get_expr()) {
1100 output << " ";
[9857e8d]1101 throwStmt->get_expr()->accept( *visitor );
[daf1af8]1102 }
1103 if (throwStmt->get_target()) {
1104 output << " _At ";
[9857e8d]1105 throwStmt->get_target()->accept( *visitor );
[daf1af8]1106 }
1107 output << ";";
1108 }
[e4ea10b7]1109 void CodeGenerator::postvisit( CatchStmt * stmt ) {
[42a36d9]1110 assertf( ! options.genC, "Catch statements should not reach code generation." );
[e4ea10b7]1111
1112 output << ((stmt->get_kind() == CatchStmt::Terminate) ?
[60a8062]1113 "catch" : "catchResume");
[e4ea10b7]1114 output << "( ";
1115 stmt->decl->accept( *visitor );
1116 output << " ) ";
1117
1118 if( stmt->cond ) {
1119 output << "if/when(?) (";
1120 stmt->cond->accept( *visitor );
1121 output << ") ";
1122 }
1123 stmt->body->accept( *visitor );
1124 }
1125
1126 void CodeGenerator::postvisit( WaitForStmt * stmt ) {
[42a36d9]1127 assertf( ! options.genC, "Waitfor statements should not reach code generation." );
[e4ea10b7]1128
1129 bool first = true;
1130 for( auto & clause : stmt->clauses ) {
1131 if(first) { output << "or "; first = false; }
1132 if( clause.condition ) {
1133 output << "when(";
1134 stmt->timeout.condition->accept( *visitor );
1135 output << ") ";
1136 }
1137 output << "waitfor(";
1138 clause.target.function->accept( *visitor );
1139 for( Expression * expr : clause.target.arguments ) {
1140 output << ",";
1141 expr->accept( *visitor );
1142 }
1143 output << ") ";
1144 clause.statement->accept( *visitor );
1145 }
1146
1147 if( stmt->timeout.statement ) {
1148 output << "or ";
1149 if( stmt->timeout.condition ) {
1150 output << "when(";
1151 stmt->timeout.condition->accept( *visitor );
1152 output << ") ";
1153 }
1154 output << "timeout(";
1155 stmt->timeout.time->accept( *visitor );
1156 output << ") ";
1157 stmt->timeout.statement->accept( *visitor );
1158 }
1159
1160 if( stmt->orelse.statement ) {
1161 output << "or ";
1162 if( stmt->orelse.condition ) {
1163 output << "when(";
1164 stmt->orelse.condition->accept( *visitor );
1165 output << ")";
1166 }
1167 output << "else ";
1168 stmt->orelse.statement->accept( *visitor );
1169 }
1170 }
1171
[55d6e8de]1172 void CodeGenerator::postvisit( WithStmt * with ) {
[42a36d9]1173 if ( ! options.genC ) {
[55d6e8de]1174 output << "with ( ";
1175 genCommaList( with->exprs.begin(), with->exprs.end() );
1176 output << " ) ";
1177 }
1178 with->stmt->accept( *visitor );
1179 }
[daf1af8]1180
[3b0bc16]1181 void CodeGenerator::postvisit( WhileDoStmt * whileDoStmt ) {
1182 if ( whileDoStmt->get_isDoWhile() ) {
[d7312ac]1183 output << "do";
[321a2481]1184 } else {
[d7312ac]1185 output << "while (";
[3b0bc16]1186 whileDoStmt->get_condition()->accept( *visitor );
[6c4ff37]1187 output << ")";
[51587aa]1188 } // if
[2b6c1e0]1189 output << " ";
[51587aa]1190
[3b0bc16]1191 output << CodeGenerator::printLabels( whileDoStmt->get_body()->get_labels() );
1192 whileDoStmt->get_body()->accept( *visitor );
[51587aa]1193
[cda48b6]1194 output << indent;
[51587aa]1195
[3b0bc16]1196 if ( whileDoStmt->get_isDoWhile() ) {
[d7312ac]1197 output << " while (";
[3b0bc16]1198 whileDoStmt->get_condition()->accept( *visitor );
[6c4ff37]1199 output << ");";
[51587aa]1200 } // if
1201 }
1202
[9857e8d]1203 void CodeGenerator::postvisit( ForStmt * forStmt ) {
[8e9cbb2]1204 // initialization is always hoisted, so don't bother doing anything with that
[145f1fc]1205 output << "for (;";
[51587aa]1206
[321a2481]1207 if ( forStmt->get_condition() != 0 ) {
[9857e8d]1208 forStmt->get_condition()->accept( *visitor );
[3778cb2]1209 } // if
[6c4ff37]1210 output << ";";
[51587aa]1211
[321a2481]1212 if ( forStmt->get_increment() != 0 ) {
1213 // cast the top-level expression to void to reduce gcc warnings.
1214 Expression * expr = new CastExpr( forStmt->get_increment() );
[9857e8d]1215 expr->accept( *visitor );
[3778cb2]1216 } // if
[2b6c1e0]1217 output << ") ";
[51587aa]1218
1219 if ( forStmt->get_body() != 0 ) {
[2b6c1e0]1220 output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
[9857e8d]1221 forStmt->get_body()->accept( *visitor );
[51587aa]1222 } // if
1223 }
1224
[9857e8d]1225 void CodeGenerator::postvisit( __attribute__((unused)) NullStmt * nullStmt ) {
[cda48b6]1226 //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
[6c4ff37]1227 output << "/* null statement */ ;";
[51587aa]1228 }
1229
[9857e8d]1230 void CodeGenerator::postvisit( DeclStmt * declStmt ) {
1231 declStmt->get_decl()->accept( *visitor );
[71f4e4f]1232
[51587aa]1233 if ( doSemicolon( declStmt->get_decl() ) ) {
[6c4ff37]1234 output << ";";
[51587aa]1235 } // if
1236 }
1237
[9857e8d]1238 void CodeGenerator::postvisit( ImplicitCtorDtorStmt * stmt ) {
[42a36d9]1239 assertf( ! options.genC, "ImplicitCtorDtorStmts should not reach code generation." );
[9857e8d]1240 stmt->callStmt->accept( *visitor );
1241 }
1242
[6cebfef]1243 void CodeGenerator::postvisit( MutexStmt * stmt ) {
1244 assertf( ! options.genC, "ImplicitCtorDtorStmts should not reach code generation." );
1245 stmt->stmt->accept( *visitor );
1246 }
1247
[dd020c0]1248 void CodeGenerator::handleStorageClass( DeclarationWithType * decl ) {
[fb04321]1249 if ( decl->get_storageClasses().any() ) {
[6e8bd43]1250 decl->get_storageClasses().print( output );
[a7c90d4]1251 } // if
[dd020c0]1252 } // CodeGenerator::handleStorageClass
[9facf3b]1253
1254 std::string genName( DeclarationWithType * decl ) {
[60a8062]1255 const OperatorInfo * opInfo = operatorLookup( decl->get_name() );
1256 if ( opInfo ) {
1257 return opInfo->outputName;
[9facf3b]1258 } else {
1259 return decl->get_name();
1260 } // if
1261 }
[1931bb01]1262
1263std::string genName( ast::DeclWithType const * decl ) {
1264 if ( const OperatorInfo * opInfo = operatorLookup( decl->name ) ) {
1265 return opInfo->outputName;
1266 } else {
1267 return decl->name;
1268 }
1269}
1270
[51b73452]1271} // namespace CodeGen
[51587aa]1272
1273// Local Variables: //
1274// tab-width: 4 //
1275// mode: c++ //
1276// compile-command: "make install" //
1277// End: //
Note: See TracBrowser for help on using the repository browser.