source: src/CodeGen/CodeGenerator.cc @ 9d944b2

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 9d944b2 was d9c8a59, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Now generating names for anonymous unions properly

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