source: src/CodeGen/CodeGenerator.cc@ 27cc24e

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 27cc24e was 615a096, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

fix BFCommon problem on gcc-4.9, and begin consistent renaming

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