Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    rfbcde64 r5f642e38  
    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 );
     
    171172        }
    172173
    173         void CodeGenerator::handleAggregate( AggregateDecl * aggDecl ) {
     174        void CodeGenerator::handleAggregate( AggregateDecl * aggDecl, const std::string & kind ) {
    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
     184                output << kind;
    176185                if ( aggDecl->get_name() != "" )
    177186                        output << aggDecl->get_name();
    178187
    179                 // std::list< Declaration * > & memb = aggDecl->get_members();
    180                 // if ( ! memb.empty() ) {
    181188                if ( aggDecl->has_body() ) {
    182189                        std::list< Declaration * > & memb = aggDecl->get_members();
     
    198205        void CodeGenerator::visit( StructDecl * structDecl ) {
    199206                extension( structDecl );
    200                 output << "struct ";
    201                 handleAggregate( structDecl );
     207                handleAggregate( structDecl, "struct " );
    202208        }
    203209
    204210        void CodeGenerator::visit( UnionDecl * unionDecl ) {
    205211                extension( unionDecl );
    206                 output << "union ";
    207                 handleAggregate( unionDecl );
     212                handleAggregate( unionDecl, "union " );
    208213        }
    209214
     
    242247
    243248        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 );
     249                assertf( ! genC, "Typedefs are removed and substituted in earlier passes." );
     250                output << "typedef ";
     251                output << genType( typeDecl->get_base(), typeDecl->get_name(), pretty, genC ) << endl;
    247252        }
    248253
    249254        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
     255                if ( genC ) {
     256                        // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
     257                        // still to be done
     258                        extension( typeDecl );
     259                        output << "extern unsigned long " << typeDecl->get_name();
     260                        if ( typeDecl->get_base() ) {
     261                                output << " = sizeof( " << genType( typeDecl->get_base(), "", pretty, genC ) << " )";
     262                        } // if
     263                } else {
     264                        output << typeDecl->typeString() << " " << typeDecl->get_name();
     265                        if ( ! typeDecl->get_assertions().empty() ) {
     266                                output << " | { ";
     267                                genCommaList( typeDecl->get_assertions().begin(), typeDecl->get_assertions().end() );
     268                                output << " }";
     269                        }
     270                }
    257271        }
    258272
     
    293307
    294308        void CodeGenerator::visit( ConstructorInit * init ){
    295                 assertf( false, "ConstructorInit nodes should not make it to CodeGen." );
     309                assertf( ! genC, "ConstructorInit nodes should not reach code generation." );
     310                // xxx - generate something reasonable for constructor/destructor pairs
     311                output << "<ctorinit>";
    296312        }
    297313
     
    547563                        // at least one result type of cast, but not an lvalue
    548564                        output << "(";
    549                         output << genType( castExpr->get_result(), "", pretty );
     565                        output << genType( castExpr->get_result(), "", pretty, genC );
    550566                        output << ")";
    551567                } else {
     
    558574
    559575        void CodeGenerator::visit( UntypedMemberExpr * memberExpr ) {
    560                 assert( false );
     576                assertf( ! genC, "UntypedMemberExpr should not reach code generation." );
     577                extension( memberExpr );
     578                memberExpr->get_aggregate()->accept( *this );
     579                output << ".";
     580                memberExpr->get_member()->accept( *this );
    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.