source: src/CodeGen/CodeGenerator.cc @ a5a71d0

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since a5a71d0 was a5a71d0, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

Merge branch 'fix-memory-error' into ctor

Conflicts:

src/CodeGen/CodeGenerator.cc
src/Makefile.in
src/Parser/DeclarationNode.cc
src/Parser/ParseNode.h
src/Parser/TypeData.cc
src/Parser/parser.cc
src/Parser/parser.yy
src/ResolvExpr/Resolver.cc
src/SymTab/Validate.cc
src/SynTree/Declaration.h
src/SynTree/Mutator.cc
src/SynTree/Mutator.h
src/SynTree/SynTree.h
src/SynTree/Visitor.cc
src/SynTree/Visitor.h
src/libcfa/prelude.cf

  • Property mode set to 100644
File size: 21.6 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 : Rob Schluntz
12// Last Modified On : Mon Apr 04 17:08:06 2016
13// Update Count     : 255
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
29#include "Common/utility.h"
30#include "Common/UnimplementedError.h"
31
32#include "CodeGenerator.h"
33#include "OperatorTable.h"
34#include "GenType.h"
35
36using namespace std;
37
38namespace CodeGen {
39        int CodeGenerator::tabsize = 4;
40
41        // the kinds of statements that would ideally be followed by whitespace
42        bool wantSpacing( Statement * stmt) {
43                return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) ||
44                        dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * > ( stmt ) || dynamic_cast< SwitchStmt *>( stmt );
45        }
46
47        ostream & CodeGenerator::Indenter::operator()( ostream & output ) {
48          return output << string( cg.cur_indent, ' ' );
49        }
50
51        ostream & operator<<( ostream & output, CodeGenerator::Indenter &indent ) {
52                return indent( output );
53        }
54
55        CodeGenerator::CodeGenerator( std::ostream &os ) : indent( *this), cur_indent( 0 ), insideFunction( false ), output( os ) { }
56
57        CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indentation, bool infunp )
58                        : indent( *this), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
59                //output << std::string( init );
60        }
61
62        CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indentation, bool infunp )
63                        : indent( *this ), cur_indent( indentation ), insideFunction( infunp ), output( os ) {
64                //output << std::string( init );
65        }
66
67        string mangleName( DeclarationWithType *decl ) {
68                if ( decl->get_mangleName() != "" ) {
69                        return decl->get_mangleName();
70                } else {
71                        return decl->get_name();
72                } // if
73        }
74
75        //*** Declarations
76        void CodeGenerator::visit( FunctionDecl *functionDecl ) {
77                handleStorageClass( functionDecl );
78                if ( functionDecl->get_isInline() ) {
79                        output << "inline ";
80                } // if
81                if ( functionDecl->get_isNoreturn() ) {
82                        output << "_Noreturn ";
83                } // if
84                output << genType( functionDecl->get_functionType(), mangleName( functionDecl ) );
85
86                // how to get this to the Functype?
87                std::list< Declaration * > olds = functionDecl->get_oldDecls();
88                if ( ! olds.empty() ) {
89                        output << " /* function has old declaration */";
90                } // if
91
92                // acceptAll( functionDecl->get_oldDecls(), *this );
93                if ( functionDecl->get_statements() ) {
94                        functionDecl->get_statements()->accept( *this );
95                } // if
96        }
97
98        void CodeGenerator::visit( ObjectDecl *objectDecl ) {
99                handleStorageClass( objectDecl );
100                output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
101
102                if ( objectDecl->get_init() ) {
103                        output << " = ";
104                        objectDecl->get_init()->accept( *this );
105                } // if
106                if ( objectDecl->get_bitfieldWidth() ) {
107                        output << ":";
108                        objectDecl->get_bitfieldWidth()->accept( *this );
109                } // if
110        }
111
112        void CodeGenerator::handleAggregate( AggregateDecl *aggDecl ) {
113                if ( aggDecl->get_name() != "" )
114                        output << aggDecl->get_name();
115
116                std::list< Declaration * > &memb = aggDecl->get_members();
117
118                if ( ! memb.empty() ) {
119                        output << " {" << endl;
120
121                        cur_indent += CodeGenerator::tabsize;
122                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
123                                output << indent;
124                                (*i)->accept( *this );
125                                output << ";" << endl;
126                        }
127
128                        cur_indent -= CodeGenerator::tabsize;
129
130                        output << indent << "}";
131                } // if
132        }
133
134        void CodeGenerator::visit( StructDecl *structDecl ) {
135                output << "struct ";
136                handleAggregate( structDecl );
137        }
138
139        void CodeGenerator::visit( UnionDecl *aggregateDecl ) {
140                output << "union ";
141                handleAggregate( aggregateDecl );
142        }
143
144        void CodeGenerator::visit( EnumDecl *aggDecl ) {
145                output << "enum ";
146
147                if ( aggDecl->get_name() != "" )
148                        output << aggDecl->get_name();
149
150                std::list< Declaration* > &memb = aggDecl->get_members();
151
152                if ( ! memb.empty() ) {
153                        output << " {" << endl;
154
155                        cur_indent += CodeGenerator::tabsize;
156                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
157                                ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
158                                assert( obj );
159                                output << indent << mangleName( obj );
160                                if ( obj->get_init() ) {
161                                        output << " = ";
162                                        obj->get_init()->accept( *this );
163                                } // if
164                                output << "," << endl;
165                        } // for
166
167                        cur_indent -= CodeGenerator::tabsize;
168
169                        output << indent << "}";
170                } // if
171        }
172
173        void CodeGenerator::visit( TraitDecl *aggregateDecl ) {}
174
175        void CodeGenerator::visit( TypedefDecl *typeDecl ) {
176                output << "typedef ";
177                output << genType( typeDecl->get_base(), typeDecl->get_name() );
178        }
179
180        void CodeGenerator::visit( TypeDecl *typeDecl ) {
181                // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
182                // still to be done
183                output << "extern unsigned long " << typeDecl->get_name();
184                if ( typeDecl->get_base() ) {
185                        output << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )";
186                } // if
187        }
188
189        void CodeGenerator::printDesignators( std::list< Expression * > & designators ) {
190                typedef std::list< Expression * > DesignatorList;
191                if ( designators.size() == 0 ) return;
192                for ( DesignatorList::iterator iter = designators.begin(); iter != designators.end(); ++iter ) {
193                        if ( dynamic_cast< NameExpr * >( *iter ) ) {
194                                // if expression is a name, then initializing aggregate member
195                                output << ".";
196                                (*iter)->accept( *this );
197                        } else {
198                                // if not a simple name, it has to be a constant expression, i.e. an array designator
199                                output << "[";
200                                (*iter)->accept( *this );
201                                output << "]";
202                        }
203                }
204                output << " = ";
205        }
206
207        void CodeGenerator::visit( SingleInit *init ) {
208                printDesignators( init->get_designators() );
209                init->get_value()->accept( *this );
210        }
211
212        void CodeGenerator::visit( ListInit *init ) {
213                printDesignators( init->get_designators() );
214                output << "{ ";
215                if ( init->begin_initializers() == init->end_initializers() ) {
216                        // illegal to leave initializer list empty for scalar initializers,
217                        // but always legal to have 0
218                        output << "0";
219                } else {
220                        genCommaList( init->begin_initializers(), init->end_initializers() );
221                }
222                output << " }";
223        }
224
225        void CodeGenerator::visit( Constant *constant ) {
226                output << constant->get_value() ;
227        }
228
229        //*** Expressions
230        void CodeGenerator::visit( ApplicationExpr *applicationExpr ) {
231                if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
232                        OperatorInfo opInfo;
233                        if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
234                                std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
235                                switch ( opInfo.type ) {
236                                  case OT_PREFIXASSIGN:
237                                  case OT_POSTFIXASSIGN:
238                                  case OT_INFIXASSIGN:
239                                        {
240                                                assert( arg != applicationExpr->get_args().end() );
241                                                if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
242
243                                                        *arg = addrExpr->get_arg();
244                                                } else {
245                                                        UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
246                                                        newExpr->get_args().push_back( *arg );
247                                                        *arg = newExpr;
248                                                } // if
249                                                break;
250                                        }
251
252                                  default:
253                                        // do nothing
254                                        ;
255                                }
256
257                                switch ( opInfo.type ) {
258                                  case OT_INDEX:
259                                        assert( applicationExpr->get_args().size() == 2 );
260                                        (*arg++)->accept( *this );
261                                        output << "[";
262                                        (*arg)->accept( *this );
263                                        output << "]";
264                                        break;
265
266                                  case OT_CALL:
267                                        // there are no intrinsic definitions of the function call operator or constructors or destructors
268                                        assert( false );
269                                        break;
270
271                                  case OT_CTOR:
272                                  // it's just an optimization to disallow this, so for now let it through
273                                  // since it makes autogenerating constructors a lot easier
274                                varExpr->accept( *this );
275                                        output << "(";
276                                        genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
277                                        output << ")";
278
279                                  // intrinsic constructors should never be called directly - they should be transformed back into Initializer nodes
280                                  // assert(false);
281                                  break;
282
283                                  case OT_DTOR:
284                                  // intrinsic destructors do nothing - don't generate any code
285                                  output << " /* " << dynamic_cast<VariableExpr*>(applicationExpr->get_function())->get_var()->get_name() << " */";
286                                  break;
287
288                                  case OT_PREFIX:
289                                  case OT_PREFIXASSIGN:
290                                        assert( applicationExpr->get_args().size() == 1 );
291                                        output << "(";
292                                        output << opInfo.symbol;
293                                        (*arg)->accept( *this );
294                                        output << ")";
295                                        break;
296
297                                  case OT_POSTFIX:
298                                  case OT_POSTFIXASSIGN:
299                                        assert( applicationExpr->get_args().size() == 1 );
300                                        (*arg)->accept( *this );
301                                        output << opInfo.symbol;
302                                        break;
303
304
305                                  case OT_INFIX:
306                                  case OT_INFIXASSIGN:
307                                        assert( applicationExpr->get_args().size() == 2 );
308                                        output << "(";
309                                        (*arg++)->accept( *this );
310                                        output << opInfo.symbol;
311                                        (*arg)->accept( *this );
312                                        output << ")";
313                                        break;
314
315                                  case OT_CONSTANT:
316                                  case OT_LABELADDRESS:
317                                        // there are no intrinsic definitions of 0/1 or label addresses as functions
318                                        assert( false );
319                                }
320                        } else {
321                                varExpr->accept( *this );
322                                output << "(";
323                                genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
324                                output << ")";
325                        } // if
326                } else {
327                        applicationExpr->get_function()->accept( *this );
328                        output << "(";
329                        genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
330                        output << ")";
331                } // if
332        }
333
334        void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
335                if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
336                        OperatorInfo opInfo;
337                        if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
338                                std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
339                                switch ( opInfo.type ) {
340                                  case OT_INDEX:
341                                        assert( untypedExpr->get_args().size() == 2 );
342                                        (*arg++)->accept( *this );
343                                        output << "[";
344                                        (*arg)->accept( *this );
345                                        output << "]";
346                                        break;
347
348                                  case OT_CALL:
349                                        assert( false );
350
351                                        case OT_CTOR:
352                                        case OT_DTOR:
353                                        // intrinsic constructors should never be called
354                                        // intrinsic destructors do nothing
355                                        break;
356
357                                  case OT_PREFIX:
358                                  case OT_PREFIXASSIGN:
359                                  case OT_LABELADDRESS:
360                                        assert( untypedExpr->get_args().size() == 1 );
361                                        output << "(";
362                                        output << opInfo.symbol;
363                                        (*arg)->accept( *this );
364                                        output << ")";
365                                        break;
366
367                                  case OT_POSTFIX:
368                                  case OT_POSTFIXASSIGN:
369                                        assert( untypedExpr->get_args().size() == 1 );
370                                        (*arg)->accept( *this );
371                                        output << opInfo.symbol;
372                                        break;
373
374                                  case OT_INFIX:
375                                  case OT_INFIXASSIGN:
376                                        assert( untypedExpr->get_args().size() == 2 );
377                                        output << "(";
378                                        (*arg++)->accept( *this );
379                                        output << opInfo.symbol;
380                                        (*arg)->accept( *this );
381                                        output << ")";
382                                        break;
383
384                                  case OT_CONSTANT:
385                                        // there are no intrinsic definitions of 0 or 1 as functions
386                                        assert( false );
387                                }
388                        } else {
389                                nameExpr->accept( *this );
390                                output << "(";
391                                genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
392                                output << ")";
393                        } // if
394                } else {
395                        untypedExpr->get_function()->accept( *this );
396                        output << "(";
397                        genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
398                        output << ")";
399                } // if
400        }
401
402        void CodeGenerator::visit( NameExpr *nameExpr ) {
403                OperatorInfo opInfo;
404                if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
405                        assert( opInfo.type == OT_CONSTANT );
406                        output << opInfo.symbol;
407                } else {
408                        output << nameExpr->get_name();
409                } // if
410        }
411
412        void CodeGenerator::visit( AddressExpr *addressExpr ) {
413                output << "(&";
414                // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
415                if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
416                        output << mangleName( variableExpr->get_var() );
417                } else {
418                        addressExpr->get_arg()->accept( *this );
419                } // if
420                output << ")";
421        }
422
423        void CodeGenerator::visit( CastExpr *castExpr ) {
424                output << "(";
425                if ( castExpr->get_results().empty() ) {
426                        output << "(void)" ;
427                } else if ( ! castExpr->get_results().front()->get_isLvalue() ) {
428                        // at least one result type of cast, but not an lvalue
429                        output << "(";
430                        output << genType( castExpr->get_results().front(), "" );
431                        output << ")";
432                } else {
433                        // otherwise, the cast is to an lvalue type, so the cast
434                        // should be dropped, since the result of a cast is
435                        // never an lvalue in C
436                }
437                castExpr->get_arg()->accept( *this );
438                output << ")";
439        }
440
441        void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
442                assert( false );
443        }
444
445        void CodeGenerator::visit( MemberExpr *memberExpr ) {
446                memberExpr->get_aggregate()->accept( *this );
447                output << "." << mangleName( memberExpr->get_member() );
448        }
449
450        void CodeGenerator::visit( VariableExpr *variableExpr ) {
451                OperatorInfo opInfo;
452                if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
453                        output << opInfo.symbol;
454                } else {
455                        output << mangleName( variableExpr->get_var() );
456                } // if
457        }
458
459        void CodeGenerator::visit( ConstantExpr *constantExpr ) {
460                assert( constantExpr->get_constant() );
461                constantExpr->get_constant()->accept( *this );
462        }
463
464        void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
465                output << "sizeof(";
466                if ( sizeofExpr->get_isType() ) {
467                        output << genType( sizeofExpr->get_type(), "" );
468                } else {
469                        sizeofExpr->get_expr()->accept( *this );
470                } // if
471                output << ")";
472        }
473
474        void CodeGenerator::visit( AlignofExpr *alignofExpr ) {
475                // use GCC extension to avoid bumping std to C11
476                output << "__alignof__(";
477                if ( alignofExpr->get_isType() ) {
478                        output << genType( alignofExpr->get_type(), "" );
479                } else {
480                        alignofExpr->get_expr()->accept( *this );
481                } // if
482                output << ")";
483        }
484
485        void CodeGenerator::visit( UntypedOffsetofExpr *offsetofExpr ) {
486                assert( false );
487        }
488
489        void CodeGenerator::visit( OffsetofExpr *offsetofExpr ) {
490                // use GCC builtin
491                output << "__builtin_offsetof(";
492                output << genType( offsetofExpr->get_type(), "" );
493                output << ", " << mangleName( offsetofExpr->get_member() );
494                output << ")";
495        }
496
497        void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
498                output << "(";
499                logicalExpr->get_arg1()->accept( *this );
500                if ( logicalExpr->get_isAnd() ) {
501                        output << " && ";
502                } else {
503                        output << " || ";
504                } // if
505                logicalExpr->get_arg2()->accept( *this );
506                output << ")";
507        }
508
509        void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
510                output << "(";
511                conditionalExpr->get_arg1()->accept( *this );
512                output << " ? ";
513                conditionalExpr->get_arg2()->accept( *this );
514                output << " : ";
515                conditionalExpr->get_arg3()->accept( *this );
516                output << ")";
517        }
518
519        void CodeGenerator::visit( CommaExpr *commaExpr ) {
520                output << "(";
521                commaExpr->get_arg1()->accept( *this );
522                output << " , ";
523                commaExpr->get_arg2()->accept( *this );
524                output << ")";
525        }
526
527        void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
528
529        void CodeGenerator::visit( TypeExpr *typeExpr ) {}
530
531        void CodeGenerator::visit( AsmExpr *asmExpr ) {
532                if ( asmExpr->get_inout() ) {
533                        output << "[ ";
534                        asmExpr->get_inout()->accept( *this );
535                        output << " ] ";
536                } // if
537                asmExpr->get_constraint()->accept( *this );
538                output << " ( ";
539                asmExpr->get_operand()->accept( *this );
540                output << " )";
541        }
542
543        //*** Statements
544        void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
545                std::list<Statement*> ks = compoundStmt->get_kids();
546                output << "{" << endl;
547
548                cur_indent += CodeGenerator::tabsize;
549
550                for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++ ) {
551                        output << indent << printLabels( (*i)->get_labels() );
552                        (*i)->accept( *this );
553
554                        output << endl;
555                        if ( wantSpacing( *i ) ) {
556                                output << endl;
557                        }
558                }
559                cur_indent -= CodeGenerator::tabsize;
560
561                output << indent << "}";
562        }
563
564        void CodeGenerator::visit( ExprStmt *exprStmt ) {
565                // I don't see why this check is necessary.
566                // If this starts to cause problems then put it back in,
567                // with an explanation
568                assert( exprStmt );
569
570                // if ( exprStmt != 0 ) {
571                exprStmt->get_expr()->accept( *this );
572                output << ";" ;
573                // } // if
574        }
575
576        void CodeGenerator::visit( AsmStmt *asmStmt ) {
577                output << "asm ";
578                if ( asmStmt->get_voltile() ) output << "volatile ";
579                if ( ! asmStmt->get_gotolabels().empty()  ) output << "goto ";
580                output << "( ";
581                if ( asmStmt->get_instruction() ) asmStmt->get_instruction()->accept( *this );
582                output << " : ";
583                genCommaList( asmStmt->get_output().begin(), asmStmt->get_output().end() );
584                output << " : ";
585                genCommaList( asmStmt->get_input().begin(), asmStmt->get_input().end() );
586                output << " : ";
587                genCommaList( asmStmt->get_clobber().begin(), asmStmt->get_clobber().end() );
588                if ( ! asmStmt->get_gotolabels().empty() ) {
589                        output << " : ";
590                        for ( std::list<Label>::iterator begin = asmStmt->get_gotolabels().begin();; ) {
591                                output << *begin++;
592                                if ( begin == asmStmt->get_gotolabels().end() ) break;
593                                output << ", ";
594                        } // for
595                } // if
596                output << " );" ;
597        }
598
599        void CodeGenerator::visit( IfStmt *ifStmt ) {
600                output << "if ( ";
601                ifStmt->get_condition()->accept( *this );
602                output << " ) ";
603
604                ifStmt->get_thenPart()->accept( *this );
605
606                if ( ifStmt->get_elsePart() != 0) {
607                        output << " else ";
608                        ifStmt->get_elsePart()->accept( *this );
609                } // if
610        }
611
612        void CodeGenerator::visit( SwitchStmt *switchStmt ) {
613                output << "switch ( " ;
614                switchStmt->get_condition()->accept( *this );
615                output << " ) ";
616
617                output << "{" << std::endl;
618                cur_indent += CodeGenerator::tabsize;
619
620                acceptAll( switchStmt->get_branches(), *this );
621
622                cur_indent -= CodeGenerator::tabsize;
623
624                output << indent << "}";
625        }
626
627        void CodeGenerator::visit( CaseStmt *caseStmt ) {
628                output << indent;
629                if ( caseStmt->isDefault()) {
630                        output << "default";
631                } else {
632                        output << "case ";
633                        caseStmt->get_condition()->accept( *this );
634                } // if
635                output << ":\n";
636
637                std::list<Statement *> sts = caseStmt->get_statements();
638
639                cur_indent += CodeGenerator::tabsize;
640                for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end();  i++) {
641                        output << indent << printLabels( (*i)->get_labels() )  ;
642                        (*i)->accept( *this );
643                        output << endl;
644                }
645                cur_indent -= CodeGenerator::tabsize;
646        }
647
648        void CodeGenerator::visit( BranchStmt *branchStmt ) {
649                switch ( branchStmt->get_type()) {
650                  case BranchStmt::Goto:
651                        if ( ! branchStmt->get_target().empty() )
652                                output << "goto " << branchStmt->get_target();
653                        else {
654                                if ( branchStmt->get_computedTarget() != 0 ) {
655                                        output << "goto *";
656                                        branchStmt->get_computedTarget()->accept( *this );
657                                } // if
658                        } // if
659                        break;
660                  case BranchStmt::Break:
661                        output << "break";
662                        break;
663                  case BranchStmt::Continue:
664                        output << "continue";
665                        break;
666                }
667                output << ";";
668        }
669
670
671        void CodeGenerator::visit( ReturnStmt *returnStmt ) {
672                output << "return ";
673
674                // xxx -- check for null expression;
675                if ( returnStmt->get_expr() ) {
676                        returnStmt->get_expr()->accept( *this );
677                } // if
678                output << ";";
679        }
680
681        void CodeGenerator::visit( WhileStmt *whileStmt ) {
682                if ( whileStmt->get_isDoWhile() )
683                        output << "do" ;
684                else {
685                        output << "while (" ;
686                        whileStmt->get_condition()->accept( *this );
687                        output << ")";
688                } // if
689                output << " ";
690
691                output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
692                whileStmt->get_body()->accept( *this );
693
694                output << indent;
695
696                if ( whileStmt->get_isDoWhile() ) {
697                        output << " while (" ;
698                        whileStmt->get_condition()->accept( *this );
699                        output << ");";
700                } // if
701        }
702
703        void CodeGenerator::visit( ForStmt *forStmt ) {
704                // initialization is always hoisted, so don't
705                // bother doing anything with that
706                output << "for (;";
707
708                if ( forStmt->get_condition() != 0 )
709                        forStmt->get_condition()->accept( *this );
710                output << ";";
711
712                if ( forStmt->get_increment() != 0 )
713                        forStmt->get_increment()->accept( *this );
714                output << ") ";
715
716                if ( forStmt->get_body() != 0 ) {
717                        output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
718                        forStmt->get_body()->accept( *this );
719                } // if
720        }
721
722        void CodeGenerator::visit( NullStmt *nullStmt ) {
723                //output << indent << CodeGenerator::printLabels( nullStmt->get_labels() );
724                output << "/* null statement */ ;";
725        }
726
727        void CodeGenerator::visit( DeclStmt *declStmt ) {
728                declStmt->get_decl()->accept( *this );
729
730                if ( doSemicolon( declStmt->get_decl() ) ) {
731                        output << ";";
732                } // if
733        }
734
735        std::string CodeGenerator::printLabels( std::list< Label > &l ) {
736                std::string str( "" );
737                l.unique(); // assumes a sorted list. Why not use set?
738
739                for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ )
740                        str += *i + ": ";
741
742                return str;
743        }
744
745        void CodeGenerator::handleStorageClass( Declaration *decl ) {
746                switch ( decl->get_storageClass() ) {
747                  case DeclarationNode::Extern:
748                        output << "extern ";
749                        break;
750                  case DeclarationNode::Static:
751                        output << "static ";
752                        break;
753                  case DeclarationNode::Auto:
754                        // silently drop storage class
755                        break;
756                  case DeclarationNode::Register:
757                        output << "register ";
758                        break;
759                  case DeclarationNode::Inline:
760                        output << "inline ";
761                        break;
762                  case DeclarationNode::Fortran:
763                        output << "fortran ";
764                        break;
765                  case DeclarationNode::Noreturn:
766                        output << "_Noreturn ";
767                        break;
768                  case DeclarationNode::Threadlocal:
769                        output << "_Thread_local ";
770                        break;
771                  case DeclarationNode::NoStorageClass:
772                        break;
773                } // switch
774        }
775} // namespace CodeGen
776
777// Local Variables: //
778// tab-width: 4 //
779// mode: c++ //
780// compile-command: "make install" //
781// End: //
Note: See TracBrowser for help on using the repository browser.