Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    r2794fff rf32c7f4  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Rob Schluntz
    12 // Last Modified On : Wed Jul 15 14:47:42 2015
    13 // Update Count     : 177
     12// Last Modified On : Wed Aug 12 14:33:52 2015
     13// Update Count     : 222
    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 ( NameExpr * nm = dynamic_cast< NameExpr * >( *iter ) ) {
     193                                // if expression is a name, then initializing aggregate member
     194                                output << ".";
     195                                (*iter)->accept( *this );
     196                        } else {
     197                                // if not a simple name, it has to be a constant expression, i.e. an array designator
     198                                output << "[";
     199                                (*iter)->accept( *this );
     200                                output << "]";
     201                        }
     202                }
     203                output << " = ";
     204        }
     205
    188206        void CodeGenerator::visit( SingleInit *init ) {
     207                printDesignators( init->get_designators() );
    189208                init->get_value()->accept( *this );
    190209        }
    191210
    192211        void CodeGenerator::visit( ListInit *init ) {
     212                printDesignators( init->get_designators() );
    193213                output << "{ ";
    194214                genCommaList( init->begin_initializers(), init->end_initializers() );
     
    238258             
    239259                                  case OT_CALL:
    240                                   case OT_CTOR:
    241                                   case OT_DTOR:
    242                                         // there are no intrinsic definitions of the function call operator or constructors or destructors
     260                                        // there are no intrinsic definitions of the function call operator
    243261                                        assert( false );
    244262                                        break;
     
    452470        void CodeGenerator::visit( TypeExpr *typeExpr ) {}
    453471
     472        void CodeGenerator::visit( AsmExpr *asmExpr ) {
     473                if ( asmExpr->get_inout() ) {
     474                        output << "[ ";
     475                        asmExpr->get_inout()->accept( *this );
     476                        output << " ] ";
     477                } // if
     478                asmExpr->get_constraint()->accept( *this );
     479                output << " ( ";
     480                asmExpr->get_operand()->accept( *this );
     481                output << " )";
     482        }
     483
    454484        //*** Statements
    455485        void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
     
    459489                cur_indent += CodeGenerator::tabsize;
    460490
    461                 for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++) {
     491                for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++ ) {
    462492                        output << indent << printLabels( (*i)->get_labels() );
    463                         (*i)->accept(*this );
     493                        (*i)->accept( *this );
    464494
    465495                        output << endl;
     
    485515        }
    486516
     517        void CodeGenerator::visit( AsmStmt *asmStmt ) {
     518                output << "asm ";
     519                if ( asmStmt->get_voltile() ) output << "volatile ";
     520                if ( ! asmStmt->get_gotolabels().empty()  ) output << "goto ";
     521                output << "( ";
     522                if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
     523                output << " : ";
     524                genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
     525                output << " : ";
     526                genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
     527                output << " : ";
     528                genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
     529                if ( ! asmStmt->get_gotolabels().empty() ) {
     530                        output << " : ";
     531                        for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
     532                                output << *begin++;
     533                                if ( begin == asmStmt->get_gotolabels().end() ) break;
     534                                output << ", ";
     535                        } // for
     536                } // if
     537                output << " );" ;
     538        }
     539
    487540        void CodeGenerator::visit( IfStmt *ifStmt ) {
    488                 output << "if (";
    489                 ifStmt->get_condition()->accept(*this );
    490                 output << ") ";
    491 
    492                 ifStmt->get_thenPart()->accept(*this );
     541                output << "if ( ";
     542                ifStmt->get_condition()->accept( *this );
     543                output << " ) ";
     544
     545                ifStmt->get_thenPart()->accept( *this );
    493546
    494547                if ( ifStmt->get_elsePart() != 0) {
    495548                        output << " else ";
    496                         ifStmt->get_elsePart()->accept(*this );
     549                        ifStmt->get_elsePart()->accept( *this );
    497550                } // if
    498551        }
    499552
    500553        void CodeGenerator::visit( SwitchStmt *switchStmt ) {
    501                 output << "switch (" ;
    502                 switchStmt->get_condition()->accept(*this );
    503                 output << ") ";
     554                output << "switch ( " ;
     555                switchStmt->get_condition()->accept( *this );
     556                output << " ) ";
    504557               
    505558                output << "{" << std::endl;
     
    519572                } else {
    520573                        output << "case ";
    521                         caseStmt->get_condition()->accept(*this );
     574                        caseStmt->get_condition()->accept( *this );
    522575                } // if
    523576                output << ":\n";
     
    528581                for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end();  i++) {
    529582                        output << indent << printLabels( (*i)->get_labels() )  ;
    530                         (*i)->accept(*this );
     583                        (*i)->accept( *this );
    531584                        output << endl;
    532585                }
     
    572625                else {
    573626                        output << "while (" ;
    574                         whileStmt->get_condition()->accept(*this );
     627                        whileStmt->get_condition()->accept( *this );
    575628                        output << ")";
    576629                } // if
     
    584637                if ( whileStmt->get_isDoWhile() ) {
    585638                        output << " while (" ;
    586                         whileStmt->get_condition()->accept(*this );
     639                        whileStmt->get_condition()->accept( *this );
    587640                        output << ");";
    588641                } // if
Note: See TracChangeset for help on using the changeset viewer.