source: src/CodeGen/CodeGenerator.cc@ 658fafe4

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox memory 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 658fafe4 was 8688ce1, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

move case-list management into parser

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