Ignore:
Timestamp:
Aug 16, 2016, 3:20:06 PM (9 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:
1f6d4624
Parents:
950f7a7 (diff), 7880579 (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

    r950f7a7 r7527e63  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Jul 15 15:53:15 2016
    13 // Update Count     : 306
     12// Last Modified On : Thu Aug  4 13:35:30 2016
     13// Update Count     : 352
    1414//
    1515
     
    4545        bool wantSpacing( Statement * stmt) {
    4646                return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) ||
    47                         dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * > ( stmt ) || dynamic_cast< SwitchStmt *>( stmt );
    48         }
    49 
    50         void CodeGenerator::extension( Expression *expr ) {
     47                        dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * >( stmt ) || dynamic_cast< SwitchStmt *>( stmt );
     48        }
     49
     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 ) );
     
    155157                        objectDecl->get_init()->accept( *this );
    156158                } // if
     159
    157160                if ( objectDecl->get_bitfieldWidth() ) {
    158161                        output << ":";
     
    161164        }
    162165
    163         void CodeGenerator::handleAggregate( AggregateDecl *aggDecl ) {
     166        void CodeGenerator::handleAggregate( AggregateDecl * aggDecl ) {
    164167                if ( aggDecl->get_name() != "" )
    165168                        output << aggDecl->get_name();
    166169
    167                 std::list< Declaration * > &memb = aggDecl->get_members();
     170                std::list< Declaration * > & memb = aggDecl->get_members();
    168171                if ( ! memb.empty() ) {
    169172//              if ( aggDecl->has_body() ) {
    170 //                      std::list< Declaration * > &memb = aggDecl->get_members();
     173//                      std::list< Declaration * > & memb = aggDecl->get_members();
    171174                        output << " {" << endl;
    172175
    173176                        cur_indent += CodeGenerator::tabsize;
    174                         for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
     177                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) {
    175178                                output << indent;
    176179                                (*i)->accept( *this );
    177180                                output << ";" << endl;
    178                         }
     181                        } // for
    179182
    180183                        cur_indent -= CodeGenerator::tabsize;
     
    184187        }
    185188
    186         void CodeGenerator::visit( StructDecl *structDecl ) {
     189        void CodeGenerator::visit( StructDecl * structDecl ) {
    187190                extension( structDecl );
    188191                output << "struct ";
     
    190193        }
    191194
    192         void CodeGenerator::visit( UnionDecl *unionDecl ) {
     195        void CodeGenerator::visit( UnionDecl * unionDecl ) {
    193196                extension( unionDecl );
    194197                output << "union ";
     
    196199        }
    197200
    198         void CodeGenerator::visit( EnumDecl *enumDecl ) {
     201        void CodeGenerator::visit( EnumDecl * enumDecl ) {
    199202                extension( enumDecl );
    200203                output << "enum ";
     
    210213                        cur_indent += CodeGenerator::tabsize;
    211214                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    212                                 ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
     215                                ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
    213216                                assert( obj );
    214217                                output << indent << mangleName( obj );
     
    226229        }
    227230
    228         void CodeGenerator::visit( TraitDecl *traitDecl ) {}
    229 
    230         void CodeGenerator::visit( TypedefDecl *typeDecl ) {
     231        void CodeGenerator::visit( TraitDecl * traitDecl ) {}
     232
     233        void CodeGenerator::visit( TypedefDecl * typeDecl ) {
    231234                assert( false && "Typedefs are removed and substituted in earlier passes." );
    232235                //output << "typedef ";
     
    234237        }
    235238
    236         void CodeGenerator::visit( TypeDecl *typeDecl ) {
     239        void CodeGenerator::visit( TypeDecl * typeDecl ) {
    237240                // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
    238241                // still to be done
     
    257260                                (*iter)->accept( *this );
    258261                                output << "]";
    259                         }
    260                 }
     262                        } // if
     263                } // for
    261264                output << " = ";
    262265        }
    263266
    264         void CodeGenerator::visit( SingleInit *init ) {
     267        void CodeGenerator::visit( SingleInit * init ) {
    265268                printDesignators( init->get_designators() );
    266269                init->get_value()->accept( *this );
    267270        }
    268271
    269         void CodeGenerator::visit( ListInit *init ) {
     272        void CodeGenerator::visit( ListInit * init ) {
    270273                printDesignators( init->get_designators() );
    271274                output << "{ ";
    272                 if ( init->begin_initializers() == init->end_initializers() ) {
     275                if ( init->begin() == init->end() ) {
    273276                        // illegal to leave initializer list empty for scalar initializers, but always legal to have 0
    274277                        output << "0";
    275278                } else {
    276                         genCommaList( init->begin_initializers(), init->end_initializers() );
    277                 }
     279                        genCommaList( init->begin(), init->end() );
     280                } // if
    278281                output << " }";
    279282        }
    280283
    281         void CodeGenerator::visit( Constant *constant ) {
     284        void CodeGenerator::visit( Constant * constant ) {
    282285                output << constant->get_value() ;
    283286        }
    284287
    285288        //*** Expressions
    286         void CodeGenerator::visit( ApplicationExpr *applicationExpr ) {
     289        void CodeGenerator::visit( ApplicationExpr * applicationExpr ) {
    287290                extension( applicationExpr );
    288                 if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
     291                if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
    289292                        OperatorInfo opInfo;
    290293                        if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
     
    298301                                        {
    299302                                                assert( arg != applicationExpr->get_args().end() );
    300                                                 if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
     303                                                if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
    301304                                                        // remove & from first assignment/ctor argument
    302305                                                        *arg = addrExpr->get_arg();
    303306                                                } else {
    304307                                                        // no address-of operator, so must be a pointer - add dereference
    305                                                         UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
     308                                                        UntypedExpr * newExpr = new UntypedExpr( new NameExpr( "*?" ) );
    306309                                                        newExpr->get_args().push_back( *arg );
    307310                                                        assert( (*arg)->get_results().size() == 1 );
     
    317320                                        // do nothing
    318321                                        ;
    319                                 }
     322                                } // switch
    320323
    321324                                switch ( opInfo.type ) {
     
    351354                                                // no constructors with 0 or more than 2 parameters
    352355                                                assert( false );
    353                                         }
     356                                        } // if
    354357                                        break;
    355358
     
    385388                                        // there are no intrinsic definitions of 0/1 or label addresses as functions
    386389                                        assert( false );
    387                                 }
     390                                } // switch
    388391                        } else {
    389392                                varExpr->accept( *this );
     
    400403        }
    401404
    402         void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
     405        void CodeGenerator::visit( UntypedExpr * untypedExpr ) {
    403406                extension( untypedExpr );
    404                 if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
     407                if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
    405408                        OperatorInfo opInfo;
    406409                        if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
     
    417420                                  case OT_CALL:
    418421                                        assert( false );
    419 
    420422
    421423                                  case OT_CTOR:
     
    437439                                                // no constructors with 0 or more than 2 parameters
    438440                                                assert( false );
    439                                         }
     441                                        } // if
    440442                                        break;
    441443
     
    470472                                        // there are no intrinsic definitions of 0 or 1 as functions
    471473                                        assert( false );
    472                                 }
     474                                } // switch
    473475                        } else {
    474                                 nameExpr->accept( *this );
    475                                 output << "(";
    476                                 genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
    477                                 output << ")";
     476                                if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2
     477                                        assert( untypedExpr->get_args().size() == 2 );
     478                                        (*untypedExpr->get_args().begin())->accept( *this );
     479                                        output << " ... ";
     480                                        (*--untypedExpr->get_args().end())->accept( *this );
     481                                } else {                                                                // builtin routines
     482                                        nameExpr->accept( *this );
     483                                        output << "(";
     484                                        genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
     485                                        output << ")";
     486                                } // if
    478487                        } // if
    479488                } else {
     
    485494        }
    486495
    487         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 ) {
    488503                extension( nameExpr );
    489504                OperatorInfo opInfo;
     
    496511        }
    497512
    498         void CodeGenerator::visit( AddressExpr *addressExpr ) {
     513        void CodeGenerator::visit( AddressExpr * addressExpr ) {
    499514                extension( addressExpr );
    500515                output << "(&";
    501516                // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
    502                 if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
     517                if ( VariableExpr * variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
    503518                        output << mangleName( variableExpr->get_var() );
    504519                } else {
     
    508523        }
    509524
    510         void CodeGenerator::visit( CastExpr *castExpr ) {
     525        void CodeGenerator::visit( CastExpr * castExpr ) {
    511526                extension( castExpr );
    512527                output << "(";
     
    521536                        // otherwise, the cast is to an lvalue type, so the cast should be dropped, since the result of a cast is
    522537                        // never an lvalue in C
    523                 }
     538                } // if
    524539                castExpr->get_arg()->accept( *this );
    525540                output << ")";
    526541        }
    527542
    528         void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
     543        void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {
    529544                assert( false );
    530545        }
    531546
    532         void CodeGenerator::visit( MemberExpr *memberExpr ) {
     547        void CodeGenerator::visit( MemberExpr * memberExpr ) {
    533548                extension( memberExpr );
    534549                memberExpr->get_aggregate()->accept( *this );
     
    536551        }
    537552
    538         void CodeGenerator::visit( VariableExpr *variableExpr ) {
     553        void CodeGenerator::visit( VariableExpr * variableExpr ) {
    539554                extension( variableExpr );
    540555                OperatorInfo opInfo;
     
    546561        }
    547562
    548         void CodeGenerator::visit( ConstantExpr *constantExpr ) {
     563        void CodeGenerator::visit( ConstantExpr * constantExpr ) {
    549564                assert( constantExpr->get_constant() );
    550565                extension( constantExpr );
     
    552567        }
    553568
    554         void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
     569        void CodeGenerator::visit( SizeofExpr * sizeofExpr ) {
    555570                extension( sizeofExpr );
    556571                output << "sizeof(";
     
    563578        }
    564579
    565         void CodeGenerator::visit( AlignofExpr *alignofExpr ) {
     580        void CodeGenerator::visit( AlignofExpr * alignofExpr ) {
    566581                // use GCC extension to avoid bumping std to C11
    567582                extension( alignofExpr );
     
    575590        }
    576591
    577         void CodeGenerator::visit( UntypedOffsetofExpr *offsetofExpr ) {
     592        void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) {
    578593                assert( false && "UntypedOffsetofExpr should not reach code generation." );
    579594        }
    580595
    581         void CodeGenerator::visit( OffsetofExpr *offsetofExpr ) {
     596        void CodeGenerator::visit( OffsetofExpr * offsetofExpr ) {
    582597                // use GCC builtin
    583598                output << "__builtin_offsetof(";
     
    587602        }
    588603
    589         void CodeGenerator::visit( OffsetPackExpr *offsetPackExpr ) {
     604        void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) {
    590605                assert( false && "OffsetPackExpr should not reach code generation." );
    591606        }
    592607
    593         void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
     608        void CodeGenerator::visit( LogicalExpr * logicalExpr ) {
    594609                extension( logicalExpr );
    595610                output << "(";
     
    604619        }
    605620
    606         void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
     621        void CodeGenerator::visit( ConditionalExpr * conditionalExpr ) {
    607622                extension( conditionalExpr );
    608623                output << "(";
     
    615630        }
    616631
    617         void CodeGenerator::visit( CommaExpr *commaExpr ) {
     632        void CodeGenerator::visit( CommaExpr * commaExpr ) {
    618633                extension( commaExpr );
    619634                output << "(";
     
    624639        }
    625640
    626         void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
    627 
    628         void CodeGenerator::visit( TypeExpr *typeExpr ) {}
    629 
    630         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 ) {
    631646                if ( asmExpr->get_inout() ) {
    632647                        output << "[ ";
     
    641656
    642657        //*** Statements
    643         void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
     658        void CodeGenerator::visit( CompoundStmt * compoundStmt ) {
    644659                std::list<Statement*> ks = compoundStmt->get_kids();
    645660                output << "{" << endl;
     
    654669                        if ( wantSpacing( *i ) ) {
    655670                                output << endl;
    656                         }
    657                 }
     671                        } // if
     672                } // for
    658673                cur_indent -= CodeGenerator::tabsize;
    659674
     
    661676        }
    662677
    663         void CodeGenerator::visit( ExprStmt *exprStmt ) {
     678        void CodeGenerator::visit( ExprStmt * exprStmt ) {
    664679                assert( exprStmt );
    665680                // cast the top-level expression to void to reduce gcc warnings.
     
    669684        }
    670685
    671         void CodeGenerator::visit( AsmStmt *asmStmt ) {
     686        void CodeGenerator::visit( AsmStmt * asmStmt ) {
    672687                output << "asm ";
    673688                if ( asmStmt->get_voltile() ) output << "volatile ";
     
    692707        }
    693708
    694         void CodeGenerator::visit( IfStmt *ifStmt ) {
     709        void CodeGenerator::visit( IfStmt * ifStmt ) {
    695710                output << "if ( ";
    696711                ifStmt->get_condition()->accept( *this );
     
    705720        }
    706721
    707         void CodeGenerator::visit( SwitchStmt *switchStmt ) {
     722        void CodeGenerator::visit( SwitchStmt * switchStmt ) {
    708723                output << "switch ( " ;
    709724                switchStmt->get_condition()->accept( *this );
     
    712727                output << "{" << std::endl;
    713728                cur_indent += CodeGenerator::tabsize;
    714 
    715                 acceptAll( switchStmt->get_branches(), *this );
    716 
     729                acceptAll( switchStmt->get_statements(), *this );
    717730                cur_indent -= CodeGenerator::tabsize;
    718 
    719731                output << indent << "}";
    720732        }
    721733
    722         void CodeGenerator::visit( CaseStmt *caseStmt ) {
     734        void CodeGenerator::visit( CaseStmt * caseStmt ) {
    723735                output << indent;
    724736                if ( caseStmt->isDefault()) {
     
    737749                        (*i)->accept( *this );
    738750                        output << endl;
    739                 }
     751                } // for
    740752                cur_indent -= CodeGenerator::tabsize;
    741753        }
    742754
    743         void CodeGenerator::visit( BranchStmt *branchStmt ) {
     755        void CodeGenerator::visit( BranchStmt * branchStmt ) {
    744756                switch ( branchStmt->get_type()) {
    745757                  case BranchStmt::Goto:
     
    759771                        output << "continue";
    760772                        break;
    761                 }
     773                } // switch
    762774                output << ";";
    763775        }
    764776
    765777
    766         void CodeGenerator::visit( ReturnStmt *returnStmt ) {
     778        void CodeGenerator::visit( ReturnStmt * returnStmt ) {
    767779                output << "return ";
    768780                maybeAccept( returnStmt->get_expr(), *this );
     
    770782        }
    771783
    772         void CodeGenerator::visit( WhileStmt *whileStmt ) {
     784        void CodeGenerator::visit( WhileStmt * whileStmt ) {
    773785                if ( whileStmt->get_isDoWhile() ) {
    774786                        output << "do" ;
     
    792804        }
    793805
    794         void CodeGenerator::visit( ForStmt *forStmt ) {
     806        void CodeGenerator::visit( ForStmt * forStmt ) {
    795807                // initialization is always hoisted, so don't bother doing anything with that
    796808                output << "for (;";
     
    798810                if ( forStmt->get_condition() != 0 ) {
    799811                        forStmt->get_condition()->accept( *this );
    800                 }
     812                } // if
    801813                output << ";";
    802814
     
    805817                        Expression * expr = new CastExpr( forStmt->get_increment() );
    806818                        expr->accept( *this );
    807                 }
     819                } // if
    808820                output << ") ";
    809821
     
    814826        }
    815827
    816         void CodeGenerator::visit( NullStmt *nullStmt ) {
     828        void CodeGenerator::visit( NullStmt * nullStmt ) {
    817829                //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
    818830                output << "/* null statement */ ;";
    819831        }
    820832
    821         void CodeGenerator::visit( DeclStmt *declStmt ) {
     833        void CodeGenerator::visit( DeclStmt * declStmt ) {
    822834                declStmt->get_decl()->accept( *this );
    823835
     
    827839        }
    828840
    829         void CodeGenerator::handleStorageClass( Declaration *decl ) {
     841        void CodeGenerator::handleStorageClass( Declaration * decl ) {
    830842                switch ( decl->get_storageClass() ) {
    831843                  case DeclarationNode::Extern:
Note: See TracChangeset for help on using the changeset viewer.