source: src/CodeGen/CodeGenerator.cc@ 6ac2ada

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 6ac2ada was 6e300d9, checked in by Andrew Beach <ajbeach@…>, 9 years ago

Updated last update marker.

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