Changeset 7baed7d


Ignore:
Timestamp:
Jun 6, 2016, 5:46:09 PM (8 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
64a32c6
Parents:
64071c2
Message:

generalize notion of a GCC attribute

Location:
src
Files:
8 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    r64071c2 r7baed7d  
    2626#include "SynTree/Statement.h"
    2727#include "SynTree/Type.h"
     28#include "SynTree/Attribute.h"
    2829
    2930#include "Common/utility.h"
     
    7677        }
    7778
     79        void CodeGenerator::genAttributes( std::list< Attribute * > & attributes ) {
     80                if ( ! attributes.empty() ) {
     81                        output << "__attribute__ ((";
     82                        for ( Attribute *& attr : attributes ) {
     83                                if ( ! attr->empty() ) {
     84                                        output << attr->get_name() << "(";
     85                                        genCommaList( attr->get_parameters().begin(), attr->get_parameters().end() );
     86                                        output << ")";
     87                                }
     88                                output << ",";
     89                        }
     90                        output << ")) ";
     91                }
     92        }
     93
     94
    7895        //*** Declarations
    7996        void CodeGenerator::visit( FunctionDecl *functionDecl ) {
    80                 // generalize this
    81                 FunctionDecl::Attribute attr = functionDecl->get_attribute();
    82                 switch ( attr.type ) {
    83                         case FunctionDecl::Attribute::Constructor:
    84                                 output << "__attribute__ ((constructor";
    85                                 if ( attr.priority != FunctionDecl::Attribute::Default ) {
    86                                         output << "(" << attr.priority << ")";
    87                                 }
    88                                 output << ")) ";
    89                                 break;
    90                         case FunctionDecl::Attribute::Destructor:
    91                                 output << "__attribute__ ((destructor";
    92                                 if ( attr.priority != FunctionDecl::Attribute::Default ) {
    93                                         output << "(" << attr.priority << ")";
    94                                 }
    95                                 output << ")) ";
    96                                 break;
    97                         default:
    98                                 break;
    99                 }
     97                genAttributes( functionDecl->get_attributes() );
     98
    10099                handleStorageClass( functionDecl );
    101100                if ( functionDecl->get_isInline() ) {
  • src/CodeGen/CodeGenerator.h

    r64071c2 r7baed7d  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // CodeGenerator.h -- 
     7// CodeGenerator.h --
    88//
    99// Author           : Richard C. Bilson
     
    6060                virtual void visit( MemberExpr *memberExpr );
    6161                virtual void visit( VariableExpr *variableExpr );
    62                 virtual void visit( ConstantExpr *constantExpr ); 
     62                virtual void visit( ConstantExpr *constantExpr );
    6363                virtual void visit( SizeofExpr *sizeofExpr );
    6464                virtual void visit( AlignofExpr *alignofExpr );
     
    8585                virtual void visit( ForStmt * );
    8686                virtual void visit( NullStmt * );
    87                 virtual void visit( DeclStmt * );
     87                virtual void visit( DeclStmt * );
     88
     89                void genAttributes( std::list< Attribute * > & attributes );
    8890
    8991                template< class Iterator > void genCommaList( Iterator begin, Iterator end );
     
    108110
    109111        };
    110        
     112
    111113        template< class Iterator >
    112114        void CodeGenerator::genCommaList( Iterator begin, Iterator end ) {
     
    119121                } // for
    120122        }
    121  
     123
    122124        inline bool doSemicolon( Declaration* decl ) {
    123125                if ( FunctionDecl* func = dynamic_cast< FunctionDecl* >( decl ) ) {
  • src/InitTweak/FixGlobalInit.cc

    r64071c2 r7baed7d  
    2222#include "SynTree/Initializer.h"
    2323#include "SynTree/Visitor.h"
     24#include "SynTree/Attribute.h"
    2425#include <algorithm>
    2526
     
    116117        GlobalFixer::GlobalFixer( const std::string & name, bool inLibrary ) : tempNamer( "_global_init" ) {
    117118                std::string fixedName = globalFunctionName( name );
    118                 initFunction = new FunctionDecl( "_init_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false, FunctionDecl::Attribute( FunctionDecl::Attribute::Constructor, inLibrary ? FunctionDecl::Attribute::High : FunctionDecl::Attribute::Default ) );
    119 
    120                 destroyFunction = new FunctionDecl( "_destroy_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false, FunctionDecl::Attribute( FunctionDecl::Attribute::Destructor, inLibrary ? FunctionDecl::Attribute::High : FunctionDecl::Attribute::Default ) );
     119                std::list< Expression * > ctorParameters;
     120                std::list< Expression * > dtorParameters;
     121                if ( inLibrary ) {
     122                        // Constructor/destructor attributes take a single parameter which
     123                        // is the priority, with lower numbers meaning higher priority.
     124                        // Functions specified with priority are guaranteed to run before
     125                        // functions without a priority. To ensure that constructors and destructors
     126                        // for library code are run before constructors and destructors for user code,
     127                        // specify a priority when building the library. Priorities 0-100 are reserved by gcc.
     128                        ctorParameters.push_back( new ConstantExpr( Constant::from_int( 101 ) ) );
     129                        dtorParameters.push_back( new ConstantExpr( Constant::from_int( 101 ) ) );
     130                }
     131                initFunction = new FunctionDecl( "_init_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false );
     132                initFunction->get_attributes().push_back( new Attribute( "constructor", ctorParameters ) );
     133                destroyFunction = new FunctionDecl( "_destroy_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false );
     134                destroyFunction->get_attributes().push_back( new Attribute( "destructor", dtorParameters ) );
    121135        }
    122136
  • src/Makefile.in

    r64071c2 r7baed7d  
    195195        SynTree/driver_cfa_cpp-Mutator.$(OBJEXT) \
    196196        SynTree/driver_cfa_cpp-TypeSubstitution.$(OBJEXT) \
     197        SynTree/driver_cfa_cpp-Attribute.$(OBJEXT) \
    197198        Tuples/driver_cfa_cpp-Mutate.$(OBJEXT) \
    198199        Tuples/driver_cfa_cpp-AssignExpand.$(OBJEXT) \
     
    383384        SynTree/NamedTypeDecl.cc SynTree/TypeDecl.cc \
    384385        SynTree/Initializer.cc SynTree/Visitor.cc SynTree/Mutator.cc \
    385         SynTree/TypeSubstitution.cc Tuples/Mutate.cc \
    386         Tuples/AssignExpand.cc Tuples/FunctionFixer.cc \
    387         Tuples/TupleAssignment.cc Tuples/FunctionChecker.cc \
    388         Tuples/NameMatcher.cc
     386        SynTree/TypeSubstitution.cc SynTree/Attribute.cc \
     387        Tuples/Mutate.cc Tuples/AssignExpand.cc \
     388        Tuples/FunctionFixer.cc Tuples/TupleAssignment.cc \
     389        Tuples/FunctionChecker.cc Tuples/NameMatcher.cc
    389390MAINTAINERCLEANFILES = Parser/parser.output ${libdir}/${notdir \
    390391        ${cfa_cpplib_PROGRAMS}}
     
    755756SynTree/driver_cfa_cpp-TypeSubstitution.$(OBJEXT):  \
    756757        SynTree/$(am__dirstamp) SynTree/$(DEPDIR)/$(am__dirstamp)
     758SynTree/driver_cfa_cpp-Attribute.$(OBJEXT): SynTree/$(am__dirstamp) \
     759        SynTree/$(DEPDIR)/$(am__dirstamp)
    757760Tuples/$(am__dirstamp):
    758761        @$(MKDIR_P) Tuples
     
    852855        -rm -f SynTree/driver_cfa_cpp-ArrayType.$(OBJEXT)
    853856        -rm -f SynTree/driver_cfa_cpp-AttrType.$(OBJEXT)
     857        -rm -f SynTree/driver_cfa_cpp-Attribute.$(OBJEXT)
    854858        -rm -f SynTree/driver_cfa_cpp-BasicType.$(OBJEXT)
    855859        -rm -f SynTree/driver_cfa_cpp-CommaExpr.$(OBJEXT)
     
    961965@AMDEP_TRUE@@am__include@ @am__quote@SynTree/$(DEPDIR)/driver_cfa_cpp-ArrayType.Po@am__quote@
    962966@AMDEP_TRUE@@am__include@ @am__quote@SynTree/$(DEPDIR)/driver_cfa_cpp-AttrType.Po@am__quote@
     967@AMDEP_TRUE@@am__include@ @am__quote@SynTree/$(DEPDIR)/driver_cfa_cpp-Attribute.Po@am__quote@
    963968@AMDEP_TRUE@@am__include@ @am__quote@SynTree/$(DEPDIR)/driver_cfa_cpp-BasicType.Po@am__quote@
    964969@AMDEP_TRUE@@am__include@ @am__quote@SynTree/$(DEPDIR)/driver_cfa_cpp-CommaExpr.Po@am__quote@
     
    23962401@AMDEP_TRUE@@am__fastdepCXX_FALSE@      DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
    23972402@am__fastdepCXX_FALSE@  $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(driver_cfa_cpp_CXXFLAGS) $(CXXFLAGS) -c -o SynTree/driver_cfa_cpp-TypeSubstitution.obj `if test -f 'SynTree/TypeSubstitution.cc'; then $(CYGPATH_W) 'SynTree/TypeSubstitution.cc'; else $(CYGPATH_W) '$(srcdir)/SynTree/TypeSubstitution.cc'; fi`
     2403
     2404SynTree/driver_cfa_cpp-Attribute.o: SynTree/Attribute.cc
     2405@am__fastdepCXX_TRUE@   $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(driver_cfa_cpp_CXXFLAGS) $(CXXFLAGS) -MT SynTree/driver_cfa_cpp-Attribute.o -MD -MP -MF SynTree/$(DEPDIR)/driver_cfa_cpp-Attribute.Tpo -c -o SynTree/driver_cfa_cpp-Attribute.o `test -f 'SynTree/Attribute.cc' || echo '$(srcdir)/'`SynTree/Attribute.cc
     2406@am__fastdepCXX_TRUE@   $(am__mv) SynTree/$(DEPDIR)/driver_cfa_cpp-Attribute.Tpo SynTree/$(DEPDIR)/driver_cfa_cpp-Attribute.Po
     2407@AMDEP_TRUE@@am__fastdepCXX_FALSE@      source='SynTree/Attribute.cc' object='SynTree/driver_cfa_cpp-Attribute.o' libtool=no @AMDEPBACKSLASH@
     2408@AMDEP_TRUE@@am__fastdepCXX_FALSE@      DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
     2409@am__fastdepCXX_FALSE@  $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(driver_cfa_cpp_CXXFLAGS) $(CXXFLAGS) -c -o SynTree/driver_cfa_cpp-Attribute.o `test -f 'SynTree/Attribute.cc' || echo '$(srcdir)/'`SynTree/Attribute.cc
     2410
     2411SynTree/driver_cfa_cpp-Attribute.obj: SynTree/Attribute.cc
     2412@am__fastdepCXX_TRUE@   $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(driver_cfa_cpp_CXXFLAGS) $(CXXFLAGS) -MT SynTree/driver_cfa_cpp-Attribute.obj -MD -MP -MF SynTree/$(DEPDIR)/driver_cfa_cpp-Attribute.Tpo -c -o SynTree/driver_cfa_cpp-Attribute.obj `if test -f 'SynTree/Attribute.cc'; then $(CYGPATH_W) 'SynTree/Attribute.cc'; else $(CYGPATH_W) '$(srcdir)/SynTree/Attribute.cc'; fi`
     2413@am__fastdepCXX_TRUE@   $(am__mv) SynTree/$(DEPDIR)/driver_cfa_cpp-Attribute.Tpo SynTree/$(DEPDIR)/driver_cfa_cpp-Attribute.Po
     2414@AMDEP_TRUE@@am__fastdepCXX_FALSE@      source='SynTree/Attribute.cc' object='SynTree/driver_cfa_cpp-Attribute.obj' libtool=no @AMDEPBACKSLASH@
     2415@AMDEP_TRUE@@am__fastdepCXX_FALSE@      DEPDIR=$(DEPDIR) $(CXXDEPMODE) $(depcomp) @AMDEPBACKSLASH@
     2416@am__fastdepCXX_FALSE@  $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(driver_cfa_cpp_CXXFLAGS) $(CXXFLAGS) -c -o SynTree/driver_cfa_cpp-Attribute.obj `if test -f 'SynTree/Attribute.cc'; then $(CYGPATH_W) 'SynTree/Attribute.cc'; else $(CYGPATH_W) '$(srcdir)/SynTree/Attribute.cc'; fi`
    23982417
    23992418Tuples/driver_cfa_cpp-Mutate.o: Tuples/Mutate.cc
  • src/SynTree/Declaration.h

    r64071c2 r7baed7d  
    115115        typedef DeclarationWithType Parent;
    116116  public:
    117         // temporary - merge this into general GCC attributes
    118         struct Attribute {
    119                 enum Type {
    120                         NoAttribute, Constructor, Destructor,
    121                 } type;
    122                 enum Priority {
    123                         // priorities 0-100 are reserved by gcc, so it's okay to use 100 an exceptional case
    124                         Default = 100, High,
    125                 } priority;
    126                 Attribute(Type t = NoAttribute, Priority p = Default) : type(t), priority(p) {};
    127         };
    128 
    129         FunctionDecl( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Type linkage, FunctionType *type, CompoundStmt *statements, bool isInline, bool isNoreturn, Attribute attribute = Attribute() );
     117        FunctionDecl( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Type linkage, FunctionType *type, CompoundStmt *statements, bool isInline, bool isNoreturn, const std::list< Attribute * > attributes = std::list< Attribute * >() );
    130118        FunctionDecl( const FunctionDecl &other );
    131119        virtual ~FunctionDecl();
     
    140128        std::list< std::string >& get_oldIdents() { return oldIdents; }
    141129        std::list< Declaration* >& get_oldDecls() { return oldDecls; }
    142         Attribute get_attribute() const { return attribute; }
    143         void set_attribute( Attribute newValue ) { attribute = newValue; }
     130        std::list< Attribute * >& get_attributes() { return attributes; }
    144131
    145132        virtual FunctionDecl *clone() const { return new FunctionDecl( *this ); }
     
    153140        std::list< std::string > oldIdents;
    154141        std::list< Declaration* > oldDecls;
    155         Attribute attribute;
     142        std::list< Attribute * > attributes;
    156143};
    157144
  • src/SynTree/FunctionDecl.cc

    r64071c2 r7baed7d  
    1919#include "Statement.h"
    2020#include "Type.h"
     21#include "Attribute.h"
    2122#include "Common/utility.h"
    2223
    23 FunctionDecl::FunctionDecl( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Type linkage, FunctionType *type, CompoundStmt *statements, bool isInline, bool isNoreturn, Attribute attribute )
    24                 : Parent( name, sc, linkage ), type( type ), statements( statements ), attribute( attribute ) {
     24FunctionDecl::FunctionDecl( const std::string &name, DeclarationNode::StorageClass sc, LinkageSpec::Type linkage, FunctionType *type, CompoundStmt *statements, bool isInline, bool isNoreturn, std::list< Attribute * > attributes )
     25                : Parent( name, sc, linkage ), type( type ), statements( statements ), attributes( attributes ) {
    2526        set_isInline( isInline );
    2627        set_isNoreturn( isNoreturn );
     
    3233
    3334FunctionDecl::FunctionDecl( const FunctionDecl &other )
    34         : Parent( other ), type( maybeClone( other.type ) ), statements( maybeClone( other.statements ) ), attribute( other.attribute ) {
     35        : Parent( other ), type( maybeClone( other.type ) ), statements( maybeClone( other.statements ) ) {
     36                cloneAll( other.attributes, attributes );
    3537}
    3638
     
    3840        delete type;
    3941        delete statements;
     42        deleteAll( attributes );
    4043}
    4144
     
    6568                os << "_Noreturn ";
    6669        } // if
    67         switch ( attribute.type ) {
    68                 case Attribute::Constructor:
    69                         os << "Global Constructor ";
    70                         break;
    71                 case Attribute::Destructor:
    72                         os << "Global Destructor ";
    73                         break;
    74                 default:
    75                         break;
    76         }
    77         if ( attribute.priority != Attribute::Default ) {
    78                 os << "with priority " << attribute.priority << " ";
    79         }
     70
     71        printAll( attributes, os, indent );
     72
    8073        if ( get_storageClass() != DeclarationNode::NoStorageClass ) {
    8174                os << DeclarationNode::storageName[ get_storageClass() ] << ' ';
     
    118111                os << "_Noreturn ";
    119112        } // if
    120         switch ( attribute.type ) {
    121                 case Attribute::Constructor:
    122                         os << " Global Constructor ";
    123                         break;
    124                 case Attribute::Destructor:
    125                         os << " Global Destructor ";
    126                         break;
    127                 default:
    128                         break;
    129         }
    130         if ( attribute.priority != Attribute::Default ) {
    131                 os << "with priority " << attribute.priority << " ";
    132         }
     113
     114        // xxx - should printShort print attributes?
     115
    133116        if ( get_storageClass() != DeclarationNode::NoStorageClass ) {
    134117                os << DeclarationNode::storageName[ get_storageClass() ] << ' ';
  • src/SynTree/SynTree.h

    r64071c2 r7baed7d  
    118118class TypeSubstitution;
    119119
     120// gcc attribute
     121class Attribute;
     122
    120123#endif // SYNTREE_H
    121124
  • src/SynTree/module.mk

    r64071c2 r7baed7d  
    66## file "LICENCE" distributed with Cforall.
    77##
    8 ## module.mk -- 
     8## module.mk --
    99##
    1010## Author           : Richard C. Bilson
     
    4646       SynTree/Visitor.cc \
    4747       SynTree/Mutator.cc \
    48        SynTree/TypeSubstitution.cc
     48       SynTree/TypeSubstitution.cc \
     49       SynTree/Attribute.cc
    4950
Note: See TracChangeset for help on using the changeset viewer.