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