ETISS 0.8.0
Extendable Translating Instruction Set Simulator (version 0.8.0)
CodePart.h
Go to the documentation of this file.
1 
51 #ifndef ETISS_INCLUDE_CODEPART_H_
52 #define ETISS_INCLUDE_CODEPART_H_
53 
54 #include <string>
55 #include <set>
56 #include <list>
57 #include <vector>
58 #include <map>
59 #include <sstream>
60 #include <iostream>
61 
62 #include "etiss/jit/types.h"
63 #include "etiss/Misc.h"
64 
65 namespace etiss
66 {
67 
76 {
77  public:
78  inline RegisterPart() : name(""), bits(0), regWidth(0) {}
85  inline RegisterPart(const std::string &name, unsigned registerWidth, etiss::uintMax bits = (uintMax)((intMax)-1))
86  : name(name), regWidth(registerWidth)
87  {
88  uintMax mask = 0;
89  for (unsigned i = 0; i < regWidth; i++)
90  {
91  mask |= ((uintMax)1) << i;
92  }
93  this->bits = bits & mask;
94  }
95  inline RegisterPart(const RegisterPart &cpy)
96  {
97  this->name = cpy.name;
98  this->bits = cpy.bits;
99  this->regWidth = cpy.regWidth;
100  }
101 
102  public:
106  inline bool matches(const RegisterPart &rp) const
107  {
108  if (this->name != rp.name)
109  return false;
110 #if DEBUG
111  if (this->regWidth != rp.regWidth && !(isEmpty() || rp.isEmpty()))
112  {
113  std::cout << "Serve Warning: RegisterPart definitions exist with same name (=same register) but different "
114  "register width values"
115  << std::endl;
116  return false;
117  }
118 #endif
119  return this->bits == rp.bits;
120  }
124  inline bool contains(const RegisterPart &rp) const
125  {
126  if (this->name != rp.name)
127  return false;
128 #if DEBUG
129  if (this->regWidth != rp.regWidth && !(isEmpty() || rp.isEmpty()))
130  {
131  std::cout << "Serve Warning: RegisterPart definitions exist with same name (=same register) but different "
132  "register width values"
133  << std::endl;
134  return false;
135  }
136 #endif
137  return (this->bits & rp.bits) == rp.bits; // this->bits has every bit of rp.bits
138  }
142  inline void applyShadow(const RegisterPart &rp) const
143  {
144  if (name == rp.name)
145  bits = bits & (~rp.bits);
146  }
150  inline void merge(const RegisterPart &rp) const
151  {
152  if (name == rp.name)
153  bits = bits | rp.bits;
154  }
158  inline void intersect(const RegisterPart &rp) const
159  {
160  if (name == rp.name)
161  bits = bits & rp.bits;
162  }
163  inline bool maskedBy(const RegisterPart &rp) const { return rp.contains(*this); }
164  inline const std::string &getName() const { return name; }
165  inline const etiss::uintMax &getAffectedBits() const { return bits; }
166  inline const unsigned &getRegisterWidth() const { return regWidth; }
170  inline bool isEmpty() const { return bits == 0 || name.length() == 0; }
171  inline bool operator<(const RegisterPart &other) const
172  {
173  if (name < other.name)
174  return true;
175 #if DEBUG
176  if (regWidth < other.regWidth && !(isEmpty() || other.isEmpty()))
177  {
178  std::cout << "Serve Warning: RegisterPart definitions exist with same name (=same register) but different "
179  "register width values"
180  << std::endl;
181  return true;
182  }
183 #endif
184  return bits < other.bits;
185  }
189  inline bool operator==(const RegisterPart &other) const { return matches(other); }
190 
191  private:
192  std::string name;
193  mutable etiss::uintMax bits;
194  unsigned regWidth;
195 };
196 
201 {
207  struct lex_compare
208  {
209  bool operator()(const RegisterPart &lhs, const RegisterPart &rhs) const
210  {
211  return lhs.getName() < rhs.getName();
212  }
213  };
214 
215  public:
216  inline RegisterSet() {}
217  inline RegisterSet(const RegisterSet &rs) { set_ = rs.set_; }
222  inline void add(const RegisterPart &rp)
223  {
224  if (rp.isEmpty())
225  return;
226  std::set<RegisterPart, lex_compare>::iterator iter = set_.find(rp);
227  if (iter != set_.end())
228  {
229  (*iter).merge(rp);
230  if (iter->isEmpty())
231  {
232  set_.erase(iter);
233  }
234  }
235  else
236  {
237  set_.insert(rp);
238  }
239  }
243  inline void add(const std::string &name, unsigned registerWidth, etiss::uintMax bits = (uintMax)((intMax)-1))
244  {
245  add(RegisterPart(name, registerWidth, bits));
246  }
250  inline void applyShadow(const RegisterSet &rs)
251  {
252  std::set<RegisterPart, lex_compare>::iterator iter = set_.begin();
253  std::set<RegisterPart, lex_compare>::const_iterator oiter;
254  while (iter != set_.end())
255  {
256  oiter = rs.set_.find(*iter);
257  if (oiter != rs.set_.end())
258  {
259  iter->applyShadow(*oiter);
260  if (iter->isEmpty())
261  {
262  set_.erase(iter++);
263  }
264  else
265  {
266  iter++;
267  }
268  }
269  else
270  {
271  iter++;
272  }
273  }
274  }
279  inline void merge(const RegisterSet &rs)
280  {
281  std::set<RegisterPart, lex_compare>::const_iterator iter = rs.set_.begin();
282  while (iter != rs.set_.end())
283  {
284  add(*iter);
285  iter++;
286  }
287  }
291  inline void intersect(const RegisterSet &rs)
292  {
293  std::set<RegisterPart, lex_compare>::iterator iter = set_.begin();
294  while (iter != set_.end())
295  {
296  std::set<RegisterPart, lex_compare>::const_iterator f = rs.set_.find(*iter);
297  if (f != rs.set_.end())
298  {
299  iter->intersect(*f);
300  if (iter->isEmpty())
301  {
302  set_.erase(iter++);
303  }
304  else
305  {
306  iter++;
307  }
308  }
309  else
310  {
311  set_.erase(iter++);
312  }
313  }
314  }
318  inline bool disjoint(const RegisterSet &rs) const
319  {
320  std::set<RegisterPart, lex_compare>::const_iterator iter = set_.begin();
321  while (iter != set_.end())
322  {
323  std::set<RegisterPart, lex_compare>::const_iterator f = rs.set_.find(*iter);
324  if (f != rs.set_.end())
325  {
326  return false;
327  }
328  iter++;
329  }
330  return true;
331  }
335  inline bool maskedBy(const RegisterSet &rs) const
336  {
337  std::set<RegisterPart, lex_compare>::const_iterator iter = set_.begin();
338  while (iter != set_.end())
339  {
340  std::set<RegisterPart, lex_compare>::const_iterator oiter = rs.set_.find(*iter);
341  if (oiter != rs.set_.end())
342  {
343  if (!iter->maskedBy(*oiter))
344  {
345  return false;
346  }
347  }
348  else
349  {
350  return false;
351  }
352  iter++;
353  }
354  return true;
355  }
356  inline bool isEmpty() { return set_.empty(); }
357  inline void clear() { set_.clear(); }
361  inline std::string _dbg_print() const
362  {
363  std::stringstream ss;
364  std::set<RegisterPart, lex_compare>::const_iterator iter = set_.begin();
365  while (iter != set_.end())
366  {
367  ss << iter->getName() << ": " << std::hex << iter->getAffectedBits() << "\n";
368  iter++;
369  }
370  return ss.str();
371  }
372 
373  private:
374  std::set<RegisterPart, lex_compare> set_;
375 };
376 
385 class CodePart
386 {
387  public:
395  enum TYPE
396  {
403  };
405  inline CodePart(const std::string &code, const RegisterSet &registerDependencies = RegisterSet(),
406  const RegisterSet &affectedRegisters = RegisterSet())
407  : code_(code)
408  , registerDependencies_(registerDependencies)
409  , affectedRegisters_(affectedRegisters)
411  {
412  }
415  inline std::string &getCode() { return code_; }
416  inline std::string &code() { return code_; }
417  inline const std::string &code() const { return code_; }
419  inline const RegisterSet &getAffectedRegisters() const { return affectedRegisters_; }
420  inline const std::string &getCode() const { return code_; }
421  inline const bool &fullRegistersDependency() const { return flag_requireAll_; }
422 
423  private:
424  std::string code_;
428 };
429 
431 class Translation;
436 class CodeSet
437 {
438 
439  public:
440  inline CodeSet() {}
441  inline CodeSet(const CodeSet &cs)
442  {
449  }
450  inline void append(const CodePart &part, CodePart::TYPE type)
451  {
452  switch (type)
453  {
455  pindbgretreq_parts_.push_front(part);
456  break;
458  inireq_parts_.push_front(part);
459  break;
461  midopt_parts_.push_front(part);
462  break;
464  appreq_parts_.push_front(part);
465  break;
467  appopt_parts_.push_front(part);
468  break;
470  appretreq_parts_.push_front(part);
471  break;
472  }
473  }
475  {
476  switch (type)
477  {
479  pindbgretreq_parts_.emplace_front();
480  return pindbgretreq_parts_.front();
482  inireq_parts_.emplace_front();
483  return inireq_parts_.front();
485  midopt_parts_.emplace_front();
486  return midopt_parts_.front();
488  appreq_parts_.emplace_front();
489  return appreq_parts_.front();
491  appopt_parts_.emplace_front();
492  return appopt_parts_.front();
494  appretreq_parts_.emplace_front();
495  return appretreq_parts_.front();
496  default:
497  std::cout << "ERROR: etiss::CodePart::append called with invalid etiss::CodePart::TYPE parameter"
498  << std::endl;
499  appretreq_parts_.emplace_front();
500  return appretreq_parts_.front();
501  }
502  }
504  {
505  switch (type)
506  {
508  pindbgretreq_parts_.emplace_back();
509  return pindbgretreq_parts_.back();
511  inireq_parts_.emplace_back();
512  return inireq_parts_.back();
514  midopt_parts_.emplace_back();
515  return midopt_parts_.back();
517  appreq_parts_.emplace_back();
518  return appreq_parts_.back();
520  appopt_parts_.emplace_back();
521  return appopt_parts_.back();
523  appretreq_parts_.emplace_back();
524  return appretreq_parts_.back();
525  default:
526  std::cout << "ERROR: etiss::CodePart::prepend called with invalid etiss::CodePart::TYPE parameter"
527  << std::endl;
528  appretreq_parts_.emplace_back();
529  return appretreq_parts_.back();
530  }
531  }
532 
533  private:
534  static void writeCodeParts(std::string &code, const std::list<CodePart> &parts, bool required, RegisterSet &ignored,
535  bool intersect);
536 
537  public:
542  std::string toString(RegisterSet &ignored, bool &ok) const;
543 
544  private:
545  std::list<CodePart> pindbgretreq_parts_;
546  std::list<CodePart> inireq_parts_;
547  std::list<CodePart> midopt_parts_;
548  std::list<CodePart> appreq_parts_;
549  std::list<CodePart> appopt_parts_;
550  std::list<CodePart> appretreq_parts_;
551 };
552 
570 {
571  friend class Translation;
572 
573  public:
577  class Line
578  {
579  friend class CodeBlock;
580 
581  private:
582  public:
583  Line(const Line &line) : codeset_(line.codeset_), addr_(line.addr_) {}
584  inline Line(etiss::uint64 addr) : addr_(addr) {}
585  inline CodeSet &getCodeSet() { return codeset_; }
586  inline const etiss::uint64 &getAddress() { return addr_; }
587 
588  private:
590  bool ismeta_;
592  };
593 
594  public:
595  inline CodeBlock(etiss::uint64 startindex) : startindex_(startindex) {}
596  inline void reserve(int num) { lines_.reserve(num); }
597  inline Line &get(unsigned index) { return lines_[index]; }
598  inline Line &append(etiss::uint64 addr)
599  {
600  lines_.push_back(Line(addr));
601  return lines_.back();
602  }
603  inline unsigned length() const { return (unsigned)lines_.size(); }
604  inline std::set<std::string> &fileglobalCode() { return fileglobal_code; }
605  inline std::set<std::string> &functionglobalCode() { return functionglobal_code; }
606  void toCode(std::stringstream &out, const std::string &funcname, std::set<std::string> *fileglobalcode);
607 
608  private:
609  std::vector<Line> lines_;
612  std::set<std::string> fileglobal_code;
613  std::set<std::string> functionglobal_code;
614 };
615 } // namespace etiss
616 #endif
etiss_uint64 uint64
Definition: 386-GCC.h:82
general configuration and logging
equivalent of a translated instruction
Definition: CodePart.h:578
etiss::uint64 addr_
Definition: CodePart.h:591
Line(const Line &line)
Definition: CodePart.h:583
Line(etiss::uint64 addr)
Definition: CodePart.h:584
CodeSet & getCodeSet()
Definition: CodePart.h:585
const etiss::uint64 & getAddress()
Definition: CodePart.h:586
A list of CodeSets.
Definition: CodePart.h:570
unsigned length() const
Definition: CodePart.h:603
void toCode(std::stringstream &out, const std::string &funcname, std::set< std::string > *fileglobalcode)
Definition: CodePart.cpp:146
std::set< std::string > functionglobal_code
Definition: CodePart.h:613
std::set< std::string > & functionglobalCode()
Definition: CodePart.h:605
std::vector< Line > lines_
Definition: CodePart.h:609
std::set< std::string > & fileglobalCode()
Definition: CodePart.h:604
void reserve(int num)
Definition: CodePart.h:596
Line & append(etiss::uint64 addr)
Definition: CodePart.h:598
etiss::uint64 endaddress_
Definition: CodePart.h:611
Line & get(unsigned index)
Definition: CodePart.h:597
CodeBlock(etiss::uint64 startindex)
Definition: CodePart.h:595
etiss::uint64 startindex_
Definition: CodePart.h:610
std::set< std::string > fileglobal_code
Definition: CodePart.h:612
Contains a small code snipped.
Definition: CodePart.h:386
const std::string & getCode() const
Definition: CodePart.h:420
std::string code_
Definition: CodePart.h:424
bool flag_requireAll_
Definition: CodePart.h:427
TYPE
defines position,optionality and the ability to return a code for a CodePart.
Definition: CodePart.h:396
@ APPENDEDRETURNINGREQUIRED
Definition: CodePart.h:402
@ PREINITIALDEBUGRETURNING
Definition: CodePart.h:397
std::string & code()
Definition: CodePart.h:416
RegisterSet & getRegisterDependencies()
Definition: CodePart.h:413
CodePart(const std::string &code, const RegisterSet &registerDependencies=RegisterSet(), const RegisterSet &affectedRegisters=RegisterSet())
Definition: CodePart.h:405
RegisterSet & getAffectedRegisters()
Definition: CodePart.h:414
const std::string & code() const
Definition: CodePart.h:417
const RegisterSet & getRegisterDependencies() const
Definition: CodePart.h:418
const RegisterSet & getAffectedRegisters() const
Definition: CodePart.h:419
RegisterSet registerDependencies_
Definition: CodePart.h:425
std::string & getCode()
Definition: CodePart.h:415
RegisterSet affectedRegisters_
Definition: CodePart.h:426
const bool & fullRegistersDependency() const
Definition: CodePart.h:421
A set of CodeParts.
Definition: CodePart.h:437
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:120
std::list< CodePart > appretreq_parts_
Definition: CodePart.h:550
std::list< CodePart > midopt_parts_
Definition: CodePart.h:547
CodePart & append(CodePart::TYPE type)
Definition: CodePart.h:474
std::list< CodePart > pindbgretreq_parts_
Definition: CodePart.h:545
std::list< CodePart > appreq_parts_
Definition: CodePart.h:548
CodeSet(const CodeSet &cs)
Definition: CodePart.h:441
std::list< CodePart > inireq_parts_
Definition: CodePart.h:546
void append(const CodePart &part, CodePart::TYPE type)
Definition: CodePart.h:450
static void writeCodeParts(std::string &code, const std::list< CodePart > &parts, bool required, RegisterSet &ignored, bool intersect)
Definition: CodePart.cpp:57
CodePart & prepend(CodePart::TYPE type)
Definition: CodePart.h:503
std::list< CodePart > appopt_parts_
Definition: CodePart.h:549
abstract description of needed or affected register bits.
Definition: CodePart.h:76
void intersect(const RegisterPart &rp) const
this is not a const function in the usual sense.
Definition: CodePart.h:158
std::string name
Definition: CodePart.h:192
RegisterPart(const RegisterPart &cpy)
Definition: CodePart.h:95
const etiss::uintMax & getAffectedBits() const
Definition: CodePart.h:165
bool contains(const RegisterPart &rp) const
check if passed RegisterPart is a subset of relevant bits of the same register
Definition: CodePart.h:124
unsigned regWidth
Definition: CodePart.h:194
bool matches(const RegisterPart &rp) const
check for equality
Definition: CodePart.h:106
void applyShadow(const RegisterPart &rp) const
this is not a const function in the usual sense.
Definition: CodePart.h:142
void merge(const RegisterPart &rp) const
this is not a const function in the usual sense.
Definition: CodePart.h:150
RegisterPart(const std::string &name, unsigned registerWidth, etiss::uintMax bits=(uintMax)((intMax) -1))
Definition: CodePart.h:85
bool isEmpty() const
true if unnamed or no relevant bits
Definition: CodePart.h:170
etiss::uintMax bits
Definition: CodePart.h:193
bool operator<(const RegisterPart &other) const
Definition: CodePart.h:171
const unsigned & getRegisterWidth() const
Definition: CodePart.h:166
bool maskedBy(const RegisterPart &rp) const
Definition: CodePart.h:163
bool operator==(const RegisterPart &other) const
same as matches(const RegisterPart & other)
Definition: CodePart.h:189
const std::string & getName() const
Definition: CodePart.h:164
set of register parts.
Definition: CodePart.h:201
void add(const std::string &name, unsigned registerWidth, etiss::uintMax bits=(uintMax)((intMax) -1))
Definition: CodePart.h:243
bool disjoint(const RegisterSet &rs) const
Definition: CodePart.h:318
void applyShadow(const RegisterSet &rs)
any register bits set in the passed RegisterSet won't be set in this RegisterSet
Definition: CodePart.h:250
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:279
std::set< RegisterPart, lex_compare > set_
Definition: CodePart.h:374
bool maskedBy(const RegisterSet &rs) const
Definition: CodePart.h:335
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:222
RegisterSet(const RegisterSet &rs)
Definition: CodePart.h:217
void intersect(const RegisterSet &rs)
only register bits set in this AND the passed RegisterSet remain set in this RegisterSet
Definition: CodePart.h:291
std::string _dbg_print() const
writes a human readable string listing all set bit of every register in this RegisterSet
Definition: CodePart.h:361
Page Table Entry (PTE) defines the composition of Page Frame Number (PFN) and relavant flags.
Definition: Benchmark.h:53
#define false
Definition: stdbool.h:17
special comparison function that only takes register name into account.
Definition: CodePart.h:208
bool operator()(const RegisterPart &lhs, const RegisterPart &rhs) const
Definition: CodePart.h:209