source: src/CodeGen/CodeGenerator.cc @ 0a81c3f

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 0a81c3f was 0a81c3f, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Remove several missing-reference related hacks

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