+void BuildFlowSegment(pCodeFlow *pcflow)
+{
+ static int recursion=0;
+ pCodeFlow *pcflow_other;
+ set *flowset;
+
+ if(!pcflow)
+ return;
+
+ if(recursion++ > 200) {
+ fprintf(stderr, " exceeded recursion\n");
+ return;
+ }
+
+ fprintf(stderr,"examining 0x%x\n",pcflow->pc.seq);
+
+ if(pcflow->from) {
+
+
+ flowset = pcflow->from;
+
+ if(flowset && flowset->next == NULL) {
+
+ /*
+ There is a flow before this one. In fact, there's
+ exactly one flow before this one (because ->next is
+ NULL). That means all children of this node pass
+ through both this node and the node immediately
+ before this one; i.e. they have the same ancestor.
+ */
+
+ if( (NULL == (pcflow_other = PCFLINK(flowset->item)->pcflow)) ||
+ (NULL == pcflow_other)) {
+ fprintf(stderr,"2 error in flow link\n");
+ exit(1);
+ }
+ pcflow->ancestor = pcflow_other->ancestor ;
+
+ fprintf(stderr,"Assigning ancestor 0x%x from flow 0x%x\n",
+ pcflow->ancestor->pc.seq, pcflow_other->pc.seq);
+
+ } else {
+
+ if(flowset == NULL) {
+
+ /* There are no flows before this one.
+ * If this is not the first flow object in the pBlock then
+ * there's an error */
+
+ if(!pcflow->ancestor) {
+ fprintf(stderr,"error in flow link\n");
+ exit(1);
+
+ }
+
+ } else {
+
+ /* Flow passes to this flow from multiple flows. Let's
+ look at just one of these. If the one we look at has
+ an ancestor, then that's our ancestor too. If the one
+ we look at doesn't have an ancestor, then that means
+ we haven't traversed that branch of the call tree
+ yet - but we will */
+
+ pcflow_other = PCFLINK(flowset->item)->pcflow;
+ if(pcflow_other) {
+ fprintf(stderr, "coming from 0x%x\n",pcflow_other->pc.seq);
+ if( pcflow_other->ancestor)
+ pcflow->ancestor = pcflow_other->ancestor;
+ }
+ }
+
+
+ }
+
+ } else {
+ /* there are no nodes before this one */
+ if(!pcflow->ancestor)
+ fprintf(stderr,"3 Error in flow link\n");
+ }
+
+ /* Now let's recursively expand the call tree */
+
+ if(pcflow->ancestor && pcflow->to) {
+ flowset = pcflow->to;
+ while(flowset) {
+ BuildFlowSegment(PCFLINK(flowset->item)->pcflow);
+ flowset = flowset->next;
+ }
+ }
+
+}