Skip to content

Commit

Permalink
simplify trace operations (#56)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGrulich authored Oct 6, 2024
1 parent c5f2bbf commit 7adef5c
Show file tree
Hide file tree
Showing 683 changed files with 1,444 additions and 1,325 deletions.
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ AccessModifierOffset: -4
AllowShortFunctionsOnASingleLine: false
AllowShortLambdasOnASingleLine: Inline
AlwaysBreakTemplateDeclarations: Yes
ColumnLimit: 240
ColumnLimit: 120
CompactNamespaces: true
IncludeBlocks: Merge
IncludeCategories:
Expand Down
2 changes: 1 addition & 1 deletion nautilus/include/nautilus/Engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ std::function<void()> createFunctionWrapper(std::index_sequence<Indices...>, std
auto traceFunc = [=]() {
if constexpr (std::is_void_v<R>) {
func(details::createTraceableArgument<FunctionArguments, Indices>()...);
tracing::traceReturnOperation(Type::v, tracing::value_ref());
tracing::traceReturnOperation(Type::v, tracing::TypedValueRef());
} else {
auto returnValue = func(details::createTraceableArgument<FunctionArguments, Indices>()...);
auto type = tracing::to_type<typename decltype(returnValue)::raw_type>();
Expand Down
4 changes: 2 additions & 2 deletions nautilus/include/nautilus/function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ namespace nautilus {

template <typename... ValueArguments>
auto getArgumentReferences(const ValueArguments&... arguments) {
std::vector<tracing::value_ref> functionArgumentReferences;
std::vector<tracing::TypedValueRef> functionArgumentReferences;
if constexpr (sizeof...(ValueArguments) > 0) {
functionArgumentReferences.reserve(sizeof...(ValueArguments));
for (const tracing::value_ref& p : {details::getState(arguments)...}) {
for (const tracing::TypedValueRef& p : {details::getState(arguments)...}) {
functionArgumentReferences.emplace_back(p);
}
}
Expand Down
28 changes: 20 additions & 8 deletions nautilus/include/nautilus/std/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,8 @@ class val<std::basic_string<CharT, Traits>> {
}

/**
* Informs a std::basic_string object of a planned change in size, so that it can manage the storage allocation appropriately.
* Informs a std::basic_string object of a planned change in size, so that it can manage the storage allocation
* appropriately.
* @param new_cap
*/
void reserve(val<size_type> new_cap) const {
Expand Down Expand Up @@ -147,7 +148,8 @@ class val<std::basic_string<CharT, Traits>> {
*/
auto& insert(val<size_type> index, val<size_type> count, val<CharT> ch) const {
invoke(
+[](base_type* ptr, size_type index, size_type count, CharT ch) -> void { ptr->insert(index, count, ch); }, data_ptr, index, count, ch);
+[](base_type* ptr, size_type index, size_type count, CharT ch) -> void { ptr->insert(index, count, ch); },
data_ptr, index, count, ch);
return *this;
}

Expand All @@ -156,7 +158,8 @@ class val<std::basic_string<CharT, Traits>> {
*/
auto& insert(val<size_type> index, val<const CharT*> s) const {
invoke(
+[](base_type* ptr, size_type index, const CharT* s) -> void { ptr->insert(index, s); }, data_ptr, index, s);
+[](base_type* ptr, size_type index, const CharT* s) -> void { ptr->insert(index, s); }, data_ptr, index,
s);
return *this;
}

Expand All @@ -181,9 +184,13 @@ class val<std::basic_string<CharT, Traits>> {
/**
* Appends additional characters to the string.
*/
auto& append(const val<std::basic_string<CharT, Traits>>& str, const val<size_type>& pos, const val<size_type>& count) {
auto& append(const val<std::basic_string<CharT, Traits>>& str, const val<size_type>& pos,
const val<size_type>& count) {
invoke(
+[](base_type* ptr, base_type* other, size_type pos, size_type count) -> void { ptr->append(*other, pos, count); }, data_ptr, str.data_ptr, pos, count);
+[](base_type* ptr, base_type* other, size_type pos, size_type count) -> void {
ptr->append(*other, pos, count);
},
data_ptr, str.data_ptr, pos, count);
return *this;
}

Expand All @@ -192,7 +199,8 @@ class val<std::basic_string<CharT, Traits>> {
*/
auto& append(const val<std::basic_string<CharT, Traits>>& str, const val<size_type>& count) {
invoke(
+[](base_type* ptr, base_type* other, size_type count) -> void { ptr->append(*other, count); }, data_ptr, str.data_ptr, count);
+[](base_type* ptr, base_type* other, size_type count) -> void { ptr->append(*other, count); }, data_ptr,
str.data_ptr, count);
return *this;
}

Expand Down Expand Up @@ -243,11 +251,15 @@ class val<std::basic_string<CharT, Traits>> {
}

/**
* Copies a substring [pos, pos + count) to character string pointed to by dest. If the requested substring lasts past the end of the string, or if count == npos, the copied substring is [pos, size()).
* Copies a substring [pos, pos + count) to character string pointed to by dest. If the requested substring lasts
* past the end of the string, or if count == npos, the copied substring is [pos, size()).
*/
val<size_type> copy(const val<CharT*>& dest, const val<size_type>& count, const val<size_type>& pos = 0) {
return invoke(
+[](base_type* ptr, CharT* dest, size_type count, size_type pos) -> size_type { return ptr->copy(dest, count, pos); }, data_ptr, dest, count, pos);
+[](base_type* ptr, CharT* dest, size_type count, size_type pos) -> size_type {
return ptr->copy(dest, count, pos);
},
data_ptr, dest, count, pos);
}

~val() {
Expand Down
88 changes: 64 additions & 24 deletions nautilus/include/nautilus/std/string_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,16 @@ class val<std::basic_string_view<CharT, Traits>> {

val<value_type> operator[](val<size_type> pos) const {
return invoke(
+[](std::basic_string_view<CharT, Traits>* ptr, size_type pos) -> value_type { return ptr->operator[](pos); }, data_ptr, pos);
+[](std::basic_string_view<CharT, Traits>* ptr, size_type pos) -> value_type {
return ptr->operator[](pos);
},
data_ptr, pos);
}

const val<value_type> at(val<size_type> index) const {
return invoke(
+[](std::basic_string_view<CharT, Traits>* ptr, size_type index) -> value_type { return ptr->at(index); }, data_ptr, index);
+[](std::basic_string_view<CharT, Traits>* ptr, size_type index) -> value_type { return ptr->at(index); },
data_ptr, index);
}

val<size_type> size() const noexcept {
Expand Down Expand Up @@ -107,22 +111,30 @@ class val<std::basic_string_view<CharT, Traits>> {

void remove_prefix(val<size_type> n) {
invoke(
+[](std::basic_string_view<CharT, Traits>* ptr, size_type n) -> void { ptr->remove_prefix(n); }, data_ptr, n);
+[](std::basic_string_view<CharT, Traits>* ptr, size_type n) -> void { ptr->remove_prefix(n); }, data_ptr,
n);
}

void remove_suffix(val<size_type> n) {
invoke(
+[](std::basic_string_view<CharT, Traits>* ptr, size_type n) -> void { ptr->remove_suffix(n); }, data_ptr, n);
+[](std::basic_string_view<CharT, Traits>* ptr, size_type n) -> void { ptr->remove_suffix(n); }, data_ptr,
n);
}

void swap(val<std::basic_string_view<CharT, Traits>>& v) {
invoke(
+[](std::basic_string_view<CharT, Traits>* ptr, std::basic_string_view<CharT, Traits>* other) -> void { ptr->swap(*other); }, data_ptr, v.data_ptr);
+[](std::basic_string_view<CharT, Traits>* ptr, std::basic_string_view<CharT, Traits>* other) -> void {
ptr->swap(*other);
},
data_ptr, v.data_ptr);
}

val<size_type> copy(val<CharT*> dest, val<size_type> count, val<size_type> pos = 0) {
return invoke(
+[](std::basic_string_view<CharT, Traits>* ptr, CharT* dest, size_type count, size_type pos) -> size_type { return ptr->copy(dest, count, pos); }, data_ptr, dest, count, pos);
+[](std::basic_string_view<CharT, Traits>* ptr, CharT* dest, size_type count, size_type pos) -> size_type {
return ptr->copy(dest, count, pos);
},
data_ptr, dest, count, pos);
}

/*
Expand All @@ -142,76 +154,104 @@ class val<std::basic_string_view<CharT, Traits>> {

val<int> compare(val<std::basic_string_view<CharT, Traits>>& v) {
return invoke(
+[](std::basic_string_view<CharT, Traits>* ptr, std::basic_string_view<CharT, Traits>* other) -> int { return ptr->compare(*other); }, data_ptr, v.data_ptr);
+[](std::basic_string_view<CharT, Traits>* ptr, std::basic_string_view<CharT, Traits>* other) -> int {
return ptr->compare(*other);
},
data_ptr, v.data_ptr);
}

val<int> compare(val<size_type> pos1, val<size_type> count1, val<std::basic_string_view<CharT, Traits>>& v) {
return invoke(
+[](std::basic_string_view<CharT, Traits>* ptr, size_type pos1, size_t count1, std::basic_string_view<CharT, Traits>* v) -> int { return ptr->compare(pos1, count1, *v); }, data_ptr, pos1, count1, v.data_ptr);
+[](std::basic_string_view<CharT, Traits>* ptr, size_type pos1, size_t count1,
std::basic_string_view<CharT, Traits>* v) -> int { return ptr->compare(pos1, count1, *v); },
data_ptr, pos1, count1, v.data_ptr);
}

val<int> compare(val<size_type> pos1, val<size_type> count1, val<std::basic_string_view<CharT, Traits>>& v, val<size_type> pos2, val<size_type> count2) {
val<int> compare(val<size_type> pos1, val<size_type> count1, val<std::basic_string_view<CharT, Traits>>& v,
val<size_type> pos2, val<size_type> count2) {
return invoke(
+[](std::basic_string_view<CharT, Traits>* ptr, size_type pos1, size_t count1, std::basic_string_view<CharT, Traits>* v, size_type pos2, size_t count2) -> int { return ptr->compare(pos1, count1, *v, pos2, count2); }, data_ptr,
pos1, count1, v.data_ptr, pos2, count2);
+[](std::basic_string_view<CharT, Traits>* ptr, size_type pos1, size_t count1,
std::basic_string_view<CharT, Traits>* v, size_type pos2,
size_t count2) -> int { return ptr->compare(pos1, count1, *v, pos2, count2); },
data_ptr, pos1, count1, v.data_ptr, pos2, count2);
}

val<int> compare(val<const CharT*> s) {
return invoke(
+[](std::basic_string_view<CharT, Traits>* ptr, const CharT* s) -> int { return ptr->compare(s); }, data_ptr, s);
+[](std::basic_string_view<CharT, Traits>* ptr, const CharT* s) -> int { return ptr->compare(s); },
data_ptr, s);
}

val<int> compare(val<size_type> pos1, val<size_type> count1, val<const CharT*> s) {
return invoke(
+[](std::basic_string_view<CharT, Traits>* ptr, size_type pos1, size_t count1, const CharT* s) -> int { return ptr->compare(pos1, count1, s); }, data_ptr, pos1, count1, s);
+[](std::basic_string_view<CharT, Traits>* ptr, size_type pos1, size_t count1, const CharT* s) -> int {
return ptr->compare(pos1, count1, s);
},
data_ptr, pos1, count1, s);
}

val<int> compare(val<size_type> pos1, val<size_type> count1, val<const CharT*> s, val<size_type> pos2, val<size_type> count2) {
val<int> compare(val<size_type> pos1, val<size_type> count1, val<const CharT*> s, val<size_type> pos2,
val<size_type> count2) {
return invoke(
+[](std::basic_string_view<CharT, Traits>* ptr, size_type pos1, size_t count1, const CharT* s, size_type pos2, size_t count2) -> int { return ptr->compare(pos1, count1, s, pos2, count2); }, data_ptr, pos1, count1, s, pos2,
count2);
+[](std::basic_string_view<CharT, Traits>* ptr, size_type pos1, size_t count1, const CharT* s,
size_type pos2, size_t count2) -> int { return ptr->compare(pos1, count1, s, pos2, count2); },
data_ptr, pos1, count1, s, pos2, count2);
}
#ifdef __cpp_lib_starts_ends_with
val<bool> start_with(val<std::basic_string_view<CharT, Traits>>& v) {
return invoke(
+[](std::basic_string_view<CharT, Traits>* ptr, std::basic_string_view<CharT, Traits>* v) -> bool { return ptr->starts_with(*v); }, data_ptr, v.data_ptr);
+[](std::basic_string_view<CharT, Traits>* ptr, std::basic_string_view<CharT, Traits>* v) -> bool {
return ptr->starts_with(*v);
},
data_ptr, v.data_ptr);
}

val<bool> start_with(val<const CharT*> s) {
return invoke(
+[](std::basic_string_view<CharT, Traits>* ptr, const CharT* s) -> bool { return ptr->starts_with(s); }, data_ptr, s);
+[](std::basic_string_view<CharT, Traits>* ptr, const CharT* s) -> bool { return ptr->starts_with(s); },
data_ptr, s);
}

val<bool> start_with(val<CharT> s) {
return invoke(
+[](std::basic_string_view<CharT, Traits>* ptr, CharT s) -> bool { return ptr->starts_with(s); }, data_ptr, s);
+[](std::basic_string_view<CharT, Traits>* ptr, CharT s) -> bool { return ptr->starts_with(s); }, data_ptr,
s);
}

val<bool> end_with(val<std::basic_string_view<CharT, Traits>>& v) {
return invoke(
+[](std::basic_string_view<CharT, Traits>* ptr, std::basic_string_view<CharT, Traits>* v) -> bool { return ptr->ends_with(*v); }, data_ptr, v.data_ptr);
+[](std::basic_string_view<CharT, Traits>* ptr, std::basic_string_view<CharT, Traits>* v) -> bool {
return ptr->ends_with(*v);
},
data_ptr, v.data_ptr);
}

val<bool> end_with(val<const CharT*> s) {
return invoke(
+[](std::basic_string_view<CharT, Traits>* ptr, const CharT* s) -> bool { return ptr->ends_with(s); }, data_ptr, s);
+[](std::basic_string_view<CharT, Traits>* ptr, const CharT* s) -> bool { return ptr->ends_with(s); },
data_ptr, s);
}

val<bool> end_with(val<CharT> s) {
return invoke(
+[](std::basic_string_view<CharT, Traits>* ptr, CharT s) -> bool { return ptr->ends_with(s); }, data_ptr, s);
+[](std::basic_string_view<CharT, Traits>* ptr, CharT s) -> bool { return ptr->ends_with(s); }, data_ptr,
s);
}
#endif

#ifdef __cpp_lib_string_contains
val<bool> contains(val<std::basic_string_view<CharT, Traits>>& v) {
return invoke(
+[](std::basic_string_view<CharT, Traits>* ptr, std::basic_string_view<CharT, Traits>* v) -> bool { return ptr->contains(*v); }, data_ptr, v.data_ptr);
+[](std::basic_string_view<CharT, Traits>* ptr, std::basic_string_view<CharT, Traits>* v) -> bool {
return ptr->contains(*v);
},
data_ptr, v.data_ptr);
}

val<bool> contains(val<const CharT*> s) {
return invoke(
+[](std::basic_string_view<CharT, Traits>* ptr, const CharT* s) -> bool { return ptr->contains(s); }, data_ptr, s);
+[](std::basic_string_view<CharT, Traits>* ptr, const CharT* s) -> bool { return ptr->contains(s); },
data_ptr, s);
}

val<bool> contains(val<CharT> s) {
Expand Down
22 changes: 9 additions & 13 deletions nautilus/include/nautilus/tracing/TracingUtil.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,28 @@ namespace nautilus::tracing {

bool inTracer();

value_ref traceBinaryOp(Op operation, Type resultType, const TypedValueRef& leftState, const TypedValueRef& rightState);
value_ref traceUnaryOp(Op operation, Type resultType, const TypedValueRef& inputState);
TypedValueRef& traceBinaryOp(Op operation, Type resultType, const TypedValueRef& leftState, const TypedValueRef& rightState);
TypedValueRef& traceUnaryOp(Op operation, Type resultType, const TypedValueRef& inputState);

bool traceBool(const TypedValueRef& state);
value_ref traceConstant(Type type, const ConstantLiteral& value);
TypedValueRef& traceConstant(Type type, const ConstantLiteral& value);
template <typename T>
value_ref traceConstant(T&& value) {
TypedValueRef traceConstant(T&& value) {
if (inTracer()) {
return traceConstant(to_type<T>(), createConstLiteral(value));
}
return {0, to_type<T>()};
}

value_ref traceLoad(const TypedValueRef& src, Type resultType);
void traceStore(const TypedValueRef& target, const TypedValueRef& src, Type valueType);

value_ref traceCast(const value_ref& state, Type resultType);
void traceAssignment(const TypedValueRef& target, const TypedValueRef& source, Type resultType);
value_ref traceCopy(const TypedValueRef& state);
TypedValueRef traceCopy(const TypedValueRef& state);

value_ref traceCall(void* fptn, Type resultType, const std::vector<tracing::value_ref>& arguments);
TypedValueRef& traceCall(void* fptn, Type resultType, const std::vector<tracing::TypedValueRef>& arguments);

value_ref registerFunctionArgument(Type type, size_t index);
TypedValueRef& registerFunctionArgument(Type type, size_t index);

void traceReturnOperation(Type type, const value_ref& ref);
void traceValueDestruction(value_ref ref);
void traceReturnOperation(Type type, const TypedValueRef& ref);
void traceValueDestruction(TypedValueRef ref);

void pushStaticVal(void* ptr);
void popStaticVal();
Expand Down
1 change: 0 additions & 1 deletion nautilus/include/nautilus/tracing/TypedValueRef.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,4 @@ class TypedValueRefHolder {
TypedValueRef valueRef;
};

using value_ref = TypedValueRef;
} // namespace nautilus::tracing
Loading

0 comments on commit 7adef5c

Please sign in to comment.