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