source: src/CodeGen/CodeGenerator.cc @ aa8f9df

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

Merge branch 'replace-results-list' into tuples

Conflicts:

src/ResolvExpr/AlternativeFinder.cc
src/SymTab/Indexer.cc
src/SynTree/Mutator.cc
src/SynTree/Visitor.cc
src/Tuples/TupleAssignment.cc
src/Tuples/TupleAssignment.h

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