Debian Codegraph @

visa/iga/IGALibrary/Backend/BitProcessor.hpp

raw
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_BACKEND_BIT_PROCESSOR_HPP
10 #define IGA_BACKEND_BIT_PROCESSOR_HPP
11
12 #include "../ErrorHandler.hpp"
13 #include "../IR/Instruction.hpp"
14 #include "../strings.hpp"
15
16 #include <cstdarg>
17 #include <cstdint>
18
19 namespace iga {
20 // This class is a super class of all traversals over a set of encoded
21 // bits. It is an adapter to ErrorHandler, but extends that functionality
22 // with the notion of a current PC. The PC may be reset via
23 // restart(), advanced via advancePc(...), and changed via setPc.
24 // Subclasses should be sensitive to fatal conditions that will propagate
25 // up via exception.
26 //
27 // This traversal supports three levels of errors.
28 // - warnings: potentially non-fatal messages; tools may just report
29 // these to stderr and exit 0 still
30 // - errors: indicate a condition where tools will want to exit non-zero
31 // and potentially stop processing early. The underlying BitProcessor
32 // will continue
33 // - fatal: a panic state that logs an error and throws an exception
34 // to exit processing
35 class BitProcessor {
36 private:
37 ErrorHandler &m_errorHandler;
38 int32_t m_currentPc;
39 const Instruction *m_currInst;
40
41 protected:
42 ErrorHandler &errorHandler() { return m_errorHandler; }
43
44 public:
45 BitProcessor(ErrorHandler &eh)
46 : m_errorHandler(eh), m_currentPc(0), m_currInst(nullptr) {}
47
48 int32_t currentPc() const { return m_currentPc; }
49 const Instruction *currentInst() const { return m_currInst; }
50
51 void restart() {
52 m_currentPc = 0;
53 m_currInst = nullptr;
54 }
55 void advance(int32_t by, const Instruction *inst) {
56 setPc(m_currentPc + by);
57 setCurrInst(inst);
58 }
59 void advancePc(int32_t by) { setPc(m_currentPc + by); }
60 void setCurrInst(const Instruction *inst) { m_currInst = inst; }
61 void setPc(int32_t pc) { m_currentPc = pc; }
62 Loc defaultLoc() const {
63 return m_currInst ? m_currInst->getLoc() : m_currentPc;
64 }
65
66 ///////////////////////////////////////////////////////////////////////
67 // warnings
68 template <typename... Ts> void warningT(Ts... ts) {
69 warningAtT(defaultLoc(), iga::format(ts...));
70 }
71 template <typename... Ts> void warningAtT(const Loc &loc, Ts... ts) {
72 warningAtS(loc, iga::format(ts...));
73 }
74 void warningAtS(const Loc &loc, std::string msg);
75
76 ///////////////////////////////////////////////////////////////////////
77 // errors
78 template <typename... Ts> void errorT(Ts... ts) {
79 errorAtT(defaultLoc(), iga::format(ts...));
80 }
81 template <typename... Ts> void errorAtT(const Loc &loc, Ts... ts) {
82 errorAtS(loc, iga::format(ts...));
83 }
84 void errorAtS(const Loc &loc, std::string msg);
85
86 ///////////////////////////////////////////////////////////////////////
87 // fatals stop processing (unless exceptions are disabled)
88 template <typename... Ts> void fatalT(Ts... ts) {
89 fatalAtS(defaultLoc(), iga::format(ts...));
90 }
91 template <typename... Ts> void fatalAtT(const Loc &loc, Ts... ts) {
92 fatalAtS(loc, iga::format(ts...));
93 }
94 void fatalAtS(const Loc &loc, std::string msg);
95
96 ///////////////////////////////////////////////////////////////////////
97 bool hasFatalError() const { return m_errorHandler.hasFatalError(); }
98 size_t getErrorCount() const { return m_errorHandler.getErrors().size(); }
99 };
100
101 // can be intialized in .h, but this is a work around for
102 // gcc 4.8.3 bug on linux 64bit debug
103 const static int32_t COMPACTION_CONTROL = 29;
104 const static int32_t COMPACTED_SIZE = 8;
105 const static int32_t UNCOMPACTED_SIZE = 16;
106 } // namespace iga
107 #endif // IGA_BACKEND_BIT_PROCESSOR_HPP