source: src/CodeGen/CodeGenerator.cc @ 2b6c1e0

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

more output code formatting

  • Property mode set to 100644
File size: 17.7 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 : Tue Jun 02 13:25:22 2015
13// Update Count     : 118
14//
15
16#include <algorithm>
17#include <iostream>
18#include <cassert>
19#include <list>
20
21#include "SynTree/Type.h"
22#include "SynTree/Declaration.h"
23#include "SynTree/Statement.h"
24#include "SynTree/Expression.h"
25#include "SynTree/Initializer.h"
26
27#include "utility.h"
28#include "UnimplementedError.h"
29
30#include "CodeGenerator.h"
31#include "OperatorTable.h"
32#include "GenType.h"
33
34using namespace std;
35
36namespace CodeGen {
37        int CodeGenerator::tabsize = 4;
38
39        // the kinds of statements that would ideally be separated by more whitespace
40        bool wantSpacing( Statement * stmt) {
41                return dynamic_cast< IfStmt * >( stmt ) || dynamic_cast< CompoundStmt * >( stmt ) ||
42                        dynamic_cast< WhileStmt * >( stmt ) || dynamic_cast< ForStmt * > ( stmt ) || dynamic_cast< SwitchStmt *>( stmt );
43        }
44
45        CodeGenerator::CodeGenerator( std::ostream &os ) : cur_indent( 0 ), insideFunction( false ), output( os ) { }
46
47        CodeGenerator::CodeGenerator( std::ostream &os, std::string init, int indent, bool infunp )
48                        : cur_indent( indent ), insideFunction( infunp ), output( os ) {
49                //output << std::string( init );
50        }
51
52        CodeGenerator::CodeGenerator( std::ostream &os, char *init, int indent, bool infunp )
53                        : cur_indent( indent ), insideFunction( infunp ), output( os ) {
54                //output << std::string( init );
55        }
56
57        string mangleName( DeclarationWithType *decl ) {
58                if ( decl->get_mangleName() != "" ) {
59                        return decl->get_mangleName();
60                } else {
61                        return decl->get_name();
62                } // if
63        }
64 
65        //*** Declarations
66        void CodeGenerator::visit( FunctionDecl *functionDecl ) {
67                handleStorageClass( functionDecl );
68                if ( functionDecl->get_isInline() ) {
69                        output << "inline ";
70                } // if
71                output << genType( functionDecl->get_functionType(), mangleName( functionDecl ) );
72
73                // how to get this to the Functype?
74                std::list< Declaration * > olds = functionDecl->get_oldDecls();
75                if ( ! olds.empty() ) {
76                        output << " /* function has old declaration */";
77                } // if
78
79                // acceptAll( functionDecl->get_oldDecls(), *this );
80                if ( functionDecl->get_statements() ) {
81                        functionDecl->get_statements()->accept(*this );
82                } // if
83        }
84
85        void CodeGenerator::visit( ObjectDecl *objectDecl ) {
86                handleStorageClass( objectDecl );
87                output << genType( objectDecl->get_type(), mangleName( objectDecl ) );
88       
89                if ( objectDecl->get_init() ) {
90                        output << " = ";
91                        objectDecl->get_init()->accept( *this );
92                } // if
93                if ( objectDecl->get_bitfieldWidth() ) {
94                        output << ":";
95                        objectDecl->get_bitfieldWidth()->accept( *this );
96                } // if
97        }
98
99        void CodeGenerator::handleAggregate( AggregateDecl *aggDecl ) {
100                if ( aggDecl->get_name() != "" )
101                        output << aggDecl->get_name();
102       
103                std::list< Declaration * > &memb = aggDecl->get_members();
104
105                if ( ! memb.empty() ) {
106                        output << endl << string( cur_indent, ' ' ) << "{" << endl;
107
108                        cur_indent += CodeGenerator::tabsize; 
109                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
110                                output << string( cur_indent, ' ' ); 
111                                (*i)->accept(*this );
112                                output << ";" << endl;
113                        }
114
115                        cur_indent -= CodeGenerator::tabsize; 
116
117                        output << string( cur_indent, ' ' ) << "}";
118                } // if
119        }
120
121        void CodeGenerator::visit( StructDecl *structDecl ) {
122                output << "struct ";
123                handleAggregate( structDecl );
124        }
125
126        void CodeGenerator::visit( UnionDecl *aggregateDecl ) {
127                output << "union ";
128                handleAggregate( aggregateDecl );
129        }
130 
131        void CodeGenerator::visit( EnumDecl *aggDecl ) {
132                output << "enum ";
133
134                if ( aggDecl->get_name() != "" )
135                        output << aggDecl->get_name();
136       
137                std::list< Declaration* > &memb = aggDecl->get_members();
138
139                if ( ! memb.empty() ) {
140                        output << endl << "{" << endl;
141
142                        cur_indent += CodeGenerator::tabsize; 
143                        for ( std::list< Declaration* >::iterator i = memb.begin(); i != memb.end();  i++) {
144                                ObjectDecl *obj = dynamic_cast< ObjectDecl* >( *i );
145                                assert( obj );
146                                output << string( cur_indent, ' ' ) << mangleName( obj ); 
147                                if ( obj->get_init() ) {
148                                        output << " = ";
149                                        obj->get_init()->accept(*this );
150                                } // if
151                                output << "," << endl;
152                        } // for
153
154                        cur_indent -= CodeGenerator::tabsize; 
155
156                        output << "}" << endl;
157                } // if
158        }
159 
160        void CodeGenerator::visit( ContextDecl *aggregateDecl ) {}
161 
162        void CodeGenerator::visit( TypedefDecl *typeDecl ) {
163                output << "typedef ";
164                output << genType( typeDecl->get_base(), typeDecl->get_name() );
165        }
166 
167        void CodeGenerator::visit( TypeDecl *typeDecl ) {
168                // really, we should mutate this into something that isn't a TypeDecl but that requires large-scale changes,
169                // still to be done
170                output << "extern unsigned long " << typeDecl->get_name();
171                if ( typeDecl->get_base() ) {
172                        output << " = sizeof( " << genType( typeDecl->get_base(), "" ) << " )";
173                } // if
174        }
175
176        void CodeGenerator::visit( SingleInit *init ) {
177                init->get_value()->accept( *this );
178        }
179
180        void CodeGenerator::visit( ListInit *init ) {
181                output << "{ ";
182                genCommaList( init->begin_initializers(), init->end_initializers() );
183                output << " }";
184        }
185
186        void CodeGenerator::visit( Constant *constant ) { 
187                output << constant->get_value() ;
188        }
189
190        //*** Expressions
191        void CodeGenerator::visit( ApplicationExpr *applicationExpr ) {
192                if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( applicationExpr->get_function() ) ) {
193                        OperatorInfo opInfo;
194                        if ( varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( varExpr->get_var()->get_name(), opInfo ) ) {
195                                std::list< Expression* >::iterator arg = applicationExpr->get_args().begin();
196                                switch ( opInfo.type ) {
197                                  case OT_PREFIXASSIGN:
198                                  case OT_POSTFIXASSIGN:
199                                  case OT_INFIXASSIGN:
200                                        {
201                                                assert( arg != applicationExpr->get_args().end() );
202                                                if ( AddressExpr *addrExpr = dynamic_cast< AddressExpr * >( *arg ) ) {
203               
204                                                        *arg = addrExpr->get_arg();
205                                                } else {
206                                                        UntypedExpr *newExpr = new UntypedExpr( new NameExpr( "*?" ) );
207                                                        newExpr->get_args().push_back( *arg );
208                                                        *arg = newExpr;
209                                                } // if
210                                                break;
211                                        }
212             
213                                  default:
214                                        // do nothing
215                                        ;
216                                }
217           
218                                switch ( opInfo.type ) {
219                                  case OT_INDEX:
220                                        assert( applicationExpr->get_args().size() == 2 );
221                                        (*arg++)->accept( *this );
222                                        output << "[";
223                                        (*arg)->accept( *this );
224                                        output << "]";
225                                        break;
226             
227                                  case OT_CALL:
228                                        // there are no intrinsic definitions of the function call operator
229                                        assert( false );
230                                        break;
231             
232                                  case OT_PREFIX:
233                                  case OT_PREFIXASSIGN:
234                                        assert( applicationExpr->get_args().size() == 1 );
235                                        output << "(";
236                                        output << opInfo.symbol;
237                                        (*arg)->accept( *this );
238                                        output << ")";
239                                        break;
240             
241                                  case OT_POSTFIX:
242                                  case OT_POSTFIXASSIGN:
243                                        assert( applicationExpr->get_args().size() == 1 );
244                                        (*arg)->accept( *this );
245                                        output << opInfo.symbol;
246                                        break;
247
248                                  case OT_INFIX:
249                                  case OT_INFIXASSIGN:
250                                        assert( applicationExpr->get_args().size() == 2 );
251                                        output << "(";
252                                        (*arg++)->accept( *this );
253                                        output << opInfo.symbol;
254                                        (*arg)->accept( *this );
255                                        output << ")";
256                                        break;
257             
258                                  case OT_CONSTANT:
259                                        // there are no intrinsic definitions of 0 or 1 as functions
260                                        assert( false );
261                                }
262                        } else {
263                                varExpr->accept( *this );
264                                output << "(";
265                                genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
266                                output << ")";
267                        } // if
268                } else {
269                        applicationExpr->get_function()->accept( *this );
270                        output << "(";
271                        genCommaList( applicationExpr->get_args().begin(), applicationExpr->get_args().end() );
272                        output << ")";
273                } // if
274        }
275 
276        void CodeGenerator::visit( UntypedExpr *untypedExpr ) {
277                if ( NameExpr *nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
278                        OperatorInfo opInfo;
279                        if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
280                                std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
281                                switch ( opInfo.type ) {
282                                  case OT_INDEX:
283                                        assert( untypedExpr->get_args().size() == 2 );
284                                        (*arg++)->accept( *this );
285                                        output << "[";
286                                        (*arg)->accept( *this );
287                                        output << "]";
288                                        break;
289             
290                                  case OT_CALL:
291                                        assert( false );
292                                        break;
293             
294                                  case OT_PREFIX:
295                                  case OT_PREFIXASSIGN:
296                                        assert( untypedExpr->get_args().size() == 1 );
297                                        output << "(";
298                                        output << opInfo.symbol;
299                                        (*arg)->accept( *this );
300                                        output << ")";
301                                        break;
302             
303                                  case OT_POSTFIX:
304                                  case OT_POSTFIXASSIGN:
305                                        assert( untypedExpr->get_args().size() == 1 );
306                                        (*arg)->accept( *this );
307                                        output << opInfo.symbol;
308                                        break;
309 
310                                  case OT_INFIX:
311                                  case OT_INFIXASSIGN:
312                                        assert( untypedExpr->get_args().size() == 2 );
313                                        output << "(";
314                                        (*arg++)->accept( *this );
315                                        output << opInfo.symbol;
316                                        (*arg)->accept( *this );
317                                        output << ")";
318                                        break;
319             
320                                  case OT_CONSTANT:
321                                        // there are no intrinsic definitions of 0 or 1 as functions
322                                        assert( false );
323                                }
324                        } else {
325                                nameExpr->accept( *this );
326                                output << "(";
327                                genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
328                                output << ")";
329                        } // if
330                } else {
331                        untypedExpr->get_function()->accept( *this );
332                        output << "(";
333                        genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
334                        output << ")";
335                } // if
336        }
337 
338        void CodeGenerator::visit( NameExpr *nameExpr ) {
339                OperatorInfo opInfo;
340                if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
341                        assert( opInfo.type == OT_CONSTANT );
342                        output << opInfo.symbol;
343                } else {
344                        output << nameExpr->get_name();
345                } // if
346        }
347 
348        void CodeGenerator::visit( AddressExpr *addressExpr ) {
349                output << "(&";
350                // this hack makes sure that we don't convert "constant_zero" to "0" if we're taking its address
351                if ( VariableExpr *variableExpr = dynamic_cast< VariableExpr* >( addressExpr->get_arg() ) ) {
352                        output << mangleName( variableExpr->get_var() );
353                } else {
354                        addressExpr->get_arg()->accept( *this );
355                } // if
356                output << ")";
357        }
358
359        void CodeGenerator::visit( CastExpr *castExpr ) {
360                output << "((";
361                if ( castExpr->get_results().empty() ) {
362                        output << "void" ;
363                } else {
364                        output << genType( castExpr->get_results().front(), "" );
365                } // if
366                output << ")";
367                castExpr->get_arg()->accept( *this );
368                output << ")";
369        }
370 
371        void CodeGenerator::visit( UntypedMemberExpr *memberExpr ) {
372                assert( false );
373        }
374 
375        void CodeGenerator::visit( MemberExpr *memberExpr ) {
376                memberExpr->get_aggregate()->accept( *this );
377                output << "." << mangleName( memberExpr->get_member() );
378        }
379 
380        void CodeGenerator::visit( VariableExpr *variableExpr ) {
381                OperatorInfo opInfo;
382                if ( variableExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic && operatorLookup( variableExpr->get_var()->get_name(), opInfo ) && opInfo.type == OT_CONSTANT ) {
383                        output << opInfo.symbol;
384                } else {
385                        output << mangleName( variableExpr->get_var() );
386                } // if
387        }
388 
389        void CodeGenerator::visit( ConstantExpr *constantExpr ) {
390                assert( constantExpr->get_constant() );
391                constantExpr->get_constant()->accept( *this );
392        }
393 
394        void CodeGenerator::visit( SizeofExpr *sizeofExpr ) {
395                output << "sizeof(";
396                if ( sizeofExpr->get_isType() ) {
397                        output << genType( sizeofExpr->get_type(), "" );
398                } else {
399                        sizeofExpr->get_expr()->accept( *this );
400                } // if
401                output << ")";
402        }
403 
404        void CodeGenerator::visit( LogicalExpr *logicalExpr ) {
405                output << "(";
406                logicalExpr->get_arg1()->accept( *this );
407                if ( logicalExpr->get_isAnd() ) {
408                        output << " && ";
409                } else {
410                        output << " || ";
411                } // if
412                logicalExpr->get_arg2()->accept( *this );
413                output << ")";
414        }
415 
416        void CodeGenerator::visit( ConditionalExpr *conditionalExpr ) {
417                output << "(";
418                conditionalExpr->get_arg1()->accept( *this );
419                output << " ? ";
420                conditionalExpr->get_arg2()->accept( *this );
421                output << " : ";
422                conditionalExpr->get_arg3()->accept( *this );
423                output << ")";
424        }
425 
426        void CodeGenerator::visit( CommaExpr *commaExpr ) {
427                output << "(";
428                commaExpr->get_arg1()->accept( *this );
429                output << " , ";
430                commaExpr->get_arg2()->accept( *this );
431                output << ")";
432        }
433 
434        void CodeGenerator::visit( TupleExpr *tupleExpr ) {}
435 
436        void CodeGenerator::visit( TypeExpr *typeExpr ) {}
437
438        //*** Statements
439        void CodeGenerator::visit( CompoundStmt *compoundStmt ) {
440                std::list<Statement*> ks = compoundStmt->get_kids();
441                output << "{" << endl;
442
443                cur_indent += CodeGenerator::tabsize;
444
445                for ( std::list<Statement *>::iterator i = ks.begin(); i != ks.end();  i++) {
446                        output << string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() );
447                        (*i)->accept(*this );
448
449                        output << endl;
450                        if ( wantSpacing( *i ) ) {
451                                output << endl;
452                        }
453                }
454                cur_indent -= CodeGenerator::tabsize; 
455
456                output << string( cur_indent, ' ' ) << "}";
457        }
458
459        void CodeGenerator::visit( ExprStmt *exprStmt ) {
460                // I don't see why this check is necessary.
461                // If this starts to cause problems then put it back in,
462                // with an explanation
463                assert( exprStmt );
464
465                // if ( exprStmt != 0 ) {
466                exprStmt->get_expr()->accept( *this );
467                output << ";" ;
468                // } // if
469        }
470
471        void CodeGenerator::visit( IfStmt *ifStmt ) {
472                output << "if (";
473                ifStmt->get_condition()->accept(*this );
474                output << ") ";
475
476                ifStmt->get_thenPart()->accept(*this );
477
478                if ( ifStmt->get_elsePart() != 0) {
479                        output << " else ";
480                        ifStmt->get_elsePart()->accept(*this );
481                } // if
482        }
483
484        void CodeGenerator::visit( SwitchStmt *switchStmt ) {
485                //output << /* "\r" << */ string( cur_indent, ' ' ) << CodeGenerator::printLabels( switchStmt->get_labels() )
486                output << "switch (" ;
487                switchStmt->get_condition()->accept(*this );
488                output << ") ";
489               
490                output << "{" << std::endl;
491                cur_indent += CodeGenerator::tabsize;
492
493                acceptAll( switchStmt->get_branches(), *this );
494
495                cur_indent -= CodeGenerator::tabsize;
496
497                output << string( cur_indent, ' ' ) << "}";
498        }
499
500        void CodeGenerator::visit( CaseStmt *caseStmt ) {
501                output << string( cur_indent, ' ' );
502                if ( caseStmt->isDefault()) 
503                        output << "default";
504                else {
505                        output << "case ";
506                        caseStmt->get_condition()->accept(*this );
507                } // if
508                output << ":\n";
509               
510                std::list<Statement *> sts = caseStmt->get_statements();
511
512                cur_indent += CodeGenerator::tabsize;
513                for ( std::list<Statement *>::iterator i = sts.begin(); i != sts.end();  i++) {
514                        output << /* "\r" << */ string( cur_indent, ' ' ) << printLabels( (*i)->get_labels() )  ;
515                        (*i)->accept(*this );
516                        output << endl;
517                }
518                cur_indent -= CodeGenerator::tabsize;
519        }
520
521        void CodeGenerator::visit( BranchStmt *branchStmt ) {
522                output << "\r" << string( cur_indent, ' ' );
523                output << CodeGenerator::printLabels( branchStmt->get_labels());
524
525                switch ( branchStmt->get_type()) {
526                  case BranchStmt::Goto:
527                        if ( ! branchStmt->get_target().empty() )
528                                output << "goto " << branchStmt->get_target();
529                        else { 
530                                if ( branchStmt->get_computedTarget() != 0 ) {
531                                        output << "goto *";
532                                        branchStmt->get_computedTarget()->accept( *this );
533                                } // if
534                        } // if
535                        break;
536                  case BranchStmt::Break:
537                        output << "break";
538                        break;
539                  case BranchStmt::Continue:
540                        output << "continue";
541                        break;
542                }
543                output << ";";
544        }
545
546
547        void CodeGenerator::visit( ReturnStmt *returnStmt ) {
548                output << "return ";
549
550                // xxx -- check for null expression;
551                if ( returnStmt->get_expr() ) {
552                        returnStmt->get_expr()->accept( *this );
553                } // if
554                output << ";";
555        }
556
557        void CodeGenerator::visit( WhileStmt *whileStmt ) {
558                if ( whileStmt->get_isDoWhile() )
559                        output << "do" ;
560                else {
561                        output << "while (" ;
562                        whileStmt->get_condition()->accept(*this );
563                        output << ")";
564                } // if
565                output << " ";
566
567                output << CodeGenerator::printLabels( whileStmt->get_body()->get_labels() );
568                whileStmt->get_body()->accept( *this );
569
570                output << /* "\r" << */ string( cur_indent, ' ' );
571
572                if ( whileStmt->get_isDoWhile() ) {
573                        output << " while (" ;
574                        whileStmt->get_condition()->accept(*this );
575                        output << ");";
576                } // if
577        }
578
579        void CodeGenerator::visit( ForStmt *forStmt ) {
580                output << "for (";
581
582                if ( forStmt->get_initialization() != 0 )
583                        forStmt->get_initialization()->accept( *this );
584                else
585                        output << ";";
586               
587                if ( forStmt->get_condition() != 0 )
588                        forStmt->get_condition()->accept( *this );
589                output << ";";
590
591                if ( forStmt->get_increment() != 0 )
592                        forStmt->get_increment()->accept( *this );
593                output << ") ";
594
595                if ( forStmt->get_body() != 0 ) {
596                        output << CodeGenerator::printLabels( forStmt->get_body()->get_labels() );
597                        forStmt->get_body()->accept( *this );
598                } // if
599        }
600
601        void CodeGenerator::visit( NullStmt *nullStmt ) {
602                //output << /* "\r" << */ string( cur_indent, ' ' ) << CodeGenerator::printLabels( nullStmt->get_labels() );
603                output << "/* null statement */ ;";
604        }
605
606        void CodeGenerator::visit( DeclStmt *declStmt ) {
607                declStmt->get_decl()->accept( *this );
608       
609                if ( doSemicolon( declStmt->get_decl() ) ) {
610                        output << ";";
611                } // if
612        }
613
614        std::string CodeGenerator::printLabels( std::list< Label > &l ) {
615                std::string str( "" );
616                l.unique(); // assumes a sorted list. Why not use set?
617
618                for ( std::list< Label >::iterator i = l.begin(); i != l.end(); i++ )
619                        str += *i + ": ";
620
621                return str;
622        }
623
624        void CodeGenerator::handleStorageClass( Declaration *decl ) {
625                switch ( decl->get_storageClass() ) {
626                  case Declaration::NoStorageClass:
627                        break;
628                  case Declaration::Extern:
629                        output << "extern ";
630                        break;
631                  case Declaration::Static:
632                        output << "static ";
633                        break;
634                  case Declaration::Auto:
635                        // silently drop storage class
636                        break;
637                  case Declaration::Register:
638                        output << "register ";
639                        break;
640                  case Declaration::Inline:
641                        // handled as special via isInline flag (FIX)
642                        break;
643                  case Declaration::Fortran:
644                        // not handled
645                        break;
646                } // switch
647        }
648} // namespace CodeGen
649
650// Local Variables: //
651// tab-width: 4 //
652// mode: c++ //
653// compile-command: "make install" //
654// End: //
Note: See TracBrowser for help on using the repository browser.