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 /*========================== begin_copyright_notice ============================
10 
11 This file is distributed under the University of Illinois Open Source License.
12 See LICENSE.TXT for details.
13 
14 ============================= end_copyright_notice ===========================*/
15 
16 /*========================== begin_copyright_notice ============================
17 
18 Copyright (C) 2014 Advanced Micro Devices, Inc. All rights reserved.
19 
20 Permission is hereby granted, free of charge, to any person obtaining a
21 copy of this software and associated documentation files (the "Software"),
22 to deal with the Software without restriction, including without limitation
23 the rights to use, copy, modify, merge, publish, distribute, sublicense,
24 and/or sell copies of the Software, and to permit persons to whom the
25 Software is furnished to do so, subject to the following conditions:
26 
27 Redistributions of source code must retain the above copyright notice,
28 this list of conditions and the following disclaimers.
29 Redistributions in binary form must reproduce the above copyright notice,
30 this list of conditions and the following disclaimers in the documentation
31 and/or other materials provided with the distribution.
32 Neither the names of Advanced Micro Devices, Inc., nor the names of its
33 contributors may be used to endorse or promote products derived from this
34 Software without specific prior written permission.
35 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38 CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH
41 THE SOFTWARE.
42 
43 ============================= end_copyright_notice ===========================*/
44 
45 // This file defines Word class for SPIR-V.
46 
47 #ifndef SPIRVSTREAM_H
48 #define SPIRVSTREAM_H
49 
50 #include "SPIRVDebug.h"
51 #include "SPIRVModule.h"
52 #include "SPIRVExtInst.h"
53 #include <algorithm>
54 #include <cstdint>
55 #include <iostream>
56 #include <iterator>
57 #include <vector>
58 #include <string>
59 
60 namespace igc_spv{
61 
62 class SPIRVFunction;
63 class SPIRVBasicBlock;
64 
65 class SPIRVDecoder {
66 public:
SPIRVDecoder(std::istream & InputStream,SPIRVModule & Module)67   SPIRVDecoder(std::istream& InputStream, SPIRVModule& Module)
68     :IS(InputStream), M(Module), WordCount(0), OpCode(OpNop),
69      Scope(NULL){}
70   SPIRVDecoder(std::istream& InputStream, SPIRVFunction& F);
71   SPIRVDecoder(std::istream& InputStream, SPIRVBasicBlock &BB);
72 
73   void setScope(SPIRVEntry *);
74   bool getWordCountAndOpCode();
75   SPIRVEntry *getEntry();
76   void validate()const;
77 
78   std::istream &IS;
79   SPIRVModule &M;
80   SPIRVWord WordCount;
81   Op OpCode;
82   SPIRVEntry *Scope; // A function or basic block
83 
84   std::vector<SPIRVEntry*>
85       getContinuedInstructions(const Op ContinuedOpCode);
86 };
87 
88 template<typename T>
89 const SPIRVDecoder&
90 DecodeBinary(const SPIRVDecoder& I, T &V);
91 
92 template<typename T>
93 const SPIRVDecoder&
94 operator>>(const SPIRVDecoder& I, T &V) {
95   return DecodeBinary(I, V);
96 }
97 
98 template<typename T>
99 const SPIRVDecoder&
100 operator>>(const SPIRVDecoder& I, T *&P) {
101   SPIRVId Id;
102   I >> Id;
103   P = static_cast<T*>(I.M.getEntry(Id));
104   return I;
105 }
106 
107 template<typename IterTy>
108 const SPIRVDecoder&
109 operator>>(const SPIRVDecoder& Decoder, const std::pair<IterTy,IterTy> &Range) {
110   for (IterTy I = Range.first, E = Range.second; I != E; ++I)
111     Decoder >> *I;
112   return Decoder;
113 }
114 
115 template<typename T>
116 const SPIRVDecoder&
117 operator>>(const SPIRVDecoder& I, std::vector<T> &V) {
118   for (size_t i = 0, e = V.size(); i != e; ++i)
119     I >> V[i];
120   return I;
121 }
122 
123 #define SPIRV_DEC_DEC(Type) \
124     const SPIRVDecoder& operator>>(const SPIRVDecoder& I, Type &V);
125 
126 SPIRV_DEC_DEC(Op)
127 SPIRV_DEC_DEC(Decoration)
128 SPIRV_DEC_DEC(OCLExtOpKind)
129 SPIRV_DEC_DEC(OCLExtOpDbgKind)
130 
131 const SPIRVDecoder& operator>>(const SPIRVDecoder&I, std::string& Str);
132 
133 } // namespace igc_spv
134 #endif
135