Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    rf9cebb5 r064e3ff  
    99// Author           : Richard C. Bilson
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By :
    12 // Last Modified On : Sun Jul 31 08:42:18 2016
    13 // Update Count     : 345
     11// Last Modified By : Peter A. Buhr
     12// Last Modified On : Thu Aug  4 13:35:30 2016
     13// Update Count     : 352
    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                 }
     81                } // for
    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                                 }
     114                                } // if
    115115                                output << ",";
    116                         }
     116                        } // for
    117117                        output << ")) ";
    118                 }
     118                } // if
    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 
    152150                handleStorageClass( objectDecl );
    153151                output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
     
    164162        }
    165163
    166         void CodeGenerator::handleAggregate( AggregateDecl *aggDecl ) {
     164        void CodeGenerator::handleAggregate( AggregateDecl * aggDecl ) {
    167165                if ( aggDecl->get_name() != "" )
    168166                        output << aggDecl->get_name();
    169167
    170                 std::list< Declaration * > &memb = aggDecl->get_members();
     168                std::list< Declaration * > & memb = aggDecl->get_members();
    171169                if ( ! memb.empty() ) {
    172170//              if ( aggDecl->has_body() ) {
    173 //                      std::list< Declaration * > &memb = aggDecl->get_members();
     171//                      std::list< Declaration * > & memb = aggDecl->get_members();
    174172                        output << " {" << endl;
    175173
     
    187185        }
    188186
    189         void CodeGenerator::visit( StructDecl *structDecl ) {
     187        void CodeGenerator::visit( StructDecl * structDecl ) {
    190188                extension( structDecl );
    191189                output << "struct ";
     
    193191        }
    194192
    195         void CodeGenerator::visit( UnionDecl *unionDecl ) {
     193        void CodeGenerator::visit( UnionDecl * unionDecl ) {
    196194                extension( unionDecl );
    197195                output << "union ";
     
    199197        }
    200198
    201         void CodeGenerator::visit( EnumDecl *enumDecl ) {
     199        void CodeGenerator::visit( EnumDecl * enumDecl ) {
    202200                extension( enumDecl );
    203201                output << "enum ";
     
    213211                        cur_indent += CodeGenerator::tabsize;
    214212                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    215                                 ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
     213                                ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
    216214                                assert( obj );
    217215                                output << indent << mangleName( obj );
     
    229227        }
    230228
    231         void CodeGenerator::visit( TraitDecl *traitDecl ) {}
    232 
    233         void CodeGenerator::visit( TypedefDecl *typeDecl ) {
     229        void CodeGenerator::visit( TraitDecl * traitDecl ) {}
     230
     231        void CodeGenerator::visit( TypedefDecl * typeDecl ) {
    234232                assert( false && "Typedefs are removed and substituted in earlier passes." );
    235233                //output << "typedef ";
     
    237235        }
    238236
    239         void CodeGenerator::visit( TypeDecl *typeDecl ) {
     237        void CodeGenerator::visit( TypeDecl * typeDecl ) {
    240238                // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
    241239                // still to be done
     
    265263        }
    266264
    267         void CodeGenerator::visit( SingleInit *init ) {
     265        void CodeGenerator::visit( SingleInit * init ) {
    268266                printDesignators( init->get_designators() );
    269267                init->get_value()->accept( *this );
    270268        }
    271269
    272         void CodeGenerator::visit( ListInit *init ) {
     270        void CodeGenerator::visit( ListInit * init ) {
    273271                printDesignators( init->get_designators() );
    274272                output << "{ ";
    275                 if ( init->begin() == init->end() ) {
     273                if ( init->begin_initializers() == init->end_initializers() ) {
    276274                        // illegal to leave initializer list empty for scalar initializers, but always legal to have 0
    277275                        output << "0";
    278276                } else {
    279                         genCommaList( init->begin(), init->end() );
    280                 }
     277                        genCommaList( init->begin_initializers(), init->end_initializers() );
     278                } // if
    281279                output << " }";
    282280        }
    283281
    284         void CodeGenerator::visit( Constant *constant ) {
     282        void CodeGenerator::visit( Constant * constant ) {
    285283                output << constant->get_value() ;
    286284        }
    287285
    288286        //*** Expressions
    289         void CodeGenerator::visit( ApplicationExpr *applicationExpr ) {
     287        void CodeGenerator::visit( ApplicationExpr * applicationExpr ) {
    290288                extension( applicationExpr );
    291                 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
     289                if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
    292290                        OperatorInfo opInfo;
    293291                        if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
     
    301299                                        {
    302300                                                assert( arg != applicationExpr->get_args().end() );
    303                                                 if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
     301                                                if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
    304302                                                        // remove & from first assignment/ctor argument
    305303                                                        *arg = addrExpr->get_arg();
    306304                                                } else {
    307305                                                        // no address-of operator, so must be a pointer - add dereference
    308                                                         UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
     306                                                        UntypedExpr * newExpr = new UntypedExpr( new NameExpr( "*?" ) );
    309307                                                        newExpr->get_args().push_back( *arg );
    310308                                                        assert( (*arg)->get_results().size() == 1 );
     
    354352                                                // no constructors with 0 or more than 2 parameters
    355353                                                assert( false );
    356                                         }
     354                                        } // if
    357355                                        break;
    358356
     
    403401        }
    404402
    405         void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
     403        void CodeGenerator::visit( UntypedExpr * untypedExpr ) {
    406404                extension( untypedExpr );
    407                 if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
     405                if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
    408406                        OperatorInfo opInfo;
    409407                        if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
     
    474472                                } // switch
    475473                        } else {
    476                                 if ( nameExpr->get_name() == "Range" ) { // case V1 ... V2 or case V1~V2
     474                                if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2
    477475                                        assert( untypedExpr->get_args().size() == 2 );
    478476                                        (*untypedExpr->get_args().begin())->accept( *this );
     
    494492        }
    495493
    496         void CodeGenerator::visit( NameExpr *nameExpr ) {
     494        void CodeGenerator::visit( RangeExpr * rangeExpr ) {
     495                rangeExpr->get_low()->accept( *this );
     496                output << " ... ";
     497                rangeExpr->get_high()->accept( *this );
     498        }
     499
     500        void CodeGenerator::visit( NameExpr * nameExpr ) {
    497501                extension( nameExpr );
    498502                OperatorInfo opInfo;
     
    505509        }
    506510
    507         void CodeGenerator::visit( AddressExpr *addressExpr ) {
     511        void CodeGenerator::visit( AddressExpr * addressExpr ) {
    508512                extension( addressExpr );
    509513                output << "(&";
    510514                // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
    511                 if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
     515                if ( VariableExpr * variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
    512516                        output << mangleName( variableExpr->get_var() );
    513517                } else {
     
    517521        }
    518522
    519         void CodeGenerator::visit( CastExpr *castExpr ) {
     523        void CodeGenerator::visit( CastExpr * castExpr ) {
    520524                extension( castExpr );
    521525                output << "(";
     
    535539        }
    536540
    537         void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
     541        void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {
    538542                assert( false );
    539543        }
    540544
    541         void CodeGenerator::visit( MemberExpr *memberExpr ) {
     545        void CodeGenerator::visit( MemberExpr * memberExpr ) {
    542546                extension( memberExpr );
    543547                memberExpr->get_aggregate()->accept( *this );
     
    545549        }
    546550
    547         void CodeGenerator::visit( VariableExpr *variableExpr ) {
     551        void CodeGenerator::visit( VariableExpr * variableExpr ) {
    548552                extension( variableExpr );
    549553                OperatorInfo opInfo;
     
    555559        }
    556560
    557         void CodeGenerator::visit( ConstantExpr *constantExpr ) {
     561        void CodeGenerator::visit( ConstantExpr * constantExpr ) {
    558562                assert( constantExpr->get_constant() );
    559563                extension( constantExpr );
     
    561565        }
    562566
    563         void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
     567        void CodeGenerator::visit( SizeofExpr * sizeofExpr ) {
    564568                extension( sizeofExpr );
    565569                output << "sizeof(";
     
    572576        }
    573577
    574         void CodeGenerator::visit( AlignofExpr *alignofExpr ) {
     578        void CodeGenerator::visit( AlignofExpr * alignofExpr ) {
    575579                // use GCC extension to avoid bumping std to C11
    576580                extension( alignofExpr );
     
    584588        }
    585589
    586         void CodeGenerator::visit( UntypedOffsetofExpr *offsetofExpr ) {
     590        void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) {
    587591                assert( false && "UntypedOffsetofExpr should not reach code generation." );
    588592        }
    589593
    590         void CodeGenerator::visit( OffsetofExpr *offsetofExpr ) {
     594        void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) {
    591595                // use GCC builtin
    592596                output << "__builtin_offsetof(";
     
    596600        }
    597601
    598         void CodeGenerator::visit( OffsetPackExpr *offsetPackExpr ) {
     602        void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) {
    599603                assert( false && "OffsetPackExpr should not reach code generation." );
    600604        }
    601605
    602         void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
     606        void CodeGenerator::visit( LogicalExpr * logicalExpr ) {
    603607                extension( logicalExpr );
    604608                output << "(";
     
    613617        }
    614618
    615         void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
     619        void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) {
    616620                extension( conditionalExpr );
    617621                output << "(";
     
    624628        }
    625629
    626         void CodeGenerator::visit( CommaExpr *commaExpr ) {
     630        void CodeGenerator::visit( CommaExpr * commaExpr ) {
    627631                extension( commaExpr );
    628632                output << "(";
     
    633637        }
    634638
    635         void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
    636 
    637         void CodeGenerator::visit( TypeExpr *typeExpr ) {}
    638 
    639         void CodeGenerator::visit( AsmExpr *asmExpr ) {
     639        void CodeGenerator::visit( TupleExpr * tupleExpr ) {}
     640
     641        void CodeGenerator::visit( TypeExpr * typeExpr ) {}
     642
     643        void CodeGenerator::visit( AsmExpr * asmExpr ) {
    640644                if ( asmExpr->get_inout() ) {
    641645                        output << "[ ";
     
    650654
    651655        //*** Statements
    652         void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
     656        void CodeGenerator::visit( CompoundStmt * compoundStmt ) {
    653657                std::list<Statement*> ks = compoundStmt->get_kids();
    654658                output << "{" << endl;
     
    664668                                output << endl;
    665669                        } // if
    666                 }
     670                } // for
    667671                cur_indent -= CodeGenerator::tabsize;
    668672
     
    670674        }
    671675
    672         void CodeGenerator::visit( ExprStmt *exprStmt ) {
     676        void CodeGenerator::visit( ExprStmt * exprStmt ) {
    673677                assert( exprStmt );
    674678                // cast the top-level expression to void to reduce gcc warnings.
     
    678682        }
    679683
    680         void CodeGenerator::visit( AsmStmt *asmStmt ) {
     684        void CodeGenerator::visit( AsmStmt * asmStmt ) {
    681685                output << "asm ";
    682686                if ( asmStmt->get_voltile() ) output << "volatile ";
     
    701705        }
    702706
    703         void CodeGenerator::visit( IfStmt *ifStmt ) {
     707        void CodeGenerator::visit( IfStmt * ifStmt ) {
    704708                output << "if ( ";
    705709                ifStmt->get_condition()->accept( *this );
     
    714718        }
    715719
    716         void CodeGenerator::visit( SwitchStmt *switchStmt ) {
     720        void CodeGenerator::visit( SwitchStmt * switchStmt ) {
    717721                output << "switch ( " ;
    718722                switchStmt->get_condition()->accept( *this );
     
    721725                output << "{" << std::endl;
    722726                cur_indent += CodeGenerator::tabsize;
    723 
    724                 acceptAll( switchStmt->get_branches(), *this );
    725 
     727                acceptAll( switchStmt->get_statements(), *this );
    726728                cur_indent -= CodeGenerator::tabsize;
    727 
    728729                output << indent << "}";
    729730        }
    730731
    731         void CodeGenerator::visit( CaseStmt *caseStmt ) {
     732        void CodeGenerator::visit( CaseStmt * caseStmt ) {
    732733                output << indent;
    733734                if ( caseStmt->isDefault()) {
     
    750751        }
    751752
    752         void CodeGenerator::visit( BranchStmt *branchStmt ) {
     753        void CodeGenerator::visit( BranchStmt * branchStmt ) {
    753754                switch ( branchStmt->get_type()) {
    754755                  case BranchStmt::Goto:
     
    773774
    774775
    775         void CodeGenerator::visit( ReturnStmt *returnStmt ) {
     776        void CodeGenerator::visit( ReturnStmt * returnStmt ) {
    776777                output << "return ";
    777778                maybeAccept( returnStmt->get_expr(), *this );
     
    779780        }
    780781
    781         void CodeGenerator::visit( WhileStmt *whileStmt ) {
     782        void CodeGenerator::visit( WhileStmt * whileStmt ) {
    782783                if ( whileStmt->get_isDoWhile() ) {
    783784                        output << "do" ;
     
    801802        }
    802803
    803         void CodeGenerator::visit( ForStmt *forStmt ) {
     804        void CodeGenerator::visit( ForStmt * forStmt ) {
    804805                // initialization is always hoisted, so don't bother doing anything with that
    805806                output << "for (;";
     
    823824        }
    824825
    825         void CodeGenerator::visit( NullStmt *nullStmt ) {
     826        void CodeGenerator::visit( NullStmt * nullStmt ) {
    826827                //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
    827828                output << "/* null statement */ ;";
    828829        }
    829830
    830         void CodeGenerator::visit( DeclStmt *declStmt ) {
     831        void CodeGenerator::visit( DeclStmt * declStmt ) {
    831832                declStmt->get_decl()->accept( *this );
    832833
     
    836837        }
    837838
    838         void CodeGenerator::handleStorageClass( Declaration *decl ) {
     839        void CodeGenerator::handleStorageClass( Declaration * decl ) {
    839840                switch ( decl->get_storageClass() ) {
    840841                  case DeclarationNode::Extern:
Note: See TracChangeset for help on using the changeset viewer.