source: src/CodeGen/CodeGenerator.cc@ f31cb3e

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 f31cb3e was e994912, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

code generation for external asm statement (declaration)

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