source: src/CodeGen/CodeGenerator.cc @ 68cd1ce

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 68cd1ce was 68cd1ce, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

unify and fix storage class

  • Property mode set to 100644
File size: 17.7 KB
RevLine 
[51587aa]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[6c4ff37]7// CodeGenerator.cc --
[51587aa]8//
9// Author           : Richard C. Bilson
10// Created On       : Mon May 18 07:44:20 2015
[68cd1ce]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sat Jun 13 07:12:27 2015
13// Update Count     : 129
[51587aa]14//
15
[51b7345]16#include <algorithm>
17#include <iostream>
18#include <cassert>
19#include <list>
20
[68cd1ce]21#include "Parser/ParseNode.h"
22
[51b7345]23#include "SynTree/Type.h"
24#include "SynTree/Expression.h"
25#include "SynTree/Initializer.h"
[68cd1ce]26#include "SynTree/Statement.h"
[51b7345]27
28#include "utility.h"
29#include "UnimplementedError.h"
30
[a61fea9a]31#include "CodeGenerator.h"
[51b7345]32#include "OperatorTable.h"
33#include "GenType.h"
34
35using namespace std;
36
37namespace CodeGen {
[6c4ff37]38        int CodeGenerator::tabsize = 4;
[51587aa]39
[2b6c1e0]40        // the kinds of statements that would ideally be separated by more whitespace
41        bool wantSpacing( Statement * stmt) {
42                return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) ||
43                        dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * > ( stmt ) || dynamic_cast< SwitchStmt *>( stmt );
44        }
45
[6c4ff37]46        CodeGenerator::CodeGenerator( std::ostream &os ) : cur_indent( 0 ), insideFunction( false ), output( os ) { }
[51587aa]47
[6c4ff37]48        CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indent, bool infunp )
49                        : cur_indent( indent ), insideFunction( infunp ), output( os ) {
50                //output << std::string( init );
[51587aa]51        }
52
[6c4ff37]53        CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indent, bool infunp )
54                        : cur_indent( indent ), insideFunction( infunp ), output( os ) {
55                //output << std::string( init );
[51587aa]56        }
57
58        string mangleName( DeclarationWithType *decl ) {
59                if ( decl->get_mangleName() != "" ) {
60                        return decl->get_mangleName();
61                } else {
62                        return decl->get_name();
63                } // if
64        }
[51b7345]65 
[51587aa]66        //*** Declarations
[6c4ff37]67        void CodeGenerator::visit( FunctionDecl *functionDecl ) {
[51587aa]68                handleStorageClass( functionDecl );
[f38c8d9]69                if ( functionDecl->get_isInline() ) {
[6c4ff37]70                        output << "inline ";
[f38c8d9]71                } // if
[6c4ff37]72                output << genType( functionDecl->get_functionType(), mangleName( functionDecl ) );
[51587aa]73
74                // how to get this to the Functype?
75                std::list< Declaration * > olds = functionDecl->get_oldDecls();
76                if ( ! olds.empty() ) {
[6c4ff37]77                        output << " /* function has old declaration */";
[51587aa]78                } // if
79
80                // acceptAll( functionDecl->get_oldDecls(), *this );
81                if ( functionDecl->get_statements() ) {
82                        functionDecl->get_statements()->accept(*this );
83                } // if
84        }
85
[6c4ff37]86        void CodeGenerator::visit( ObjectDecl *objectDecl ) {
[51587aa]87                handleStorageClass( objectDecl );
[6c4ff37]88                output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
[51587aa]89       
90                if ( objectDecl->get_init() ) {
[6c4ff37]91                        output << " = ";
[51587aa]92                        objectDecl->get_init()->accept( *this );
93                } // if
94                if ( objectDecl->get_bitfieldWidth() ) {
[6c4ff37]95                        output << ":";
[51587aa]96                        objectDecl->get_bitfieldWidth()->accept( *this );
97                } // if
98        }
99
[6c4ff37]100        void CodeGenerator::handleAggregate( AggregateDecl *aggDecl ) {
[51587aa]101                if ( aggDecl->get_name() != "" )
[6c4ff37]102                        output << aggDecl->get_name();
[51587aa]103       
104                std::list< Declaration * > &memb = aggDecl->get_members();
105
106                if ( ! memb.empty() ) {
[6c4ff37]107                        output << endl << string( cur_indent, ' ' ) << "{" << endl;
[51587aa]108
[6c4ff37]109                        cur_indent += CodeGenerator::tabsize; 
[51587aa]110                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
[6c4ff37]111                                output << string( cur_indent, ' ' ); 
[51587aa]112                                (*i)->accept(*this );
[6c4ff37]113                                output << ";" << endl;
[51587aa]114                        }
115
[6c4ff37]116                        cur_indent -= CodeGenerator::tabsize; 
[51587aa]117
[6c4ff37]118                        output << string( cur_indent, ' ' ) << "}";
[17cd4eb]119                } // if
[51587aa]120        }
[17cd4eb]121
[6c4ff37]122        void CodeGenerator::visit( StructDecl *structDecl ) {
123                output << "struct ";
[51587aa]124                handleAggregate( structDecl );
125        }
[17cd4eb]126
[6c4ff37]127        void CodeGenerator::visit( UnionDecl *aggregateDecl ) {
128                output << "union ";
[51587aa]129                handleAggregate( aggregateDecl );
130        }
[51b7345]131 
[6c4ff37]132        void CodeGenerator::visit( EnumDecl *aggDecl ) {
133                output << "enum ";
[51587aa]134
135                if ( aggDecl->get_name() != "" )
[6c4ff37]136                        output << aggDecl->get_name();
[51587aa]137       
138                std::list< Declaration* > &memb = aggDecl->get_members();
139
140                if ( ! memb.empty() ) {
[6c4ff37]141                        output << endl << "{" << endl;
[51587aa]142
[6c4ff37]143                        cur_indent += CodeGenerator::tabsize; 
[51587aa]144                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
145                                ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
146                                assert( obj );
[6c4ff37]147                                output << string( cur_indent, ' ' ) << mangleName( obj ); 
[51587aa]148                                if ( obj->get_init() ) {
[6c4ff37]149                                        output << " = ";
[51587aa]150                                        obj->get_init()->accept(*this );
151                                } // if
[6c4ff37]152                                output << "," << endl;
[51587aa]153                        } // for
154
[6c4ff37]155                        cur_indent -= CodeGenerator::tabsize; 
[51587aa]156
[6c4ff37]157                        output << "}" << endl;
[51587aa]158                } // if
159        }
[51b7345]160 
[6c4ff37]161        void CodeGenerator::visit( ContextDecl *aggregateDecl ) {}
[51587aa]162 
[6c4ff37]163        void CodeGenerator::visit( TypedefDecl *typeDecl ) {
164                output << "typedef ";
165                output << genType( typeDecl->get_base(), typeDecl->get_name() );
[51587aa]166        }
[51b7345]167 
[6c4ff37]168        void CodeGenerator::visit( TypeDecl *typeDecl ) {
[51587aa]169                // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
170                // still to be done
[6c4ff37]171                output << "extern unsigned long " << typeDecl->get_name();
[51587aa]172                if ( typeDecl->get_base() ) {
[6c4ff37]173                        output << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )";
[51587aa]174                } // if
175        }
176
[6c4ff37]177        void CodeGenerator::visit( SingleInit *init ) {
[51587aa]178                init->get_value()->accept( *this );
179        }
180
[6c4ff37]181        void CodeGenerator::visit( ListInit *init ) {
182                output << "{ ";
[51587aa]183                genCommaList( init->begin_initializers(), init->end_initializers() );
[6c4ff37]184                output << " }";
[51587aa]185        }
186
[6c4ff37]187        void CodeGenerator::visit( Constant *constant ) { 
188                output << constant->get_value() ;
[51587aa]189        }
190
191        //*** Expressions
[6c4ff37]192        void CodeGenerator::visit( ApplicationExpr *applicationExpr ) {
[51587aa]193                if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
194                        OperatorInfo opInfo;
195                        if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
196                                std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
197                                switch ( opInfo.type ) {
198                                  case OT_PREFIXASSIGN:
199                                  case OT_POSTFIXASSIGN:
200                                  case OT_INFIXASSIGN:
201                                        {
202                                                assert( arg != applicationExpr->get_args().end() );
203                                                if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
204               
205                                                        *arg = addrExpr->get_arg();
206                                                } else {
207                                                        UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
208                                                        newExpr->get_args().push_back( *arg );
209                                                        *arg = newExpr;
210                                                } // if
211                                                break;
212                                        }
213             
214                                  default:
215                                        // do nothing
216                                        ;
217                                }
218           
219                                switch ( opInfo.type ) {
220                                  case OT_INDEX:
221                                        assert( applicationExpr->get_args().size() == 2 );
222                                        (*arg++)->accept( *this );
[6c4ff37]223                                        output << "[";
[51587aa]224                                        (*arg)->accept( *this );
[6c4ff37]225                                        output << "]";
[51587aa]226                                        break;
227             
228                                  case OT_CALL:
229                                        // there are no intrinsic definitions of the function call operator
230                                        assert( false );
231                                        break;
232             
233                                  case OT_PREFIX:
234                                  case OT_PREFIXASSIGN:
235                                        assert( applicationExpr->get_args().size() == 1 );
[6c4ff37]236                                        output << "(";
237                                        output << opInfo.symbol;
[51587aa]238                                        (*arg)->accept( *this );
[6c4ff37]239                                        output << ")";
[51587aa]240                                        break;
241             
242                                  case OT_POSTFIX:
243                                  case OT_POSTFIXASSIGN:
244                                        assert( applicationExpr->get_args().size() == 1 );
245                                        (*arg)->accept( *this );
[6c4ff37]246                                        output << opInfo.symbol;
[51587aa]247                                        break;
248
249                                  case OT_INFIX:
250                                  case OT_INFIXASSIGN:
251                                        assert( applicationExpr->get_args().size() == 2 );
[6c4ff37]252                                        output << "(";
[51587aa]253                                        (*arg++)->accept( *this );
[6c4ff37]254                                        output << opInfo.symbol;
[51587aa]255                                        (*arg)->accept( *this );
[6c4ff37]256                                        output << ")";
[51587aa]257                                        break;
258             
259                                  case OT_CONSTANT:
260                                        // there are no intrinsic definitions of 0 or 1 as functions
261                                        assert( false );
262                                }
[17cd4eb]263                        } else {
[51587aa]264                                varExpr->accept( *this );
[6c4ff37]265                                output << "(";
[51587aa]266                                genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
[6c4ff37]267                                output << ")";
[17cd4eb]268                        } // if
[51587aa]269                } else {
270                        applicationExpr->get_function()->accept( *this );
[6c4ff37]271                        output << "(";
[51587aa]272                        genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
[6c4ff37]273                        output << ")";
[51587aa]274                } // if
275        }
[51b7345]276 
[6c4ff37]277        void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
[51587aa]278                if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
279                        OperatorInfo opInfo;
280                        if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
281                                std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
282                                switch ( opInfo.type ) {
283                                  case OT_INDEX:
284                                        assert( untypedExpr->get_args().size() == 2 );
285                                        (*arg++)->accept( *this );
[6c4ff37]286                                        output << "[";
[51587aa]287                                        (*arg)->accept( *this );
[6c4ff37]288                                        output << "]";
[51587aa]289                                        break;
290             
291                                  case OT_CALL:
292                                        assert( false );
293                                        break;
294             
295                                  case OT_PREFIX:
296                                  case OT_PREFIXASSIGN:
297                                        assert( untypedExpr->get_args().size() == 1 );
[6c4ff37]298                                        output << "(";
299                                        output << opInfo.symbol;
[51587aa]300                                        (*arg)->accept( *this );
[6c4ff37]301                                        output << ")";
[51587aa]302                                        break;
303             
304                                  case OT_POSTFIX:
305                                  case OT_POSTFIXASSIGN:
306                                        assert( untypedExpr->get_args().size() == 1 );
307                                        (*arg)->accept( *this );
[6c4ff37]308                                        output << opInfo.symbol;
[51587aa]309                                        break;
[51b7345]310 
[51587aa]311                                  case OT_INFIX:
312                                  case OT_INFIXASSIGN:
313                                        assert( untypedExpr->get_args().size() == 2 );
[6c4ff37]314                                        output << "(";
[51587aa]315                                        (*arg++)->accept( *this );
[6c4ff37]316                                        output << opInfo.symbol;
[51587aa]317                                        (*arg)->accept( *this );
[6c4ff37]318                                        output << ")";
[51587aa]319                                        break;
320             
321                                  case OT_CONSTANT:
322                                        // there are no intrinsic definitions of 0 or 1 as functions
323                                        assert( false );
324                                }
325                        } else {
326                                nameExpr->accept( *this );
[6c4ff37]327                                output << "(";
[51587aa]328                                genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
[6c4ff37]329                                output << ")";
[51587aa]330                        } // if
331                } else {
332                        untypedExpr->get_function()->accept( *this );
[6c4ff37]333                        output << "(";
[51587aa]334                        genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
[6c4ff37]335                        output << ")";
[51587aa]336                } // if
337        }
[51b7345]338 
[6c4ff37]339        void CodeGenerator::visit( NameExpr *nameExpr ) {
[51587aa]340                OperatorInfo opInfo;
341                if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
342                        assert( opInfo.type == OT_CONSTANT );
[6c4ff37]343                        output << opInfo.symbol;
[51587aa]344                } else {
[6c4ff37]345                        output << nameExpr->get_name();
[51587aa]346                } // if
347        }
[51b7345]348 
[6c4ff37]349        void CodeGenerator::visit( AddressExpr *addressExpr ) {
350                output << "(&";
[51587aa]351                // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
352                if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
[6c4ff37]353                        output << mangleName( variableExpr->get_var() );
[51587aa]354                } else {
355                        addressExpr->get_arg()->accept( *this );
356                } // if
[6c4ff37]357                output << ")";
[51587aa]358        }
359
[6c4ff37]360        void CodeGenerator::visit( CastExpr *castExpr ) {
361                output << "((";
[51587aa]362                if ( castExpr->get_results().empty() ) {
[6c4ff37]363                        output << "void" ;
[51587aa]364                } else {
[6c4ff37]365                        output << genType( castExpr->get_results().front(), "" );
[51587aa]366                } // if
[6c4ff37]367                output << ")";
[51587aa]368                castExpr->get_arg()->accept( *this );
[6c4ff37]369                output << ")";
[51587aa]370        }
[51b7345]371 
[6c4ff37]372        void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
[51587aa]373                assert( false );
374        }
[51b7345]375 
[6c4ff37]376        void CodeGenerator::visit( MemberExpr *memberExpr ) {
[51587aa]377                memberExpr->get_aggregate()->accept( *this );
[6c4ff37]378                output << "." << mangleName( memberExpr->get_member() );
[51587aa]379        }
[51b7345]380 
[6c4ff37]381        void CodeGenerator::visit( VariableExpr *variableExpr ) {
[51587aa]382                OperatorInfo opInfo;
383                if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
[6c4ff37]384                        output << opInfo.symbol;
[51587aa]385                } else {
[6c4ff37]386                        output << mangleName( variableExpr->get_var() );
[51587aa]387                } // if
388        }
[51b7345]389 
[6c4ff37]390        void CodeGenerator::visit( ConstantExpr *constantExpr ) {
[51587aa]391                assert( constantExpr->get_constant() );
392                constantExpr->get_constant()->accept( *this );
393        }
[51b7345]394 
[6c4ff37]395        void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
396                output << "sizeof(";
[51587aa]397                if ( sizeofExpr->get_isType() ) {
[6c4ff37]398                        output << genType( sizeofExpr->get_type(), "" );
[51587aa]399                } else {
400                        sizeofExpr->get_expr()->accept( *this );
401                } // if
[6c4ff37]402                output << ")";
[51587aa]403        }
[51b7345]404 
[6c4ff37]405        void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
406                output << "(";
[51587aa]407                logicalExpr->get_arg1()->accept( *this );
408                if ( logicalExpr->get_isAnd() ) {
[6c4ff37]409                        output << " && ";
[51587aa]410                } else {
[6c4ff37]411                        output << " || ";
[51587aa]412                } // if
413                logicalExpr->get_arg2()->accept( *this );
[6c4ff37]414                output << ")";
[51587aa]415        }
[51b7345]416 
[6c4ff37]417        void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
418                output << "(";
[51587aa]419                conditionalExpr->get_arg1()->accept( *this );
[6c4ff37]420                output << " ? ";
[51587aa]421                conditionalExpr->get_arg2()->accept( *this );
[6c4ff37]422                output << " : ";
[51587aa]423                conditionalExpr->get_arg3()->accept( *this );
[6c4ff37]424                output << ")";
[51587aa]425        }
[51b7345]426 
[6c4ff37]427        void CodeGenerator::visit( CommaExpr *commaExpr ) {
428                output << "(";
[51587aa]429                commaExpr->get_arg1()->accept( *this );
[6c4ff37]430                output << " , ";
[51587aa]431                commaExpr->get_arg2()->accept( *this );
[6c4ff37]432                output << ")";
[51587aa]433        }
[51b7345]434 
[6c4ff37]435        void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
[51b7345]436 
[6c4ff37]437        void CodeGenerator::visit( TypeExpr *typeExpr ) {}
[2b6c1e0]438
[51587aa]439        //*** Statements
[6c4ff37]440        void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
[51587aa]441                std::list<Statement*> ks = compoundStmt->get_kids();
[2b6c1e0]442                output << "{" << endl;
[51587aa]443
[2b6c1e0]444                cur_indent += CodeGenerator::tabsize;
[51587aa]445
446                for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++) {
[2b6c1e0]447                        output << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() );
[51587aa]448                        (*i)->accept(*this );
[2b6c1e0]449
[6c4ff37]450                        output << endl;
[2b6c1e0]451                        if ( wantSpacing( *i ) ) {
452                                output << endl;
453                        }
[51587aa]454                }
[6c4ff37]455                cur_indent -= CodeGenerator::tabsize; 
[51587aa]456
[2b6c1e0]457                output << string( cur_indent, ' ' ) << "}";
[51587aa]458        }
459
[6c4ff37]460        void CodeGenerator::visit( ExprStmt *exprStmt ) {
461                // I don't see why this check is necessary.
462                // If this starts to cause problems then put it back in,
463                // with an explanation
464                assert( exprStmt );
465
466                // if ( exprStmt != 0 ) {
467                exprStmt->get_expr()->accept( *this );
468                output << ";" ;
469                // } // if
[51587aa]470        }
471
[6c4ff37]472        void CodeGenerator::visit( IfStmt *ifStmt ) {
473                output << "if (";
[51587aa]474                ifStmt->get_condition()->accept(*this );
[2b6c1e0]475                output << ") ";
[51587aa]476
477                ifStmt->get_thenPart()->accept(*this );
478
479                if ( ifStmt->get_elsePart() != 0) {
[2b6c1e0]480                        output << " else ";
[51587aa]481                        ifStmt->get_elsePart()->accept(*this );
482                } // if
483        }
484
[6c4ff37]485        void CodeGenerator::visit( SwitchStmt *switchStmt ) {
486                output << "switch (" ;
[51587aa]487                switchStmt->get_condition()->accept(*this );
[2b6c1e0]488                output << ") ";
[6c4ff37]489               
[2b6c1e0]490                output << "{" << std::endl;
[6c4ff37]491                cur_indent += CodeGenerator::tabsize;
[51587aa]492
[6c4ff37]493                acceptAll( switchStmt->get_branches(), *this );
[51587aa]494
[6c4ff37]495                cur_indent -= CodeGenerator::tabsize;
[51587aa]496
[2b6c1e0]497                output << string( cur_indent, ' ' ) << "}";
[51587aa]498        }
499
[6c4ff37]500        void CodeGenerator::visit( CaseStmt *caseStmt ) {
501                output << string( cur_indent, ' ' );
[eb3261f]502                if ( caseStmt->isDefault()) {
[2b6c1e0]503                        output << "default";
[eb3261f]504                } else {
[2b6c1e0]505                        output << "case ";
[51587aa]506                        caseStmt->get_condition()->accept(*this );
[17cd4eb]507                } // if
[6c4ff37]508                output << ":\n";
509               
[51587aa]510                std::list<Statement *> sts = caseStmt->get_statements();
511
[6c4ff37]512                cur_indent += CodeGenerator::tabsize;
[51587aa]513                for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end();  i++) {
[eb3261f]514                        output << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() )  ;
[51587aa]515                        (*i)->accept(*this );
[6c4ff37]516                        output << endl;
[51587aa]517                }
[6c4ff37]518                cur_indent -= CodeGenerator::tabsize;
[51587aa]519        }
520
[6c4ff37]521        void CodeGenerator::visit( BranchStmt *branchStmt ) {
[51587aa]522                switch ( branchStmt->get_type()) {
523                  case BranchStmt::Goto:
524                        if ( ! branchStmt->get_target().empty() )
[6c4ff37]525                                output << "goto " << branchStmt->get_target();
[51587aa]526                        else { 
527                                if ( branchStmt->get_computedTarget() != 0 ) {
[6c4ff37]528                                        output << "goto *";
[51587aa]529                                        branchStmt->get_computedTarget()->accept( *this );
530                                } // if
531                        } // if
532                        break;
533                  case BranchStmt::Break:
[6c4ff37]534                        output << "break";
[51587aa]535                        break;
536                  case BranchStmt::Continue:
[6c4ff37]537                        output << "continue";
[51587aa]538                        break;
539                }
[2b6c1e0]540                output << ";";
[51587aa]541        }
542
543
[6c4ff37]544        void CodeGenerator::visit( ReturnStmt *returnStmt ) {
545                output << "return ";
[51587aa]546
547                // xxx -- check for null expression;
548                if ( returnStmt->get_expr() ) {
549                        returnStmt->get_expr()->accept( *this );
550                } // if
[6c4ff37]551                output << ";";
[51587aa]552        }
553
[6c4ff37]554        void CodeGenerator::visit( WhileStmt *whileStmt ) {
[51587aa]555                if ( whileStmt->get_isDoWhile() )
[6c4ff37]556                        output << "do" ;
[51587aa]557                else {
[6c4ff37]558                        output << "while (" ;
[51587aa]559                        whileStmt->get_condition()->accept(*this );
[6c4ff37]560                        output << ")";
[51587aa]561                } // if
[2b6c1e0]562                output << " ";
[51587aa]563
[2b6c1e0]564                output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
[51587aa]565                whileStmt->get_body()->accept( *this );
566
[eb3261f]567                output << string( cur_indent, ' ' );
[51587aa]568
569                if ( whileStmt->get_isDoWhile() ) {
[6c4ff37]570                        output << " while (" ;
[51587aa]571                        whileStmt->get_condition()->accept(*this );
[6c4ff37]572                        output << ");";
[51587aa]573                } // if
574        }
575
[6c4ff37]576        void CodeGenerator::visit( ForStmt *forStmt ) {
577                output << "for (";
[51587aa]578
579                if ( forStmt->get_initialization() != 0 )
580                        forStmt->get_initialization()->accept( *this );
581                else
[6c4ff37]582                        output << ";";
583               
[51587aa]584                if ( forStmt->get_condition() != 0 )
585                        forStmt->get_condition()->accept( *this );
[6c4ff37]586                output << ";";
[51587aa]587
588                if ( forStmt->get_increment() != 0 )
589                        forStmt->get_increment()->accept( *this );
[2b6c1e0]590                output << ") ";
[51587aa]591
592                if ( forStmt->get_body() != 0 ) {
[2b6c1e0]593                        output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
[51587aa]594                        forStmt->get_body()->accept( *this );
595                } // if
596        }
597
[6c4ff37]598        void CodeGenerator::visit( NullStmt *nullStmt ) {
[eb3261f]599                //output << string( cur_indent, ' ' ) << CodeGenerator::printLabels( nullStmt->get_labels() );
[6c4ff37]600                output << "/* null statement */ ;";
[51587aa]601        }
602
[6c4ff37]603        void CodeGenerator::visit( DeclStmt *declStmt ) {
[51587aa]604                declStmt->get_decl()->accept( *this );
605       
606                if ( doSemicolon( declStmt->get_decl() ) ) {
[6c4ff37]607                        output << ";";
[51587aa]608                } // if
609        }
610
[6c4ff37]611        std::string CodeGenerator::printLabels( std::list< Label > &l ) {
[51587aa]612                std::string str( "" );
[6c4ff37]613                l.unique(); // assumes a sorted list. Why not use set?
[51587aa]614
615                for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ )
616                        str += *i + ": ";
617
618                return str;
619        }
620
[6c4ff37]621        void CodeGenerator::handleStorageClass( Declaration *decl ) {
[51587aa]622                switch ( decl->get_storageClass() ) {
[68cd1ce]623                  case DeclarationNode::Extern:
[6c4ff37]624                        output << "extern ";
[51587aa]625                        break;
[68cd1ce]626                  case DeclarationNode::Static:
[6c4ff37]627                        output << "static ";
[51587aa]628                        break;
[68cd1ce]629                  case DeclarationNode::Auto:
[51587aa]630                        // silently drop storage class
631                        break;
[68cd1ce]632                  case DeclarationNode::Register:
[6c4ff37]633                        output << "register ";
[51587aa]634                        break;
[68cd1ce]635                  case DeclarationNode::Inline:
[f38c8d9]636                        // handled as special via isInline flag (FIX)
637                        break;
[68cd1ce]638                  case DeclarationNode::Fortran:
[f38c8d9]639                        // not handled
[68cd1ce]640                        output << "fortran ";
641                        break;
642                  case DeclarationNode::Noreturn:
643                        // not handled
644                        output << "_Noreturn ";
645                        break;
646                  case DeclarationNode::Threadlocal:
647                        // not handled
648                        output << "_Thread_local ";
649                        break;
650                  case DeclarationNode::NoStorageClass:
[f38c8d9]651                        break;
[843054c2]652                } // switch
[51587aa]653        }
[51b7345]654} // namespace CodeGen
[51587aa]655
656// Local Variables: //
657// tab-width: 4 //
658// mode: c++ //
659// compile-command: "make install" //
660// End: //
Note: See TracBrowser for help on using the repository browser.