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