Changes in / [de62360d:94e0864d]


Ignore:
Location:
src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    rde62360d r94e0864d  
    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 : Tue Jun 23 16:16:57 2015
    13 // Update Count     : 133
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Wed Jun 24 16:11:41 2015
     13// Update Count     : 143
    1414//
    1515
     
    4444        }
    4545
    46         CodeGenerator::CodeGenerator( std::ostream &os ) : cur_indent( 0 ), insideFunction( false ), output( os ) { }
    47 
    48         CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indent, bool infunp )
    49                         : cur_indent( indent ), insideFunction( infunp ), output( os ) {
     46        ostream & CodeGenerator::Indenter::operator()( ostream & output ) {
     47          return output << string( cg.cur_indent, ' ' );
     48        }
     49
     50        ostream & operator<<( ostream & output, CodeGenerator::Indenter &indent ) {
     51                return indent( output );
     52        }
     53
     54        CodeGenerator::CodeGenerator( std::ostream &os ) : indent(*this), cur_indent( 0 ), insideFunction( false ), output( os ) { }
     55
     56        CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indentation, bool infunp )
     57                        : indent(*this), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
    5058                //output << std::string( init );
    5159        }
    5260
    53         CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indent, bool infunp )
    54                         : cur_indent( indent ), insideFunction( infunp ), output( os ) {
     61        CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indentation, bool infunp )
     62                        : indent(*this), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
    5563                //output << std::string( init );
    5664        }
     
    6371                } // if
    6472        }
    65  
     73
    6674        //*** Declarations
    6775        void CodeGenerator::visit( FunctionDecl *functionDecl ) {
     
    108116
    109117                if ( ! memb.empty() ) {
    110                         output << endl << string( cur_indent, ' ' ) << "{" << endl;
     118                        output << " {" << endl;
    111119
    112120                        cur_indent += CodeGenerator::tabsize;
    113121                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    114                                 output << string( cur_indent, ' ' );
     122                                output << indent;
    115123                                (*i)->accept(*this );
    116124                                output << ";" << endl;
     
    119127                        cur_indent -= CodeGenerator::tabsize;
    120128
    121                         output << string( cur_indent, ' ' ) << "}";
     129                        output << indent << "}";
    122130                } // if
    123131        }
     
    142150
    143151                if ( ! memb.empty() ) {
    144                         output << endl << "{" << endl;
     152                        output << " {" << endl;
    145153
    146154                        cur_indent += CodeGenerator::tabsize;
     
    148156                                ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
    149157                                assert( obj );
    150                                 output << string( cur_indent, ' ' ) << mangleName( obj );
     158                                output << indent << mangleName( obj );
    151159                                if ( obj->get_init() ) {
    152160                                        output << " = ";
     
    158166                        cur_indent -= CodeGenerator::tabsize;
    159167
    160                         output << "}" << endl;
     168                        output << indent << "}";
    161169                } // if
    162170        }
     
    449457
    450458                for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++) {
    451                         output << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() );
     459                        output << indent << printLabels( (*i)->get_labels() );
    452460                        (*i)->accept(*this );
    453461
     
    459467                cur_indent -= CodeGenerator::tabsize;
    460468
    461                 output << string( cur_indent, ' ' ) << "}";
     469                output << indent << "}";
    462470        }
    463471
     
    499507                cur_indent -= CodeGenerator::tabsize;
    500508
    501                 output << string( cur_indent, ' ' ) << "}";
     509                output << indent << "}";
    502510        }
    503511
    504512        void CodeGenerator::visit( CaseStmt *caseStmt ) {
    505                 output << string( cur_indent, ' ' );
     513                output << indent;
    506514                if ( caseStmt->isDefault()) {
    507515                        output << "default";
     
    516524                cur_indent += CodeGenerator::tabsize;
    517525                for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end();  i++) {
    518                         output << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() )  ;
     526                        output << indent << printLabels( (*i)->get_labels() )  ;
    519527                        (*i)->accept(*this );
    520528                        output << endl;
     
    569577                whileStmt->get_body()->accept( *this );
    570578
    571                 output << string( cur_indent, ' ' );
     579                output << indent;
    572580
    573581                if ( whileStmt->get_isDoWhile() ) {
     
    601609
    602610        void CodeGenerator::visit( NullStmt *nullStmt ) {
    603                 //output << string( cur_indent, ' ' ) << CodeGenerator::printLabels( nullStmt->get_labels() );
     611                //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
    604612                output << "/* null statement */ ;";
    605613        }
  • src/CodeGen/CodeGenerator.h

    rde62360d r94e0864d  
    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 Jun  8 14:34:43 2015
    13 // Update Count     : 15
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Thu Jun 11 13:24:23 2015
     13// Update Count     : 23
    1414//
    1515
     
    8080
    8181                template< class Iterator > void genCommaList( Iterator begin, Iterator end );
     82
     83                struct Indenter {
     84                        Indenter(CodeGenerator &cg) : cg(cg) {}
     85                        CodeGenerator & cg;
     86                        std::ostream& operator()(std::ostream & os);
     87                };
    8288          private:
     89
     90                Indenter indent;
    8391                int cur_indent;
    8492                bool insideFunction;
  • src/Parser/ExpressionNode.cc

    rde62360d r94e0864d  
    99// Author           : Rodolfo G. Esteves
    1010// Created On       : Sat May 16 13:17:07 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Jun 23 17:46:23 2015
    13 // Update Count     : 152
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Wed Jun 24 16:10:47 2015
     13// Update Count     : 154
    1414//
    1515
     
    252252                                                                                   new ConstantExpr(
    253253                                                                                           Constant( new BasicType( q, BasicType::UnsignedInt ),
    254                                                                                                                  toString( value.size() + 1 ) ) ),  // account for '\0'
     254                                                                                                                 toString( value.size()+1-2 ) ) ),  // +1 for '\0' and -2 for '"'
    255255                                                                                   false, false );
    256256                        return new ConstantExpr( Constant( at, value ), maybeBuild< Expression >( get_argName() ) );
  • src/ResolvExpr/Resolver.cc

    rde62360d r94e0864d  
    99// Author           : Richard C. Bilson
    1010// Created On       : Sun May 17 12:17:01 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Jun 24 15:47:16 2015
    13 // Update Count     : 50
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Wed Jun 24 16:08:49 2015
     13// Update Count     : 155
    1414//
    1515
     
    3838                virtual void visit( TypeDecl *typeDecl );
    3939
     40                virtual void visit( ArrayType * at );
     41
    4042                virtual void visit( ExprStmt *exprStmt );
    4143                virtual void visit( IfStmt *ifStmt );
     
    5153                virtual void visit( ListInit *listInit );
    5254          private:
     55        typedef std::list< Initializer * >::iterator InitIterator;
     56
     57          void resolveAggrInit( AggregateDecl *, InitIterator &, InitIterator & );
     58          void resolveSingleAggrInit( Declaration *, InitIterator &, InitIterator & );
     59
    5360                std::list< Type * > functionReturn;
    5461                Type *initContext;
     
    158165                initContext = new_type;
    159166                SymTab::Indexer::visit( objectDecl );
    160 
    161                 if ( ArrayType * at = dynamic_cast< ArrayType * >( new_type ) ){
    162                         if ( at->get_dimension() ) {
    163                                 BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
    164                                 CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() );
    165                                 Expression *newExpr = findSingleExpression( castExpr, *this );
    166                                 delete at->get_dimension();
    167                                 at->set_dimension( newExpr );
    168                         }
    169                 }
    170         }
    171  
     167        }
     168
     169        void Resolver::visit( ArrayType * at ) {
     170                if ( at->get_dimension() ) {
     171                        BasicType arrayLenType = BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
     172                        CastExpr *castExpr = new CastExpr( at->get_dimension(), arrayLenType.clone() );
     173                        Expression *newExpr = findSingleExpression( castExpr, *this );
     174                        delete at->get_dimension();
     175                        at->set_dimension( newExpr );
     176                }
     177                Visitor::visit( at );
     178        }
     179
    172180        void Resolver::visit( TypeDecl *typeDecl ) {
    173181                if ( typeDecl->get_base() ) {
     
    177185                SymTab::Indexer::visit( typeDecl );
    178186        }
    179  
     187
    180188        void Resolver::visit( FunctionDecl *functionDecl ) {
    181189#if 0
     
    284292                        returnStmt->set_expr( newExpr );
    285293                } // if
     294        }
     295
     296        template< typename T >
     297        bool isCharType( T t ) {
     298                if ( BasicType * bt = dynamic_cast< BasicType * >( t ) ) {
     299                        return bt->get_kind() == BasicType::Char || bt->get_kind() == BasicType::SignedChar ||
     300                                bt->get_kind() == BasicType::UnsignedChar;
     301                }
     302                return false;
    286303        }
    287304
     
    310327                        delete castExpr;
    311328                        singleInit->set_value( newExpr );
     329
     330                        // check if initializing type is char[]
     331                        if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
     332                                if ( isCharType( at->get_base() ) ) {
     333                                        // check if the resolved type is char *
     334                                        if ( PointerType * pt = dynamic_cast< PointerType *>( newExpr->get_results().front() ) ) {
     335                                                if ( isCharType( pt->get_base() ) ) {
     336                                                        // strip cast if we're initializing a char[] with a char *, e.g.
     337                                                        // char x[] = "hello";
     338                                                        CastExpr *ce = dynamic_cast< CastExpr * >( newExpr );
     339                                                        singleInit->set_value( ce->get_arg() );
     340                                                        ce->set_arg( NULL );
     341                                                        delete ce;                                                                     
     342                                                }
     343                                        }
     344                                }
     345                        }
    312346                } // if
    313347//      singleInit->get_value()->accept( *this );
    314348        }
    315349
    316         void Resolver::visit( ListInit *listInit ) {
    317                 Visitor::visit(listInit);
     350        void Resolver::resolveSingleAggrInit( Declaration * dcl, InitIterator & init, InitIterator & initEnd ) {
     351                DeclarationWithType * dt = dynamic_cast< DeclarationWithType * >( dcl );
     352                assert( dt );
     353                initContext = dt->get_type();
     354                try {
     355                        if ( init == initEnd ) return; // stop when there are no more initializers
     356                        (*init)->accept( *this );
     357                        ++init; // made it past an initializer
     358                } catch( SemanticError & ) {
     359                        // need to delve deeper, if you can
     360                        if ( StructInstType * sit = dynamic_cast< StructInstType * >( dt->get_type() ) ) {
     361                                resolveAggrInit( sit->get_baseStruct(), init, initEnd );
     362                        } else if ( UnionInstType * uit = dynamic_cast< UnionInstType * >( dt->get_type() ) ) {
     363                                resolveAggrInit( uit->get_baseUnion(), init, initEnd );
     364                        } else {
     365                                // might need to rethink what is being thrown
     366                                throw;
     367                        } // if
     368                }
     369        }
     370
     371        void Resolver::resolveAggrInit( AggregateDecl * aggr, InitIterator & init, InitIterator & initEnd ) {
     372                if ( StructDecl * st = dynamic_cast< StructDecl * >( aggr ) ) {
     373                        // want to resolve each initializer to the members of the struct,
     374                        // but if there are more initializers than members we should stop
     375                        list< Declaration * >::iterator it = st->get_members().begin();
     376                        for ( ; it != st->get_members().end(); ++it) {
     377                                resolveSingleAggrInit( *it, init, initEnd );
     378                        }
     379                } else if ( UnionDecl * un = dynamic_cast< UnionDecl * >( aggr ) ) {
     380                        // only resolve to the first member of a union
     381                        resolveSingleAggrInit( *un->get_members().begin(), init, initEnd );
     382                } // if
     383        }
     384
     385        void Resolver::visit( ListInit * listInit ) {
     386                InitIterator iter = listInit->begin_initializers();
     387                InitIterator end = listInit->end_initializers();
     388
     389                if ( ArrayType * at = dynamic_cast< ArrayType * >( initContext ) ) {
     390                        // resolve each member to the base type of the array
     391                        for ( ; iter != end; ++iter ) {
     392                                initContext = at->get_base();
     393                                (*iter)->accept( *this );
     394                        } // for
     395                } else if ( StructInstType * st = dynamic_cast< StructInstType * >( initContext ) ) {
     396                        resolveAggrInit( st->get_baseStruct(), iter, end );
     397                } else if ( UnionInstType *st = dynamic_cast< UnionInstType * >( initContext ) ) {
     398                        resolveAggrInit( st->get_baseUnion(), iter, end );
     399                } else {
     400                        // basic types are handled here
     401                        Visitor::visit( listInit );
     402                }
     403
    318404#if 0
    319405                if ( ArrayType *at = dynamic_cast<ArrayType*>(initContext) ) {
  • src/SynTree/Constant.cc

    rde62360d r94e0864d  
    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 : Sun Jun  7 08:45:30 2015
    13 // Update Count     : 5
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Wed Jun 10 14:41:03 2015
     13// Update Count     : 8
    1414//
    1515
     
    2727
    2828void Constant::print( std::ostream &os ) const {
    29         os << value;
     29        os << "(" << value;
    3030        if ( type ) {
    31                 os << " ";
     31                os << ": ";
    3232                type->print( os );
    3333        } // if
     34  os << ")";
    3435}
    3536
Note: See TracChangeset for help on using the changeset viewer.