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