Ignore:
Timestamp:
Apr 17, 2017, 5:43:01 PM (7 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:
7069652
Parents:
4ae83a4b
Message:

allow codegen as an alternative to AST dump after any pass with the -z option

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    r4ae83a4b re39241b  
    8989        }
    9090
    91         CodeGenerator::CodeGenerator( std::ostream & os, bool pretty ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ), printLabels( *this ), pretty( pretty ) {}
     91        CodeGenerator::CodeGenerator( std::ostream & os, bool pretty, bool genC ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ), printLabels( *this ), pretty( pretty ), genC( genC ) {}
    9292
    9393        CodeGenerator::CodeGenerator( std::ostream & os, std::string init, int indentation, bool infunp )
     
    136136                functionDecl->get_funcSpec().print( output );
    137137
    138                 output << genType( functionDecl->get_functionType(), mangleName( functionDecl ), pretty );
     138                output << genType( functionDecl->get_functionType(), mangleName( functionDecl ), pretty, genC );
    139139
    140140                asmName( functionDecl );
     
    147147
    148148        void CodeGenerator::visit( ObjectDecl * objectDecl ) {
    149                 if (objectDecl->get_name().empty()) {
     149                if (objectDecl->get_name().empty() && genC ) {
     150                        // only generate an anonymous name when generating C code, otherwise it clutters the output too much
    150151                        static UniqueName name = { "__anonymous_object" };
    151152                        objectDecl->set_name( name.newName() );
     
    156157
    157158                handleStorageClass( objectDecl );
    158                 output << genType( objectDecl->get_type(), mangleName( objectDecl ), pretty );
     159                output << genType( objectDecl->get_type(), mangleName( objectDecl ), pretty, genC );
    159160
    160161                asmName( objectDecl );
     
    174175                genAttributes( aggDecl->get_attributes() );
    175176
     177                if( ! aggDecl->get_parameters().empty() && ! genC ) {
     178                        // assertf( ! genC, "Aggregate type parameters should not reach code generation." );
     179                        output << "forall(";
     180                        genCommaList( aggDecl->get_parameters().begin(), aggDecl->get_parameters().end() );
     181                        output << ")" << endl;
     182                }
     183
    176184                if ( aggDecl->get_name() != "" )
    177185                        output << aggDecl->get_name();
    178186
    179                 // std::list< Declaration * > & memb = aggDecl->get_members();
    180                 // if ( ! memb.empty() ) {
    181187                if ( aggDecl->has_body() ) {
    182188                        std::list< Declaration * > & memb = aggDecl->get_members();
     
    242248
    243249        void CodeGenerator::visit( TypedefDecl * typeDecl ) {
    244                 assert( false && "Typedefs are removed and substituted in earlier passes." );
    245                 //output << "typedef ";
    246                 //output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty );
     250                assertf( ! genC, "Typedefs are removed and substituted in earlier passes." );
     251                output << "typedef ";
     252                output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl;
    247253        }
    248254
    249255        void CodeGenerator::visit( TypeDecl * typeDecl ) {
    250                 // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
    251                 // still to be done
    252                 extension( typeDecl );
    253                 output << "extern unsigned long " << typeDecl->get_name();
    254                 if ( typeDecl->get_base() ) {
    255                         output << " = sizeof( " << genType( typeDecl->get_base(), "", pretty ) << " )";
    256                 } // if
     256                if ( genC ) {
     257                        // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
     258                        // still to be done
     259                        extension( typeDecl );
     260                        output << "extern unsigned long " << typeDecl->get_name();
     261                        if ( typeDecl->get_base() ) {
     262                                output << " = sizeof( " << genType( typeDecl->get_base(), "", pretty, genC ) << " )";
     263                        } // if
     264                } else {
     265                        output << typeDecl->typeString() << " " << typeDecl->get_name();
     266                        if ( ! typeDecl->get_assertions().empty() ) {
     267                                output << " | { ";
     268                                genCommaList( typeDecl->get_assertions().begin(), typeDecl->get_assertions().end() );
     269                                output << " }";
     270                        }
     271                }
    257272        }
    258273
     
    293308
    294309        void CodeGenerator::visit( ConstructorInit * init ){
    295                 assertf( false, "ConstructorInit nodes should not make it to CodeGen." );
     310                assertf( ! genC, "ConstructorInit nodes should not reach code generation." );
     311                // xxx - generate something reasonable for constructor/destructor pairs
     312                output << "<ctorinit>";
    296313        }
    297314
     
    547564                        // at least one result type of cast, but not an lvalue
    548565                        output << "(";
    549                         output << genType( castExpr->get_result(), "", pretty );
     566                        output << genType( castExpr->get_result(), "", pretty, genC );
    550567                        output << ")";
    551568                } else {
     
    558575
    559576        void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {
    560                 assert( false );
     577                assertf( ! genC, "UntypedMemberExpr should not reach code generation." );
     578                extension( memberExpr );
     579                memberExpr->get_aggregate()->accept( *this );
     580                output << "." << memberExpr->get_member();
    561581        }
    562582
     
    587607                output << "sizeof(";
    588608                if ( sizeofExpr->get_isType() ) {
    589                         output << genType( sizeofExpr->get_type(), "", pretty );
     609                        output << genType( sizeofExpr->get_type(), "", pretty, genC );
    590610                } else {
    591611                        sizeofExpr->get_expr()->accept( *this );
     
    599619                output << "__alignof__(";
    600620                if ( alignofExpr->get_isType() ) {
    601                         output << genType( alignofExpr->get_type(), "", pretty );
     621                        output << genType( alignofExpr->get_type(), "", pretty, genC );
    602622                } else {
    603623                        alignofExpr->get_expr()->accept( *this );
     
    607627
    608628        void CodeGenerator::visit( UntypedOffsetofExpr * offsetofExpr ) {
    609                 assert( false && "UntypedOffsetofExpr should not reach code generation." );
     629                assertf( ! genC, "UntypedOffsetofExpr should not reach code generation." );
     630                output << "offsetof(";
     631                output << genType( offsetofExpr->get_type(), "", pretty, genC );
     632                output << ", " << offsetofExpr->get_member();
     633                output << ")";
    610634        }
    611635
     
    613637                // use GCC builtin
    614638                output << "__builtin_offsetof(";
    615                 output << genType( offsetofExpr->get_type(), "", pretty );
     639                output << genType( offsetofExpr->get_type(), "", pretty, genC );
    616640                output << ", " << mangleName( offsetofExpr->get_member() );
    617641                output << ")";
     
    619643
    620644        void CodeGenerator::visit( OffsetPackExpr * offsetPackExpr ) {
    621                 assert( false && "OffsetPackExpr should not reach code generation." );
     645                assertf( ! genC, "OffsetPackExpr should not reach code generation." );
     646                output << "__CFA_offsetpack(" << genType( offsetPackExpr->get_type(), "", pretty, genC ) << ")";
    622647        }
    623648
     
    655680        }
    656681
    657         void CodeGenerator::visit( UntypedTupleExpr * tupleExpr ) { assertf( false, "UntypedTupleExpr should not make it to Code Gen" ); }
    658 
    659         void CodeGenerator::visit( TupleExpr * tupleExpr ) { assertf( false, "TupleExpr should not make it to Code Gen" ); }
    660 
    661         void CodeGenerator::visit( TypeExpr * typeExpr ) {}
     682        void CodeGenerator::visit( UntypedTupleExpr * tupleExpr ) {
     683                assertf( ! genC, "UntypedTupleExpr should not reach code generation." );
     684                output << "[";
     685                genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() );
     686                output << "]";
     687        }
     688
     689        void CodeGenerator::visit( TupleExpr * tupleExpr ) {
     690                assertf( ! genC, "TupleExpr should not reach code generation." );
     691                output << "[";
     692                genCommaList( tupleExpr->get_exprs().begin(), tupleExpr->get_exprs().end() );
     693                output << "]";
     694        }
     695
     696        void CodeGenerator::visit( TypeExpr * typeExpr ) {
     697                assertf( ! genC, "TypeExpr should not reach code generation." );
     698                output<< genType( typeExpr->get_type(), "", pretty, genC );
     699        }
    662700
    663701        void CodeGenerator::visit( AsmExpr * asmExpr ) {
     
    675713        void CodeGenerator::visit( CompoundLiteralExpr *compLitExpr ) {
    676714                assert( compLitExpr->get_result() && dynamic_cast< ListInit * > ( compLitExpr->get_initializer() ) );
    677                 output << "(" << genType( compLitExpr->get_result(), "", pretty ) << ")";
     715                output << "(" << genType( compLitExpr->get_result(), "", pretty, genC ) << ")";
    678716                compLitExpr->get_initializer()->accept( *this );
    679717        }
Note: See TracChangeset for help on using the changeset viewer.