Skip to contentSkip to conversation

cleanup write to argument array hack

PATCH In review Christopher Li 2 participants 2 messages Nov 30, 2006 – Dec 5, 2006 Sparse checker

lore
Share & tools
lore

Christopher Li

author maintainer
#0 lore raw

PATCH 1/1 cleanup write to argument array hack

The sparse interface is a kind of snaky that it change the input argument array. The function sparse() does the same hack just to skip the files.

This patch add the ptr list for string. So sparse_initialize will return list of file to compile. The string pointer is not aligned at word boundary. This patch introduce non taged version of the ptr list iteration function.

lib.h +4 -3
Index: sparse/lib.h
===================================================================
@@ -51,6 +51,7 @@
 DECLARE_PTR_LIST(instruction_list, struct instruction);
 DECLARE_PTR_LIST(multijmp_list, struct multijmp);
 DECLARE_PTR_LIST(pseudo_list, struct pseudo);
+DECLARE_PTR_LIST(string_list, char);
 
 typedef struct pseudo *pseudo_t;
 
@@ -89,9 +90,9 @@
 
 extern void declare_builtin_functions(void);
 extern void create_builtin_stream(void);
-extern struct symbol_list *sparse_initialize(int argc, char **argv);
-extern struct symbol_list *__sparse(char **argv);
-extern struct symbol_list *sparse(char **argv);
+extern struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list** files);
+extern struct symbol_list *__sparse(char *filename);
+extern struct symbol_list *sparse(char *filename);
 
 static inline int symbol_list_size(struct symbol_list* list)
 {
lib.c +6 -22
Index: sparse/lib.c
===================================================================
@@ -612,10 +612,9 @@
 	return sparse_tokenstream(token);
 }
 
-struct symbol_list *sparse_initialize(int argc, char **argv)
+struct symbol_list *sparse_initialize(int argc, char **argv, struct string_list **filelist)
 {
 	char **args;
-	int files = 0;
 	struct symbol_list *list;
 
 	// Initialize symbol stream first, so that we can add defines etc
@@ -631,17 +630,11 @@
 			args = handle_switch(arg+1, args);
 			continue;
 		}
-
-		/*
-		 * Hacky hacky hacky: we re-use the argument space
-		 * to save the filenames.
-		 */
-		argv[files++] = arg;
+		add_ptr_list_notag(filelist, arg);
 	}
 
 	list = NULL;
-	argv[files] = NULL;
-	if (files) {
+	if (!ptr_list_empty(filelist)) {
 		// Initialize type system
 		init_ctype();
 
@@ -661,22 +654,13 @@
 	return list;
 }
 
-struct symbol_list * __sparse(char **argv)
+struct symbol_list * __sparse(char *filename)
 {
 	struct symbol_list *res;
-	char *filename, *next;
 
 	/* Clear previous symbol list */
 	translation_unit_used_list = NULL;
 
-	filename = *argv;
-	if (!filename)
-		return NULL;
-	do {
-		next = argv[1];
-		*argv++ = next;
-	} while (next);
-
 	start_file_scope();
 	res = sparse_file(filename);
 	end_file_scope();
@@ -688,9 +672,9 @@
 	return res;
 }
 
-struct symbol_list * sparse(char **argv)
+struct symbol_list * sparse(char *filename)
 {
-	struct symbol_list *res = __sparse(argv);
+	struct symbol_list *res = __sparse(filename);
 
 	/* Evaluate the complete symbol list */
 	evaluate_symbol_list(res);
test-lexing.c +7 -3
Index: sparse/test-lexing.c
===================================================================
@@ -20,10 +20,14 @@
 
 int main(int argc, char **argv)
 {
+	struct string_list *filelist = NULL;
+	char *file;
+
 	preprocess_only = 1;
-	sparse_initialize(argc, argv);
-	while (*argv)
-		sparse(argv);
+	sparse_initialize(argc, argv, &filelist);
+	FOR_EACH_PTR_NOTAG(filelist, file) {
+		sparse(file);
+	} END_FOR_EACH_PTR_NOTAG(file);
 	show_identifier_stats();
 	return 0;
 }
test-dissect.c +6 -4
Index: sparse/test-dissect.c
===================================================================
@@ -83,13 +83,15 @@
 		.r_symbol = r_symbol,
 		.r_member = r_member,
 	};
+	struct string_list *filelist = NULL;
+	char *file;
 
-	sparse_initialize(argc, argv);
+	sparse_initialize(argc, argv, &filelist);
 
-	while (*argv) {
+	FOR_EACH_PTR_NOTAG(filelist, file) {
 		dotc_stream = input_stream_nr;
-		dissect(__sparse(argv), &reporter);
-	}
+		dissect(__sparse(file), &reporter);
+	} END_FOR_EACH_PTR_NOTAG(file);
 
 	return 0;
 }
test-linearize.c +7 -3
Index: sparse/test-linearize.c
===================================================================
@@ -38,8 +38,12 @@
 
 int main(int argc, char **argv)
 {
-	clean_up_symbols(sparse_initialize(argc, argv));
-	while (*argv)
-		clean_up_symbols(sparse(argv));
+	struct string_list *filelist = NULL;
+	char *file;
+
+	clean_up_symbols(sparse_initialize(argc, argv, &filelist));
+	FOR_EACH_PTR_NOTAG(filelist, file) {
+		clean_up_symbols(sparse(file));
+	} END_FOR_EACH_PTR_NOTAG(file);
 	return 0;
 }
test-parsing.c +6 -4
Index: sparse/test-parsing.c
===================================================================
@@ -35,8 +35,10 @@
 int main(int argc, char **argv)
 {
 	struct symbol_list * list;
+	struct string_list * filelist = NULL;
+	char *file;
 
-	list = sparse_initialize(argc, argv);
+	list = sparse_initialize(argc, argv, &filelist);
 
 	// Simplification
 	clean_up_symbols(list);
@@ -46,8 +48,8 @@
 	printf("\n\n");
 #endif
 
-	while (*argv) {
-		list = sparse(argv);
+	FOR_EACH_PTR_NOTAG(filelist, file) {
+		list = sparse(file);
 
 		// Simplification
 		clean_up_symbols(list);
@@ -57,7 +59,7 @@
 		show_symbol_list(list, "\n\n");
 		printf("\n\n");
 #endif
-	}
+	} END_FOR_EACH_PTR_NOTAG(file);
 
 #if 0
 	// And show the allocation statistics

Index: sparse/test-sort.c
===================================================================
Index: sparse/test-unssa.c
===================================================================

ptrlist.h +32 -16

136 lines not on this page · raw message

Index: sparse/ptrlist.c
===================================================================

sort.c +3 -3
Index: sparse/sort.c
===================================================================
@@ -138,7 +138,7 @@
 	// Do a quick skip in case entire blocks from b1 are
 	// already less than smallest element in b2.
 	while (b1->nr == 0 ||
-	       cmp (PTR_ENTRY(b1, b1->nr - 1), PTR_ENTRY(b2,0)) < 0) {
+	       cmp (PTR_ENTRY_WITHTAG(b1, b1->nr - 1), PTR_ENTRY_WITHTAG(b2,0)) < 0) {
 		// printf ("Skipping whole block.\n");
 		BEEN_THERE('H');
 		b1 = b1->next;
@@ -149,8 +149,8 @@
 	}
 
 	while (1) {
-		const void *d1 = PTR_ENTRY(b1,i1);
-		const void *d2 = PTR_ENTRY(b2,i2);
+		const void *d1 = PTR_ENTRY_WITHTAG(b1,i1);
+		const void *d2 = PTR_ENTRY_WITHTAG(b2,i2);
 
 		assert (i1 >= 0 && i1 < b1->nr);
 		assert (i2 >= 0 && i2 < b2->nr);
obfuscate.c +7 -3
Index: sparse/obfuscate.c
===================================================================
@@ -49,8 +49,12 @@
 
 int main(int argc, char **argv)
 {
-	emit_symbol_list(sparse_initialize(argc, argv));
-	while (*argv)
-		emit_symbol_list(sparse(argv));
+	struct string_list *filelist = NULL;
+	char *file;
+
+	emit_symbol_list(sparse_initialize(argc, argv, &filelist));
+	FOR_EACH_PTR_NOTAG(filelist, file) {
+		emit_symbol_list(sparse(file));
+	} END_FOR_EACH_PTR_NOTAG(file);
 	return 0;
 }
compile.c +10 -9
Index: sparse/compile.c
===================================================================
@@ -37,21 +37,22 @@
 
 int main(int argc, char **argv)
 {
-	const char *filename;
+	char *file;
+	struct string_list *filelist = NULL;
 
-	clean_up_symbols(sparse_initialize(argc, argv));
-	while ((filename = *argv) != NULL) {
+	clean_up_symbols(sparse_initialize(argc, argv, &filelist));
+	FOR_EACH_PTR_NOTAG(filelist, file) {
 		struct symbol_list *list;
-		const char *basename = strrchr(filename, '/');
-		if (basename)
-			filename = basename+1;
-		list = sparse(argv);
+		const char *basename = strrchr(file, '/');
+		basename = basename ?  basename+1 : file;
+
+		list = sparse(file);
 
 		// Do type evaluation and simplification
-		emit_unit_begin(filename);
+		emit_unit_begin(basename);
 		clean_up_symbols(list);
 		emit_unit_end();
-	}
+	} END_FOR_EACH_PTR_NOTAG(file);
 
 #if 0
 	// And show the allocation statistics
graph.c +8 -3
Index: sparse/graph.c
===================================================================
@@ -55,10 +55,15 @@
 
 int main(int argc, char **argv)
 {
+	struct string_list *filelist = NULL;
+	char *file;
+
 	printf("digraph control_flow {\n");
-	graph_symbols(sparse_initialize(argc, argv));
-	while (*argv)
-		graph_symbols(sparse(argv));
+	graph_symbols(sparse_initialize(argc, argv, &filelist));
+	
+	FOR_EACH_PTR_NOTAG(filelist, file) {
+		graph_symbols(sparse(file));
+	} END_FOR_EACH_PTR_NOTAG(file);
 	printf("}\n");
 	return 0;
 }
check.c +7 -3
Index: sparse/check.c
===================================================================
@@ -271,9 +271,13 @@
 
 int main(int argc, char **argv)
 {
+	struct string_list *filelist = NULL;
+	char *file;
+
 	// Expand, linearize and show it.
-	check_symbols(sparse_initialize(argc, argv));
-	while (*argv)
-		check_symbols(sparse(argv));
+	check_symbols(sparse_initialize(argc, argv, &filelist));
+	FOR_EACH_PTR_NOTAG(filelist, file) {
+		check_symbols(sparse(file));
+	} END_FOR_EACH_PTR_NOTAG(file);
 	return 0;
 }
example.c +7 -3
Index: sparse/example.c
===================================================================
@@ -1942,9 +1942,13 @@
 
 int main(int argc, char **argv)
 {
-	compile(sparse_initialize(argc, argv));
-	while (*argv)
-		compile(sparse(argv));
+	struct string_list *filelist = NULL;
+	char *file;
+
+	compile(sparse_initialize(argc, argv, &filelist));
+	FOR_EACH_PTR_NOTAG(filelist, file) {
+		compile(sparse(file));
+	} END_FOR_EACH_PTR_NOTAG(file);
 	return 0;
 }
 
#1 lore raw
Quoted 7 lines · Christopher Li

The sparse interface is a kind of snaky that it change the input argument array. The function sparse() does the same hack just to skip the files.

This patch add the ptr list for string. So sparse_initialize will return list of file to compile. The string pointer is not aligned at word boundary. This patch introduce non taged version of the ptr list iteration function.

Nice improvement; thanks.

Since tagged lists seem like by far the common case, I'd prefer to see PTR_ENTRY keep its current name, as you did with add_ptr_list. Also, your patch didn't update test-unssa.c. I've merged your patch, with those two additional changes.

I'd also love to see some additional static type checking here, to prevent using the untagged functions on a tagged list or vice versa at compile time, rather than allowing mysterious corruption at runtime.

- Josh Triplett