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