From 28c8f68d8d15f085a1ce8eed5f70cc8d7bbeca35 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 11 Jun 2023 13:57:07 +0800 Subject: [PATCH] add `config.h` --- amalgamate.py | 2 +- src/common.h | 47 ++++++++----------------------------------- src/config.h | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/frame.h | 2 +- src/obj.h | 5 +++++ 5 files changed, 70 insertions(+), 41 deletions(-) create mode 100644 src/config.h diff --git a/amalgamate.py b/amalgamate.py index 67c8d5a3..a57380c4 100644 --- a/amalgamate.py +++ b/amalgamate.py @@ -6,7 +6,7 @@ with open("src/opcodes.h", "rt", encoding='utf-8') as f: OPCODES_TEXT = f.read() pipeline = [ - ["common.h", "memory.h", "vector.h", "str.h", "tuplelist.h", "namedict.h", "error.h", "lexer.h"], + ["config.h", "common.h", "memory.h", "vector.h", "str.h", "tuplelist.h", "namedict.h", "error.h", "lexer.h"], ["obj.h", "dict.h", "codeobject.h", "frame.h"], ["gc.h", "vm.h", "ceval.h", "expr.h", "compiler.h", "repl.h"], ["_generated.h", "cffi.h", "iter.h", "base64.h", "re.h", "linalg.h", "easing.h", "requests.h", "io.h"], diff --git a/src/common.h b/src/common.h index 98c17a81..e15ef490 100644 --- a/src/common.h +++ b/src/common.h @@ -29,36 +29,17 @@ #include #include -#define PK_VERSION "1.0.2" +#include "config.h" -// debug macros -#define DEBUG_NO_BUILTIN_MODULES 0 -#define DEBUG_EXTRA_CHECK 0 -#define DEBUG_DIS_EXEC 0 -#define DEBUG_CEVAL_STEP 0 -#define DEBUG_FULL_EXCEPTION 0 -#define DEBUG_MEMORY_POOL 0 -#define DEBUG_NO_MEMORY_POOL 0 -#define DEBUG_NO_AUTO_GC 0 -#define DEBUG_GC_STATS 0 +#define PK_VERSION "1.0.3" -// config macros -#ifndef PK_ENABLE_OS +/*******************************************************************************/ -#ifdef __ANDROID__ -#include - -#if __NDK_MAJOR__ <= 22 -#define PK_ENABLE_OS 0 -#else -#define PK_ENABLE_OS 1 +#if PK_ENABLE_STD_FUNCTION +#include #endif -#else -#define PK_ENABLE_OS 1 -#endif - -#endif +/*******************************************************************************/ #if PK_ENABLE_THREAD #define THREAD_LOCAL thread_local @@ -77,11 +58,6 @@ struct GIL { #endif /*******************************************************************************/ - -// This is the maximum number of arguments in a function declaration -// including positional arguments, keyword-only arguments, and varargs -#define PK_MAX_CO_VARNAMES 255 - #if _MSC_VER #define PK_ENABLE_COMPUTED_GOTO 0 #define UNREACHABLE() __assume(0) @@ -95,6 +71,8 @@ struct GIL { #endif +/*******************************************************************************/ + namespace pkpy{ namespace std = ::std; @@ -165,9 +143,6 @@ struct Type { #define PK_ASSERT(x) if(!(x)) FATAL_ERROR(); -inline const float kInstAttrLoadFactor = 0.67f; -inline const float kTypeAttrLoadFactor = 0.5f; - struct PyObject; #define BITS(p) (reinterpret_cast(p)) inline bool is_tagged(PyObject* p) noexcept { return (BITS(p) & 0b11) != 0b00; } @@ -188,10 +163,4 @@ inline PyObject* const PY_NULL = (PyObject*)0b000011; // tagged null inline PyObject* const PY_OP_CALL = (PyObject*)0b100011; inline PyObject* const PY_OP_YIELD = (PyObject*)0b110011; -#ifdef _WIN32 - inline const char kPlatformSep = '\\'; -#else - inline const char kPlatformSep = '/'; -#endif - } // namespace pkpy \ No newline at end of file diff --git a/src/config.h b/src/config.h new file mode 100644 index 00000000..3e61abfc --- /dev/null +++ b/src/config.h @@ -0,0 +1,55 @@ +#pragma once + +#ifdef PK_USER_CONFIG + +#include "user_config.h" + +#else + +/*************** feature settings ***************/ +#define PK_ENABLE_OS 1 +#define PK_ENABLE_THREAD 0 + +// Whether to use `std::function` to do bindings +// By default, the function to be binded must be a C function pointer with no capture +// which is fast and simple. However, someone thinks it's not convenient enough. +// By setting this to 1, capturing lambdas can be binded, +// but it's slower and may cause severe "code bloat", also needs more time to compile. +#define PK_ENABLE_STD_FUNCTION 0 + +/*************** internal settings ***************/ + +// This is the maximum size of the value stack in void* units, not bytes +// The actual size is `sizeof(void*) * PK_VM_STACK_SIZE` +#define PK_VM_STACK_SIZE 32768 + +// This is the maximum number of arguments in a function declaration +// including positional arguments, keyword-only arguments, and varargs +#define PK_MAX_CO_VARNAMES 255 + +// Hash table load factor (smaller ones mean less collision but more memory) +inline const float kInstAttrLoadFactor = 0.67f; +inline const float kTypeAttrLoadFactor = 0.5f; + +#ifdef _WIN32 + inline const char kPlatformSep = '\\'; +#else + inline const char kPlatformSep = '/'; +#endif + +/*************** debug settings ***************/ + +// Enable this may help you to find bugs in the VM +#define DEBUG_EXTRA_CHECK 0 + +// Do not edit the following settings unless you know what you are doing +#define DEBUG_NO_BUILTIN_MODULES 0 +#define DEBUG_DIS_EXEC 0 +#define DEBUG_CEVAL_STEP 0 +#define DEBUG_FULL_EXCEPTION 0 +#define DEBUG_MEMORY_POOL 0 +#define DEBUG_NO_MEMORY_POOL 0 +#define DEBUG_NO_AUTO_GC 0 +#define DEBUG_GC_STATS 0 + +#endif \ No newline at end of file diff --git a/src/frame.h b/src/frame.h index faf13dd7..3960dfc2 100644 --- a/src/frame.h +++ b/src/frame.h @@ -100,7 +100,7 @@ struct ValueStackImpl { ValueStackImpl& operator=(ValueStackImpl&&) = delete; }; -using ValueStack = ValueStackImpl<32768>; +using ValueStack = ValueStackImpl; struct Frame { int _ip = -1; diff --git a/src/obj.h b/src/obj.h index 69919e98..f187fb8e 100644 --- a/src/obj.h +++ b/src/obj.h @@ -11,7 +11,12 @@ struct Frame; struct Function; class VM; +#if PK_ENABLE_STD_FUNCTION +using NativeFuncC = std::function; +#else typedef PyObject* (*NativeFuncC)(VM*, ArgsView); +#endif + typedef int (*LuaStyleFuncC)(VM*); struct NativeFunc {