Skip to content

Commit

Permalink
separate runtime to core and base parts (#1006)
Browse files Browse the repository at this point in the history
  • Loading branch information
astrophysik authored Jun 26, 2024
1 parent 39d905a commit 91a1f28
Show file tree
Hide file tree
Showing 217 changed files with 1,117 additions and 883 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ include(${COMMON_DIR}/common.cmake)
include(${COMMON_DIR}/tl/tl.cmake)
include(${COMMON_DIR}/unicode/unicode.cmake)

include(${BASE_DIR}/runtime-core/runtime-core.cmake)
include(${BASE_DIR}/runtime/runtime.cmake)
include(${BASE_DIR}/server/server.cmake)
include(${BASE_DIR}/compiler/compiler.cmake)
Expand Down
2 changes: 1 addition & 1 deletion builtin-functions/_functions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ function vk_stats_hll_is_packed($hll ::: string) ::: bool;
/** @kphp-extern-func-info cpp_template_call */
function vk_dot_product ($a ::: array, $b ::: array) ::: ^1[*] | ^2[*];

/** defined in kphp_core.h **/
/** defined in runtime-core.h **/
function likely ($x ::: bool) ::: bool;
function unlikely ($x ::: bool) ::: bool;

Expand Down
1 change: 1 addition & 0 deletions common/stats/provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <vector>

#include "common/stats/buffer.h"
#include "common/mixin/not_copyable.h"

constexpr int am_get_memory_usage_self = 1;
constexpr int am_get_memory_usage_overall = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ Then you think about type inferring. What type should using power operator lead
On type inferring step, you introduce *recalc_power()*, call it as necessary, and implement given logic.

Next, you need to tie codegeneration and C++ implementation together.
As you resulted in having 3 different inferrings, you need at least 3 C++ functions: say, you name them *int_power()*, *float_power()*, and *mixed_power()* and implement them somewhere in runtime — in *kphp_core.h* for example; the last one not only returns *mixed* but accepts *mixed* also, even though arguments could be inferred as clean types, they would be implicitly converted to *mixed* — it's easier to create a single function without lots overloads in this case.
As you resulted in having 3 different inferrings, you need at least 3 C++ functions: say, you name them *int_power()*, *float_power()*, and *mixed_power()* and implement them somewhere in runtime — in *runtime-core.h* for example; the last one not only returns *mixed* but accepts *mixed* also, even though arguments could be inferred as clean types, they would be implicitly converted to *mixed* — it's easier to create a single function without lots overloads in this case.
On codegeneration of *op_pow*, you take the inferred result and output calling one of these functions.

To support `**=`, you consider how `+=` and similar are made: "set operator" depends on "base operator".
Expand Down
27 changes: 27 additions & 0 deletions runtime-core/allocator/script-allocator-managed.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#pragma once

#include <cstddef>

#include "runtime-core/runtime-core.h"

class ScriptAllocatorManaged {
public:
static void *operator new(size_t size) noexcept {
return RuntimeAllocator::current().alloc_script_memory(size);
}

static void *operator new(size_t, void *ptr) noexcept {
return ptr;
}

static void operator delete(void *ptr, size_t size) noexcept {
RuntimeAllocator::current().free_script_memory(ptr, size);
}

static void *operator new[](size_t count) = delete;
static void operator delete[](void *ptr, size_t sz) = delete;
static void operator delete[](void *ptr) = delete;

protected:
~ScriptAllocatorManaged() = default;
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include "common/smart_ptrs/intrusive_ptr.h"

#ifndef INCLUDED_FROM_KPHP_CORE
#error "this file must be included only from kphp_core.h"
#error "this file must be included only from runtime-core.h"
#endif

// PHP classes produce the C++ structures of the form:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#ifndef INCLUDED_FROM_KPHP_CORE
#error "this file must be included only from kphp_core.h"
#error "this file must be included only from runtime-core.h"
#endif

template<class T>
Expand Down Expand Up @@ -43,8 +43,7 @@ class_instance<T> class_instance<T>::alloc(Args &&... args) {
template<class T>
inline class_instance<T> class_instance<T>::empty_alloc() {
static_assert(std::is_empty<T>{}, "class T must be empty");
static uint32_t obj;
obj++;
uint32_t obj = ++KphpCoreContext::current().empty_obj_count;
new (&o) vk::intrusive_ptr<T>(reinterpret_cast<T*>(obj));
return *this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

#include "common/php-functions.h"

#include "runtime/allocator.h"
#include "runtime-core/allocator/script-allocator-managed.h"

class abstract_refcountable_php_interface : public ManagedThroughDlAllocator {
class abstract_refcountable_php_interface : public ScriptAllocatorManaged {
public:
abstract_refcountable_php_interface() __attribute__((always_inline)) = default;
virtual ~abstract_refcountable_php_interface() noexcept __attribute__((always_inline)) = default;
Expand Down Expand Up @@ -98,7 +98,7 @@ class refcountable_polymorphic_php_classes_virt<> : public virtual abstract_refc
};

template<class Derived>
class refcountable_php_classes : public ManagedThroughDlAllocator {
class refcountable_php_classes : public ScriptAllocatorManaged {
public:
void add_ref() noexcept {
if (refcnt < ExtraRefCnt::for_global_const) {
Expand All @@ -118,7 +118,7 @@ class refcountable_php_classes : public ManagedThroughDlAllocator {
if (refcnt == 0) {
static_assert(!std::is_polymorphic<Derived>{}, "Derived may not be polymorphic");
/**
* because of inheritance from ManagedThroughDlAllocator, which override operators new/delete
* because of inheritance from ScriptAllocatorManaged, which override operators new/delete
* we should have vptr for passing proper sizeof of Derived class, but we don't want to increase size of every class
* therefore we use static_cast here
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#pragma once

#include "runtime/migration_php8.h"
#include "runtime-core/utils/migration-php8.h"

#ifndef INCLUDED_FROM_KPHP_CORE
#error "this file must be included only from kphp_core.h"
#error "this file must be included only from runtime-core.h"
#endif

namespace impl_ {
Expand Down Expand Up @@ -92,7 +92,7 @@ inline bool eq2_number_string_as_php8(T lhs, const string &rhs) {

inline bool eq2(int64_t lhs, const string &rhs) {
const auto php7_result = eq2(lhs, rhs.to_float());
if (show_migration_php8_warning & MIGRATION_PHP8_STRING_COMPARISON_FLAG) {
if (KphpCoreContext::current().show_migration_php8_warning & MIGRATION_PHP8_STRING_COMPARISON_FLAG) {
const auto php8_result = eq2_number_string_as_php8(lhs, rhs);
if (php7_result == php8_result) {
return php7_result;
Expand All @@ -113,7 +113,7 @@ inline bool eq2(const string &lhs, int64_t rhs) {

inline bool eq2(double lhs, const string &rhs) {
const auto php7_result = lhs == rhs.to_float();
if (show_migration_php8_warning & MIGRATION_PHP8_STRING_COMPARISON_FLAG) {
if (KphpCoreContext::current().show_migration_php8_warning & MIGRATION_PHP8_STRING_COMPARISON_FLAG) {
const auto php8_result = eq2_number_string_as_php8(lhs, rhs);
if (php7_result == php8_result) {
return php7_result;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#ifndef INCLUDED_FROM_KPHP_CORE
#error "this file must be included only from kphp_core.h"
#error "this file must be included only from runtime-core.h"
#endif

template<class T, class = enable_for_bool_int_double<T>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

#pragma once

#include "runtime/array_iterator.h"
#include "runtime/include.h"
#include "runtime-core/core-types/decl/array_iterator.h"
#include "runtime-core/include.h"

#ifndef INCLUDED_FROM_KPHP_CORE
#error "this file must be included only from kphp_core.h"
#error "this file must be included only from runtime-core.h"
#endif

struct array_size {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "common/type_traits/list_of_types.h"
#include "common/sanitizer.h"

#include "runtime/declarations.h"
#include "runtime-core/core-types/decl/declarations.h"

template<class T>
class array_iterator {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#pragma once

#ifndef INCLUDED_FROM_KPHP_CORE
#error "this file must be included only from kphp_core.h"
#error "this file must be included only from runtime-core.h"
#endif

template<typename T>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
#include "common/type_traits/is_constructible.h"
#include "common/type_traits/list_of_types.h"

#include "runtime/php_assert.h"
#include "runtime/declarations.h"
#include "runtime-core/utils/kphp-assert-core.h"
#include "runtime-core/core-types/decl/declarations.h"

template<class T>
class Optional;
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
#pragma once

#ifndef INCLUDED_FROM_KPHP_CORE
#error "this file must be included only from kphp_core.h"
#error "this file must be included only from runtime-core.h"
#endif

#define STRING_BUFFER_ERROR_FLAG_ON -1
#define STRING_BUFFER_ERROR_FLAG_OFF 0
#define STRING_BUFFER_ERROR_FLAG_FAILED 1

class string_buffer {
static string::size_type MIN_BUFFER_LEN;
static string::size_type MAX_BUFFER_LEN;
char *buffer_end;
char *buffer_begin;
string::size_type buffer_len;
Expand All @@ -21,7 +19,6 @@ class string_buffer {
string_buffer &operator=(const string_buffer &other) = delete;

public:
static int string_buffer_error_flag;
explicit string_buffer(string::size_type buffer_len = 4000) noexcept;

inline string_buffer &clean() noexcept;
Expand Down Expand Up @@ -56,7 +53,7 @@ public:

~string_buffer() noexcept;

friend void init_string_buffer_lib(int max_length);
friend void init_string_buffer_lib(string::size_type min_length, string::size_type max_length);

inline void debug_print() const;

Expand All @@ -66,5 +63,8 @@ public:
friend inline bool operator!=(const string_buffer &lhs, const string_buffer &rhs);
};

extern string_buffer static_SB;
extern string_buffer static_SB_spare;
struct string_buffer_lib_context {
string::size_type MIN_BUFFER_LEN;
string::size_type MAX_BUFFER_LEN;
int error_flag = 0;
};
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#ifndef INCLUDED_FROM_KPHP_CORE
#error "this file must be included only from kphp_core.h"
#error "this file must be included only from runtime-core.h"
#endif

using string_size_type = uint32_t;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "common/algorithms/fastmod.h"

#ifndef INCLUDED_FROM_KPHP_CORE
#error "this file must be included only from kphp_core.h"
#error "this file must be included only from runtime-core.h"
#endif

array_size::array_size(int64_t int_size, bool is_vector) noexcept
Expand Down Expand Up @@ -237,7 +237,7 @@ template<class T>
typename array<T>::array_inner *array<T>::array_inner::create(int64_t new_int_size, bool is_vector) {
const size_t mem_size = estimate_size(new_int_size, is_vector);
if (is_vector) {
auto p = reinterpret_cast<array_inner *>(dl::allocate(mem_size));
auto p = reinterpret_cast<array_inner *>(RuntimeAllocator::current().alloc_script_memory(mem_size));
p->is_vector_internal = true;
p->ref_cnt = 0;
p->max_key = -1;
Expand All @@ -250,7 +250,7 @@ typename array<T>::array_inner *array<T>::array_inner::create(int64_t new_int_si
return reinterpret_cast<array_inner *>(static_cast<char *>(mem) + sizeof(array_inner_fields_for_map));
};

array_inner *p = shift_pointer_to_array_inner(dl::allocate0(mem_size));
array_inner *p = shift_pointer_to_array_inner(RuntimeAllocator::current().alloc0_script_memory(mem_size));
p->is_vector_internal = false;
p->ref_cnt = 0;
p->max_key = -1;
Expand All @@ -275,7 +275,7 @@ void array<T>::array_inner::dispose() {
((T *)entries)[i].~T();
}

dl::deallocate((void *)this, sizeof_vector(buf_size));
RuntimeAllocator::current().free_script_memory((void *)this, sizeof_vector(buf_size));
return;
}

Expand All @@ -288,7 +288,7 @@ void array<T>::array_inner::dispose() {

php_assert(this != empty_array());
auto shifted_this = reinterpret_cast<char *>(this) - sizeof(array_inner_fields_for_map);
dl::deallocate(shifted_this, sizeof_map(buf_size));
RuntimeAllocator::current().free_script_memory(shifted_this, sizeof_map(buf_size));
}
}
}
Expand Down Expand Up @@ -716,7 +716,7 @@ void array<T>::mutate_to_size(int64_t int_size) {
php_critical_error ("max array size exceeded: int_size = %" PRIi64, int_size);
}
const auto new_int_buff_size = static_cast<uint32_t>(int_size);
p = static_cast<array_inner *>(dl::reallocate(p, p->sizeof_vector(new_int_buff_size), p->sizeof_vector(p->buf_size)));
p = static_cast<array_inner *>(RuntimeAllocator::current().realloc_script_memory(p, p->sizeof_vector(new_int_buff_size), p->sizeof_vector(p->buf_size)));
p->buf_size = new_int_buff_size;
}

Expand Down Expand Up @@ -1650,7 +1650,7 @@ array<T> &array<T>::operator+=(const array<T> &other) {
p = new_array;
} else if (p->buf_size < size + 2) {
uint32_t new_size = max(size + 2, p->buf_size * 2);
p = (array_inner *)dl::reallocate((void *)p, p->sizeof_vector(new_size), p->sizeof_vector(p->buf_size));
p = (array_inner *)RuntimeAllocator::current().realloc_script_memory((void *)p, p->sizeof_vector(new_size), p->sizeof_vector(p->buf_size));
p->buf_size = new_size;
}

Expand Down Expand Up @@ -1894,7 +1894,7 @@ void array<T>::sort(const T1 &compare, bool renumber) {
mutate_if_map_shared();
}

array_bucket **arTmp = (array_bucket **)dl::allocate(n * sizeof(array_bucket * ));
array_bucket **arTmp = (array_bucket **)RuntimeAllocator::current().alloc_script_memory(n * sizeof(array_bucket * ));
uint32_t i = 0;
for (array_bucket *it = p->begin(); it != p->end(); it = p->next(it)) {
arTmp[i++] = it;
Expand All @@ -1916,7 +1916,7 @@ void array<T>::sort(const T1 &compare, bool renumber) {
arTmp[n - 1]->next = p->get_pointer(p->end());
p->end()->prev = p->get_pointer(arTmp[n - 1]);

dl::deallocate(arTmp, n * sizeof(array_bucket * ));
RuntimeAllocator::current().free_script_memory(arTmp, n * sizeof(array_bucket * ));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright (c) 2021 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#include "runtime/kphp_core.h"
#include "runtime-core/runtime-core.h"

void mixed::destroy() noexcept {
switch (get_type()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

#include "common/algorithms/find.h"

#include "runtime/migration_php8.h"
#include "runtime-core/utils/migration-php8.h"

#ifndef INCLUDED_FROM_KPHP_CORE
#error "this file must be included only from kphp_core.h"
#error "this file must be included only from runtime-core.h"
#endif

static_assert(vk::all_of_equal(sizeof(string), sizeof(double), sizeof(array<mixed>)), "sizeof of array<mixed>, string and double must be equal");
Expand Down Expand Up @@ -1760,7 +1760,7 @@ inline bool less_string_number_as_php8_impl(const string &lhs, T rhs) {

template <typename T>
inline bool less_number_string_as_php8(bool php7_result, T lhs, const string &rhs) {
if (show_migration_php8_warning & MIGRATION_PHP8_STRING_COMPARISON_FLAG) {
if (KphpCoreContext::current().show_migration_php8_warning & MIGRATION_PHP8_STRING_COMPARISON_FLAG) {
const auto php8_result = less_number_string_as_php8_impl(lhs, rhs);
if (php7_result == php8_result) {
return php7_result;
Expand All @@ -1778,7 +1778,7 @@ inline bool less_number_string_as_php8(bool php7_result, T lhs, const string &rh

template <typename T>
inline bool less_string_number_as_php8(bool php7_result, const string &lhs, T rhs) {
if (show_migration_php8_warning & MIGRATION_PHP8_STRING_COMPARISON_FLAG) {
if (KphpCoreContext::current().show_migration_php8_warning & MIGRATION_PHP8_STRING_COMPARISON_FLAG) {
const auto php8_result = less_string_number_as_php8_impl(lhs, rhs);
if (php7_result == php8_result) {
return php7_result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// Copyright (c) 2021 LLC «V Kontakte»
// Distributed under the GPL v3 License, see LICENSE.notice.txt

#include "runtime/kphp_core.h"
#include "runtime-core/runtime-core.h"

// Don't move this destructor to the headers, it spoils addr2line traces
string::~string() noexcept {
Expand Down
Loading

0 comments on commit 91a1f28

Please sign in to comment.