summaryrefslogtreecommitdiff
path: root/src/pic16/pcodeflow.c
blob: fc8ec86c182a2e9d7e11a3e327a3ec72f46cadda (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/*-------------------------------------------------------------------------

  pcodeflow.c - post code generation flow analysis

   Written By -  Scott Dattalo scott@dattalo.com
   Ported to PIC16 By -  Martin Dubuc m.dubuc@rogers.com

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version.
   
   This program 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 this program; if not, write to the Free Software
   Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
   
-------------------------------------------------------------------------*/
/*
  pcodeflow.c

  The purpose of the code in this file is to analyze the flow of the
  pCode.

*/

#include <stdio.h>

#include "common.h"   // Include everything in the SDCC src directory
#include "newalloc.h"
#include "ralloc.h"
#include "device.h"
#include "pcode.h"
#include "pcoderegs.h"
#include "pcodeflow.h"

#if 0

/* 
   In the section that follows, an exhaustive flow "tree" is built.
   It's not a tree that's really built, but instead every possible
   flow path is constructed. 

   This is really overkill...
*/

static set *FlowTree=NULL;

void dbg_dumpFlowTree(set *FlowTree)
{
  set *segment;
  pCodeFlow *pcflow;

  fprintf(stderr,"Flow Tree: \n");

  for(segment = setFirstItem(FlowTree); segment; segment=setNextItem(FlowTree)) {

    fprintf(stderr,"Segment:\n");

    for(pcflow=PCFL(setFirstItem(segment)); pcflow; pcflow=PCFL(setNextItem(segment))) {
      fprintf(stderr, "  0x%03x",pcflow->pc.seq);
    }
    fprintf(stderr,"\n");
  }

}




/*-----------------------------------------------------------------*
 * void BuildFlowSegment(set *segment, pCodeFlow *pcflow)
 *-----------------------------------------------------------------*/
static void BuildFlowSegment(set *segment, pCodeFlow *pcflow)
{

  int nNextFlow=0;
  pCodeFlowLink *pcflowLink=NULL;

  /* Add this flow to the set if it's not in there already */
  if(isinSet(segment, pcflow)) {
    addSetHead(&FlowTree, segment);
    return;
  }

  addSetHead(&segment, pcflow);

  /* Continue to add contiguous flows */

  while( pcflow->to && ((nNextFlow = elementsInSet(pcflow->to)) == 1)) {
    pcflowLink = (pCodeFlowLink *)(setFirstItem(pcflow->to));
    pcflow = pcflowLink->pcflow;

    if(isinSet(segment, pcflow)) {
      addSetIfnotP(&FlowTree, segment);
      return;
    }

    addSetIfnotP(&segment, pcflow);

  }

  /* Branch: for each branch, duplicate the set and recursively call */
  if(pcflow->to && nNextFlow>=2) {
    pCodeFlow *pcflow_to;

    set *branch_segment=NULL;
    
    pcflowLink = (pCodeFlowLink *)(setFirstItem(pcflow->to));
    pcflow_to = pcflowLink->pcflow;

    addSetIfnotP(&segment, pcflow);

    pcflowLink = (pCodeFlowLink *)(setNextItem(pcflow->to));

    while(pcflowLink) {

      branch_segment = setFromSet(segment);
      BuildFlowSegment(setFromSet(segment),pcflowLink->pcflow);
      pcflowLink = (pCodeFlowLink *)(setNextItem(pcflow->to));
    }

    /* add the first branch to this segment */
    BuildFlowSegment(segment,pcflow_to);

  }

  addSetIfnotP(&FlowTree, segment);

  /* done */
}

/*-----------------------------------------------------------------*
 * void pic16_BuildFlowTree(pBlock *pb)
 *-----------------------------------------------------------------*/
void pic16_BuildFlowTree(pBlock *pb)
{
  pCodeFlow *pcflow;
  set *segment;

  FlowTree = newSet();

  /* Start with the first flow object of this pBlock */

  pcflow = PCFL(pic16_findNextpCode(pb->pcHead, PC_FLOW));

  segment = newSet();
  BuildFlowSegment(segment, pcflow);

  pb->FlowTree = FlowTree;

  dbg_dumpFlowTree(FlowTree);
}

static void dbg_dumpFlow(pBlock *pb)
{
  pCode *pcflow;

  for( pcflow = pic16_findNextpCode(pb->pcHead, PC_FLOW); 
       pcflow != NULL;
       pcflow = pic16_findNextpCode(pcflow->next, PC_FLOW) ) {

    if(!isPCFL(pcflow))
      fprintf(stderr, "LinkFlow - pcflow is not a flow object ");

    fprintf(stderr, "Flow: 0x%x",pcflow->seq);
    if(PCFL(pcflow) && PCFL(pcflow)->ancestor)
      fprintf(stderr,", ancestor 0x%x\n",
	      PCFL(pcflow)->ancestor->pc.seq);
    else {
      pCodeFlowLink *from = (PCFL(pcflow)->from) ? (PCFLINK(setFirstItem(PCFL(pcflow)->from))) : NULL;
      fprintf(stderr," no ancestor");
      while(from) {
	fprintf(stderr," (0x%x)",from->pcflow->pc.seq);
	from = setNextItem(PCFL(pcflow)->from);
      }
      fprintf(stderr,"\n");
    }
  }
}
#endif

#if 0
/*-----------------------------------------------------------------*
 * void BuildFlowSegment(set *segment, pCodeFlow *pcflow)
 *-----------------------------------------------------------------*/
static 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;
    }
  }

}
#endif

void pic16_BuildFlowTree(pBlock *pb)
{
  pCodeFlow *first_pcflow, *pcflow;


  //  fprintf(stderr,"pic16_BuildFlowTree \n");

  first_pcflow = PCFL(pic16_findNextpCode(pb->pcHead, PC_FLOW));
  if(!first_pcflow)
    return;

  /* The very first node is like Adam, it's its own ancestor (i.e. 
   * there are no other flows in this pBlock prior to the first one).
   */

  first_pcflow->ancestor = first_pcflow;

  /* For each flow that has only one predecessor, it's easy to 
     identify the ancestor */
  pcflow = PCFL(pic16_findNextpCode(first_pcflow->pc.next, PC_FLOW));

  while(pcflow) {
    if(elementsInSet(pcflow->from) == 1) {
      pCodeFlowLink *from = PCFLINK(setFirstItem(pcflow->from));

      pcflow->ancestor = from->pcflow;
      /*
	fprintf(stderr,"Assigning ancestor 0x%x to flow 0x%x\n",
	pcflow->ancestor->pc.seq, pcflow->pc.seq);
      */
    }

    pcflow = PCFL(pic16_findNextpCode(pcflow->pc.next, PC_FLOW));

  }

  pcflow = PCFL(pic16_findNextpCode(first_pcflow->pc.next, PC_FLOW));

  while(pcflow) {
    if(elementsInSet(pcflow->from) > 1) {
      pCodeFlow *min_pcflow;
      pCodeFlowLink *from = PCFLINK(setFirstItem(pcflow->from));

      min_pcflow = from->pcflow;

      while( (from = setNextItem(pcflow->from)) != NULL) {
	if(from->pcflow->pc.seq < min_pcflow->pc.seq)
	  min_pcflow = from->pcflow;
      }

      pcflow->ancestor = min_pcflow;
      /*
	fprintf(stderr,"Assigning ancestor 0x%x to flow 0x%x from multiple\n",
	pcflow->ancestor->pc.seq, pcflow->pc.seq);
      */

    }

    pcflow = PCFL(pic16_findNextpCode(pcflow->pc.next, PC_FLOW));

  }
  
  //  BuildFlowSegment(pcflow);

  //dbg_dumpFlow(pb);
  
}