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