source: src/CodeGen/CodeGenerator.cc@ a64644c

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 a64644c was fc638d2, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

fixed StmtExpr code in PolyMutator, added missing copy constructor, misc documentation

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