Changeset 1cdfa82 for src/CodeGen


Ignore:
Timestamp:
Apr 25, 2018, 4:55:53 PM (6 years ago)
Author:
Aaron Moss <a3moss@…>
Branches:
new-env, with_gc
Children:
42107b4
Parents:
2efe4b8 (diff), 9d5fb67 (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 remote-tracking branch 'origin/master' into with_gc

Location:
src/CodeGen
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    r2efe4b8 r1cdfa82  
    116116        }
    117117
    118         CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks ) : indent( CodeGenerator::tabsize ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ), endl( *this ) {}
     118        CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC, bool lineMarks, bool printExprTypes ) : indent( CodeGenerator::tabsize ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ), lineMarks( lineMarks ), printExprTypes( printExprTypes ), endl( *this ) {}
    119119
    120120        string CodeGenerator::mangleName( DeclarationWithType * decl ) {
     
    159159        }
    160160
     161        // *** Expression
     162        void CodeGenerator::previsit( Expression * node ) {
     163                previsit( (BaseSyntaxNode *)node );
     164                GuardAction( [this, node](){
     165                        if ( printExprTypes ) {
     166                                output << " /* " << genType( node->result, "", pretty, genC ) << " */ ";
     167                        }
     168                } );
     169        }
     170
    161171        // *** Declarations
    162172        void CodeGenerator::postvisit( FunctionDecl * functionDecl ) {
     
    203213
    204214        void CodeGenerator::handleAggregate( AggregateDecl * aggDecl, const std::string & kind ) {
    205                 genAttributes( aggDecl->get_attributes() );
    206 
    207215                if( ! aggDecl->get_parameters().empty() && ! genC ) {
    208216                        // assertf( ! genC, "Aggregate type parameters should not reach code generation." );
     
    213221                }
    214222
    215                 output << kind << aggDecl->get_name();
     223                output << kind;
     224                genAttributes( aggDecl->get_attributes() );
     225                output << aggDecl->get_name();
    216226
    217227                if ( aggDecl->has_body() ) {
     
    298308                        output << " }";
    299309                }
     310        }
     311
     312        void CodeGenerator::postvisit( StaticAssertDecl * assertDecl ) {
     313                output << "_Static_assert(";
     314                assertDecl->condition->accept( *visitor );
     315                output << ", ";
     316                assertDecl->message->accept( *visitor );
     317                output << ")";
    300318        }
    301319
     
    578596                        output << ")";
    579597                } // if
    580                 castExpr->get_arg()->accept( *visitor );
     598                castExpr->arg->accept( *visitor );
     599                output << ")";
     600        }
     601
     602        void CodeGenerator::postvisit( KeywordCastExpr * castExpr ) {
     603                assertf( ! genC, "KeywordCast should not reach code generation." );
     604                extension( castExpr );
     605                output << "((" << castExpr->targetString() << " &)";
     606                castExpr->arg->accept( *visitor );
    581607                output << ")";
    582608        }
     
    928954                        output << "continue";
    929955                        break;
     956                  case BranchStmt::FallThrough:
     957                  case BranchStmt::FallThroughDefault:
     958                        assertf( ! genC, "fallthru should not reach code generation." );
     959                  output << "fallthru";
     960                        break;
    930961                } // switch
     962                // print branch target for labelled break/continue/fallthru in debug mode
     963                if ( ! genC && branchStmt->get_type() != BranchStmt::Goto ) {
     964                        if ( ! branchStmt->get_target().empty() ) {
     965                                output << " " << branchStmt->get_target();
     966                        } else if ( branchStmt->get_type() == BranchStmt::FallThrough ) {
     967                                output << " default";
     968                        }
     969                }
    931970                output << ";";
    932971        }
  • src/CodeGen/CodeGenerator.h

    r2efe4b8 r1cdfa82  
    2727
    2828namespace CodeGen {
    29         struct CodeGenerator : public WithShortCircuiting, public WithVisitorRef<CodeGenerator> {
     29        struct CodeGenerator : public WithShortCircuiting, public WithGuards, public WithVisitorRef<CodeGenerator> {
    3030          static int tabsize;
    3131
    32                 CodeGenerator( std::ostream &os, bool pretty = false, bool genC = false, bool lineMarks = false );
     32                CodeGenerator( std::ostream &os, bool pretty = false, bool genC = false, bool lineMarks = false, bool printExprTypes = false );
    3333
    3434                //*** Turn off visit_children for all nodes
     
    3838                void postvisit( BaseSyntaxNode * );
    3939
     40                //*** print type for all expressions
     41                void previsit( Expression * node );
     42
    4043                //*** Declaration
    4144                void postvisit( StructDecl * );
    4245                void postvisit( FunctionDecl * );
    4346                void postvisit( ObjectDecl * );
    44                 void postvisit( UnionDecl *aggregateDecl );
    45                 void postvisit( EnumDecl *aggregateDecl );
    46                 void postvisit( TraitDecl *aggregateDecl );
    47                 void postvisit( TypedefDecl *typeDecl );
    48                 void postvisit( TypeDecl *typeDecl );
     47                void postvisit( UnionDecl * aggregateDecl );
     48                void postvisit( EnumDecl * aggregateDecl );
     49                void postvisit( TraitDecl * aggregateDecl );
     50                void postvisit( TypedefDecl * typeDecl );
     51                void postvisit( TypeDecl * typeDecl );
     52                void postvisit( StaticAssertDecl * assertDecl );
    4953
    5054                //*** Initializer
     
    6569                void postvisit( LabelAddressExpr *addressExpr );
    6670                void postvisit( CastExpr *castExpr );
     71                void postvisit( KeywordCastExpr * castExpr );
    6772                void postvisit( VirtualCastExpr *castExpr );
    6873                void postvisit( UntypedMemberExpr *memberExpr );
     
    139144                bool genC = false;    // true if output has to be C code
    140145                bool lineMarks = false;
     146                bool printExprTypes = false;
    141147        public:
    142148                LineEnder endl;
  • src/CodeGen/GenType.cc

    r2efe4b8 r1cdfa82  
    4848                void postvisit( ZeroType * zeroType );
    4949                void postvisit( OneType * oneType );
     50                void postvisit( TraitInstType * inst );
     51                void postvisit( TypeofType * typeof );
    5052
    5153          private:
     
    289291        }
    290292
     293        void GenType::postvisit( TraitInstType * inst ) {
     294                assertf( ! genC, "Trait types should not reach code generation." );
     295                typeString = inst->name + " " + typeString;
     296                handleQualifiers( inst );
     297        }
     298
     299        void GenType::postvisit( TypeofType * typeof ) {
     300                std::ostringstream os;
     301                PassVisitor<CodeGenerator> cg( os, pretty, genC, lineMarks );
     302                os << "typeof(";
     303                typeof->expr->accept( cg );
     304                os << ") " << typeString;
     305                typeString = os.str();
     306                handleQualifiers( typeof );
     307        }
     308
    291309        void GenType::handleQualifiers( Type * type ) {
    292310                if ( type->get_const() ) {
  • src/CodeGen/Generate.cc

    r2efe4b8 r1cdfa82  
    4646        } // namespace
    4747
    48         void generate( std::list< Declaration* > translationUnit, std::ostream &os, bool doIntrinsics, bool pretty, bool generateC, bool lineMarks ) {
     48        void generate( std::list< Declaration* > translationUnit, std::ostream &os, bool doIntrinsics, bool pretty, bool generateC, bool lineMarks, bool printExprTypes ) {
    4949                cleanTree( translationUnit );
    5050
    51                 PassVisitor<CodeGenerator> cgv( os, pretty, generateC, lineMarks );
     51                PassVisitor<CodeGenerator> cgv( os, pretty, generateC, lineMarks, printExprTypes );
    5252                for ( auto & dcl : translationUnit ) {
    5353                        if ( LinkageSpec::isGeneratable( dcl->get_linkage() ) && (doIntrinsics || ! LinkageSpec::isBuiltin( dcl->get_linkage() ) ) ) {
     
    6666                        os << CodeGen::genPrettyType( type, "" );
    6767                } else {
    68                         PassVisitor<CodeGenerator> cgv( os, true, false, false );
     68                        PassVisitor<CodeGenerator> cgv( os, true, false, false, false );
    6969                        node->accept( cgv );
    7070                }
  • src/CodeGen/Generate.h

    r2efe4b8 r1cdfa82  
    2424namespace CodeGen {
    2525        /// Generates code. doIntrinsics determines if intrinsic functions are printed, pretty formats output nicely (e.g., uses unmangled names, etc.), generateC is true when the output must consist only of C code (allows some assertions, etc.)
    26         void generate( std::list< Declaration* > translationUnit, std::ostream &os, bool doIntrinsics, bool pretty, bool generateC = false , bool lineMarks = false );
     26        void generate( std::list< Declaration* > translationUnit, std::ostream &os, bool doIntrinsics, bool pretty, bool generateC = false , bool lineMarks = false, bool printTypeExpr = false );
    2727
    2828        /// Generate code for a single node -- helpful for debugging in gdb
  • src/CodeGen/OperatorTable.cc

    r2efe4b8 r1cdfa82  
    7979        } // namespace
    8080
    81         bool operatorLookup( std::string funcName, OperatorInfo &info ) {
     81        bool operatorLookup( const std::string & funcName, OperatorInfo & info ) {
    8282                static bool init = false;
    8383                if ( ! init ) {
     
    100100                        return true;
    101101                } // if
     102        }
     103
     104        bool isOperator( const std::string & funcName ) {
     105                OperatorInfo info;
     106                return operatorLookup( funcName, info );
    102107        }
    103108
  • src/CodeGen/OperatorTable.h

    r2efe4b8 r1cdfa82  
    4141        };
    4242
    43         bool operatorLookup( std::string funcName, OperatorInfo &info );
     43        bool isOperator( const std::string & funcName );
     44        bool operatorLookup( const std::string & funcName, OperatorInfo & info );
    4445
    4546        bool isConstructor( const std::string & );
Note: See TracChangeset for help on using the changeset viewer.