GPUOcelot

PTXParser.h

Go to the documentation of this file.
00001 
00007 #ifndef PTX_PARSER_H_INCLUDED
00008 #define PTX_PARSER_H_INCLUDED
00009 
00010 #include <ocelot/parser/interface/Parser.h>
00011 
00012 #undef yyFlexLexer
00013 #define yyFlexLexer ptxFlexLexer
00014 #include <FlexLexer.h>
00015 
00016 #include <ocelot/parser/interface/PTXLexer.h>
00017 #include <ocelot/ir/interface/Module.h>
00018 #include <unordered_map>
00019 #include <unordered_set>
00020 #include <vector>
00021 
00022 namespace parser
00023 {
00025   class PTXParser : public Parser
00026   {
00027     public:
00028       class State
00029       {
00030         public:
00031           class OperandWrapper
00032           {
00033             public:
00034               OperandWrapper( const ir::PTXOperand& o, 
00035                 ir::PTXInstruction::AddressSpace s 
00036                 = ir::PTXInstruction::AddressSpace_Invalid );
00037             public:
00038               ir::PTXOperand operand;
00039               ir::PTXInstruction::AddressSpace space;
00040           };
00041           
00042           class FunctionPrototype
00043           {
00044             public:
00045               typedef std::vector< ir::PTXOperand::DataType > 
00046                 TypeVector;
00047           
00048             public:
00049               TypeVector returnTypes;
00050               TypeVector argumentTypes;
00051               std::string name;
00052           
00053             public:
00054               void clear();
00055               bool compare( const FunctionPrototype& t );
00056               std::string toString() const;
00057           };
00058         
00059           typedef std::unordered_map< std::string, unsigned int > 
00060             StringMap;
00061           typedef std::vector< std::string > StringList;
00062           typedef std::unordered_set< std::string > StringSet;
00063           typedef std::vector< StringSet > StringSetStack;
00064           typedef std::vector< unsigned int > UintStack;
00065           typedef std::vector< unsigned int > DimensionVector;
00066           typedef std::vector< double > DoubleVector;
00067           typedef std::unordered_map< std::string, 
00068             OperandWrapper > OperandMap;
00069           typedef std::vector< ir::PTXOperand > OperandVector;
00070           typedef std::unordered_map< std::string, 
00071             FunctionPrototype > PrototypeMap;
00072       
00073           enum Error
00074           {
00075             Success,
00076             SyntaxError,
00077             MalformedVersion,
00078             InvalidDataType,
00079             InvalidVecType,
00080             InitializerSizeMismatch,
00081             InvalidInstruction,
00082             DuplicateDeclaration,
00083             NoDeclaration,
00084             InvalidOpcode,
00085             DuplicateLabel,
00086             NoPrototype,
00087             PrototypeMismatch,
00088             NoLabel,
00089             InvalidArray,
00090             NotPredicate,
00091             NotSupported,
00092             NotVersion2_1,
00093             Invalid
00094           };
00095       
00096         public:
00097           ir::Module::StatementVector statements;
00098           StringMap labels;
00099           std::string fileName;
00100         
00101         public:
00102           std::string sectionType;
00103           std::string sectionName;
00104           
00105           StringList identifiers;
00106           OperandMap operands;
00107           StringSetStack localOperands;
00108 
00109           PrototypeMap prototypes;
00110           StringSetStack localPrototypes;
00111           
00112           bool inEntry;
00113           bool inArgumentList;
00114           bool inReturnList;
00115           
00116           unsigned int returnOperands;
00117           unsigned int alignment;
00118           ir::PTXOperand operand;
00119           OperandVector operandVector;
00120           ir::PTXStatement statement;
00121           FunctionPrototype prototype;
00122           
00123           ir::PTXStatement::Directive directive;
00124           std::string comment;
00125                   
00126         private:
00127           static ir::PTXInstruction::AddressSpace _toAddressSpace( 
00128             ir::PTXStatement::Directive directive );
00129         
00130         private:
00131           void _setImmediateTypes();
00132         
00133         public:
00134           void addSpecialRegisters();
00135         
00136           void maxnreg( unsigned int regs );
00137           void maxntid( unsigned int tidx, unsigned int tidy = 1024, 
00138             unsigned int tidz = 1024 );
00139           void ctapersm( int target, unsigned int ctas );
00140           void maxnctapersm( unsigned int ctas );
00141           void maxnctapersm();
00142           void minnctapersm( unsigned int ctas );
00143           void minnctapersm();
00144         
00145         public:
00146           void preprocessor( int token );
00147           void version( double version, YYLTYPE& location );
00148           void addressSize( unsigned int size );
00149           void identifierList( const std::string& identifier );
00150           void identifierList2( const std::string& identifier );
00151           void decimalListSingle( long long int value );
00152           void decimalListSingle2( long long int value );
00153           void symbolListSingle( const std::string& identifier );
00154           void symbolListSingle2( const std::string& identifier );
00155           void floatList( double value );
00156           void floatList1( double value );
00157           void singleList( float value );
00158           void singleList1( float value );
00159           void targetElement( int token );
00160           void target();
00161           void noAddressSpace();
00162           void addressSpace( int token );
00163           void dataType( int token );
00164           void statementVectorType( int token );
00165           void instructionVectorType( int token );
00166           void attribute( bool visible, bool external );
00167           void shiftAmount( bool shift );
00168           void vectorIndex( int token );
00169           
00170           void arrayDimensionSet( long long int value, 
00171             YYLTYPE& location, bool add );
00172           void arrayDimensionSet();
00173           void arrayDimensions();
00174           void assignment();
00175           void registerDeclaration( const std::string& name, 
00176             YYLTYPE& location, unsigned int regs = 0 );
00177           void registerSeperator( YYLTYPE& location );
00178           void fileDeclaration( unsigned int id, 
00179             const std::string& name );
00180           void initializableDeclaration( const std::string& name, 
00181             YYLTYPE& one, YYLTYPE& two );
00182           void textureDeclaration( int token,const std::string& name, 
00183             YYLTYPE& location );
00184           void surfaceDeclaration( int token, 
00185             const std::string &name, YYLTYPE &location);
00186           void samplerDeclaration( int token, 
00187             const std::string &name, YYLTYPE &location);
00188           void argumentDeclaration( const std::string& name, 
00189             YYLTYPE& location );
00190           
00191           void openBrace( YYLTYPE& location );
00192           void closeBrace( YYLTYPE& location );
00193           void returnArgumentListBegin( YYLTYPE& location );
00194           void returnArgumentListEnd( YYLTYPE& location );
00195           void argumentListBegin( YYLTYPE& location );
00196           void argumentListEnd( YYLTYPE& location );
00197           
00198           void functionBegin( YYLTYPE& location );
00199           void functionName( const std::string& name, 
00200             YYLTYPE& location );
00201           void functionDeclaration( YYLTYPE& location, bool body );
00202           
00203           void entry( const std::string& name, YYLTYPE& location );
00204           void entryDeclaration( YYLTYPE& location );
00205           void entryStatement( YYLTYPE& location );
00206           void metadata( const std::string& comment );
00207           
00208           void locationAddress( int token );
00209           void uninitializableDeclaration( const std::string& name );
00210           void location( long long int one, long long int two, 
00211             long long int three );
00212           void label( const std::string& string );
00213           void pragma( const std::string& string );
00214           void labelOperand( const std::string& string );
00215           void nonLabelOperand( const std::string& string, 
00216             YYLTYPE& location, bool invert );
00217           void constantOperand( long long int value );
00218           void constantOperand( unsigned long long int value );
00219           void constantOperand( float value );
00220           void constantOperand( double value );
00221           void indexedOperand( const std::string& name, 
00222             YYLTYPE& location, long long int value );
00223           void addressableOperand( const std::string& name, 
00224             long long int value, YYLTYPE& location, 
00225             bool invert );
00226           void arrayOperand( YYLTYPE& location );
00227           void returnOperand();
00228           void guard( const std::string& name, YYLTYPE& one, 
00229             bool invert );
00230           void guard();
00231           void statementEnd( YYLTYPE& location );
00232           
00233           void tail( bool condition );
00234           void uni( bool condition );
00235           void carry( bool condition );
00236           void modifier( int token );
00237           void atomic( int token );
00238           void volatileFlag( bool condition );
00239           void reduction( int token );
00240           void comparison( int token );
00241           void boolean( int token );
00242           void geometry( int token );
00243           void vote( int token );
00244           void level( int token );
00245           void permute( int token );
00246           void floatingPointMode( int token );
00247           void defaultPermute();
00248           void full();
00249           
00250           void instruction();
00251           void instruction( const std::string& opcode, int dataType );
00252           void instruction( const std::string& opcode );
00253           void tex( int dataType );
00254           void tld4( int dataType );
00255           void callPrototypeName( const std::string& identifier );
00256           void call( const std::string& identifier,
00257             YYLTYPE& location );
00258           void carryIn();
00259           void relaxedConvert( int token, YYLTYPE& location );
00260           void cvtaTo();
00261           void convert( int token, YYLTYPE& location );
00262           void convertC( int token, YYLTYPE& location );
00263           void convertD( int token, YYLTYPE& location );
00264           void barrierOperation( int token, YYLTYPE & location);
00265           void cacheOperation(int token );
00266           void clampOperation(int token);
00267           void formatMode(int token);
00268           void surfaceQuery(int token);
00269           void colorComponent(int token);
00270           
00271           void returnType( int token );
00272           void argumentType( int token );
00273           void callPrototype( const std::string& name, 
00274             const std::string& identifier, YYLTYPE& location );
00275           void callTargets( const std::string& name, 
00276             YYLTYPE& location );
00277       };
00278       
00279       class Exception : public std::exception
00280       {
00281         public:
00282           std::string message;
00283           State::Error error;
00284           
00285         public:
00286           const char* what() const throw();
00287           ~Exception() throw();
00288       };
00289     
00290     private:
00291       State state;
00292     
00293     private:
00294       void checkLabels();
00295       void reset();
00296     
00297     public:
00298       static std::string toString( YYLTYPE&, State& );
00299       static ir::PTXOperand::DataType tokenToDataType( int );
00300       static ir::PTXOperand::VectorIndex tokenToVectorIndex( int );
00301       static ir::PTXInstruction::Vec tokenToVec( int );
00302       static ir::PTXInstruction::Opcode stringToOpcode( std::string );
00303       static ir::PTXOperand::SpecialRegister 
00304         stringToSpecial( std::string );
00305       static ir::PTXInstruction::Modifier tokenToModifier( int );
00306       static ir::PTXInstruction::AddressSpace tokenToAddressSpace( int );
00307       static ir::PTXStatement::Directive tokenToDirective( int );
00308       static ir::PTXInstruction::ReductionOperation 
00309         tokenToReductionOperation( int );
00310       static ir::PTXInstruction::AtomicOperation 
00311         tokenToAtomicOperation( int );
00312       static ir::PTXInstruction::CmpOp tokenToCmpOp( int );
00313       static ir::PTXInstruction::BarrierOperation tokenToBarrierOp(int);
00314       static ir::PTXInstruction::CacheOperation tokenToCacheOperation(int);
00315       static ir::PTXInstruction::ClampOperation tokenToClampOperation(int);
00316       static ir::PTXInstruction::FormatMode tokenToFormatMode(int);
00317       static ir::PTXInstruction::SurfaceQuery tokenToSurfaceQuery(int);
00318       static ir::PTXInstruction::ColorComponent
00319         tokenToColorComponent(int);
00320       static ir::PTXInstruction::BoolOp tokenToBoolOp( int );
00321       static ir::PTXInstruction::Geometry tokenToGeometry( int );
00322       static ir::PTXInstruction::VoteMode tokenToVoteMode( int );
00323       static ir::PTXInstruction::Level tokenToLevel( int );
00324       static ir::PTXInstruction::PermuteMode tokenToPermuteMode( int );
00325       static ir::PTXInstruction::FloatingPointMode
00326         tokenToFloatingPointMode( int);
00327       static ir::PTXStatement::TextureSpace tokenToTextureSpace( int );
00328       static ir::PTXOperand::DataType smallestType( long long int );
00329       static ir::PTXOperand::DataType 
00330         smallestType( long long unsigned int );
00331       
00332     public:
00333       PTXParser();
00334       void parse( std::istream& input, 
00335         ir::Instruction::Architecture language = ir::Instruction::PTX );
00336       ir::Module::StatementVector&& statements(); 
00337   };
00338 
00339 }
00340 
00341 #endif
00342 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines