Changeset 49ae2bc
- Timestamp:
- Nov 3, 2023, 4:27:21 PM (14 months ago)
- Branches:
- master
- Children:
- 3d9d017
- Parents:
- 9cbdc13
- Location:
- src/CodeGen
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/CodeGen/OperatorTable.cc
r9cbdc13 r49ae2bc 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Tue Feb 18 15:55:01 202013 // Update Count : 5 511 // Last Modified By : Andrew Beach 12 // Last Modified On : Fri Nov 3 16:00:00 2023 13 // Update Count : 56 14 14 // 15 15 16 #include <algorithm> // for any_of 17 #include <map> // for map, _Rb_tree_const_iterator, map<>::const_iterator 18 #include <utility> // for pair 19 using namespace std; 16 #include "OperatorTable.h" 20 17 21 #include "OperatorTable.h"22 #include "Common/utility.h"18 #include <cassert> // for assert 19 #include <unordered_map> // for unordered_map 23 20 24 21 namespace CodeGen { 25 const OperatorInfo CodeGen::tableValues[] = {26 // inputName symbol outputName friendlyName type27 { "?[?]", "", "_operator_index", "Index", OT_INDEX },28 { "?{}", "=", "_constructor", "Constructor", OT_CTOR },29 { "^?{}", "", "_destructor", "Destructor", OT_DTOR },30 { "?()", "", "_operator_call", "Call Operator", OT_CALL },31 { "?++", "++", "_operator_postincr", "Postfix Increment", OT_POSTFIXASSIGN },32 { "?--", "--", "_operator_postdecr", "Postfix Decrement", OT_POSTFIXASSIGN },33 { "*?", "*", "_operator_deref", "Dereference", OT_PREFIX },34 { "+?", "+", "_operator_unaryplus", "Plus", OT_PREFIX },35 { "-?", "-", "_operator_unaryminus", "Minus", OT_PREFIX },36 { "~?", "~", "_operator_bitnot", "Bitwise Not", OT_PREFIX },37 { "!?", "!", "_operator_lognot", "Logical Not", OT_PREFIX },38 { "++?", "++", "_operator_preincr", "Prefix Increment", OT_PREFIXASSIGN },39 { "--?", "--", "_operator_predecr", "Prefix Decrement", OT_PREFIXASSIGN },40 { "?\\?", "\\", "_operator_exponential", "Exponentiation", OT_INFIX },41 { "?*?", "*", "_operator_multiply", "Multiplication", OT_INFIX },42 { "?/?", "/", "_operator_divide", "Division", OT_INFIX },43 { "?%?", "%", "_operator_modulus", "Modulo", OT_INFIX },44 { "?+?", "+", "_operator_add", "Addition", OT_INFIX },45 { "?-?", "-", "_operator_subtract", "Substraction", OT_INFIX },46 { "?<<?", "<<", "_operator_shiftleft", "Shift Left", OT_INFIX },47 { "?>>?", ">>", "_operator_shiftright", "Shift Right", OT_INFIX },48 { "?<?", "<", "_operator_less", "Less-than", OT_INFIX },49 { "?>?", ">", "_operator_greater", "Greater-than", OT_INFIX },50 { "?<=?", "<=", "_operator_lessequal", "Less-than-or-Equal", OT_INFIX },51 { "?>=?", ">=", "_operator_greaterequal", "Greater-than-or-Equal", OT_INFIX },52 { "?==?", "==", "_operator_equal", "Equality", OT_INFIX },53 { "?!=?", "!=", "_operator_notequal", "Not-Equal", OT_INFIX },54 { "?&?", "&", "_operator_bitand", "Bitwise And", OT_INFIX },55 { "?^?", "^", "_operator_bitxor", "Bitwise Xor", OT_INFIX },56 { "?|?", "|", "_operator_bitor", "Bitwise Or", OT_INFIX },57 { "?=?", "=", "_operator_assign", "Assignment", OT_INFIXASSIGN },58 { "?\\=?", "\\=", "_operator_expassign", "Exponentiation Assignment", OT_INFIXASSIGN },59 { "?*=?", "*=", "_operator_multassign", "Multiplication Assignment", OT_INFIXASSIGN },60 { "?/=?", "/=", "_operator_divassign", "Division Assignment", OT_INFIXASSIGN },61 { "?%=?", "%=", "_operator_modassign", "Modulo Assignment", OT_INFIXASSIGN },62 { "?+=?", "+=", "_operator_addassign", "Addition Assignment", OT_INFIXASSIGN },63 { "?-=?", "-=", "_operator_subassign", "Substrction Assignment", OT_INFIXASSIGN },64 { "?<<=?", "<<=", "_operator_shiftleftassign", "Shift Left Assignment", OT_INFIXASSIGN },65 { "?>>=?", ">>=", "_operator_shiftrightassign", "Shift Right Assignment", OT_INFIXASSIGN },66 { "?&=?", "&=", "_operator_bitandassign", "Bitwise And Assignment", OT_INFIXASSIGN },67 { "?^=?", "^=", "_operator_bitxorassign", "Bitwise Xor Assignment", OT_INFIXASSIGN },68 { "?|=?", "|=", "_operator_bitorassign", "Bitwise Or Assignment", OT_INFIXASSIGN },69 }; // tableValues70 22 71 std::map< std::string, OperatorInfo > CodeGen::table; 23 static const OperatorInfo tableValues[] = { 24 // inputName symbol outputName friendlyName type 25 { "?[?]", "", "_operator_index", "Index", OT_INDEX }, 26 { "?{}", "=", "_constructor", "Constructor", OT_CTOR }, 27 { "^?{}", "", "_destructor", "Destructor", OT_DTOR }, 28 { "?()", "", "_operator_call", "Call Operator", OT_CALL }, 29 { "?++", "++", "_operator_postincr", "Postfix Increment", OT_POSTFIXASSIGN }, 30 { "?--", "--", "_operator_postdecr", "Postfix Decrement", OT_POSTFIXASSIGN }, 31 { "*?", "*", "_operator_deref", "Dereference", OT_PREFIX }, 32 { "+?", "+", "_operator_unaryplus", "Plus", OT_PREFIX }, 33 { "-?", "-", "_operator_unaryminus", "Minus", OT_PREFIX }, 34 { "~?", "~", "_operator_bitnot", "Bitwise Not", OT_PREFIX }, 35 { "!?", "!", "_operator_lognot", "Logical Not", OT_PREFIX }, 36 { "++?", "++", "_operator_preincr", "Prefix Increment", OT_PREFIXASSIGN }, 37 { "--?", "--", "_operator_predecr", "Prefix Decrement", OT_PREFIXASSIGN }, 38 { "?\\?", "\\", "_operator_exponential", "Exponentiation", OT_INFIX }, 39 { "?*?", "*", "_operator_multiply", "Multiplication", OT_INFIX }, 40 { "?/?", "/", "_operator_divide", "Division", OT_INFIX }, 41 { "?%?", "%", "_operator_modulus", "Modulo", OT_INFIX }, 42 { "?+?", "+", "_operator_add", "Addition", OT_INFIX }, 43 { "?-?", "-", "_operator_subtract", "Substraction", OT_INFIX }, 44 { "?<<?", "<<", "_operator_shiftleft", "Shift Left", OT_INFIX }, 45 { "?>>?", ">>", "_operator_shiftright", "Shift Right", OT_INFIX }, 46 { "?<?", "<", "_operator_less", "Less-than", OT_INFIX }, 47 { "?>?", ">", "_operator_greater", "Greater-than", OT_INFIX }, 48 { "?<=?", "<=", "_operator_lessequal", "Less-than-or-Equal", OT_INFIX }, 49 { "?>=?", ">=", "_operator_greaterequal", "Greater-than-or-Equal", OT_INFIX }, 50 { "?==?", "==", "_operator_equal", "Equality", OT_INFIX }, 51 { "?!=?", "!=", "_operator_notequal", "Not-Equal", OT_INFIX }, 52 { "?&?", "&", "_operator_bitand", "Bitwise And", OT_INFIX }, 53 { "?^?", "^", "_operator_bitxor", "Bitwise Xor", OT_INFIX }, 54 { "?|?", "|", "_operator_bitor", "Bitwise Or", OT_INFIX }, 55 { "?=?", "=", "_operator_assign", "Assignment", OT_INFIXASSIGN }, 56 { "?\\=?", "\\=", "_operator_expassign", "Exponentiation Assignment", OT_INFIXASSIGN }, 57 { "?*=?", "*=", "_operator_multassign", "Multiplication Assignment", OT_INFIXASSIGN }, 58 { "?/=?", "/=", "_operator_divassign", "Division Assignment", OT_INFIXASSIGN }, 59 { "?%=?", "%=", "_operator_modassign", "Modulo Assignment", OT_INFIXASSIGN }, 60 { "?+=?", "+=", "_operator_addassign", "Addition Assignment", OT_INFIXASSIGN }, 61 { "?-=?", "-=", "_operator_subassign", "Substrction Assignment", OT_INFIXASSIGN }, 62 { "?<<=?", "<<=", "_operator_shiftleftassign", "Shift Left Assignment", OT_INFIXASSIGN }, 63 { "?>>=?", ">>=", "_operator_shiftrightassign", "Shift Right Assignment", OT_INFIXASSIGN }, 64 { "?&=?", "&=", "_operator_bitandassign", "Bitwise And Assignment", OT_INFIXASSIGN }, 65 { "?^=?", "^=", "_operator_bitxorassign", "Bitwise Xor Assignment", OT_INFIXASSIGN }, 66 { "?|=?", "|=", "_operator_bitorassign", "Bitwise Or Assignment", OT_INFIXASSIGN }, 67 }; // tableValues 72 68 73 CodeGen::CodeGen() { 74 enum { numOps = sizeof( tableValues ) / sizeof( OperatorInfo ) }; 75 for ( int i = 0; i < numOps; i += 1 ) { 76 table[ tableValues[i].inputName ] = tableValues[i]; 77 } // for 69 enum { numOps = sizeof( tableValues ) / sizeof( OperatorInfo ) }; 70 71 const OperatorInfo * operatorLookup( const std::string & inputName ) { 72 // Static information set up: 73 static std::unordered_map<std::string, const OperatorInfo *> inputTable; 74 if ( inputTable.empty() ) for ( const OperatorInfo & op : tableValues ) { 75 inputTable[ op.inputName ] = &op; 78 76 } 79 77 80 const OperatorInfo * operatorLookup( const string & funcName ) { 81 if ( funcName.find_first_of( "?^*+-!", 0, 1 ) == string::npos ) return nullptr; // prefilter 82 const OperatorInfo * ret = &CodeGen::table.find( funcName )->second; // must be in the table 83 assert( ret ); 84 return ret; 78 if ( inputName.find_first_of( "?^*+-!", 0, 1 ) == std::string::npos ) return nullptr; // prefilter 79 const OperatorInfo * ret = inputTable.find( inputName )->second; 80 // This can only happen if an invalid identifier name has been used. 81 assert( ret ); 82 return ret; 83 } 84 85 bool isOperator( const std::string & inputName ) { 86 return operatorLookup( inputName ) != nullptr; 87 } 88 89 std::string operatorFriendlyName( const std::string & inputName ) { 90 const OperatorInfo * info = operatorLookup( inputName ); 91 if ( info ) return info->friendlyName; 92 return ""; 93 } 94 95 // This is only used in the demangler, so it is smaller (and only maybe slow). 96 const OperatorInfo * operatorLookupByOutput( const std::string & outputName ) { 97 if ( '_' != outputName[0] ) return nullptr; 98 for ( const OperatorInfo & op : tableValues ) { 99 if ( outputName == op.outputName ) { 100 return &op; 101 } 85 102 } 103 return nullptr; 104 } 86 105 87 bool isOperator( const string & funcName ) { 88 return operatorLookup( funcName ) != nullptr; 89 } 106 bool isConstructor( const std::string & inputName ) { 107 const OperatorInfo * info = operatorLookup( inputName ); 108 if ( info ) return info->type == OT_CTOR; 109 return false; 110 } 90 111 91 string operatorFriendlyName( const string & funcName ) {92 const OperatorInfo * info = operatorLookup( funcName );93 if ( info ) return info->friendlyName;94 return "";95 112 bool isDestructor( const std::string & inputName ) { 113 const OperatorInfo * info = operatorLookup( inputName ); 114 if ( info ) return info->type == OT_DTOR; 115 return false; 116 } 96 117 97 bool isConstructor( const string & funcName ) {98 const OperatorInfo * info = operatorLookup( funcName );99 if ( info ) return info->type == OT_CTOR;100 101 118 bool isCtorDtor( const std::string & inputName ) { 119 const OperatorInfo * info = operatorLookup( inputName ); 120 if ( info ) return info->type <= OT_CONSTRUCTOR; 121 return false; 122 } 102 123 103 bool isDestructor( const string & funcName ) {104 const OperatorInfo * info = operatorLookup( funcName );105 if ( info ) return info->type == OT_DTOR;106 107 124 bool isAssignment( const std::string & inputName ) { 125 const OperatorInfo * info = operatorLookup( inputName ); 126 if ( info ) return info->type > OT_CONSTRUCTOR && info->type <= OT_ASSIGNMENT; 127 return false; 128 } 108 129 109 bool isCtorDtor( const string & funcName ) {110 const OperatorInfo * info = operatorLookup( funcName );111 if ( info ) return info->type <= OT_CONSTRUCTOR;112 113 130 bool isCtorDtorAssign( const std::string & inputName ) { 131 const OperatorInfo * info = operatorLookup( inputName ); 132 if ( info ) return info->type <= OT_ASSIGNMENT; 133 return false; 134 } 114 135 115 bool isAssignment( const string & funcName ) {116 const OperatorInfo * info = operatorLookup( funcName );117 if ( info ) return info->type > OT_CONSTRUCTOR && info->type <= OT_ASSIGNMENT;118 return false;119 }120 121 bool isCtorDtorAssign( const string & funcName ) {122 const OperatorInfo * info = operatorLookup( funcName );123 if ( info ) return info->type <= OT_ASSIGNMENT;124 return false;125 }126 127 CodeGen codegen; // initialize singleton package128 136 } // namespace CodeGen 129 137 -
src/CodeGen/OperatorTable.h
r9cbdc13 r49ae2bc 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Sun Feb 16 08:13:34 202013 // Update Count : 2 611 // Last Modified By : Andrew Beach 12 // Last Modified On : Fri Nov 3 14:53:00 2023 13 // Update Count : 27 14 14 // 15 15 … … 17 17 18 18 #include <string> 19 #include <map>20 19 21 20 namespace CodeGen { 22 enum OperatorType {23 OT_CTOR,24 OT_DTOR,25 OT_CONSTRUCTOR = OT_DTOR,26 OT_PREFIXASSIGN,27 OT_POSTFIXASSIGN,28 OT_INFIXASSIGN,29 OT_ASSIGNMENT = OT_INFIXASSIGN,30 OT_CALL,31 OT_PREFIX,32 OT_INFIX,33 OT_POSTFIX,34 OT_INDEX,35 OT_LABELADDRESS,36 OT_CONSTANT37 };38 21 39 struct OperatorInfo { 40 std::string inputName; 41 std::string symbol; 42 std::string outputName; 43 std::string friendlyName; 44 OperatorType type; 45 }; 22 enum OperatorType { 23 OT_CTOR, 24 OT_DTOR, 25 OT_CONSTRUCTOR = OT_DTOR, 26 OT_PREFIXASSIGN, 27 OT_POSTFIXASSIGN, 28 OT_INFIXASSIGN, 29 OT_ASSIGNMENT = OT_INFIXASSIGN, 30 OT_CALL, 31 OT_PREFIX, 32 OT_INFIX, 33 OT_POSTFIX, 34 OT_INDEX, 35 OT_LABELADDRESS, 36 OT_CONSTANT 37 }; 46 38 47 class CodeGen { 48 friend const OperatorInfo * operatorLookup( const std::string & funcName ); 39 struct OperatorInfo { 40 // The Cforall special function name. 41 std::string inputName; 42 // The string used when the operator is used as an operator. 43 std::string symbol; 44 // The base name used in the mangled name. 45 std::string outputName; 46 // Human-readable name of the operator. 47 std::string friendlyName; 48 // The type of operator shows how it is used as an operator. 49 OperatorType type; 50 }; 49 51 50 static const OperatorInfo tableValues[]; 51 static std::map< std::string, OperatorInfo > table; 52 public: 53 CodeGen(); 54 }; // CodeGen 52 // Look up the operator (by inputName), return nullptr if no such operator. 53 const OperatorInfo * operatorLookup( const std::string & inputName ); 54 // Is there an operator with this name? 55 bool isOperator( const std::string & inputName ); 56 // Get the friendlyName of the operator with the inputName 57 std::string operatorFriendlyName( const std::string & inputName ); 58 // Get the OperatorInfo with the given outputName, if one exists. 59 const OperatorInfo * operatorLookupByOutput( const std::string & outputName ); 55 60 56 bool isOperator( const std::string & funcName ); 57 const OperatorInfo * operatorLookup( const std::string & funcName ); 58 std::string operatorFriendlyName( const std::string & funcName ); 61 // Is the operator a constructor, destructor or any form of assignment. 62 // (Last two are "or" combinations of the first three.) 63 bool isConstructor( const std::string & ); 64 bool isDestructor( const std::string & ); 65 bool isAssignment( const std::string & ); 66 bool isCtorDtor( const std::string & ); 67 bool isCtorDtorAssign( const std::string & ); 59 68 60 bool isConstructor( const std::string & );61 bool isDestructor( const std::string & );62 bool isAssignment( const std::string & );63 bool isCtorDtor( const std::string & );64 bool isCtorDtorAssign( const std::string & );65 69 } // namespace CodeGen 66 70
Note: See TracChangeset
for help on using the changeset viewer.