Skip to content

Commit

Permalink
Added support for nautilus::val<T> and T is a enum class
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGrulich committed Sep 20, 2024
1 parent 2a29e9d commit 4196c18
Show file tree
Hide file tree
Showing 23 changed files with 130 additions and 30 deletions.
39 changes: 29 additions & 10 deletions nautilus/include/nautilus/val_enum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "nautilus/val_concepts.hpp"
#include <iostream>
#include <memory>
#include <utility>

#ifdef ENABLE_TRACING

Expand Down Expand Up @@ -36,7 +37,7 @@ class val<T> {
}
val(val<T>&& t) : state(t.state), value(t.value) {
}
val(T val) : state(tracing::traceConstant((underlying_type_t) val)), value(val) {
val(T val) : state(tracing::traceConstant(std::__to_underlying(val))), value(val) {
}
#else
template <T>
Expand All @@ -57,20 +58,38 @@ class val<T> {
}
#endif

template <typename RHS>
requires std::is_enum_v<RHS> && (!std::is_convertible_v<RHS, std::underlying_type_t<RHS>>)
bool operator==(const RHS& other) const {
return val(value) == val(static_cast<std::underlying_type_t<RHS>>(other));

val<bool> operator==(val<T>& other) const {
#ifdef ENABLE_TRACING
if (tracing::inTracer()) {
auto tc = tracing::traceBinaryOp(tracing::EQ, Type::b, state, other.state);
return val<bool>(tc);
}
#endif
return value == other.value;
}

bool operator==(const T& other) const {
auto res = val<T>(other);
return *this == res;
}

val<bool> operator!=(val<T>& other) const {
#ifdef ENABLE_TRACING
if (tracing::inTracer()) {
auto tc = tracing::traceBinaryOp(tracing::NEQ, Type::b, state, other.state);
return val<bool>(tc);
}
#endif
return value != other.value;
}

template <typename RHS>
requires std::is_enum_v<RHS> && (!std::is_convertible_v<RHS, std::underlying_type_t<RHS>>)
bool operator!=(const RHS& other) const {
return val(value) != val(static_cast<std::underlying_type_t<RHS>>(other));
val<bool> operator!=(const T& other) const {
return *this == val<T>(other);
}

operator val<underlying_type_t>() const {
return value;
return val<underlying_type_t>(state);
}

val<T>& operator=(const val<T>& other) {
Expand Down
2 changes: 1 addition & 1 deletion nautilus/src/nautilus/tracing/TraceOperation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ std::ostream& operator<<(std::ostream& os, const TraceOperation& operation) {
[&](auto&& value) -> void {
using T = std::decay_t<decltype(value)>;
if constexpr (!std::is_pointer_v<T>) {
os << value << "\t";
os << fmt::format("{}\t", value);
} else {
os << "*\t";
}
Expand Down
2 changes: 1 addition & 1 deletion nautilus/test/data/bool-tests/after_ssa/boolConst.trace
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
B0($1:bool)
CONST $3 1 :bool
CONST $3 true :bool
AND $4 $1 $3 :bool
RETURN $4 :bool
4 changes: 2 additions & 2 deletions nautilus/test/data/bool-tests/after_ssa/boolIfElse.trace
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ B0($1:bool,$2:bool)
AND $5 $1 $2 :bool
CMP $6 $5 B1() B2() :void
B1()
CONST $7 1 :bool
CONST $7 true :bool
JMP $0 B3($7) :void
B2()
CONST $10 0 :bool
CONST $10 false :bool
JMP $0 B3($10) :void
B3($7:bool)
RETURN $7 :bool
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ B0($1:bool,$2:bool)
EQ $5 $1 $2 :bool
CMP $6 $5 B1() B2() :void
B1()
CONST $7 1 :bool
CONST $7 true :bool
JMP $0 B3($7) :void
B2()
CONST $10 0 :bool
CONST $10 false :bool
JMP $0 B3($10) :void
B3($7:bool)
RETURN $7 :bool
2 changes: 1 addition & 1 deletion nautilus/test/data/bool-tests/tracing/boolConst.trace
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
B0($1:bool)
ASSIGN $2 $1 :bool
CONST $3 1 :bool
CONST $3 true :bool
AND $4 $2 $3 :bool
RETURN $4 :bool
4 changes: 2 additions & 2 deletions nautilus/test/data/bool-tests/tracing/boolIfElse.trace
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ B0($1:bool,$2:bool)
AND $5 $3 $4 :bool
CMP $6 $5 B1() B2() :void
B1()
CONST $7 1 :bool
CONST $7 true :bool
RETURN $7 :bool
B2()
CONST $10 0 :bool
CONST $10 false :bool
RETURN $10 :bool
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ B0($1:bool,$2:bool)
EQ $5 $3 $4 :bool
CMP $6 $5 B1() B2() :void
B1()
CONST $7 1 :bool
CONST $7 true :bool
RETURN $7 :bool
B2()
CONST $10 0 :bool
CONST $10 false :bool
RETURN $10 :bool
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
B0($1:i32)
CONST $2 1 :bool
CONST $2 true :bool
CONST $4 42 :i64
CAST $5 $1 :i64
EQ $6 $5 $4 :bool
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
B0($1:i32)
CONST $2 1 :bool
CONST $2 true :bool
ASSIGN $3 $2 :bool
CONST $4 42 :i64
CAST $5 $1 :i64
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
B0($1:ui8)
CALL $2 nautilus::enumClassFunction(nautilus::LogLevel)($1) :i32
RETURN $2 :i32
19 changes: 19 additions & 0 deletions nautilus/test/data/enum-tests/after_ssa/handleEnumLogLevel.trace
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
B0($1:ui8)
CONST $2 6 :ui8
EQ $3 $1 $2 :bool
CMP $4 $3 B1() B2($1) :void
B1()
CONST $5 true :bool
JMP $0 B5($5) :void
B2($1:ui8)
CONST $7 5 :ui8
EQ $8 $1 $7 :bool
CMP $9 $8 B3() B4() :void
B3()
CONST $10 true :bool
JMP $0 B5($10) :void
B4()
CONST $12 false :bool
JMP $0 B5($12) :void
B5($5:bool)
RETURN $5 :bool
7 changes: 7 additions & 0 deletions nautilus/test/data/enum-tests/ir/callEnumClassFunction.trace
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
NESIR {
execute() {
Block_0($1:ui8):
$2 = nautilus::enumClassFunction(nautilus::LogLevel)($1) :i32
return ($2) :i32
}
} //NESIR
28 changes: 28 additions & 0 deletions nautilus/test/data/enum-tests/ir/handleEnumLogLevel.trace
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
NESIR {
execute() {
Block_0($1:ui8):
$2 = 6 :ui8
$3 = $1 == $2 :bool
if $3 ? Block_1() : Block_2($1) :void

Block_1():
$5 = 1 :bool
br Block_5($5) :void

Block_5($5:bool):
return ($5) :bool

Block_2($1:ui8):
$7 = 5 :ui8
$8 = $1 == $7 :bool
if $8 ? Block_3() : Block_4() :void

Block_3():
$10 = 1 :bool
br Block_5($10) :void

Block_4():
$12 = 0 :bool
br Block_5($12) :void
}
} //NESIR
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
B0($1:ui8)
CALL $2 nautilus::enumClassFunction(nautilus::LogLevel)($1) :i32
RETURN $2 :i32
19 changes: 19 additions & 0 deletions nautilus/test/data/enum-tests/tracing/handleEnumLogLevel.trace
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
B0($1:ui8)
CONST $2 6 :ui8
EQ $3 $1 $2 :bool
CMP $4 $3 B1() B2() :void
B1()
CONST $5 true :bool
RETURN $5 :bool
B2()
CONST $7 5 :ui8
EQ $8 $1 $7 :bool
CMP $9 $8 B3() B4() :void
B3()
CONST $10 true :bool
ASSIGN $5 $10 :bool
RETURN $5 :bool
B4()
CONST $12 false :bool
ASSIGN $5 $12 :bool
RETURN $5 :bool
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
B0($1:i8)
CONST $2  :i8
CONST $2 2 :i8
ADD $3 $2 $1 :i32
RETURN $3 :i8
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
B0($1:i8)
CONST $2  :i8
CONST $2 2 :i8
ADD $3 $2 $1 :i32
RETURN $3 :i8
4 changes: 2 additions & 2 deletions nautilus/test/data/loop-tests/after_ssa/isPrime.trace
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
B0($1:i32)
CONST $2 0 :bool
CONST $3 1 :bool
CONST $2 false :bool
CONST $3 true :bool
CONST $4 1 :i32
LTE $5 $1 $4 :bool
CMP $6 $5 B1($2) B2($2,$1,$3) :void
Expand Down
4 changes: 2 additions & 2 deletions nautilus/test/data/loop-tests/tracing/isPrime.trace
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
B0($1:i32)
CONST $2 0 :bool
CONST $3 1 :bool
CONST $2 false :bool
CONST $3 true :bool
CONST $4 1 :i32
LTE $5 $1 $4 :bool
CMP $6 $5 B1() B2() :void
Expand Down
Binary file modified nautilus/test/data/pointer-tests/after_ssa/addArray_i8.trace
Binary file not shown.
Binary file modified nautilus/test/data/pointer-tests/tracing/addArray_i8.trace
Binary file not shown.
4 changes: 3 additions & 1 deletion nautilus/test/execution-tests/TracingTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ void runTraceTests(const std::string& category, std::vector<std::tuple<std::stri
for (auto& test : tests) {
auto func = std::get<1>(test);
auto name = std::get<0>(test);
auto executionTrace = tracing::TraceContext::trace(func);
DYNAMIC_SECTION(name) {
auto executionTrace = tracing::TraceContext::trace(func);
DYNAMIC_SECTION("tracing") {
REQUIRE(checkTestFile(executionTrace.get(), category, "tracing", name));
}
Expand Down Expand Up @@ -276,6 +276,8 @@ TEST_CASE("Enum Trace Test") {
{"isEnum", details::createFunctionWrapper(isEnum)},
{"getEnum", details::createFunctionWrapper(getEnum)},
{"callEnumFunction", details::createFunctionWrapper(callEnumFunction)},
{"callEnumClassFunction", details::createFunctionWrapper(callEnumClassFunction)},
{"handleEnumLogLevel", details::createFunctionWrapper(handleEnumLogLevel)},
};
runTraceTests("enum-tests", tests);
}
Expand Down

0 comments on commit 4196c18

Please sign in to comment.