Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    r7f5566b re45215c  
    99// Author           : Richard C. Bilson
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Jul 27 14:40:06 2015
    13 // Update Count     : 218
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Wed Jul 15 14:47:42 2015
     13// Update Count     : 177
    1414//
    1515
     
    5252        }
    5353
    54         CodeGenerator::CodeGenerator( std::ostream &os ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ) { }
     54        CodeGenerator::CodeGenerator( std::ostream &os ) : indent(*this), cur_indent( 0 ), insideFunction( false ), output( os ) { }
    5555
    5656        CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indentation, bool infunp )
    57                         : indent( *this), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
     57                        : indent(*this), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
    5858                //output << std::string( init );
    5959        }
    6060
    6161        CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indentation, bool infunp )
    62                         : indent( *this ), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
     62                        : indent(*this), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
    6363                //output << std::string( init );
    6464        }
     
    9191                // acceptAll( functionDecl->get_oldDecls(), *this );
    9292                if ( functionDecl->get_statements() ) {
    93                         functionDecl->get_statements()->accept( *this );
     93                        functionDecl->get_statements()->accept(*this );
    9494                } // if
    9595        }
     
    121121                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    122122                                output << indent;
    123                                 (*i)->accept( *this );
     123                                (*i)->accept(*this );
    124124                                output << ";" << endl;
    125125                        }
     
    159159                                if ( obj->get_init() ) {
    160160                                        output << " = ";
    161                                         obj->get_init()->accept( *this );
     161                                        obj->get_init()->accept(*this );
    162162                                } // if
    163163                                output << "," << endl;
     
    186186        }
    187187
     188        void CodeGenerator::printDesignators( std::list< Expression * > & designators ) {
     189                typedef std::list< Expression * > DesignatorList;
     190                if ( designators.size() == 0 ) return;
     191                for ( DesignatorList::iterator iter = designators.begin(); iter != designators.end(); ++iter ) {
     192                        // if expression is a name, then initializing aggregate member, if constant expression then array
     193                        if ( NameExpr * nm = dynamic_cast< NameExpr * >( *iter ) ) {
     194                                if ( nm->get_name() == "0" || nm->get_name() == "1" ) {
     195                                        // except if name is 0 or 1...
     196                                        output << "[";
     197                                        nm->accept( *this );
     198                                        output << "]";
     199                                } else {
     200                                        output << ".";
     201                                        nm->accept( *this );
     202                                }
     203                        } else if ( dynamic_cast< ConstantExpr * >( *iter ) ) {
     204                                output << "[";
     205                                (*iter)->accept( *this );
     206                                output << "]";
     207                        } else {
     208                                assert(0);
     209                        }
     210                }
     211                output << " = ";
     212        }
     213
    188214        void CodeGenerator::visit( SingleInit *init ) {
     215                printDesignators( init->get_designators() );
    189216                init->get_value()->accept( *this );
    190217        }
    191218
    192219        void CodeGenerator::visit( ListInit *init ) {
     220                printDesignators( init->get_designators() );
    193221                output << "{ ";
    194222                genCommaList( init->begin_initializers(), init->end_initializers() );
     
    450478        void CodeGenerator::visit( TypeExpr *typeExpr ) {}
    451479
    452         void CodeGenerator::visit( AsmExpr *asmExpr ) {
    453                 if ( asmExpr->get_inout() ) {
    454                         output << "[ ";
    455                         asmExpr->get_inout()->accept( *this );
    456                         output << " ] ";
    457                 } // if
    458                 asmExpr->get_constraint()->accept( *this );
    459                 output << " ( ";
    460                 asmExpr->get_operand()->accept( *this );
    461                 output << " )";
    462         }
    463 
    464480        //*** Statements
    465481        void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
     
    469485                cur_indent += CodeGenerator::tabsize;
    470486
    471                 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++ ) {
     487                for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++) {
    472488                        output << indent << printLabels( (*i)->get_labels() );
    473                         (*i)->accept( *this );
     489                        (*i)->accept(*this );
    474490
    475491                        output << endl;
     
    495511        }
    496512
    497         void CodeGenerator::visit( AsmStmt *asmStmt ) {
    498                 output << "asm ";
    499                 if ( asmStmt->get_voltile() ) output << "volatile ";
    500                 if ( ! asmStmt->get_gotolabels().empty()  ) output << "goto ";
    501                 output << "( ";
    502                 if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
    503                 output << " : ";
    504                 genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
    505                 output << " : ";
    506                 genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
    507                 output << " : ";
    508                 genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
    509                 if ( ! asmStmt->get_gotolabels().empty() ) {
    510                         output << " : ";
    511                         for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
    512                                 output << *begin++;
    513                                 if ( begin == asmStmt->get_gotolabels().end() ) break;
    514                                 output << ", ";
    515                         } // for
    516                 } // if
    517                 output << " );" ;
    518         }
    519 
    520513        void CodeGenerator::visit( IfStmt *ifStmt ) {
    521                 output << "if ( ";
    522                 ifStmt->get_condition()->accept( *this );
    523                 output << " ) ";
    524 
    525                 ifStmt->get_thenPart()->accept( *this );
     514                output << "if (";
     515                ifStmt->get_condition()->accept(*this );
     516                output << ") ";
     517
     518                ifStmt->get_thenPart()->accept(*this );
    526519
    527520                if ( ifStmt->get_elsePart() != 0) {
    528521                        output << " else ";
    529                         ifStmt->get_elsePart()->accept( *this );
     522                        ifStmt->get_elsePart()->accept(*this );
    530523                } // if
    531524        }
    532525
    533526        void CodeGenerator::visit( SwitchStmt *switchStmt ) {
    534                 output << "switch ( " ;
    535                 switchStmt->get_condition()->accept( *this );
    536                 output << " ) ";
     527                output << "switch (" ;
     528                switchStmt->get_condition()->accept(*this );
     529                output << ") ";
    537530               
    538531                output << "{" << std::endl;
     
    552545                } else {
    553546                        output << "case ";
    554                         caseStmt->get_condition()->accept( *this );
     547                        caseStmt->get_condition()->accept(*this );
    555548                } // if
    556549                output << ":\n";
     
    561554                for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end();  i++) {
    562555                        output << indent << printLabels( (*i)->get_labels() )  ;
    563                         (*i)->accept( *this );
     556                        (*i)->accept(*this );
    564557                        output << endl;
    565558                }
     
    605598                else {
    606599                        output << "while (" ;
    607                         whileStmt->get_condition()->accept( *this );
     600                        whileStmt->get_condition()->accept(*this );
    608601                        output << ")";
    609602                } // if
     
    617610                if ( whileStmt->get_isDoWhile() ) {
    618611                        output << " while (" ;
    619                         whileStmt->get_condition()->accept( *this );
     612                        whileStmt->get_condition()->accept(*this );
    620613                        output << ");";
    621614                } // if
Note: See TracChangeset for help on using the changeset viewer.