source: src/CodeGen/CodeGenerator.cc @ 262f085f

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

add version of CodeGen::generate for a single node for use in gdb

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