source: src/CodeGen/CodeGenerator.cc @ 3874269

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 3874269 was 066d77a, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

documentation for an assertion

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