X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2Fpic%2Fralloc.c;h=50c224ade294eda07c26cbcd0d5d68db3a509109;hb=195ee3f3ee25ce2c5f2a59fbd2779c4cb80527c3;hp=dad9f52261ad1bb01cd72b4c225b2b0309378212;hpb=25837611386183e136d41617166c3a81b52ce430;p=fw%2Fsdcc diff --git a/src/pic/ralloc.c b/src/pic/ralloc.c index dad9f522..50c224ad 100644 --- a/src/pic/ralloc.c +++ b/src/pic/ralloc.c @@ -5,23 +5,23 @@ Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998) Added Pic Port T.scott Dattalo scott@dattalo.com (2000) - This program is free software; you can redistribute it and/or modify it - under the terms of the GNU General Public License as published by the - Free Software Foundation; either version 2, or (at your option) any - later version. + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - In other words, you are welcome to use, share and improve this program. - You are forbidden to forbid anyone else to use, share and improve - what you give them. Help stamp out software-hoarding! + In other words, you are welcome to use, share and improve this program. + You are forbidden to forbid anyone else to use, share and improve + what you give them. Help stamp out software-hoarding! -------------------------------------------------------------------------*/ #include "common.h" @@ -134,7 +134,7 @@ static void fprintf (debugF, "%s", buffer); /* - while (isspace(*bufferP)) bufferP++; + while (isspace((unsigned char)*bufferP)) bufferP++; if (bufferP && *bufferP) lineCurr = (lineCurr ? @@ -193,208 +193,107 @@ static char * switch (op) { - case IDENTIFIER: - return "IDENTIFIER"; - case TYPE_NAME: - return "TYPE_NAME"; - case CONSTANT: - return "CONSTANT"; - case STRING_LITERAL: - return "STRING_LITERAL"; - case SIZEOF: - return "SIZEOF"; - case PTR_OP: - return "PTR_OP"; - case INC_OP: - return "INC_OP"; - case DEC_OP: - return "DEC_OP"; - case LEFT_OP: - return "LEFT_OP"; - case RIGHT_OP: - return "RIGHT_OP"; - case LE_OP: - return "LE_OP"; - case GE_OP: - return "GE_OP"; - case EQ_OP: - return "EQ_OP"; - case NE_OP: - return "NE_OP"; - case AND_OP: - return "AND_OP"; - case OR_OP: - return "OR_OP"; - case MUL_ASSIGN: - return "MUL_ASSIGN"; - case DIV_ASSIGN: - return "DIV_ASSIGN"; - case MOD_ASSIGN: - return "MOD_ASSIGN"; - case ADD_ASSIGN: - return "ADD_ASSIGN"; - case SUB_ASSIGN: - return "SUB_ASSIGN"; - case LEFT_ASSIGN: - return "LEFT_ASSIGN"; - case RIGHT_ASSIGN: - return "RIGHT_ASSIGN"; - case AND_ASSIGN: - return "AND_ASSIGN"; - case XOR_ASSIGN: - return "XOR_ASSIGN"; - case OR_ASSIGN: - return "OR_ASSIGN"; - case TYPEDEF: - return "TYPEDEF"; - case EXTERN: - return "EXTERN"; - case STATIC: - return "STATIC"; - case AUTO: - return "AUTO"; - case REGISTER: - return "REGISTER"; - case CODE: - return "CODE"; - case EEPROM: - return "EEPROM"; - case INTERRUPT: - return "INTERRUPT"; - case SFR: - return "SFR"; - case AT: - return "AT"; - case SBIT: - return "SBIT"; - case REENTRANT: - return "REENTRANT"; - case USING: - return "USING"; - case XDATA: - return "XDATA"; - case DATA: - return "DATA"; - case IDATA: - return "IDATA"; - case PDATA: - return "PDATA"; - case VAR_ARGS: - return "VAR_ARGS"; - case CRITICAL: - return "CRITICAL"; - case NONBANKED: - return "NONBANKED"; - case BANKED: - return "BANKED"; - case CHAR: - return "CHAR"; - case SHORT: - return "SHORT"; - case INT: - return "INT"; - case LONG: - return "LONG"; - case SIGNED: - return "SIGNED"; - case UNSIGNED: - return "UNSIGNED"; - case FLOAT: - return "FLOAT"; - case DOUBLE: - return "DOUBLE"; - case CONST: - return "CONST"; - case VOLATILE: - return "VOLATILE"; - case VOID: - return "VOID"; - case BIT: - return "BIT"; - case STRUCT: - return "STRUCT"; - case UNION: - return "UNION"; - case ENUM: - return "ENUM"; - case ELIPSIS: - return "ELIPSIS"; - case RANGE: - return "RANGE"; - case FAR: - return "FAR"; - case CASE: - return "CASE"; - case DEFAULT: - return "DEFAULT"; - case IF: - return "IF"; - case ELSE: - return "ELSE"; - case SWITCH: - return "SWITCH"; - case WHILE: - return "WHILE"; - case DO: - return "DO"; - case FOR: - return "FOR"; - case GOTO: - return "GOTO"; - case CONTINUE: - return "CONTINUE"; - case BREAK: - return "BREAK"; - case RETURN: - return "RETURN"; - case INLINEASM: - return "INLINEASM"; - case IFX: - return "IFX"; - case ADDRESS_OF: - return "ADDRESS_OF"; - case GET_VALUE_AT_ADDRESS: - return "GET_VALUE_AT_ADDRESS"; - case SPIL: - return "SPIL"; - case UNSPIL: - return "UNSPIL"; - case GETHBIT: - return "GETHBIT"; - case BITWISEAND: - return "BITWISEAND"; - case UNARYMINUS: - return "UNARYMINUS"; - case IPUSH: - return "IPUSH"; - case IPOP: - return "IPOP"; - case PCALL: - return "PCALL"; - case ENDFUNCTION: - return "ENDFUNCTION"; - case JUMPTABLE: - return "JUMPTABLE"; - case RRC: - return "RRC"; - case RLC: - return "RLC"; - case CAST: - return "CAST"; - case CALL: - return "CALL"; - case PARAM: - return "PARAM "; - case NULLOP: - return "NULLOP"; - case BLOCK: - return "BLOCK"; - case LABEL: - return "LABEL"; - case RECEIVE: - return "RECEIVE"; - case SEND: - return "SEND"; + case IDENTIFIER: return "IDENTIFIER"; + case TYPE_NAME: return "TYPE_NAME"; + case CONSTANT: return "CONSTANT"; + case STRING_LITERAL: return "STRING_LITERAL"; + case SIZEOF: return "SIZEOF"; + case PTR_OP: return "PTR_OP"; + case INC_OP: return "INC_OP"; + case DEC_OP: return "DEC_OP"; + case LEFT_OP: return "LEFT_OP"; + case RIGHT_OP: return "RIGHT_OP"; + case LE_OP: return "LE_OP"; + case GE_OP: return "GE_OP"; + case EQ_OP: return "EQ_OP"; + case NE_OP: return "NE_OP"; + case AND_OP: return "AND_OP"; + case OR_OP: return "OR_OP"; + case MUL_ASSIGN: return "MUL_ASSIGN"; + case DIV_ASSIGN: return "DIV_ASSIGN"; + case MOD_ASSIGN: return "MOD_ASSIGN"; + case ADD_ASSIGN: return "ADD_ASSIGN"; + case SUB_ASSIGN: return "SUB_ASSIGN"; + case LEFT_ASSIGN: return "LEFT_ASSIGN"; + case RIGHT_ASSIGN: return "RIGHT_ASSIGN"; + case AND_ASSIGN: return "AND_ASSIGN"; + case XOR_ASSIGN: return "XOR_ASSIGN"; + case OR_ASSIGN: return "OR_ASSIGN"; + case TYPEDEF: return "TYPEDEF"; + case EXTERN: return "EXTERN"; + case STATIC: return "STATIC"; + case AUTO: return "AUTO"; + case REGISTER: return "REGISTER"; + case CODE: return "CODE"; + case EEPROM: return "EEPROM"; + case INTERRUPT: return "INTERRUPT"; + case SFR: return "SFR"; + case AT: return "AT"; + case SBIT: return "SBIT"; + case REENTRANT: return "REENTRANT"; + case USING: return "USING"; + case XDATA: return "XDATA"; + case DATA: return "DATA"; + case IDATA: return "IDATA"; + case PDATA: return "PDATA"; + case VAR_ARGS: return "VAR_ARGS"; + case CRITICAL: return "CRITICAL"; + case NONBANKED: return "NONBANKED"; + case BANKED: return "BANKED"; + case CHAR: return "CHAR"; + case SHORT: return "SHORT"; + case INT: return "INT"; + case LONG: return "LONG"; + case SIGNED: return "SIGNED"; + case UNSIGNED: return "UNSIGNED"; + case FLOAT: return "FLOAT"; + case DOUBLE: return "DOUBLE"; + case CONST: return "CONST"; + case VOLATILE: return "VOLATILE"; + case VOID: return "VOID"; + case BIT: return "BIT"; + case STRUCT: return "STRUCT"; + case UNION: return "UNION"; + case ENUM: return "ENUM"; + case ELIPSIS: return "ELIPSIS"; + case RANGE: return "RANGE"; + case FAR: return "FAR"; + case CASE: return "CASE"; + case DEFAULT: return "DEFAULT"; + case IF: return "IF"; + case ELSE: return "ELSE"; + case SWITCH: return "SWITCH"; + case WHILE: return "WHILE"; + case DO: return "DO"; + case FOR: return "FOR"; + case GOTO: return "GOTO"; + case CONTINUE: return "CONTINUE"; + case BREAK: return "BREAK"; + case RETURN: return "RETURN"; + case INLINEASM: return "INLINEASM"; + case IFX: return "IFX"; + case ADDRESS_OF: return "ADDRESS_OF"; + case GET_VALUE_AT_ADDRESS: return "GET_VALUE_AT_ADDRESS"; + case SPIL: return "SPIL"; + case UNSPIL: return "UNSPIL"; + case GETHBIT: return "GETHBIT"; + case BITWISEAND: return "BITWISEAND"; + case UNARYMINUS: return "UNARYMINUS"; + case IPUSH: return "IPUSH"; + case IPOP: return "IPOP"; + case PCALL: return "PCALL"; + case ENDFUNCTION: return "ENDFUNCTION"; + case JUMPTABLE: return "JUMPTABLE"; + case RRC: return "RRC"; + case RLC: return "RLC"; + case CAST: return "CAST"; + case CALL: return "CALL"; + case PARAM: return "PARAM "; + case NULLOP: return "NULLOP"; + case BLOCK: return "BLOCK"; + case LABEL: return "LABEL"; + case RECEIVE: return "RECEIVE"; + case SEND: return "SEND"; } sprintf (buffer, "unknown op %d %c", op, op & 0xff); return buffer; @@ -407,12 +306,9 @@ debugLogRegType (short type) switch (type) { - case REG_GPR: - return "REG_GPR"; - case REG_PTR: - return "REG_PTR"; - case REG_CND: - return "REG_CND"; + case REG_GPR: return "REG_GPR"; + case REG_PTR: return "REG_PTR"; + case REG_CND: return "REG_CND"; } sprintf (buffer, "unknown reg type %d", type); @@ -456,7 +352,6 @@ static regs* newReg(short type, short pc_type, int rIdx, char *name, int size, i sprintf(buffer,"r0x%02X", dReg->rIdx); dReg->name = Safe_strdup(buffer); } - //fprintf(stderr,"newReg: %s, rIdx = 0x%02x\n",dReg->name,rIdx); dReg->isFree = 0; dReg->wasUsed = 1; if (type == REG_SFR) @@ -476,7 +371,7 @@ static regs* newReg(short type, short pc_type, int rIdx, char *name, int size, i dReg->reglives.assignedpFlows = newSet(); hTabAddItem(&dynDirectRegNames, regname2key(name), dReg); - + return dReg; } @@ -628,12 +523,20 @@ allocInternalRegister(int rIdx, char * name, short po_type, int alias) static regs * allocReg (short type) { + regs *reg; debugLog ("%s of type %s\n", __FUNCTION__, debugLogRegType (type)); //fprintf(stderr,"allocReg\n"); + reg = pic14_findFreeReg (type); + + reg->isFree = 0; + reg->wasUsed = 1; + + return reg; + - return addSet(&dynAllocRegs,newReg(REG_GPR, PO_GPR_TEMP,dynrIdx++,NULL,1,0)); + //return addSet(&dynAllocRegs,newReg(REG_GPR, PO_GPR_TEMP,dynrIdx++,NULL,1,0)); } @@ -683,10 +586,11 @@ allocNewDirReg (sym_link *symlnk,const char *name) { regs *reg; int address = 0; + sym_link *spec = getSpec (symlnk); /* if this is at an absolute address, then get the address. */ - if (SPEC_ABSA (symlnk) ) { - address = SPEC_ADDR (symlnk); + if (SPEC_ABSA (spec) ) { + address = SPEC_ADDR (spec); //fprintf(stderr,"reg %s is at an absolute address: 0x%03x\n",name,address); } @@ -699,7 +603,7 @@ allocNewDirReg (sym_link *symlnk,const char *name) } else { int idx; if (address) { - if (IS_BITVAR (symlnk)) + if (IS_BITVAR (spec)) idx = address >> 3; else idx = address; @@ -709,20 +613,20 @@ allocNewDirReg (sym_link *symlnk,const char *name) reg = newReg(REG_GPR, PO_DIR, idx, (char*)name,getSize (symlnk),0 ); debugLog (" -- added %s to hash, size = %d\n", (char*)name,reg->size); - if (SPEC_ABSA (symlnk) ) { + if (SPEC_ABSA (spec) ) { reg->type = REG_SFR; } - if (IS_BITVAR (symlnk)) { + if (IS_BITVAR (spec)) { addSet(&dynDirectBitRegs, reg); reg->isBitField = 1; } else addSet(&dynDirectRegs, reg); - if (!IS_STATIC (symlnk)) { + if (!IS_STATIC (spec)) { reg->isPublic = 1; } - if (IS_EXTERN (symlnk)) { + if (IS_EXTERN (spec)) { reg->isExtern = 1; } @@ -862,7 +766,7 @@ allocDirReg (operand *op ) debugLog (" -- and it is at a fixed address 0x%02x\n",reg->address); } } else { - allocNewDirReg (OP_SYM_ETYPE(op),name); + allocNewDirReg (OP_SYM_TYPE(op),name); } return reg; @@ -893,7 +797,7 @@ allocRegByName (char *name, int size) /* Register wasn't found in hash, so let's create * a new one and put it in the hash table AND in the * dynDirectRegNames set */ - //fprintf (stderr,"%s symbol name %s\n", __FUNCTION__,name); + //fprintf (stderr,"%s symbol name %s, size:%d\n", __FUNCTION__,name,size); reg = newReg(REG_GPR, PO_DIR, rDirectIdx++, name,size,0 ); for (sym = setFirstItem(sfr->syms); sym; sym = setNextItem(sfr->syms)) { if (strcmp(reg->name+1,sym->name)==0) { @@ -1281,6 +1185,7 @@ void writeUsedRegs(FILE *of) assignRelocatableRegisters(dynAllocRegs,0); assignRelocatableRegisters(dynStackRegs,0); + assignRelocatableRegisters(dynDirectRegs,0); /* assignRelocatableRegisters(dynDirectRegs,0); printf("assignRelocatableRegisters(dynDirectRegs,0);\n"); @@ -1998,7 +1903,7 @@ tryAgain: if (sym->regs[j]) sym->regs[j]->isFree = 0; - /* this looks like an infinite loop but + /* this looks like an infinite loop but in really selectSpil will abort */ goto tryAgain; } @@ -2039,7 +1944,36 @@ deassignLRs (iCode * ic, eBBlock * ebp) /* if it does not end here */ if (sym->liveTo > ic->seq) continue; - + + /* Prevent the result from being assigned the same registers as (one) + * operand as many genXXX-functions fail otherwise. + * POINTER_GET(ic) || ic->op == LEFT_OP || ic->op == RIGHT_OP || ic->op == NOT + * are known to fail. */ + if (sym->liveTo == ic->seq && IC_RESULT(ic)) + { + switch (ic->op) + { + case '=': /* assignment */ + case BITWISEAND: /* bitwise AND */ + case '|': /* bitwise OR */ + case '^': /* bitwise XOR */ + case '~': /* bitwise negate */ + case RLC: /* rotate through carry */ + case RRC: + case UNARYMINUS: + case '+': /* addition */ + case '-': /* subtraction */ + /* go ahead, these are safe to use with + * non-disjoint register sets */ + break; + + default: + /* do not release operand registers */ + //fprintf (stderr, "%s:%u: operand not freed: ", __FILE__, __LINE__); piCode (ic, stderr); fprintf (stderr, "\n"); + continue; + } // switch + } + /* if it was spilt on stack then we can mark the stack spil location as free */ if (sym->isspilt) @@ -2054,7 +1988,6 @@ deassignLRs (iCode * ic, eBBlock * ebp) if (!bitVectBitValue (_G.regAssigned, sym->key)) continue; - /* special case check if this is an IFX & the privious one was a pop and the previous one was not spilt then keep track @@ -2062,6 +1995,7 @@ deassignLRs (iCode * ic, eBBlock * ebp) if (ic->op == IFX && ic->prev && ic->prev->op == IPOP && !ic->prev->parmPush && + IS_SYMOP(IC_LEFT (ic->prev)) && !OP_SYMBOL (IC_LEFT (ic->prev))->isspilt) psym = OP_SYMBOL (IC_LEFT (ic->prev)); @@ -2082,6 +2016,7 @@ deassignLRs (iCode * ic, eBBlock * ebp) ic->op == IPOP || ic->op == RETURN || POINTER_SET (ic)) && + IS_SYMOP (IC_RESULT (ic)) && (result = OP_SYMBOL (IC_RESULT (ic))) && /* has a result */ result->liveTo > ic->seq && /* and will live beyond this */ result->liveTo <= ebp->lSeq && /* does not go beyond this block */ @@ -2108,7 +2043,7 @@ deassignLRs (iCode * ic, eBBlock * ebp) _G.regAssigned = bitVectSetBit (_G.regAssigned, result->key); } - + /* free the remaining */ for (; i < sym->nRegs; i++) { @@ -2273,7 +2208,6 @@ serialRegAssign (eBBlock ** ebbs, int count) /* of all instructions do */ for (ic = ebbs[i]->sch; ic; ic = ic->next) { - debugLog (" op: %s\n", decodeOp (ic->op)); /* if this is an ipop that means some live @@ -2301,7 +2235,7 @@ serialRegAssign (eBBlock ** ebbs, int count) /* now we need to allocate registers only for the result */ - if (IC_RESULT (ic)) + if (IC_RESULT (ic) && IS_SYMOP (IC_RESULT (ic))) { symbol *sym = OP_SYMBOL (IC_RESULT (ic)); bitVect *spillable; @@ -2387,8 +2321,10 @@ serialRegAssign (eBBlock ** ebbs, int count) /* if we need ptr regs for the right side then mark it */ - if (POINTER_GET (ic) && getSize (OP_SYMBOL (IC_LEFT (ic))->type) - <= (unsigned) PTRSIZE) + if (POINTER_GET (ic) + && IS_SYMOP(IC_LEFT(ic)) + && getSize (OP_SYMBOL (IC_LEFT (ic))->type) + <= (unsigned) PTRSIZE) { pic14_ptrRegReq++; ptrRegSet = 1; @@ -2416,11 +2352,13 @@ serialRegAssign (eBBlock ** ebbs, int count) /* if it shares registers with operands make sure that they are in the same position */ if (IC_LEFT (ic) && IS_SYMOP (IC_LEFT (ic)) && + IS_SYMOP(IC_RESULT(ic)) && OP_SYMBOL (IC_LEFT (ic))->nRegs && ic->op != '=') positionRegs (OP_SYMBOL (IC_RESULT (ic)), OP_SYMBOL (IC_LEFT (ic)), ic->lineno); /* do the same for the right operand */ if (IC_RIGHT (ic) && IS_SYMOP (IC_RIGHT (ic)) && + IS_SYMOP(IC_RESULT(ic)) && OP_SYMBOL (IC_RIGHT (ic))->nRegs && ic->op != '=') positionRegs (OP_SYMBOL (IC_RESULT (ic)), OP_SYMBOL (IC_RIGHT (ic)), ic->lineno); @@ -3493,10 +3431,22 @@ isBitwiseOptimizable (iCode * ic) static void packRegsForAccUse (iCode * ic) { - iCode *uic; + //iCode *uic; debugLog ("%s\n", __FUNCTION__); + + /* result too large for WREG? */ + if (getSize (operandType (IC_RESULT (ic))) > 1) + return; + /* We have to make sure that OP_SYMBOL(IC_RESULT(ic)) + * is never used as an operand to an instruction that + * cannot have WREG as an operand (e.g. BTFSx cannot + * operate on WREG... + * For now, store all results into proper registers. */ + return; + +#if 0 /* if this is an aggregate, e.g. a one byte char array */ if (IS_AGGREGATE(operandType(IC_RESULT(ic)))) { return; @@ -3633,8 +3583,7 @@ packRegsForAccUse (iCode * ic) accuse: debugLog ("%s - Yes we are using the accumulator\n", __FUNCTION__); OP_SYMBOL (IC_RESULT (ic))->accuse = 1; - - +#endif } /*-----------------------------------------------------------------*/ @@ -3918,13 +3867,13 @@ packRegisters (eBBlock * ebp) } /* mark the pointer usages */ - if (POINTER_SET (ic)) + if (POINTER_SET (ic) && IS_SYMOP(IC_RESULT(ic))) { OP_SYMBOL (IC_RESULT (ic))->uptr = 1; debugLog (" marking as a pointer (set) =>"); debugAopGet (" result:", IC_RESULT (ic)); } - if (POINTER_GET (ic)) + if (POINTER_GET (ic) && IS_SYMOP(IC_LEFT(ic))) { OP_SYMBOL (IC_LEFT (ic))->uptr = 1; debugLog (" marking as a pointer (get) =>"); @@ -3996,6 +3945,7 @@ packRegisters (eBBlock * ebp) one and right is not in far space */ if (POINTER_SET (ic) && !isOperandInFarSpace (IC_RIGHT (ic)) && + IS_SYMOP(IC_RESULT(ic)) && !OP_SYMBOL (IC_RESULT (ic))->remat && !IS_OP_RUONLY (IC_RIGHT (ic)) && getSize (aggrToPtr (operandType (IC_RESULT (ic)), FALSE)) > 1) @@ -4005,6 +3955,7 @@ packRegisters (eBBlock * ebp) /* if pointer get */ if (POINTER_GET (ic) && !isOperandInFarSpace (IC_RESULT (ic)) && + IS_SYMOP(IC_LEFT(ic)) && !OP_SYMBOL (IC_LEFT (ic))->remat && !IS_OP_RUONLY (IC_RESULT (ic)) && getSize (aggrToPtr (operandType (IC_LEFT (ic)), FALSE)) > 1)