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