1 /*------------------------------------------------------------------------
3 SDCCralloc.c - source file for register allocation. (8051) specific
5 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
6 Added Pic Port T.scott Dattalo scott@dattalo.com (2000)
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
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.
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.
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 -------------------------------------------------------------------------*/
32 /*-----------------------------------------------------------------*/
33 /* At this point we start getting processor specific although */
34 /* some routines are non-processor specific & can be reused when */
35 /* targetting other processors. The decision for this will have */
36 /* to be made on a routine by routine basis */
37 /* routines used to pack registers are most definitely not reusable */
38 /* since the pack the registers depending strictly on the MCU */
39 /*-----------------------------------------------------------------*/
41 extern void genpic14Code (iCode *);
51 bitVect *funcrUsed; /* registers used in a function */
57 /* Shared with gen.c */
58 int pic14_ptrRegReq; /* one byte pointer register required */
61 /* A nasty, awful, disgusting hack for register declarations */
67 {REG_GPR, PO_GPR_TEMP, 0x0C, "r0x0C", "r0x0C", 0x0C, 1, 0},
68 {REG_GPR, PO_GPR_TEMP, 0x0D, "r0x0D", "r0x0C", 0x0D, 1, 0},
69 {REG_GPR, PO_GPR_TEMP, 0x0E, "r0x0E", "r0x0C", 0x0E, 1, 0},
70 {REG_GPR, PO_GPR_TEMP, 0x0F, "r0x0F", "r0x0C", 0x0F, 1, 0},
71 {REG_GPR, PO_GPR_TEMP, 0x10, "r0x10", "r0x10", 0x10, 1, 0},
72 {REG_GPR, PO_GPR_TEMP, 0x11, "r0x11", "r0x11", 0x11, 1, 0},
73 {REG_GPR, PO_GPR_TEMP, 0x12, "r0x12", "r0x12", 0x12, 1, 0},
74 {REG_GPR, PO_GPR_TEMP, 0x13, "r0x13", "r0x13", 0x13, 1, 0},
75 {REG_GPR, PO_GPR_TEMP, 0x14, "r0x14", "r0x14", 0x14, 1, 0},
76 {REG_GPR, PO_GPR_TEMP, 0x15, "r0x15", "r0x15", 0x15, 1, 0},
77 {REG_GPR, PO_GPR_TEMP, 0x16, "r0x16", "r0x16", 0x16, 1, 0},
78 {REG_GPR, PO_GPR_TEMP, 0x17, "r0x17", "r0x17", 0x17, 1, 0},
79 {REG_GPR, PO_GPR_TEMP, 0x18, "r0x18", "r0x18", 0x18, 1, 0},
80 {REG_GPR, PO_GPR_TEMP, 0x19, "r0x19", "r0x19", 0x19, 1, 0},
81 {REG_GPR, PO_GPR_TEMP, 0x1A, "r0x1A", "r0x1A", 0x1A, 1, 0},
82 {REG_GPR, PO_GPR_TEMP, 0x1B, "r0x1B", "r0x1B", 0x1B, 1, 0},
83 {REG_GPR, PO_GPR_TEMP, 0x1C, "r0x1C", "r0x1C", 0x1C, 1, 0},
84 {REG_GPR, PO_GPR_TEMP, 0x1D, "r0x1D", "r0x1D", 0x1D, 1, 0},
85 {REG_GPR, PO_GPR_TEMP, 0x1E, "r0x1E", "r0x1E", 0x1E, 1, 0},
86 {REG_GPR, PO_GPR_TEMP, 0x1F, "r0x1F", "r0x1F", 0x1F, 1, 0},
87 {REG_PTR, PO_FSR, 4, "FSR", "FSR", 4, 1, 0},
92 int Gstack_base_addr=0x38; /* The starting address of registers that
93 * are used to pass and return parameters */
96 {REG_GPR, PO_GPR_TEMP, 0x20, "r0x20", "r0x20", 0x20, 1, 0},
97 {REG_GPR, PO_GPR_TEMP, 0x21, "r0x21", "r0x21", 0x21, 1, 0},
98 {REG_GPR, PO_GPR_TEMP, 0x22, "r0x22", "r0x22", 0x22, 1, 0},
99 {REG_GPR, PO_GPR_TEMP, 0x23, "r0x23", "r0x23", 0x23, 1, 0},
100 {REG_GPR, PO_GPR_TEMP, 0x24, "r0x24", "r0x24", 0x24, 1, 0},
101 {REG_GPR, PO_GPR_TEMP, 0x25, "r0x25", "r0x25", 0x25, 1, 0},
102 {REG_GPR, PO_GPR_TEMP, 0x26, "r0x26", "r0x26", 0x26, 1, 0},
103 {REG_GPR, PO_GPR_TEMP, 0x27, "r0x27", "r0x27", 0x27, 1, 0},
104 {REG_GPR, PO_GPR_TEMP, 0x28, "r0x28", "r0x28", 0x28, 1, 0},
105 {REG_GPR, PO_GPR_TEMP, 0x29, "r0x29", "r0x29", 0x29, 1, 0},
106 {REG_GPR, PO_GPR_TEMP, 0x2A, "r0x2A", "r0x2A", 0x2A, 1, 0},
107 {REG_GPR, PO_GPR_TEMP, 0x2B, "r0x2B", "r0x2B", 0x2B, 1, 0},
108 {REG_GPR, PO_GPR_TEMP, 0x2C, "r0x2C", "r0x2C", 0x2C, 1, 0},
109 {REG_GPR, PO_GPR_TEMP, 0x2D, "r0x2D", "r0x2D", 0x2D, 1, 0},
110 {REG_GPR, PO_GPR_TEMP, 0x2E, "r0x2E", "r0x2E", 0x2E, 1, 0},
111 {REG_GPR, PO_GPR_TEMP, 0x2F, "r0x2F", "r0x2F", 0x2F, 1, 0},
112 {REG_GPR, PO_GPR_TEMP, 0x30, "r0x30", "r0x30", 0x30, 1, 0},
113 {REG_GPR, PO_GPR_TEMP, 0x31, "r0x31", "r0x31", 0x31, 1, 0},
114 {REG_GPR, PO_GPR_TEMP, 0x32, "r0x32", "r0x32", 0x32, 1, 0},
115 {REG_GPR, PO_GPR_TEMP, 0x33, "r0x33", "r0x33", 0x33, 1, 0},
116 {REG_GPR, PO_GPR_TEMP, 0x34, "r0x34", "r0x34", 0x34, 1, 0},
117 {REG_GPR, PO_GPR_TEMP, 0x35, "r0x35", "r0x35", 0x35, 1, 0},
118 {REG_GPR, PO_GPR_TEMP, 0x36, "r0x36", "r0x36", 0x36, 1, 0},
119 {REG_GPR, PO_GPR_TEMP, 0x37, "r0x37", "r0x37", 0x37, 1, 0},
120 {REG_STK, PO_GPR_TEMP, 0x38, "r0x38", "r0x38", 0x38, 1, 0},
121 {REG_STK, PO_GPR_TEMP, 0x39, "r0x39", "r0x39", 0x39, 1, 0},
122 {REG_STK, PO_GPR_TEMP, 0x3A, "r0x3A", "r0x3A", 0x3A, 1, 0},
123 {REG_STK, PO_GPR_TEMP, 0x3B, "r0x3B", "r0x3B", 0x3B, 1, 0},
124 {REG_STK, PO_GPR_TEMP, 0x3C, "r0x3C", "r0x3C", 0x3C, 1, 0},
125 {REG_STK, PO_GPR_TEMP, 0x3D, "r0x3D", "r0x3D", 0x3D, 1, 0},
126 {REG_STK, PO_GPR_TEMP, 0x3E, "r0x3E", "r0x3E", 0x3E, 1, 0},
127 {REG_STK, PO_GPR_TEMP, 0x3F, "r0x3F", "r0x3F", 0x3F, 1, 0},
128 {REG_STK, PO_GPR_TEMP, 0x40, "r0x40", "r0x40", 0x40, 1, 0},
129 {REG_STK, PO_GPR_TEMP, 0x41, "r0x41", "r0x41", 0x41, 1, 0},
130 {REG_STK, PO_GPR_TEMP, 0x42, "r0x42", "r0x42", 0x42, 1, 0},
131 {REG_STK, PO_GPR_TEMP, 0x43, "r0x43", "r0x43", 0x43, 1, 0},
132 {REG_STK, PO_GPR_TEMP, 0x44, "r0x44", "r0x44", 0x44, 1, 0},
133 {REG_STK, PO_GPR_TEMP, 0x45, "r0x45", "r0x45", 0x45, 1, 0},
134 {REG_STK, PO_GPR_TEMP, 0x46, "r0x46", "r0x46", 0x46, 1, 0},
135 {REG_STK, PO_GPR_TEMP, 0x47, "r0x47", "r0x47", 0x47, 1, 0},
137 {REG_PTR, PO_FSR, 4, "FSR", "FSR", 4, 1, 0},
143 int pic14_nRegs = sizeof (regspic14) / sizeof (regs);
144 static void spillThis (symbol *);
145 static int debug = 1;
146 static FILE *debugF = NULL;
147 /*-----------------------------------------------------------------*/
148 /* debugLog - open a file for debugging information */
149 /*-----------------------------------------------------------------*/
150 //static void debugLog(char *inst,char *fmt, ...)
152 debugLog (char *fmt,...)
154 static int append = 0; // First time through, open the file without append.
157 //char *bufferP=buffer;
160 if (!debug || !srcFileName)
166 /* create the file name */
167 strcpy (buffer, srcFileName);
168 strcat (buffer, ".d");
170 if (!(debugF = fopen (buffer, (append ? "a+" : "w"))))
172 werror (E_FILE_OPEN_ERR, buffer);
175 append = 1; // Next time debubLog is called, we'll append the debug info
181 vsprintf (buffer, fmt, ap);
183 fprintf (debugF, "%s", buffer);
185 while (isspace(*bufferP)) bufferP++;
187 if (bufferP && *bufferP)
188 lineCurr = (lineCurr ?
189 connectLine(lineCurr,newLineNode(lb)) :
190 (lineHead = newLineNode(lb)));
191 lineCurr->isInline = _G.inLine;
192 lineCurr->isDebug = _G.debugLine;
202 fputc ('\n', debugF);
204 /*-----------------------------------------------------------------*/
205 /* debugLogClose - closes the debug log file (if opened) */
206 /*-----------------------------------------------------------------*/
216 #define AOP(op) op->aop
219 debugAopGet (char *str, operand * op)
224 printOperand (op, debugF);
232 decodeOp (unsigned int op)
235 if (op < 128 && op > ' ')
237 buffer[0] = (op & 0xff);
251 return "STRING_LITERAL";
287 return "LEFT_ASSIGN";
289 return "RIGHT_ASSIGN";
404 case GET_VALUE_AT_ADDRESS:
405 return "GET_VALUE_AT_ADDRESS";
423 return "ENDFUNCTION";
447 sprintf (buffer, "unkown op %d %c", op, op & 0xff);
450 /*-----------------------------------------------------------------*/
451 /*-----------------------------------------------------------------*/
453 debugLogRegType (short type)
466 sprintf (buffer, "unkown reg type %d", type);
470 /*-----------------------------------------------------------------*/
471 /* allocReg - allocates register of given type */
472 /*-----------------------------------------------------------------*/
474 allocReg (short type)
478 debugLog ("%s of type %s\n", __FUNCTION__, debugLogRegType (type));
480 for (i = 0; i < pic14_nRegs; i++)
483 /* if type is given as 0 then any
484 free register will do */
488 regspic14[i].isFree = 0;
489 regspic14[i].wasUsed = 1;
492 bitVectSetBit (currFunc->regsUsed, i);
493 debugLog (" returning %s\n", regspic14[i].name);
494 return ®spic14[i];
496 /* other wise look for specific type
498 if (regspic14[i].isFree &&
499 regspic14[i].type == type)
501 regspic14[i].isFree = 0;
502 regspic14[i].wasUsed = 1;
505 bitVectSetBit (currFunc->regsUsed, i);
506 debugLog (" returning %s\n", regspic14[i].name);
507 return ®spic14[i];
513 /*-----------------------------------------------------------------*/
514 /* pic14_regWithIdx - returns pointer to register wit index number */
515 /*-----------------------------------------------------------------*/
517 pic14_regWithIdx (int idx)
521 debugLog ("%s - requesting index = 0x%x\n", __FUNCTION__,idx);
523 for (i = 0; i < pic14_nRegs; i++)
524 if (regspic14[i].rIdx == idx)
525 return ®spic14[i];
527 //return ®spic14[0];
528 fprintf(stderr,"%s %d - requested register: 0x%x\n",__FUNCTION__,__LINE__,idx);
529 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
530 "regWithIdx not found");
534 /*-----------------------------------------------------------------*/
535 /*-----------------------------------------------------------------*/
537 pic14_findFreeReg(short type)
541 for (i = 0; i < pic14_nRegs; i++) {
542 if (!type && regspic14[i].isFree)
543 return ®spic14[i];
544 if (regspic14[i].isFree &&
545 regspic14[i].type == type)
546 return ®spic14[i];
550 /*-----------------------------------------------------------------*/
551 /* freeReg - frees a register */
552 /*-----------------------------------------------------------------*/
556 debugLog ("%s\n", __FUNCTION__);
561 /*-----------------------------------------------------------------*/
562 /* nFreeRegs - returns number of free registers */
563 /*-----------------------------------------------------------------*/
570 debugLog ("%s\n", __FUNCTION__);
571 for (i = 0; i < pic14_nRegs; i++)
572 if (regspic14[i].isFree && regspic14[i].type == type)
577 /*-----------------------------------------------------------------*/
578 /* nfreeRegsType - free registers with type */
579 /*-----------------------------------------------------------------*/
581 nfreeRegsType (int type)
584 debugLog ("%s\n", __FUNCTION__);
587 if ((nfr = nFreeRegs (type)) == 0)
588 return nFreeRegs (REG_GPR);
591 return nFreeRegs (type);
595 /*-----------------------------------------------------------------*/
596 /* allDefsOutOfRange - all definitions are out of a range */
597 /*-----------------------------------------------------------------*/
599 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
603 debugLog ("%s\n", __FUNCTION__);
607 for (i = 0; i < defs->size; i++)
611 if (bitVectBitValue (defs, i) &&
612 (ic = hTabItemWithKey (iCodehTab, i)) &&
613 (ic->seq >= fseq && ic->seq <= toseq))
622 /*-----------------------------------------------------------------*/
623 /* computeSpillable - given a point find the spillable live ranges */
624 /*-----------------------------------------------------------------*/
626 computeSpillable (iCode * ic)
630 debugLog ("%s\n", __FUNCTION__);
631 /* spillable live ranges are those that are live at this
632 point . the following categories need to be subtracted
634 a) - those that are already spilt
635 b) - if being used by this one
636 c) - defined by this one */
638 spillable = bitVectCopy (ic->rlive);
640 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
642 bitVectCplAnd (spillable, ic->uses); /* used in this one */
643 bitVectUnSetBit (spillable, ic->defKey);
644 spillable = bitVectIntersect (spillable, _G.regAssigned);
649 /*-----------------------------------------------------------------*/
650 /* noSpilLoc - return true if a variable has no spil location */
651 /*-----------------------------------------------------------------*/
653 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
655 debugLog ("%s\n", __FUNCTION__);
656 return (sym->usl.spillLoc ? 0 : 1);
659 /*-----------------------------------------------------------------*/
660 /* hasSpilLoc - will return 1 if the symbol has spil location */
661 /*-----------------------------------------------------------------*/
663 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
665 debugLog ("%s\n", __FUNCTION__);
666 return (sym->usl.spillLoc ? 1 : 0);
669 /*-----------------------------------------------------------------*/
670 /* directSpilLoc - will return 1 if the splilocation is in direct */
671 /*-----------------------------------------------------------------*/
673 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
675 debugLog ("%s\n", __FUNCTION__);
676 if (sym->usl.spillLoc &&
677 (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
683 /*-----------------------------------------------------------------*/
684 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
685 /* but is not used as a pointer */
686 /*-----------------------------------------------------------------*/
688 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
690 debugLog ("%s\n", __FUNCTION__);
691 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
694 /*-----------------------------------------------------------------*/
695 /* rematable - will return 1 if the remat flag is set */
696 /*-----------------------------------------------------------------*/
698 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
700 debugLog ("%s\n", __FUNCTION__);
704 /*-----------------------------------------------------------------*/
705 /* notUsedInBlock - not used in this block */
706 /*-----------------------------------------------------------------*/
708 notUsedInBlock (symbol * sym, eBBlock * ebp, iCode * ic)
710 debugLog ("%s\n", __FUNCTION__);
711 return (!bitVectBitsInCommon (sym->defs, ebp->usesDefs) &&
712 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
713 /* return (!bitVectBitsInCommon(sym->defs,ebp->usesDefs)); */
716 /*-----------------------------------------------------------------*/
717 /* notUsedInRemaining - not used or defined in remain of the block */
718 /*-----------------------------------------------------------------*/
720 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
722 debugLog ("%s\n", __FUNCTION__);
723 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
724 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
727 /*-----------------------------------------------------------------*/
728 /* allLRs - return true for all */
729 /*-----------------------------------------------------------------*/
731 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
733 debugLog ("%s\n", __FUNCTION__);
737 /*-----------------------------------------------------------------*/
738 /* liveRangesWith - applies function to a given set of live range */
739 /*-----------------------------------------------------------------*/
741 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
742 eBBlock * ebp, iCode * ic)
747 debugLog ("%s\n", __FUNCTION__);
748 if (!lrs || !lrs->size)
751 for (i = 1; i < lrs->size; i++)
754 if (!bitVectBitValue (lrs, i))
757 /* if we don't find it in the live range
758 hash table we are in serious trouble */
759 if (!(sym = hTabItemWithKey (liveRanges, i)))
761 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
762 "liveRangesWith could not find liveRange");
766 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
767 addSetHead (&rset, sym);
774 /*-----------------------------------------------------------------*/
775 /* leastUsedLR - given a set determines which is the least used */
776 /*-----------------------------------------------------------------*/
778 leastUsedLR (set * sset)
780 symbol *sym = NULL, *lsym = NULL;
782 debugLog ("%s\n", __FUNCTION__);
783 sym = lsym = setFirstItem (sset);
788 for (; lsym; lsym = setNextItem (sset))
791 /* if usage is the same then prefer
792 the spill the smaller of the two */
793 if (lsym->used == sym->used)
794 if (getSize (lsym->type) < getSize (sym->type))
798 if (lsym->used < sym->used)
803 setToNull ((void **) &sset);
808 /*-----------------------------------------------------------------*/
809 /* noOverLap - will iterate through the list looking for over lap */
810 /*-----------------------------------------------------------------*/
812 noOverLap (set * itmpStack, symbol * fsym)
815 debugLog ("%s\n", __FUNCTION__);
818 for (sym = setFirstItem (itmpStack); sym;
819 sym = setNextItem (itmpStack))
821 if (sym->liveTo > fsym->liveFrom)
829 /*-----------------------------------------------------------------*/
830 /* isFree - will return 1 if the a free spil location is found */
831 /*-----------------------------------------------------------------*/
836 V_ARG (symbol **, sloc);
837 V_ARG (symbol *, fsym);
839 debugLog ("%s\n", __FUNCTION__);
840 /* if already found */
844 /* if it is free && and the itmp assigned to
845 this does not have any overlapping live ranges
846 with the one currently being assigned and
847 the size can be accomodated */
849 noOverLap (sym->usl.itmpStack, fsym) &&
850 getSize (sym->type) >= getSize (fsym->type))
859 /*-----------------------------------------------------------------*/
860 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
861 /*-----------------------------------------------------------------*/
863 spillLRWithPtrReg (symbol * forSym)
869 debugLog ("%s\n", __FUNCTION__);
870 if (!_G.regAssigned ||
871 bitVectIsZero (_G.regAssigned))
874 r0 = pic14_regWithIdx (R0_IDX);
875 r1 = pic14_regWithIdx (R1_IDX);
877 /* for all live ranges */
878 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
879 lrsym = hTabNextItem (liveRanges, &k))
883 /* if no registers assigned to it or
885 /* if it does not overlap with this then
886 not need to spill it */
888 if (lrsym->isspilt || !lrsym->nRegs ||
889 (lrsym->liveTo < forSym->liveFrom))
892 /* go thru the registers : if it is either
893 r0 or r1 then spil it */
894 for (j = 0; j < lrsym->nRegs; j++)
895 if (lrsym->regs[j] == r0 ||
896 lrsym->regs[j] == r1)
905 /*-----------------------------------------------------------------*/
906 /* createStackSpil - create a location on the stack to spil */
907 /*-----------------------------------------------------------------*/
909 createStackSpil (symbol * sym)
912 int useXstack, model, noOverlay;
915 debugLog ("%s\n", __FUNCTION__);
917 /* first go try and find a free one that is already
918 existing on the stack */
919 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
921 /* found a free one : just update & return */
922 sym->usl.spillLoc = sloc;
925 addSetHead (&sloc->usl.itmpStack, sym);
929 /* could not then have to create one , this is the hard part
930 we need to allocate this on the stack : this is really a
931 hack!! but cannot think of anything better at this time */
933 if (sprintf (slocBuffer, "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
935 fprintf (stderr, "kkkInternal error: slocBuffer overflowed: %s:%d\n",
940 sloc = newiTemp (slocBuffer);
942 /* set the type to the spilling symbol */
943 sloc->type = copyLinkChain (sym->type);
944 sloc->etype = getSpec (sloc->type);
945 SPEC_SCLS (sloc->etype) = S_DATA;
946 SPEC_EXTR (sloc->etype) = 0;
947 SPEC_STAT (sloc->etype) = 0;
949 /* we don't allow it to be allocated`
950 onto the external stack since : so we
951 temporarily turn it off ; we also
952 turn off memory model to prevent
953 the spil from going to the external storage
954 and turn off overlaying
957 useXstack = options.useXstack;
958 model = options.model;
959 noOverlay = options.noOverlay;
960 options.noOverlay = 1;
961 options.model = options.useXstack = 0;
965 options.useXstack = useXstack;
966 options.model = model;
967 options.noOverlay = noOverlay;
968 sloc->isref = 1; /* to prevent compiler warning */
970 /* if it is on the stack then update the stack */
971 if (IN_STACK (sloc->etype))
973 currFunc->stack += getSize (sloc->type);
974 _G.stackExtend += getSize (sloc->type);
977 _G.dataExtend += getSize (sloc->type);
979 /* add it to the _G.stackSpil set */
980 addSetHead (&_G.stackSpil, sloc);
981 sym->usl.spillLoc = sloc;
984 /* add it to the set of itempStack set
985 of the spill location */
986 addSetHead (&sloc->usl.itmpStack, sym);
990 /*-----------------------------------------------------------------*/
991 /* isSpiltOnStack - returns true if the spil location is on stack */
992 /*-----------------------------------------------------------------*/
994 isSpiltOnStack (symbol * sym)
998 debugLog ("%s\n", __FUNCTION__);
1005 /* if (sym->_G.stackSpil) */
1008 if (!sym->usl.spillLoc)
1011 etype = getSpec (sym->usl.spillLoc->type);
1012 if (IN_STACK (etype))
1018 /*-----------------------------------------------------------------*/
1019 /* spillThis - spils a specific operand */
1020 /*-----------------------------------------------------------------*/
1022 spillThis (symbol * sym)
1025 debugLog ("%s : %s\n", __FUNCTION__, sym->rname);
1027 /* if this is rematerializable or has a spillLocation
1028 we are okay, else we need to create a spillLocation
1030 if (!(sym->remat || sym->usl.spillLoc))
1031 createStackSpil (sym);
1034 /* mark it has spilt & put it in the spilt set */
1036 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
1038 bitVectUnSetBit (_G.regAssigned, sym->key);
1040 for (i = 0; i < sym->nRegs; i++)
1044 freeReg (sym->regs[i]);
1045 sym->regs[i] = NULL;
1048 /* if spilt on stack then free up r0 & r1
1049 if they could have been assigned to some
1051 if (!pic14_ptrRegReq && isSpiltOnStack (sym))
1054 spillLRWithPtrReg (sym);
1057 if (sym->usl.spillLoc && !sym->remat)
1058 sym->usl.spillLoc->allocreq = 1;
1062 /*-----------------------------------------------------------------*/
1063 /* selectSpil - select a iTemp to spil : rather a simple procedure */
1064 /*-----------------------------------------------------------------*/
1066 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
1068 bitVect *lrcs = NULL;
1072 debugLog ("%s\n", __FUNCTION__);
1073 /* get the spillable live ranges */
1074 lrcs = computeSpillable (ic);
1076 /* get all live ranges that are rematerizable */
1077 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
1080 /* return the least used of these */
1081 return leastUsedLR (selectS);
1084 /* get live ranges with spillLocations in direct space */
1085 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
1087 sym = leastUsedLR (selectS);
1088 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
1089 sym->usl.spillLoc->rname :
1090 sym->usl.spillLoc->name));
1092 /* mark it as allocation required */
1093 sym->usl.spillLoc->allocreq = 1;
1097 /* if the symbol is local to the block then */
1098 if (forSym->liveTo < ebp->lSeq)
1101 /* check if there are any live ranges allocated
1102 to registers that are not used in this block */
1103 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
1105 sym = leastUsedLR (selectS);
1106 /* if this is not rematerializable */
1115 /* check if there are any live ranges that not
1116 used in the remainder of the block */
1117 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
1119 sym = leastUsedLR (selectS);
1122 sym->remainSpil = 1;
1129 /* find live ranges with spillocation && not used as pointers */
1130 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
1133 sym = leastUsedLR (selectS);
1134 /* mark this as allocation required */
1135 sym->usl.spillLoc->allocreq = 1;
1139 /* find live ranges with spillocation */
1140 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
1143 sym = leastUsedLR (selectS);
1144 sym->usl.spillLoc->allocreq = 1;
1148 /* couldn't find then we need to create a spil
1149 location on the stack , for which one? the least
1151 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
1154 /* return a created spil location */
1155 sym = createStackSpil (leastUsedLR (selectS));
1156 sym->usl.spillLoc->allocreq = 1;
1160 /* this is an extreme situation we will spill
1161 this one : happens very rarely but it does happen */
1167 /*-----------------------------------------------------------------*/
1168 /* spilSomething - spil some variable & mark registers as free */
1169 /*-----------------------------------------------------------------*/
1171 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
1176 debugLog ("%s\n", __FUNCTION__);
1177 /* get something we can spil */
1178 ssym = selectSpil (ic, ebp, forSym);
1180 /* mark it as spilt */
1182 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
1184 /* mark it as not register assigned &
1185 take it away from the set */
1186 bitVectUnSetBit (_G.regAssigned, ssym->key);
1188 /* mark the registers as free */
1189 for (i = 0; i < ssym->nRegs; i++)
1191 freeReg (ssym->regs[i]);
1193 /* if spilt on stack then free up r0 & r1
1194 if they could have been assigned to as gprs */
1195 if (!pic14_ptrRegReq && isSpiltOnStack (ssym))
1198 spillLRWithPtrReg (ssym);
1201 /* if this was a block level spil then insert push & pop
1202 at the start & end of block respectively */
1203 if (ssym->blockSpil)
1205 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
1206 /* add push to the start of the block */
1207 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
1208 ebp->sch->next : ebp->sch));
1209 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
1210 /* add pop to the end of the block */
1211 addiCodeToeBBlock (ebp, nic, NULL);
1214 /* if spilt because not used in the remainder of the
1215 block then add a push before this instruction and
1216 a pop at the end of the block */
1217 if (ssym->remainSpil)
1220 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
1221 /* add push just before this instruction */
1222 addiCodeToeBBlock (ebp, nic, ic);
1224 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
1225 /* add pop to the end of the block */
1226 addiCodeToeBBlock (ebp, nic, NULL);
1235 /*-----------------------------------------------------------------*/
1236 /* getRegPtr - will try for PTR if not a GPR type if not spil */
1237 /*-----------------------------------------------------------------*/
1239 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
1243 debugLog ("%s\n", __FUNCTION__);
1245 /* try for a ptr type */
1246 if ((reg = allocReg (REG_PTR)))
1249 /* try for gpr type */
1250 if ((reg = allocReg (REG_GPR)))
1253 /* we have to spil */
1254 if (!spilSomething (ic, ebp, sym))
1257 /* this looks like an infinite loop but
1258 in really selectSpil will abort */
1262 /*-----------------------------------------------------------------*/
1263 /* getRegGpr - will try for GPR if not spil */
1264 /*-----------------------------------------------------------------*/
1266 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
1270 debugLog ("%s\n", __FUNCTION__);
1272 /* try for gpr type */
1273 if ((reg = allocReg (REG_GPR)))
1276 if (!pic14_ptrRegReq)
1277 if ((reg = allocReg (REG_PTR)))
1280 /* we have to spil */
1281 if (!spilSomething (ic, ebp, sym))
1284 /* this looks like an infinite loop but
1285 in really selectSpil will abort */
1289 /*-----------------------------------------------------------------*/
1290 /* symHasReg - symbol has a given register */
1291 /*-----------------------------------------------------------------*/
1293 symHasReg (symbol * sym, regs * reg)
1297 debugLog ("%s\n", __FUNCTION__);
1298 for (i = 0; i < sym->nRegs; i++)
1299 if (sym->regs[i] == reg)
1305 /*-----------------------------------------------------------------*/
1306 /* deassignLRs - check the live to and if they have registers & are */
1307 /* not spilt then free up the registers */
1308 /*-----------------------------------------------------------------*/
1310 deassignLRs (iCode * ic, eBBlock * ebp)
1316 debugLog ("%s\n", __FUNCTION__);
1317 for (sym = hTabFirstItem (liveRanges, &k); sym;
1318 sym = hTabNextItem (liveRanges, &k))
1321 symbol *psym = NULL;
1322 /* if it does not end here */
1323 if (sym->liveTo > ic->seq)
1326 /* if it was spilt on stack then we can
1327 mark the stack spil location as free */
1332 sym->usl.spillLoc->isFree = 1;
1338 if (!bitVectBitValue (_G.regAssigned, sym->key))
1341 /* special case check if this is an IFX &
1342 the privious one was a pop and the
1343 previous one was not spilt then keep track
1345 if (ic->op == IFX && ic->prev &&
1346 ic->prev->op == IPOP &&
1347 !ic->prev->parmPush &&
1348 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
1349 psym = OP_SYMBOL (IC_LEFT (ic->prev));
1355 bitVectUnSetBit (_G.regAssigned, sym->key);
1357 /* if the result of this one needs registers
1358 and does not have it then assign it right
1360 if (IC_RESULT (ic) &&
1361 !(SKIP_IC2 (ic) || /* not a special icode */
1362 ic->op == JUMPTABLE ||
1367 POINTER_SET (ic)) &&
1368 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
1369 result->liveTo > ic->seq && /* and will live beyond this */
1370 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
1371 result->regType == sym->regType && /* same register types */
1372 result->nRegs && /* which needs registers */
1373 !result->isspilt && /* and does not already have them */
1375 !bitVectBitValue (_G.regAssigned, result->key) &&
1376 /* the number of free regs + number of regs in this LR
1377 can accomodate the what result Needs */
1378 ((nfreeRegsType (result->regType) +
1379 sym->nRegs) >= result->nRegs)
1383 for (i = 0; i < max (sym->nRegs, result->nRegs); i++)
1385 result->regs[i] = sym->regs[i];
1387 result->regs[i] = getRegGpr (ic, ebp, result);
1389 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
1393 /* free the remaining */
1394 for (; i < sym->nRegs; i++)
1398 if (!symHasReg (psym, sym->regs[i]))
1399 freeReg (sym->regs[i]);
1402 freeReg (sym->regs[i]);
1409 /*-----------------------------------------------------------------*/
1410 /* reassignLR - reassign this to registers */
1411 /*-----------------------------------------------------------------*/
1413 reassignLR (operand * op)
1415 symbol *sym = OP_SYMBOL (op);
1418 debugLog ("%s\n", __FUNCTION__);
1419 /* not spilt any more */
1420 sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
1421 bitVectUnSetBit (_G.spiltSet, sym->key);
1423 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1427 for (i = 0; i < sym->nRegs; i++)
1428 sym->regs[i]->isFree = 0;
1431 /*-----------------------------------------------------------------*/
1432 /* willCauseSpill - determines if allocating will cause a spill */
1433 /*-----------------------------------------------------------------*/
1435 willCauseSpill (int nr, int rt)
1437 debugLog ("%s\n", __FUNCTION__);
1438 /* first check if there are any avlb registers
1439 of te type required */
1442 /* special case for pointer type
1443 if pointer type not avlb then
1444 check for type gpr */
1445 if (nFreeRegs (rt) >= nr)
1447 if (nFreeRegs (REG_GPR) >= nr)
1452 if (pic14_ptrRegReq)
1454 if (nFreeRegs (rt) >= nr)
1459 if (nFreeRegs (REG_PTR) +
1460 nFreeRegs (REG_GPR) >= nr)
1465 debugLog (" ... yep it will (cause a spill)\n");
1466 /* it will cause a spil */
1470 /*-----------------------------------------------------------------*/
1471 /* positionRegs - the allocator can allocate same registers to res- */
1472 /* ult and operand, if this happens make sure they are in the same */
1473 /* position as the operand otherwise chaos results */
1474 /*-----------------------------------------------------------------*/
1476 positionRegs (symbol * result, symbol * opsym, int lineno)
1478 int count = min (result->nRegs, opsym->nRegs);
1479 int i, j = 0, shared = 0;
1481 debugLog ("%s\n", __FUNCTION__);
1482 /* if the result has been spilt then cannot share */
1487 /* first make sure that they actually share */
1488 for (i = 0; i < count; i++)
1490 for (j = 0; j < count; j++)
1492 if (result->regs[i] == opsym->regs[j] && i != j)
1502 regs *tmp = result->regs[i];
1503 result->regs[i] = result->regs[j];
1504 result->regs[j] = tmp;
1509 /*-----------------------------------------------------------------*/
1510 /* serialRegAssign - serially allocate registers to the variables */
1511 /*-----------------------------------------------------------------*/
1513 serialRegAssign (eBBlock ** ebbs, int count)
1517 debugLog ("%s\n", __FUNCTION__);
1518 /* for all blocks */
1519 for (i = 0; i < count; i++)
1524 if (ebbs[i]->noPath &&
1525 (ebbs[i]->entryLabel != entryLabel &&
1526 ebbs[i]->entryLabel != returnLabel))
1529 /* of all instructions do */
1530 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1533 debugLog (" op: %s\n", decodeOp (ic->op));
1535 /* if this is an ipop that means some live
1536 range will have to be assigned again */
1538 reassignLR (IC_LEFT (ic));
1540 /* if result is present && is a true symbol */
1541 if (IC_RESULT (ic) && ic->op != IFX &&
1542 IS_TRUE_SYMOP (IC_RESULT (ic)))
1543 OP_SYMBOL (IC_RESULT (ic))->allocreq = 1;
1545 /* take away registers from live
1546 ranges that end at this instruction */
1547 deassignLRs (ic, ebbs[i]);
1549 /* some don't need registers */
1550 if (SKIP_IC2 (ic) ||
1551 ic->op == JUMPTABLE ||
1555 (IC_RESULT (ic) && POINTER_SET (ic)))
1558 /* now we need to allocate registers
1559 only for the result */
1562 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1568 /* if it does not need or is spilt
1569 or is already assigned to registers
1570 or will not live beyond this instructions */
1573 bitVectBitValue (_G.regAssigned, sym->key) ||
1574 sym->liveTo <= ic->seq)
1577 /* if some liverange has been spilt at the block level
1578 and this one live beyond this block then spil this
1580 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1585 /* if trying to allocate this will cause
1586 a spill and there is nothing to spill
1587 or this one is rematerializable then
1589 willCS = willCauseSpill (sym->nRegs, sym->regType);
1590 spillable = computeSpillable (ic);
1592 (willCS && bitVectIsZero (spillable)))
1600 /* if it has a spillocation & is used less than
1601 all other live ranges then spill this */
1603 if (sym->usl.spillLoc) {
1604 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1605 allLRs, ebbs[i], ic));
1606 if (leastUsed && leastUsed->used > sym->used) {
1611 /* if none of the liveRanges have a spillLocation then better
1612 to spill this one than anything else already assigned to registers */
1613 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1614 /* if this is local to this block then we might find a block spil */
1615 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
1623 if (ic->op == RECEIVE)
1624 debugLog ("When I get clever, I'll optimize the receive logic\n");
1626 /* if we need ptr regs for the right side
1628 if (POINTER_GET (ic) && getSize (OP_SYMBOL (IC_LEFT (ic))->type)
1629 <= (unsigned) PTRSIZE)
1634 /* else we assign registers to it */
1635 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1637 debugLog (" %d - \n", __LINE__);
1639 bitVectDebugOn(_G.regAssigned, debugF);
1641 for (j = 0; j < sym->nRegs; j++)
1643 if (sym->regType == REG_PTR)
1644 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1646 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1648 /* if the allocation falied which means
1649 this was spilt then break */
1653 debugLog (" %d - \n", __LINE__);
1655 /* if it shares registers with operands make sure
1656 that they are in the same position */
1657 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1658 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
1659 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1660 OP_SYMBOL (IC_LEFT (ic)), ic->lineno);
1661 /* do the same for the right operand */
1662 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1663 OP_SYMBOL (IC_RIGHT (ic))->nRegs && ic->op != '=')
1664 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1665 OP_SYMBOL (IC_RIGHT (ic)), ic->lineno);
1667 debugLog (" %d - \n", __LINE__);
1670 debugLog (" %d - \n", __LINE__);
1680 /*-----------------------------------------------------------------*/
1681 /* rUmaskForOp :- returns register mask for an operand */
1682 /*-----------------------------------------------------------------*/
1684 rUmaskForOp (operand * op)
1690 debugLog ("%s\n", __FUNCTION__);
1691 /* only temporaries are assigned registers */
1695 sym = OP_SYMBOL (op);
1697 /* if spilt or no registers assigned to it
1699 if (sym->isspilt || !sym->nRegs)
1702 rumask = newBitVect (pic14_nRegs);
1704 for (j = 0; j < sym->nRegs; j++)
1706 rumask = bitVectSetBit (rumask,
1707 sym->regs[j]->rIdx);
1713 /*-----------------------------------------------------------------*/
1714 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1715 /*-----------------------------------------------------------------*/
1717 regsUsedIniCode (iCode * ic)
1719 bitVect *rmask = newBitVect (pic14_nRegs);
1721 debugLog ("%s\n", __FUNCTION__);
1722 /* do the special cases first */
1725 rmask = bitVectUnion (rmask,
1726 rUmaskForOp (IC_COND (ic)));
1730 /* for the jumptable */
1731 if (ic->op == JUMPTABLE)
1733 rmask = bitVectUnion (rmask,
1734 rUmaskForOp (IC_JTCOND (ic)));
1739 /* of all other cases */
1741 rmask = bitVectUnion (rmask,
1742 rUmaskForOp (IC_LEFT (ic)));
1746 rmask = bitVectUnion (rmask,
1747 rUmaskForOp (IC_RIGHT (ic)));
1750 rmask = bitVectUnion (rmask,
1751 rUmaskForOp (IC_RESULT (ic)));
1757 /*-----------------------------------------------------------------*/
1758 /* createRegMask - for each instruction will determine the regsUsed */
1759 /*-----------------------------------------------------------------*/
1761 createRegMask (eBBlock ** ebbs, int count)
1765 debugLog ("%s\n", __FUNCTION__);
1766 /* for all blocks */
1767 for (i = 0; i < count; i++)
1771 if (ebbs[i]->noPath &&
1772 (ebbs[i]->entryLabel != entryLabel &&
1773 ebbs[i]->entryLabel != returnLabel))
1776 /* for all instructions */
1777 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1782 if (SKIP_IC2 (ic) || !ic->rlive)
1785 /* first mark the registers used in this
1787 ic->rUsed = regsUsedIniCode (ic);
1788 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1790 /* now create the register mask for those
1791 registers that are in use : this is a
1792 super set of ic->rUsed */
1793 ic->rMask = newBitVect (pic14_nRegs + 1);
1795 /* for all live Ranges alive at this point */
1796 for (j = 1; j < ic->rlive->size; j++)
1801 /* if not alive then continue */
1802 if (!bitVectBitValue (ic->rlive, j))
1805 /* find the live range we are interested in */
1806 if (!(sym = hTabItemWithKey (liveRanges, j)))
1808 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1809 "createRegMask cannot find live range");
1813 /* if no register assigned to it */
1814 if (!sym->nRegs || sym->isspilt)
1817 /* for all the registers allocated to it */
1818 for (k = 0; k < sym->nRegs; k++)
1821 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1827 /*-----------------------------------------------------------------*/
1828 /* rematStr - returns the rematerialized string for a remat var */
1829 /*-----------------------------------------------------------------*/
1831 rematStr (symbol * sym)
1834 iCode *ic = sym->rematiCode;
1836 debugLog ("%s\n", __FUNCTION__);
1841 /* if plus or minus print the right hand side */
1843 if (ic->op == '+' || ic->op == '-') {
1844 sprintf(s,"0x%04x %c ",(int) operandLitValue(IC_RIGHT(ic)),
1847 ic = OP_SYMBOL(IC_LEFT(ic))->rematiCode;
1851 if (ic->op == '+' || ic->op == '-')
1853 iCode *ric = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1854 sprintf (s, "(%s %c 0x%04x)",
1855 OP_SYMBOL (IC_LEFT (ric))->rname,
1857 (int) operandLitValue (IC_RIGHT (ic)));
1860 //ic = OP_SYMBOL(IC_LEFT(ic))->rematiCode;
1865 /* we reached the end */
1866 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1870 printf ("%s\n", buffer);
1874 /*-----------------------------------------------------------------*/
1875 /* regTypeNum - computes the type & number of registers required */
1876 /*-----------------------------------------------------------------*/
1884 debugLog ("%s\n", __FUNCTION__);
1885 /* for each live range do */
1886 for (sym = hTabFirstItem (liveRanges, &k); sym;
1887 sym = hTabNextItem (liveRanges, &k)) {
1889 debugLog (" %d - %s\n", __LINE__, sym->rname);
1891 /* if used zero times then no registers needed */
1892 if ((sym->liveTo - sym->liveFrom) == 0)
1896 /* if the live range is a temporary */
1899 debugLog (" %d - itemp register\n", __LINE__);
1901 /* if the type is marked as a conditional */
1902 if (sym->regType == REG_CND)
1905 /* if used in return only then we don't
1907 if (sym->ruonly || sym->accuse) {
1908 if (IS_AGGREGATE (sym->type) || sym->isptr)
1909 sym->type = aggrToPtr (sym->type, FALSE);
1910 debugLog (" %d - no reg needed - used as a return\n", __LINE__);
1915 /* if the symbol has only one definition &
1916 that definition is a get_pointer and the
1917 pointer we are getting is rematerializable and
1920 if (bitVectnBitsOn (sym->defs) == 1 &&
1921 (ic = hTabItemWithKey (iCodehTab,
1922 bitVectFirstBit (sym->defs))) &&
1925 !IS_BITVAR (sym->etype)) {
1928 debugLog (" %d - \n", __LINE__);
1930 /* if remat in data space */
1931 if (OP_SYMBOL (IC_LEFT (ic))->remat &&
1932 DCL_TYPE (aggrToPtr (sym->type, FALSE)) == POINTER) {
1934 /* create a psuedo symbol & force a spil */
1935 symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
1936 psym->type = sym->type;
1937 psym->etype = sym->etype;
1938 strcpy (psym->rname, psym->name);
1940 sym->usl.spillLoc = psym;
1944 /* if in data space or idata space then try to
1945 allocate pointer register */
1949 /* if not then we require registers */
1950 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1951 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1952 getSize (sym->type));
1954 if (sym->nRegs > 4) {
1955 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1956 printTypeChain (sym->type, stderr);
1957 fprintf (stderr, "\n");
1960 /* determine the type of register required */
1961 if (sym->nRegs == 1 &&
1962 IS_PTR (sym->type) &&
1964 sym->regType = REG_PTR;
1966 sym->regType = REG_GPR;
1969 debugLog (" reg name %s, reg type %s\n", sym->rname, debugLogRegType (sym->regType));
1973 /* for the first run we don't provide */
1974 /* registers for true symbols we will */
1975 /* see how things go */
1981 /*-----------------------------------------------------------------*/
1982 /* freeAllRegs - mark all registers as free */
1983 /*-----------------------------------------------------------------*/
1985 pic14_freeAllRegs ()
1989 debugLog ("%s\n", __FUNCTION__);
1990 for (i = 0; i < pic14_nRegs; i++)
1991 regspic14[i].isFree = 1;
1994 /*-----------------------------------------------------------------*/
1995 /*-----------------------------------------------------------------*/
1997 pic14_deallocateAllRegs ()
2001 debugLog ("%s\n", __FUNCTION__);
2002 for (i = 0; i < pic14_nRegs; i++) {
2003 regspic14[i].isFree = 1;
2004 regspic14[i].wasUsed = 0;
2009 /*-----------------------------------------------------------------*/
2010 /* deallocStackSpil - this will set the stack pointer back */
2011 /*-----------------------------------------------------------------*/
2013 DEFSETFUNC (deallocStackSpil)
2017 debugLog ("%s\n", __FUNCTION__);
2022 /*-----------------------------------------------------------------*/
2023 /* farSpacePackable - returns the packable icode for far variables */
2024 /*-----------------------------------------------------------------*/
2026 farSpacePackable (iCode * ic)
2030 debugLog ("%s\n", __FUNCTION__);
2031 /* go thru till we find a definition for the
2032 symbol on the right */
2033 for (dic = ic->prev; dic; dic = dic->prev)
2036 /* if the definition is a call then no */
2037 if ((dic->op == CALL || dic->op == PCALL) &&
2038 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2043 /* if shift by unknown amount then not */
2044 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
2045 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2048 /* if pointer get and size > 1 */
2049 if (POINTER_GET (dic) &&
2050 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
2053 if (POINTER_SET (dic) &&
2054 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
2057 /* if any three is a true symbol in far space */
2058 if (IC_RESULT (dic) &&
2059 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
2060 isOperandInFarSpace (IC_RESULT (dic)))
2063 if (IC_RIGHT (dic) &&
2064 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
2065 isOperandInFarSpace (IC_RIGHT (dic)) &&
2066 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
2069 if (IC_LEFT (dic) &&
2070 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
2071 isOperandInFarSpace (IC_LEFT (dic)) &&
2072 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
2075 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
2077 if ((dic->op == LEFT_OP ||
2078 dic->op == RIGHT_OP ||
2080 IS_OP_LITERAL (IC_RIGHT (dic)))
2090 /*-----------------------------------------------------------------*/
2091 /* packRegsForAssign - register reduction for assignment */
2092 /*-----------------------------------------------------------------*/
2094 packRegsForAssign (iCode * ic, eBBlock * ebp)
2099 debugLog ("%s\n", __FUNCTION__);
2101 debugAopGet (" result:", IC_RESULT (ic));
2102 debugAopGet (" left:", IC_LEFT (ic));
2103 debugAopGet (" right:", IC_RIGHT (ic));
2105 if (!IS_ITEMP (IC_RIGHT (ic)) ||
2106 OP_SYMBOL (IC_RIGHT (ic))->isind ||
2107 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
2109 debugLog (" %d - not packing - right side fails \n", __LINE__);
2113 /* if the true symbol is defined in far space or on stack
2114 then we should not since this will increase register pressure */
2115 if (isOperandInFarSpace (IC_RESULT (ic)))
2117 if ((dic = farSpacePackable (ic)))
2123 /* find the definition of iTempNN scanning backwards if we find a
2124 a use of the true symbol before we find the definition then
2126 for (dic = ic->prev; dic; dic = dic->prev)
2129 /* if there is a function call and this is
2130 a parameter & not my parameter then don't pack it */
2131 if ((dic->op == CALL || dic->op == PCALL) &&
2132 (OP_SYMBOL (IC_RESULT (ic))->_isparm &&
2133 !OP_SYMBOL (IC_RESULT (ic))->ismyparm))
2135 debugLog (" %d - \n", __LINE__);
2143 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
2144 IS_OP_VOLATILE (IC_RESULT (dic)))
2146 debugLog (" %d - \n", __LINE__);
2151 if (IS_SYMOP (IC_RESULT (dic)) &&
2152 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2154 debugLog (" %d - dic key == ic key -- pointer set=%c\n", __LINE__, ((POINTER_SET (dic)) ? 'Y' : 'N'));
2155 if (POINTER_SET (dic))
2161 if (IS_SYMOP (IC_RIGHT (dic)) &&
2162 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
2163 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
2165 debugLog (" %d - \n", __LINE__);
2170 if (IS_SYMOP (IC_LEFT (dic)) &&
2171 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
2172 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
2174 debugLog (" %d - \n", __LINE__);
2179 if (POINTER_SET (dic) &&
2180 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
2182 debugLog (" %d - \n", __LINE__);
2189 return 0; /* did not find */
2191 /* if the result is on stack or iaccess then it must be
2192 the same atleast one of the operands */
2193 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
2194 OP_SYMBOL (IC_RESULT (ic))->iaccess)
2197 /* the operation has only one symbol
2198 operator then we can pack */
2199 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
2200 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
2203 if (!((IC_LEFT (dic) &&
2204 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
2206 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
2210 debugLog (" packing. removing %s\n", OP_SYMBOL (IC_RIGHT (ic))->rname);
2211 /* found the definition */
2212 /* replace the result with the result of */
2213 /* this assignment and remove this assignment */
2214 IC_RESULT (dic) = IC_RESULT (ic);
2216 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
2218 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
2220 /* delete from liverange table also
2221 delete from all the points inbetween and the new
2223 for (sic = dic; sic != ic; sic = sic->next)
2225 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
2226 if (IS_ITEMP (IC_RESULT (dic)))
2227 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
2230 remiCodeFromeBBlock (ebp, ic);
2231 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2232 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2238 /*-----------------------------------------------------------------*/
2239 /* findAssignToSym : scanning backwards looks for first assig found */
2240 /*-----------------------------------------------------------------*/
2242 findAssignToSym (operand * op, iCode * ic)
2246 debugLog ("%s\n", __FUNCTION__);
2247 for (dic = ic->prev; dic; dic = dic->prev)
2250 /* if definition by assignment */
2251 if (dic->op == '=' &&
2252 !POINTER_SET (dic) &&
2253 IC_RESULT (dic)->key == op->key
2254 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
2258 /* we are interested only if defined in far space */
2259 /* or in stack space in case of + & - */
2261 /* if assigned to a non-symbol then return
2263 if (!IS_SYMOP (IC_RIGHT (dic)))
2266 /* if the symbol is in far space then
2268 if (isOperandInFarSpace (IC_RIGHT (dic)))
2271 /* for + & - operations make sure that
2272 if it is on the stack it is the same
2273 as one of the three operands */
2274 if ((ic->op == '+' || ic->op == '-') &&
2275 OP_SYMBOL (IC_RIGHT (dic))->onStack)
2278 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
2279 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
2280 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
2288 /* if we find an usage then we cannot delete it */
2289 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
2292 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
2295 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
2299 /* now make sure that the right side of dic
2300 is not defined between ic & dic */
2303 iCode *sic = dic->next;
2305 for (; sic != ic; sic = sic->next)
2306 if (IC_RESULT (sic) &&
2307 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2316 /*-----------------------------------------------------------------*/
2317 /* packRegsForSupport :- reduce some registers for support calls */
2318 /*-----------------------------------------------------------------*/
2320 packRegsForSupport (iCode * ic, eBBlock * ebp)
2324 debugLog ("%s\n", __FUNCTION__);
2325 /* for the left & right operand :- look to see if the
2326 left was assigned a true symbol in far space in that
2327 case replace them */
2328 if (IS_ITEMP (IC_LEFT (ic)) &&
2329 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2331 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
2337 debugAopGet ("removing left:", IC_LEFT (ic));
2339 /* found it we need to remove it from the
2341 for (sic = dic; sic != ic; sic = sic->next)
2342 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
2344 IC_LEFT (ic)->operand.symOperand =
2345 IC_RIGHT (dic)->operand.symOperand;
2346 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2347 remiCodeFromeBBlock (ebp, dic);
2348 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2352 /* do the same for the right operand */
2355 IS_ITEMP (IC_RIGHT (ic)) &&
2356 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2358 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2364 /* if this is a subtraction & the result
2365 is a true symbol in far space then don't pack */
2366 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2368 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2369 if (IN_FARSPACE (SPEC_OCLS (etype)))
2373 debugAopGet ("removing right:", IC_RIGHT (ic));
2375 /* found it we need to remove it from the
2377 for (sic = dic; sic != ic; sic = sic->next)
2378 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
2380 IC_RIGHT (ic)->operand.symOperand =
2381 IC_RIGHT (dic)->operand.symOperand;
2382 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2384 remiCodeFromeBBlock (ebp, dic);
2385 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2392 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2395 /*-----------------------------------------------------------------*/
2396 /* packRegsForOneuse : - will reduce some registers for single Use */
2397 /*-----------------------------------------------------------------*/
2399 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
2404 debugLog ("%s\n", __FUNCTION__);
2405 /* if returning a literal then do nothing */
2409 /* only upto 2 bytes since we cannot predict
2410 the usage of b, & acc */
2411 if (getSize (operandType (op)) > (fReturnSizePic - 2) &&
2416 /* this routine will mark the a symbol as used in one
2417 instruction use only && if the definition is local
2418 (ie. within the basic block) && has only one definition &&
2419 that definition is either a return value from a
2420 function or does not contain any variables in
2422 uses = bitVectCopy (OP_USES (op));
2423 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
2424 if (!bitVectIsZero (uses)) /* has other uses */
2427 /* if it has only one defintion */
2428 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2429 return NULL; /* has more than one definition */
2431 /* get that definition */
2433 hTabItemWithKey (iCodehTab,
2434 bitVectFirstBit (OP_DEFS (op)))))
2437 /* found the definition now check if it is local */
2438 if (dic->seq < ebp->fSeq ||
2439 dic->seq > ebp->lSeq)
2440 return NULL; /* non-local */
2442 /* now check if it is the return from
2444 if (dic->op == CALL || dic->op == PCALL)
2446 if (ic->op != SEND && ic->op != RETURN)
2448 OP_SYMBOL (op)->ruonly = 1;
2455 /* otherwise check that the definition does
2456 not contain any symbols in far space */
2457 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2458 isOperandInFarSpace (IC_RIGHT (dic)) ||
2459 IS_OP_RUONLY (IC_LEFT (ic)) ||
2460 IS_OP_RUONLY (IC_RIGHT (ic)))
2465 /* if pointer set then make sure the pointer
2467 if (POINTER_SET (dic) &&
2468 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2471 if (POINTER_GET (dic) &&
2472 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2477 /* also make sure the intervenening instructions
2478 don't have any thing in far space */
2479 for (dic = dic->next; dic && dic != ic; dic = dic->next)
2482 /* if there is an intervening function call then no */
2483 if (dic->op == CALL || dic->op == PCALL)
2485 /* if pointer set then make sure the pointer
2487 if (POINTER_SET (dic) &&
2488 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2491 if (POINTER_GET (dic) &&
2492 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2495 /* if address of & the result is remat then okay */
2496 if (dic->op == ADDRESS_OF &&
2497 OP_SYMBOL (IC_RESULT (dic))->remat)
2500 /* if operand has size of three or more & this
2501 operation is a '*','/' or '%' then 'b' may
2503 if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
2504 getSize (operandType (op)) >= 3)
2507 /* if left or right or result is in far space */
2508 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2509 isOperandInFarSpace (IC_RIGHT (dic)) ||
2510 isOperandInFarSpace (IC_RESULT (dic)) ||
2511 IS_OP_RUONLY (IC_LEFT (dic)) ||
2512 IS_OP_RUONLY (IC_RIGHT (dic)) ||
2513 IS_OP_RUONLY (IC_RESULT (dic)))
2519 OP_SYMBOL (op)->ruonly = 1;
2524 /*-----------------------------------------------------------------*/
2525 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2526 /*-----------------------------------------------------------------*/
2528 isBitwiseOptimizable (iCode * ic)
2530 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2531 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2533 debugLog ("%s\n", __FUNCTION__);
2534 /* bitwise operations are considered optimizable
2535 under the following conditions (Jean-Louis VERN)
2547 if (IS_LITERAL (rtype) ||
2548 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2554 /*-----------------------------------------------------------------*/
2555 /* packRegsForAccUse - pack registers for acc use */
2556 /*-----------------------------------------------------------------*/
2558 packRegsForAccUse (iCode * ic)
2562 debugLog ("%s\n", __FUNCTION__);
2563 /* if + or - then it has to be one byte result */
2564 if ((ic->op == '+' || ic->op == '-')
2565 && getSize (operandType (IC_RESULT (ic))) > 1)
2568 /* if shift operation make sure right side is not a literal */
2569 if (ic->op == RIGHT_OP &&
2570 (isOperandLiteral (IC_RIGHT (ic)) ||
2571 getSize (operandType (IC_RESULT (ic))) > 1))
2574 if (ic->op == LEFT_OP &&
2575 (isOperandLiteral (IC_RIGHT (ic)) ||
2576 getSize (operandType (IC_RESULT (ic))) > 1))
2579 if (IS_BITWISE_OP (ic) &&
2580 getSize (operandType (IC_RESULT (ic))) > 1)
2584 /* has only one definition */
2585 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2588 /* has only one use */
2589 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2592 /* and the usage immediately follows this iCode */
2593 if (!(uic = hTabItemWithKey (iCodehTab,
2594 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2597 if (ic->next != uic)
2600 /* if it is a conditional branch then we definitely can */
2604 if (uic->op == JUMPTABLE)
2607 /* if the usage is not is an assignment
2608 or an arithmetic / bitwise / shift operation then not */
2609 if (POINTER_SET (uic) &&
2610 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2613 if (uic->op != '=' &&
2614 !IS_ARITHMETIC_OP (uic) &&
2615 !IS_BITWISE_OP (uic) &&
2616 uic->op != LEFT_OP &&
2617 uic->op != RIGHT_OP)
2620 /* if used in ^ operation then make sure right is not a
2622 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2625 /* if shift operation make sure right side is not a literal */
2626 if (uic->op == RIGHT_OP &&
2627 (isOperandLiteral (IC_RIGHT (uic)) ||
2628 getSize (operandType (IC_RESULT (uic))) > 1))
2631 if (uic->op == LEFT_OP &&
2632 (isOperandLiteral (IC_RIGHT (uic)) ||
2633 getSize (operandType (IC_RESULT (uic))) > 1))
2636 /* make sure that the result of this icode is not on the
2637 stack, since acc is used to compute stack offset */
2638 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2639 OP_SYMBOL (IC_RESULT (uic))->onStack)
2642 /* if either one of them in far space then we cannot */
2643 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2644 isOperandInFarSpace (IC_LEFT (uic))) ||
2645 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2646 isOperandInFarSpace (IC_RIGHT (uic))))
2649 /* if the usage has only one operand then we can */
2650 if (IC_LEFT (uic) == NULL ||
2651 IC_RIGHT (uic) == NULL)
2654 /* make sure this is on the left side if not
2655 a '+' since '+' is commutative */
2656 if (ic->op != '+' &&
2657 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2660 /* if one of them is a literal then we can */
2661 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2662 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2664 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2668 /* if the other one is not on stack then we can */
2669 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2670 (IS_ITEMP (IC_RIGHT (uic)) ||
2671 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2672 !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
2675 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2676 (IS_ITEMP (IC_LEFT (uic)) ||
2677 (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2678 !OP_SYMBOL (IC_LEFT (uic))->onStack)))
2684 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2689 /*-----------------------------------------------------------------*/
2690 /* packForPush - hueristics to reduce iCode for pushing */
2691 /*-----------------------------------------------------------------*/
2693 packForReceive (iCode * ic, eBBlock * ebp)
2697 debugLog ("%s\n", __FUNCTION__);
2698 debugAopGet (" result:", IC_RESULT (ic));
2699 debugAopGet (" left:", IC_LEFT (ic));
2700 debugAopGet (" right:", IC_RIGHT (ic));
2705 for (dic = ic->next; dic; dic = dic->next)
2710 if (IC_LEFT (dic) && (IC_RESULT (ic)->key == IC_LEFT (dic)->key))
2711 debugLog (" used on left\n");
2712 if (IC_RIGHT (dic) && IC_RESULT (ic)->key == IC_RIGHT (dic)->key)
2713 debugLog (" used on right\n");
2714 if (IC_RESULT (dic) && IC_RESULT (ic)->key == IC_RESULT (dic)->key)
2715 debugLog (" used on result\n");
2717 if ((IC_LEFT (dic) && (IC_RESULT (ic)->key == IC_LEFT (dic)->key)) ||
2718 (IC_RESULT (dic) && IC_RESULT (ic)->key == IC_RESULT (dic)->key))
2723 debugLog (" hey we can remove this unnecessary assign\n");
2725 /*-----------------------------------------------------------------*/
2726 /* packForPush - hueristics to reduce iCode for pushing */
2727 /*-----------------------------------------------------------------*/
2729 packForPush (iCode * ic, eBBlock * ebp)
2733 debugLog ("%s\n", __FUNCTION__);
2734 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
2737 /* must have only definition & one usage */
2738 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2739 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2742 /* find the definition */
2743 if (!(dic = hTabItemWithKey (iCodehTab,
2744 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2747 if (dic->op != '=' || POINTER_SET (dic))
2750 /* we now we know that it has one & only one def & use
2751 and the that the definition is an assignment */
2752 IC_LEFT (ic) = IC_RIGHT (dic);
2754 remiCodeFromeBBlock (ebp, dic);
2755 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2758 /*-----------------------------------------------------------------*/
2759 /* packRegisters - does some transformations to reduce register */
2761 /*-----------------------------------------------------------------*/
2763 packRegisters (eBBlock * ebp)
2768 debugLog ("%s\n", __FUNCTION__);
2775 /* look for assignments of the form */
2776 /* iTempNN = TRueSym (someoperation) SomeOperand */
2778 /* TrueSym := iTempNN:1 */
2779 for (ic = ebp->sch; ic; ic = ic->next)
2782 /* find assignment of the form TrueSym := iTempNN:1 */
2783 if (ic->op == '=' && !POINTER_SET (ic))
2784 change += packRegsForAssign (ic, ebp);
2788 if (POINTER_SET (ic))
2789 debugLog ("pointer is set\n");
2790 debugAopGet (" result:", IC_RESULT (ic));
2791 debugAopGet (" left:", IC_LEFT (ic));
2792 debugAopGet (" right:", IC_RIGHT (ic));
2801 for (ic = ebp->sch; ic; ic = ic->next)
2804 /* if this is an itemp & result of a address of a true sym
2805 then mark this as rematerialisable */
2806 if (ic->op == ADDRESS_OF &&
2807 IS_ITEMP (IC_RESULT (ic)) &&
2808 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2809 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2810 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2813 debugLog (" %d - %s. result is rematerializable\n", __LINE__,__FUNCTION__);
2815 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2816 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2817 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2821 /* if straight assignment then carry remat flag if
2822 this is the only definition */
2823 if (ic->op == '=' &&
2824 !POINTER_SET (ic) &&
2825 IS_SYMOP (IC_RIGHT (ic)) &&
2826 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2827 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2829 debugLog (" %d - %s. straight rematerializable\n", __LINE__,__FUNCTION__);
2831 OP_SYMBOL (IC_RESULT (ic))->remat =
2832 OP_SYMBOL (IC_RIGHT (ic))->remat;
2833 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2834 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2837 /* if this is a +/- operation with a rematerizable
2838 then mark this as rematerializable as well */
2839 if ((ic->op == '+' || ic->op == '-') &&
2840 (IS_SYMOP (IC_LEFT (ic)) &&
2841 IS_ITEMP (IC_RESULT (ic)) &&
2842 OP_SYMBOL (IC_LEFT (ic))->remat &&
2843 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2844 IS_OP_LITERAL (IC_RIGHT (ic))))
2846 debugLog (" %d - %s. rematerializable because op is +/-\n", __LINE__,__FUNCTION__);
2848 operandLitValue (IC_RIGHT (ic));
2849 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2850 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2851 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2854 /* mark the pointer usages */
2855 if (POINTER_SET (ic))
2857 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2858 debugLog (" marking as a pointer (set) =>");
2859 debugAopGet (" result:", IC_RESULT (ic));
2861 if (POINTER_GET (ic))
2863 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2864 debugLog (" marking as a pointer (get) =>");
2865 debugAopGet (" left:", IC_LEFT (ic));
2870 /* if we are using a symbol on the stack
2871 then we should say pic14_ptrRegReq */
2872 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2873 pic14_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ||
2874 OP_SYMBOL (IC_COND (ic))->iaccess) ? 1 : 0);
2875 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2876 pic14_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ||
2877 OP_SYMBOL (IC_JTCOND (ic))->iaccess) ? 1 : 0);
2880 if (IS_SYMOP (IC_LEFT (ic)))
2881 pic14_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ||
2882 OP_SYMBOL (IC_LEFT (ic))->iaccess) ? 1 : 0);
2883 if (IS_SYMOP (IC_RIGHT (ic)))
2884 pic14_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ||
2885 OP_SYMBOL (IC_RIGHT (ic))->iaccess) ? 1 : 0);
2886 if (IS_SYMOP (IC_RESULT (ic)))
2887 pic14_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ||
2888 OP_SYMBOL (IC_RESULT (ic))->iaccess) ? 1 : 0);
2892 /* if the condition of an if instruction
2893 is defined in the previous instruction then
2894 mark the itemp as a conditional */
2895 if ((IS_CONDITIONAL (ic) ||
2896 ((ic->op == BITWISEAND ||
2899 isBitwiseOptimizable (ic))) &&
2900 ic->next && ic->next->op == IFX &&
2901 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2902 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2905 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2909 /* reduce for support function calls */
2910 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2911 packRegsForSupport (ic, ebp);
2913 /* if a parameter is passed, it's in W, so we may not
2914 need to place a copy in a register */
2915 if (ic->op == RECEIVE)
2916 packForReceive (ic, ebp);
2918 /* some cases the redundant moves can
2919 can be eliminated for return statements */
2920 if ((ic->op == RETURN || ic->op == SEND) &&
2921 !isOperandInFarSpace (IC_LEFT (ic)) &&
2923 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2925 /* if pointer set & left has a size more than
2926 one and right is not in far space */
2927 if (POINTER_SET (ic) &&
2928 !isOperandInFarSpace (IC_RIGHT (ic)) &&
2929 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2930 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2931 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2933 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2935 /* if pointer get */
2936 if (POINTER_GET (ic) &&
2937 !isOperandInFarSpace (IC_RESULT (ic)) &&
2938 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2939 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2940 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2942 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2945 /* if this is cast for intergral promotion then
2946 check if only use of the definition of the
2947 operand being casted/ if yes then replace
2948 the result of that arithmetic operation with
2949 this result and get rid of the cast */
2952 sym_link *fromType = operandType (IC_RIGHT (ic));
2953 sym_link *toType = operandType (IC_LEFT (ic));
2955 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2956 getSize (fromType) != getSize (toType))
2959 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2962 if (IS_ARITHMETIC_OP (dic))
2964 IC_RESULT (dic) = IC_RESULT (ic);
2965 remiCodeFromeBBlock (ebp, ic);
2966 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2967 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2971 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2977 /* if the type from and type to are the same
2978 then if this is the only use then packit */
2979 if (compareType (operandType (IC_RIGHT (ic)),
2980 operandType (IC_LEFT (ic))) == 1)
2982 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2985 IC_RESULT (dic) = IC_RESULT (ic);
2986 remiCodeFromeBBlock (ebp, ic);
2987 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2988 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2996 iTempNN := (some variable in farspace) V1
3001 if (ic->op == IPUSH)
3003 packForPush (ic, ebp);
3007 /* pack registers for accumulator use, when the
3008 result of an arithmetic or bit wise operation
3009 has only one use, that use is immediately following
3010 the defintion and the using iCode has only one
3011 operand or has two operands but one is literal &
3012 the result of that operation is not on stack then
3013 we can leave the result of this operation in acc:b
3015 if ((IS_ARITHMETIC_OP (ic)
3017 || IS_BITWISE_OP (ic)
3019 || ic->op == LEFT_OP || ic->op == RIGHT_OP
3022 IS_ITEMP (IC_RESULT (ic)) &&
3023 getSize (operandType (IC_RESULT (ic))) <= 2)
3025 packRegsForAccUse (ic);
3031 dumpEbbsToDebug (eBBlock ** ebbs, int count)
3035 if (!debug || !debugF)
3038 for (i = 0; i < count; i++)
3040 fprintf (debugF, "\n----------------------------------------------------------------\n");
3041 fprintf (debugF, "Basic Block %s : loop Depth = %d noPath = %d , lastinLoop = %d\n",
3042 ebbs[i]->entryLabel->name,
3045 ebbs[i]->isLastInLoop);
3046 fprintf (debugF, "depth 1st num %d : bbnum = %d 1st iCode = %d , last iCode = %d\n",
3051 fprintf (debugF, "visited %d : hasFcall = %d\n",
3055 fprintf (debugF, "\ndefines bitVector :");
3056 bitVectDebugOn (ebbs[i]->defSet, debugF);
3057 fprintf (debugF, "\nlocal defines bitVector :");
3058 bitVectDebugOn (ebbs[i]->ldefs, debugF);
3059 fprintf (debugF, "\npointers Set bitvector :");
3060 bitVectDebugOn (ebbs[i]->ptrsSet, debugF);
3061 fprintf (debugF, "\nin pointers Set bitvector :");
3062 bitVectDebugOn (ebbs[i]->inPtrsSet, debugF);
3063 fprintf (debugF, "\ninDefs Set bitvector :");
3064 bitVectDebugOn (ebbs[i]->inDefs, debugF);
3065 fprintf (debugF, "\noutDefs Set bitvector :");
3066 bitVectDebugOn (ebbs[i]->outDefs, debugF);
3067 fprintf (debugF, "\nusesDefs Set bitvector :");
3068 bitVectDebugOn (ebbs[i]->usesDefs, debugF);
3069 fprintf (debugF, "\n----------------------------------------------------------------\n");
3070 printiCChain (ebbs[i]->sch, debugF);
3073 /*-----------------------------------------------------------------*/
3074 /* assignRegisters - assigns registers to each live range as need */
3075 /*-----------------------------------------------------------------*/
3077 pic14_assignRegisters (eBBlock ** ebbs, int count)
3082 debugLog ("<><><><><><><><><><><><><><><><><>\nstarting\t%s:%s", __FILE__, __FUNCTION__);
3083 debugLog ("ebbs before optimizing:\n");
3084 dumpEbbsToDebug (ebbs, count);
3086 setToNull ((void *) &_G.funcrUsed);
3087 pic14_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
3090 /* change assignments this will remove some
3091 live ranges reducing some register pressure */
3092 for (i = 0; i < count; i++)
3093 packRegisters (ebbs[i]);
3095 if (options.dump_pack)
3096 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
3098 /* first determine for each live range the number of
3099 registers & the type of registers required for each */
3102 /* and serially allocate registers */
3103 serialRegAssign (ebbs, count);
3105 /* if stack was extended then tell the user */
3108 /* werror(W_TOOMANY_SPILS,"stack", */
3109 /* _G.stackExtend,currFunc->name,""); */
3115 /* werror(W_TOOMANY_SPILS,"data space", */
3116 /* _G.dataExtend,currFunc->name,""); */
3120 /* after that create the register mask
3121 for each of the instruction */
3122 createRegMask (ebbs, count);
3124 /* redo that offsets for stacked automatic variables */
3125 redoStackOffsets ();
3127 if (options.dump_rassgn)
3128 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
3130 /* now get back the chain */
3131 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
3133 debugLog ("ebbs after optimizing:\n");
3134 dumpEbbsToDebug (ebbs, count);
3139 /* free up any _G.stackSpil locations allocated */
3140 applyToSet (_G.stackSpil, deallocStackSpil);
3142 setToNull ((void **) &_G.stackSpil);
3143 setToNull ((void **) &_G.spiltSet);
3144 /* mark all registers as free */
3145 pic14_freeAllRegs ();
3147 debugLog ("leaving\n<><><><><><><><><><><><><><><><><>\n");