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