source: src/CodeGen/CodeGenerator.cc @ 3bb51e1

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 3bb51e1 was 3a5131ed, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

handle KR function declarations

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