Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    rafc1045 r03e5d14  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // CodeGenerator.cc -- 
     7// CodeGenerator.cc --
    88//
    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 : Wed Mar  2 17:32:16 2016
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Fri May 06 15:40:35 2016
    1313// Update Count     : 243
    1414//
     
    7575        //*** Declarations
    7676        void CodeGenerator::visit( FunctionDecl *functionDecl ) {
     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                }
    7797                handleStorageClass( functionDecl );
    7898                if ( functionDecl->get_isInline() ) {
     
    99119                handleStorageClass( objectDecl );
    100120                output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
    101        
     121
    102122                if ( objectDecl->get_init() ) {
    103123                        output << " = ";
     
    113133                if ( aggDecl->get_name() != "" )
    114134                        output << aggDecl->get_name();
    115        
     135
    116136                std::list< Declaration * > &memb = aggDecl->get_members();
    117137
     
    119139                        output << " {" << endl;
    120140
    121                         cur_indent += CodeGenerator::tabsize; 
     141                        cur_indent += CodeGenerator::tabsize;
    122142                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    123                                 output << indent; 
     143                                output << indent;
    124144                                (*i)->accept( *this );
    125145                                output << ";" << endl;
    126146                        }
    127147
    128                         cur_indent -= CodeGenerator::tabsize; 
     148                        cur_indent -= CodeGenerator::tabsize;
    129149
    130150                        output << indent << "}";
     
    141161                handleAggregate( aggregateDecl );
    142162        }
    143  
     163
    144164        void CodeGenerator::visit( EnumDecl *aggDecl ) {
    145165                output << "enum ";
     
    147167                if ( aggDecl->get_name() != "" )
    148168                        output << aggDecl->get_name();
    149        
     169
    150170                std::list< Declaration* > &memb = aggDecl->get_members();
    151171
     
    153173                        output << " {" << endl;
    154174
    155                         cur_indent += CodeGenerator::tabsize; 
     175                        cur_indent += CodeGenerator::tabsize;
    156176                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    157177                                ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
    158178                                assert( obj );
    159                                 output << indent << mangleName( obj ); 
     179                                output << indent << mangleName( obj );
    160180                                if ( obj->get_init() ) {
    161181                                        output << " = ";
     
    165185                        } // for
    166186
    167                         cur_indent -= CodeGenerator::tabsize; 
     187                        cur_indent -= CodeGenerator::tabsize;
    168188
    169189                        output << indent << "}";
    170190                } // if
    171191        }
    172  
     192
    173193        void CodeGenerator::visit( TraitDecl *aggregateDecl ) {}
    174  
     194
    175195        void CodeGenerator::visit( TypedefDecl *typeDecl ) {
    176196                output << "typedef ";
    177197                output << genType( typeDecl->get_base(), typeDecl->get_name() );
    178198        }
    179  
     199
    180200        void CodeGenerator::visit( TypeDecl *typeDecl ) {
    181201                // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
     
    217237        }
    218238
    219         void CodeGenerator::visit( Constant *constant ) { 
     239        void CodeGenerator::visit( Constant *constant ) {
    220240                output << constant->get_value() ;
    221241        }
     
    234254                                                assert( arg != applicationExpr->get_args().end() );
    235255                                                if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
    236                
     256
    237257                                                        *arg = addrExpr->get_arg();
    238258                                                } else {
     
    243263                                                break;
    244264                                        }
    245              
     265
    246266                                  default:
    247267                                        // do nothing
    248268                                        ;
    249269                                }
    250            
     270
    251271                                switch ( opInfo.type ) {
    252272                                  case OT_INDEX:
     
    257277                                        output << "]";
    258278                                        break;
    259              
     279
    260280                                  case OT_CALL:
    261281                                        // there are no intrinsic definitions of the function call operator
    262282                                        assert( false );
    263283                                        break;
    264              
     284
    265285                                  case OT_PREFIX:
    266286                                  case OT_PREFIXASSIGN:
     
    271291                                        output << ")";
    272292                                        break;
    273              
     293
    274294                                  case OT_POSTFIX:
    275295                                  case OT_POSTFIXASSIGN:
     
    288308                                        output << ")";
    289309                                        break;
    290              
     310
    291311                                  case OT_CONSTANT:
    292312                                  case OT_LABELADDRESS:
     
    307327                } // if
    308328        }
    309  
     329
    310330        void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
    311331                if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
     
    321341                                        output << "]";
    322342                                        break;
    323              
     343
    324344                                  case OT_CALL:
    325345                                        assert( false );
    326346                                        break;
    327              
     347
    328348                                  case OT_PREFIX:
    329349                                  case OT_PREFIXASSIGN:
     
    335355                                        output << ")";
    336356                                        break;
    337              
     357
    338358                                  case OT_POSTFIX:
    339359                                  case OT_POSTFIXASSIGN:
     
    342362                                        output << opInfo.symbol;
    343363                                        break;
    344  
     364
    345365                                  case OT_INFIX:
    346366                                  case OT_INFIXASSIGN:
     
    352372                                        output << ")";
    353373                                        break;
    354                                        
     374
    355375                                  case OT_CONSTANT:
    356376                                        // there are no intrinsic definitions of 0 or 1 as functions
     
    370390                } // if
    371391        }
    372  
     392
    373393        void CodeGenerator::visit( NameExpr *nameExpr ) {
    374394                OperatorInfo opInfo;
     
    380400                } // if
    381401        }
    382  
     402
    383403        void CodeGenerator::visit( AddressExpr *addressExpr ) {
    384404                output << "(&";
     
    409429                output << ")";
    410430        }
    411  
     431
    412432        void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
    413433                assert( false );
    414434        }
    415  
     435
    416436        void CodeGenerator::visit( MemberExpr *memberExpr ) {
    417437                memberExpr->get_aggregate()->accept( *this );
    418438                output << "." << mangleName( memberExpr->get_member() );
    419439        }
    420  
     440
    421441        void CodeGenerator::visit( VariableExpr *variableExpr ) {
    422442                OperatorInfo opInfo;
     
    427447                } // if
    428448        }
    429  
     449
    430450        void CodeGenerator::visit( ConstantExpr *constantExpr ) {
    431451                assert( constantExpr->get_constant() );
    432452                constantExpr->get_constant()->accept( *this );
    433453        }
    434  
     454
    435455        void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
    436456                output << "sizeof(";
     
    469489                assert( false && "OffsetPackExpr should not reach code generation" );
    470490        }
    471  
     491
    472492        void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
    473493                output << "(";
     
    481501                output << ")";
    482502        }
    483  
     503
    484504        void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
    485505                output << "(";
     
    491511                output << ")";
    492512        }
    493  
     513
    494514        void CodeGenerator::visit( CommaExpr *commaExpr ) {
    495515                output << "(";
     
    499519                output << ")";
    500520        }
    501  
     521
    502522        void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
    503  
     523
    504524        void CodeGenerator::visit( TypeExpr *typeExpr ) {}
    505525
     
    532552                        }
    533553                }
    534                 cur_indent -= CodeGenerator::tabsize; 
     554                cur_indent -= CodeGenerator::tabsize;
    535555
    536556                output << indent << "}";
     
    538558
    539559        void CodeGenerator::visit( ExprStmt *exprStmt ) {
    540                 // I don't see why this check is necessary. 
    541                 // If this starts to cause problems then put it back in, 
     560                // I don't see why this check is necessary.
     561                // If this starts to cause problems then put it back in,
    542562                // with an explanation
    543563                assert( exprStmt );
     
    589609                switchStmt->get_condition()->accept( *this );
    590610                output << " ) ";
    591                
     611
    592612                output << "{" << std::endl;
    593613                cur_indent += CodeGenerator::tabsize;
     
    609629                } // if
    610630                output << ":\n";
    611                
     631
    612632                std::list<Statement *> sts = caseStmt->get_statements();
    613633
     
    626646                        if ( ! branchStmt->get_target().empty() )
    627647                                output << "goto " << branchStmt->get_target();
    628                         else { 
     648                        else {
    629649                                if ( branchStmt->get_computedTarget() != 0 ) {
    630650                                        output << "goto *";
     
    677697
    678698        void CodeGenerator::visit( ForStmt *forStmt ) {
    679                 // initialization is always hoisted, so don't 
    680                 // bother doing anything with that 
     699                // initialization is always hoisted, so don't
     700                // bother doing anything with that
    681701                output << "for (;";
    682702
     
    702722        void CodeGenerator::visit( DeclStmt *declStmt ) {
    703723                declStmt->get_decl()->accept( *this );
    704        
     724
    705725                if ( doSemicolon( declStmt->get_decl() ) ) {
    706726                        output << ";";
Note: See TracChangeset for help on using the changeset viewer.