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