common.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.8"
  30. // debug macros
  31. #define DEBUG_NO_BUILTIN_MODULES 0
  32. #define DEBUG_EXTRA_CHECK 0
  33. #define DEBUG_DIS_EXEC 0
  34. #define DEBUG_CEVAL_STEP 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. #if (defined(__ANDROID__) && __ANDROID_API__ <= 22) || defined(__EMSCRIPTEN__)
  41. #define PK_ENABLE_FILEIO 0
  42. #else
  43. #define PK_ENABLE_FILEIO 0 // TODO: refactor this
  44. #endif
  45. #if _MSC_VER
  46. #define PK_ENABLE_COMPUTED_GOTO 0
  47. #define UNREACHABLE() __assume(0)
  48. #else
  49. #define PK_ENABLE_COMPUTED_GOTO 1
  50. #define UNREACHABLE() __builtin_unreachable()
  51. #endif
  52. #if defined(__EMSCRIPTEN__) || defined(__arm__) || defined(__i386__)
  53. typedef int32_t i64;
  54. typedef float f64;
  55. #define S_TO_INT(...) static_cast<i64>(std::stoi(__VA_ARGS__))
  56. #define S_TO_FLOAT(...) static_cast<f64>(std::stof(__VA_ARGS__))
  57. #else
  58. typedef int64_t i64;
  59. typedef double f64;
  60. #define S_TO_INT(...) static_cast<i64>(std::stoll(__VA_ARGS__))
  61. #define S_TO_FLOAT(...) static_cast<f64>(std::stod(__VA_ARGS__))
  62. #endif
  63. namespace pkpy{
  64. namespace std = ::std;
  65. struct Dummy { };
  66. struct DummyInstance { };
  67. struct DummyModule { };
  68. struct Discarded { };
  69. struct Type {
  70. int index;
  71. Type(): index(-1) {}
  72. Type(int index): index(index) {}
  73. bool operator==(Type other) const noexcept { return this->index == other.index; }
  74. bool operator!=(Type other) const noexcept { return this->index != other.index; }
  75. operator int() const noexcept { return this->index; }
  76. };
  77. #define THREAD_LOCAL // thread_local
  78. #define CPP_LAMBDA(x) ([](VM* vm, Args& args) { return x; })
  79. #define CPP_NOT_IMPLEMENTED() ([](VM* vm, Args& args) { vm->NotImplementedError(); return vm->None; })
  80. #ifdef POCKETPY_H
  81. #define FATAL_ERROR() throw std::runtime_error( "L" + std::to_string(__LINE__) + " FATAL_ERROR()!");
  82. #else
  83. #define FATAL_ERROR() throw std::runtime_error( __FILE__ + std::string(":") + std::to_string(__LINE__) + " FATAL_ERROR()!");
  84. #endif
  85. inline const float kInstAttrLoadFactor = 0.67f;
  86. inline const float kTypeAttrLoadFactor = 0.5f;
  87. static_assert(sizeof(i64) == sizeof(int*));
  88. static_assert(sizeof(f64) == sizeof(int*));
  89. static_assert(std::numeric_limits<float>::is_iec559);
  90. static_assert(std::numeric_limits<double>::is_iec559);
  91. struct PyObject;
  92. #define BITS(p) (reinterpret_cast<i64>(p))
  93. inline bool is_tagged(PyObject* p) noexcept { return (BITS(p) & 0b11) != 0b00; }
  94. inline bool is_int(PyObject* p) noexcept { return (BITS(p) & 0b11) == 0b01; }
  95. inline bool is_float(PyObject* p) noexcept { return (BITS(p) & 0b11) == 0b10; }
  96. inline bool is_both_int_or_float(PyObject* a, PyObject* b) noexcept {
  97. return is_tagged(a) && is_tagged(b);
  98. }
  99. inline bool is_both_int(PyObject* a, PyObject* b) noexcept {
  100. return is_int(a) && is_int(b);
  101. }
  102. struct Expr;
  103. typedef std::unique_ptr<Expr> Expr_;
  104. } // namespace pkpy