* .version: changed to version 2.5.3
[fw/sdcc] / src / mcs51 / ralloc.c
1 /*------------------------------------------------------------------------
2
3   SDCCralloc.c - source file for register allocation. (8051) specific
4
5                 Written By -  Sandeep Dutta . sandeep.dutta@usa.net (1998)
6
7    This program is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by the
9    Free Software Foundation; either version 2, or (at your option) any
10    later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20
21    In other words, you are welcome to use, share and improve this program.
22    You are forbidden to forbid anyone else to use, share and improve
23    what you give them.   Help stamp out software-hoarding!
24 -------------------------------------------------------------------------*/
25
26 #include "common.h"
27 #include "ralloc.h"
28 #include "gen.h"
29
30 /*-----------------------------------------------------------------*/
31 /* At this point we start getting processor specific although      */
32 /* some routines are non-processor specific & can be reused when   */
33 /* targetting other processors. The decision for this will have    */
34 /* to be made on a routine by routine basis                        */
35 /* routines used to pack registers are most definitely not reusable */
36 /* since the pack the registers depending strictly on the MCU      */
37 /*-----------------------------------------------------------------*/
38
39 extern void gen51Code (iCode *);
40 #define D(x)
41
42 /* Global data */
43 static struct
44   {
45     bitVect *spiltSet;
46     set *stackSpil;
47     bitVect *regAssigned;
48     bitVect *totRegAssigned;    /* final set of LRs that got into registers */
49     short blockSpil;
50     int slocNum;
51     bitVect *funcrUsed;         /* registers used in a function */
52     int stackExtend;
53     int dataExtend;
54     bitVect *allBitregs;        /* all bit registers */
55   }
56 _G;
57
58 /* Shared with gen.c */
59 int mcs51_ptrRegReq;            /* one byte pointer register required */
60
61 /* 8051 registers */
62 regs regs8051[] =
63 {
64
65   {REG_GPR, R2_IDX,  REG_GPR, "r2",  "ar2", "0",    2, 1},
66   {REG_GPR, R3_IDX,  REG_GPR, "r3",  "ar3", "0",    3, 1},
67   {REG_GPR, R4_IDX,  REG_GPR, "r4",  "ar4", "0",    4, 1},
68   {REG_GPR, R5_IDX,  REG_GPR, "r5",  "ar5", "0",    5, 1},
69   {REG_GPR, R6_IDX,  REG_GPR, "r6",  "ar6", "0",    6, 1},
70   {REG_GPR, R7_IDX,  REG_GPR, "r7",  "ar7", "0",    7, 1},
71   {REG_PTR, R0_IDX,  REG_PTR, "r0",  "ar0", "0",    0, 1},
72   {REG_PTR, R1_IDX,  REG_PTR, "r1",  "ar1", "0",    1, 1},
73   {REG_BIT, B0_IDX,  REG_BIT, "b0",  "b0",  "bits", 0, 1},
74   {REG_BIT, B1_IDX,  REG_BIT, "b1",  "b1",  "bits", 1, 1},
75   {REG_BIT, B2_IDX,  REG_BIT, "b2",  "b2",  "bits", 2, 1},
76   {REG_BIT, B3_IDX,  REG_BIT, "b3",  "b3",  "bits", 3, 1},
77   {REG_BIT, B4_IDX,  REG_BIT, "b4",  "b4",  "bits", 4, 1},
78   {REG_BIT, B5_IDX,  REG_BIT, "b5",  "b5",  "bits", 5, 1},
79   {REG_BIT, B6_IDX,  REG_BIT, "b6",  "b6",  "bits", 6, 1},
80   {REG_BIT, B7_IDX,  REG_BIT, "b7",  "b7",  "bits", 7, 1},
81   {REG_GPR, X8_IDX,  REG_GPR, "x8",  "x8",  "xreg", 0, 1},
82   {REG_GPR, X9_IDX,  REG_GPR, "x9",  "x9",  "xreg", 1, 1},
83   {REG_GPR, X10_IDX, REG_GPR, "x10", "x10", "xreg", 2, 1},
84   {REG_GPR, X11_IDX, REG_GPR, "x11", "x11", "xreg", 3, 1},
85   {REG_GPR, X12_IDX, REG_GPR, "x12", "x12", "xreg", 4, 1},
86   {REG_CND, CND_IDX, REG_CND, "C",   "psw", "0xd0", 0, 1},
87   {0,       DPL_IDX, 0,       "dpl", "dpl", "0x82", 0, 0},
88   {0,       DPH_IDX, 0,       "dph", "dph", "0x83", 0, 0},
89   {0,       B_IDX,   0,       "b",   "b",   "0xf0", 0, 0},
90   {0,       A_IDX,   0,       "a",   "acc", "0xe0", 0, 0},
91 };
92 int mcs51_nRegs = 16;
93 static void spillThis (symbol *);
94 static void freeAllRegs ();
95
96 /*-----------------------------------------------------------------*/
97 /* allocReg - allocates register of given type                     */
98 /*-----------------------------------------------------------------*/
99 static regs *
100 allocReg (short type)
101 {
102   int i;
103
104   for (i = 0; i < mcs51_nRegs; i++)
105     {
106
107       /* if type is given as 0 then any
108          free register will do */
109       if (!type &&
110           regs8051[i].isFree)
111         {
112           regs8051[i].isFree = 0;
113           if (currFunc)
114             currFunc->regsUsed =
115               bitVectSetBit (currFunc->regsUsed, i);
116           return &regs8051[i];
117         }
118       /* other wise look for specific type
119          of register */
120       if (regs8051[i].isFree &&
121           regs8051[i].type == type)
122         {
123           regs8051[i].isFree = 0;
124           if (currFunc)
125             currFunc->regsUsed =
126               bitVectSetBit (currFunc->regsUsed, i);
127           return &regs8051[i];
128         }
129     }
130   return NULL;
131 }
132
133 /*-----------------------------------------------------------------*/
134 /* allocThisReg - allocates a particular register (if free)        */
135 /*-----------------------------------------------------------------*/
136 static regs *
137 allocThisReg (regs * reg)
138 {
139   if (!reg->isFree)
140     return NULL;
141
142   reg->isFree = 0;
143   if (currFunc)
144     currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, reg->rIdx);
145
146   return reg;
147 }
148
149
150 /*-----------------------------------------------------------------*/
151 /* mcs51_regWithIdx - returns pointer to register with index number*/
152 /*-----------------------------------------------------------------*/
153 regs *
154 mcs51_regWithIdx (int idx)
155 {
156   int i;
157
158   for (i = 0; i < sizeof(regs8051)/sizeof(regs); i++)
159     if (regs8051[i].rIdx == idx)
160       return &regs8051[i];
161
162   werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
163           "regWithIdx not found");
164   exit (1);
165 }
166
167 /*-----------------------------------------------------------------*/
168 /* freeReg - frees a register                                      */
169 /*-----------------------------------------------------------------*/
170 static void
171 freeReg (regs * reg)
172 {
173   if (!reg)
174     {
175       werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
176               "freeReg - Freeing NULL register");
177       exit (1);
178     }
179
180   reg->isFree = 1;
181 }
182
183
184 /*-----------------------------------------------------------------*/
185 /* nFreeRegs - returns number of free registers                    */
186 /*-----------------------------------------------------------------*/
187 static int
188 nFreeRegs (int type)
189 {
190   int i;
191   int nfr = 0;
192
193   for (i = 0; i < mcs51_nRegs; i++)
194     if (regs8051[i].isFree && regs8051[i].type == type)
195       nfr++;
196   return nfr;
197 }
198
199 /*-----------------------------------------------------------------*/
200 /* nfreeRegsType - free registers with type                         */
201 /*-----------------------------------------------------------------*/
202 static int
203 nfreeRegsType (int type)
204 {
205   int nfr;
206   if (type == REG_PTR)
207     {
208       if ((nfr = nFreeRegs (type)) == 0)
209         return nFreeRegs (REG_GPR);
210     }
211
212   return nFreeRegs (type);
213 }
214
215 /*-----------------------------------------------------------------*/
216 /* useReg - marks a register  as used                              */
217 /*-----------------------------------------------------------------*/
218 static void
219 useReg (regs * reg)
220 {
221   reg->isFree = 0;
222 }
223
224 /*-----------------------------------------------------------------*/
225 /* computeSpillable - given a point find the spillable live ranges */
226 /*-----------------------------------------------------------------*/
227 static bitVect *
228 computeSpillable (iCode * ic)
229 {
230   bitVect *spillable;
231
232   /* spillable live ranges are those that are live at this
233      point . the following categories need to be subtracted
234      from this set.
235      a) - those that are already spilt
236      b) - if being used by this one
237      c) - defined by this one */
238
239   spillable = bitVectCopy (ic->rlive);
240   spillable =
241     bitVectCplAnd (spillable, _G.spiltSet);     /* those already spilt */
242   spillable =
243     bitVectCplAnd (spillable, ic->uses);        /* used in this one */
244   bitVectUnSetBit (spillable, ic->defKey);
245   spillable = bitVectIntersect (spillable, _G.regAssigned);
246   return spillable;
247
248 }
249
250 /*-----------------------------------------------------------------*/
251 /* noSpilLoc - return true if a variable has no spil location      */
252 /*-----------------------------------------------------------------*/
253 static int
254 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
255 {
256   return (sym->usl.spillLoc ? 0 : 1);
257 }
258
259 /*-----------------------------------------------------------------*/
260 /* hasSpilLoc - will return 1 if the symbol has spil location      */
261 /*-----------------------------------------------------------------*/
262 static int
263 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
264 {
265   return (sym->usl.spillLoc ? 1 : 0);
266 }
267
268 /*-----------------------------------------------------------------*/
269 /* directSpilLoc - will return 1 if the splilocation is in direct  */
270 /*-----------------------------------------------------------------*/
271 static int
272 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
273 {
274   if (sym->usl.spillLoc &&
275       (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
276     return 1;
277   else
278     return 0;
279 }
280
281 /*-----------------------------------------------------------------*/
282 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
283 /*                    but is not used as a pointer                 */
284 /*-----------------------------------------------------------------*/
285 static int
286 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
287 {
288   return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
289 }
290
291 /*-----------------------------------------------------------------*/
292 /* rematable - will return 1 if the remat flag is set              */
293 /*-----------------------------------------------------------------*/
294 static int
295 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
296 {
297   return sym->remat;
298 }
299
300 /*-----------------------------------------------------------------*/
301 /* notUsedInRemaining - not used or defined in remain of the block */
302 /*-----------------------------------------------------------------*/
303 static int
304 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
305 {
306   return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
307           allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
308 }
309
310 /*-----------------------------------------------------------------*/
311 /* allLRs - return true for all                                    */
312 /*-----------------------------------------------------------------*/
313 static int
314 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
315 {
316   return 1;
317 }
318
319 /*-----------------------------------------------------------------*/
320 /* liveRangesWith - applies function to a given set of live range  */
321 /*-----------------------------------------------------------------*/
322 static set *
323 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
324                 eBBlock * ebp, iCode * ic)
325 {
326   set *rset = NULL;
327   int i;
328
329   if (!lrs || !lrs->size)
330     return NULL;
331
332   for (i = 1; i < lrs->size; i++)
333     {
334       symbol *sym;
335       if (!bitVectBitValue (lrs, i))
336         continue;
337
338       /* if we don't find it in the live range
339          hash table we are in serious trouble */
340       if (!(sym = hTabItemWithKey (liveRanges, i)))
341         {
342           werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
343                   "liveRangesWith could not find liveRange");
344           exit (1);
345         }
346
347       if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
348         addSetHead (&rset, sym);
349     }
350
351   return rset;
352 }
353
354
355 /*-----------------------------------------------------------------*/
356 /* leastUsedLR - given a set determines which is the least used    */
357 /*-----------------------------------------------------------------*/
358 static symbol *
359 leastUsedLR (set * sset)
360 {
361   symbol *sym = NULL, *lsym = NULL;
362
363   sym = lsym = setFirstItem (sset);
364
365   if (!lsym)
366     return NULL;
367
368   for (; lsym; lsym = setNextItem (sset))
369     {
370
371       /* if usage is the same then prefer
372          the spill the smaller of the two */
373       if (lsym->used == sym->used)
374         if (getSize (lsym->type) < getSize (sym->type))
375           sym = lsym;
376
377       /* if less usage */
378       if (lsym->used < sym->used)
379         sym = lsym;
380
381     }
382
383   setToNull ((void *) &sset);
384   sym->blockSpil = 0;
385   return sym;
386 }
387
388 /*-----------------------------------------------------------------*/
389 /* noOverLap - will iterate through the list looking for over lap  */
390 /*-----------------------------------------------------------------*/
391 static int
392 noOverLap (set * itmpStack, symbol * fsym)
393 {
394   symbol *sym;
395
396   for (sym = setFirstItem (itmpStack); sym;
397        sym = setNextItem (itmpStack))
398     {
399         if (bitVectBitValue(sym->clashes,fsym->key)) return 0;
400     }
401   return 1;
402 }
403
404 /*-----------------------------------------------------------------*/
405 /* isFree - will return 1 if the a free spil location is found     */
406 /*-----------------------------------------------------------------*/
407 static
408 DEFSETFUNC (isFree)
409 {
410   symbol *sym = item;
411   V_ARG (symbol **, sloc);
412   V_ARG (symbol *, fsym);
413
414   /* if already found */
415   if (*sloc)
416     return 0;
417
418   /* if it is free && and the itmp assigned to
419      this does not have any overlapping live ranges
420      with the one currently being assigned and
421      the size can be accomodated  */
422   if (sym->isFree &&
423       noOverLap (sym->usl.itmpStack, fsym) &&
424       getSize (sym->type) >= getSize (fsym->type))
425     {
426       *sloc = sym;
427       return 1;
428     }
429
430   return 0;
431 }
432
433 /*-----------------------------------------------------------------*/
434 /* spillLRWithPtrReg :- will spil those live ranges which use PTR  */
435 /*-----------------------------------------------------------------*/
436 static void
437 spillLRWithPtrReg (symbol * forSym)
438 {
439   symbol *lrsym;
440   regs *r0, *r1;
441   int k;
442
443   if (!_G.regAssigned ||
444       bitVectIsZero (_G.regAssigned))
445     return;
446
447   r0 = mcs51_regWithIdx (R0_IDX);
448   r1 = mcs51_regWithIdx (R1_IDX);
449
450   /* for all live ranges */
451   for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
452        lrsym = hTabNextItem (liveRanges, &k))
453     {
454       int j;
455
456       /* if no registers assigned to it or spilt */
457       /* if it does not overlap this then
458          no need to spill it */
459
460       if (lrsym->isspilt || !lrsym->nRegs ||
461           (lrsym->liveTo < forSym->liveFrom))
462         continue;
463
464       /* go thru the registers : if it is either
465          r0 or r1 then spill it */
466       for (j = 0; j < lrsym->nRegs; j++)
467         if (lrsym->regs[j] == r0 ||
468             lrsym->regs[j] == r1)
469           {
470             spillThis (lrsym);
471             break;
472           }
473     }
474
475 }
476
477 /*-----------------------------------------------------------------*/
478 /* createStackSpil - create a location on the stack to spil        */
479 /*-----------------------------------------------------------------*/
480 static symbol *
481 createStackSpil (symbol * sym)
482 {
483   symbol *sloc = NULL;
484   int useXstack, model;
485
486   char slocBuffer[30];
487
488   /* first go try and find a free one that is already
489      existing on the stack */
490   if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
491     {
492       /* found a free one : just update & return */
493       sym->usl.spillLoc = sloc;
494       sym->stackSpil = 1;
495       sloc->isFree = 0;
496       addSetHead (&sloc->usl.itmpStack, sym);
497       return sym;
498     }
499
500   /* could not then have to create one , this is the hard part
501      we need to allocate this on the stack : this is really a
502      hack!! but cannot think of anything better at this time */
503
504   if (SNPRINTF (slocBuffer, sizeof(slocBuffer),
505                 "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
506     {
507       fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
508                __FILE__, __LINE__);
509       exit (1);
510     }
511
512   sloc = newiTemp (slocBuffer);
513
514   /* set the type to the spilling symbol */
515   sloc->type = copyLinkChain (sym->type);
516   sloc->etype = getSpec (sloc->type);
517   if (!IS_BIT (sloc->etype))
518     {
519       SPEC_SCLS (sloc->etype) = S_DATA;
520     }
521   SPEC_EXTR (sloc->etype) = 0;
522   SPEC_STAT (sloc->etype) = 0;
523   SPEC_VOLATILE(sloc->etype) = 0;
524   SPEC_ABSA(sloc->etype) = 0;
525
526   /* we don't allow it to be allocated`
527      onto the external stack since : so we
528      temporarily turn it off ; we also
529      turn off memory model to prevent
530      the spil from going to the external storage
531    */
532
533   useXstack = options.useXstack;
534   model = options.model;
535 /*     noOverlay = options.noOverlay; */
536 /*     options.noOverlay = 1; */
537   options.model = options.useXstack = 0;
538
539   allocLocal (sloc);
540
541   options.useXstack = useXstack;
542   options.model = model;
543 /*     options.noOverlay = noOverlay; */
544   sloc->isref = 1;              /* to prevent compiler warning */
545
546   /* if it is on the stack then update the stack */
547   if (IN_STACK (sloc->etype))
548     {
549       currFunc->stack += getSize (sloc->type);
550       _G.stackExtend += getSize (sloc->type);
551     }
552   else
553     _G.dataExtend += getSize (sloc->type);
554
555   /* add it to the _G.stackSpil set */
556   addSetHead (&_G.stackSpil, sloc);
557   sym->usl.spillLoc = sloc;
558   sym->stackSpil = 1;
559
560   /* add it to the set of itempStack set
561      of the spill location */
562   addSetHead (&sloc->usl.itmpStack, sym);
563   return sym;
564 }
565
566 /*-----------------------------------------------------------------*/
567 /* isSpiltOnStack - returns true if the spil location is on stack  */
568 /*-----------------------------------------------------------------*/
569 static bool
570 isSpiltOnStack (symbol * sym)
571 {
572   sym_link *etype;
573
574   if (!sym)
575     return FALSE;
576
577   if (!sym->isspilt)
578     return FALSE;
579
580 /*     if (sym->_G.stackSpil) */
581 /*      return TRUE; */
582
583   if (!sym->usl.spillLoc)
584     return FALSE;
585
586   etype = getSpec (sym->usl.spillLoc->type);
587   if (IN_STACK (etype))
588     return TRUE;
589
590   return FALSE;
591 }
592
593 /*-----------------------------------------------------------------*/
594 /* spillThis - spils a specific operand                            */
595 /*-----------------------------------------------------------------*/
596 static void
597 spillThis (symbol * sym)
598 {
599   int i;
600   /* if this is rematerializable or has a spillLocation
601      we are okay, else we need to create a spillLocation
602      for it */
603   if (!(sym->remat || sym->usl.spillLoc))
604     createStackSpil (sym);
605
606   /* mark it has spilt & put it in the spilt set */
607   sym->isspilt = sym->spillA = 1;
608   _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
609
610   bitVectUnSetBit (_G.regAssigned, sym->key);
611   bitVectUnSetBit (_G.totRegAssigned, sym->key);
612
613   for (i = 0; i < sym->nRegs; i++)
614
615     if (sym->regs[i])
616       {
617         freeReg (sym->regs[i]);
618         sym->regs[i] = NULL;
619       }
620
621   /* if spilt on stack then free up r0 & r1
622      if they could have been assigned to some
623      LIVE ranges */
624   if (!mcs51_ptrRegReq && isSpiltOnStack (sym))
625     {
626       mcs51_ptrRegReq++;
627       spillLRWithPtrReg (sym);
628     }
629
630   if (sym->usl.spillLoc && !sym->remat)
631     sym->usl.spillLoc->allocreq++;
632   return;
633 }
634
635 /*-----------------------------------------------------------------*/
636 /* selectSpil - select a iTemp to spil : rather a simple procedure */
637 /*-----------------------------------------------------------------*/
638 static symbol *
639 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
640 {
641   bitVect *lrcs = NULL;
642   set *selectS;
643   symbol *sym;
644
645   /* get the spillable live ranges */
646   lrcs = computeSpillable (ic);
647
648   /* get all live ranges that are rematerizable */
649   if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
650     {
651
652       /* return the least used of these */
653       return leastUsedLR (selectS);
654     }
655
656   /* get live ranges with spillLocations in direct space */
657   if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
658     {
659       sym = leastUsedLR (selectS);
660       strncpyz (sym->rname,
661                 sym->usl.spillLoc->rname[0] ?
662                    sym->usl.spillLoc->rname : sym->usl.spillLoc->name,
663                 sizeof(sym->rname));
664       sym->spildir = 1;
665       /* mark it as allocation required */
666       sym->usl.spillLoc->allocreq++;
667       return sym;
668     }
669
670   /* if the symbol is local to the block then */
671   if (forSym->liveTo < ebp->lSeq)
672     {
673
674       /* check if there are any live ranges allocated
675          to registers that are not used in this block */
676       if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
677         {
678           sym = leastUsedLR (selectS);
679           /* if this is not rematerializable */
680           if (!sym->remat)
681             {
682               _G.blockSpil++;
683               sym->blockSpil = 1;
684             }
685           return sym;
686         }
687
688       /* check if there are any live ranges that not
689          used in the remainder of the block */
690       if (!_G.blockSpil &&
691           !isiCodeInFunctionCall (ic) &&
692           (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
693         {
694           sym = leastUsedLR (selectS);
695           if (sym != forSym)
696             {
697               if (!sym->remat)
698                 {
699                   sym->remainSpil = 1;
700                   _G.blockSpil++;
701                 }
702               return sym;
703             }
704         }
705     }
706
707   /* find live ranges with spillocation && not used as pointers */
708   if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
709     {
710
711       sym = leastUsedLR (selectS);
712       /* mark this as allocation required */
713       sym->usl.spillLoc->allocreq++;
714       return sym;
715     }
716
717   /* find live ranges with spillocation */
718   if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
719     {
720
721       sym = leastUsedLR (selectS);
722       sym->usl.spillLoc->allocreq++;
723       return sym;
724     }
725
726   /* couldn't find then we need to create a spil
727      location on the stack , for which one? the least
728      used ofcourse */
729   if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
730     {
731
732       /* return a created spil location */
733       sym = createStackSpil (leastUsedLR (selectS));
734       sym->usl.spillLoc->allocreq++;
735       return sym;
736     }
737
738   /* this is an extreme situation we will spill
739      this one : happens very rarely but it does happen */
740   spillThis (forSym);
741   return forSym;
742
743 }
744
745 /*-----------------------------------------------------------------*/
746 /* spilSomething - spil some variable & mark registers as free     */
747 /*-----------------------------------------------------------------*/
748 static bool
749 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
750 {
751   symbol *ssym;
752   int i;
753
754   /* get something we can spil */
755   ssym = selectSpil (ic, ebp, forSym);
756
757   /* mark it as spilt */
758   ssym->isspilt = ssym->spillA = 1;
759   _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
760
761   /* mark it as not register assigned &
762      take it away from the set */
763   bitVectUnSetBit (_G.regAssigned, ssym->key);
764   bitVectUnSetBit (_G.totRegAssigned, ssym->key);
765
766   /* mark the registers as free */
767   for (i = 0; i < ssym->nRegs; i++)
768     if (ssym->regs[i])
769       freeReg (ssym->regs[i]);
770
771   /* if spilt on stack then free up r0 & r1
772      if they could have been assigned to as gprs */
773   if (!mcs51_ptrRegReq && isSpiltOnStack (ssym))
774     {
775       mcs51_ptrRegReq++;
776       spillLRWithPtrReg (ssym);
777     }
778
779   /* if this was a block level spil then insert push & pop
780      at the start & end of block respectively */
781   if (ssym->blockSpil)
782     {
783       iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
784       /* add push to the start of the block */
785       addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
786                                     ebp->sch->next : ebp->sch));
787       nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
788       /* add pop to the end of the block */
789       addiCodeToeBBlock (ebp, nic, NULL);
790     }
791
792   /* if spilt because not used in the remainder of the
793      block then add a push before this instruction and
794      a pop at the end of the block */
795   if (ssym->remainSpil)
796     {
797
798       iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
799       /* add push just before this instruction */
800       addiCodeToeBBlock (ebp, nic, ic);
801
802       nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
803       /* add pop to the end of the block */
804       addiCodeToeBBlock (ebp, nic, NULL);
805     }
806
807   if (ssym == forSym)
808     return FALSE;
809   else
810     return TRUE;
811 }
812
813 /*-----------------------------------------------------------------*/
814 /* getRegPtr - will try for PTR if not a GPR type if not spil      */
815 /*-----------------------------------------------------------------*/
816 static regs *
817 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
818 {
819   regs *reg;
820   int j;
821
822 tryAgain:
823   /* try for a ptr type */
824   if ((reg = allocReg (REG_PTR)))
825     return reg;
826
827   /* try for gpr type */
828   if ((reg = allocReg (REG_GPR)))
829     return reg;
830
831   /* we have to spil */
832   if (!spilSomething (ic, ebp, sym))
833     return NULL;
834
835   /* make sure partially assigned registers aren't reused */
836   for (j=0; j<=sym->nRegs; j++)
837     if (sym->regs[j])
838       sym->regs[j]->isFree = 0;
839
840   /* this looks like an infinite loop but
841      in really selectSpil will abort  */
842   goto tryAgain;
843 }
844
845 /*-----------------------------------------------------------------*/
846 /* getRegGpr - will try for GPR if not spil                        */
847 /*-----------------------------------------------------------------*/
848 static regs *
849 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
850 {
851   regs *reg;
852   int j;
853
854 tryAgain:
855   /* try for gpr type */
856   if ((reg = allocReg (REG_GPR)))
857     return reg;
858
859   if (!mcs51_ptrRegReq)
860     if ((reg = allocReg (REG_PTR)))
861       return reg;
862
863   /* we have to spil */
864   if (!spilSomething (ic, ebp, sym))
865     return NULL;
866
867   /* make sure partially assigned registers aren't reused */
868   for (j=0; j<=sym->nRegs; j++)
869     if (sym->regs[j])
870       sym->regs[j]->isFree = 0;
871
872   /* this looks like an infinite loop but
873      in really selectSpil will abort  */
874   goto tryAgain;
875 }
876
877 /*-----------------------------------------------------------------*/
878 /* getRegBit - will try for Bit if not spill this                  */
879 /*-----------------------------------------------------------------*/
880 static regs *getRegBit (symbol * sym)
881 {
882   regs *reg;
883
884   /* try for a bit type */
885   if ((reg = allocReg (REG_BIT)))
886     return reg;
887
888   spillThis (sym);
889   return 0;
890 }
891
892 /*-----------------------------------------------------------------*/
893 /* getRegPtrNoSpil - get it cannot be spilt                        */
894 /*-----------------------------------------------------------------*/
895 static regs *getRegPtrNoSpil()
896 {
897   regs *reg;
898
899   /* try for a ptr type */
900   if ((reg = allocReg (REG_PTR)))
901     return reg;
902
903   /* try for gpr type */
904   if ((reg = allocReg (REG_GPR)))
905     return reg;
906
907   assert(0);
908
909   /* just to make the compiler happy */
910   return 0;
911 }
912
913 /*-----------------------------------------------------------------*/
914 /* getRegGprNoSpil - get it cannot be spilt                           */
915 /*-----------------------------------------------------------------*/
916 static regs *getRegGprNoSpil()
917 {
918
919   regs *reg;
920   if ((reg = allocReg (REG_GPR)))
921     return reg;
922
923   if (!mcs51_ptrRegReq)
924     if ((reg = allocReg (REG_PTR)))
925       return reg;
926
927   assert(0);
928
929   /* just to make the compiler happy */
930   return 0;
931 }
932
933 /*-----------------------------------------------------------------*/
934 /* getRegBitNoSpil - get it cannot be spilt                        */
935 /*-----------------------------------------------------------------*/
936 static regs *getRegBitNoSpil()
937 {
938   regs *reg;
939
940   /* try for a ptr type */
941   if ((reg = allocReg (REG_BIT)))
942     return reg;
943
944   /* try for gpr type */
945   if ((reg = allocReg (REG_GPR)))
946     return reg;
947
948   assert(0);
949
950   /* just to make the compiler happy */
951   return 0;
952 }
953
954 /*-----------------------------------------------------------------*/
955 /* symHasReg - symbol has a given register                         */
956 /*-----------------------------------------------------------------*/
957 static bool
958 symHasReg (symbol * sym, regs * reg)
959 {
960   int i;
961
962   for (i = 0; i < sym->nRegs; i++)
963     if (sym->regs[i] == reg)
964       return TRUE;
965
966   return FALSE;
967 }
968
969 /*-----------------------------------------------------------------*/
970 /* updateRegUsage -  update the registers in use at the start of   */
971 /*                   this icode                                    */
972 /*-----------------------------------------------------------------*/
973 static void
974 updateRegUsage (iCode * ic)
975 {
976   int reg;
977
978   for (reg=0; reg<mcs51_nRegs; reg++)
979     {
980       if (regs8051[reg].isFree)
981         {
982           ic->riu &= ~(1<<regs8051[reg].offset);
983         }
984       else
985         {
986           ic->riu |= (1<<regs8051[reg].offset);
987           BitBankUsed |= (reg >= 8);
988         }
989     }
990 }
991
992 /*-----------------------------------------------------------------*/
993 /* deassignLRs - check the live to and if they have registers & are */
994 /*               not spilt then free up the registers              */
995 /*-----------------------------------------------------------------*/
996 static void
997 deassignLRs (iCode * ic, eBBlock * ebp)
998 {
999   symbol *sym;
1000   int k;
1001   symbol *result;
1002
1003   for (sym = hTabFirstItem (liveRanges, &k); sym;
1004        sym = hTabNextItem (liveRanges, &k))
1005     {
1006
1007       symbol *psym = NULL;
1008       /* if it does not end here */
1009       if (sym->liveTo > ic->seq)
1010         continue;
1011
1012       /* if it was spilt on stack then we can
1013          mark the stack spil location as free */
1014       if (sym->isspilt)
1015         {
1016           if (sym->stackSpil)
1017             {
1018               sym->usl.spillLoc->isFree = 1;
1019               sym->stackSpil = 0;
1020             }
1021           continue;
1022         }
1023
1024       if (!bitVectBitValue (_G.regAssigned, sym->key))
1025         continue;
1026
1027       /* special case check if this is an IFX &
1028          the privious one was a pop and the
1029          previous one was not spilt then keep track
1030          of the symbol */
1031       if (ic->op == IFX && ic->prev &&
1032           ic->prev->op == IPOP &&
1033           !ic->prev->parmPush &&
1034           !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
1035         psym = OP_SYMBOL (IC_LEFT (ic->prev));
1036
1037       if (sym->nRegs)
1038         {
1039           int i = 0;
1040
1041           bitVectUnSetBit (_G.regAssigned, sym->key);
1042
1043           /* if the result of this one needs registers
1044              and does not have it then assign it right
1045              away */
1046           if (IC_RESULT (ic) &&
1047               !(SKIP_IC2 (ic) ||        /* not a special icode */
1048                 ic->op == JUMPTABLE ||
1049                 ic->op == IFX ||
1050                 ic->op == IPUSH ||
1051                 ic->op == IPOP ||
1052                 ic->op == RETURN ||
1053                 POINTER_SET (ic)) &&
1054               (result = OP_SYMBOL (IC_RESULT (ic))) &&  /* has a result */
1055               result->liveTo > ic->seq &&       /* and will live beyond this */
1056               result->liveTo <= ebp->lSeq &&    /* does not go beyond this block */
1057               result->liveFrom == ic->seq &&    /* does not start before here */
1058               result->regType == sym->regType &&        /* same register types */
1059               result->nRegs &&  /* which needs registers */
1060               !result->isspilt &&       /* and does not already have them */
1061               !result->remat &&
1062               !bitVectBitValue (_G.regAssigned, result->key) &&
1063           /* the number of free regs + number of regs in this LR
1064              can accomodate the what result Needs */
1065               ((nfreeRegsType (result->regType) +
1066                 sym->nRegs) >= result->nRegs)
1067             )
1068             {
1069
1070               for (i = 0; i < result->nRegs; i++)
1071                 if (i < sym->nRegs)
1072                   result->regs[i] = sym->regs[i];
1073                 else
1074                   result->regs[i] = getRegGpr (ic, ebp, result);
1075
1076               _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
1077               _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
1078
1079             }
1080
1081           /* free the remaining */
1082           for (; i < sym->nRegs; i++)
1083             {
1084               if (psym)
1085                 {
1086                   if (!symHasReg (psym, sym->regs[i]))
1087                     freeReg (sym->regs[i]);
1088                 }
1089               else
1090                 freeReg (sym->regs[i]);
1091             }
1092         }
1093     }
1094 }
1095
1096
1097 /*-----------------------------------------------------------------*/
1098 /* reassignLR - reassign this to registers                         */
1099 /*-----------------------------------------------------------------*/
1100 static void
1101 reassignLR (operand * op)
1102 {
1103   symbol *sym = OP_SYMBOL (op);
1104   int i;
1105
1106   /* not spilt any more */
1107   sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1108   bitVectUnSetBit (_G.spiltSet, sym->key);
1109
1110   _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1111   _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1112
1113   _G.blockSpil--;
1114
1115   for (i = 0; i < sym->nRegs; i++)
1116     sym->regs[i]->isFree = 0;
1117 }
1118
1119 /*-----------------------------------------------------------------*/
1120 /* willCauseSpill - determines if allocating will cause a spill    */
1121 /*-----------------------------------------------------------------*/
1122 static int
1123 willCauseSpill (int nr, int rt)
1124 {
1125   /* first check if there are any available registers
1126      of the type required */
1127   if (rt == REG_PTR)
1128     {
1129       /* special case for pointer type
1130          if pointer type not avlb then
1131          check for type gpr */
1132       if (nFreeRegs (rt) >= nr)
1133         return 0;
1134       if (nFreeRegs (REG_GPR) >= nr)
1135         return 0;
1136     }
1137   else if (rt == REG_BIT)
1138     {
1139       if (nFreeRegs (rt) >= nr)
1140         return 0;
1141     }
1142   else
1143     {
1144       if (mcs51_ptrRegReq)
1145         {
1146           if (nFreeRegs (rt) >= nr)
1147             return 0;
1148         }
1149       else
1150         {
1151           if (nFreeRegs (REG_PTR) +
1152               nFreeRegs (REG_GPR) >= nr)
1153             return 0;
1154         }
1155     }
1156
1157   /* it will cause a spil */
1158   return 1;
1159 }
1160
1161 /*-----------------------------------------------------------------*/
1162 /* positionRegs - the allocator can allocate same registers to res- */
1163 /* ult and operand, if this happens make sure they are in the same */
1164 /* position as the operand otherwise chaos results                 */
1165 /*-----------------------------------------------------------------*/
1166 static int
1167 positionRegs (symbol * result, symbol * opsym)
1168 {
1169   int count = min (result->nRegs, opsym->nRegs);
1170   int i, j = 0, shared = 0;
1171   int change = 0;
1172
1173   /* if the result has been spilt then cannot share */
1174   if (opsym->isspilt)
1175     return 0;
1176 again:
1177   shared = 0;
1178   /* first make sure that they actually share */
1179   for (i = 0; i < count; i++)
1180     {
1181       for (j = 0; j < count; j++)
1182         {
1183           if (result->regs[i] == opsym->regs[j] && i != j)
1184             {
1185               shared = 1;
1186               goto xchgPositions;
1187             }
1188         }
1189     }
1190 xchgPositions:
1191   if (shared)
1192     {
1193       regs *tmp = result->regs[i];
1194       result->regs[i] = result->regs[j];
1195       result->regs[j] = tmp;
1196       change ++;
1197       goto again;
1198     }
1199   return change;
1200 }
1201
1202 /*------------------------------------------------------------------*/
1203 /* verifyRegsAssigned - make sure an iTemp is properly initialized; */
1204 /* it should either have registers or have beed spilled. Otherwise, */
1205 /* there was an uninitialized variable, so just spill this to get   */
1206 /* the operand in a valid state.                                    */
1207 /*------------------------------------------------------------------*/
1208 static void
1209 verifyRegsAssigned (operand *op, iCode * ic)
1210 {
1211   symbol * sym;
1212
1213   if (!op) return;
1214   if (!IS_ITEMP (op)) return;
1215
1216   sym = OP_SYMBOL (op);
1217   if (sym->isspilt) return;
1218   if (!sym->nRegs) return;
1219   if (sym->regs[0]) return;
1220
1221   werrorfl (ic->filename, ic->lineno, W_LOCAL_NOINIT,
1222             sym->prereqv ? sym->prereqv->name : sym->name);
1223   spillThis (sym);
1224 }
1225
1226
1227 /*-----------------------------------------------------------------*/
1228 /* serialRegAssign - serially allocate registers to the variables  */
1229 /*-----------------------------------------------------------------*/
1230 static void
1231 serialRegAssign (eBBlock ** ebbs, int count)
1232 {
1233   int i;
1234
1235   /* for all blocks */
1236   for (i = 0; i < count; i++)
1237     {
1238
1239       iCode *ic;
1240
1241       if (ebbs[i]->noPath &&
1242           (ebbs[i]->entryLabel != entryLabel &&
1243            ebbs[i]->entryLabel != returnLabel))
1244         continue;
1245
1246       /* for all instructions do */
1247       for (ic = ebbs[i]->sch; ic; ic = ic->next)
1248         {
1249             updateRegUsage(ic);
1250
1251             /* if this is an ipop that means some live
1252                range will have to be assigned again */
1253             if (ic->op == IPOP)
1254                 reassignLR (IC_LEFT (ic));
1255
1256             /* if result is present && is a true symbol */
1257             if (IC_RESULT (ic) && ic->op != IFX &&
1258                 IS_TRUE_SYMOP (IC_RESULT (ic)))
1259                 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1260
1261             /* take away registers from live
1262                ranges that end at this instruction */
1263             deassignLRs (ic, ebbs[i]);
1264
1265             /* some don't need registers */
1266             if (SKIP_IC2 (ic) ||
1267                 ic->op == JUMPTABLE ||
1268                 ic->op == IFX ||
1269                 ic->op == IPUSH ||
1270                 ic->op == IPOP ||
1271                 (IC_RESULT (ic) && POINTER_SET (ic)))
1272                 continue;
1273
1274             /* now we need to allocate registers
1275                only for the result */
1276             if (IC_RESULT (ic)) {
1277                 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1278                 bitVect *spillable;
1279                 int willCS;
1280                 int j;
1281                 int ptrRegSet = 0;
1282
1283                 /* Make sure any spill location is definitely allocated */
1284                 if (sym->isspilt && !sym->remat && sym->usl.spillLoc &&
1285                     !sym->usl.spillLoc->allocreq)
1286                   {
1287                     sym->usl.spillLoc->allocreq++;
1288                   }
1289                   
1290                 /* if it does not need or is spilt
1291                    or is already assigned to registers
1292                    or will not live beyond this instructions */
1293                 if (!sym->nRegs ||
1294                     sym->isspilt ||
1295                     bitVectBitValue (_G.regAssigned, sym->key) ||
1296                     sym->liveTo <= ic->seq)
1297                     continue;
1298
1299                 /* if some liverange has been spilt at the block level
1300                    and this one live beyond this block then spil this
1301                    to be safe */
1302                 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq) {
1303                     spillThis (sym);
1304                     continue;
1305                 }
1306
1307                 willCS = willCauseSpill (sym->nRegs, sym->regType);
1308                 /* if this is a bit variable then don't use precious registers
1309                    along with expensive bit-to-char conversions but just spill
1310                    it */
1311                 if (willCS && SPEC_NOUN(sym->etype) == V_BIT) {
1312                     spillThis (sym);
1313                     continue;
1314                 }
1315
1316                 /* if trying to allocate this will cause
1317                    a spill and there is nothing to spill
1318                    or this one is rematerializable then
1319                    spill this one */
1320                 spillable = computeSpillable (ic);
1321                 if (sym->remat || (willCS && bitVectIsZero (spillable))) {
1322                     spillThis (sym);
1323                     continue;
1324                 }
1325
1326                 /* If the live range preceeds the point of definition
1327                    then ideally we must take into account registers that
1328                    have been allocated after sym->liveFrom but freed
1329                    before ic->seq. This is complicated, so spill this
1330                    symbol instead and let fillGaps handle the allocation. */
1331                 if (sym->liveFrom < ic->seq) {
1332                     spillThis (sym);
1333                     continue;
1334                 }
1335
1336                 /* if it has a spillocation & is used less than
1337                    all other live ranges then spill this */
1338                 if (willCS) {
1339                     if (sym->usl.spillLoc) {
1340                         symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1341                                                                          allLRs, ebbs[i], ic));
1342                         if (leastUsed && leastUsed->used > sym->used) {
1343                             spillThis (sym);
1344                             continue;
1345                         }
1346                     } else {
1347                         /* if none of the liveRanges have a spillLocation then better
1348                            to spill this one than anything else already assigned to registers */
1349                         if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1350                             /* if this is local to this block then we might find a block spil */
1351                             if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1352                                 spillThis (sym);
1353                                 continue;
1354                             }
1355                         }
1356                     }
1357                 }
1358                 /* if we need ptr regs for the right side
1359                    then mark it */
1360                 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1361                     && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned int) PTRSIZE) {
1362                     mcs51_ptrRegReq++;
1363                     ptrRegSet = 1;
1364                 }
1365                 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic))
1366                     && SPEC_OCLS(OP_SYMBOL (IC_LEFT (ic))->etype) == idata) {
1367                     mcs51_ptrRegReq++;
1368                     ptrRegSet = 1;
1369                 }
1370                 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic))
1371                     && SPEC_OCLS(OP_SYMBOL (IC_RIGHT (ic))->etype) == idata) {
1372                     mcs51_ptrRegReq++;
1373                     ptrRegSet = 1;
1374                 }
1375
1376                 /* else we assign registers to it */
1377                 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1378                 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1379
1380                 for (j = 0; j < sym->nRegs; j++) {
1381                     sym->regs[j] = NULL;
1382                     if (sym->regType == REG_PTR)
1383                         sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1384                     else if (sym->regType == REG_BIT)
1385                         sym->regs[j] = getRegBit (sym);
1386                     else
1387                       {
1388                         if (ic->op == CAST && IS_SYMOP (IC_RIGHT (ic)))
1389                           {
1390                             symbol * right = OP_SYMBOL (IC_RIGHT (ic));
1391
1392                             if (right->regs[j] && (right->regType != REG_BIT))
1393                               sym->regs[j] = allocThisReg (right->regs[j]);
1394                           }
1395                         if (!sym->regs[j])
1396                           sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1397                       }
1398
1399                     /* if the allocation failed which means
1400                        this was spilt then break */
1401                     if (!sym->regs[j])
1402                       {
1403                         int i;
1404                         for (i=0; i < sym->nRegs ; i++ )
1405                           sym->regs[i] = NULL;
1406                         break;
1407                       }
1408                 }
1409
1410                 if (!POINTER_SET(ic) && !POINTER_GET(ic)) {
1411                     /* if it shares registers with operands make sure
1412                        that they are in the same position */
1413                     if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1414                         OP_SYMBOL (IC_LEFT (ic))->nRegs) {
1415                         positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1416                                       OP_SYMBOL (IC_LEFT (ic)));
1417                     }
1418                     /* do the same for the right operand */
1419                     if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1420                         OP_SYMBOL (IC_RIGHT (ic))->nRegs) {
1421                         positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1422                                       OP_SYMBOL (IC_RIGHT (ic)));
1423                     }
1424                 }
1425
1426                 if (ptrRegSet) {
1427                     mcs51_ptrRegReq--;
1428                     ptrRegSet = 0;
1429                 }
1430
1431             }
1432         }
1433     }
1434
1435     /* Check for and fix any problems with uninitialized operands */
1436     for (i = 0; i < count; i++)
1437       {
1438         iCode *ic;
1439
1440         if (ebbs[i]->noPath &&
1441             (ebbs[i]->entryLabel != entryLabel &&
1442              ebbs[i]->entryLabel != returnLabel))
1443             continue;
1444
1445         for (ic = ebbs[i]->sch; ic; ic = ic->next)
1446           {
1447             if (SKIP_IC2 (ic))
1448               continue;
1449
1450             if (ic->op == IFX)
1451               {
1452                 verifyRegsAssigned (IC_COND (ic), ic);
1453                 continue;
1454               }
1455
1456             if (ic->op == JUMPTABLE)
1457               {
1458                 verifyRegsAssigned (IC_JTCOND (ic), ic);
1459                 continue;
1460               }
1461
1462             verifyRegsAssigned (IC_RESULT (ic), ic);
1463             verifyRegsAssigned (IC_LEFT (ic), ic);
1464             verifyRegsAssigned (IC_RIGHT (ic), ic);
1465           }
1466       }
1467 }
1468
1469 /*-----------------------------------------------------------------*/
1470 /* fillGaps - Try to fill in the Gaps left by Pass1                */
1471 /*-----------------------------------------------------------------*/
1472 static void fillGaps()
1473 {
1474     symbol *sym =NULL;
1475     int key =0;
1476     int pass;
1477     iCode *ic = NULL;
1478
1479     if (getenv("DISABLE_FILL_GAPS")) return;
1480
1481     /* look for liveranges that were spilt by the allocator */
1482     for (sym = hTabFirstItem(liveRanges,&key) ; sym ;
1483          sym = hTabNextItem(liveRanges,&key)) {
1484
1485         int i;
1486         int pdone = 0;
1487
1488         if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1489
1490         /* find the liveRanges this one clashes with, that are
1491            still assigned to registers & mark the registers as used*/
1492         for ( i = 0 ; i < sym->clashes->size ; i ++) {
1493             int k;
1494             symbol *clr;
1495
1496             if (bitVectBitValue(sym->clashes,i) == 0 ||    /* those that clash with this */
1497                 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1498                 continue ;
1499
1500             clr = hTabItemWithKey(liveRanges,i);
1501             assert(clr);
1502
1503             /* mark these registers as used */
1504             for (k = 0 ; k < clr->nRegs ; k++ )
1505                 useReg(clr->regs[k]);
1506         }
1507
1508         if (willCauseSpill(sym->nRegs,sym->regType)) {
1509             /* NOPE :( clear all registers & and continue */
1510             freeAllRegs();
1511             continue ;
1512         }
1513
1514         ic = NULL;
1515         for (i = 0 ; i < sym->defs->size ; i++ )
1516           {
1517             if (bitVectBitValue(sym->defs,i))
1518               {
1519                 if (!(ic = hTabItemWithKey(iCodehTab,i)))
1520                   continue;
1521                 if (ic->op == CAST)
1522                   break;
1523               }
1524           }
1525
1526         D(printf("Attempting fillGaps on %s: [",sym->name));
1527         /* THERE IS HOPE !!!! */
1528         for (i=0; i < sym->nRegs ; i++ ) {
1529             if (sym->regType == REG_PTR)
1530                 sym->regs[i] = getRegPtrNoSpil ();
1531             else if (sym->regType == REG_BIT)
1532                 sym->regs[i] = getRegBitNoSpil ();
1533             else
1534               {
1535                 sym->regs[i] = NULL;
1536                 if (ic && ic->op == CAST && IS_SYMOP (IC_RIGHT (ic)))
1537                   {
1538                     symbol * right = OP_SYMBOL (IC_RIGHT (ic));
1539
1540                     if (right->regs[i])
1541                       sym->regs[i] = allocThisReg (right->regs[i]);
1542                   }
1543                 if (!sym->regs[i])
1544                   sym->regs[i] = getRegGprNoSpil ();
1545               }
1546             D(printf("%s ", sym->regs[i]->name));
1547         }
1548         D(printf("]\n"));
1549
1550         /* For all its definitions check if the registers
1551            allocated needs positioning NOTE: we can position
1552            only ONCE if more than One positioning required
1553            then give up.
1554            We may need to perform the checks twice; once to
1555            position the registers as needed, the second to
1556            verify any register repositioning is still
1557            compatible.
1558           */
1559         sym->isspilt = 0;
1560         for (pass=0; pass<2; pass++) {
1561             D(printf(" checking definitions\n"));
1562             for (i = 0 ; i < sym->defs->size ; i++ ) {
1563                 if (bitVectBitValue(sym->defs,i)) {
1564                     if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1565                     D(printf("  ic->seq = %d\n", ic->seq));
1566                     if (SKIP_IC(ic)) continue;
1567                     assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1568                     /* if left is assigned to registers */
1569                     if (IS_SYMOP(IC_LEFT(ic)))
1570                       {
1571                         D(printf("   left = "));
1572                         D(printOperand(IC_LEFT(ic),NULL));
1573                       }
1574                     if (IS_SYMOP(IC_LEFT(ic)) &&
1575                       bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1576                         pdone += (positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)))>0);
1577                     }
1578                     if (IS_SYMOP(IC_RIGHT(ic)))
1579                       {
1580                         D(printf("   right = "));
1581                         D(printOperand(IC_RIGHT(ic),NULL));
1582                       }
1583                     if (IS_SYMOP(IC_RIGHT(ic)) &&
1584                       bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1585                         pdone += (positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)))>0);
1586                     }
1587                     D(printf("   pdone = %d\n", pdone));
1588                     if (pdone > 1) break;
1589                 }
1590             }
1591             D(printf(" checking uses\n"));
1592             for (i = 0 ; i < sym->uses->size ; i++ ) {
1593                 if (bitVectBitValue(sym->uses,i)) {
1594                     iCode *ic;
1595                     if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1596                     D(printf("  ic->seq = %d\n", ic->seq));
1597                     if (SKIP_IC(ic)) continue;
1598                     if (POINTER_SET(ic) || POINTER_GET(ic)) continue ;
1599
1600                     /* if result is assigned to registers */
1601                     if (IS_SYMOP(IC_RESULT(ic)))
1602                       {
1603                         D(printf("   result = "));
1604                         D(printOperand(IC_RESULT(ic),NULL));
1605                       }
1606                     if (IS_SYMOP(IC_RESULT(ic)) &&
1607                         bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RESULT(ic))->key)) {
1608                         pdone += (positionRegs(sym,OP_SYMBOL(IC_RESULT(ic)))>0);
1609                     }
1610                     D(printf("   pdone = %d\n", pdone));
1611                     if (pdone > 1) break;
1612                 }
1613             }
1614             if (pdone == 0) break; /* second pass only if regs repositioned */
1615             if (pdone > 1) break;
1616         }
1617         D(printf(" sym->regs = ["));
1618         for (i=0; i < sym->nRegs ; i++ )
1619           D(printf("%s ", sym->regs[i]->name));
1620         D(printf("]\n"));
1621         /* had to position more than once GIVE UP */
1622         if (pdone > 1) {
1623             /* UNDO all the changes we made to try this */
1624             sym->isspilt = 1;
1625             for (i=0; i < sym->nRegs ; i++ ) {
1626                     sym->regs[i] = NULL;
1627             }
1628             freeAllRegs();
1629             D(printf ("Fill Gap gave up due to positioning for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1630             continue ;
1631         }
1632         D(printf ("FILLED GAP for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1633
1634         _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1635         sym->isspilt = sym->spillA = 0 ;
1636         sym->usl.spillLoc->allocreq--;
1637         freeAllRegs();
1638     }
1639 }
1640
1641 /*-----------------------------------------------------------------*/
1642 /* findAllBitregs :- returns bit vector of all bit registers       */
1643 /*-----------------------------------------------------------------*/
1644 static bitVect *
1645 findAllBitregs (void)
1646 {
1647   bitVect *rmask = newBitVect (mcs51_nRegs);
1648   int j;
1649
1650   for (j = 0; j < mcs51_nRegs; j++)
1651     {
1652       if (regs8051[j].type == REG_BIT)
1653         rmask = bitVectSetBit (rmask, regs8051[j].rIdx);
1654     }
1655
1656   return rmask;
1657 }
1658
1659 /*-----------------------------------------------------------------*/
1660 /* mcs51_allBitregs :- returns bit vector of all bit registers     */
1661 /*-----------------------------------------------------------------*/
1662 bitVect *
1663 mcs51_allBitregs (void)
1664 {
1665   return _G.allBitregs;
1666 }
1667
1668 /*-----------------------------------------------------------------*/
1669 /* rUmaskForOp :- returns register mask for an operand             */
1670 /*-----------------------------------------------------------------*/
1671 bitVect *
1672 mcs51_rUmaskForOp (operand * op)
1673 {
1674   bitVect *rumask;
1675   symbol *sym;
1676   int j;
1677
1678   /* only temporaries are assigned registers */
1679   if (!IS_ITEMP (op))
1680     return NULL;
1681
1682   sym = OP_SYMBOL (op);
1683
1684   /* if spilt or no registers assigned to it
1685      then nothing */
1686   if (sym->isspilt || !sym->nRegs)
1687     return NULL;
1688
1689   rumask = newBitVect (mcs51_nRegs);
1690
1691   for (j = 0; j < sym->nRegs; j++)
1692     {
1693       if (sym->regs[j]) /* EEP - debug */
1694         rumask = bitVectSetBit (rumask, sym->regs[j]->rIdx);
1695     }
1696
1697   return rumask;
1698 }
1699
1700 /*-----------------------------------------------------------------*/
1701 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1702 /*-----------------------------------------------------------------*/
1703 static bitVect *
1704 regsUsedIniCode (iCode * ic)
1705 {
1706   bitVect *rmask = newBitVect (mcs51_nRegs);
1707
1708   /* do the special cases first */
1709   if (ic->op == IFX)
1710     {
1711       rmask = bitVectUnion (rmask,
1712                             mcs51_rUmaskForOp (IC_COND (ic)));
1713       goto ret;
1714     }
1715
1716   /* for the jumptable */
1717   if (ic->op == JUMPTABLE)
1718     {
1719       rmask = bitVectUnion (rmask,
1720                             mcs51_rUmaskForOp (IC_JTCOND (ic)));
1721
1722       goto ret;
1723     }
1724
1725   /* of all other cases */
1726   if (IC_LEFT (ic))
1727     rmask = bitVectUnion (rmask,
1728                           mcs51_rUmaskForOp (IC_LEFT (ic)));
1729
1730
1731   if (IC_RIGHT (ic))
1732     rmask = bitVectUnion (rmask,
1733                           mcs51_rUmaskForOp (IC_RIGHT (ic)));
1734
1735   if (IC_RESULT (ic))
1736     rmask = bitVectUnion (rmask,
1737                           mcs51_rUmaskForOp (IC_RESULT (ic)));
1738
1739 ret:
1740   return rmask;
1741 }
1742
1743 /*-----------------------------------------------------------------*/
1744 /* createRegMask - for each instruction will determine the regsUsed */
1745 /*-----------------------------------------------------------------*/
1746 static void
1747 createRegMask (eBBlock ** ebbs, int count)
1748 {
1749   int i;
1750
1751   /* for all blocks */
1752   for (i = 0; i < count; i++)
1753     {
1754       iCode *ic;
1755
1756       if (ebbs[i]->noPath &&
1757           (ebbs[i]->entryLabel != entryLabel &&
1758            ebbs[i]->entryLabel != returnLabel))
1759         continue;
1760
1761       /* for all instructions */
1762       for (ic = ebbs[i]->sch; ic; ic = ic->next)
1763         {
1764
1765           int j;
1766
1767           if (SKIP_IC2 (ic) || !ic->rlive)
1768             continue;
1769
1770           /* first mark the registers used in this
1771              instruction */
1772           ic->rUsed = regsUsedIniCode (ic);
1773           _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1774
1775           /* now create the register mask for those
1776              registers that are in use : this is a
1777              super set of ic->rUsed */
1778           ic->rMask = newBitVect (mcs51_nRegs + 1);
1779
1780           /* for all live Ranges alive at this point */
1781           for (j = 1; j < ic->rlive->size; j++)
1782             {
1783               symbol *sym;
1784               int k;
1785
1786               /* if not alive then continue */
1787               if (!bitVectBitValue (ic->rlive, j))
1788                 continue;
1789
1790               /* find the live range we are interested in */
1791               if (!(sym = hTabItemWithKey (liveRanges, j)))
1792                 {
1793                   werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1794                           "createRegMask cannot find live range");
1795                   fprintf(stderr, "\tmissing live range: key=%d\n", j);
1796                   exit (0);
1797                 }
1798
1799               /* if no register assigned to it */
1800               if (!sym->nRegs || sym->isspilt)
1801                 continue;
1802
1803               /* for all the registers allocated to it */
1804               for (k = 0; k < sym->nRegs; k++)
1805                 if (sym->regs[k])
1806                   ic->rMask =
1807                     bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1808             }
1809         }
1810     }
1811 }
1812
1813 /*-----------------------------------------------------------------*/
1814 /* rematStr - returns the rematerialized string for a remat var    */
1815 /*-----------------------------------------------------------------*/
1816 static char *
1817 rematStr (symbol * sym)
1818 {
1819   char *s = buffer;
1820   iCode *ic = sym->rematiCode;
1821
1822   *s = 0;
1823
1824   while (1)
1825     {
1826
1827       /* if plus or minus print the right hand side */
1828       if (ic->op == '+' || ic->op == '-')
1829         {
1830           SNPRINTF (s, sizeof(buffer) - strlen(buffer),
1831                     "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1832                     ic->op);
1833           s += strlen (s);
1834           ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1835           continue;
1836         }
1837
1838       /* cast then continue */
1839       if (IS_CAST_ICODE(ic)) {
1840           ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1841           continue;
1842       }
1843       /* we reached the end */
1844       SNPRINTF (s, sizeof(buffer) - strlen(buffer),
1845                 "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1846       break;
1847     }
1848
1849   return buffer;
1850 }
1851
1852 /*-----------------------------------------------------------------*/
1853 /* regTypeNum - computes the type & number of registers required   */
1854 /*-----------------------------------------------------------------*/
1855 static void
1856 regTypeNum (eBBlock *ebbs)
1857 {
1858   symbol *sym;
1859   int k;
1860   iCode *ic;
1861
1862   /* for each live range do */
1863   for (sym = hTabFirstItem (liveRanges, &k); sym;
1864        sym = hTabNextItem (liveRanges, &k))
1865     {
1866
1867       /* if used zero times then no registers needed */
1868       if ((sym->liveTo - sym->liveFrom) == 0)
1869         continue;
1870
1871
1872       /* if the live range is a temporary */
1873       if (sym->isitmp)
1874         {
1875
1876           /* if the type is marked as a conditional */
1877           if (sym->regType == REG_CND)
1878             continue;
1879
1880           /* if used in return only then we don't
1881              need registers */
1882           if (sym->ruonly || sym->accuse)
1883             {
1884               if (IS_AGGREGATE (sym->type) || sym->isptr)
1885                 sym->type = aggrToPtr (sym->type, FALSE);
1886               continue;
1887             }
1888
1889           /* if the symbol has only one definition &
1890              that definition is a get_pointer */
1891           if (bitVectnBitsOn (sym->defs) == 1 &&
1892               (ic = hTabItemWithKey (iCodehTab,
1893                                      bitVectFirstBit (sym->defs))) &&
1894               POINTER_GET (ic) &&
1895               !IS_BITVAR (sym->etype) &&
1896               (aggrToPtrDclType (operandType (IC_LEFT (ic)), FALSE) == POINTER))
1897             {
1898
1899               if (ptrPseudoSymSafe (sym, ic))
1900                 {
1901                   ptrPseudoSymConvert (sym, ic, rematStr (OP_SYMBOL (IC_LEFT (ic))));
1902                   continue;
1903                 }
1904
1905               /* if in data space or idata space then try to
1906                  allocate pointer register */
1907
1908             }
1909
1910           /* if not then we require registers */
1911           sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1912                         getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1913                         getSize (sym->type));
1914
1915           if (sym->nRegs > 4)
1916             {
1917               fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1918               printTypeChain (sym->type, stderr);
1919               fprintf (stderr, "\n");
1920             }
1921
1922           /* determine the type of register required */
1923           if (sym->nRegs == 1 && IS_PTR (sym->type) && sym->uptr)
1924             sym->regType = REG_PTR;
1925           else if (IS_BIT(sym->type))
1926             sym->regType = REG_BIT;
1927           else
1928             sym->regType = REG_GPR;
1929         }
1930       else
1931         /* for the first run we don't provide */
1932         /* registers for true symbols we will */
1933         /* see how things go                  */
1934         sym->nRegs = 0;
1935     }
1936
1937 }
1938
1939 /*-----------------------------------------------------------------*/
1940 /* freeAllRegs - mark all registers as free                        */
1941 /*-----------------------------------------------------------------*/
1942 static void
1943 freeAllRegs ()
1944 {
1945   int i;
1946
1947   for (i = 0; i < mcs51_nRegs; i++)
1948     regs8051[i].isFree = 1;
1949 }
1950
1951 /*-----------------------------------------------------------------*/
1952 /* deallocStackSpil - this will set the stack pointer back         */
1953 /*-----------------------------------------------------------------*/
1954 static
1955 DEFSETFUNC (deallocStackSpil)
1956 {
1957   symbol *sym = item;
1958
1959   deallocLocal (sym);
1960   return 0;
1961 }
1962
1963 /*-----------------------------------------------------------------*/
1964 /* farSpacePackable - returns the packable icode for far variables */
1965 /*-----------------------------------------------------------------*/
1966 static iCode *
1967 farSpacePackable (iCode * ic)
1968 {
1969   iCode *dic;
1970
1971   /* go thru till we find a definition for the
1972      symbol on the right */
1973   for (dic = ic->prev; dic; dic = dic->prev)
1974     {
1975       /* if the definition is a call then no */
1976       if ((dic->op == CALL || dic->op == PCALL) &&
1977           IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1978         {
1979           return NULL;
1980         }
1981
1982       /* if shift by unknown amount then not */
1983       if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1984           IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1985         return NULL;
1986
1987       /* if pointer get and size > 1 */
1988       if (POINTER_GET (dic) &&
1989           getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1990         return NULL;
1991
1992       if (POINTER_SET (dic) &&
1993           getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1994         return NULL;
1995
1996       if (dic->op == IFX)
1997         {
1998           if (IC_COND (dic) &&
1999               IS_TRUE_SYMOP (IC_COND (dic)) &&
2000               isOperandInFarSpace (IC_COND (dic)))
2001             return NULL;
2002         }
2003       else if (dic->op == JUMPTABLE)
2004         {
2005           if (IC_JTCOND (dic) &&
2006               IS_TRUE_SYMOP (IC_JTCOND (dic)) &&
2007               isOperandInFarSpace (IC_JTCOND (dic)))
2008             return NULL;
2009         }
2010       else
2011         {
2012           /* if any tree is a true symbol in far space */
2013           if (IC_RESULT (dic) &&
2014               IS_TRUE_SYMOP (IC_RESULT (dic)) &&
2015               isOperandInFarSpace (IC_RESULT (dic)))
2016             return NULL;
2017
2018           if (IC_RIGHT (dic) &&
2019               IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
2020               isOperandInFarSpace (IC_RIGHT (dic)) &&
2021               !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
2022             return NULL;
2023
2024           if (IC_LEFT (dic) &&
2025               IS_TRUE_SYMOP (IC_LEFT (dic)) &&
2026               isOperandInFarSpace (IC_LEFT (dic)) &&
2027               !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
2028             return NULL;
2029         }
2030
2031       if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
2032         {
2033           if ((dic->op == LEFT_OP ||
2034                dic->op == RIGHT_OP ||
2035                dic->op == '-') &&
2036               IS_OP_LITERAL (IC_RIGHT (dic)))
2037             return NULL;
2038           else
2039             return dic;
2040         }
2041     }
2042
2043   return NULL;
2044 }
2045
2046 /*-----------------------------------------------------------------*/
2047 /* packRegsForAssign - register reduction for assignment           */
2048 /*-----------------------------------------------------------------*/
2049 static int
2050 packRegsForAssign (iCode * ic, eBBlock * ebp)
2051 {
2052   iCode *dic, *sic;
2053
2054   if (!IS_ITEMP (IC_RIGHT (ic)) ||
2055       OP_SYMBOL (IC_RIGHT (ic))->isind ||
2056       OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
2057     {
2058       return 0;
2059     }
2060
2061   /* if the true symbol is defined in far space or on stack
2062      then we should not since this will increase register pressure */
2063   if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
2064     return 0;
2065   }
2066
2067   /* find the definition of iTempNN scanning backwards if we find a
2068      a use of the true symbol in before we find the definition then
2069      we cannot */
2070   for (dic = ic->prev; dic; dic = dic->prev)
2071     {
2072       int crossedCall = 0;
2073
2074       /* We can pack across a function call only if it's a local */
2075       /* variable or our parameter. Never pack global variables */
2076       /* or parameters to a function we call. */
2077       if ((dic->op == CALL || dic->op == PCALL))
2078         {
2079           if (!OP_SYMBOL (IC_RESULT (ic))->ismyparm
2080               && !OP_SYMBOL (IC_RESULT (ic))->islocal)
2081             {
2082               crossedCall = 1;
2083             }
2084         }
2085
2086       if (SKIP_IC2 (dic))
2087         continue;
2088
2089       if (dic->op == IFX)
2090         {
2091           if (IS_SYMOP (IC_COND (dic)) &&
2092               (IC_COND (dic)->key == IC_RESULT (ic)->key ||
2093                IC_COND (dic)->key == IC_RIGHT (ic)->key))
2094             {
2095               dic = NULL;
2096               break;
2097             }
2098         }
2099       else
2100         {
2101           if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
2102               IS_OP_VOLATILE (IC_RESULT (dic)))
2103             {
2104               dic = NULL;
2105               break;
2106             }
2107
2108           if (IS_SYMOP (IC_RESULT (dic)) &&
2109               IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2110             {
2111               if (POINTER_SET (dic))
2112                 dic = NULL;
2113
2114               break;
2115             }
2116
2117           if (IS_SYMOP (IC_RIGHT (dic)) &&
2118               (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
2119                IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
2120             {
2121               dic = NULL;
2122               break;
2123             }
2124
2125           if (IS_SYMOP (IC_LEFT (dic)) &&
2126               (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
2127                IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
2128             {
2129               dic = NULL;
2130               break;
2131             }
2132
2133           if (IS_SYMOP (IC_RESULT (dic)) &&
2134               IC_RESULT (dic)->key == IC_RESULT (ic)->key)
2135             {
2136               dic = NULL;
2137               break;
2138             }
2139
2140           if (crossedCall)
2141             {
2142               dic = NULL;
2143               break;
2144             }
2145
2146         }
2147     }
2148
2149   if (!dic)
2150     return 0;                   /* did not find */
2151
2152   /* if assignment then check that right is not a bit */
2153   if (ASSIGNMENT (ic) && !POINTER_SET (ic))
2154     {
2155       sym_link *etype = operandType (IC_RESULT (dic));
2156       if (IS_BITFIELD (etype))
2157         {
2158           /* if result is a bit too then it's ok */
2159           etype = operandType (IC_RESULT (ic));
2160           if (!IS_BITFIELD (etype))
2161             {
2162               return 0;
2163             }
2164        }
2165     }
2166 #if 0
2167   /* if assignment then check that right is not a bit */
2168   if (ASSIGNMENT (dic) && !POINTER_SET (dic))
2169     {
2170       sym_link *etype = operandType (IC_RIGHT (dic));
2171       if (IS_BITFIELD (etype))
2172         {
2173           /* if result is a bit too then it's ok */
2174           etype = operandType (IC_RESULT (dic));
2175           if (!IS_BITFIELD (etype))
2176             return 0;
2177         }
2178     }
2179 #endif
2180   /* if the result is on stack or iaccess then it must be
2181      the same atleast one of the operands */
2182   if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
2183       OP_SYMBOL (IC_RESULT (ic))->iaccess)
2184     {
2185
2186       /* the operation has only one symbol
2187          operator then we can pack */
2188       if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
2189           (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
2190         goto pack;
2191
2192       if (!((IC_LEFT (dic) &&
2193              IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
2194             (IC_RIGHT (dic) &&
2195              IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
2196         return 0;
2197     }
2198 pack:
2199   /* found the definition */
2200   /* replace the result with the result of */
2201   /* this assignment and remove this assignment */
2202   bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2203   ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
2204
2205   if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
2206     {
2207       OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
2208     }
2209   // TODO: and the otherway around?
2210
2211   /* delete from liverange table also
2212      delete from all the points inbetween and the new
2213      one */
2214   for (sic = dic; sic != ic; sic = sic->next)
2215     {
2216       bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
2217       if (IS_ITEMP (IC_RESULT (dic)))
2218         bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
2219     }
2220
2221   remiCodeFromeBBlock (ebp, ic);
2222   bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2223   hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2224   OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2225   return 1;
2226 }
2227
2228 /*------------------------------------------------------------------*/
2229 /* findAssignToSym : scanning backwards looks for first assig found */
2230 /*------------------------------------------------------------------*/
2231 static iCode *
2232 findAssignToSym (operand * op, iCode * ic)
2233 {
2234   iCode *dic;
2235
2236   /* This routine is used to find sequences like
2237      iTempAA = FOO;
2238      ...;  (intervening ops don't use iTempAA or modify FOO)
2239      blah = blah + iTempAA;
2240
2241      and eliminate the use of iTempAA, freeing up its register for
2242      other uses.
2243   */
2244
2245   for (dic = ic->prev; dic; dic = dic->prev)
2246     {
2247
2248       /* if definition by assignment */
2249       if (dic->op == '=' &&
2250           !POINTER_SET (dic) &&
2251           IC_RESULT (dic)->key == op->key
2252 /*          &&  IS_TRUE_SYMOP(IC_RIGHT(dic)) */
2253         )
2254         break;  /* found where this temp was defined */
2255
2256       /* if we find an usage then we cannot delete it */
2257
2258       if (dic->op == IFX)
2259         {
2260           if (IC_COND (dic) && IC_COND (dic)->key == op->key)
2261             return NULL;
2262         }
2263       else if (dic->op == JUMPTABLE)
2264         {
2265           if (IC_JTCOND (dic) && IC_JTCOND (dic)->key == op->key)
2266             return NULL;
2267         }
2268       else
2269         {
2270           if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
2271             return NULL;
2272
2273           if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
2274             return NULL;
2275
2276           if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
2277             return NULL;
2278         }
2279     }
2280
2281   if (!dic)
2282     return NULL;   /* didn't find any assignment to op */
2283
2284   /* we are interested only if defined in far space */
2285   /* or in stack space in case of + & - */
2286
2287   /* if assigned to a non-symbol then don't repack regs */
2288   if (!IS_SYMOP (IC_RIGHT (dic)))
2289     return NULL;
2290
2291   /* if the symbol is volatile then we should not */
2292   if (isOperandVolatile (IC_RIGHT (dic), TRUE))
2293     return NULL;
2294   /* XXX TODO --- should we be passing FALSE to isOperandVolatile()?
2295      What does it mean for an iTemp to be volatile, anyway? Passing
2296      TRUE is more cautious but may prevent possible optimizations */
2297
2298   /* if the symbol is in far space then we should not */
2299   if (isOperandInFarSpace (IC_RIGHT (dic)))
2300     return NULL;
2301
2302   /* for + & - operations make sure that
2303      if it is on the stack it is the same
2304      as one of the three operands */
2305   if ((ic->op == '+' || ic->op == '-') &&
2306       OP_SYMBOL (IC_RIGHT (dic))->onStack)
2307     {
2308
2309       if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
2310           IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
2311           IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
2312         return NULL;
2313     }
2314
2315   /* now make sure that the right side of dic
2316      is not defined between ic & dic */
2317   if (dic)
2318     {
2319       iCode *sic = dic->next;
2320
2321       for (; sic != ic; sic = sic->next)
2322         if (IC_RESULT (sic) &&
2323             IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2324           return NULL;
2325     }
2326
2327   return dic;
2328 }
2329
2330 /*-----------------------------------------------------------------*/
2331 /* reassignAliasedSym - used by packRegsForSupport to replace      */
2332 /*                      redundant iTemp with equivalent symbol     */
2333 /*-----------------------------------------------------------------*/
2334 static void
2335 reassignAliasedSym (eBBlock *ebp, iCode *assignment, iCode *use, operand *op)
2336 {
2337   iCode *ic;
2338   unsigned oldSymKey, newSymKey;
2339
2340   oldSymKey = op->key;
2341   newSymKey = IC_RIGHT(assignment)->key;
2342
2343   /* only track live ranges of compiler-generated temporaries */
2344   if (!IS_ITEMP(IC_RIGHT(assignment)))
2345     newSymKey = 0;
2346
2347   /* update the live-value bitmaps */
2348   for (ic = assignment; ic != use; ic = ic->next) {
2349     bitVectUnSetBit (ic->rlive, oldSymKey);
2350     if (newSymKey != 0)
2351       ic->rlive = bitVectSetBit (ic->rlive, newSymKey);
2352   }
2353
2354   /* update the sym of the used operand */
2355   OP_SYMBOL(op) = OP_SYMBOL(IC_RIGHT(assignment));
2356   op->key = OP_SYMBOL(op)->key;
2357   OP_SYMBOL(op)->accuse = 0;
2358
2359   /* update the sym's liverange */
2360   if ( OP_LIVETO(op) < ic->seq )
2361     setToRange(op, ic->seq, FALSE);
2362
2363   /* remove the assignment iCode now that its result is unused */
2364   remiCodeFromeBBlock (ebp, assignment);
2365   bitVectUnSetBit(OP_SYMBOL(IC_RESULT(assignment))->defs, assignment->key);
2366   hTabDeleteItem (&iCodehTab, assignment->key, assignment, DELETE_ITEM, NULL);
2367 }
2368
2369
2370 /*-----------------------------------------------------------------*/
2371 /* packRegsForSupport :- reduce some registers for support calls   */
2372 /*-----------------------------------------------------------------*/
2373 static int
2374 packRegsForSupport (iCode * ic, eBBlock * ebp)
2375 {
2376   iCode *dic;
2377
2378   /* for the left & right operand :- look to see if the
2379      left was assigned a true symbol in far space in that
2380      case replace them */
2381
2382   if (IS_ITEMP (IC_LEFT (ic)) &&
2383       OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2384     {
2385       dic = findAssignToSym (IC_LEFT (ic), ic);
2386
2387       if (dic)
2388         {
2389           /* found it we need to remove it from the block */
2390           reassignAliasedSym (ebp, dic, ic, IC_LEFT(ic));
2391           return 1;
2392         }
2393     }
2394
2395   /* do the same for the right operand */
2396   if (IS_ITEMP (IC_RIGHT (ic)) &&
2397       OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2398     {
2399       iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2400
2401       if (dic)
2402         {
2403           /* if this is a subtraction & the result
2404              is a true symbol in far space then don't pack */
2405           if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2406             {
2407               sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2408               if (IN_FARSPACE (SPEC_OCLS (etype)))
2409                 return 0;
2410             }
2411           /* found it we need to remove it from the
2412              block */
2413           reassignAliasedSym (ebp, dic, ic, IC_RIGHT(ic));
2414
2415           return 1;
2416         }
2417     }
2418
2419   return 0;
2420 }
2421
2422 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2423
2424
2425 /*-----------------------------------------------------------------*/
2426 /* packRegsForOneuse : - will reduce some registers for single Use */
2427 /*-----------------------------------------------------------------*/
2428 static iCode *
2429 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
2430 {
2431   iCode *dic, *sic;
2432
2433   /* if returning a literal then do nothing */
2434   if (!IS_ITEMP (op))
2435     return NULL;
2436
2437   /* if rematerializable or already return use then do nothing */
2438   if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly)
2439     return NULL;
2440
2441   /* only upto 2 bytes since we cannot predict
2442      the usage of b, & acc */
2443   if (getSize (operandType (op)) > (fReturnSizeMCS51 - 2))
2444     return NULL;
2445
2446   if (ic->op != RETURN &&
2447       ic->op != SEND &&
2448       !POINTER_SET (ic) &&
2449       !POINTER_GET (ic))
2450     return NULL;
2451
2452   if (ic->op == SEND && ic->argreg != 1) return NULL;
2453
2454   /* this routine will mark the a symbol as used in one
2455      instruction use only && if the defintion is local
2456      (ie. within the basic block) && has only one definition &&
2457      that definition is either a return value from a
2458      function or does not contain any variables in
2459      far space */
2460   if (bitVectnBitsOn (OP_USES (op)) > 1)
2461     return NULL;
2462
2463   /* if it has only one defintion */
2464   if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2465     return NULL;                /* has more than one definition */
2466
2467   /* get that definition */
2468   if (!(dic =
2469         hTabItemWithKey (iCodehTab,
2470                          bitVectFirstBit (OP_DEFS (op)))))
2471     return NULL;
2472
2473   /* if that only usage is a cast */
2474   if (dic->op == CAST) {
2475     /* to a bigger type */
2476     if (getSize(OP_SYM_TYPE(IC_RESULT(dic))) >
2477         getSize(OP_SYM_TYPE(IC_RIGHT(dic)))) {
2478       /* than we can not, since we cannot predict the usage of b & acc */
2479       return NULL;
2480     }
2481   }
2482
2483   /* found the definition now check if it is local */
2484   if (dic->seq < ebp->fSeq ||
2485       dic->seq > ebp->lSeq)
2486     return NULL;                /* non-local */
2487
2488   /* now check if it is the return from
2489      a function call */
2490   if (dic->op == CALL || dic->op == PCALL)
2491     {
2492       if (ic->op != SEND && ic->op != RETURN &&
2493           !POINTER_SET(ic) && !POINTER_GET(ic))
2494         {
2495           OP_SYMBOL (op)->ruonly = 1;
2496           return dic;
2497         }
2498     }
2499   else
2500     {
2501   /* otherwise check that the definition does
2502      not contain any symbols in far space */
2503   if (isOperandInFarSpace (IC_LEFT (dic)) ||
2504       isOperandInFarSpace (IC_RIGHT (dic)) ||
2505       IS_OP_RUONLY (IC_LEFT (ic)) ||
2506       IS_OP_RUONLY (IC_RIGHT (ic)))
2507     {
2508       return NULL;
2509     }
2510
2511   /* if pointer set then make sure the pointer
2512      is one byte */
2513   if (POINTER_SET (dic) &&
2514       !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2515     return NULL;
2516
2517   if (POINTER_GET (dic) &&
2518       !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2519     return NULL;
2520     }
2521
2522   /* Make sure no overlapping liverange is already assigned to DPTR */
2523   if (OP_SYMBOL(op)->clashes)
2524     {
2525       symbol *sym;
2526       int i;
2527
2528       for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ )
2529         {
2530           if (bitVectBitValue(OP_SYMBOL(op)->clashes,i))
2531             {
2532               sym = hTabItemWithKey(liveRanges,i);
2533               if (sym->ruonly)
2534                 return NULL ;
2535             }
2536         }
2537     }
2538
2539   sic = dic;
2540
2541   /* also make sure the intervenening instructions
2542      don't have any thing in far space */
2543   for (dic = dic->next; dic && dic != ic && sic != ic; dic = dic->next)
2544     {
2545
2546       /* if there is an intervening function call then no */
2547       if (dic->op == CALL || dic->op == PCALL)
2548         return NULL;
2549       /* if pointer set then make sure the pointer
2550          is one byte */
2551       if (POINTER_SET (dic) &&
2552           !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2553         return NULL;
2554
2555       if (POINTER_GET (dic) &&
2556           !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2557         return NULL;
2558
2559       /* if address of & the result is remat the okay */
2560       if (dic->op == ADDRESS_OF &&
2561           OP_SYMBOL (IC_RESULT (dic))->remat)
2562         continue;
2563
2564       /* if operand has size of three or more & this
2565          operation is a '*','/' or '%' then 'b' may
2566          cause a problem */
2567       if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
2568           getSize (operandType (op)) >= 3)
2569         return NULL;
2570
2571       /* if left or right or result is in far space */
2572       if (isOperandInFarSpace (IC_LEFT (dic)) ||
2573           isOperandInFarSpace (IC_RIGHT (dic)) ||
2574           isOperandInFarSpace (IC_RESULT (dic)) ||
2575           IS_OP_RUONLY (IC_LEFT (dic)) ||
2576           IS_OP_RUONLY (IC_RIGHT (dic)) ||
2577           IS_OP_RUONLY (IC_RESULT (dic)))
2578         {
2579           return NULL;
2580         }
2581       /* if left or right or result is on stack */
2582       if (isOperandOnStack(IC_LEFT(dic)) ||
2583           isOperandOnStack(IC_RIGHT(dic)) ||
2584           isOperandOnStack(IC_RESULT(dic))) {
2585         return NULL;
2586       }
2587     }
2588
2589   OP_SYMBOL (op)->ruonly = 1;
2590   return sic;
2591 }
2592
2593 /*-----------------------------------------------------------------*/
2594 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN          */
2595 /*-----------------------------------------------------------------*/
2596 static bool
2597 isBitwiseOptimizable (iCode * ic)
2598 {
2599   sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2600   sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2601
2602   /* bitwise operations are considered optimizable
2603      under the following conditions (Jean-Louis VERN)
2604
2605      x & lit
2606      bit & bit
2607      bit & x
2608      bit ^ bit
2609      bit ^ x
2610      x   ^ lit
2611      x   | lit
2612      bit | bit
2613      bit | x
2614   */
2615   if (IS_LITERAL(rtype) ||
2616       (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2617     return TRUE;
2618   else
2619     return FALSE;
2620 }
2621
2622 /*-----------------------------------------------------------------*/
2623 /* isCommutativeOp - tests whether this op cares what order its    */
2624 /*                   operands are in                               */
2625 /*-----------------------------------------------------------------*/
2626 bool isCommutativeOp(unsigned int op)
2627 {
2628   if (op == '+' || op == '*' || op == EQ_OP ||
2629       op == '^' || op == '|' || op == BITWISEAND)
2630     return TRUE;
2631   else
2632     return FALSE;
2633 }
2634
2635 /*-----------------------------------------------------------------*/
2636 /* operandUsesAcc - determines whether the code generated for this */
2637 /*                  operand will have to use the accumulator       */
2638 /*-----------------------------------------------------------------*/
2639 bool operandUsesAcc(operand *op)
2640 {
2641   if (!op)
2642     return FALSE;
2643
2644   if (IS_SYMOP(op)) {
2645     symbol *sym = OP_SYMBOL(op);
2646     memmap *symspace;
2647
2648     if (sym->accuse)
2649       return TRUE;  /* duh! */
2650
2651     if (IN_STACK(sym->etype) || sym->onStack ||
2652         (SPIL_LOC(op) && SPIL_LOC(op)->onStack))
2653       return TRUE;  /* acc is used to calc stack offset */
2654
2655     if (IS_ITEMP(op))
2656       {
2657         if (SPIL_LOC(op)) {
2658           sym = SPIL_LOC(op);  /* if spilled, look at spill location */
2659         } else {
2660           return FALSE;  /* more checks? */
2661         }
2662       }
2663
2664     symspace = SPEC_OCLS(sym->etype);
2665
2666     if (sym->iaccess && symspace->paged)
2667       return TRUE;  /* must fetch paged indirect sym via accumulator */
2668
2669     if (IN_BITSPACE(symspace))
2670       return TRUE;  /* fetching bit vars uses the accumulator */
2671
2672     if (IN_FARSPACE(symspace) || IN_CODESPACE(symspace))
2673       return TRUE;  /* fetched via accumulator and dptr */
2674   }
2675
2676   return FALSE;
2677 }
2678
2679 /*-----------------------------------------------------------------*/
2680 /* packRegsForAccUse - pack registers for acc use                  */
2681 /*-----------------------------------------------------------------*/
2682 static void
2683 packRegsForAccUse (iCode * ic)
2684 {
2685   iCode *uic;
2686
2687   /* if this is an aggregate, e.g. a one byte char array */
2688   if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2689     return;
2690   }
2691
2692   /* if we are calling a reentrant function that has stack parameters */
2693   if (ic->op == CALL &&
2694        IFFUNC_ISREENT(operandType(IC_LEFT(ic))) &&
2695        FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))))
2696       return;
2697
2698   if (ic->op == PCALL &&
2699        IFFUNC_ISREENT(operandType(IC_LEFT(ic))->next) &&
2700        FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))->next))
2701       return;
2702
2703   /* if + or - then it has to be one byte result */
2704   if ((ic->op == '+' || ic->op == '-')
2705       && getSize (operandType (IC_RESULT (ic))) > 1)
2706     return;
2707
2708   /* if shift operation make sure right side is not a literal */
2709   if (ic->op == RIGHT_OP &&
2710       (isOperandLiteral (IC_RIGHT (ic)) ||
2711        getSize (operandType (IC_RESULT (ic))) > 1))
2712     return;
2713
2714   if (ic->op == LEFT_OP &&
2715       (isOperandLiteral (IC_RIGHT (ic)) ||
2716        getSize (operandType (IC_RESULT (ic))) > 1))
2717     return;
2718
2719   if (IS_BITWISE_OP (ic) &&
2720       getSize (operandType (IC_RESULT (ic))) > 1)
2721     return;
2722
2723
2724   /* has only one definition */
2725   if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2726     return;
2727
2728   /* has only one use */
2729   if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2730     return;
2731
2732   /* and the usage immediately follows this iCode */
2733   if (!(uic = hTabItemWithKey (iCodehTab,
2734                                bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2735     return;
2736
2737   if (ic->next != uic)
2738     return;
2739
2740   /* if it is a conditional branch then we definitely can */
2741   if (uic->op == IFX)
2742     goto accuse;
2743
2744   if (uic->op == JUMPTABLE)
2745     return;
2746
2747   if (POINTER_SET (uic) &&
2748       getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2749     return;
2750
2751   /* if the usage is not is an assignment
2752      or an arithmetic / bitwise / shift operation then not */
2753   if (uic->op != '=' &&
2754       !IS_ARITHMETIC_OP (uic) &&
2755       !IS_BITWISE_OP (uic) &&
2756       uic->op != LEFT_OP &&
2757       uic->op != RIGHT_OP)
2758     return;
2759
2760   /* if used in ^ operation then make sure right is not a
2761      literal (WIML: Why is this?) */
2762   if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2763     return;
2764
2765   /* if shift operation make sure right side is not a literal */
2766   /* WIML: Why is this? */
2767   if (uic->op == RIGHT_OP &&
2768       (isOperandLiteral (IC_RIGHT (uic)) ||
2769        getSize (operandType (IC_RESULT (uic))) > 1))
2770     return;
2771   if (uic->op == LEFT_OP &&
2772       (isOperandLiteral (IC_RIGHT (uic)) ||
2773        getSize (operandType (IC_RESULT (uic))) > 1))
2774     return;
2775
2776   /* make sure that the result of this icode is not on the
2777      stack, since acc is used to compute stack offset */
2778 #if 0
2779   if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2780       OP_SYMBOL (IC_RESULT (uic))->onStack)
2781     return;
2782 #else
2783   if (isOperandOnStack(IC_RESULT(uic)))
2784     return;
2785 #endif
2786
2787   /* if the usage has only one operand then we can */
2788   if (IC_LEFT (uic) == NULL ||
2789       IC_RIGHT (uic) == NULL)
2790     goto accuse;
2791
2792   /* if the other operand uses the accumulator then we cannot */
2793   if ( (IC_LEFT(uic)->key == IC_RESULT(ic)->key &&
2794         operandUsesAcc(IC_RIGHT(uic))) ||
2795        (IC_RIGHT(uic)->key == IC_RESULT(ic)->key &&
2796         operandUsesAcc(IC_LEFT(uic))) )
2797     return;
2798
2799   /* make sure this is on the left side if not commutative */
2800   /* except for '-', which has been written to be able to
2801      handle reversed operands */
2802   if (!(isCommutativeOp(ic->op) || ic->op == '-') &&
2803        IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2804     return;
2805
2806 #if 0
2807   // this is too dangerous and need further restrictions
2808   // see bug #447547
2809
2810   /* if one of them is a literal then we can */
2811   if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2812       (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2813     {
2814       OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2815       return;
2816     }
2817 #endif
2818
2819 accuse:
2820   OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2821
2822 }
2823
2824 /*-----------------------------------------------------------------*/
2825 /* packForPush - heuristics to reduce iCode for pushing            */
2826 /*-----------------------------------------------------------------*/
2827 static void
2828 packForPush (iCode * ic, eBBlock ** ebpp, int blockno)
2829 {
2830   iCode *dic, *lic;
2831   bitVect *dbv;
2832   struct eBBlock * ebp=ebpp[blockno];
2833
2834   if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
2835     return;
2836
2837   /* must have only definition & one usage */
2838   if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2839       bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2840     return;
2841
2842   /* find the definition */
2843   if (!(dic = hTabItemWithKey (iCodehTab,
2844                                bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2845     return;
2846
2847   if (dic->op != '=' || POINTER_SET (dic))
2848     return;
2849
2850   if (dic->seq < ebp->fSeq) { // Evelyn did this
2851     int i;
2852     for (i=0; i<blockno; i++) {
2853       if (dic->seq >= ebpp[i]->fSeq && dic->seq <= ebpp[i]->lSeq) {
2854         ebp=ebpp[i];
2855         break;
2856       }
2857     }
2858     wassert (i!=blockno); // no way to recover from here
2859   }
2860
2861   if (IS_SYMOP(IC_RIGHT(dic))) {
2862     /* make sure the right side does not have any definitions
2863        inbetween */
2864     dbv = OP_DEFS(IC_RIGHT(dic));
2865     for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2866       if (bitVectBitValue(dbv,lic->key))
2867         return ;
2868     }
2869     /* make sure they have the same type */
2870     if (IS_SPEC(operandType(IC_LEFT(ic))))
2871     {
2872       sym_link *itype=operandType(IC_LEFT(ic));
2873       sym_link *ditype=operandType(IC_RIGHT(dic));
2874
2875       if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2876           SPEC_LONG(itype)!=SPEC_LONG(ditype))
2877         return;
2878     }
2879     /* extend the live range of replaced operand if needed */
2880     if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < ic->seq) {
2881       OP_SYMBOL(IC_RIGHT(dic))->liveTo = ic->seq;
2882     }
2883     bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2884   }
2885
2886   /* we now we know that it has one & only one def & use
2887      and the that the definition is an assignment */
2888   ReplaceOpWithCheaperOp(&IC_LEFT (ic), IC_RIGHT (dic));
2889   remiCodeFromeBBlock (ebp, dic);
2890   hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2891 }
2892
2893 /*-----------------------------------------------------------------*/
2894 /* packRegisters - does some transformations to reduce register    */
2895 /*                   pressure                                      */
2896 /*-----------------------------------------------------------------*/
2897 static void
2898 packRegisters (eBBlock ** ebpp, int blockno)
2899 {
2900   iCode *ic;
2901   int change = 0;
2902   eBBlock *ebp=ebpp[blockno];
2903
2904   while (1)
2905     {
2906       change = 0;
2907
2908       /* look for assignments of the form */
2909       /* iTempNN = TRueSym (someoperation) SomeOperand */
2910       /*       ....                       */
2911       /* TrueSym := iTempNN:1             */
2912       for (ic = ebp->sch; ic; ic = ic->next)
2913         {
2914           /* find assignment of the form TrueSym := iTempNN:1 */
2915           if (ic->op == '=' && !POINTER_SET (ic))
2916             change += packRegsForAssign (ic, ebp);
2917         }
2918
2919       if (!change)
2920         break;
2921     }
2922
2923   for (ic = ebp->sch; ic; ic = ic->next)
2924     {
2925       /* Fix for bug #979599:   */
2926       /* P0 &= ~1;              */
2927
2928       /* Look for two subsequent iCodes with */
2929       /*   iTemp := _c;         */
2930       /*   _c = iTemp & op;     */
2931       /* and replace them by    */
2932       /*   iTemp := _c;         */
2933       /*   _c = _c & op;        */
2934       if ((ic->op == BITWISEAND || ic->op == '|' || ic->op == '^') &&
2935           ic->prev &&
2936           ic->prev->op == '=' &&
2937           IS_ITEMP (IC_LEFT (ic)) &&
2938           IC_LEFT (ic) == IC_RESULT (ic->prev) &&
2939           isOperandEqual (IC_RESULT(ic), IC_RIGHT(ic->prev)))
2940         {
2941           iCode* ic_prev = ic->prev;
2942           symbol* prev_result_sym = OP_SYMBOL (IC_RESULT (ic_prev));
2943
2944           ReplaceOpWithCheaperOp (&IC_LEFT (ic), IC_RESULT (ic));
2945           if (IC_RESULT (ic_prev) != IC_RIGHT (ic))
2946             {
2947               bitVectUnSetBit (OP_USES (IC_RESULT (ic_prev)), ic->key);
2948               if (/*IS_ITEMP (IC_RESULT (ic_prev)) && */
2949                   prev_result_sym->liveTo == ic->seq)
2950                 {
2951                   prev_result_sym->liveTo = ic_prev->seq;
2952                 }
2953             }
2954           bitVectSetBit (OP_USES (IC_RESULT (ic)), ic->key);
2955
2956           bitVectSetBit (ic->rlive, IC_RESULT (ic)->key);
2957
2958           if (bitVectIsZero (OP_USES (IC_RESULT (ic_prev))))
2959             {
2960               bitVectUnSetBit (ic->rlive, IC_RESULT (ic)->key);
2961               bitVectUnSetBit (OP_DEFS (IC_RESULT (ic_prev)), ic_prev->key);
2962               remiCodeFromeBBlock (ebp, ic_prev);
2963               hTabDeleteItem (&iCodehTab, ic_prev->key, ic_prev, DELETE_ITEM, NULL);
2964             }
2965         }
2966
2967       /* if this is an itemp & result of an address of a true sym
2968          then mark this as rematerialisable   */
2969       if (ic->op == ADDRESS_OF &&
2970           IS_ITEMP (IC_RESULT (ic)) &&
2971           IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2972           bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2973           !OP_SYMBOL (IC_LEFT (ic))->onStack)
2974         {
2975           OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2976           OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2977           OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2978         }
2979
2980       /* if straight assignment then carry remat flag if
2981          this is the only definition */
2982       if (ic->op == '=' &&
2983           !POINTER_SET (ic) &&
2984           IS_SYMOP (IC_RIGHT (ic)) &&
2985           OP_SYMBOL (IC_RIGHT (ic))->remat &&
2986           !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2987           bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2988         {
2989           OP_SYMBOL (IC_RESULT (ic))->remat =
2990             OP_SYMBOL (IC_RIGHT (ic))->remat;
2991           OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2992             OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2993         }
2994
2995       /* if cast to a generic pointer & the pointer being
2996          cast is remat, then we can remat this cast as well */
2997       if (ic->op == CAST &&
2998           IS_SYMOP(IC_RIGHT(ic)) &&
2999           OP_SYMBOL(IC_RIGHT(ic))->remat &&
3000           bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1)
3001         {
3002           sym_link *to_type = operandType(IC_LEFT(ic));
3003           sym_link *from_type = operandType(IC_RIGHT(ic));
3004           if (IS_GENPTR(to_type) && IS_PTR(from_type))
3005             {
3006               OP_SYMBOL (IC_RESULT (ic))->remat = 1;
3007               OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
3008               OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
3009             }
3010         }
3011
3012       /* if this is a +/- operation with a rematerizable
3013          then mark this as rematerializable as well */
3014       if ((ic->op == '+' || ic->op == '-') &&
3015           (IS_SYMOP (IC_LEFT (ic)) &&
3016            IS_ITEMP (IC_RESULT (ic)) &&
3017            IS_OP_LITERAL (IC_RIGHT (ic))) &&
3018            OP_SYMBOL (IC_LEFT (ic))->remat &&
3019           (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
3020            bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1)
3021         {
3022           OP_SYMBOL (IC_RESULT (ic))->remat = 1;
3023           OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
3024           OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
3025         }
3026
3027       /* mark the pointer usages */
3028       if (POINTER_SET (ic))
3029         OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
3030
3031       if (POINTER_GET (ic) &&
3032           IS_SYMOP(IC_LEFT (ic)))
3033         OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
3034
3035       if (!SKIP_IC2 (ic))
3036         {
3037           /* if we are using a symbol on the stack
3038              then we should say mcs51_ptrRegReq */
3039           if (options.useXstack && ic->parmPush
3040               && (ic->op == IPUSH || ic->op == IPOP))
3041             mcs51_ptrRegReq++;
3042           if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
3043             mcs51_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ||
3044                                  OP_SYMBOL (IC_COND (ic))->iaccess ||
3045                                  SPEC_OCLS(OP_SYMBOL (IC_COND (ic))->etype) == idata) ? 1 : 0);
3046           else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
3047             mcs51_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ||
3048                               OP_SYMBOL (IC_JTCOND (ic))->iaccess ||
3049                               SPEC_OCLS(OP_SYMBOL (IC_JTCOND (ic))->etype) == idata) ? 1 : 0);
3050           else
3051             {
3052               if (IS_SYMOP (IC_LEFT (ic)))
3053                 mcs51_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ||
3054                                 OP_SYMBOL (IC_LEFT (ic))->iaccess ||
3055                                 SPEC_OCLS(OP_SYMBOL (IC_LEFT (ic))->etype) == idata) ? 1 : 0);
3056               if (IS_SYMOP (IC_RIGHT (ic)))
3057                 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ||
3058                                OP_SYMBOL (IC_RIGHT (ic))->iaccess ||
3059                                SPEC_OCLS(OP_SYMBOL (IC_RIGHT (ic))->etype) == idata) ? 1 : 0);
3060               if (IS_SYMOP (IC_RESULT (ic)))
3061                 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ||
3062                               OP_SYMBOL (IC_RESULT (ic))->iaccess ||
3063                               SPEC_OCLS(OP_SYMBOL (IC_RESULT (ic))->etype) == idata) ? 1 : 0);
3064               if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
3065                   && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned int) PTRSIZE)
3066                 mcs51_ptrRegReq ++;
3067               if (POINTER_SET (ic) && IS_SYMOP (IC_RESULT (ic))
3068                   && getSize (OP_SYMBOL (IC_RESULT (ic))->type) <= (unsigned int) PTRSIZE)
3069                 mcs51_ptrRegReq ++;
3070             }
3071         }
3072
3073       /* if the condition of an if instruction
3074          is defined in the previous instruction and
3075          this is the only usage then
3076          mark the itemp as a conditional */
3077       if ((IS_CONDITIONAL (ic) ||
3078            (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
3079           ic->next && ic->next->op == IFX &&
3080           bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
3081           isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
3082           OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
3083         {
3084           OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
3085           continue;
3086         }
3087
3088       /* if the condition of an if instruction
3089          is defined in the previous GET_POINTER instruction and
3090          this is the only usage then
3091          mark the itemp as accumulator use */
3092       if ((POINTER_GET (ic) && getSize (operandType (IC_RESULT (ic))) <=1) &&
3093           ic->next && ic->next->op == IFX &&
3094           bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
3095           isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
3096           OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
3097         {
3098           OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
3099           continue;
3100         }
3101
3102       /* reduce for support function calls */
3103       if (ic->supportRtn || ic->op == '+' || ic->op == '-')
3104         packRegsForSupport (ic, ebp);
3105
3106       /* some cases the redundant moves can
3107          can be eliminated for return statements */
3108       if ((ic->op == RETURN || (ic->op == SEND && ic->argreg == 1)) &&
3109           !isOperandInFarSpace (IC_LEFT (ic)) &&
3110           options.model == MODEL_SMALL) {
3111         packRegsForOneuse (ic, IC_LEFT (ic), ebp);
3112       }
3113
3114       /* if pointer set & left has a size more than
3115          one and right is not in far space */
3116       if (POINTER_SET (ic) &&
3117           !isOperandInFarSpace (IC_RIGHT (ic)) &&
3118           !OP_SYMBOL (IC_RESULT (ic))->remat &&
3119           !IS_OP_RUONLY (IC_RIGHT (ic)) &&
3120           getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
3121         packRegsForOneuse (ic, IC_RESULT (ic), ebp);
3122
3123       /* if pointer get */
3124       if (POINTER_GET (ic) &&
3125           IS_SYMOP (IC_LEFT (ic)) &&
3126           !isOperandInFarSpace (IC_RESULT (ic)) &&
3127           !OP_SYMBOL (IC_LEFT (ic))->remat &&
3128           !IS_OP_RUONLY (IC_RESULT (ic)) &&
3129           getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
3130         packRegsForOneuse (ic, IC_LEFT (ic), ebp);
3131
3132
3133       /* if this is a cast for intergral promotion then
3134          check if it's the only use of the definition of the
3135          operand being casted/ if yes then replace
3136          the result of that arithmetic operation with
3137          this result and get rid of the cast */
3138       if (ic->op == CAST)
3139         {
3140           sym_link *fromType = operandType (IC_RIGHT (ic));
3141           sym_link *toType = operandType (IC_LEFT (ic));
3142
3143           if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
3144               getSize (fromType) != getSize (toType) &&
3145               SPEC_USIGN (fromType) == SPEC_USIGN (toType))
3146             {
3147
3148               iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
3149               if (dic)
3150                 {
3151                   if (IS_ARITHMETIC_OP (dic))
3152                     {
3153                       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
3154                       ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
3155                       remiCodeFromeBBlock (ebp, ic);
3156                       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3157                       hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
3158                       OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
3159                       ic = ic->prev;
3160                     }
3161                   else
3162                     OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
3163                 }
3164             }
3165           else
3166             {
3167
3168               /* if the type from and type to are the same
3169                  then if this is the only use then packit */
3170               if (compareType (operandType (IC_RIGHT (ic)),
3171                              operandType (IC_LEFT (ic))) == 1)
3172                 {
3173                   iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
3174                   if (dic)
3175                     {
3176                       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
3177                       ReplaceOpWithCheaperOp(&IC_RESULT (dic), IC_RESULT (ic));
3178                       remiCodeFromeBBlock (ebp, ic);
3179                       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3180                       hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
3181                       OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
3182                       ic = ic->prev;
3183                     }
3184                 }
3185             }
3186         }
3187
3188       /* pack for PUSH
3189          iTempNN := (some variable in farspace) V1
3190          push iTempNN ;
3191          -------------
3192          push V1
3193        */
3194       if (ic->op == IPUSH)
3195         {
3196           packForPush (ic, ebpp, blockno);
3197         }
3198
3199
3200       /* pack registers for accumulator use, when the
3201          result of an arithmetic or bit wise operation
3202          has only one use, that use is immediately following
3203          the defintion and the using iCode has only one
3204          operand or has two operands but one is literal &
3205          the result of that operation is not on stack then
3206          we can leave the result of this operation in acc:b
3207          combination */
3208       if ((IS_ARITHMETIC_OP (ic)
3209            || IS_CONDITIONAL(ic)
3210            || IS_BITWISE_OP (ic)
3211            || ic->op == LEFT_OP || ic->op == RIGHT_OP || ic->op == CALL
3212            || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
3213           ) &&
3214           IS_ITEMP (IC_RESULT (ic)) &&
3215           getSize (operandType (IC_RESULT (ic))) <= 2)
3216
3217         packRegsForAccUse (ic);
3218     }
3219 }
3220
3221 /*-----------------------------------------------------------------*/
3222 /* assignRegisters - assigns registers to each live range as need  */
3223 /*-----------------------------------------------------------------*/
3224 void
3225 mcs51_assignRegisters (ebbIndex * ebbi)
3226 {
3227   eBBlock ** ebbs = ebbi->bbOrder;
3228   int count = ebbi->count;
3229   iCode *ic;
3230   int i;
3231
3232   setToNull ((void *) &_G.funcrUsed);
3233   setToNull ((void *) &_G.regAssigned);
3234   setToNull ((void *) &_G.totRegAssigned);
3235   mcs51_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
3236   if ((currFunc && IFFUNC_ISREENT (currFunc->type)) || options.stackAuto)
3237     {
3238       mcs51_nRegs = 16;
3239     }
3240   else
3241     {
3242   mcs51_nRegs = 8;
3243     }
3244   _G.allBitregs = findAllBitregs ();
3245
3246
3247   /* change assignments this will remove some
3248      live ranges reducing some register pressure */
3249
3250   for (i = 0; i < count; i++)
3251     packRegisters (ebbs, i);
3252
3253   /* liveranges probably changed by register packing
3254      so we compute them again */
3255   recomputeLiveRanges (ebbs, count);
3256
3257   if (options.dump_pack)
3258     dumpEbbsToFileExt (DUMP_PACK, ebbi);
3259
3260   /* first determine for each live range the number of
3261      registers & the type of registers required for each */
3262   regTypeNum (*ebbs);
3263
3264   /* and serially allocate registers */
3265   serialRegAssign (ebbs, count);
3266
3267   freeAllRegs ();
3268   //setToNull ((void *) &_G.regAssigned);
3269   //setToNull ((void *) &_G.totRegAssigned);
3270   fillGaps();
3271
3272   /* if stack was extended then tell the user */
3273   if (_G.stackExtend)
3274     {
3275 /*      werror(W_TOOMANY_SPILS,"stack", */
3276 /*             _G.stackExtend,currFunc->name,""); */
3277       _G.stackExtend = 0;
3278     }
3279
3280   if (_G.dataExtend)
3281     {
3282 /*      werror(W_TOOMANY_SPILS,"data space", */
3283 /*             _G.dataExtend,currFunc->name,""); */
3284       _G.dataExtend = 0;
3285     }
3286
3287   /* after that create the register mask
3288      for each of the instruction */
3289   createRegMask (ebbs, count);
3290
3291   /* redo that offsets for stacked automatic variables */
3292   if (currFunc) {
3293     redoStackOffsets ();
3294   }
3295
3296   /* make sure r0 & r1 are flagged as used if they might be used */
3297   /* as pointers */
3298   if (currFunc && mcs51_ptrRegReq)
3299     {
3300       currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, R0_IDX);
3301       currFunc->regsUsed = bitVectSetBit (currFunc->regsUsed, R1_IDX);
3302     }
3303
3304   if (options.dump_rassgn)
3305     {
3306       dumpEbbsToFileExt (DUMP_RASSGN, ebbi);
3307       dumpLiveRanges (DUMP_LRANGE, liveRanges);
3308     }
3309
3310   /* do the overlaysegment stuff SDCCmem.c */
3311   doOverlays (ebbs, count);
3312
3313   /* now get back the chain */
3314   ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
3315
3316   gen51Code (ic);
3317
3318   /* free up any _G.stackSpil locations allocated */
3319   applyToSet (_G.stackSpil, deallocStackSpil);
3320   _G.slocNum = 0;
3321   setToNull ((void *) &_G.stackSpil);
3322   setToNull ((void *) &_G.spiltSet);
3323   /* mark all registers as free */
3324   freeAllRegs ();
3325
3326   return;
3327 }