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