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;
948 /* we don't allow it to be allocated`
949 onto the external stack since : so we
950 temporarily turn it off ; we also
951 turn off memory model to prevent
952 the spil from going to the external storage
953 and turn off overlaying
956 useXstack = options.useXstack;
957 model = options.model;
958 noOverlay = options.noOverlay;
959 options.noOverlay = 1;
960 options.model = options.useXstack = 0;
964 options.useXstack = useXstack;
965 options.model = model;
966 options.noOverlay = noOverlay;
967 sloc->isref = 1; /* to prevent compiler warning */
969 /* if it is on the stack then update the stack */
970 if (IN_STACK (sloc->etype))
972 currFunc->stack += getSize (sloc->type);
973 _G.stackExtend += getSize (sloc->type);
976 _G.dataExtend += getSize (sloc->type);
978 /* add it to the _G.stackSpil set */
979 addSetHead (&_G.stackSpil, sloc);
980 sym->usl.spillLoc = sloc;
983 /* add it to the set of itempStack set
984 of the spill location */
985 addSetHead (&sloc->usl.itmpStack, sym);
989 /*-----------------------------------------------------------------*/
990 /* isSpiltOnStack - returns true if the spil location is on stack */
991 /*-----------------------------------------------------------------*/
993 isSpiltOnStack (symbol * sym)
997 debugLog ("%s\n", __FUNCTION__);
1004 /* if (sym->_G.stackSpil) */
1007 if (!sym->usl.spillLoc)
1010 etype = getSpec (sym->usl.spillLoc->type);
1011 if (IN_STACK (etype))
1017 /*-----------------------------------------------------------------*/
1018 /* spillThis - spils a specific operand */
1019 /*-----------------------------------------------------------------*/
1021 spillThis (symbol * sym)
1024 debugLog ("%s : %s\n", __FUNCTION__, sym->rname);
1026 /* if this is rematerializable or has a spillLocation
1027 we are okay, else we need to create a spillLocation
1029 if (!(sym->remat || sym->usl.spillLoc))
1030 createStackSpil (sym);
1033 /* mark it has spilt & put it in the spilt set */
1035 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
1037 bitVectUnSetBit (_G.regAssigned, sym->key);
1039 for (i = 0; i < sym->nRegs; i++)
1043 freeReg (sym->regs[i]);
1044 sym->regs[i] = NULL;
1047 /* if spilt on stack then free up r0 & r1
1048 if they could have been assigned to some
1050 if (!pic14_ptrRegReq && isSpiltOnStack (sym))
1053 spillLRWithPtrReg (sym);
1056 if (sym->usl.spillLoc && !sym->remat)
1057 sym->usl.spillLoc->allocreq = 1;
1061 /*-----------------------------------------------------------------*/
1062 /* selectSpil - select a iTemp to spil : rather a simple procedure */
1063 /*-----------------------------------------------------------------*/
1065 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
1067 bitVect *lrcs = NULL;
1071 debugLog ("%s\n", __FUNCTION__);
1072 /* get the spillable live ranges */
1073 lrcs = computeSpillable (ic);
1075 /* get all live ranges that are rematerizable */
1076 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
1079 /* return the least used of these */
1080 return leastUsedLR (selectS);
1083 /* get live ranges with spillLocations in direct space */
1084 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
1086 sym = leastUsedLR (selectS);
1087 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
1088 sym->usl.spillLoc->rname :
1089 sym->usl.spillLoc->name));
1091 /* mark it as allocation required */
1092 sym->usl.spillLoc->allocreq = 1;
1096 /* if the symbol is local to the block then */
1097 if (forSym->liveTo < ebp->lSeq)
1100 /* check if there are any live ranges allocated
1101 to registers that are not used in this block */
1102 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
1104 sym = leastUsedLR (selectS);
1105 /* if this is not rematerializable */
1114 /* check if there are any live ranges that not
1115 used in the remainder of the block */
1116 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
1118 sym = leastUsedLR (selectS);
1121 sym->remainSpil = 1;
1128 /* find live ranges with spillocation && not used as pointers */
1129 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
1132 sym = leastUsedLR (selectS);
1133 /* mark this as allocation required */
1134 sym->usl.spillLoc->allocreq = 1;
1138 /* find live ranges with spillocation */
1139 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
1142 sym = leastUsedLR (selectS);
1143 sym->usl.spillLoc->allocreq = 1;
1147 /* couldn't find then we need to create a spil
1148 location on the stack , for which one? the least
1150 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
1153 /* return a created spil location */
1154 sym = createStackSpil (leastUsedLR (selectS));
1155 sym->usl.spillLoc->allocreq = 1;
1159 /* this is an extreme situation we will spill
1160 this one : happens very rarely but it does happen */
1166 /*-----------------------------------------------------------------*/
1167 /* spilSomething - spil some variable & mark registers as free */
1168 /*-----------------------------------------------------------------*/
1170 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
1175 debugLog ("%s\n", __FUNCTION__);
1176 /* get something we can spil */
1177 ssym = selectSpil (ic, ebp, forSym);
1179 /* mark it as spilt */
1181 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
1183 /* mark it as not register assigned &
1184 take it away from the set */
1185 bitVectUnSetBit (_G.regAssigned, ssym->key);
1187 /* mark the registers as free */
1188 for (i = 0; i < ssym->nRegs; i++)
1190 freeReg (ssym->regs[i]);
1192 /* if spilt on stack then free up r0 & r1
1193 if they could have been assigned to as gprs */
1194 if (!pic14_ptrRegReq && isSpiltOnStack (ssym))
1197 spillLRWithPtrReg (ssym);
1200 /* if this was a block level spil then insert push & pop
1201 at the start & end of block respectively */
1202 if (ssym->blockSpil)
1204 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
1205 /* add push to the start of the block */
1206 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
1207 ebp->sch->next : ebp->sch));
1208 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
1209 /* add pop to the end of the block */
1210 addiCodeToeBBlock (ebp, nic, NULL);
1213 /* if spilt because not used in the remainder of the
1214 block then add a push before this instruction and
1215 a pop at the end of the block */
1216 if (ssym->remainSpil)
1219 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
1220 /* add push just before this instruction */
1221 addiCodeToeBBlock (ebp, nic, ic);
1223 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
1224 /* add pop to the end of the block */
1225 addiCodeToeBBlock (ebp, nic, NULL);
1234 /*-----------------------------------------------------------------*/
1235 /* getRegPtr - will try for PTR if not a GPR type if not spil */
1236 /*-----------------------------------------------------------------*/
1238 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
1242 debugLog ("%s\n", __FUNCTION__);
1244 /* try for a ptr type */
1245 if ((reg = allocReg (REG_PTR)))
1248 /* try for gpr type */
1249 if ((reg = allocReg (REG_GPR)))
1252 /* we have to spil */
1253 if (!spilSomething (ic, ebp, sym))
1256 /* this looks like an infinite loop but
1257 in really selectSpil will abort */
1261 /*-----------------------------------------------------------------*/
1262 /* getRegGpr - will try for GPR if not spil */
1263 /*-----------------------------------------------------------------*/
1265 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
1269 debugLog ("%s\n", __FUNCTION__);
1271 /* try for gpr type */
1272 if ((reg = allocReg (REG_GPR)))
1275 if (!pic14_ptrRegReq)
1276 if ((reg = allocReg (REG_PTR)))
1279 /* we have to spil */
1280 if (!spilSomething (ic, ebp, sym))
1283 /* this looks like an infinite loop but
1284 in really selectSpil will abort */
1288 /*-----------------------------------------------------------------*/
1289 /* symHasReg - symbol has a given register */
1290 /*-----------------------------------------------------------------*/
1292 symHasReg (symbol * sym, regs * reg)
1296 debugLog ("%s\n", __FUNCTION__);
1297 for (i = 0; i < sym->nRegs; i++)
1298 if (sym->regs[i] == reg)
1304 /*-----------------------------------------------------------------*/
1305 /* deassignLRs - check the live to and if they have registers & are */
1306 /* not spilt then free up the registers */
1307 /*-----------------------------------------------------------------*/
1309 deassignLRs (iCode * ic, eBBlock * ebp)
1315 debugLog ("%s\n", __FUNCTION__);
1316 for (sym = hTabFirstItem (liveRanges, &k); sym;
1317 sym = hTabNextItem (liveRanges, &k))
1320 symbol *psym = NULL;
1321 /* if it does not end here */
1322 if (sym->liveTo > ic->seq)
1325 /* if it was spilt on stack then we can
1326 mark the stack spil location as free */
1331 sym->usl.spillLoc->isFree = 1;
1337 if (!bitVectBitValue (_G.regAssigned, sym->key))
1340 /* special case check if this is an IFX &
1341 the privious one was a pop and the
1342 previous one was not spilt then keep track
1344 if (ic->op == IFX && ic->prev &&
1345 ic->prev->op == IPOP &&
1346 !ic->prev->parmPush &&
1347 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
1348 psym = OP_SYMBOL (IC_LEFT (ic->prev));
1354 bitVectUnSetBit (_G.regAssigned, sym->key);
1356 /* if the result of this one needs registers
1357 and does not have it then assign it right
1359 if (IC_RESULT (ic) &&
1360 !(SKIP_IC2 (ic) || /* not a special icode */
1361 ic->op == JUMPTABLE ||
1366 POINTER_SET (ic)) &&
1367 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
1368 result->liveTo > ic->seq && /* and will live beyond this */
1369 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
1370 result->regType == sym->regType && /* same register types */
1371 result->nRegs && /* which needs registers */
1372 !result->isspilt && /* and does not already have them */
1374 !bitVectBitValue (_G.regAssigned, result->key) &&
1375 /* the number of free regs + number of regs in this LR
1376 can accomodate the what result Needs */
1377 ((nfreeRegsType (result->regType) +
1378 sym->nRegs) >= result->nRegs)
1382 for (i = 0; i < max (sym->nRegs, result->nRegs); i++)
1384 result->regs[i] = sym->regs[i];
1386 result->regs[i] = getRegGpr (ic, ebp, result);
1388 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
1392 /* free the remaining */
1393 for (; i < sym->nRegs; i++)
1397 if (!symHasReg (psym, sym->regs[i]))
1398 freeReg (sym->regs[i]);
1401 freeReg (sym->regs[i]);
1408 /*-----------------------------------------------------------------*/
1409 /* reassignLR - reassign this to registers */
1410 /*-----------------------------------------------------------------*/
1412 reassignLR (operand * op)
1414 symbol *sym = OP_SYMBOL (op);
1417 debugLog ("%s\n", __FUNCTION__);
1418 /* not spilt any more */
1419 sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
1420 bitVectUnSetBit (_G.spiltSet, sym->key);
1422 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1426 for (i = 0; i < sym->nRegs; i++)
1427 sym->regs[i]->isFree = 0;
1430 /*-----------------------------------------------------------------*/
1431 /* willCauseSpill - determines if allocating will cause a spill */
1432 /*-----------------------------------------------------------------*/
1434 willCauseSpill (int nr, int rt)
1436 debugLog ("%s\n", __FUNCTION__);
1437 /* first check if there are any avlb registers
1438 of te type required */
1441 /* special case for pointer type
1442 if pointer type not avlb then
1443 check for type gpr */
1444 if (nFreeRegs (rt) >= nr)
1446 if (nFreeRegs (REG_GPR) >= nr)
1451 if (pic14_ptrRegReq)
1453 if (nFreeRegs (rt) >= nr)
1458 if (nFreeRegs (REG_PTR) +
1459 nFreeRegs (REG_GPR) >= nr)
1464 debugLog (" ... yep it will (cause a spill)\n");
1465 /* it will cause a spil */
1469 /*-----------------------------------------------------------------*/
1470 /* positionRegs - the allocator can allocate same registers to res- */
1471 /* ult and operand, if this happens make sure they are in the same */
1472 /* position as the operand otherwise chaos results */
1473 /*-----------------------------------------------------------------*/
1475 positionRegs (symbol * result, symbol * opsym, int lineno)
1477 int count = min (result->nRegs, opsym->nRegs);
1478 int i, j = 0, shared = 0;
1480 debugLog ("%s\n", __FUNCTION__);
1481 /* if the result has been spilt then cannot share */
1486 /* first make sure that they actually share */
1487 for (i = 0; i < count; i++)
1489 for (j = 0; j < count; j++)
1491 if (result->regs[i] == opsym->regs[j] && i != j)
1501 regs *tmp = result->regs[i];
1502 result->regs[i] = result->regs[j];
1503 result->regs[j] = tmp;
1508 /*-----------------------------------------------------------------*/
1509 /* serialRegAssign - serially allocate registers to the variables */
1510 /*-----------------------------------------------------------------*/
1512 serialRegAssign (eBBlock ** ebbs, int count)
1516 debugLog ("%s\n", __FUNCTION__);
1517 /* for all blocks */
1518 for (i = 0; i < count; i++)
1523 if (ebbs[i]->noPath &&
1524 (ebbs[i]->entryLabel != entryLabel &&
1525 ebbs[i]->entryLabel != returnLabel))
1528 /* of all instructions do */
1529 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1532 debugLog (" op: %s\n", decodeOp (ic->op));
1534 /* if this is an ipop that means some live
1535 range will have to be assigned again */
1537 reassignLR (IC_LEFT (ic));
1539 /* if result is present && is a true symbol */
1540 if (IC_RESULT (ic) && ic->op != IFX &&
1541 IS_TRUE_SYMOP (IC_RESULT (ic)))
1542 OP_SYMBOL (IC_RESULT (ic))->allocreq = 1;
1544 /* take away registers from live
1545 ranges that end at this instruction */
1546 deassignLRs (ic, ebbs[i]);
1548 /* some don't need registers */
1549 if (SKIP_IC2 (ic) ||
1550 ic->op == JUMPTABLE ||
1554 (IC_RESULT (ic) && POINTER_SET (ic)))
1557 /* now we need to allocate registers
1558 only for the result */
1561 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
1567 /* if it does not need or is spilt
1568 or is already assigned to registers
1569 or will not live beyond this instructions */
1572 bitVectBitValue (_G.regAssigned, sym->key) ||
1573 sym->liveTo <= ic->seq)
1576 /* if some liverange has been spilt at the block level
1577 and this one live beyond this block then spil this
1579 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
1584 /* if trying to allocate this will cause
1585 a spill and there is nothing to spill
1586 or this one is rematerializable then
1588 willCS = willCauseSpill (sym->nRegs, sym->regType);
1589 spillable = computeSpillable (ic);
1591 (willCS && bitVectIsZero (spillable)))
1599 /* if it has a spillocation & is used less than
1600 all other live ranges then spill this */
1602 if (sym->usl.spillLoc) {
1603 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
1604 allLRs, ebbs[i], ic));
1605 if (leastUsed && leastUsed->used > sym->used) {
1610 /* if none of the liveRanges have a spillLocation then better
1611 to spill this one than anything else already assigned to registers */
1612 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
1619 if (ic->op == RECEIVE)
1620 debugLog ("When I get clever, I'll optimize the receive logic\n");
1622 /* if we need ptr regs for the right side
1624 if (POINTER_GET (ic) && getSize (OP_SYMBOL (IC_LEFT (ic))->type)
1625 <= (unsigned) PTRSIZE)
1630 /* else we assign registers to it */
1631 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1633 debugLog (" %d - \n", __LINE__);
1635 bitVectDebugOn(_G.regAssigned, debugF);
1637 for (j = 0; j < sym->nRegs; j++)
1639 if (sym->regType == REG_PTR)
1640 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
1642 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
1644 /* if the allocation falied which means
1645 this was spilt then break */
1649 debugLog (" %d - \n", __LINE__);
1651 /* if it shares registers with operands make sure
1652 that they are in the same position */
1653 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
1654 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
1655 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1656 OP_SYMBOL (IC_LEFT (ic)), ic->lineno);
1657 /* do the same for the right operand */
1658 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
1659 OP_SYMBOL (IC_RIGHT (ic))->nRegs && ic->op != '=')
1660 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
1661 OP_SYMBOL (IC_RIGHT (ic)), ic->lineno);
1663 debugLog (" %d - \n", __LINE__);
1666 debugLog (" %d - \n", __LINE__);
1676 /*-----------------------------------------------------------------*/
1677 /* rUmaskForOp :- returns register mask for an operand */
1678 /*-----------------------------------------------------------------*/
1680 rUmaskForOp (operand * op)
1686 debugLog ("%s\n", __FUNCTION__);
1687 /* only temporaries are assigned registers */
1691 sym = OP_SYMBOL (op);
1693 /* if spilt or no registers assigned to it
1695 if (sym->isspilt || !sym->nRegs)
1698 rumask = newBitVect (pic14_nRegs);
1700 for (j = 0; j < sym->nRegs; j++)
1702 rumask = bitVectSetBit (rumask,
1703 sym->regs[j]->rIdx);
1709 /*-----------------------------------------------------------------*/
1710 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
1711 /*-----------------------------------------------------------------*/
1713 regsUsedIniCode (iCode * ic)
1715 bitVect *rmask = newBitVect (pic14_nRegs);
1717 debugLog ("%s\n", __FUNCTION__);
1718 /* do the special cases first */
1721 rmask = bitVectUnion (rmask,
1722 rUmaskForOp (IC_COND (ic)));
1726 /* for the jumptable */
1727 if (ic->op == JUMPTABLE)
1729 rmask = bitVectUnion (rmask,
1730 rUmaskForOp (IC_JTCOND (ic)));
1735 /* of all other cases */
1737 rmask = bitVectUnion (rmask,
1738 rUmaskForOp (IC_LEFT (ic)));
1742 rmask = bitVectUnion (rmask,
1743 rUmaskForOp (IC_RIGHT (ic)));
1746 rmask = bitVectUnion (rmask,
1747 rUmaskForOp (IC_RESULT (ic)));
1753 /*-----------------------------------------------------------------*/
1754 /* createRegMask - for each instruction will determine the regsUsed */
1755 /*-----------------------------------------------------------------*/
1757 createRegMask (eBBlock ** ebbs, int count)
1761 debugLog ("%s\n", __FUNCTION__);
1762 /* for all blocks */
1763 for (i = 0; i < count; i++)
1767 if (ebbs[i]->noPath &&
1768 (ebbs[i]->entryLabel != entryLabel &&
1769 ebbs[i]->entryLabel != returnLabel))
1772 /* for all instructions */
1773 for (ic = ebbs[i]->sch; ic; ic = ic->next)
1778 if (SKIP_IC2 (ic) || !ic->rlive)
1781 /* first mark the registers used in this
1783 ic->rUsed = regsUsedIniCode (ic);
1784 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
1786 /* now create the register mask for those
1787 registers that are in use : this is a
1788 super set of ic->rUsed */
1789 ic->rMask = newBitVect (pic14_nRegs + 1);
1791 /* for all live Ranges alive at this point */
1792 for (j = 1; j < ic->rlive->size; j++)
1797 /* if not alive then continue */
1798 if (!bitVectBitValue (ic->rlive, j))
1801 /* find the live range we are interested in */
1802 if (!(sym = hTabItemWithKey (liveRanges, j)))
1804 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1805 "createRegMask cannot find live range");
1809 /* if no register assigned to it */
1810 if (!sym->nRegs || sym->isspilt)
1813 /* for all the registers allocated to it */
1814 for (k = 0; k < sym->nRegs; k++)
1817 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
1823 /*-----------------------------------------------------------------*/
1824 /* rematStr - returns the rematerialized string for a remat var */
1825 /*-----------------------------------------------------------------*/
1827 rematStr (symbol * sym)
1830 iCode *ic = sym->rematiCode;
1832 debugLog ("%s\n", __FUNCTION__);
1837 /* if plus or minus print the right hand side */
1839 if (ic->op == '+' || ic->op == '-') {
1840 sprintf(s,"0x%04x %c ",(int) operandLitValue(IC_RIGHT(ic)),
1843 ic = OP_SYMBOL(IC_LEFT(ic))->rematiCode;
1847 if (ic->op == '+' || ic->op == '-')
1849 iCode *ric = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
1850 sprintf (s, "(%s %c 0x%04x)",
1851 OP_SYMBOL (IC_LEFT (ric))->rname,
1853 (int) operandLitValue (IC_RIGHT (ic)));
1856 //ic = OP_SYMBOL(IC_LEFT(ic))->rematiCode;
1861 /* we reached the end */
1862 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
1866 printf ("%s\n", buffer);
1870 /*-----------------------------------------------------------------*/
1871 /* regTypeNum - computes the type & number of registers required */
1872 /*-----------------------------------------------------------------*/
1880 debugLog ("%s\n", __FUNCTION__);
1881 /* for each live range do */
1882 for (sym = hTabFirstItem (liveRanges, &k); sym;
1883 sym = hTabNextItem (liveRanges, &k)) {
1885 debugLog (" %d - %s\n", __LINE__, sym->rname);
1887 /* if used zero times then no registers needed */
1888 if ((sym->liveTo - sym->liveFrom) == 0)
1892 /* if the live range is a temporary */
1895 debugLog (" %d - itemp register\n", __LINE__);
1897 /* if the type is marked as a conditional */
1898 if (sym->regType == REG_CND)
1901 /* if used in return only then we don't
1903 if (sym->ruonly || sym->accuse) {
1904 if (IS_AGGREGATE (sym->type) || sym->isptr)
1905 sym->type = aggrToPtr (sym->type, FALSE);
1906 debugLog (" %d - no reg needed - used as a return\n", __LINE__);
1911 /* if the symbol has only one definition &
1912 that definition is a get_pointer and the
1913 pointer we are getting is rematerializable and
1916 if (bitVectnBitsOn (sym->defs) == 1 &&
1917 (ic = hTabItemWithKey (iCodehTab,
1918 bitVectFirstBit (sym->defs))) &&
1921 !IS_BITVAR (sym->etype)) {
1924 debugLog (" %d - \n", __LINE__);
1926 /* if remat in data space */
1927 if (OP_SYMBOL (IC_LEFT (ic))->remat &&
1928 DCL_TYPE (aggrToPtr (sym->type, FALSE)) == POINTER) {
1930 /* create a psuedo symbol & force a spil */
1931 symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
1932 psym->type = sym->type;
1933 psym->etype = sym->etype;
1934 strcpy (psym->rname, psym->name);
1936 sym->usl.spillLoc = psym;
1940 /* if in data space or idata space then try to
1941 allocate pointer register */
1945 /* if not then we require registers */
1946 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
1947 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
1948 getSize (sym->type));
1950 if (sym->nRegs > 4) {
1951 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
1952 printTypeChain (sym->type, stderr);
1953 fprintf (stderr, "\n");
1956 /* determine the type of register required */
1957 if (sym->nRegs == 1 &&
1958 IS_PTR (sym->type) &&
1960 sym->regType = REG_PTR;
1962 sym->regType = REG_GPR;
1965 debugLog (" reg name %s, reg type %s\n", sym->rname, debugLogRegType (sym->regType));
1969 /* for the first run we don't provide */
1970 /* registers for true symbols we will */
1971 /* see how things go */
1977 /*-----------------------------------------------------------------*/
1978 /* freeAllRegs - mark all registers as free */
1979 /*-----------------------------------------------------------------*/
1981 pic14_freeAllRegs ()
1985 debugLog ("%s\n", __FUNCTION__);
1986 for (i = 0; i < pic14_nRegs; i++)
1987 regspic14[i].isFree = 1;
1990 /*-----------------------------------------------------------------*/
1991 /*-----------------------------------------------------------------*/
1993 pic14_deallocateAllRegs ()
1997 debugLog ("%s\n", __FUNCTION__);
1998 for (i = 0; i < pic14_nRegs; i++) {
1999 regspic14[i].isFree = 1;
2000 regspic14[i].wasUsed = 0;
2005 /*-----------------------------------------------------------------*/
2006 /* deallocStackSpil - this will set the stack pointer back */
2007 /*-----------------------------------------------------------------*/
2009 DEFSETFUNC (deallocStackSpil)
2013 debugLog ("%s\n", __FUNCTION__);
2018 /*-----------------------------------------------------------------*/
2019 /* farSpacePackable - returns the packable icode for far variables */
2020 /*-----------------------------------------------------------------*/
2022 farSpacePackable (iCode * ic)
2026 debugLog ("%s\n", __FUNCTION__);
2027 /* go thru till we find a definition for the
2028 symbol on the right */
2029 for (dic = ic->prev; dic; dic = dic->prev)
2032 /* if the definition is a call then no */
2033 if ((dic->op == CALL || dic->op == PCALL) &&
2034 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2039 /* if shift by unknown amount then not */
2040 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
2041 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2044 /* if pointer get and size > 1 */
2045 if (POINTER_GET (dic) &&
2046 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
2049 if (POINTER_SET (dic) &&
2050 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
2053 /* if any three is a true symbol in far space */
2054 if (IC_RESULT (dic) &&
2055 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
2056 isOperandInFarSpace (IC_RESULT (dic)))
2059 if (IC_RIGHT (dic) &&
2060 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
2061 isOperandInFarSpace (IC_RIGHT (dic)) &&
2062 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
2065 if (IC_LEFT (dic) &&
2066 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
2067 isOperandInFarSpace (IC_LEFT (dic)) &&
2068 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
2071 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
2073 if ((dic->op == LEFT_OP ||
2074 dic->op == RIGHT_OP ||
2076 IS_OP_LITERAL (IC_RIGHT (dic)))
2086 /*-----------------------------------------------------------------*/
2087 /* packRegsForAssign - register reduction for assignment */
2088 /*-----------------------------------------------------------------*/
2090 packRegsForAssign (iCode * ic, eBBlock * ebp)
2095 debugLog ("%s\n", __FUNCTION__);
2097 debugAopGet (" result:", IC_RESULT (ic));
2098 debugAopGet (" left:", IC_LEFT (ic));
2099 debugAopGet (" right:", IC_RIGHT (ic));
2101 if (!IS_ITEMP (IC_RIGHT (ic)) ||
2102 OP_SYMBOL (IC_RIGHT (ic))->isind ||
2103 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
2105 debugLog (" %d - not packing - right side fails \n", __LINE__);
2109 /* if the true symbol is defined in far space or on stack
2110 then we should not since this will increase register pressure */
2111 if (isOperandInFarSpace (IC_RESULT (ic)))
2113 if ((dic = farSpacePackable (ic)))
2119 /* find the definition of iTempNN scanning backwards if we find a
2120 a use of the true symbol before we find the definition then
2122 for (dic = ic->prev; dic; dic = dic->prev)
2125 /* if there is a function call and this is
2126 a parameter & not my parameter then don't pack it */
2127 if ((dic->op == CALL || dic->op == PCALL) &&
2128 (OP_SYMBOL (IC_RESULT (ic))->_isparm &&
2129 !OP_SYMBOL (IC_RESULT (ic))->ismyparm))
2131 debugLog (" %d - \n", __LINE__);
2139 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
2140 IS_OP_VOLATILE (IC_RESULT (dic)))
2142 debugLog (" %d - \n", __LINE__);
2147 if (IS_SYMOP (IC_RESULT (dic)) &&
2148 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2150 debugLog (" %d - dic key == ic key -- pointer set=%c\n", __LINE__, ((POINTER_SET (dic)) ? 'Y' : 'N'));
2151 if (POINTER_SET (dic))
2157 if (IS_SYMOP (IC_RIGHT (dic)) &&
2158 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
2159 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
2161 debugLog (" %d - \n", __LINE__);
2166 if (IS_SYMOP (IC_LEFT (dic)) &&
2167 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
2168 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
2170 debugLog (" %d - \n", __LINE__);
2175 if (POINTER_SET (dic) &&
2176 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
2178 debugLog (" %d - \n", __LINE__);
2185 return 0; /* did not find */
2187 /* if the result is on stack or iaccess then it must be
2188 the same atleast one of the operands */
2189 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
2190 OP_SYMBOL (IC_RESULT (ic))->iaccess)
2193 /* the operation has only one symbol
2194 operator then we can pack */
2195 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
2196 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
2199 if (!((IC_LEFT (dic) &&
2200 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
2202 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
2206 debugLog (" packing. removing %s\n", OP_SYMBOL (IC_RIGHT (ic))->rname);
2207 /* found the definition */
2208 /* replace the result with the result of */
2209 /* this assignment and remove this assignment */
2210 IC_RESULT (dic) = IC_RESULT (ic);
2212 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
2214 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
2216 /* delete from liverange table also
2217 delete from all the points inbetween and the new
2219 for (sic = dic; sic != ic; sic = sic->next)
2221 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
2222 if (IS_ITEMP (IC_RESULT (dic)))
2223 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
2226 remiCodeFromeBBlock (ebp, ic);
2227 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2228 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2234 /*-----------------------------------------------------------------*/
2235 /* findAssignToSym : scanning backwards looks for first assig found */
2236 /*-----------------------------------------------------------------*/
2238 findAssignToSym (operand * op, iCode * ic)
2242 debugLog ("%s\n", __FUNCTION__);
2243 for (dic = ic->prev; dic; dic = dic->prev)
2246 /* if definition by assignment */
2247 if (dic->op == '=' &&
2248 !POINTER_SET (dic) &&
2249 IC_RESULT (dic)->key == op->key
2250 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
2254 /* we are interested only if defined in far space */
2255 /* or in stack space in case of + & - */
2257 /* if assigned to a non-symbol then return
2259 if (!IS_SYMOP (IC_RIGHT (dic)))
2262 /* if the symbol is in far space then
2264 if (isOperandInFarSpace (IC_RIGHT (dic)))
2267 /* for + & - operations make sure that
2268 if it is on the stack it is the same
2269 as one of the three operands */
2270 if ((ic->op == '+' || ic->op == '-') &&
2271 OP_SYMBOL (IC_RIGHT (dic))->onStack)
2274 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
2275 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
2276 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
2284 /* if we find an usage then we cannot delete it */
2285 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
2288 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
2291 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
2295 /* now make sure that the right side of dic
2296 is not defined between ic & dic */
2299 iCode *sic = dic->next;
2301 for (; sic != ic; sic = sic->next)
2302 if (IC_RESULT (sic) &&
2303 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2312 /*-----------------------------------------------------------------*/
2313 /* packRegsForSupport :- reduce some registers for support calls */
2314 /*-----------------------------------------------------------------*/
2316 packRegsForSupport (iCode * ic, eBBlock * ebp)
2320 debugLog ("%s\n", __FUNCTION__);
2321 /* for the left & right operand :- look to see if the
2322 left was assigned a true symbol in far space in that
2323 case replace them */
2324 if (IS_ITEMP (IC_LEFT (ic)) &&
2325 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2327 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
2333 debugAopGet ("removing left:", IC_LEFT (ic));
2335 /* found it we need to remove it from the
2337 for (sic = dic; sic != ic; sic = sic->next)
2338 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
2340 IC_LEFT (ic)->operand.symOperand =
2341 IC_RIGHT (dic)->operand.symOperand;
2342 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2343 remiCodeFromeBBlock (ebp, dic);
2344 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2348 /* do the same for the right operand */
2351 IS_ITEMP (IC_RIGHT (ic)) &&
2352 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2354 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2360 /* if this is a subtraction & the result
2361 is a true symbol in far space then don't pack */
2362 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2364 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2365 if (IN_FARSPACE (SPEC_OCLS (etype)))
2369 debugAopGet ("removing right:", IC_RIGHT (ic));
2371 /* found it we need to remove it from the
2373 for (sic = dic; sic != ic; sic = sic->next)
2374 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
2376 IC_RIGHT (ic)->operand.symOperand =
2377 IC_RIGHT (dic)->operand.symOperand;
2378 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2380 remiCodeFromeBBlock (ebp, dic);
2381 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2388 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
2391 /*-----------------------------------------------------------------*/
2392 /* packRegsForOneuse : - will reduce some registers for single Use */
2393 /*-----------------------------------------------------------------*/
2395 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
2400 debugLog ("%s\n", __FUNCTION__);
2401 /* if returning a literal then do nothing */
2405 /* only upto 2 bytes since we cannot predict
2406 the usage of b, & acc */
2407 if (getSize (operandType (op)) > (fReturnSizePic - 2) &&
2412 /* this routine will mark the a symbol as used in one
2413 instruction use only && if the definition is local
2414 (ie. within the basic block) && has only one definition &&
2415 that definition is either a return value from a
2416 function or does not contain any variables in
2418 uses = bitVectCopy (OP_USES (op));
2419 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
2420 if (!bitVectIsZero (uses)) /* has other uses */
2423 /* if it has only one defintion */
2424 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
2425 return NULL; /* has more than one definition */
2427 /* get that definition */
2429 hTabItemWithKey (iCodehTab,
2430 bitVectFirstBit (OP_DEFS (op)))))
2433 /* found the definition now check if it is local */
2434 if (dic->seq < ebp->fSeq ||
2435 dic->seq > ebp->lSeq)
2436 return NULL; /* non-local */
2438 /* now check if it is the return from
2440 if (dic->op == CALL || dic->op == PCALL)
2442 if (ic->op != SEND && ic->op != RETURN)
2444 OP_SYMBOL (op)->ruonly = 1;
2451 /* otherwise check that the definition does
2452 not contain any symbols in far space */
2453 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2454 isOperandInFarSpace (IC_RIGHT (dic)) ||
2455 IS_OP_RUONLY (IC_LEFT (ic)) ||
2456 IS_OP_RUONLY (IC_RIGHT (ic)))
2461 /* if pointer set then make sure the pointer
2463 if (POINTER_SET (dic) &&
2464 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2467 if (POINTER_GET (dic) &&
2468 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2473 /* also make sure the intervenening instructions
2474 don't have any thing in far space */
2475 for (dic = dic->next; dic && dic != ic; dic = dic->next)
2478 /* if there is an intervening function call then no */
2479 if (dic->op == CALL || dic->op == PCALL)
2481 /* if pointer set then make sure the pointer
2483 if (POINTER_SET (dic) &&
2484 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
2487 if (POINTER_GET (dic) &&
2488 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
2491 /* if address of & the result is remat then okay */
2492 if (dic->op == ADDRESS_OF &&
2493 OP_SYMBOL (IC_RESULT (dic))->remat)
2496 /* if operand has size of three or more & this
2497 operation is a '*','/' or '%' then 'b' may
2499 if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
2500 getSize (operandType (op)) >= 3)
2503 /* if left or right or result is in far space */
2504 if (isOperandInFarSpace (IC_LEFT (dic)) ||
2505 isOperandInFarSpace (IC_RIGHT (dic)) ||
2506 isOperandInFarSpace (IC_RESULT (dic)) ||
2507 IS_OP_RUONLY (IC_LEFT (dic)) ||
2508 IS_OP_RUONLY (IC_RIGHT (dic)) ||
2509 IS_OP_RUONLY (IC_RESULT (dic)))
2515 OP_SYMBOL (op)->ruonly = 1;
2520 /*-----------------------------------------------------------------*/
2521 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
2522 /*-----------------------------------------------------------------*/
2524 isBitwiseOptimizable (iCode * ic)
2526 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
2527 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
2529 debugLog ("%s\n", __FUNCTION__);
2530 /* bitwise operations are considered optimizable
2531 under the following conditions (Jean-Louis VERN)
2543 if (IS_LITERAL (rtype) ||
2544 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
2550 /*-----------------------------------------------------------------*/
2551 /* packRegsForAccUse - pack registers for acc use */
2552 /*-----------------------------------------------------------------*/
2554 packRegsForAccUse (iCode * ic)
2558 debugLog ("%s\n", __FUNCTION__);
2559 /* if + or - then it has to be one byte result */
2560 if ((ic->op == '+' || ic->op == '-')
2561 && getSize (operandType (IC_RESULT (ic))) > 1)
2564 /* if shift operation make sure right side is not a literal */
2565 if (ic->op == RIGHT_OP &&
2566 (isOperandLiteral (IC_RIGHT (ic)) ||
2567 getSize (operandType (IC_RESULT (ic))) > 1))
2570 if (ic->op == LEFT_OP &&
2571 (isOperandLiteral (IC_RIGHT (ic)) ||
2572 getSize (operandType (IC_RESULT (ic))) > 1))
2575 if (IS_BITWISE_OP (ic) &&
2576 getSize (operandType (IC_RESULT (ic))) > 1)
2580 /* has only one definition */
2581 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
2584 /* has only one use */
2585 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
2588 /* and the usage immediately follows this iCode */
2589 if (!(uic = hTabItemWithKey (iCodehTab,
2590 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
2593 if (ic->next != uic)
2596 /* if it is a conditional branch then we definitely can */
2600 if (uic->op == JUMPTABLE)
2603 /* if the usage is not is an assignment
2604 or an arithmetic / bitwise / shift operation then not */
2605 if (POINTER_SET (uic) &&
2606 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
2609 if (uic->op != '=' &&
2610 !IS_ARITHMETIC_OP (uic) &&
2611 !IS_BITWISE_OP (uic) &&
2612 uic->op != LEFT_OP &&
2613 uic->op != RIGHT_OP)
2616 /* if used in ^ operation then make sure right is not a
2618 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
2621 /* if shift operation make sure right side is not a literal */
2622 if (uic->op == RIGHT_OP &&
2623 (isOperandLiteral (IC_RIGHT (uic)) ||
2624 getSize (operandType (IC_RESULT (uic))) > 1))
2627 if (uic->op == LEFT_OP &&
2628 (isOperandLiteral (IC_RIGHT (uic)) ||
2629 getSize (operandType (IC_RESULT (uic))) > 1))
2632 /* make sure that the result of this icode is not on the
2633 stack, since acc is used to compute stack offset */
2634 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
2635 OP_SYMBOL (IC_RESULT (uic))->onStack)
2638 /* if either one of them in far space then we cannot */
2639 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2640 isOperandInFarSpace (IC_LEFT (uic))) ||
2641 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2642 isOperandInFarSpace (IC_RIGHT (uic))))
2645 /* if the usage has only one operand then we can */
2646 if (IC_LEFT (uic) == NULL ||
2647 IC_RIGHT (uic) == NULL)
2650 /* make sure this is on the left side if not
2651 a '+' since '+' is commutative */
2652 if (ic->op != '+' &&
2653 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
2656 /* if one of them is a literal then we can */
2657 if ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
2658 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic))))
2660 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2664 /* if the other one is not on stack then we can */
2665 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
2666 (IS_ITEMP (IC_RIGHT (uic)) ||
2667 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
2668 !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
2671 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
2672 (IS_ITEMP (IC_LEFT (uic)) ||
2673 (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
2674 !OP_SYMBOL (IC_LEFT (uic))->onStack)))
2680 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
2685 /*-----------------------------------------------------------------*/
2686 /* packForPush - hueristics to reduce iCode for pushing */
2687 /*-----------------------------------------------------------------*/
2689 packForReceive (iCode * ic, eBBlock * ebp)
2693 debugLog ("%s\n", __FUNCTION__);
2694 debugAopGet (" result:", IC_RESULT (ic));
2695 debugAopGet (" left:", IC_LEFT (ic));
2696 debugAopGet (" right:", IC_RIGHT (ic));
2701 for (dic = ic->next; dic; dic = dic->next)
2706 if (IC_LEFT (dic) && (IC_RESULT (ic)->key == IC_LEFT (dic)->key))
2707 debugLog (" used on left\n");
2708 if (IC_RIGHT (dic) && IC_RESULT (ic)->key == IC_RIGHT (dic)->key)
2709 debugLog (" used on right\n");
2710 if (IC_RESULT (dic) && IC_RESULT (ic)->key == IC_RESULT (dic)->key)
2711 debugLog (" used on result\n");
2713 if ((IC_LEFT (dic) && (IC_RESULT (ic)->key == IC_LEFT (dic)->key)) ||
2714 (IC_RESULT (dic) && IC_RESULT (ic)->key == IC_RESULT (dic)->key))
2719 debugLog (" hey we can remove this unnecessary assign\n");
2721 /*-----------------------------------------------------------------*/
2722 /* packForPush - hueristics to reduce iCode for pushing */
2723 /*-----------------------------------------------------------------*/
2725 packForPush (iCode * ic, eBBlock * ebp)
2729 debugLog ("%s\n", __FUNCTION__);
2730 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
2733 /* must have only definition & one usage */
2734 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
2735 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
2738 /* find the definition */
2739 if (!(dic = hTabItemWithKey (iCodehTab,
2740 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
2743 if (dic->op != '=' || POINTER_SET (dic))
2746 /* we now we know that it has one & only one def & use
2747 and the that the definition is an assignment */
2748 IC_LEFT (ic) = IC_RIGHT (dic);
2750 remiCodeFromeBBlock (ebp, dic);
2751 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2754 /*-----------------------------------------------------------------*/
2755 /* packRegisters - does some transformations to reduce register */
2757 /*-----------------------------------------------------------------*/
2759 packRegisters (eBBlock * ebp)
2764 debugLog ("%s\n", __FUNCTION__);
2771 /* look for assignments of the form */
2772 /* iTempNN = TRueSym (someoperation) SomeOperand */
2774 /* TrueSym := iTempNN:1 */
2775 for (ic = ebp->sch; ic; ic = ic->next)
2778 /* find assignment of the form TrueSym := iTempNN:1 */
2779 if (ic->op == '=' && !POINTER_SET (ic))
2780 change += packRegsForAssign (ic, ebp);
2784 if (POINTER_SET (ic))
2785 debugLog ("pointer is set\n");
2786 debugAopGet (" result:", IC_RESULT (ic));
2787 debugAopGet (" left:", IC_LEFT (ic));
2788 debugAopGet (" right:", IC_RIGHT (ic));
2797 for (ic = ebp->sch; ic; ic = ic->next)
2800 /* if this is an itemp & result of a address of a true sym
2801 then mark this as rematerialisable */
2802 if (ic->op == ADDRESS_OF &&
2803 IS_ITEMP (IC_RESULT (ic)) &&
2804 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
2805 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2806 !OP_SYMBOL (IC_LEFT (ic))->onStack)
2809 debugLog (" %d - %s. result is rematerializable\n", __LINE__,__FUNCTION__);
2811 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2812 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2813 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2817 /* if straight assignment then carry remat flag if
2818 this is the only definition */
2819 if (ic->op == '=' &&
2820 !POINTER_SET (ic) &&
2821 IS_SYMOP (IC_RIGHT (ic)) &&
2822 OP_SYMBOL (IC_RIGHT (ic))->remat &&
2823 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
2825 debugLog (" %d - %s. straight rematerializable\n", __LINE__,__FUNCTION__);
2827 OP_SYMBOL (IC_RESULT (ic))->remat =
2828 OP_SYMBOL (IC_RIGHT (ic))->remat;
2829 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
2830 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
2833 /* if this is a +/- operation with a rematerizable
2834 then mark this as rematerializable as well */
2835 if ((ic->op == '+' || ic->op == '-') &&
2836 (IS_SYMOP (IC_LEFT (ic)) &&
2837 IS_ITEMP (IC_RESULT (ic)) &&
2838 OP_SYMBOL (IC_LEFT (ic))->remat &&
2839 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
2840 IS_OP_LITERAL (IC_RIGHT (ic))))
2842 debugLog (" %d - %s. rematerializable because op is +/-\n", __LINE__,__FUNCTION__);
2844 operandLitValue (IC_RIGHT (ic));
2845 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
2846 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
2847 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
2850 /* mark the pointer usages */
2851 if (POINTER_SET (ic))
2853 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
2854 debugLog (" marking as a pointer (set) =>");
2855 debugAopGet (" result:", IC_RESULT (ic));
2857 if (POINTER_GET (ic))
2859 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
2860 debugLog (" marking as a pointer (get) =>");
2861 debugAopGet (" left:", IC_LEFT (ic));
2866 /* if we are using a symbol on the stack
2867 then we should say pic14_ptrRegReq */
2868 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
2869 pic14_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ||
2870 OP_SYMBOL (IC_COND (ic))->iaccess) ? 1 : 0);
2871 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
2872 pic14_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ||
2873 OP_SYMBOL (IC_JTCOND (ic))->iaccess) ? 1 : 0);
2876 if (IS_SYMOP (IC_LEFT (ic)))
2877 pic14_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ||
2878 OP_SYMBOL (IC_LEFT (ic))->iaccess) ? 1 : 0);
2879 if (IS_SYMOP (IC_RIGHT (ic)))
2880 pic14_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ||
2881 OP_SYMBOL (IC_RIGHT (ic))->iaccess) ? 1 : 0);
2882 if (IS_SYMOP (IC_RESULT (ic)))
2883 pic14_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ||
2884 OP_SYMBOL (IC_RESULT (ic))->iaccess) ? 1 : 0);
2888 /* if the condition of an if instruction
2889 is defined in the previous instruction then
2890 mark the itemp as a conditional */
2891 if ((IS_CONDITIONAL (ic) ||
2892 ((ic->op == BITWISEAND ||
2895 isBitwiseOptimizable (ic))) &&
2896 ic->next && ic->next->op == IFX &&
2897 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
2898 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
2901 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
2905 /* reduce for support function calls */
2906 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
2907 packRegsForSupport (ic, ebp);
2909 /* if a parameter is passed, it's in W, so we may not
2910 need to place a copy in a register */
2911 if (ic->op == RECEIVE)
2912 packForReceive (ic, ebp);
2914 /* some cases the redundant moves can
2915 can be eliminated for return statements */
2916 if ((ic->op == RETURN || ic->op == SEND) &&
2917 !isOperandInFarSpace (IC_LEFT (ic)) &&
2919 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2921 /* if pointer set & left has a size more than
2922 one and right is not in far space */
2923 if (POINTER_SET (ic) &&
2924 !isOperandInFarSpace (IC_RIGHT (ic)) &&
2925 !OP_SYMBOL (IC_RESULT (ic))->remat &&
2926 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
2927 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
2929 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
2931 /* if pointer get */
2932 if (POINTER_GET (ic) &&
2933 !isOperandInFarSpace (IC_RESULT (ic)) &&
2934 !OP_SYMBOL (IC_LEFT (ic))->remat &&
2935 !IS_OP_RUONLY (IC_RESULT (ic)) &&
2936 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
2938 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
2941 /* if this is cast for intergral promotion then
2942 check if only use of the definition of the
2943 operand being casted/ if yes then replace
2944 the result of that arithmetic operation with
2945 this result and get rid of the cast */
2948 sym_link *fromType = operandType (IC_RIGHT (ic));
2949 sym_link *toType = operandType (IC_LEFT (ic));
2951 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
2952 getSize (fromType) != getSize (toType))
2955 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2958 if (IS_ARITHMETIC_OP (dic))
2960 IC_RESULT (dic) = IC_RESULT (ic);
2961 remiCodeFromeBBlock (ebp, ic);
2962 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2963 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2967 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
2973 /* if the type from and type to are the same
2974 then if this is the only use then packit */
2975 if (compareType (operandType (IC_RIGHT (ic)),
2976 operandType (IC_LEFT (ic))) == 1)
2978 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
2981 IC_RESULT (dic) = IC_RESULT (ic);
2982 remiCodeFromeBBlock (ebp, ic);
2983 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2984 OP_DEFS (IC_RESULT (dic)) = bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2992 iTempNN := (some variable in farspace) V1
2997 if (ic->op == IPUSH)
2999 packForPush (ic, ebp);
3003 /* pack registers for accumulator use, when the
3004 result of an arithmetic or bit wise operation
3005 has only one use, that use is immediately following
3006 the defintion and the using iCode has only one
3007 operand or has two operands but one is literal &
3008 the result of that operation is not on stack then
3009 we can leave the result of this operation in acc:b
3011 if ((IS_ARITHMETIC_OP (ic)
3013 || IS_BITWISE_OP (ic)
3015 || ic->op == LEFT_OP || ic->op == RIGHT_OP
3018 IS_ITEMP (IC_RESULT (ic)) &&
3019 getSize (operandType (IC_RESULT (ic))) <= 2)
3021 packRegsForAccUse (ic);
3027 dumpEbbsToDebug (eBBlock ** ebbs, int count)
3031 if (!debug || !debugF)
3034 for (i = 0; i < count; i++)
3036 fprintf (debugF, "\n----------------------------------------------------------------\n");
3037 fprintf (debugF, "Basic Block %s : loop Depth = %d noPath = %d , lastinLoop = %d\n",
3038 ebbs[i]->entryLabel->name,
3041 ebbs[i]->isLastInLoop);
3042 fprintf (debugF, "depth 1st num %d : bbnum = %d 1st iCode = %d , last iCode = %d\n",
3047 fprintf (debugF, "visited %d : hasFcall = %d\n",
3051 fprintf (debugF, "\ndefines bitVector :");
3052 bitVectDebugOn (ebbs[i]->defSet, debugF);
3053 fprintf (debugF, "\nlocal defines bitVector :");
3054 bitVectDebugOn (ebbs[i]->ldefs, debugF);
3055 fprintf (debugF, "\npointers Set bitvector :");
3056 bitVectDebugOn (ebbs[i]->ptrsSet, debugF);
3057 fprintf (debugF, "\nin pointers Set bitvector :");
3058 bitVectDebugOn (ebbs[i]->inPtrsSet, debugF);
3059 fprintf (debugF, "\ninDefs Set bitvector :");
3060 bitVectDebugOn (ebbs[i]->inDefs, debugF);
3061 fprintf (debugF, "\noutDefs Set bitvector :");
3062 bitVectDebugOn (ebbs[i]->outDefs, debugF);
3063 fprintf (debugF, "\nusesDefs Set bitvector :");
3064 bitVectDebugOn (ebbs[i]->usesDefs, debugF);
3065 fprintf (debugF, "\n----------------------------------------------------------------\n");
3066 printiCChain (ebbs[i]->sch, debugF);
3069 /*-----------------------------------------------------------------*/
3070 /* assignRegisters - assigns registers to each live range as need */
3071 /*-----------------------------------------------------------------*/
3073 pic14_assignRegisters (eBBlock ** ebbs, int count)
3078 debugLog ("<><><><><><><><><><><><><><><><><>\nstarting\t%s:%s", __FILE__, __FUNCTION__);
3079 debugLog ("ebbs before optimizing:\n");
3080 dumpEbbsToDebug (ebbs, count);
3082 setToNull ((void *) &_G.funcrUsed);
3083 pic14_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
3086 /* change assignments this will remove some
3087 live ranges reducing some register pressure */
3088 for (i = 0; i < count; i++)
3089 packRegisters (ebbs[i]);
3091 if (options.dump_pack)
3092 dumpEbbsToFileExt (DUMP_PACK, ebbs, count);
3094 /* first determine for each live range the number of
3095 registers & the type of registers required for each */
3098 /* and serially allocate registers */
3099 serialRegAssign (ebbs, count);
3101 /* if stack was extended then tell the user */
3104 /* werror(W_TOOMANY_SPILS,"stack", */
3105 /* _G.stackExtend,currFunc->name,""); */
3111 /* werror(W_TOOMANY_SPILS,"data space", */
3112 /* _G.dataExtend,currFunc->name,""); */
3116 /* after that create the register mask
3117 for each of the instruction */
3118 createRegMask (ebbs, count);
3120 /* redo that offsets for stacked automatic variables */
3121 redoStackOffsets ();
3123 if (options.dump_rassgn)
3124 dumpEbbsToFileExt (DUMP_RASSGN, ebbs, count);
3126 /* now get back the chain */
3127 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
3129 debugLog ("ebbs after optimizing:\n");
3130 dumpEbbsToDebug (ebbs, count);
3135 /* free up any _G.stackSpil locations allocated */
3136 applyToSet (_G.stackSpil, deallocStackSpil);
3138 setToNull ((void **) &_G.stackSpil);
3139 setToNull ((void **) &_G.spiltSet);
3140 /* mark all registers as free */
3141 pic14_freeAllRegs ();
3143 debugLog ("leaving\n<><><><><><><><><><><><><><><><><>\n");