From 38766114c76a469f67df77a2684154e2ca7a1a94 Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Tue, 29 Aug 2006 17:45:20 -0700 Subject: [PATCH] [PATCH] Add backend to graph basic blocks 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 Signed-off-by: Linus Torvalds --- .gitignore | 1 + Makefile | 6 +++++- graph.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 graph.c diff --git a/.gitignore b/.gitignore index 32d6e70a..3e12ce74 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 6755e07e..ccf9f891 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,7 @@ CFLAGS += -DDEBUG 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 00000000..db940ae7 --- /dev/null +++ b/graph.c @@ -0,0 +1,63 @@ +/* Copyright © International Business Machines Corp., 2006 + * + * Author: Josh Triplett + * + * Licensed under the Open Software License version 1.1 + */ +#include +#include +#include +#include +#include +#include +#include + +#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; +} -- 2.11.4.GIT