source: src/CodeGen/CodeGenerator.cc @ 4e9151f

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 4e9151f was 5f642e38, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

more work on codegen output

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