source: src/CodeGen/CodeGenerator.cc @ 37024fd

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 37024fd was 37024fd, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

cleanup global init code, don't need to disambiguate with libcfa prefix

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