source: src/CodeGen/CodeGenerator.cc @ d5f1cfc

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since d5f1cfc was 9e2c1f0, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Merge branch 'global-init' into ctor and add global destroy function to call destructors on global objects

Conflicts:

src/CodeGen/CodeGenerator.cc
src/InitTweak/module.mk
src/Makefile.in
src/SynTree/Declaration.h
src/SynTree/FunctionDecl.cc
src/main.cc

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