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