common.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #pragma once
  2. #ifdef _MSC_VER
  3. #pragma warning (disable:4267)
  4. #pragma warning (disable:4101)
  5. #pragma warning (disable:4244)
  6. #define _CRT_NONSTDC_NO_DEPRECATE
  7. #define strdup _strdup
  8. #endif
  9. #include <cmath>
  10. #include <cstring>
  11. #include <sstream>
  12. #include <regex>
  13. #include <stdexcept>
  14. #include <vector>
  15. #include <string>
  16. #include <chrono>
  17. #include <string_view>
  18. #include <iomanip>
  19. #include <memory>
  20. #include <iostream>
  21. #include <map>
  22. #include <set>
  23. #include <algorithm>
  24. #include <random>
  25. #include <initializer_list>
  26. #include <variant>
  27. #include <type_traits>
  28. #define PK_VERSION "1.0.0"
  29. // debug macros
  30. #define DEBUG_NO_BUILTIN_MODULES 0
  31. #define DEBUG_EXTRA_CHECK 0
  32. #define DEBUG_DIS_EXEC 0
  33. #define DEBUG_CEVAL_STEP 0
  34. #define DEBUG_FULL_EXCEPTION 0
  35. #define DEBUG_MEMORY_POOL 0
  36. #define DEBUG_NO_MEMORY_POOL 0
  37. #define DEBUG_NO_AUTO_GC 0
  38. #define DEBUG_GC_STATS 0
  39. // config macros
  40. #ifndef PK_ENABLE_OS
  41. #ifdef __ANDROID__
  42. #include <android/ndk-version.h>
  43. #if __NDK_MAJOR__ <= 22
  44. #define PK_ENABLE_OS 0
  45. #else
  46. #define PK_ENABLE_OS 1
  47. #endif
  48. #else
  49. #define PK_ENABLE_OS 1
  50. #endif
  51. #endif
  52. #if PK_ENABLE_THREAD
  53. #define THREAD_LOCAL thread_local
  54. #else
  55. #define THREAD_LOCAL
  56. #endif
  57. /*******************************************************************************/
  58. // This is the maximum number of arguments in a function declaration
  59. // including positional arguments, keyword-only arguments, and varargs
  60. #define PK_MAX_CO_VARNAMES 255
  61. #if _MSC_VER
  62. #define PK_ENABLE_COMPUTED_GOTO 0
  63. #define UNREACHABLE() __assume(0)
  64. #else
  65. #define PK_ENABLE_COMPUTED_GOTO 1
  66. #define UNREACHABLE() __builtin_unreachable()
  67. #if DEBUG_CEVAL_STEP
  68. #undef PK_ENABLE_COMPUTED_GOTO
  69. #endif
  70. #endif
  71. namespace pkpy{
  72. namespace std = ::std;
  73. template <size_t T>
  74. struct NumberTraits;
  75. template <>
  76. struct NumberTraits<4> {
  77. using int_t = int32_t;
  78. using float_t = float;
  79. template<typename... Args>
  80. static int_t stoi(Args&&... args) { return std::stoi(std::forward<Args>(args)...); }
  81. template<typename... Args>
  82. static float_t stof(Args&&... args) { return std::stof(std::forward<Args>(args)...); }
  83. static constexpr int_t c0 = 0b00000000011111111111111111111100;
  84. static constexpr int_t c1 = 0b11111111111111111111111111111100;
  85. static constexpr int_t c2 = 0b00000000000000000000000000000011;
  86. };
  87. template <>
  88. struct NumberTraits<8> {
  89. using int_t = int64_t;
  90. using float_t = double;
  91. template<typename... Args>
  92. static int_t stoi(Args&&... args) { return std::stoll(std::forward<Args>(args)...); }
  93. template<typename... Args>
  94. static float_t stof(Args&&... args) { return std::stod(std::forward<Args>(args)...); }
  95. static constexpr int_t c0 = 0b0000000000001111111111111111111111111111111111111111111111111100;
  96. static constexpr int_t c1 = 0b1111111111111111111111111111111111111111111111111111111111111100;
  97. static constexpr int_t c2 = 0b0000000000000000000000000000000000000000000000000000000000000011;
  98. };
  99. using Number = NumberTraits<sizeof(void*)>;
  100. using i64 = Number::int_t;
  101. using f64 = Number::float_t;
  102. static_assert(sizeof(i64) == sizeof(void*));
  103. static_assert(sizeof(f64) == sizeof(void*));
  104. static_assert(std::numeric_limits<f64>::is_iec559);
  105. struct Dummy { };
  106. struct DummyInstance { };
  107. struct DummyModule { };
  108. struct NoReturn { };
  109. struct Discarded { };
  110. struct Type {
  111. int index;
  112. Type(): index(-1) {}
  113. Type(int index): index(index) {}
  114. bool operator==(Type other) const noexcept { return this->index == other.index; }
  115. bool operator!=(Type other) const noexcept { return this->index != other.index; }
  116. operator int() const noexcept { return this->index; }
  117. };
  118. #define CPP_LAMBDA(x) ([](VM* vm, ArgsView args) { return x; })
  119. #ifdef POCKETPY_H
  120. #define FATAL_ERROR() throw std::runtime_error( "L" + std::to_string(__LINE__) + " FATAL_ERROR()!");
  121. #else
  122. #define FATAL_ERROR() throw std::runtime_error( __FILE__ + std::string(":") + std::to_string(__LINE__) + " FATAL_ERROR()!");
  123. #endif
  124. inline const float kInstAttrLoadFactor = 0.67f;
  125. inline const float kTypeAttrLoadFactor = 0.5f;
  126. struct PyObject;
  127. #define BITS(p) (reinterpret_cast<i64>(p))
  128. inline bool is_tagged(PyObject* p) noexcept { return (BITS(p) & 0b11) != 0b00; }
  129. inline bool is_int(PyObject* p) noexcept { return (BITS(p) & 0b11) == 0b01; }
  130. inline bool is_float(PyObject* p) noexcept { return (BITS(p) & 0b11) == 0b10; }
  131. inline bool is_special(PyObject* p) noexcept { return (BITS(p) & 0b11) == 0b11; }
  132. inline bool is_both_int_or_float(PyObject* a, PyObject* b) noexcept {
  133. return is_tagged(a) && is_tagged(b);
  134. }
  135. inline bool is_both_int(PyObject* a, PyObject* b) noexcept {
  136. return is_int(a) && is_int(b);
  137. }
  138. // special singals, is_tagged() for them is true
  139. inline PyObject* const PY_NULL = (PyObject*)0b000011; // tagged null
  140. inline PyObject* const PY_BEGIN_CALL = (PyObject*)0b010011;
  141. inline PyObject* const PY_OP_CALL = (PyObject*)0b100011;
  142. inline PyObject* const PY_OP_YIELD = (PyObject*)0b110011;
  143. #ifdef _WIN32
  144. char kPlatformSep = '\\';
  145. #else
  146. char kPlatformSep = '/';
  147. #endif
  148. } // namespace pkpy