Ignore:
Timestamp:
Aug 4, 2016, 1:18:38 PM (8 years ago)
Author:
Thierry Delisle <tdelisle@…>
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:
4819cac, 658fafe4, b87e2b60
Parents:
00c32e9 (diff), 76e8c55 (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 branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    r00c32e9 rcf37a8e  
    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 11:16:21 2016
     13// Update Count     : 351
    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 );
    150150                handleStorageClass( objectDecl );
     
    162162        }
    163163
    164         void CodeGenerator::handleAggregate( AggregateDecl *aggDecl ) {
     164        void CodeGenerator::handleAggregate( AggregateDecl * aggDecl ) {
    165165                if ( aggDecl->get_name() != "" )
    166166                        output << aggDecl->get_name();
    167167
    168                 std::list< Declaration * > &memb = aggDecl->get_members();
     168                std::list< Declaration * > & memb = aggDecl->get_members();
    169169                if ( ! memb.empty() ) {
    170170//              if ( aggDecl->has_body() ) {
    171 //                      std::list< Declaration * > &memb = aggDecl->get_members();
     171//                      std::list< Declaration * > & memb = aggDecl->get_members();
    172172                        output << " {" << endl;
    173173
     
    185185        }
    186186
    187         void CodeGenerator::visit( StructDecl *structDecl ) {
     187        void CodeGenerator::visit( StructDecl * structDecl ) {
    188188                extension( structDecl );
    189189                output << "struct ";
     
    191191        }
    192192
    193         void CodeGenerator::visit( UnionDecl *unionDecl ) {
     193        void CodeGenerator::visit( UnionDecl * unionDecl ) {
    194194                extension( unionDecl );
    195195                output << "union ";
     
    197197        }
    198198
    199         void CodeGenerator::visit( EnumDecl *enumDecl ) {
     199        void CodeGenerator::visit( EnumDecl * enumDecl ) {
    200200                extension( enumDecl );
    201201                output << "enum ";
     
    211211                        cur_indent += CodeGenerator::tabsize;
    212212                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    213                                 ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
     213                                ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
    214214                                assert( obj );
    215215                                output << indent << mangleName( obj );
     
    227227        }
    228228
    229         void CodeGenerator::visit( TraitDecl *traitDecl ) {}
    230 
    231         void CodeGenerator::visit( TypedefDecl *typeDecl ) {
     229        void CodeGenerator::visit( TraitDecl * traitDecl ) {}
     230
     231        void CodeGenerator::visit( TypedefDecl * typeDecl ) {
    232232                assert( false && "Typedefs are removed and substituted in earlier passes." );
    233233                //output << "typedef ";
     
    235235        }
    236236
    237         void CodeGenerator::visit( TypeDecl *typeDecl ) {
     237        void CodeGenerator::visit( TypeDecl * typeDecl ) {
    238238                // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
    239239                // still to be done
     
    263263        }
    264264
    265         void CodeGenerator::visit( SingleInit *init ) {
     265        void CodeGenerator::visit( SingleInit * init ) {
    266266                printDesignators( init->get_designators() );
    267267                init->get_value()->accept( *this );
    268268        }
    269269
    270         void CodeGenerator::visit( ListInit *init ) {
     270        void CodeGenerator::visit( ListInit * init ) {
    271271                printDesignators( init->get_designators() );
    272272                output << "{ ";
     
    276276                } else {
    277277                        genCommaList( init->begin_initializers(), init->end_initializers() );
    278                 }
     278                } // if
    279279                output << " }";
    280280        }
    281281
    282         void CodeGenerator::visit( Constant *constant ) {
     282        void CodeGenerator::visit( Constant * constant ) {
    283283                output << constant->get_value() ;
    284284        }
    285285
    286286        //*** Expressions
    287         void CodeGenerator::visit( ApplicationExpr *applicationExpr ) {
     287        void CodeGenerator::visit( ApplicationExpr * applicationExpr ) {
    288288                extension( applicationExpr );
    289                 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
     289                if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
    290290                        OperatorInfo opInfo;
    291291                        if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
     
    299299                                        {
    300300                                                assert( arg != applicationExpr->get_args().end() );
    301                                                 if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
     301                                                if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
    302302                                                        // remove & from first assignment/ctor argument
    303303                                                        *arg = addrExpr->get_arg();
    304304                                                } else {
    305305                                                        // no address-of operator, so must be a pointer - add dereference
    306                                                         UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
     306                                                        UntypedExpr * newExpr = new UntypedExpr( new NameExpr( "*?" ) );
    307307                                                        newExpr->get_args().push_back( *arg );
    308308                                                        assert( (*arg)->get_results().size() == 1 );
     
    352352                                                // no constructors with 0 or more than 2 parameters
    353353                                                assert( false );
    354                                         }
     354                                        } // if
    355355                                        break;
    356356
     
    401401        }
    402402
    403         void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
     403        void CodeGenerator::visit( UntypedExpr * untypedExpr ) {
    404404                extension( untypedExpr );
    405                 if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
     405                if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
    406406                        OperatorInfo opInfo;
    407407                        if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
     
    472472                                } // switch
    473473                        } else {
    474                                 if ( nameExpr->get_name() == "Range" ) { // case V1 ... V2 or case V1~V2
     474                                if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2
    475475                                        assert( untypedExpr->get_args().size() == 2 );
    476476                                        (*untypedExpr->get_args().begin())->accept( *this );
     
    492492        }
    493493
    494         void CodeGenerator::visit( NameExpr *nameExpr ) {
     494        void CodeGenerator::visit( NameExpr * nameExpr ) {
    495495                extension( nameExpr );
    496496                OperatorInfo opInfo;
     
    503503        }
    504504
    505         void CodeGenerator::visit( AddressExpr *addressExpr ) {
     505        void CodeGenerator::visit( AddressExpr * addressExpr ) {
    506506                extension( addressExpr );
    507507                output << "(&";
    508508                // 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() ) ) {
     509                if ( VariableExpr * variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
    510510                        output << mangleName( variableExpr->get_var() );
    511511                } else {
     
    515515        }
    516516
    517         void CodeGenerator::visit( CastExpr *castExpr ) {
     517        void CodeGenerator::visit( CastExpr * castExpr ) {
    518518                extension( castExpr );
    519519                output << "(";
     
    533533        }
    534534
    535         void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
     535        void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {
    536536                assert( false );
    537537        }
    538538
    539         void CodeGenerator::visit( MemberExpr *memberExpr ) {
     539        void CodeGenerator::visit( MemberExpr * memberExpr ) {
    540540                extension( memberExpr );
    541541                memberExpr->get_aggregate()->accept( *this );
     
    543543        }
    544544
    545         void CodeGenerator::visit( VariableExpr *variableExpr ) {
     545        void CodeGenerator::visit( VariableExpr * variableExpr ) {
    546546                extension( variableExpr );
    547547                OperatorInfo opInfo;
     
    553553        }
    554554
    555         void CodeGenerator::visit( ConstantExpr *constantExpr ) {
     555        void CodeGenerator::visit( ConstantExpr * constantExpr ) {
    556556                assert( constantExpr->get_constant() );
    557557                extension( constantExpr );
     
    559559        }
    560560
    561         void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
     561        void CodeGenerator::visit( SizeofExpr * sizeofExpr ) {
    562562                extension( sizeofExpr );
    563563                output << "sizeof(";
     
    570570        }
    571571
    572         void CodeGenerator::visit( AlignofExpr *alignofExpr ) {
     572        void CodeGenerator::visit( AlignofExpr * alignofExpr ) {
    573573                // use GCC extension to avoid bumping std to C11
    574574                extension( alignofExpr );
     
    582582        }
    583583
    584         void CodeGenerator::visit( UntypedOffsetofExpr *offsetofExpr ) {
     584        void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) {
    585585                assert( false && "UntypedOffsetofExpr should not reach code generation." );
    586586        }
    587587
    588         void CodeGenerator::visit( OffsetofExpr *offsetofExpr ) {
     588        void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) {
    589589                // use GCC builtin
    590590                output << "__builtin_offsetof(";
     
    594594        }
    595595
    596         void CodeGenerator::visit( OffsetPackExpr *offsetPackExpr ) {
     596        void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) {
    597597                assert( false && "OffsetPackExpr should not reach code generation." );
    598598        }
    599599
    600         void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
     600        void CodeGenerator::visit( LogicalExpr * logicalExpr ) {
    601601                extension( logicalExpr );
    602602                output << "(";
     
    611611        }
    612612
    613         void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
     613        void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) {
    614614                extension( conditionalExpr );
    615615                output << "(";
     
    622622        }
    623623
    624         void CodeGenerator::visit( CommaExpr *commaExpr ) {
     624        void CodeGenerator::visit( CommaExpr * commaExpr ) {
    625625                extension( commaExpr );
    626626                output << "(";
     
    631631        }
    632632
    633         void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
    634 
    635         void CodeGenerator::visit( TypeExpr *typeExpr ) {}
    636 
    637         void CodeGenerator::visit( AsmExpr *asmExpr ) {
     633        void CodeGenerator::visit( TupleExpr * tupleExpr ) {}
     634
     635        void CodeGenerator::visit( TypeExpr * typeExpr ) {}
     636
     637        void CodeGenerator::visit( AsmExpr * asmExpr ) {
    638638                if ( asmExpr->get_inout() ) {
    639639                        output << "[ ";
     
    648648
    649649        //*** Statements
    650         void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
     650        void CodeGenerator::visit( CompoundStmt * compoundStmt ) {
    651651                std::list<Statement*> ks = compoundStmt->get_kids();
    652652                output << "{" << endl;
     
    662662                                output << endl;
    663663                        } // if
    664                 }
     664                } // for
    665665                cur_indent -= CodeGenerator::tabsize;
    666666
     
    668668        }
    669669
    670         void CodeGenerator::visit( ExprStmt *exprStmt ) {
     670        void CodeGenerator::visit( ExprStmt * exprStmt ) {
    671671                assert( exprStmt );
    672672                // cast the top-level expression to void to reduce gcc warnings.
     
    676676        }
    677677
    678         void CodeGenerator::visit( AsmStmt *asmStmt ) {
     678        void CodeGenerator::visit( AsmStmt * asmStmt ) {
    679679                output << "asm ";
    680680                if ( asmStmt->get_voltile() ) output << "volatile ";
     
    699699        }
    700700
    701         void CodeGenerator::visit( IfStmt *ifStmt ) {
     701        void CodeGenerator::visit( IfStmt * ifStmt ) {
    702702                output << "if ( ";
    703703                ifStmt->get_condition()->accept( *this );
     
    712712        }
    713713
    714         void CodeGenerator::visit( SwitchStmt *switchStmt ) {
     714        void CodeGenerator::visit( SwitchStmt * switchStmt ) {
    715715                output << "switch ( " ;
    716716                switchStmt->get_condition()->accept( *this );
     
    719719                output << "{" << std::endl;
    720720                cur_indent += CodeGenerator::tabsize;
    721 
    722                 acceptAll( switchStmt->get_branches(), *this );
    723 
     721                acceptAll( switchStmt->get_statements(), *this );
    724722                cur_indent -= CodeGenerator::tabsize;
    725 
    726723                output << indent << "}";
    727724        }
    728725
    729         void CodeGenerator::visit( CaseStmt *caseStmt ) {
     726        void CodeGenerator::visit( CaseStmt * caseStmt ) {
    730727                output << indent;
    731728                if ( caseStmt->isDefault()) {
     
    748745        }
    749746
    750         void CodeGenerator::visit( BranchStmt *branchStmt ) {
     747        void CodeGenerator::visit( BranchStmt * branchStmt ) {
    751748                switch ( branchStmt->get_type()) {
    752749                  case BranchStmt::Goto:
     
    771768
    772769
    773         void CodeGenerator::visit( ReturnStmt *returnStmt ) {
     770        void CodeGenerator::visit( ReturnStmt * returnStmt ) {
    774771                output << "return ";
    775772                maybeAccept( returnStmt->get_expr(), *this );
     
    777774        }
    778775
    779         void CodeGenerator::visit( WhileStmt *whileStmt ) {
     776        void CodeGenerator::visit( WhileStmt * whileStmt ) {
    780777                if ( whileStmt->get_isDoWhile() ) {
    781778                        output << "do" ;
     
    799796        }
    800797
    801         void CodeGenerator::visit( ForStmt *forStmt ) {
     798        void CodeGenerator::visit( ForStmt * forStmt ) {
    802799                // initialization is always hoisted, so don't bother doing anything with that
    803800                output << "for (;";
     
    821818        }
    822819
    823         void CodeGenerator::visit( NullStmt *nullStmt ) {
     820        void CodeGenerator::visit( NullStmt * nullStmt ) {
    824821                //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
    825822                output << "/* null statement */ ;";
    826823        }
    827824
    828         void CodeGenerator::visit( DeclStmt *declStmt ) {
     825        void CodeGenerator::visit( DeclStmt * declStmt ) {
    829826                declStmt->get_decl()->accept( *this );
    830827
     
    834831        }
    835832
    836         void CodeGenerator::handleStorageClass( Declaration *decl ) {
     833        void CodeGenerator::handleStorageClass( Declaration * decl ) {
    837834                switch ( decl->get_storageClass() ) {
    838835                  case DeclarationNode::Extern:
Note: See TracChangeset for help on using the changeset viewer.