Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    r60a8062 r4e5e6cc  
    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 Feb 16 08:32:48 2020
    13 // Update Count     : 532
     11// Last Modified By : Andrew Beach
     12// Last Modified On : Thr May  2 10:47:00 2019
     13// Update Count     : 497
    1414//
    1515#include "CodeGenerator.h"
     
    2323#include "InitTweak/InitTweak.h"     // for getPointerBase
    2424#include "OperatorTable.h"           // for OperatorInfo, operatorLookup
    25 #include "SynTree/LinkageSpec.h"     // for Spec, Intrinsic
     25#include "Parser/LinkageSpec.h"      // for Spec, Intrinsic
    2626#include "SynTree/Attribute.h"       // for Attribute
    2727#include "SynTree/BaseSyntaxNode.h"  // for BaseSyntaxNode
     
    3939        int CodeGenerator::tabsize = 4;
    4040
    41         // The kinds of statements that would ideally be followed by whitespace.
     41        // the kinds of statements that would ideally be followed by whitespace
    4242        bool wantSpacing( Statement * stmt) {
    4343                return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) ||
     
    7878        }
    7979
    80         // Using updateLocation at the beginning of a node and endl within a node should become the method of formating.
     80        /* Using updateLocation at the beginning of a node and endl
     81         * within a node should become the method of formating.
     82         */
    8183        void CodeGenerator::updateLocation( CodeLocation const & to ) {
    8284                // skip if linemarks shouldn't appear or if codelocation is unset
     
    9395                } else {
    9496                        output << "\n# " << to.first_line << " \"" << to.filename
    95                                    << "\"\n" << indent;
     97                               << "\"\n" << indent;
    9698                        currentLocation = to;
    9799                }
     
    129131
    130132        void CodeGenerator::genAttributes( list< Attribute * > & attributes ) {
    131                 if ( attributes.empty() ) return;
     133          if ( attributes.empty() ) return;
    132134                output << "__attribute__ ((";
    133135                for ( list< Attribute * >::iterator attr( attributes.begin() );; ) {
     
    138140                                output << ")";
    139141                        } // if
    140                         if ( ++attr == attributes.end() ) break;
     142                  if ( ++attr == attributes.end() ) break;
    141143                        output << ",";                                                          // separator
    142144                } // for
     
    163165                previsit( (BaseSyntaxNode *)node );
    164166                GuardAction( [this, node](){
    165                                 if ( options.printExprTypes && node->result ) {
    166                                         output << " /* " << genType( node->result, "", options ) << " */ ";
    167                                 }
    168                         } );
     167                        if ( options.printExprTypes && node->result ) {
     168                                output << " /* " << genType( node->result, "", options ) << " */ ";
     169                        }
     170                } );
    169171        }
    170172
     
    196198                // deleted decls should never be used, so don't print them
    197199                if ( objectDecl->isDeleted && options.genC ) return;
    198 
    199                 // gcc allows an empty declarator (no name) for bit-fields and C states: 6.7.2.1 Structure and union specifiers,
    200                 // point 4, page 113: If the (bit field) value is zero, the declaration shall have no declarator.  For anything
    201                 // else, the anonymous name refers to the anonymous object for plan9 inheritance.
    202                 if ( objectDecl->get_name().empty() && options.genC && ! objectDecl->get_bitfieldWidth() ) {
     200                if (objectDecl->get_name().empty() && options.genC ) {
    203201                        // only generate an anonymous name when generating C code, otherwise it clutters the output too much
    204202                        static UniqueName name = { "__anonymous_object" };
    205203                        objectDecl->set_name( name.newName() );
    206                         // Stops unused parameter warnings.
    207                         if ( options.anonymousUnused ) {
    208                                 objectDecl->attributes.push_back( new Attribute( "unused" ) );
    209                         }
     204            // Stops unused parameter warnings.
     205            if ( options.anonymousUnused ) {
     206                objectDecl->attributes.push_back( new Attribute( "unused" ) );
     207            }
    210208                }
    211209
     
    397395                extension( applicationExpr );
    398396                if ( VariableExpr * varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
    399                         const OperatorInfo * opInfo;
    400                         if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && ( opInfo = operatorLookup( varExpr->get_var()->get_name() ) ) ) {
     397                        OperatorInfo opInfo;
     398                        if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
    401399                                std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
    402                                 switch ( opInfo->type ) {
     400                                switch ( opInfo.type ) {
    403401                                  case OT_INDEX:
    404402                                        assert( applicationExpr->get_args().size() == 2 );
     
    421419                                                output << "(";
    422420                                                (*arg++)->accept( *visitor );
    423                                                 output << ") /* " << opInfo->inputName << " */";
     421                                                output << ") /* " << opInfo.inputName << " */";
    424422                                        } else if ( applicationExpr->get_args().size() == 2 ) {
    425423                                                // intrinsic two parameter constructors are essentially bitwise assignment
    426424                                                output << "(";
    427425                                                (*arg++)->accept( *visitor );
    428                                                 output << opInfo->symbol;
     426                                                output << opInfo.symbol;
    429427                                                (*arg)->accept( *visitor );
    430                                                 output << ") /* " << opInfo->inputName << " */";
     428                                                output << ") /* " << opInfo.inputName << " */";
    431429                                        } else {
    432430                                                // no constructors with 0 or more than 2 parameters
     
    439437                                        assert( applicationExpr->get_args().size() == 1 );
    440438                                        output << "(";
    441                                         output << opInfo->symbol;
     439                                        output << opInfo.symbol;
    442440                                        (*arg)->accept( *visitor );
    443441                                        output << ")";
     
    448446                                        assert( applicationExpr->get_args().size() == 1 );
    449447                                        (*arg)->accept( *visitor );
    450                                         output << opInfo->symbol;
     448                                        output << opInfo.symbol;
    451449                                        break;
    452450
     
    457455                                        output << "(";
    458456                                        (*arg++)->accept( *visitor );
    459                                         output << opInfo->symbol;
     457                                        output << opInfo.symbol;
    460458                                        (*arg)->accept( *visitor );
    461459                                        output << ")";
     
    484482                extension( untypedExpr );
    485483                if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->function ) ) {
    486                         const OperatorInfo * opInfo = operatorLookup( nameExpr->name );
    487                         if ( opInfo ) {
     484                        OperatorInfo opInfo;
     485                        if ( operatorLookup( nameExpr->name, opInfo ) ) {
    488486                                std::list< Expression* >::iterator arg = untypedExpr->args.begin();
    489                                 switch ( opInfo->type ) {
     487                                switch ( opInfo.type ) {
    490488                                  case OT_INDEX:
    491489                                        assert( untypedExpr->args.size() == 2 );
     
    506504                                                output << "(";
    507505                                                (*arg++)->accept( *visitor );
    508                                                 output << ") /* " << opInfo->inputName << " */";
     506                                                output << ") /* " << opInfo.inputName << " */";
    509507                                        } else if ( untypedExpr->get_args().size() == 2 ) {
    510508                                                // intrinsic two parameter constructors are essentially bitwise assignment
    511509                                                output << "(";
    512510                                                (*arg++)->accept( *visitor );
    513                                                 output << opInfo->symbol;
     511                                                output << opInfo.symbol;
    514512                                                (*arg)->accept( *visitor );
    515                                                 output << ") /* " << opInfo->inputName << " */";
     513                                                output << ") /* " << opInfo.inputName << " */";
    516514                                        } else {
    517515                                                // no constructors with 0 or more than 2 parameters
     
    519517                                                output << "(";
    520518                                                (*arg++)->accept( *visitor );
    521                                                 output << opInfo->symbol << "{ ";
     519                                                output << opInfo.symbol << "{ ";
    522520                                                genCommaList( arg, untypedExpr->args.end() );
    523                                                 output << "}) /* " << opInfo->inputName << " */";
     521                                                output << "}) /* " << opInfo.inputName << " */";
    524522                                        } // if
    525523                                        break;
     
    530528                                        assert( untypedExpr->args.size() == 1 );
    531529                                        output << "(";
    532                                         output << opInfo->symbol;
     530                                        output << opInfo.symbol;
    533531                                        (*arg)->accept( *visitor );
    534532                                        output << ")";
     
    539537                                        assert( untypedExpr->args.size() == 1 );
    540538                                        (*arg)->accept( *visitor );
    541                                         output << opInfo->symbol;
     539                                        output << opInfo.symbol;
    542540                                        break;
    543541
     
    547545                                        output << "(";
    548546                                        (*arg++)->accept( *visitor );
    549                                         output << opInfo->symbol;
     547                                        output << opInfo.symbol;
    550548                                        (*arg)->accept( *visitor );
    551549                                        output << ")";
     
    579577        void CodeGenerator::postvisit( NameExpr * nameExpr ) {
    580578                extension( nameExpr );
    581                 const OperatorInfo * opInfo = operatorLookup( nameExpr->name );
    582                 if ( opInfo ) {
    583                         if ( opInfo->type == OT_CONSTANT ) {
    584                                 output << opInfo->symbol;
     579                OperatorInfo opInfo;
     580                if ( operatorLookup( nameExpr->name, opInfo ) ) {
     581                        if ( opInfo.type == OT_CONSTANT ) {
     582                                output << opInfo.symbol;
    585583                        } else {
    586                                 output << opInfo->outputName;
     584                                output << opInfo.outputName;
    587585                        }
    588586                } else {
     
    652650        void CodeGenerator::postvisit( VariableExpr * variableExpr ) {
    653651                extension( variableExpr );
    654                 const OperatorInfo * opInfo;
    655                 if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && (opInfo = operatorLookup( variableExpr->get_var()->get_name() )) && opInfo->type == OT_CONSTANT ) {
    656                         output << opInfo->symbol;
     652                OperatorInfo opInfo;
     653                if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
     654                        output << opInfo.symbol;
    657655                } else {
    658656                        output << mangleName( variableExpr->get_var() );
     
    784782
    785783        void CodeGenerator::postvisit( AsmExpr * asmExpr ) {
    786                 if ( !asmExpr->inout.empty() ) {
     784                if ( asmExpr->get_inout() ) {
    787785                        output << "[ ";
    788                         output << asmExpr->inout;
     786                        asmExpr->get_inout()->accept( *visitor );
    789787                        output << " ] ";
    790788                } // if
    791                 asmExpr->constraint->accept( *visitor );
     789                asmExpr->get_constraint()->accept( *visitor );
    792790                output << " ( ";
    793                 asmExpr->operand->accept( *visitor );
     791                asmExpr->get_operand()->accept( *visitor );
    794792                output << " )";
    795793        }
     
    10091007                  case BranchStmt::FallThroughDefault:
    10101008                        assertf( ! options.genC, "fallthru should not reach code generation." );
    1011                         output << "fallthru";
     1009                  output << "fallthru";
    10121010                        break;
    10131011                } // switch
     
    10331031
    10341032                output << ((throwStmt->get_kind() == ThrowStmt::Terminate) ?
    1035                                    "throw" : "throwResume");
     1033                           "throw" : "throwResume");
    10361034                if (throwStmt->get_expr()) {
    10371035                        output << " ";
     
    10481046
    10491047                output << ((stmt->get_kind() == CatchStmt::Terminate) ?
    1050                                    "catch" : "catchResume");
     1048                "catch" : "catchResume");
    10511049                output << "( ";
    10521050                stmt->decl->accept( *visitor );
     
    11851183
    11861184        std::string genName( DeclarationWithType * decl ) {
    1187                 const OperatorInfo * opInfo = operatorLookup( decl->get_name() );
    1188                 if ( opInfo ) {
    1189                         return opInfo->outputName;
     1185                CodeGen::OperatorInfo opInfo;
     1186                if ( operatorLookup( decl->get_name(), opInfo ) ) {
     1187                        return opInfo.outputName;
    11901188                } else {
    11911189                        return decl->get_name();
Note: See TracChangeset for help on using the changeset viewer.