Static Value-Flow Analysis
Loading...
Searching...
No Matches
CFGrammar.h
Go to the documentation of this file.
1//===----- CFGrammar.h -- Context-free grammar --------------------------//
2//
3// SVF: Static Value-Flow Analysis
4//
5// Copyright (C) <2013-> <Yulei Sui>
6//
7
8// This program is free software: you can redistribute it and/or modify
9// it under the terms of the GNU Affero General Public License as published by
10// the Free Software Foundation, either version 3 of the License, or
11// (at your option) any later version.
12
13// This program is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16// GNU Affero General Public License for more details.
17
18// You should have received a copy of the GNU Affero General Public License
19// along with this program. If not, see <http://www.gnu.org/licenses/>.
20//
21//===----------------------------------------------------------------------===//
22
23/*
24 * CFGrammar.h
25 *
26 * Created on: March 5, 2022
27 * Author: Yulei Sui
28 */
29#ifndef CFLGrammar_H_
30#define CFLGrammar_H_
31
32#include <deque>
33#include <vector>
34
35#include "Util/GeneralType.h"
36
37namespace SVF
38{
39
41{
42public:
43 typedef u32_t Kind;
46 typedef struct Symbol
47 {
51
54
56 Symbol(const u32_t& num) : kind(num & 0xFF), attribute((num >> 8 ) & 0xFFFF), variableAttribute((num >> 24)) {}
57
59 operator u32_t()
60 {
61 static_assert(sizeof(struct Symbol)==sizeof(u32_t), "sizeof(struct Symbol)!=sizeof(u32_t)");
62 u32_t num = 0;
63 num += this->variableAttribute << 24;
64 num += this->attribute << 8;
65 num += this->kind;
66 return num;
67 }
68
69 operator u32_t() const
70 {
71 static_assert(sizeof(struct Symbol)==sizeof(u32_t), "sizeof(struct Symbol)!=sizeof(u32_t)");
72 u32_t num = 0;
73 num += this->variableAttribute << 24;
74 num += this->attribute << 8;
75 num += this->kind;
76 return num;
77 }
78
79 bool operator<(const Symbol& rhs)
80 {
81 return u32_t(*this) < u32_t(rhs);
82 }
83
84 void operator=(const u32_t& i)
85 {
86 this->kind = EdgeKindMask & i;
87 this->attribute = i >> EdgeKindMaskBits;
89 }
90
91 void operator=(unsigned long long num)
92 {
93 this->kind = num & 0xFF;
94 this->attribute = (num >> 8 ) & 0xFFFF;
95 this->variableAttribute = num >> 24;
96 }
97
98 bool operator==(const Symbol& s)
99 {
100 return ((this->kind == s.kind) && (this->attribute == s.attribute) && (this->variableAttribute == s.variableAttribute));
101 }
102
103 bool operator==(const Symbol& s) const
104 {
105 return ((kind == s.kind) && (attribute == s.attribute) && (variableAttribute == s.variableAttribute));
106 }
107
108 bool operator!=(const Symbol& s) const
109 {
110 return ! (*this == s) ;
111 }
112
113 bool operator==(const u32_t& i)
114 {
115 return u32_t(*this) == u32_t(i);
116 }
117
118 bool operator==(const Kind& k) const
119 {
120 return (this->kind == k);
121 }
122 } Symbol;
123
125 {
126 public:
127 size_t operator()(const Symbol &s) const
128 {
129 std::hash<u32_t> h;
130 return h(u32_t(s));
131 }
132 };
133
134
136 {
137 size_t operator()(const std::vector<Symbol> &v) const
138 {
139 size_t h = v.size();
140
142 for (const Symbol &t : v)
143 {
144 h ^= hf(t) + 0x9e3779b9 + (h << 6) + (h >> 2);
145 }
146
147 return h;
148 }
149 };
150
151 template<typename Key, typename Value, typename Hash = SymbolHash,
152 typename KeyEqual = std::equal_to<Key>,
153 typename Allocator = std::allocator<std::pair<const Key, Value>>>
155
156 template <typename Key, typename Hash = SymbolVectorHash, typename KeyEqual = std::equal_to<Key>,
157 typename Allocator = std::allocator<Key>>
159
160 typedef std::vector<Symbol> Production;
162
163
165 {
166 return this->nonterminals;
167 }
168
170 {
171 this->nonterminals = nonterminals;
172 }
173
175 {
176 return this->terminals;
177 }
178
180 {
181 this->terminals = terminals;
182 }
183
185 {
186 return this->EBNFSigns;
187 }
188
190 {
191 this->EBNFSigns = EBNFSigns;
192 }
193
195 {
196 return this->rawProductions;
197 }
198
200 {
201 return this->kindToAttrsMap;
202 }
203
204 inline Kind getTotalKind()
205 {
206 return this->totalKind;
207 }
208
209 inline Kind getStartKind()
210 {
211 return this->startKind;
212 }
213
214 inline void setStartKind(Kind startKind)
215 {
216 this->startKind = startKind;
217 }
218
220 {
221 this->totalKind = totalKind;
222 }
223
224 std::string extractKindStrFromSymbolStr(const std::string& symbolStr) const;
225
226 std::string extractAttributeStrFromSymbolStr(const std::string& symbolStr) const;
227
229
231
233
234 Kind strToKind(std::string str) const;
235
236 Symbol strToSymbol(const std::string str) const;
237
238 std::string kindToStr(Kind kind) const;
239
240 std::string symToStrDump(Symbol sym) const;
241
243 {
244 return prod.at(pos);
245 }
246
247 inline const Set<Kind>& getAttrSyms() const
248 {
249 return this->attributeKinds;
250 }
251
253 Kind insertNonterminalKind(std::string const kindStr);
254
257 Kind insertTerminalKind(std::string strLit);
258
259 Symbol insertSymbol(std::string strLit);
260
261 Symbol insertNonTerminalSymbol(std::string strLit);
262
263 Symbol insertTerminalSymbol(std::string strLit);
264
265 Symbol insertEBNFSigns(std::string strLit);
266
267 void insertAttribute(Kind kind, Attribute a);
268
269 inline static Kind getAttributedKind(Attribute attribute, Kind kind)
270 {
271 return ((attribute << EdgeKindMaskBits)| kind );
272 }
273
274 inline static Kind getVariabledKind(VariableAttribute variableAttribute, Kind kind)
275 {
276 return ((variableAttribute << AttributedKindMaskBits) | kind);
277 }
278
279protected:
280 static constexpr unsigned char EdgeKindMaskBits = 8;
281 static constexpr unsigned char AttributedKindMaskBits = 24;
282 static constexpr u64_t EdgeKindMask = (~0ULL) >> (64 - EdgeKindMaskBits);
284private:
292};
293
294class CFGrammar : public GrammarBase
295{
296
297public:
298 CFGrammar();
299
301
302 static inline bool classof(const CFGrammar *)
303 {
304 return true;
305 }
306
307 static inline bool classof(const GrammarBase *node)
308 {
309 return true;
310 }
311
313 {
314 return epsilonProds;
315 }
316
321
326
331
332 const bool hasProdsFromFirstRHS(const Symbol sym) const
333 {
334 auto it = firstRHSToProds.find(sym);
335 return it!=firstRHSToProds.end();
336 }
337
338 const bool hasProdsFromSingleRHS(const Symbol sym) const
339 {
340 auto it = singleRHSToProds.find(sym);
341 return it!=singleRHSToProds.end();
342 }
343
344 const bool hasProdsFromSecondRHS(const Symbol sym) const
345 {
346 auto it = secondRHSToProds.find(sym);
347 return it!=secondRHSToProds.end();
348 }
349
351 {
352 auto it = singleRHSToProds.find(sym);
353 assert(it!=singleRHSToProds.end() && "production (X -> sym) not found for sym!!");
354 return it->second;
355 }
356
358 {
359 auto it = firstRHSToProds.find(sym);
360 assert(it!=firstRHSToProds.end() && "production (X -> sym Y ) not found for sym!!");
361 return it->second;
362 }
363
365 {
366 auto it = secondRHSToProds.find(sym);
367 assert(it!=secondRHSToProds.end() && "production (X -> Y sym) not found for sym!!");
368 return it->second;
369 }
370
371
372 const Symbol& getLHSSymbol(const Production& prod) const
373 {
374 return prod.at(0);
375 }
376
378 {
379 return prod.at(1);
380 }
381
383 {
384 return prod.at(2);
385 }
386
387 void dump() const;
388
389 void dump(std::string fileName) const;
390
391
392 const inline u32_t num_generator()
393 {
394 return newTerminalSubscript++;
395 }
396
397private:
403};
404
410template<class Data>
412{
414 typedef std::deque<Data> DataDeque;
415public:
417
419
420 inline bool empty() const
421 {
422 return data_list.empty();
423 }
424
425 inline bool find(Data data) const
426 {
427 return (data_set.find(data) == data_set.end() ? false : true);
428 }
429
433 inline bool push(Data data)
434 {
435 if (data_set.find(data) == data_set.end())
436 {
437 data_list.push_back(data);
438 data_set.insert(data);
439 return true;
440 }
441 else
442 return false;
443 }
444
448 inline Data pop()
449 {
450 assert(!empty() && "work list is empty");
451 Data data = data_list.front();
452 data_list.pop_front();
453 data_set.erase(data);
454 return data;
455 }
456
460 inline void clear()
461 {
462 data_list.clear();
463 data_set.clear();
464 }
465
466private:
469};
470
471}
472#endif /* CFLGrammar_H_ */
cJSON * a
Definition cJSON.cpp:2560
SymbolMap< Symbol, Productions > firstRHSToProds
Definition CFGrammar.h:400
void dump() const
SymbolMap< Symbol, Productions > & getFirstRHSToProds()
Definition CFGrammar.h:322
const Symbol & getFirstRHSSymbol(const Production &prod) const
Definition CFGrammar.h:377
const Productions & getProdsFromFirstRHS(const Symbol sym) const
Definition CFGrammar.h:357
const Symbol & getSecondRHSSymbol(const Production &prod) const
Definition CFGrammar.h:382
SymbolMap< Symbol, Productions > & getSingleRHSToProds()
Definition CFGrammar.h:317
const u32_t num_generator()
Definition CFGrammar.h:392
SymbolMap< Symbol, Productions > & getSecondRHSToProds()
Definition CFGrammar.h:327
Productions & getEpsilonProds()
Definition CFGrammar.h:312
SymbolSet< Production > epsilonProds
Definition CFGrammar.h:398
const bool hasProdsFromFirstRHS(const Symbol sym) const
Definition CFGrammar.h:332
const bool hasProdsFromSecondRHS(const Symbol sym) const
Definition CFGrammar.h:344
const Symbol & getLHSSymbol(const Production &prod) const
Definition CFGrammar.h:372
SymbolMap< Symbol, Productions > singleRHSToProds
Definition CFGrammar.h:399
static bool classof(const GrammarBase *node)
Definition CFGrammar.h:307
static bool classof(const CFGrammar *)
Methods for support type inquiry through isa, cast, and dyn_cast:
Definition CFGrammar.h:302
const Productions & getProdsFromSecondRHS(const Symbol sym) const
Definition CFGrammar.h:364
SymbolMap< Symbol, Productions > secondRHSToProds
Definition CFGrammar.h:401
const Productions & getProdsFromSingleRHS(const Symbol sym) const
Definition CFGrammar.h:350
u32_t newTerminalSubscript
Definition CFGrammar.h:402
const bool hasProdsFromSingleRHS(const Symbol sym) const
Definition CFGrammar.h:338
GrammarBase::SymbolSet< Data > DataSet
Definition CFGrammar.h:413
bool find(Data data) const
Definition CFGrammar.h:425
DataDeque data_list
work list using std::vector.
Definition CFGrammar.h:468
DataSet data_set
store all data in the work list.
Definition CFGrammar.h:467
bool empty() const
Definition CFGrammar.h:420
std::deque< Data > DataDeque
Definition CFGrammar.h:414
bool push(Data data)
Definition CFGrammar.h:433
size_t operator()(const Symbol &s) const
Definition CFGrammar.h:127
Map< std::string, Kind > terminals
Definition CFGrammar.h:286
std::string extractAttributeStrFromSymbolStr(const std::string &symbolStr) const
void setStartKind(Kind startKind)
Definition CFGrammar.h:214
void setAttributeKinds(const Set< Kind > &attributeKind)
Definition CFGrammar.cpp:52
const Map< Kind, Set< Attribute > > & getKindToAttrsMap() const
Definition CFGrammar.h:199
Symbol insertEBNFSigns(std::string strLit)
static constexpr unsigned char AttributedKindMaskBits
We use the lower 24 bits to denote attributed kind.
Definition CFGrammar.h:281
void setRawProductions(SymbolMap< Symbol, Productions > &rawProductions)
Definition CFGrammar.cpp:42
std::string extractKindStrFromSymbolStr(const std::string &symbolStr) const
void setTotalKind(Kind totalKind)
Definition CFGrammar.h:219
Kind strToKind(std::string str) const
Definition CFGrammar.cpp:57
Symbol insertSymbol(std::string strLit)
Symbol strToSymbol(const std::string str) const
Definition CFGrammar.cpp:76
std::string symToStrDump(Symbol sym) const
Map< std::string, Kind > & getEBNFSigns()
Definition CFGrammar.h:184
Map< std::string, Kind > & getNonterminals()
Definition CFGrammar.h:164
SymbolMap< Symbol, Productions > rawProductions
Definition CFGrammar.h:290
Symbol insertNonTerminalSymbol(std::string strLit)
SymbolMap< Symbol, Productions > & getRawProductions()
Definition CFGrammar.h:194
Map< std::string, Kind > & getTerminals()
Definition CFGrammar.h:174
u32_t VariableAttribute
Definition CFGrammar.h:45
SymbolSet< Production > Productions
Definition CFGrammar.h:161
Symbol insertTerminalSymbol(std::string strLit)
Kind getTotalKind()
Definition CFGrammar.h:204
Map< Kind, Set< Attribute > > kindToAttrsMap
Definition CFGrammar.h:289
Map< std::string, Kind > EBNFSigns
Definition CFGrammar.h:287
const Set< Kind > & getAttrSyms() const
Definition CFGrammar.h:247
Kind getStartKind()
Definition CFGrammar.h:209
Set< Key, Hash, KeyEqual, Allocator > SymbolSet
Definition CFGrammar.h:158
static constexpr u64_t EdgeKindMask
Definition CFGrammar.h:282
void insertAttribute(Kind kind, Attribute a)
Map< Key, Value, Hash, KeyEqual, Allocator > SymbolMap
Definition CFGrammar.h:154
Map< std::string, Kind > nonterminals
Definition CFGrammar.h:285
Kind insertNonterminalKind(std::string const kindStr)
Insert kind to nonterminal and return kind.
Set< Kind > attributeKinds
Map contains Signs' String and associated Symbols.
Definition CFGrammar.h:288
void setKindToAttrsMap(const Map< Kind, Set< Attribute > > &kindToAttrsMap)
Definition CFGrammar.cpp:47
std::string kindToStr(Kind kind) const
Kind insertTerminalKind(std::string strLit)
static Kind getVariabledKind(VariableAttribute variableAttribute, Kind kind)
Definition CFGrammar.h:274
void setEBNFSigns(Map< std::string, Kind > &EBNFSigns)
Definition CFGrammar.h:189
static constexpr unsigned char EdgeKindMaskBits
We use the lower 8 bits to denote edge kind.
Definition CFGrammar.h:280
void setTerminals(Map< std::string, Kind > &terminals)
Definition CFGrammar.h:179
std::vector< Symbol > Production
Definition CFGrammar.h:160
static Kind getAttributedKind(Attribute attribute, Kind kind)
Definition CFGrammar.h:269
Symbol getSymbol(const Production &prod, u32_t pos)
Definition CFGrammar.h:242
void setNonterminals(Map< std::string, Kind > &nonterminals)
Definition CFGrammar.h:169
for isBitcode
Definition BasicTypes.h:70
unsigned long long u64_t
Definition GeneralType.h:69
std::unordered_map< Key, Value, Hash, KeyEqual, Allocator > Map
Definition GeneralType.h:56
llvm::Value Value
LLVM Basic classes.
Definition BasicTypes.h:86
llvm::IRBuilder IRBuilder
Definition BasicTypes.h:76
unsigned u32_t
Definition GeneralType.h:67
size_t operator()(const std::vector< Symbol > &v) const
Definition CFGrammar.h:137
bool operator<(const Symbol &rhs)
Definition CFGrammar.h:79
bool operator==(const Symbol &s)
Definition CFGrammar.h:98
bool operator!=(const Symbol &s) const
Definition CFGrammar.h:108
void operator=(const u32_t &i)
Definition CFGrammar.h:84
bool operator==(const Kind &k) const
Definition CFGrammar.h:118
bool operator==(const Symbol &s) const
Definition CFGrammar.h:103
Symbol(const u32_t &num)
Construct from u32_t move the bit to right field.
Definition CFGrammar.h:56
void operator=(unsigned long long num)
Definition CFGrammar.h:91
bool operator==(const u32_t &i)
Definition CFGrammar.h:113
Symbol()
Default Value for Symbol is 0.
Definition CFGrammar.h:53
VariableAttribute variableAttribute
Definition CFGrammar.h:50
provide extra hash function for std::pair handling
Definition Hash.h:29