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