ETISS 0.11.2
ExtendableTranslatingInstructionSetSimulator(version0.11.2)
Loading...
Searching...
No Matches
Misc.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.
14#ifndef ETISS_INCLUDE_MISC_H_
15#define ETISS_INCLUDE_MISC_H_
16
17#include <functional>
18#include <iostream>
19#include <list>
20#include <map>
21#include <mutex>
22#include <regex>
23#include <set>
24#include <sstream>
25#include <string>
26#include <typeinfo>
27#include <vector>
28
29#include "etiss/config.h"
30
31#include "etiss/jit/types.h"
32
33// some msvc pathces
34#ifdef _MSC_VER
35typedef std::make_signed<size_t>::type ssize_t;
36#endif
37
38#define ETISS_TOSTRING2(X) #X
39#define ETISS_TOSTRING(X) ETISS_TOSTRING2(X)
40
41#define etiss_log(LEVEL, MSG) \
42 etiss::log(etiss::LEVEL, \
43 std::string("On line " ETISS_TOSTRING(__LINE__) " in file " ETISS_TOSTRING(__FILE__) ": ") + (MSG));
44
45#define etiss_del_copy(CLASS) \
46 CLASS(const CLASS &) = delete; \
47 CLASS &operator=(const CLASS &) = delete;
48#define etiss_del_move(CLASS) \
49 CLASS &operator=(CLASS &&) = delete; \
50 CLASS(CLASS &&) = delete;
51
52#define etiss_del_como(CLASS) etiss_del_copy(CLASS) etiss_del_move(CLASS)
53
54// special define that creates a STRUCT whose STRUCT::value is true if EXPRESSION is valid given template parameter T
55// (note expression may not have a ';': e.g. ((T*)0)->toString() NOT ((T*)0)->toString();
56#define ETISS_TEMPLATE_EXPRESSION_IS_VALID(STRUCT, EXPRESSION) \
57 template <typename> \
58 struct STRUCT##Void \
59 { \
60 typedef void type; \
61 }; \
62 template <typename T, typename Sfinae = void> \
63 struct STRUCT : std::false_type \
64 { \
65 }; \
66 template <typename T> \
67 struct STRUCT<T, typename STRUCT##Void<decltype(EXPRESSION)>::type> : std::true_type \
68 { \
69 };
70
71namespace etiss
72{
73class Configuration;
74
82{
83 SILENT = 0,
85 ERROR = 2,
87 INFO = 4,
88 VERBOSE = 5
89};
90
95{
96 public:
97 inline ToString() {}
98 virtual inline ~ToString() {}
99};
100
105template <typename T>
106typename std::enable_if<std::is_base_of<etiss::ToString, T>::value, std::ostream &>::type operator<<(std::ostream &os,
107 const T &val)
108{
109 os << val.toString();
110 return os;
111}
112
120bool &log_to_stderr();
121
132template <typename T>
133std::string toString(const T &val)
134{
135 std::stringstream ss;
136 ss << val;
137 return ss.str();
138}
139
140std::list<std::string> split(
141 const std::string &str,
142 std::function<size_t(const std::string & , size_t , size_t & )> findsplit);
143inline std::list<std::string> split(const std::string &str, char splitchar)
144{
145 return split(str,
146 [splitchar](const std::string &str, size_t from, size_t &seperatorsize)
147 {
148 seperatorsize = 1;
149 return str.find(splitchar, from);
150 });
151}
152
153inline std::list<std::string> split(const std::string &str, const std::string &splitstring)
154{
155 return split(str,
156 [splitstring](const std::string &str, size_t from, size_t &seperatorsize)
157 {
158 seperatorsize = 1;
159 return str.find(splitstring, from);
160 });
161}
162
167template <typename T, typename T2, typename... O>
168std::string toString(const T &val, const T2 &val2, const O &...others)
169{
170 return toString(val) + ";" + toString(val2, others...);
171}
172
176std::string toString(const Verbosity &val);
177
191{
192 public:
193 inline SourceCodeLocation(const std::string &file, size_t line) : file_(file), line_(line) {}
194 inline std::string toString() const
195 {
196 return std::string("SourceCodeLocation { file=\"") + file_ + "\", line=" + etiss::toString(line_) + "}";
197 }
198 const std::string file_;
199 const size_t line_;
200};
201
202#define ETISS_SRCLOC etiss::SourceCodeLocation(__FILE__, __LINE__)
203#define ETISS_VARVAL(VAR) (std::string(ETISS_TOSTRING(VAR)) + "={" + toLogString(VAR) + "}")
204
209void log(Verbosity level, std::string msg);
210
215void logC(Verbosity level, std::function<std::string(void)> msgGen);
216
223template <typename T>
224std::string toLogString(const T &t)
225{
226 return std::string("{") + etiss::toString(t) + "}";
227}
228
236template <typename T1, typename T2, typename... O>
237std::string toLogString(const T1 &t1, const T2 &t2, const O &...os)
238{
239 return toLogString(t1) + "\n\t\t" + toLogString(t2, os...);
240}
241
246template <typename T, typename... O>
247void log(Verbosity level, std::string msg, const T &t, const O &...args)
248{
249 if (level <= (int)verbosity())
250 {
251 etiss::log(level, msg + " \n\t{\n\t\t" + toLogString(t, args...) + "\n\t}");
252 }
253}
254
255std::vector<std::string> parseCommands(const std::string &cmdline);
256
260Configuration &cfg(const std::string &cfgName);
261Configuration &cfg();
262
266std::string installDir();
267
271std::string jitFiles();
272
276std::vector<std::string> jitExtHeaders();
277
281std::vector<std::string> jitExtLibraries();
282
286std::vector<std::string> jitExtHeaderPaths();
290std::vector<std::string> jitExtLibPaths();
291
296{
297 public:
299 Configuration(std::string args);
300 Configuration(const std::list<std::string> &args);
301
305 std::map<std::string, std::string> &config();
306
311 template <typename T>
312 T get(const std::string &key, T default_, bool *default_used = 0)
313 {
314 etiss::log(etiss::ERROR, std::string("etiss::Configuration::get not implemented for requested type")
315 /* + typeid(T).name()*/);
316 if (default_used)
317 *default_used = true;
318 return default_;
319 }
320
326 bool debug();
327
334 template <typename T>
335 bool set(const std::string &key, T value)
336 {
337 // GCC 4.9.0 required:
338 /*
339 std::regex reModule("(^|::)core[[:digit:]]+::");
340 std::smatch match;
341 if(std::regex_search(key, match, reModule))
342 {
343 std::ptrdiff_t const match_count(std::distance(
344 std::sregex_iterator(key.begin(), key.end(), reModule),
345 std::sregex_iterator()));
346 if (match_count > 1)
347 {
348 etiss::log(etiss::ERROR, std::string("Config key invalid") +
349 " (more than one module specialization) key: " + key);
350 return false;
351 }
352 std::string strReplace = match.str(0).substr(0,2).compare("")?"":"::";
353 std::string newkey = key;
354 newkey.replace(match.position(0),match.length(0),strReplace);
355 std::cout << "new key: " << newkey << std::endl;
356 return etiss::cfg(newkey).set<T>(newkey, value);
357 }*/
358 size_t coreStart = key.find("core");
359 size_t coreEnd = key.find("::", coreStart + 1);
360 if (coreStart != std::string::npos && coreEnd != std::string::npos &&
361 key.substr(coreStart + 4, coreEnd - (coreStart + 4)).find_first_not_of("0123456789") == std::string::npos)
362 {
363 std::string newkey = key;
364 newkey.replace(coreStart, coreEnd - coreStart + 2, "");
365 etiss::log(etiss::INFO, std::string("use ") + newkey + " in Configuration for module " +
366 key.substr(coreStart, coreEnd - coreStart));
367 return etiss::cfg(key.substr(coreStart, coreEnd - coreStart)).set<T>(newkey, value);
368 }
369
370 std::stringstream ss;
371 ss << value;
372 std::lock_guard<std::mutex> lock(mu_);
373 if (final_.find(key) != final_.end())
374 return false;
375
376 auto hlp = helpers_.find(key);
377 if (hlp != helpers_.end())
378 {
379 cfg_[key] = (hlp->second)(ss.str());
380 }
381 else
382 {
383 cfg_[key] = ss.str();
384 }
385 return true;
386 }
387
391 bool isSet(std::string val);
392
396 void remove(const std::string &key);
397
404 std::list<std::string> set(const std::list<std::string> &args);
405
406 static std::pair<std::string, std::string> set_cmd_line_boost(const std::string &s);
407
411 std::map<std::string, std::string> listFullConfiguration();
412
416 void makeFinal(const std::string &key);
417
422 void announce(std::string key, std::string type = std::string(), std::string values = std::string(),
423 std::string description = std::string());
424
428 std::map<std::string, std::tuple<std::string, std::string, std::string>> getAnnounced() const;
429
431 {
432 std::lock_guard<std::mutex> lock(other.mu_);
433 std::lock_guard<std::mutex> lock2(mu_);
434 cfg_ = other.cfg_;
435 final_ = other.final_;
436 helpers_ = other.helpers_;
437 announced_ = other.announced_;
438 }
439
440 private:
441 std::map<std::string, std::string> cfg_;
442 std::set<std::string> final_;
443 std::map<std::string, std::function<std::string(std::string)>> helpers_;
444 mutable std::mutex mu_;
445 std::map<std::string, std::tuple<std::string, std::string, std::string>> announced_;
446};
447
448// template specializations
449template <>
450std::string Configuration::get<std::string>(const std::string &key, std::string default_, bool *default_used);
451
452template <>
453bool Configuration::get<bool>(const std::string &key, bool default_, bool *default_used);
454
455template <>
456int Configuration::get<int>(const std::string &key, int default_, bool *default_used);
457
458template <>
459unsigned Configuration::get<unsigned>(const std::string &key, unsigned default_, bool *default_used);
460
461template <>
462uint64_t Configuration::get<uint64_t>(const std::string &key, uint64_t default_, bool *default_used);
463
469{
470 public:
471 inline ConfigAnnouncer(Configuration &config, std::string key, std::string type = std::string(),
472 std::string values = std::string(), std::string description = std::string())
473 {
474 config.announce(key, type, values, description);
475 }
476
477 inline ConfigAnnouncer(std::string key, std::string type = std::string(), std::string values = std::string(),
478 std::string description = std::string())
479 {
480 cfg().announce(key, type, values, description);
481 }
482};
483
493
499#if ETISS_USE_CONSTEXPR
500constexpr
501#else
502inline
503#endif
506{
507 return ((0xFFFFFFFF & 1) == _LITTLE_ENDIAN_) ? _LITTLE_ENDIAN_
508 : ((0xFFFFFFFF & 1) == _BIG_ENDIAN_) ? _BIG_ENDIAN_
510}
511
512} // end of namespace etiss
513
514#include "etiss/ClassDefs.h"
515
516#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:469
ConfigAnnouncer(std::string key, std::string type=std::string(), std::string values=std::string(), std::string description=std::string())
Definition Misc.h:477
ConfigAnnouncer(Configuration &config, std::string key, std::string type=std::string(), std::string values=std::string(), std::string description=std::string())
Definition Misc.h:471
simple class to hold configuration options
Definition Misc.h:296
std::map< std::string, std::string > & config()
access to the configuration key value map
Definition Misc.cpp:218
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:519
std::map< std::string, std::string > listFullConfiguration()
get a copy of the configuration as a map
Definition Misc.cpp:493
void operator=(Configuration &&other)
Definition Misc.h:430
static std::pair< std::string, std::string > set_cmd_line_boost(const std::string &s)
Definition Misc.cpp:360
void makeFinal(const std::string &key)
makes an option final (option can no longer be changed)
Definition Misc.cpp:513
std::map< std::string, std::tuple< std::string, std::string, std::string > > getAnnounced() const
get a map copy with announced options
Definition Misc.cpp:525
bool isSet(std::string val)
return true if the value of an configuration key has been set
Definition Misc.cpp:354
bool debug()
Get the value of the key "debug".
Definition Misc.cpp:341
std::map< std::string, std::tuple< std::string, std::string, std::string > > announced_
Definition Misc.h:445
std::mutex mu_
Definition Misc.h:444
void remove(const std::string &key)
removes a key value mapping
Definition Misc.cpp:346
bool set(const std::string &key, T value)
template function to set the value of a configuration key.
Definition Misc.h:335
std::map< std::string, std::string > cfg_
Definition Misc.h:441
std::set< std::string > final_
Definition Misc.h:442
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:312
std::map< std::string, std::function< std::string(std::string)> > helpers_
Definition Misc.h:443
simple class that store source location information.
Definition Misc.h:191
const std::string file_
Definition Misc.h:198
const size_t line_
Definition Misc.h:199
SourceCodeLocation(const std::string &file, size_t line)
Definition Misc.h:193
std::string toString() const
Definition Misc.h:194
Marker interface for toString() support.
Definition Misc.h:95
virtual ~ToString()
Definition Misc.h:98
contains defines to configure ETISS.
#define ETISS_LITTLE_ENDIAN
Definition config.h:58
#define ETISS_UNKNOWN_ENDIAN
Definition config.h:60
#define ETISS_BIG_ENDIAN
Definition config.h:59
forwards: include/jit/*
Definition Benchmark.h:17
std::vector< std::string > parseCommands(const std::string &cmdline)
Definition Misc.cpp:116
std::list< std::string > split(const std::string &str, std::function< size_t(const std::string &, size_t, size_t &)> findsplit)
Definition Misc.cpp:54
std::string jitFiles()
Get ETISS JIT files path.
Definition Misc.cpp:563
void logC(Verbosity level, std::function< std::string(void)> msgGen)
write log message at the given level.
Definition Misc.cpp:108
std::vector< std::string > jitExtLibPaths()
Get ETISS JIT external path.
Definition Misc.cpp:592
std::string toString(const T &val)
conversion of type T to std::string.
Definition Misc.h:133
std::string installDir()
Get ETISS installation directory.
Definition Misc.cpp:554
endian_t
Enumeration type for the endianness.
Definition Misc.h:488
@ _LITTLE_ENDIAN_
Definition Misc.h:489
@ _BIG_ENDIAN_
Definition Misc.h:490
@ _UNKNOWN_ENDIAN_
Definition Misc.h:491
std::vector< std::string > jitExtLibraries()
Get ETISS JIT external libraries.
Definition Misc.cpp:576
std::vector< std::string > jitExtHeaders()
Get ETISS JIT external headers.
Definition Misc.cpp:568
std::vector< std::string > jitExtHeaderPaths()
Get ETISS JIT external path.
Definition Misc.cpp:584
Verbosity
Enumeration type for the log levels.
Definition Misc.h:82
@ INFO
Definition Misc.h:87
@ VERBOSE
Definition Misc.h:88
@ WARNING
Definition Misc.h:86
@ ERROR
Definition Misc.h:85
@ FATALERROR
Definition Misc.h:84
@ SILENT
Definition Misc.h:83
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:106
endian_t getEndianness()
evaluates the endianness of the current build as a constexpr.
Definition Misc.h:505
Configuration & cfg()
Definition Misc.cpp:548
bool & log_to_stderr()
Definition Misc.cpp:89
std::string toLogString(const T &t)
Format the string representation of a value for the log output.
Definition Misc.h:224
Verbosity & verbosity()
Get log level reference.
Definition Misc.cpp:84
void log(Verbosity level, std::string msg)
write log message at the given level.
Definition Misc.cpp:94
#define log(__x)
Definition tgmath.h:460