Changed OP_DEFS and OP_USES from macros to function to catch symbol abuse
[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                 clr = hTabItemWithKey(liveRanges,i);
1440                 assert(clr);
1441                 
1442                 /* mark these registers as used */
1443                 for (k = 0 ; k < clr->nRegs ; k++ ) 
1444                     useReg(clr->regs[k]);
1445             }
1446             
1447             if (willCauseSpill(sym->nRegs,sym->regType)) {
1448                 /* NOPE :( clear all registers & and continue */
1449                 freeAllRegs();
1450                 continue ;
1451             }
1452             
1453             /* THERE IS HOPE !!!! */
1454             for (i=0; i < sym->nRegs ; i++ ) {
1455                 if (sym->regType == REG_PTR)
1456                     sym->regs[i] = getRegPtrNoSpil ();
1457                 else
1458                     sym->regs[i] = getRegGprNoSpil ();            
1459             }
1460             
1461             /* for all its definitions & uses check if the registers
1462                allocated needs positioning NOTE: we can position
1463                only ONCE if more than One positioning required 
1464                then give up */
1465             sym->isspilt = 0;
1466             for (i = 0 ; i < sym->defs->size ; i++ ) {
1467                 if (bitVectBitValue(sym->defs,i)) {
1468                     iCode *ic;
1469                     if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1470                     if (SKIP_IC(ic)) continue;
1471                     assert(isSymbolEqual(sym,OP_SYMBOL(IC_RESULT(ic)))); /* just making sure */
1472                     /* if left is assigned to registers */
1473                     if (IS_SYMOP(IC_LEFT(ic)) && 
1474                         bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_LEFT(ic))->key)) {
1475                         pdone += positionRegs(sym,OP_SYMBOL(IC_LEFT(ic)));
1476                     }
1477                     if (IS_SYMOP(IC_RIGHT(ic)) && 
1478                         bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RIGHT(ic))->key)) {
1479                         pdone += positionRegs(sym,OP_SYMBOL(IC_RIGHT(ic)));
1480                     }
1481                     if (pdone > 1) break;
1482                 }
1483             }
1484             for (i = 0 ; i < sym->uses->size ; i++ ) {
1485                 if (bitVectBitValue(sym->uses,i)) {
1486                     iCode *ic;
1487                     if (!(ic = hTabItemWithKey(iCodehTab,i))) continue ;
1488                     if (SKIP_IC(ic)) continue;
1489                     if (!IS_ASSIGN_ICODE(ic)) continue ;
1490                     
1491                     /* if result is assigned to registers */
1492                     if (IS_SYMOP(IC_RESULT(ic)) && 
1493                         bitVectBitValue(_G.totRegAssigned,OP_SYMBOL(IC_RESULT(ic))->key)) {
1494                         pdone += positionRegs(sym,OP_SYMBOL(IC_RESULT(ic)));
1495                     }
1496                     if (pdone > 1) break;
1497                 }
1498             }
1499             /* had to position more than once GIVE UP */
1500             if (pdone > 1) {
1501                 /* UNDO all the changes we made to try this */
1502                 sym->isspilt = 1;
1503                 for (i=0; i < sym->nRegs ; i++ ) {
1504                     sym->regs[i] = NULL;
1505                 }
1506                 freeAllRegs();
1507                 D (fprintf (stderr, "Fill Gap gave up due to positioning for "
1508                             "%s in function %s\n",
1509                             sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1510                 continue ;          
1511             }
1512             D (fprintf (stderr, "FILLED GAP for %s in function %s\n",
1513                         sym->name, currFunc ? currFunc->name : "UNKNOWN"));
1514             _G.totRegAssigned = bitVectSetBit(_G.totRegAssigned,sym->key);
1515             sym->isspilt = sym->spillA = 0 ;
1516             sym->usl.spillLoc->allocreq--;
1517             sym->usl.spillLoc = NULL;
1518             freeAllRegs();
1519             change ++;
1520         }
1521         if (!change) break;
1522     }
1523 }
1524
1525 /*-----------------------------------------------------------------*/
1526 /* rUmaskForOp :- returns register mask for an operand             */
1527 /*-----------------------------------------------------------------*/
1528 bitVect *
1529 ds390_rUmaskForOp (operand * op)
1530 {
1531   bitVect *rumask;
1532   symbol *sym;
1533   int j;
1534
1535   /* only temporaries are assigned registers */
1536   if (!IS_ITEMP (op))
1537     return NULL;
1538
1539   sym = OP_SYMBOL (op);
1540
1541   /* if spilt or no registers assigned to it
1542      then nothing */
1543   if (sym->isspilt || !sym->nRegs)
1544     return NULL;
1545
1546   rumask = newBitVect (ds390_nRegs);
1547
1548   for (j = 0; j < sym->nRegs; j++)
1549     {
1550       rumask = bitVectSetBit (rumask,
1551                               sym->regs[j]->rIdx);
1552     }
1553
1554   return rumask;
1555 }
1556
1557 /*-----------------------------------------------------------------*/
1558 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1559 /*-----------------------------------------------------------------*/
1560 static bitVect *
1561 regsUsedIniCode (iCode * ic)
1562 {
1563   bitVect *rmask = newBitVect (ds390_nRegs);
1564
1565   /* do the special cases first */
1566   if (ic->op == IFX)
1567     {
1568       rmask = bitVectUnion (rmask,
1569                             ds390_rUmaskForOp (IC_COND (ic)));
1570       goto ret;
1571     }
1572
1573   /* for the jumptable */
1574   if (ic->op == JUMPTABLE)
1575     {
1576       rmask = bitVectUnion (rmask,
1577                             ds390_rUmaskForOp (IC_JTCOND (ic)));
1578
1579       goto ret;
1580     }
1581
1582   /* of all other cases */
1583   if (IC_LEFT (ic))
1584     rmask = bitVectUnion (rmask,
1585                           ds390_rUmaskForOp (IC_LEFT (ic)));
1586
1587
1588   if (IC_RIGHT (ic))
1589     rmask = bitVectUnion (rmask,
1590                           ds390_rUmaskForOp (IC_RIGHT (ic)));
1591
1592   if (IC_RESULT (ic))
1593     rmask = bitVectUnion (rmask,
1594                           ds390_rUmaskForOp (IC_RESULT (ic)));
1595
1596 ret:
1597   return rmask;
1598 }
1599
1600 /*-----------------------------------------------------------------*/
1601 /* createRegMask - for each instruction will determine the regsUsed */
1602 /*-----------------------------------------------------------------*/
1603 static void
1604 createRegMask (eBBlock ** ebbs, int count)
1605 {
1606   int i;
1607
1608   /* for all blocks */
1609   for (i = 0; i < count; i++)
1610     {
1611       iCode *ic;
1612
1613       if (ebbs[i]->noPath &&
1614           (ebbs[i]->entryLabel != entryLabel &&
1615            ebbs[i]->entryLabel != returnLabel))
1616         continue;
1617
1618       /* for all instructions */
1619       for (ic = ebbs[i]->sch; ic; ic = ic->next)
1620         {
1621
1622           int j;
1623
1624           if (SKIP_IC2 (ic) || !ic->rlive)
1625             continue;
1626
1627           /* first mark the registers used in this
1628              instruction */
1629           ic->rUsed = regsUsedIniCode (ic);
1630           _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1631
1632           /* now create the register mask for those 
1633              registers that are in use : this is a
1634              super set of ic->rUsed */
1635           ic->rMask = newBitVect (ds390_nRegs + 1);
1636
1637           /* for all live Ranges alive at this point */
1638           for (j = 1; j < ic->rlive->size; j++)
1639             {
1640               symbol *sym;
1641               int k;
1642
1643               /* if not alive then continue */
1644               if (!bitVectBitValue (ic->rlive, j))
1645                 continue;
1646
1647               /* find the live range we are interested in */
1648               if (!(sym = hTabItemWithKey (liveRanges, j)))
1649                 {
1650                   werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1651                           "createRegMask cannot find live range");
1652                   exit (0);
1653                 }
1654               
1655               /* special case for ruonly */
1656               if (sym->ruonly && sym->liveFrom != sym->liveTo) {
1657                   int size = getSize(sym->type);
1658                   int j = DPL_IDX;
1659                   for (k = 0 ; k < size; k++ )
1660                       ic->rMask = bitVectSetBit (ic->rMask, j++);
1661                   continue ;
1662               }
1663               /* if no register assigned to it */
1664               if (!sym->nRegs || sym->isspilt)
1665                 continue;
1666
1667               /* for all the registers allocated to it */
1668               for (k = 0; k < sym->nRegs; k++)
1669                 if (sym->regs[k])
1670                   ic->rMask =
1671                     bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1672             }
1673         }
1674     }
1675 }
1676
1677 /*-----------------------------------------------------------------*/
1678 /* rematStr - returns the rematerialized string for a remat var    */
1679 /*-----------------------------------------------------------------*/
1680 static char *
1681 rematStr (symbol * sym)
1682 {
1683   char *s = buffer;
1684   iCode *ic = sym->rematiCode;
1685
1686   while (1)
1687     {
1688
1689       /* if plus or minus print the right hand side */
1690       if (ic->op == '+' || ic->op == '-')
1691         {
1692           sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1693                    ic->op);
1694           s += strlen (s);
1695           ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1696           continue;
1697         }
1698       /* cast then continue */
1699       if (IS_CAST_ICODE(ic)) {
1700           ic = OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
1701           continue;
1702       }
1703       /* we reached the end */
1704       sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1705       break;
1706     }
1707
1708   return buffer;
1709 }
1710
1711 /*-----------------------------------------------------------------*/
1712 /* regTypeNum - computes the type & number of registers required   */
1713 /*-----------------------------------------------------------------*/
1714 static void
1715 regTypeNum ()
1716 {
1717   symbol *sym;
1718   int k;
1719   iCode *ic;
1720
1721   /* for each live range do */
1722   for (sym = hTabFirstItem (liveRanges, &k); sym;
1723        sym = hTabNextItem (liveRanges, &k))
1724     {
1725
1726       /* if used zero times then no registers needed */
1727       if ((sym->liveTo - sym->liveFrom) == 0)
1728         continue;
1729
1730
1731       /* if the live range is a temporary */
1732       if (sym->isitmp)
1733         {
1734
1735           /* if the type is marked as a conditional */
1736           if (sym->regType == REG_CND)
1737             continue;
1738
1739           /* if used in return only then we don't 
1740              need registers */
1741           if (sym->ruonly || sym->accuse)
1742             {
1743               if (IS_AGGREGATE (sym->type) || sym->isptr)
1744                 sym->type = aggrToPtr (sym->type, FALSE);
1745               continue;
1746             }
1747
1748           /* if the symbol has only one definition &
1749              that definition is a get_pointer */
1750           if (bitVectnBitsOn (sym->defs) == 1 &&
1751               (ic = hTabItemWithKey (iCodehTab,
1752                                      bitVectFirstBit (sym->defs))) &&
1753               POINTER_GET (ic) &&
1754               !sym->noSpilLoc &&
1755               !IS_BITVAR (sym->etype))
1756             {
1757               /* and that pointer is remat in data space */
1758               if (OP_SYMBOL (IC_LEFT (ic))->remat &&
1759                   !IS_CAST_ICODE(OP_SYMBOL (IC_LEFT (ic))->rematiCode) &&
1760                   DCL_TYPE (aggrToPtr (operandType(IC_LEFT(ic)), FALSE)) == POINTER)
1761                 {
1762
1763                   /* create a psuedo symbol & force a spil */
1764                   symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
1765                   psym->type = sym->type;
1766                   psym->etype = sym->etype;
1767                   strcpy (psym->rname, psym->name);
1768                   sym->isspilt = 1;
1769                   sym->usl.spillLoc = psym;
1770                   continue;
1771                 }
1772
1773               /* if in data space or idata space then try to
1774                  allocate pointer register */
1775
1776             }
1777
1778           /* if not then we require registers */
1779           sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1780                         getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1781                         getSize (sym->type));
1782
1783           if (sym->nRegs > 4)
1784             {
1785               fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1786               printTypeChain (sym->type, stderr);
1787               fprintf (stderr, "\n");
1788             }
1789
1790           /* determine the type of register required */
1791           if (sym->nRegs == 1 &&
1792               IS_PTR (sym->type) &&
1793               sym->uptr)
1794             sym->regType = REG_PTR;
1795           else
1796             sym->regType = REG_GPR;
1797
1798         }
1799       else
1800         /* for the first run we don't provide */
1801         /* registers for true symbols we will */
1802         /* see how things go                  */
1803         sym->nRegs = 0;
1804     }
1805
1806 }
1807
1808 /*-----------------------------------------------------------------*/
1809 /* freeAllRegs - mark all registers as free                        */
1810 /*-----------------------------------------------------------------*/
1811 static void
1812 freeAllRegs ()
1813 {
1814   int i;
1815
1816   for (i = 0; i < ds390_nRegs; i++)
1817     regs390[i].isFree = 1;
1818 }
1819
1820 /*-----------------------------------------------------------------*/
1821 /* deallocStackSpil - this will set the stack pointer back         */
1822 /*-----------------------------------------------------------------*/
1823 static
1824 DEFSETFUNC (deallocStackSpil)
1825 {
1826   symbol *sym = item;
1827
1828   deallocLocal (sym);
1829   return 0;
1830 }
1831
1832 /*-----------------------------------------------------------------*/
1833 /* farSpacePackable - returns the packable icode for far variables */
1834 /*-----------------------------------------------------------------*/
1835 static iCode *
1836 farSpacePackable (iCode * ic)
1837 {
1838   iCode *dic;
1839
1840   /* go thru till we find a definition for the
1841      symbol on the right */
1842   for (dic = ic->prev; dic; dic = dic->prev)
1843     {
1844
1845       /* if the definition is a call then no */
1846       if ((dic->op == CALL || dic->op == PCALL) &&
1847           IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1848         {
1849           return NULL;
1850         }
1851
1852       /* if shift by unknown amount then not */
1853       if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
1854           IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1855         return NULL;
1856
1857       /* if pointer get and size > 1 */
1858       if (POINTER_GET (dic) &&
1859           getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
1860         return NULL;
1861
1862       if (POINTER_SET (dic) &&
1863           getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
1864         return NULL;
1865
1866       /* if any three is a true symbol in far space */
1867       if (IC_RESULT (dic) &&
1868           IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1869           isOperandInFarSpace (IC_RESULT (dic)))
1870         return NULL;
1871
1872       if (IC_RIGHT (dic) &&
1873           IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
1874           isOperandInFarSpace (IC_RIGHT (dic)) &&
1875           !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
1876         return NULL;
1877
1878       if (IC_LEFT (dic) &&
1879           IS_TRUE_SYMOP (IC_LEFT (dic)) &&
1880           isOperandInFarSpace (IC_LEFT (dic)) &&
1881           !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
1882         return NULL;
1883
1884       if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
1885         {
1886           if ((dic->op == LEFT_OP ||
1887                dic->op == RIGHT_OP ||
1888                dic->op == '-') &&
1889               IS_OP_LITERAL (IC_RIGHT (dic)))
1890             return NULL;
1891           else
1892             return dic;
1893         }
1894     }
1895
1896   return NULL;
1897 }
1898
1899 /*-----------------------------------------------------------------*/
1900 /* packRegsForAssign - register reduction for assignment           */
1901 /*-----------------------------------------------------------------*/
1902 static int
1903 packRegsForAssign (iCode * ic, eBBlock * ebp)
1904 {
1905   iCode *dic, *sic;
1906
1907   if (!IS_ITEMP (IC_RIGHT (ic)) ||
1908       OP_SYMBOL (IC_RIGHT (ic))->isind ||
1909       OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
1910     {
1911       return 0;
1912     }
1913
1914   /* if the true symbol is defined in far space or on stack
1915      then we should not since this will increase register pressure */
1916 #if 0
1917   if (isOperandInFarSpace (IC_RESULT (ic)))
1918     {
1919       if ((dic = farSpacePackable (ic)))
1920         goto pack;
1921       else
1922         return 0;
1923     }
1924 #else
1925   if (isOperandInFarSpace(IC_RESULT(ic)) && !farSpacePackable(ic)) {
1926     return 0;
1927   }
1928 #endif
1929
1930   /* find the definition of iTempNN scanning backwards if we find a 
1931      a use of the true symbol in before we find the definition then 
1932      we cannot */
1933   for (dic = ic->prev; dic; dic = dic->prev)
1934     {
1935       /* if there is a function call then don't pack it */
1936       if ((dic->op == CALL || dic->op == PCALL))
1937         {
1938           dic = NULL;
1939           break;
1940         }
1941
1942       if (SKIP_IC2 (dic))
1943         continue;
1944
1945       if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
1946           IS_OP_VOLATILE (IC_RESULT (dic)))
1947         {
1948           dic = NULL;
1949           break;
1950         }
1951
1952       if (IS_SYMOP (IC_RESULT (dic)) &&
1953           IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1954         {
1955           if (POINTER_SET (dic))
1956             dic = NULL;
1957
1958           break;
1959         }
1960
1961       if (IS_SYMOP (IC_RIGHT (dic)) &&
1962           (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1963            IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1964         {
1965           dic = NULL;
1966           break;
1967         }
1968
1969       if (IS_SYMOP (IC_LEFT (dic)) &&
1970           (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1971            IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1972         {
1973           dic = NULL;
1974           break;
1975         }
1976
1977       if (POINTER_SET (dic) &&
1978           IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1979         {
1980           dic = NULL;
1981           break;
1982         }
1983     }
1984
1985   if (!dic)
1986     return 0;                   /* did not find */
1987
1988   /* if the result is on stack or iaccess then it must be
1989      the same atleast one of the operands */
1990   if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1991       OP_SYMBOL (IC_RESULT (ic))->iaccess)
1992     {
1993
1994       /* the operation has only one symbol
1995          operator then we can pack */
1996       if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1997           (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1998         goto pack;
1999
2000       if (!((IC_LEFT (dic) &&
2001              IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
2002             (IC_RIGHT (dic) &&
2003              IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
2004         return 0;
2005     }
2006 pack:
2007   /* found the definition */
2008   /* replace the result with the result of */
2009   /* this assignment and remove this assignment */
2010   bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2011
2012   IC_RESULT (dic) = IC_RESULT (ic);
2013
2014   if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
2015     {
2016       OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
2017     }
2018   /* delete from liverange table also 
2019      delete from all the points inbetween and the new
2020      one */
2021   for (sic = dic; sic != ic; sic = sic->next)
2022     {
2023       bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
2024       if (IS_ITEMP (IC_RESULT (dic)))
2025         bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
2026     }
2027
2028   remiCodeFromeBBlock (ebp, ic);
2029   bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2030   hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2031   OP_DEFS_SET ((IC_RESULT (dic)), bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key));
2032   return 1;
2033
2034 }
2035
2036 /*-----------------------------------------------------------------*/
2037 /* findAssignToSym : scanning backwards looks for first assig found */
2038 /*-----------------------------------------------------------------*/
2039 static iCode *
2040 findAssignToSym (operand * op, iCode * ic)
2041 {
2042   iCode *dic;
2043
2044   for (dic = ic->prev; dic; dic = dic->prev)
2045     {
2046
2047       /* if definition by assignment */
2048       if (dic->op == '=' &&
2049           !POINTER_SET (dic) &&
2050           IC_RESULT (dic)->key == op->key
2051 /*          &&  IS_TRUE_SYMOP(IC_RIGHT(dic)) */
2052         )
2053         {
2054
2055           /* we are interested only if defined in far space */
2056           /* or in stack space in case of + & - */
2057
2058           /* if assigned to a non-symbol then return
2059              FALSE */
2060           if (!IS_SYMOP (IC_RIGHT (dic)))
2061             return NULL;
2062
2063           /* if the symbol is in far space then
2064              we should not */
2065           if (isOperandInFarSpace (IC_RIGHT (dic)))
2066             return NULL;
2067
2068           /* for + & - operations make sure that
2069              if it is on the stack it is the same
2070              as one of the three operands */
2071           if ((ic->op == '+' || ic->op == '-') &&
2072               OP_SYMBOL (IC_RIGHT (dic))->onStack)
2073             {
2074
2075               if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
2076                   IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
2077                   IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
2078                 return NULL;
2079             }
2080
2081           break;
2082
2083         }
2084
2085       /* if we find an usage then we cannot delete it */
2086       if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
2087         return NULL;
2088
2089       if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
2090         return NULL;
2091
2092       if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
2093         return NULL;
2094     }
2095
2096   /* now make sure that the right side of dic
2097      is not defined between ic & dic */
2098   if (dic)
2099     {
2100       iCode *sic = dic->next;
2101
2102       for (; sic != ic; sic = sic->next)
2103         if (IC_RESULT (sic) &&
2104             IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2105           return NULL;
2106     }
2107
2108   return dic;
2109
2110
2111 }
2112
2113 /*-----------------------------------------------------------------*/
2114 /* packRegsForSupport :- reduce some registers for support calls   */
2115 /*-----------------------------------------------------------------*/
2116 static int
2117 packRegsForSupport (iCode * ic, eBBlock * ebp)
2118 {    
2119   int change = 0;
2120   
2121   /* for the left & right operand :- look to see if the
2122      left was assigned a true symbol in far space in that
2123      case replace them */
2124   if (IS_ITEMP (IC_LEFT (ic)) &&
2125       OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2126     {
2127       iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
2128       iCode *sic;
2129
2130       if (!dic)
2131         goto right;
2132
2133       /* found it we need to remove it from the
2134          block */
2135       for (sic = dic; sic != ic; sic = sic->next) {
2136         bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
2137         sic->rlive = bitVectSetBit (sic->rlive, IC_RIGHT (dic)->key);
2138       }
2139
2140       IC_LEFT (ic)->operand.symOperand =
2141         IC_RIGHT (dic)->operand.symOperand;
2142       OP_SYMBOL(IC_LEFT(ic))->liveTo = ic->seq;
2143       IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2144       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2145       remiCodeFromeBBlock (ebp, dic);
2146       hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2147       change++;
2148     }
2149
2150   /* do the same for the right operand */
2151 right:
2152   if (!change &&
2153       IS_ITEMP (IC_RIGHT (ic)) &&
2154       OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2155     {
2156       iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2157       iCode *sic;
2158
2159       if (!dic)
2160         return change;
2161
2162       /* if this is a subtraction & the result
2163          is a true symbol in far space then don't pack */
2164       if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2165         {
2166           sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2167           if (IN_FARSPACE (SPEC_OCLS (etype)))
2168             return change;
2169         }
2170       /* found it we need to remove it from the
2171          block */
2172       for (sic = dic; sic != ic; sic = sic->next) {
2173         bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
2174         sic->rlive = bitVectSetBit (sic->rlive, IC_RIGHT (dic)->key);
2175       }
2176
2177       IC_RIGHT (ic)->operand.symOperand =
2178         IC_RIGHT (dic)->operand.symOperand;
2179       IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2180       OP_SYMBOL(IC_RIGHT(ic))->liveTo = ic->seq;
2181       remiCodeFromeBBlock (ebp, dic);
2182       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2183       hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2184       change++;
2185     }
2186
2187   return change;
2188 }
2189
2190 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2191
2192
2193 /*-----------------------------------------------------------------*/
2194 /* packRegsDPTRnuse - color live ranges that can go into extra DPTRS */
2195 /*-----------------------------------------------------------------*/
2196 static int packRegsDPTRnuse( operand *op , int dptr)
2197 {
2198     int i,key;
2199     iCode *ic;
2200
2201     if (!IS_SYMOP(op) || !IS_ITEMP(op)) return 0;
2202     if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly || OP_SYMBOL(op)->dptr) 
2203         return 0; 
2204     
2205     /* first check if any overlapping liverange has already been
2206        assigned to this DPTR */
2207     if (OP_SYMBOL(op)->clashes) {
2208         for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ ) {
2209             symbol *sym;
2210             if (bitVectBitValue(OP_SYMBOL(op)->clashes,i)) {
2211                 sym = hTabItemWithKey(liveRanges,i);
2212                 if (sym->dptr == dptr) return 0;
2213             }
2214         }
2215     }
2216    
2217     /* future for more dptrs */
2218     if (dptr > 1) {
2219         OP_SYMBOL(op)->dptr = dptr;
2220         return 1;
2221     }
2222
2223     /* DPTR1 is special since it is also used as a scratch by the backend .
2224        so we walk thru the entire live range of this operand and make sure
2225        DPTR1 will not be used by the backed . The logic here is to find out if 
2226        more than one operand in an icode is in far space then we give up : we 
2227        don't keep it live across functions for now
2228     */
2229     
2230     ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2231     for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2232          ic = hTabNextItem(iCodeSeqhTab,&key)) {
2233         int nfs =0;
2234         
2235         if (ic->op == CALL || ic->op == PCALL) return 0;
2236
2237         /* single operand icode are ok */
2238         if (ic->op == IFX || ic->op == IPUSH)
2239             continue ;
2240
2241         if (ic->op == SEND ) {
2242             if (ic->argreg != 1 ) return 0;
2243             else continue ;
2244         }
2245         /* two special cases first */
2246         if (POINTER_GET(ic) && !isOperandEqual(IC_LEFT(ic),op)  && /* pointer get */
2247             !OP_SYMBOL(IC_LEFT(ic))->ruonly                     && /* with result in far space */
2248             (isOperandInFarSpace(IC_RESULT(ic)) &&              
2249              !isOperandInReg(IC_RESULT(ic)))) {
2250             return 0;
2251         }
2252
2253         if (POINTER_SET(ic) && !isOperandEqual(IC_RESULT(ic),op)        && /* pointer set */
2254             !OP_SYMBOL(IC_RESULT(ic))->ruonly                           && /* with right in far space */
2255             (isOperandInFarSpace(IC_RIGHT(ic)) &&               
2256              !isOperandInReg(IC_RIGHT(ic)))) {
2257             return 0;
2258         }
2259
2260         if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic))    && /* if symbol operand */
2261             !isOperandEqual(IC_RESULT(ic),op)           && /* not the same as this */
2262             ((isOperandInFarSpace(IC_RESULT(ic)) ||        /* in farspace or */
2263               OP_SYMBOL(IC_RESULT(ic))->onStack)        && /* on the stack   */
2264              !isOperandInReg(IC_RESULT(ic)))) {            /* and not in register */
2265             nfs++;
2266         }
2267         /* same for left */
2268         if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic))        && /* if symbol operand */
2269             !isOperandEqual(IC_LEFT(ic),op)             && /* not the same as this */
2270             ((isOperandInFarSpace(IC_LEFT(ic)) ||          /* in farspace or */
2271               OP_SYMBOL(IC_LEFT(ic))->onStack)          && /* on the stack   */
2272              !isOperandInReg(IC_LEFT(ic)))) {              /* and not in register */
2273             nfs++;
2274         }
2275         /* same for right */
2276         if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic))      && /* if symbol operand */
2277             !isOperandEqual(IC_RIGHT(ic),op)            && /* not the same as this */
2278             ((isOperandInFarSpace(IC_RIGHT(ic)) ||         /* in farspace or */
2279               OP_SYMBOL(IC_RIGHT(ic))->onStack)         && /* on the stack   */
2280              !isOperandInReg(IC_RIGHT(ic)))) {             /* and not in register */
2281             nfs++;
2282         }
2283         
2284         if (nfs && IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) &&
2285             OP_SYMBOL(IC_RESULT(ic))->ruonly) return 0;
2286
2287         if (nfs > 1) return 0;
2288     }
2289     OP_SYMBOL(op)->dptr = dptr;
2290     return 1;
2291 }
2292
2293 /*-----------------------------------------------------------------*/
2294 /* packRegsDPTRuse : - will reduce some registers for single Use */
2295 /*-----------------------------------------------------------------*/
2296 static iCode *
2297 packRegsDPTRuse (operand * op)
2298 {
2299     /* go thru entire liveRange of this variable & check for
2300        other possible usage of DPTR , if we don't find it the
2301        assign this to DPTR (ruonly)
2302     */
2303     int i, key;
2304     symbol *sym;
2305     iCode *ic, *dic;
2306     sym_link *type, *etype;
2307     
2308     if (!IS_SYMOP(op) || !IS_ITEMP(op)) return NULL;
2309     if (OP_SYMBOL(op)->remat || OP_SYMBOL(op)->ruonly) return NULL; 
2310
2311     /* first check if any overlapping liverange has already been
2312        assigned to DPTR */
2313     if (OP_SYMBOL(op)->clashes) {
2314         for (i = 0 ; i < OP_SYMBOL(op)->clashes->size ; i++ ) {
2315             if (bitVectBitValue(OP_SYMBOL(op)->clashes,i)) {
2316                 sym = hTabItemWithKey(liveRanges,i);
2317                 if (sym->ruonly) return NULL ;
2318             }
2319         }
2320     }
2321
2322     /* no then go thru this guys live range */
2323     dic = ic = hTabFirstItemWK(iCodeSeqhTab,OP_SYMBOL(op)->liveFrom);
2324     for (; ic && ic->seq <= OP_SYMBOL(op)->liveTo;
2325          ic = hTabNextItem(iCodeSeqhTab,&key)) {
2326
2327         if (SKIP_IC3(ic)) continue;
2328
2329         /* if PCALL cannot be sure give up */
2330         if (ic->op == PCALL) return NULL;
2331
2332         /* if SEND & not the first parameter then giveup */
2333         if (ic->op == SEND && ic->argreg != 1 &&
2334             ((isOperandInFarSpace(IC_LEFT(ic))  && !isOperandInReg(IC_LEFT(ic))) || 
2335              isOperandEqual(op,IC_LEFT(ic)))) return NULL;
2336
2337         /* if CALL then make sure it is VOID || return value not used 
2338            or the return value is assigned to this one */
2339         if (ic->op == CALL) {
2340             if (OP_SYMBOL(IC_RESULT(ic))->liveTo == 
2341                 OP_SYMBOL(IC_RESULT(ic))->liveFrom) continue ;
2342             etype = getSpec(type = operandType(IC_RESULT(ic)));
2343             if (getSize(type) == 0 || isOperandEqual(op,IC_RESULT(ic))) 
2344                 continue ;
2345             return NULL ;
2346         }
2347
2348         /* special case of add with a [remat] */
2349         if (ic->op == '+' && 
2350             OP_SYMBOL(IC_LEFT(ic))->remat &&
2351             (isOperandInFarSpace(IC_RIGHT(ic)) &&
2352              !isOperandInReg(IC_RIGHT(ic)))) return NULL ;
2353
2354         /* special cases  */
2355         /* pointerGet */
2356         if (POINTER_GET(ic) && !isOperandEqual(IC_LEFT(ic),op) &&
2357             getSize(operandType(IC_LEFT(ic))) > 1 ) return NULL ;
2358
2359         /* pointerSet */
2360         if (POINTER_SET(ic) && !isOperandEqual(IC_RESULT(ic),op) &&
2361             getSize(operandType(IC_RESULT(ic))) > 1 ) return NULL;
2362
2363         /* conditionals can destroy 'b' - make sure B wont 
2364            be used in this one*/
2365         if ((IS_CONDITIONAL(ic) || ic->op == '*' || ic->op == '/'  || 
2366              ic->op == LEFT_OP || ic->op == RIGHT_OP ) && 
2367             getSize(operandType(op)) > 3) return NULL;
2368
2369         /* if this is a cast to a bigger type */
2370         if (ic->op==CAST) {
2371           if (!IS_PTR(OP_SYM_TYPE(IC_RESULT(ic))) && 
2372               getSize(OP_SYM_TYPE(IC_RESULT(ic))) >
2373               getSize(OP_SYM_TYPE(IC_RIGHT(ic)))) {
2374             return 0;
2375           }
2376         }
2377
2378         /* general case */
2379         if (IC_RESULT(ic) && IS_SYMOP(IC_RESULT(ic)) && 
2380             !isOperandEqual(IC_RESULT(ic),op) &&
2381             ( ( ( isOperandInFarSpace(IC_RESULT(ic)) || OP_SYMBOL(IC_RESULT(ic))->onStack) && 
2382                 !isOperandInReg(IC_RESULT(ic))) || 
2383              OP_SYMBOL(IC_RESULT(ic))->ruonly)) return NULL;
2384
2385         if (IC_RIGHT(ic) && IS_SYMOP(IC_RIGHT(ic)) && 
2386             !isOperandEqual(IC_RIGHT(ic),op) &&
2387             (OP_SYMBOL(IC_RIGHT(ic))->liveTo >= ic->seq || 
2388              IS_TRUE_SYMOP(IC_RIGHT(ic))               ||
2389              OP_SYMBOL(IC_RIGHT(ic))->ruonly) &&
2390             ( ( isOperandInFarSpace(IC_RIGHT(ic)) || OP_SYMBOL(IC_RIGHT(ic))->onStack) && 
2391                 !isOperandInReg(IC_RIGHT(ic))) ) return NULL;
2392
2393         if (IC_LEFT(ic) && IS_SYMOP(IC_LEFT(ic)) && 
2394             !isOperandEqual(IC_LEFT(ic),op) &&
2395             (OP_SYMBOL(IC_LEFT(ic))->liveTo >= ic->seq || 
2396              IS_TRUE_SYMOP(IC_LEFT(ic))               ||
2397              OP_SYMBOL(IC_LEFT(ic))->ruonly) &&
2398             ( ( isOperandInFarSpace(IC_LEFT(ic)) || OP_SYMBOL(IC_LEFT(ic))->onStack) && 
2399                 !isOperandInReg(IC_LEFT(ic))) ) return NULL;
2400         
2401         if (IC_LEFT(ic) && IC_RIGHT(ic) && 
2402             IS_ITEMP(IC_LEFT(ic)) && IS_ITEMP(IC_RIGHT(ic)) &&
2403             (isOperandInFarSpace(IC_LEFT(ic)) && !isOperandInReg(IC_LEFT(ic))) && 
2404             (isOperandInFarSpace(IC_RIGHT(ic)) && !isOperandInReg(IC_RIGHT(ic))))
2405             return NULL;
2406     }
2407     OP_SYMBOL(op)->ruonly = 1;
2408     if (OP_SYMBOL(op)->usl.spillLoc) {
2409         if (OP_SYMBOL(op)->spillA)
2410             OP_SYMBOL(op)->usl.spillLoc->allocreq--;
2411         OP_SYMBOL(op)->usl.spillLoc = NULL;
2412     }
2413     return dic;
2414 }
2415
2416 /*-----------------------------------------------------------------*/
2417 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN          */
2418 /*-----------------------------------------------------------------*/
2419 static bool
2420 isBitwiseOptimizable (iCode * ic)
2421 {
2422   sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2423   sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2424
2425   /* bitwise operations are considered optimizable
2426      under the following conditions (Jean-Louis VERN) 
2427
2428      x & lit
2429      bit & bit
2430      bit & x
2431      bit ^ bit
2432      bit ^ x
2433      x   ^ lit
2434      x   | lit
2435      bit | bit
2436      bit | x
2437    */
2438   if ( IS_LITERAL (rtype) ||
2439       (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2440     return TRUE;
2441   else
2442     return FALSE;
2443 }
2444
2445 /*-----------------------------------------------------------------*/
2446 /* packRegsForAccUse - pack registers for acc use                  */
2447 /*-----------------------------------------------------------------*/
2448 static void
2449 packRegsForAccUse (iCode * ic)
2450 {
2451   iCode *uic;
2452
2453   /* if this is an aggregate, e.g. a one byte char array */
2454   if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
2455     return;
2456   }
2457
2458   /* if we are calling a reentrant function that has stack parameters */
2459   if (ic->op == CALL &&
2460        IFFUNC_ISREENT(operandType(IC_LEFT(ic))) &&
2461        FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))))
2462       return;
2463
2464   if (ic->op == PCALL &&
2465        IFFUNC_ISREENT(operandType(IC_LEFT(ic))->next) &&
2466        FUNC_HASSTACKPARM(operandType(IC_LEFT(ic))->next))
2467       return;
2468
2469   /* if + or - then it has to be one byte result */
2470   if ((ic->op == '+' || ic->op == '-')
2471       && getSize (operandType (IC_RESULT (ic))) > 1)
2472     return;
2473
2474   /* if shift operation make sure right side is not a literal */
2475   if (ic->op == RIGHT_OP &&
2476       (isOperandLiteral (IC_RIGHT (ic)) ||
2477        getSize (operandType (IC_RESULT (ic))) > 1))
2478     return;
2479
2480   if (ic->op == LEFT_OP &&
2481       (isOperandLiteral (IC_RIGHT (ic)) ||
2482        getSize (operandType (IC_RESULT (ic))) > 1))
2483     return;
2484
2485   if (IS_BITWISE_OP (ic) &&
2486       getSize (operandType (IC_RESULT (ic))) > 1)
2487     return;
2488
2489
2490   /* has only one definition */
2491   if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2492     return;
2493
2494   /* has only one use */
2495   if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2496     return;
2497
2498   /* and the usage immediately follows this iCode */
2499   if (!(uic = hTabItemWithKey (iCodehTab,
2500                                bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2501     return;
2502
2503   if (ic->next != uic)
2504     return;
2505
2506   /* if it is a conditional branch then we definitely can */
2507   if (uic->op == IFX)
2508     goto accuse;
2509
2510   if (uic->op == JUMPTABLE)
2511     return;
2512
2513   /* if the usage is not is an assignment
2514      or an arithmetic / bitwise / shift operation then not */
2515   if (POINTER_SET (uic) &&
2516       getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2517     return;
2518
2519   if (uic->op != '=' &&
2520       !IS_ARITHMETIC_OP (uic) &&
2521       !IS_BITWISE_OP (uic) &&
2522       uic->op != LEFT_OP &&
2523       uic->op != RIGHT_OP)
2524     return;
2525
2526   /* if used in ^ operation then make sure right is not a 
2527      literl */
2528   if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2529     return;
2530
2531   /* if shift operation make sure right side is not a literal */
2532   if (uic->op == RIGHT_OP &&
2533       (isOperandLiteral (IC_RIGHT (uic)) ||
2534        getSize (operandType (IC_RESULT (uic))) > 1))
2535     return;
2536
2537   if (uic->op == LEFT_OP &&
2538       (isOperandLiteral (IC_RIGHT (uic)) ||
2539        getSize (operandType (IC_RESULT (uic))) > 1))
2540     return;
2541
2542   /* make sure that the result of this icode is not on the
2543      stack, since acc is used to compute stack offset */
2544   if (isOperandOnStack(IC_RESULT(uic)))
2545     return;
2546
2547   /* if either one of them in far space then we cannot */
2548   if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2549        isOperandInFarSpace (IC_LEFT (uic))) ||
2550       (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2551        isOperandInFarSpace (IC_RIGHT (uic))))
2552     return;
2553
2554   /* if the usage has only one operand then we can */
2555   if (IC_LEFT (uic) == NULL ||
2556       IC_RIGHT (uic) == NULL)
2557     goto accuse;
2558
2559   /* make sure this is on the left side if not
2560      a '+' since '+' is commutative */
2561   if (ic->op != '+' &&
2562       IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2563     return;
2564
2565   /* if the other one is not on stack then we can */
2566   if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2567       (IS_ITEMP (IC_RIGHT (uic)) ||
2568        (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2569         !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
2570     goto accuse;
2571
2572   if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2573       (IS_ITEMP (IC_LEFT (uic)) ||
2574        (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2575         !OP_SYMBOL (IC_LEFT (uic))->onStack)))
2576     goto accuse;
2577
2578   return;
2579
2580 accuse:
2581   OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2582
2583
2584 }
2585
2586 /*-----------------------------------------------------------------*/
2587 /* packForPush - hueristics to reduce iCode for pushing            */
2588 /*-----------------------------------------------------------------*/
2589 static void
2590 packForPush (iCode * ic, eBBlock * ebp)
2591 {
2592   iCode *dic, *lic;
2593   bitVect *dbv;
2594
2595   if ((ic->op != IPUSH && ic->op != SEND) || !IS_ITEMP (IC_LEFT (ic)))
2596     return;
2597
2598   /* must have only definition & one usage */
2599   if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2600       bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2601     return;
2602
2603   /* find the definition */
2604   if (!(dic = hTabItemWithKey (iCodehTab,
2605                                bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2606     return;
2607
2608   if (dic->op != '=' || POINTER_SET (dic))
2609     return;
2610   
2611   if (dic->eBBlockNum != ic->eBBlockNum) return ;
2612
2613   /* make sure the right side does not have any definitions
2614      inbetween */
2615   dbv = OP_DEFS(IC_RIGHT(dic));
2616   for (lic = ic; lic && lic != dic ; lic = lic->prev) {
2617           if (bitVectBitValue(dbv,lic->key)) return ;
2618   }
2619   /* make sure they have the same type */
2620   {
2621     sym_link *itype=operandType(IC_LEFT(ic));
2622     sym_link *ditype=operandType(IC_RIGHT(dic));
2623
2624     if (SPEC_USIGN(itype)!=SPEC_USIGN(ditype) ||
2625         SPEC_LONG(itype)!=SPEC_LONG(ditype))
2626       return;
2627   }
2628   /* extend the live range of replaced operand if needed */
2629   if (OP_SYMBOL(IC_RIGHT(dic))->liveTo < OP_SYMBOL(IC_LEFT(ic))->liveTo) {
2630           OP_SYMBOL(IC_RIGHT(dic))->liveTo = OP_SYMBOL(IC_LEFT(ic))->liveTo;
2631           OP_SYMBOL(IC_RIGHT(dic))->clashes =
2632               bitVectUnion(OP_SYMBOL(IC_RIGHT(dic))->clashes,
2633                            OP_SYMBOL(IC_LEFT(ic))->clashes);
2634   }
2635   for (lic = ic; lic && lic != dic; lic = lic->prev)
2636     {
2637       bitVectUnSetBit (lic->rlive, IC_LEFT (ic)->key);
2638       if (IS_ITEMP (IC_RIGHT (dic)))
2639         bitVectSetBit (lic->rlive, IC_RIGHT (dic)->key);
2640     }
2641   /* we now we know that it has one & only one def & use
2642      and the that the definition is an assignment */
2643   IC_LEFT (ic) = IC_RIGHT (dic);
2644
2645   remiCodeFromeBBlock (ebp, dic);
2646   bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2647   hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2648 }
2649
2650 /*-----------------------------------------------------------------*/
2651 /* packRegisters - does some transformations to reduce register    */
2652 /*                   pressure                                      */
2653 /*-----------------------------------------------------------------*/
2654 static void
2655 packRegisters (eBBlock * ebp)
2656 {
2657   iCode *ic;
2658   int change = 0;
2659
2660   while (1)
2661     {
2662
2663       change = 0;
2664
2665       /* look for assignments of the form */
2666       /* iTempNN = TRueSym (someoperation) SomeOperand */
2667       /*       ....                       */
2668       /* TrueSym := iTempNN:1             */
2669       for (ic = ebp->sch; ic; ic = ic->next)
2670         {
2671
2672
2673           /* find assignment of the form TrueSym := iTempNN:1 */
2674           if (ic->op == '=' && !POINTER_SET (ic))
2675             change += packRegsForAssign (ic, ebp);
2676         }
2677
2678       if (!change)
2679         break;
2680     }
2681
2682   for (ic = ebp->sch; ic; ic = ic->next)
2683     {
2684
2685       /* if this is an itemp & result of a address of a true sym 
2686          then mark this as rematerialisable   */
2687       if (ic->op == ADDRESS_OF &&
2688           IS_ITEMP (IC_RESULT (ic)) &&
2689           IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2690           bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2691           !OP_SYMBOL (IC_LEFT (ic))->onStack)
2692         {
2693
2694           OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2695           OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2696           OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2697
2698         }
2699
2700         /* if this is an itemp & used as a pointer
2701            & assigned to a literal then remat */
2702         if (IS_ASSIGN_ICODE(ic) && 
2703             IS_ITEMP(IC_RESULT(ic)) &&
2704             bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2705             isOperandLiteral(IC_RIGHT(ic))) 
2706         {
2707           OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2708           OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2709           OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;          
2710         }
2711
2712       /* if straight assignment then carry remat flag if
2713          this is the only definition */
2714       if (ic->op == '=' &&
2715           !POINTER_SET (ic) &&
2716           IS_SYMOP (IC_RIGHT (ic)) &&
2717           OP_SYMBOL (IC_RIGHT (ic))->remat &&
2718           !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode) &&
2719           bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2720         {
2721
2722           OP_SYMBOL (IC_RESULT (ic))->remat =
2723             OP_SYMBOL (IC_RIGHT (ic))->remat;
2724           OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2725             OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2726         }
2727       
2728       /* if cast to a generic pointer & the pointer being
2729          cast is remat, then we can remat this cast as well */
2730       if (ic->op == CAST && 
2731           IS_SYMOP(IC_RIGHT(ic)) &&
2732           !OP_SYMBOL(IC_RESULT(ic))->isreqv &&
2733           OP_SYMBOL(IC_RIGHT(ic))->remat ) {
2734               sym_link *to_type = operandType(IC_LEFT(ic));
2735               sym_link *from_type = operandType(IC_RIGHT(ic));
2736               if (IS_GENPTR(to_type) && IS_PTR(from_type)) {                  
2737                       OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2738                       OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2739                       OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2740               }
2741       }
2742
2743       /* if this is a +/- operation with a rematerizable 
2744          then mark this as rematerializable as well */
2745       if ((ic->op == '+' || ic->op == '-') &&
2746           (IS_SYMOP (IC_LEFT (ic)) &&
2747            IS_ITEMP (IC_RESULT (ic)) &&
2748            OP_SYMBOL (IC_LEFT (ic))->remat &&
2749            (!IS_SYMOP (IC_RIGHT (ic)) || !IS_CAST_ICODE(OP_SYMBOL (IC_RIGHT (ic))->rematiCode)) &&
2750            bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2751            IS_OP_LITERAL (IC_RIGHT (ic))))
2752         {
2753
2754           //int i = operandLitValue(IC_RIGHT(ic));
2755           OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2756           OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2757           OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2758         }
2759
2760       /* mark the pointer usages */
2761       if (POINTER_SET (ic))
2762         OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2763
2764       if (POINTER_GET (ic))
2765         OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2766
2767       if (ic->op == RETURN && IS_SYMOP (IC_LEFT(ic)))
2768           OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2769
2770       if (ic->op == RECEIVE && ic->argreg == 1 &&
2771           getSize (operandType(IC_RESULT(ic))) <= 3)
2772           OP_SYMBOL (IC_RESULT(ic))->uptr = 1;
2773
2774       if (ic->op == SEND && ic->argreg == 1 &&
2775           getSize (aggrToPtr(operandType(IC_LEFT(ic)),FALSE)) <= 3)
2776           OP_SYMBOL (IC_LEFT(ic))->uptr = 1;
2777
2778       if (!SKIP_IC2 (ic))
2779         {
2780           /* if we are using a symbol on the stack
2781              then we should say ds390_ptrRegReq */
2782           if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2783                   ds390_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ? !options.stack10bit : 0) +
2784                                       OP_SYMBOL (IC_COND (ic))->iaccess);
2785           else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2786                   ds390_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ? !options.stack10bit : 0) +
2787                                       OP_SYMBOL (IC_JTCOND (ic))->iaccess);
2788           else
2789             {
2790               if (IS_SYMOP (IC_LEFT (ic)))
2791                       ds390_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ? !options.stack10bit : 0) +
2792                                           OP_SYMBOL (IC_LEFT (ic))->iaccess);
2793               if (IS_SYMOP (IC_RIGHT (ic)))
2794                       ds390_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ? !options.stack10bit : 0) +
2795                                           OP_SYMBOL (IC_RIGHT (ic))->iaccess);
2796               if (IS_SYMOP (IC_RESULT (ic)))
2797                       ds390_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ? !options.stack10bit : 0) +
2798                                           OP_SYMBOL (IC_RESULT (ic))->iaccess);
2799             }
2800         }
2801
2802       /* if the condition of an if instruction
2803          is defined in the previous instruction and
2804          this is the only usage then
2805          mark the itemp as a conditional */
2806       if ((IS_CONDITIONAL (ic) ||
2807            (IS_BITWISE_OP(ic) && isBitwiseOptimizable (ic))) &&
2808           ic->next && ic->next->op == IFX &&
2809           bitVectnBitsOn (OP_USES(IC_RESULT(ic)))==1 &&
2810           isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2811           OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2812         {
2813           OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2814           continue;
2815         }
2816 #if 1
2817       /* reduce for support function calls */
2818       if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2819         packRegsForSupport (ic, ebp);
2820 #endif
2821       /* some cases the redundant moves can
2822          can be eliminated for return statements . Can be elminated for the first SEND */      
2823       if ((ic->op == RETURN || 
2824            ((ic->op == SEND || ic->op == RECEIVE)&& ic->argreg == 1)) &&          
2825           !isOperandInFarSpace (IC_LEFT (ic)) &&
2826           !options.model) {
2827          
2828           packRegsDPTRuse (IC_LEFT (ic));
2829       }
2830
2831       if (ic->op == CALL) {
2832           sym_link *ftype = operandType(IC_LEFT(ic));
2833           if (getSize(operandType(IC_RESULT(ic))) <= 4 &&
2834               !IFFUNC_ISBUILTIN(ftype)) {
2835               packRegsDPTRuse (IC_RESULT (ic));   
2836           }
2837       }
2838
2839       /* if pointer set & left has a size more than
2840          one and right is not in far space */
2841       if (POINTER_SET (ic) &&
2842           !isOperandInFarSpace (IC_RIGHT (ic)) &&
2843           !OP_SYMBOL (IC_RESULT (ic))->remat &&
2844           !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2845           getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1) {
2846           
2847           packRegsDPTRuse (IC_RESULT (ic));
2848       }
2849
2850       /* if pointer get */
2851       if (POINTER_GET (ic) &&
2852           !isOperandInFarSpace (IC_RESULT (ic)) &&
2853           !OP_SYMBOL (IC_LEFT (ic))->remat &&
2854           !IS_OP_RUONLY (IC_RESULT (ic)) &&
2855           getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1) {
2856
2857           packRegsDPTRuse (IC_LEFT (ic));
2858       }
2859
2860       /* if this is cast for intergral promotion then
2861          check if only use of  the definition of the 
2862          operand being casted/ if yes then replace
2863          the result of that arithmetic operation with 
2864          this result and get rid of the cast */
2865       if (ic->op == CAST)
2866         {
2867           sym_link *fromType = operandType (IC_RIGHT (ic));
2868           sym_link *toType = operandType (IC_LEFT (ic));
2869
2870           if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2871               getSize (fromType) != getSize (toType) &&
2872               SPEC_USIGN (fromType) == SPEC_USIGN (toType))
2873             {
2874
2875               iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
2876               if (dic)
2877                 {
2878                   if (IS_ARITHMETIC_OP (dic))
2879                     {
2880                       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2881                       IC_RESULT (dic) = IC_RESULT (ic);
2882                       remiCodeFromeBBlock (ebp, ic);
2883                       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2884                       hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2885                       OP_DEFS_SET ((IC_RESULT (dic)), bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key));
2886                       ic = ic->prev;
2887                     }
2888                   else
2889                     OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2890                 }
2891             }
2892           else
2893             {
2894
2895               /* if the type from and type to are the same
2896                  then if this is the only use then packit */
2897               if (compareType (operandType (IC_RIGHT (ic)),
2898                              operandType (IC_LEFT (ic))) == 1)
2899                 {
2900                   iCode *dic = packRegsDPTRuse (IC_RIGHT (ic));
2901                   if (dic)
2902                     {
2903                       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2904                       IC_RESULT (dic) = IC_RESULT (ic);
2905                       remiCodeFromeBBlock (ebp, ic);
2906                       bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2907                       hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2908                       OP_DEFS_SET ((IC_RESULT (dic)), bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key));
2909                       ic = ic->prev;
2910                     }
2911                 }
2912             }
2913         }
2914
2915       /* pack for PUSH 
2916          iTempNN := (some variable in farspace) V1
2917          push iTempNN ;
2918          -------------
2919          push V1
2920        */
2921       if (ic->op == IPUSH || ic->op == SEND)
2922         {
2923           packForPush (ic, ebp);
2924         }
2925
2926
2927       /* pack registers for accumulator use, when the
2928          result of an arithmetic or bit wise operation
2929          has only one use, that use is immediately following
2930          the defintion and the using iCode has only one
2931          operand or has two operands but one is literal &
2932          the result of that operation is not on stack then
2933          we can leave the result of this operation in acc:b
2934          combination */
2935       if ((IS_ARITHMETIC_OP (ic)
2936            || IS_CONDITIONAL(ic)
2937            || IS_BITWISE_OP (ic)
2938            || ic->op == LEFT_OP || ic->op == RIGHT_OP 
2939            || (ic->op == ADDRESS_OF && isOperandOnStack (IC_LEFT (ic)))
2940           ) &&
2941           IS_ITEMP (IC_RESULT (ic)) &&
2942           getSize (operandType (IC_RESULT (ic))) <= 2)
2943
2944         packRegsForAccUse (ic);
2945       
2946     }
2947 }
2948
2949 /*-----------------------------------------------------------------*/
2950 /* assignRegisters - assigns registers to each live range as need  */
2951 /*-----------------------------------------------------------------*/
2952 void
2953 ds390_assignRegisters (eBBlock ** ebbs, int count)
2954 {
2955   iCode *ic;
2956   int i;
2957
2958   setToNull ((void *) &_G.funcrUsed);  
2959   setToNull ((void *) &_G.regAssigned);  
2960   setToNull ((void *) &_G.totRegAssigned);  
2961   setToNull ((void *) &_G.funcrUsed);  
2962   ds390_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
2963   ds390_nRegs = 12;
2964   if (options.model != MODEL_FLAT24) options.stack10bit = 0;
2965   /* change assignments this will remove some
2966      live ranges reducing some register pressure */
2967   for (i = 0; i < count; i++)
2968     packRegisters (ebbs[i]);
2969
2970   if (options.dump_pack)
2971     dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2972
2973   /* first determine for each live range the number of 
2974      registers & the type of registers required for each */
2975   regTypeNum ();
2976
2977   /* and serially allocate registers */
2978   serialRegAssign (ebbs, count);
2979
2980   ds390_nRegs = 8;
2981   freeAllRegs ();
2982   fillGaps();
2983   ds390_nRegs = 12;
2984
2985   /* if stack was extended then tell the user */
2986   if (_G.stackExtend)
2987     {
2988 /*      werror(W_TOOMANY_SPILS,"stack", */
2989 /*             _G.stackExtend,currFunc->name,""); */
2990       _G.stackExtend = 0;
2991     }
2992
2993   if (_G.dataExtend)
2994     {
2995 /*      werror(W_TOOMANY_SPILS,"data space", */
2996 /*             _G.dataExtend,currFunc->name,""); */
2997       _G.dataExtend = 0;
2998     }
2999
3000   /* after that create the register mask
3001      for each of the instruction */
3002   createRegMask (ebbs, count);
3003
3004   /* redo that offsets for stacked automatic variables */
3005   redoStackOffsets ();
3006
3007   if (options.dump_rassgn) {
3008     dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
3009     dumpLiveRanges (DUMP_LRANGE, liveRanges);
3010   }
3011
3012   /* do the overlaysegment stuff SDCCmem.c */
3013   doOverlays (ebbs, count);
3014
3015   /* now get back the chain */
3016   ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
3017
3018
3019   gen390Code (ic);
3020
3021   /* free up any _G.stackSpil locations allocated */
3022   applyToSet (_G.stackSpil, deallocStackSpil);
3023   _G.slocNum = 0;
3024   setToNull ((void **) &_G.stackSpil);
3025   setToNull ((void **) &_G.spiltSet);
3026   /* mark all registers as free */
3027   ds390_nRegs = 8;
3028   freeAllRegs ();
3029
3030   return;
3031 }