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

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 262f085f was 262f085f, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

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

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