Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    rd3b7937 re551c69  
    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 : Thu Sep 17 15:24:08 2015
     13// Update Count     : 231
    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 << "(&";
     
    392392
    393393        void CodeGenerator::visit( CastExpr *castExpr ) {
    394                 output << "(";
    395                 if ( castExpr->get_results().empty() ) {
    396                         output << "(void)" ;
    397                 } else if ( ! castExpr->get_results().front()->get_isLvalue() ) {
    398                         // at least one result type of cast, but not an lvalue
    399                         output << "(";
    400                         output << genType( castExpr->get_results().front(), "" );
     394                // if the cast is to an lvalue type, then the cast
     395                // should be dropped, since the result of a cast is
     396                // never an lvalue in C
     397                if ( castExpr->get_results().front()->get_isLvalue() ) {
     398                        castExpr->get_arg()->accept( *this );
     399                } else {
     400                        output << "((";
     401                        if ( castExpr->get_results().empty() ) {
     402                                output << "void" ;
     403                        } else {
     404                                output << genType( castExpr->get_results().front(), "" );
     405                        } // if
    401406                        output << ")";
    402                 } else {
    403                         // otherwise, the cast is to an lvalue type, so the cast
    404                         // should be dropped, since the result of a cast is
    405                         // never an lvalue in C
     407                        castExpr->get_arg()->accept( *this );
     408                        output << ")";                 
    406409                }
    407                 castExpr->get_arg()->accept( *this );
    408                 output << ")";
    409         }
    410 
     410        }
     411 
    411412        void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
    412413                assert( false );
    413414        }
    414 
     415 
    415416        void CodeGenerator::visit( MemberExpr *memberExpr ) {
    416417                memberExpr->get_aggregate()->accept( *this );
    417418                output << "." << mangleName( memberExpr->get_member() );
    418419        }
    419 
     420 
    420421        void CodeGenerator::visit( VariableExpr *variableExpr ) {
    421422                OperatorInfo opInfo;
     
    426427                } // if
    427428        }
    428 
     429 
    429430        void CodeGenerator::visit( ConstantExpr *constantExpr ) {
    430431                assert( constantExpr->get_constant() );
    431432                constantExpr->get_constant()->accept( *this );
    432433        }
    433 
     434 
    434435        void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
    435436                output << "sizeof(";
     
    442443        }
    443444
    444         void CodeGenerator::visit( AlignofExpr *sizeofExpr ) {
     445        void CodeGenerator::visit( AlignofExpr *alignofExpr ) {
    445446                // use GCC extension to avoid bumping std to C11
    446447                output << "__alignof__(";
    447                 if ( sizeofExpr->get_isType() ) {
    448                         output << genType( sizeofExpr->get_type(), "" );
    449                 } else {
    450                         sizeofExpr->get_expr()->accept( *this );
     448                if ( alignofExpr->get_isType() ) {
     449                        output << genType( alignofExpr->get_type(), "" );
     450                } else {
     451                        alignofExpr->get_expr()->accept( *this );
    451452                } // if
    452453                output << ")";
    453454        }
    454455
     456        void CodeGenerator::visit( OffsetofExpr *offsetofExpr ) {
     457                // use GCC builtin
     458                output << "__builtin_offsetof(";
     459                output << genType( offsetofExpr->get_type(), "" );
     460                output << ", " << mangleName( offsetofExpr->get_member() );
     461                output << ")";
     462        }
     463 
    455464        void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
    456465                output << "(";
     
    464473                output << ")";
    465474        }
    466 
     475 
    467476        void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
    468477                output << "(";
     
    474483                output << ")";
    475484        }
    476 
     485 
    477486        void CodeGenerator::visit( CommaExpr *commaExpr ) {
    478487                output << "(";
     
    482491                output << ")";
    483492        }
    484 
     493 
    485494        void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
    486 
     495 
    487496        void CodeGenerator::visit( TypeExpr *typeExpr ) {}
    488497
     
    515524                        }
    516525                }
    517                 cur_indent -= CodeGenerator::tabsize;
     526                cur_indent -= CodeGenerator::tabsize; 
    518527
    519528                output << indent << "}";
     
    521530
    522531        void CodeGenerator::visit( ExprStmt *exprStmt ) {
    523                 // I don't see why this check is necessary.
    524                 // If this starts to cause problems then put it back in,
     532                // I don't see why this check is necessary. 
     533                // If this starts to cause problems then put it back in, 
    525534                // with an explanation
    526535                assert( exprStmt );
     
    572581                switchStmt->get_condition()->accept( *this );
    573582                output << " ) ";
    574 
     583               
    575584                output << "{" << std::endl;
    576585                cur_indent += CodeGenerator::tabsize;
     
    592601                } // if
    593602                output << ":\n";
    594 
     603               
    595604                std::list<Statement *> sts = caseStmt->get_statements();
    596605
     
    609618                        if ( ! branchStmt->get_target().empty() )
    610619                                output << "goto " << branchStmt->get_target();
    611                         else {
     620                        else { 
    612621                                if ( branchStmt->get_computedTarget() != 0 ) {
    613622                                        output << "goto *";
     
    660669
    661670        void CodeGenerator::visit( ForStmt *forStmt ) {
    662                 // initialization is always hoisted, so don't
    663                 // bother doing anything with that
     671                // initialization is always hoisted, so don't 
     672                // bother doing anything with that 
    664673                output << "for (;";
    665674
     
    685694        void CodeGenerator::visit( DeclStmt *declStmt ) {
    686695                declStmt->get_decl()->accept( *this );
    687 
     696       
    688697                if ( doSemicolon( declStmt->get_decl() ) ) {
    689698                        output << ";";
Note: See TracChangeset for help on using the changeset viewer.