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