ETISS 0.11.2
ExtendableTranslatingInstructionSetSimulator(version0.11.2)
Loading...
Searching...
No Matches
CodePart.h
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-3-Clause
2//
3// This file is part of ETISS. It is licensed under the BSD 3-Clause License; you may not use this file except in
4// compliance with the License. You should have received a copy of the license along with this project. If not, see the
5// LICENSE file.
13#ifndef ETISS_INCLUDE_CODEPART_H_
14#define ETISS_INCLUDE_CODEPART_H_
15
16#include <string>
17#include <set>
18#include <list>
19#include <vector>
20#include <map>
21#include <sstream>
22#include <iostream>
23
24#include "etiss/jit/types.h"
25#include "etiss/Misc.h"
26
27namespace etiss
28{
29
38{
39 public:
40 inline RegisterPart() : name(""), bits(0), regWidth(0) {}
47 inline RegisterPart(const std::string &name, unsigned registerWidth, etiss::uintMax bits = (uintMax)((intMax)-1))
48 : name(name), regWidth(registerWidth)
49 {
50 uintMax mask = 0;
51 for (unsigned i = 0; i < regWidth; i++)
52 {
53 mask |= ((uintMax)1) << i;
54 }
55 this->bits = bits & mask;
56 }
57 inline RegisterPart(const RegisterPart &cpy)
58 {
59 this->name = cpy.name;
60 this->bits = cpy.bits;
61 this->regWidth = cpy.regWidth;
62 }
63
64 public:
68 inline bool matches(const RegisterPart &rp) const
69 {
70 if (this->name != rp.name)
71 return false;
72#if DEBUG
73 if (this->regWidth != rp.regWidth && !(isEmpty() || rp.isEmpty()))
74 {
75 std::cout << "Serve Warning: RegisterPart definitions exist with same name (=same register) but different "
76 "register width values"
77 << std::endl;
78 return false;
79 }
80#endif
81 return this->bits == rp.bits;
82 }
86 inline bool contains(const RegisterPart &rp) const
87 {
88 if (this->name != rp.name)
89 return false;
90#if DEBUG
91 if (this->regWidth != rp.regWidth && !(isEmpty() || rp.isEmpty()))
92 {
93 std::cout << "Serve Warning: RegisterPart definitions exist with same name (=same register) but different "
94 "register width values"
95 << std::endl;
96 return false;
97 }
98#endif
99 return (this->bits & rp.bits) == rp.bits; // this->bits has every bit of rp.bits
100 }
104 inline void applyShadow(const RegisterPart &rp) const
105 {
106 if (name == rp.name)
107 bits = bits & (~rp.bits);
108 }
112 inline void merge(const RegisterPart &rp) const
113 {
114 if (name == rp.name)
115 bits = bits | rp.bits;
116 }
120 inline void intersect(const RegisterPart &rp) const
121 {
122 if (name == rp.name)
123 bits = bits & rp.bits;
124 }
125 inline bool maskedBy(const RegisterPart &rp) const { return rp.contains(*this); }
126 inline const std::string &getName() const { return name; }
127 inline const etiss::uintMax &getAffectedBits() const { return bits; }
128 inline const unsigned &getRegisterWidth() const { return regWidth; }
132 inline bool isEmpty() const { return bits == 0 || name.length() == 0; }
133 inline bool operator<(const RegisterPart &other) const
134 {
135 if (name < other.name)
136 return true;
137#if DEBUG
138 if (regWidth < other.regWidth && !(isEmpty() || other.isEmpty()))
139 {
140 std::cout << "Serve Warning: RegisterPart definitions exist with same name (=same register) but different "
141 "register width values"
142 << std::endl;
143 return true;
144 }
145#endif
146 return bits < other.bits;
147 }
151 inline bool operator==(const RegisterPart &other) const { return matches(other); }
152
153 private:
154 std::string name;
155 mutable etiss::uintMax bits;
156 unsigned regWidth;
157};
158
163{
170 {
171 bool operator()(const RegisterPart &lhs, const RegisterPart &rhs) const
172 {
173 return lhs.getName() < rhs.getName();
174 }
175 };
176
177 public:
178 inline RegisterSet() {}
179 inline RegisterSet(const RegisterSet &rs) { set_ = rs.set_; }
184 inline void add(const RegisterPart &rp)
185 {
186 if (rp.isEmpty())
187 return;
188 std::set<RegisterPart, lex_compare>::iterator iter = set_.find(rp);
189 if (iter != set_.end())
190 {
191 (*iter).merge(rp);
192 if (iter->isEmpty())
193 {
194 set_.erase(iter);
195 }
196 }
197 else
198 {
199 set_.insert(rp);
200 }
201 }
205 inline void add(const std::string &name, unsigned registerWidth, etiss::uintMax bits = (uintMax)((intMax)-1))
206 {
207 add(RegisterPart(name, registerWidth, bits));
208 }
212 inline void applyShadow(const RegisterSet &rs)
213 {
214 std::set<RegisterPart, lex_compare>::iterator iter = set_.begin();
215 std::set<RegisterPart, lex_compare>::const_iterator oiter;
216 while (iter != set_.end())
217 {
218 oiter = rs.set_.find(*iter);
219 if (oiter != rs.set_.end())
220 {
221 iter->applyShadow(*oiter);
222 if (iter->isEmpty())
223 {
224 set_.erase(iter++);
225 }
226 else
227 {
228 iter++;
229 }
230 }
231 else
232 {
233 iter++;
234 }
235 }
236 }
241 inline void merge(const RegisterSet &rs)
242 {
243 std::set<RegisterPart, lex_compare>::const_iterator iter = rs.set_.begin();
244 while (iter != rs.set_.end())
245 {
246 add(*iter);
247 iter++;
248 }
249 }
253 inline void intersect(const RegisterSet &rs)
254 {
255 std::set<RegisterPart, lex_compare>::iterator iter = set_.begin();
256 while (iter != set_.end())
257 {
258 std::set<RegisterPart, lex_compare>::const_iterator f = rs.set_.find(*iter);
259 if (f != rs.set_.end())
260 {
261 iter->intersect(*f);
262 if (iter->isEmpty())
263 {
264 set_.erase(iter++);
265 }
266 else
267 {
268 iter++;
269 }
270 }
271 else
272 {
273 set_.erase(iter++);
274 }
275 }
276 }
280 inline bool disjoint(const RegisterSet &rs) const
281 {
282 std::set<RegisterPart, lex_compare>::const_iterator iter = set_.begin();
283 while (iter != set_.end())
284 {
285 std::set<RegisterPart, lex_compare>::const_iterator f = rs.set_.find(*iter);
286 if (f != rs.set_.end())
287 {
288 return false;
289 }
290 iter++;
291 }
292 return true;
293 }
297 inline bool maskedBy(const RegisterSet &rs) const
298 {
299 std::set<RegisterPart, lex_compare>::const_iterator iter = set_.begin();
300 while (iter != set_.end())
301 {
302 std::set<RegisterPart, lex_compare>::const_iterator oiter = rs.set_.find(*iter);
303 if (oiter != rs.set_.end())
304 {
305 if (!iter->maskedBy(*oiter))
306 {
307 return false;
308 }
309 }
310 else
311 {
312 return false;
313 }
314 iter++;
315 }
316 return true;
317 }
318 inline bool isEmpty() { return set_.empty(); }
319 inline void clear() { set_.clear(); }
323 inline std::string _dbg_print() const
324 {
325 std::stringstream ss;
326 std::set<RegisterPart, lex_compare>::const_iterator iter = set_.begin();
327 while (iter != set_.end())
328 {
329 ss << iter->getName() << ": " << std::hex << iter->getAffectedBits() << "\n";
330 iter++;
331 }
332 return ss.str();
333 }
334
335 private:
336 std::set<RegisterPart, lex_compare> set_;
337};
338
348{
349 public:
367 inline CodePart(const std::string &code, const RegisterSet &registerDependencies = RegisterSet(),
368 const RegisterSet &affectedRegisters = RegisterSet())
369 : code_(code)
370 , registerDependencies_(registerDependencies)
371 , affectedRegisters_(affectedRegisters)
373 {
374 }
377 inline std::string &getCode() { return code_; }
378 inline std::string &code() { return code_; }
379 inline const std::string &code() const { return code_; }
381 inline const RegisterSet &getAffectedRegisters() const { return affectedRegisters_; }
382 inline const std::string &getCode() const { return code_; }
383 inline const bool &fullRegistersDependency() const { return flag_requireAll_; }
384
385 private:
386 std::string code_;
390};
391
393class Translation;
399{
400
401 public:
402 inline CodeSet() {}
412 inline void append(const CodePart &part, CodePart::TYPE type)
413 {
414 switch (type)
415 {
417 pindbgretreq_parts_.push_front(part);
418 break;
420 inireq_parts_.push_front(part);
421 break;
423 midopt_parts_.push_front(part);
424 break;
426 appreq_parts_.push_front(part);
427 break;
429 appopt_parts_.push_front(part);
430 break;
432 appretreq_parts_.push_front(part);
433 break;
434 }
435 }
437 {
438 switch (type)
439 {
441 pindbgretreq_parts_.emplace_front();
442 return pindbgretreq_parts_.front();
444 inireq_parts_.emplace_front();
445 return inireq_parts_.front();
447 midopt_parts_.emplace_front();
448 return midopt_parts_.front();
450 appreq_parts_.emplace_front();
451 return appreq_parts_.front();
453 appopt_parts_.emplace_front();
454 return appopt_parts_.front();
456 appretreq_parts_.emplace_front();
457 return appretreq_parts_.front();
458 default:
459 std::cout << "ERROR: etiss::CodePart::append called with invalid etiss::CodePart::TYPE parameter"
460 << std::endl;
461 appretreq_parts_.emplace_front();
462 return appretreq_parts_.front();
463 }
464 }
466 {
467 switch (type)
468 {
470 pindbgretreq_parts_.emplace_back();
471 return pindbgretreq_parts_.back();
473 inireq_parts_.emplace_back();
474 return inireq_parts_.back();
476 midopt_parts_.emplace_back();
477 return midopt_parts_.back();
479 appreq_parts_.emplace_back();
480 return appreq_parts_.back();
482 appopt_parts_.emplace_back();
483 return appopt_parts_.back();
485 appretreq_parts_.emplace_back();
486 return appretreq_parts_.back();
487 default:
488 std::cout << "ERROR: etiss::CodePart::prepend called with invalid etiss::CodePart::TYPE parameter"
489 << std::endl;
490 appretreq_parts_.emplace_back();
491 return appretreq_parts_.back();
492 }
493 }
494
495 private:
496 static void writeCodeParts(std::string &code, const std::list<CodePart> &parts, bool required, RegisterSet &ignored,
497 bool intersect);
498
499 public:
504 std::string toString(RegisterSet &ignored, bool &ok) const;
505
506 private:
507 std::list<CodePart> pindbgretreq_parts_;
508 std::list<CodePart> inireq_parts_;
509 std::list<CodePart> midopt_parts_;
510 std::list<CodePart> appreq_parts_;
511 std::list<CodePart> appopt_parts_;
512 std::list<CodePart> appretreq_parts_;
513};
514
532{
533 friend class Translation;
534
535 public:
539 class Line
540 {
541 friend class CodeBlock;
542
543 private:
544 public:
545 Line(const Line &line) : codeset_(line.codeset_), addr_(line.addr_) {}
546 inline Line(etiss::uint64 addr) : addr_(addr) {}
547 inline CodeSet &getCodeSet() { return codeset_; }
548 inline const etiss::uint64 &getAddress() { return addr_; }
549
550 private:
553 etiss::uint64 addr_;
554 };
555
556 public:
557 inline CodeBlock(etiss::uint64 startindex) : startindex_(startindex) {}
558 inline void reserve(int num) { lines_.reserve(num); }
559 inline Line &get(unsigned index) { return lines_[index]; }
560 inline Line &append(etiss::uint64 addr)
561 {
562 lines_.push_back(Line(addr));
563 return lines_.back();
564 }
565 inline unsigned length() const { return (unsigned)lines_.size(); }
566 inline std::set<std::string> &fileglobalCode() { return fileglobal_code; }
567 inline std::set<std::string> &functionglobalCode() { return functionglobal_code; }
568 void toCode(std::stringstream &out, const std::string &funcname, std::set<std::string> *fileglobalcode);
569
570 private:
571 std::vector<Line> lines_;
572 etiss::uint64 startindex_;
573 etiss::uint64 endaddress_;
574 std::set<std::string> fileglobal_code;
575 std::set<std::string> functionglobal_code;
576};
577} // namespace etiss
578#endif
general configuration and logging
equivalent of a translated instruction
Definition CodePart.h:540
etiss::uint64 addr_
Definition CodePart.h:553
Line(const Line &line)
Definition CodePart.h:545
const etiss::uint64 & getAddress()
Definition CodePart.h:548
Line(etiss::uint64 addr)
Definition CodePart.h:546
CodeSet & getCodeSet()
Definition CodePart.h:547
A list of CodeSets.
Definition CodePart.h:532
unsigned length() const
Definition CodePart.h:565
void toCode(std::stringstream &out, const std::string &funcname, std::set< std::string > *fileglobalcode)
Definition CodePart.cpp:108
Line & get(unsigned index)
Definition CodePart.h:559
std::set< std::string > functionglobal_code
Definition CodePart.h:575
std::vector< Line > lines_
Definition CodePart.h:571
std::set< std::string > & functionglobalCode()
Definition CodePart.h:567
void reserve(int num)
Definition CodePart.h:558
etiss::uint64 endaddress_
Definition CodePart.h:573
CodeBlock(etiss::uint64 startindex)
Definition CodePart.h:557
std::set< std::string > & fileglobalCode()
Definition CodePart.h:566
etiss::uint64 startindex_
Definition CodePart.h:572
Line & append(etiss::uint64 addr)
Definition CodePart.h:560
std::set< std::string > fileglobal_code
Definition CodePart.h:574
Contains a small code snipped.
Definition CodePart.h:348
const std::string & code() const
Definition CodePart.h:379
std::string code_
Definition CodePart.h:386
bool flag_requireAll_
Definition CodePart.h:389
TYPE
defines position,optionality and the ability to return a code for a CodePart.
Definition CodePart.h:358
@ APPENDEDRETURNINGREQUIRED
Definition CodePart.h:364
@ PREINITIALDEBUGRETURNING
Definition CodePart.h:359
std::string & getCode()
Definition CodePart.h:377
const bool & fullRegistersDependency() const
Definition CodePart.h:383
std::string & code()
Definition CodePart.h:378
CodePart(const std::string &code, const RegisterSet &registerDependencies=RegisterSet(), const RegisterSet &affectedRegisters=RegisterSet())
Definition CodePart.h:367
RegisterSet & getRegisterDependencies()
Definition CodePart.h:375
const RegisterSet & getRegisterDependencies() const
Definition CodePart.h:380
const RegisterSet & getAffectedRegisters() const
Definition CodePart.h:381
RegisterSet & getAffectedRegisters()
Definition CodePart.h:376
RegisterSet registerDependencies_
Definition CodePart.h:387
const std::string & getCode() const
Definition CodePart.h:382
RegisterSet affectedRegisters_
Definition CodePart.h:388
A set of CodeParts.
Definition CodePart.h:399
CodePart & append(CodePart::TYPE type)
Definition CodePart.h:436
std::string toString(RegisterSet &ignored, bool &ok) const
writes the contained CodeParts as needed with respect to the given RegisterSet of bits that are not r...
Definition CodePart.cpp:82
std::list< CodePart > appretreq_parts_
Definition CodePart.h:512
std::list< CodePart > midopt_parts_
Definition CodePart.h:509
std::list< CodePart > pindbgretreq_parts_
Definition CodePart.h:507
std::list< CodePart > appreq_parts_
Definition CodePart.h:510
CodeSet(const CodeSet &cs)
Definition CodePart.h:403
std::list< CodePart > inireq_parts_
Definition CodePart.h:508
void append(const CodePart &part, CodePart::TYPE type)
Definition CodePart.h:412
static void writeCodeParts(std::string &code, const std::list< CodePart > &parts, bool required, RegisterSet &ignored, bool intersect)
Definition CodePart.cpp:19
CodePart & prepend(CodePart::TYPE type)
Definition CodePart.h:465
std::list< CodePart > appopt_parts_
Definition CodePart.h:511
abstract description of needed or affected register bits.
Definition CodePart.h:38
void intersect(const RegisterPart &rp) const
this is not a const function in the usual sense.
Definition CodePart.h:120
std::string name
Definition CodePart.h:154
RegisterPart(const RegisterPart &cpy)
Definition CodePart.h:57
const unsigned & getRegisterWidth() const
Definition CodePart.h:128
bool contains(const RegisterPart &rp) const
check if passed RegisterPart is a subset of relevant bits of the same register
Definition CodePart.h:86
const std::string & getName() const
Definition CodePart.h:126
const etiss::uintMax & getAffectedBits() const
Definition CodePart.h:127
bool matches(const RegisterPart &rp) const
check for equality
Definition CodePart.h:68
void applyShadow(const RegisterPart &rp) const
this is not a const function in the usual sense.
Definition CodePart.h:104
void merge(const RegisterPart &rp) const
this is not a const function in the usual sense.
Definition CodePart.h:112
RegisterPart(const std::string &name, unsigned registerWidth, etiss::uintMax bits=(uintMax)((intMax) -1))
Definition CodePart.h:47
bool isEmpty() const
true if unnamed or no relevant bits
Definition CodePart.h:132
etiss::uintMax bits
Definition CodePart.h:155
bool operator<(const RegisterPart &other) const
Definition CodePart.h:133
bool maskedBy(const RegisterPart &rp) const
Definition CodePart.h:125
bool operator==(const RegisterPart &other) const
same as matches(const RegisterPart & other)
Definition CodePart.h:151
set of register parts.
Definition CodePart.h:163
void add(const std::string &name, unsigned registerWidth, etiss::uintMax bits=(uintMax)((intMax) -1))
Definition CodePart.h:205
bool disjoint(const RegisterSet &rs) const
Definition CodePart.h:280
void applyShadow(const RegisterSet &rs)
any register bits set in the passed RegisterSet won't be set in this RegisterSet
Definition CodePart.h:212
void merge(const RegisterSet &rs)
any register bits set in the passed RegisterSet will be set in this RegisterSet (plus previously set ...
Definition CodePart.h:241
std::set< RegisterPart, lex_compare > set_
Definition CodePart.h:336
bool maskedBy(const RegisterSet &rs) const
Definition CodePart.h:297
void add(const RegisterPart &rp)
add a registerPart to the set or just its relevant bits if a register with the same name is already p...
Definition CodePart.h:184
RegisterSet(const RegisterSet &rs)
Definition CodePart.h:179
void intersect(const RegisterSet &rs)
only register bits set in this AND the passed RegisterSet remain set in this RegisterSet
Definition CodePart.h:253
std::string _dbg_print() const
writes a human readable string listing all set bit of every register in this RegisterSet
Definition CodePart.h:323
forwards: include/jit/*
Definition Benchmark.h:17
#define false
Definition stdbool.h:17
special comparison function that only takes register name into account.
Definition CodePart.h:170
bool operator()(const RegisterPart &lhs, const RegisterPart &rhs) const
Definition CodePart.h:171