1 /*------------------------------------------------------------------------
3 SDCCralloc.c - source file for register allocation. (8051) specific
5 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
6 Added Pic Port T.scott Dattalo scott@dattalo.com (2000)
8 This program is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 2, or (at your option) any
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 In other words, you are welcome to use, share and improve this program.
23 You are forbidden to forbid anyone else to use, share and improve
24 what you give them. Help stamp out software-hoarding!
25 -------------------------------------------------------------------------*/
32 set *dynAllocRegs=NULL;
33 set *dynStackRegs=NULL;
34 set *dynProcessorRegs=NULL;
35 set *dynDirectRegs=NULL;
36 set *dynDirectBitRegs=NULL;
37 set *dynInternalRegs=NULL;
40 #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 /*-----------------------------------------------------------------*/
62 bitVect *funcrUsed; /* registers used in a function */
68 static int pic14_ptrRegReq; /* one byte pointer register required */
70 static hTab *dynDirectRegNames= NULL;
71 // static hTab *regHash = NULL; /* a hash table containing ALL registers */
73 static int dynrIdx = 0x1000;
75 int Gstack_base_addr=0; /* The starting address of registers that
76 * are used to pass and return parameters */
77 static int Gstack_size = 0;
79 static int debug = 0; // should be 0 when committed, creates .d files
80 static FILE *debugF = NULL;
82 /*-----------------------------------------------------------------*/
83 /* debugLog - open a file for debugging information */
84 /*-----------------------------------------------------------------*/
86 debugLog (char *fmt,...)
88 static int append = 0; // First time through, open the file without append.
91 //char *bufferP=buffer;
94 if (!debug || !dstFileName)
100 /* create the file name */
101 strcpy (buffer, dstFileName);
102 strcat (buffer, ".d");
104 if (!(debugF = fopen (buffer, (append ? "a+" : "w"))))
106 werror (E_FILE_OPEN_ERR, buffer);
109 append = 1; // Next time debugLog is called, we'll append the debug info
114 vsprintf (buffer, fmt, ap);
117 fprintf (debugF, "%s", buffer);
118 //if (options.verbose) fprintf (stderr, "%s: %s", __FUNCTION__, buffer);
125 fputc ('\n', debugF);
127 /*-----------------------------------------------------------------*/
128 /* pic14_debugLogClose - closes the debug log file (if opened) */
129 /*-----------------------------------------------------------------*/
131 pic14_debugLogClose (void)
141 debugAopGet (char *str, operand * op)
143 if (!debug) return NULL;
145 if (str) debugLog (str);
147 printOperand (op, debugF);
154 decodeOp (unsigned int op)
157 if (op < 128 && op > ' ')
159 buffer[0] = (op & 0xff);
166 case IDENTIFIER: return "IDENTIFIER";
167 case TYPE_NAME: return "TYPE_NAME";
168 case CONSTANT: return "CONSTANT";
169 case STRING_LITERAL: return "STRING_LITERAL";
170 case SIZEOF: return "SIZEOF";
171 case PTR_OP: return "PTR_OP";
172 case INC_OP: return "INC_OP";
173 case DEC_OP: return "DEC_OP";
174 case LEFT_OP: return "LEFT_OP";
175 case RIGHT_OP: return "RIGHT_OP";
176 case LE_OP: return "LE_OP";
177 case GE_OP: return "GE_OP";
178 case EQ_OP: return "EQ_OP";
179 case NE_OP: return "NE_OP";
180 case AND_OP: return "AND_OP";
181 case OR_OP: return "OR_OP";
182 case MUL_ASSIGN: return "MUL_ASSIGN";
183 case DIV_ASSIGN: return "DIV_ASSIGN";
184 case MOD_ASSIGN: return "MOD_ASSIGN";
185 case ADD_ASSIGN: return "ADD_ASSIGN";
186 case SUB_ASSIGN: return "SUB_ASSIGN";
187 case LEFT_ASSIGN: return "LEFT_ASSIGN";
188 case RIGHT_ASSIGN: return "RIGHT_ASSIGN";
189 case AND_ASSIGN: return "AND_ASSIGN";
190 case XOR_ASSIGN: return "XOR_ASSIGN";
191 case OR_ASSIGN: return "OR_ASSIGN";
192 case TYPEDEF: return "TYPEDEF";
193 case EXTERN: return "EXTERN";
194 case STATIC: return "STATIC";
195 case AUTO: return "AUTO";
196 case REGISTER: return "REGISTER";
197 case CODE: return "CODE";
198 case EEPROM: return "EEPROM";
199 case INTERRUPT: return "INTERRUPT";
200 case SFR: return "SFR";
201 case AT: return "AT";
202 case SBIT: return "SBIT";
203 case REENTRANT: return "REENTRANT";
204 case USING: return "USING";
205 case XDATA: return "XDATA";
206 case DATA: return "DATA";
207 case IDATA: return "IDATA";
208 case PDATA: return "PDATA";
209 case VAR_ARGS: return "VAR_ARGS";
210 case CRITICAL: return "CRITICAL";
211 case NONBANKED: return "NONBANKED";
212 case BANKED: return "BANKED";
213 case CHAR: return "CHAR";
214 case SHORT: return "SHORT";
215 case INT: return "INT";
216 case LONG: return "LONG";
217 case SIGNED: return "SIGNED";
218 case UNSIGNED: return "UNSIGNED";
219 case FLOAT: return "FLOAT";
220 case DOUBLE: return "DOUBLE";
221 case CONST: return "CONST";
222 case VOLATILE: return "VOLATILE";
223 case VOID: return "VOID";
224 case BIT: return "BIT";
225 case STRUCT: return "STRUCT";
226 case UNION: return "UNION";
227 case ENUM: return "ENUM";
228 case RANGE: return "RANGE";
229 case FAR: return "FAR";
230 case CASE: return "CASE";
231 case DEFAULT: return "DEFAULT";
232 case IF: return "IF";
233 case ELSE: return "ELSE";
234 case SWITCH: return "SWITCH";
235 case WHILE: return "WHILE";
236 case DO: return "DO";
237 case FOR: return "FOR";
238 case GOTO: return "GOTO";
239 case CONTINUE: return "CONTINUE";
240 case BREAK: return "BREAK";
241 case RETURN: return "RETURN";
242 case INLINEASM: return "INLINEASM";
243 case IFX: return "IFX";
244 case ADDRESS_OF: return "ADDRESS_OF";
245 case GET_VALUE_AT_ADDRESS: return "GET_VALUE_AT_ADDRESS";
246 case SPIL: return "SPIL";
247 case UNSPIL: return "UNSPIL";
248 case GETHBIT: return "GETHBIT";
249 case BITWISEAND: return "BITWISEAND";
250 case UNARYMINUS: return "UNARYMINUS";
251 case IPUSH: return "IPUSH";
252 case IPOP: return "IPOP";
253 case PCALL: return "PCALL";
254 case ENDFUNCTION: return "ENDFUNCTION";
255 case JUMPTABLE: return "JUMPTABLE";
256 case RRC: return "RRC";
257 case RLC: return "RLC";
258 case CAST: return "CAST";
259 case CALL: return "CALL";
260 case PARAM: return "PARAM ";
261 case NULLOP: return "NULLOP";
262 case BLOCK: return "BLOCK";
263 case LABEL: return "LABEL";
264 case RECEIVE: return "RECEIVE";
265 case SEND: return "SEND";
267 sprintf (buffer, "unknown op %d %c", op, op & 0xff);
270 /*-----------------------------------------------------------------*/
271 /*-----------------------------------------------------------------*/
273 debugLogRegType (short type)
278 case REG_GPR: return "REG_GPR";
279 case REG_PTR: return "REG_PTR";
280 case REG_CND: return "REG_CND";
283 sprintf (buffer, "unknown reg type %d", type);
287 /*-----------------------------------------------------------------*/
288 /*-----------------------------------------------------------------*/
289 static int regname2key(char const *name)
298 key += (*name++) + 1;
302 return ( (key + (key >> 4) + (key>>8)) & 0x3f);
306 /*-----------------------------------------------------------------*/
307 /* regWithIdx - Search through a set of registers that matches idx */
308 /*-----------------------------------------------------------------*/
310 regWithIdx (set *dRegs, int idx, int fixed)
314 for (dReg = setFirstItem(dRegs) ; dReg ;
315 dReg = setNextItem(dRegs)) {
317 if(idx == dReg->rIdx && (fixed == (int)dReg->isFixed)) {
318 while (dReg->reg_alias) dReg = dReg->reg_alias;
326 /*-----------------------------------------------------------------*/
327 /* newReg - allocate and init memory for a new register */
328 /*-----------------------------------------------------------------*/
329 static regs* newReg(short type, PIC_OPTYPE pc_type, int rIdx, char *name, int size, int alias)
332 regs *dReg, *reg_alias;
334 /* check whether a matching register already exists */
335 dReg = dirregWithName( name );
337 //printf( "%s: already present: %s\n", __FUNCTION__, name );
341 // check whether a register at that location exists
342 reg_alias = regWithIdx( dynDirectRegs, rIdx, 0 );
343 if (!reg_alias) reg_alias = regWithIdx( dynDirectRegs, rIdx, 1 );
345 // create a new register
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 = reg_alias;
371 dReg->reglives.usedpFlows = newSet();
372 dReg->reglives.assignedpFlows = newSet();
373 if (type != REG_STK) hTabAddItem(&dynDirectRegNames, regname2key(dReg->name), dReg);
374 debugLog( "%s: Created register %s.\n", __FUNCTION__, dReg->name);
379 /*-----------------------------------------------------------------*/
380 /* regWithName - Search through a set of registers that matches name */
381 /*-----------------------------------------------------------------*/
383 regWithName (set *dRegs, const char *name)
387 for (dReg = setFirstItem(dRegs) ; dReg ;
388 dReg = setNextItem(dRegs)) {
390 if((strcmp(name,dReg->name)==0)) {
398 /*-----------------------------------------------------------------*/
399 /* regWithName - Search for a registers that matches name */
400 /*-----------------------------------------------------------------*/
402 regFindWithName (const char *name)
406 if( (dReg = regWithName ( dynDirectRegs, name)) != NULL ) {
407 debugLog ("Found a Direct Register!\n");
410 if( (dReg = regWithName ( dynDirectBitRegs, name)) != NULL) {
411 debugLog ("Found a Direct Bit Register!\n");
415 if (*name=='_') name++; // Step passed '_'
417 if( (dReg = regWithName ( dynAllocRegs, name)) != NULL) {
418 debugLog ("Found a Dynamic Register!\n");
421 if( (dReg = regWithName ( dynProcessorRegs, name)) != NULL) {
422 debugLog ("Found a Processor Register!\n");
425 if( (dReg = regWithName ( dynInternalRegs, name)) != NULL) {
426 debugLog ("Found an Internal Register!\n");
429 if( (dReg = regWithName ( dynStackRegs, name)) != NULL) {
430 debugLog ("Found an Stack Register!\n");
437 /*-----------------------------------------------------------------*/
438 /* regFindFree - Search for a free register in a set of registers */
439 /*-----------------------------------------------------------------*/
441 regFindFree (set *dRegs)
445 for (dReg = setFirstItem(dRegs) ; dReg ;
446 dReg = setNextItem(dRegs)) {
454 /*-----------------------------------------------------------------*/
455 /* initStack - allocate registers for a pseudo stack */
456 /*-----------------------------------------------------------------*/
457 void initStack(int base_address, int size, int shared)
463 pic = pic14_getPIC();
464 Gstack_base_addr = base_address;
466 //fprintf(stderr,"initStack [base:0x%02x, size:%d]\n", base_address, size);
468 for(i = 0; i<size; i++) {
471 SNPRINTF(&buffer[0], 16, "STK%02d", i);
472 // multi-bank device, sharebank prohibited by user
473 r = newReg(REG_STK, PO_GPR_TEMP, base_address--, buffer, 1, shared ? (pic ? pic->bankMask : 0x180) : 0x0);
478 addSet(&dynStackRegs,r);
482 /*-----------------------------------------------------------------*
483 *-----------------------------------------------------------------*/
485 allocProcessorRegister(int rIdx, char * name, short po_type, int alias)
488 //fprintf(stderr,"allocProcessorRegister %s addr =0x%x\n",name,rIdx);
489 return addSet(&dynProcessorRegs,newReg(REG_SFR, po_type, rIdx, name,1,alias));
492 /*-----------------------------------------------------------------*
493 *-----------------------------------------------------------------*/
496 allocInternalRegister(int rIdx, char * name, PIC_OPTYPE po_type, int alias)
498 regs * reg = newReg(REG_GPR, po_type, rIdx, name,1,alias);
500 //fprintf(stderr,"allocInternalRegister %s addr =0x%x\n",name,rIdx);
503 return addSet(&dynInternalRegs,reg);
508 /*-----------------------------------------------------------------*/
509 /* allocReg - allocates register of given type */
510 /*-----------------------------------------------------------------*/
512 allocReg (short type)
516 debugLog ("%s of type %s\n", __FUNCTION__, debugLogRegType (type));
517 //fprintf(stderr,"allocReg\n");
519 reg = pic14_findFreeReg (type);
527 //return addSet(&dynAllocRegs,newReg(REG_GPR, PO_GPR_TEMP,dynrIdx++,NULL,1,0));
532 /*-----------------------------------------------------------------*/
533 /* dirregWithName - search for register by name */
534 /*-----------------------------------------------------------------*/
536 dirregWithName (char *name)
544 /* hash the name to get a key */
546 hkey = regname2key(name);
548 reg = hTabFirstItemWK(dynDirectRegNames, hkey);
552 if(STRCASECMP(reg->name, name) == 0) {
553 // handle registers with multiple names
554 while (reg->reg_alias) reg = reg->reg_alias;
558 reg = hTabNextItemWK (dynDirectRegNames);
562 return NULL; // name wasn't found in the hash table
565 /*-----------------------------------------------------------------*/
566 /* allocNewDirReg - allocates a new register of given type */
567 /*-----------------------------------------------------------------*/
569 allocNewDirReg (sym_link *symlnk,const char *name)
573 sym_link *spec = getSpec (symlnk);
575 /* if this is at an absolute address, then get the address. */
576 if (SPEC_ABSA (spec) ) {
577 address = SPEC_ADDR (spec);
578 //fprintf(stderr,"reg %s is at an absolute address: 0x%03x\n",name,address);
581 /* Register wasn't found in hash, so let's create
582 * a new one and put it in the hash table AND in the
583 * dynDirectRegNames set */
584 if (IS_CONFIG_ADDRESS(address)) {
585 debugLog (" -- %s is declared at address 0x2007\n",name);
590 if (IS_BITVAR (spec))
597 reg = newReg(REG_GPR, PO_DIR, idx, (char*)name,getSize (symlnk),0 );
598 debugLog (" -- added %s to hash, size = %d\n", (char*)name,reg->size);
600 if (SPEC_ABSA (spec) ) {
604 if (IS_BITVAR (spec)) {
605 addSet(&dynDirectBitRegs, reg);
608 addSet(&dynDirectRegs, reg);
610 if (!IS_STATIC (spec)) {
613 if (IS_EXTERN (spec)) {
619 if (address && reg) {
621 reg->address = address;
622 debugLog (" -- and it is at a fixed address 0x%02x\n",reg->address);
628 /*-----------------------------------------------------------------*/
629 /* allocDirReg - allocates register of given type */
630 /*-----------------------------------------------------------------*/
632 allocDirReg (operand *op )
639 debugLog ("%s BAD, op is NULL\n", __FUNCTION__);
643 name = OP_SYMBOL (op)->rname[0] ? OP_SYMBOL (op)->rname : OP_SYMBOL (op)->name;
645 /* If the symbol is at a fixed address, then remove the leading underscore
646 * from the name. This is hack to allow the .asm include file named registers
647 * to match the .c declared register names */
649 //if (SPEC_ABSA ( OP_SYM_ETYPE(op)) && (*name == '_'))
652 debugLog ("%s symbol name %s\n", __FUNCTION__,name);
654 if(SPEC_CONST ( OP_SYM_ETYPE(op)) && (IS_CHAR ( OP_SYM_ETYPE(op)) )) {
655 debugLog(" %d const char\n",__LINE__);
656 debugLog(" value = %s \n",SPEC_CVAL( OP_SYM_ETYPE(op)));
659 debugLog(" %d storage class %d \n",__LINE__,SPEC_SCLS( OP_SYM_ETYPE(op)));
660 if (IS_CODE ( OP_SYM_ETYPE(op)) )
661 debugLog(" %d code space\n",__LINE__);
663 if (IS_INTEGRAL ( OP_SYM_ETYPE(op)) )
664 debugLog(" %d integral\n",__LINE__);
665 if (IS_LITERAL ( OP_SYM_ETYPE(op)) )
666 debugLog(" %d literal\n",__LINE__);
667 if (IS_SPEC ( OP_SYM_ETYPE(op)) )
668 debugLog(" %d specifier\n",__LINE__);
669 debugAopGet(NULL, op);
672 if (IS_CODE ( OP_SYM_ETYPE(op)) )
675 /* First, search the hash table to see if there is a register with this name */
676 if (SPEC_ABSA ( OP_SYM_ETYPE(op)) && !(IS_BITVAR (OP_SYM_ETYPE(op))) ) {
677 reg = regWithIdx (dynProcessorRegs, SPEC_ADDR ( OP_SYM_ETYPE(op)), 1);
680 fprintf(stderr,"ralloc %s is at fixed address but not a processor reg, addr=0x%x\n",
681 name, SPEC_ADDR ( OP_SYM_ETYPE(op)));
683 fprintf(stderr,"ralloc %s at fixed address has already been declared, addr=0x%x\n",
684 name, SPEC_ADDR ( OP_SYM_ETYPE(op)));
687 //fprintf(stderr,"ralloc:%d %s \n", __LINE__,name);
689 reg = dirregWithName(name);
696 /* if this is at an absolute address, then get the address. */
697 if (SPEC_ABSA ( OP_SYM_ETYPE(op)) ) {
698 address = SPEC_ADDR ( OP_SYM_ETYPE(op));
699 //fprintf(stderr,"reg %s is at an absolute address: 0x%03x\n",name,address);
702 /* Register wasn't found in hash, so let's create
703 * a new one and put it in the hash table AND in the
704 * dynDirectRegNames set */
705 if(!IS_CONFIG_ADDRESS(address)) {
706 //fprintf(stderr,"allocating new reg %s\n",name);
708 reg = newReg(REG_GPR, PO_DIR, dynrIdx++, name,getSize (OP_SYMBOL (op)->type),0 );
709 debugLog (" -- added %s to hash, size = %d\n", name,reg->size);
711 //hTabAddItem(&dynDirectRegNames, regname2key(name), reg);
713 if (SPEC_ABSA ( OP_SYM_ETYPE(op)) ) {
715 //fprintf(stderr, " ralloc.c at fixed address: %s - changing to REG_SFR\n",name);
719 if (IS_BITVAR (OP_SYM_ETYPE(op))) {
720 addSet(&dynDirectBitRegs, reg);
723 addSet(&dynDirectRegs, reg);
725 if (!IS_STATIC (OP_SYM_ETYPE(op))) {
728 if (IS_EXTERN (OP_SYM_ETYPE(op))) {
734 debugLog (" -- %s is declared at address 0x2007\n",name);
739 if (SPEC_ABSA ( OP_SYM_ETYPE(op)) ) {
741 reg->address = SPEC_ADDR ( OP_SYM_ETYPE(op));
742 debugLog (" -- and it is at a fixed address 0x%02x\n",reg->address);
747 if (SPEC_ABSA ( OP_SYM_ETYPE(op)) ) {
749 reg->address = SPEC_ADDR ( OP_SYM_ETYPE(op));
750 debugLog (" -- and it is at a fixed address 0x%02x\n",reg->address);
753 allocNewDirReg (OP_SYM_TYPE(op),name);
760 /*-----------------------------------------------------------------*/
761 /* allocRegByName - allocates register with given name */
762 /*-----------------------------------------------------------------*/
764 allocRegByName (char *name, int size)
770 fprintf(stderr, "%s - allocating a NULL register\n",__FUNCTION__);
774 /* First, search the hash table to see if there is a register with this name */
775 reg = dirregWithName(name);
781 /* Register wasn't found in hash, so let's create
782 * a new one and put it in the hash table AND in the
783 * dynDirectRegNames set */
784 //fprintf (stderr,"%s symbol name %s, size:%d\n", __FUNCTION__,name,size);
785 reg = newReg(REG_GPR, PO_DIR, dynrIdx++, name,size,0 );
786 for (sym = setFirstItem(sfr->syms); sym; sym = setNextItem(sfr->syms)) {
787 if (strcmp(reg->name+1,sym->name)==0) {
788 unsigned a = SPEC_ADDR(sym->etype);
792 if (!IS_STATIC (sym->etype)) {
795 if (IS_EXTERN (sym->etype)) {
798 if (IS_BITVAR (sym->etype))
805 for (sym = setFirstItem(data->syms); sym; sym = setNextItem(data->syms)) {
806 if (strcmp(reg->name+1,sym->name)==0) {
807 unsigned a = SPEC_ADDR(sym->etype);
809 if (!IS_STATIC (sym->etype)) {
812 if (IS_EXTERN (sym->etype)) {
815 if (IS_BITVAR (sym->etype))
823 debugLog (" -- added %s to hash, size = %d\n", name,reg->size);
825 //hTabAddItem(&dynDirectRegNames, regname2key(name), reg);
826 if (reg->isBitField) {
827 addSet(&dynDirectBitRegs, reg);
829 addSet(&dynDirectRegs, reg);
835 /*-----------------------------------------------------------------*/
836 /* RegWithIdx - returns pointer to register with index number */
837 /*-----------------------------------------------------------------*/
839 typeRegWithIdx (int idx, int type, int fixed)
844 debugLog ("%s - requesting index = 0x%x\n", __FUNCTION__,idx);
849 if( (dReg = regWithIdx ( dynAllocRegs, idx, fixed)) != NULL) {
851 debugLog ("Found a Dynamic Register!\n");
854 if( (dReg = regWithIdx ( dynDirectRegs, idx, fixed)) != NULL ) {
855 debugLog ("Found a Direct Register!\n");
861 if( (dReg = regWithIdx ( dynStackRegs, idx, 0)) != NULL ) {
862 debugLog ("Found a Stack Register!\n");
865 if( (dReg = regWithIdx ( dynStackRegs, idx, 1)) != NULL ) {
866 debugLog ("Found a Stack Register!\n");
870 werror (E_STACK_OUT, "Register");
871 /* return an existing register just to avoid the SDCC crash */
872 return regWithIdx ( dynStackRegs, 0x7f, 0);
876 if( (dReg = regWithIdx ( dynProcessorRegs, idx, fixed)) != NULL ) {
877 debugLog ("Found a Processor Register!\n");
891 /*-----------------------------------------------------------------*/
892 /* pic14_regWithIdx - returns pointer to register with index number*/
893 /*-----------------------------------------------------------------*/
895 pic14_regWithIdx (int idx)
899 if( (dReg = typeRegWithIdx(idx,REG_GPR,0)) != NULL)
902 if( (dReg = typeRegWithIdx(idx,REG_SFR,0)) != NULL)
908 /*-----------------------------------------------------------------*/
909 /* pic14_regWithIdx - returns pointer to register with index number */
910 /*-----------------------------------------------------------------*/
912 pic14_allocWithIdx (int idx)
917 debugLog ("%s - allocating with index = 0x%x\n", __FUNCTION__,idx);
919 if( (dReg = regWithIdx ( dynAllocRegs, idx,0)) != NULL) {
921 debugLog ("Found a Dynamic Register!\n");
922 } else if( (dReg = regWithIdx ( dynStackRegs, idx,0)) != NULL ) {
923 debugLog ("Found a Stack Register!\n");
924 } else if( (dReg = regWithIdx ( dynProcessorRegs, idx,0)) != NULL ) {
925 debugLog ("Found a Processor Register!\n");
926 } else if( (dReg = regWithIdx ( dynInternalRegs, idx,0)) != NULL ) {
927 debugLog ("Found an Internal Register!\n");
928 } else if( (dReg = regWithIdx ( dynInternalRegs, idx,1)) != NULL ) {
929 debugLog ("Found an Internal Register!\n");
932 debugLog ("Dynamic Register not found\n");
935 //fprintf(stderr,"%s %d - requested register: 0x%x\n",__FUNCTION__,__LINE__,idx);
936 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
937 "regWithIdx not found");
947 /*-----------------------------------------------------------------*/
948 /*-----------------------------------------------------------------*/
950 pic14_findFreeReg(short type)
957 if((dReg = regFindFree(dynAllocRegs)) != NULL)
959 return addSet(&dynAllocRegs,newReg(REG_GPR, PO_GPR_TEMP,dynrIdx++,NULL,1,0));
963 if((dReg = regFindFree(dynStackRegs)) != NULL)
975 /*-----------------------------------------------------------------*/
976 /* freeReg - frees a register */
977 /*-----------------------------------------------------------------*/
981 debugLog ("%s\n", __FUNCTION__);
986 /*-----------------------------------------------------------------*/
987 /* nFreeRegs - returns number of free registers */
988 /*-----------------------------------------------------------------*/
992 /* dynamically allocate as many as we need and worry about
993 * fitting them into a PIC later */
1000 debugLog ("%s\n", __FUNCTION__);
1001 for (i = 0; i < pic14_nRegs; i++)
1002 if (regspic14[i].isFree && regspic14[i].type == type)
1008 /*-----------------------------------------------------------------*/
1009 /* nfreeRegsType - free registers with type */
1010 /*-----------------------------------------------------------------*/
1012 nfreeRegsType (int type)
1015 debugLog ("%s\n", __FUNCTION__);
1016 if (type == REG_PTR)
1018 if ((nfr = nFreeRegs (type)) == 0)
1019 return nFreeRegs (REG_GPR);
1022 return nFreeRegs (type);
1025 static void packBits(set *bregs)
1029 regs *bitfield=NULL;
1030 regs *relocbitfield=NULL;
1036 for (regset = bregs ; regset ;
1037 regset = regset->next) {
1039 breg = regset->item;
1040 breg->isBitField = 1;
1041 //fprintf(stderr,"bit reg: %s\n",breg->name);
1044 //fprintf(stderr,"packing bit at fixed address = 0x%03x\n",breg->address);
1046 bitfield = typeRegWithIdx (breg->address >> 3, -1 , 1);
1047 breg->rIdx = breg->address & 7;
1048 breg->address >>= 3;
1051 //sprintf (buffer, "fbitfield%02x", breg->address);
1052 sprintf (buffer, "0x%02x", breg->address);
1053 //fprintf(stderr,"new bit field\n");
1054 bitfield = newReg(REG_SFR, PO_GPR_BIT,breg->address,buffer,1,0);
1055 bitfield->isBitField = 1;
1056 bitfield->isFixed = 1;
1057 bitfield->address = breg->address;
1058 //addSet(&dynDirectRegs,bitfield);
1059 addSet(&dynInternalRegs,bitfield);
1060 //hTabAddItem(&dynDirectRegNames, regname2key(buffer), bitfield);
1062 //fprintf(stderr," which is occupied by %s (addr = %d)\n",bitfield->name,bitfield->address);
1065 breg->reg_alias = bitfield;
1069 if(!relocbitfield || bit_no >7) {
1072 sprintf (buffer, "bitfield%d", byte_no);
1073 //fprintf(stderr,"new relocatable bit field\n");
1074 relocbitfield = newReg(REG_GPR, PO_GPR_BIT,dynrIdx++,buffer,1,0);
1075 relocbitfield->isBitField = 1;
1076 //addSet(&dynDirectRegs,relocbitfield);
1077 addSet(&dynInternalRegs,relocbitfield);
1078 //hTabAddItem(&dynDirectRegNames, regname2key(buffer), relocbitfield);
1082 breg->reg_alias = relocbitfield;
1083 breg->address = dynrIdx; /* byte_no; */
1084 breg->rIdx = bit_no++;
1092 static void bitEQUs(FILE *of, set *bregs)
1094 regs *breg,*bytereg;
1097 //fprintf(stderr," %s\n",__FUNCTION__);
1098 for (breg = setFirstItem(bregs) ; breg ;
1099 breg = setNextItem(bregs)) {
1101 //fprintf(stderr,"bit reg: %s\n",breg->name);
1103 bytereg = breg->reg_alias;
1105 fprintf (of, "%s\tEQU\t( (%s<<3)+%d)\n",
1108 breg->rIdx & 0x0007);
1111 //fprintf(stderr, "bit field is not assigned to a register\n");
1112 fprintf (of, "%s\tEQU\t( (bitfield%d<<3)+%d)\n",
1123 void writeUsedRegs(FILE *of)
1126 packBits(dynDirectBitRegs);
1128 bitEQUs(of,dynDirectBitRegs);
1131 /*-----------------------------------------------------------------*/
1132 /* computeSpillable - given a point find the spillable live ranges */
1133 /*-----------------------------------------------------------------*/
1135 computeSpillable (iCode * ic)
1139 debugLog ("%s\n", __FUNCTION__);
1140 /* spillable live ranges are those that are live at this
1141 point . the following categories need to be subtracted
1143 a) - those that are already spilt
1144 b) - if being used by this one
1145 c) - defined by this one */
1147 spillable = bitVectCopy (ic->rlive);
1149 bitVectCplAnd (spillable, _G.spiltSet); /* those already spilt */
1151 bitVectCplAnd (spillable, ic->uses); /* used in this one */
1152 bitVectUnSetBit (spillable, ic->defKey);
1153 spillable = bitVectIntersect (spillable, _G.regAssigned);
1158 /*-----------------------------------------------------------------*/
1159 /* noSpilLoc - return true if a variable has no spil location */
1160 /*-----------------------------------------------------------------*/
1162 noSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
1164 debugLog ("%s\n", __FUNCTION__);
1165 return (sym->usl.spillLoc ? 0 : 1);
1168 /*-----------------------------------------------------------------*/
1169 /* hasSpilLoc - will return 1 if the symbol has spil location */
1170 /*-----------------------------------------------------------------*/
1172 hasSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
1174 debugLog ("%s\n", __FUNCTION__);
1175 return (sym->usl.spillLoc ? 1 : 0);
1178 /*-----------------------------------------------------------------*/
1179 /* directSpilLoc - will return 1 if the splilocation is in direct */
1180 /*-----------------------------------------------------------------*/
1182 directSpilLoc (symbol * sym, eBBlock * ebp, iCode * ic)
1184 debugLog ("%s\n", __FUNCTION__);
1185 if (sym->usl.spillLoc &&
1186 (IN_DIRSPACE (SPEC_OCLS (sym->usl.spillLoc->etype))))
1192 /*-----------------------------------------------------------------*/
1193 /* hasSpilLocnoUptr - will return 1 if the symbol has spil location */
1194 /* but is not used as a pointer */
1195 /*-----------------------------------------------------------------*/
1197 hasSpilLocnoUptr (symbol * sym, eBBlock * ebp, iCode * ic)
1199 debugLog ("%s\n", __FUNCTION__);
1200 return ((sym->usl.spillLoc && !sym->uptr) ? 1 : 0);
1203 /*-----------------------------------------------------------------*/
1204 /* rematable - will return 1 if the remat flag is set */
1205 /*-----------------------------------------------------------------*/
1207 rematable (symbol * sym, eBBlock * ebp, iCode * ic)
1209 debugLog ("%s\n", __FUNCTION__);
1213 /*-----------------------------------------------------------------*/
1214 /* notUsedInRemaining - not used or defined in remain of the block */
1215 /*-----------------------------------------------------------------*/
1217 notUsedInRemaining (symbol * sym, eBBlock * ebp, iCode * ic)
1219 debugLog ("%s\n", __FUNCTION__);
1220 return ((usedInRemaining (operandFromSymbol (sym), ic) ? 0 : 1) &&
1221 allDefsOutOfRange (sym->defs, ebp->fSeq, ebp->lSeq));
1224 /*-----------------------------------------------------------------*/
1225 /* allLRs - return true for all */
1226 /*-----------------------------------------------------------------*/
1228 allLRs (symbol * sym, eBBlock * ebp, iCode * ic)
1230 debugLog ("%s\n", __FUNCTION__);
1234 /*-----------------------------------------------------------------*/
1235 /* liveRangesWith - applies function to a given set of live range */
1236 /*-----------------------------------------------------------------*/
1238 liveRangesWith (bitVect * lrs, int (func) (symbol *, eBBlock *, iCode *),
1239 eBBlock * ebp, iCode * ic)
1244 debugLog ("%s\n", __FUNCTION__);
1245 if (!lrs || !lrs->size)
1248 for (i = 1; i < lrs->size; i++)
1251 if (!bitVectBitValue (lrs, i))
1254 /* if we don't find it in the live range
1255 hash table we are in serious trouble */
1256 if (!(sym = hTabItemWithKey (liveRanges, i)))
1258 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
1259 "liveRangesWith could not find liveRange");
1263 if (func (sym, ebp, ic) && bitVectBitValue (_G.regAssigned, sym->key))
1264 addSetHead (&rset, sym);
1271 /*-----------------------------------------------------------------*/
1272 /* leastUsedLR - given a set determines which is the least used */
1273 /*-----------------------------------------------------------------*/
1275 leastUsedLR (set * sset)
1277 symbol *sym = NULL, *lsym = NULL;
1279 debugLog ("%s\n", __FUNCTION__);
1280 sym = lsym = setFirstItem (sset);
1285 for (; lsym; lsym = setNextItem (sset))
1288 /* if usage is the same then prefer
1289 the spill the smaller of the two */
1290 if (lsym->used == sym->used)
1291 if (getSize (lsym->type) < getSize (sym->type))
1295 if (lsym->used < sym->used)
1300 setToNull ((void *) &sset);
1305 /*-----------------------------------------------------------------*/
1306 /* noOverLap - will iterate through the list looking for over lap */
1307 /*-----------------------------------------------------------------*/
1309 noOverLap (set * itmpStack, symbol * fsym)
1312 debugLog ("%s\n", __FUNCTION__);
1315 for (sym = setFirstItem (itmpStack); sym;
1316 sym = setNextItem (itmpStack))
1318 if (sym->liveTo > fsym->liveFrom)
1326 /*-----------------------------------------------------------------*/
1327 /* isFree - will return 1 if the a free spil location is found */
1328 /*-----------------------------------------------------------------*/
1333 V_ARG (symbol **, sloc);
1334 V_ARG (symbol *, fsym);
1336 debugLog ("%s\n", __FUNCTION__);
1337 /* if already found */
1341 /* if it is free && and the itmp assigned to
1342 this does not have any overlapping live ranges
1343 with the one currently being assigned and
1344 the size can be accomodated */
1346 noOverLap (sym->usl.itmpStack, fsym) &&
1347 getSize (sym->type) >= getSize (fsym->type))
1356 /*-----------------------------------------------------------------*/
1357 /* spillLRWithPtrReg :- will spil those live ranges which use PTR */
1358 /*-----------------------------------------------------------------*/
1360 spillLRWithPtrReg (symbol * forSym)
1365 debugLog ("%s\n", __FUNCTION__);
1366 if (!_G.regAssigned ||
1367 bitVectIsZero (_G.regAssigned))
1370 /* for all live ranges */
1371 for (lrsym = hTabFirstItem (liveRanges, &k); lrsym;
1372 lrsym = hTabNextItem (liveRanges, &k))
1374 /* if no registers assigned to it or
1376 /* if it does not overlap with this then
1377 not need to spill it */
1379 if (lrsym->isspilt || !lrsym->nRegs ||
1380 (lrsym->liveTo < forSym->liveFrom))
1387 /*-----------------------------------------------------------------*/
1388 /* createStackSpil - create a location on the stack to spil */
1389 /*-----------------------------------------------------------------*/
1391 createStackSpil (symbol * sym)
1393 symbol *sloc = NULL;
1394 int useXstack, model, noOverlay;
1396 char slocBuffer[30];
1397 debugLog ("%s\n", __FUNCTION__);
1401 /* first go try and find a free one that is already
1402 existing on the stack */
1403 if (applyToSet (_G.stackSpil, isFree, &sloc, sym))
1405 /* found a free one : just update & return */
1406 sym->usl.spillLoc = sloc;
1409 addSetHead (&sloc->usl.itmpStack, sym);
1413 /* could not then have to create one , this is the hard part
1414 we need to allocate this on the stack : this is really a
1415 hack!! but cannot think of anything better at this time */
1417 if (sprintf (slocBuffer, "sloc%d", _G.slocNum++) >= sizeof (slocBuffer))
1419 fprintf (stderr, "kkkInternal error: slocBuffer overflowed: %s:%d\n",
1420 __FILE__, __LINE__);
1424 sloc = newiTemp (slocBuffer);
1426 /* set the type to the spilling symbol */
1427 sloc->type = copyLinkChain (sym->type);
1428 sloc->etype = getSpec (sloc->type);
1429 SPEC_SCLS (sloc->etype) = S_DATA;
1430 SPEC_EXTR (sloc->etype) = 0;
1431 SPEC_STAT (sloc->etype) = 0;
1433 /* we don't allow it to be allocated`
1434 onto the external stack since : so we
1435 temporarily turn it off ; we also
1436 turn off memory model to prevent
1437 the spil from going to the external storage
1438 and turn off overlaying
1441 useXstack = options.useXstack;
1442 model = options.model;
1443 noOverlay = options.noOverlay;
1444 options.noOverlay = 1;
1445 options.model = options.useXstack = 0;
1449 options.useXstack = useXstack;
1450 options.model = model;
1451 options.noOverlay = noOverlay;
1452 sloc->isref = 1; /* to prevent compiler warning */
1454 /* if it is on the stack then update the stack */
1455 if (IN_STACK (sloc->etype))
1457 currFunc->stack += getSize (sloc->type);
1458 _G.stackExtend += getSize (sloc->type);
1461 _G.dataExtend += getSize (sloc->type);
1463 /* add it to the _G.stackSpil set */
1464 addSetHead (&_G.stackSpil, sloc);
1465 sym->usl.spillLoc = sloc;
1468 /* add it to the set of itempStack set
1469 of the spill location */
1470 addSetHead (&sloc->usl.itmpStack, sym);
1474 /*-----------------------------------------------------------------*/
1475 /* isSpiltOnStack - returns true if the spil location is on stack */
1476 /*-----------------------------------------------------------------*/
1478 isSpiltOnStack (symbol * sym)
1482 debugLog ("%s\n", __FUNCTION__);
1491 /* if (sym->_G.stackSpil) */
1494 if (!sym->usl.spillLoc)
1497 etype = getSpec (sym->usl.spillLoc->type);
1498 if (IN_STACK (etype))
1504 /*-----------------------------------------------------------------*/
1505 /* spillThis - spils a specific operand */
1506 /*-----------------------------------------------------------------*/
1508 spillThis (symbol * sym)
1511 debugLog ("%s : %s\n", __FUNCTION__, sym->rname);
1512 FENTRY2("sym: %s, spillLoc:%p (%s)\n", sym->rname, sym->usl.spillLoc, sym->usl.spillLoc ? sym->usl.spillLoc->rname : "<unknown>");
1514 /* if this is rematerializable or has a spillLocation
1515 we are okay, else we need to create a spillLocation
1517 if (!(sym->remat || sym->usl.spillLoc))
1518 createStackSpil (sym);
1521 /* mark it has spilt & put it in the spilt set */
1523 _G.spiltSet = bitVectSetBit (_G.spiltSet, sym->key);
1525 bitVectUnSetBit (_G.regAssigned, sym->key);
1527 for (i = 0; i < sym->nRegs; i++)
1531 freeReg (sym->regs[i]);
1532 sym->regs[i] = NULL;
1536 /* if spilt on stack then free up r0 & r1
1537 if they could have been assigned to some
1539 if (!pic14_ptrRegReq && isSpiltOnStack (sym))
1542 spillLRWithPtrReg (sym);
1545 if (sym->usl.spillLoc && !sym->remat)
1546 sym->usl.spillLoc->allocreq = 1;
1551 /*-----------------------------------------------------------------*/
1552 /* selectSpil - select a iTemp to spil : rather a simple procedure */
1553 /*-----------------------------------------------------------------*/
1555 selectSpil (iCode * ic, eBBlock * ebp, symbol * forSym)
1557 bitVect *lrcs = NULL;
1561 debugLog ("%s\n", __FUNCTION__);
1563 /* get the spillable live ranges */
1564 lrcs = computeSpillable (ic);
1567 /* get all live ranges that are rematerizable */
1568 if ((selectS = liveRangesWith (lrcs, rematable, ebp, ic)))
1570 /* return the least used of these */
1571 return leastUsedLR (selectS);
1574 /* get live ranges with spillLocations in direct space */
1575 if ((selectS = liveRangesWith (lrcs, directSpilLoc, ebp, ic)))
1577 sym = leastUsedLR (selectS);
1578 strcpy (sym->rname, (sym->usl.spillLoc->rname[0] ?
1579 sym->usl.spillLoc->rname :
1580 sym->usl.spillLoc->name));
1582 /* mark it as allocation required */
1583 sym->usl.spillLoc->allocreq = 1;
1587 /* if the symbol is local to the block then */
1588 if (forSym->liveTo < ebp->lSeq)
1591 /* check if there are any live ranges allocated
1592 to registers that are not used in this block */
1593 if (!_G.blockSpil && (selectS = liveRangesWith (lrcs, notUsedInBlock, ebp, ic)))
1595 sym = leastUsedLR (selectS);
1596 /* if this is not rematerializable */
1605 /* check if there are any live ranges that not
1606 used in the remainder of the block */
1607 if (!_G.blockSpil &&
1608 !isiCodeInFunctionCall (ic) &&
1609 (selectS = liveRangesWith (lrcs, notUsedInRemaining, ebp, ic)))
1611 sym = leastUsedLR (selectS);
1614 sym->remainSpil = 1;
1621 /* find live ranges with spillocation && not used as pointers */
1622 if ((selectS = liveRangesWith (lrcs, hasSpilLocnoUptr, ebp, ic)))
1625 sym = leastUsedLR (selectS);
1626 /* mark this as allocation required */
1627 sym->usl.spillLoc->allocreq = 1;
1631 /* find live ranges with spillocation */
1632 if ((selectS = liveRangesWith (lrcs, hasSpilLoc, ebp, ic)))
1635 sym = leastUsedLR (selectS);
1636 sym->usl.spillLoc->allocreq = 1;
1640 /* couldn't find then we need to create a spil
1641 location on the stack , for which one? the least
1643 if ((selectS = liveRangesWith (lrcs, noSpilLoc, ebp, ic)))
1646 /* return a created spil location */
1647 sym = createStackSpil (leastUsedLR (selectS));
1648 sym->usl.spillLoc->allocreq = 1;
1652 /* this is an extreme situation we will spill
1653 this one : happens very rarely but it does happen */
1659 /*-----------------------------------------------------------------*/
1660 /* spilSomething - spil some variable & mark registers as free */
1661 /*-----------------------------------------------------------------*/
1663 spilSomething (iCode * ic, eBBlock * ebp, symbol * forSym)
1668 debugLog ("%s\n", __FUNCTION__);
1669 /* get something we can spil */
1670 ssym = selectSpil (ic, ebp, forSym);
1672 /* mark it as spilt */
1674 _G.spiltSet = bitVectSetBit (_G.spiltSet, ssym->key);
1676 /* mark it as not register assigned &
1677 take it away from the set */
1678 bitVectUnSetBit (_G.regAssigned, ssym->key);
1680 /* mark the registers as free */
1681 for (i = 0; i < ssym->nRegs; i++)
1683 freeReg (ssym->regs[i]);
1685 /* if spilt on stack then free up r0 & r1
1686 if they could have been assigned to as gprs */
1687 if (!pic14_ptrRegReq && isSpiltOnStack (ssym))
1690 spillLRWithPtrReg (ssym);
1693 /* if this was a block level spil then insert push & pop
1694 at the start & end of block respectively */
1695 if (ssym->blockSpil)
1697 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
1698 /* add push to the start of the block */
1699 addiCodeToeBBlock (ebp, nic, (ebp->sch->op == LABEL ?
1700 ebp->sch->next : ebp->sch));
1701 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
1702 /* add pop to the end of the block */
1703 addiCodeToeBBlock (ebp, nic, NULL);
1706 /* if spilt because not used in the remainder of the
1707 block then add a push before this instruction and
1708 a pop at the end of the block */
1709 if (ssym->remainSpil)
1712 iCode *nic = newiCode (IPUSH, operandFromSymbol (ssym), NULL);
1713 /* add push just before this instruction */
1714 addiCodeToeBBlock (ebp, nic, ic);
1716 nic = newiCode (IPOP, operandFromSymbol (ssym), NULL);
1717 /* add pop to the end of the block */
1718 addiCodeToeBBlock (ebp, nic, NULL);
1727 /*-----------------------------------------------------------------*/
1728 /* getRegPtr - will try for PTR if not a GPR type if not spil */
1729 /*-----------------------------------------------------------------*/
1731 getRegPtr (iCode * ic, eBBlock * ebp, symbol * sym)
1736 debugLog ("%s\n", __FUNCTION__);
1738 /* try for a ptr type */
1739 if ((reg = allocReg (REG_PTR)))
1742 /* try for gpr type */
1743 if ((reg = allocReg (REG_GPR)))
1746 /* we have to spil */
1747 if (!spilSomething (ic, ebp, sym))
1750 /* make sure partially assigned registers aren't reused */
1751 for (j=0; j<=sym->nRegs; j++)
1753 sym->regs[j]->isFree = 0;
1755 /* this looks like an infinite loop but
1756 in really selectSpil will abort */
1760 /*-----------------------------------------------------------------*/
1761 /* getRegGpr - will try for GPR if not spil */
1762 /*-----------------------------------------------------------------*/
1764 getRegGpr (iCode * ic, eBBlock * ebp, symbol * sym)
1769 debugLog ("%s\n", __FUNCTION__);
1771 /* try for gpr type */
1772 if ((reg = allocReg (REG_GPR)))
1775 if (!pic14_ptrRegReq)
1776 if ((reg = allocReg (REG_PTR)))
1779 /* we have to spil */
1780 if (!spilSomething (ic, ebp, sym))
1783 /* make sure partially assigned registers aren't reused */
1784 for (j=0; j<=sym->nRegs; j++)
1786 sym->regs[j]->isFree = 0;
1788 /* this looks like an infinite loop but
1789 in really selectSpil will abort */
1793 /*-----------------------------------------------------------------*/
1794 /* symHasReg - symbol has a given register */
1795 /*-----------------------------------------------------------------*/
1797 symHasReg (symbol * sym, regs * reg)
1801 debugLog ("%s\n", __FUNCTION__);
1802 for (i = 0; i < sym->nRegs; i++)
1803 if (sym->regs[i] == reg)
1809 /*-----------------------------------------------------------------*/
1810 /* deassignLRs - check the live to and if they have registers & are */
1811 /* not spilt then free up the registers */
1812 /*-----------------------------------------------------------------*/
1814 deassignLRs (iCode * ic, eBBlock * ebp)
1820 debugLog ("%s\n", __FUNCTION__);
1821 for (sym = hTabFirstItem (liveRanges, &k); sym;
1822 sym = hTabNextItem (liveRanges, &k))
1825 symbol *psym = NULL;
1826 /* if it does not end here */
1827 if (sym->liveTo > ic->seq)
1830 /* Prevent the result from being assigned the same registers as (one)
1831 * operand as many genXXX-functions fail otherwise.
1832 * POINTER_GET(ic) || ic->op == LEFT_OP || ic->op == RIGHT_OP || ic->op == NOT
1833 * are known to fail. */
1834 if (sym->liveTo == ic->seq && IC_RESULT(ic))
1838 case '=': /* assignment */
1839 case BITWISEAND: /* bitwise AND */
1840 case '|': /* bitwise OR */
1841 case '^': /* bitwise XOR */
1842 case '~': /* bitwise negate */
1843 case RLC: /* rotate through carry */
1846 case '+': /* addition */
1847 case '-': /* subtraction */
1848 /* go ahead, these are safe to use with
1849 * non-disjoint register sets */
1853 /* do not release operand registers */
1854 //fprintf (stderr, "%s:%u: operand not freed: ", __FILE__, __LINE__); piCode (ic, stderr); fprintf (stderr, "\n");
1859 /* if it was spilt on stack then we can
1860 mark the stack spil location as free */
1865 sym->usl.spillLoc->isFree = 1;
1871 if (!bitVectBitValue (_G.regAssigned, sym->key))
1873 /* special case check if this is an IFX &
1874 the privious one was a pop and the
1875 previous one was not spilt then keep track
1877 if (ic->op == IFX && ic->prev &&
1878 ic->prev->op == IPOP &&
1879 !ic->prev->parmPush &&
1880 IS_SYMOP(IC_LEFT (ic->prev)) &&
1881 !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt)
1882 psym = OP_SYMBOL (IC_LEFT (ic->prev));
1888 bitVectUnSetBit (_G.regAssigned, sym->key);
1890 /* if the result of this one needs registers
1891 and does not have it then assign it right
1893 if (IC_RESULT (ic) &&
1894 !(SKIP_IC2 (ic) || /* not a special icode */
1895 ic->op == JUMPTABLE ||
1900 POINTER_SET (ic)) &&
1901 IS_SYMOP (IC_RESULT (ic)) &&
1902 (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */
1903 result->liveTo > ic->seq && /* and will live beyond this */
1904 result->liveTo <= ebp->lSeq && /* does not go beyond this block */
1905 result->liveFrom == ic->seq && /* does not start before here */
1906 result->regType == sym->regType && /* same register types */
1907 result->regType == sym->regType && /* same register types */
1908 result->nRegs && /* which needs registers */
1909 !result->isspilt && /* and does not already have them */
1911 !bitVectBitValue (_G.regAssigned, result->key) &&
1912 /* the number of free regs + number of regs in this LR
1913 can accomodate the what result Needs */
1914 ((nfreeRegsType (result->regType) +
1915 sym->nRegs) >= result->nRegs)
1919 for (i = 0; i < max (sym->nRegs, result->nRegs); i++)
1921 result->regs[i] = sym->regs[i];
1923 result->regs[i] = getRegGpr (ic, ebp, result);
1925 _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key);
1929 /* free the remaining */
1930 for (; i < sym->nRegs; i++)
1934 if (!symHasReg (psym, sym->regs[i]))
1935 freeReg (sym->regs[i]);
1938 freeReg (sym->regs[i]);
1945 /*-----------------------------------------------------------------*/
1946 /* reassignLR - reassign this to registers */
1947 /*-----------------------------------------------------------------*/
1949 reassignLR (operand * op)
1951 symbol *sym = OP_SYMBOL (op);
1954 debugLog ("%s\n", __FUNCTION__);
1955 /* not spilt any more */
1956 sym->isspilt = sym->blockSpil = sym->remainSpil = 0;
1957 bitVectUnSetBit (_G.spiltSet, sym->key);
1959 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
1963 for (i = 0; i < sym->nRegs; i++)
1964 sym->regs[i]->isFree = 0;
1967 /*-----------------------------------------------------------------*/
1968 /* willCauseSpill - determines if allocating will cause a spill */
1969 /*-----------------------------------------------------------------*/
1971 willCauseSpill (int nr, int rt)
1973 debugLog ("%s\n", __FUNCTION__);
1974 /* first check if there are any avlb registers
1975 of te type required */
1978 /* special case for pointer type
1979 if pointer type not avlb then
1980 check for type gpr */
1981 if (nFreeRegs (rt) >= nr)
1983 if (nFreeRegs (REG_GPR) >= nr)
1988 if (pic14_ptrRegReq)
1990 if (nFreeRegs (rt) >= nr)
1995 if (nFreeRegs (REG_PTR) +
1996 nFreeRegs (REG_GPR) >= nr)
2001 debugLog (" ... yep it will (cause a spill)\n");
2002 /* it will cause a spil */
2006 /*-----------------------------------------------------------------*/
2007 /* positionRegs - the allocator can allocate same registers to res- */
2008 /* ult and operand, if this happens make sure they are in the same */
2009 /* position as the operand otherwise chaos results */
2010 /*-----------------------------------------------------------------*/
2012 positionRegs (symbol * result, symbol * opsym, int lineno)
2014 int count = min (result->nRegs, opsym->nRegs);
2015 int i, j = 0, shared = 0;
2017 debugLog ("%s\n", __FUNCTION__);
2018 /* if the result has been spilt then cannot share */
2023 /* first make sure that they actually share */
2024 for (i = 0; i < count; i++)
2026 for (j = 0; j < count; j++)
2028 if (result->regs[i] == opsym->regs[j] && i != j)
2038 regs *tmp = result->regs[i];
2039 result->regs[i] = result->regs[j];
2040 result->regs[j] = tmp;
2045 /*------------------------------------------------------------------*/
2046 /* verifyRegsAssigned - make sure an iTemp is properly initialized; */
2047 /* it should either have registers or have beed spilled. Otherwise, */
2048 /* there was an uninitialized variable, so just spill this to get */
2049 /* the operand in a valid state. */
2050 /*------------------------------------------------------------------*/
2052 verifyRegsAssigned (operand *op, iCode * ic)
2057 if (!IS_ITEMP (op)) return;
2059 sym = OP_SYMBOL (op);
2060 if (sym->isspilt) return;
2061 if (!sym->nRegs) return;
2062 if (sym->regs[0]) return;
2064 werrorfl (ic->filename, ic->lineno, W_LOCAL_NOINIT,
2065 sym->prereqv ? sym->prereqv->name : sym->name);
2070 /*-----------------------------------------------------------------*/
2071 /* serialRegAssign - serially allocate registers to the variables */
2072 /*-----------------------------------------------------------------*/
2074 serialRegAssign (eBBlock ** ebbs, int count)
2078 debugLog ("%s\n", __FUNCTION__);
2079 /* for all blocks */
2080 for (i = 0; i < count; i++)
2085 if (ebbs[i]->noPath &&
2086 (ebbs[i]->entryLabel != entryLabel &&
2087 ebbs[i]->entryLabel != returnLabel))
2090 /* of all instructions do */
2091 for (ic = ebbs[i]->sch; ic; ic = ic->next)
2093 debugLog (" op: %s\n", decodeOp (ic->op));
2095 /* if this is an ipop that means some live
2096 range will have to be assigned again */
2098 reassignLR (IC_LEFT (ic));
2100 /* if result is present && is a true symbol */
2101 if (IC_RESULT (ic) && ic->op != IFX &&
2102 IS_TRUE_SYMOP (IC_RESULT (ic)))
2103 OP_SYMBOL (IC_RESULT (ic))->allocreq = 1;
2105 /* take away registers from live
2106 ranges that end at this instruction */
2107 deassignLRs (ic, ebbs[i]);
2109 /* some don't need registers */
2110 if (SKIP_IC2 (ic) ||
2111 ic->op == JUMPTABLE ||
2115 (IC_RESULT (ic) && POINTER_SET (ic)))
2118 /* now we need to allocate registers
2119 only for the result */
2120 if (IC_RESULT (ic) && IS_SYMOP (IC_RESULT (ic)))
2122 symbol *sym = OP_SYMBOL (IC_RESULT (ic));
2128 /* Make sure any spill location is definately allocated */
2129 if (sym->isspilt && !sym->remat && sym->usl.spillLoc &&
2130 !sym->usl.spillLoc->allocreq)
2132 sym->usl.spillLoc->allocreq++;
2135 /* if it does not need or is spilt
2136 or is already assigned to registers
2137 or will not live beyond this instructions */
2140 bitVectBitValue (_G.regAssigned, sym->key) ||
2141 sym->liveTo <= ic->seq)
2144 /* if some liverange has been spilt at the block level
2145 and this one live beyond this block then spil this
2147 if (_G.blockSpil && sym->liveTo > ebbs[i]->lSeq)
2152 /* if trying to allocate this will cause
2153 a spill and there is nothing to spill
2154 or this one is rematerializable then
2156 willCS = willCauseSpill (sym->nRegs, sym->regType);
2157 spillable = computeSpillable (ic);
2159 (willCS && bitVectIsZero (spillable)))
2167 /* If the live range preceeds the point of definition
2168 then ideally we must take into account registers that
2169 have been allocated after sym->liveFrom but freed
2170 before ic->seq. This is complicated, so spill this
2171 symbol instead and let fillGaps handle the allocation. */
2172 if (sym->liveFrom < ic->seq)
2178 /* if it has a spillocation & is used less than
2179 all other live ranges then spill this */
2181 if (sym->usl.spillLoc) {
2182 symbol *leastUsed = leastUsedLR (liveRangesWith (spillable,
2183 allLRs, ebbs[i], ic));
2184 if (leastUsed && leastUsed->used > sym->used) {
2189 /* if none of the liveRanges have a spillLocation then better
2190 to spill this one than anything else already assigned to registers */
2191 if (liveRangesWith(spillable,noSpilLoc,ebbs[i],ic)) {
2192 /* if this is local to this block then we might find a block spil */
2193 if (!(sym->liveFrom >= ebbs[i]->fSeq && sym->liveTo <= ebbs[i]->lSeq)) {
2201 if (ic->op == RECEIVE)
2202 debugLog ("When I get clever, I'll optimize the receive logic\n");
2204 /* if we need ptr regs for the right side
2206 if (POINTER_GET (ic)
2207 && IS_SYMOP(IC_LEFT(ic))
2208 && getSize (OP_SYMBOL (IC_LEFT (ic))->type)
2209 <= (unsigned) PTRSIZE)
2214 /* else we assign registers to it */
2215 _G.regAssigned = bitVectSetBit (_G.regAssigned, sym->key);
2217 debugLog (" %d - \n", __LINE__);
2219 bitVectDebugOn(_G.regAssigned, debugF);
2220 for (j = 0; j < sym->nRegs; j++)
2222 if (sym->regType == REG_PTR)
2223 sym->regs[j] = getRegPtr (ic, ebbs[i], sym);
2225 sym->regs[j] = getRegGpr (ic, ebbs[i], sym);
2227 /* if the allocation failed which means
2228 this was spilt then break */
2232 debugLog (" %d - \n", __LINE__);
2234 /* if it shares registers with operands make sure
2235 that they are in the same position */
2236 if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) &&
2237 IS_SYMOP(IC_RESULT(ic)) &&
2238 OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=')
2239 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
2240 OP_SYMBOL (IC_LEFT (ic)), ic->lineno);
2241 /* do the same for the right operand */
2242 if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) &&
2243 IS_SYMOP(IC_RESULT(ic)) &&
2244 OP_SYMBOL (IC_RIGHT (ic))->nRegs && ic->op != '=')
2245 positionRegs (OP_SYMBOL (IC_RESULT (ic)),
2246 OP_SYMBOL (IC_RIGHT (ic)), ic->lineno);
2248 debugLog (" %d - \n", __LINE__);
2251 debugLog (" %d - \n", __LINE__);
2260 /* Check for and fix any problems with uninitialized operands */
2261 for (i = 0; i < count; i++)
2265 if (ebbs[i]->noPath &&
2266 (ebbs[i]->entryLabel != entryLabel &&
2267 ebbs[i]->entryLabel != returnLabel))
2270 for (ic = ebbs[i]->sch; ic; ic = ic->next)
2277 verifyRegsAssigned (IC_COND (ic), ic);
2281 if (ic->op == JUMPTABLE)
2283 verifyRegsAssigned (IC_JTCOND (ic), ic);
2287 verifyRegsAssigned (IC_RESULT (ic), ic);
2288 verifyRegsAssigned (IC_LEFT (ic), ic);
2289 verifyRegsAssigned (IC_RIGHT (ic), ic);
2295 /*-----------------------------------------------------------------*/
2296 /* rUmaskForOp :- returns register mask for an operand */
2297 /*-----------------------------------------------------------------*/
2299 rUmaskForOp (operand * op)
2305 debugLog ("%s\n", __FUNCTION__);
2306 /* only temporaries are assigned registers */
2310 sym = OP_SYMBOL (op);
2312 /* if spilt or no registers assigned to it
2314 if (sym->isspilt || !sym->nRegs)
2317 rumask = newBitVect (pic14_nRegs);
2319 for (j = 0; j < sym->nRegs; j++)
2321 rumask = bitVectSetBit (rumask,
2322 sym->regs[j]->rIdx);
2328 /*-----------------------------------------------------------------*/
2329 /* regsUsedIniCode :- returns bit vector of registers used in iCode */
2330 /*-----------------------------------------------------------------*/
2332 regsUsedIniCode (iCode * ic)
2334 bitVect *rmask = newBitVect (pic14_nRegs);
2336 debugLog ("%s\n", __FUNCTION__);
2337 /* do the special cases first */
2340 rmask = bitVectUnion (rmask,
2341 rUmaskForOp (IC_COND (ic)));
2345 /* for the jumptable */
2346 if (ic->op == JUMPTABLE)
2348 rmask = bitVectUnion (rmask,
2349 rUmaskForOp (IC_JTCOND (ic)));
2354 /* of all other cases */
2356 rmask = bitVectUnion (rmask,
2357 rUmaskForOp (IC_LEFT (ic)));
2361 rmask = bitVectUnion (rmask,
2362 rUmaskForOp (IC_RIGHT (ic)));
2365 rmask = bitVectUnion (rmask,
2366 rUmaskForOp (IC_RESULT (ic)));
2372 /*-----------------------------------------------------------------*/
2373 /* createRegMask - for each instruction will determine the regsUsed */
2374 /*-----------------------------------------------------------------*/
2376 createRegMask (eBBlock ** ebbs, int count)
2380 debugLog ("%s\n", __FUNCTION__);
2381 /* for all blocks */
2382 for (i = 0; i < count; i++)
2386 if (ebbs[i]->noPath &&
2387 (ebbs[i]->entryLabel != entryLabel &&
2388 ebbs[i]->entryLabel != returnLabel))
2391 /* for all instructions */
2392 for (ic = ebbs[i]->sch; ic; ic = ic->next)
2397 if (SKIP_IC2 (ic) || !ic->rlive)
2400 /* first mark the registers used in this
2402 ic->rUsed = regsUsedIniCode (ic);
2403 _G.funcrUsed = bitVectUnion (_G.funcrUsed, ic->rUsed);
2405 /* now create the register mask for those
2406 registers that are in use : this is a
2407 super set of ic->rUsed */
2408 ic->rMask = newBitVect (pic14_nRegs + 1);
2410 /* for all live Ranges alive at this point */
2411 for (j = 1; j < ic->rlive->size; j++)
2416 /* if not alive then continue */
2417 if (!bitVectBitValue (ic->rlive, j))
2420 /* find the live range we are interested in */
2421 if (!(sym = hTabItemWithKey (liveRanges, j)))
2423 werror (E_INTERNAL_ERROR, __FILE__, __LINE__,
2424 "createRegMask cannot find live range");
2428 /* if no register assigned to it */
2429 if (!sym->nRegs || sym->isspilt)
2432 /* for all the registers allocated to it */
2433 for (k = 0; k < sym->nRegs; k++)
2436 bitVectSetBit (ic->rMask, sym->regs[k]->rIdx);
2442 /*-----------------------------------------------------------------*/
2443 /* regTypeNum - computes the type & number of registers required */
2444 /*-----------------------------------------------------------------*/
2452 debugLog ("%s\n", __FUNCTION__);
2453 /* for each live range do */
2454 for (sym = hTabFirstItem (liveRanges, &k); sym;
2455 sym = hTabNextItem (liveRanges, &k)) {
2457 debugLog (" %d - %s\n", __LINE__, sym->rname);
2459 /* if used zero times then no registers needed */
2460 if ((sym->liveTo - sym->liveFrom) == 0)
2464 /* if the live range is a temporary */
2467 debugLog (" %d - itemp register\n", __LINE__);
2469 /* if the type is marked as a conditional */
2470 if (sym->regType == REG_CND)
2473 /* if used in return only then we don't
2476 if (IS_AGGREGATE (sym->type) || sym->isptr)
2477 sym->type = aggrToPtr (sym->type, FALSE);
2478 debugLog (" %d - no reg needed - accumulator used\n", __LINE__);
2484 //if (IS_AGGREGATE (sym->type) || sym->isptr)
2485 // sym->type = aggrToPtr (sym->type, FALSE);
2486 debugLog (" %d - used as a return\n", __LINE__);
2491 /* if the symbol has only one definition &
2492 that definition is a get_pointer and the
2493 pointer we are getting is rematerializable and
2497 if (bitVectnBitsOn (sym->defs) == 1 &&
2498 (ic = hTabItemWithKey (iCodehTab,
2499 bitVectFirstBit (sym->defs))) &&
2501 !IS_BITVAR (sym->etype) &&
2502 (aggrToPtrDclType (operandType (IC_LEFT (ic)), FALSE) == POINTER)) {
2504 if (ptrPseudoSymSafe (sym, ic)) {
2508 debugLog (" %d - \n", __LINE__);
2510 /* create a pseudo symbol & force a spil */
2511 //X symbol *psym = newSymbol (rematStr (OP_SYMBOL (IC_LEFT (ic))), 1);
2512 psym = rematStr (OP_SYMBOL (IC_LEFT (ic)));
2513 psym->type = sym->type;
2514 psym->etype = sym->etype;
2515 psym->psbase = ptrBaseRematSym (OP_SYMBOL (IC_LEFT (ic)));
2516 strcpy (psym->rname, psym->name);
2518 sym->usl.spillLoc = psym;
2522 /* if in data space or idata space then try to
2523 allocate pointer register */
2528 /* if not then we require registers */
2529 sym->nRegs = ((IS_AGGREGATE (sym->type) || sym->isptr) ?
2530 getSize (sym->type = aggrToPtr (sym->type, FALSE)) :
2531 getSize (sym->type));
2534 if(IS_PTR_CONST (sym->type)) {
2535 debugLog (" %d const pointer type requires %d registers, changing to 2\n",__LINE__,sym->nRegs);
2539 if (sym->nRegs > 4) {
2540 fprintf (stderr, "allocated more than 4 or 0 registers for type ");
2541 printTypeChain (sym->type, stderr);
2542 fprintf (stderr, "\n");
2545 /* determine the type of register required */
2546 if (sym->nRegs == 1 &&
2547 IS_PTR (sym->type) &&
2549 sym->regType = REG_PTR;
2551 sym->regType = REG_GPR;
2554 debugLog (" reg name %s, reg type %s\n", sym->rname, debugLogRegType (sym->regType));
2558 /* for the first run we don't provide */
2559 /* registers for true symbols we will */
2560 /* see how things go */
2566 /*-----------------------------------------------------------------*/
2567 /* deallocStackSpil - this will set the stack pointer back */
2568 /*-----------------------------------------------------------------*/
2570 DEFSETFUNC (deallocStackSpil)
2574 debugLog ("%s\n", __FUNCTION__);
2579 /*-----------------------------------------------------------------*/
2580 /* farSpacePackable - returns the packable icode for far variables */
2581 /*-----------------------------------------------------------------*/
2583 farSpacePackable (iCode * ic)
2587 debugLog ("%s\n", __FUNCTION__);
2588 /* go thru till we find a definition for the
2589 symbol on the right */
2590 for (dic = ic->prev; dic; dic = dic->prev)
2593 /* if the definition is a call then no */
2594 if ((dic->op == CALL || dic->op == PCALL) &&
2595 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2600 /* if shift by unknown amount then not */
2601 if ((dic->op == LEFT_OP || dic->op == RIGHT_OP) &&
2602 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2605 /* if pointer get and size > 1 */
2606 if (POINTER_GET (dic) &&
2607 getSize (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)) > 1)
2610 if (POINTER_SET (dic) &&
2611 getSize (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)) > 1)
2614 /* if any three is a true symbol in far space */
2615 if (IC_RESULT (dic) &&
2616 IS_TRUE_SYMOP (IC_RESULT (dic)) &&
2617 isOperandInFarSpace (IC_RESULT (dic)))
2620 if (IC_RIGHT (dic) &&
2621 IS_TRUE_SYMOP (IC_RIGHT (dic)) &&
2622 isOperandInFarSpace (IC_RIGHT (dic)) &&
2623 !isOperandEqual (IC_RIGHT (dic), IC_RESULT (ic)))
2626 if (IC_LEFT (dic) &&
2627 IS_TRUE_SYMOP (IC_LEFT (dic)) &&
2628 isOperandInFarSpace (IC_LEFT (dic)) &&
2629 !isOperandEqual (IC_LEFT (dic), IC_RESULT (ic)))
2632 if (isOperandEqual (IC_RIGHT (ic), IC_RESULT (dic)))
2634 if ((dic->op == LEFT_OP ||
2635 dic->op == RIGHT_OP ||
2637 IS_OP_LITERAL (IC_RIGHT (dic)))
2647 /*-----------------------------------------------------------------*/
2648 /* packRegsForAssign - register reduction for assignment */
2649 /*-----------------------------------------------------------------*/
2651 packRegsForAssign (iCode * ic, eBBlock * ebp)
2656 debugLog ("%s\n", __FUNCTION__);
2658 debugAopGet (" result:", IC_RESULT (ic));
2659 debugAopGet (" left:", IC_LEFT (ic));
2660 debugAopGet (" right:", IC_RIGHT (ic));
2662 /* if this is at an absolute address, then get the address. */
2663 if (SPEC_ABSA ( OP_SYM_ETYPE(IC_RESULT(ic))) ) {
2664 if(IS_CONFIG_ADDRESS( SPEC_ADDR ( OP_SYM_ETYPE(IC_RESULT(ic))))) {
2665 debugLog (" %d - found config word declaration\n", __LINE__);
2666 if(IS_VALOP(IC_RIGHT(ic))) {
2667 debugLog (" setting config word to %x\n",
2668 (int) ulFromVal (IC_RIGHT(ic)->operand.valOperand));
2669 pic14_assignConfigWordValue( SPEC_ADDR ( OP_SYM_ETYPE(IC_RESULT(ic))),
2670 (int) ulFromVal (IC_RIGHT(ic)->operand.valOperand));
2673 /* remove the assignment from the iCode chain. */
2675 remiCodeFromeBBlock (ebp, ic);
2676 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2677 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2684 if (!IS_ITEMP (IC_RESULT (ic))) {
2685 allocDirReg(IC_RESULT (ic));
2686 debugLog (" %d - result is not temp\n", __LINE__);
2689 if (IC_LEFT (ic) && !IS_ITEMP (IC_LEFT (ic))) {
2690 debugLog (" %d - left is not temp, allocating\n", __LINE__);
2691 allocDirReg(IC_LEFT (ic));
2695 if (!IS_ITEMP (IC_RIGHT (ic))) {
2696 debugLog (" %d - not packing - right is not temp\n", __LINE__);
2698 /* only pack if this is not a function pointer */
2699 if (!IS_REF (IC_RIGHT (ic)))
2700 allocDirReg(IC_RIGHT (ic));
2704 if (OP_SYMBOL (IC_RIGHT (ic))->isind ||
2705 OP_LIVETO (IC_RIGHT (ic)) > ic->seq)
2707 debugLog (" %d - not packing - right side fails \n", __LINE__);
2711 /* if the true symbol is defined in far space or on stack
2712 then we should not since this will increase register pressure */
2713 if (isOperandInFarSpace (IC_RESULT (ic)))
2715 if ((dic = farSpacePackable (ic)))
2721 /* find the definition of iTempNN scanning backwards if we find a
2722 a use of the true symbol before we find the definition then
2724 for (dic = ic->prev; dic; dic = dic->prev)
2727 /* if there is a function call and this is
2728 a parameter & not my parameter then don't pack it */
2729 if ((dic->op == CALL || dic->op == PCALL) &&
2730 (OP_SYMBOL (IC_RESULT (ic))->_isparm &&
2731 !OP_SYMBOL (IC_RESULT (ic))->ismyparm))
2733 debugLog (" %d - \n", __LINE__);
2741 if (IS_TRUE_SYMOP (IC_RESULT (dic)) &&
2742 IS_OP_VOLATILE (IC_RESULT (dic)))
2744 debugLog (" %d - dic is VOLATILE \n", __LINE__);
2749 if (IS_SYMOP (IC_RESULT (dic)) &&
2750 IC_RESULT (dic)->key == IC_RIGHT (ic)->key)
2752 /* A previous result was assigned to the same register - we'll our definition */
2753 debugLog (" %d - dic result key == ic right key -- pointer set=%c\n",
2754 __LINE__, ((POINTER_SET (dic)) ? 'Y' : 'N'));
2755 if (POINTER_SET (dic))
2761 if (IS_SYMOP (IC_RIGHT (dic)) &&
2762 (IC_RIGHT (dic)->key == IC_RESULT (ic)->key ||
2763 IC_RIGHT (dic)->key == IC_RIGHT (ic)->key))
2765 debugLog (" %d - dic right key == ic rightor result key\n", __LINE__);
2770 if (IS_SYMOP (IC_LEFT (dic)) &&
2771 (IC_LEFT (dic)->key == IC_RESULT (ic)->key ||
2772 IC_LEFT (dic)->key == IC_RIGHT (ic)->key))
2774 debugLog (" %d - dic left key == ic rightor result key\n", __LINE__);
2779 if (POINTER_SET (dic) &&
2780 IC_RESULT (dic)->key == IC_RESULT (ic)->key)
2782 debugLog (" %d - dic result key == ic result key -- pointer set=Y\n",
2790 return 0; /* did not find */
2792 /* if assignment then check that right is not a bit */
2793 if (ASSIGNMENT (ic) && !POINTER_SET (ic))
2795 sym_link *etype = operandType (IC_RESULT (dic));
2796 if (IS_BITFIELD (etype))
2798 /* if result is a bit too then it's ok */
2799 etype = operandType (IC_RESULT (ic));
2800 if (!IS_BITFIELD (etype))
2805 /* if the result is on stack or iaccess then it must be
2806 the same at least one of the operands */
2807 if (OP_SYMBOL (IC_RESULT (ic))->onStack ||
2808 OP_SYMBOL (IC_RESULT (ic))->iaccess)
2811 /* the operation has only one symbol
2812 operator then we can pack */
2813 if ((IC_LEFT (dic) && !IS_SYMOP (IC_LEFT (dic))) ||
2814 (IC_RIGHT (dic) && !IS_SYMOP (IC_RIGHT (dic))))
2817 if (!((IC_LEFT (dic) &&
2818 IC_RESULT (ic)->key == IC_LEFT (dic)->key) ||
2820 IC_RESULT (ic)->key == IC_RIGHT (dic)->key)))
2824 debugLog (" packing. removing %s\n", OP_SYMBOL (IC_RIGHT (ic))->rname);
2825 debugLog (" replacing with %s\n", OP_SYMBOL (IC_RESULT (dic))->rname);
2826 /* found the definition */
2827 /* replace the result with the result of */
2828 /* this assignment and remove this assignment */
2829 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2830 IC_RESULT (dic) = IC_RESULT (ic);
2832 if (IS_ITEMP (IC_RESULT (dic)) && OP_SYMBOL (IC_RESULT (dic))->liveFrom > dic->seq)
2834 OP_SYMBOL (IC_RESULT (dic))->liveFrom = dic->seq;
2836 /* delete from liverange table also
2837 delete from all the points inbetween and the new
2839 for (sic = dic; sic != ic; sic = sic->next)
2841 bitVectUnSetBit (sic->rlive, IC_RESULT (ic)->key);
2842 if (IS_ITEMP (IC_RESULT (dic)))
2843 bitVectSetBit (sic->rlive, IC_RESULT (dic)->key);
2846 remiCodeFromeBBlock (ebp, ic);
2847 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
2848 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
2849 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
2855 /*-----------------------------------------------------------------*/
2856 /* findAssignToSym : scanning backwards looks for first assig found */
2857 /*-----------------------------------------------------------------*/
2859 findAssignToSym (operand * op, iCode * ic)
2863 debugLog ("%s\n", __FUNCTION__);
2864 for (dic = ic->prev; dic; dic = dic->prev)
2867 /* if definition by assignment */
2868 if (dic->op == '=' &&
2869 !POINTER_SET (dic) &&
2870 IC_RESULT (dic)->key == op->key
2871 /* && IS_TRUE_SYMOP(IC_RIGHT(dic)) */
2875 /* we are interested only if defined in far space */
2876 /* or in stack space in case of + & - */
2878 /* if assigned to a non-symbol then return
2880 if (!IS_SYMOP (IC_RIGHT (dic)))
2883 /* if the symbol is in far space then
2885 if (isOperandInFarSpace (IC_RIGHT (dic)))
2888 /* for + & - operations make sure that
2889 if it is on the stack it is the same
2890 as one of the three operands */
2891 if ((ic->op == '+' || ic->op == '-') &&
2892 OP_SYMBOL (IC_RIGHT (dic))->onStack)
2895 if (IC_RESULT (ic)->key != IC_RIGHT (dic)->key &&
2896 IC_LEFT (ic)->key != IC_RIGHT (dic)->key &&
2897 IC_RIGHT (ic)->key != IC_RIGHT (dic)->key)
2905 /* if we find an usage then we cannot delete it */
2906 if (IC_LEFT (dic) && IC_LEFT (dic)->key == op->key)
2909 if (IC_RIGHT (dic) && IC_RIGHT (dic)->key == op->key)
2912 if (POINTER_SET (dic) && IC_RESULT (dic)->key == op->key)
2916 /* now make sure that the right side of dic
2917 is not defined between ic & dic */
2920 iCode *sic = dic->next;
2922 for (; sic != ic; sic = sic->next)
2923 if (IC_RESULT (sic) &&
2924 IC_RESULT (sic)->key == IC_RIGHT (dic)->key)
2933 /*-----------------------------------------------------------------*/
2934 /* packRegsForSupport :- reduce some registers for support calls */
2935 /*-----------------------------------------------------------------*/
2937 packRegsForSupport (iCode * ic, eBBlock * ebp)
2941 debugLog ("%s\n", __FUNCTION__);
2942 /* for the left & right operand :- look to see if the
2943 left was assigned a true symbol in far space in that
2944 case replace them */
2945 if (IS_ITEMP (IC_LEFT (ic)) &&
2946 OP_SYMBOL (IC_LEFT (ic))->liveTo <= ic->seq)
2948 iCode *dic = findAssignToSym (IC_LEFT (ic), ic);
2954 debugAopGet ("removing left:", IC_LEFT (ic));
2956 /* found it we need to remove it from the
2958 for (sic = dic; sic != ic; sic = sic->next)
2959 bitVectUnSetBit (sic->rlive, IC_LEFT (ic)->key);
2961 IC_LEFT (ic)->operand.symOperand =
2962 IC_RIGHT (dic)->operand.symOperand;
2963 IC_LEFT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
2964 remiCodeFromeBBlock (ebp, dic);
2965 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
2966 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
2970 /* do the same for the right operand */
2973 IS_ITEMP (IC_RIGHT (ic)) &&
2974 OP_SYMBOL (IC_RIGHT (ic))->liveTo <= ic->seq)
2976 iCode *dic = findAssignToSym (IC_RIGHT (ic), ic);
2982 /* if this is a subtraction & the result
2983 is a true symbol in far space then don't pack */
2984 if (ic->op == '-' && IS_TRUE_SYMOP (IC_RESULT (dic)))
2986 sym_link *etype = getSpec (operandType (IC_RESULT (dic)));
2987 if (IN_FARSPACE (SPEC_OCLS (etype)))
2991 debugAopGet ("removing right:", IC_RIGHT (ic));
2993 /* found it we need to remove it from the
2995 for (sic = dic; sic != ic; sic = sic->next)
2996 bitVectUnSetBit (sic->rlive, IC_RIGHT (ic)->key);
2998 IC_RIGHT (ic)->operand.symOperand =
2999 IC_RIGHT (dic)->operand.symOperand;
3000 IC_RIGHT (ic)->key = IC_RIGHT (dic)->operand.symOperand->key;
3002 remiCodeFromeBBlock (ebp, dic);
3003 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
3004 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
3012 /*-----------------------------------------------------------------*/
3013 /* packRegsForOneuse : - will reduce some registers for single Use */
3014 /*-----------------------------------------------------------------*/
3016 packRegsForOneuse (iCode * ic, operand * op, eBBlock * ebp)
3021 debugLog ("%s\n", __FUNCTION__);
3022 /* if returning a literal then do nothing */
3026 /* only upto 2 bytes since we cannot predict
3027 the usage of b, & acc */
3028 if (getSize (operandType (op)) > (fReturnSizePic - 2) &&
3033 /* this routine will mark the a symbol as used in one
3034 instruction use only && if the definition is local
3035 (ie. within the basic block) && has only one definition &&
3036 that definition is either a return value from a
3037 function or does not contain any variables in
3039 uses = bitVectCopy (OP_USES (op));
3040 bitVectUnSetBit (uses, ic->key); /* take away this iCode */
3041 if (!bitVectIsZero (uses)) /* has other uses */
3044 /* if it has only one defintion */
3045 if (bitVectnBitsOn (OP_DEFS (op)) > 1)
3046 return NULL; /* has more than one definition */
3048 /* get that definition */
3050 hTabItemWithKey (iCodehTab,
3051 bitVectFirstBit (OP_DEFS (op)))))
3054 /* found the definition now check if it is local */
3055 if (dic->seq < ebp->fSeq ||
3056 dic->seq > ebp->lSeq)
3057 return NULL; /* non-local */
3059 /* now check if it is the return from
3061 if (dic->op == CALL || dic->op == PCALL)
3063 if (ic->op != SEND && ic->op != RETURN &&
3064 !POINTER_SET(ic) && !POINTER_GET(ic))
3066 OP_SYMBOL (op)->ruonly = 1;
3073 /* otherwise check that the definition does
3074 not contain any symbols in far space */
3075 if (isOperandInFarSpace (IC_LEFT (dic)) ||
3076 isOperandInFarSpace (IC_RIGHT (dic)) ||
3077 IS_OP_RUONLY (IC_LEFT (ic)) ||
3078 IS_OP_RUONLY (IC_RIGHT (ic)))
3083 /* if pointer set then make sure the pointer
3085 if (POINTER_SET (dic) &&
3086 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
3089 if (POINTER_GET (dic) &&
3090 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
3095 /* also make sure the intervenening instructions
3096 don't have any thing in far space */
3097 for (dic = dic->next; dic && dic != ic; dic = dic->next)
3100 /* if there is an intervening function call then no */
3101 if (dic->op == CALL || dic->op == PCALL)
3103 /* if pointer set then make sure the pointer
3105 if (POINTER_SET (dic) &&
3106 !IS_DATA_PTR (aggrToPtr (operandType (IC_RESULT (dic)), FALSE)))
3109 if (POINTER_GET (dic) &&
3110 !IS_DATA_PTR (aggrToPtr (operandType (IC_LEFT (dic)), FALSE)))
3113 /* if address of & the result is remat then okay */
3114 if (dic->op == ADDRESS_OF &&
3115 OP_SYMBOL (IC_RESULT (dic))->remat)
3118 /* if operand has size of three or more & this
3119 operation is a '*','/' or '%' then 'b' may
3121 if ((dic->op == '%' || dic->op == '/' || dic->op == '*') &&
3122 getSize (operandType (op)) >= 3)
3125 /* if left or right or result is in far space */
3126 if (isOperandInFarSpace (IC_LEFT (dic)) ||
3127 isOperandInFarSpace (IC_RIGHT (dic)) ||
3128 isOperandInFarSpace (IC_RESULT (dic)) ||
3129 IS_OP_RUONLY (IC_LEFT (dic)) ||
3130 IS_OP_RUONLY (IC_RIGHT (dic)) ||
3131 IS_OP_RUONLY (IC_RESULT (dic)))
3137 OP_SYMBOL (op)->ruonly = 1;
3142 /*-----------------------------------------------------------------*/
3143 /* isBitwiseOptimizable - requirements of JEAN LOUIS VERN */
3144 /*-----------------------------------------------------------------*/
3146 isBitwiseOptimizable (iCode * ic)
3148 sym_link *ltype = getSpec (operandType (IC_LEFT (ic)));
3149 sym_link *rtype = getSpec (operandType (IC_RIGHT (ic)));
3151 debugLog ("%s\n", __FUNCTION__);
3152 /* bitwise operations are considered optimizable
3153 under the following conditions (Jean-Louis VERN)
3165 if (IS_LITERAL (rtype) ||
3166 (IS_BITVAR (ltype) && IN_BITSPACE (SPEC_OCLS (ltype))))
3172 /*-----------------------------------------------------------------*/
3173 /* packRegsForAccUse - pack registers for acc use */
3174 /*-----------------------------------------------------------------*/
3176 packRegsForAccUse (iCode * ic)
3180 debugLog ("%s\n", __FUNCTION__);
3182 /* result too large for WREG? */
3183 if (getSize (operandType (IC_RESULT (ic))) > 1)
3186 /* We have to make sure that OP_SYMBOL(IC_RESULT(ic))
3187 * is never used as an operand to an instruction that
3188 * cannot have WREG as an operand (e.g. BTFSx cannot
3189 * operate on WREG...
3190 * For now, store all results into proper registers. */
3194 /* if this is an aggregate, e.g. a one byte char array */
3195 if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) {
3198 debugLog (" %s:%d\n", __FUNCTION__,__LINE__);
3200 /* if + or - then it has to be one byte result */
3201 if ((ic->op == '+' || ic->op == '-')
3202 && getSize (operandType (IC_RESULT (ic))) > 1)
3205 debugLog (" %s:%d\n", __FUNCTION__,__LINE__);
3206 /* if shift operation make sure right side is not a literal */
3207 if (ic->op == RIGHT_OP &&
3208 (isOperandLiteral (IC_RIGHT (ic)) ||
3209 getSize (operandType (IC_RESULT (ic))) > 1))
3212 if (ic->op == LEFT_OP &&
3213 (isOperandLiteral (IC_RIGHT (ic)) ||
3214 getSize (operandType (IC_RESULT (ic))) > 1))
3217 if (IS_BITWISE_OP (ic) &&
3218 getSize (operandType (IC_RESULT (ic))) > 1)
3222 /* has only one definition */
3223 if (bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) > 1)
3226 /* has only one use */
3227 if (bitVectnBitsOn (OP_USES (IC_RESULT (ic))) > 1)
3230 /* and the usage immediately follows this iCode */
3231 if (!(uic = hTabItemWithKey (iCodehTab,
3232 bitVectFirstBit (OP_USES (IC_RESULT (ic))))))
3235 debugLog (" %s:%d\n", __FUNCTION__,__LINE__);
3236 if (ic->next != uic)
3239 /* if it is a conditional branch then we definitely can */
3243 if (uic->op == JUMPTABLE)
3246 /* if the usage is not is an assignment
3247 or an arithmetic / bitwise / shift operation then not */
3248 if (POINTER_SET (uic) &&
3249 getSize (aggrToPtr (operandType (IC_RESULT (uic)), FALSE)) > 1)
3252 debugLog (" %s:%d\n", __FUNCTION__,__LINE__);
3253 if (uic->op != '=' &&
3254 !IS_ARITHMETIC_OP (uic) &&
3255 !IS_BITWISE_OP (uic) &&
3256 uic->op != LEFT_OP &&
3257 uic->op != RIGHT_OP)
3260 debugLog (" %s:%d\n", __FUNCTION__,__LINE__);
3261 /* if used in ^ operation then make sure right is not a
3263 if (uic->op == '^' && isOperandLiteral (IC_RIGHT (uic)))
3266 /* if shift operation make sure right side is not a literal */
3267 if (uic->op == RIGHT_OP &&
3268 (isOperandLiteral (IC_RIGHT (uic)) ||
3269 getSize (operandType (IC_RESULT (uic))) > 1))
3272 if (uic->op == LEFT_OP &&
3273 (isOperandLiteral (IC_RIGHT (uic)) ||
3274 getSize (operandType (IC_RESULT (uic))) > 1))
3277 /* make sure that the result of this icode is not on the
3278 stack, since acc is used to compute stack offset */
3279 if (IS_TRUE_SYMOP (IC_RESULT (uic)) &&
3280 OP_SYMBOL (IC_RESULT (uic))->onStack)
3283 /* if either one of them in far space then we cannot */
3284 if ((IS_TRUE_SYMOP (IC_LEFT (uic)) &&
3285 isOperandInFarSpace (IC_LEFT (uic))) ||
3286 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
3287 isOperandInFarSpace (IC_RIGHT (uic))))
3290 /* if the usage has only one operand then we can */
3291 if (IC_LEFT (uic) == NULL ||
3292 IC_RIGHT (uic) == NULL)
3295 /* make sure this is on the left side if not
3296 a '+' since '+' is commutative */
3297 if (ic->op != '+' &&
3298 IC_LEFT (uic)->key != IC_RESULT (ic)->key)
3301 debugLog (" %s:%d\n", __FUNCTION__,__LINE__);
3302 /* if one of them is a literal then we can */
3303 if ( ((IC_LEFT (uic) && IS_OP_LITERAL (IC_LEFT (uic))) ||
3304 (IC_RIGHT (uic) && IS_OP_LITERAL (IC_RIGHT (uic)))) &&
3305 (getSize (operandType (IC_RESULT (uic))) <= 1))
3307 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
3311 debugLog (" %s:%d\n", __FUNCTION__,__LINE__);
3312 /* if the other one is not on stack then we can */
3313 if (IC_LEFT (uic)->key == IC_RESULT (ic)->key &&
3314 (IS_ITEMP (IC_RIGHT (uic)) ||
3315 (IS_TRUE_SYMOP (IC_RIGHT (uic)) &&
3316 !OP_SYMBOL (IC_RIGHT (uic))->onStack)))
3319 if (IC_RIGHT (uic)->key == IC_RESULT (ic)->key &&
3320 (IS_ITEMP (IC_LEFT (uic)) ||
3321 (IS_TRUE_SYMOP (IC_LEFT (uic)) &&
3322 !OP_SYMBOL (IC_LEFT (uic))->onStack)))
3328 debugLog ("%s - Yes we are using the accumulator\n", __FUNCTION__);
3329 OP_SYMBOL (IC_RESULT (ic))->accuse = 1;
3333 /*-----------------------------------------------------------------*/
3334 /* packForPush - hueristics to reduce iCode for pushing */
3335 /*-----------------------------------------------------------------*/
3337 packForReceive (iCode * ic, eBBlock * ebp)
3341 debugLog ("%s\n", __FUNCTION__);
3342 debugAopGet (" result:", IC_RESULT (ic));
3343 debugAopGet (" left:", IC_LEFT (ic));
3344 debugAopGet (" right:", IC_RIGHT (ic));
3349 for (dic = ic->next; dic; dic = dic->next)
3354 if (IC_LEFT (dic) && (IC_RESULT (ic)->key == IC_LEFT (dic)->key))
3355 debugLog (" used on left\n");
3356 if (IC_RIGHT (dic) && IC_RESULT (ic)->key == IC_RIGHT (dic)->key)
3357 debugLog (" used on right\n");
3358 if (IC_RESULT (dic) && IC_RESULT (ic)->key == IC_RESULT (dic)->key)
3359 debugLog (" used on result\n");
3361 if ((IC_LEFT (dic) && (IC_RESULT (ic)->key == IC_LEFT (dic)->key)) ||
3362 (IC_RESULT (dic) && IC_RESULT (ic)->key == IC_RESULT (dic)->key))
3367 debugLog (" hey we can remove this unnecessary assign\n");
3369 /*-----------------------------------------------------------------*/
3370 /* packForPush - hueristics to reduce iCode for pushing */
3371 /*-----------------------------------------------------------------*/
3373 packForPush (iCode * ic, eBBlock * ebp)
3377 debugLog ("%s\n", __FUNCTION__);
3378 if (ic->op != IPUSH || !IS_ITEMP (IC_LEFT (ic)))
3381 /* must have only definition & one usage */
3382 if (bitVectnBitsOn (OP_DEFS (IC_LEFT (ic))) != 1 ||
3383 bitVectnBitsOn (OP_USES (IC_LEFT (ic))) != 1)
3386 /* find the definition */
3387 if (!(dic = hTabItemWithKey (iCodehTab,
3388 bitVectFirstBit (OP_DEFS (IC_LEFT (ic))))))
3391 if (dic->op != '=' || POINTER_SET (dic))
3394 /* we now we know that it has one & only one def & use
3395 and the that the definition is an assignment */
3396 IC_LEFT (ic) = IC_RIGHT (dic);
3398 remiCodeFromeBBlock (ebp, dic);
3399 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
3400 hTabDeleteItem (&iCodehTab, dic->key, dic, DELETE_ITEM, NULL);
3403 static void printSymType(char * str, sym_link *sl)
3406 debugLog (" %s Symbol type: ",str);
3407 printTypeChain( sl, debugF);
3412 /*-----------------------------------------------------------------*/
3413 /* some debug code to print the symbol S_TYPE. Note that
3414 * the function checkSClass in src/SDCCsymt.c dinks with
3415 * the S_TYPE in ways the PIC port doesn't fully like...*/
3416 /*-----------------------------------------------------------------*/
3417 static void isData(sym_link *sl)
3421 // avoid garbage `data' and `sfr' output
3428 for ( ; sl; sl=sl->next) {
3430 switch (SPEC_SCLS(sl)) {
3432 case S_DATA: fprintf (of, "data "); break;
3433 case S_XDATA: fprintf (of, "xdata "); break;
3434 case S_SFR: fprintf (of, "sfr "); break;
3435 case S_SBIT: fprintf (of, "sbit "); break;
3436 case S_CODE: fprintf (of, "code "); break;
3437 case S_IDATA: fprintf (of, "idata "); break;
3438 case S_PDATA: fprintf (of, "pdata "); break;
3439 case S_LITERAL: fprintf (of, "literal "); break;
3440 case S_STACK: fprintf (of, "stack "); break;
3441 case S_XSTACK: fprintf (of, "xstack "); break;
3442 case S_BIT: fprintf (of, "bit "); break;
3443 case S_EEPROM: fprintf (of, "eeprom "); break;
3453 /*-----------------------------------------------------------------*/
3454 /* packRegisters - does some transformations to reduce register */
3456 /*-----------------------------------------------------------------*/
3458 packRegisters (eBBlock * ebp)
3463 debugLog ("%s\n", __FUNCTION__);
3469 /* look for assignments of the form */
3470 /* iTempNN = TRueSym (someoperation) SomeOperand */
3472 /* TrueSym := iTempNN:1 */
3473 for (ic = ebp->sch; ic; ic = ic->next)
3476 /* find assignment of the form TrueSym := iTempNN:1 */
3477 if (ic->op == '=' && !POINTER_SET (ic))
3478 change += packRegsForAssign (ic, ebp);
3482 if (POINTER_SET (ic))
3483 debugLog ("pointer is set\n");
3484 debugAopGet (" result:", IC_RESULT (ic));
3485 debugAopGet (" left:", IC_LEFT (ic));
3486 debugAopGet (" right:", IC_RIGHT (ic));
3495 for (ic = ebp->sch; ic; ic = ic->next) {
3497 if(IS_SYMOP ( IC_LEFT(ic))) {
3498 sym_link *etype = getSpec (operandType (IC_LEFT (ic)));
3500 debugAopGet (" left:", IC_LEFT (ic));
3501 if(IS_PTR_CONST(OP_SYMBOL(IC_LEFT(ic))->type))
3502 debugLog (" is a pointer\n");
3504 if(IS_OP_VOLATILE(IC_LEFT(ic)))
3505 debugLog (" is volatile\n");
3509 printSymType(" ", OP_SYMBOL(IC_LEFT(ic))->type);
3512 if(IS_SYMOP ( IC_RIGHT(ic))) {
3513 debugAopGet (" right:", IC_RIGHT (ic));
3514 printSymType(" ", OP_SYMBOL(IC_RIGHT(ic))->type);
3517 if(IS_SYMOP ( IC_RESULT(ic))) {
3518 debugAopGet (" result:", IC_RESULT (ic));
3519 printSymType(" ", OP_SYMBOL(IC_RESULT(ic))->type);
3522 if (POINTER_SET (ic))
3523 debugLog (" %d - Pointer set\n", __LINE__);
3526 /* Look for two subsequent iCodes with */
3528 /* _c = iTemp & op; */
3529 /* and replace them by */
3532 if ((ic->op == BITWISEAND || ic->op == '|' || ic->op == '^') &&
3534 ic->prev->op == '=' &&
3535 IS_ITEMP (IC_LEFT (ic)) &&
3536 IC_LEFT (ic) == IC_RESULT (ic->prev) &&
3537 isOperandEqual (IC_RESULT(ic), IC_RIGHT(ic->prev)))
3539 iCode* ic_prev = ic->prev;
3540 symbol* prev_result_sym = OP_SYMBOL (IC_RESULT (ic_prev));
3542 ReplaceOpWithCheaperOp (&IC_LEFT (ic), IC_RESULT (ic));
3543 if (IC_RESULT (ic_prev) != IC_RIGHT (ic))
3545 bitVectUnSetBit (OP_USES (IC_RESULT (ic_prev)), ic->key);
3546 if (/*IS_ITEMP (IC_RESULT (ic_prev)) && */
3547 prev_result_sym->liveTo == ic->seq)
3549 prev_result_sym->liveTo = ic_prev->seq;
3552 bitVectSetBit (OP_USES (IC_RESULT (ic)), ic->key);
3554 bitVectSetBit (ic->rlive, IC_RESULT (ic)->key);
3556 if (bitVectIsZero (OP_USES (IC_RESULT (ic_prev))))
3558 bitVectUnSetBit (ic->rlive, IC_RESULT (ic)->key);
3559 bitVectUnSetBit (OP_DEFS (IC_RESULT (ic_prev)), ic_prev->key);
3560 remiCodeFromeBBlock (ebp, ic_prev);
3561 hTabDeleteItem (&iCodehTab, ic_prev->key, ic_prev, DELETE_ITEM, NULL);
3565 /* if this is an itemp & result of a address of a true sym
3566 then mark this as rematerialisable */
3567 if (ic->op == ADDRESS_OF &&
3568 IS_ITEMP (IC_RESULT (ic)) &&
3569 IS_TRUE_SYMOP (IC_LEFT (ic)) &&
3570 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
3571 !OP_SYMBOL (IC_LEFT (ic))->onStack)
3574 debugLog (" %d - %s. result is rematerializable\n", __LINE__,__FUNCTION__);
3576 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
3577 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
3578 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
3582 /* if straight assignment then carry remat flag if
3583 this is the only definition */
3584 if (ic->op == '=' &&
3585 !POINTER_SET (ic) &&
3586 IS_SYMOP (IC_RIGHT (ic)) &&
3587 OP_SYMBOL (IC_RIGHT (ic))->remat &&
3588 bitVectnBitsOn (OP_SYMBOL (IC_RESULT (ic))->defs) <= 1)
3590 debugLog (" %d - %s. straight rematerializable\n", __LINE__,__FUNCTION__);
3592 OP_SYMBOL (IC_RESULT (ic))->remat =
3593 OP_SYMBOL (IC_RIGHT (ic))->remat;
3594 OP_SYMBOL (IC_RESULT (ic))->rematiCode =
3595 OP_SYMBOL (IC_RIGHT (ic))->rematiCode;
3598 /* if this is a +/- operation with a rematerizable
3599 then mark this as rematerializable as well */
3600 if ((ic->op == '+' || ic->op == '-') &&
3601 (IS_SYMOP (IC_LEFT (ic)) &&
3602 IS_ITEMP (IC_RESULT (ic)) &&
3603 OP_SYMBOL (IC_LEFT (ic))->remat &&
3604 bitVectnBitsOn (OP_DEFS (IC_RESULT (ic))) == 1 &&
3605 IS_OP_LITERAL (IC_RIGHT (ic))))
3607 debugLog (" %d - %s. rematerializable because op is +/-\n", __LINE__,__FUNCTION__);
3609 operandLitValue (IC_RIGHT (ic));
3610 OP_SYMBOL (IC_RESULT (ic))->remat = 1;
3611 OP_SYMBOL (IC_RESULT (ic))->rematiCode = ic;
3612 OP_SYMBOL (IC_RESULT (ic))->usl.spillLoc = NULL;
3615 /* mark the pointer usages */
3616 if (POINTER_SET (ic) && IS_SYMOP(IC_RESULT(ic)))
3618 OP_SYMBOL (IC_RESULT (ic))->uptr = 1;
3619 debugLog (" marking as a pointer (set) =>");
3620 debugAopGet (" result:", IC_RESULT (ic));
3622 if (POINTER_GET (ic) && IS_SYMOP(IC_LEFT(ic)))
3624 OP_SYMBOL (IC_LEFT (ic))->uptr = 1;
3625 debugLog (" marking as a pointer (get) =>");
3626 debugAopGet (" left:", IC_LEFT (ic));
3631 /* if we are using a symbol on the stack
3632 then we should say pic14_ptrRegReq */
3633 if (ic->op == IFX && IS_SYMOP (IC_COND (ic)))
3634 pic14_ptrRegReq += ((OP_SYMBOL (IC_COND (ic))->onStack ||
3635 OP_SYMBOL (IC_COND (ic))->iaccess) ? 1 : 0);
3636 else if (ic->op == JUMPTABLE && IS_SYMOP (IC_JTCOND (ic)))
3637 pic14_ptrRegReq += ((OP_SYMBOL (IC_JTCOND (ic))->onStack ||
3638 OP_SYMBOL (IC_JTCOND (ic))->iaccess) ? 1 : 0);
3641 if (IS_SYMOP (IC_LEFT (ic)))
3642 pic14_ptrRegReq += ((OP_SYMBOL (IC_LEFT (ic))->onStack ||
3643 OP_SYMBOL (IC_LEFT (ic))->iaccess) ? 1 : 0);
3644 if (IS_SYMOP (IC_RIGHT (ic)))
3645 pic14_ptrRegReq += ((OP_SYMBOL (IC_RIGHT (ic))->onStack ||
3646 OP_SYMBOL (IC_RIGHT (ic))->iaccess) ? 1 : 0);
3647 if (IS_SYMOP (IC_RESULT (ic)))
3648 pic14_ptrRegReq += ((OP_SYMBOL (IC_RESULT (ic))->onStack ||
3649 OP_SYMBOL (IC_RESULT (ic))->iaccess) ? 1 : 0);
3652 debugLog (" %d - pointer reg req = %d\n", __LINE__,pic14_ptrRegReq);
3656 /* if the condition of an if instruction
3657 is defined in the previous instruction then
3658 mark the itemp as a conditional */
3659 if ((IS_CONDITIONAL (ic) ||
3660 ((ic->op == BITWISEAND ||
3663 isBitwiseOptimizable (ic))) &&
3664 ic->next && ic->next->op == IFX &&
3665 isOperandEqual (IC_RESULT (ic), IC_COND (ic->next)) &&
3666 OP_SYMBOL (IC_RESULT (ic))->liveTo <= ic->next->seq)
3669 debugLog (" %d\n", __LINE__);
3670 OP_SYMBOL (IC_RESULT (ic))->regType = REG_CND;
3674 /* reduce for support function calls */
3675 if (ic->supportRtn || ic->op == '+' || ic->op == '-')
3676 packRegsForSupport (ic, ebp);
3678 /* if a parameter is passed, it's in W, so we may not
3679 need to place a copy in a register */
3680 if (ic->op == RECEIVE)
3681 packForReceive (ic, ebp);
3683 /* some cases the redundant moves can
3684 can be eliminated for return statements */
3685 if ((ic->op == RETURN || ic->op == SEND) &&
3686 !isOperandInFarSpace (IC_LEFT (ic)) &&
3688 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
3690 /* if pointer set & left has a size more than
3691 one and right is not in far space */
3692 if (POINTER_SET (ic) &&
3693 !isOperandInFarSpace (IC_RIGHT (ic)) &&
3694 IS_SYMOP(IC_RESULT(ic)) &&
3695 !OP_SYMBOL (IC_RESULT (ic))->remat &&
3696 !IS_OP_RUONLY (IC_RIGHT (ic)) &&
3697 getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1)
3699 packRegsForOneuse (ic, IC_RESULT (ic), ebp);
3701 /* if pointer get */
3702 if (POINTER_GET (ic) &&
3703 !isOperandInFarSpace (IC_RESULT (ic)) &&
3704 IS_SYMOP(IC_LEFT(ic)) &&
3705 !OP_SYMBOL (IC_LEFT (ic))->remat &&
3706 !IS_OP_RUONLY (IC_RESULT (ic)) &&
3707 getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)
3709 packRegsForOneuse (ic, IC_LEFT (ic), ebp);
3712 /* if this is cast for intergral promotion then
3713 check if only use of the definition of the
3714 operand being casted/ if yes then replace
3715 the result of that arithmetic operation with
3716 this result and get rid of the cast */
3717 if (ic->op == CAST) {
3719 sym_link *fromType = operandType (IC_RIGHT (ic));
3720 sym_link *toType = operandType (IC_LEFT (ic));
3722 debugLog (" %d - casting\n", __LINE__);
3724 if (IS_INTEGRAL (fromType) && IS_INTEGRAL (toType) &&
3725 getSize (fromType) != getSize (toType)) {
3728 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
3731 if (IS_ARITHMETIC_OP (dic)) {
3733 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
3734 IC_RESULT (dic) = IC_RESULT (ic);
3735 remiCodeFromeBBlock (ebp, ic);
3736 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3737 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
3738 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
3742 OP_SYMBOL (IC_RIGHT (ic))->ruonly = 0;
3746 /* if the type from and type to are the same
3747 then if this is the only use then packit */
3748 if (compareType (operandType (IC_RIGHT (ic)),
3749 operandType (IC_LEFT (ic))) == 1) {
3751 iCode *dic = packRegsForOneuse (ic, IC_RIGHT (ic), ebp);
3754 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(dic))->defs,dic->key);
3755 IC_RESULT (dic) = IC_RESULT (ic);
3756 bitVectUnSetBit(OP_SYMBOL(IC_RESULT(ic))->defs,ic->key);
3757 remiCodeFromeBBlock (ebp, ic);
3758 hTabDeleteItem (&iCodehTab, ic->key, ic, DELETE_ITEM, NULL);
3759 OP_DEFS(IC_RESULT (dic))=bitVectSetBit (OP_DEFS (IC_RESULT (dic)), dic->key);
3767 iTempNN := (some variable in farspace) V1
3772 if (ic->op == IPUSH)
3774 packForPush (ic, ebp);
3778 /* pack registers for accumulator use, when the
3779 result of an arithmetic or bit wise operation
3780 has only one use, that use is immediately following
3781 the defintion and the using iCode has only one
3782 operand or has two operands but one is literal &
3783 the result of that operation is not on stack then
3784 we can leave the result of this operation in acc:b
3786 if ((IS_ARITHMETIC_OP (ic)
3788 || IS_BITWISE_OP (ic)
3790 || ic->op == LEFT_OP || ic->op == RIGHT_OP
3793 IS_ITEMP (IC_RESULT (ic)) &&
3794 getSize (operandType (IC_RESULT (ic))) <= 2)
3796 packRegsForAccUse (ic);
3802 dumpEbbsToDebug (eBBlock ** ebbs, int count)
3806 if (!debug || !debugF)
3809 for (i = 0; i < count; i++)
3811 fprintf (debugF, "\n----------------------------------------------------------------\n");
3812 fprintf (debugF, "Basic Block %s : loop Depth = %d noPath = %d , lastinLoop = %d\n",
3813 ebbs[i]->entryLabel->name,
3816 ebbs[i]->isLastInLoop);
3817 fprintf (debugF, "depth 1st num %d : bbnum = %d 1st iCode = %d , last iCode = %d\n",
3822 fprintf (debugF, "visited %d : hasFcall = %d\n",
3826 fprintf (debugF, "\ndefines bitVector :");
3827 bitVectDebugOn (ebbs[i]->defSet, debugF);
3828 fprintf (debugF, "\nlocal defines bitVector :");
3829 bitVectDebugOn (ebbs[i]->ldefs, debugF);
3830 fprintf (debugF, "\npointers Set bitvector :");
3831 bitVectDebugOn (ebbs[i]->ptrsSet, debugF);
3832 fprintf (debugF, "\nin pointers Set bitvector :");
3833 bitVectDebugOn (ebbs[i]->inPtrsSet, debugF);
3834 fprintf (debugF, "\ninDefs Set bitvector :");
3835 bitVectDebugOn (ebbs[i]->inDefs, debugF);
3836 fprintf (debugF, "\noutDefs Set bitvector :");
3837 bitVectDebugOn (ebbs[i]->outDefs, debugF);
3838 fprintf (debugF, "\nusesDefs Set bitvector :");
3839 bitVectDebugOn (ebbs[i]->usesDefs, debugF);
3840 fprintf (debugF, "\n----------------------------------------------------------------\n");
3841 printiCChain (ebbs[i]->sch, debugF);
3844 /*-----------------------------------------------------------------*/
3845 /* assignRegisters - assigns registers to each live range as need */
3846 /*-----------------------------------------------------------------*/
3848 pic14_assignRegisters (ebbIndex * ebbi)
3850 eBBlock ** ebbs = ebbi->bbOrder;
3851 int count = ebbi->count;
3855 debugLog ("<><><><><><><><><><><><><><><><><>\nstarting\t%s:%s\n", __FILE__, __FUNCTION__);
3856 debugLog ("ebbs before optimizing:\n");
3857 dumpEbbsToDebug (ebbs, count);
3859 setToNull ((void *) &_G.funcrUsed);
3860 pic14_ptrRegReq = _G.stackExtend = _G.dataExtend = 0;
3863 /* change assignments this will remove some
3864 live ranges reducing some register pressure */
3865 for (i = 0; i < count; i++)
3866 packRegisters (ebbs[i]);
3873 debugLog("dir registers allocated so far:\n");
3874 reg = hTabFirstItem(dynDirectRegNames, &hkey);
3877 debugLog(" -- #%d reg = %s key %d, rIdx = %d, size %d\n",i++,reg->name,hkey, reg->rIdx,reg->size);
3878 reg = hTabNextItem(dynDirectRegNames, &hkey);
3883 if (options.dump_pack)
3884 dumpEbbsToFileExt (DUMP_PACK, ebbi);
3886 /* first determine for each live range the number of
3887 registers & the type of registers required for each */
3890 /* and serially allocate registers */
3891 serialRegAssign (ebbs, count);
3893 /* if stack was extended then tell the user */
3896 /* werror(W_TOOMANY_SPILS,"stack", */
3897 /* _G.stackExtend,currFunc->name,""); */
3903 /* werror(W_TOOMANY_SPILS,"data space", */
3904 /* _G.dataExtend,currFunc->name,""); */
3908 /* after that create the register mask
3909 for each of the instruction */
3910 createRegMask (ebbs, count);
3912 /* redo that offsets for stacked automatic variables */
3913 redoStackOffsets ();
3915 if (options.dump_rassgn)
3916 dumpEbbsToFileExt (DUMP_RASSGN, ebbi);
3918 /* now get back the chain */
3919 ic = iCodeLabelOptimize (iCodeFromeBBlock (ebbs, count));
3921 debugLog ("ebbs after optimizing:\n");
3922 dumpEbbsToDebug (ebbs, count);
3927 /* free up any _G.stackSpil locations allocated */
3928 applyToSet (_G.stackSpil, deallocStackSpil);
3930 setToNull ((void *) &_G.stackSpil);
3931 setToNull ((void *) &_G.spiltSet);
3933 debugLog ("leaving\n<><><><><><><><><><><><><><><><><>\n");
3934 pic14_debugLogClose ();