source: src/CodeGen/CodeGenerator.cc @ cb2e8ce

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

add more code to handle gcc extension and test program, second attempt

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