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