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