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 -------------------------------------------------------------------------*/
33 #if defined(__BORLANDC__) || defined(_MSC_VER)
34 #define STRCASECMP stricmp
35 #define FENTRY2 1 ? (void)0 : printf
37 #define STRCASECMP strcasecmp
38 //#define FENTRY2(fmt,...) do { fprintf (stderr, "%s:%d: called.\n", __FUNCTION__, __LINE__); fprintf (stderr, fmt, ## __VA_ARGS__); } while (0)
39 #define FENTRY2 1 ? (void)0 : printf
42 /* this should go in SDCCicode.h, but it doesn't. */
43 #define IS_REF(op) (IS_SYMOP(op) && op->operand.symOperand->isref == 1)
45 /*-----------------------------------------------------------------*/
46 /* At this point we start getting processor specific although */
47 /* some routines are non-processor specific & can be reused when */
48 /* targetting other processors. The decision for this will have */
49 /* to be made on a routine by routine basis */
50 /* routines used to pack registers are most definitely not reusable */
51 /* since the pack the registers depending strictly on the MCU */
52 /*-----------------------------------------------------------------*/
54 extern void genpic14Code (iCode *);
55 extern void assignConfigWordValue(int address, int value);
65 bitVect *funcrUsed; /* registers used in a function */
71 /* Shared with gen.c */
72 int pic14_ptrRegReq; /* one byte pointer register required */
75 set *dynAllocRegs=NULL;
76 set *dynStackRegs=NULL;
77 set *dynProcessorRegs=NULL;
78 set *dynDirectRegs=NULL;
79 set *dynDirectBitRegs=NULL;
80 set *dynInternalRegs=NULL;
82 static hTab *dynDirectRegNames= NULL;
83 // static hTab *regHash = NULL; /* a hash table containing ALL registers */
85 static int dynrIdx=0x20;
86 static int rDirectIdx=0;
88 int pic14_nRegs = 128; // = sizeof (regspic14) / sizeof (regs);
90 int Gstack_base_addr=0; /* The starting address of registers that
91 * are used to pass and return parameters */
97 static void spillThis (symbol *);
99 static FILE *debugF = NULL;
100 /*-----------------------------------------------------------------*/
101 /* debugLog - open a file for debugging information */
102 /*-----------------------------------------------------------------*/
103 //static void debugLog(char *inst,char *fmt, ...)
105 debugLog (char *fmt,...)
107 static int append = 0; // First time through, open the file without append.
110 //char *bufferP=buffer;
113 if (!debug || !dstFileName)
119 /* create the file name */
120 strcpy (buffer, dstFileName);
121 strcat (buffer, ".d");
123 if (!(debugF = fopen (buffer, (append ? "a+" : "w"))))
125 werror (E_FILE_OPEN_ERR, buffer);
128 append = 1; // Next time debubLog is called, we'll append the debug info
134 vsprintf (buffer, fmt, ap);
136 fprintf (debugF, "%s", buffer);
138 while (isspace((unsigned char)*bufferP)) bufferP++;
140 if (bufferP && *bufferP)
141 lineCurr = (lineCurr ?
142 connectLine(lineCurr,newLineNode(lb)) :
143 (lineHead = newLineNode(lb)));
144 lineCurr->isInline = _G.inLine;
145 lineCurr->isDebug = _G.debugLine;
155 fputc ('\n', debugF);
157 /*-----------------------------------------------------------------*/
158 /* debugLogClose - closes the debug log file (if opened) */
159 /*-----------------------------------------------------------------*/
169 #define AOP(op) op->aop
172 debugAopGet (char *str, operand * op)
177 printOperand (op, debugF);
185 decodeOp (unsigned int op)
188 if (op < 128 && op > ' ')
190 buffer[0] = (op & 0xff);
197 case IDENTIFIER: return "IDENTIFIER";
198 case TYPE_NAME: return "TYPE_NAME";
199 case CONSTANT: return "CONSTANT";
200 case STRING_LITERAL: return "STRING_LITERAL";
201 case SIZEOF: return "SIZEOF";
202 case PTR_OP: return "PTR_OP";
203 case INC_OP: return "INC_OP";
204 case DEC_OP: return "DEC_OP";
205 case LEFT_OP: return "LEFT_OP";
206 case RIGHT_OP: return "RIGHT_OP";
207 case LE_OP: return "LE_OP";
208 case GE_OP: return "GE_OP";
209 case EQ_OP: return "EQ_OP";
210 case NE_OP: return "NE_OP";
211 case AND_OP: return "AND_OP";
212 case OR_OP: return "OR_OP";
213 case MUL_ASSIGN: return "MUL_ASSIGN";
214 case DIV_ASSIGN: return "DIV_ASSIGN";
215 case MOD_ASSIGN: return "MOD_ASSIGN";
216 case ADD_ASSIGN: return "ADD_ASSIGN";
217 case SUB_ASSIGN: return "SUB_ASSIGN";
218 case LEFT_ASSIGN: return "LEFT_ASSIGN";
219 case RIGHT_ASSIGN: return "RIGHT_ASSIGN";
220 case AND_ASSIGN: return "AND_ASSIGN";
221 case XOR_ASSIGN: return "XOR_ASSIGN";
222 case OR_ASSIGN: return "OR_ASSIGN";
223 case TYPEDEF: return "TYPEDEF";
224 case EXTERN: return "EXTERN";
225 case STATIC: return "STATIC";
226 case AUTO: return "AUTO";
227 case REGISTER: return "REGISTER";
228 case CODE: return "CODE";
229 case EEPROM: return "EEPROM";
230 case INTERRUPT: return "INTERRUPT";
231 case SFR: return "SFR";
232 case AT: return "AT";
233 case SBIT: return "SBIT";
234 case REENTRANT: return "REENTRANT";
235 case USING: return "USING";
236 case XDATA: return "XDATA";
237 case DATA: return "DATA";
238 case IDATA: return "IDATA";
239 case PDATA: return "PDATA";
240 case VAR_ARGS: return "VAR_ARGS";
241 case CRITICAL: return "CRITICAL";
242 case NONBANKED: return "NONBANKED";
243 case BANKED: return "BANKED";
244 case CHAR: return "CHAR";
245 case SHORT: return "SHORT";
246 case INT: return "INT";
247 case LONG: return "LONG";
248 case SIGNED: return "SIGNED";
249 case UNSIGNED: return "UNSIGNED";
250 case FLOAT: return "FLOAT";
251 case DOUBLE: return "DOUBLE";
252 case CONST: return "CONST";
253 case VOLATILE: return "VOLATILE";
254 case VOID: return "VOID";
255 case BIT: return "BIT";
256 case STRUCT: return "STRUCT";
257 case UNION: return "UNION";
258 case ENUM: return "ENUM";
259 case ELIPSIS: return "ELIPSIS";
260 case RANGE: return "RANGE";
261 case FAR: return "FAR";
262 case CASE: return "CASE";
263 case DEFAULT: return "DEFAULT";
264 case IF: return "IF";
265 case ELSE: return "ELSE";
266 case SWITCH: return "SWITCH";
267 case WHILE: return "WHILE";
268 case DO: return "DO";
269 case FOR: return "FOR";
270 case GOTO: return "GOTO";
271 case CONTINUE: return "CONTINUE";
272 case BREAK: return "BREAK";
273 case RETURN: return "RETURN";
274 case INLINEASM: return "INLINEASM";
275 case IFX: return "IFX";
276 case ADDRESS_OF: return "ADDRESS_OF";
277 case GET_VALUE_AT_ADDRESS: return "GET_VALUE_AT_ADDRESS";
278 case SPIL: return "SPIL";
279 case UNSPIL: return "UNSPIL";
280 case GETHBIT: return "GETHBIT";
281 case BITWISEAND: return "BITWISEAND";
282 case UNARYMINUS: return "UNARYMINUS";
283 case IPUSH: return "IPUSH";
284 case IPOP: return "IPOP";
285 case PCALL: return "PCALL";
286 case ENDFUNCTION: return "ENDFUNCTION";
287 case JUMPTABLE: return "JUMPTABLE";
288 case RRC: return "RRC";
289 case RLC: return "RLC";
290 case CAST: return "CAST";
291 case CALL: return "CALL";
292 case PARAM: return "PARAM ";
293 case NULLOP: return "NULLOP";
294 case BLOCK: return "BLOCK";
295 case LABEL: return "LABEL";
296 case RECEIVE: return "RECEIVE";
297 case SEND: return "SEND";
299 sprintf (buffer, "unknown op %d %c", op, op & 0xff);
302 /*-----------------------------------------------------------------*/
303 /*-----------------------------------------------------------------*/
305 debugLogRegType (short type)
310 case REG_GPR: return "REG_GPR";
311 case REG_PTR: return "REG_PTR";
312 case REG_CND: return "REG_CND";
315 sprintf (buffer, "unknown reg type %d", type);
319 /*-----------------------------------------------------------------*/
320 /*-----------------------------------------------------------------*/
321 static int regname2key(char const *name)
330 key += (*name++) + 1;
334 return ( (key + (key >> 4) + (key>>8)) & 0x3f);
338 /*-----------------------------------------------------------------*/
339 /* newReg - allocate and init memory for a new register */
340 /*-----------------------------------------------------------------*/
341 static regs* newReg(short type, short pc_type, int rIdx, char *name, int size, int alias)
346 dReg = Safe_calloc(1,sizeof(regs));
348 dReg->pc_type = pc_type;
351 dReg->name = Safe_strdup(name);
353 sprintf(buffer,"r0x%02X", dReg->rIdx);
354 dReg->name = Safe_strdup(buffer);
370 dReg->reg_alias = NULL;
371 dReg->reglives.usedpFlows = newSet();
372 dReg->reglives.assignedpFlows = newSet();
374 hTabAddItem(&dynDirectRegNames, regname2key(name), dReg);
379 /*-----------------------------------------------------------------*/
380 /* regWithIdx - Search through a set of registers that matches idx */
381 /*-----------------------------------------------------------------*/
383 regWithIdx (set *dRegs, int idx, int fixed)
387 for (dReg = setFirstItem(dRegs) ; dReg ;
388 dReg = setNextItem(dRegs)) {
390 if(idx == dReg->rIdx && (fixed == (int)dReg->isFixed)) {
398 /*-----------------------------------------------------------------*/
399 /* regWithName - Search through a set of registers that matches name */
400 /*-----------------------------------------------------------------*/
402 regWithName (set *dRegs, const char *name)
406 for (dReg = setFirstItem(dRegs) ; dReg ;
407 dReg = setNextItem(dRegs)) {
409 if((strcmp(name,dReg->name)==0)) {
417 /*-----------------------------------------------------------------*/
418 /* regWithName - Search for a registers that matches name */
419 /*-----------------------------------------------------------------*/
421 regFindWithName (const char *name)
425 if( (dReg = regWithName ( dynDirectRegs, name)) != NULL ) {
426 debugLog ("Found a Direct Register!\n");
429 if( (dReg = regWithName ( dynDirectBitRegs, name)) != NULL) {
430 debugLog ("Found a Direct Bit Register!\n");
434 if (*name=='_') name++; // Step passed '_'
436 if( (dReg = regWithName ( dynAllocRegs, name)) != NULL) {
437 debugLog ("Found a Dynamic Register!\n");
440 if( (dReg = regWithName ( dynProcessorRegs, name)) != NULL) {
441 debugLog ("Found a Processor Register!\n");
444 if( (dReg = regWithName ( dynInternalRegs, name)) != NULL) {
445 debugLog ("Found an Internal Register!\n");
448 if( (dReg = regWithName ( dynStackRegs, name)) != NULL) {
449 debugLog ("Found an Stack Register!\n");
456 /*-----------------------------------------------------------------*/
457 /* regFindFree - Search for a free register in a set of registers */
458 /*-----------------------------------------------------------------*/
460 regFindFree (set *dRegs)
464 for (dReg = setFirstItem(dRegs) ; dReg ;
465 dReg = setNextItem(dRegs)) {
473 /*-----------------------------------------------------------------*/
474 /* initStack - allocate registers for a psuedo stack */
475 /*-----------------------------------------------------------------*/
476 void initStack(int base_address, int size)
481 Gstack_base_addr = base_address;
483 //fprintf(stderr,"initStack [base:0x%02x, size:%d]\n", base_address, size);
485 for(i = 0; i<size; i++) {
487 SNPRINTF(&buffer[0], 16, "STK%02d", i);
488 regs *r = newReg(REG_STK, PO_GPR_TEMP,base_address,buffer,1,0);
489 r->address = base_address; // Pseudo stack needs a fixed location that can be known by all modules
493 r->alias = 0x180; // Using shared memory for pseudo stack
494 addSet(&dynStackRegs,r);
499 /*-----------------------------------------------------------------*
500 *-----------------------------------------------------------------*/
502 allocProcessorRegister(int rIdx, char * name, short po_type, int alias)
505 //fprintf(stderr,"allocProcessorRegister %s addr =0x%x\n",name,rIdx);
506 return addSet(&dynProcessorRegs,newReg(REG_SFR, po_type, rIdx, name,1,alias));
509 /*-----------------------------------------------------------------*
510 *-----------------------------------------------------------------*/
513 allocInternalRegister(int rIdx, char * name, short po_type, int alias)
515 regs * reg = newReg(REG_GPR, po_type, rIdx, name,1,alias);
517 //fprintf(stderr,"allocInternalRegister %s addr =0x%x\n",name,rIdx);
520 return addSet(&dynInternalRegs,reg);
525 /*-----------------------------------------------------------------*/
526 /* allocReg - allocates register of given type */
527 /*-----------------------------------------------------------------*/
529 allocReg (short type)
533 debugLog ("%s of type %s\n", __FUNCTION__, debugLogRegType (type));
534 //fprintf(stderr,"allocReg\n");
536 reg = pic14_findFreeReg (type);
544 //return addSet(&dynAllocRegs,newReg(REG_GPR, PO_GPR_TEMP,dynrIdx++,NULL,1,0));
549 /*-----------------------------------------------------------------*/
550 /* dirregWithName - search for register by name */
551 /*-----------------------------------------------------------------*/
553 dirregWithName (char *name)
561 /* hash the name to get a key */
563 hkey = regname2key(name);
565 reg = hTabFirstItemWK(dynDirectRegNames, hkey);
569 if(STRCASECMP(reg->name, name) == 0) {
573 reg = hTabNextItemWK (dynDirectRegNames);
577 return NULL; // name wasn't found in the hash table
580 int IS_CONFIG_ADDRESS(int address)
583 return address == 0x2007;
586 /*-----------------------------------------------------------------*/
587 /* allocNewDirReg - allocates a new register of given type */
588 /*-----------------------------------------------------------------*/
590 allocNewDirReg (sym_link *symlnk,const char *name)
594 sym_link *spec = getSpec (symlnk);
596 /* if this is at an absolute address, then get the address. */
597 if (SPEC_ABSA (spec) ) {
598 address = SPEC_ADDR (spec);
599 //fprintf(stderr,"reg %s is at an absolute address: 0x%03x\n",name,address);
602 /* Register wasn't found in hash, so let's create
603 * a new one and put it in the hash table AND in the
604 * dynDirectRegNames set */
605 if (IS_CONFIG_ADDRESS(address)) {
606 debugLog (" -- %s is declared at address 0x2007\n",name);
611 if (IS_BITVAR (spec))
618 reg = newReg(REG_GPR, PO_DIR, idx, (char*)name,getSize (symlnk),0 );
619 debugLog (" -- added %s to hash, size = %d\n", (char*)name,reg->size);
621 if (SPEC_ABSA (spec) ) {
625 if (IS_BITVAR (spec)) {
626 addSet(&dynDirectBitRegs, reg);
629 addSet(&dynDirectRegs, reg);
631 if (!IS_STATIC (spec)) {
634 if (IS_EXTERN (spec)) {
640 if (address && reg) {
642 reg->address = address;
643 debugLog (" -- and it is at a fixed address 0x%02x\n",reg->address);
649 /*-----------------------------------------------------------------*/
650 /* allocDirReg - allocates register of given type */
651 /*-----------------------------------------------------------------*/
653 allocDirReg (operand *op )
660 debugLog ("%s BAD, op is NULL\n", __FUNCTION__);
664 name = OP_SYMBOL (op)->rname[0] ? OP_SYMBOL (op)->rname : OP_SYMBOL (op)->name;
666 /* If the symbol is at a fixed address, then remove the leading underscore
667 * from the name. This is hack to allow the .asm include file named registers
668 * to match the .c declared register names */
670 //if (SPEC_ABSA ( OP_SYM_ETYPE(op)) && (*name == '_'))
673 debugLog ("%s symbol name %s\n", __FUNCTION__,name);
675 if(SPEC_CONST ( OP_SYM_ETYPE(op)) && (IS_CHAR ( OP_SYM_ETYPE(op)) )) {
676 debugLog(" %d const char\n",__LINE__);
677 debugLog(" value = %s \n",SPEC_CVAL( OP_SYM_ETYPE(op)));
680 debugLog(" %d storage class %d \n",__LINE__,SPEC_SCLS( OP_SYM_ETYPE(op)));
681 if (IS_CODE ( OP_SYM_ETYPE(op)) )
682 debugLog(" %d code space\n",__LINE__);
684 if (IS_INTEGRAL ( OP_SYM_ETYPE(op)) )
685 debugLog(" %d integral\n",__LINE__);
686 if (IS_LITERAL ( OP_SYM_ETYPE(op)) )
687 debugLog(" %d literal\n",__LINE__);
688 if (IS_SPEC ( OP_SYM_ETYPE(op)) )
689 debugLog(" %d specifier\n",__LINE__);
690 debugAopGet(NULL, op);
693 if (IS_CODE ( OP_SYM_ETYPE(op)) )
696 /* First, search the hash table to see if there is a register with this name */
697 if (SPEC_ABSA ( OP_SYM_ETYPE(op)) && !(IS_BITVAR (OP_SYM_ETYPE(op))) ) {
698 reg = regWithIdx (dynProcessorRegs, SPEC_ADDR ( OP_SYM_ETYPE(op)), 1);
701 fprintf(stderr,"ralloc %s is at fixed address but not a processor reg, addr=0x%x\n",
702 name, SPEC_ADDR ( OP_SYM_ETYPE(op)));
704 fprintf(stderr,"ralloc %s at fixed address has already been declared, addr=0x%x\n",
705 name, SPEC_ADDR ( OP_SYM_ETYPE(op)));
708 //fprintf(stderr,"ralloc:%d %s \n", __LINE__,name);
710 reg = dirregWithName(name);
717 /* if this is at an absolute address, then get the address. */
718 if (SPEC_ABSA ( OP_SYM_ETYPE(op)) ) {
719 address = SPEC_ADDR ( OP_SYM_ETYPE(op));
720 //fprintf(stderr,"reg %s is at an absolute address: 0x%03x\n",name,address);
723 /* Register wasn't found in hash, so let's create
724 * a new one and put it in the hash table AND in the
725 * dynDirectRegNames set */
726 if(!IS_CONFIG_ADDRESS(address)) {
727 //fprintf(stderr,"allocating new reg %s\n",name);
729 reg = newReg(REG_GPR, PO_DIR, rDirectIdx++, name,getSize (OP_SYMBOL (op)->type),0 );
730 debugLog (" -- added %s to hash, size = %d\n", name,reg->size);
732 //hTabAddItem(&dynDirectRegNames, regname2key(name), reg);
734 if (SPEC_ABSA ( OP_SYM_ETYPE(op)) ) {
736 //fprintf(stderr, " ralloc.c at fixed address: %s - changing to REG_SFR\n",name);
740 if (IS_BITVAR (OP_SYM_ETYPE(op))) {
741 addSet(&dynDirectBitRegs, reg);
744 addSet(&dynDirectRegs, reg);
746 if (!IS_STATIC (OP_SYM_ETYPE(op))) {
749 if (IS_EXTERN (OP_SYM_ETYPE(op))) {
755 debugLog (" -- %s is declared at address 0x2007\n",name);
760 if (SPEC_ABSA ( OP_SYM_ETYPE(op)) ) {
762 reg->address = SPEC_ADDR ( OP_SYM_ETYPE(op));
763 debugLog (" -- and it is at a fixed address 0x%02x\n",reg->address);
768 if (SPEC_ABSA ( OP_SYM_ETYPE(op)) ) {
770 reg->address = SPEC_ADDR ( OP_SYM_ETYPE(op));
771 debugLog (" -- and it is at a fixed address 0x%02x\n",reg->address);
774 allocNewDirReg (OP_SYM_TYPE(op),name);
781 /*-----------------------------------------------------------------*/
782 /* allocRegByName - allocates register with given name */
783 /*-----------------------------------------------------------------*/
785 allocRegByName (char *name, int size)
791 fprintf(stderr, "%s - allocating a NULL register\n",__FUNCTION__);
795 /* First, search the hash table to see if there is a register with this name */
796 reg = dirregWithName(name);
802 /* Register wasn't found in hash, so let's create
803 * a new one and put it in the hash table AND in the
804 * dynDirectRegNames set */
805 //fprintf (stderr,"%s symbol name %s, size:%d\n", __FUNCTION__,name,size);
806 reg = newReg(REG_GPR, PO_DIR, rDirectIdx++, name,size,0 );
807 for (sym = setFirstItem(sfr->syms); sym; sym = setNextItem(sfr->syms)) {
808 if (strcmp(reg->name+1,sym->name)==0) {
809 unsigned a = SPEC_ADDR(sym->etype);
813 if (!IS_STATIC (sym->etype)) {
816 if (IS_EXTERN (sym->etype)) {
819 if (IS_BITVAR (sym->etype))
826 for (sym = setFirstItem(data->syms); sym; sym = setNextItem(data->syms)) {
827 if (strcmp(reg->name+1,sym->name)==0) {
828 unsigned a = SPEC_ADDR(sym->etype);
830 if (!IS_STATIC (sym->etype)) {
833 if (IS_EXTERN (sym->etype)) {
836 if (IS_BITVAR (sym->etype))
844 debugLog (" -- added %s to hash, size = %d\n", name,reg->size);
846 //hTabAddItem(&dynDirectRegNames, regname2key(name), reg);
847 if (reg->isBitField) {
848 addSet(&dynDirectBitRegs, reg);
850 addSet(&dynDirectRegs, reg);
856 /*-----------------------------------------------------------------*/
857 /* RegWithIdx - returns pointer to register with index number */
858 /*-----------------------------------------------------------------*/
860 typeRegWithIdx (int idx, int type, int fixed)
865 debugLog ("%s - requesting index = 0x%x\n", __FUNCTION__,idx);
870 if( (dReg = regWithIdx ( dynAllocRegs, idx, fixed)) != NULL) {
872 debugLog ("Found a Dynamic Register!\n");
875 if( (dReg = regWithIdx ( dynDirectRegs, idx, fixed)) != NULL ) {
876 debugLog ("Found a Direct Register!\n");
882 if( (dReg = regWithIdx ( dynStackRegs, idx, fixed)) != NULL ) {
883 debugLog ("Found a Stack Register!\n");
887 werror (E_STACK_OUT, "Register");
888 /* return an existing register just to avoid the SDCC crash */
889 return regWithIdx ( dynStackRegs, 0x7f, fixed);
893 if( (dReg = regWithIdx ( dynProcessorRegs, idx, fixed)) != NULL ) {
894 debugLog ("Found a Processor Register!\n");
908 /*-----------------------------------------------------------------*/
909 /* pic14_regWithIdx - returns pointer to register with index number*/
910 /*-----------------------------------------------------------------*/
912 pic14_regWithIdx (int idx)
916 if( (dReg = typeRegWithIdx(idx,REG_GPR,0)) != NULL)
919 if( (dReg = typeRegWithIdx(idx,REG_SFR,0)) != NULL)
925 /*-----------------------------------------------------------------*/
926 /* pic14_regWithIdx - returns pointer to register with index number */
927 /*-----------------------------------------------------------------*/
929 pic14_allocWithIdx (int idx)
934 debugLog ("%s - allocating with index = 0x%x\n", __FUNCTION__,idx);
936 if( (dReg = regWithIdx ( dynAllocRegs, idx,0)) != NULL) {
938 debugLog ("Found a Dynamic Register!\n");
939 } else if( (dReg = regWithIdx ( dynStackRegs, idx,0)) != NULL ) {
940 debugLog ("Found a Stack Register!\n");
941 } else if( (dReg = regWithIdx ( dynProcessorRegs, idx,0)) != NULL ) {
942 debugLog ("Found a Processor Register!\n");
943 } else if( (dReg = regWithIdx ( dynInternalRegs, idx,0)) != NULL ) {
944 debugLog ("Found an Internal Register!\n");
945 } else if( (dReg = regWithIdx ( dynInternalRegs, idx,1)) != NULL ) {
946 debugLog ("Found an Internal Register!\n");
949 debugLog ("Dynamic Register not found\n");
952 //fprintf(stderr,"%s %d - requested register: 0x%x\n",__FUNCTION__,__LINE__,idx);
953 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
954 "regWithIdx not found");
964 /*-----------------------------------------------------------------*/
965 /*-----------------------------------------------------------------*/
967 pic14_findFreeReg(short type)
974 if((dReg = regFindFree(dynAllocRegs)) != NULL)
976 return addSet(&dynAllocRegs,newReg(REG_GPR, PO_GPR_TEMP,dynrIdx++,NULL,1,0));
980 if((dReg = regFindFree(dynStackRegs)) != NULL)
992 /*-----------------------------------------------------------------*/
993 /* freeReg - frees a register */
994 /*-----------------------------------------------------------------*/
998 debugLog ("%s\n", __FUNCTION__);
1003 /*-----------------------------------------------------------------*/
1004 /* nFreeRegs - returns number of free registers */
1005 /*-----------------------------------------------------------------*/
1007 nFreeRegs (int type)
1009 /* dynamically allocate as many as we need and worry about
1010 * fitting them into a PIC later */
1017 debugLog ("%s\n", __FUNCTION__);
1018 for (i = 0; i < pic14_nRegs; i++)
1019 if (regspic14[i].isFree && regspic14[i].type == type)
1025 /*-----------------------------------------------------------------*/
1026 /* nfreeRegsType - free registers with type */
1027 /*-----------------------------------------------------------------*/
1029 nfreeRegsType (int type)
1032 debugLog ("%s\n", __FUNCTION__);
1033 if (type == REG_PTR)
1035 if ((nfr = nFreeRegs (type)) == 0)
1036 return nFreeRegs (REG_GPR);
1039 return nFreeRegs (type);
1042 void writeSetUsedRegs(FILE *of, set *dRegs)
1047 for (dReg = setFirstItem(dRegs) ; dReg ;
1048 dReg = setNextItem(dRegs)) {
1051 fprintf (of, "\t%s\n",dReg->name);
1055 extern void assignFixedRegisters(set *regset);
1056 extern void assignRelocatableRegisters(set *regset,int used);
1057 extern void dump_map(void);
1058 extern void dump_sfr(FILE *of);
1060 void packBits(set *bregs)
1064 regs *bitfield=NULL;
1065 regs *relocbitfield=NULL;
1071 for (regset = bregs ; regset ;
1072 regset = regset->next) {
1074 breg = regset->item;
1075 breg->isBitField = 1;
1076 //fprintf(stderr,"bit reg: %s\n",breg->name);
1079 //fprintf(stderr,"packing bit at fixed address = 0x%03x\n",breg->address);
1081 bitfield = typeRegWithIdx (breg->address >> 3, -1 , 1);
1082 breg->rIdx = breg->address & 7;
1083 breg->address >>= 3;
1086 //sprintf (buffer, "fbitfield%02x", breg->address);
1087 sprintf (buffer, "0x%02x", breg->address);
1088 //fprintf(stderr,"new bit field\n");
1089 bitfield = newReg(REG_SFR, PO_GPR_BIT,breg->address,buffer,1,0);
1090 bitfield->isBitField = 1;
1091 bitfield->isFixed = 1;
1092 bitfield->address = breg->address;
1093 //addSet(&dynDirectRegs,bitfield);
1094 addSet(&dynInternalRegs,bitfield);
1095 //hTabAddItem(&dynDirectRegNames, regname2key(buffer), bitfield);
1097 //fprintf(stderr," which is occupied by %s (addr = %d)\n",bitfield->name,bitfield->address);
1100 breg->reg_alias = bitfield;
1104 if(!relocbitfield || bit_no >7) {
1107 sprintf (buffer, "bitfield%d", byte_no);
1108 //fprintf(stderr,"new relocatable bit field\n");
1109 relocbitfield = newReg(REG_GPR, PO_GPR_BIT,rDirectIdx++,buffer,1,0);
1110 relocbitfield->isBitField = 1;
1111 //addSet(&dynDirectRegs,relocbitfield);
1112 addSet(&dynInternalRegs,relocbitfield);
1113 //hTabAddItem(&dynDirectRegNames, regname2key(buffer), relocbitfield);
1117 breg->reg_alias = relocbitfield;
1118 breg->address = rDirectIdx; /* byte_no; */
1119 breg->rIdx = bit_no++;
1127 void bitEQUs(FILE *of, set *bregs)
1129 regs *breg,*bytereg;
1132 //fprintf(stderr," %s\n",__FUNCTION__);
1133 for (breg = setFirstItem(bregs) ; breg ;
1134 breg = setNextItem(bregs)) {
1136 //fprintf(stderr,"bit reg: %s\n",breg->name);
1138 bytereg = breg->reg_alias;
1140 fprintf (of, "%s\tEQU\t( (%s<<3)+%d)\n",
1143 breg->rIdx & 0x0007);
1146 //fprintf(stderr, "bit field is not assigned to a register\n");
1147 fprintf (of, "%s\tEQU\t( (bitfield%d<<3)+%d)\n",
1157 void aliasEQUs(FILE *of, set *fregs, int use_rIdx)
1162 for (reg = setFirstItem(fregs) ; reg ;
1163 reg = setNextItem(fregs)) {
1165 //if(!reg->isEmitted && reg->wasUsed) {
1168 fprintf (of, "%s\tEQU\t0x%03x\n",
1172 fprintf (of, "%s\tEQU\t0x%03x\n",
1180 void writeUsedRegs(FILE *of)
1182 packBits(dynDirectBitRegs);
1184 assignFixedRegisters(dynInternalRegs);
1185 assignFixedRegisters(dynAllocRegs);
1186 assignFixedRegisters(dynStackRegs);
1187 assignFixedRegisters(dynDirectRegs);
1189 assignRelocatableRegisters(dynInternalRegs,0);
1190 assignRelocatableRegisters(dynAllocRegs,0);
1191 assignRelocatableRegisters(dynStackRegs,0);
1193 assignRelocatableRegisters(dynDirectRegs,0);
1195 assignRelocatableRegisters(dynDirectRegs,0);
1196 printf("assignRelocatableRegisters(dynDirectRegs,0);\n");
1201 bitEQUs(of,dynDirectBitRegs);
1203 aliasEQUs(of,dynAllocRegs,0);
1204 aliasEQUs(of,dynDirectRegs,0);
1205 aliasEQUs(of,dynStackRegs,0);
1206 aliasEQUs(of,dynProcessorRegs,1);
1211 /*-----------------------------------------------------------------*/
1212 /* allDefsOutOfRange - all definitions are out of a range */
1213 /*-----------------------------------------------------------------*/
1215 allDefsOutOfRange (bitVect * defs, int fseq, int toseq)
1219 debugLog ("%s\n", __FUNCTION__);
1223 for (i = 0; i < defs->size; i++)
1227 if (bitVectBitValue (defs, i) &&
1228 (ic = hTabItemWithKey (iCodehTab, i)) &&
1229 (ic->seq >= fseq && ic->seq <= toseq))
1239 /*-----------------------------------------------------------------*/
1240 /* computeSpillable - given a point find the spillable live ranges */
1241 /*-----------------------------------------------------------------*/
1243 computeSpillable (iCode * ic)
1247 debugLog ("%s\n", __FUNCTION__);
1248 /* spillable live ranges are those that are live at this
1249 point . the following categories need to be subtracted
1251 a) - those that are already spilt
1252 b) - if being used by this one
1253 c) - defined by this one */
1255 spillable = bitVectCopy (ic->rlive);
1257 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
1259 bitVectCplAnd (spillable, ic->uses); /* used in this one */
1260 bitVectUnSetBit (spillable, ic->defKey);
1261 spillable = bitVectIntersect (spillable, _G.regAssigned);
1266 /*-----------------------------------------------------------------*/
1267 /* noSpilLoc - return true if a variable has no spil location */
1268 /*-----------------------------------------------------------------*/
1270 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
1272 debugLog ("%s\n", __FUNCTION__);
1273 return (sym->usl.spillLoc ? 0 : 1);
1276 /*-----------------------------------------------------------------*/
1277 /* hasSpilLoc - will return 1 if the symbol has spil location */
1278 /*-----------------------------------------------------------------*/
1280 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
1282 debugLog ("%s\n", __FUNCTION__);
1283 return (sym->usl.spillLoc ? 1 : 0);
1286 /*-----------------------------------------------------------------*/
1287 /* directSpilLoc - will return 1 if the splilocation is in direct */
1288 /*-----------------------------------------------------------------*/
1290 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
1292 debugLog ("%s\n", __FUNCTION__);
1293 if (sym->usl.spillLoc &&
1294 (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
1300 /*-----------------------------------------------------------------*/
1301 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
1302 /* but is not used as a pointer */
1303 /*-----------------------------------------------------------------*/
1305 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
1307 debugLog ("%s\n", __FUNCTION__);
1308 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
1311 /*-----------------------------------------------------------------*/
1312 /* rematable - will return 1 if the remat flag is set */
1313 /*-----------------------------------------------------------------*/
1315 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
1317 debugLog ("%s\n", __FUNCTION__);
1321 /*-----------------------------------------------------------------*/
1322 /* notUsedInRemaining - not used or defined in remain of the block */
1323 /*-----------------------------------------------------------------*/
1325 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
1327 debugLog ("%s\n", __FUNCTION__);
1328 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
1329 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
1332 /*-----------------------------------------------------------------*/
1333 /* allLRs - return true for all */
1334 /*-----------------------------------------------------------------*/
1336 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
1338 debugLog ("%s\n", __FUNCTION__);
1342 /*-----------------------------------------------------------------*/
1343 /* liveRangesWith - applies function to a given set of live range */
1344 /*-----------------------------------------------------------------*/
1346 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
1347 eBBlock * ebp, iCode * ic)
1352 debugLog ("%s\n", __FUNCTION__);
1353 if (!lrs || !lrs->size)
1356 for (i = 1; i < lrs->size; i++)
1359 if (!bitVectBitValue (lrs, i))
1362 /* if we don't find it in the live range
1363 hash table we are in serious trouble */
1364 if (!(sym = hTabItemWithKey (liveRanges, i)))
1366 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1367 "liveRangesWith could not find liveRange");
1371 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
1372 addSetHead (&rset, sym);
1379 /*-----------------------------------------------------------------*/
1380 /* leastUsedLR - given a set determines which is the least used */
1381 /*-----------------------------------------------------------------*/
1383 leastUsedLR (set * sset)
1385 symbol *sym = NULL, *lsym = NULL;
1387 debugLog ("%s\n", __FUNCTION__);
1388 sym = lsym = setFirstItem (sset);
1393 for (; lsym; lsym = setNextItem (sset))
1396 /* if usage is the same then prefer
1397 the spill the smaller of the two */
1398 if (lsym->used == sym->used)
1399 if (getSize (lsym->type) < getSize (sym->type))
1403 if (lsym->used < sym->used)
1408 setToNull ((void *) &sset);
1413 /*-----------------------------------------------------------------*/
1414 /* noOverLap - will iterate through the list looking for over lap */
1415 /*-----------------------------------------------------------------*/
1417 noOverLap (set * itmpStack, symbol * fsym)
1420 debugLog ("%s\n", __FUNCTION__);
1423 for (sym = setFirstItem (itmpStack); sym;
1424 sym = setNextItem (itmpStack))
1426 if (sym->liveTo > fsym->liveFrom)
1434 /*-----------------------------------------------------------------*/
1435 /* isFree - will return 1 if the a free spil location is found */
1436 /*-----------------------------------------------------------------*/
1441 V_ARG (symbol **, sloc);
1442 V_ARG (symbol *, fsym);
1444 debugLog ("%s\n", __FUNCTION__);
1445 /* if already found */
1449 /* if it is free && and the itmp assigned to
1450 this does not have any overlapping live ranges
1451 with the one currently being assigned and
1452 the size can be accomodated */
1454 noOverLap (sym->usl.itmpStack, fsym) &&
1455 getSize (sym->type) >= getSize (fsym->type))
1464 /*-----------------------------------------------------------------*/
1465 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
1466 /*-----------------------------------------------------------------*/
1468 spillLRWithPtrReg (symbol * forSym)
1474 debugLog ("%s\n", __FUNCTION__);
1475 if (!_G.regAssigned ||
1476 bitVectIsZero (_G.regAssigned))
1479 r0 = pic14_regWithIdx (R0_IDX);
1480 r1 = pic14_regWithIdx (R1_IDX);
1482 /* for all live ranges */
1483 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
1484 lrsym = hTabNextItem (liveRanges, &k))
1488 /* if no registers assigned to it or
1490 /* if it does not overlap with this then
1491 not need to spill it */
1493 if (lrsym->isspilt || !lrsym->nRegs ||
1494 (lrsym->liveTo < forSym->liveFrom))
1497 /* go thru the registers : if it is either
1498 r0 or r1 then spil it */
1499 for (j = 0; j < lrsym->nRegs; j++)
1500 if (lrsym->regs[j] == r0 ||
1501 lrsym->regs[j] == r1)
1510 /*-----------------------------------------------------------------*/
1511 /* createStackSpil - create a location on the stack to spil */
1512 /*-----------------------------------------------------------------*/
1514 createStackSpil (symbol * sym)
1516 symbol *sloc = NULL;
1517 int useXstack, model, noOverlay;
1519 char slocBuffer[30];
1520 debugLog ("%s\n", __FUNCTION__);
1524 /* first go try and find a free one that is already
1525 existing on the stack */
1526 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
1528 /* found a free one : just update & return */
1529 sym->usl.spillLoc = sloc;
1532 addSetHead (&sloc->usl.itmpStack, sym);
1536 /* could not then have to create one , this is the hard part
1537 we need to allocate this on the stack : this is really a
1538 hack!! but cannot think of anything better at this time */
1540 if (sprintf (slocBuffer, "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
1542 fprintf (stderr, "kkkInternal error: slocBuffer overflowed: %s:%d\n",
1543 __FILE__, __LINE__);
1547 sloc = newiTemp (slocBuffer);
1549 /* set the type to the spilling symbol */
1550 sloc->type = copyLinkChain (sym->type);
1551 sloc->etype = getSpec (sloc->type);
1552 SPEC_SCLS (sloc->etype) = S_DATA;
1553 SPEC_EXTR (sloc->etype) = 0;
1554 SPEC_STAT (sloc->etype) = 0;
1556 /* we don't allow it to be allocated`
1557 onto the external stack since : so we
1558 temporarily turn it off ; we also
1559 turn off memory model to prevent
1560 the spil from going to the external storage
1561 and turn off overlaying
1564 useXstack = options.useXstack;
1565 model = options.model;
1566 noOverlay = options.noOverlay;
1567 options.noOverlay = 1;
1568 options.model = options.useXstack = 0;
1572 options.useXstack = useXstack;
1573 options.model = model;
1574 options.noOverlay = noOverlay;
1575 sloc->isref = 1; /* to prevent compiler warning */
1577 /* if it is on the stack then update the stack */
1578 if (IN_STACK (sloc->etype))
1580 currFunc->stack += getSize (sloc->type);
1581 _G.stackExtend += getSize (sloc->type);
1584 _G.dataExtend += getSize (sloc->type);
1586 /* add it to the _G.stackSpil set */
1587 addSetHead (&_G.stackSpil, sloc);
1588 sym->usl.spillLoc = sloc;
1591 /* add it to the set of itempStack set
1592 of the spill location */
1593 addSetHead (&sloc->usl.itmpStack, sym);
1597 /*-----------------------------------------------------------------*/
1598 /* isSpiltOnStack - returns true if the spil location is on stack */
1599 /*-----------------------------------------------------------------*/
1601 isSpiltOnStack (symbol * sym)
1605 debugLog ("%s\n", __FUNCTION__);
1614 /* if (sym->_G.stackSpil) */
1617 if (!sym->usl.spillLoc)
1620 etype = getSpec (sym->usl.spillLoc->type);
1621 if (IN_STACK (etype))
1627 /*-----------------------------------------------------------------*/
1628 /* spillThis - spils a specific operand */
1629 /*-----------------------------------------------------------------*/
1631 spillThis (symbol * sym)
1634 debugLog ("%s : %s\n", __FUNCTION__, sym->rname);
1635 FENTRY2("sym: %s, spillLoc:%p (%s)\n", sym->rname, sym->usl.spillLoc, sym->usl.spillLoc ? sym->usl.spillLoc->rname : "<unknown>");
1637 /* if this is rematerializable or has a spillLocation
1638 we are okay, else we need to create a spillLocation
1640 if (!(sym->remat || sym->usl.spillLoc))
1641 createStackSpil (sym);
1644 /* mark it has spilt & put it in the spilt set */
1646 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
1648 bitVectUnSetBit (_G.regAssigned, sym->key);
1650 for (i = 0; i < sym->nRegs; i++)
1654 freeReg (sym->regs[i]);
1655 sym->regs[i] = NULL;
1659 /* if spilt on stack then free up r0 & r1
1660 if they could have been assigned to some
1662 if (!pic14_ptrRegReq && isSpiltOnStack (sym))
1665 spillLRWithPtrReg (sym);
1668 if (sym->usl.spillLoc && !sym->remat)
1669 sym->usl.spillLoc->allocreq = 1;
1674 /*-----------------------------------------------------------------*/
1675 /* selectSpil - select a iTemp to spil : rather a simple procedure */
1676 /*-----------------------------------------------------------------*/
1678 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
1680 bitVect *lrcs = NULL;
1684 debugLog ("%s\n", __FUNCTION__);
1686 /* get the spillable live ranges */
1687 lrcs = computeSpillable (ic);
1690 /* get all live ranges that are rematerizable */
1691 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
1693 /* return the least used of these */
1694 return leastUsedLR (selectS);
1697 /* get live ranges with spillLocations in direct space */
1698 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
1700 sym = leastUsedLR (selectS);
1701 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
1702 sym->usl.spillLoc->rname :
1703 sym->usl.spillLoc->name));
1705 /* mark it as allocation required */
1706 sym->usl.spillLoc->allocreq = 1;
1710 /* if the symbol is local to the block then */
1711 if (forSym->liveTo < ebp->lSeq)
1714 /* check if there are any live ranges allocated
1715 to registers that are not used in this block */
1716 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
1718 sym = leastUsedLR (selectS);
1719 /* if this is not rematerializable */
1728 /* check if there are any live ranges that not
1729 used in the remainder of the block */
1730 if (!_G.blockSpil &&
1731 !isiCodeInFunctionCall (ic) &&
1732 (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
1734 sym = leastUsedLR (selectS);
1737 sym->remainSpil = 1;
1744 /* find live ranges with spillocation && not used as pointers */
1745 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
1748 sym = leastUsedLR (selectS);
1749 /* mark this as allocation required */
1750 sym->usl.spillLoc->allocreq = 1;
1754 /* find live ranges with spillocation */
1755 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
1758 sym = leastUsedLR (selectS);
1759 sym->usl.spillLoc->allocreq = 1;
1763 /* couldn't find then we need to create a spil
1764 location on the stack , for which one? the least
1766 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
1769 /* return a created spil location */
1770 sym = createStackSpil (leastUsedLR (selectS));
1771 sym->usl.spillLoc->allocreq = 1;
1775 /* this is an extreme situation we will spill
1776 this one : happens very rarely but it does happen */
1782 /*-----------------------------------------------------------------*/
1783 /* spilSomething - spil some variable & mark registers as free */
1784 /*-----------------------------------------------------------------*/
1786 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
1791 debugLog ("%s\n", __FUNCTION__);
1792 /* get something we can spil */
1793 ssym = selectSpil (ic, ebp, forSym);
1795 /* mark it as spilt */
1797 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
1799 /* mark it as not register assigned &
1800 take it away from the set */
1801 bitVectUnSetBit (_G.regAssigned, ssym->key);
1803 /* mark the registers as free */
1804 for (i = 0; i < ssym->nRegs; i++)
1806 freeReg (ssym->regs[i]);
1808 /* if spilt on stack then free up r0 & r1
1809 if they could have been assigned to as gprs */
1810 if (!pic14_ptrRegReq && isSpiltOnStack (ssym))
1813 spillLRWithPtrReg (ssym);
1816 /* if this was a block level spil then insert push & pop
1817 at the start & end of block respectively */
1818 if (ssym->blockSpil)
1820 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
1821 /* add push to the start of the block */
1822 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
1823 ebp->sch->next : ebp->sch));
1824 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
1825 /* add pop to the end of the block */
1826 addiCodeToeBBlock (ebp, nic, NULL);
1829 /* if spilt because not used in the remainder of the
1830 block then add a push before this instruction and
1831 a pop at the end of the block */
1832 if (ssym->remainSpil)
1835 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
1836 /* add push just before this instruction */
1837 addiCodeToeBBlock (ebp, nic, ic);
1839 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
1840 /* add pop to the end of the block */
1841 addiCodeToeBBlock (ebp, nic, NULL);
1850 /*-----------------------------------------------------------------*/
1851 /* getRegPtr - will try for PTR if not a GPR type if not spil */
1852 /*-----------------------------------------------------------------*/
1854 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
1859 debugLog ("%s\n", __FUNCTION__);
1861 /* try for a ptr type */
1862 if ((reg = allocReg (REG_PTR)))
1865 /* try for gpr type */
1866 if ((reg = allocReg (REG_GPR)))
1869 /* we have to spil */
1870 if (!spilSomething (ic, ebp, sym))
1873 /* make sure partially assigned registers aren't reused */
1874 for (j=0; j<=sym->nRegs; j++)
1876 sym->regs[j]->isFree = 0;
1878 /* this looks like an infinite loop but
1879 in really selectSpil will abort */
1883 /*-----------------------------------------------------------------*/
1884 /* getRegGpr - will try for GPR if not spil */
1885 /*-----------------------------------------------------------------*/
1887 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
1892 debugLog ("%s\n", __FUNCTION__);
1894 /* try for gpr type */
1895 if ((reg = allocReg (REG_GPR)))
1898 if (!pic14_ptrRegReq)
1899 if ((reg = allocReg (REG_PTR)))
1902 /* we have to spil */
1903 if (!spilSomething (ic, ebp, sym))
1906 /* make sure partially assigned registers aren't reused */
1907 for (j=0; j<=sym->nRegs; j++)
1909 sym->regs[j]->isFree = 0;
1911 /* this looks like an infinite loop but
1912 in really selectSpil will abort */
1916 /*-----------------------------------------------------------------*/
1917 /* symHasReg - symbol has a given register */
1918 /*-----------------------------------------------------------------*/
1920 symHasReg (symbol * sym, regs * reg)
1924 debugLog ("%s\n", __FUNCTION__);
1925 for (i = 0; i < sym->nRegs; i++)
1926 if (sym->regs[i] == reg)
1932 /*-----------------------------------------------------------------*/
1933 /* deassignLRs - check the live to and if they have registers & are */
1934 /* not spilt then free up the registers */
1935 /*-----------------------------------------------------------------*/
1937 deassignLRs (iCode * ic, eBBlock * ebp)
1943 debugLog ("%s\n", __FUNCTION__);
1944 for (sym = hTabFirstItem (liveRanges, &k); sym;
1945 sym = hTabNextItem (liveRanges, &k))
1948 symbol *psym = NULL;
1949 /* if it does not end here */
1950 if (sym->liveTo > ic->seq)
1953 /* Prevent the result from being assigned the same registers as (one)
1954 * operand as many genXXX-functions fail otherwise.
1955 * POINTER_GET(ic) || ic->op == LEFT_OP || ic->op == RIGHT_OP || ic->op == NOT
1956 * are known to fail. */
1957 if (sym->liveTo == ic->seq && IC_RESULT(ic))
1961 case '=': /* assignment */
1962 case BITWISEAND: /* bitwise AND */
1963 case '|': /* bitwise OR */
1964 case '^': /* bitwise XOR */
1965 case '~': /* bitwise negate */
1966 case RLC: /* rotate through carry */
1969 case '+': /* addition */
1970 case '-': /* subtraction */
1971 /* go ahead, these are safe to use with
1972 * non-disjoint register sets */
1976 /* do not release operand registers */
1977 //fprintf (stderr, "%s:%u: operand not freed: ", __FILE__, __LINE__); piCode (ic, stderr); fprintf (stderr, "\n");
1982 /* if it was spilt on stack then we can
1983 mark the stack spil location as free */
1988 sym->usl.spillLoc->isFree = 1;
1994 if (!bitVectBitValue (_G.regAssigned, sym->key))
1996 /* special case check if this is an IFX &
1997 the privious one was a pop and the
1998 previous one was not spilt then keep track
2000 if (ic->op == IFX && ic->prev &&
2001 ic->prev->op == IPOP &&
2002 !ic->prev->parmPush &&
2003 IS_SYMOP(IC_LEFT (ic->prev)) &&
2004 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
2005 psym = OP_SYMBOL (IC_LEFT (ic->prev));
2011 bitVectUnSetBit (_G.regAssigned, sym->key);
2013 /* if the result of this one needs registers
2014 and does not have it then assign it right
2016 if (IC_RESULT (ic) &&
2017 !(SKIP_IC2 (ic) || /* not a special icode */
2018 ic->op == JUMPTABLE ||
2023 POINTER_SET (ic)) &&
2024 IS_SYMOP (IC_RESULT (ic)) &&
2025 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
2026 result->liveTo > ic->seq && /* and will live beyond this */
2027 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
2028 result->liveFrom == ic->seq && /* does not start before here */
2029 result->regType == sym->regType && /* same register types */
2030 result->regType == sym->regType && /* same register types */
2031 result->nRegs && /* which needs registers */
2032 !result->isspilt && /* and does not already have them */
2034 !bitVectBitValue (_G.regAssigned, result->key) &&
2035 /* the number of free regs + number of regs in this LR
2036 can accomodate the what result Needs */
2037 ((nfreeRegsType (result->regType) +
2038 sym->nRegs) >= result->nRegs)
2042 for (i = 0; i < max (sym->nRegs, result->nRegs); i++)
2044 result->regs[i] = sym->regs[i];
2046 result->regs[i] = getRegGpr (ic, ebp, result);
2048 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
2052 /* free the remaining */
2053 for (; i < sym->nRegs; i++)
2057 if (!symHasReg (psym, sym->regs[i]))
2058 freeReg (sym->regs[i]);
2061 freeReg (sym->regs[i]);
2068 /*-----------------------------------------------------------------*/
2069 /* reassignLR - reassign this to registers */
2070 /*-----------------------------------------------------------------*/
2072 reassignLR (operand * op)
2074 symbol *sym = OP_SYMBOL (op);
2077 debugLog ("%s\n", __FUNCTION__);
2078 /* not spilt any more */
2079 sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
2080 bitVectUnSetBit (_G.spiltSet, sym->key);
2082 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
2086 for (i = 0; i < sym->nRegs; i++)
2087 sym->regs[i]->isFree = 0;
2090 /*-----------------------------------------------------------------*/
2091 /* willCauseSpill - determines if allocating will cause a spill */
2092 /*-----------------------------------------------------------------*/
2094 willCauseSpill (int nr, int rt)
2096 debugLog ("%s\n", __FUNCTION__);
2097 /* first check if there are any avlb registers
2098 of te type required */
2101 /* special case for pointer type
2102 if pointer type not avlb then
2103 check for type gpr */
2104 if (nFreeRegs (rt) >= nr)
2106 if (nFreeRegs (REG_GPR) >= nr)
2111 if (pic14_ptrRegReq)
2113 if (nFreeRegs (rt) >= nr)
2118 if (nFreeRegs (REG_PTR) +
2119 nFreeRegs (REG_GPR) >= nr)
2124 debugLog (" ... yep it will (cause a spill)\n");
2125 /* it will cause a spil */
2129 /*-----------------------------------------------------------------*/
2130 /* positionRegs - the allocator can allocate same registers to res- */
2131 /* ult and operand, if this happens make sure they are in the same */
2132 /* position as the operand otherwise chaos results */
2133 /*-----------------------------------------------------------------*/
2135 positionRegs (symbol * result, symbol * opsym, int lineno)
2137 int count = min (result->nRegs, opsym->nRegs);
2138 int i, j = 0, shared = 0;
2140 debugLog ("%s\n", __FUNCTION__);
2141 /* if the result has been spilt then cannot share */
2146 /* first make sure that they actually share */
2147 for (i = 0; i < count; i++)
2149 for (j = 0; j < count; j++)
2151 if (result->regs[i] == opsym->regs[j] && i != j)
2161 regs *tmp = result->regs[i];
2162 result->regs[i] = result->regs[j];
2163 result->regs[j] = tmp;
2168 /*------------------------------------------------------------------*/
2169 /* verifyRegsAssigned - make sure an iTemp is properly initialized; */
2170 /* it should either have registers or have beed spilled. Otherwise, */
2171 /* there was an uninitialized variable, so just spill this to get */
2172 /* the operand in a valid state. */
2173 /*------------------------------------------------------------------*/
2175 verifyRegsAssigned (operand *op, iCode * ic)
2180 if (!IS_ITEMP (op)) return;
2182 sym = OP_SYMBOL (op);
2183 if (sym->isspilt) return;
2184 if (!sym->nRegs) return;
2185 if (sym->regs[0]) return;
2187 werrorfl (ic->filename, ic->lineno, W_LOCAL_NOINIT,
2188 sym->prereqv ? sym->prereqv->name : sym->name);
2193 /*-----------------------------------------------------------------*/
2194 /* serialRegAssign - serially allocate registers to the variables */
2195 /*-----------------------------------------------------------------*/
2197 serialRegAssign (eBBlock ** ebbs, int count)
2201 debugLog ("%s\n", __FUNCTION__);
2202 /* for all blocks */
2203 for (i = 0; i < count; i++)
2208 if (ebbs[i]->noPath &&
2209 (ebbs[i]->entryLabel != entryLabel &&
2210 ebbs[i]->entryLabel != returnLabel))
2213 /* of all instructions do */
2214 for (ic = ebbs[i]->sch; ic; ic = ic->next)
2216 debugLog (" op: %s\n", decodeOp (ic->op));
2218 /* if this is an ipop that means some live
2219 range will have to be assigned again */
2221 reassignLR (IC_LEFT (ic));
2223 /* if result is present && is a true symbol */
2224 if (IC_RESULT (ic) && ic->op != IFX &&
2225 IS_TRUE_SYMOP (IC_RESULT (ic)))
2226 OP_SYMBOL (IC_RESULT (ic))->allocreq = 1;
2228 /* take away registers from live
2229 ranges that end at this instruction */
2230 deassignLRs (ic, ebbs[i]);
2232 /* some don't need registers */
2233 if (SKIP_IC2 (ic) ||
2234 ic->op == JUMPTABLE ||
2238 (IC_RESULT (ic) && POINTER_SET (ic)))
2241 /* now we need to allocate registers
2242 only for the result */
2243 if (IC_RESULT (ic) && IS_SYMOP (IC_RESULT (ic)))
2245 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
2251 /* Make sure any spill location is definately allocated */
2252 if (sym->isspilt && !sym->remat && sym->usl.spillLoc &&
2253 !sym->usl.spillLoc->allocreq)
2255 sym->usl.spillLoc->allocreq++;
2258 /* if it does not need or is spilt
2259 or is already assigned to registers
2260 or will not live beyond this instructions */
2263 bitVectBitValue (_G.regAssigned, sym->key) ||
2264 sym->liveTo <= ic->seq)
2267 /* if some liverange has been spilt at the block level
2268 and this one live beyond this block then spil this
2270 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
2275 /* if trying to allocate this will cause
2276 a spill and there is nothing to spill
2277 or this one is rematerializable then
2279 willCS = willCauseSpill (sym->nRegs, sym->regType);
2280 spillable = computeSpillable (ic);
2282 (willCS && bitVectIsZero (spillable)))
2290 /* If the live range preceeds the point of definition
2291 then ideally we must take into account registers that
2292 have been allocated after sym->liveFrom but freed
2293 before ic->seq. This is complicated, so spill this
2294 symbol instead and let fillGaps handle the allocation. */
2295 if (sym->liveFrom < ic->seq)
2301 /* if it has a spillocation & is used less than
2302 all other live ranges then spill this */
2304 if (sym->usl.spillLoc) {
2305 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
2306 allLRs, ebbs[i], ic));
2307 if (leastUsed && leastUsed->used > sym->used) {
2312 /* if none of the liveRanges have a spillLocation then better
2313 to spill this one than anything else already assigned to registers */
2314 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
2315 /* if this is local to this block then we might find a block spil */
2316 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
2324 if (ic->op == RECEIVE)
2325 debugLog ("When I get clever, I'll optimize the receive logic\n");
2327 /* if we need ptr regs for the right side
2329 if (POINTER_GET (ic)
2330 && IS_SYMOP(IC_LEFT(ic))
2331 && getSize (OP_SYMBOL (IC_LEFT (ic))->type)
2332 <= (unsigned) PTRSIZE)
2337 /* else we assign registers to it */
2338 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
2340 debugLog (" %d - \n", __LINE__);
2342 bitVectDebugOn(_G.regAssigned, debugF);
2343 for (j = 0; j < sym->nRegs; j++)
2345 if (sym->regType == REG_PTR)
2346 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
2348 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
2350 /* if the allocation failed which means
2351 this was spilt then break */
2355 debugLog (" %d - \n", __LINE__);
2357 /* if it shares registers with operands make sure
2358 that they are in the same position */
2359 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
2360 IS_SYMOP(IC_RESULT(ic)) &&
2361 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
2362 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
2363 OP_SYMBOL (IC_LEFT (ic)), ic->lineno);
2364 /* do the same for the right operand */
2365 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
2366 IS_SYMOP(IC_RESULT(ic)) &&
2367 OP_SYMBOL (IC_RIGHT (ic))->nRegs && ic->op != '=')
2368 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
2369 OP_SYMBOL (IC_RIGHT (ic)), ic->lineno);
2371 debugLog (" %d - \n", __LINE__);
2374 debugLog (" %d - \n", __LINE__);
2383 /* Check for and fix any problems with uninitialized operands */
2384 for (i = 0; i < count; i++)
2388 if (ebbs[i]->noPath &&
2389 (ebbs[i]->entryLabel != entryLabel &&
2390 ebbs[i]->entryLabel != returnLabel))
2393 for (ic = ebbs[i]->sch; ic; ic = ic->next)
2400 verifyRegsAssigned (IC_COND (ic), ic);
2404 if (ic->op == JUMPTABLE)
2406 verifyRegsAssigned (IC_JTCOND (ic), ic);
2410 verifyRegsAssigned (IC_RESULT (ic), ic);
2411 verifyRegsAssigned (IC_LEFT (ic), ic);
2412 verifyRegsAssigned (IC_RIGHT (ic), ic);
2418 /*-----------------------------------------------------------------*/
2419 /* rUmaskForOp :- returns register mask for an operand */
2420 /*-----------------------------------------------------------------*/
2422 rUmaskForOp (operand * op)
2428 debugLog ("%s\n", __FUNCTION__);
2429 /* only temporaries are assigned registers */
2433 sym = OP_SYMBOL (op);
2435 /* if spilt or no registers assigned to it
2437 if (sym->isspilt || !sym->nRegs)
2440 rumask = newBitVect (pic14_nRegs);
2442 for (j = 0; j < sym->nRegs; j++)
2444 rumask = bitVectSetBit (rumask,
2445 sym->regs[j]->rIdx);
2451 /*-----------------------------------------------------------------*/
2452 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
2453 /*-----------------------------------------------------------------*/
2455 regsUsedIniCode (iCode * ic)
2457 bitVect *rmask = newBitVect (pic14_nRegs);
2459 debugLog ("%s\n", __FUNCTION__);
2460 /* do the special cases first */
2463 rmask = bitVectUnion (rmask,
2464 rUmaskForOp (IC_COND (ic)));
2468 /* for the jumptable */
2469 if (ic->op == JUMPTABLE)
2471 rmask = bitVectUnion (rmask,
2472 rUmaskForOp (IC_JTCOND (ic)));
2477 /* of all other cases */
2479 rmask = bitVectUnion (rmask,
2480 rUmaskForOp (IC_LEFT (ic)));
2484 rmask = bitVectUnion (rmask,
2485 rUmaskForOp (IC_RIGHT (ic)));
2488 rmask = bitVectUnion (rmask,
2489 rUmaskForOp (IC_RESULT (ic)));
2495 /*-----------------------------------------------------------------*/
2496 /* createRegMask - for each instruction will determine the regsUsed */
2497 /*-----------------------------------------------------------------*/
2499 createRegMask (eBBlock ** ebbs, int count)
2503 debugLog ("%s\n", __FUNCTION__);
2504 /* for all blocks */
2505 for (i = 0; i < count; i++)
2509 if (ebbs[i]->noPath &&
2510 (ebbs[i]->entryLabel != entryLabel &&
2511 ebbs[i]->entryLabel != returnLabel))
2514 /* for all instructions */
2515 for (ic = ebbs[i]->sch; ic; ic = ic->next)
2520 if (SKIP_IC2 (ic) || !ic->rlive)
2523 /* first mark the registers used in this
2525 ic->rUsed = regsUsedIniCode (ic);
2526 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
2528 /* now create the register mask for those
2529 registers that are in use : this is a
2530 super set of ic->rUsed */
2531 ic->rMask = newBitVect (pic14_nRegs + 1);
2533 /* for all live Ranges alive at this point */
2534 for (j = 1; j < ic->rlive->size; j++)
2539 /* if not alive then continue */
2540 if (!bitVectBitValue (ic->rlive, j))
2543 /* find the live range we are interested in */
2544 if (!(sym = hTabItemWithKey (liveRanges, j)))
2546 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
2547 "createRegMask cannot find live range");
2551 /* if no register assigned to it */
2552 if (!sym->nRegs || sym->isspilt)
2555 /* for all the registers allocated to it */
2556 for (k = 0; k < sym->nRegs; k++)
2559 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
2565 /* This was the active version */
2566 /*-----------------------------------------------------------------*/
2567 /* rematStr - returns the rematerialized string for a remat var */
2568 /*-----------------------------------------------------------------*/
2570 rematStr (symbol * sym)
2573 iCode *ic = sym->rematiCode;
2574 symbol *psym = NULL;
2576 debugLog ("%s\n", __FUNCTION__);
2578 //printf ("%s\n", s);
2580 /* if plus or minus print the right hand side */
2582 if (ic->op == '+' || ic->op == '-') {
2584 iCode *ric = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
2586 sprintf (s, "(%s %c 0x%04x)",
2587 OP_SYMBOL (IC_LEFT (ric))->rname,
2589 (int) operandLitValue (IC_RIGHT (ic)));
2592 //fprintf(stderr, "ralloc.c:%d OOPS %s\n",__LINE__,s);
2594 psym = newSymbol (OP_SYMBOL (IC_LEFT (ric))->rname, 1);
2595 psym->offset = (int) operandLitValue (IC_RIGHT (ic));
2600 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
2601 psym = newSymbol (OP_SYMBOL (IC_LEFT (ic))->rname, 1);
2603 //printf ("ralloc.c:%d %s\n", __LINE__,buffer);
2609 /* deprecated version */
2610 /*-----------------------------------------------------------------*/
2611 /* rematStr - returns the rematerialized string for a remat var */
2612 /*-----------------------------------------------------------------*/
2614 rematStr (symbol * sym)
2617 iCode *ic = sym->rematiCode;
2619 debugLog ("%s\n", __FUNCTION__);
2624 /* if plus or minus print the right hand side */
2626 if (ic->op == '+' || ic->op == '-') {
2627 sprintf(s,"0x%04x %c ",(int) operandLitValue(IC_RIGHT(ic)),
2630 ic = OP_SYMBOL(IC_LEFT(ic))->rematiCode;
2634 if (ic->op == '+' || ic->op == '-')
2636 iCode *ric = OP_SYMBOL (IC_LEFT (ic))->rematiCode;
2637 sprintf (s, "(%s %c 0x%04x)",
2638 OP_SYMBOL (IC_LEFT (ric))->rname,
2640 (int) operandLitValue (IC_RIGHT (ic)));
2643 //ic = OP_SYMBOL(IC_LEFT(ic))->rematiCode;
2645 //fprintf(stderr, "ralloc.c:%d OOPS %s\n",__LINE__,s);
2649 /* we reached the end */
2650 sprintf (s, "%s", OP_SYMBOL (IC_LEFT (ic))->rname);
2654 printf ("%s\n", buffer);
2659 /*-----------------------------------------------------------------*/
2660 /* regTypeNum - computes the type & number of registers required */
2661 /*-----------------------------------------------------------------*/
2669 debugLog ("%s\n", __FUNCTION__);
2670 /* for each live range do */
2671 for (sym = hTabFirstItem (liveRanges, &k); sym;
2672 sym = hTabNextItem (liveRanges, &k)) {
2674 debugLog (" %d - %s\n", __LINE__, sym->rname);
2676 /* if used zero times then no registers needed */
2677 if ((sym->liveTo - sym->liveFrom) == 0)
2681 /* if the live range is a temporary */
2684 debugLog (" %d - itemp register\n", __LINE__);
2686 /* if the type is marked as a conditional */
2687 if (sym->regType == REG_CND)
2690 /* if used in return only then we don't
2693 if (IS_AGGREGATE (sym->type) || sym->isptr)
2694 sym->type = aggrToPtr (sym->type, FALSE);
2695 debugLog (" %d - no reg needed - accumulator used\n", __LINE__);
2701 //if (IS_AGGREGATE (sym->type) || sym->isptr)
2702 // sym->type = aggrToPtr (sym->type, FALSE);
2703 debugLog (" %d - used as a return\n", __LINE__);
2708 /* if the symbol has only one definition &
2709 that definition is a get_pointer and the
2710 pointer we are getting is rematerializable and
2714 if (bitVectnBitsOn (sym->defs) == 1 &&
2715 (ic = hTabItemWithKey (iCodehTab,
2716 bitVectFirstBit (sym->defs))) &&
2718 !IS_BITVAR (sym->etype) &&
2719 (aggrToPtrDclType (operandType (IC_LEFT (ic)), FALSE) == POINTER)) {
2721 if (ptrPseudoSymSafe (sym, ic)) {
2725 debugLog (" %d - \n", __LINE__);
2727 /* create a psuedo symbol & force a spil */
2728 //X symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
2729 psym = rematStr (OP_SYMBOL (IC_LEFT (ic)));
2730 psym->type = sym->type;
2731 psym->etype = sym->etype;
2732 psym->psbase = ptrBaseRematSym (OP_SYMBOL (IC_LEFT (ic)));
2733 strcpy (psym->rname, psym->name);
2735 sym->usl.spillLoc = psym;
2739 /* if in data space or idata space then try to
2740 allocate pointer register */
2745 /* if not then we require registers */
2746 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
2747 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
2748 getSize (sym->type));
2751 if(IS_PTR_CONST (sym->type)) {
2752 debugLog (" %d const pointer type requires %d registers, changing to 2\n",__LINE__,sym->nRegs);
2756 if (sym->nRegs > 4) {
2757 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
2758 printTypeChain (sym->type, stderr);
2759 fprintf (stderr, "\n");
2762 /* determine the type of register required */
2763 if (sym->nRegs == 1 &&
2764 IS_PTR (sym->type) &&
2766 sym->regType = REG_PTR;
2768 sym->regType = REG_GPR;
2771 debugLog (" reg name %s, reg type %s\n", sym->rname, debugLogRegType (sym->regType));
2775 /* for the first run we don't provide */
2776 /* registers for true symbols we will */
2777 /* see how things go */
2782 DEFSETFUNC (markRegFree)
2784 ((regs *)item)->isFree = 1;
2789 DEFSETFUNC (deallocReg)
2791 fprintf(stderr,"deallocting register %s\n",((regs *)item)->name);
2792 ((regs *)item)->isFree = 1;
2793 ((regs *)item)->wasUsed = 0;
2797 /*-----------------------------------------------------------------*/
2798 /* freeAllRegs - mark all registers as free */
2799 /*-----------------------------------------------------------------*/
2801 pic14_freeAllRegs ()
2805 debugLog ("%s\n", __FUNCTION__);
2807 applyToSet(dynAllocRegs,markRegFree);
2808 applyToSet(dynStackRegs,markRegFree);
2811 for (i = 0; i < pic14_nRegs; i++)
2812 regspic14[i].isFree = 1;
2816 /*-----------------------------------------------------------------*/
2817 /*-----------------------------------------------------------------*/
2819 pic14_deallocateAllRegs ()
2823 debugLog ("%s\n", __FUNCTION__);
2825 applyToSet(dynAllocRegs,deallocReg);
2828 for (i = 0; i < pic14_nRegs; i++) {
2829 if(regspic14[i].pc_type == PO_GPR_TEMP) {
2830 regspic14[i].isFree = 1;
2831 regspic14[i].wasUsed = 0;
2838 /*-----------------------------------------------------------------*/
2839 /* deallocStackSpil - this will set the stack pointer back */
2840 /*-----------------------------------------------------------------*/
2842 DEFSETFUNC (deallocStackSpil)
2846 debugLog ("%s\n", __FUNCTION__);
2851 /*-----------------------------------------------------------------*/
2852 /* farSpacePackable - returns the packable icode for far variables */
2853 /*-----------------------------------------------------------------*/
2855 farSpacePackable (iCode * ic)
2859 debugLog ("%s\n", __FUNCTION__);
2860 /* go thru till we find a definition for the
2861 symbol on the right */
2862 for (dic = ic->prev; dic; dic = dic->prev)
2865 /* if the definition is a call then no */
2866 if ((dic->op == CALL || dic->op == PCALL) &&
2867 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2872 /* if shift by unknown amount then not */
2873 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
2874 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2877 /* if pointer get and size > 1 */
2878 if (POINTER_GET (dic) &&
2879 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
2882 if (POINTER_SET (dic) &&
2883 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
2886 /* if any three is a true symbol in far space */
2887 if (IC_RESULT (dic) &&
2888 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
2889 isOperandInFarSpace (IC_RESULT (dic)))
2892 if (IC_RIGHT (dic) &&
2893 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
2894 isOperandInFarSpace (IC_RIGHT (dic)) &&
2895 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
2898 if (IC_LEFT (dic) &&
2899 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
2900 isOperandInFarSpace (IC_LEFT (dic)) &&
2901 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
2904 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
2906 if ((dic->op == LEFT_OP ||
2907 dic->op == RIGHT_OP ||
2909 IS_OP_LITERAL (IC_RIGHT (dic)))
2919 /*-----------------------------------------------------------------*/
2920 /* packRegsForAssign - register reduction for assignment */
2921 /*-----------------------------------------------------------------*/
2923 packRegsForAssign (iCode * ic, eBBlock * ebp)
2928 debugLog ("%s\n", __FUNCTION__);
2930 debugAopGet (" result:", IC_RESULT (ic));
2931 debugAopGet (" left:", IC_LEFT (ic));
2932 debugAopGet (" right:", IC_RIGHT (ic));
2934 /* if this is at an absolute address, then get the address. */
2935 if (SPEC_ABSA ( OP_SYM_ETYPE(IC_RESULT(ic))) ) {
2936 if(IS_CONFIG_ADDRESS( SPEC_ADDR ( OP_SYM_ETYPE(IC_RESULT(ic))))) {
2937 debugLog (" %d - found config word declaration\n", __LINE__);
2938 if(IS_VALOP(IC_RIGHT(ic))) {
2939 debugLog (" setting config word to %x\n",
2940 (int) floatFromVal (IC_RIGHT(ic)->operand.valOperand));
2941 assignConfigWordValue( SPEC_ADDR ( OP_SYM_ETYPE(IC_RESULT(ic))),
2942 (int) floatFromVal (IC_RIGHT(ic)->operand.valOperand));
2945 /* remove the assignment from the iCode chain. */
2947 remiCodeFromeBBlock (ebp, ic);
2948 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2949 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2956 if (!IS_ITEMP (IC_RESULT (ic))) {
2957 allocDirReg(IC_RESULT (ic));
2958 debugLog (" %d - result is not temp\n", __LINE__);
2961 if (IC_LEFT (ic) && !IS_ITEMP (IC_LEFT (ic))) {
2962 debugLog (" %d - left is not temp, allocating\n", __LINE__);
2963 allocDirReg(IC_LEFT (ic));
2967 if (!IS_ITEMP (IC_RIGHT (ic))) {
2968 debugLog (" %d - not packing - right is not temp\n", __LINE__);
2970 /* only pack if this is not a function pointer */
2971 if (!IS_REF (IC_RIGHT (ic)))
2972 allocDirReg(IC_RIGHT (ic));
2976 if (OP_SYMBOL (IC_RIGHT (ic))->isind ||
2977 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
2979 debugLog (" %d - not packing - right side fails \n", __LINE__);
2983 /* if the true symbol is defined in far space or on stack
2984 then we should not since this will increase register pressure */
2985 if (isOperandInFarSpace (IC_RESULT (ic)))
2987 if ((dic = farSpacePackable (ic)))
2993 /* find the definition of iTempNN scanning backwards if we find a
2994 a use of the true symbol before we find the definition then
2996 for (dic = ic->prev; dic; dic = dic->prev)
2999 /* if there is a function call and this is
3000 a parameter & not my parameter then don't pack it */
3001 if ((dic->op == CALL || dic->op == PCALL) &&
3002 (OP_SYMBOL (IC_RESULT (ic))->_isparm &&
3003 !OP_SYMBOL (IC_RESULT (ic))->ismyparm))
3005 debugLog (" %d - \n", __LINE__);
3013 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
3014 IS_OP_VOLATILE (IC_RESULT (dic)))
3016 debugLog (" %d - dic is VOLATILE \n", __LINE__);
3021 if (IS_SYMOP (IC_RESULT (dic)) &&
3022 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
3024 /* A previous result was assigned to the same register - we'll our definition */
3025 debugLog (" %d - dic result key == ic right key -- pointer set=%c\n",
3026 __LINE__, ((POINTER_SET (dic)) ? 'Y' : 'N'));
3027 if (POINTER_SET (dic))
3033 if (IS_SYMOP (IC_RIGHT (dic)) &&
3034 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
3035 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
3037 debugLog (" %d - dic right key == ic rightor result key\n", __LINE__);
3042 if (IS_SYMOP (IC_LEFT (dic)) &&
3043 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
3044 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
3046 debugLog (" %d - dic left key == ic rightor result key\n", __LINE__);
3051 if (POINTER_SET (dic) &&
3052 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
3054 debugLog (" %d - dic result key == ic result key -- pointer set=Y\n",
3062 return 0; /* did not find */
3064 /* if the result is on stack or iaccess then it must be
3065 the same at least one of the operands */
3066 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
3067 OP_SYMBOL (IC_RESULT (ic))->iaccess)
3070 /* the operation has only one symbol
3071 operator then we can pack */
3072 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
3073 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
3076 if (!((IC_LEFT (dic) &&
3077 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
3079 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
3083 debugLog (" packing. removing %s\n", OP_SYMBOL (IC_RIGHT (ic))->rname);
3084 debugLog (" replacing with %s\n", OP_SYMBOL (IC_RESULT (dic))->rname);
3085 /* found the definition */
3086 /* replace the result with the result of */
3087 /* this assignment and remove this assignment */
3088 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
3089 IC_RESULT (dic) = IC_RESULT (ic);
3091 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
3093 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
3095 /* delete from liverange table also
3096 delete from all the points inbetween and the new
3098 for (sic = dic; sic != ic; sic = sic->next)
3100 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
3101 if (IS_ITEMP (IC_RESULT (dic)))
3102 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
3105 remiCodeFromeBBlock (ebp, ic);
3106 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3107 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
3108 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
3114 /*-----------------------------------------------------------------*/
3115 /* findAssignToSym : scanning backwards looks for first assig found */
3116 /*-----------------------------------------------------------------*/
3118 findAssignToSym (operand * op, iCode * ic)
3122 debugLog ("%s\n", __FUNCTION__);
3123 for (dic = ic->prev; dic; dic = dic->prev)
3126 /* if definition by assignment */
3127 if (dic->op == '=' &&
3128 !POINTER_SET (dic) &&
3129 IC_RESULT (dic)->key == op->key
3130 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
3134 /* we are interested only if defined in far space */
3135 /* or in stack space in case of + & - */
3137 /* if assigned to a non-symbol then return
3139 if (!IS_SYMOP (IC_RIGHT (dic)))
3142 /* if the symbol is in far space then
3144 if (isOperandInFarSpace (IC_RIGHT (dic)))
3147 /* for + & - operations make sure that
3148 if it is on the stack it is the same
3149 as one of the three operands */
3150 if ((ic->op == '+' || ic->op == '-') &&
3151 OP_SYMBOL (IC_RIGHT (dic))->onStack)
3154 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
3155 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
3156 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
3164 /* if we find an usage then we cannot delete it */
3165 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
3168 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
3171 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
3175 /* now make sure that the right side of dic
3176 is not defined between ic & dic */
3179 iCode *sic = dic->next;
3181 for (; sic != ic; sic = sic->next)
3182 if (IC_RESULT (sic) &&
3183 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
3192 /*-----------------------------------------------------------------*/
3193 /* packRegsForSupport :- reduce some registers for support calls */
3194 /*-----------------------------------------------------------------*/
3196 packRegsForSupport (iCode * ic, eBBlock * ebp)
3200 debugLog ("%s\n", __FUNCTION__);
3201 /* for the left & right operand :- look to see if the
3202 left was assigned a true symbol in far space in that
3203 case replace them */
3204 if (IS_ITEMP (IC_LEFT (ic)) &&
3205 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
3207 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
3213 debugAopGet ("removing left:", IC_LEFT (ic));
3215 /* found it we need to remove it from the
3217 for (sic = dic; sic != ic; sic = sic->next)
3218 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
3220 IC_LEFT (ic)->operand.symOperand =
3221 IC_RIGHT (dic)->operand.symOperand;
3222 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
3223 remiCodeFromeBBlock (ebp, dic);
3224 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
3225 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
3229 /* do the same for the right operand */
3232 IS_ITEMP (IC_RIGHT (ic)) &&
3233 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
3235 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
3241 /* if this is a subtraction & the result
3242 is a true symbol in far space then don't pack */
3243 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
3245 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
3246 if (IN_FARSPACE (SPEC_OCLS (etype)))
3250 debugAopGet ("removing right:", IC_RIGHT (ic));
3252 /* found it we need to remove it from the
3254 for (sic = dic; sic != ic; sic = sic->next)
3255 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
3257 IC_RIGHT (ic)->operand.symOperand =
3258 IC_RIGHT (dic)->operand.symOperand;
3259 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
3261 remiCodeFromeBBlock (ebp, dic);
3262 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
3263 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
3270 #define IS_OP_RUONLY(x) (x && IS_SYMOP(x) && OP_SYMBOL(x)->ruonly)
3273 /*-----------------------------------------------------------------*/
3274 /* packRegsForOneuse : - will reduce some registers for single Use */
3275 /*-----------------------------------------------------------------*/
3277 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
3282 debugLog ("%s\n", __FUNCTION__);
3283 /* if returning a literal then do nothing */
3287 /* only upto 2 bytes since we cannot predict
3288 the usage of b, & acc */
3289 if (getSize (operandType (op)) > (fReturnSizePic - 2) &&
3294 /* this routine will mark the a symbol as used in one
3295 instruction use only && if the definition is local
3296 (ie. within the basic block) && has only one definition &&
3297 that definition is either a return value from a
3298 function or does not contain any variables in
3300 uses = bitVectCopy (OP_USES (op));
3301 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
3302 if (!bitVectIsZero (uses)) /* has other uses */
3305 /* if it has only one defintion */
3306 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
3307 return NULL; /* has more than one definition */
3309 /* get that definition */
3311 hTabItemWithKey (iCodehTab,
3312 bitVectFirstBit (OP_DEFS (op)))))
3315 /* found the definition now check if it is local */
3316 if (dic->seq < ebp->fSeq ||
3317 dic->seq > ebp->lSeq)
3318 return NULL; /* non-local */
3320 /* now check if it is the return from
3322 if (dic->op == CALL || dic->op == PCALL)
3324 if (ic->op != SEND && ic->op != RETURN &&
3325 !POINTER_SET(ic) && !POINTER_GET(ic))
3327 OP_SYMBOL (op)->ruonly = 1;
3334 /* otherwise check that the definition does
3335 not contain any symbols in far space */
3336 if (isOperandInFarSpace (IC_LEFT (dic)) ||
3337 isOperandInFarSpace (IC_RIGHT (dic)) ||
3338 IS_OP_RUONLY (IC_LEFT (ic)) ||
3339 IS_OP_RUONLY (IC_RIGHT (ic)))
3344 /* if pointer set then make sure the pointer
3346 if (POINTER_SET (dic) &&
3347 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
3350 if (POINTER_GET (dic) &&
3351 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
3356 /* also make sure the intervenening instructions
3357 don't have any thing in far space */
3358 for (dic = dic->next; dic && dic != ic; dic = dic->next)
3361 /* if there is an intervening function call then no */
3362 if (dic->op == CALL || dic->op == PCALL)
3364 /* if pointer set then make sure the pointer
3366 if (POINTER_SET (dic) &&
3367 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
3370 if (POINTER_GET (dic) &&
3371 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
3374 /* if address of & the result is remat then okay */
3375 if (dic->op == ADDRESS_OF &&
3376 OP_SYMBOL (IC_RESULT (dic))->remat)
3379 /* if operand has size of three or more & this
3380 operation is a '*','/' or '%' then 'b' may
3382 if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
3383 getSize (operandType (op)) >= 3)
3386 /* if left or right or result is in far space */
3387 if (isOperandInFarSpace (IC_LEFT (dic)) ||
3388 isOperandInFarSpace (IC_RIGHT (dic)) ||
3389 isOperandInFarSpace (IC_RESULT (dic)) ||
3390 IS_OP_RUONLY (IC_LEFT (dic)) ||
3391 IS_OP_RUONLY (IC_RIGHT (dic)) ||
3392 IS_OP_RUONLY (IC_RESULT (dic)))
3398 OP_SYMBOL (op)->ruonly = 1;
3403 /*-----------------------------------------------------------------*/
3404 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
3405 /*-----------------------------------------------------------------*/
3407 isBitwiseOptimizable (iCode * ic)
3409 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
3410 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
3412 debugLog ("%s\n", __FUNCTION__);
3413 /* bitwise operations are considered optimizable
3414 under the following conditions (Jean-Louis VERN)
3426 if (IS_LITERAL (rtype) ||
3427 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
3433 /*-----------------------------------------------------------------*/
3434 /* packRegsForAccUse - pack registers for acc use */
3435 /*-----------------------------------------------------------------*/
3437 packRegsForAccUse (iCode * ic)
3441 debugLog ("%s\n", __FUNCTION__);
3443 /* result too large for WREG? */
3444 if (getSize (operandType (IC_RESULT (ic))) > 1)
3447 /* We have to make sure that OP_SYMBOL(IC_RESULT(ic))
3448 * is never used as an operand to an instruction that
3449 * cannot have WREG as an operand (e.g. BTFSx cannot
3450 * operate on WREG...
3451 * For now, store all results into proper registers. */
3455 /* if this is an aggregate, e.g. a one byte char array */
3456 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
3459 debugLog (" %s:%d\n", __FUNCTION__,__LINE__);
3461 /* if + or - then it has to be one byte result */
3462 if ((ic->op == '+' || ic->op == '-')
3463 && getSize (operandType (IC_RESULT (ic))) > 1)
3466 debugLog (" %s:%d\n", __FUNCTION__,__LINE__);
3467 /* if shift operation make sure right side is not a literal */
3468 if (ic->op == RIGHT_OP &&
3469 (isOperandLiteral (IC_RIGHT (ic)) ||
3470 getSize (operandType (IC_RESULT (ic))) > 1))
3473 if (ic->op == LEFT_OP &&
3474 (isOperandLiteral (IC_RIGHT (ic)) ||
3475 getSize (operandType (IC_RESULT (ic))) > 1))
3478 if (IS_BITWISE_OP (ic) &&
3479 getSize (operandType (IC_RESULT (ic))) > 1)
3483 /* has only one definition */
3484 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
3487 /* has only one use */
3488 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
3491 /* and the usage immediately follows this iCode */
3492 if (!(uic = hTabItemWithKey (iCodehTab,
3493 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
3496 debugLog (" %s:%d\n", __FUNCTION__,__LINE__);
3497 if (ic->next != uic)
3500 /* if it is a conditional branch then we definitely can */
3504 if (uic->op == JUMPTABLE)
3507 /* if the usage is not is an assignment
3508 or an arithmetic / bitwise / shift operation then not */
3509 if (POINTER_SET (uic) &&
3510 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
3513 debugLog (" %s:%d\n", __FUNCTION__,__LINE__);
3514 if (uic->op != '=' &&
3515 !IS_ARITHMETIC_OP (uic) &&
3516 !IS_BITWISE_OP (uic) &&
3517 uic->op != LEFT_OP &&
3518 uic->op != RIGHT_OP)
3521 debugLog (" %s:%d\n", __FUNCTION__,__LINE__);
3522 /* if used in ^ operation then make sure right is not a
3524 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
3527 /* if shift operation make sure right side is not a literal */
3528 if (uic->op == RIGHT_OP &&
3529 (isOperandLiteral (IC_RIGHT (uic)) ||
3530 getSize (operandType (IC_RESULT (uic))) > 1))
3533 if (uic->op == LEFT_OP &&
3534 (isOperandLiteral (IC_RIGHT (uic)) ||
3535 getSize (operandType (IC_RESULT (uic))) > 1))
3538 /* make sure that the result of this icode is not on the
3539 stack, since acc is used to compute stack offset */
3540 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
3541 OP_SYMBOL (IC_RESULT (uic))->onStack)
3544 /* if either one of them in far space then we cannot */
3545 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
3546 isOperandInFarSpace (IC_LEFT (uic))) ||
3547 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
3548 isOperandInFarSpace (IC_RIGHT (uic))))
3551 /* if the usage has only one operand then we can */
3552 if (IC_LEFT (uic) == NULL ||
3553 IC_RIGHT (uic) == NULL)
3556 /* make sure this is on the left side if not
3557 a '+' since '+' is commutative */
3558 if (ic->op != '+' &&
3559 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
3562 debugLog (" %s:%d\n", __FUNCTION__,__LINE__);
3563 /* if one of them is a literal then we can */
3564 if ( ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
3565 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic)))) &&
3566 (getSize (operandType (IC_RESULT (uic))) <= 1))
3568 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
3572 debugLog (" %s:%d\n", __FUNCTION__,__LINE__);
3573 /* if the other one is not on stack then we can */
3574 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
3575 (IS_ITEMP (IC_RIGHT (uic)) ||
3576 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
3577 !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
3580 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
3581 (IS_ITEMP (IC_LEFT (uic)) ||
3582 (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
3583 !OP_SYMBOL (IC_LEFT (uic))->onStack)))
3589 debugLog ("%s - Yes we are using the accumulator\n", __FUNCTION__);
3590 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
3594 /*-----------------------------------------------------------------*/
3595 /* packForPush - hueristics to reduce iCode for pushing */
3596 /*-----------------------------------------------------------------*/
3598 packForReceive (iCode * ic, eBBlock * ebp)
3602 debugLog ("%s\n", __FUNCTION__);
3603 debugAopGet (" result:", IC_RESULT (ic));
3604 debugAopGet (" left:", IC_LEFT (ic));
3605 debugAopGet (" right:", IC_RIGHT (ic));
3610 for (dic = ic->next; dic; dic = dic->next)
3615 if (IC_LEFT (dic) && (IC_RESULT (ic)->key == IC_LEFT (dic)->key))
3616 debugLog (" used on left\n");
3617 if (IC_RIGHT (dic) && IC_RESULT (ic)->key == IC_RIGHT (dic)->key)
3618 debugLog (" used on right\n");
3619 if (IC_RESULT (dic) && IC_RESULT (ic)->key == IC_RESULT (dic)->key)
3620 debugLog (" used on result\n");
3622 if ((IC_LEFT (dic) && (IC_RESULT (ic)->key == IC_LEFT (dic)->key)) ||
3623 (IC_RESULT (dic) && IC_RESULT (ic)->key == IC_RESULT (dic)->key))
3628 debugLog (" hey we can remove this unnecessary assign\n");
3630 /*-----------------------------------------------------------------*/
3631 /* packForPush - hueristics to reduce iCode for pushing */
3632 /*-----------------------------------------------------------------*/
3634 packForPush (iCode * ic, eBBlock * ebp)
3638 debugLog ("%s\n", __FUNCTION__);
3639 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
3642 /* must have only definition & one usage */
3643 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
3644 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
3647 /* find the definition */
3648 if (!(dic = hTabItemWithKey (iCodehTab,
3649 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
3652 if (dic->op != '=' || POINTER_SET (dic))
3655 /* we now we know that it has one & only one def & use
3656 and the that the definition is an assignment */
3657 IC_LEFT (ic) = IC_RIGHT (dic);
3659 remiCodeFromeBBlock (ebp, dic);
3660 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
3661 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
3664 void printSymType(char * str, sym_link *sl)
3666 debugLog (" %s Symbol type: ",str);
3667 printTypeChain( sl, debugF);
3672 /*-----------------------------------------------------------------*/
3673 /* some debug code to print the symbol S_TYPE. Note that
3674 * the function checkSClass in src/SDCCsymt.c dinks with
3675 * the S_TYPE in ways the PIC port doesn't fully like...*/
3676 /*-----------------------------------------------------------------*/
3677 void isData(sym_link *sl)
3687 for ( ; sl; sl=sl->next) {
3689 switch (SPEC_SCLS(sl)) {
3691 case S_DATA: fprintf (of, "data "); break;
3692 case S_XDATA: fprintf (of, "xdata "); break;
3693 case S_SFR: fprintf (of, "sfr "); break;
3694 case S_SBIT: fprintf (of, "sbit "); break;
3695 case S_CODE: fprintf (of, "code "); break;
3696 case S_IDATA: fprintf (of, "idata "); break;
3697 case S_PDATA: fprintf (of, "pdata "); break;
3698 case S_LITERAL: fprintf (of, "literal "); break;
3699 case S_STACK: fprintf (of, "stack "); break;
3700 case S_XSTACK: fprintf (of, "xstack "); break;
3701 case S_BIT: fprintf (of, "bit "); break;
3702 case S_EEPROM: fprintf (of, "eeprom "); break;
3712 /*-----------------------------------------------------------------*/
3713 /* packRegisters - does some transformations to reduce register */
3715 /*-----------------------------------------------------------------*/
3717 packRegisters (eBBlock * ebp)
3722 debugLog ("%s\n", __FUNCTION__);
3728 /* look for assignments of the form */
3729 /* iTempNN = TRueSym (someoperation) SomeOperand */
3731 /* TrueSym := iTempNN:1 */
3732 for (ic = ebp->sch; ic; ic = ic->next)
3735 /* find assignment of the form TrueSym := iTempNN:1 */
3736 if (ic->op == '=' && !POINTER_SET (ic))
3737 change += packRegsForAssign (ic, ebp);
3741 if (POINTER_SET (ic))
3742 debugLog ("pointer is set\n");
3743 debugAopGet (" result:", IC_RESULT (ic));
3744 debugAopGet (" left:", IC_LEFT (ic));
3745 debugAopGet (" right:", IC_RIGHT (ic));
3754 for (ic = ebp->sch; ic; ic = ic->next) {
3756 if(IS_SYMOP ( IC_LEFT(ic))) {
3757 sym_link *etype = getSpec (operandType (IC_LEFT (ic)));
3759 debugAopGet (" left:", IC_LEFT (ic));
3760 if(IS_PTR_CONST(OP_SYMBOL(IC_LEFT(ic))->type))
3761 debugLog (" is a pointer\n");
3763 if(IS_OP_VOLATILE(IC_LEFT(ic)))
3764 debugLog (" is volatile\n");
3768 printSymType(" ", OP_SYMBOL(IC_LEFT(ic))->type);
3771 if(IS_SYMOP ( IC_RIGHT(ic))) {
3772 debugAopGet (" right:", IC_RIGHT (ic));
3773 printSymType(" ", OP_SYMBOL(IC_RIGHT(ic))->type);
3776 if(IS_SYMOP ( IC_RESULT(ic))) {
3777 debugAopGet (" result:", IC_RESULT (ic));
3778 printSymType(" ", OP_SYMBOL(IC_RESULT(ic))->type);
3781 if (POINTER_SET (ic))
3782 debugLog (" %d - Pointer set\n", __LINE__);
3785 /* Look for two subsequent iCodes with */
3787 /* _c = iTemp & op; */
3788 /* and replace them by */
3791 if ((ic->op == BITWISEAND || ic->op == '|' || ic->op == '^') &&
3793 ic->prev->op == '=' &&
3794 IS_ITEMP (IC_LEFT (ic)) &&
3795 IC_LEFT (ic) == IC_RESULT (ic->prev) &&
3796 isOperandEqual (IC_RESULT(ic), IC_RIGHT(ic->prev)))
3798 iCode* ic_prev = ic->prev;
3799 symbol* prev_result_sym = OP_SYMBOL (IC_RESULT (ic_prev));
3801 ReplaceOpWithCheaperOp (&IC_LEFT (ic), IC_RESULT (ic));
3802 if (IC_RESULT (ic_prev) != IC_RIGHT (ic))
3804 bitVectUnSetBit (OP_USES (IC_RESULT (ic_prev)), ic->key);
3805 if (/*IS_ITEMP (IC_RESULT (ic_prev)) && */
3806 prev_result_sym->liveTo == ic->seq)
3808 prev_result_sym->liveTo = ic_prev->seq;
3811 bitVectSetBit (OP_USES (IC_RESULT (ic)), ic->key);
3813 bitVectSetBit (ic->rlive, IC_RESULT (ic)->key);
3815 if (bitVectIsZero (OP_USES (IC_RESULT (ic_prev))))
3817 bitVectUnSetBit (ic->rlive, IC_RESULT (ic)->key);
3818 bitVectUnSetBit (OP_DEFS (IC_RESULT (ic_prev)), ic_prev->key);
3819 remiCodeFromeBBlock (ebp, ic_prev);
3820 hTabDeleteItem (&iCodehTab, ic_prev->key, ic_prev, DELETE_ITEM, NULL);
3824 /* if this is an itemp & result of a address of a true sym
3825 then mark this as rematerialisable */
3826 if (ic->op == ADDRESS_OF &&
3827 IS_ITEMP (IC_RESULT (ic)) &&
3828 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
3829 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
3830 !OP_SYMBOL (IC_LEFT (ic))->onStack)
3833 debugLog (" %d - %s. result is rematerializable\n", __LINE__,__FUNCTION__);
3835 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
3836 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
3837 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
3841 /* if straight assignment then carry remat flag if
3842 this is the only definition */
3843 if (ic->op == '=' &&
3844 !POINTER_SET (ic) &&
3845 IS_SYMOP (IC_RIGHT (ic)) &&
3846 OP_SYMBOL (IC_RIGHT (ic))->remat &&
3847 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
3849 debugLog (" %d - %s. straight rematerializable\n", __LINE__,__FUNCTION__);
3851 OP_SYMBOL (IC_RESULT (ic))->remat =
3852 OP_SYMBOL (IC_RIGHT (ic))->remat;
3853 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
3854 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
3857 /* if this is a +/- operation with a rematerizable
3858 then mark this as rematerializable as well */
3859 if ((ic->op == '+' || ic->op == '-') &&
3860 (IS_SYMOP (IC_LEFT (ic)) &&
3861 IS_ITEMP (IC_RESULT (ic)) &&
3862 OP_SYMBOL (IC_LEFT (ic))->remat &&
3863 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
3864 IS_OP_LITERAL (IC_RIGHT (ic))))
3866 debugLog (" %d - %s. rematerializable because op is +/-\n", __LINE__,__FUNCTION__);
3868 operandLitValue (IC_RIGHT (ic));
3869 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
3870 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
3871 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
3874 /* mark the pointer usages */
3875 if (POINTER_SET (ic) && IS_SYMOP(IC_RESULT(ic)))
3877 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
3878 debugLog (" marking as a pointer (set) =>");
3879 debugAopGet (" result:", IC_RESULT (ic));
3881 if (POINTER_GET (ic) && IS_SYMOP(IC_LEFT(ic)))
3883 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
3884 debugLog (" marking as a pointer (get) =>");
3885 debugAopGet (" left:", IC_LEFT (ic));
3890 /* if we are using a symbol on the stack
3891 then we should say pic14_ptrRegReq */
3892 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
3893 pic14_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ||
3894 OP_SYMBOL (IC_COND (ic))->iaccess) ? 1 : 0);
3895 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
3896 pic14_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ||
3897 OP_SYMBOL (IC_JTCOND (ic))->iaccess) ? 1 : 0);
3900 if (IS_SYMOP (IC_LEFT (ic)))
3901 pic14_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ||
3902 OP_SYMBOL (IC_LEFT (ic))->iaccess) ? 1 : 0);
3903 if (IS_SYMOP (IC_RIGHT (ic)))
3904 pic14_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ||
3905 OP_SYMBOL (IC_RIGHT (ic))->iaccess) ? 1 : 0);
3906 if (IS_SYMOP (IC_RESULT (ic)))
3907 pic14_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ||
3908 OP_SYMBOL (IC_RESULT (ic))->iaccess) ? 1 : 0);
3911 debugLog (" %d - pointer reg req = %d\n", __LINE__,pic14_ptrRegReq);
3915 /* if the condition of an if instruction
3916 is defined in the previous instruction then
3917 mark the itemp as a conditional */
3918 if ((IS_CONDITIONAL (ic) ||
3919 ((ic->op == BITWISEAND ||
3922 isBitwiseOptimizable (ic))) &&
3923 ic->next && ic->next->op == IFX &&
3924 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
3925 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
3928 debugLog (" %d\n", __LINE__);
3929 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
3933 /* reduce for support function calls */
3934 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
3935 packRegsForSupport (ic, ebp);
3937 /* if a parameter is passed, it's in W, so we may not
3938 need to place a copy in a register */
3939 if (ic->op == RECEIVE)
3940 packForReceive (ic, ebp);
3942 /* some cases the redundant moves can
3943 can be eliminated for return statements */
3944 if ((ic->op == RETURN || ic->op == SEND) &&
3945 !isOperandInFarSpace (IC_LEFT (ic)) &&
3947 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
3949 /* if pointer set & left has a size more than
3950 one and right is not in far space */
3951 if (POINTER_SET (ic) &&
3952 !isOperandInFarSpace (IC_RIGHT (ic)) &&
3953 IS_SYMOP(IC_RESULT(ic)) &&
3954 !OP_SYMBOL (IC_RESULT (ic))->remat &&
3955 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
3956 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
3958 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
3960 /* if pointer get */
3961 if (POINTER_GET (ic) &&
3962 !isOperandInFarSpace (IC_RESULT (ic)) &&
3963 IS_SYMOP(IC_LEFT(ic)) &&
3964 !OP_SYMBOL (IC_LEFT (ic))->remat &&
3965 !IS_OP_RUONLY (IC_RESULT (ic)) &&
3966 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
3968 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
3971 /* if this is cast for intergral promotion then
3972 check if only use of the definition of the
3973 operand being casted/ if yes then replace
3974 the result of that arithmetic operation with
3975 this result and get rid of the cast */
3976 if (ic->op == CAST) {
3978 sym_link *fromType = operandType (IC_RIGHT (ic));
3979 sym_link *toType = operandType (IC_LEFT (ic));
3981 debugLog (" %d - casting\n", __LINE__);
3983 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
3984 getSize (fromType) != getSize (toType)) {
3987 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
3990 if (IS_ARITHMETIC_OP (dic)) {
3992 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
3993 IC_RESULT (dic) = IC_RESULT (ic);
3994 remiCodeFromeBBlock (ebp, ic);
3995 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3996 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
3997 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
4001 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
4005 /* if the type from and type to are the same
4006 then if this is the only use then packit */
4007 if (compareType (operandType (IC_RIGHT (ic)),
4008 operandType (IC_LEFT (ic))) == 1) {
4010 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
4013 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
4014 IC_RESULT (dic) = IC_RESULT (ic);
4015 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
4016 remiCodeFromeBBlock (ebp, ic);
4017 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
4018 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
4026 iTempNN := (some variable in farspace) V1
4031 if (ic->op == IPUSH)
4033 packForPush (ic, ebp);
4037 /* pack registers for accumulator use, when the
4038 result of an arithmetic or bit wise operation
4039 has only one use, that use is immediately following
4040 the defintion and the using iCode has only one
4041 operand or has two operands but one is literal &
4042 the result of that operation is not on stack then
4043 we can leave the result of this operation in acc:b
4045 if ((IS_ARITHMETIC_OP (ic)
4047 || IS_BITWISE_OP (ic)
4049 || ic->op == LEFT_OP || ic->op == RIGHT_OP
4052 IS_ITEMP (IC_RESULT (ic)) &&
4053 getSize (operandType (IC_RESULT (ic))) <= 2)
4055 packRegsForAccUse (ic);
4061 dumpEbbsToDebug (eBBlock ** ebbs, int count)
4065 if (!debug || !debugF)
4068 for (i = 0; i < count; i++)
4070 fprintf (debugF, "\n----------------------------------------------------------------\n");
4071 fprintf (debugF, "Basic Block %s : loop Depth = %d noPath = %d , lastinLoop = %d\n",
4072 ebbs[i]->entryLabel->name,
4075 ebbs[i]->isLastInLoop);
4076 fprintf (debugF, "depth 1st num %d : bbnum = %d 1st iCode = %d , last iCode = %d\n",
4081 fprintf (debugF, "visited %d : hasFcall = %d\n",
4085 fprintf (debugF, "\ndefines bitVector :");
4086 bitVectDebugOn (ebbs[i]->defSet, debugF);
4087 fprintf (debugF, "\nlocal defines bitVector :");
4088 bitVectDebugOn (ebbs[i]->ldefs, debugF);
4089 fprintf (debugF, "\npointers Set bitvector :");
4090 bitVectDebugOn (ebbs[i]->ptrsSet, debugF);
4091 fprintf (debugF, "\nin pointers Set bitvector :");
4092 bitVectDebugOn (ebbs[i]->inPtrsSet, debugF);
4093 fprintf (debugF, "\ninDefs Set bitvector :");
4094 bitVectDebugOn (ebbs[i]->inDefs, debugF);
4095 fprintf (debugF, "\noutDefs Set bitvector :");
4096 bitVectDebugOn (ebbs[i]->outDefs, debugF);
4097 fprintf (debugF, "\nusesDefs Set bitvector :");
4098 bitVectDebugOn (ebbs[i]->usesDefs, debugF);
4099 fprintf (debugF, "\n----------------------------------------------------------------\n");
4100 printiCChain (ebbs[i]->sch, debugF);
4103 /*-----------------------------------------------------------------*/
4104 /* assignRegisters - assigns registers to each live range as need */
4105 /*-----------------------------------------------------------------*/
4107 pic14_assignRegisters (ebbIndex * ebbi)
4109 eBBlock ** ebbs = ebbi->bbOrder;
4110 int count = ebbi->count;
4114 debugLog ("<><><><><><><><><><><><><><><><><>\nstarting\t%s:%s", __FILE__, __FUNCTION__);
4115 debugLog ("\nebbs before optimizing:\n");
4116 dumpEbbsToDebug (ebbs, count);
4118 setToNull ((void *) &_G.funcrUsed);
4119 pic14_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
4122 /* change assignments this will remove some
4123 live ranges reducing some register pressure */
4124 for (i = 0; i < count; i++)
4125 packRegisters (ebbs[i]);
4132 debugLog("dir registers allocated so far:\n");
4133 reg = hTabFirstItem(dynDirectRegNames, &hkey);
4136 debugLog(" -- #%d reg = %s key %d, rIdx = %d, size %d\n",i++,reg->name,hkey, reg->rIdx,reg->size);
4137 reg = hTabNextItem(dynDirectRegNames, &hkey);
4142 if (options.dump_pack)
4143 dumpEbbsToFileExt (DUMP_PACK, ebbi);
4145 /* first determine for each live range the number of
4146 registers & the type of registers required for each */
4149 /* and serially allocate registers */
4150 serialRegAssign (ebbs, count);
4152 /* if stack was extended then tell the user */
4155 /* werror(W_TOOMANY_SPILS,"stack", */
4156 /* _G.stackExtend,currFunc->name,""); */
4162 /* werror(W_TOOMANY_SPILS,"data space", */
4163 /* _G.dataExtend,currFunc->name,""); */
4167 /* after that create the register mask
4168 for each of the instruction */
4169 createRegMask (ebbs, count);
4171 /* redo that offsets for stacked automatic variables */
4172 redoStackOffsets ();
4174 if (options.dump_rassgn)
4175 dumpEbbsToFileExt (DUMP_RASSGN, ebbi);
4177 /* now get back the chain */
4178 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
4180 debugLog ("ebbs after optimizing:\n");
4181 dumpEbbsToDebug (ebbs, count);
4186 /* free up any _G.stackSpil locations allocated */
4187 applyToSet (_G.stackSpil, deallocStackSpil);
4189 setToNull ((void *) &_G.stackSpil);
4190 setToNull ((void *) &_G.spiltSet);
4191 /* mark all registers as free */
4192 //pic14_freeAllRegs ();
4194 debugLog ("leaving\n<><><><><><><><><><><><><><><><><>\n");