Ignore:
Timestamp:
Aug 5, 2016, 11:03:04 AM (8 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
a2f920f
Parents:
5070fe4 (diff), 1b0020a (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge changes from master

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    r5070fe4 rc331406  
    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
    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                 }
     279                        genCommaList( init->begin(), init->end() );
     280                } // if
    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                                         }
     356                                        } // if
    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() == "Range" ) { // case V1 ... V2 or case V1~V2
     476                                if ( nameExpr->get_name() == "..." ) { // 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( RangeExpr * rangeExpr ) {
     497                rangeExpr->get_low()->accept( *this );
     498                output << " ... ";
     499                rangeExpr->get_high()->accept( *this );
     500        }
     501
     502        void CodeGenerator::visit( NameExpr * nameExpr ) {
    495503                extension( nameExpr );
    496504                OperatorInfo opInfo;
     
    503511        }
    504512
    505         void CodeGenerator::visit( AddressExpr *addressExpr ) {
     513        void CodeGenerator::visit( AddressExpr * addressExpr ) {
    506514                extension( addressExpr );
    507515                output << "(&";
    508516                // 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() ) ) {
     517                if ( VariableExpr * variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
    510518                        output << mangleName( variableExpr->get_var() );
    511519                } else {
     
    515523        }
    516524
    517         void CodeGenerator::visit( CastExpr *castExpr ) {
     525        void CodeGenerator::visit( CastExpr * castExpr ) {
    518526                extension( castExpr );
    519527                output << "(";
     
    533541        }
    534542
    535         void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
     543        void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {
    536544                assert( false );
    537545        }
    538546
    539         void CodeGenerator::visit( MemberExpr *memberExpr ) {
     547        void CodeGenerator::visit( MemberExpr * memberExpr ) {
    540548                extension( memberExpr );
    541549                memberExpr->get_aggregate()->accept( *this );
     
    543551        }
    544552
    545         void CodeGenerator::visit( VariableExpr *variableExpr ) {
     553        void CodeGenerator::visit( VariableExpr * variableExpr ) {
    546554                extension( variableExpr );
    547555                OperatorInfo opInfo;
     
    553561        }
    554562
    555         void CodeGenerator::visit( ConstantExpr *constantExpr ) {
     563        void CodeGenerator::visit( ConstantExpr * constantExpr ) {
    556564                assert( constantExpr->get_constant() );
    557565                extension( constantExpr );
     
    559567        }
    560568
    561         void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
     569        void CodeGenerator::visit( SizeofExpr * sizeofExpr ) {
    562570                extension( sizeofExpr );
    563571                output << "sizeof(";
     
    570578        }
    571579
    572         void CodeGenerator::visit( AlignofExpr *alignofExpr ) {
     580        void CodeGenerator::visit( AlignofExpr * alignofExpr ) {
    573581                // use GCC extension to avoid bumping std to C11
    574582                extension( alignofExpr );
     
    582590        }
    583591
    584         void CodeGenerator::visit( UntypedOffsetofExpr *offsetofExpr ) {
     592        void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) {
    585593                assert( false && "UntypedOffsetofExpr should not reach code generation." );
    586594        }
    587595
    588         void CodeGenerator::visit( OffsetofExpr *offsetofExpr ) {
     596        void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) {
    589597                // use GCC builtin
    590598                output << "__builtin_offsetof(";
     
    594602        }
    595603
    596         void CodeGenerator::visit( OffsetPackExpr *offsetPackExpr ) {
     604        void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) {
    597605                assert( false && "OffsetPackExpr should not reach code generation." );
    598606        }
    599607
    600         void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
     608        void CodeGenerator::visit( LogicalExpr * logicalExpr ) {
    601609                extension( logicalExpr );
    602610                output << "(";
     
    611619        }
    612620
    613         void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
     621        void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) {
    614622                extension( conditionalExpr );
    615623                output << "(";
     
    622630        }
    623631
    624         void CodeGenerator::visit( CommaExpr *commaExpr ) {
     632        void CodeGenerator::visit( CommaExpr * commaExpr ) {
    625633                extension( commaExpr );
    626634                output << "(";
     
    631639        }
    632640
    633         void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
    634 
    635         void CodeGenerator::visit( TypeExpr *typeExpr ) {}
    636 
    637         void CodeGenerator::visit( AsmExpr *asmExpr ) {
     641        void CodeGenerator::visit( TupleExpr * tupleExpr ) {}
     642
     643        void CodeGenerator::visit( TypeExpr * typeExpr ) {}
     644
     645        void CodeGenerator::visit( AsmExpr * asmExpr ) {
    638646                if ( asmExpr->get_inout() ) {
    639647                        output << "[ ";
     
    648656
    649657        //*** Statements
    650         void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
     658        void CodeGenerator::visit( CompoundStmt * compoundStmt ) {
    651659                std::list<Statement*> ks = compoundStmt->get_kids();
    652660                output << "{" << endl;
     
    662670                                output << endl;
    663671                        } // if
    664                 }
     672                } // for
    665673                cur_indent -= CodeGenerator::tabsize;
    666674
     
    668676        }
    669677
    670         void CodeGenerator::visit( ExprStmt *exprStmt ) {
     678        void CodeGenerator::visit( ExprStmt * exprStmt ) {
    671679                assert( exprStmt );
    672680                // cast the top-level expression to void to reduce gcc warnings.
     
    676684        }
    677685
    678         void CodeGenerator::visit( AsmStmt *asmStmt ) {
     686        void CodeGenerator::visit( AsmStmt * asmStmt ) {
    679687                output << "asm ";
    680688                if ( asmStmt->get_voltile() ) output << "volatile ";
     
    699707        }
    700708
    701         void CodeGenerator::visit( IfStmt *ifStmt ) {
     709        void CodeGenerator::visit( IfStmt * ifStmt ) {
    702710                output << "if ( ";
    703711                ifStmt->get_condition()->accept( *this );
     
    712720        }
    713721
    714         void CodeGenerator::visit( SwitchStmt *switchStmt ) {
     722        void CodeGenerator::visit( SwitchStmt * switchStmt ) {
    715723                output << "switch ( " ;
    716724                switchStmt->get_condition()->accept( *this );
     
    719727                output << "{" << std::endl;
    720728                cur_indent += CodeGenerator::tabsize;
    721 
    722                 acceptAll( switchStmt->get_branches(), *this );
    723 
     729                acceptAll( switchStmt->get_statements(), *this );
    724730                cur_indent -= CodeGenerator::tabsize;
    725 
    726731                output << indent << "}";
    727732        }
    728733
    729         void CodeGenerator::visit( CaseStmt *caseStmt ) {
     734        void CodeGenerator::visit( CaseStmt * caseStmt ) {
    730735                output << indent;
    731736                if ( caseStmt->isDefault()) {
     
    748753        }
    749754
    750         void CodeGenerator::visit( BranchStmt *branchStmt ) {
     755        void CodeGenerator::visit( BranchStmt * branchStmt ) {
    751756                switch ( branchStmt->get_type()) {
    752757                  case BranchStmt::Goto:
     
    771776
    772777
    773         void CodeGenerator::visit( ReturnStmt *returnStmt ) {
     778        void CodeGenerator::visit( ReturnStmt * returnStmt ) {
    774779                output << "return ";
    775780                maybeAccept( returnStmt->get_expr(), *this );
     
    777782        }
    778783
    779         void CodeGenerator::visit( WhileStmt *whileStmt ) {
     784        void CodeGenerator::visit( WhileStmt * whileStmt ) {
    780785                if ( whileStmt->get_isDoWhile() ) {
    781786                        output << "do" ;
     
    799804        }
    800805
    801         void CodeGenerator::visit( ForStmt *forStmt ) {
     806        void CodeGenerator::visit( ForStmt * forStmt ) {
    802807                // initialization is always hoisted, so don't bother doing anything with that
    803808                output << "for (;";
     
    821826        }
    822827
    823         void CodeGenerator::visit( NullStmt *nullStmt ) {
     828        void CodeGenerator::visit( NullStmt * nullStmt ) {
    824829                //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
    825830                output << "/* null statement */ ;";
    826831        }
    827832
    828         void CodeGenerator::visit( DeclStmt *declStmt ) {
     833        void CodeGenerator::visit( DeclStmt * declStmt ) {
    829834                declStmt->get_decl()->accept( *this );
    830835
     
    834839        }
    835840
    836         void CodeGenerator::handleStorageClass( Declaration *decl ) {
     841        void CodeGenerator::handleStorageClass( Declaration * decl ) {
    837842                switch ( decl->get_storageClass() ) {
    838843                  case DeclarationNode::Extern:
Note: See TracChangeset for help on using the changeset viewer.