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