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