Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    r2a4b088 r803deb1  
    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 : Mon Jan 25 21:22:00 2016
    13 // Update Count     : 242
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Wed Jan 20 12:54:50 2016
     13// Update Count     : 241
    1414//
    1515
     
    2626#include "SynTree/Statement.h"
    2727
    28 #include "Common/utility.h"
    29 #include "Common/UnimplementedError.h"
     28#include "utility.h"
     29#include "UnimplementedError.h"
    3030
    3131#include "CodeGenerator.h"
     
    9898                handleStorageClass( objectDecl );
    9999                output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
    100        
     100
    101101                if ( objectDecl->get_init() ) {
    102102                        output << " = ";
     
    112112                if ( aggDecl->get_name() != "" )
    113113                        output << aggDecl->get_name();
    114        
     114
    115115                std::list< Declaration * > &memb = aggDecl->get_members();
    116116
     
    118118                        output << " {" << endl;
    119119
    120                         cur_indent += CodeGenerator::tabsize; 
     120                        cur_indent += CodeGenerator::tabsize;
    121121                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    122                                 output << indent; 
     122                                output << indent;
    123123                                (*i)->accept( *this );
    124124                                output << ";" << endl;
    125125                        }
    126126
    127                         cur_indent -= CodeGenerator::tabsize; 
     127                        cur_indent -= CodeGenerator::tabsize;
    128128
    129129                        output << indent << "}";
     
    140140                handleAggregate( aggregateDecl );
    141141        }
    142  
     142
    143143        void CodeGenerator::visit( EnumDecl *aggDecl ) {
    144144                output << "enum ";
     
    146146                if ( aggDecl->get_name() != "" )
    147147                        output << aggDecl->get_name();
    148        
     148
    149149                std::list< Declaration* > &memb = aggDecl->get_members();
    150150
     
    152152                        output << " {" << endl;
    153153
    154                         cur_indent += CodeGenerator::tabsize; 
     154                        cur_indent += CodeGenerator::tabsize;
    155155                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
    156156                                ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
    157157                                assert( obj );
    158                                 output << indent << mangleName( obj ); 
     158                                output << indent << mangleName( obj );
    159159                                if ( obj->get_init() ) {
    160160                                        output << " = ";
     
    164164                        } // for
    165165
    166                         cur_indent -= CodeGenerator::tabsize; 
     166                        cur_indent -= CodeGenerator::tabsize;
    167167
    168168                        output << indent << "}";
    169169                } // if
    170170        }
    171  
     171
    172172        void CodeGenerator::visit( ContextDecl *aggregateDecl ) {}
    173  
     173
    174174        void CodeGenerator::visit( TypedefDecl *typeDecl ) {
    175175                output << "typedef ";
    176176                output << genType( typeDecl->get_base(), typeDecl->get_name() );
    177177        }
    178  
     178
    179179        void CodeGenerator::visit( TypeDecl *typeDecl ) {
    180180                // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
     
    216216        }
    217217
    218         void CodeGenerator::visit( Constant *constant ) { 
     218        void CodeGenerator::visit( Constant *constant ) {
    219219                output << constant->get_value() ;
    220220        }
     
    233233                                                assert( arg != applicationExpr->get_args().end() );
    234234                                                if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
    235                
     235
    236236                                                        *arg = addrExpr->get_arg();
    237237                                                } else {
     
    242242                                                break;
    243243                                        }
    244              
     244
    245245                                  default:
    246246                                        // do nothing
    247247                                        ;
    248248                                }
    249            
     249
    250250                                switch ( opInfo.type ) {
    251251                                  case OT_INDEX:
     
    256256                                        output << "]";
    257257                                        break;
    258              
     258
    259259                                  case OT_CALL:
    260260                                        // there are no intrinsic definitions of the function call operator
    261261                                        assert( false );
    262262                                        break;
    263              
     263
    264264                                  case OT_PREFIX:
    265265                                  case OT_PREFIXASSIGN:
     
    270270                                        output << ")";
    271271                                        break;
    272              
     272
    273273                                  case OT_POSTFIX:
    274274                                  case OT_POSTFIXASSIGN:
     
    287287                                        output << ")";
    288288                                        break;
    289              
     289
    290290                                  case OT_CONSTANT:
    291291                                  case OT_LABELADDRESS:
     
    306306                } // if
    307307        }
    308  
     308
    309309        void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
    310310                if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
     
    320320                                        output << "]";
    321321                                        break;
    322              
     322
    323323                                  case OT_CALL:
    324324                                        assert( false );
    325325                                        break;
    326              
     326
    327327                                  case OT_PREFIX:
    328328                                  case OT_PREFIXASSIGN:
     
    334334                                        output << ")";
    335335                                        break;
    336              
     336
    337337                                  case OT_POSTFIX:
    338338                                  case OT_POSTFIXASSIGN:
     
    341341                                        output << opInfo.symbol;
    342342                                        break;
    343  
     343
    344344                                  case OT_INFIX:
    345345                                  case OT_INFIXASSIGN:
     
    351351                                        output << ")";
    352352                                        break;
    353                                        
     353
    354354                                  case OT_CONSTANT:
    355355                                        // there are no intrinsic definitions of 0 or 1 as functions
     
    369369                } // if
    370370        }
    371  
     371
    372372        void CodeGenerator::visit( NameExpr *nameExpr ) {
    373373                OperatorInfo opInfo;
     
    379379                } // if
    380380        }
    381  
     381
    382382        void CodeGenerator::visit( AddressExpr *addressExpr ) {
    383383                output << "(&";
     
    408408                output << ")";
    409409        }
    410  
     410
    411411        void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
    412412                assert( false );
    413413        }
    414  
     414
    415415        void CodeGenerator::visit( MemberExpr *memberExpr ) {
    416416                memberExpr->get_aggregate()->accept( *this );
    417417                output << "." << mangleName( memberExpr->get_member() );
    418418        }
    419  
     419
    420420        void CodeGenerator::visit( VariableExpr *variableExpr ) {
    421421                OperatorInfo opInfo;
     
    426426                } // if
    427427        }
    428  
     428
    429429        void CodeGenerator::visit( ConstantExpr *constantExpr ) {
    430430                assert( constantExpr->get_constant() );
    431431                constantExpr->get_constant()->accept( *this );
    432432        }
    433  
     433
    434434        void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
    435435                output << "sizeof(";
     
    442442        }
    443443
    444         void CodeGenerator::visit( AlignofExpr *alignofExpr ) {
     444        void CodeGenerator::visit( AlignofExpr *sizeofExpr ) {
    445445                // use GCC extension to avoid bumping std to C11
    446446                output << "__alignof__(";
    447                 if ( alignofExpr->get_isType() ) {
    448                         output << genType( alignofExpr->get_type(), "" );
    449                 } else {
    450                         alignofExpr->get_expr()->accept( *this );
     447                if ( sizeofExpr->get_isType() ) {
     448                        output << genType( sizeofExpr->get_type(), "" );
     449                } else {
     450                        sizeofExpr->get_expr()->accept( *this );
    451451                } // if
    452452                output << ")";
    453453        }
    454454
    455         void CodeGenerator::visit( UntypedOffsetofExpr *offsetofExpr ) {
    456                 assert( false );
    457         }
    458 
    459         void CodeGenerator::visit( OffsetofExpr *offsetofExpr ) {
    460                 // use GCC builtin
    461                 output << "__builtin_offsetof(";
    462                 output << genType( offsetofExpr->get_type(), "" );
    463                 output << ", " << mangleName( offsetofExpr->get_member() );
    464                 output << ")";
    465         }
    466  
    467455        void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
    468456                output << "(";
     
    476464                output << ")";
    477465        }
    478  
     466
    479467        void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
    480468                output << "(";
     
    486474                output << ")";
    487475        }
    488  
     476
    489477        void CodeGenerator::visit( CommaExpr *commaExpr ) {
    490478                output << "(";
     
    494482                output << ")";
    495483        }
    496  
     484
    497485        void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
    498  
     486
    499487        void CodeGenerator::visit( TypeExpr *typeExpr ) {}
    500488
     
    527515                        }
    528516                }
    529                 cur_indent -= CodeGenerator::tabsize; 
     517                cur_indent -= CodeGenerator::tabsize;
    530518
    531519                output << indent << "}";
     
    533521
    534522        void CodeGenerator::visit( ExprStmt *exprStmt ) {
    535                 // I don't see why this check is necessary. 
    536                 // If this starts to cause problems then put it back in, 
     523                // I don't see why this check is necessary.
     524                // If this starts to cause problems then put it back in,
    537525                // with an explanation
    538526                assert( exprStmt );
     
    584572                switchStmt->get_condition()->accept( *this );
    585573                output << " ) ";
    586                
     574
    587575                output << "{" << std::endl;
    588576                cur_indent += CodeGenerator::tabsize;
     
    604592                } // if
    605593                output << ":\n";
    606                
     594
    607595                std::list<Statement *> sts = caseStmt->get_statements();
    608596
     
    621609                        if ( ! branchStmt->get_target().empty() )
    622610                                output << "goto " << branchStmt->get_target();
    623                         else { 
     611                        else {
    624612                                if ( branchStmt->get_computedTarget() != 0 ) {
    625613                                        output << "goto *";
     
    672660
    673661        void CodeGenerator::visit( ForStmt *forStmt ) {
    674                 // initialization is always hoisted, so don't 
    675                 // bother doing anything with that 
     662                // initialization is always hoisted, so don't
     663                // bother doing anything with that
    676664                output << "for (;";
    677665
     
    697685        void CodeGenerator::visit( DeclStmt *declStmt ) {
    698686                declStmt->get_decl()->accept( *this );
    699        
     687
    700688                if ( doSemicolon( declStmt->get_decl() ) ) {
    701689                        output << ";";
Note: See TracChangeset for help on using the changeset viewer.