| 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 "../ErrorHandler.hpp" |
| 13 |
#include "../IR/InstBuilder.hpp" |
| 14 |
#include "../IR/Kernel.hpp" |
| 15 |
#include "../IR/Loc.hpp" |
| 16 |
#include "../IR/Types.hpp" |
| 17 |
#include "Parser.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 |
|
| 37 |
ParseOpts(const Model &model) { swsbEncodeMode = model.getSWSBEncodeMode(); } |
| 38 |
}; |
| 39 |
|
| 40 |
// The primary API for parsing GEN kernels |
| 41 |
Kernel *ParseGenKernel(const Model &model, const char *inp, ErrorHandler &e, |
| 42 |
const ParseOpts &popts); |
| 43 |
|
| 44 |
// using SymbolTableFunc = |
| 45 |
// std::function<bool(const std::string &, ImmVal &)>; |
| 46 |
|
| 47 |
struct ExprParseOpts { |
| 48 |
bool allowFloat = true; // (vs int) |
| 49 |
std::map<std::string, ImmVal> symbols; |
| 50 |
|
| 51 |
ExprParseOpts(bool allowFlts = true) : allowFloat(allowFlts) {} |
| 52 |
}; |
| 53 |
|
| 54 |
// Generic GEN parser that can: |
| 55 |
// - parse constant expressions and knows it's model |
| 56 |
// - etc... |
| 57 |
// |
| 58 |
struct GenParser : public Parser { |
| 59 |
const Model &m_model; |
| 60 |
InstBuilder &m_builder; |
| 61 |
const ParseOpts m_opts; |
| 62 |
|
| 63 |
GenParser(const Model &model, InstBuilder &handler, const std::string &inp, |
| 64 |
ErrorHandler &eh, const ParseOpts &pots); |
| 65 |
|
| 66 |
Platform platform() const { return m_model.platform; } |
| 67 |
|
| 68 |
bool LookupReg(const std::string &str, const RegInfo *®Info, int ®Num); |
| 69 |
bool PeekReg(const RegInfo *®Info, int ®Num); |
| 70 |
bool ConsumeReg(const RegInfo *®Info, int ®Num); |
| 71 |
|
| 72 |
////////////////////////////////////////////////////////////////////// |
| 73 |
// expression parsing |
| 74 |
// &,| |
| 75 |
// <<,>> |
| 76 |
// +,- |
| 77 |
// *,/,% |
| 78 |
// -(unary neg) |
| 79 |
// literals |
| 80 |
// |
| 81 |
// attempts to parse; |
| 82 |
// * returns false if parsing failed with no progress was made |
| 83 |
bool TryParseConstExpr(ImmVal &v); |
| 84 |
bool TryParseConstExpr(const ExprParseOpts &pos, ImmVal &v); |
| 85 |
// |
| 86 |
// parses something, but requires it evaluate to an int |
| 87 |
bool TryParseIntConstExpr(ImmVal &v, const char *forWhat = nullptr); |
| 88 |
bool TryParseIntConstExpr(const ExprParseOpts &pos, ImmVal &v, |
| 89 |
const char *forWhat = nullptr); |
| 90 |
// |
| 91 |
// same as above, but starts at additive level |
| 92 |
bool TryParseIntConstExprAdd(ImmVal &v, const char *forWhat = nullptr); |
| 93 |
bool TryParseIntConstExprAdd(const ExprParseOpts &pos, ImmVal &v, |
| 94 |
const char *forWhat = nullptr); |
| 95 |
// assumes you are here |
| 96 |
// X + E1 - E2 |
| 97 |
// ^ (starts here with 0) |
| 98 |
// don't want to negate the whole term |
| 99 |
// X - 1 - 1 |
| 100 |
// ^^^^^^^ == -2 |
| 101 |
// if nothing showing then we return 0 and success (true) |
| 102 |
bool TryParseIntConstExprAddChain(const ExprParseOpts &pos, ImmVal &v, |
| 103 |
const char *forWhat = nullptr); |
| 104 |
bool TryParseIntConstExprAddChain(ImmVal &v, const char *forWhat = nullptr); |
| 105 |
// |
| 106 |
// same as above, but starts at the primary level |
| 107 |
// so operators require grouping parentheses to start e.g. (1 + 2) |
| 108 |
bool TryParseIntConstExprPrimary(ImmVal &v, const char *forWhat = nullptr); |
| 109 |
bool TryParseIntConstExprPrimary(const ExprParseOpts &pos, ImmVal &v, |
| 110 |
const char *forWhat = nullptr); |
| 111 |
|
| 112 |
protected: |
| 113 |
void ensureIntegral(const Token &t, const ImmVal &v); |
| 114 |
void checkNumTypes(const ImmVal &v1, const Token &t, const ImmVal &v2); |
| 115 |
void checkIntTypes(const ImmVal &v1, const Token &t, const ImmVal &v2); |
| 116 |
|
| 117 |
ImmVal evalBinExpr(const ImmVal &v1, const Token &t, const ImmVal &v2); |
| 118 |
bool parseBitwiseExpr(const ExprParseOpts &po, bool consumed, ImmVal &v); |
| 119 |
bool parseBitwiseANDExpr(const ExprParseOpts &po, bool consumed, ImmVal &v); |
| 120 |
bool parseBitwiseXORExpr(const ExprParseOpts &po, bool consumed, ImmVal &v); |
| 121 |
bool parseBitwiseORExpr(const ExprParseOpts &po, bool consumed, ImmVal &v); |
| 122 |
bool parseShiftExpr(const ExprParseOpts &po, bool consumed, ImmVal &v); |
| 123 |
bool parseAddExpr(const ExprParseOpts &po, bool consumed, ImmVal &v); |
| 124 |
bool parseMulExpr(const ExprParseOpts &po, bool consumed, ImmVal &v); |
| 125 |
bool parseUnExpr(const ExprParseOpts &po, bool consumed, ImmVal &v); |
| 126 |
bool parsePrimaryExpr(const ExprParseOpts &po, bool consumed, ImmVal &v); |
| 127 |
|
| 128 |
private: |
| 129 |
void initSymbolMaps(); |
| 130 |
std::map<std::string, const RegInfo *> m_regmap; |
| 131 |
}; // class GenParser |
| 132 |
} // namespace iga |
| 133 |
#endif |