Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGrulich committed Mar 23, 2024
1 parent 16b53ad commit 0d1bcd4
Show file tree
Hide file tree
Showing 29 changed files with 1,205 additions and 304 deletions.
106 changes: 96 additions & 10 deletions nautilus-api/include/Interface/DataTypes/Val.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,31 @@ namespace nautilus {


inline val<ValueType>(ValueType value, const std::source_location loc = std::source_location::current())
: value(value), loc(loc) {

std::cout << loc.file_name() << std::endl;
: value(value), loc(loc), state(tracing::traceConstant(value)) {
//std::cout << loc.file_name() << std::endl;
// ((auto tag = tracing::getTag();
tracing::getVarRefMap()[state]++;
// std::cout << "value constructor " << state << " - " << tag << std::endl;
};


inline val<ValueType>(const val<ValueType> &other,
const std::source_location loc = std::source_location::current())
: value(other.value), loc(loc) {};
: value(other.value), loc(loc), state(other.state) {

tracing::getVarRefMap()[state]++;
//std::cout << "copy constructor " << this->state << " - " << other.state << " - " << tag << std::endl;
//std::cout << "copy constructor " << this->state << " - " << other.state << " - " << std::endl;
// tracing::traceAssignment(other.state, other.state);
};

val<ValueType>(val<ValueType> &&other) noexcept // move constructor
: value(other.value), loc(other.loc), state(other.state) {
// auto tag = tracing::getTag();
tracing::getVarRefMap()[state]++;
//std::cout << "move constructor " << other.state << " - " << tag << std::endl;
}

#else

inline val<ValueType>(ValueType value) : value(value) {};
Expand All @@ -157,10 +173,31 @@ namespace nautilus {

#ifdef ENABLE_TRACING

inline val<ValueType>(tracing::value_ref *tc) : state(tc) {};
inline val<ValueType>(tracing::value_ref &tc) : state(tc) {
// auto tag = tracing::getTag();
tracing::getVarRefMap()[state]++;
// std::cout << "trace constructor " << state << " - " << tag << std::endl;
};
#endif


~val<ValueType>() {
// auto tag = tracing::getTag();
tracing::getVarRefMap()[state]--;
if (tracing::getVarRefMap()[state] == 0) {
tracing::traceValueDestruction(state);
//std::cout << "destructor " << state << " - " << tag << std::endl;
}
}

val<ValueType> &operator=(const val<ValueType> &other) {
// auto tag = tracing::getTag();
//std::cout << "copy assignment " << this->state << "<- " << other.state << " - " << tag << std::endl;
#ifdef ENABLE_TRACING
if (tracing::inTracer()) {
tracing::traceAssignment(state, other.state);
}
#endif
this->value = other.value;
return *this;
};
Expand All @@ -176,10 +213,10 @@ namespace nautilus {
static_assert(std::is_same_v<ValueType, bool>);
#ifdef ENABLE_TRACING
if (tracing::inTracer()) {
return tracing::traceBool(state);
auto ref = state;
return tracing::traceBool(ref);
}
#endif
tracing::traceBool(state);
// cast
return value;
}
Expand Down Expand Up @@ -220,7 +257,7 @@ namespace nautilus {
#endif

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


Expand Down Expand Up @@ -303,7 +340,7 @@ namespace nautilus {
auto cast_value(val<LeftType> &value) {
typedef typename std::common_type<LeftType, RightType>::type commonType;
if constexpr (std::is_same_v<LeftType, RightType>) {
return value;
return (val<LeftType> &) value;
} else {
return static_cast<val<commonType>>(value);
}
Expand Down Expand Up @@ -622,7 +659,7 @@ namespace nautilus {
}

template<typename LHS, typename RHS>
val<bool> inline operator>(LHS left, RHS right) {
val<bool> inline operator>(LHS &left, RHS right) {
if constexpr (is_fundamental<LHS>) {
auto leftV = make_value(left);
return details::gt<>(right, leftV);
Expand Down Expand Up @@ -839,4 +876,53 @@ namespace nautilus {
}
}


template<typename T> requires std::is_integral_v<T>
class sval {
public:

sval<T>(T value) : value(value) {};

const auto &operator++() {
nautilus::tracing::getVarRefMap()[54]++;
// increment
++value;
// assign
return *this;
}

auto operator++(int) {
nautilus::tracing::getVarRefMap()[54]++;
// returned value should be a copy of the object before increment
auto temp = *this;
++value;
return temp;
}

auto &operator--() {
nautilus::tracing::getVarRefMap()[54]++;
// decrement
--value;
// assign
return *this;
}

auto operator--(int) {
nautilus::tracing::getVarRefMap()[54]++;
// returned value should be a copy of the object before decrement
auto temp = *this;
--value;
return temp;
}

bool operator<(const T &other) const {
//nautilus::tracing::getVarRefMap()[54]++;
return value < other;
}

private:
T value;
};


#endif //NAUTILUS_LIB_VAL_HPP
51 changes: 35 additions & 16 deletions nautilus-api/include/engine/engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,56 @@

namespace nautilus::engine {

namespace details {

template<is_base_type R, is_base_type... FunctionArguments>
class CallableFunction {
public:

CallableFunction(void *func) : func(func) {}
template<typename Arg>
auto createTraceArgument() {
auto type = tracing::to_type<typename Arg::basic_type>();
auto valueRef = tracing::registerFunctionArgument(type);
return Arg(valueRef);
}

template<typename Arg>
auto transform(Arg argument) {
return make_value(argument);
}

template<is_base_type R, is_base_type... FunctionArguments>
std::function<void()> createFunctionWrapper(R (*fnptr)(FunctionArguments...)) {
[[maybe_unused]] std::size_t args = sizeof...(FunctionArguments);
auto traceFunc = [=]() {
if constexpr (std::is_void_v<R>) {
fnptr(details::createTraceArgument<FunctionArguments>()...);
} else {
auto returnValue = fnptr(details::createTraceArgument<FunctionArguments>()...);
auto type = tracing::to_type<typename decltype(returnValue)::basic_type>();
tracing::traceReturnOperation(type, returnValue.state);
}
};
return traceFunc;
}
}

template<is_base_type R, is_base_type... FunctionArguments>
class CallableFunction {
public:

CallableFunction(void *func) : func(func) {}

template<is_fundamental... FunctionArgumentsRaw>
auto operator()(FunctionArgumentsRaw... args) {
// function is called from an external context.
using FunctionType = R(FunctionArguments...);
auto fnptr = reinterpret_cast<FunctionType *>(func);
auto result = fnptr(transform((args))...);
return details::getRawValue(result);
auto result = fnptr(details::transform((args))...);
return nautilus::details::getRawValue(result);
}

private:
void *func;
};


class NautilusEngine {
public:

Expand All @@ -40,9 +65,10 @@ namespace nautilus::engine {
//auto wrapper = createFunctionWrapper(fnptr);
//auto executable = jit->compile(wrapper);
//auto ptr = reinterpret_cast<R (*)(FunctionArguments...)>(executable);
return CallableFunction<R, FunctionArguments...>((void*)fnptr);
return CallableFunction<R, FunctionArguments...>((void *) fnptr);
}


private:
std::unique_ptr<JITCompiler> jit;

Expand All @@ -52,14 +78,7 @@ namespace nautilus::engine {
return Arg(tc);
}

template<is_base_type R, is_base_type... FunctionArguments>
auto createFunctionWrapper(R (*fnptr)(FunctionArguments...)) {
[[maybe_unused]] std::size_t args = sizeof...(FunctionArguments);
auto traceFunc = [&]() {
return fnptr(transform<FunctionArguments>()...);
};
return traceFunc;
}

};

}
1 change: 1 addition & 0 deletions nautilus-api/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ add_executable(nautilus-api-tests
# Interface/FloatTypeTest.cpp
)


target_link_libraries(nautilus-api-tests PUBLIC nautilus-api nautilus-jit nautilus-common nautilus_fmt Catch2::Catch2WithMain)
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
include(CTest)
Expand Down
14 changes: 12 additions & 2 deletions nautilus-api/test/ExecutionTests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
add_executable(nautilus-execution-tests
Executions.cpp
ExecutionTest.cpp
ControlFlowFunctions.hpp
)

add_executable(nautilus-tracing-tests
TracingTest.cpp
)

target_link_libraries(nautilus-execution-tests PUBLIC nautilus-api nautilus-jit nautilus-common nautilus_fmt Catch2::Catch2WithMain)
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
include(CTest)
include(Catch)
catch_discover_tests(nautilus-execution-tests EXTRA_ARGS --allow-running-no-tests)
catch_discover_tests(nautilus-execution-tests EXTRA_ARGS --allow-running-no-tests)

target_link_libraries(nautilus-tracing-tests PUBLIC nautilus-api nautilus-jit nautilus-common nautilus_fmt Catch2::Catch2WithMain)
list(APPEND CMAKE_MODULE_PATH ${catch2_SOURCE_DIR}/extras)
include(CTest)
include(Catch)
catch_discover_tests(nautilus-tracing-tests EXTRA_ARGS --allow-running-no-tests)
2 changes: 2 additions & 0 deletions nautilus-api/test/ExecutionTests/ControlFlowFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#include <engine/engine.hpp>

namespace nautilus::engine {


val<int32_t> ifThenCondition(val<int32_t> value) {
val<int32_t> iw = 1;
if (value == 42) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ namespace nautilus::engine {

}

TEMPLATE_TEST_CASE("Engine Test", "[value][template]", int8_t) {
TEST_CASE("Engine Test") {
auto engine = engine::NautilusEngine();
SECTION("expressionTest") {
addTest(engine);
Expand Down
10 changes: 3 additions & 7 deletions nautilus-api/test/ExecutionTests/ExpressionFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
namespace nautilus::engine {
val<int8_t> int8AddExpression(val<int8_t> x) {
val<int8_t> y = (int8_t) 2;
return x + y;
auto res = x + y;
y = res;
return y + x;
}


val<int16_t> int16AddExpression(val<int16_t> x) {
val<int16_t> y = (int16_t) 5;
return x + y;
Expand Down Expand Up @@ -39,18 +40,13 @@ namespace nautilus::engine {
return x + y;
}



val<int64_t> castInt8ToInt64AddExpression(val<int8_t> x) {
val<int64_t> y = (int64_t) 7;
return x + y;
}


val<int64_t> castInt8ToInt64AddExpression2(val<int8_t> x) {
val<int64_t> y = (int64_t) 42;
return y + x;
}


}
Loading

0 comments on commit 0d1bcd4

Please sign in to comment.