common.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #ifndef PK_ENABLE_OS
  40. #ifdef __ANDROID__
  41. #include <android/ndk-version.h>
  42. #if __NDK_MAJOR__ <= 22
  43. #define PK_ENABLE_OS 0
  44. #else
  45. #define PK_ENABLE_OS 1
  46. #endif
  47. #else
  48. #define PK_ENABLE_OS 1
  49. #endif
  50. #endif
  51. // This is the maximum number of arguments in a function declaration
  52. // including positional arguments, keyword-only arguments, and varargs
  53. #define PK_MAX_CO_VARNAMES 255
  54. #if _MSC_VER
  55. #define PK_ENABLE_COMPUTED_GOTO 0
  56. #define UNREACHABLE() __assume(0)
  57. #else
  58. #define PK_ENABLE_COMPUTED_GOTO 1
  59. #define UNREACHABLE() __builtin_unreachable()
  60. #if DEBUG_CEVAL_STEP
  61. #undef PK_ENABLE_COMPUTED_GOTO
  62. #endif
  63. #endif
  64. namespace pkpy{
  65. namespace std = ::std;
  66. template <size_t T>
  67. struct NumberTraits;
  68. template <>
  69. struct NumberTraits<4> {
  70. using int_t = int32_t;
  71. using float_t = float;
  72. template<typename... Args>
  73. static int_t stoi(Args&&... args) { return std::stoi(std::forward<Args>(args)...); }
  74. template<typename... Args>
  75. static float_t stof(Args&&... args) { return std::stof(std::forward<Args>(args)...); }
  76. };
  77. template <>
  78. struct NumberTraits<8> {
  79. using int_t = int64_t;
  80. using float_t = double;
  81. template<typename... Args>
  82. static int_t stoi(Args&&... args) { return std::stoll(std::forward<Args>(args)...); }
  83. template<typename... Args>
  84. static float_t stof(Args&&... args) { return std::stod(std::forward<Args>(args)...); }
  85. };
  86. using Number = NumberTraits<sizeof(void*)>;
  87. using i64 = Number::int_t;
  88. using f64 = Number::float_t;
  89. static_assert(sizeof(i64) == sizeof(void*));
  90. static_assert(sizeof(f64) == sizeof(void*));
  91. static_assert(std::numeric_limits<f64>::is_iec559);
  92. struct Dummy { };
  93. struct DummyInstance { };
  94. struct DummyModule { };
  95. struct NoReturn { };
  96. struct Discarded { };
  97. struct Type {
  98. int index;
  99. Type(): index(-1) {}
  100. Type(int index): index(index) {}
  101. bool operator==(Type other) const noexcept { return this->index == other.index; }
  102. bool operator!=(Type other) const noexcept { return this->index != other.index; }
  103. operator int() const noexcept { return this->index; }
  104. };
  105. #define THREAD_LOCAL // thread_local
  106. #define CPP_LAMBDA(x) ([](VM* vm, ArgsView args) { return x; })
  107. #ifdef POCKETPY_H
  108. #define FATAL_ERROR() throw std::runtime_error( "L" + std::to_string(__LINE__) + " FATAL_ERROR()!");
  109. #else
  110. #define FATAL_ERROR() throw std::runtime_error( __FILE__ + std::string(":") + std::to_string(__LINE__) + " FATAL_ERROR()!");
  111. #endif
  112. inline const float kInstAttrLoadFactor = 0.67f;
  113. inline const float kTypeAttrLoadFactor = 0.5f;
  114. struct PyObject;
  115. #define BITS(p) (reinterpret_cast<i64>(p))
  116. inline bool is_tagged(PyObject* p) noexcept { return (BITS(p) & 0b11) != 0b00; }
  117. inline bool is_int(PyObject* p) noexcept { return (BITS(p) & 0b11) == 0b01; }
  118. inline bool is_float(PyObject* p) noexcept { return (BITS(p) & 0b11) == 0b10; }
  119. inline bool is_special(PyObject* p) noexcept { return (BITS(p) & 0b11) == 0b11; }
  120. inline bool is_both_int_or_float(PyObject* a, PyObject* b) noexcept {
  121. return is_tagged(a) && is_tagged(b);
  122. }
  123. inline bool is_both_int(PyObject* a, PyObject* b) noexcept {
  124. return is_int(a) && is_int(b);
  125. }
  126. // special singals, is_tagged() for them is true
  127. inline PyObject* const PY_NULL = (PyObject*)0b000011; // tagged null
  128. inline PyObject* const PY_BEGIN_CALL = (PyObject*)0b010011;
  129. inline PyObject* const PY_OP_CALL = (PyObject*)0b100011;
  130. inline PyObject* const PY_OP_YIELD = (PyObject*)0b110011;
  131. } // namespace pkpy