[PATCH] Add backend to graph basic blocks

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add a new backend program which parses the input files, processes them through
the linearization pass, and outputs a graphviz graph of the resulting basic
blocks.  Each entrypoint gets labelled by name, but for now the basic blocks
just get labelled with the address of the basic_block structure.

Signed-off-by: Josh Triplett <josh@xxxxxxxxxxxxxxx>
---
 .gitignore |    1 +
 Makefile   |    6 +++++-
 graph.c    |   63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 69 insertions(+), 1 deletions(-)

diff --git a/.gitignore b/.gitignore
index 32d6e70..3e12ce7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -13,6 +13,7 @@ test-parsing
 obfuscate
 check
 compile
+graph
 test-dissect
 test-linearize
 example
diff --git a/Makefile b/Makefile
index 378ee72..e71f360 100644
--- a/Makefile
+++ b/Makefile
@@ -21,7 +21,7 @@ # LDFLAGS += -Wl,-rpath,$(BINDIR)
 
 PREFIX=$(HOME)
 BINDIR=$(PREFIX)/bin
-PROGRAMS=test-lexing test-parsing obfuscate check compile test-linearize example test-unssa test-dissect
+PROGRAMS=test-lexing test-parsing obfuscate check compile graph test-linearize example test-unssa test-dissect
 
 LIB_H=    token.h parse.h lib.h symbol.h scope.h expression.h target.h \
 	  linearize.h bitmap.h ident-list.h compat.h flow.h allocate.h \
@@ -77,6 +77,9 @@ obfuscate: obfuscate.o $(LIBS)
 check: check.o $(LIBS)
 	$(CC) $(LDFLAGS) -o $@ $< $(LIBS)
 
+graph: graph.o $(LIBS)
+	$(CC) $(LDFLAGS) -o $@ $< $(LIBS)
+
 example: example.o $(LIBS)
 	$(CC) $(LDFLAGS) -o $@ $< $(LIBS)
 
@@ -123,6 +126,7 @@ obfuscate.o: $(LIB_H)
 example.o: $(LIB_H)
 storage.o: $(LIB_H) storage.h
 dissect.o: $(LIB_H) dissect.h
+graph.o: $(LIB_H)
 
 compat-linux.o: compat/strtold.c compat/mmap-blob.c \
 	$(LIB_H)
diff --git a/graph.c b/graph.c
new file mode 100644
index 0000000..db940ae
--- /dev/null
+++ b/graph.c
@@ -0,0 +1,63 @@
+/* Copyright © International Business Machines Corp., 2006
+ *
+ * Author: Josh Triplett <josh@xxxxxxxxxxxxxxx>
+ *
+ * Licensed under the Open Software License version 1.1
+ */
+#include <stdarg.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <ctype.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+#include "lib.h"
+#include "allocate.h"
+#include "token.h"
+#include "parse.h"
+#include "symbol.h"
+#include "expression.h"
+#include "linearize.h"
+
+static void graph_ep(struct entrypoint *ep)
+{
+	struct basic_block *bb;
+
+	printf("ep%p [label=\"%s\",shape=ellipse];\n",
+	       ep, show_ident(ep->name->ident));
+	FOR_EACH_PTR(ep->bbs, bb) {
+		printf("bb%p [shape=record,label=\"bb at %p\"]\n", bb, bb);
+	} END_FOR_EACH_PTR(bb);
+	FOR_EACH_PTR(ep->bbs, bb) {
+		struct basic_block *child;
+		FOR_EACH_PTR(bb->children, child) {
+			printf("bb%p -> bb%p;\n", bb, child);
+		} END_FOR_EACH_PTR(child);
+	} END_FOR_EACH_PTR(bb);
+	printf("ep%p -> bb%p;\n", ep, ep->entry->bb);
+}
+
+static void graph_symbols(struct symbol_list *list)
+{
+	struct symbol *sym;
+
+	FOR_EACH_PTR(list, sym) {
+		struct entrypoint *ep;
+
+		expand_symbol(sym);
+		ep = linearize_symbol(sym);
+		if (ep)
+			graph_ep(ep);
+	} END_FOR_EACH_PTR(sym);
+}
+
+int main(int argc, char **argv)
+{
+	printf("digraph control_flow {\n");
+	graph_symbols(sparse_initialize(argc, argv));
+	while (*argv)
+		graph_symbols(sparse(argv));
+	printf("}\n");
+	return 0;
+}


-
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Newbies FAQ]     [LKML]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Trinity Fuzzer Tool]

  Powered by Linux