1 /*========================== begin_copyright_notice ============================ 2 3 Copyright (C) 2017-2021 Intel Corporation 4 5 SPDX-License-Identifier: MIT 6 7 ============================= end_copyright_notice ===========================*/ 8 9 #ifndef IGA_FRONTEND_KERNELPARSER 10 #define IGA_FRONTEND_KERNELPARSER 11 12 #include "Parser.hpp" 13 #include "../IR/InstBuilder.hpp" 14 #include "../IR/Kernel.hpp" 15 #include "../IR/Loc.hpp" 16 #include "../IR/Types.hpp" 17 #include "../ErrorHandler.hpp" 18 19 // #include <functional> 20 #include <map> 21 #include <string> 22 23 namespace iga { 24 struct ParseOpts { 25 // Enables certain IsaAsm-era directives 26 // .default_execution_size(...) 27 // .default_register_type(...) 28 bool supportLegacyDirectives = false; 29 // emits warnings about deprecated syntax 30 bool deprecatedSyntaxWarnings = true; 31 SWSB_ENCODE_MODE swsbEncodeMode = SWSB_ENCODE_MODE::SWSBInvalidMode; 32 33 // sets the maximum number of fatal syntax errors allowable 34 // before we give up on the parse 35 size_t maxSyntaxErrors = 3; 36 ParseOptsiga::ParseOpts37 ParseOpts(const Model &model) { 38 swsbEncodeMode = model.getSWSBEncodeMode(); 39 } 40 }; 41 42 // The primary API for parsing GEN kernels 43 Kernel *ParseGenKernel( 44 const Model &model, 45 const char *inp, 46 ErrorHandler &e, 47 const ParseOpts &popts); 48 49 // using SymbolTableFunc = 50 // std::function<bool(const std::string &, ImmVal &)>; 51 52 struct ExprParseOpts { 53 bool allowFloat = true; // (vs int) 54 std::map<std::string,ImmVal> symbols; 55 ExprParseOptsiga::ExprParseOpts56 ExprParseOpts(bool allowFlts = true) 57 : allowFloat(allowFlts) 58 { 59 } 60 }; 61 62 // Generic GEN parser that can: 63 // - parse constant expressions and knows it's model 64 // - etc... 65 // 66 struct GenParser : public Parser 67 { 68 const Model& m_model; 69 InstBuilder& m_builder; 70 const ParseOpts m_opts; 71 72 73 GenParser( 74 const Model &model, 75 InstBuilder &handler, 76 const std::string &inp, 77 ErrorHandler &eh, 78 const ParseOpts &pots); 79 platformiga::GenParser80 Platform platform() const {return m_model.platform;} 81 82 bool LookupReg( 83 const std::string &str, 84 const RegInfo *®Info, 85 int ®Num); 86 bool PeekReg(const RegInfo*& regInfo, int& regNum); 87 bool ConsumeReg(const RegInfo*& regInfo, int ®Num); 88 89 ////////////////////////////////////////////////////////////////////// 90 // expression parsing 91 // &,| 92 // <<,>> 93 // +,- 94 // *,/,% 95 // -(unary neg) 96 // literals 97 // 98 // attempts to parse; 99 // * returns false if parsing failed with no progress was made 100 bool TryParseConstExpr(ImmVal &v); 101 bool TryParseConstExpr(const ExprParseOpts &pos, ImmVal &v); 102 // 103 // parses something, but requires it evaluate to an int 104 bool TryParseIntConstExpr(ImmVal &v, 105 const char *forWhat = nullptr); 106 bool TryParseIntConstExpr( 107 const ExprParseOpts &pos, ImmVal &v, 108 const char *forWhat = nullptr); 109 // 110 // same as above, but starts at additive level 111 bool TryParseIntConstExprAdd(ImmVal &v, 112 const char *forWhat = nullptr); 113 bool TryParseIntConstExprAdd( 114 const ExprParseOpts &pos, ImmVal &v, 115 const char *forWhat = nullptr); 116 // assumes you are here 117 // X + E1 - E2 118 // ^ (starts here with 0) 119 // don't want to negate the whole term 120 // X - 1 - 1 121 // ^^^^^^^ == -2 122 // if nothing showing then we return 0 and success (true) 123 bool TryParseIntConstExprAddChain( 124 const ExprParseOpts &pos, ImmVal &v, 125 const char *forWhat = nullptr); 126 bool TryParseIntConstExprAddChain(ImmVal &v, 127 const char *forWhat = nullptr); 128 // 129 // same as above, but starts at the primary level 130 // so operators require grouping parentheses to start e.g. (1 + 2) 131 bool TryParseIntConstExprPrimary(ImmVal &v, 132 const char *forWhat = nullptr); 133 bool TryParseIntConstExprPrimary( 134 const ExprParseOpts &pos, ImmVal &v, 135 const char *forWhat = nullptr); 136 137 protected: 138 void ensureIntegral(const Token &t, const ImmVal &v); 139 void checkNumTypes(const ImmVal &v1, const Token &t, const ImmVal &v2); 140 void checkIntTypes(const ImmVal &v1, const Token &t, const ImmVal &v2); 141 142 ImmVal evalBinExpr(const ImmVal &v1, const Token &t, const ImmVal &v2); 143 bool parseBitwiseExpr(const ExprParseOpts &po, bool consumed, ImmVal &v); 144 bool parseBitwiseANDExpr(const ExprParseOpts &po, bool consumed, ImmVal &v); 145 bool parseBitwiseXORExpr(const ExprParseOpts &po, bool consumed, ImmVal &v); 146 bool parseBitwiseORExpr(const ExprParseOpts &po, bool consumed, ImmVal &v); 147 bool parseShiftExpr(const ExprParseOpts &po, bool consumed, ImmVal &v); 148 bool parseAddExpr(const ExprParseOpts &po, bool consumed, ImmVal &v); 149 bool parseMulExpr(const ExprParseOpts &po, bool consumed, ImmVal &v); 150 bool parseUnExpr(const ExprParseOpts &po, bool consumed, ImmVal &v); 151 bool parsePrimaryExpr(const ExprParseOpts &po, bool consumed, ImmVal &v); 152 private: 153 void initSymbolMaps(); 154 std::map<std::string,const RegInfo*> m_regmap; 155 }; // class GenParser 156 } // iga:: 157 #endif 158