From c20e29a930e01aa884911791bce08bc447bb0239 Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Sun, 29 Dec 2019 17:50:42 +0100 Subject: ptrlist: remove one pointer level from TYPEOF() The macro TYPEOF() return the type of the addresses of the pointers stored in the list. That's one level too much in general. Change it to simply return the type of the stored pointers. Signed-off-by: Luc Van Oostenryck --- ptrlist.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ptrlist.h b/ptrlist.h index c5fa4cdd..41d9011c 100644 --- a/ptrlist.h +++ b/ptrlist.h @@ -12,7 +12,7 @@ /* Silly type-safety check ;) */ #define CHECK_TYPE(head,ptr) (void)(&(ptr) == &(head)->list[0]) -#define TYPEOF(head) __typeof__(&(head)->list[0]) +#define TYPEOF(head) __typeof__((head)->list[0]) #define VRFY_PTR_LIST(head) (void)(sizeof((head)->list[0])) #define LIST_NODE_NR (13) @@ -251,7 +251,7 @@ extern void __free_ptr_list(struct ptr_list **); extern void split_ptr_list_head(struct ptr_list *); #define DO_INSERT_CURRENT(new, __head, __list, __nr) do { \ - TYPEOF(__head) __this, __last; \ + TYPEOF(__head) *__this, *__last; \ if (__list->nr == LIST_NODE_NR) { \ split_ptr_list_head((struct ptr_list*)__list); \ if (__nr >= __list->nr) { \ @@ -270,8 +270,8 @@ extern void split_ptr_list_head(struct ptr_list *); } while (0) #define DO_DELETE_CURRENT(__head, __list, __nr) do { \ - TYPEOF(__head) __this = __list->list + __nr; \ - TYPEOF(__head) __last = __list->list + __list->nr - 1; \ + TYPEOF(__head) *__this = __list->list + __nr; \ + TYPEOF(__head) *__last = __list->list + __list->nr - 1; \ while (__this < __last) { \ __this[0] = __this[1]; \ __this++; \ -- cgit 1.2.3-korg From f909765ac1e826b7360775438c34f5401e80fca0 Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Sun, 14 Feb 2021 15:25:03 +0100 Subject: ptrlist: change TYPEOF() into PTRLIST_TYPE() The name of the macro TYPEOF() is too generic and doesn't explain that it only returns the type of the pointers stored in ptrlists. So, change the name to something more explicit: PTRLIST_TYPE(). Signed-off-by: Luc Van Oostenryck --- ptrlist.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ptrlist.h b/ptrlist.h index 41d9011c..3b952097 100644 --- a/ptrlist.h +++ b/ptrlist.h @@ -12,7 +12,7 @@ /* Silly type-safety check ;) */ #define CHECK_TYPE(head,ptr) (void)(&(ptr) == &(head)->list[0]) -#define TYPEOF(head) __typeof__((head)->list[0]) +#define PTRLIST_TYPE(head) __typeof__((head)->list[0]) #define VRFY_PTR_LIST(head) (void)(sizeof((head)->list[0])) #define LIST_NODE_NR (13) @@ -75,7 +75,7 @@ extern void __free_ptr_list(struct ptr_list **); #define ptr_list_nth(lst, nth) ({ \ struct ptr_list* head = (struct ptr_list*)(lst); \ - (__typeof__((lst)->list[0])) ptr_list_nth_entry(head, nth);\ + (PTRLIST_TYPE(lst)) ptr_list_nth_entry(head, nth);\ }) //////////////////////////////////////////////////////////////////////// @@ -251,7 +251,7 @@ extern void __free_ptr_list(struct ptr_list **); extern void split_ptr_list_head(struct ptr_list *); #define DO_INSERT_CURRENT(new, __head, __list, __nr) do { \ - TYPEOF(__head) *__this, *__last; \ + PTRLIST_TYPE(__head) *__this, *__last; \ if (__list->nr == LIST_NODE_NR) { \ split_ptr_list_head((struct ptr_list*)__list); \ if (__nr >= __list->nr) { \ @@ -270,8 +270,8 @@ extern void split_ptr_list_head(struct ptr_list *); } while (0) #define DO_DELETE_CURRENT(__head, __list, __nr) do { \ - TYPEOF(__head) *__this = __list->list + __nr; \ - TYPEOF(__head) *__last = __list->list + __list->nr - 1; \ + PTRLIST_TYPE(__head) *__this = __list->list + __nr; \ + PTRLIST_TYPE(__head) *__last = __list->list + __list->nr - 1; \ while (__this < __last) { \ __this[0] = __this[1]; \ __this++; \ -- cgit 1.2.3-korg From 6bd31037f468ca68c1fea5463d8ff786618467cd Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Sun, 29 Dec 2019 17:51:03 +0100 Subject: ptrlist: add pop_ptr_list() Some algorithms need a stack or a working list from which the last element can be removed. The ptrlist API has a function to do this but it's not typed and thus needs a wrapper for each type it's used for. Simplify this by adding a generic (but type-safe) macro for this while also giving it a nicer name: pop_ptr_list(). Signed-off-by: Luc Van Oostenryck --- ptrlist.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ptrlist.h b/ptrlist.h index 3b952097..0b061422 100644 --- a/ptrlist.h +++ b/ptrlist.h @@ -67,6 +67,12 @@ extern void **__add_ptr_list_tag(struct ptr_list **, void *, unsigned long); (__typeof__(&(ptr))) __add_ptr_list_tag(head, ptr, tag);\ }) +#define pop_ptr_list(l) ({ \ + PTRLIST_TYPE(*(l)) ptr; \ + ptr = delete_ptr_list_last((struct ptr_list**)(l)); \ + ptr; \ + }) + extern void __free_ptr_list(struct ptr_list **); #define free_ptr_list(list) do { \ VRFY_PTR_LIST(*(list)); \ -- cgit 1.2.3-korg From a69f8d70d4979e482214d9604ab4223dc3a00ed6 Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Sun, 14 Feb 2021 15:17:44 +0100 Subject: ptrlist: use ptr_list_nth() instead of linearize_ptr_list() Sparse has a few extra checkers for some functions. The one for memset has its own helper to retrieve its 3rd arguments. Remove this helper and use the generic ptr_list_nth() instead. Signed-off-by: Luc Van Oostenryck --- sparse.c | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/sparse.c b/sparse.c index 151eaf4e..9d62d4fe 100644 --- a/sparse.c +++ b/sparse.c @@ -163,20 +163,9 @@ static void check_byte_count(struct instruction *insn, pseudo_t count) /* OK, we could try to do the range analysis here */ } -static pseudo_t argument(struct instruction *call, unsigned int argno) -{ - pseudo_t args[8]; - struct ptr_list *arg_list = (struct ptr_list *) call->arguments; - - argno--; - if (linearize_ptr_list(arg_list, (void *)args, 8) > argno) - return args[argno]; - return NULL; -} - static void check_memset(struct instruction *insn) { - check_byte_count(insn, argument(insn, 3)); + check_byte_count(insn, ptr_list_nth(insn->arguments, 3)); } #define check_memcpy check_memset -- cgit 1.2.3-korg From c50f5272ea4275b3f79d2c71115f2be014ed11ee Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Sun, 29 Dec 2019 17:51:03 +0100 Subject: ptrlist: make linearize_ptr_list() generic The ptrlist API has a function to copy the elements of a ptrlist into an array but it's not typed and thus needs a wrapper (or casts) for each type it's used for. Also, 'linearize' is confusing since this is unrelated to Sparse's linearization. Simplify this by adding a generic (but type-safe) macro for this with a more descriptive name: ptr_list_to_array() Signed-off-by: Luc Van Oostenryck --- ptrlist.h | 6 ++++++ simplify.c | 4 ++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/ptrlist.h b/ptrlist.h index 0b061422..5a3dcbeb 100644 --- a/ptrlist.h +++ b/ptrlist.h @@ -84,6 +84,12 @@ extern void __free_ptr_list(struct ptr_list **); (PTRLIST_TYPE(lst)) ptr_list_nth_entry(head, nth);\ }) +#define ptr_list_to_array(list, array, size) ({ \ + struct ptr_list* head = (struct ptr_list*)(list); \ + CHECK_TYPE(list, *array); \ + linearize_ptr_list(head, (void**)array, size); \ + }) + //////////////////////////////////////////////////////////////////////// // API #define PREPARE_PTR_LIST(head, ptr) \ diff --git a/simplify.c b/simplify.c index 584078dd..cf5b3d74 100644 --- a/simplify.c +++ b/simplify.c @@ -83,7 +83,7 @@ static struct basic_block *phi_parent(struct basic_block *source, pseudo_t pseud // number of element, a positive number if there was // more than expected and a negative one if less. // -// :note: we can't reuse a function like linearize_ptr_list() +// :note: we can't reuse ptr_list_to_array() for the phi-sources // because any VOIDs in the phi-list must be ignored here // as in this context they mean 'entry has been removed'. static int get_phisources(struct instruction *sources[], int nbr, struct instruction *insn) @@ -116,7 +116,7 @@ static int if_convert_phi(struct instruction *insn) bb = insn->bb; if (get_phisources(array, 2, insn)) return 0; - if (linearize_ptr_list((struct ptr_list *)bb->parents, (void **)parents, 3) != 2) + if (ptr_list_to_array(bb->parents, parents, 3) != 2) return 0; p1 = array[0]->phi_src; bb1 = array[0]->bb; -- cgit 1.2.3-korg From f94f4a89ff6f31384cdfe77b5084601e7de2a131 Mon Sep 17 00:00:00 2001 From: Luc Van Oostenryck Date: Sun, 14 Feb 2021 15:11:13 +0100 Subject: ptrlist: change return value of linearize_ptr_list()/ptr_list_to_array() The function linearize_ptr_list() is annoying to use because it returns the number of elements put in the array. So, if you need to know if the list contained the expected number of entries, you need to allocate an array with one extra entry and check that the return value is one less than this size. So, change the function to return the total number of entries in the list. In other words, the return value corresponds now to the number of entries that could be copied if the size would be unlimited, much like it's done for snprintf(). The number of entries effectively copied stays, of course, limited by the size specified for the array. Signed-off-by: Luc Van Oostenryck --- ptrlist.c | 10 +++++----- simplify.c | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ptrlist.c b/ptrlist.c index 0f0b3f6d..c5766002 100644 --- a/ptrlist.c +++ b/ptrlist.c @@ -154,10 +154,10 @@ void *ptr_list_nth_entry(struct ptr_list *list, unsigned int idx) // @head: the list to be linearized // @arr: a ``void*`` array to fill with @head's entries // @max: the maximum number of entries to store into @arr -// @return: the number of entries linearized. +// @return: the number of entries in the list. // // Linearize the entries of a list up to a total of @max, -// and return the nr of entries linearized. +// and return the number of entries in the list. // // The array to linearize into (@arr) should really // be ``void *x[]``, but we want to let people fill in any kind @@ -170,14 +170,14 @@ int linearize_ptr_list(struct ptr_list *head, void **arr, int max) do { int i = list->nr; + nr += i; + if (max == 0) + continue; if (i > max) i = max; memcpy(arr, list->list, i*sizeof(void *)); arr += i; - nr += i; max -= i; - if (!max) - break; } while ((list = list->next) != head); } return nr; diff --git a/simplify.c b/simplify.c index cf5b3d74..207af8ed 100644 --- a/simplify.c +++ b/simplify.c @@ -108,7 +108,7 @@ static int get_phisources(struct instruction *sources[], int nbr, struct instruc static int if_convert_phi(struct instruction *insn) { struct instruction *array[2]; - struct basic_block *parents[3]; + struct basic_block *parents[2]; struct basic_block *bb, *bb1, *bb2, *source; struct instruction *br; pseudo_t p1, p2; @@ -116,7 +116,7 @@ static int if_convert_phi(struct instruction *insn) bb = insn->bb; if (get_phisources(array, 2, insn)) return 0; - if (ptr_list_to_array(bb->parents, parents, 3) != 2) + if (ptr_list_to_array(bb->parents, parents, 2) != 2) return 0; p1 = array[0]->phi_src; bb1 = array[0]->bb; -- cgit 1.2.3-korg