Skip to content

Commit

Permalink
Refactor code to use DeeObject_TryCompareEq()
Browse files Browse the repository at this point in the history
  • Loading branch information
GrieferAtWork committed May 5, 2024
1 parent 06d203d commit c0c5f64
Show file tree
Hide file tree
Showing 34 changed files with 506 additions and 475 deletions.
1 change: 0 additions & 1 deletion cpp.hint
Original file line number Diff line number Diff line change
Expand Up @@ -1485,7 +1485,6 @@
#define DEFINE_SEO_UNARY_INPLACE(n,...) int n(SeqEachOperator**p_self){}
#define DEFINE_SEO_BINARY_INPLACE(n,...) int n(SeqEachOperator**p_self,DeeObject*other){}
#define DEFINE_SUBRANGEITERATOR_COMPARE(n,...) DeeObject*n(SubRangeIterator*self,SubRangeIterator*other){}
#define DEFINE_TRANSFORMATIONITERATOR_COMPARE(n,...) DeeObject*n(TransformationIterator*self,TransformationIterator*other){}
#define DEFINE_FOLD_COMPARE(n,T) size_t n(T const*data,size_t datalen,uint32_t fold[UNICODE_FOLDED_MAX],size_t fold_len);
#define DEFINE_MEMCASECHR(n,rname,T,dee_foldcmp) \
T*n(T const*haystack,T needle,size_t haystack_length){}\
Expand Down
57 changes: 49 additions & 8 deletions include/deemon/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ DFUNDEF ATTR_PURE WUNUSED ATTR_INS(1, 2) Dee_hash_t (DCALL Dee_HashCase4Byte)(ui
/* Helper macros for implementing compare operators. */
#define Dee_CompareNe(a, b) ((a) < (b) ? -1 : 1)
#define Dee_Compare(a, b) ((a) == (b) ? 0 : Dee_CompareNe(a, b))

#define Dee_return_compare_if_ne(a, b) \
do { \
if ((a) != (b)) \
Expand All @@ -241,6 +242,27 @@ DFUNDEF ATTR_PURE WUNUSED ATTR_INS(1, 2) Dee_hash_t (DCALL Dee_HashCase4Byte)(ui
do { \
return Dee_Compare(a, b); \
} __WHILE0
#define Dee_return_DeeObject_Compare_if_ne(a, b) \
do { \
int _rtceqin_temp = DeeObject_Compare((DeeObject *)Dee_REQUIRES_OBJECT(a), \
(DeeObject *)Dee_REQUIRES_OBJECT(b)); \
if (_rtceqin_temp != 0) \
return _rtceqin_temp; \
} __WHILE0
#define Dee_return_DeeObject_CompareEq_if_ne(a, b) \
do { \
int _rtceqin_temp = DeeObject_CompareEq((DeeObject *)Dee_REQUIRES_OBJECT(a), \
(DeeObject *)Dee_REQUIRES_OBJECT(b)); \
if (_rtceqin_temp != 0) \
return _rtceqin_temp; \
} __WHILE0
#define Dee_return_DeeObject_TryCompareEq_if_ne(a, b) \
do { \
int _rtceqin_temp = DeeObject_TryCompareEq((DeeObject *)Dee_REQUIRES_OBJECT(a), \
(DeeObject *)Dee_REQUIRES_OBJECT(b)); \
if (_rtceqin_temp != 0) \
return _rtceqin_temp; \
} __WHILE0


typedef __hybrid_uint128_t Dee_uint128_t;
Expand Down Expand Up @@ -4502,8 +4524,16 @@ DFUNDEF WUNUSED NONNULL((1, 2)) int (DCALL DeeObject_CmpGeAsBool)(DeeObject *sel
* @return: 1 : Compare returns "true"
* @return: 0 : Compare returns "false"
* @return: -1: Error */
DFUNDEF WUNUSED NONNULL((1, 2)) int
(DCALL DeeObject_TryCmpEqAsBool)(DeeObject *self, DeeObject *some_object); /* DEPRECATED! */
DFUNDEF WUNUSED NONNULL((1, 2)) int /* DEPRECATED! */
(DCALL DeeObject_TryCmpEqAsBool)(DeeObject *self, DeeObject *some_object);

/* Deprecated wrapper around "DeeObject_TryCompareEqKey()"
* @return: 1 : Compare returns "true"
* @return: 0 : Compare returns "false"
* @return: -1: Error */
DFUNDEF WUNUSED NONNULL((1, 2)) int /* DEPRECATED! */
(DCALL DeeObject_TryCmpKeyEqAsBool)(DeeObject *keyed_search_item,
DeeObject *elem, /*nullable*/ DeeObject *key);



Expand Down Expand Up @@ -4544,13 +4574,24 @@ DFUNDEF WUNUSED NONNULL((1, 2)) int
(DCALL DeeObject_CompareKey)(DeeObject *lhs_keyed,
DeeObject *rhs, /*nullable*/ DeeObject *key);

/* Compare a pre-keyed `keyed_search_item' with `elem' using the given (optional) `key' function
* @return: > 0: `keyed_search_item == key(elem)'
* @return: == 0: `keyed_search_item != key(elem)'
* @return: < 0: An error occurred. */
/* Compare a pre-keyed `lhs_keyed' with `rhs' using the given (optional) `key' function
* @return: == -1: `lhs_keyed != key(rhs)'
* @return: == 0: `lhs_keyed == key(rhs)'
* @return: == 1: `lhs_keyed != key(rhs)'
* @return: == Dee_COMPARE_ERR: An error occurred. */
DFUNDEF WUNUSED NONNULL((1, 2)) int
(DCALL DeeObject_CompareKeyEq)(DeeObject *keyed_search_item,
DeeObject *elem, /*nullable*/ DeeObject *key);
(DCALL DeeObject_CompareKeyEq)(DeeObject *lhs_keyed,
DeeObject *rhs, /*nullable*/ DeeObject *key);

/* Compare a pre-keyed `lhs_keyed' with `rhs' using the given (optional) `key' function
* @return: == -1: `lhs_keyed != key(rhs)'
* @return: == 0: `lhs_keyed == key(rhs)'
* @return: == 1: `lhs_keyed != key(rhs)'
* @return: == Dee_COMPARE_ERR: An error occurred. */
DFUNDEF WUNUSED NONNULL((1, 2)) int
(DCALL DeeObject_TryCompareKeyEq)(DeeObject *lhs_keyed,
DeeObject *rhs, /*nullable*/ DeeObject *key);


/* Sequence operator invocation. */
DFUNDEF WUNUSED NONNULL((1)) size_t (DCALL DeeObject_Size)(DeeObject *__restrict self); /* @return: (size_t)-1: Error */
Expand Down
34 changes: 17 additions & 17 deletions src/deemon/execute/code-exec.c.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2357,23 +2357,23 @@ do_push_module:
DISPATCH();
}

#define DEFINE_COMPARE_INSTR(ASM_CMP_EQ, DeeObject_CmpEq) \
TARGET(ASM_CMP_EQ, -2, +1) { \
DREF DeeObject *temp; \
temp = DeeObject_CmpEq(SECOND, FIRST); \
if unlikely(!temp) \
HANDLE_EXCEPT(); \
POPREF(); \
Dee_Decref(TOP); \
TOP = temp; /* Inherit reference. */ \
DISPATCH(); \
}
DEFINE_COMPARE_INSTR(ASM_CMP_EQ, DeeObject_CmpEq)
DEFINE_COMPARE_INSTR(ASM_CMP_NE, DeeObject_CmpNe)
DEFINE_COMPARE_INSTR(ASM_CMP_LO, DeeObject_CmpLo)
DEFINE_COMPARE_INSTR(ASM_CMP_LE, DeeObject_CmpLe)
DEFINE_COMPARE_INSTR(ASM_CMP_GR, DeeObject_CmpGr)
DEFINE_COMPARE_INSTR(ASM_CMP_GE, DeeObject_CmpGe)
#define DEFINE_COMPARE_INSTR(EQ, DeeObject_CmpEq) \
TARGET(ASM_CMP_##EQ, -2, +1) { \
DREF DeeObject *temp; \
temp = DeeObject_CmpEq(SECOND, FIRST); \
if unlikely(!temp) \
HANDLE_EXCEPT(); \
POPREF(); \
Dee_Decref(TOP); \
TOP = temp; /* Inherit reference. */ \
DISPATCH(); \
}
DEFINE_COMPARE_INSTR(EQ, DeeObject_CmpEq)
DEFINE_COMPARE_INSTR(NE, DeeObject_CmpNe)
DEFINE_COMPARE_INSTR(LO, DeeObject_CmpLo)
DEFINE_COMPARE_INSTR(LE, DeeObject_CmpLe)
DEFINE_COMPARE_INSTR(GR, DeeObject_CmpGr)
DEFINE_COMPARE_INSTR(GE, DeeObject_CmpGe)
#undef DEFINE_COMPARE_INSTR

TARGET(ASM_CLASS_C, -1, +1) {
Expand Down
2 changes: 2 additions & 0 deletions src/deemon/linker-scripts/link-deemon-gcc-i386-cygwin.def
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,9 @@ EXPORTS
_DeeObject_ThisCallTuple@12=DeeObject_ThisCallTuple@12
_DeeObject_ThisCallTupleKw@16=DeeObject_ThisCallTupleKw@16
_DeeObject_TryCmpEqAsBool@8=DeeObject_TryCmpEqAsBool@8
_DeeObject_TryCmpKeyEqAsBool@12=DeeObject_TryCmpKeyEqAsBool@12
_DeeObject_TryCompareEq@8=DeeObject_TryCompareEq@8
_DeeObject_TryCompareKeyEq@12=DeeObject_TryCompareKeyEq@12
_DeeObject_TryGetItem@8=DeeObject_TryGetItem@8
_DeeObject_TryGetItemIndex@8=DeeObject_TryGetItemIndex@8
_DeeObject_TryGetItemStringHash@12=DeeObject_TryGetItemStringHash@12
Expand Down
2 changes: 2 additions & 0 deletions src/deemon/linker-scripts/link-deemon-msvc-i386-win32.def
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,9 @@ EXPORTS
DeeObject_ThisCallTuple@12=_DeeObject_ThisCallTuple@12
DeeObject_ThisCallTupleKw@16=_DeeObject_ThisCallTupleKw@16
DeeObject_TryCmpEqAsBool@8=_DeeObject_TryCmpEqAsBool@8
DeeObject_TryCmpKeyEqAsBool@12=_DeeObject_TryCmpKeyEqAsBool@12
DeeObject_TryCompareEq@8=_DeeObject_TryCompareEq@8
DeeObject_TryCompareKeyEq@12=_DeeObject_TryCompareKeyEq@12
DeeObject_TryGetItem@8=_DeeObject_TryGetItem@8
DeeObject_TryGetItemIndex@8=_DeeObject_TryGetItemIndex@8
DeeObject_TryGetItemStringHash@12=_DeeObject_TryGetItemStringHash@12
Expand Down
36 changes: 13 additions & 23 deletions src/deemon/objects/attribute.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,9 @@ attr_visit(Attr *__restrict self, dvisit_t proc, void *arg) {
Dee_XVisit(self->a_info.a_attrtype);
}

PRIVATE WUNUSED NONNULL((1)) dhash_t DCALL
PRIVATE WUNUSED NONNULL((1)) Dee_hash_t DCALL
attr_hash(Attr *__restrict self) {
dhash_t result;
Dee_hash_t result;
result = self->a_info.a_perm;
result = Dee_HashCombine(result, DeeObject_Hash(self->a_info.a_decl));
result = Dee_HashCombine(result, Dee_HashPointer(self->a_info.a_attrtype));
Expand All @@ -111,21 +111,14 @@ attr_hash(Attr *__restrict self) {
return result;
}

PRIVATE WUNUSED NONNULL((1, 2)) DREF DeeObject *DCALL
attr_eq(Attr *self, Attr *other) {
int result;
PRIVATE WUNUSED NONNULL((1, 2)) int DCALL
attr_compare_eq(Attr *self, Attr *other) {
if (DeeObject_AssertType(other, &DeeAttribute_Type))
goto err;
if (self->a_info.a_attrtype != other->a_info.a_attrtype)
goto nope;
if (self->a_info.a_decl != other->a_info.a_decl) {
result = DeeObject_TryCmpEqAsBool(self->a_info.a_decl,
other->a_info.a_decl);
if unlikely(result < 0)
goto err;
if (!result)
goto nope;
}
if (self->a_info.a_decl != other->a_info.a_decl)
Dee_return_DeeObject_TryCompareEq_if_ne(self->a_info.a_decl, other->a_info.a_decl);
if ((self->a_info.a_perm & ~(ATTR_NAMEOBJ | ATTR_DOCOBJ)) !=
(other->a_info.a_perm & ~(ATTR_NAMEOBJ | ATTR_DOCOBJ)))
goto nope;
Expand All @@ -144,19 +137,16 @@ attr_eq(Attr *self, Attr *other) {
goto nope;
}
yup:
return_true;
return 0;
nope:
return_false;
return 1;
err:
return NULL;
return Dee_COMPARE_ERR;
}

PRIVATE struct type_cmp attr_cmp = {
/* .tp_hash = */ (dhash_t (DCALL *)(DeeObject *__restrict))&attr_hash,
/* .tp_compare_eq = */ NULL,
/* .tp_compare = */ NULL,
/* .tp_trycompare_eq = */ NULL,
/* .tp_eq = */ (DREF DeeObject *(DCALL *)(DeeObject *, DeeObject *))&attr_eq,
/* .tp_hash = */ (Dee_hash_t (DCALL *)(DeeObject *__restrict))&attr_hash,
/* .tp_compare_eq = */ (int (DCALL *)(DeeObject *, DeeObject *))&attr_compare_eq,
};

PRIVATE struct type_member tpconst attr_members[] = {
Expand Down Expand Up @@ -905,7 +895,7 @@ enumattr_iter(EnumAttr *__restrict self) {
return result;
}

PRIVATE WUNUSED NONNULL((1)) dhash_t DCALL
PRIVATE WUNUSED NONNULL((1)) Dee_hash_t DCALL
enumattr_hash(EnumAttr *__restrict self) {
return ((self->ea_obj ? DeeObject_Hash(self->ea_obj) : 0) ^
Dee_HashPointer(self->ea_type));
Expand Down Expand Up @@ -936,7 +926,7 @@ enumattr_ne(EnumAttr *self,
}

PRIVATE struct type_cmp enumattr_cmp = {
/* .tp_hash = */ (dhash_t (DCALL *)(DeeObject *__restrict))&enumattr_hash,
/* .tp_hash = */ (Dee_hash_t (DCALL *)(DeeObject *__restrict))&enumattr_hash,
/* .tp_compare_eq = */ NULL,
/* .tp_compare = */ NULL,
/* .tp_trycompare_eq = */ NULL,
Expand Down
30 changes: 15 additions & 15 deletions src/deemon/objects/cached-dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -499,11 +499,11 @@ DeeCachedDict_Remember(DeeCachedDictObject *__restrict self,
Dee_Incref(item_key);
DeeCachedDict_LockEndRead(self);
/* Invoke the compare operator outside of any lock. */
error = DeeObject_TryCmpEqAsBool(key, item_key);
error = DeeObject_TryCompareEq(key, item_key);
Dee_Decref(item_key);
if unlikely(error < 0)
if unlikely(error == Dee_COMPARE_ERR)
goto err; /* Error in compare operator. */
if (error > 0) {
if (error == 0) {
DREF DeeObject *item_value;
/* Found an existing item. */
DeeCachedDict_LockWrite(self);
Expand Down Expand Up @@ -712,11 +712,11 @@ DeeCachedDict_GetItemNR(DeeCachedDictObject *self, DeeObject *key) {
DeeCachedDict_LockEndRead(self);

/* Invoke the compare operator outside of any lock. */
error = DeeObject_TryCmpEqAsBool(key, item_key);
if (error > 0)
return item_value; /* Found the item. */
if unlikely(error < 0)
error = DeeObject_TryCompareEq(key, item_key);
if unlikely(error == Dee_COMPARE_ERR)
goto err; /* Error in compare operator. */
if (error == 0)
return item_value; /* Found the item. */
DeeCachedDict_LockRead(self);

/* Check if the CachedDict was modified. */
Expand Down Expand Up @@ -808,11 +808,11 @@ DeeCachedDict_TryGetItemNR(DeeCachedDictObject *self, DeeObject *key) {
DeeCachedDict_LockEndRead(self);

/* Invoke the compare operator outside of any lock. */
error = DeeObject_TryCmpEqAsBool(key, item_key);
if (error > 0)
return item_value; /* Found the item. */
if unlikely(error < 0)
error = DeeObject_TryCompareEq(key, item_key);
if unlikely(error == Dee_COMPARE_ERR)
goto err; /* Error in compare operator. */
if (error == 0)
return item_value; /* Found the item. */
DeeCachedDict_LockRead(self);

/* Check if the CachedDict was modified. */
Expand Down Expand Up @@ -902,11 +902,11 @@ cdict_iscached(DeeCachedDictObject *self, DeeObject *key, Dee_hash_t hash) {
DeeCachedDict_LockEndRead(self);

/* Invoke the compare operator outside of any lock. */
error = DeeObject_TryCmpEqAsBool(key, item_key);
if (error > 0)
return 1; /* Found the item. */
if unlikely(error < 0)
error = DeeObject_TryCompareEq(key, item_key);
if unlikely(error == Dee_COMPARE_ERR)
goto err; /* Error in compare operator. */
if (error == 0)
return 1; /* Found the item. */
DeeCachedDict_LockRead(self);

/* Check if the CachedDict was modified. */
Expand Down
Loading

0 comments on commit c0c5f64

Please sign in to comment.