Implemented FillGaps for z80 & mcs51
[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
41 /* Global data */
42 static struct
43   {
44     bitVect *spiltSet;
45     set *stackSpil;
46     bitVect *regAssigned;
47     bitVect *totRegAssigned;    /* final set of LRs that got into registers */
48     short blockSpil;
49     int slocNum;
50     bitVect *funcrUsed;         /* registers used in a function */
51     int stackExtend;
52     int dataExtend;
53   }
54 _G;
55
56 /* Shared with gen.c */
57 int mcs51_ptrRegReq;            /* one byte pointer register required */
58
59 /* 8051 registers */
60 regs regs8051[] =
61 {
62
63   {REG_GPR, R2_IDX, REG_GPR, "r2", "ar2", "0", 2, 1},
64   {REG_GPR, R3_IDX, REG_GPR, "r3", "ar3", "0", 3, 1},
65   {REG_GPR, R4_IDX, REG_GPR, "r4", "ar4", "0", 4, 1},
66   {REG_GPR, R5_IDX, REG_GPR, "r5", "ar5", "0", 5, 1},
67   {REG_GPR, R6_IDX, REG_GPR, "r6", "ar6", "0", 6, 1},
68   {REG_GPR, R7_IDX, REG_GPR, "r7", "ar7", "0", 7, 1},
69   {REG_PTR, R0_IDX, REG_PTR, "r0", "ar0", "0", 0, 1},
70   {REG_PTR, R1_IDX, REG_PTR, "r1", "ar1", "0", 1, 1},
71   {REG_GPR, X8_IDX, REG_GPR, "x8", "x8", "xreg", 0, 1},
72   {REG_GPR, X9_IDX, REG_GPR, "x9", "x9", "xreg", 1, 1},
73   {REG_GPR, X10_IDX, REG_GPR, "x10", "x10", "xreg", 2, 1},
74   {REG_GPR, X11_IDX, REG_GPR, "x11", "x11", "xreg", 3, 1},
75   {REG_GPR, X12_IDX, REG_GPR, "x12", "x12", "xreg", 4, 1},
76   {REG_CND, CND_IDX, REG_CND, "C", "C", "xreg", 0, 1},
77 };
78 int mcs51_nRegs = 13;
79 static void spillThis (symbol *);
80 static void freeAllRegs ();
81
82 /*-----------------------------------------------------------------*/
83 /* allocReg - allocates register of given type                     */
84 /*-----------------------------------------------------------------*/
85 static regs *
86 allocReg (short type)
87 {
88   int i;
89
90   for (i = 0; i < mcs51_nRegs; i++)
91     {
92
93       /* if type is given as 0 then any
94          free register will do */
95       if (!type &&
96           regs8051[i].isFree)
97         {
98           regs8051[i].isFree = 0;
99           if (currFunc)
100             currFunc->regsUsed =
101               bitVectSetBit (currFunc->regsUsed, i);
102           return &regs8051[i];
103         }
104       /* other wise look for specific type
105          of register */
106       if (regs8051[i].isFree &&
107           regs8051[i].type == type)
108         {
109           regs8051[i].isFree = 0;
110           if (currFunc)
111             currFunc->regsUsed =
112               bitVectSetBit (currFunc->regsUsed, i);
113           return &regs8051[i];
114         }
115     }
116   return NULL;
117 }
118
119 /*-----------------------------------------------------------------*/
120 /* mcs51_regWithIdx - returns pointer to register wit index number       */
121 /*-----------------------------------------------------------------*/
122 regs *
123 mcs51_regWithIdx (int idx)
124 {
125   int i;
126
127   for (i = 0; i < mcs51_nRegs; i++)
128     if (regs8051[i].rIdx == idx)
129       return &regs8051[i];
130
131   werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
132           "regWithIdx not found");
133   exit (1);
134 }
135
136 /*-----------------------------------------------------------------*/
137 /* freeReg - frees a register                                      */
138 /*-----------------------------------------------------------------*/
139 static void
140 freeReg (regs * reg)
141 {
142   if (!reg)
143     {
144       werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
145               "freeReg - Freeing NULL register");
146       exit (1);
147     }
148
149   reg->isFree = 1;
150 }
151
152
153 /*-----------------------------------------------------------------*/
154 /* nFreeRegs - returns number of free registers                    */
155 /*-----------------------------------------------------------------*/
156 static int
157 nFreeRegs (int type)
158 {
159   int i;
160   int nfr = 0;
161
162   for (i = 0; i < mcs51_nRegs; i++)
163     if (regs8051[i].isFree && regs8051[i].type == type)
164       nfr++;
165   return nfr;
166 }
167
168 /*-----------------------------------------------------------------*/
169 /* nfreeRegsType - free registers with type                         */
170 /*-----------------------------------------------------------------*/
171 static int
172 nfreeRegsType (int type)
173 {
174   int nfr;
175   if (type == REG_PTR)
176     {
177       if ((nfr = nFreeRegs (type)) == 0)
178         return nFreeRegs (REG_GPR);
179     }
180
181   return nFreeRegs (type);
182 }
183
184 /*-----------------------------------------------------------------*/
185 /* useReg - marks a register  as used                              */
186 /*-----------------------------------------------------------------*/
187 static void
188 useReg (regs * reg)
189 {
190   reg->isFree = 0;
191 }
192
193 /*-----------------------------------------------------------------*/
194 /* allDefsOutOfRange - all definitions are out of a range          */
195 /*-----------------------------------------------------------------*/
196 static bool
197 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
198 {
199   int i;
200
201   if (!defs)
202     return TRUE;
203
204   for (i = 0; i < defs->size; i++)
205     {
206       iCode *ic;
207
208       if (bitVectBitValue (defs, i) &&
209           (ic = hTabItemWithKey (iCodehTab, i)) &&
210           (ic->seq >= fseq && ic->seq <= toseq))
211
212         return FALSE;
213
214     }
215
216   return TRUE;
217 }
218
219 /*-----------------------------------------------------------------*/
220 /* computeSpillable - given a point find the spillable live ranges */
221 /*-----------------------------------------------------------------*/
222 static bitVect *
223 computeSpillable (iCode * ic)
224 {
225   bitVect *spillable;
226
227   /* spillable live ranges are those that are live at this 
228      point . the following categories need to be subtracted
229      from this set. 
230      a) - those that are already spilt
231      b) - if being used by this one
232      c) - defined by this one */
233
234   spillable = bitVectCopy (ic->rlive);
235   spillable =
236     bitVectCplAnd (spillable, _G.spiltSet);     /* those already spilt */
237   spillable =
238     bitVectCplAnd (spillable, ic->uses);        /* used in this one */
239   bitVectUnSetBit (spillable, ic->defKey);
240   spillable = bitVectIntersect (spillable, _G.regAssigned);
241   return spillable;
242
243 }
244
245 /*-----------------------------------------------------------------*/
246 /* noSpilLoc - return true if a variable has no spil location      */
247 /*-----------------------------------------------------------------*/
248 static int
249 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
250 {
251   return (sym->usl.spillLoc ? 0 : 1);
252 }
253
254 /*-----------------------------------------------------------------*/
255 /* hasSpilLoc - will return 1 if the symbol has spil location      */
256 /*-----------------------------------------------------------------*/
257 static int
258 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
259 {
260   return (sym->usl.spillLoc ? 1 : 0);
261 }
262
263 /*-----------------------------------------------------------------*/
264 /* directSpilLoc - will return 1 if the splilocation is in direct  */
265 /*-----------------------------------------------------------------*/
266 static int
267 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
268 {
269   if (sym->usl.spillLoc &&
270       (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
271     return 1;
272   else
273     return 0;
274 }
275
276 /*-----------------------------------------------------------------*/
277 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
278 /*                    but is not used as a pointer                 */
279 /*-----------------------------------------------------------------*/
280 static int
281 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
282 {
283   return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
284 }
285
286 /*-----------------------------------------------------------------*/
287 /* rematable - will return 1 if the remat flag is set              */
288 /*-----------------------------------------------------------------*/
289 static int
290 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
291 {
292   return sym->remat;
293 }
294
295 /*-----------------------------------------------------------------*/
296 /* notUsedInBlock - not used in this block                         */
297 /*-----------------------------------------------------------------*/
298 static int
299 notUsedInBlock (symbol * sym, eBBlock * ebp, iCode * ic)
300 {
301   return (!bitVectBitsInCommon (sym->defs, ebp->usesDefs) &&
302           allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
303 /*     return (!bitVectBitsInCommon(sym->defs,ebp->usesDefs)); */
304 }
305
306 /*-----------------------------------------------------------------*/
307 /* notUsedInRemaining - not used or defined in remain of the block */
308 /*-----------------------------------------------------------------*/
309 static int
310 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
311 {
312   return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
313           allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
314 }
315
316 /*-----------------------------------------------------------------*/
317 /* allLRs - return true for all                                    */
318 /*-----------------------------------------------------------------*/
319 static int
320 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
321 {
322   return 1;
323 }
324
325 /*-----------------------------------------------------------------*/
326 /* liveRangesWith - applies function to a given set of live range  */
327 /*-----------------------------------------------------------------*/
328 static set *
329 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
330                 eBBlock * ebp, iCode * ic)
331 {
332   set *rset = NULL;
333   int i;
334
335   if (!lrs || !lrs->size)
336     return NULL;
337
338   for (i = 1; i < lrs->size; i++)
339     {
340       symbol *sym;
341       if (!bitVectBitValue (lrs, i))
342         continue;
343
344       /* if we don't find it in the live range 
345          hash table we are in serious trouble */
346       if (!(sym = hTabItemWithKey (liveRanges, i)))
347         {
348           werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
349                   "liveRangesWith could not find liveRange");
350           exit (1);
351         }
352
353       if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
354         addSetHead (&rset, sym);
355     }
356
357   return rset;
358 }
359
360
361 /*-----------------------------------------------------------------*/
362 /* leastUsedLR - given a set determines which is the least used    */
363 /*-----------------------------------------------------------------*/
364 static symbol *
365 leastUsedLR (set * sset)
366 {
367   symbol *sym = NULL, *lsym = NULL;
368
369   sym = lsym = setFirstItem (sset);
370
371   if (!lsym)
372     return NULL;
373
374   for (; lsym; lsym = setNextItem (sset))
375     {
376
377       /* if usage is the same then prefer
378          the spill the smaller of the two */
379       if (lsym->used == sym->used)
380         if (getSize (lsym->type) < getSize (sym->type))
381           sym = lsym;
382
383       /* if less usage */
384       if (lsym->used < sym->used)
385         sym = lsym;
386
387     }
388
389   setToNull ((void **) &sset);
390   sym->blockSpil = 0;
391   return sym;
392 }
393
394 /*-----------------------------------------------------------------*/
395 /* noOverLap - will iterate through the list looking for over lap  */
396 /*-----------------------------------------------------------------*/
397 static int
398 noOverLap (set * itmpStack, symbol * fsym)
399 {
400   symbol *sym;
401
402
403   for (sym = setFirstItem (itmpStack); sym;
404        sym = setNextItem (itmpStack))
405     {
406         if (bitVectBitValue(sym->clashes,fsym->key)) return 0;
407     }
408
409   return 1;
410 }
411
412 /*-----------------------------------------------------------------*/
413 /* isFree - will return 1 if the a free spil location is found     */
414 /*-----------------------------------------------------------------*/
415 static
416 DEFSETFUNC (isFree)
417 {
418   symbol *sym = item;
419   V_ARG (symbol **, sloc);
420   V_ARG (symbol *, fsym);
421
422   /* if already found */
423   if (*sloc)
424     return 0;
425
426   /* if it is free && and the itmp assigned to
427      this does not have any overlapping live ranges
428      with the one currently being assigned and
429      the size can be accomodated  */
430   if (sym->isFree &&
431       noOverLap (sym->usl.itmpStack, fsym) &&
432       getSize (sym->type) >= getSize (fsym->type))
433     {
434       *sloc = sym;
435       return 1;
436     }
437
438   return 0;
439 }
440
441 /*-----------------------------------------------------------------*/
442 /* spillLRWithPtrReg :- will spil those live ranges which use PTR  */
443 /*-----------------------------------------------------------------*/
444 static void
445 spillLRWithPtrReg (symbol * forSym)
446 {
447   symbol *lrsym;
448   regs *r0, *r1;
449   int k;
450
451   if (!_G.regAssigned ||
452       bitVectIsZero (_G.regAssigned))
453     return;
454
455   r0 = mcs51_regWithIdx (R0_IDX);
456   r1 = mcs51_regWithIdx (R1_IDX);
457
458   /* for all live ranges */
459   for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
460        lrsym = hTabNextItem (liveRanges, &k))
461     {
462       int j;
463
464       /* if no registers assigned to it or spilt */
465       /* if it does not overlap with this then 
466          not need to spill it */
467
468       if (lrsym->isspilt || !lrsym->nRegs ||
469           (lrsym->liveTo < forSym->liveFrom))
470         continue;
471
472       /* go thru the registers : if it is either
473          r0 or r1 then spil it */
474       for (j = 0; j < lrsym->nRegs; j++)
475         if (lrsym->regs[j] == r0 ||
476             lrsym->regs[j] == r1)
477           {
478             spillThis (lrsym);
479             break;
480           }
481     }
482
483 }
484
485 /*-----------------------------------------------------------------*/
486 /* createStackSpil - create a location on the stack to spil        */
487 /*-----------------------------------------------------------------*/
488 static symbol *
489 createStackSpil (symbol * sym)
490 {
491   symbol *sloc = NULL;
492   int useXstack, model;
493
494   char slocBuffer[30];
495
496   /* first go try and find a free one that is already 
497      existing on the stack */
498   if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
499     {
500       /* found a free one : just update & return */
501       sym->usl.spillLoc = sloc;
502       sym->stackSpil = 1;
503       sloc->isFree = 0;
504       addSetHead (&sloc->usl.itmpStack, sym);
505       return sym;
506     }
507
508   /* could not then have to create one , this is the hard part
509      we need to allocate this on the stack : this is really a
510      hack!! but cannot think of anything better at this time */
511
512   if (sprintf (slocBuffer, "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
513     {
514       fprintf (stderr, "***Internal error: slocBuffer overflowed: %s:%d\n",
515                __FILE__, __LINE__);
516       exit (1);
517     }
518
519   sloc = newiTemp (slocBuffer);
520
521   /* set the type to the spilling symbol */
522   sloc->type = copyLinkChain (sym->type);
523   sloc->etype = getSpec (sloc->type);
524   SPEC_SCLS (sloc->etype) = S_DATA;
525   SPEC_EXTR (sloc->etype) = 0;
526   SPEC_STAT (sloc->etype) = 0;
527   SPEC_VOLATILE(sloc->etype) = 0;
528
529   /* we don't allow it to be allocated`
530      onto the external stack since : so we
531      temporarily turn it off ; we also
532      turn off memory model to prevent
533      the spil from going to the external storage
534    */
535
536   useXstack = options.useXstack;
537   model = options.model;
538 /*     noOverlay = options.noOverlay; */
539 /*     options.noOverlay = 1; */
540   options.model = options.useXstack = 0;
541
542   allocLocal (sloc);
543
544   options.useXstack = useXstack;
545   options.model = model;
546 /*     options.noOverlay = noOverlay; */
547   sloc->isref = 1;              /* to prevent compiler warning */
548
549   /* if it is on the stack then update the stack */
550   if (IN_STACK (sloc->etype))
551     {
552       currFunc->stack += getSize (sloc->type);
553       _G.stackExtend += getSize (sloc->type);
554     }
555   else
556     _G.dataExtend += getSize (sloc->type);
557
558   /* add it to the _G.stackSpil set */
559   addSetHead (&_G.stackSpil, sloc);
560   sym->usl.spillLoc = sloc;
561   sym->stackSpil = 1;
562
563   /* add it to the set of itempStack set 
564      of the spill location */
565   addSetHead (&sloc->usl.itmpStack, sym);
566   return sym;
567 }
568
569 /*-----------------------------------------------------------------*/
570 /* isSpiltOnStack - returns true if the spil location is on stack  */
571 /*-----------------------------------------------------------------*/
572 static bool
573 isSpiltOnStack (symbol * sym)
574 {
575   sym_link *etype;
576
577   if (!sym)
578     return FALSE;
579
580   if (!sym->isspilt)
581     return FALSE;
582
583 /*     if (sym->_G.stackSpil) */
584 /*      return TRUE; */
585
586   if (!sym->usl.spillLoc)
587     return FALSE;
588
589   etype = getSpec (sym->usl.spillLoc->type);
590   if (IN_STACK (etype))
591     return TRUE;
592
593   return FALSE;
594 }
595
596 /*-----------------------------------------------------------------*/
597 /* spillThis - spils a specific operand                            */
598 /*-----------------------------------------------------------------*/
599 static void
600 spillThis (symbol * sym)
601 {
602   int i;
603   /* if this is rematerializable or has a spillLocation
604      we are okay, else we need to create a spillLocation
605      for it */
606   if (!(sym->remat || sym->usl.spillLoc))
607     createStackSpil (sym);
608
609
610   /* mark it has spilt & put it in the spilt set */
611   sym->isspilt = sym->spillA = 1;
612   _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
613
614   bitVectUnSetBit (_G.regAssigned, sym->key);
615   bitVectUnSetBit (_G.totRegAssigned, sym->key);
616
617   for (i = 0; i < sym->nRegs; i++)
618
619     if (sym->regs[i])
620       {
621         freeReg (sym->regs[i]);
622         sym->regs[i] = NULL;
623       }
624
625   /* if spilt on stack then free up r0 & r1 
626      if they could have been assigned to some
627      LIVE ranges */
628   if (!mcs51_ptrRegReq && isSpiltOnStack (sym))
629     {
630       mcs51_ptrRegReq++;
631       spillLRWithPtrReg (sym);
632     }
633
634   if (sym->usl.spillLoc && !sym->remat)
635     sym->usl.spillLoc->allocreq++;
636   return;
637 }
638
639 /*-----------------------------------------------------------------*/
640 /* selectSpil - select a iTemp to spil : rather a simple procedure */
641 /*-----------------------------------------------------------------*/
642 static symbol *
643 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
644 {
645   bitVect *lrcs = NULL;
646   set *selectS;
647   symbol *sym;
648
649   /* get the spillable live ranges */
650   lrcs = computeSpillable (ic);
651
652   /* get all live ranges that are rematerizable */
653   if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
654     {
655
656       /* return the least used of these */
657       return leastUsedLR (selectS);
658     }
659
660   /* get live ranges with spillLocations in direct space */
661   if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
662     {
663       sym = leastUsedLR (selectS);
664       strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
665                            sym->usl.spillLoc->rname :
666                            sym->usl.spillLoc->name));
667       sym->spildir = 1;
668       /* mark it as allocation required */
669       sym->usl.spillLoc->allocreq++;
670       return sym;
671     }
672
673   /* if the symbol is local to the block then */
674   if (forSym->liveTo < ebp->lSeq)
675     {
676
677       /* check if there are any live ranges allocated
678          to registers that are not used in this block */
679       if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
680         {
681           sym = leastUsedLR (selectS);
682           /* if this is not rematerializable */
683           if (!sym->remat)
684             {
685               _G.blockSpil++;
686               sym->blockSpil = 1;
687             }
688           return sym;
689         }
690
691       /* check if there are any live ranges that not
692          used in the remainder of the block */
693       if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
694         {
695           sym = leastUsedLR (selectS);
696           if (sym != forSym)
697             {
698               if (!sym->remat)
699                 {
700                   sym->remainSpil = 1;
701                   _G.blockSpil++;
702                 }
703               return sym;
704             }
705         }
706     }
707
708   /* find live ranges with spillocation && not used as pointers */
709   if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
710     {
711
712       sym = leastUsedLR (selectS);
713       /* mark this as allocation required */
714       sym->usl.spillLoc->allocreq++;
715       return sym;
716     }
717
718   /* find live ranges with spillocation */
719   if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
720     {
721
722       sym = leastUsedLR (selectS);
723       sym->usl.spillLoc->allocreq++;
724       return sym;
725     }
726
727   /* couldn't find then we need to create a spil
728      location on the stack , for which one? the least
729      used ofcourse */
730   if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
731     {
732
733       /* return a created spil location */
734       sym = createStackSpil (leastUsedLR (selectS));
735       sym->usl.spillLoc->allocreq++;
736       return sym;
737     }
738
739   /* this is an extreme situation we will spill
740      this one : happens very rarely but it does happen */
741   spillThis (forSym);
742   return forSym;
743
744 }
745
746 /*-----------------------------------------------------------------*/
747 /* spilSomething - spil some variable & mark registers as free     */
748 /*-----------------------------------------------------------------*/
749 static bool
750 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
751 {
752   symbol *ssym;
753   int i;
754
755   /* get something we can spil */
756   ssym = selectSpil (ic, ebp, forSym);
757
758   /* mark it as spilt */
759   ssym->isspilt = ssym->spillA = 1;
760   _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
761
762   /* mark it as not register assigned &
763      take it away from the set */
764   bitVectUnSetBit (_G.regAssigned, ssym->key);
765   bitVectUnSetBit (_G.totRegAssigned, ssym->key);
766
767   /* mark the registers as free */
768   for (i = 0; i < ssym->nRegs; i++)
769     if (ssym->regs[i])
770       freeReg (ssym->regs[i]);
771
772   /* if spilt on stack then free up r0 & r1 
773      if they could have been assigned to as gprs */
774   if (!mcs51_ptrRegReq && isSpiltOnStack (ssym))
775     {
776       mcs51_ptrRegReq++;
777       spillLRWithPtrReg (ssym);
778     }
779
780   /* if this was a block level spil then insert push & pop 
781      at the start & end of block respectively */
782   if (ssym->blockSpil)
783     {
784       iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
785       /* add push to the start of the block */
786       addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
787                                     ebp->sch->next : ebp->sch));
788       nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
789       /* add pop to the end of the block */
790       addiCodeToeBBlock (ebp, nic, NULL);
791     }
792
793   /* if spilt because not used in the remainder of the
794      block then add a push before this instruction and
795      a pop at the end of the block */
796   if (ssym->remainSpil)
797     {
798
799       iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
800       /* add push just before this instruction */
801       addiCodeToeBBlock (ebp, nic, ic);
802
803       nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
804       /* add pop to the end of the block */
805       addiCodeToeBBlock (ebp, nic, NULL);
806     }
807
808   if (ssym == forSym)
809     return FALSE;
810   else
811     return TRUE;
812 }
813
814 /*-----------------------------------------------------------------*/
815 /* getRegPtr - will try for PTR if not a GPR type if not spil      */
816 /*-----------------------------------------------------------------*/
817 static regs *
818 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
819 {
820   regs *reg;
821
822 tryAgain:
823   /* try for a ptr type */
824   if ((reg = allocReg (REG_PTR)))
825     return reg;
826
827   /* try for gpr type */
828   if ((reg = allocReg (REG_GPR)))
829     return reg;
830
831   /* we have to spil */
832   if (!spilSomething (ic, ebp, sym))
833     return NULL;
834
835   /* this looks like an infinite loop but 
836      in really selectSpil will abort  */
837   goto tryAgain;
838 }
839
840 /*-----------------------------------------------------------------*/
841 /* getRegGpr - will try for GPR if not spil                        */
842 /*-----------------------------------------------------------------*/
843 static regs *
844 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
845 {
846   regs *reg;
847
848 tryAgain:
849   /* try for gpr type */
850   if ((reg = allocReg (REG_GPR)))
851     return reg;
852
853   if (!mcs51_ptrRegReq)
854     if ((reg = allocReg (REG_PTR)))
855       return reg;
856
857   /* we have to spil */
858   if (!spilSomething (ic, ebp, sym))
859     return NULL;
860
861   /* this looks like an infinite loop but 
862      in really selectSpil will abort  */
863   goto tryAgain;
864 }
865
866 /*-----------------------------------------------------------------*/
867 /* getRegPtrNoSpil - get it cannot split                           */
868 /*-----------------------------------------------------------------*/
869 static regs *getRegPtrNoSpil()
870 {
871   regs *reg;
872
873   /* try for a ptr type */
874   if ((reg = allocReg (REG_PTR)))
875     return reg;
876
877   /* try for gpr type */
878   if ((reg = allocReg (REG_GPR)))
879     return reg;
880
881   assert(0);
882 }
883
884 /*-----------------------------------------------------------------*/
885 /* getRegGprNoSpil - get it cannot split                           */
886 /*-----------------------------------------------------------------*/
887 static regs *getRegGprNoSpil()
888 {
889
890   regs *reg;
891   if ((reg = allocReg (REG_GPR)))
892     return reg;
893
894   if (!mcs51_ptrRegReq)
895     if ((reg = allocReg (REG_PTR)))
896       return reg;
897
898   assert(0);
899 }
900
901 /*-----------------------------------------------------------------*/
902 /* symHasReg - symbol has a given register                         */
903 /*-----------------------------------------------------------------*/
904 static bool
905 symHasReg (symbol * sym, regs * reg)
906 {
907   int i;
908
909   for (i = 0; i < sym->nRegs; i++)
910     if (sym->regs[i] == reg)
911       return TRUE;
912
913   return FALSE;
914 }
915
916 /*-----------------------------------------------------------------*/
917 /* deassignLRs - check the live to and if they have registers & are */
918 /*               not spilt then free up the registers              */
919 /*-----------------------------------------------------------------*/
920 static void
921 deassignLRs (iCode * ic, eBBlock * ebp)
922 {
923   symbol *sym;
924   int k;
925   symbol *result;
926
927   for (sym = hTabFirstItem (liveRanges, &k); sym;
928        sym = hTabNextItem (liveRanges, &k))
929     {
930
931       symbol *psym = NULL;
932       /* if it does not end here */
933       if (sym->liveTo > ic->seq)
934         continue;
935
936       /* if it was spilt on stack then we can 
937          mark the stack spil location as free */
938       if (sym->isspilt)
939         {
940           if (sym->stackSpil)
941             {
942               sym->usl.spillLoc->isFree = 1;
943               sym->stackSpil = 0;
944             }
945           continue;
946         }
947
948       if (!bitVectBitValue (_G.regAssigned, sym->key))
949         continue;
950
951       /* special case check if this is an IFX &
952          the privious one was a pop and the 
953          previous one was not spilt then keep track
954          of the symbol */
955       if (ic->op == IFX && ic->prev &&
956           ic->prev->op == IPOP &&
957           !ic->prev->parmPush &&
958           !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
959         psym = OP_SYMBOL (IC_LEFT (ic->prev));
960
961       if (sym->nRegs)
962         {
963           int i = 0;
964
965           bitVectUnSetBit (_G.regAssigned, sym->key);
966
967           /* if the result of this one needs registers
968              and does not have it then assign it right
969              away */
970           if (IC_RESULT (ic) &&
971               !(SKIP_IC2 (ic) ||        /* not a special icode */
972                 ic->op == JUMPTABLE ||
973                 ic->op == IFX ||
974                 ic->op == IPUSH ||
975                 ic->op == IPOP ||
976                 ic->op == RETURN ||
977                 POINTER_SET (ic)) &&
978               (result = OP_SYMBOL (IC_RESULT (ic))) &&  /* has a result */
979               result->liveTo > ic->seq &&       /* and will live beyond this */
980               result->liveTo <= ebp->lSeq &&    /* does not go beyond this block */
981               result->regType == sym->regType &&        /* same register types */
982               result->nRegs &&  /* which needs registers */
983               !result->isspilt &&       /* and does not already have them */
984               !result->remat &&
985               !bitVectBitValue (_G.regAssigned, result->key) &&
986           /* the number of free regs + number of regs in this LR
987              can accomodate the what result Needs */
988               ((nfreeRegsType (result->regType) +
989                 sym->nRegs) >= result->nRegs)
990             )
991             {
992
993               for (i = 0; i < result->nRegs; i++)
994                 if (i < sym->nRegs)
995                   result->regs[i] = sym->regs[i];
996                 else
997                   result->regs[i] = getRegGpr (ic, ebp, result);
998
999               _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
1000               _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
1001
1002             }
1003
1004           /* free the remaining */
1005           for (; i < sym->nRegs; i++)
1006             {
1007               if (psym)
1008                 {
1009                   if (!symHasReg (psym, sym->regs[i]))
1010                     freeReg (sym->regs[i]);
1011                 }
1012               else
1013                 freeReg (sym->regs[i]);
1014             }
1015         }
1016     }
1017 }
1018
1019
1020 /*-----------------------------------------------------------------*/
1021 /* reassignLR - reassign this to registers                         */
1022 /*-----------------------------------------------------------------*/
1023 static void
1024 reassignLR (operand * op)
1025 {
1026   symbol *sym = OP_SYMBOL (op);
1027   int i;
1028
1029   /* not spilt any more */
1030   sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1031   bitVectUnSetBit (_G.spiltSet, sym->key);
1032
1033   _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1034   _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1035
1036   _G.blockSpil--;
1037
1038   for (i = 0; i < sym->nRegs; i++)
1039     sym->regs[i]->isFree = 0;
1040 }
1041
1042 /*-----------------------------------------------------------------*/
1043 /* willCauseSpill - determines if allocating will cause a spill    */
1044 /*-----------------------------------------------------------------*/
1045 static int
1046 willCauseSpill (int nr, int rt)
1047 {
1048   /* first check if there are any avlb registers
1049      of te type required */
1050   if (rt == REG_PTR)
1051     {
1052       /* special case for pointer type 
1053          if pointer type not avlb then 
1054          check for type gpr */
1055       if (nFreeRegs (rt) >= nr)
1056         return 0;
1057       if (nFreeRegs (REG_GPR) >= nr)
1058         return 0;
1059     }
1060   else
1061     {
1062       if (mcs51_ptrRegReq)
1063         {
1064           if (nFreeRegs (rt) >= nr)
1065             return 0;
1066         }
1067       else
1068         {
1069           if (nFreeRegs (REG_PTR) +
1070               nFreeRegs (REG_GPR) >= nr)
1071             return 0;
1072         }
1073     }
1074
1075   /* it will cause a spil */
1076   return 1;
1077 }
1078
1079 /*-----------------------------------------------------------------*/
1080 /* positionRegs - the allocator can allocate same registers to res- */
1081 /* ult and operand, if this happens make sure they are in the same */
1082 /* position as the operand otherwise chaos results                 */
1083 /*-----------------------------------------------------------------*/
1084 static int
1085 positionRegs (symbol * result, symbol * opsym)
1086 {
1087   int count = min (result->nRegs, opsym->nRegs);
1088   int i, j = 0, shared = 0;
1089   int change = 0;
1090
1091   /* if the result has been spilt then cannot share */
1092   if (opsym->isspilt)
1093     return 0;
1094 again:
1095   shared = 0;
1096   /* first make sure that they actually share */
1097   for (i = 0; i < count; i++)
1098     {
1099       for (j = 0; j < count; j++)
1100         {
1101           if (result->regs[i] == opsym->regs[j] && i != j)
1102             {
1103               shared = 1;
1104               goto xchgPositions;
1105             }
1106         }
1107     }
1108 xchgPositions:
1109   if (shared)
1110     {
1111       regs *tmp = result->regs[i];
1112       result->regs[i] = result->regs[j];
1113       result->regs[j] = tmp;
1114       change ++;
1115       goto again;
1116     }
1117   return change;
1118 }
1119
1120 /*-----------------------------------------------------------------*/
1121 /* serialRegAssign - serially allocate registers to the variables  */
1122 /*-----------------------------------------------------------------*/
1123 static void
1124 serialRegAssign (eBBlock ** ebbs, int count)
1125 {
1126     int i;
1127
1128     /* for all blocks */
1129     for (i = 0; i < count; i++) {
1130
1131         iCode *ic;
1132
1133         if (ebbs[i]->noPath &&
1134             (ebbs[i]->entryLabel != entryLabel &&
1135              ebbs[i]->entryLabel != returnLabel))
1136             continue;
1137
1138         /* of all instructions do */
1139         for (ic = ebbs[i]->sch; ic; ic = ic->next) {
1140
1141             /* if this is an ipop that means some live
1142                range will have to be assigned again */
1143             if (ic->op == IPOP)
1144                 reassignLR (IC_LEFT (ic));
1145
1146             /* if result is present && is a true symbol */
1147             if (IC_RESULT (ic) && ic->op != IFX &&
1148                 IS_TRUE_SYMOP (IC_RESULT (ic)))
1149                 OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1150
1151             /* take away registers from live
1152                ranges that end at this instruction */
1153             deassignLRs (ic, ebbs[i]);
1154
1155             /* some don't need registers */
1156             if (SKIP_IC2 (ic) ||
1157                 ic->op == JUMPTABLE ||
1158                 ic->op == IFX ||
1159                 ic->op == IPUSH ||
1160                 ic->op == IPOP ||
1161                 (IC_RESULT (ic) && POINTER_SET (ic)))
1162                 continue;
1163
1164             /* now we need to allocate registers
1165                only for the result */
1166             if (IC_RESULT (ic)) {
1167                 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1168                 bitVect *spillable;
1169                 int willCS;
1170                 int j;
1171                 int ptrRegSet = 0;
1172
1173                 /* if it does not need or is spilt 
1174                    or is already assigned to registers
1175                    or will not live beyond this instructions */
1176                 if (!sym->nRegs ||
1177                     sym->isspilt ||
1178                     bitVectBitValue (_G.regAssigned, sym->key) ||
1179                     sym->liveTo <= ic->seq)
1180                     continue;
1181
1182                 /* if some liverange has been spilt at the block level
1183                    and this one live beyond this block then spil this
1184                    to be safe */
1185                 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq) {
1186                     spillThis (sym);
1187                     continue;
1188                 }
1189                 /* if trying to allocate this will cause
1190                    a spill and there is nothing to spill 
1191                    or this one is rematerializable then
1192                    spill this one */
1193                 willCS = willCauseSpill (sym->nRegs, sym->regType);
1194                 spillable = computeSpillable (ic);
1195                 if (sym->remat || (willCS && bitVectIsZero (spillable))) {                    
1196                     spillThis (sym);
1197                     continue;                 
1198                 }
1199
1200                 /* if it has a spillocation & is used less than
1201                    all other live ranges then spill this */
1202                 if (willCS) {
1203                     if (sym->usl.spillLoc) {
1204                         symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1205                                                                          allLRs, ebbs[i], ic));
1206                         if (leastUsed && leastUsed->used > sym->used) {
1207                             spillThis (sym);
1208                             continue;
1209                         }
1210                     } else {
1211                         /* if none of the liveRanges have a spillLocation then better
1212                            to spill this one than anything else already assigned to registers */
1213                         if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1214                             /* if this is local to this block then we might find a block spil */
1215                             if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1216                                 spillThis (sym);
1217                                 continue;
1218                             }
1219                         }
1220                     }
1221                 }
1222                 /* if we need ptr regs for the right side
1223                    then mark it */
1224                 if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1225                     && getSize (OP_SYMBOL (IC_LEFT (ic))->type) <= (unsigned int) PTRSIZE) {
1226                     mcs51_ptrRegReq++;
1227                     ptrRegSet = 1;
1228                 }
1229                 /* else we assign registers to it */
1230                 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1231                 _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1232
1233                 for (j = 0; j < sym->nRegs; j++) {
1234                     if (sym->regType == REG_PTR)
1235                         sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1236                     else
1237                         sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1238
1239                     /* if the allocation failed which means
1240                        this was spilt then break */
1241                     if (!sym->regs[j]) {
1242                       if (j) {
1243                         fprintf (stderr, "%d reg(s) lost in %s:%d\n",
1244                                  j, __FILE__,__LINE__);
1245                       }
1246                       break;
1247                     }
1248                 }
1249
1250                 /* if it shares registers with operands make sure
1251                    that they are in the same position */
1252                 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1253                     OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=') {
1254                     positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1255                                   OP_SYMBOL (IC_LEFT (ic)));
1256                 }
1257                 /* do the same for the right operand */
1258                 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1259                     OP_SYMBOL (IC_RIGHT (ic))->nRegs) {
1260                     positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1261                                   OP_SYMBOL (IC_RIGHT (ic)));
1262                 }
1263
1264                 if (ptrRegSet) {
1265                     mcs51_ptrRegReq--;
1266                     ptrRegSet = 0;
1267                 }
1268
1269             }
1270         }
1271     }
1272 }
1273
1274 /*-----------------------------------------------------------------*/
1275 /* fillGaps - Try to fill in the Gaps left by Pass1                */
1276 /*-----------------------------------------------------------------*/
1277 static void fillGaps()
1278 {
1279     symbol *sym =NULL;
1280     int key =0;    
1281     
1282     if (getenv("DISABLE_FILL_GAPS")) return;
1283     
1284     /* look for livernages that was spilt by the allocator */
1285     for (sym = hTabFirstItem(liveRanges,&key) ; sym ; 
1286          sym = hTabNextItem(liveRanges,&key)) {
1287
1288         int i;
1289         int pdone = 0;
1290
1291         if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1292
1293         /* find the liveRanges this one clashes with, that are
1294            still assigned to registers & mark the registers as used*/
1295         for ( i = 0 ; i < sym->clashes->size ; i ++) {
1296             int k;
1297             symbol *clr;
1298
1299             if (bitVectBitValue(sym->clashes,i) == 0 ||    /* those that clash with this */
1300                 bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1301                 continue ;
1302
1303             assert (clr = hTabItemWithKey(liveRanges,i));
1304          
1305             /* mark these registers as used */
1306             for (k = 0 ; k < clr->nRegs ; k++ ) 
1307                 useReg(clr->regs[k]);
1308         }
1309
1310         if (willCauseSpill(sym->nRegs,sym->regType)) {
1311             /* NOPE :( clear all registers & and continue */
1312             freeAllRegs();
1313             continue ;
1314         }
1315
1316         /* THERE IS HOPE !!!! */
1317         for (i=0; i < sym->nRegs ; i++ ) {
1318                 sym->regs[i] = getRegGprNoSpil ();                
1319         }
1320
1321         /* for all its definitions check if the registers
1322            allocated needs positioning NOTE: we can position
1323            only ONCE if more than One positioning required 
1324            then give up */
1325         sym->isspilt = 0;
1326         for (i = 0 ; i < sym->defs->size ; i++ ) {
1327             if (bitVectBitValue(sym->defs,i)) {
1328                 iCode *ic;
1329                 if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1330                 if (SKIP_IC(ic)) continue;
1331                 assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1332                 /* if left is assigned to registers */
1333                 if (IS_SYMOP(IC_LEFT(ic)) && 
1334                     bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1335                     pdone += positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)));
1336                 }
1337                 if (IS_SYMOP(IC_RIGHT(ic)) && 
1338                     bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1339                     pdone += positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)));
1340                 }
1341                 if (pdone > 1) break;
1342             }
1343         }
1344         /* had to position more than once GIVE UP */
1345         if (pdone > 1) {
1346             /* UNDO all the changes we made to try this */
1347             sym->isspilt = 0;
1348             for (i=0; i < sym->nRegs ; i++ ) {
1349                 if (sym->regType == REG_PTR)
1350                     sym->regs[i] = getRegPtrNoSpil ();
1351                 else
1352                     sym->regs[i] = getRegGprNoSpil ();
1353             }
1354             freeAllRegs();
1355             D(printf ("Fill Gap gave up due to positioning for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1356             continue ;      
1357         }
1358         D(printf ("FILLED GAP for %s in function %s\n",sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1359         _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1360         sym->isspilt = sym->spillA = 0 ;
1361         sym->usl.spillLoc->allocreq--;
1362         freeAllRegs();
1363     }
1364 }
1365
1366 /*-----------------------------------------------------------------*/
1367 /* rUmaskForOp :- returns register mask for an operand             */
1368 /*-----------------------------------------------------------------*/
1369 bitVect *
1370 mcs51_rUmaskForOp (operand * op)
1371 {
1372   bitVect *rumask;
1373   symbol *sym;
1374   int j;
1375
1376   /* only temporaries are assigned registers */
1377   if (!IS_ITEMP (op))
1378     return NULL;
1379
1380   sym = OP_SYMBOL (op);
1381
1382   /* if spilt or no registers assigned to it
1383      then nothing */
1384   if (sym->isspilt || !sym->nRegs)
1385     return NULL;
1386
1387   rumask = newBitVect (mcs51_nRegs);
1388
1389   for (j = 0; j < sym->nRegs; j++)
1390     {
1391       rumask = bitVectSetBit (rumask,
1392                               sym->regs[j]->rIdx);
1393     }
1394
1395   return rumask;
1396 }
1397
1398 /*-----------------------------------------------------------------*/
1399 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1400 /*-----------------------------------------------------------------*/
1401 static bitVect *
1402 regsUsedIniCode (iCode * ic)
1403 {
1404   bitVect *rmask = newBitVect (mcs51_nRegs);
1405
1406   /* do the special cases first */
1407   if (ic->op == IFX)
1408     {
1409       rmask = bitVectUnion (rmask,
1410                             mcs51_rUmaskForOp (IC_COND (ic)));
1411       goto ret;
1412     }
1413
1414   /* for the jumptable */
1415   if (ic->op == JUMPTABLE)
1416     {
1417       rmask = bitVectUnion (rmask,
1418                             mcs51_rUmaskForOp (IC_JTCOND (ic)));
1419
1420       goto ret;
1421     }
1422
1423   /* of all other cases */
1424   if (IC_LEFT (ic))
1425     rmask = bitVectUnion (rmask,
1426                           mcs51_rUmaskForOp (IC_LEFT (ic)));
1427
1428
1429   if (IC_RIGHT (ic))
1430     rmask = bitVectUnion (rmask,
1431                           mcs51_rUmaskForOp (IC_RIGHT (ic)));
1432
1433   if (IC_RESULT (ic))
1434     rmask = bitVectUnion (rmask,
1435                           mcs51_rUmaskForOp (IC_RESULT (ic)));
1436
1437 ret:
1438   return rmask;
1439 }
1440
1441 /*-----------------------------------------------------------------*/
1442 /* createRegMask - for each instruction will determine the regsUsed */
1443 /*-----------------------------------------------------------------*/
1444 static void
1445 createRegMask (eBBlock ** ebbs, int count)
1446 {
1447   int i;
1448
1449   /* for all blocks */
1450   for (i = 0; i < count; i++)
1451     {
1452       iCode *ic;
1453
1454       if (ebbs[i]->noPath &&
1455           (ebbs[i]->entryLabel != entryLabel &&
1456            ebbs[i]->entryLabel != returnLabel))
1457         continue;
1458
1459       /* for all instructions */
1460       for (ic = ebbs[i]->sch; ic; ic = ic->next)
1461         {
1462
1463           int j;
1464
1465           if (SKIP_IC2 (ic) || !ic->rlive)
1466             continue;
1467
1468           /* first mark the registers used in this
1469              instruction */
1470           ic->rUsed = regsUsedIniCode (ic);
1471           _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1472
1473           /* now create the register mask for those 
1474              registers that are in use : this is a
1475              super set of ic->rUsed */
1476           ic->rMask = newBitVect (mcs51_nRegs + 1);
1477
1478           /* for all live Ranges alive at this point */
1479           for (j = 1; j < ic->rlive->size; j++)
1480             {
1481               symbol *sym;
1482               int k;
1483
1484               /* if not alive then continue */
1485               if (!bitVectBitValue (ic->rlive, j))
1486                 continue;
1487
1488               /* find the live range we are interested in */
1489               if (!(sym = hTabItemWithKey (liveRanges, j)))
1490                 {
1491                   werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1492                           "createRegMask cannot find live range");
1493                   exit (0);
1494                 }
1495
1496               /* if no register assigned to it */
1497               if (!sym->nRegs || sym->isspilt)
1498                 continue;
1499
1500               /* for all the registers allocated to it */
1501               for (k = 0; k < sym->nRegs; k++)
1502                 if (sym->regs[k])
1503                   ic->rMask =
1504                     bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1505             }
1506         }
1507     }
1508 }
1509
1510 /*-----------------------------------------------------------------*/
1511 /* rematStr - returns the rematerialized string for a remat var    */
1512 /*-----------------------------------------------------------------*/
1513 static char *
1514 rematStr (symbol * sym)
1515 {
1516   char *s = buffer;
1517   iCode *ic = sym->rematiCode;
1518
1519   while (1)
1520     {
1521
1522       /* if plus or minus print the right hand side */
1523       if (ic->op == '+' || ic->op == '-')
1524         {
1525           sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1526                    ic->op);
1527           s += strlen (s);
1528           ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1529           continue;
1530         }
1531
1532       /* cast then continue */
1533       if (IS_CAST_ICODE(ic)) {
1534           ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1535           continue;
1536       }
1537       /* we reached the end */
1538       sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1539       break;
1540     }
1541
1542   return buffer;
1543 }
1544
1545 /*-----------------------------------------------------------------*/
1546 /* regTypeNum - computes the type & number of registers required   */
1547 /*-----------------------------------------------------------------*/
1548 static void
1549 regTypeNum (eBBlock *ebbs)
1550 {
1551   symbol *sym;
1552   int k;
1553   iCode *ic;
1554
1555   /* for each live range do */
1556   for (sym = hTabFirstItem (liveRanges, &k); sym;
1557        sym = hTabNextItem (liveRanges, &k))
1558     {
1559
1560       /* if used zero times then no registers needed */
1561       if ((sym->liveTo - sym->liveFrom) == 0)
1562         continue;
1563
1564
1565       /* if the live range is a temporary */
1566       if (sym->isitmp)
1567         {
1568
1569           /* if the type is marked as a conditional */
1570           if (sym->regType == REG_CND)
1571             continue;
1572
1573           /* if used in return only then we don't 
1574              need registers */
1575           if (sym->ruonly || sym->accuse)
1576             {
1577               if (IS_AGGREGATE (sym->type) || sym->isptr)
1578                 sym->type = aggrToPtr (sym->type, FALSE);
1579               continue;
1580             }
1581
1582           /* if the symbol has only one definition &
1583              that definition is a get_pointer and the
1584              pointer we are getting is rematerializable and
1585              in "data" space */
1586
1587           if (bitVectnBitsOn (sym->defs) == 1 &&
1588               (ic = hTabItemWithKey (iCodehTab,
1589                                      bitVectFirstBit (sym->defs))) &&
1590               POINTER_GET (ic) &&
1591               !sym->noSpilLoc &&
1592               !IS_BITVAR (sym->etype))
1593             {
1594
1595
1596               /* if remat in data space */
1597               if (OP_SYMBOL (IC_LEFT (ic))->remat &&
1598                   !IS_CAST_ICODE(OP_SYMBOL (IC_LEFT (ic))->rematiCode) &&
1599                   DCL_TYPE (aggrToPtr (sym->type, FALSE)) == POINTER)
1600                 {
1601                   /* create a psuedo symbol & force a spil */
1602                   symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
1603                   psym->type = sym->type;
1604                   psym->etype = sym->etype;
1605                   strcpy (psym->rname, psym->name);
1606                   sym->isspilt = 1;
1607                   sym->usl.spillLoc = psym;
1608 #if 0 // an alternative fix for bug #480076
1609                   /* now this is a useless assignment to itself */
1610                   remiCodeFromeBBlock (ebbs, ic);
1611 #else
1612                   /* now this really is an assignment to itself, make it so;
1613                      it will be optimized out later */
1614                   ic->op='=';
1615                   IC_RIGHT(ic)=IC_RESULT(ic);
1616                   IC_LEFT(ic)=NULL;
1617 #endif
1618                   continue;
1619                 }
1620
1621               /* if in data space or idata space then try to
1622                  allocate pointer register */
1623
1624             }
1625
1626           /* if not then we require registers */
1627           sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1628                         getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1629                         getSize (sym->type));
1630
1631           if (sym->nRegs > 4)
1632             {
1633               fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1634               printTypeChain (sym->type, stderr);
1635               fprintf (stderr, "\n");
1636             }
1637
1638           /* determine the type of register required */
1639           if (sym->nRegs == 1 &&
1640               IS_PTR (sym->type) &&
1641               sym->uptr)
1642             sym->regType = REG_PTR;
1643           else
1644             sym->regType = REG_GPR;
1645
1646         }
1647       else
1648         /* for the first run we don't provide */
1649         /* registers for true symbols we will */
1650         /* see how things go                  */
1651         sym->nRegs = 0;
1652     }
1653
1654 }
1655
1656 /*-----------------------------------------------------------------*/
1657 /* freeAllRegs - mark all registers as free                        */
1658 /*-----------------------------------------------------------------*/
1659 static void
1660 freeAllRegs ()
1661 {
1662   int i;
1663
1664   for (i = 0; i < mcs51_nRegs; i++)
1665     regs8051[i].isFree = 1;
1666 }
1667
1668 /*-----------------------------------------------------------------*/
1669 /* deallocStackSpil - this will set the stack pointer back         */
1670 /*-----------------------------------------------------------------*/
1671 static
1672 DEFSETFUNC (deallocStackSpil)
1673 {
1674   symbol *sym = item;
1675
1676   deallocLocal (sym);
1677   return 0;
1678 }
1679
1680 /*-----------------------------------------------------------------*/
1681 /* farSpacePackable - returns the packable icode for far variables */
1682 /*-----------------------------------------------------------------*/
1683 static iCode *
1684 farSpacePackable (iCode * ic)
1685 {
1686   iCode *dic;
1687
1688   /* go thru till we find a definition for the
1689      symbol on the right */
1690   for (dic = ic->prev; dic; dic = dic->prev)
1691     {
1692       /* if the definition is a call then no */
1693       if ((dic->op == CALL || dic->op == PCALL) &&
1694           IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1695         {
1696           return NULL;
1697         }
1698
1699       /* if shift by unknown amount then not */
1700       if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1701           IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1702         return NULL;
1703
1704       /* if pointer get and size > 1 */
1705       if (POINTER_GET (dic) &&
1706           getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1707         return NULL;
1708
1709       if (POINTER_SET (dic) &&
1710           getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1711         return NULL;
1712
1713       /* if any three is a true symbol in far space */
1714       if (IC_RESULT (dic) &&
1715           IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1716           isOperandInFarSpace (IC_RESULT (dic)))
1717         return NULL;
1718
1719       if (IC_RIGHT (dic) &&
1720           IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1721           isOperandInFarSpace (IC_RIGHT (dic)) &&
1722           !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1723         return NULL;
1724
1725       if (IC_LEFT (dic) &&
1726           IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1727           isOperandInFarSpace (IC_LEFT (dic)) &&
1728           !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1729         return NULL;
1730
1731       if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1732         {
1733           if ((dic->op == LEFT_OP ||
1734                dic->op == RIGHT_OP ||
1735                dic->op == '-') &&
1736               IS_OP_LITERAL (IC_RIGHT (dic)))
1737             return NULL;
1738           else
1739             return dic;
1740         }
1741     }
1742
1743   return NULL;
1744 }
1745
1746 /*-----------------------------------------------------------------*/
1747 /* packRegsForAssign - register reduction for assignment           */
1748 /*-----------------------------------------------------------------*/
1749 static int
1750 packRegsForAssign (iCode * ic, eBBlock * ebp)
1751 {
1752   iCode *dic, *sic;
1753   //sym_link *etype = operandType (IC_RIGHT (ic));
1754
1755   if (!IS_ITEMP (IC_RIGHT (ic)) ||
1756       OP_SYMBOL (IC_RIGHT (ic))->isind ||
1757       OP_LIVETO (IC_RIGHT (ic)) > ic->seq
1758       /* why? || IS_BITFIELD (etype) */ )
1759     {
1760       return 0;
1761     }
1762
1763   /* if the true symbol is defined in far space or on stack
1764      then we should not since this will increase register pressure */
1765   if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1766     return 0;
1767   }
1768
1769   /* find the definition of iTempNN scanning backwards if we find a 
1770      a use of the true symbol in before we find the definition then 
1771      we cannot */
1772   for (dic = ic->prev; dic; dic = dic->prev)
1773     {
1774       /* if there is a function call then don't pack it */
1775       if ((dic->op == CALL || dic->op == PCALL))
1776         {
1777           dic = NULL;
1778           break;
1779         }
1780
1781       if (SKIP_IC2 (dic))
1782         continue;
1783
1784       if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1785           IS_OP_VOLATILE (IC_RESULT (dic)))
1786         {
1787           dic = NULL;
1788           break;
1789         }
1790
1791       if (IS_SYMOP (IC_RESULT (dic)) &&
1792           IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1793         {
1794           if (POINTER_SET (dic))
1795             dic = NULL;
1796
1797           break;
1798         }
1799
1800       if (IS_SYMOP (IC_RIGHT (dic)) &&
1801           (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1802            IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1803         {
1804           dic = NULL;
1805           break;
1806         }
1807
1808       if (IS_SYMOP (IC_LEFT (dic)) &&
1809           (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1810            IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1811         {
1812           dic = NULL;
1813           break;
1814         }
1815
1816       if (POINTER_SET (dic) &&
1817           IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1818         {
1819           dic = NULL;
1820           break;
1821         }
1822     }
1823
1824   if (!dic)
1825     return 0;                   /* did not find */
1826
1827   /* if assignment then check that right is not a bit */
1828   if (ASSIGNMENT (dic) && !POINTER_SET (dic))
1829     {
1830       sym_link *etype = operandType (IC_RIGHT (dic));
1831       if (IS_BITFIELD (etype))
1832         return 0;
1833     }
1834   /* if the result is on stack or iaccess then it must be
1835      the same atleast one of the operands */
1836   if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1837       OP_SYMBOL (IC_RESULT (ic))->iaccess)
1838     {
1839
1840       /* the operation has only one symbol
1841          operator then we can pack */
1842       if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1843           (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1844         goto pack;
1845
1846       if (!((IC_LEFT (dic) &&
1847              IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1848             (IC_RIGHT (dic) &&
1849              IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1850         return 0;
1851     }
1852 pack:
1853   /* found the definition */
1854   /* replace the result with the result of */
1855   /* this assignment and remove this assignment */
1856   bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
1857   IC_RESULT (dic) = IC_RESULT (ic);
1858
1859   if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1860     {
1861       OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1862     }
1863   /* delete from liverange table also 
1864      delete from all the points inbetween and the new
1865      one */
1866   for (sic = dic; sic != ic; sic = sic->next)
1867     {
1868       bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1869       if (IS_ITEMP (IC_RESULT (dic)))
1870         bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1871     }
1872
1873   remiCodeFromeBBlock (ebp, ic);
1874   bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
1875   hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
1876   OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
1877   return 1;
1878
1879 }
1880
1881 /*-----------------------------------------------------------------*/
1882 /* findAssignToSym : scanning backwards looks for first assig found */
1883 /*-----------------------------------------------------------------*/
1884 static iCode *
1885 findAssignToSym (operand * op, iCode * ic)
1886 {
1887   iCode *dic;
1888
1889   for (dic = ic->prev; dic; dic = dic->prev)
1890     {
1891
1892       /* if definition by assignment */
1893       if (dic->op == '=' &&
1894           !POINTER_SET (dic) &&
1895           IC_RESULT (dic)->key == op->key
1896 /*          &&  IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1897         )
1898         {
1899
1900           /* we are interested only if defined in far space */
1901           /* or in stack space in case of + & - */
1902
1903           /* if assigned to a non-symbol then return
1904              FALSE */
1905           if (!IS_SYMOP (IC_RIGHT (dic)))
1906             return NULL;
1907
1908           /* if the symbol is in far space then
1909              we should not */
1910           if (isOperandInFarSpace (IC_RIGHT (dic)))
1911             return NULL;
1912
1913           /* for + & - operations make sure that
1914              if it is on the stack it is the same
1915              as one of the three operands */
1916           if ((ic->op == '+' || ic->op == '-') &&
1917               OP_SYMBOL (IC_RIGHT (dic))->onStack)
1918             {
1919
1920               if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
1921                   IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
1922                   IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
1923                 return NULL;
1924             }
1925
1926           break;
1927
1928         }
1929
1930       /* if we find an usage then we cannot delete it */
1931       if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1932         return NULL;
1933
1934       if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1935         return NULL;
1936
1937       if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1938         return NULL;
1939     }
1940
1941   /* now make sure that the right side of dic
1942      is not defined between ic & dic */
1943   if (dic)
1944     {
1945       iCode *sic = dic->next;
1946
1947       for (; sic != ic; sic = sic->next)
1948         if (IC_RESULT (sic) &&
1949             IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
1950           return NULL;
1951     }
1952
1953   return dic;
1954
1955
1956 }
1957
1958 /*-----------------------------------------------------------------*/
1959 /* packRegsForSupport :- reduce some registers for support calls   */
1960 /*-----------------------------------------------------------------*/
1961 static int
1962 packRegsForSupport (iCode * ic, eBBlock * ebp)
1963 {
1964   int change = 0;
1965   iCode *dic, *sic;
1966
1967   /* for the left & right operand :- look to see if the
1968      left was assigned a true symbol in far space in that
1969      case replace them */
1970
1971   if (IS_ITEMP (IC_LEFT (ic)) &&
1972       OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
1973     {
1974       dic = findAssignToSym (IC_LEFT (ic), ic);
1975
1976       if (!dic)
1977         goto right;
1978
1979       /* found it we need to remove it from the
1980          block */
1981       for (sic = dic; sic != ic; sic = sic->next)
1982         bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
1983
1984       OP_SYMBOL(IC_LEFT (ic))=OP_SYMBOL(IC_RIGHT (dic));
1985       IC_LEFT (ic)->key = OP_SYMBOL(IC_RIGHT (dic))->key;
1986       remiCodeFromeBBlock (ebp, dic);
1987       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
1988       hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
1989       change++;
1990     }
1991
1992   /* do the same for the right operand */
1993  right:
1994   if (!change &&
1995       IS_ITEMP (IC_RIGHT (ic)) &&
1996       OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
1997     {
1998       iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
1999       iCode *sic;
2000
2001       if (!dic)
2002         return change;
2003
2004       /* if this is a subtraction & the result
2005          is a true symbol in far space then don't pack */
2006       if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2007         {
2008           sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2009           if (IN_FARSPACE (SPEC_OCLS (etype)))
2010             return change;
2011         }
2012       /* found it we need to remove it from the
2013          block */
2014       for (sic = dic; sic != ic; sic = sic->next)
2015         bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
2016
2017       IC_RIGHT (ic)->operand.symOperand =
2018         IC_RIGHT (dic)->operand.symOperand;
2019       IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2020
2021       remiCodeFromeBBlock (ebp, dic);
2022       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2023       hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2024       change++;
2025     }
2026
2027   return change;
2028 }
2029
2030 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2031
2032
2033 /*-----------------------------------------------------------------*/
2034 /* packRegsForOneuse : - will reduce some registers for single Use */
2035 /*-----------------------------------------------------------------*/
2036 static iCode *
2037 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
2038 {
2039   bitVect *uses;
2040   iCode *dic, *sic;
2041
2042   /* if returning a literal then do nothing */
2043   if (!IS_SYMOP (op))
2044     return NULL;
2045
2046   /* only upto 2 bytes since we cannot predict
2047      the usage of b, & acc */
2048   if (getSize (operandType (op)) > (fReturnSizeMCS51 - 2))
2049     return NULL;
2050
2051   if (ic->op != RETURN &&
2052       ic->op != SEND &&
2053       !POINTER_SET (ic) &&
2054       !POINTER_GET (ic))
2055     return NULL;
2056   
2057   /* this routine will mark the a symbol as used in one 
2058      instruction use only && if the defintion is local 
2059      (ie. within the basic block) && has only one definition &&
2060      that definiion is either a return value from a 
2061      function or does not contain any variables in
2062      far space */
2063   uses = bitVectCopy (OP_USES (op));
2064   bitVectUnSetBit (uses, ic->key);      /* take away this iCode */
2065   if (!bitVectIsZero (uses))    /* has other uses */
2066     return NULL;
2067
2068   /* if it has only one defintion */
2069   if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2070     return NULL;                /* has more than one definition */
2071
2072   /* get that definition */
2073   if (!(dic =
2074         hTabItemWithKey (iCodehTab,
2075                          bitVectFirstBit (OP_DEFS (op)))))
2076     return NULL;
2077
2078   /* if that only usage is a cast */
2079   if (dic->op == CAST) {
2080     /* to a bigger type */
2081     if (getSize(OP_SYM_TYPE(IC_RESULT(dic))) > 
2082         getSize(OP_SYM_TYPE(IC_RIGHT(dic)))) {
2083       /* than we can not, since we cannot predict the usage of b & acc */
2084       return NULL;
2085     }
2086   }
2087
2088   /* found the definition now check if it is local */
2089   if (dic->seq < ebp->fSeq ||
2090       dic->seq > ebp->lSeq)
2091     return NULL;                /* non-local */
2092
2093   /* now check if it is the return from
2094      a function call */
2095   if (dic->op == CALL || dic->op == PCALL)
2096     {
2097       if (ic->op != SEND && ic->op != RETURN)
2098         {
2099           OP_SYMBOL (op)->ruonly = 1;
2100           return dic;
2101         }
2102       dic = dic->next;
2103     }
2104
2105
2106   /* otherwise check that the definition does
2107      not contain any symbols in far space */
2108   if (isOperandInFarSpace (IC_LEFT (dic)) ||
2109       isOperandInFarSpace (IC_RIGHT (dic)) ||
2110       IS_OP_RUONLY (IC_LEFT (ic)) ||
2111       IS_OP_RUONLY (IC_RIGHT (ic)))
2112     {
2113       return NULL;
2114     }
2115
2116   /* if pointer set then make sure the pointer
2117      is one byte */
2118   if (POINTER_SET (dic) &&
2119       !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2120     return NULL;
2121
2122   if (POINTER_GET (dic) &&
2123       !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2124     return NULL;
2125
2126   sic = dic;
2127
2128   /* also make sure the intervenening instructions
2129      don't have any thing in far space */
2130   for (dic = dic->next; dic && dic != ic && sic != ic; dic = dic->next)
2131     {
2132
2133       /* if there is an intervening function call then no */
2134       if (dic->op == CALL || dic->op == PCALL)
2135         return NULL;
2136       /* if pointer set then make sure the pointer
2137          is one byte */
2138       if (POINTER_SET (dic) &&
2139           !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2140         return NULL;
2141
2142       if (POINTER_GET (dic) &&
2143           !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2144         return NULL;
2145
2146       /* if address of & the result is remat the okay */
2147       if (dic->op == ADDRESS_OF &&
2148           OP_SYMBOL (IC_RESULT (dic))->remat)
2149         continue;
2150
2151       /* if operand has size of three or more & this
2152          operation is a '*','/' or '%' then 'b' may
2153          cause a problem */
2154       if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
2155           getSize (operandType (op)) >= 3)
2156         return NULL;
2157
2158       /* if left or right or result is in far space */
2159       if (isOperandInFarSpace (IC_LEFT (dic)) ||
2160           isOperandInFarSpace (IC_RIGHT (dic)) ||
2161           isOperandInFarSpace (IC_RESULT (dic)) ||
2162           IS_OP_RUONLY (IC_LEFT (dic)) ||
2163           IS_OP_RUONLY (IC_RIGHT (dic)) ||
2164           IS_OP_RUONLY (IC_RESULT (dic)))
2165         {
2166           return NULL;
2167         }
2168       /* if left or right or result is on stack */
2169       if (isOperandOnStack(IC_LEFT(dic)) ||
2170           isOperandOnStack(IC_RIGHT(dic)) ||
2171           isOperandOnStack(IC_RESULT(dic))) {
2172         return NULL;
2173       }
2174     }
2175
2176   OP_SYMBOL (op)->ruonly = 1;
2177   return sic;
2178
2179 }
2180
2181 /*-----------------------------------------------------------------*/
2182 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN          */
2183 /*-----------------------------------------------------------------*/
2184 static bool
2185 isBitwiseOptimizable (iCode * ic)
2186 {
2187   sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2188   sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2189
2190   /* bitwise operations are considered optimizable
2191      under the following conditions (Jean-Louis VERN) 
2192
2193      x & lit
2194      bit & bit
2195      bit & x
2196      bit ^ bit
2197      bit ^ x
2198      x   ^ lit
2199      x   | lit
2200      bit | bit
2201      bit | x
2202   */
2203   if (IS_LITERAL(rtype) ||
2204       (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2205     return TRUE;
2206   else
2207     return FALSE;
2208 }
2209
2210 /*-----------------------------------------------------------------*/
2211 /* packRegsForAccUse - pack registers for acc use                  */
2212 /*-----------------------------------------------------------------*/
2213 static void
2214 packRegsForAccUse (iCode * ic)
2215 {
2216   iCode *uic;
2217
2218   /* if this is an aggregate, e.g. a one byte char array */
2219   if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2220     return;
2221   }
2222
2223   /* if + or - then it has to be one byte result */
2224   if ((ic->op == '+' || ic->op == '-')
2225       && getSize (operandType (IC_RESULT (ic))) > 1)
2226     return;
2227
2228   /* if shift operation make sure right side is not a literal */
2229   if (ic->op == RIGHT_OP &&
2230       (isOperandLiteral (IC_RIGHT (ic)) ||
2231        getSize (operandType (IC_RESULT (ic))) > 1))
2232     return;
2233
2234   if (ic->op == LEFT_OP &&
2235       (isOperandLiteral (IC_RIGHT (ic)) ||
2236        getSize (operandType (IC_RESULT (ic))) > 1))
2237     return;
2238
2239   if (IS_BITWISE_OP (ic) &&
2240       getSize (operandType (IC_RESULT (ic))) > 1)
2241     return;
2242
2243
2244   /* has only one definition */
2245   if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2246     return;
2247
2248   /* has only one use */
2249   if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2250     return;
2251
2252   /* and the usage immediately follows this iCode */
2253   if (!(uic = hTabItemWithKey (iCodehTab,
2254                                bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2255     return;
2256
2257   if (ic->next != uic)
2258     return;
2259
2260   /* if it is a conditional branch then we definitely can */
2261   if (uic->op == IFX)
2262     goto accuse;
2263
2264   if (uic->op == JUMPTABLE)
2265     return;
2266
2267   /* if the usage is not is an assignment
2268      or an arithmetic / bitwise / shift operation then not */
2269   if (POINTER_SET (uic) &&
2270       getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2271     return;
2272
2273   if (uic->op != '=' &&
2274       !IS_ARITHMETIC_OP (uic) &&
2275       !IS_BITWISE_OP (uic) &&
2276       uic->op != LEFT_OP &&
2277       uic->op != RIGHT_OP)
2278     return;
2279
2280   /* if used in ^ operation then make sure right is not a 
2281      literl */
2282   if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2283     return;
2284
2285   /* if shift operation make sure right side is not a literal */
2286   if (uic->op == RIGHT_OP &&
2287       (isOperandLiteral (IC_RIGHT (uic)) ||
2288        getSize (operandType (IC_RESULT (uic))) > 1))
2289     return;
2290
2291   if (uic->op == LEFT_OP &&
2292       (isOperandLiteral (IC_RIGHT (uic)) ||
2293        getSize (operandType (IC_RESULT (uic))) > 1))
2294     return;
2295
2296   /* make sure that the result of this icode is not on the
2297      stack, since acc is used to compute stack offset */
2298 #if 0
2299   if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2300       OP_SYMBOL (IC_RESULT (uic))->onStack)
2301     return;
2302 #else
2303   if (isOperandOnStack(IC_RESULT(uic)))
2304     return;
2305 #endif
2306
2307   /* if either one of them in far space then we cannot */
2308   if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2309        isOperandInFarSpace (IC_LEFT (uic))) ||
2310       (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2311        isOperandInFarSpace (IC_RIGHT (uic))))
2312     return;
2313
2314   /* if the usage has only one operand then we can */
2315   if (IC_LEFT (uic) == NULL ||
2316       IC_RIGHT (uic) == NULL)
2317     goto accuse;
2318
2319   /* make sure this is on the left side if not
2320      a '+' since '+' is commutative */
2321   if (ic->op != '+' &&
2322       IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2323     return;
2324
2325 #if 0
2326   // this is too dangerous and need further restrictions
2327   // see bug #447547
2328
2329   /* if one of them is a literal then we can */
2330   if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2331       (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2332     {
2333       OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2334       return;
2335     }
2336 #endif
2337
2338   /* if the other one is not on stack then we can */
2339   if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2340       (IS_ITEMP (IC_RIGHT (uic)) ||
2341        (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2342         !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
2343     goto accuse;
2344
2345   if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2346       (IS_ITEMP (IC_LEFT (uic)) ||
2347        (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2348         !OP_SYMBOL (IC_LEFT (uic))->onStack)))
2349     goto accuse;
2350
2351   return;
2352
2353 accuse:
2354   OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2355
2356
2357 }
2358
2359 /*-----------------------------------------------------------------*/
2360 /* packForPush - hueristics to reduce iCode for pushing            */
2361 /*-----------------------------------------------------------------*/
2362 static void
2363 packForPush (iCode * ic, eBBlock * ebp)
2364 {
2365   iCode *dic, *lic;
2366   bitVect *dbv;
2367
2368   if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
2369     return;
2370
2371   /* must have only definition & one usage */
2372   if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2373       bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2374     return;
2375
2376   /* find the definition */
2377   if (!(dic = hTabItemWithKey (iCodehTab,
2378                                bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2379     return;
2380
2381   if (dic->op != '=' || POINTER_SET (dic))
2382     return;
2383
2384   /* make sure the right side does not have any definitions
2385      inbetween */
2386   dbv = OP_DEFS(IC_RIGHT(dic));
2387   for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2388     if (bitVectBitValue(dbv,lic->key)) 
2389       return ;
2390   }
2391   /* make sure they have the same type */
2392   {
2393     sym_link *itype=operandType(IC_LEFT(ic));
2394     sym_link *ditype=operandType(IC_RIGHT(dic));
2395
2396     if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2397         SPEC_LONG(itype)!=SPEC_LONG(ditype))
2398       return;
2399   }
2400   /* extend the live range of replaced operand if needed */
2401   if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < ic->seq) {
2402           OP_SYMBOL(IC_RIGHT(dic))->liveTo = ic->seq;
2403   }
2404   /* we now we know that it has one & only one def & use
2405      and the that the definition is an assignment */
2406   IC_LEFT (ic) = IC_RIGHT (dic);
2407    
2408   remiCodeFromeBBlock (ebp, dic);
2409   bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2410   hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2411 }
2412
2413 /*-----------------------------------------------------------------*/
2414 /* packRegisters - does some transformations to reduce register    */
2415 /*                   pressure                                      */
2416 /*-----------------------------------------------------------------*/
2417 static void
2418 packRegisters (eBBlock * ebp)
2419 {
2420   iCode *ic;
2421   int change = 0;
2422
2423   while (1)
2424     {
2425
2426       change = 0;
2427
2428       /* look for assignments of the form */
2429       /* iTempNN = TRueSym (someoperation) SomeOperand */
2430       /*       ....                       */
2431       /* TrueSym := iTempNN:1             */
2432       for (ic = ebp->sch; ic; ic = ic->next)
2433         {
2434           /* find assignment of the form TrueSym := iTempNN:1 */
2435           if (ic->op == '=' && !POINTER_SET (ic))
2436             change += packRegsForAssign (ic, ebp);
2437         }
2438
2439       if (!change)
2440         break;
2441     }
2442
2443   for (ic = ebp->sch; ic; ic = ic->next)
2444     {
2445       /* if this is an itemp & result of an address of a true sym 
2446          then mark this as rematerialisable   */
2447       if (ic->op == ADDRESS_OF &&
2448           IS_ITEMP (IC_RESULT (ic)) &&
2449           IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2450           bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2451           !OP_SYMBOL (IC_LEFT (ic))->onStack)
2452         {
2453
2454           OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2455           OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2456           OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2457
2458         }
2459
2460       /* if straight assignment then carry remat flag if
2461          this is the only definition */
2462       if (ic->op == '=' &&
2463           !POINTER_SET (ic) &&
2464           IS_SYMOP (IC_RIGHT (ic)) &&
2465           OP_SYMBOL (IC_RIGHT (ic))->remat &&
2466           !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2467           bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2468         {
2469
2470           OP_SYMBOL (IC_RESULT (ic))->remat =
2471             OP_SYMBOL (IC_RIGHT (ic))->remat;
2472           OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2473             OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2474         }
2475
2476       /* if cast to a generic pointer & the pointer being
2477          cast is remat, then we can remat this cast as well */
2478       if (ic->op == CAST && 
2479           IS_SYMOP(IC_RIGHT(ic)) &&
2480           OP_SYMBOL(IC_RIGHT(ic))->remat ) {
2481               sym_link *to_type = operandType(IC_LEFT(ic));
2482               sym_link *from_type = operandType(IC_RIGHT(ic));
2483               if (IS_GENPTR(to_type) && IS_PTR(from_type)) {                  
2484                       OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2485                       OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2486                       OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2487               }
2488       }
2489
2490       /* if this is a +/- operation with a rematerizable 
2491          then mark this as rematerializable as well */
2492       if ((ic->op == '+' || ic->op == '-') &&
2493           (IS_SYMOP (IC_LEFT (ic)) &&
2494            IS_ITEMP (IC_RESULT (ic)) &&
2495            IS_OP_LITERAL (IC_RIGHT (ic))) &&
2496            OP_SYMBOL (IC_LEFT (ic))->remat &&
2497           (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
2498            bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1)
2499         {
2500           OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2501           OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2502           OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2503         }
2504
2505       /* mark the pointer usages */
2506       if (POINTER_SET (ic))
2507         OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2508
2509       if (POINTER_GET (ic))
2510         OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2511
2512       if (!SKIP_IC2 (ic))
2513         {
2514           /* if we are using a symbol on the stack
2515              then we should say mcs51_ptrRegReq */
2516           if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2517             mcs51_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ||
2518                                  OP_SYMBOL (IC_COND (ic))->iaccess) ? 1 : 0);
2519           else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2520             mcs51_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ||
2521                               OP_SYMBOL (IC_JTCOND (ic))->iaccess) ? 1 : 0);
2522           else
2523             {
2524               if (IS_SYMOP (IC_LEFT (ic)))
2525                 mcs51_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ||
2526                                 OP_SYMBOL (IC_LEFT (ic))->iaccess) ? 1 : 0);
2527               if (IS_SYMOP (IC_RIGHT (ic)))
2528                 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ||
2529                                OP_SYMBOL (IC_RIGHT (ic))->iaccess) ? 1 : 0);
2530               if (IS_SYMOP (IC_RESULT (ic)))
2531                 mcs51_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ||
2532                               OP_SYMBOL (IC_RESULT (ic))->iaccess) ? 1 : 0);
2533             }
2534         }
2535
2536       /* if the condition of an if instruction
2537          is defined in the previous instruction and
2538          this is the only usage then
2539          mark the itemp as a conditional */
2540       if ((IS_CONDITIONAL (ic) ||
2541            (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2542           ic->next && ic->next->op == IFX &&
2543           bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2544           isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2545           OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2546         {
2547           OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2548           continue;
2549         }
2550
2551       /* reduce for support function calls */
2552       if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2553         packRegsForSupport (ic, ebp);
2554
2555       /* some cases the redundant moves can
2556          can be eliminated for return statements */
2557       if ((ic->op == RETURN || ic->op == SEND) &&
2558           !isOperandInFarSpace (IC_LEFT (ic)) &&
2559           options.model == MODEL_SMALL) {
2560         if (0 && options.stackAuto) {
2561           /* we should check here if acc will be clobbered for stack
2562              offset calculations */
2563         } else {
2564           packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2565         }
2566       }
2567
2568       /* if pointer set & left has a size more than
2569          one and right is not in far space */
2570       if (POINTER_SET (ic) &&
2571           !isOperandInFarSpace (IC_RIGHT (ic)) &&
2572           !OP_SYMBOL (IC_RESULT (ic))->remat &&
2573           !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2574           getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2575
2576         packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2577
2578       /* if pointer get */
2579       if (POINTER_GET (ic) &&
2580           !isOperandInFarSpace (IC_RESULT (ic)) &&
2581           !OP_SYMBOL (IC_LEFT (ic))->remat &&
2582           !IS_OP_RUONLY (IC_RESULT (ic)) &&
2583           getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2584
2585         packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2586
2587
2588       /* if this is cast for intergral promotion then
2589          check if only use of  the definition of the 
2590          operand being casted/ if yes then replace
2591          the result of that arithmetic operation with 
2592          this result and get rid of the cast */
2593       if (ic->op == CAST)
2594         {
2595           sym_link *fromType = operandType (IC_RIGHT (ic));
2596           sym_link *toType = operandType (IC_LEFT (ic));
2597
2598           if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2599               getSize (fromType) != getSize (toType) &&
2600               SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2601             {
2602
2603               iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2604               if (dic)
2605                 {
2606                   if (IS_ARITHMETIC_OP (dic))
2607                     {                  
2608                       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2609                       IC_RESULT (dic) = IC_RESULT (ic);
2610                       remiCodeFromeBBlock (ebp, ic);
2611                       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2612                       hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2613                       OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2614                       ic = ic->prev;
2615                     }
2616                   else
2617                     OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2618                 }
2619             }
2620           else
2621             {
2622
2623               /* if the type from and type to are the same
2624                  then if this is the only use then packit */
2625               if (compareType (operandType (IC_RIGHT (ic)),
2626                              operandType (IC_LEFT (ic))) == 1)
2627                 {
2628                   iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2629                   if (dic)
2630                     {
2631                       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2632                       IC_RESULT (dic) = IC_RESULT (ic);
2633                       remiCodeFromeBBlock (ebp, ic);
2634                       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2635                       hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2636                       OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2637                       ic = ic->prev;
2638                     }
2639                 }
2640             }
2641         }
2642
2643       /* pack for PUSH 
2644          iTempNN := (some variable in farspace) V1
2645          push iTempNN ;
2646          -------------
2647          push V1
2648        */
2649       if (ic->op == IPUSH)
2650         {
2651           packForPush (ic, ebp);
2652         }
2653
2654
2655       /* pack registers for accumulator use, when the
2656          result of an arithmetic or bit wise operation
2657          has only one use, that use is immediately following
2658          the defintion and the using iCode has only one
2659          operand or has two operands but one is literal &
2660          the result of that operation is not on stack then
2661          we can leave the result of this operation in acc:b
2662          combination */
2663       if ((IS_ARITHMETIC_OP (ic)
2664            || IS_CONDITIONAL(ic)
2665            || IS_BITWISE_OP (ic)
2666            || ic->op == LEFT_OP || ic->op == RIGHT_OP || ic->op == CALL
2667            || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
2668           ) &&
2669           IS_ITEMP (IC_RESULT (ic)) &&
2670           getSize (operandType (IC_RESULT (ic))) <= 2)
2671
2672         packRegsForAccUse (ic);
2673     }
2674 }
2675
2676 /*-----------------------------------------------------------------*/
2677 /* assignRegisters - assigns registers to each live range as need  */
2678 /*-----------------------------------------------------------------*/
2679 void
2680 mcs51_assignRegisters (eBBlock ** ebbs, int count)
2681 {
2682   iCode *ic;
2683   int i;
2684
2685   setToNull ((void *) &_G.funcrUsed);
2686   setToNull ((void *) &_G.totRegAssigned);
2687   mcs51_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2688   mcs51_nRegs = 8;
2689
2690   /* change assignments this will remove some
2691      live ranges reducing some register pressure */
2692   for (i = 0; i < count; i++)
2693     packRegisters (ebbs[i]);
2694
2695   if (options.dump_pack)
2696     dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2697
2698   /* first determine for each live range the number of 
2699      registers & the type of registers required for each */
2700   regTypeNum (*ebbs);
2701
2702   /* and serially allocate registers */
2703   serialRegAssign (ebbs, count);
2704
2705   freeAllRegs ();
2706   fillGaps();
2707
2708   /* if stack was extended then tell the user */
2709   if (_G.stackExtend)
2710     {
2711 /*      werror(W_TOOMANY_SPILS,"stack", */
2712 /*             _G.stackExtend,currFunc->name,""); */
2713       _G.stackExtend = 0;
2714     }
2715
2716   if (_G.dataExtend)
2717     {
2718 /*      werror(W_TOOMANY_SPILS,"data space", */
2719 /*             _G.dataExtend,currFunc->name,""); */
2720       _G.dataExtend = 0;
2721     }
2722
2723   /* after that create the register mask
2724      for each of the instruction */
2725   createRegMask (ebbs, count);
2726
2727   /* redo that offsets for stacked automatic variables */
2728   redoStackOffsets ();
2729
2730   if (options.dump_rassgn)
2731     {
2732       dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
2733       dumpLiveRanges (DUMP_LRANGE, liveRanges);
2734     }
2735
2736   /* do the overlaysegment stuff SDCCmem.c */
2737   doOverlays (ebbs, count);
2738
2739   /* now get back the chain */
2740   ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2741
2742   gen51Code (ic);
2743
2744   /* free up any _G.stackSpil locations allocated */
2745   applyToSet (_G.stackSpil, deallocStackSpil);
2746   _G.slocNum = 0;
2747   setToNull ((void **) &_G.stackSpil);
2748   setToNull ((void **) &_G.spiltSet);
2749   /* mark all registers as free */
2750   freeAllRegs ();
2751
2752   return;
2753 }