Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    r8688ce1 rf9cebb5  
    99// Author           : Richard C. Bilson
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Aug  4 11:16:21 2016
    13 // Update Count     : 351
     11// Last Modified By :
     12// Last Modified On : Sun Jul 31 08:42:18 2016
     13// Update Count     : 345
    1414//
    1515
     
    4848        }
    4949
    50         void CodeGenerator::extension( Expression * expr ) {
     50        void CodeGenerator::extension( Expression *expr ) {
    5151                if ( expr->get_extension() ) {
    5252                        output << "__extension__ ";
     
    5454        } // extension
    5555
    56         void CodeGenerator::extension( Declaration * decl ) {
     56        void CodeGenerator::extension( Declaration *decl ) {
    5757                if ( decl->get_extension() ) {
    5858                        output << "__extension__ ";
     
    7373        }
    7474
    75         ostream & operator<<( ostream & output, CodeGenerator::LabelPrinter & printLabels ) {
     75        ostream & operator<<( ostream & output, CodeGenerator::LabelPrinter &printLabels ) {
    7676                std::list< Label > & labs = *printLabels.labels;
    7777                // l.unique(); // assumes a sorted list. Why not use set? Does order matter?
     
    7979                        output << l.get_name() + ": ";
    8080                        printLabels.cg.genAttributes( l.get_attributes() );
    81                 } // for
     81                }
    8282                return output;
    8383        }
    8484
    85         CodeGenerator::CodeGenerator( std::ostream & os ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ), printLabels( *this ) {}
    86 
    87         CodeGenerator::CodeGenerator( std::ostream & os, std::string init, int indentation, bool infunp )
     85        CodeGenerator::CodeGenerator( std::ostream &os ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ), printLabels( *this ) {}
     86
     87        CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indentation, bool infunp )
    8888                        : indent( *this), cur_indent( indentation ), insideFunction( infunp ), output( os ), printLabels( *this ) {
    8989                //output << std::string( init );
    9090        }
    9191
    92         CodeGenerator::CodeGenerator( std::ostream & os, char * init, int indentation, bool infunp )
     92        CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indentation, bool infunp )
    9393                        : indent( *this ), cur_indent( indentation ), insideFunction( infunp ), output( os ), printLabels( *this ) {
    9494                //output << std::string( init );
    9595        }
    9696
    97         string mangleName( DeclarationWithType * decl ) {
     97        string mangleName( DeclarationWithType *decl ) {
    9898                if ( decl->get_mangleName() != "" ) {
    9999                        // need to incorporate scope level in order to differentiate names for destructors
     
    112112                                        genCommaList( attr->get_parameters().begin(), attr->get_parameters().end() );
    113113                                        output << ")";
    114                                 } // if
     114                                }
    115115                                output << ",";
    116                         } // for
     116                        }
    117117                        output << ")) ";
    118                 } // if
     118                }
    119119        }
    120120
    121121
    122122        //*** Declarations
    123         void CodeGenerator::visit( FunctionDecl * functionDecl ) {
     123        void CodeGenerator::visit( FunctionDecl *functionDecl ) {
    124124                extension( functionDecl );
    125125                genAttributes( functionDecl->get_attributes() );
     
    146146        }
    147147
    148         void CodeGenerator::visit( ObjectDecl * objectDecl ) {
     148        void CodeGenerator::visit( ObjectDecl *objectDecl ) {
    149149                extension( objectDecl );
     150                genAttributes( objectDecl->get_attributes() );
     151
    150152                handleStorageClass( objectDecl );
    151153                output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
     
    162164        }
    163165
    164         void CodeGenerator::handleAggregate( AggregateDecl * aggDecl ) {
     166        void CodeGenerator::handleAggregate( AggregateDecl *aggDecl ) {
    165167                if ( aggDecl->get_name() != "" )
    166168                        output << aggDecl->get_name();
    167169
    168                 std::list< Declaration * > & memb = aggDecl->get_members();
     170                std::list< Declaration * > &memb = aggDecl->get_members();
    169171                if ( ! memb.empty() ) {
    170172//              if ( aggDecl->has_body() ) {
    171 //                      std::list< Declaration * > & memb = aggDecl->get_members();
     173//                      std::list< Declaration * > &memb = aggDecl->get_members();
    172174                        output << " {" << endl;
    173175
     
    185187        }
    186188
    187         void CodeGenerator::visit( StructDecl * structDecl ) {
     189        void CodeGenerator::visit( StructDecl *structDecl ) {
    188190                extension( structDecl );
    189191                output << "struct ";
     
    191193        }
    192194
    193         void CodeGenerator::visit( UnionDecl * unionDecl ) {
     195        void CodeGenerator::visit( UnionDecl *unionDecl ) {
    194196                extension( unionDecl );
    195197                output << "union ";
     
    197199        }
    198200
    199         void CodeGenerator::visit( EnumDecl * enumDecl ) {
     201        void CodeGenerator::visit( EnumDecl *enumDecl ) {
    200202                extension( enumDecl );
    201203                output << "enum ";
     
    211213                        cur_indent += CodeGenerator::tabsize;
    212214                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    213                                 ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
     215                                ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
    214216                                assert( obj );
    215217                                output << indent << mangleName( obj );
     
    227229        }
    228230
    229         void CodeGenerator::visit( TraitDecl * traitDecl ) {}
    230 
    231         void CodeGenerator::visit( TypedefDecl * typeDecl ) {
     231        void CodeGenerator::visit( TraitDecl *traitDecl ) {}
     232
     233        void CodeGenerator::visit( TypedefDecl *typeDecl ) {
    232234                assert( false && "Typedefs are removed and substituted in earlier passes." );
    233235                //output << "typedef ";
     
    235237        }
    236238
    237         void CodeGenerator::visit( TypeDecl * typeDecl ) {
     239        void CodeGenerator::visit( TypeDecl *typeDecl ) {
    238240                // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
    239241                // still to be done
     
    263265        }
    264266
    265         void CodeGenerator::visit( SingleInit * init ) {
     267        void CodeGenerator::visit( SingleInit *init ) {
    266268                printDesignators( init->get_designators() );
    267269                init->get_value()->accept( *this );
    268270        }
    269271
    270         void CodeGenerator::visit( ListInit * init ) {
     272        void CodeGenerator::visit( ListInit *init ) {
    271273                printDesignators( init->get_designators() );
    272274                output << "{ ";
    273                 if ( init->begin_initializers() == init->end_initializers() ) {
     275                if ( init->begin() == init->end() ) {
    274276                        // illegal to leave initializer list empty for scalar initializers, but always legal to have 0
    275277                        output << "0";
    276278                } else {
    277                         genCommaList( init->begin_initializers(), init->end_initializers() );
    278                 } // if
     279                        genCommaList( init->begin(), init->end() );
     280                }
    279281                output << " }";
    280282        }
    281283
    282         void CodeGenerator::visit( Constant * constant ) {
     284        void CodeGenerator::visit( Constant *constant ) {
    283285                output << constant->get_value() ;
    284286        }
    285287
    286288        //*** Expressions
    287         void CodeGenerator::visit( ApplicationExpr * applicationExpr ) {
     289        void CodeGenerator::visit( ApplicationExpr *applicationExpr ) {
    288290                extension( applicationExpr );
    289                 if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
     291                if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
    290292                        OperatorInfo opInfo;
    291293                        if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
     
    299301                                        {
    300302                                                assert( arg != applicationExpr->get_args().end() );
    301                                                 if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
     303                                                if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
    302304                                                        // remove & from first assignment/ctor argument
    303305                                                        *arg = addrExpr->get_arg();
    304306                                                } else {
    305307                                                        // no address-of operator, so must be a pointer - add dereference
    306                                                         UntypedExpr * newExpr = new UntypedExpr( new NameExpr( "*?" ) );
     308                                                        UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
    307309                                                        newExpr->get_args().push_back( *arg );
    308310                                                        assert( (*arg)->get_results().size() == 1 );
     
    352354                                                // no constructors with 0 or more than 2 parameters
    353355                                                assert( false );
    354                                         } // if
     356                                        }
    355357                                        break;
    356358
     
    401403        }
    402404
    403         void CodeGenerator::visit( UntypedExpr * untypedExpr ) {
     405        void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
    404406                extension( untypedExpr );
    405                 if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
     407                if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
    406408                        OperatorInfo opInfo;
    407409                        if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
     
    472474                                } // switch
    473475                        } else {
    474                                 if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2
     476                                if ( nameExpr->get_name() == "Range" ) { // case V1 ... V2 or case V1~V2
    475477                                        assert( untypedExpr->get_args().size() == 2 );
    476478                                        (*untypedExpr->get_args().begin())->accept( *this );
     
    492494        }
    493495
    494         void CodeGenerator::visit( NameExpr * nameExpr ) {
     496        void CodeGenerator::visit( NameExpr *nameExpr ) {
    495497                extension( nameExpr );
    496498                OperatorInfo opInfo;
     
    503505        }
    504506
    505         void CodeGenerator::visit( AddressExpr * addressExpr ) {
     507        void CodeGenerator::visit( AddressExpr *addressExpr ) {
    506508                extension( addressExpr );
    507509                output << "(&";
    508510                // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
    509                 if ( VariableExpr * variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
     511                if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
    510512                        output << mangleName( variableExpr->get_var() );
    511513                } else {
     
    515517        }
    516518
    517         void CodeGenerator::visit( CastExpr * castExpr ) {
     519        void CodeGenerator::visit( CastExpr *castExpr ) {
    518520                extension( castExpr );
    519521                output << "(";
     
    533535        }
    534536
    535         void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {
     537        void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
    536538                assert( false );
    537539        }
    538540
    539         void CodeGenerator::visit( MemberExpr * memberExpr ) {
     541        void CodeGenerator::visit( MemberExpr *memberExpr ) {
    540542                extension( memberExpr );
    541543                memberExpr->get_aggregate()->accept( *this );
     
    543545        }
    544546
    545         void CodeGenerator::visit( VariableExpr * variableExpr ) {
     547        void CodeGenerator::visit( VariableExpr *variableExpr ) {
    546548                extension( variableExpr );
    547549                OperatorInfo opInfo;
     
    553555        }
    554556
    555         void CodeGenerator::visit( ConstantExpr * constantExpr ) {
     557        void CodeGenerator::visit( ConstantExpr *constantExpr ) {
    556558                assert( constantExpr->get_constant() );
    557559                extension( constantExpr );
     
    559561        }
    560562
    561         void CodeGenerator::visit( SizeofExpr * sizeofExpr ) {
     563        void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
    562564                extension( sizeofExpr );
    563565                output << "sizeof(";
     
    570572        }
    571573
    572         void CodeGenerator::visit( AlignofExpr * alignofExpr ) {
     574        void CodeGenerator::visit( AlignofExpr *alignofExpr ) {
    573575                // use GCC extension to avoid bumping std to C11
    574576                extension( alignofExpr );
     
    582584        }
    583585
    584         void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) {
     586        void CodeGenerator::visit( UntypedOffsetofExpr *offsetofExpr ) {
    585587                assert( false && "UntypedOffsetofExpr should not reach code generation." );
    586588        }
    587589
    588         void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) {
     590        void CodeGenerator::visit( OffsetofExpr *offsetofExpr ) {
    589591                // use GCC builtin
    590592                output << "__builtin_offsetof(";
     
    594596        }
    595597
    596         void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) {
     598        void CodeGenerator::visit( OffsetPackExpr *offsetPackExpr ) {
    597599                assert( false && "OffsetPackExpr should not reach code generation." );
    598600        }
    599601
    600         void CodeGenerator::visit( LogicalExpr * logicalExpr ) {
     602        void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
    601603                extension( logicalExpr );
    602604                output << "(";
     
    611613        }
    612614
    613         void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) {
     615        void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
    614616                extension( conditionalExpr );
    615617                output << "(";
     
    622624        }
    623625
    624         void CodeGenerator::visit( CommaExpr * commaExpr ) {
     626        void CodeGenerator::visit( CommaExpr *commaExpr ) {
    625627                extension( commaExpr );
    626628                output << "(";
     
    631633        }
    632634
    633         void CodeGenerator::visit( TupleExpr * tupleExpr ) {}
    634 
    635         void CodeGenerator::visit( TypeExpr * typeExpr ) {}
    636 
    637         void CodeGenerator::visit( AsmExpr * asmExpr ) {
     635        void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
     636
     637        void CodeGenerator::visit( TypeExpr *typeExpr ) {}
     638
     639        void CodeGenerator::visit( AsmExpr *asmExpr ) {
    638640                if ( asmExpr->get_inout() ) {
    639641                        output << "[ ";
     
    648650
    649651        //*** Statements
    650         void CodeGenerator::visit( CompoundStmt * compoundStmt ) {
     652        void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
    651653                std::list<Statement*> ks = compoundStmt->get_kids();
    652654                output << "{" << endl;
     
    662664                                output << endl;
    663665                        } // if
    664                 } // for
     666                }
    665667                cur_indent -= CodeGenerator::tabsize;
    666668
     
    668670        }
    669671
    670         void CodeGenerator::visit( ExprStmt * exprStmt ) {
     672        void CodeGenerator::visit( ExprStmt *exprStmt ) {
    671673                assert( exprStmt );
    672674                // cast the top-level expression to void to reduce gcc warnings.
     
    676678        }
    677679
    678         void CodeGenerator::visit( AsmStmt * asmStmt ) {
     680        void CodeGenerator::visit( AsmStmt *asmStmt ) {
    679681                output << "asm ";
    680682                if ( asmStmt->get_voltile() ) output << "volatile ";
     
    699701        }
    700702
    701         void CodeGenerator::visit( IfStmt * ifStmt ) {
     703        void CodeGenerator::visit( IfStmt *ifStmt ) {
    702704                output << "if ( ";
    703705                ifStmt->get_condition()->accept( *this );
     
    712714        }
    713715
    714         void CodeGenerator::visit( SwitchStmt * switchStmt ) {
     716        void CodeGenerator::visit( SwitchStmt *switchStmt ) {
    715717                output << "switch ( " ;
    716718                switchStmt->get_condition()->accept( *this );
     
    719721                output << "{" << std::endl;
    720722                cur_indent += CodeGenerator::tabsize;
    721                 acceptAll( switchStmt->get_statements(), *this );
     723
     724                acceptAll( switchStmt->get_branches(), *this );
     725
    722726                cur_indent -= CodeGenerator::tabsize;
     727
    723728                output << indent << "}";
    724729        }
    725730
    726         void CodeGenerator::visit( CaseStmt * caseStmt ) {
     731        void CodeGenerator::visit( CaseStmt *caseStmt ) {
    727732                output << indent;
    728733                if ( caseStmt->isDefault()) {
     
    745750        }
    746751
    747         void CodeGenerator::visit( BranchStmt * branchStmt ) {
     752        void CodeGenerator::visit( BranchStmt *branchStmt ) {
    748753                switch ( branchStmt->get_type()) {
    749754                  case BranchStmt::Goto:
     
    768773
    769774
    770         void CodeGenerator::visit( ReturnStmt * returnStmt ) {
     775        void CodeGenerator::visit( ReturnStmt *returnStmt ) {
    771776                output << "return ";
    772777                maybeAccept( returnStmt->get_expr(), *this );
     
    774779        }
    775780
    776         void CodeGenerator::visit( WhileStmt * whileStmt ) {
     781        void CodeGenerator::visit( WhileStmt *whileStmt ) {
    777782                if ( whileStmt->get_isDoWhile() ) {
    778783                        output << "do" ;
     
    796801        }
    797802
    798         void CodeGenerator::visit( ForStmt * forStmt ) {
     803        void CodeGenerator::visit( ForStmt *forStmt ) {
    799804                // initialization is always hoisted, so don't bother doing anything with that
    800805                output << "for (;";
     
    818823        }
    819824
    820         void CodeGenerator::visit( NullStmt * nullStmt ) {
     825        void CodeGenerator::visit( NullStmt *nullStmt ) {
    821826                //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
    822827                output << "/* null statement */ ;";
    823828        }
    824829
    825         void CodeGenerator::visit( DeclStmt * declStmt ) {
     830        void CodeGenerator::visit( DeclStmt *declStmt ) {
    826831                declStmt->get_decl()->accept( *this );
    827832
     
    831836        }
    832837
    833         void CodeGenerator::handleStorageClass( Declaration * decl ) {
     838        void CodeGenerator::handleStorageClass( Declaration *decl ) {
    834839                switch ( decl->get_storageClass() ) {
    835840                  case DeclarationNode::Extern:
Note: See TracChangeset for help on using the changeset viewer.