Source

LIME / lib / cgen.awk

Full commit
##################################################################
# Old stuff (from wesp) to generate dot from gcc parse tree
#
#Copyright (C) 2008 NXP Semiconductors B.V.
#
#This file is part of LIME.
#
#LIME is free software: you can redistribute it and/or modify
#it under the terms of the GNU General Public License version 2
#as published by the Free Software Foundation; either version 2
#of the License, or (at your option) any later version.
#
#LIME is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#GNU General Public License for more details.
#
#You should have received a copy of the GNU General Public License
#along with LIME.  If not, see <http://www.gnu.org/licenses/>.
##################################################################
BEGIN {
	if (OP=="esrap")  for (ix=1; ix<=nr; ix++)
	{
	    split(un[sorted[ix]],el," ")
	    n=el[++cnt[un[sorted[ix]]]]

	    if (modi[n] ~ /undefined/) continue
	    if (tag[n] !~ /function_decl/ || (FUNAME && name[n]!=FUNAME)) 
	    	continue

	    level=0
	    printf "%s",esrap(n)
	}

	if (OP=="gendot") {
	  printf "digraph tree_directed {\n"	\
		 "label=\"%s\"\n"		\
		 "overlap=false\n","top-level"
	  for (ix=1; ix<=nr; ix++)
	  {
	    	split(un[sorted[ix]],el," ")
		n=el[++cnt[un[sorted[ix]]]]

		if (modi[n] ~ /undefined/) continue
	    	if (tag[n] !~ /function_decl/ || (FUNAME && name[n]!=FUNAME)) 
			continue

		level=0
		printf "subgraph cluster_%i {\n",n
		gendot(n)
		print "}"
	  }
	  print "}"
	}
}

func gendot(b,old,exits,edge_label) {
  print "node [peripheries=2]"
  for (;b;b=Next[b])
  {
    print tag[b],tag[old] >STDERR

    emit_edge=1

    if (tag[b]=="function_decl") {
		printf "label=\"%s %s %s(%s)\"\n"	\
	       "color=blue\n",
		modi[b],
		_return_type(type[b]),
		name[b],
		_params(args[b])
		gendot(body[b])
    } 
    else if (tag[b]=="label_stmt") {
	    #printf "%i [label=\"%s\"]\n",b,name[labl[b]]
	    emit_edge=0
    }
    else if (tag[b]=="goto_stmt") {
	if (!old) {
		printf "%i [shape=none label=\"goto %s\"]\n",b,dest[b]
		old=b
	}
	goto_dest=0
	for (i in labl) if (labl[i]==dest[b]) {
		goto_dest=i
		break
	}
	printf "%i -> %i [label=\"goto %s\"]\n",old,Next[goto_dest],name[dest[b]]
	emit_edge=0
    }
    else if (tag[b]=="compound_stmt") {
	old=gendot(body[b])
	emit_edge=0
    }
    else if (tag[b]=="scope_stmt") {
	if(modi[b] ~ /begn/) {
		print "subgraph cluster_" b " {\n"	\
		      "  label=\"" name[b] "\"\n"	\
		      "  color=blue"
		#printf "%i [shape=point label=\"{\"]\n",b
		emit_edge=0
	} else {
		for (i in exits) printf "%i -> %i\n",i,b
		printf "%i [shape=point label=\"}\"]\n",b
  		print "}"
	}

    }
    else if (tag[b]=="decl_stmt") {
	printf "%i [shape=box label=\"%s\"]\n",b,
	       _decl(decl[b])
    }
    else if (tag[b]=="if_stmt") {
	printf "%i [shape=triangle label=\"%s\"]\n",b,
	       pexpr(cond[b])
	if (Else[b]) {
		printf "%i -> %i [label=else]\n",b,Else[b]
		gendot(Else[b])
	}
	if (then[b]) {
		printf "%i -> %i [label=then]\n",b,then[b]
		gendot(then[b])
	}
    }
    else if (tag[b]=="expr_stmt") {
	printf "%i [shape=box label=\"%s\"]\n",b,
	       pexpr(expr[b])
    }
    else if (tag[b]=="return_stmt") {
	printf "%i [shape=box peripheries=2 style=filled label=\"%s\"]\n",b,
	       pexpr(expr[b])
    }
    else if (tag[b]=="switch_stmt") {
	if (old) printf "%i -> %i\n",old,b
	printf "%i [shape=house label=\"%s\"]\n",b,
	       pexpr(cond[b])
	switch_node=b
	old=gendot(body[b])
    }
    else if (tag[b]=="case_label") {
	if (low[b])
		lab= _const(substr(low[b],2))
	else
		lab="default";
	printf "%i -> %i [label=\"%s\"]", switch_node, Next[b], lab
	emit_edge=0   
	old=0
    }
    else if (tag[b]=="for_stmt") {
	if (old) printf "%i -> %i\n",old,init[b]
	printf "%i [shape=box label=\"%s\"]\n",init[b],
	       pexpr(init[b])
	printf "%i [shape=triangle label=\"%s\"]\n",cond[b],
	       pexpr(cond[b])
	printf "%i -> %i\n",init[b],cond[b]
	bb=gendot(body[b])
	printf "%i -> %i [label=true]\n",cond[b],bb
	#printf "%i -> %i [label=false]\n",cond[b],b
	printf "%i [shape=box label=\"%s\"]\n",expr[b],
	       pexpr(expr[b])
	printf "%i -> %i\n",bb,expr[b]
	printf "%i -> %i\n",expr[b],cond[b]
	emit_edge=0
	edge_label="false"
	old=cond[b]
    }
    else if (tag[b]=="do_stmt") {
	bb=gendot(body[b])
	if (old) printf "%i -> %i\n",old,bb
	printf "%i [shape=triangle label=\"%s\"]\n",cond[b],
	       pexpr(cond[b])
	printf "%i -> %i\n",bb,cond[b]
	printf "%i -> %i [label=true]\n",cond[b],bb
	emit_edge=0
	edge_label="false"
	old=cond[b]
    }
    else if (tag[b]=="while_stmt") {
	if (old) printf "%i -> %i\n",old,cond[b]
	printf "%i [shape=triangle label=\"%s\"]\n",cond[b],
	       pexpr(cond[b])
	bb=gendot(body[b])
	printf "%i -> %i [label=true]\n",cond[b],bb
	printf "%i -> %i\n",bb,cond[b]
	emit_edge=0
	edge_label="false"
	old=cond[b]
    }
    else if (tag[b]=="continue_stmt") {
	#printf "%i [shape=none label=\"continue\"]\n",b
	exits[old]=1
	emit_edge=0
    }
    else if (tag[b]=="break_stmt") {
	#printf "%i [shape=none label=\"break\"]\n",b
	exits[old]=1
	emit_edge=0
    }
    if (emit_edge) {
	if (old) {
	    printf "%i -> %i",old,b
	    if (edge_label) {
		printf "[label=\"%s\"]",edge_label
		edge_label=""
	    }
	    printf "\n"
	}
   	old=b
        print "node [peripheries=1]"
    }
  }
  return old
}