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