source: src/CodeGen/CodeGenerator.cc @ fa2de95

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

first attempt at gcc attributes

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