Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    r7baed7d r03e5d14  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Rob Schluntz
    12 // Last Modified On : Fri May 06 16:01:00 2016
    13 // Update Count     : 255
     12// Last Modified On : Fri May 06 15:40:35 2016
     13// Update Count     : 243
    1414//
    1515
     
    2626#include "SynTree/Statement.h"
    2727#include "SynTree/Type.h"
    28 #include "SynTree/Attribute.h"
    2928
    3029#include "Common/utility.h"
     
    3433#include "OperatorTable.h"
    3534#include "GenType.h"
    36 
    37 #include "InitTweak/InitTweak.h"
    3835
    3936using namespace std;
     
    7067        string mangleName( DeclarationWithType *decl ) {
    7168                if ( decl->get_mangleName() != "" ) {
    72                         // need to incorporate scope level in order to differentiate names for destructors
    73                         return decl->get_scopedMangleName();
     69                        return decl->get_mangleName();
    7470                } else {
    7571                        return decl->get_name();
    7672                } // if
    7773        }
    78 
    79         void CodeGenerator::genAttributes( std::list< Attribute * > & attributes ) {
    80                 if ( ! attributes.empty() ) {
    81                         output << "__attribute__ ((";
    82                         for ( Attribute *& attr : attributes ) {
    83                                 if ( ! attr->empty() ) {
    84                                         output << attr->get_name() << "(";
    85                                         genCommaList( attr->get_parameters().begin(), attr->get_parameters().end() );
    86                                         output << ")";
    87                                 }
    88                                 output << ",";
    89                         }
    90                         output << ")) ";
    91                 }
    92         }
    93 
    9474
    9575        //*** Declarations
    9676        void CodeGenerator::visit( FunctionDecl *functionDecl ) {
    97                 genAttributes( functionDecl->get_attributes() );
    98 
     77                // generalize this
     78                FunctionDecl::Attribute attr = functionDecl->get_attribute();
     79                switch ( attr.type ) {
     80                        case FunctionDecl::Attribute::Constructor:
     81                                output << "__attribute__ ((constructor";
     82                                if ( attr.priority != FunctionDecl::Attribute::Default ) {
     83                                        output << "(" << attr.priority << ")";
     84                                }
     85                                output << ")) ";
     86                                break;
     87                        case FunctionDecl::Attribute::Destructor:
     88                                output << "__attribute__ ((destructor";
     89                                if ( attr.priority != FunctionDecl::Attribute::Default ) {
     90                                        output << "(" << attr.priority << ")";
     91                                }
     92                                output << ")) ";
     93                                break;
     94                        default:
     95                                break;
     96                }
    9997                handleStorageClass( functionDecl );
    10098                if ( functionDecl->get_isInline() ) {
     
    235233                printDesignators( init->get_designators() );
    236234                output << "{ ";
    237                 if ( init->begin_initializers() == init->end_initializers() ) {
    238                         // illegal to leave initializer list empty for scalar initializers,
    239                         // but always legal to have 0
    240                         output << "0";
    241                 } else {
    242                         genCommaList( init->begin_initializers(), init->end_initializers() );
    243                 }
     235                genCommaList( init->begin_initializers(), init->end_initializers() );
    244236                output << " }";
    245237        }
     
    259251                                  case OT_POSTFIXASSIGN:
    260252                                  case OT_INFIXASSIGN:
    261                                   case OT_CTOR:
    262                                   case OT_DTOR:
    263253                                        {
    264254                                                assert( arg != applicationExpr->get_args().end() );
    265255                                                if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
    266                                                         // remove & from first assignment/ctor argument
     256
    267257                                                        *arg = addrExpr->get_arg();
    268258                                                } else {
    269                                                         // no address-of operator, so must be a pointer - add dereference
    270259                                                        UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
    271260                                                        newExpr->get_args().push_back( *arg );
    272                                                         assert( (*arg)->get_results().size() == 1 );
    273                                                         Type * type = InitTweak::getPointerBase( (*arg)->get_results().front() );
    274                                                         assert( type );
    275                                                         newExpr->get_results().push_back( type );
    276261                                                        *arg = newExpr;
    277262                                                } // if
     
    298283                                        break;
    299284
    300                                   case OT_CTOR:
    301                                   case OT_DTOR:
    302                                         if ( applicationExpr->get_args().size() == 1 ) {
    303                                                 // the expression fed into a single parameter constructor or destructor
    304                                                 // may contain side effects, so must still output this expression
    305                                                 output << "(";
    306                                                 (*arg++)->accept( *this );
    307                                                 output << ") /* " << opInfo.inputName << " */";
    308                                         } else if ( applicationExpr->get_args().size() == 2 ) {
    309                                                 // intrinsic two parameter constructors are essentially bitwise assignment
    310                                                 output << "(";
    311                                                 (*arg++)->accept( *this );
    312                                                 output << opInfo.symbol;
    313                                                 (*arg)->accept( *this );
    314                                                 output << ") /* " << opInfo.inputName << " */";
    315                                         } else {
    316                                                 // no constructors with 0 or more than 2 parameters
    317                                                 assert( false );
    318                                         }
    319                                         break;
    320 
    321285                                  case OT_PREFIX:
    322286                                  case OT_PREFIXASSIGN:
     
    334298                                        output << opInfo.symbol;
    335299                                        break;
    336 
    337300
    338301                                  case OT_INFIX:
     
    381344                                  case OT_CALL:
    382345                                        assert( false );
    383 
    384 
    385                                   case OT_CTOR:
    386                                   case OT_DTOR:
    387                                         if ( untypedExpr->get_args().size() == 1 ) {
    388                                                 // the expression fed into a single parameter constructor or destructor
    389                                                 // may contain side effects, so must still output this expression
    390                                                 output << "(";
    391                                                 (*arg++)->accept( *this );
    392                                                 output << ") /* " << opInfo.inputName << " */";
    393                                         } else if ( untypedExpr->get_args().size() == 2 ) {
    394                                                 // intrinsic two parameter constructors are essentially bitwise assignment
    395                                                 output << "(";
    396                                                 (*arg++)->accept( *this );
    397                                                 output << opInfo.symbol;
    398                                                 (*arg)->accept( *this );
    399                                                 output << ") /* " << opInfo.inputName << " */";
    400                                         } else {
    401                                                 // no constructors with 0 or more than 2 parameters
    402                                                 assert( false );
    403                                         }
    404346                                        break;
    405347
     
    616558
    617559        void CodeGenerator::visit( ExprStmt *exprStmt ) {
     560                // I don't see why this check is necessary.
     561                // If this starts to cause problems then put it back in,
     562                // with an explanation
    618563                assert( exprStmt );
    619                 // cast the top-level expression to void to reduce gcc warnings.
    620                 Expression * expr = new CastExpr( exprStmt->get_expr() );
    621                 expr->accept( *this );
    622                 output << ";";
     564
     565                // if ( exprStmt != 0 ) {
     566                exprStmt->get_expr()->accept( *this );
     567                output << ";" ;
     568                // } // if
    623569        }
    624570
     
    729675
    730676        void CodeGenerator::visit( WhileStmt *whileStmt ) {
    731                 if ( whileStmt->get_isDoWhile() ) {
     677                if ( whileStmt->get_isDoWhile() )
    732678                        output << "do" ;
    733                 } else {
     679                else {
    734680                        output << "while (" ;
    735681                        whileStmt->get_condition()->accept( *this );
     
    755701                output << "for (;";
    756702
    757                 if ( forStmt->get_condition() != 0 ) {
     703                if ( forStmt->get_condition() != 0 )
    758704                        forStmt->get_condition()->accept( *this );
    759                 }
    760705                output << ";";
    761706
    762                 if ( forStmt->get_increment() != 0 ) {
    763                         // cast the top-level expression to void to reduce gcc warnings.
    764                         Expression * expr = new CastExpr( forStmt->get_increment() );
    765                         expr->accept( *this );
    766                 }
     707                if ( forStmt->get_increment() != 0 )
     708                        forStmt->get_increment()->accept( *this );
    767709                output << ") ";
    768710
Note: See TracChangeset for help on using the changeset viewer.