Changeset b1e63ac5 for src/CodeGen


Ignore:
Timestamp:
Jul 4, 2017, 9:40:16 AM (8 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
208e5be
Parents:
9c951e3 (diff), f7cb0bc (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' into references

Location:
src/CodeGen
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    r9c951e3 rb1e63ac5  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Wed May 10 14:45:00 2017
    13 // Update Count     : 484
     12// Last Modified On : Thu Jun  8 16:00:00 2017
     13// Update Count     : 485
    1414//
    1515
     
    6565                } // if
    6666        } // extension
    67 
    68         ostream & CodeGenerator::Indenter::operator()( ostream & output ) const {
    69           return output << string( cg.cur_indent, ' ' );
    70         }
    71 
    72         ostream & operator<<( ostream & output, const CodeGenerator::Indenter &indent ) {
    73                 return indent( output );
    74         }
    7567
    7668        CodeGenerator::LabelPrinter & CodeGenerator::LabelPrinter::operator()( std::list< Label > & l ) {
     
    111103        }
    112104
    113         CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {}
    114 
    115         CodeGenerator::CodeGenerator( std::ostream & os, std::string init, int indentation, bool infunp )
    116                         : indent( *this), cur_indent( indentation ), insideFunction( infunp ), output( os ), printLabels( *this ) {
    117                 //output << std::string( init );
    118         }
    119 
    120         CodeGenerator::CodeGenerator( std::ostream & os, char * init, int indentation, bool infunp )
    121                         : indent( *this ), cur_indent( indentation ), insideFunction( infunp ), output( os ), printLabels( *this ) {
    122                 //output << std::string( init );
    123         }
     105        CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks ) : indent( CodeGenerator::tabsize ), insideFunction( false ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ) {}
    124106
    125107        string CodeGenerator::mangleName( DeclarationWithType * decl ) {
     
    212194                        output << " {" << endl;
    213195
    214                         cur_indent += CodeGenerator::tabsize;
     196                        ++indent;
    215197                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end(); i++ ) {
    216198                                output << lineDirective( *i ) << indent;
     
    219201                        } // for
    220202
    221                         cur_indent -= CodeGenerator::tabsize;
     203                        --indent;
    222204
    223205                        output << indent << "}";
     
    249231                        output << " {" << endl;
    250232
    251                         cur_indent += CodeGenerator::tabsize;
     233                        ++indent;
    252234                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    253235                                ObjectDecl * obj = dynamic_cast< ObjectDecl* >( *i );
     
    261243                        } // for
    262244
    263                         cur_indent -= CodeGenerator::tabsize;
     245                        --indent;
    264246
    265247                        output << indent << "}";
     
    267249        }
    268250
    269         void CodeGenerator::visit( TraitDecl * traitDecl ) {}
     251        void CodeGenerator::visit( __attribute__((unused)) TraitDecl * traitDecl ) {}
    270252
    271253        void CodeGenerator::visit( TypedefDecl * typeDecl ) {
     
    298280        }
    299281
    300         void CodeGenerator::printDesignators( std::list< Expression * > & designators ) {
    301                 typedef std::list< Expression * > DesignatorList;
     282        void CodeGenerator::visit( Designation * designation ) {
     283                std::list< Expression * > designators = designation->get_designators();
    302284                if ( designators.size() == 0 ) return;
    303                 for ( DesignatorList::iterator iter = designators.begin(); iter != designators.end(); ++iter ) {
    304                         if ( dynamic_cast< NameExpr * >( *iter ) ) {
    305                                 // if expression is a name, then initializing aggregate member
     285                for ( Expression * des : designators ) {
     286                        if ( dynamic_cast< NameExpr * >( des ) || dynamic_cast< VariableExpr * >( des ) ) {
     287                                // if expression is a NameExpr or VariableExpr, then initializing aggregate member
    306288                                output << ".";
    307                                 (*iter)->accept( *this );
     289                                des->accept( *this );
    308290                        } else {
    309                                 // if not a simple name, it has to be a constant expression, i.e. an array designator
     291                                // otherwise, it has to be a ConstantExpr or CastExpr, initializing array eleemnt
    310292                                output << "[";
    311                                 (*iter)->accept( *this );
     293                                des->accept( *this );
    312294                                output << "]";
    313295                        } // if
     
    317299
    318300        void CodeGenerator::visit( SingleInit * init ) {
    319                 printDesignators( init->get_designators() );
    320301                init->get_value()->accept( *this );
    321302        }
    322303
    323304        void CodeGenerator::visit( ListInit * init ) {
    324                 printDesignators( init->get_designators() );
     305                auto initBegin = init->begin();
     306                auto initEnd = init->end();
     307                auto desigBegin = init->get_designations().begin();
     308                auto desigEnd = init->get_designations().end();
     309
    325310                output << "{ ";
    326                 if ( init->begin() == init->end() ) {
    327                         // illegal to leave initializer list empty for scalar initializers, but always legal to have 0
    328                         output << "0";
    329                 } else {
    330                         genCommaList( init->begin(), init->end() );
    331                 } // if
     311                for ( ; initBegin != initEnd && desigBegin != desigEnd; ) {
     312                        (*desigBegin)->accept( *this );
     313                        (*initBegin)->accept( *this );
     314                        ++initBegin, ++desigBegin;
     315                        if ( initBegin != initEnd ) {
     316                                output << ", ";
     317                        }
     318                }
    332319                output << " }";
    333         }
    334 
    335         void CodeGenerator::visit( ConstructorInit * init ){
     320                assertf( initBegin == initEnd && desigBegin == desigEnd, "Initializers and designators not the same length. %s", toString( init ).c_str() );
     321        }
     322
     323        void CodeGenerator::visit( __attribute__((unused)) ConstructorInit * init ){
    336324                assertf( ! genC, "ConstructorInit nodes should not reach code generation." );
    337325                // xxx - generate something reasonable for constructor/destructor pairs
     
    731719
    732720        void CodeGenerator::visit( TypeExpr * typeExpr ) {
    733                 assertf( ! genC, "TypeExpr should not reach code generation." );
    734                 output<< genType( typeExpr->get_type(), "", pretty, genC );
     721                // if ( genC ) std::cerr << "typeexpr still exists: " << typeExpr << std::endl;
     722                // assertf( ! genC, "TypeExpr should not reach code generation." );
     723                if ( ! genC ) {
     724                        output<< genType( typeExpr->get_type(), "", pretty, genC );
     725                }
    735726        }
    736727
     
    756747                std::list< Statement * > & stmts = stmtExpr->get_statements()->get_kids();
    757748                output << lineDirective( stmtExpr) << "({" << std::endl;
    758                 cur_indent += CodeGenerator::tabsize;
     749                ++indent;
    759750                unsigned int numStmts = stmts.size();
    760751                unsigned int i = 0;
     
    779770                        ++i;
    780771                }
    781                 cur_indent -= CodeGenerator::tabsize;
     772                --indent;
    782773                output << indent << "})";
    783774        }
     
    788779                output << "{" << endl;
    789780
    790                 cur_indent += CodeGenerator::tabsize;
     781                ++indent;
    791782
    792783                for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++ ) {
     
    799790                        } // if
    800791                } // for
    801                 cur_indent -= CodeGenerator::tabsize;
     792                --indent;
    802793
    803794                output << indent << "}";
     
    867858
    868859                output << "{" << std::endl;
    869                 cur_indent += CodeGenerator::tabsize;
     860                ++indent;
    870861                acceptAll( switchStmt->get_statements(), *this );
    871                 cur_indent -= CodeGenerator::tabsize;
     862                --indent;
    872863                output << indent << "}";
    873864        }
     
    886877                std::list<Statement *> sts = caseStmt->get_statements();
    887878
    888                 cur_indent += CodeGenerator::tabsize;
     879                ++indent;
    889880                for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end();  i++) {
    890881                        output << indent << printLabels( (*i)->get_labels() )  ;
     
    892883                        output << endl;
    893884                } // for
    894                 cur_indent -= CodeGenerator::tabsize;
     885                --indent;
    895886        }
    896887
     
    923914        }
    924915
     916        void CodeGenerator::visit( ThrowStmt * throwStmt ) {
     917                assertf( ! genC, "Throw statements should not reach code generation." );
     918
     919                output << ((throwStmt->get_kind() == ThrowStmt::Terminate) ?
     920                           "throw" : "throwResume");
     921                if (throwStmt->get_expr()) {
     922                        output << " ";
     923                        throwStmt->get_expr()->accept( *this );
     924                }
     925                if (throwStmt->get_target()) {
     926                        output << " _At ";
     927                        throwStmt->get_target()->accept( *this );
     928                }
     929                output << ";";
     930        }
     931
    925932        void CodeGenerator::visit( WhileStmt * whileStmt ) {
    926933                if ( whileStmt->get_isDoWhile() ) {
     
    967974        }
    968975
    969         void CodeGenerator::visit( NullStmt * nullStmt ) {
     976        void CodeGenerator::visit( __attribute__((unused)) NullStmt * nullStmt ) {
    970977                //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
    971978                output << "/* null statement */ ;";
  • src/CodeGen/CodeGenerator.h

    r9c951e3 rb1e63ac5  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Andrew Beach
    12 // Last Modified On : Wed May 10 10:57:00 2017
    13 // Update Count     : 51
     12// Last Modified On : Thu Jun  8 15:48:00 2017
     13// Update Count     : 52
    1414//
    1515
     
    2525#include "SymTab/Indexer.h"
    2626
     27#include "Common/Indenter.h"
    2728#include "Common/utility.h"
    2829
     
    4748
    4849                //*** Initializer
     50                virtual void visit( Designation * );
    4951                virtual void visit( SingleInit * );
    5052                virtual void visit( ListInit * );
     
    9193                virtual void visit( BranchStmt * );
    9294                virtual void visit( ReturnStmt * );
     95                virtual void visit( ThrowStmt * );
    9396                virtual void visit( WhileStmt * );
    9497                virtual void visit( ForStmt * );
     
    99102
    100103                template< class Iterator > void genCommaList( Iterator begin, Iterator end );
    101 
    102                 struct Indenter {
    103                         Indenter(CodeGenerator &cg) : cg(cg) {}
    104                         CodeGenerator & cg;
    105                         std::ostream& operator()(std::ostream & os) const;
    106                 };
    107104
    108105                struct LabelPrinter {
     
    128125          private:
    129126                Indenter indent;
    130                 int cur_indent;
    131127                bool insideFunction;
    132128                std::ostream &output;
     
    136132                bool lineMarks = false;
    137133
    138                 void printDesignators( std::list< Expression * > & );
    139134                void handleStorageClass( DeclarationWithType *decl );
    140135                void handleAggregate( AggregateDecl *aggDecl, const std::string & kind );
  • src/CodeGen/FixNames.cc

    r9c951e3 rb1e63ac5  
    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 : Thu Mar 16 07:50:30 2017
    13 // Update Count     : 16
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Wed Jun 28 15:26:00 2017
     13// Update Count     : 20
    1414//
    1515
     
    9393        void FixNames::fixDWT( DeclarationWithType *dwt ) {
    9494                if ( dwt->get_name() != "" ) {
    95                         if ( LinkageSpec::isDecoratable( dwt->get_linkage() ) ) {
     95                        if ( LinkageSpec::isMangled( dwt->get_linkage() ) ) {
    9696                                dwt->set_mangleName( SymTab::Mangler::mangle( dwt ) );
    9797                                dwt->set_scopeLevel( scopeLevel );
     
    114114                                throw SemanticError("Main expected to have 0, 2 or 3 arguments\n", functionDecl);
    115115                        }
    116                         functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new ConstantExpr( Constant( new BasicType( Type::Qualifiers(), BasicType::SignedInt ), "0") ) ) );
     116                        functionDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new ConstantExpr( Constant::from_int( 0 ) ) ) );
    117117                        CodeGen::FixMain::registerMain( functionDecl );
    118118                }
Note: See TracChangeset for help on using the changeset viewer.