common.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 <sstream>
  10. #include <regex>
  11. #include <cmath>
  12. #include <stdexcept>
  13. #include <vector>
  14. #include <string>
  15. #include <cstring>
  16. #include <chrono>
  17. #include <string_view>
  18. #include <iomanip>
  19. #include <memory>
  20. #include <functional>
  21. #include <iostream>
  22. #include <map>
  23. #include <set>
  24. #include <algorithm>
  25. #include <random>
  26. #include <initializer_list>
  27. #include <variant>
  28. #include <type_traits>
  29. #define PK_VERSION "0.9.9"
  30. // debug macros
  31. #define DEBUG_NO_BUILTIN_MODULES 0
  32. #define DEBUG_EXTRA_CHECK 1
  33. #define DEBUG_DIS_EXEC 0
  34. #define DEBUG_CEVAL_STEP 0
  35. #define DEBUG_CEVAL_STEP_MIN 0
  36. #define DEBUG_FULL_EXCEPTION 0
  37. #define DEBUG_MEMORY_POOL 0
  38. #define DEBUG_NO_MEMORY_POOL 0
  39. #define DEBUG_NO_AUTO_GC 0
  40. #define DEBUG_GC_STATS 0
  41. #if (defined(__ANDROID__) && __ANDROID_API__ <= 22) || defined(__EMSCRIPTEN__)
  42. #define PK_ENABLE_FILEIO 0
  43. #else
  44. #define PK_ENABLE_FILEIO 0 // TODO: refactor this
  45. #endif
  46. #if _MSC_VER
  47. #define PK_ENABLE_COMPUTED_GOTO 0
  48. #define UNREACHABLE() __assume(0)
  49. #else
  50. #define PK_ENABLE_COMPUTED_GOTO 1
  51. #define UNREACHABLE() __builtin_unreachable()
  52. #if DEBUG_CEVAL_STEP || DEBUG_CEVAL_STEP_MIN
  53. #undef PK_ENABLE_COMPUTED_GOTO
  54. #endif
  55. #endif
  56. #if defined(__EMSCRIPTEN__) || defined(__arm__) || defined(__i386__)
  57. typedef int32_t i64;
  58. typedef float f64;
  59. #define S_TO_INT(...) static_cast<i64>(std::stoi(__VA_ARGS__))
  60. #define S_TO_FLOAT(...) static_cast<f64>(std::stof(__VA_ARGS__))
  61. #else
  62. typedef int64_t i64;
  63. typedef double f64;
  64. #define S_TO_INT(...) static_cast<i64>(std::stoll(__VA_ARGS__))
  65. #define S_TO_FLOAT(...) static_cast<f64>(std::stod(__VA_ARGS__))
  66. #endif
  67. namespace pkpy{
  68. namespace std = ::std;
  69. struct Dummy { };
  70. struct DummyInstance { };
  71. struct DummyModule { };
  72. struct Discarded { };
  73. struct Type {
  74. int index;
  75. Type(): index(-1) {}
  76. Type(int index): index(index) {}
  77. bool operator==(Type other) const noexcept { return this->index == other.index; }
  78. bool operator!=(Type other) const noexcept { return this->index != other.index; }
  79. operator int() const noexcept { return this->index; }
  80. };
  81. #define THREAD_LOCAL // thread_local
  82. #define CPP_LAMBDA(x) ([](VM* vm, ArgsView args) { return x; })
  83. #define CPP_NOT_IMPLEMENTED() ([](VM* vm, ArgsView args) { vm->NotImplementedError(); return vm->None; })
  84. #ifdef POCKETPY_H
  85. #define FATAL_ERROR() throw std::runtime_error( "L" + std::to_string(__LINE__) + " FATAL_ERROR()!");
  86. #else
  87. #define FATAL_ERROR() throw std::runtime_error( __FILE__ + std::string(":") + std::to_string(__LINE__) + " FATAL_ERROR()!");
  88. #endif
  89. inline const float kInstAttrLoadFactor = 0.67f;
  90. inline const float kTypeAttrLoadFactor = 0.5f;
  91. static_assert(sizeof(i64) == sizeof(int*));
  92. static_assert(sizeof(f64) == sizeof(int*));
  93. static_assert(std::numeric_limits<float>::is_iec559);
  94. static_assert(std::numeric_limits<double>::is_iec559);
  95. struct PyObject;
  96. #define BITS(p) (reinterpret_cast<i64>(p))
  97. inline bool is_tagged(PyObject* p) noexcept { return (BITS(p) & 0b11) != 0b00; }
  98. inline bool is_int(PyObject* p) noexcept { return (BITS(p) & 0b11) == 0b01; }
  99. inline bool is_float(PyObject* p) noexcept { return (BITS(p) & 0b11) == 0b10; }
  100. inline bool is_both_int_or_float(PyObject* a, PyObject* b) noexcept {
  101. return is_tagged(a) && is_tagged(b);
  102. }
  103. inline bool is_both_int(PyObject* a, PyObject* b) noexcept {
  104. return is_int(a) && is_int(b);
  105. }
  106. struct Expr;
  107. typedef std::unique_ptr<Expr> Expr_;
  108. } // namespace pkpy