source: src/CodeGen/CodeGenerator.cc @ 5ded739

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 5ded739 was 44a81853, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

first attempt at gcc attributes

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