common.h 4.4 KB

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