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