source: src/CodeGen/CodeGenerator.cc @ e04ef3a

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 e04ef3a was e04ef3a, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

add gcc extension, first attempt, not done yet

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