* Fixed va_args for the z80
[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 bitVect *
1191 z80_rUmaskForOp (operand * op)
1192 {
1193   return rUmaskForOp (op);
1194 }
1195
1196 /** Returns bit vector of registers used in iCode.
1197  */
1198 bitVect *
1199 regsUsedIniCode (iCode * ic)
1200 {
1201   bitVect *rmask = newBitVect (_nRegs);
1202
1203   /* do the special cases first */
1204   if (ic->op == IFX)
1205     {
1206       rmask = bitVectUnion (rmask,
1207                             rUmaskForOp (IC_COND (ic)));
1208       goto ret;
1209     }
1210
1211   /* for the jumptable */
1212   if (ic->op == JUMPTABLE)
1213     {
1214       rmask = bitVectUnion (rmask,
1215                             rUmaskForOp (IC_JTCOND (ic)));
1216
1217       goto ret;
1218     }
1219
1220   /* of all other cases */
1221   if (IC_LEFT (ic))
1222     rmask = bitVectUnion (rmask,
1223                           rUmaskForOp (IC_LEFT (ic)));
1224
1225
1226   if (IC_RIGHT (ic))
1227     rmask = bitVectUnion (rmask,
1228                           rUmaskForOp (IC_RIGHT (ic)));
1229
1230   if (IC_RESULT (ic))
1231     rmask = bitVectUnion (rmask,
1232                           rUmaskForOp (IC_RESULT (ic)));
1233
1234 ret:
1235   return rmask;
1236 }
1237
1238 /** For each instruction will determine the regsUsed.
1239  */
1240 static void 
1241 createRegMask (eBBlock ** ebbs, int count)
1242 {
1243   int i;
1244
1245   /* for all blocks */
1246   for (i = 0; i < count; i++)
1247     {
1248       iCode *ic;
1249
1250       if (ebbs[i]->noPath &&
1251           (ebbs[i]->entryLabel != entryLabel &&
1252            ebbs[i]->entryLabel != returnLabel))
1253         continue;
1254
1255       /* for all instructions */
1256       for (ic = ebbs[i]->sch; ic; ic = ic->next)
1257         {
1258
1259           int j;
1260
1261           if (SKIP_IC2 (ic) || !ic->rlive)
1262             continue;
1263
1264           /* first mark the registers used in this
1265              instruction */
1266           ic->rUsed = regsUsedIniCode (ic);
1267           funcrUsed = bitVectUnion (funcrUsed, ic->rUsed);
1268
1269           /* now create the register mask for those 
1270              registers that are in use : this is a
1271              super set of ic->rUsed */
1272           ic->rMask = newBitVect (_nRegs + 1);
1273
1274           /* for all live Ranges alive at this point */
1275           for (j = 1; j < ic->rlive->size; j++)
1276             {
1277               symbol *sym;
1278               int k;
1279
1280               /* if not alive then continue */
1281               if (!bitVectBitValue (ic->rlive, j))
1282                 continue;
1283
1284               /* find the live range we are interested in */
1285               if (!(sym = hTabItemWithKey (liveRanges, j)))
1286                 {
1287                   werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1288                           "createRegMask cannot find live range");
1289                   exit (0);
1290                 }
1291
1292               /* if no register assigned to it */
1293               if (!sym->nRegs || sym->isspilt)
1294                 continue;
1295
1296               /* for all the registers allocated to it */
1297               for (k = 0; k < sym->nRegs; k++)
1298                 if (sym->regs[k])
1299                   ic->rMask =
1300                     bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1301             }
1302         }
1303     }
1304 }
1305
1306 /** Returns the rematerialized string for a remat var.
1307  */
1308 char *
1309 rematStr (symbol * sym)
1310 {
1311   char *s = buffer;
1312   iCode *ic = sym->rematiCode;
1313
1314   while (1)
1315     {
1316
1317       /* if plus or minus print the right hand side */
1318       if (ic->op == '+' || ic->op == '-')
1319         {
1320           sprintf (s, "0x%04x %c ", (int) operandLitValue (IC_RIGHT (ic)),
1321                    ic->op);
1322           s += strlen (s);
1323           ic = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1324           continue;
1325         }
1326       /* we reached the end */
1327       sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1328       break;
1329     }
1330
1331   return buffer;
1332 }
1333
1334 /*-----------------------------------------------------------------*/
1335 /* regTypeNum - computes the type & number of registers required   */
1336 /*-----------------------------------------------------------------*/
1337 static void 
1338 regTypeNum (void)
1339 {
1340   symbol *sym;
1341   int k;
1342
1343   /* for each live range do */
1344   for (sym = hTabFirstItem (liveRanges, &k); sym;
1345        sym = hTabNextItem (liveRanges, &k))
1346     {
1347
1348       /* if used zero times then no registers needed */
1349       if ((sym->liveTo - sym->liveFrom) == 0)
1350         continue;
1351
1352       D (D_ALLOC, ("regTypeNum: loop on sym %p\n", sym));
1353
1354       /* if the live range is a temporary */
1355       if (sym->isitmp)
1356         {
1357
1358           /* if the type is marked as a conditional */
1359           if (sym->regType == REG_CND)
1360             continue;
1361
1362           /* if used in return only then we don't 
1363              need registers */
1364           if (sym->ruonly || sym->accuse)
1365             {
1366               if (IS_AGGREGATE (sym->type) || sym->isptr)
1367                 sym->type = aggrToPtr (sym->type, FALSE);
1368               continue;
1369             }
1370
1371           /* if not then we require registers */
1372           D (D_ALLOC, ("regTypeNum: isagg %u nRegs %u type %p\n", IS_AGGREGATE (sym->type) || sym->isptr, sym->nRegs, sym->type));
1373           sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1374                         getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1375                         getSize (sym->type));
1376           D (D_ALLOC, ("regTypeNum: setting nRegs of %s (%p) to %u\n", sym->name, sym, sym->nRegs));
1377
1378           D (D_ALLOC, ("regTypeNum: setup to assign regs sym %p\n", sym));
1379
1380           if (sym->nRegs > 4)
1381             {
1382               fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1383               printTypeChain (sym->type, stderr);
1384               fprintf (stderr, "\n");
1385             }
1386
1387           /* determine the type of register required */
1388           /* Always general purpose */
1389           sym->regType = REG_GPR;
1390
1391         }
1392       else
1393         {
1394           /* for the first run we don't provide */
1395           /* registers for true symbols we will */
1396           /* see how things go                  */
1397           D (D_ALLOC, ("regTypeNum: #2 setting num of %p to 0\n", sym));
1398           sym->nRegs = 0;
1399         }
1400     }
1401
1402 }
1403
1404 /** Mark all registers as free.
1405  */
1406 static void 
1407 freeAllRegs ()
1408 {
1409   int i;
1410
1411   D (D_ALLOC, ("freeAllRegs: running.\n"));
1412
1413   for (i = 0; i < _nRegs; i++)
1414     regsZ80[i].isFree = 1;
1415 }
1416
1417 /*-----------------------------------------------------------------*/
1418 /* deallocStackSpil - this will set the stack pointer back         */
1419 /*-----------------------------------------------------------------*/
1420 DEFSETFUNC (deallocStackSpil)
1421 {
1422   symbol *sym = item;
1423
1424   deallocLocal (sym);
1425   return 0;
1426 }
1427
1428 /** Register reduction for assignment.
1429  */
1430 static int 
1431 packRegsForAssign (iCode * ic, eBBlock * ebp)
1432 {
1433   iCode *dic, *sic;
1434
1435   D (D_ALLOC, ("packRegsForAssing: running on ic %p\n", ic));
1436
1437   if (
1438   /*      !IS_TRUE_SYMOP(IC_RESULT(ic)) || */
1439        !IS_ITEMP (IC_RIGHT (ic)) ||
1440        OP_LIVETO (IC_RIGHT (ic)) > ic->seq ||
1441        OP_SYMBOL (IC_RIGHT (ic))->isind)
1442     return 0;
1443
1444 #if 0
1445   /* if the true symbol is defined in far space or on stack
1446      then we should not since this will increase register pressure */
1447   if (isOperandInFarSpace (IC_RESULT (ic)))
1448     {
1449       if ((dic = farSpacePackable (ic)))
1450         goto pack;
1451       else
1452         return 0;
1453     }
1454 #endif
1455
1456   /* find the definition of iTempNN scanning backwards if we find a 
1457      a use of the true symbol in before we find the definition then 
1458      we cannot */
1459   for (dic = ic->prev; dic; dic = dic->prev)
1460     {
1461       /* if there is a function call and this is
1462          a parameter & not my parameter then don't pack it */
1463       if ((dic->op == CALL || dic->op == PCALL) &&
1464           (OP_SYMBOL (IC_RESULT (ic))->_isparm &&
1465            !OP_SYMBOL (IC_RESULT (ic))->ismyparm))
1466         {
1467           dic = NULL;
1468           break;
1469         }
1470
1471       if (SKIP_IC2 (dic))
1472         continue;
1473
1474       if (IS_SYMOP (IC_RESULT (dic)) &&
1475           IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
1476         {
1477           break;
1478         }
1479
1480       if (IS_SYMOP (IC_RIGHT (dic)) &&
1481           (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
1482            IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
1483         {
1484           dic = NULL;
1485           break;
1486         }
1487
1488       if (IS_SYMOP (IC_LEFT (dic)) &&
1489           (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
1490            IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
1491         {
1492           dic = NULL;
1493           break;
1494         }
1495 #if 0
1496       if (POINTER_SET (dic) &&
1497           IC_RESULT (dic)->key == IC_RESULT (ic)->key)
1498         {
1499           dic = NULL;
1500           break;
1501         }
1502 #endif
1503     }
1504
1505   if (!dic)
1506     return 0;                   /* did not find */
1507
1508   /* if the result is on stack or iaccess then it must be
1509      the same atleast one of the operands */
1510   if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
1511       OP_SYMBOL (IC_RESULT (ic))->iaccess)
1512     {
1513
1514       /* the operation has only one symbol
1515          operator then we can pack */
1516       if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
1517           (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
1518         goto pack;
1519
1520       if (!((IC_LEFT (dic) &&
1521              IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
1522             (IC_RIGHT (dic) &&
1523              IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
1524         return 0;
1525     }
1526 pack:
1527   /* found the definition */
1528   /* replace the result with the result of */
1529   /* this assignment and remove this assignment */
1530   IC_RESULT (dic) = IC_RESULT (ic);
1531
1532   if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
1533     {
1534       OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
1535     }
1536   /* delete from liverange table also 
1537      delete from all the points inbetween and the new
1538      one */
1539   for (sic = dic; sic != ic; sic = sic->next)
1540     {
1541       bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
1542       if (IS_ITEMP (IC_RESULT (dic)))
1543         bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
1544     }
1545
1546   remiCodeFromeBBlock (ebp, ic);
1547   return 1;
1548 }
1549
1550 /** Scanning backwards looks for first assig found.
1551  */
1552 iCode *
1553 findAssignToSym (operand * op, iCode * ic)
1554 {
1555   iCode *dic;
1556
1557   for (dic = ic->prev; dic; dic = dic->prev)
1558     {
1559
1560       /* if definition by assignment */
1561       if (dic->op == '=' &&
1562           !POINTER_SET (dic) &&
1563           IC_RESULT (dic)->key == op->key)
1564         /*      &&  IS_TRUE_SYMOP(IC_RIGHT(dic)) */
1565         {
1566
1567           /* we are interested only if defined in far space */
1568           /* or in stack space in case of + & - */
1569
1570           /* if assigned to a non-symbol then return
1571              true */
1572           if (!IS_SYMOP (IC_RIGHT (dic)))
1573             break;
1574
1575           /* if the symbol is in far space then
1576              we should not */
1577           if (isOperandInFarSpace (IC_RIGHT (dic)))
1578             return NULL;
1579
1580           /* for + & - operations make sure that
1581              if it is on the stack it is the same
1582              as one of the three operands */
1583           if ((ic->op == '+' || ic->op == '-') &&
1584               OP_SYMBOL (IC_RIGHT (dic))->onStack)
1585             {
1586
1587               if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
1588                   IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
1589                   IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
1590                 return NULL;
1591             }
1592
1593           break;
1594
1595         }
1596
1597       /* if we find an usage then we cannot delete it */
1598       if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
1599         return NULL;
1600
1601       if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
1602         return NULL;
1603
1604       if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
1605         return NULL;
1606     }
1607
1608   /* now make sure that the right side of dic
1609      is not defined between ic & dic */
1610   if (dic)
1611     {
1612       iCode *sic = dic->next;
1613
1614       for (; sic != ic; sic = sic->next)
1615         if (IC_RESULT (sic) &&
1616             IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
1617           return NULL;
1618     }
1619
1620   return dic;
1621
1622
1623 }
1624
1625 #if !DISABLE_PACKREGSFORSUPPORT
1626 // PENDING
1627
1628 /*-----------------------------------------------------------------*/
1629 /* packRegsForSupport :- reduce some registers for support calls   */
1630 /*-----------------------------------------------------------------*/
1631 static int 
1632 packRegsForSupport (iCode * ic, eBBlock * ebp)
1633 {
1634   int change = 0;
1635   /* for the left & right operand :- look to see if the
1636      left was assigned a true symbol in far space in that
1637      case replace them */
1638   D (D_ALLOC, ("packRegsForSupport: running on ic %p\n", ic));
1639
1640   if (IS_ITEMP (IC_LEFT (ic)) &&
1641       OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
1642     {
1643       iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
1644       iCode *sic;
1645
1646       if (!dic)
1647         goto right;
1648
1649       /* found it we need to remove it from the
1650          block */
1651       for (sic = dic; sic != ic; sic = sic->next)
1652         bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
1653
1654       IC_LEFT (ic)->operand.symOperand =
1655         IC_RIGHT (dic)->operand.symOperand;
1656       IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1657       remiCodeFromeBBlock (ebp, dic);
1658       change++;
1659     }
1660
1661   /* do the same for the right operand */
1662 right:
1663   if (!change &&
1664       IS_ITEMP (IC_RIGHT (ic)) &&
1665       OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
1666     {
1667       iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
1668       iCode *sic;
1669
1670       if (!dic)
1671         return change;
1672
1673       /* found it we need to remove it from the block */
1674       for (sic = dic; sic != ic; sic = sic->next)
1675         bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
1676
1677       IC_RIGHT (ic)->operand.symOperand =
1678         IC_RIGHT (dic)->operand.symOperand;
1679       IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
1680
1681       remiCodeFromeBBlock (ebp, dic);
1682       change++;
1683     }
1684
1685   return change;
1686 }
1687 #endif
1688
1689 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
1690
1691 /** Will reduce some registers for single use.
1692  */
1693 static iCode *
1694 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
1695 {
1696   bitVect *uses;
1697   iCode *dic, *sic;
1698
1699   D (D_ALLOC, ("packRegsForOneUse: running on ic %p\n", ic));
1700
1701   /* if returning a literal then do nothing */
1702   if (!IS_SYMOP (op))
1703     return NULL;
1704
1705   /* only upto 2 bytes since we cannot predict
1706      the usage of b, & acc */
1707   if (getSize (operandType (op)) > 2 &&
1708       ic->op != RETURN &&
1709       ic->op != SEND)
1710     return NULL;
1711
1712   /* this routine will mark the a symbol as used in one 
1713      instruction use only && if the defintion is local 
1714      (ie. within the basic block) && has only one definition &&
1715      that definiion is either a return value from a 
1716      function or does not contain any variables in
1717      far space */
1718   uses = bitVectCopy (OP_USES (op));
1719   bitVectUnSetBit (uses, ic->key);      /* take away this iCode */
1720   if (!bitVectIsZero (uses))    /* has other uses */
1721     return NULL;
1722
1723   /* if it has only one defintion */
1724   if (bitVectnBitsOn (OP_DEFS (op)) > 1)
1725     return NULL;                /* has more than one definition */
1726
1727   /* get the that definition */
1728   if (!(dic =
1729         hTabItemWithKey (iCodehTab,
1730                          bitVectFirstBit (OP_DEFS (op)))))
1731     return NULL;
1732
1733   /* found the definition now check if it is local */
1734   if (dic->seq < ebp->fSeq ||
1735       dic->seq > ebp->lSeq)
1736     return NULL;                /* non-local */
1737
1738   /* now check if it is the return from a function call */
1739   if (dic->op == CALL || dic->op == PCALL)
1740     {
1741       if (ic->op != SEND && ic->op != RETURN)
1742         {
1743           OP_SYMBOL (op)->ruonly = 1;
1744           return dic;
1745         }
1746       dic = dic->next;
1747     }
1748
1749   /* otherwise check that the definition does
1750      not contain any symbols in far space */
1751   if (isOperandInFarSpace (IC_LEFT (dic)) ||
1752       isOperandInFarSpace (IC_RIGHT (dic)) ||
1753       IS_OP_RUONLY (IC_LEFT (ic)) ||
1754       IS_OP_RUONLY (IC_RIGHT (ic)))
1755     {
1756       return NULL;
1757     }
1758
1759   /* if pointer set then make sure the pointer is one byte */
1760   if (POINTER_SET (dic))
1761     return NULL;
1762
1763   if (POINTER_GET (dic))
1764     return NULL;
1765
1766   sic = dic;
1767
1768   /* also make sure the intervenening instructions
1769      don't have any thing in far space */
1770   for (dic = dic->next; dic && dic != ic; dic = dic->next)
1771     {
1772       /* if there is an intervening function call then no */
1773       if (dic->op == CALL || dic->op == PCALL)
1774         return NULL;
1775       /* if pointer set then make sure the pointer
1776          is one byte */
1777       if (POINTER_SET (dic))
1778         return NULL;
1779
1780       if (POINTER_GET (dic))
1781         return NULL;
1782
1783       /* if address of & the result is remat the okay */
1784       if (dic->op == ADDRESS_OF &&
1785           OP_SYMBOL (IC_RESULT (dic))->remat)
1786         continue;
1787
1788       /* if left or right or result is in far space */
1789       if (isOperandInFarSpace (IC_LEFT (dic)) ||
1790           isOperandInFarSpace (IC_RIGHT (dic)) ||
1791           isOperandInFarSpace (IC_RESULT (dic)) ||
1792           IS_OP_RUONLY (IC_LEFT (dic)) ||
1793           IS_OP_RUONLY (IC_RIGHT (dic)) ||
1794           IS_OP_RUONLY (IC_RESULT (dic)))
1795         {
1796           return NULL;
1797         }
1798     }
1799
1800   OP_SYMBOL (op)->ruonly = 1;
1801   return sic;
1802 }
1803
1804 /*-----------------------------------------------------------------*/
1805 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN          */
1806 /*-----------------------------------------------------------------*/
1807 static bool 
1808 isBitwiseOptimizable (iCode * ic)
1809 {
1810   sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
1811
1812   /* bitwise operations are considered optimizable
1813      under the following conditions (Jean-Louis VERN) 
1814
1815      x & lit
1816      bit & bit
1817      bit & x
1818      bit ^ bit
1819      bit ^ x
1820      x   ^ lit
1821      x   | lit
1822      bit | bit
1823      bit | x
1824    */
1825   if (IS_LITERAL (rtype))
1826     return TRUE;
1827   return FALSE;
1828 }
1829
1830 /** Optimisations:
1831     Certian assignments involving pointers can be temporarly stored
1832     in HL.  Esp.
1833 genAssign
1834     ld  iy,#_Blah
1835     ld  bc,(iy)
1836 genAssign (ptr)
1837     ld  hl,bc
1838     ld  iy,#_Blah2
1839     ld  (iy),(hl)
1840 */
1841
1842 #if !DISABLE_PACKREGSFORACCUSE
1843 // PENDING
1844
1845 /** Pack registers for acc use.
1846     When the result of this operation is small and short lived it may
1847     be able to be stored in the accumelator.
1848  */
1849 static void 
1850 packRegsForAccUse (iCode * ic)
1851 {
1852   iCode *uic;
1853
1854   /* if + or - then it has to be one byte result */
1855   if ((ic->op == '+' || ic->op == '-')
1856       && getSize (operandType (IC_RESULT (ic))) > 1)
1857     return;
1858
1859   /* if shift operation make sure right side is not a literal */
1860   if (ic->op == RIGHT_OP &&
1861       (isOperandLiteral (IC_RIGHT (ic)) ||
1862        getSize (operandType (IC_RESULT (ic))) > 1))
1863     return;
1864
1865   if (ic->op == LEFT_OP &&
1866       (isOperandLiteral (IC_RIGHT (ic)) ||
1867        getSize (operandType (IC_RESULT (ic))) > 1))
1868     return;
1869
1870   /* has only one definition */
1871   if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
1872     return;
1873
1874   /* has only one use */
1875   if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
1876     return;
1877
1878   /* and the usage immediately follows this iCode */
1879   if (!(uic = hTabItemWithKey (iCodehTab,
1880                                bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
1881     return;
1882
1883   if (ic->next != uic)
1884     return;
1885
1886   /* if it is a conditional branch then we definitely can */
1887   if (uic->op == IFX)
1888     goto accuse;
1889
1890   if (uic->op == JUMPTABLE)
1891     return;
1892
1893 #if 0
1894   /* if the usage is not is an assignment or an 
1895      arithmetic / bitwise / shift operation then not */
1896   if (POINTER_SET (uic) &&
1897       getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
1898     return;
1899 #endif
1900
1901   if (uic->op != '=' &&
1902       !IS_ARITHMETIC_OP (uic) &&
1903       !IS_BITWISE_OP (uic) &&
1904       uic->op != LEFT_OP &&
1905       uic->op != RIGHT_OP)
1906     return;
1907
1908   /* if used in ^ operation then make sure right is not a 
1909      literl */
1910   if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
1911     return;
1912
1913   /* if shift operation make sure right side is not a literal */
1914   if (uic->op == RIGHT_OP &&
1915       (isOperandLiteral (IC_RIGHT (uic)) ||
1916        getSize (operandType (IC_RESULT (uic))) > 1))
1917     return;
1918
1919   if (uic->op == LEFT_OP &&
1920       (isOperandLiteral (IC_RIGHT (uic)) ||
1921        getSize (operandType (IC_RESULT (uic))) > 1))
1922     return;
1923
1924 #if 0
1925   /* make sure that the result of this icode is not on the
1926      stack, since acc is used to compute stack offset */
1927   if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
1928       OP_SYMBOL (IC_RESULT (uic))->onStack)
1929     return;
1930 #endif
1931
1932 #if 0
1933   /* if either one of them in far space then we cannot */
1934   if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
1935        isOperandInFarSpace (IC_LEFT (uic))) ||
1936       (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
1937        isOperandInFarSpace (IC_RIGHT (uic))))
1938     return;
1939 #endif
1940
1941   /* if the usage has only one operand then we can */
1942   if (IC_LEFT (uic) == NULL ||
1943       IC_RIGHT (uic) == NULL)
1944     goto accuse;
1945
1946   /* make sure this is on the left side if not
1947      a '+' since '+' is commutative */
1948   if (ic->op != '+' &&
1949       IC_LEFT (uic)->key != IC_RESULT (ic)->key)
1950     return;
1951
1952   /* if one of them is a literal then we can */
1953   if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
1954       (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
1955     {
1956       goto accuse;
1957       return;
1958     }
1959
1960 /** This is confusing :)  Guess for now */
1961   if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
1962       (IS_ITEMP (IC_RIGHT (uic)) ||
1963        (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
1964     goto accuse;
1965
1966   if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
1967       (IS_ITEMP (IC_LEFT (uic)) ||
1968        (IS_TRUE_SYMOP (IC_LEFT (uic)))))
1969     goto accuse;
1970   return;
1971 accuse:
1972   OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
1973 }
1974 #endif
1975
1976 static void 
1977 packRegsForHLUse (iCode * ic)
1978 {
1979   iCode *uic;
1980
1981   if (IS_GB)
1982     return;
1983
1984   /* has only one definition */
1985   if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
1986     return;
1987
1988   /* has only one use */
1989   if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
1990     return;
1991
1992   /* and the usage immediately follows this iCode */
1993   if (!(uic = hTabItemWithKey (iCodehTab,
1994                                bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
1995     return;
1996
1997   if (ic->next != uic)
1998     return;
1999
2000   if (ic->op == ADDRESS_OF && uic->op == IPUSH)
2001     goto hluse;
2002   if (ic->op == CALL && ic->parmBytes == 0 && (uic->op == '-' || uic->op == '+'))
2003     goto hluse;
2004   return;
2005 hluse:
2006   OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_HL;
2007 }
2008
2009 bool 
2010 opPreservesA (iCode * ic, iCode * uic)
2011 {
2012   /* if it is a conditional branch then we definitely can */
2013   if (uic->op == IFX)
2014     return FALSE;
2015
2016   if (uic->op == JUMPTABLE)
2017     return FALSE;
2018
2019   /* if the usage has only one operand then we can */
2020   /* PENDING: check */
2021   if (IC_LEFT (uic) == NULL ||
2022       IC_RIGHT (uic) == NULL)
2023     return FALSE;
2024
2025   /* PENDING: check this rule */
2026   if (getSize (operandType (IC_RESULT (uic))) > 1)
2027     {
2028       return FALSE;
2029     }
2030
2031   /*
2032      Bad:
2033      !IS_ARITHMETIC_OP(uic) (sub requires A)
2034    */
2035   if (
2036        uic->op != '+' &&
2037        !IS_BITWISE_OP (uic) &&
2038        uic->op != '=' &&
2039        uic->op != EQ_OP &&
2040        !POINTER_GET (uic) &&
2041   /*
2042      uic->op != LEFT_OP &&
2043      uic->op != RIGHT_OP && */
2044        1
2045     )
2046     {
2047       return FALSE;
2048     }
2049
2050   /* PENDING */
2051   if (!IC_LEFT (uic) || !IC_RESULT (ic))
2052     return FALSE;
2053
2054 /** This is confusing :)  Guess for now */
2055   if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2056       (IS_ITEMP (IC_RIGHT (uic)) ||
2057        (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
2058     return TRUE;
2059
2060   if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2061       (IS_ITEMP (IC_LEFT (uic)) ||
2062        (IS_TRUE_SYMOP (IC_LEFT (uic)))))
2063     return TRUE;
2064
2065   return FALSE;
2066 }
2067
2068 static void 
2069 joinPushes (iCode * ic)
2070 {
2071 #if 0
2072   if (ic->op == IPUSH &&
2073       isOperandLiteral (IC_LEFT (ic)) &&
2074       getSize (operandType (IC_LEFT (ic))) == 1 &&
2075       ic->next->op == IPUSH &&
2076       isOperandLiteral (IC_LEFT (ic->next)) &&
2077       getSize (operandType (IC_LEFT (ic->next))) == 1)
2078     {
2079       /* This is a bit tricky as michaelh doesnt know what he's doing.
2080        */
2081       /* First upgrade the size of (first) to int */
2082       SPEC_NOUN (operandType (IC_LEFT (ic))) = V_INT;
2083
2084       floatFromVal (AOP /* need some sleep ... */ );
2085       /* Now get and join the values */
2086       value *val = aop->aopu.aop_lit;
2087       /* if it is a float then it gets tricky */
2088       /* otherwise it is fairly simple */
2089       if (!IS_FLOAT (val->type))
2090         {
2091           unsigned long v = floatFromVal (val);
2092
2093           floatFrom ( /* need some sleep ... */ );
2094           printf ("Size %u\n", getSize (operandType (IC_LEFT (ic))));
2095           ic->next = ic->next->next;
2096         }
2097     }
2098 #endif
2099 }
2100
2101 /** Pack registers for acc use.
2102     When the result of this operation is small and short lived it may
2103     be able to be stored in the accumulator.
2104
2105     Note that the 'A preserving' list is currently emperical :)e
2106  */
2107 static void 
2108 packRegsForAccUse2 (iCode * ic)
2109 {
2110   iCode *uic;
2111
2112   D (D_ALLOC, ("packRegsForAccUse2: running on ic %p\n", ic));
2113
2114   /* Filter out all but those 'good' commands */
2115   if (
2116        !POINTER_GET (ic) &&
2117        ic->op != '+' &&
2118        !IS_BITWISE_OP (ic) &&
2119        ic->op != '=' &&
2120        ic->op != EQ_OP &&
2121        ic->op != CAST &&
2122        1)
2123     return;
2124
2125   /* if + or - then it has to be one byte result.
2126      MLH: Ok.
2127    */
2128   if ((ic->op == '+' || ic->op == '-')
2129       && getSize (operandType (IC_RESULT (ic))) > 1)
2130     return;
2131
2132   /* if shift operation make sure right side is not a literal.
2133      MLH: depends.
2134    */
2135 #if 0
2136   if (ic->op == RIGHT_OP &&
2137       (isOperandLiteral (IC_RIGHT (ic)) ||
2138        getSize (operandType (IC_RESULT (ic))) > 1))
2139     return;
2140
2141   if (ic->op == LEFT_OP &&
2142       (isOperandLiteral (IC_RIGHT (ic)) ||
2143        getSize (operandType (IC_RESULT (ic))) > 1))
2144     return;
2145 #endif
2146
2147   /* has only one definition */
2148   if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2149     {
2150       return;
2151     }
2152
2153   /* Right.  We may be able to propagate it through if:
2154      For each in the chain of uses the intermediate is OK.
2155    */
2156   /* Get next with 'uses result' bit on
2157      If this->next == next
2158      Validate use of next
2159      If OK, increase count
2160    */
2161   /* and the usage immediately follows this iCode */
2162   if (!(uic = hTabItemWithKey (iCodehTab,
2163                                bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2164     {
2165       return;
2166     }
2167
2168   {
2169     /* Create a copy of the OP_USES bit vect */
2170     bitVect *uses = bitVectCopy (OP_USES (IC_RESULT (ic)));
2171     int setBit;
2172     iCode *scan = ic, *next;
2173
2174     do
2175       {
2176         setBit = bitVectFirstBit (uses);
2177         next = hTabItemWithKey (iCodehTab, setBit);
2178         if (scan->next == next)
2179           {
2180             bitVectUnSetBit (uses, setBit);
2181             /* Still contigous. */
2182             if (!opPreservesA (ic, next))
2183               {
2184                 return;
2185               }
2186             scan = next;
2187           }
2188         else
2189           {
2190             return;
2191           }
2192       }
2193     while (!bitVectIsZero (uses));
2194     OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2195     return;
2196   }
2197
2198   /* OLD CODE FOLLOWS */
2199   /* if it is a conditional branch then we definitely can
2200      MLH: Depends.
2201    */
2202 #if 0
2203   if (uic->op == IFX)
2204     goto accuse;
2205
2206   /* MLH: Depends. */
2207   if (uic->op == JUMPTABLE)
2208     return;
2209 #endif
2210
2211   /* if the usage is not is an assignment or an 
2212      arithmetic / bitwise / shift operation then not.
2213      MLH: Pending:  Invalid.  Our pointer sets are always peechy.
2214    */
2215 #if 0
2216   if (POINTER_SET (uic) &&
2217       getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2218     {
2219       printf ("e5 %u\n", getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)));
2220       return;
2221     }
2222 #endif
2223
2224   printf ("1\n");
2225   if (uic->op != '=' &&
2226       !IS_ARITHMETIC_OP (uic) &&
2227       !IS_BITWISE_OP (uic) &&
2228       uic->op != LEFT_OP &&
2229       uic->op != RIGHT_OP)
2230     {
2231       printf ("e6\n");
2232       return;
2233     }
2234
2235   /* if used in ^ operation then make sure right is not a 
2236      literl */
2237   if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2238     return;
2239
2240   /* if shift operation make sure right side is not a literal */
2241   if (uic->op == RIGHT_OP &&
2242       (isOperandLiteral (IC_RIGHT (uic)) ||
2243        getSize (operandType (IC_RESULT (uic))) > 1))
2244     return;
2245
2246   if (uic->op == LEFT_OP &&
2247       (isOperandLiteral (IC_RIGHT (uic)) ||
2248        getSize (operandType (IC_RESULT (uic))) > 1))
2249     return;
2250
2251 #if 0
2252   /* make sure that the result of this icode is not on the
2253      stack, since acc is used to compute stack offset */
2254   if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2255       OP_SYMBOL (IC_RESULT (uic))->onStack)
2256     return;
2257 #endif
2258
2259 #if 0
2260   /* if either one of them in far space then we cannot */
2261   if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2262        isOperandInFarSpace (IC_LEFT (uic))) ||
2263       (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2264        isOperandInFarSpace (IC_RIGHT (uic))))
2265     return;
2266 #endif
2267
2268   /* if the usage has only one operand then we can */
2269   if (IC_LEFT (uic) == NULL ||
2270       IC_RIGHT (uic) == NULL)
2271     goto accuse;
2272
2273   /* make sure this is on the left side if not
2274      a '+' since '+' is commutative */
2275   if (ic->op != '+' &&
2276       IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2277     return;
2278
2279   /* if one of them is a literal then we can */
2280   if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2281       (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2282     {
2283       goto accuse;
2284       return;
2285     }
2286
2287 /** This is confusing :)  Guess for now */
2288   if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2289       (IS_ITEMP (IC_RIGHT (uic)) ||
2290        (IS_TRUE_SYMOP (IC_RIGHT (uic)))))
2291     goto accuse;
2292
2293   if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2294       (IS_ITEMP (IC_LEFT (uic)) ||
2295        (IS_TRUE_SYMOP (IC_LEFT (uic)))))
2296     goto accuse;
2297   return;
2298 accuse:
2299   printf ("acc ok!\n");
2300   OP_SYMBOL (IC_RESULT (ic))->accuse = ACCUSE_A;
2301 }
2302
2303 /** Does some transformations to reduce register pressure.
2304  */
2305 static void 
2306 packRegisters (eBBlock * ebp)
2307 {
2308   iCode *ic;
2309   int change = 0;
2310
2311   D (D_ALLOC, ("packRegisters: entered.\n"));
2312
2313   while (1 && !DISABLE_PACK_ASSIGN)
2314     {
2315       change = 0;
2316       /* look for assignments of the form */
2317       /* iTempNN = TRueSym (someoperation) SomeOperand */
2318       /*       ....                       */
2319       /* TrueSym := iTempNN:1             */
2320       for (ic = ebp->sch; ic; ic = ic->next)
2321         {
2322           /* find assignment of the form TrueSym := iTempNN:1 */
2323           if (ic->op == '=' && !POINTER_SET (ic))
2324             change += packRegsForAssign (ic, ebp);
2325         }
2326       if (!change)
2327         break;
2328     }
2329
2330   for (ic = ebp->sch; ic; ic = ic->next)
2331     {
2332       /* Safe: address of a true sym is always constant. */
2333       /* if this is an itemp & result of a address of a true sym 
2334          then mark this as rematerialisable   */
2335       D (D_ALLOC, ("packRegisters: looping on ic %p\n", ic));
2336
2337       if (ic->op == ADDRESS_OF &&
2338           IS_ITEMP (IC_RESULT (ic)) &&
2339           IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2340           bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2341           !OP_SYMBOL (IC_LEFT (ic))->onStack)
2342         {
2343
2344           OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2345           OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2346           OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2347         }
2348
2349       /* Safe: just propagates the remat flag */
2350       /* if straight assignment then carry remat flag if this is the
2351          only definition */
2352       if (ic->op == '=' &&
2353           !POINTER_SET (ic) &&
2354           IS_SYMOP (IC_RIGHT (ic)) &&
2355           OP_SYMBOL (IC_RIGHT (ic))->remat &&
2356           bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2357         {
2358
2359           OP_SYMBOL (IC_RESULT (ic))->remat =
2360             OP_SYMBOL (IC_RIGHT (ic))->remat;
2361           OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2362             OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2363         }
2364
2365       /* if the condition of an if instruction is defined in the
2366          previous instruction then mark the itemp as a conditional */
2367       if ((IS_CONDITIONAL (ic) ||
2368            ((ic->op == BITWISEAND ||
2369              ic->op == '|' ||
2370              ic->op == '^') &&
2371             isBitwiseOptimizable (ic))) &&
2372           ic->next && ic->next->op == IFX &&
2373           isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2374           OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2375         {
2376
2377           OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2378           continue;
2379         }
2380
2381 #if 0
2382       /* reduce for support function calls */
2383       if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2384         packRegsForSupport (ic, ebp);
2385 #endif
2386
2387 #if 0
2388       /* some cases the redundant moves can
2389          can be eliminated for return statements */
2390       if ((ic->op == RETURN || ic->op == SEND) &&
2391           !isOperandInFarSpace (IC_LEFT (ic)) &&
2392           !options.model)
2393         packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2394 #endif
2395       /* if pointer set & left has a size more than
2396          one and right is not in far space */
2397       if (POINTER_SET (ic) &&
2398       /* MLH: no such thing.
2399          !isOperandInFarSpace(IC_RIGHT(ic)) && */
2400           !OP_SYMBOL (IC_RESULT (ic))->remat &&
2401           !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2402           getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2403         {
2404
2405           packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2406         }
2407
2408       /* if pointer get */
2409       if (!DISABLE_PACK_ONE_USE &&
2410           POINTER_GET (ic) &&
2411       /* MLH: dont have far space
2412          !isOperandInFarSpace(IC_RESULT(ic))&& */
2413           !OP_SYMBOL (IC_LEFT (ic))->remat &&
2414           !IS_OP_RUONLY (IC_RESULT (ic)) &&
2415           getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2416         {
2417
2418           packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2419         }
2420       /* pack registers for accumulator use, when the result of an
2421          arithmetic or bit wise operation has only one use, that use is
2422          immediately following the defintion and the using iCode has
2423          only one operand or has two operands but one is literal & the
2424          result of that operation is not on stack then we can leave the
2425          result of this operation in acc:b combination */
2426
2427       if (!DISABLE_PACK_HL && IS_ITEMP (IC_RESULT (ic)))
2428         {
2429           packRegsForHLUse (ic);
2430         }
2431 #if 0
2432       if ((IS_ARITHMETIC_OP (ic)
2433            || IS_BITWISE_OP (ic)
2434            || ic->op == LEFT_OP || ic->op == RIGHT_OP
2435           ) &&
2436           IS_ITEMP (IC_RESULT (ic)) &&
2437           getSize (operandType (IC_RESULT (ic))) <= 2)
2438         packRegsForAccUse (ic);
2439 #else
2440       if (!DISABLE_PACK_ACC && IS_ITEMP (IC_RESULT (ic)) &&
2441           getSize (operandType (IC_RESULT (ic))) == 1)
2442         {
2443           packRegsForAccUse2 (ic);
2444         }
2445 #endif
2446       joinPushes (ic);
2447     }
2448 }
2449
2450 /*-----------------------------------------------------------------*/
2451 /* assignRegisters - assigns registers to each live range as need  */
2452 /*-----------------------------------------------------------------*/
2453 void 
2454 z80_assignRegisters (eBBlock ** ebbs, int count)
2455 {
2456   iCode *ic;
2457   int i;
2458
2459   D (D_ALLOC, ("\n-> z80_assignRegisters: entered.\n"));
2460
2461   setToNull ((void *) &funcrUsed);
2462   stackExtend = dataExtend = 0;
2463
2464   if (IS_GB)
2465     {
2466       /* DE is required for the code gen. */
2467       _nRegs = GBZ80_MAX_REGS;
2468       regsZ80 = _gbz80_regs;
2469     }
2470   else
2471     {
2472       _nRegs = Z80_MAX_REGS;
2473       regsZ80 = _z80_regs;
2474     }
2475
2476   /* change assignments this will remove some
2477      live ranges reducing some register pressure */
2478   for (i = 0; i < count; i++)
2479     packRegisters (ebbs[i]);
2480
2481   if (options.dump_pack)
2482     dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
2483
2484   /* first determine for each live range the number of 
2485      registers & the type of registers required for each */
2486   regTypeNum ();
2487
2488   /* and serially allocate registers */
2489   serialRegAssign (ebbs, count);
2490
2491   /* if stack was extended then tell the user */
2492   if (stackExtend)
2493     {
2494 /*      werror(W_TOOMANY_SPILS,"stack", */
2495 /*             stackExtend,currFunc->name,""); */
2496       stackExtend = 0;
2497     }
2498
2499   if (dataExtend)
2500     {
2501 /*      werror(W_TOOMANY_SPILS,"data space", */
2502 /*             dataExtend,currFunc->name,""); */
2503       dataExtend = 0;
2504     }
2505
2506   if (options.dump_rassgn)
2507     dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
2508
2509   /* after that create the register mask
2510      for each of the instruction */
2511   createRegMask (ebbs, count);
2512
2513   /* now get back the chain */
2514   ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
2515
2516   /* redo that offsets for stacked automatic variables */
2517   redoStackOffsets ();
2518
2519   genZ80Code (ic);
2520
2521   /* free up any stackSpil locations allocated */
2522   applyToSet (stackSpil, deallocStackSpil);
2523   slocNum = 0;
2524   setToNull ((void **) &stackSpil);
2525   setToNull ((void **) &spiltSet);
2526   /* mark all registers as free */
2527   freeAllRegs ();
2528
2529   return;
2530 }