Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGrulich committed Mar 17, 2024
1 parent c8945e6 commit 16b53ad
Show file tree
Hide file tree
Showing 6 changed files with 293 additions and 69 deletions.
27 changes: 23 additions & 4 deletions nautilus-api/include/Interface/DataTypes/Val.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ namespace nautilus {
concept is_base_type = std::is_base_of<base_value, T>::value;

template<class T>
concept is_fundamental = std::is_fundamental_v<T> && !std::is_reference_v<T>;
concept is_fundamental = std::is_fundamental_v<T> && !std::is_reference_v<T> && !std::is_pointer_v<T>;

template<class T>
concept is_fundamental_ptr = std::is_fundamental_v<std::remove_pointer_t<T>> && std::is_pointer_v<T>;

template<class T>
concept is_member_ptr = !std::is_fundamental_v<std::remove_pointer_t<T>> &&
std::is_standard_layout_v<std::remove_pointer_t<T>> &&
std::is_pointer_v<T>;

template<class T>
concept is_fundamental_ref = std::is_reference_v<T>;
Expand Down Expand Up @@ -121,8 +129,9 @@ namespace nautilus {
}

template<is_fundamental ValueType>
class val<ValueType> :public base_value{
class val<ValueType> : public base_value {
public:
using basic_type = ValueType;
static const inline auto type = NES::Nautilus::TypeIdentifier::create<val>();


Expand Down Expand Up @@ -512,7 +521,16 @@ namespace nautilus {

}

template<typename LHS, typename RHS>
template<class T>
concept is_fundamental_value = requires(val<T> value) {
std::is_fundamental_v<typename T::basic_type>;
};

template<class T>
concept is_fundamental_or_val = is_fundamental<T> || is_fundamental_value<T>;


template<is_fundamental_or_val LHS, is_fundamental_or_val RHS>
auto inline operator+(LHS left, RHS right) {
if constexpr (is_fundamental<LHS>) {
auto leftV = make_value(left);
Expand Down Expand Up @@ -814,7 +832,8 @@ namespace nautilus {
}

template<typename LHS>
std::ostream& operator<<(std::ostream& out, const val<LHS>& v) {
requires is_fundamental<LHS>
std::ostream &operator<<(std::ostream &out, const val<LHS> &v) {
out << v.value;
return out;
}
Expand Down
165 changes: 124 additions & 41 deletions nautilus-api/include/Interface/DataTypes/ValPtr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,117 +5,200 @@
#pragma once

#include "Val.hpp"
#include "../function.hpp"

namespace nautilus {


template<is_fundamental_ref ValueType>
class val<ValueType> : public base_value {
public:
using baseType = std::remove_cvref_t<ValueType>;
using ptrType = baseType *;

val(ptrType ptr, int8_t alignment) : ptr(ptr), alignment(alignment) {};

template<class T>
requires std::is_convertible_v<T, baseType>
void operator=(T other) noexcept {
auto value = make_value<baseType>(other);
// store value
*ptr = value.value;
}
*ptr = details::getRawValue(value);
};

template<class T>
requires std::is_convertible_v<T, baseType>
void operator=(val<T> other) noexcept {
// store value
*ptr = details::getRawValue(other);
};

operator val<baseType>() const {
// load
return val<baseType>(*ptr);
}

private:
ptrType ptr;
int8_t alignment;

val(ptrType ptr, val<size_t> &index, int8_t alignment) : ptr(ptr), index(index), alignment(alignment) {};
private:
const ptrType ptr;
const val<size_t> index;
const int8_t alignment;
friend val<ptrType>;
};

template<typename ValuePtrType> requires std::is_pointer_v<ValuePtrType>
class val_ptr {
template<is_fundamental_ptr ValuePtrType>
class val<ValuePtrType> : public base_value {
public:
using ValType = std::remove_pointer_t<ValuePtrType>;


val_ptr(ValuePtrType ptr, int8_t alignment = 1) : ptr(ptr), alignment(alignment) {};
val(ValuePtrType ptr, val<size_t> index = val<size_t>((size_t) 0), int8_t alignment = 1) : ptr(ptr),
index(index),
alignment(
alignment) {};

inline val<ValuePtrType>(tracing::value_ref *tc, val<size_t> index,
int8_t alignment = 1) : state(tc),
index(index),
alignment(
alignment) {};

val<ValType &> operator*() {
return val<ValType &>(ptr, alignment);
return val<ValType &>(ptr, index, alignment);
};

val<ValType &> operator[](int index) {
auto res = ptr + index;
return val<ValType &>(res, alignment);
val<ValType &> operator[](int indexOffset) {
auto res = ptr + indexOffset;
return val<ValType &>(res, index, alignment);
}


template<typename OtherType>
requires std::is_pointer_v<OtherType>
explicit operator val_ptr<OtherType>() const {
operator val<OtherType>() const {
// ptr cast
return val_ptr<OtherType>((OtherType) ptr);
val<size_t> newIndex = index / sizeof(OtherType);
return val<OtherType>((OtherType) ptr, newIndex, alignment);
}

ValuePtrType ptr;
val<size_t> index;
int8_t alignment;

#ifdef ENABLE_TRACING
tracing::value_ref *state;
#endif
};

#define TRAC_BINARY_PTR_OP(OP) \
if (tracing::inTracer()) {\
auto tc = tracing::traceBinaryOp<tracing::OP, ValueType>(left.state, right.state);\
return val_ptr<ValueType>(tc);\
}
template<typename T, typename M>
M get_member_type(M T::*);

template<typename T, typename M>
T get_class_type(M T::*);

template<typename ValueType>
auto inline operator+(val_ptr<ValueType> left, val<ValueType> right) {
TRAC_BINARY_PTR_OP(ADD)
return left.ptr + details::getRawValue(right);
template<typename T, typename R, R T::*M>
std::size_t offset_of() {
return reinterpret_cast<std::size_t>(&(((T *) 0)->*M));
}

#define OFFSET_OF(m) offset_of<decltype(get_class_type(m)), \
decltype(get_member_type(m)), m>()


template<is_member_ptr ValuePtrType>
class val<ValuePtrType> : public base_value {
public:
using ValType = std::remove_pointer_t<ValuePtrType>;


val(ValuePtrType ptr, val<size_t> index = val<size_t>((size_t) 0), int8_t alignment = 1) : ptr(ptr),
index(index),
alignment(
alignment) {};

inline val<ValuePtrType>(tracing::value_ref *tc, val<size_t> index,
int8_t alignment = 1) : state(tc),
index(index),
alignment(
alignment) {};

template<typename R>
val<R &> get(R ValType::*ptiptr) {
[[maybe_unused]] auto offset = reinterpret_cast<std::size_t>(&(((ValType *) 0)->*ptiptr));
auto memPtr = ((int8_t *) ptr) + offset;
return val<R &>((R *) memPtr, index, alignment);
}

template<typename TP, typename... ValueArguments>
auto
invoke(TP ValType::* __pm, ValueArguments... args) {
auto member = MemberFunction(__pm);
return member(ptr, args...);
}

ValuePtrType ptr;
val<size_t> index;
int8_t alignment;

#ifdef ENABLE_TRACING
tracing::value_ref *state;
#endif
};


template<typename ValueType, typename IndexType>
requires std::is_pointer_v<ValueType>
val<ValueType> inline operator+(val<ValueType> left, IndexType offset) {
val<size_t> index = left.index + offset;
return val<ValueType>(left.ptr, index, left.alignment);
}


template<typename ValueType>
auto inline operator-(val_ptr<ValueType> left, val<ValueType> right) {
TRAC_BINARY_PTR_OP(SUB)
return left.ptr - details::getRawValue(right);
requires std::is_pointer_v<ValueType>
auto inline operator-(val<ValueType> left, std::remove_pointer_t<ValueType> right) {
return val<ValueType>(left.ptr, left.index - right, left.alignment);
}

template<typename ValueType>
auto inline operator==(val_ptr<ValueType> left, val_ptr<ValueType> right) {
requires std::is_pointer_v<ValueType>
auto inline operator==(val<ValueType> left, val<ValueType> right) {
return val<bool>(left.ptr == right.ptr);
}

template<typename ValueType>
auto inline operator<=(val_ptr<ValueType> left, val_ptr<ValueType> right) {
return val<bool>(left.ptr == right.ptr);
requires std::is_pointer_v<ValueType>
auto inline operator<=(val<ValueType> left, val<ValueType> right) {
return val<bool>(left.ptr <= right.ptr);
}

template<typename ValueType>
auto inline operator<(val_ptr<ValueType> left, val_ptr<ValueType> right) {
return val<bool>(left.ptr == right.ptr);
requires std::is_pointer_v<ValueType>
auto inline operator<(val<ValueType> left, val<ValueType> right) {
return val<bool>(left.ptr < right.ptr);
}

template<typename ValueType>
auto inline operator>(val_ptr<ValueType> left, val_ptr<ValueType> right) {
return val<bool>(left.ptr == right.ptr);
requires std::is_pointer_v<ValueType>
auto inline operator>(val<ValueType> left, val<ValueType> right) {
return val<bool>(left.ptr > right.ptr);
}

template<typename ValueType>
auto inline operator>=(val_ptr<ValueType> left, val_ptr<ValueType> right) {
return val<bool>(left.ptr == right.ptr);
requires std::is_pointer_v<ValueType>
auto inline operator>=(val<ValueType> left, val<ValueType> right) {
return val<bool>(left.ptr >= right.ptr);
}

template<typename ValueType>
auto inline operator!=(val_ptr<ValueType> left, val_ptr<ValueType> right) {
requires std::is_pointer_v<ValueType>
auto inline operator!=(val<ValueType> left, val<ValueType> right) {
return val<bool>(left.ptr != right.ptr);
}


template<int8_t alignment, typename ValuePtrType>
val_ptr<ValuePtrType> assume_aligned(val_ptr<ValuePtrType> ptr) {
return val_ptr<ValuePtrType>(ptr.ptr, alignment);
requires std::is_pointer_v<ValuePtrType>
val<ValuePtrType> assume_aligned(val<ValuePtrType> ptr) {
return val<ValuePtrType>(ptr.ptr, alignment);
}

}
2 changes: 1 addition & 1 deletion nautilus-api/include/Interface/function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ namespace nautilus {
// keep current tracing context and continue tracing
// TODO implement polymorphic inline cache
// TODO pass member as val
auto result = std::invoke(__pm, mem, transform((args))...);
auto result = std::invoke(__pm, mem, transform(make_value(args))...);
return make_value(result);
}

Expand Down
Loading

0 comments on commit 16b53ad

Please sign in to comment.