source: src/CodeGen/CodeGenerator.cc @ e8032b0

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

Switch Indexer over to copy-on-write semantics for dramatic speedup

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