source: src/CodeGen/CodeGenerator.cc@ dbfb35d

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 dbfb35d was 29cf9c8, checked in by Andrew Beach <ajbeach@…>, 8 years ago

Working, but inaccurate, line directives are being added.

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