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