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