ETISS 0.8.0
Extendable Translating Instruction Set Simulator (version 0.8.0)
Misc.h
Go to the documentation of this file.
1 
56 #ifndef ETISS_INCLUDE_MISC_H_
57 #define ETISS_INCLUDE_MISC_H_
58 
59 #include <functional>
60 #include <iostream>
61 #include <list>
62 #include <map>
63 #include <mutex>
64 #include <regex>
65 #include <set>
66 #include <sstream>
67 #include <string>
68 #include <typeinfo>
69 #include <vector>
70 
71 #include "etiss/config.h"
72 
73 #include "etiss/jit/types.h"
74 
75 // some msvc pathces
76 #ifdef _MSC_VER
77 typedef std::make_signed<size_t>::type ssize_t;
78 #endif
79 
80 #define ETISS_TOSTRING2(X) #X
81 #define ETISS_TOSTRING(X) ETISS_TOSTRING2(X)
82 
83 #define etiss_log(LEVEL, MSG) \
84  etiss::log(etiss::LEVEL, \
85  std::string("On line " ETISS_TOSTRING(__LINE__) " in file " ETISS_TOSTRING(__FILE__) ": ") + (MSG));
86 
87 #define etiss_del_copy(CLASS) \
88  CLASS(const CLASS &) = delete; \
89  CLASS &operator=(const CLASS &) = delete;
90 #define etiss_del_move(CLASS) \
91  CLASS &operator=(CLASS &&) = delete; \
92  CLASS(CLASS &&) = delete;
93 
94 #define etiss_del_como(CLASS) etiss_del_copy(CLASS) etiss_del_move(CLASS)
95 
96 // special define that creates a STRUCT whose STRUCT::value is true if EXPRESSION is valid given template parameter T
97 // (note expression may not have a ';': e.g. ((T*)0)->toString() NOT ((T*)0)->toString();
98 #define ETISS_TEMPLATE_EXPRESSION_IS_VALID(STRUCT, EXPRESSION) \
99  template <typename> \
100  struct STRUCT##Void \
101  { \
102  typedef void type; \
103  }; \
104  template <typename T, typename Sfinae = void> \
105  struct STRUCT : std::false_type \
106  { \
107  }; \
108  template <typename T> \
109  struct STRUCT<T, typename STRUCT##Void<decltype(EXPRESSION)>::type> : std::true_type \
110  { \
111  };
112 
113 namespace etiss
114 {
115 class Configuration;
116 
124 {
125  SILENT = 0,
127  ERROR = 2,
128  WARNING = 3,
129  INFO = 4,
130  VERBOSE = 5
131 };
132 
136 class ToString
137 {
138  public:
139  inline ToString() {}
140  virtual inline ~ToString() {}
141 };
142 
147 template <typename T>
148 typename std::enable_if<std::is_base_of<etiss::ToString, T>::value, std::ostream &>::type operator<<(std::ostream &os,
149  const T &val)
150 {
151  os << val.toString();
152  return os;
153 }
154 
162 
173 template <typename T>
174 std::string toString(const T &val)
175 {
176  std::stringstream ss;
177  ss << val;
178  return ss.str();
179 }
180 
181 std::list<std::string> split(
182  const std::string &str,
183  std::function<size_t(const std::string & , size_t , size_t & )> findsplit);
184 inline std::list<std::string> split(const std::string &str, char splitchar)
185 {
186  return split(str, [splitchar](const std::string &str, size_t from, size_t &seperatorsize) {
187  seperatorsize = 1;
188  return str.find(splitchar, from);
189  });
190 }
191 
192 inline std::list<std::string> split(const std::string &str, const std::string &splitstring)
193 {
194  return split(str, [splitstring](const std::string &str, size_t from, size_t &seperatorsize) {
195  seperatorsize = 1;
196  return str.find(splitstring, from);
197  });
198 }
199 
204 template <typename T, typename T2, typename... O>
205 std::string toString(const T &val, const T2 &val2, const O &... others)
206 {
207  return toString(val) + ";" + toString(val2, others...);
208 }
209 
213 std::string toString(const Verbosity &val);
214 
228 {
229  public:
230  inline SourceCodeLocation(const std::string &file, size_t line) : file_(file), line_(line) {}
231  inline std::string toString() const
232  {
233  return std::string("SourceCodeLocation { file=\"") + file_ + "\", line=" + etiss::toString(line_) + "}";
234  }
235  const std::string file_;
236  const size_t line_;
237 };
238 
239 #define ETISS_SRCLOC etiss::SourceCodeLocation(__FILE__, __LINE__)
240 #define ETISS_VARVAL(VAR) (std::string(ETISS_TOSTRING(VAR)) + "={" + toLogString(VAR) + "}")
241 
246 void log(Verbosity level, std::string msg);
247 
252 void logC(Verbosity level, std::function<std::string(void)> msgGen);
253 
260 template <typename T>
261 std::string toLogString(const T &t)
262 {
263  return std::string("{") + etiss::toString(t) + "}";
264 }
265 
273 template <typename T1, typename T2, typename... O>
274 std::string toLogString(const T1 &t1, const T2 &t2, const O &... os)
275 {
276  return toLogString(t1) + "\n\t\t" + toLogString(t2, os...);
277 }
278 
283 template <typename T, typename... O>
284 void log(Verbosity level, std::string msg, const T &t, const O &... args)
285 {
286  if (level <= (int)verbosity())
287  {
288  etiss::log(level, msg + " \n\t{\n\t\t" + toLogString(t, args...) + "\n\t}");
289  }
290 }
291 
292 std::vector<std::string> parseCommands(const std::string &cmdline);
293 
297 Configuration &cfg(const std::string &cfgName);
298 Configuration &cfg();
299 
303 std::string installDir();
304 
308 std::string jitFiles();
309 
313 std::vector<std::string> jitExtHeaders();
314 
318 std::vector<std::string> jitExtLibraries();
319 
323 std::vector<std::string> jitExtHeaderPaths();
327 std::vector<std::string> jitExtLibPaths();
328 
333 {
334  public:
335  Configuration();
336  Configuration(std::string args);
337  Configuration(const std::list<std::string> &args);
338 
342  std::map<std::string, std::string> &config();
343 
348  template <typename T>
349  T get(const std::string &key, T default_, bool *default_used = 0)
350  {
351  etiss::log(etiss::ERROR, std::string("etiss::Configuration::get not implemented for requested type")
352  /* + typeid(T).name()*/);
353  if (default_used)
354  *default_used = true;
355  return default_;
356  }
357 
363  bool debug();
364 
371  template <typename T>
372  bool set(const std::string &key, T value)
373  {
374  // GCC 4.9.0 required:
375  /*
376  std::regex reModule("(^|::)core[[:digit:]]+::");
377  std::smatch match;
378  if(std::regex_search(key, match, reModule))
379  {
380  std::ptrdiff_t const match_count(std::distance(
381  std::sregex_iterator(key.begin(), key.end(), reModule),
382  std::sregex_iterator()));
383  if (match_count > 1)
384  {
385  etiss::log(etiss::ERROR, std::string("Config key invalid") +
386  " (more than one module specialization) key: " + key);
387  return false;
388  }
389  std::string strReplace = match.str(0).substr(0,2).compare("")?"":"::";
390  std::string newkey = key;
391  newkey.replace(match.position(0),match.length(0),strReplace);
392  std::cout << "new key: " << newkey << std::endl;
393  return etiss::cfg(newkey).set<T>(newkey, value);
394  }*/
395  size_t coreStart = key.find("core");
396  size_t coreEnd = key.find("::", coreStart + 1);
397  if (coreStart != std::string::npos && coreEnd != std::string::npos &&
398  key.substr(coreStart + 4, coreEnd - (coreStart + 4)).find_first_not_of("0123456789") == std::string::npos)
399  {
400  std::string newkey = key;
401  newkey.replace(coreStart, coreEnd - coreStart + 2, "");
402  etiss::log(etiss::INFO, std::string("use ") + newkey + " in Configuration for module " +
403  key.substr(coreStart, coreEnd - coreStart));
404  return etiss::cfg(key.substr(coreStart, coreEnd - coreStart)).set<T>(newkey, value);
405  }
406 
407  std::stringstream ss;
408  ss << value;
409  std::lock_guard<std::mutex> lock(mu_);
410  if (final_.find(key) != final_.end())
411  return false;
412 
413  auto hlp = helpers_.find(key);
414  if (hlp != helpers_.end())
415  {
416  cfg_[key] = (hlp->second)(ss.str());
417  }
418  else
419  {
420  cfg_[key] = ss.str();
421  }
422  return true;
423  }
424 
428  bool isSet(std::string val);
429 
433  void remove(const std::string &key);
434 
441  std::list<std::string> set(const std::list<std::string> &args);
442 
443  static std::pair<std::string, std::string> set_cmd_line_boost(const std::string& s);
444 
448  std::map<std::string, std::string> listFullConfiguration();
449 
453  void makeFinal(const std::string &key);
454 
459  void announce(std::string key, std::string type = std::string(), std::string values = std::string(),
460  std::string description = std::string());
461 
465  std::map<std::string, std::tuple<std::string, std::string, std::string>> getAnnounced() const;
466 
467  void operator=(Configuration &&other)
468  {
469  std::lock_guard<std::mutex> lock(other.mu_);
470  std::lock_guard<std::mutex> lock2(mu_);
471  cfg_ = other.cfg_;
472  final_ = other.final_;
473  helpers_ = other.helpers_;
474  announced_ = other.announced_;
475  }
476 
477  private:
478  std::map<std::string, std::string> cfg_;
479  std::set<std::string> final_;
480  std::map<std::string, std::function<std::string(std::string)>> helpers_;
481  mutable std::mutex mu_;
482  std::map<std::string, std::tuple<std::string, std::string, std::string>> announced_;
483 };
484 
485 // template specializations
486 template <>
487 std::string Configuration::get<std::string>(const std::string &key, std::string default_, bool *default_used);
488 
489 template <>
490 bool Configuration::get<bool>(const std::string &key, bool default_, bool *default_used);
491 
492 template <>
493 int Configuration::get<int>(const std::string &key, int default_, bool *default_used);
494 
495 template <>
496 unsigned Configuration::get<unsigned>(const std::string &key, unsigned default_, bool *default_used);
497 
498 template <>
499 uint64_t Configuration::get<uint64_t>(const std::string &key, uint64_t default_, bool *default_used);
500 
506 {
507  public:
508  inline ConfigAnnouncer(Configuration &config, std::string key, std::string type = std::string(),
509  std::string values = std::string(), std::string description = std::string())
510  {
511  config.announce(key, type, values, description);
512  }
513 
514  inline ConfigAnnouncer(std::string key, std::string type = std::string(), std::string values = std::string(),
515  std::string description = std::string())
516  {
517  cfg().announce(key, type, values, description);
518  }
519 };
520 
525 {
529 };
530 
536 #if ETISS_USE_CONSTEXPR
537 constexpr
538 #else
539 inline
540 #endif
541  endian_t
543 {
544  return ((0xFFFFFFFF & 1) == _LITTLE_ENDIAN_) ? _LITTLE_ENDIAN_
545  : ((0xFFFFFFFF & 1) == _BIG_ENDIAN_) ? _BIG_ENDIAN_ : _UNKNOWN_ENDIAN_;
546 }
547 
548 } // end of namespace etiss
549 
550 #include "etiss/ClassDefs.h"
551 
552 #endif
__device__ __2f16 float bool s
static __inline__ uint32_t
Definition: arm_cde.h:25
static __inline__ uint64_t
Definition: arm_cde.h:31
simple class to add a configuration option to the list of known options
Definition: Misc.h:506
ConfigAnnouncer(std::string key, std::string type=std::string(), std::string values=std::string(), std::string description=std::string())
Definition: Misc.h:514
ConfigAnnouncer(Configuration &config, std::string key, std::string type=std::string(), std::string values=std::string(), std::string description=std::string())
Definition: Misc.h:508
simple class to hold configuration options
Definition: Misc.h:333
std::map< std::string, std::string > & config()
access to the configuration key value map
Definition: Misc.cpp:246
void announce(std::string key, std::string type=std::string(), std::string values=std::string(), std::string description=std::string())
add a possible option to a list.
Definition: Misc.cpp:548
std::map< std::string, std::string > listFullConfiguration()
get a copy of the configuration as a map
Definition: Misc.cpp:522
void operator=(Configuration &&other)
Definition: Misc.h:467
static std::pair< std::string, std::string > set_cmd_line_boost(const std::string &s)
Definition: Misc.cpp:388
void makeFinal(const std::string &key)
makes an option final (option can no longer be changed)
Definition: Misc.cpp:542
std::map< std::string, std::tuple< std::string, std::string, std::string > > getAnnounced() const
get a map copy with announced options
Definition: Misc.cpp:554
bool isSet(std::string val)
return true if the value of an configuration key has been set
Definition: Misc.cpp:382
bool debug()
Get the value of the key "debug".
Definition: Misc.cpp:369
std::map< std::string, std::tuple< std::string, std::string, std::string > > announced_
Definition: Misc.h:482
std::mutex mu_
Definition: Misc.h:481
void remove(const std::string &key)
removes a key value mapping
Definition: Misc.cpp:374
bool set(const std::string &key, T value)
template function to set the value of a configuration key.
Definition: Misc.h:372
std::map< std::string, std::string > cfg_
Definition: Misc.h:478
std::set< std::string > final_
Definition: Misc.h:479
T get(const std::string &key, T default_, bool *default_used=0)
template function to read the value of a configuration key.
Definition: Misc.h:349
std::map< std::string, std::function< std::string(std::string)> > helpers_
Definition: Misc.h:480
simple class that store source location information.
Definition: Misc.h:228
const std::string file_
Definition: Misc.h:235
const size_t line_
Definition: Misc.h:236
SourceCodeLocation(const std::string &file, size_t line)
Definition: Misc.h:230
std::string toString() const
Definition: Misc.h:231
Marker interface for toString() support.
Definition: Misc.h:137
virtual ~ToString()
Definition: Misc.h:140
contains defines to configure ETISS.
#define ETISS_LITTLE_ENDIAN
Definition: config.h:96
#define ETISS_UNKNOWN_ENDIAN
Definition: config.h:98
#define ETISS_BIG_ENDIAN
Definition: config.h:97
Page Table Entry (PTE) defines the composition of Page Frame Number (PFN) and relavant flags.
Definition: Benchmark.h:53
std::vector< std::string > parseCommands(const std::string &cmdline)
Definition: Misc.cpp:144
std::list< std::string > split(const std::string &str, std::function< size_t(const std::string &, size_t, size_t &)> findsplit)
Definition: Misc.cpp:91
std::string jitFiles()
Get ETISS JIT files path.
Definition: Misc.cpp:592
void logC(Verbosity level, std::function< std::string(void)> msgGen)
write log message at the given level.
Definition: Misc.cpp:136
std::vector< std::string > jitExtLibPaths()
Get ETISS JIT external path.
Definition: Misc.cpp:618
std::string toString(const T &val)
conversion of type T to std::string.
Definition: Misc.h:174
std::string installDir()
Get ETISS installation directory.
Definition: Misc.cpp:583
endian_t
Enumeration type for the endianness.
Definition: Misc.h:525
@ _LITTLE_ENDIAN_
Definition: Misc.h:526
@ _BIG_ENDIAN_
Definition: Misc.h:527
@ _UNKNOWN_ENDIAN_
Definition: Misc.h:528
std::vector< std::string > jitExtLibraries()
Get ETISS JIT external libraries.
Definition: Misc.cpp:604
std::vector< std::string > jitExtHeaders()
Get ETISS JIT external headers.
Definition: Misc.cpp:597
std::vector< std::string > jitExtHeaderPaths()
Get ETISS JIT external path.
Definition: Misc.cpp:611
Verbosity
Enumeration type for the log levels.
Definition: Misc.h:124
@ INFO
Definition: Misc.h:129
@ VERBOSE
Definition: Misc.h:130
@ WARNING
Definition: Misc.h:128
@ ERROR
Definition: Misc.h:127
@ FATALERROR
Definition: Misc.h:126
@ SILENT
Definition: Misc.h:125
Configuration & cfg(const std::string &cfgName)
Get reference of the global ETISS configuration object.
Definition: Misc.cpp:560
endian_t getEndianness()
evaluates the endianness of the current build as a constexpr.
Definition: Misc.h:542
std::enable_if< std::is_base_of< etiss::ToString, T >::value, std::ostream & >::type operator<<(std::ostream &os, const T &val)
Implementation of the write to stream operator for objects, that implement the etiss::ToString interf...
Definition: Misc.h:148
std::string toLogString(const T &t)
Format the string representation of a value for the log output.
Definition: Misc.h:261
Verbosity & verbosity()
Get log level reference.
Definition: Misc.cpp:120
void log(Verbosity level, std::string msg)
write log message at the given level.
Definition: Misc.cpp:125