Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix bool operation return type in tracing #15

Merged
merged 2 commits into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 41 additions & 42 deletions nautilus/include/nautilus/val.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,7 @@ class val<bool> {
};

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

template <class T>
concept is_arithmetic_value = std::is_arithmetic_v<typename std::remove_reference_t<T>::basic_type>;
Expand Down Expand Up @@ -357,44 +356,46 @@ auto&& cast_value(LeftType&& value) {
namespace details {

#ifdef ENABLE_TRACING
#define TRAC_BINARY_OP(OP) \
if (tracing::inTracer()) { \
auto tc = tracing::traceBinaryOp<tracing::OP, commonType>(lValue.state, rValue.state); \
return val<commonType>(tc); \
#define TRAC_BINARY_OP(OP) \
if (tracing::inTracer()) { \
auto tc = tracing::traceBinaryOp<tracing::OP, commonType>(lValue.state, rValue.state); \
return val<commonType>(tc); \
}

#define TRAC_BINARY_OP_DIRECT(OP) \
if (tracing::inTracer()) { \
auto tc = tracing::traceBinaryOp<tracing::OP, commonType>(left.state, right.state); \
return val<commonType>(tc); \
#define TRAC_BINARY_OP_DIRECT(OP) \
if (tracing::inTracer()) { \
auto tc = tracing::traceBinaryOp<tracing::OP, commonType>(left.state, right.state); \
return val<commonType>(tc); \
}
#else
#define TRAC_OP(OP)
#endif

#ifdef ENABLE_TRACING
#define TRAC_LOGICAL_BINARY_OP(OP) \
if (tracing::inTracer()) { \
auto tc = tracing::traceBinaryOp<tracing::OP, bool>(lValue.state, rValue.state); \
return val<bool>(tc); \
#define TRAC_LOGICAL_BINARY_OP(OP) \
if (tracing::inTracer()) { \
auto tc = tracing::traceBinaryOp<tracing::OP, bool>(lValue.state, rValue.state); \
return val<bool>(tc); \
}
#else
#define TRAC_LOGICAL_BINARY_OP(OP)
#endif

#define DEFINE_BINARY_OPERATOR_HELPER(OP, OP_NAME, OP_TRACE, RES_TYPE) \
template <typename LHS, typename RHS> \
auto inline OP_NAME(LHS&& left, RHS&& right) { \
typedef typename std::common_type<typename LHS::basic_type, typename RHS::basic_type>::type commonType; \
auto&& lValue = cast_value<LHS, commonType>(std::forward<LHS>(left)); \
auto&& rValue = cast_value<RHS, commonType>(std::forward<RHS>(right)); \
if SHOULD_TRACE () { \
auto tc = tracing::traceBinaryOp<tracing::OP_TRACE, commonType>(details::getState(lValue), details::getState(rValue)); \
return RES_TYPE(tc); \
} \
return RES_TYPE(getRawValue(lValue) OP getRawValue(rValue)); \
#define DEFINE_BINARY_OPERATOR_HELPER(OP, OP_NAME, OP_TRACE, RES_TYPE) \
template <typename LHS, typename RHS> \
auto inline OP_NAME(LHS&& left, RHS&& right) { \
typedef typename std::common_type<typename LHS::basic_type, typename RHS::basic_type>::type commonType; \
auto&& lValue = cast_value<LHS, commonType>(std::forward<LHS>(left)); \
auto&& rValue = cast_value<RHS, commonType>(std::forward<RHS>(right)); \
using resultType = decltype(getRawValue(lValue) OP getRawValue(rValue)); \
if SHOULD_TRACE () { \
auto tc = tracing::traceBinaryOp<tracing::OP_TRACE, resultType>(details::getState(lValue), details::getState(rValue)); \
return RES_TYPE(tc); \
} \
return RES_TYPE(getRawValue(lValue) OP getRawValue(rValue)); \
}


DEFINE_BINARY_OPERATOR_HELPER(+, add, ADD, COMMON_RETURN_TYPE)

DEFINE_BINARY_OPERATOR_HELPER(-, sub, SUB, COMMON_RETURN_TYPE)
Expand Down Expand Up @@ -428,7 +429,7 @@ DEFINE_BINARY_OPERATOR_HELPER(|, bOr, BOR, COMMON_RETURN_TYPE)
DEFINE_BINARY_OPERATOR_HELPER(^, bXOr, BXOR, COMMON_RETURN_TYPE)

template <is_fundamental LHS>
val<LHS> neg(val<LHS>& val) {
val<LHS> neg(val<LHS>& val) {
#ifdef ENABLE_TRACING
if (tracing::inTracer()) {
auto tc = traceUnaryOp<tracing::NEGATE, LHS>(val.state);
Expand All @@ -445,23 +446,21 @@ LHS inline getRawValue(val<LHS>& val) {

} // namespace details

#define DEFINE_BINARY_OPERATOR(OP, FUNC) \
template <typename LHS, typename RHS> \
requires(is_fundamental_value<LHS> && (is_fundamental_value<RHS> || convertible_to_fundamental<RHS>) ) || \
((is_fundamental_value<LHS> || convertible_to_fundamental<LHS>) && is_fundamental_value<RHS>) \
auto inline operator OP(LHS&& left, RHS&& right) { \
auto&& lhsV = make_value(std::forward<LHS>(left)); \
auto&& rhsV = make_value(std::forward<RHS>(right)); \
return details::FUNC(std::move(lhsV), std::move(rhsV)); \
#define DEFINE_BINARY_OPERATOR(OP, FUNC) \
template <typename LHS, typename RHS> \
requires(is_fundamental_value<LHS> && (is_fundamental_value<RHS> || convertible_to_fundamental<RHS>) ) || ((is_fundamental_value<LHS> || convertible_to_fundamental<LHS>) && is_fundamental_value<RHS>) \
auto inline operator OP(LHS&& left, RHS&& right) { \
auto&& lhsV = make_value(std::forward<LHS>(left)); \
auto&& rhsV = make_value(std::forward<RHS>(right)); \
return details::FUNC(std::move(lhsV), std::move(rhsV)); \
}
#define DEFINE_ARITHMETICAL_BINARY_OPERATOR(OP, FUNC) \
template <typename LHS, typename RHS> \
requires(is_arithmetic_value<LHS> && (is_arithmetic_value<RHS> || convertible_to_fundamental<RHS>) ) || \
((is_arithmetic_value<LHS> || convertible_to_fundamental<LHS>) && is_arithmetic_value<RHS>) \
auto inline operator OP(LHS&& left, RHS&& right) { \
auto&& lhsV = make_value(std::forward<LHS>(left)); \
auto&& rhsV = make_value(std::forward<RHS>(right)); \
return details::FUNC(std::move(lhsV), std::move(rhsV)); \
#define DEFINE_ARITHMETICAL_BINARY_OPERATOR(OP, FUNC) \
template <typename LHS, typename RHS> \
requires(is_arithmetic_value<LHS> && (is_arithmetic_value<RHS> || convertible_to_fundamental<RHS>) ) || ((is_arithmetic_value<LHS> || convertible_to_fundamental<LHS>) && is_arithmetic_value<RHS>) \
auto inline operator OP(LHS&& left, RHS&& right) { \
auto&& lhsV = make_value(std::forward<LHS>(left)); \
auto&& rhsV = make_value(std::forward<RHS>(right)); \
return details::FUNC(std::move(lhsV), std::move(rhsV)); \
}

DEFINE_ARITHMETICAL_BINARY_OPERATOR(+, add)
Expand Down
2 changes: 1 addition & 1 deletion nautilus/test/data/after_ssa/addArrayInt32.trace
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ B1($3:i32,$4:i32,$1:ptr,$2:ptr)
B2()
RETURN $0 :void
B3($4:i32,$3:i32,$1:ptr,$2:ptr) ControlFlowMerge
LT $5 $4 $3 :i32
LT $5 $4 $3 :bool
CMP $6 $5 B1($3,$4,$1,$2) B2() :void
4 changes: 2 additions & 2 deletions nautilus/test/data/after_ssa/andCondition.trace
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
B0($1:i32,$2:i32)
CONST $3 1 :i32
CONST $4 8 :i32
EQ $5 $1 $4 :i32
EQ $5 $1 $4 :bool
CONST $6 1 :i32
EQ $7 $2 $6 :i32
EQ $7 $2 $6 :bool
AND $8 $5 $7 :bool
CMP $9 $8 B1($3) B2($3) :void
B1($3:i32)
Expand Down
6 changes: 3 additions & 3 deletions nautilus/test/data/after_ssa/andFunction.trace
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ B0($1:i32)
CONST $2 :bool
CONST $4 42 :i64
CAST $5 $1 :i64
EQ $6 $5 $4 :i64
EQ $6 $5 $4 :bool
AND $7 $2 $6 :bool
CONST $9 42 :i64
CAST $10 $1 :i64
EQ $11 $10 $9 :i64
EQ $11 $10 $9 :bool
AND $12 $7 $11 :bool
CONST $14 42 :i64
CAST $15 $1 :i64
EQ $16 $15 $14 :i64
EQ $16 $15 $14 :bool
AND $17 $12 $16 :bool
RETURN $17 :bool
4 changes: 2 additions & 2 deletions nautilus/test/data/after_ssa/collatz.trace
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ B1($2:i32,$1:i32)
CONST $6 2 :i32
MOD $7 $1 $6 :i32
CONST $8 0 :i32
EQ $9 $7 $8 :i32
EQ $9 $7 $8 :bool
CMP $10 $9 B3($2,$1) B4($2,$1) :void
B2($2:i32)
RETURN $2 :i32
Expand All @@ -23,7 +23,7 @@ B4($2:i32,$1:i32)
CONST $22 1 :i32
JMP $0 B6($21,$2,$22) :void
B5($2:i32,$1:i32,$3:i32) ControlFlowMerge
NEQ $4 $1 $3 :i32
NEQ $4 $1 $3 :bool
CMP $5 $4 B1($2,$1) B2($2) :void
B6($1:i32,$2:i32,$13:i32) ControlFlowMerge
ADD $14 $2 $13 :i32
Expand Down
8 changes: 4 additions & 4 deletions nautilus/test/data/after_ssa/complexLogicalExpressions.trace
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
B0($1:i32)
CONST $2 0 :i32
CONST $3 5 :i32
GT $4 $1 $3 :i32
GT $4 $1 $3 :bool
CONST $5 10 :i32
LT $6 $1 $5 :i32
LT $6 $1 $5 :bool
AND $7 $4 $6 :bool
CONST $8 15 :i32
GT $9 $1 $8 :i32
GT $9 $1 $8 :bool
CONST $10 20 :i32
LT $11 $1 $10 :i32
LT $11 $1 $10 :bool
AND $12 $9 $11 :bool
OR $13 $7 $12 :bool
CMP $14 $13 B1() B2($2) :void
Expand Down
2 changes: 1 addition & 1 deletion nautilus/test/data/after_ssa/compoundStatements.trace
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
B0($1:i32)
CONST $2 0 :i32
CONST $3 5 :i32
GT $4 $1 $3 :i32
GT $4 $1 $3 :bool
CMP $5 $4 B1() B2($2) :void
B1()
CONST $6 1 :i32
Expand Down
2 changes: 1 addition & 1 deletion nautilus/test/data/after_ssa/conditionalReturn.trace
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
B0($1:i32)
CONST $2 42 :i32
EQ $3 $1 $2 :i32
EQ $3 $1 $2 :bool
CMP $4 $3 B1() B2() :void
B1()
CONST $5 1 :i32
Expand Down
2 changes: 1 addition & 1 deletion nautilus/test/data/after_ssa/countDigits.trace
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ B1($2:i32,$1:i32)
B2($2:i32)
RETURN $2 :i32
B3($2:i32,$1:i32,$3:i32) ControlFlowMerge
NEQ $4 $1 $3 :i32
NEQ $4 $1 $3 :bool
CMP $5 $4 B1($2,$1) B2($2) :void
2 changes: 1 addition & 1 deletion nautilus/test/data/after_ssa/decimalToBinary.trace
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ B3($1:i32,$3:i32,$2:i32,$4:i32) ControlFlowMerge
CONST $10 2 :i32
DIV $11 $1 $10 :i32
CONST $12 0 :i32
GT $13 $11 $12 :i32
GT $13 $11 $12 :bool
CMP $14 $13 B1($11,$9,$7) B2($7) :void
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
B0()
CONST $1 5 :i32
CONST $2 8 :i32
LT $3 $1 $2 :i32
LT $3 $1 $2 :bool
CMP $4 $3 B1($1) B2($1) :void
B1($1:i32)
CONST $5 6 :i32
GT $6 $1 $5 :i32
GT $6 $1 $5 :bool
CMP $7 $6 B3($1) B4($1) :void
B2($1:i32)
CONST $13 20 :i32
Expand All @@ -19,13 +19,13 @@ B3($1:i32)
JMP $0 B5($9,$10) :void
B4($1:i32)
CONST $16 6 :i32
LT $17 $1 $16 :i32
LT $17 $1 $16 :bool
CMP $18 $17 B6($1) B7($1) :void
B5($1:i32,$10:i32) ControlFlowMerge
JMP $0 B10($1,$10) :void
B6($1:i32)
CONST $19 5 :i32
EQ $20 $1 $19 :i32
EQ $20 $1 $19 :bool
CMP $21 $20 B8($1) B9($1) :void
B7($1:i32)
CONST $25 2 :i32
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
B0()
CONST $1 5 :i32
CONST $2 8 :i32
LT $3 $1 $2 :i32
LT $3 $1 $2 :bool
CMP $4 $3 B1($1) B2($1) :void
B1($1:i32)
CONST $5 10 :i32
Expand All @@ -10,7 +10,7 @@ B1($1:i32)
JMP $0 B5($6,$7) :void
B2($1:i32)
CONST $10 5 :i32
EQ $11 $1 $10 :i32
EQ $11 $1 $10 :bool
CMP $12 $11 B3($1) B4($1) :void
B3($1:i32)
CONST $13 5 :i32
Expand All @@ -19,7 +19,7 @@ B3($1:i32)
JMP $0 B5($14,$15) :void
B4($1:i32)
CONST $16 4 :i32
EQ $17 $1 $16 :i32
EQ $17 $1 $16 :bool
CMP $18 $17 B6($1) B7($1) :void
B5($1:i32,$7:i32) ControlFlowMerge
JMP $0 B8($1,$7) :void
Expand Down
2 changes: 1 addition & 1 deletion nautilus/test/data/after_ssa/digitSum.trace
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ B3($1:i32,$2:i32,$3:i32) ControlFlowMerge
CONST $6 10 :i32
DIV $7 $1 $6 :i32
CONST $8 0 :i32
GT $9 $7 $8 :i32
GT $9 $7 $8 :bool
CMP $10 $9 B1($7,$5) B2($5) :void
4 changes: 2 additions & 2 deletions nautilus/test/data/after_ssa/doubleIfCondition.trace
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
B0()
CONST $1 1 :i32
CONST $2 8 :i32
EQ $3 $1 $2 :i32
EQ $3 $1 $2 :bool
CMP $4 $3 B1($1) B2($1) :void
B1($1:i32)
CONST $5 1 :i32
Expand All @@ -18,7 +18,7 @@ B4($1:i32)
CONST $14 2 :i32
JMP $0 B6($1,$14) :void
B5($1:i32,$5:i32) ControlFlowMerge
EQ $6 $1 $5 :i32
EQ $6 $1 $5 :bool
CMP $7 $6 B3($1) B4($1) :void
B6($1:i32,$10:i32) ControlFlowMerge
ADD $11 $1 $10 :i32
Expand Down
4 changes: 2 additions & 2 deletions nautilus/test/data/after_ssa/elseOnlySumLoop.trace
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ B0()
JMP $0 B5($1,$2,$3) :void
B1($1:i32,$2:i32)
CONST $6 50 :i32
LT $7 $1 $6 :i32
LT $7 $1 $6 :bool
CMP $8 $7 B3($1,$2) B4($2,$1) :void
B2($1:i32)
RETURN $1 :i32
Expand All @@ -18,7 +18,7 @@ B4($2:i32,$1:i32)
CONST $14 1 :i32
JMP $0 B6($13,$2,$14) :void
B5($1:i32,$2:i32,$3:i32) ControlFlowMerge
LT $4 $2 $3 :i32
LT $4 $2 $3 :bool
CMP $5 $4 B1($1,$2) B2($1) :void
B6($1:i32,$2:i32,$9:i32) ControlFlowMerge
ADD $10 $2 $9 :i32
Expand Down
2 changes: 1 addition & 1 deletion nautilus/test/data/after_ssa/factorial.trace
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ B1($1:i32,$2:i32)
B2($2:i32)
RETURN $2 :i32
B3($2:i32,$1:i32,$3:i32) ControlFlowMerge
GT $4 $1 $3 :i32
GT $4 $1 $3 :bool
CMP $5 $4 B1($1,$2) B2($2) :void
2 changes: 1 addition & 1 deletion nautilus/test/data/after_ssa/fibonacci.trace
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ B1($1:i32,$5:i32,$3:i32,$2:i32)
B2($3:i32)
RETURN $3 :i32
B3($3:i32,$5:i32,$1:i32,$2:i32) ControlFlowMerge
LTE $6 $5 $1 :i32
LTE $6 $5 $1 :bool
CMP $7 $6 B1($1,$5,$3,$2) B2($3) :void
2 changes: 1 addition & 1 deletion nautilus/test/data/after_ssa/gcd.trace
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ B1($1:i32,$2:i32)
B2($1:i32)
RETURN $1 :i32
B3($1:i32,$2:i32,$3:i32) ControlFlowMerge
NEQ $4 $2 $3 :i32
NEQ $4 $2 $3 :bool
CMP $5 $4 B1($1,$2) B2($1) :void
Loading