source: src/CodeGen/CodeGenerator.cc @ ca1c11f

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

added intrinsic ctor/dtors to prelude, modified MakeLibCfa? to build prelude ctor/dtors, added ctor/dtor to polymorphic object type constraints, rudimentary fallback on initializer nodes if chosen ctor is intrinsic, remove intrinsic destructor statements to reduce output pollution

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