source: src/CodeGen/CodeGenerator.cc @ 7baed7d

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 7baed7d was 7baed7d, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

generalize notion of a GCC attribute

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