source: src/CodeGen/CodeGenerator.cc @ bfebb6c5

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 bfebb6c5 was e551c69, checked in by Aaron Moss <a3moss@…>, 9 years ago

Fix code generation for offsetofexpr

  • Property mode set to 100644
File size: 20.5 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
[f32c7f4]11// Last Modified By : Rob Schluntz
[971ae89]12// Last Modified On : Thu Sep 17 15:24:08 2015
13// Update Count     : 231
[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
[145f1fc]40        // the kinds of statements that would ideally be followed by whitespace
[2b6c1e0]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
[cda48b6]46        ostream & CodeGenerator::Indenter::operator()( ostream & output ) {
47          return output << string( cg.cur_indent, ' ' );
48        }
49
50        ostream & operator<<( ostream & output, CodeGenerator::Indenter &indent ) {
51                return indent( output );
52        }
[51587aa]53
[7f5566b]54        CodeGenerator::CodeGenerator( std::ostream &os ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ) { }
[cda48b6]55
56        CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indentation, bool infunp )
[7f5566b]57                        : indent( *this), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
[6c4ff37]58                //output << std::string( init );
[51587aa]59        }
60
[cda48b6]61        CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indentation, bool infunp )
[7f5566b]62                        : indent( *this ), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
[6c4ff37]63                //output << std::string( init );
[51587aa]64        }
65
66        string mangleName( DeclarationWithType *decl ) {
67                if ( decl->get_mangleName() != "" ) {
68                        return decl->get_mangleName();
69                } else {
70                        return decl->get_name();
71                } // if
72        }
[94b4364]73
[51587aa]74        //*** Declarations
[6c4ff37]75        void CodeGenerator::visit( FunctionDecl *functionDecl ) {
[51587aa]76                handleStorageClass( functionDecl );
[f38c8d9]77                if ( functionDecl->get_isInline() ) {
[6c4ff37]78                        output << "inline ";
[f38c8d9]79                } // if
[de62360d]80                if ( functionDecl->get_isNoreturn() ) {
81                        output << "_Noreturn ";
82                } // if
[6c4ff37]83                output << genType( functionDecl->get_functionType(), mangleName( functionDecl ) );
[51587aa]84
85                // how to get this to the Functype?
86                std::list< Declaration * > olds = functionDecl->get_oldDecls();
87                if ( ! olds.empty() ) {
[6c4ff37]88                        output << " /* function has old declaration */";
[51587aa]89                } // if
90
91                // acceptAll( functionDecl->get_oldDecls(), *this );
92                if ( functionDecl->get_statements() ) {
[7f5566b]93                        functionDecl->get_statements()->accept( *this );
[51587aa]94                } // if
95        }
96
[6c4ff37]97        void CodeGenerator::visit( ObjectDecl *objectDecl ) {
[51587aa]98                handleStorageClass( objectDecl );
[6c4ff37]99                output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
[51587aa]100       
101                if ( objectDecl->get_init() ) {
[6c4ff37]102                        output << " = ";
[51587aa]103                        objectDecl->get_init()->accept( *this );
104                } // if
105                if ( objectDecl->get_bitfieldWidth() ) {
[6c4ff37]106                        output << ":";
[51587aa]107                        objectDecl->get_bitfieldWidth()->accept( *this );
108                } // if
109        }
110
[6c4ff37]111        void CodeGenerator::handleAggregate( AggregateDecl *aggDecl ) {
[51587aa]112                if ( aggDecl->get_name() != "" )
[6c4ff37]113                        output << aggDecl->get_name();
[51587aa]114       
115                std::list< Declaration * > &memb = aggDecl->get_members();
116
117                if ( ! memb.empty() ) {
[94b4364]118                        output << " {" << endl;
[51587aa]119
[6c4ff37]120                        cur_indent += CodeGenerator::tabsize; 
[51587aa]121                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
[cda48b6]122                                output << indent; 
[7f5566b]123                                (*i)->accept( *this );
[6c4ff37]124                                output << ";" << endl;
[51587aa]125                        }
126
[6c4ff37]127                        cur_indent -= CodeGenerator::tabsize; 
[51587aa]128
[cda48b6]129                        output << indent << "}";
[17cd4eb]130                } // if
[51587aa]131        }
[17cd4eb]132
[6c4ff37]133        void CodeGenerator::visit( StructDecl *structDecl ) {
134                output << "struct ";
[51587aa]135                handleAggregate( structDecl );
136        }
[17cd4eb]137
[6c4ff37]138        void CodeGenerator::visit( UnionDecl *aggregateDecl ) {
139                output << "union ";
[51587aa]140                handleAggregate( aggregateDecl );
141        }
[51b7345]142 
[6c4ff37]143        void CodeGenerator::visit( EnumDecl *aggDecl ) {
144                output << "enum ";
[51587aa]145
146                if ( aggDecl->get_name() != "" )
[6c4ff37]147                        output << aggDecl->get_name();
[51587aa]148       
149                std::list< Declaration* > &memb = aggDecl->get_members();
150
151                if ( ! memb.empty() ) {
[cda48b6]152                        output << " {" << endl;
[51587aa]153
[6c4ff37]154                        cur_indent += CodeGenerator::tabsize; 
[51587aa]155                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
156                                ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
157                                assert( obj );
[cda48b6]158                                output << indent << mangleName( obj ); 
[51587aa]159                                if ( obj->get_init() ) {
[6c4ff37]160                                        output << " = ";
[7f5566b]161                                        obj->get_init()->accept( *this );
[51587aa]162                                } // if
[6c4ff37]163                                output << "," << endl;
[51587aa]164                        } // for
165
[6c4ff37]166                        cur_indent -= CodeGenerator::tabsize; 
[51587aa]167
[cda48b6]168                        output << indent << "}";
[51587aa]169                } // if
170        }
[51b7345]171 
[6c4ff37]172        void CodeGenerator::visit( ContextDecl *aggregateDecl ) {}
[51587aa]173 
[6c4ff37]174        void CodeGenerator::visit( TypedefDecl *typeDecl ) {
175                output << "typedef ";
176                output << genType( typeDecl->get_base(), typeDecl->get_name() );
[51587aa]177        }
[51b7345]178 
[6c4ff37]179        void CodeGenerator::visit( TypeDecl *typeDecl ) {
[51587aa]180                // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
181                // still to be done
[6c4ff37]182                output << "extern unsigned long " << typeDecl->get_name();
[51587aa]183                if ( typeDecl->get_base() ) {
[6c4ff37]184                        output << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )";
[51587aa]185                } // if
186        }
187
[e45215c]188        void CodeGenerator::printDesignators( std::list< Expression * > & designators ) {
189                typedef std::list< Expression * > DesignatorList;
190                if ( designators.size() == 0 ) return;
191                for ( DesignatorList::iterator iter = designators.begin(); iter != designators.end(); ++iter ) {
[8a4da06]192                        if ( dynamic_cast< NameExpr * >( *iter ) ) {
[f32c7f4]193                                // if expression is a name, then initializing aggregate member
194                                output << ".";
195                                (*iter)->accept( *this );
196                        } else {
197                                // if not a simple name, it has to be a constant expression, i.e. an array designator
[e45215c]198                                output << "[";
199                                (*iter)->accept( *this );
200                                output << "]";
201                        }
202                }
203                output << " = ";
204        }
205
[6c4ff37]206        void CodeGenerator::visit( SingleInit *init ) {
[e45215c]207                printDesignators( init->get_designators() );
[51587aa]208                init->get_value()->accept( *this );
209        }
210
[6c4ff37]211        void CodeGenerator::visit( ListInit *init ) {
[e45215c]212                printDesignators( init->get_designators() );
[6c4ff37]213                output << "{ ";
[51587aa]214                genCommaList( init->begin_initializers(), init->end_initializers() );
[6c4ff37]215                output << " }";
[51587aa]216        }
217
[6c4ff37]218        void CodeGenerator::visit( Constant *constant ) { 
219                output << constant->get_value() ;
[51587aa]220        }
221
222        //*** Expressions
[6c4ff37]223        void CodeGenerator::visit( ApplicationExpr *applicationExpr ) {
[51587aa]224                if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
225                        OperatorInfo opInfo;
226                        if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
227                                std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
228                                switch ( opInfo.type ) {
229                                  case OT_PREFIXASSIGN:
230                                  case OT_POSTFIXASSIGN:
231                                  case OT_INFIXASSIGN:
232                                        {
233                                                assert( arg != applicationExpr->get_args().end() );
234                                                if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
235               
236                                                        *arg = addrExpr->get_arg();
237                                                } else {
238                                                        UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
239                                                        newExpr->get_args().push_back( *arg );
240                                                        *arg = newExpr;
241                                                } // if
242                                                break;
243                                        }
244             
245                                  default:
246                                        // do nothing
247                                        ;
248                                }
249           
250                                switch ( opInfo.type ) {
251                                  case OT_INDEX:
252                                        assert( applicationExpr->get_args().size() == 2 );
253                                        (*arg++)->accept( *this );
[6c4ff37]254                                        output << "[";
[51587aa]255                                        (*arg)->accept( *this );
[6c4ff37]256                                        output << "]";
[51587aa]257                                        break;
258             
259                                  case OT_CALL:
260                                        // there are no intrinsic definitions of the function call operator
261                                        assert( false );
262                                        break;
263             
264                                  case OT_PREFIX:
265                                  case OT_PREFIXASSIGN:
266                                        assert( applicationExpr->get_args().size() == 1 );
[6c4ff37]267                                        output << "(";
268                                        output << opInfo.symbol;
[51587aa]269                                        (*arg)->accept( *this );
[6c4ff37]270                                        output << ")";
[51587aa]271                                        break;
272             
273                                  case OT_POSTFIX:
274                                  case OT_POSTFIXASSIGN:
275                                        assert( applicationExpr->get_args().size() == 1 );
276                                        (*arg)->accept( *this );
[6c4ff37]277                                        output << opInfo.symbol;
[51587aa]278                                        break;
279
280                                  case OT_INFIX:
281                                  case OT_INFIXASSIGN:
282                                        assert( applicationExpr->get_args().size() == 2 );
[6c4ff37]283                                        output << "(";
[51587aa]284                                        (*arg++)->accept( *this );
[6c4ff37]285                                        output << opInfo.symbol;
[51587aa]286                                        (*arg)->accept( *this );
[6c4ff37]287                                        output << ")";
[51587aa]288                                        break;
289             
290                                  case OT_CONSTANT:
[721f17a]291                                  case OT_LABELADDRESS:
292                                        // there are no intrinsic definitions of 0/1 or label addresses as functions
[51587aa]293                                        assert( false );
294                                }
[17cd4eb]295                        } else {
[51587aa]296                                varExpr->accept( *this );
[6c4ff37]297                                output << "(";
[51587aa]298                                genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
[6c4ff37]299                                output << ")";
[17cd4eb]300                        } // if
[51587aa]301                } else {
302                        applicationExpr->get_function()->accept( *this );
[6c4ff37]303                        output << "(";
[51587aa]304                        genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
[6c4ff37]305                        output << ")";
[51587aa]306                } // if
307        }
[51b7345]308 
[6c4ff37]309        void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
[51587aa]310                if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
311                        OperatorInfo opInfo;
312                        if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
313                                std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
314                                switch ( opInfo.type ) {
315                                  case OT_INDEX:
316                                        assert( untypedExpr->get_args().size() == 2 );
317                                        (*arg++)->accept( *this );
[6c4ff37]318                                        output << "[";
[51587aa]319                                        (*arg)->accept( *this );
[6c4ff37]320                                        output << "]";
[51587aa]321                                        break;
322             
323                                  case OT_CALL:
324                                        assert( false );
325                                        break;
326             
327                                  case OT_PREFIX:
328                                  case OT_PREFIXASSIGN:
[de62360d]329                                  case OT_LABELADDRESS:
[51587aa]330                                        assert( untypedExpr->get_args().size() == 1 );
[6c4ff37]331                                        output << "(";
332                                        output << opInfo.symbol;
[51587aa]333                                        (*arg)->accept( *this );
[6c4ff37]334                                        output << ")";
[51587aa]335                                        break;
336             
337                                  case OT_POSTFIX:
338                                  case OT_POSTFIXASSIGN:
339                                        assert( untypedExpr->get_args().size() == 1 );
340                                        (*arg)->accept( *this );
[6c4ff37]341                                        output << opInfo.symbol;
[51587aa]342                                        break;
[51b7345]343 
[51587aa]344                                  case OT_INFIX:
345                                  case OT_INFIXASSIGN:
346                                        assert( untypedExpr->get_args().size() == 2 );
[6c4ff37]347                                        output << "(";
[51587aa]348                                        (*arg++)->accept( *this );
[6c4ff37]349                                        output << opInfo.symbol;
[51587aa]350                                        (*arg)->accept( *this );
[6c4ff37]351                                        output << ")";
[51587aa]352                                        break;
[de62360d]353                                       
[51587aa]354                                  case OT_CONSTANT:
355                                        // there are no intrinsic definitions of 0 or 1 as functions
356                                        assert( false );
357                                }
358                        } else {
359                                nameExpr->accept( *this );
[6c4ff37]360                                output << "(";
[51587aa]361                                genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
[6c4ff37]362                                output << ")";
[51587aa]363                        } // if
364                } else {
365                        untypedExpr->get_function()->accept( *this );
[6c4ff37]366                        output << "(";
[51587aa]367                        genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
[6c4ff37]368                        output << ")";
[51587aa]369                } // if
370        }
[51b7345]371 
[6c4ff37]372        void CodeGenerator::visit( NameExpr *nameExpr ) {
[51587aa]373                OperatorInfo opInfo;
374                if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
375                        assert( opInfo.type == OT_CONSTANT );
[6c4ff37]376                        output << opInfo.symbol;
[51587aa]377                } else {
[6c4ff37]378                        output << nameExpr->get_name();
[51587aa]379                } // if
380        }
[51b7345]381 
[6c4ff37]382        void CodeGenerator::visit( AddressExpr *addressExpr ) {
383                output << "(&";
[51587aa]384                // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
385                if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
[6c4ff37]386                        output << mangleName( variableExpr->get_var() );
[51587aa]387                } else {
388                        addressExpr->get_arg()->accept( *this );
389                } // if
[6c4ff37]390                output << ")";
[51587aa]391        }
392
[6c4ff37]393        void CodeGenerator::visit( CastExpr *castExpr ) {
[76b48f1]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 );
[51587aa]399                } else {
[76b48f1]400                        output << "((";
401                        if ( castExpr->get_results().empty() ) {
402                                output << "void" ;
403                        } else {
404                                output << genType( castExpr->get_results().front(), "" );
405                        } // if
406                        output << ")";
407                        castExpr->get_arg()->accept( *this );
408                        output << ")";                 
409                }
[51587aa]410        }
[51b7345]411 
[6c4ff37]412        void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
[51587aa]413                assert( false );
414        }
[51b7345]415 
[6c4ff37]416        void CodeGenerator::visit( MemberExpr *memberExpr ) {
[51587aa]417                memberExpr->get_aggregate()->accept( *this );
[6c4ff37]418                output << "." << mangleName( memberExpr->get_member() );
[51587aa]419        }
[51b7345]420 
[6c4ff37]421        void CodeGenerator::visit( VariableExpr *variableExpr ) {
[51587aa]422                OperatorInfo opInfo;
423                if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
[6c4ff37]424                        output << opInfo.symbol;
[51587aa]425                } else {
[6c4ff37]426                        output << mangleName( variableExpr->get_var() );
[51587aa]427                } // if
428        }
[51b7345]429 
[6c4ff37]430        void CodeGenerator::visit( ConstantExpr *constantExpr ) {
[51587aa]431                assert( constantExpr->get_constant() );
432                constantExpr->get_constant()->accept( *this );
433        }
[51b7345]434 
[6c4ff37]435        void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
436                output << "sizeof(";
[51587aa]437                if ( sizeofExpr->get_isType() ) {
[6c4ff37]438                        output << genType( sizeofExpr->get_type(), "" );
[51587aa]439                } else {
440                        sizeofExpr->get_expr()->accept( *this );
441                } // if
[6c4ff37]442                output << ")";
[51587aa]443        }
[47534159]444
[25a054f]445        void CodeGenerator::visit( AlignofExpr *alignofExpr ) {
[47534159]446                // use GCC extension to avoid bumping std to C11
447                output << "__alignof__(";
[25a054f]448                if ( alignofExpr->get_isType() ) {
449                        output << genType( alignofExpr->get_type(), "" );
[47534159]450                } else {
[25a054f]451                        alignofExpr->get_expr()->accept( *this );
[47534159]452                } // if
453                output << ")";
454        }
[25a054f]455
456        void CodeGenerator::visit( OffsetofExpr *offsetofExpr ) {
457                // use GCC builtin
458                output << "__builtin_offsetof(";
459                output << genType( offsetofExpr->get_type(), "" );
[e551c69]460                output << ", " << mangleName( offsetofExpr->get_member() );
[25a054f]461                output << ")";
462        }
[51b7345]463 
[6c4ff37]464        void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
465                output << "(";
[51587aa]466                logicalExpr->get_arg1()->accept( *this );
467                if ( logicalExpr->get_isAnd() ) {
[6c4ff37]468                        output << " && ";
[51587aa]469                } else {
[6c4ff37]470                        output << " || ";
[51587aa]471                } // if
472                logicalExpr->get_arg2()->accept( *this );
[6c4ff37]473                output << ")";
[51587aa]474        }
[51b7345]475 
[6c4ff37]476        void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
477                output << "(";
[51587aa]478                conditionalExpr->get_arg1()->accept( *this );
[6c4ff37]479                output << " ? ";
[51587aa]480                conditionalExpr->get_arg2()->accept( *this );
[6c4ff37]481                output << " : ";
[51587aa]482                conditionalExpr->get_arg3()->accept( *this );
[6c4ff37]483                output << ")";
[51587aa]484        }
[51b7345]485 
[6c4ff37]486        void CodeGenerator::visit( CommaExpr *commaExpr ) {
487                output << "(";
[51587aa]488                commaExpr->get_arg1()->accept( *this );
[6c4ff37]489                output << " , ";
[51587aa]490                commaExpr->get_arg2()->accept( *this );
[6c4ff37]491                output << ")";
[51587aa]492        }
[51b7345]493 
[6c4ff37]494        void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
[51b7345]495 
[6c4ff37]496        void CodeGenerator::visit( TypeExpr *typeExpr ) {}
[2b6c1e0]497
[7f5566b]498        void CodeGenerator::visit( AsmExpr *asmExpr ) {
499                if ( asmExpr->get_inout() ) {
500                        output << "[ ";
501                        asmExpr->get_inout()->accept( *this );
502                        output << " ] ";
503                } // if
504                asmExpr->get_constraint()->accept( *this );
505                output << " ( ";
506                asmExpr->get_operand()->accept( *this );
507                output << " )";
508        }
509
[51587aa]510        //*** Statements
[6c4ff37]511        void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
[51587aa]512                std::list<Statement*> ks = compoundStmt->get_kids();
[2b6c1e0]513                output << "{" << endl;
[51587aa]514
[2b6c1e0]515                cur_indent += CodeGenerator::tabsize;
[51587aa]516
[7f5566b]517                for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++ ) {
[cda48b6]518                        output << indent << printLabels( (*i)->get_labels() );
[7f5566b]519                        (*i)->accept( *this );
[2b6c1e0]520
[6c4ff37]521                        output << endl;
[2b6c1e0]522                        if ( wantSpacing( *i ) ) {
523                                output << endl;
524                        }
[51587aa]525                }
[6c4ff37]526                cur_indent -= CodeGenerator::tabsize; 
[51587aa]527
[cda48b6]528                output << indent << "}";
[51587aa]529        }
530
[6c4ff37]531        void CodeGenerator::visit( ExprStmt *exprStmt ) {
532                // I don't see why this check is necessary.
533                // If this starts to cause problems then put it back in,
534                // with an explanation
535                assert( exprStmt );
536
537                // if ( exprStmt != 0 ) {
538                exprStmt->get_expr()->accept( *this );
539                output << ";" ;
540                // } // if
[51587aa]541        }
542
[7f5566b]543        void CodeGenerator::visit( AsmStmt *asmStmt ) {
544                output << "asm ";
545                if ( asmStmt->get_voltile() ) output << "volatile ";
546                if ( ! asmStmt->get_gotolabels().empty()  ) output << "goto ";
547                output << "( ";
548                if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
549                output << " : ";
550                genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
551                output << " : ";
552                genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
553                output << " : ";
554                genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
555                if ( ! asmStmt->get_gotolabels().empty() ) {
556                        output << " : ";
557                        for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
558                                output << *begin++;
559                                if ( begin == asmStmt->get_gotolabels().end() ) break;
560                                output << ", ";
561                        } // for
562                } // if
563                output << " );" ;
564        }
565
[6c4ff37]566        void CodeGenerator::visit( IfStmt *ifStmt ) {
[7f5566b]567                output << "if ( ";
568                ifStmt->get_condition()->accept( *this );
569                output << " ) ";
[51587aa]570
[7f5566b]571                ifStmt->get_thenPart()->accept( *this );
[51587aa]572
573                if ( ifStmt->get_elsePart() != 0) {
[2b6c1e0]574                        output << " else ";
[7f5566b]575                        ifStmt->get_elsePart()->accept( *this );
[51587aa]576                } // if
577        }
578
[6c4ff37]579        void CodeGenerator::visit( SwitchStmt *switchStmt ) {
[7f5566b]580                output << "switch ( " ;
581                switchStmt->get_condition()->accept( *this );
582                output << " ) ";
[6c4ff37]583               
[2b6c1e0]584                output << "{" << std::endl;
[6c4ff37]585                cur_indent += CodeGenerator::tabsize;
[51587aa]586
[6c4ff37]587                acceptAll( switchStmt->get_branches(), *this );
[51587aa]588
[6c4ff37]589                cur_indent -= CodeGenerator::tabsize;
[51587aa]590
[cda48b6]591                output << indent << "}";
[51587aa]592        }
593
[6c4ff37]594        void CodeGenerator::visit( CaseStmt *caseStmt ) {
[cda48b6]595                output << indent;
[eb3261f]596                if ( caseStmt->isDefault()) {
[2b6c1e0]597                        output << "default";
[eb3261f]598                } else {
[2b6c1e0]599                        output << "case ";
[7f5566b]600                        caseStmt->get_condition()->accept( *this );
[17cd4eb]601                } // if
[6c4ff37]602                output << ":\n";
603               
[51587aa]604                std::list<Statement *> sts = caseStmt->get_statements();
605
[6c4ff37]606                cur_indent += CodeGenerator::tabsize;
[51587aa]607                for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end();  i++) {
[cda48b6]608                        output << indent << printLabels( (*i)->get_labels() )  ;
[7f5566b]609                        (*i)->accept( *this );
[6c4ff37]610                        output << endl;
[51587aa]611                }
[6c4ff37]612                cur_indent -= CodeGenerator::tabsize;
[51587aa]613        }
614
[6c4ff37]615        void CodeGenerator::visit( BranchStmt *branchStmt ) {
[51587aa]616                switch ( branchStmt->get_type()) {
617                  case BranchStmt::Goto:
618                        if ( ! branchStmt->get_target().empty() )
[6c4ff37]619                                output << "goto " << branchStmt->get_target();
[51587aa]620                        else { 
621                                if ( branchStmt->get_computedTarget() != 0 ) {
[6c4ff37]622                                        output << "goto *";
[51587aa]623                                        branchStmt->get_computedTarget()->accept( *this );
624                                } // if
625                        } // if
626                        break;
627                  case BranchStmt::Break:
[6c4ff37]628                        output << "break";
[51587aa]629                        break;
630                  case BranchStmt::Continue:
[6c4ff37]631                        output << "continue";
[51587aa]632                        break;
633                }
[2b6c1e0]634                output << ";";
[51587aa]635        }
636
637
[6c4ff37]638        void CodeGenerator::visit( ReturnStmt *returnStmt ) {
639                output << "return ";
[51587aa]640
641                // xxx -- check for null expression;
642                if ( returnStmt->get_expr() ) {
643                        returnStmt->get_expr()->accept( *this );
644                } // if
[6c4ff37]645                output << ";";
[51587aa]646        }
647
[6c4ff37]648        void CodeGenerator::visit( WhileStmt *whileStmt ) {
[51587aa]649                if ( whileStmt->get_isDoWhile() )
[6c4ff37]650                        output << "do" ;
[51587aa]651                else {
[6c4ff37]652                        output << "while (" ;
[7f5566b]653                        whileStmt->get_condition()->accept( *this );
[6c4ff37]654                        output << ")";
[51587aa]655                } // if
[2b6c1e0]656                output << " ";
[51587aa]657
[2b6c1e0]658                output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
[51587aa]659                whileStmt->get_body()->accept( *this );
660
[cda48b6]661                output << indent;
[51587aa]662
663                if ( whileStmt->get_isDoWhile() ) {
[6c4ff37]664                        output << " while (" ;
[7f5566b]665                        whileStmt->get_condition()->accept( *this );
[6c4ff37]666                        output << ");";
[51587aa]667                } // if
668        }
669
[6c4ff37]670        void CodeGenerator::visit( ForStmt *forStmt ) {
[145f1fc]671                // initialization is always hoisted, so don't
672                // bother doing anything with that
673                output << "for (;";
[51587aa]674
675                if ( forStmt->get_condition() != 0 )
676                        forStmt->get_condition()->accept( *this );
[6c4ff37]677                output << ";";
[51587aa]678
679                if ( forStmt->get_increment() != 0 )
680                        forStmt->get_increment()->accept( *this );
[2b6c1e0]681                output << ") ";
[51587aa]682
683                if ( forStmt->get_body() != 0 ) {
[2b6c1e0]684                        output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
[51587aa]685                        forStmt->get_body()->accept( *this );
686                } // if
687        }
688
[6c4ff37]689        void CodeGenerator::visit( NullStmt *nullStmt ) {
[cda48b6]690                //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
[6c4ff37]691                output << "/* null statement */ ;";
[51587aa]692        }
693
[6c4ff37]694        void CodeGenerator::visit( DeclStmt *declStmt ) {
[51587aa]695                declStmt->get_decl()->accept( *this );
696       
697                if ( doSemicolon( declStmt->get_decl() ) ) {
[6c4ff37]698                        output << ";";
[51587aa]699                } // if
700        }
701
[6c4ff37]702        std::string CodeGenerator::printLabels( std::list< Label > &l ) {
[51587aa]703                std::string str( "" );
[6c4ff37]704                l.unique(); // assumes a sorted list. Why not use set?
[51587aa]705
706                for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ )
707                        str += *i + ": ";
708
709                return str;
710        }
711
[6c4ff37]712        void CodeGenerator::handleStorageClass( Declaration *decl ) {
[51587aa]713                switch ( decl->get_storageClass() ) {
[68cd1ce]714                  case DeclarationNode::Extern:
[6c4ff37]715                        output << "extern ";
[51587aa]716                        break;
[68cd1ce]717                  case DeclarationNode::Static:
[6c4ff37]718                        output << "static ";
[51587aa]719                        break;
[68cd1ce]720                  case DeclarationNode::Auto:
[51587aa]721                        // silently drop storage class
722                        break;
[68cd1ce]723                  case DeclarationNode::Register:
[6c4ff37]724                        output << "register ";
[51587aa]725                        break;
[68cd1ce]726                  case DeclarationNode::Inline:
[de62360d]727                        output << "inline ";
[f38c8d9]728                        break;
[68cd1ce]729                  case DeclarationNode::Fortran:
730                        output << "fortran ";
731                        break;
732                  case DeclarationNode::Noreturn:
733                        output << "_Noreturn ";
734                        break;
735                  case DeclarationNode::Threadlocal:
736                        output << "_Thread_local ";
737                        break;
738                  case DeclarationNode::NoStorageClass:
[f38c8d9]739                        break;
[843054c2]740                } // switch
[51587aa]741        }
[51b7345]742} // namespace CodeGen
[51587aa]743
744// Local Variables: //
745// tab-width: 4 //
746// mode: c++ //
747// compile-command: "make install" //
748// End: //
Note: See TracBrowser for help on using the repository browser.