- added return 0 statements after assert() to make compiler happy
[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   /* just to make the compiler happy */
866   return 0;
867 }
868
869 /*-----------------------------------------------------------------*/
870 /* getRegGprNoSpil - get it cannot split                           */
871 /*-----------------------------------------------------------------*/
872 static regs *getRegGprNoSpil()
873 {
874
875   regs *reg;
876   if ((reg = allocReg (REG_GPR)))
877     return reg;
878
879   if (!ds390_ptrRegReq)
880     if ((reg = allocReg (REG_PTR)))
881       return reg;
882
883   assert(0);
884
885   /* just to make the compiler happy */
886   return 0;
887 }
888
889 /*-----------------------------------------------------------------*/
890 /* symHasReg - symbol has a given register                         */
891 /*-----------------------------------------------------------------*/
892 static bool
893 symHasReg (symbol * sym, regs * reg)
894 {
895   int i;
896
897   for (i = 0; i < sym->nRegs; i++)
898     if (sym->regs[i] == reg)
899       return TRUE;
900
901   return FALSE;
902 }
903
904 /*-----------------------------------------------------------------*/
905 /* deassignLRs - check the live to and if they have registers & are */
906 /*               not spilt then free up the registers              */
907 /*-----------------------------------------------------------------*/
908 static void
909 deassignLRs (iCode * ic, eBBlock * ebp)
910 {
911   symbol *sym;
912   int k;
913   symbol *result;
914
915   for (sym = hTabFirstItem (liveRanges, &k); sym;
916        sym = hTabNextItem (liveRanges, &k))
917     {
918
919       symbol *psym = NULL;
920       /* if it does not end here */
921       if (sym->liveTo > ic->seq)
922         continue;
923
924       /* if it was spilt on stack then we can 
925          mark the stack spil location as free */
926       if (sym->isspilt)
927         {
928           if (sym->stackSpil)
929             {
930               sym->usl.spillLoc->isFree = 1;
931               sym->stackSpil = 0;
932             }
933           continue;
934         }
935
936       if (!bitVectBitValue (_G.regAssigned, sym->key))
937         continue;
938
939       /* special case check if this is an IFX &
940          the privious one was a pop and the 
941          previous one was not spilt then keep track
942          of the symbol */
943       if (ic->op == IFX && ic->prev &&
944           ic->prev->op == IPOP &&
945           !ic->prev->parmPush &&
946           !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
947         psym = OP_SYMBOL (IC_LEFT (ic->prev));
948
949       if (sym->nRegs)
950         {
951           int i = 0;
952
953           bitVectUnSetBit (_G.regAssigned, sym->key);
954
955           /* if the result of this one needs registers
956              and does not have it then assign it right
957              away */
958           if (IC_RESULT (ic) &&
959               !(SKIP_IC2 (ic) ||        /* not a special icode */
960                 ic->op == JUMPTABLE ||
961                 ic->op == IFX ||
962                 ic->op == IPUSH ||
963                 ic->op == IPOP ||
964                 ic->op == RETURN ||
965                 POINTER_SET (ic)) &&
966               (result = OP_SYMBOL (IC_RESULT (ic))) &&  /* has a result */
967               result->liveTo > ic->seq &&       /* and will live beyond this */
968               result->liveTo <= ebp->lSeq &&    /* does not go beyond this block */
969               result->regType == sym->regType &&        /* same register types */
970               result->nRegs &&  /* which needs registers */
971               !result->isspilt &&       /* and does not already have them */
972               !result->remat &&
973               !bitVectBitValue (_G.regAssigned, result->key) &&
974           /* the number of free regs + number of regs in this LR
975              can accomodate the what result Needs */
976               ((nfreeRegsType (result->regType) +
977                 sym->nRegs) >= result->nRegs)
978             )
979             {
980
981               for (i = 0; i < result->nRegs; i++)
982                 if (i < sym->nRegs)
983                   result->regs[i] = sym->regs[i];
984                 else
985                   result->regs[i] = getRegGpr (ic, ebp, result);
986
987               _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
988               _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, result->key);
989
990             }
991
992           /* free the remaining */
993           for (; i < sym->nRegs; i++)
994             {
995               if (psym)
996                 {
997                   if (!symHasReg (psym, sym->regs[i]))
998                     freeReg (sym->regs[i]);
999                 }
1000               else
1001                 freeReg (sym->regs[i]);
1002             }
1003         }
1004     }
1005 }
1006
1007
1008 /*-----------------------------------------------------------------*/
1009 /* reassignLR - reassign this to registers                         */
1010 /*-----------------------------------------------------------------*/
1011 static void
1012 reassignLR (operand * op)
1013 {
1014   symbol *sym = OP_SYMBOL (op);
1015   int i;
1016
1017   /* not spilt any more */
1018   sym->isspilt = sym->spillA = sym->blockSpil = sym->remainSpil = 0;
1019   bitVectUnSetBit (_G.spiltSet, sym->key);
1020
1021   _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1022   _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1023
1024   _G.blockSpil--;
1025
1026   for (i = 0; i < sym->nRegs; i++)
1027     sym->regs[i]->isFree = 0;
1028 }
1029
1030 /*-----------------------------------------------------------------*/
1031 /* willCauseSpill - determines if allocating will cause a spill    */
1032 /*-----------------------------------------------------------------*/
1033 static int
1034 willCauseSpill (int nr, int rt)
1035 {
1036   /* first check if there are any avlb registers
1037      of te type required */
1038   if (rt == REG_PTR)
1039     {
1040       /* special case for pointer type 
1041          if pointer type not avlb then 
1042          check for type gpr */
1043       if (nFreeRegs (rt) >= nr)
1044         return 0;
1045       if (nFreeRegs (REG_GPR) >= nr)
1046         return 0;
1047     }
1048   else
1049     {
1050       if (ds390_ptrRegReq)
1051         {
1052           if (nFreeRegs (rt) >= nr)
1053             return 0;
1054         }
1055       else
1056         {
1057           if (nFreeRegs (REG_PTR) +
1058               nFreeRegs (REG_GPR) >= nr)
1059             return 0;
1060         }
1061     }
1062
1063   /* it will cause a spil */
1064   return 1;
1065 }
1066
1067 /*-----------------------------------------------------------------*/
1068 /* positionRegs - the allocator can allocate same registers to res- */
1069 /* ult and operand, if this happens make sure they are in the same */
1070 /* position as the operand otherwise chaos results                 */
1071 /*-----------------------------------------------------------------*/
1072 static int
1073 positionRegs (symbol * result, symbol * opsym)
1074 {
1075   int count = min (result->nRegs, opsym->nRegs);
1076   int i, j = 0, shared = 0;
1077   int change = 0;
1078
1079   /* if the result has been spilt then cannot share */
1080   if (opsym->isspilt)
1081     return 0;
1082 again:
1083   shared = 0;
1084   /* first make sure that they actually share */
1085   for (i = 0; i < count; i++)
1086     {
1087       for (j = 0; j < count; j++)
1088         {
1089           if (result->regs[i] == opsym->regs[j] && i != j)
1090             {
1091               shared = 1;
1092               goto xchgPositions;
1093             }
1094         }
1095     }
1096 xchgPositions:
1097   if (shared)
1098     {
1099       regs *tmp = result->regs[i];
1100       result->regs[i] = result->regs[j];
1101       result->regs[j] = tmp;
1102       change ++;
1103       goto again;
1104     }
1105   return change ;
1106 }
1107
1108 /*-----------------------------------------------------------------*/
1109 /* unusedLRS - returns a bitVector of liveranges not used in 'ebp' */
1110 /*-----------------------------------------------------------------*/
1111 bitVect *unusedLRs (eBBlock *ebp) 
1112 {
1113     bitVect *ret = NULL;
1114     symbol *sym;
1115     int key;
1116     
1117     if (!ebp) return NULL;
1118     for (sym = hTabFirstItem(liveRanges,&key); sym ; 
1119          sym = hTabNextItem(liveRanges,&key)) {
1120         
1121         if (notUsedInBlock(sym,ebp,NULL)) {
1122             ret = bitVectSetBit(ret,sym->key);
1123         }
1124     }
1125
1126     return ret;
1127 }
1128
1129 /*-----------------------------------------------------------------*/
1130 /* deassignUnsedLRs - if this baisc block ends in a return then    */
1131 /*                    deassign symbols not used in this block      */
1132 /*-----------------------------------------------------------------*/
1133 bitVect *deassignUnsedLRs(eBBlock *ebp)
1134 {
1135     bitVect *unused = NULL;
1136     int i;
1137
1138     switch (returnAtEnd(ebp)) {
1139     case 2: /* successor block ends in a return */
1140         unused = unusedLRs((eBBlock *) setFirstItem(ebp->succList));
1141         /* fall thru */
1142     case 1: /* this block ends in a return */
1143         unused = bitVectIntersect(unused,unusedLRs(ebp));
1144         break;
1145     }
1146     
1147     if (unused) {
1148         for (i = 0 ; i < unused->size ; i++ ) {
1149
1150             /* if unused  */
1151             if (bitVectBitValue(unused,i)) {
1152
1153                 /* if assigned to registers */
1154                 if (bitVectBitValue(_G.regAssigned,i)) {
1155                     symbol *sym;
1156                     int j;
1157                     
1158                     sym = hTabItemWithKey(liveRanges,i);
1159                     /* remove it from regassigned & mark the
1160                        register free */
1161                     bitVectUnSetBit(_G.regAssigned,i);
1162                     for (j = 0 ; j < sym->nRegs; j++)
1163                         freeReg(sym->regs[j]);
1164                 } else {
1165                     /* not assigned to registers : remove from set*/
1166                     bitVectUnSetBit(unused,i);
1167                 }
1168             }
1169         }
1170     }
1171     return unused;
1172 }
1173
1174 /*-----------------------------------------------------------------*/
1175 /* reassignUnusedLRs - put registers to unused Live ranges         */
1176 /*-----------------------------------------------------------------*/
1177 void reassignUnusedLRs (bitVect *unused)
1178 {
1179     int i;
1180     if (!unused) return ;
1181
1182     for (i = 0 ; i < unused->size ; i++ ) {
1183         /* if unused : means it was assigned to registers before */
1184         if (bitVectBitValue(unused,i)) {
1185             symbol *sym;
1186             int j;
1187             
1188             /* put it back into reg set*/
1189             bitVectSetBit(_G.regAssigned,i) ;
1190
1191             sym = hTabItemWithKey(liveRanges,i);
1192             /* makr registers busy */
1193             for (j = 0 ; j < sym->nRegs; j++)
1194                 sym->regs[j]->isFree = 0;
1195         }
1196     }
1197 }
1198
1199 /*-----------------------------------------------------------------*/
1200 /* serialRegAssign - serially allocate registers to the variables  */
1201 /*-----------------------------------------------------------------*/
1202 static void
1203 serialRegAssign (eBBlock ** ebbs, int count)
1204 {
1205   int i;
1206
1207   /* for all blocks */
1208   for (i = 0; i < count; i++)
1209       { /* ebbs */
1210
1211       iCode *ic;
1212       bitVect *unusedLRs = NULL;
1213
1214       if (ebbs[i]->noPath &&
1215           (ebbs[i]->entryLabel != entryLabel &&
1216            ebbs[i]->entryLabel != returnLabel))
1217         continue;
1218       
1219       unusedLRs = deassignUnsedLRs(ebbs[i]);
1220       
1221       /* of all instructions do */
1222       for (ic = ebbs[i]->sch; ic; ic = ic->next)
1223         {
1224
1225           /* if this is an ipop that means some live
1226              range will have to be assigned again */
1227           if (ic->op == IPOP)
1228             reassignLR (IC_LEFT (ic));
1229
1230           /* if result is present && is a true symbol */
1231           if (IC_RESULT (ic) && ic->op != IFX &&
1232               IS_TRUE_SYMOP (IC_RESULT (ic)))
1233             OP_SYMBOL (IC_RESULT (ic))->allocreq++;
1234
1235           /* take away registers from live
1236              ranges that end at this instruction */
1237           deassignLRs (ic, ebbs[i]);
1238
1239           /* some don't need registers */
1240           if (SKIP_IC2 (ic) ||
1241               ic->op == JUMPTABLE ||
1242               ic->op == IFX ||
1243               ic->op == IPUSH ||
1244               ic->op == IPOP ||
1245               (IC_RESULT (ic) && POINTER_SET (ic)))
1246             continue;
1247
1248           /* now we need to allocate registers
1249              only for the result */
1250           if (IC_RESULT (ic))
1251             {
1252               symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1253               bitVect *spillable;
1254               int willCS;
1255               int j;
1256               int ptrRegSet = 0;
1257
1258               /* if it does not need or is spilt 
1259                  or is already assigned to registers
1260                  or will not live beyond this instructions */
1261               if (!sym->nRegs ||
1262                   sym->isspilt ||
1263                   bitVectBitValue (_G.regAssigned, sym->key) ||
1264                   sym->liveTo <= ic->seq)
1265                 continue;
1266
1267               /* if some liverange has been spilt at the block level
1268                  and this one live beyond this block then spil this
1269                  to be safe */
1270               if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1271                 {
1272                   spillThis (sym);
1273                   continue;
1274                 }
1275               /* if trying to allocate this will cause
1276                  a spill and there is nothing to spill 
1277                  or this one is rematerializable then
1278                  spill this one */
1279               willCS = willCauseSpill (sym->nRegs, sym->regType);
1280               spillable = computeSpillable (ic);
1281               if (sym->remat ||
1282                   (willCS && bitVectIsZero (spillable)))
1283                 {
1284
1285                   spillThis (sym);
1286                   continue;
1287
1288                 }
1289
1290               /* if it has a spillocation & is used less than
1291                  all other live ranges then spill this */
1292                 if (willCS) {
1293                     if (sym->usl.spillLoc) {
1294                         symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1295                                                                          allLRs, ebbs[i], ic));
1296                         if (leastUsed && leastUsed->used > sym->used) {
1297                             spillThis (sym);
1298                             continue;
1299                         }
1300                     } else {
1301                         /* if none of the liveRanges have a spillLocation then better
1302                            to spill this one than anything else already assigned to registers */
1303                         if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1304                             /* if this is local to this block then we might find a block spil */
1305                             if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1306                                 spillThis (sym);
1307                                 continue;
1308                             }
1309                         }
1310                     }
1311                 }
1312
1313               /* if we need ptr regs for the right side
1314                  then mark it */
1315               if (POINTER_GET (ic) && IS_SYMOP (IC_LEFT (ic))
1316                   && getSize (OP_SYMBOL (IC_LEFT (ic))->type)
1317                   <= (unsigned) PTRSIZE)
1318                 {
1319                   ds390_ptrRegReq++;
1320                   ptrRegSet = 1;
1321                 }
1322               /* else we assign registers to it */
1323               _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1324               _G.totRegAssigned = bitVectSetBit (_G.totRegAssigned, sym->key);
1325
1326               for (j = 0; j < sym->nRegs; j++)
1327                 {
1328                   if (sym->regType == REG_PTR)
1329                     sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1330                   else
1331                     sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1332
1333                   /* if the allocation falied which means
1334                      this was spilt then break */
1335                   if (!sym->regs[j])
1336                     break;
1337                 }
1338               /* if it shares registers with operands make sure
1339                  that they are in the same position */
1340               if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1341                   OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
1342                 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1343                               OP_SYMBOL (IC_LEFT (ic)));
1344               /* do the same for the right operand */
1345               if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1346                   OP_SYMBOL (IC_RIGHT (ic))->nRegs)
1347                 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1348                               OP_SYMBOL (IC_RIGHT (ic)));
1349
1350               if (ptrRegSet)
1351                 {
1352                   ds390_ptrRegReq--;
1353                   ptrRegSet = 0;
1354                 }
1355
1356             }
1357         }
1358       reassignUnusedLRs(unusedLRs);
1359     }
1360 }
1361
1362 /*-----------------------------------------------------------------*/
1363 /* fillGaps - Try to fill in the Gaps left by Pass1                */
1364 /*-----------------------------------------------------------------*/
1365 static void fillGaps()
1366 {
1367     symbol *sym =NULL;
1368     int key =0;    
1369     int loop = 0, change;
1370
1371     if (getenv("DISABLE_FILL_GAPS")) return;
1372     
1373     /* First try to do DPTRuse once more since now we know what got into
1374        registers */ 
1375     
1376     while (loop++ < 10) {
1377         change = 0;
1378
1379         for (sym = hTabFirstItem(liveRanges,&key) ; sym ; 
1380              sym = hTabNextItem(liveRanges,&key)) {
1381             int size = getSize(sym->type);
1382
1383             if (sym->liveFrom == sym->liveTo) continue;
1384
1385             if (sym->uptr && sym->dptr==0 && !sym->ruonly && 
1386                 size < 4 && size > 1) {
1387
1388                 if (packRegsDPTRuse(operandFromSymbol(sym))) {
1389                     
1390                     /* if this was ssigned to registers then */
1391                     if (bitVectBitValue(_G.totRegAssigned,sym->key)) {
1392                         /* take it out of the register assigned set */
1393                         bitVectUnSetBit(_G.totRegAssigned,sym->key);
1394                     } else if (sym->usl.spillLoc) {
1395                         sym->usl.spillLoc->allocreq--;
1396                         sym->usl.spillLoc = NULL;
1397                     }
1398                     
1399                     sym->nRegs = 0;                 
1400                     sym->isspilt = sym->spillA = 0;
1401                     continue ;
1402                 }
1403
1404                 /* try assigning other dptrs */
1405                 if (sym->dptr == 0 && packRegsDPTRnuse(operandFromSymbol(sym),1) && !getenv("DPTRnDISABLE")) {
1406                     /* if this was ssigned to registers then */
1407                     if (bitVectBitValue(_G.totRegAssigned,sym->key)) {
1408                         /* take it out of the register assigned set */
1409                         bitVectUnSetBit(_G.totRegAssigned,sym->key);
1410                     } else if (sym->usl.spillLoc) {
1411                         sym->usl.spillLoc->allocreq--;
1412                         sym->usl.spillLoc = NULL;
1413                     }
1414                     sym->nRegs = 0;                 
1415                     sym->isspilt = sym->spillA = 0;                 
1416                 }
1417             }
1418         }
1419         
1420         /* look for livernages that was spilt by the allocator */
1421         for (sym = hTabFirstItem(liveRanges,&key) ; sym ; 
1422              sym = hTabNextItem(liveRanges,&key)) {
1423             
1424             int i;
1425             int pdone = 0;
1426             
1427             if (!sym->spillA || !sym->clashes || sym->remat) continue ;
1428             if (!sym->uses || !sym->defs) continue ;
1429             /* find the liveRanges this one clashes with, that are
1430                still assigned to registers & mark the registers as used*/
1431             for ( i = 0 ; i < sym->clashes->size ; i ++) {
1432                 int k;
1433                 symbol *clr;
1434                 
1435                 if (bitVectBitValue(sym->clashes,i) == 0 ||    /* those that clash with this */
1436                     bitVectBitValue(_G.totRegAssigned,i) == 0) /* and are still assigned to registers */
1437                     continue ;
1438                 
1439                 assert (clr = hTabItemWithKey(liveRanges,i));
1440                 
1441                 /* mark these registers as used */
1442                 for (k = 0 ; k < clr->nRegs ; k++ ) 
1443                     useReg(clr->regs[k]);
1444             }
1445             
1446             if (willCauseSpill(sym->nRegs,sym->regType)) {
1447                 /* NOPE :( clear all registers & and continue */
1448                 freeAllRegs();
1449                 continue ;
1450             }
1451             
1452             /* THERE IS HOPE !!!! */
1453             for (i=0; i < sym->nRegs ; i++ ) {
1454                 if (sym->regType == REG_PTR)
1455                     sym->regs[i] = getRegPtrNoSpil ();
1456                 else
1457                     sym->regs[i] = getRegGprNoSpil ();            
1458             }
1459             
1460             /* for all its definitions & uses check if the registers
1461                allocated needs positioning NOTE: we can position
1462                only ONCE if more than One positioning required 
1463                then give up */
1464             sym->isspilt = 0;
1465             for (i = 0 ; i < sym->defs->size ; i++ ) {
1466                 if (bitVectBitValue(sym->defs,i)) {
1467                     iCode *ic;
1468                     if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1469                     if (SKIP_IC(ic)) continue;
1470                     assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1471                     /* if left is assigned to registers */
1472                     if (IS_SYMOP(IC_LEFT(ic)) && 
1473                         bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1474                         pdone += positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)));
1475                     }
1476                     if (IS_SYMOP(IC_RIGHT(ic)) && 
1477                         bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1478                         pdone += positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)));
1479                     }
1480                     if (pdone > 1) break;
1481                 }
1482             }
1483             for (i = 0 ; i < sym->uses->size ; i++ ) {
1484                 if (bitVectBitValue(sym->uses,i)) {
1485                     iCode *ic;
1486                     if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1487                     if (SKIP_IC(ic)) continue;
1488                     if (!IS_ASSIGN_ICODE(ic)) continue ;
1489                     
1490                     /* if result is assigned to registers */
1491                     if (IS_SYMOP(IC_RESULT(ic)) && 
1492                         bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RESULT(ic))->key)) {
1493                         pdone += positionRegs(sym,OP_SYMBOL(IC_RESULT(ic)));
1494                     }
1495                     if (pdone > 1) break;
1496                 }
1497             }
1498             /* had to position more than once GIVE UP */
1499             if (pdone > 1) {
1500                 /* UNDO all the changes we made to try this */
1501                 sym->isspilt = 1;
1502                 for (i=0; i < sym->nRegs ; i++ ) {
1503                     sym->regs[i] = NULL;
1504                 }
1505                 freeAllRegs();
1506                 D (fprintf (stderr, "Fill Gap gave up due to positioning for "
1507                             "%s in function %s\n",
1508                             sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1509                 continue ;          
1510             }
1511             D (fprintf (stderr, "FILLED GAP for %s in function %s\n",
1512                         sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1513             _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1514             sym->isspilt = sym->spillA = 0 ;
1515             sym->usl.spillLoc->allocreq--;
1516             sym->usl.spillLoc = NULL;
1517             freeAllRegs();
1518             change ++;
1519         }
1520         if (!change) break;
1521     }
1522 }
1523
1524 /*-----------------------------------------------------------------*/
1525 /* rUmaskForOp :- returns register mask for an operand             */
1526 /*-----------------------------------------------------------------*/
1527 bitVect *
1528 ds390_rUmaskForOp (operand * op)
1529 {
1530   bitVect *rumask;
1531   symbol *sym;
1532   int j;
1533
1534   /* only temporaries are assigned registers */
1535   if (!IS_ITEMP (op))
1536     return NULL;
1537
1538   sym = OP_SYMBOL (op);
1539
1540   /* if spilt or no registers assigned to it
1541      then nothing */
1542   if (sym->isspilt || !sym->nRegs)
1543     return NULL;
1544
1545   rumask = newBitVect (ds390_nRegs);
1546
1547   for (j = 0; j < sym->nRegs; j++)
1548     {
1549       rumask = bitVectSetBit (rumask,
1550                               sym->regs[j]->rIdx);
1551     }
1552
1553   return rumask;
1554 }
1555
1556 /*-----------------------------------------------------------------*/
1557 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1558 /*-----------------------------------------------------------------*/
1559 static bitVect *
1560 regsUsedIniCode (iCode * ic)
1561 {
1562   bitVect *rmask = newBitVect (ds390_nRegs);
1563
1564   /* do the special cases first */
1565   if (ic->op == IFX)
1566     {
1567       rmask = bitVectUnion (rmask,
1568                             ds390_rUmaskForOp (IC_COND (ic)));
1569       goto ret;
1570     }
1571
1572   /* for the jumptable */
1573   if (ic->op == JUMPTABLE)
1574     {
1575       rmask = bitVectUnion (rmask,
1576                             ds390_rUmaskForOp (IC_JTCOND (ic)));
1577
1578       goto ret;
1579     }
1580
1581   /* of all other cases */
1582   if (IC_LEFT (ic))
1583     rmask = bitVectUnion (rmask,
1584                           ds390_rUmaskForOp (IC_LEFT (ic)));
1585
1586
1587   if (IC_RIGHT (ic))
1588     rmask = bitVectUnion (rmask,
1589                           ds390_rUmaskForOp (IC_RIGHT (ic)));
1590
1591   if (IC_RESULT (ic))
1592     rmask = bitVectUnion (rmask,
1593                           ds390_rUmaskForOp (IC_RESULT (ic)));
1594
1595 ret:
1596   return rmask;
1597 }
1598
1599 /*-----------------------------------------------------------------*/
1600 /* createRegMask - for each instruction will determine the regsUsed */
1601 /*-----------------------------------------------------------------*/
1602 static void
1603 createRegMask (eBBlock ** ebbs, int count)
1604 {
1605   int i;
1606
1607   /* for all blocks */
1608   for (i = 0; i < count; i++)
1609     {
1610       iCode *ic;
1611
1612       if (ebbs[i]->noPath &&
1613           (ebbs[i]->entryLabel != entryLabel &&
1614            ebbs[i]->entryLabel != returnLabel))
1615         continue;
1616
1617       /* for all instructions */
1618       for (ic = ebbs[i]->sch; ic; ic = ic->next)
1619         {
1620
1621           int j;
1622
1623           if (SKIP_IC2 (ic) || !ic->rlive)
1624             continue;
1625
1626           /* first mark the registers used in this
1627              instruction */
1628           ic->rUsed = regsUsedIniCode (ic);
1629           _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1630
1631           /* now create the register mask for those 
1632              registers that are in use : this is a
1633              super set of ic->rUsed */
1634           ic->rMask = newBitVect (ds390_nRegs + 1);
1635
1636           /* for all live Ranges alive at this point */
1637           for (j = 1; j < ic->rlive->size; j++)
1638             {
1639               symbol *sym;
1640               int k;
1641
1642               /* if not alive then continue */
1643               if (!bitVectBitValue (ic->rlive, j))
1644                 continue;
1645
1646               /* find the live range we are interested in */
1647               if (!(sym = hTabItemWithKey (liveRanges, j)))
1648                 {
1649                   werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1650                           "createRegMask cannot find live range");
1651                   exit (0);
1652                 }
1653               
1654               /* special case for ruonly */
1655               if (sym->ruonly && sym->liveFrom != sym->liveTo) {
1656                   int size = getSize(sym->type);
1657                   int j = DPL_IDX;
1658                   for (k = 0 ; k < size; k++ )
1659                       ic->rMask = bitVectSetBit (ic->rMask, j++);
1660                   continue ;
1661               }
1662               /* if no register assigned to it */
1663               if (!sym->nRegs || sym->isspilt)
1664                 continue;
1665
1666               /* for all the registers allocated to it */
1667               for (k = 0; k < sym->nRegs; k++)
1668                 if (sym->regs[k])
1669                   ic->rMask =
1670                     bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1671             }
1672         }
1673     }
1674 }
1675
1676 /*-----------------------------------------------------------------*/
1677 /* rematStr - returns the rematerialized string for a remat var    */
1678 /*-----------------------------------------------------------------*/
1679 static char *
1680 rematStr (symbol * sym)
1681 {
1682   char *s = buffer;
1683   iCode *ic = sym->rematiCode;
1684
1685   while (1)
1686     {
1687
1688       /* if plus or minus print the right hand side */
1689       if (ic->op == '+' || ic->op == '-')
1690         {
1691           sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1692                    ic->op);
1693           s += strlen (s);
1694           ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1695           continue;
1696         }
1697       /* cast then continue */
1698       if (IS_CAST_ICODE(ic)) {
1699           ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1700           continue;
1701       }
1702       /* we reached the end */
1703       sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1704       break;
1705     }
1706
1707   return buffer;
1708 }
1709
1710 /*-----------------------------------------------------------------*/
1711 /* regTypeNum - computes the type & number of registers required   */
1712 /*-----------------------------------------------------------------*/
1713 static void
1714 regTypeNum ()
1715 {
1716   symbol *sym;
1717   int k;
1718   iCode *ic;
1719
1720   /* for each live range do */
1721   for (sym = hTabFirstItem (liveRanges, &k); sym;
1722        sym = hTabNextItem (liveRanges, &k))
1723     {
1724
1725       /* if used zero times then no registers needed */
1726       if ((sym->liveTo - sym->liveFrom) == 0)
1727         continue;
1728
1729
1730       /* if the live range is a temporary */
1731       if (sym->isitmp)
1732         {
1733
1734           /* if the type is marked as a conditional */
1735           if (sym->regType == REG_CND)
1736             continue;
1737
1738           /* if used in return only then we don't 
1739              need registers */
1740           if (sym->ruonly || sym->accuse)
1741             {
1742               if (IS_AGGREGATE (sym->type) || sym->isptr)
1743                 sym->type = aggrToPtr (sym->type, FALSE);
1744               continue;
1745             }
1746
1747           /* if the symbol has only one definition &
1748              that definition is a get_pointer */
1749           if (bitVectnBitsOn (sym->defs) == 1 &&
1750               (ic = hTabItemWithKey (iCodehTab,
1751                                      bitVectFirstBit (sym->defs))) &&
1752               POINTER_GET (ic) &&
1753               !sym->noSpilLoc &&
1754               !IS_BITVAR (sym->etype))
1755             {
1756               /* and that pointer is remat in data space */
1757               if (OP_SYMBOL (IC_LEFT (ic))->remat &&
1758                   !IS_CAST_ICODE(OP_SYMBOL (IC_LEFT (ic))->rematiCode) &&
1759                   DCL_TYPE (aggrToPtr (operandType(IC_LEFT(ic)), FALSE)) == POINTER)
1760                 {
1761
1762                   /* create a psuedo symbol & force a spil */
1763                   symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
1764                   psym->type = sym->type;
1765                   psym->etype = sym->etype;
1766                   strcpy (psym->rname, psym->name);
1767                   sym->isspilt = 1;
1768                   sym->usl.spillLoc = psym;
1769                   continue;
1770                 }
1771
1772               /* if in data space or idata space then try to
1773                  allocate pointer register */
1774
1775             }
1776
1777           /* if not then we require registers */
1778           sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1779                         getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1780                         getSize (sym->type));
1781
1782           if (sym->nRegs > 4)
1783             {
1784               fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1785               printTypeChain (sym->type, stderr);
1786               fprintf (stderr, "\n");
1787             }
1788
1789           /* determine the type of register required */
1790           if (sym->nRegs == 1 &&
1791               IS_PTR (sym->type) &&
1792               sym->uptr)
1793             sym->regType = REG_PTR;
1794           else
1795             sym->regType = REG_GPR;
1796
1797         }
1798       else
1799         /* for the first run we don't provide */
1800         /* registers for true symbols we will */
1801         /* see how things go                  */
1802         sym->nRegs = 0;
1803     }
1804
1805 }
1806
1807 /*-----------------------------------------------------------------*/
1808 /* freeAllRegs - mark all registers as free                        */
1809 /*-----------------------------------------------------------------*/
1810 static void
1811 freeAllRegs ()
1812 {
1813   int i;
1814
1815   for (i = 0; i < ds390_nRegs; i++)
1816     regs390[i].isFree = 1;
1817 }
1818
1819 /*-----------------------------------------------------------------*/
1820 /* deallocStackSpil - this will set the stack pointer back         */
1821 /*-----------------------------------------------------------------*/
1822 static
1823 DEFSETFUNC (deallocStackSpil)
1824 {
1825   symbol *sym = item;
1826
1827   deallocLocal (sym);
1828   return 0;
1829 }
1830
1831 /*-----------------------------------------------------------------*/
1832 /* farSpacePackable - returns the packable icode for far variables */
1833 /*-----------------------------------------------------------------*/
1834 static iCode *
1835 farSpacePackable (iCode * ic)
1836 {
1837   iCode *dic;
1838
1839   /* go thru till we find a definition for the
1840      symbol on the right */
1841   for (dic = ic->prev; dic; dic = dic->prev)
1842     {
1843
1844       /* if the definition is a call then no */
1845       if ((dic->op == CALL || dic->op == PCALL) &&
1846           IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1847         {
1848           return NULL;
1849         }
1850
1851       /* if shift by unknown amount then not */
1852       if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1853           IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1854         return NULL;
1855
1856       /* if pointer get and size > 1 */
1857       if (POINTER_GET (dic) &&
1858           getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1859         return NULL;
1860
1861       if (POINTER_SET (dic) &&
1862           getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1863         return NULL;
1864
1865       /* if any three is a true symbol in far space */
1866       if (IC_RESULT (dic) &&
1867           IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1868           isOperandInFarSpace (IC_RESULT (dic)))
1869         return NULL;
1870
1871       if (IC_RIGHT (dic) &&
1872           IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1873           isOperandInFarSpace (IC_RIGHT (dic)) &&
1874           !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1875         return NULL;
1876
1877       if (IC_LEFT (dic) &&
1878           IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1879           isOperandInFarSpace (IC_LEFT (dic)) &&
1880           !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1881         return NULL;
1882
1883       if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1884         {
1885           if ((dic->op == LEFT_OP ||
1886                dic->op == RIGHT_OP ||
1887                dic->op == '-') &&
1888               IS_OP_LITERAL (IC_RIGHT (dic)))
1889             return NULL;
1890           else
1891             return dic;
1892         }
1893     }
1894
1895   return NULL;
1896 }
1897
1898 /*-----------------------------------------------------------------*/
1899 /* packRegsForAssign - register reduction for assignment           */
1900 /*-----------------------------------------------------------------*/
1901 static int
1902 packRegsForAssign (iCode * ic, eBBlock * ebp)
1903 {
1904   iCode *dic, *sic;
1905
1906   if (!IS_ITEMP (IC_RIGHT (ic)) ||
1907       OP_SYMBOL (IC_RIGHT (ic))->isind ||
1908       OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1909     {
1910       return 0;
1911     }
1912
1913   /* if the true symbol is defined in far space or on stack
1914      then we should not since this will increase register pressure */
1915 #if 0
1916   if (isOperandInFarSpace (IC_RESULT (ic)))
1917     {
1918       if ((dic = farSpacePackable (ic)))
1919         goto pack;
1920       else
1921         return 0;
1922     }
1923 #else
1924   if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1925     return 0;
1926   }
1927 #endif
1928
1929   /* find the definition of iTempNN scanning backwards if we find a 
1930      a use of the true symbol in before we find the definition then 
1931      we cannot */
1932   for (dic = ic->prev; dic; dic = dic->prev)
1933     {
1934       /* if there is a function call then don't pack it */
1935       if ((dic->op == CALL || dic->op == PCALL))
1936         {
1937           dic = NULL;
1938           break;
1939         }
1940
1941       if (SKIP_IC2 (dic))
1942         continue;
1943
1944       if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1945           IS_OP_VOLATILE (IC_RESULT (dic)))
1946         {
1947           dic = NULL;
1948           break;
1949         }
1950
1951       if (IS_SYMOP (IC_RESULT (dic)) &&
1952           IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1953         {
1954           if (POINTER_SET (dic))
1955             dic = NULL;
1956
1957           break;
1958         }
1959
1960       if (IS_SYMOP (IC_RIGHT (dic)) &&
1961           (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1962            IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1963         {
1964           dic = NULL;
1965           break;
1966         }
1967
1968       if (IS_SYMOP (IC_LEFT (dic)) &&
1969           (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1970            IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1971         {
1972           dic = NULL;
1973           break;
1974         }
1975
1976       if (POINTER_SET (dic) &&
1977           IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1978         {
1979           dic = NULL;
1980           break;
1981         }
1982     }
1983
1984   if (!dic)
1985     return 0;                   /* did not find */
1986
1987   /* if the result is on stack or iaccess then it must be
1988      the same atleast one of the operands */
1989   if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1990       OP_SYMBOL (IC_RESULT (ic))->iaccess)
1991     {
1992
1993       /* the operation has only one symbol
1994          operator then we can pack */
1995       if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1996           (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1997         goto pack;
1998
1999       if (!((IC_LEFT (dic) &&
2000              IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
2001             (IC_RIGHT (dic) &&
2002              IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
2003         return 0;
2004     }
2005 pack:
2006   /* found the definition */
2007   /* replace the result with the result of */
2008   /* this assignment and remove this assignment */
2009   bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2010
2011   IC_RESULT (dic) = IC_RESULT (ic);
2012
2013   if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
2014     {
2015       OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
2016     }
2017   /* delete from liverange table also 
2018      delete from all the points inbetween and the new
2019      one */
2020   for (sic = dic; sic != ic; sic = sic->next)
2021     {
2022       bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
2023       if (IS_ITEMP (IC_RESULT (dic)))
2024         bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
2025     }
2026
2027   remiCodeFromeBBlock (ebp, ic);
2028   bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2029   hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2030   OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2031   return 1;
2032
2033 }
2034
2035 /*-----------------------------------------------------------------*/
2036 /* findAssignToSym : scanning backwards looks for first assig found */
2037 /*-----------------------------------------------------------------*/
2038 static iCode *
2039 findAssignToSym (operand * op, iCode * ic)
2040 {
2041   iCode *dic;
2042
2043   for (dic = ic->prev; dic; dic = dic->prev)
2044     {
2045
2046       /* if definition by assignment */
2047       if (dic->op == '=' &&
2048           !POINTER_SET (dic) &&
2049           IC_RESULT (dic)->key == op->key
2050 /*          &&  IS_TRUE_SYMOP(IC_RIGHT(dic)) */
2051         )
2052         {
2053
2054           /* we are interested only if defined in far space */
2055           /* or in stack space in case of + & - */
2056
2057           /* if assigned to a non-symbol then return
2058              FALSE */
2059           if (!IS_SYMOP (IC_RIGHT (dic)))
2060             return NULL;
2061
2062           /* if the symbol is in far space then
2063              we should not */
2064           if (isOperandInFarSpace (IC_RIGHT (dic)))
2065             return NULL;
2066
2067           /* for + & - operations make sure that
2068              if it is on the stack it is the same
2069              as one of the three operands */
2070           if ((ic->op == '+' || ic->op == '-') &&
2071               OP_SYMBOL (IC_RIGHT (dic))->onStack)
2072             {
2073
2074               if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
2075                   IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
2076                   IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
2077                 return NULL;
2078             }
2079
2080           break;
2081
2082         }
2083
2084       /* if we find an usage then we cannot delete it */
2085       if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
2086         return NULL;
2087
2088       if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
2089         return NULL;
2090
2091       if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
2092         return NULL;
2093     }
2094
2095   /* now make sure that the right side of dic
2096      is not defined between ic & dic */
2097   if (dic)
2098     {
2099       iCode *sic = dic->next;
2100
2101       for (; sic != ic; sic = sic->next)
2102         if (IC_RESULT (sic) &&
2103             IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2104           return NULL;
2105     }
2106
2107   return dic;
2108
2109
2110 }
2111
2112 /*-----------------------------------------------------------------*/
2113 /* packRegsForSupport :- reduce some registers for support calls   */
2114 /*-----------------------------------------------------------------*/
2115 static int
2116 packRegsForSupport (iCode * ic, eBBlock * ebp)
2117 {    
2118   int change = 0;
2119   
2120   /* for the left & right operand :- look to see if the
2121      left was assigned a true symbol in far space in that
2122      case replace them */
2123   if (IS_ITEMP (IC_LEFT (ic)) &&
2124       OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2125     {
2126       iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
2127       iCode *sic;
2128
2129       if (!dic)
2130         goto right;
2131
2132       /* found it we need to remove it from the
2133          block */
2134       for (sic = dic; sic != ic; sic = sic->next) {
2135         bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
2136         sic->rlive = bitVectSetBit (sic->rlive, IC_RIGHT (dic)->key);
2137       }
2138
2139       IC_LEFT (ic)->operand.symOperand =
2140         IC_RIGHT (dic)->operand.symOperand;
2141       OP_SYMBOL(IC_LEFT(ic))->liveTo = ic->seq;
2142       IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2143       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2144       remiCodeFromeBBlock (ebp, dic);
2145       hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2146       change++;
2147     }
2148
2149   /* do the same for the right operand */
2150 right:
2151   if (!change &&
2152       IS_ITEMP (IC_RIGHT (ic)) &&
2153       OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2154     {
2155       iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2156       iCode *sic;
2157
2158       if (!dic)
2159         return change;
2160
2161       /* if this is a subtraction & the result
2162          is a true symbol in far space then don't pack */
2163       if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2164         {
2165           sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2166           if (IN_FARSPACE (SPEC_OCLS (etype)))
2167             return change;
2168         }
2169       /* found it we need to remove it from the
2170          block */
2171       for (sic = dic; sic != ic; sic = sic->next) {
2172         bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
2173         sic->rlive = bitVectSetBit (sic->rlive, IC_RIGHT (dic)->key);
2174       }
2175
2176       IC_RIGHT (ic)->operand.symOperand =
2177         IC_RIGHT (dic)->operand.symOperand;
2178       IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2179       OP_SYMBOL(IC_RIGHT(ic))->liveTo = ic->seq;
2180       remiCodeFromeBBlock (ebp, dic);
2181       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2182       hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2183       change++;
2184     }
2185
2186   return change;
2187 }
2188
2189 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2190
2191
2192 /*-----------------------------------------------------------------*/
2193 /* packRegsDPTRnuse - color live ranges that can go into extra DPTRS */
2194 /*-----------------------------------------------------------------*/
2195 static int packRegsDPTRnuse( operand *op , int dptr)
2196 {
2197     int i,key;
2198     iCode *ic;
2199
2200     if (!IS_SYMOP(op) || !IS_ITEMP(op)) return 0;
2201     if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly || OP_SYMBOL(op)->dptr) 
2202         return 0; 
2203     
2204     /* first check if any overlapping liverange has already been
2205        assigned to this DPTR */
2206     if (OP_SYMBOL(op)->clashes) {
2207         for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ ) {
2208             symbol *sym;
2209             if (bitVectBitValue(OP_SYMBOL(op)->clashes,i)) {
2210                 sym = hTabItemWithKey(liveRanges,i);
2211                 if (sym->dptr == dptr) return 0;
2212             }
2213         }
2214     }
2215    
2216     /* future for more dptrs */
2217     if (dptr > 1) {
2218         OP_SYMBOL(op)->dptr = dptr;
2219         return 1;
2220     }
2221
2222     /* DPTR1 is special since it is also used as a scratch by the backend .
2223        so we walk thru the entire live range of this operand and make sure
2224        DPTR1 will not be used by the backed . The logic here is to find out if 
2225        more than one operand in an icode is in far space then we give up : we 
2226        don't keep it live across functions for now
2227     */
2228     
2229     ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2230     for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2231          ic = hTabNextItem(iCodeSeqhTab,&key)) {
2232         int nfs =0;
2233         
2234         if (ic->op == CALL || ic->op == PCALL) return 0;
2235
2236         /* single operand icode are ok */
2237         if (ic->op == IFX || ic->op == IPUSH)
2238             continue ;
2239
2240         if (ic->op == SEND ) {
2241             if (ic->argreg != 1 ) return 0;
2242             else continue ;
2243         }
2244         /* two special cases first */
2245         if (POINTER_GET(ic) && !isOperandEqual(IC_LEFT(ic),op)  && /* pointer get */
2246             !OP_SYMBOL(IC_LEFT(ic))->ruonly                     && /* with result in far space */
2247             (isOperandInFarSpace(IC_RESULT(ic)) &&              
2248              !isOperandInReg(IC_RESULT(ic)))) {
2249             return 0;
2250         }
2251
2252         if (POINTER_SET(ic) && !isOperandEqual(IC_RESULT(ic),op)        && /* pointer set */
2253             !OP_SYMBOL(IC_RESULT(ic))->ruonly                           && /* with right in far space */
2254             (isOperandInFarSpace(IC_RIGHT(ic)) &&               
2255              !isOperandInReg(IC_RIGHT(ic)))) {
2256             return 0;
2257         }
2258
2259         if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic))    && /* if symbol operand */
2260             !isOperandEqual(IC_RESULT(ic),op)           && /* not the same as this */
2261             ((isOperandInFarSpace(IC_RESULT(ic)) ||        /* in farspace or */
2262               OP_SYMBOL(IC_RESULT(ic))->onStack)        && /* on the stack   */
2263              !isOperandInReg(IC_RESULT(ic)))) {            /* and not in register */
2264             nfs++;
2265         }
2266         /* same for left */
2267         if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic))        && /* if symbol operand */
2268             !isOperandEqual(IC_LEFT(ic),op)             && /* not the same as this */
2269             ((isOperandInFarSpace(IC_LEFT(ic)) ||          /* in farspace or */
2270               OP_SYMBOL(IC_LEFT(ic))->onStack)          && /* on the stack   */
2271              !isOperandInReg(IC_LEFT(ic)))) {              /* and not in register */
2272             nfs++;
2273         }
2274         /* same for right */
2275         if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic))      && /* if symbol operand */
2276             !isOperandEqual(IC_RIGHT(ic),op)            && /* not the same as this */
2277             ((isOperandInFarSpace(IC_RIGHT(ic)) ||         /* in farspace or */
2278               OP_SYMBOL(IC_RIGHT(ic))->onStack)         && /* on the stack   */
2279              !isOperandInReg(IC_RIGHT(ic)))) {             /* and not in register */
2280             nfs++;
2281         }
2282         
2283         if (nfs && IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2284             OP_SYMBOL(IC_RESULT(ic))->ruonly) return 0;
2285
2286         if (nfs > 1) return 0;
2287     }
2288     OP_SYMBOL(op)->dptr = dptr;
2289     return 1;
2290 }
2291
2292 /*-----------------------------------------------------------------*/
2293 /* packRegsDPTRuse : - will reduce some registers for single Use */
2294 /*-----------------------------------------------------------------*/
2295 static iCode *
2296 packRegsDPTRuse (operand * op)
2297 {
2298     /* go thru entire liveRange of this variable & check for
2299        other possible usage of DPTR , if we don't find it the
2300        assign this to DPTR (ruonly)
2301     */
2302     int i, key;
2303     symbol *sym;
2304     iCode *ic, *dic;
2305     sym_link *type, *etype;
2306     
2307     if (!IS_SYMOP(op) || !IS_ITEMP(op)) return NULL;
2308     if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly) return NULL; 
2309
2310     /* first check if any overlapping liverange has already been
2311        assigned to DPTR */
2312     if (OP_SYMBOL(op)->clashes) {
2313         for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ ) {
2314             if (bitVectBitValue(OP_SYMBOL(op)->clashes,i)) {
2315                 sym = hTabItemWithKey(liveRanges,i);
2316                 if (sym->ruonly) return NULL ;
2317             }
2318         }
2319     }
2320
2321     /* no then go thru this guys live range */
2322     dic = ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2323     for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2324          ic = hTabNextItem(iCodeSeqhTab,&key)) {
2325
2326         if (SKIP_IC3(ic)) continue;
2327
2328         /* if PCALL cannot be sure give up */
2329         if (ic->op == PCALL) return NULL;
2330
2331         /* if SEND & not the first parameter then giveup */
2332         if (ic->op == SEND && ic->argreg != 1 &&
2333             ((isOperandInFarSpace(IC_LEFT(ic))  && !isOperandInReg(IC_LEFT(ic))) || 
2334              isOperandEqual(op,IC_LEFT(ic)))) return NULL;
2335
2336         /* if CALL then make sure it is VOID || return value not used 
2337            or the return value is assigned to this one */
2338         if (ic->op == CALL) {
2339             if (OP_SYMBOL(IC_RESULT(ic))->liveTo == 
2340                 OP_SYMBOL(IC_RESULT(ic))->liveFrom) continue ;
2341             etype = getSpec(type = operandType(IC_RESULT(ic)));
2342             if (getSize(type) == 0 || isOperandEqual(op,IC_RESULT(ic))) 
2343                 continue ;
2344             return NULL ;
2345         }
2346
2347         /* special case of add with a [remat] */
2348         if (ic->op == '+' && 
2349             OP_SYMBOL(IC_LEFT(ic))->remat &&
2350             (isOperandInFarSpace(IC_RIGHT(ic)) &&
2351              !isOperandInReg(IC_RIGHT(ic)))) return NULL ;
2352
2353         /* special cases  */
2354         /* pointerGet */
2355         if (POINTER_GET(ic) && !isOperandEqual(IC_LEFT(ic),op) &&
2356             getSize(operandType(IC_LEFT(ic))) > 1 ) return NULL ;
2357
2358         /* pointerSet */
2359         if (POINTER_SET(ic) && !isOperandEqual(IC_RESULT(ic),op) &&
2360             getSize(operandType(IC_RESULT(ic))) > 1 ) return NULL;
2361
2362         /* conditionals can destroy 'b' - make sure B wont 
2363            be used in this one*/
2364         if ((IS_CONDITIONAL(ic) || ic->op == '*' || ic->op == '/'  || 
2365              ic->op == LEFT_OP || ic->op == RIGHT_OP ) && 
2366             getSize(operandType(op)) > 3) return NULL;
2367
2368         /* if this is a cast to a bigger type */
2369         if (ic->op==CAST) {
2370           if (!IS_PTR(OP_SYM_TYPE(IC_RESULT(ic))) && 
2371               getSize(OP_SYM_TYPE(IC_RESULT(ic))) >
2372               getSize(OP_SYM_TYPE(IC_RIGHT(ic)))) {
2373             return 0;
2374           }
2375         }
2376
2377         /* general case */
2378         if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) && 
2379             !isOperandEqual(IC_RESULT(ic),op) &&
2380             ( ( ( isOperandInFarSpace(IC_RESULT(ic)) || OP_SYMBOL(IC_RESULT(ic))->onStack) && 
2381                 !isOperandInReg(IC_RESULT(ic))) || 
2382              OP_SYMBOL(IC_RESULT(ic))->ruonly)) return NULL;
2383
2384         if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) && 
2385             !isOperandEqual(IC_RIGHT(ic),op) &&
2386             (OP_SYMBOL(IC_RIGHT(ic))->liveTo >= ic->seq || 
2387              IS_TRUE_SYMOP(IC_RIGHT(ic))               ||
2388              OP_SYMBOL(IC_RIGHT(ic))->ruonly) &&
2389             ( ( isOperandInFarSpace(IC_RIGHT(ic)) || OP_SYMBOL(IC_RIGHT(ic))->onStack) && 
2390                 !isOperandInReg(IC_RIGHT(ic))) ) return NULL;
2391
2392         if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) && 
2393             !isOperandEqual(IC_LEFT(ic),op) &&
2394             (OP_SYMBOL(IC_LEFT(ic))->liveTo >= ic->seq || 
2395              IS_TRUE_SYMOP(IC_LEFT(ic))               ||
2396              OP_SYMBOL(IC_LEFT(ic))->ruonly) &&
2397             ( ( isOperandInFarSpace(IC_LEFT(ic)) || OP_SYMBOL(IC_LEFT(ic))->onStack) && 
2398                 !isOperandInReg(IC_LEFT(ic))) ) return NULL;
2399         
2400         if (IC_LEFT(ic) && IC_RIGHT(ic) && 
2401             IS_ITEMP(IC_LEFT(ic)) && IS_ITEMP(IC_RIGHT(ic)) &&
2402             (isOperandInFarSpace(IC_LEFT(ic)) && !isOperandInReg(IC_LEFT(ic))) && 
2403             (isOperandInFarSpace(IC_RIGHT(ic)) && !isOperandInReg(IC_RIGHT(ic))))
2404             return NULL;
2405     }
2406     OP_SYMBOL(op)->ruonly = 1;
2407     if (OP_SYMBOL(op)->usl.spillLoc) {
2408         if (OP_SYMBOL(op)->spillA)
2409             OP_SYMBOL(op)->usl.spillLoc->allocreq--;
2410         OP_SYMBOL(op)->usl.spillLoc = NULL;
2411     }
2412     return dic;
2413 }
2414
2415 /*-----------------------------------------------------------------*/
2416 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN          */
2417 /*-----------------------------------------------------------------*/
2418 static bool
2419 isBitwiseOptimizable (iCode * ic)
2420 {
2421   sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2422   sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2423
2424   /* bitwise operations are considered optimizable
2425      under the following conditions (Jean-Louis VERN) 
2426
2427      x & lit
2428      bit & bit
2429      bit & x
2430      bit ^ bit
2431      bit ^ x
2432      x   ^ lit
2433      x   | lit
2434      bit | bit
2435      bit | x
2436    */
2437   if ( IS_LITERAL (rtype) ||
2438       (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2439     return TRUE;
2440   else
2441     return FALSE;
2442 }
2443
2444 /*-----------------------------------------------------------------*/
2445 /* packRegsForAccUse - pack registers for acc use                  */
2446 /*-----------------------------------------------------------------*/
2447 static void
2448 packRegsForAccUse (iCode * ic)
2449 {
2450   iCode *uic;
2451
2452   /* if this is an aggregate, e.g. a one byte char array */
2453   if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2454     return;
2455   }
2456
2457   /* if we are calling a reentrant function that has stack parameters */
2458   if (ic->op == CALL &&
2459        IFFUNC_ISREENT(operandType(IC_LEFT(ic))) &&
2460        FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))))
2461       return;
2462
2463   if (ic->op == PCALL &&
2464        IFFUNC_ISREENT(operandType(IC_LEFT(ic))->next) &&
2465        FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))->next))
2466       return;
2467
2468   /* if + or - then it has to be one byte result */
2469   if ((ic->op == '+' || ic->op == '-')
2470       && getSize (operandType (IC_RESULT (ic))) > 1)
2471     return;
2472
2473   /* if shift operation make sure right side is not a literal */
2474   if (ic->op == RIGHT_OP &&
2475       (isOperandLiteral (IC_RIGHT (ic)) ||
2476        getSize (operandType (IC_RESULT (ic))) > 1))
2477     return;
2478
2479   if (ic->op == LEFT_OP &&
2480       (isOperandLiteral (IC_RIGHT (ic)) ||
2481        getSize (operandType (IC_RESULT (ic))) > 1))
2482     return;
2483
2484   if (IS_BITWISE_OP (ic) &&
2485       getSize (operandType (IC_RESULT (ic))) > 1)
2486     return;
2487
2488
2489   /* has only one definition */
2490   if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2491     return;
2492
2493   /* has only one use */
2494   if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2495     return;
2496
2497   /* and the usage immediately follows this iCode */
2498   if (!(uic = hTabItemWithKey (iCodehTab,
2499                                bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2500     return;
2501
2502   if (ic->next != uic)
2503     return;
2504
2505   /* if it is a conditional branch then we definitely can */
2506   if (uic->op == IFX)
2507     goto accuse;
2508
2509   if (uic->op == JUMPTABLE)
2510     return;
2511
2512   /* if the usage is not is an assignment
2513      or an arithmetic / bitwise / shift operation then not */
2514   if (POINTER_SET (uic) &&
2515       getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2516     return;
2517
2518   if (uic->op != '=' &&
2519       !IS_ARITHMETIC_OP (uic) &&
2520       !IS_BITWISE_OP (uic) &&
2521       uic->op != LEFT_OP &&
2522       uic->op != RIGHT_OP)
2523     return;
2524
2525   /* if used in ^ operation then make sure right is not a 
2526      literl */
2527   if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2528     return;
2529
2530   /* if shift operation make sure right side is not a literal */
2531   if (uic->op == RIGHT_OP &&
2532       (isOperandLiteral (IC_RIGHT (uic)) ||
2533        getSize (operandType (IC_RESULT (uic))) > 1))
2534     return;
2535
2536   if (uic->op == LEFT_OP &&
2537       (isOperandLiteral (IC_RIGHT (uic)) ||
2538        getSize (operandType (IC_RESULT (uic))) > 1))
2539     return;
2540
2541   /* make sure that the result of this icode is not on the
2542      stack, since acc is used to compute stack offset */
2543   if (isOperandOnStack(IC_RESULT(uic)))
2544     return;
2545
2546   /* if either one of them in far space then we cannot */
2547   if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2548        isOperandInFarSpace (IC_LEFT (uic))) ||
2549       (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2550        isOperandInFarSpace (IC_RIGHT (uic))))
2551     return;
2552
2553   /* if the usage has only one operand then we can */
2554   if (IC_LEFT (uic) == NULL ||
2555       IC_RIGHT (uic) == NULL)
2556     goto accuse;
2557
2558   /* make sure this is on the left side if not
2559      a '+' since '+' is commutative */
2560   if (ic->op != '+' &&
2561       IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2562     return;
2563
2564   /* if the other one is not on stack then we can */
2565   if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2566       (IS_ITEMP (IC_RIGHT (uic)) ||
2567        (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2568         !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
2569     goto accuse;
2570
2571   if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2572       (IS_ITEMP (IC_LEFT (uic)) ||
2573        (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2574         !OP_SYMBOL (IC_LEFT (uic))->onStack)))
2575     goto accuse;
2576
2577   return;
2578
2579 accuse:
2580   OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2581
2582
2583 }
2584
2585 /*-----------------------------------------------------------------*/
2586 /* packForPush - hueristics to reduce iCode for pushing            */
2587 /*-----------------------------------------------------------------*/
2588 static void
2589 packForPush (iCode * ic, eBBlock * ebp)
2590 {
2591   iCode *dic, *lic;
2592   bitVect *dbv;
2593
2594   if ((ic->op != IPUSH && ic->op != SEND) || !IS_ITEMP (IC_LEFT (ic)))
2595     return;
2596
2597   /* must have only definition & one usage */
2598   if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2599       bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2600     return;
2601
2602   /* find the definition */
2603   if (!(dic = hTabItemWithKey (iCodehTab,
2604                                bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2605     return;
2606
2607   if (dic->op != '=' || POINTER_SET (dic))
2608     return;
2609   
2610   if (dic->eBBlockNum != ic->eBBlockNum) return ;
2611
2612   /* make sure the right side does not have any definitions
2613      inbetween */
2614   dbv = OP_DEFS(IC_RIGHT(dic));
2615   for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2616           if (bitVectBitValue(dbv,lic->key)) return ;
2617   }
2618   /* make sure they have the same type */
2619   {
2620     sym_link *itype=operandType(IC_LEFT(ic));
2621     sym_link *ditype=operandType(IC_RIGHT(dic));
2622
2623     if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2624         SPEC_LONG(itype)!=SPEC_LONG(ditype))
2625       return;
2626   }
2627   /* extend the live range of replaced operand if needed */
2628   if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < OP_SYMBOL(IC_LEFT(ic))->liveTo) {
2629           OP_SYMBOL(IC_RIGHT(dic))->liveTo = OP_SYMBOL(IC_LEFT(ic))->liveTo;
2630           OP_SYMBOL(IC_RIGHT(dic))->clashes =
2631               bitVectUnion(OP_SYMBOL(IC_RIGHT(dic))->clashes,
2632                            OP_SYMBOL(IC_LEFT(ic))->clashes);
2633   }
2634   for (lic = ic; lic && lic != dic; lic = lic->prev)
2635     {
2636       bitVectUnSetBit (lic->rlive, IC_LEFT (ic)->key);
2637       if (IS_ITEMP (IC_RIGHT (dic)))
2638         bitVectSetBit (lic->rlive, IC_RIGHT (dic)->key);
2639     }
2640   /* we now we know that it has one & only one def & use
2641      and the that the definition is an assignment */
2642   IC_LEFT (ic) = IC_RIGHT (dic);
2643
2644   remiCodeFromeBBlock (ebp, dic);
2645   bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2646   hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2647 }
2648
2649 /*-----------------------------------------------------------------*/
2650 /* packRegisters - does some transformations to reduce register    */
2651 /*                   pressure                                      */
2652 /*-----------------------------------------------------------------*/
2653 static void
2654 packRegisters (eBBlock * ebp)
2655 {
2656   iCode *ic;
2657   int change = 0;
2658
2659   while (1)
2660     {
2661
2662       change = 0;
2663
2664       /* look for assignments of the form */
2665       /* iTempNN = TRueSym (someoperation) SomeOperand */
2666       /*       ....                       */
2667       /* TrueSym := iTempNN:1             */
2668       for (ic = ebp->sch; ic; ic = ic->next)
2669         {
2670
2671
2672           /* find assignment of the form TrueSym := iTempNN:1 */
2673           if (ic->op == '=' && !POINTER_SET (ic))
2674             change += packRegsForAssign (ic, ebp);
2675         }
2676
2677       if (!change)
2678         break;
2679     }
2680
2681   for (ic = ebp->sch; ic; ic = ic->next)
2682     {
2683
2684       /* if this is an itemp & result of a address of a true sym 
2685          then mark this as rematerialisable   */
2686       if (ic->op == ADDRESS_OF &&
2687           IS_ITEMP (IC_RESULT (ic)) &&
2688           IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2689           bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2690           !OP_SYMBOL (IC_LEFT (ic))->onStack)
2691         {
2692
2693           OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2694           OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2695           OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2696
2697         }
2698
2699         /* if this is an itemp & used as a pointer
2700            & assigned to a literal then remat */
2701         if (IS_ASSIGN_ICODE(ic) && 
2702             IS_ITEMP(IC_RESULT(ic)) &&
2703             bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2704             isOperandLiteral(IC_RIGHT(ic))) 
2705         {
2706           OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2707           OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2708           OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;          
2709         }
2710
2711       /* if straight assignment then carry remat flag if
2712          this is the only definition */
2713       if (ic->op == '=' &&
2714           !POINTER_SET (ic) &&
2715           IS_SYMOP (IC_RIGHT (ic)) &&
2716           OP_SYMBOL (IC_RIGHT (ic))->remat &&
2717           !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2718           bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2719         {
2720
2721           OP_SYMBOL (IC_RESULT (ic))->remat =
2722             OP_SYMBOL (IC_RIGHT (ic))->remat;
2723           OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2724             OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2725         }
2726       
2727       /* if cast to a generic pointer & the pointer being
2728          cast is remat, then we can remat this cast as well */
2729       if (ic->op == CAST && 
2730           IS_SYMOP(IC_RIGHT(ic)) &&
2731           !OP_SYMBOL(IC_RESULT(ic))->isreqv &&
2732           OP_SYMBOL(IC_RIGHT(ic))->remat ) {
2733               sym_link *to_type = operandType(IC_LEFT(ic));
2734               sym_link *from_type = operandType(IC_RIGHT(ic));
2735               if (IS_GENPTR(to_type) && IS_PTR(from_type)) {                  
2736                       OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2737                       OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2738                       OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2739               }
2740       }
2741
2742       /* if this is a +/- operation with a rematerizable 
2743          then mark this as rematerializable as well */
2744       if ((ic->op == '+' || ic->op == '-') &&
2745           (IS_SYMOP (IC_LEFT (ic)) &&
2746            IS_ITEMP (IC_RESULT (ic)) &&
2747            OP_SYMBOL (IC_LEFT (ic))->remat &&
2748            (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
2749            bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2750            IS_OP_LITERAL (IC_RIGHT (ic))))
2751         {
2752
2753           //int i = operandLitValue(IC_RIGHT(ic));
2754           OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2755           OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2756           OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2757         }
2758
2759       /* mark the pointer usages */
2760       if (POINTER_SET (ic))
2761         OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2762
2763       if (POINTER_GET (ic))
2764         OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2765
2766       if (ic->op == RETURN && IS_SYMOP (IC_LEFT(ic)))
2767           OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2768
2769       if (ic->op == RECEIVE && ic->argreg == 1 &&
2770           getSize (operandType(IC_RESULT(ic))) <= 3)
2771           OP_SYMBOL (IC_RESULT(ic))->uptr = 1;
2772
2773       if (ic->op == SEND && ic->argreg == 1 &&
2774           getSize (aggrToPtr(operandType(IC_LEFT(ic)),FALSE)) <= 3)
2775           OP_SYMBOL (IC_LEFT(ic))->uptr = 1;
2776
2777       if (!SKIP_IC2 (ic))
2778         {
2779           /* if we are using a symbol on the stack
2780              then we should say ds390_ptrRegReq */
2781           if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2782                   ds390_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ? !options.stack10bit : 0) +
2783                                       OP_SYMBOL (IC_COND (ic))->iaccess);
2784           else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2785                   ds390_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ? !options.stack10bit : 0) +
2786                                       OP_SYMBOL (IC_JTCOND (ic))->iaccess);
2787           else
2788             {
2789               if (IS_SYMOP (IC_LEFT (ic)))
2790                       ds390_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ? !options.stack10bit : 0) +
2791                                           OP_SYMBOL (IC_LEFT (ic))->iaccess);
2792               if (IS_SYMOP (IC_RIGHT (ic)))
2793                       ds390_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ? !options.stack10bit : 0) +
2794                                           OP_SYMBOL (IC_RIGHT (ic))->iaccess);
2795               if (IS_SYMOP (IC_RESULT (ic)))
2796                       ds390_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ? !options.stack10bit : 0) +
2797                                           OP_SYMBOL (IC_RESULT (ic))->iaccess);
2798             }
2799         }
2800
2801       /* if the condition of an if instruction
2802          is defined in the previous instruction and
2803          this is the only usage then
2804          mark the itemp as a conditional */
2805       if ((IS_CONDITIONAL (ic) ||
2806            (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2807           ic->next && ic->next->op == IFX &&
2808           bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2809           isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2810           OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2811         {
2812           OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2813           continue;
2814         }
2815 #if 1
2816       /* reduce for support function calls */
2817       if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2818         packRegsForSupport (ic, ebp);
2819 #endif
2820       /* some cases the redundant moves can
2821          can be eliminated for return statements . Can be elminated for the first SEND */      
2822       if ((ic->op == RETURN || 
2823            ((ic->op == SEND || ic->op == RECEIVE)&& ic->argreg == 1)) &&          
2824           !isOperandInFarSpace (IC_LEFT (ic)) &&
2825           !options.model) {
2826          
2827           packRegsDPTRuse (IC_LEFT (ic));
2828       }
2829
2830       if (ic->op == CALL) {
2831           sym_link *ftype = operandType(IC_LEFT(ic));
2832           if (getSize(operandType(IC_RESULT(ic))) <= 4 &&
2833               !IFFUNC_ISBUILTIN(ftype)) {
2834               packRegsDPTRuse (IC_RESULT (ic));   
2835           }
2836       }
2837
2838       /* if pointer set & left has a size more than
2839          one and right is not in far space */
2840       if (POINTER_SET (ic) &&
2841           !isOperandInFarSpace (IC_RIGHT (ic)) &&
2842           !OP_SYMBOL (IC_RESULT (ic))->remat &&
2843           !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2844           getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1) {
2845           
2846           packRegsDPTRuse (IC_RESULT (ic));
2847       }
2848
2849       /* if pointer get */
2850       if (POINTER_GET (ic) &&
2851           !isOperandInFarSpace (IC_RESULT (ic)) &&
2852           !OP_SYMBOL (IC_LEFT (ic))->remat &&
2853           !IS_OP_RUONLY (IC_RESULT (ic)) &&
2854           getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1) {
2855
2856           packRegsDPTRuse (IC_LEFT (ic));
2857       }
2858
2859       /* if this is cast for intergral promotion then
2860          check if only use of  the definition of the 
2861          operand being casted/ if yes then replace
2862          the result of that arithmetic operation with 
2863          this result and get rid of the cast */
2864       if (ic->op == CAST)
2865         {
2866           sym_link *fromType = operandType (IC_RIGHT (ic));
2867           sym_link *toType = operandType (IC_LEFT (ic));
2868
2869           if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2870               getSize (fromType) != getSize (toType) &&
2871               SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2872             {
2873
2874               iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
2875               if (dic)
2876                 {
2877                   if (IS_ARITHMETIC_OP (dic))
2878                     {
2879                       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2880                       IC_RESULT (dic) = IC_RESULT (ic);
2881                       remiCodeFromeBBlock (ebp, ic);
2882                       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2883                       hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2884                       OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2885                       ic = ic->prev;
2886                     }
2887                   else
2888                     OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2889                 }
2890             }
2891           else
2892             {
2893
2894               /* if the type from and type to are the same
2895                  then if this is the only use then packit */
2896               if (compareType (operandType (IC_RIGHT (ic)),
2897                              operandType (IC_LEFT (ic))) == 1)
2898                 {
2899                   iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
2900                   if (dic)
2901                     {
2902                       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2903                       IC_RESULT (dic) = IC_RESULT (ic);
2904                       remiCodeFromeBBlock (ebp, ic);
2905                       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2906                       hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2907                       OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2908                       ic = ic->prev;
2909                     }
2910                 }
2911             }
2912         }
2913
2914       /* pack for PUSH 
2915          iTempNN := (some variable in farspace) V1
2916          push iTempNN ;
2917          -------------
2918          push V1
2919        */
2920       if (ic->op == IPUSH || ic->op == SEND)
2921         {
2922           packForPush (ic, ebp);
2923         }
2924
2925
2926       /* pack registers for accumulator use, when the
2927          result of an arithmetic or bit wise operation
2928          has only one use, that use is immediately following
2929          the defintion and the using iCode has only one
2930          operand or has two operands but one is literal &
2931          the result of that operation is not on stack then
2932          we can leave the result of this operation in acc:b
2933          combination */
2934       if ((IS_ARITHMETIC_OP (ic)
2935            || IS_CONDITIONAL(ic)
2936            || IS_BITWISE_OP (ic)
2937            || ic->op == LEFT_OP || ic->op == RIGHT_OP 
2938            || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
2939           ) &&
2940           IS_ITEMP (IC_RESULT (ic)) &&
2941           getSize (operandType (IC_RESULT (ic))) <= 2)
2942
2943         packRegsForAccUse (ic);
2944       
2945     }
2946 }
2947
2948 /*-----------------------------------------------------------------*/
2949 /* assignRegisters - assigns registers to each live range as need  */
2950 /*-----------------------------------------------------------------*/
2951 void
2952 ds390_assignRegisters (eBBlock ** ebbs, int count)
2953 {
2954   iCode *ic;
2955   int i;
2956
2957   setToNull ((void *) &_G.funcrUsed);  
2958   setToNull ((void *) &_G.regAssigned);  
2959   setToNull ((void *) &_G.totRegAssigned);  
2960   setToNull ((void *) &_G.funcrUsed);  
2961   ds390_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2962   ds390_nRegs = 12;
2963   if (options.model != MODEL_FLAT24) options.stack10bit = 0;
2964   /* change assignments this will remove some
2965      live ranges reducing some register pressure */
2966   for (i = 0; i < count; i++)
2967     packRegisters (ebbs[i]);
2968
2969   if (options.dump_pack)
2970     dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2971
2972   /* first determine for each live range the number of 
2973      registers & the type of registers required for each */
2974   regTypeNum ();
2975
2976   /* and serially allocate registers */
2977   serialRegAssign (ebbs, count);
2978
2979   ds390_nRegs = 8;
2980   freeAllRegs ();
2981   fillGaps();
2982   ds390_nRegs = 12;
2983
2984   /* if stack was extended then tell the user */
2985   if (_G.stackExtend)
2986     {
2987 /*      werror(W_TOOMANY_SPILS,"stack", */
2988 /*             _G.stackExtend,currFunc->name,""); */
2989       _G.stackExtend = 0;
2990     }
2991
2992   if (_G.dataExtend)
2993     {
2994 /*      werror(W_TOOMANY_SPILS,"data space", */
2995 /*             _G.dataExtend,currFunc->name,""); */
2996       _G.dataExtend = 0;
2997     }
2998
2999   /* after that create the register mask
3000      for each of the instruction */
3001   createRegMask (ebbs, count);
3002
3003   /* redo that offsets for stacked automatic variables */
3004   redoStackOffsets ();
3005
3006   if (options.dump_rassgn) {
3007     dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
3008     dumpLiveRanges (DUMP_LRANGE, liveRanges);
3009   }
3010
3011   /* do the overlaysegment stuff SDCCmem.c */
3012   doOverlays (ebbs, count);
3013
3014   /* now get back the chain */
3015   ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
3016
3017
3018   gen390Code (ic);
3019
3020   /* free up any _G.stackSpil locations allocated */
3021   applyToSet (_G.stackSpil, deallocStackSpil);
3022   _G.slocNum = 0;
3023   setToNull ((void **) &_G.stackSpil);
3024   setToNull ((void **) &_G.spiltSet);
3025   /* mark all registers as free */
3026   ds390_nRegs = 8;
3027   freeAllRegs ();
3028
3029   return;
3030 }