source: src/CodeGen/CodeGenerator.cc @ 4d2434a

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 4d2434a was 4d2434a, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

major reorganization of constructor generation from initializer list so that it now works with multi-dimensional arrays

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