source: src/CodeGen/CodeGenerator.cc @ a9a259c

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

autogenerate union ctor/dtors, autogenerate struct copy ctor, temporarily allow explicit calls to autogenerated ctors, add copy ctor to type constraints

  • Property mode set to 100644
File size: 21.4 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 Feb 09 13:24:40 2016
13// Update Count     : 255
14//
15
16#include <algorithm>
17#include <iostream>
18#include <cassert>
19#include <list>
20
21#include "Parser/ParseNode.h"
22
23#include "SynTree/Type.h"
24#include "SynTree/Expression.h"
25#include "SynTree/Initializer.h"
26#include "SynTree/Statement.h"
27
28#include "Common/utility.h"
29#include "Common/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                                  // it's just an optimization to disallow this, so for now let it through
266                                  // since it makes autogenerating constructors a lot easier
267                                varExpr->accept( *this );
268                                        output << "(";
269                                        genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
270                                        output << ")";
271
272                                  // intrinsic constructors should never be called directly - they should be transformed back into Initializer nodes
273                                  // assert(false);
274                                  break;
275
276                                  case OT_DTOR:
277                                  // intrinsic destructors do nothing - don't generate any code
278                                  output << " // " << dynamic_cast<VariableExpr*>(applicationExpr->get_function())->get_var()->get_name() << endl;
279                                  break;
280
281                                  case OT_PREFIX:
282                                  case OT_PREFIXASSIGN:
283                                        assert( applicationExpr->get_args().size() == 1 );
284                                        output << "(";
285                                        output << opInfo.symbol;
286                                        (*arg)->accept( *this );
287                                        output << ")";
288                                        break;
289
290                                  case OT_POSTFIX:
291                                  case OT_POSTFIXASSIGN:
292                                        assert( applicationExpr->get_args().size() == 1 );
293                                        (*arg)->accept( *this );
294                                        output << opInfo.symbol;
295                                        break;
296
297
298                                  case OT_INFIX:
299                                  case OT_INFIXASSIGN:
300                                        assert( applicationExpr->get_args().size() == 2 );
301                                        output << "(";
302                                        (*arg++)->accept( *this );
303                                        output << opInfo.symbol;
304                                        (*arg)->accept( *this );
305                                        output << ")";
306                                        break;
307
308                                  case OT_CONSTANT:
309                                  case OT_LABELADDRESS:
310                                        // there are no intrinsic definitions of 0/1 or label addresses as functions
311                                        assert( false );
312                                }
313                        } else {
314                                varExpr->accept( *this );
315                                output << "(";
316                                genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
317                                output << ")";
318                        } // if
319                } else {
320                        applicationExpr->get_function()->accept( *this );
321                        output << "(";
322                        genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
323                        output << ")";
324                } // if
325        }
326
327        void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
328                if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
329                        OperatorInfo opInfo;
330                        if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
331                                std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
332                                switch ( opInfo.type ) {
333                                  case OT_INDEX:
334                                        assert( untypedExpr->get_args().size() == 2 );
335                                        (*arg++)->accept( *this );
336                                        output << "[";
337                                        (*arg)->accept( *this );
338                                        output << "]";
339                                        break;
340
341                                  case OT_CALL:
342                                        assert( false );
343
344                                        case OT_CTOR:
345                                        case OT_DTOR:
346                                        // intrinsic constructors should never be called
347                                        // intrinsic destructors do nothing
348                                        break;
349
350                                  case OT_PREFIX:
351                                  case OT_PREFIXASSIGN:
352                                  case OT_LABELADDRESS:
353                                        assert( untypedExpr->get_args().size() == 1 );
354                                        output << "(";
355                                        output << opInfo.symbol;
356                                        (*arg)->accept( *this );
357                                        output << ")";
358                                        break;
359
360                                  case OT_POSTFIX:
361                                  case OT_POSTFIXASSIGN:
362                                        assert( untypedExpr->get_args().size() == 1 );
363                                        (*arg)->accept( *this );
364                                        output << opInfo.symbol;
365                                        break;
366
367                                  case OT_INFIX:
368                                  case OT_INFIXASSIGN:
369                                        assert( untypedExpr->get_args().size() == 2 );
370                                        output << "(";
371                                        (*arg++)->accept( *this );
372                                        output << opInfo.symbol;
373                                        (*arg)->accept( *this );
374                                        output << ")";
375                                        break;
376
377                                  case OT_CONSTANT:
378                                        // there are no intrinsic definitions of 0 or 1 as functions
379                                        assert( false );
380                                }
381                        } else {
382                                nameExpr->accept( *this );
383                                output << "(";
384                                genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
385                                output << ")";
386                        } // if
387                } else {
388                        untypedExpr->get_function()->accept( *this );
389                        output << "(";
390                        genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
391                        output << ")";
392                } // if
393        }
394
395        void CodeGenerator::visit( NameExpr *nameExpr ) {
396                OperatorInfo opInfo;
397                if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
398                        assert( opInfo.type == OT_CONSTANT );
399                        output << opInfo.symbol;
400                } else {
401                        output << nameExpr->get_name();
402                } // if
403        }
404
405        void CodeGenerator::visit( AddressExpr *addressExpr ) {
406                output << "(&";
407                // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
408                if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
409                        output << mangleName( variableExpr->get_var() );
410                } else {
411                        addressExpr->get_arg()->accept( *this );
412                } // if
413                output << ")";
414        }
415
416        void CodeGenerator::visit( CastExpr *castExpr ) {
417                output << "(";
418                if ( castExpr->get_results().empty() ) {
419                        output << "(void)" ;
420                } else if ( ! castExpr->get_results().front()->get_isLvalue() ) {
421                        // at least one result type of cast, but not an lvalue
422                        output << "(";
423                        output << genType( castExpr->get_results().front(), "" );
424                        output << ")";
425                } else {
426                        // otherwise, the cast is to an lvalue type, so the cast
427                        // should be dropped, since the result of a cast is
428                        // never an lvalue in C
429                }
430                castExpr->get_arg()->accept( *this );
431                output << ")";
432        }
433
434        void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
435                assert( false );
436        }
437
438        void CodeGenerator::visit( MemberExpr *memberExpr ) {
439                memberExpr->get_aggregate()->accept( *this );
440                output << "." << mangleName( memberExpr->get_member() );
441        }
442
443        void CodeGenerator::visit( VariableExpr *variableExpr ) {
444                OperatorInfo opInfo;
445                if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
446                        output << opInfo.symbol;
447                } else {
448                        output << mangleName( variableExpr->get_var() );
449                } // if
450        }
451
452        void CodeGenerator::visit( ConstantExpr *constantExpr ) {
453                assert( constantExpr->get_constant() );
454                constantExpr->get_constant()->accept( *this );
455        }
456
457        void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
458                output << "sizeof(";
459                if ( sizeofExpr->get_isType() ) {
460                        output << genType( sizeofExpr->get_type(), "" );
461                } else {
462                        sizeofExpr->get_expr()->accept( *this );
463                } // if
464                output << ")";
465        }
466
467        void CodeGenerator::visit( AlignofExpr *alignofExpr ) {
468                // use GCC extension to avoid bumping std to C11
469                output << "__alignof__(";
470                if ( alignofExpr->get_isType() ) {
471                        output << genType( alignofExpr->get_type(), "" );
472                } else {
473                        alignofExpr->get_expr()->accept( *this );
474                } // if
475                output << ")";
476        }
477
478        void CodeGenerator::visit( UntypedOffsetofExpr *offsetofExpr ) {
479                assert( false );
480        }
481
482        void CodeGenerator::visit( OffsetofExpr *offsetofExpr ) {
483                // use GCC builtin
484                output << "__builtin_offsetof(";
485                output << genType( offsetofExpr->get_type(), "" );
486                output << ", " << mangleName( offsetofExpr->get_member() );
487                output << ")";
488        }
489
490        void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
491                output << "(";
492                logicalExpr->get_arg1()->accept( *this );
493                if ( logicalExpr->get_isAnd() ) {
494                        output << " && ";
495                } else {
496                        output << " || ";
497                } // if
498                logicalExpr->get_arg2()->accept( *this );
499                output << ")";
500        }
501
502        void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
503                output << "(";
504                conditionalExpr->get_arg1()->accept( *this );
505                output << " ? ";
506                conditionalExpr->get_arg2()->accept( *this );
507                output << " : ";
508                conditionalExpr->get_arg3()->accept( *this );
509                output << ")";
510        }
511
512        void CodeGenerator::visit( CommaExpr *commaExpr ) {
513                output << "(";
514                commaExpr->get_arg1()->accept( *this );
515                output << " , ";
516                commaExpr->get_arg2()->accept( *this );
517                output << ")";
518        }
519
520        void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
521
522        void CodeGenerator::visit( TypeExpr *typeExpr ) {}
523
524        void CodeGenerator::visit( AsmExpr *asmExpr ) {
525                if ( asmExpr->get_inout() ) {
526                        output << "[ ";
527                        asmExpr->get_inout()->accept( *this );
528                        output << " ] ";
529                } // if
530                asmExpr->get_constraint()->accept( *this );
531                output << " ( ";
532                asmExpr->get_operand()->accept( *this );
533                output << " )";
534        }
535
536        //*** Statements
537        void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
538                std::list<Statement*> ks = compoundStmt->get_kids();
539                output << "{" << endl;
540
541                cur_indent += CodeGenerator::tabsize;
542
543                for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++ ) {
544                        output << indent << printLabels( (*i)->get_labels() );
545                        (*i)->accept( *this );
546
547                        output << endl;
548                        if ( wantSpacing( *i ) ) {
549                                output << endl;
550                        }
551                }
552                cur_indent -= CodeGenerator::tabsize;
553
554                output << indent << "}";
555        }
556
557        void CodeGenerator::visit( ExprStmt *exprStmt ) {
558                // I don't see why this check is necessary.
559                // If this starts to cause problems then put it back in,
560                // with an explanation
561                assert( exprStmt );
562
563                // if ( exprStmt != 0 ) {
564                exprStmt->get_expr()->accept( *this );
565                output << ";" ;
566                // } // if
567        }
568
569        void CodeGenerator::visit( AsmStmt *asmStmt ) {
570                output << "asm ";
571                if ( asmStmt->get_voltile() ) output << "volatile ";
572                if ( ! asmStmt->get_gotolabels().empty()  ) output << "goto ";
573                output << "( ";
574                if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
575                output << " : ";
576                genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
577                output << " : ";
578                genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
579                output << " : ";
580                genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
581                if ( ! asmStmt->get_gotolabels().empty() ) {
582                        output << " : ";
583                        for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
584                                output << *begin++;
585                                if ( begin == asmStmt->get_gotolabels().end() ) break;
586                                output << ", ";
587                        } // for
588                } // if
589                output << " );" ;
590        }
591
592        void CodeGenerator::visit( IfStmt *ifStmt ) {
593                output << "if ( ";
594                ifStmt->get_condition()->accept( *this );
595                output << " ) ";
596
597                ifStmt->get_thenPart()->accept( *this );
598
599                if ( ifStmt->get_elsePart() != 0) {
600                        output << " else ";
601                        ifStmt->get_elsePart()->accept( *this );
602                } // if
603        }
604
605        void CodeGenerator::visit( SwitchStmt *switchStmt ) {
606                output << "switch ( " ;
607                switchStmt->get_condition()->accept( *this );
608                output << " ) ";
609
610                output << "{" << std::endl;
611                cur_indent += CodeGenerator::tabsize;
612
613                acceptAll( switchStmt->get_branches(), *this );
614
615                cur_indent -= CodeGenerator::tabsize;
616
617                output << indent << "}";
618        }
619
620        void CodeGenerator::visit( CaseStmt *caseStmt ) {
621                output << indent;
622                if ( caseStmt->isDefault()) {
623                        output << "default";
624                } else {
625                        output << "case ";
626                        caseStmt->get_condition()->accept( *this );
627                } // if
628                output << ":\n";
629
630                std::list<Statement *> sts = caseStmt->get_statements();
631
632                cur_indent += CodeGenerator::tabsize;
633                for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end();  i++) {
634                        output << indent << printLabels( (*i)->get_labels() )  ;
635                        (*i)->accept( *this );
636                        output << endl;
637                }
638                cur_indent -= CodeGenerator::tabsize;
639        }
640
641        void CodeGenerator::visit( BranchStmt *branchStmt ) {
642                switch ( branchStmt->get_type()) {
643                  case BranchStmt::Goto:
644                        if ( ! branchStmt->get_target().empty() )
645                                output << "goto " << branchStmt->get_target();
646                        else {
647                                if ( branchStmt->get_computedTarget() != 0 ) {
648                                        output << "goto *";
649                                        branchStmt->get_computedTarget()->accept( *this );
650                                } // if
651                        } // if
652                        break;
653                  case BranchStmt::Break:
654                        output << "break";
655                        break;
656                  case BranchStmt::Continue:
657                        output << "continue";
658                        break;
659                }
660                output << ";";
661        }
662
663
664        void CodeGenerator::visit( ReturnStmt *returnStmt ) {
665                output << "return ";
666
667                // xxx -- check for null expression;
668                if ( returnStmt->get_expr() ) {
669                        returnStmt->get_expr()->accept( *this );
670                } // if
671                output << ";";
672        }
673
674        void CodeGenerator::visit( WhileStmt *whileStmt ) {
675                if ( whileStmt->get_isDoWhile() )
676                        output << "do" ;
677                else {
678                        output << "while (" ;
679                        whileStmt->get_condition()->accept( *this );
680                        output << ")";
681                } // if
682                output << " ";
683
684                output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
685                whileStmt->get_body()->accept( *this );
686
687                output << indent;
688
689                if ( whileStmt->get_isDoWhile() ) {
690                        output << " while (" ;
691                        whileStmt->get_condition()->accept( *this );
692                        output << ");";
693                } // if
694        }
695
696        void CodeGenerator::visit( ForStmt *forStmt ) {
697                // initialization is always hoisted, so don't
698                // bother doing anything with that
699                output << "for (;";
700
701                if ( forStmt->get_condition() != 0 )
702                        forStmt->get_condition()->accept( *this );
703                output << ";";
704
705                if ( forStmt->get_increment() != 0 )
706                        forStmt->get_increment()->accept( *this );
707                output << ") ";
708
709                if ( forStmt->get_body() != 0 ) {
710                        output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
711                        forStmt->get_body()->accept( *this );
712                } // if
713        }
714
715        void CodeGenerator::visit( NullStmt *nullStmt ) {
716                //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
717                output << "/* null statement */ ;";
718        }
719
720        void CodeGenerator::visit( DeclStmt *declStmt ) {
721                declStmt->get_decl()->accept( *this );
722
723                if ( doSemicolon( declStmt->get_decl() ) ) {
724                        output << ";";
725                } // if
726        }
727
728        std::string CodeGenerator::printLabels( std::list< Label > &l ) {
729                std::string str( "" );
730                l.unique(); // assumes a sorted list. Why not use set?
731
732                for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ )
733                        str += *i + ": ";
734
735                return str;
736        }
737
738        void CodeGenerator::handleStorageClass( Declaration *decl ) {
739                switch ( decl->get_storageClass() ) {
740                  case DeclarationNode::Extern:
741                        output << "extern ";
742                        break;
743                  case DeclarationNode::Static:
744                        output << "static ";
745                        break;
746                  case DeclarationNode::Auto:
747                        // silently drop storage class
748                        break;
749                  case DeclarationNode::Register:
750                        output << "register ";
751                        break;
752                  case DeclarationNode::Inline:
753                        output << "inline ";
754                        break;
755                  case DeclarationNode::Fortran:
756                        output << "fortran ";
757                        break;
758                  case DeclarationNode::Noreturn:
759                        output << "_Noreturn ";
760                        break;
761                  case DeclarationNode::Threadlocal:
762                        output << "_Thread_local ";
763                        break;
764                  case DeclarationNode::NoStorageClass:
765                        break;
766                } // switch
767        }
768} // namespace CodeGen
769
770// Local Variables: //
771// tab-width: 4 //
772// mode: c++ //
773// compile-command: "make install" //
774// End: //
Note: See TracBrowser for help on using the repository browser.