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