X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2Fpic16%2Fgen.c;h=e474f6c0901fea2841419e33daeab67f8ab5cf66;hb=35dd84251b9d153bb48ad98be9c5eae77799cb42;hp=1d4021ffa20c56194ec35d41b4f261f46dd6ca9b;hpb=ebafd5b16dab393ffce489e5ea7aabcca02b652c;p=fw%2Fsdcc diff --git a/src/pic16/gen.c b/src/pic16/gen.c index 1d4021ff..e474f6c0 100644 --- a/src/pic16/gen.c +++ b/src/pic16/gen.c @@ -57,7 +57,10 @@ extern void pic16_printpBlock(FILE *of, pBlock *pb); static asmop *newAsmop (short type); static pCodeOp *pic16_popRegFromString(char *str, int size, int offset, operand *op); extern pCode *pic16_newpCodeAsmDir(char *asdir, char *argfmt, ...); -static void mov2w (asmop *aop, int offset); +static void mov2f(asmop *dst, asmop *src, int offset); +static void mov2fp(pCodeOp *dst, asmop *src, int offset); +static pCodeOp *pic16_popRegFromIdx(int rIdx); + //static int aopIdx (asmop *aop, int offset); int pic16_labelOffset=0; @@ -68,6 +71,8 @@ static int optimized_for_speed = 0; */ +extern set *externs; + /* max_key keeps track of the largest label number used in a function. This is then used to adjust the label offset for the next function. @@ -76,7 +81,6 @@ static int max_key=0; static int GpsuedoStkPtr=0; pCodeOp *pic16_popGetImmd(char *name, unsigned int offset, int index); -pCodeOp *pic16_popGet2p(pCodeOp *src, pCodeOp *dst); unsigned int pic16aopLiteral (value *val, int offset); const char *pic16_AopType(short type); @@ -91,6 +95,11 @@ static bool is_LitAOp(asmop *aop); #define BYTEofLONG(l,b) ( (l>> (b<<3)) & 0xff) +/* set the following macro to 1 to enable passing the + * first byte of functions parameters via WREG */ +#define USE_WREG_IN_FUNC_PARAMS 0 + + /* this is the down and dirty file with all kinds of kludgy & hacky stuff. This is what it is all about CODE GENERATION for a specific MCU . some of the @@ -98,7 +107,7 @@ static bool is_LitAOp(asmop *aop); static char *zero = "#0x00"; static char *one = "#0x01"; -static char *spname = "sp"; +//static char *spname = "sp"; /* @@ -112,8 +121,7 @@ static char *spname = "sp"; */ -char *fReturnpic16[] = {"temp1","temp2","temp3","temp4" }; -//char *fReturn390[] = {"dpl","dph","dpx", "b","a" }; +char *fReturnpic16[] = {"WREG", "PRODL", "PRODH", "FSR0L" }; unsigned pic16_fReturnSizePic = 4; /* shared with ralloc.c */ static char **fReturn = fReturnpic16; @@ -131,8 +139,13 @@ static struct { short nRegsSaved; short ipushRegs; set *sendSet; + set *stackRegSet; int interruptvector; int usefastretfie; + bitVect *fregsUsed; + int stack_lat; /* stack offset latency */ + int resDirect; + int useWreg; /* flag when WREG is used to pass function parameter */ } _G; /* Resolved ifx structure. This structure stores information @@ -148,7 +161,7 @@ typedef struct resolvedIfx { extern int pic16_ptrRegReq ; extern int pic16_nRegs; extern FILE *codeOutFile; -static void saverbank (int, iCode *,bool); +//static void saverbank (int, iCode *,bool); static lineNode *lineHead = NULL; static lineNode *lineCurr = NULL; @@ -169,7 +182,7 @@ static pBlock *pb; /* return y; */ /* return -1; */ /*-----------------------------------------------------------------*/ -static int my_powof2 (unsigned long num) +int pic16_my_powof2 (unsigned long num) { if(num) { if( (num & (num-1)) == 0) { @@ -273,6 +286,8 @@ void DEBUGpic16_emitcode (char *inst,char *fmt, ...) // fprintf(stderr, "%s\n", lb); } + + void pic16_emitpLabel(int key) { pic16_addpCode2pBlock(pb,pic16_newpCodeLabel(NULL,key+100+pic16_labelOffset)); @@ -290,10 +305,16 @@ void pic16_emitpcode(PIC_OPCODE poc, pCodeOp *pcop) pic16_addpCode2pBlock(pb,pic16_newpCode(poc,pcop)); else DEBUGpic16_emitcode(";","%s ignoring NULL pcop",__FUNCTION__); - -// fprintf(stderr, "%s\n", pcop->name); } +void pic16_emitpinfo(INFO_TYPE itype, pCodeOp *pcop) +{ + if(pcop) + pic16_addpCode2pBlock(pb, pic16_newpCodeInfo(itype, pcop)); + else + DEBUGpic16_emitcode(";","%s ignoring NULL pcop",__FUNCTION__); +} + void pic16_emitpcodeNULLop(PIC_OPCODE poc) { @@ -381,6 +402,16 @@ static regs *getFreePtr (iCode *ic, asmop **aopp, bool result) DEBUGpic16_emitcode("%s:%d WREG is used by this ic", __FILE__, __LINE__); } + if(!fsr0iu && !fsr0ou) { + ic->rUsed = bitVectSetBit(ic->rUsed, IDX_FSR0); + (*aopp)->type = AOP_FSR0; + + fprintf(stderr, "%s:%d returning plain FSR0\n", __FILE__, __LINE__); + + return ((*aopp)->aopu.aop_ptr = pic16_regWithIdx(IDX_FSR0)); + } + +#if 0 /* no usage of FSR2 */ if(!fsr2iu && !fsr2ou) { ic->rUsed = bitVectSetBit(ic->rUsed, IDX_FSR2); @@ -388,14 +419,8 @@ static regs *getFreePtr (iCode *ic, asmop **aopp, bool result) return (*aopp)->aopu.aop_ptr = pic16_regWithIdx(IDX_FSR2); } +#endif - if(!fsr0iu && !fsr0ou) { - ic->rUsed = bitVectSetBit(ic->rUsed, IDX_FSR0); - (*aopp)->type = AOP_FSR0; - - return ((*aopp)->aopu.aop_ptr = pic16_regWithIdx(IDX_FSR0)); - } - /* now we know they both have usage */ /* if fsr0 not used in this instruction */ if (!fsr0iu) { @@ -408,6 +433,8 @@ static regs *getFreePtr (iCode *ic, asmop **aopp, bool result) ic->rUsed = bitVectSetBit (ic->rUsed, IDX_FSR0); (*aopp)->type = AOP_FSR0; +// fprintf(stderr, "%s:%d returning FSR0 after pushing value to stack\n", __FILE__, __LINE__); + return (*aopp)->aopu.aop_ptr = pic16_regWithIdx (IDX_FSR0); } @@ -434,7 +461,7 @@ static regs *getFreePtr (iCode *ic, asmop **aopp, bool result) /* if no usage of r0 then return it */ if (!r0iu && !r0ou) { ic->rUsed = bitVectSetBit(ic->rUsed,R0_IDX); - (*aopp)->type = AOP_R0; + (*aopp)->type = AOP_R0; return (*aopp)->aopu.aop_ptr = pic16_regWithIdx(R0_IDX); } @@ -525,8 +552,9 @@ static void genSetDPTR(int n) /*-----------------------------------------------------------------*/ static void resolveIfx(resolvedIfx *resIfx, iCode *ifx) { - - DEBUGpic16_emitcode("; ***","%s %d",__FUNCTION__,__LINE__); + FENTRY2; + +// DEBUGpic16_emitcode("; ***","%s %d",__FUNCTION__,__LINE__); if(!resIfx) return; @@ -553,9 +581,13 @@ static void resolveIfx(resolvedIfx *resIfx, iCode *ifx) #if 1 if(IC_TRUE(ifx)) - DEBUGpic16_emitcode("; ***","ifx true is non-null"); + DEBUGpic16_emitcode("; +++","ifx true is non-null"); + else + DEBUGpic16_emitcode("; +++","ifx true is null"); if(IC_FALSE(ifx)) - DEBUGpic16_emitcode("; ***","ifx false is non-null"); + DEBUGpic16_emitcode("; +++","ifx false is non-null"); + else + DEBUGpic16_emitcode("; +++","ifx false is null"); #endif } @@ -583,8 +615,10 @@ static asmop *aopForSym (iCode *ic, operand *op, bool result) asmop *aop; memmap *space= SPEC_OCLS(sym->etype); - DEBUGpic16_emitcode("; ***","%s %d",__FUNCTION__,__LINE__); - + FENTRY2; + + _G.resDirect = 0; /* clear flag that instructs the result is loaded directly from aopForSym */ + // sym = OP_SYMBOL(op); /* if already has one */ @@ -593,52 +627,68 @@ static asmop *aopForSym (iCode *ic, operand *op, bool result) return sym->aop; } +#if 0 /* if symbol was initially placed onStack then we must re-place it * to direct memory, since pic16 does not have a specific stack */ if(sym->onStack) { fprintf(stderr, "%s:%d symbol %s on stack\n", __FILE__, __LINE__, OP_SYMBOL(op)->name); } - +#endif #if 1 /* assign depending on the storage class */ /* if it is on the stack or indirectly addressable */ /* space we need to assign either r0 or r1 to it */ - if ((sym->onStack && !options.stack10bit) || sym->iaccess) { - + if (sym->onStack || sym->iaccess) { + pCodeOp *pcop[4]; + int i; + DEBUGpic16_emitcode("; ***", "%s:%d sym->onStack:%d || sym->iaccess:%d", __FUNCTION__, __LINE__, sym->onStack, sym->iaccess); - sym->aop = aop = newAsmop(0); - aop->aopu.aop_ptr = getFreePtr(ic,&aop,result); + /* acquire a temporary register -- it is saved in function */ + + sym->aop = aop = newAsmop(AOP_STA); + aop->aopu.stk.stk = sym->stack; aop->size = getSize(sym->type); - fprintf(stderr, "%s:%d\t%s\n", __FILE__, __LINE__, __FUNCTION__); + + DEBUGpic16_emitcode("; +++", "%s:%d", __FILE__, __LINE__); + pic16_DumpAop("aopForSym", AOP( IC_RESULT(ic) )); + if((ic->op == '=') && IC_RESULT(ic) && AOP( IC_RESULT(ic) ) + && (AOP_TYPE(IC_RESULT(ic)) == AOP_REG) ) { + + for(i=0;isize;i++) + aop->aopu.stk.pop[i] = pcop[i] = pic16_popRegFromIdx( AOP(IC_RESULT(ic))->aopu.aop_reg[i]->rIdx); + _G.resDirect = 1; /* notify that result will be loaded directly from aopForSym */ + } else + for(i=0;isize;i++) { + aop->aopu.stk.pop[i] = pcop[i] = pic16_popGetTempRegCond( _G.fregsUsed, 0 ); + _G.fregsUsed = bitVectSetBit(_G.fregsUsed, PCOR(pcop[i])->r->rIdx); + } + + +// fprintf(stderr, "%s:%d\t%s\tsym size %d\n", __FILE__, __LINE__, __FUNCTION__, aop->size); #if 1 -// sym->aop = aop = newAsmop (AOP_REG); -// aop->aopu.aop_dir = sym->name; //sym->rname ; -// aop->aopu.aop_reg[0] = pic16_regWithIdx(IDX_PLUSW0); //pic16_pc_plusw2.r; -// aop->size = getSize(sym->type); - DEBUGpic16_emitcode(";","%d sym->rname = %s, size = %d",__LINE__,sym->rname,aop->size); - -// if(_G.accInUse) { -// pic16_pushpCodeOp( pic16_popCopyReg(&pic16_pc_wreg) ); -// } - -// pic16_emitpcode(POC_MOVFF, pic16_popGet2p( pic16_popCopyReg(&pic16_pc_fsr2l), pic16_popCopyReg(&pic16_pc_fsr0l))); -// pic16_emitpcode(POC_MOVFF, pic16_popGet2p( pic16_popCopyReg(&pic16_pc_fsr2h), pic16_popCopyReg(&pic16_pc_fsr0h))); + DEBUGpic16_emitcode(";","%d sym->rname = %s, size = %d stack = %d",__LINE__,sym->rname,aop->size, sym->stack); + if(_G.accInUse) { + pic16_pushpCodeOp( pic16_popCopyReg(&pic16_pc_wreg) ); + } - /* initialise for stack access via frame pointer */ - pic16_emitpcode(POC_MOVLW, pic16_popGetLit(sym->stack)); + for(i=0;isize;i++) { -// pic16_emitpcode(POC_MOVFF, pic16_popGet2p( -// pic16_popCopyReg(&pic16_pc_plusw2), pic16_popCopyReg(&pic16_pc_kzero))); + /* initialise for stack access via frame pointer */ + // operands on stack are accessible via "FSR2 - index" with index starting at 0 for the first operand + pic16_emitpcode(POC_MOVLW, pic16_popGetLit((sym->stack + 1 + i /*+ _G.stack_lat*/))); + pic16_emitpcode(POC_MOVFF, pic16_popGet2p( + pic16_popCopyReg(&pic16_pc_plusw2), pcop[i])); + } -// if(_G.accInUse) { -// pic16_poppCodeOp( pic16_popCopyReg(&pic16_pc_wreg) ); -// } + if(_G.accInUse) { + pic16_poppCodeOp( pic16_popCopyReg(&pic16_pc_wreg) ); + } return (aop); #endif @@ -707,6 +757,23 @@ static asmop *aopForSym (iCode *ic, operand *op, bool result) return aop; } #endif + +#if 0 + /* special case for a function */ + if (IS_FUNC(sym->type)) { + sym->aop = aop = newAsmop(AOP_PCODE); + //_ALLOC_ATOMIC(aop->aopu.aop_immd,strlen(sym->rname)+1); + aop->aopu.pcop = pic16_popGetImmd(sym->rname, 0, 0); + PCOI(aop->aopu.pcop)->_const = IN_CODESPACE(space); + PCOI(aop->aopu.pcop)->index = 0; + aop->size = FPTRSIZE; + DEBUGpic16_emitcode(";","%d size = %d, name =%s",__LINE__,aop->size,sym->rname); + return aop; + } +#endif + + + //DEBUGpic16_emitcode(";","%d",__LINE__); /* if in bit space */ if (IN_BITSPACE(space)) { @@ -726,7 +793,8 @@ static asmop *aopForSym (iCode *ic, operand *op, bool result) return aop; } - if (IN_FARSPACE(space)) { + + if (IN_FARSPACE(space) && !IN_CODESPACE(space)) { sym->aop = aop = newAsmop (AOP_DIR); aop->aopu.aop_dir = sym->rname ; aop->size = getSize(sym->type); @@ -735,22 +803,8 @@ static asmop *aopForSym (iCode *ic, operand *op, bool result) return aop; } -#if 0 // patch 14 - /* special case for a function */ - if (IS_FUNC(sym->type)) { - sym->aop = aop = newAsmop(AOP_IMMD); - //_ALLOC_ATOMIC(aop->aopu.aop_immd,strlen(sym->rname)+1); - aop->aopu.aop_immd = Safe_calloc(1,strlen(sym->rname)+1); - strcpy(aop->aopu.aop_immd,sym->rname); - aop->size = FPTRSIZE; - DEBUGpic16_emitcode(";","%d size = %d, name =%s",__LINE__,aop->size,sym->rname); - return aop; - } -#endif // patch 14 - /* only remaining is far space */ - /* in which case DPTR gets the address */ sym->aop = aop = newAsmop(AOP_PCODE); /* change the next if to 1 to revert to good old immediate code */ @@ -778,7 +832,9 @@ static asmop *aopForSym (iCode *ic, operand *op, bool result) aop->size = FPTRSIZE; else if(IC_LEFT(ic)) aop->size = AOP_SIZE( IC_LEFT(ic) ); else if(IC_RIGHT(ic)) aop->size = AOP_SIZE( IC_RIGHT(ic) ); - else { + else if(sym->onStack) { + aop->size = PTRSIZE; + } else { assert( 0 ); } @@ -797,17 +853,17 @@ static asmop *aopForSym (iCode *ic, operand *op, bool result) static asmop *aopForRemat (operand *op) // x symbol *sym) { symbol *sym = OP_SYMBOL(op); + operand *refop; iCode *ic = NULL, *oldic; asmop *aop = newAsmop(AOP_PCODE); int val = 0; int offset = 0; int viaimmd=0; - + FENTRY2; + ic = sym->rematiCode; - DEBUGpic16_emitcode(";","%s %d",__FUNCTION__,__LINE__); - if(IS_OP_POINTER(op)) { DEBUGpic16_emitcode(";","%s %d IS_OP_POINTER",__FUNCTION__,__LINE__); } @@ -828,14 +884,15 @@ static asmop *aopForRemat (operand *op) // x symbol *sym) } offset = OP_SYMBOL(IC_LEFT(ic))->offset; + refop = IC_LEFT(ic); if(!op->isaddr)viaimmd++; else viaimmd=0; /* set the following if to 1 to revert to good old immediate code */ - if(IN_CODESPACE( SPEC_OCLS( OP_SYM_ETYPE(op))) + if(IN_CODESPACE( SPEC_OCLS( OP_SYM_ETYPE(refop))) || viaimmd) { - DEBUGpic16_emitcode("%s:%d immediate", __FILE__, __LINE__); + DEBUGpic16_emitcode(";", "%s:%d immediate, size: %d", __FILE__, __LINE__, getSize( sym->type )); aop->aopu.pcop = pic16_popGetImmd(OP_SYMBOL(IC_LEFT(ic))->rname, 0, val); @@ -846,12 +903,15 @@ static asmop *aopForRemat (operand *op) // x symbol *sym) #endif PCOI(aop->aopu.pcop)->index = val; + + aop->size = getSize( sym->type ); } else { - DEBUGpic16_emitcode("%s:%d dir", __FILE__, __LINE__); + DEBUGpic16_emitcode(";", "%s:%d dir size: %d", __FILE__, __LINE__, getSize( OP_SYMBOL( IC_LEFT(ic))->type)); aop->aopu.pcop = pic16_popRegFromString(OP_SYMBOL(IC_LEFT(ic))->rname, getSize( OP_SYMBOL( IC_LEFT(ic))->type), val, op); -// aop->size = AOP_SIZE( IC_LEFT(ic) ); + + aop->size = getSize( OP_SYMBOL( IC_LEFT(ic))->type ); } @@ -990,14 +1050,31 @@ bool pic16_sameRegs (asmop *aop1, asmop *aop2 ) if (aop1->size != aop2->size ) return FALSE ; - for (i = 0 ; i < aop1->size ; i++ ) - if (aop1->aopu.aop_reg[i] != - aop2->aopu.aop_reg[i] ) + for (i = 0 ; i < aop1->size ; i++ ) { +// if(aop1->aopu.aop_reg[i]->type != aop2->aopu.aop_reg[i]->type)return FALSE; + +// if(aop1->aopu.aop_reg[i]->type == AOP_REG) + if (strcmp(aop1->aopu.aop_reg[i]->name, aop2->aopu.aop_reg[i]->name )) return FALSE ; + } return TRUE ; } +bool pic16_sameRegsOfs(asmop *aop1, asmop *aop2, int offset) +{ + DEBUGpic16_emitcode(";***", "%s aop1->type = %s\taop2->type = %s (offset = %d)\n", __FUNCTION__, + pic16_AopType(aop1->type), pic16_AopType(aop2->type), offset); + + if(aop1 == aop2)return TRUE; + if(aop1->type != AOP_REG || aop2->type != AOP_REG)return FALSE; + + if(strcmp(aop1->aopu.aop_reg[offset]->name, aop2->aopu.aop_reg[offset]->name))return FALSE; + + return TRUE; +} + + /*-----------------------------------------------------------------*/ /* pic16_aopOp - allocates an asmop for an operand : */ /*-----------------------------------------------------------------*/ @@ -1010,7 +1087,7 @@ void pic16_aopOp (operand *op, iCode *ic, bool result) if (!op) return ; -// DEBUGpic16_emitcode(";","%s %d",__FUNCTION__, __LINE__); + DEBUGpic16_emitcode(";","%s %d",__FUNCTION__, __LINE__); /* if this a literal */ if (IS_OP_LITERAL(op)) { @@ -1058,7 +1135,7 @@ void pic16_aopOp (operand *op, iCode *ic, bool result) sym = OP_SYMBOL(op); - DEBUGpic16_emitcode("; ***", "%d: symbol name = %s", __LINE__, sym->name); + DEBUGpic16_emitcode("; ***", "%d: symbol name = %s, regType = %d", __LINE__, sym->name, sym->regType); /* if the type is a conditional */ if (sym->regType == REG_CND) { aop = op->aop = sym->aop = newAsmop(AOP_CRY); @@ -1075,10 +1152,9 @@ void pic16_aopOp (operand *op, iCode *ic, bool result) /* rematerialize it NOW */ if (sym->remat) { - sym->aop = op->aop = aop = - aopForRemat (op); - aop->size = getSize(sym->type); - //DEBUGpic16_emitcode(";"," %d: size %d, %s\n",__LINE__,aop->size,aop->aopu.aop_immd); + sym->aop = op->aop = aop = aopForRemat (op); +// aop->size = getSize(sym->type); +// DEBUGpic16_emitcode(";"," %d: size %d, %s\n",__LINE__,aop->size,aop->aopu.aop_immd); return; } @@ -1122,10 +1198,13 @@ void pic16_aopOp (operand *op, iCode *ic, bool result) /* force a new aop if sizes differ */ sym->usl.spillLoc->aop = NULL; } + +#if 0 DEBUGpic16_emitcode(";","%s %d %s sym->rname = %s, offset %d", __FUNCTION__,__LINE__, sym->usl.spillLoc->rname, sym->rname, sym->usl.spillLoc->offset); +#endif sym->aop = op->aop = aop = newAsmop(AOP_PCODE); //aop->aopu.pcop = pic16_popGetImmd(sym->usl.spillLoc->rname,0,sym->usl.spillLoc->offset); @@ -1215,6 +1294,32 @@ void pic16_freeAsmop (operand *op, asmop *aaop, iCode *ic, bool pop) bitVectUnSetBit(ic->rUsed,R1_IDX); break; + case AOP_STA: + { + int i; + + /* we must store the result on stack */ + if((op == IC_RESULT(ic)) && RESULTONSTA(ic)) { + if(_G.accInUse)pic16_pushpCodeOp( pic16_popCopyReg(&pic16_pc_wreg) ); + for(i=0;isize;i++) { + /* initialise for stack access via frame pointer */ + // operands on stack are accessible via "FSR2 - index" with index starting at 0 for the first operand + pic16_emitpcode(POC_MOVLW, pic16_popGetLit((OP_SYMBOL(IC_RESULT(ic))->stack + 1 + i /*+ _G.stack_lat*/))); + pic16_emitpcode(POC_MOVFF, pic16_popGet2p( + aop->aopu.stk.pop[i], pic16_popCopyReg(&pic16_pc_plusw2))); + } + + if(_G.accInUse)pic16_poppCodeOp( pic16_popCopyReg(&pic16_pc_wreg) ); + } + + if(!_G.resDirect) { + for(i=0;isize;i++) + PCOR(aop->aopu.stk.pop[i] )->r->isFree = 1; + } + _G.resDirect = 0; + } + break; +#if 0 case AOP_STK : { int sz = aop->size; @@ -1259,6 +1364,8 @@ void pic16_freeAsmop (operand *op, asmop *aaop, iCode *ic, bool pop) _G.r1Pushed--; } } +#endif + } #endif @@ -1403,7 +1510,11 @@ char *pic16_aopGet (asmop *aop, int offset, bool bit16, bool dname) strcpy(rs,s); return rs; - case AOP_STK: + case AOP_STA: + rs = Safe_strdup(PCOR(aop->aopu.stk.pop[offset])->r->name); + return (rs); + + case AOP_STK: // pCodeOp *pcop = aop->aop break; @@ -1416,45 +1527,102 @@ char *pic16_aopGet (asmop *aop, int offset, bool bit16, bool dname) } + +/* lock has the following meaning: When allocating temporary registers + * for stack variables storage, the value of the temporary register is + * saved on stack. Its value is restored at the end. This procedure is + * done via calls to pic16_aopOp and pic16_freeAsmop functions. There is + * a possibility that before a call to pic16_aopOp, a temporary register + * is allocated for a while and it is freed after some time, this will + * mess the stack and values will not be restored properly. So use lock=1 + * to allocate temporary registers used internally by the programmer, and + * lock=0 to allocate registers for stack use. lock=1 will emit a warning + * to inform the compiler developer about a possible bug. This is an internal + * feature for developing the compiler -- VR */ + +int _TempReg_lock = 0; /*-----------------------------------------------------------------*/ /* pic16_popGetTempReg - create a new temporary pCodeOp */ /*-----------------------------------------------------------------*/ -pCodeOp *pic16_popGetTempReg(void) +pCodeOp *pic16_popGetTempReg(int lock) { pCodeOp *pcop; symbol *cfunc; -// DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); +// DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + if(_TempReg_lock) { +// werror(W_POSSBUG2, __FILE__, __LINE__); + } + + _TempReg_lock += lock; + + cfunc = currFunc; + currFunc = NULL; - cfunc = currFunc; - currFunc = NULL; + pcop = pic16_newpCodeOp(NULL, PO_GPR_TEMP); + if(pcop && pcop->type == PO_GPR_TEMP && PCOR(pcop)->r) { + PCOR(pcop)->r->wasUsed=1; + PCOR(pcop)->r->isFree=0; - pcop = pic16_newpCodeOp(NULL, PO_GPR_TEMP); - if(pcop && pcop->type == PO_GPR_TEMP && PCOR(pcop)->r) { - PCOR(pcop)->r->wasUsed=1; - PCOR(pcop)->r->isFree=0; + /* push value on stack */ + pic16_pushpCodeOp( pic16_pCodeOpCopy(pcop) ); + } - /* push value on stack */ - pic16_pushpCodeOp( pic16_pCodeOpCopy(pcop) ); - } + currFunc = cfunc; + + return pcop; +} + +/*-----------------------------------------------------------------*/ +/* pic16_popGetTempRegCond - create a new temporary pCodeOp, but */ +/* don't save if inside v */ +/*-----------------------------------------------------------------*/ +pCodeOp *pic16_popGetTempRegCond(bitVect *v, int lock) +{ + pCodeOp *pcop; + symbol *cfunc; + +// DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + + if(_TempReg_lock) { +// werror(W_POSSBUG2, __FILE__, __LINE__); + } + + _TempReg_lock += lock; + + cfunc = currFunc; + currFunc = NULL; + + pcop = pic16_newpCodeOp(NULL, PO_GPR_TEMP); + if(pcop && pcop->type == PO_GPR_TEMP && PCOR(pcop)->r) { + PCOR(pcop)->r->wasUsed=1; + PCOR(pcop)->r->isFree=0; + + if(!bitVectBitValue(v, PCOR(pcop)->r->rIdx)) { + /* push value on stack */ + pic16_pushpCodeOp( pic16_pCodeOpCopy(pcop) ); + } + } - currFunc = cfunc; + currFunc = cfunc; return pcop; } + /*-----------------------------------------------------------------*/ /* pic16_popReleaseTempReg - create a new temporary pCodeOp */ /*-----------------------------------------------------------------*/ -void pic16_popReleaseTempReg(pCodeOp *pcop) +void pic16_popReleaseTempReg(pCodeOp *pcop, int lock) { - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - if(pcop && pcop->type == PO_GPR_TEMP && PCOR(pcop)->r) { - PCOR(pcop)->r->isFree = 1; - - pic16_poppCodeOp( pic16_pCodeOpCopy(pcop) ); - } + _TempReg_lock -= lock; + + if(pcop && pcop->type == PO_GPR_TEMP && PCOR(pcop)->r) { + PCOR(pcop)->r->isFree = 1; + pic16_poppCodeOp( pic16_pCodeOpCopy(pcop) ); + } } /*-----------------------------------------------------------------*/ /* pic16_popGetLabel - create a new pCodeOp of type PO_LABEL */ @@ -1497,7 +1665,7 @@ pCodeOp *pic16_popCopyReg(pCodeOpReg *pc) /*-----------------------------------------------------------------*/ /* pic16_popGetLit - asm operator to pcode operator conversion */ /*-----------------------------------------------------------------*/ -pCodeOp *pic16_popGetLit(unsigned int lit) +pCodeOp *pic16_popGetLit(int lit) { return pic16_newpCodeOpLit(lit); } @@ -1505,7 +1673,7 @@ pCodeOp *pic16_popGetLit(unsigned int lit) /*-----------------------------------------------------------------*/ /* pic16_popGetLit2 - asm operator to pcode operator conversion */ /*-----------------------------------------------------------------*/ -pCodeOp *pic16_popGetLit2(unsigned int lit, pCodeOp *arg2) +pCodeOp *pic16_popGetLit2(int lit, pCodeOp *arg2) { return pic16_newpCodeOpLit2(lit, arg2); } @@ -1559,17 +1727,19 @@ static pCodeOp *pic16_popRegFromString(char *str, int size, int offset, operand //pcop->name = Safe_strdup( ( (str) ? str : "BAD STRING")); PCOR(pcop)->r = pic16_dirregWithName(pcop->name); - if(PCOR(pcop)->r == NULL) { + + /* make sure that register doesn't exist, + * and operand isn't NULL + * and symbol isn't in codespace (codespace symbols are handled elsewhere) */ + if((PCOR(pcop)->r == NULL) + && (op) + && !IN_CODESPACE(SPEC_OCLS(OP_SYM_ETYPE(op)))) { // fprintf(stderr, "%s:%d - couldn't find %s in allocated regsters, size= %d ofs= %d\n", // __FUNCTION__, __LINE__, str, size, offset); + PCOR(pcop)->r = pic16_allocRegByName (pcop->name,size, op); + fprintf(stderr, "%s:%d: WARNING: need to allocate new register by name -> %s\n", __FILE__, __LINE__, str); -// PCOR(pcop)->r = pic16_allocRegByName (pcop->name,size, op); - fprintf(stderr, "%s:%d: WARNING: need to allocate new register by name -> %s\n", __FILE__, __LINE__, str); - -// DEBUGpic16_emitcode(";","%d %s size= %d offset=%d - had to alloc by reg name",__LINE__,pcop->name,size,offset); - } else { -// DEBUGpic16_emitcode(";","%d %s size= %d offset=%d",__LINE__,pcop->name,size,offset); } PCOR(pcop)->instance = offset; @@ -1662,7 +1832,7 @@ pCodeOp *pic16_popGet (asmop *aop, int offset) //, bool bit16, bool dname) char *rs; pCodeOp *pcop; - //DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + FENTRY2; /* offset is greater than size then zero */ @@ -1699,6 +1869,13 @@ pCodeOp *pic16_popGet (asmop *aop, int offset) //, bool bit16, bool dname) DEBUGpic16_emitcode(";","%d\tAOP_IMMD",__LINE__); return pic16_popGetImmd(aop->aopu.aop_immd,offset,0); + case AOP_STA: + /* pCodeOp is already allocated from aopForSym */ + DEBUGpic16_emitcode(";---", "%d getting stack + offset %d\n", __LINE__, offset); + pcop = pic16_pCodeOpCopy(aop->aopu.stk.pop[offset]); + + return (pcop); + case AOP_ACC: { int rIdx = IDX_WREG; //aop->aopu.aop_reg[offset]->rIdx; @@ -1732,7 +1909,9 @@ pCodeOp *pic16_popGet (asmop *aop, int offset) //, bool bit16, bool dname) case AOP_REG: { - int rIdx = aop->aopu.aop_reg[offset]->rIdx; + int rIdx; + assert (aop && aop->aopu.aop_reg[offset] != NULL); + rIdx = aop->aopu.aop_reg[offset]->rIdx; DEBUGpic16_emitcode(";","%d\tAOP_REG", __LINE__); @@ -1745,6 +1924,8 @@ pCodeOp *pic16_popGet (asmop *aop, int offset) //, bool bit16, bool dname) PCOR(pcop)->instance = offset; pcop->type = PCOR(pcop)->r->pc_type; + + DEBUGpic16_emitcode(";*+*", "%d\tAOP_REG type = %s\n", __LINE__, dumpPicOptype(pcop->type)); rs = aop->aopu.aop_reg[offset]->name; DEBUGpic16_emitcode(";","%d regiser idx = %d name = %s",__LINE__,rIdx,rs); return pcop; @@ -1754,6 +1935,7 @@ pCodeOp *pic16_popGet (asmop *aop, int offset) //, bool bit16, bool dname) DEBUGpic16_emitcode(";","%d\tAOP_CRY", __LINE__); pcop = pic16_newpCodeOpBit(aop->aopu.aop_dir,-1,1, PO_GPR_REGISTER); + PCOR(pcop)->instance = offset; PCOR(pcop)->r = pic16_dirregWithName(aop->aopu.aop_dir); //if(PCOR(pcop)->r == NULL) //fprintf(stderr,"%d - couldn't find %s in allocated registers\n",__LINE__,aop->aopu.aop_dir); @@ -1807,6 +1989,8 @@ void pic16_aopPut (asmop *aop, char *s, int offset) char *d = buffer ; symbol *lbl ; + return; + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); if (aop->size && offset > ( aop->size - 1)) { @@ -2023,9 +2207,10 @@ void pic16_aopPut (asmop *aop, char *s, int offset) break; default : - werror(E_INTERNAL_ERROR,__FILE__,__LINE__, - "pic16_aopPut got unsupported aop->type"); - exit(0); + fprintf(stderr, "%s:%d: unknown aop->type = 0x%x\n", __FILE__, __LINE__, aop->type); +// werror(E_INTERNAL_ERROR,__FILE__,__LINE__, +// "pic16_aopPut got unsupported aop->type"); +// exit(0); } } @@ -2033,33 +2218,70 @@ void pic16_aopPut (asmop *aop, char *s, int offset) /*-----------------------------------------------------------------*/ /* mov2w - generate either a MOVLW or MOVFW based operand type */ /*-----------------------------------------------------------------*/ -static void mov2w (asmop *aop, int offset) +void mov2w (asmop *aop, int offset) { + DEBUGpic16_emitcode ("; ***","%s %d offset=%d",__FUNCTION__,__LINE__,offset); -// if(!aop) -// return; - - DEBUGpic16_emitcode ("; ***","%s %d offset=%d",__FUNCTION__,__LINE__,offset); + if(is_LitAOp(aop)) + pic16_emitpcode(POC_MOVLW,pic16_popGet(aop,offset)); + else + pic16_emitpcode(POC_MOVFW,pic16_popGet(aop,offset)); +} - if(is_LitAOp(aop)) - pic16_emitpcode(POC_MOVLW,pic16_popGet(aop,offset)); - else - pic16_emitpcode(POC_MOVFW,pic16_popGet(aop,offset)); +static void mov2f(asmop *dst, asmop *src, int offset) +{ + if(is_LitAOp(src)) { + pic16_emitpcode(POC_MOVLW, pic16_popGet(src, offset)); + pic16_emitpcode(POC_MOVWF, pic16_popGet(dst, offset)); + } else { + if(pic16_sameRegsOfs(src, dst, offset))return; + pic16_emitpcode(POC_MOVFF, pic16_popGet2p( pic16_popGet(src, offset), + pic16_popGet(dst, offset))); + } +} +static void mov2fp(pCodeOp *dst, asmop *src, int offset) +{ + if(is_LitAOp(src)) { + pic16_emitpcode(POC_MOVLW, pic16_popGet(src, offset)); + pic16_emitpcode(POC_MOVWF, dst); + } else { + pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(src, offset), dst)); + } } +void pic16_testStackOverflow(void) +{ +#define GSTACK_TEST_NAME "__gstack_test" + + pic16_emitpcode(POC_CALL, pic16_popGetWithString( GSTACK_TEST_NAME )); + + { + symbol *sym; + + sym = newSymbol( GSTACK_TEST_NAME , 0 ); + strcpy(sym->rname, GSTACK_TEST_NAME); + checkAddSym(&externs, sym); + } + +} /* push pcop into stack */ void pic16_pushpCodeOp(pCodeOp *pcop) { // DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pcop, pic16_popCopyReg(&pic16_pc_postdec1))); + pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pcop, pic16_popCopyReg(&pic16_pc_postdec1))); + if(pic16_options.gstack) + pic16_testStackOverflow(); + } /* pop pcop from stack */ void pic16_poppCodeOp(pCodeOp *pcop) { - pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popCopyReg(&pic16_pc_preinc1), pcop)); + pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popCopyReg(&pic16_pc_preinc1), pcop)); + if(pic16_options.gstack) + pic16_testStackOverflow(); } @@ -2068,8 +2290,10 @@ void pic16_poppCodeOp(pCodeOp *pcop) /*-----------------------------------------------------------------*/ void pushw(void) { - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_postdec1)); + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_postdec1)); + if(pic16_options.gstack) + pic16_testStackOverflow(); } @@ -2078,8 +2302,19 @@ void pushw(void) /*-----------------------------------------------------------------*/ void pushaop(asmop *aop, int offset) { - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - pic16_emitpcode(POC_MOVFF, pic16_popCombine2(PCOR(pic16_popGet(aop, offset)), &pic16_pc_postdec1, 0)); + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + + if(is_LitAOp(aop)) { + pic16_emitpcode(POC_MOVLW, pic16_popGet(aop, offset)); + pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_postdec1)); + } else { + pic16_emitpcode(POC_MOVFF, + pic16_popGet2p(pic16_popGet(aop, offset), pic16_popCopyReg(&pic16_pc_postdec1))); + } + +// pic16_emitpcode(POC_MOVFF, pic16_popCombine2(PCOR(pic16_popGet(aop, offset)), &pic16_pc_postdec1, 0)); + if(pic16_options.gstack) + pic16_testStackOverflow(); } /*-----------------------------------------------------------------*/ @@ -2087,20 +2322,24 @@ void pushaop(asmop *aop, int offset) /*-----------------------------------------------------------------*/ void popaop(asmop *aop, int offset) { - DEBUGpic16_emitcode("; ***", "%s %d", __FUNCTION__, __LINE__); - pic16_emitpcode(POC_MOVFF, pic16_popCombine2(&pic16_pc_preinc1, PCOR(pic16_popGet(aop, offset)), 0)); + DEBUGpic16_emitcode("; ***", "%s %d", __FUNCTION__, __LINE__); + pic16_emitpcode(POC_MOVFF, pic16_popCombine2(&pic16_pc_preinc1, PCOR(pic16_popGet(aop, offset)), 0)); + if(pic16_options.gstack) + pic16_testStackOverflow(); } void popaopidx(asmop *aop, int offset, int index) { int ofs=1; - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - if(STACK_MODEL_LARGE)ofs++; + if(STACK_MODEL_LARGE)ofs++; - pic16_emitpcode(POC_MOVLW, pic16_popGetLit(index + ofs)); - pic16_emitpcode(POC_MOVFF, pic16_popCombine2(&pic16_pc_plusw2, PCOR(pic16_popGet(aop, offset)), 0)); + pic16_emitpcode(POC_MOVLW, pic16_popGetLit(index + ofs)); + pic16_emitpcode(POC_MOVFF, pic16_popCombine2(&pic16_pc_plusw2, PCOR(pic16_popGet(aop, offset)), 0)); + if(pic16_options.gstack) + pic16_testStackOverflow(); } /*-----------------------------------------------------------------*/ @@ -2223,6 +2462,7 @@ void pic16_outBitC(operand *result) int i; DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + /* if the result is bit */ if (AOP_TYPE(result) == AOP_CRY) { fprintf(stderr, "%s:%d: pic16 port warning: unsupported case\n", __FILE__, __LINE__); @@ -2234,12 +2474,31 @@ void pic16_outBitC(operand *result) pic16_emitpcode(POC_CLRF, pic16_popGet(AOP(result), i)); } pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result), 0)); - -/* - pic16_emitcode("clr","a ; %d", __LINE__); - pic16_emitcode("rlc","a"); - pic16_outAcc(result); -*/ + } +} + +/*-----------------------------------------------------------------*/ +/* pic16_outBitOp - output a bit from Op */ +/* Move to result the value of set/clr op -- VR */ +/*-----------------------------------------------------------------*/ +void pic16_outBitOp(operand *result, pCodeOp *pcop) +{ + int i; + + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + + /* if the result is bit */ + if (AOP_TYPE(result) == AOP_CRY) { + fprintf(stderr, "%s:%d: pic16 port warning: unsupported case\n", __FILE__, __LINE__); + pic16_aopPut(AOP(result),"c",0); + } else { + + i = AOP_SIZE(result); + while(i--) { + pic16_emitpcode(POC_CLRF, pic16_popGet(AOP(result), i)); + } + pic16_emitpcode(POC_RRCF, pcop); + pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result), 0)); } } @@ -2271,7 +2530,7 @@ static void pic16_genNot (iCode *ic) symbol *tlbl; int size; - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + FENTRY; /* assign asmOps to operand & result */ pic16_aopOp (IC_LEFT(ic),ic,FALSE); pic16_aopOp (IC_RESULT(ic),ic,TRUE); @@ -2318,11 +2577,10 @@ static void pic16_genNot (iCode *ic) /*-----------------------------------------------------------------*/ static void pic16_genCpl (iCode *ic) { - int offset = 0; - int size ; - + int offset = 0; + int size ; - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + FENTRY; /* assign asmOps to operand & result */ pic16_aopOp (IC_LEFT(ic),ic,FALSE); pic16_aopOp (IC_RESULT(ic),ic,TRUE); @@ -2369,26 +2627,20 @@ release: /*-----------------------------------------------------------------*/ static void genUminusFloat(operand *op,operand *result) { - int size ,offset =0 ; - char *l; - - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + int size ,offset =0 ; + + FENTRY; /* for this we just need to flip the first it then copy the rest in place */ - size = AOP_SIZE(op) - 1; - l = pic16_aopGet(AOP(op),3,FALSE,FALSE); - - MOVA(l); - - pic16_emitcode("cpl","acc.7"); - pic16_aopPut(AOP(result),"a",3); + size = AOP_SIZE(op); while(size--) { - pic16_aopPut(AOP(result), - pic16_aopGet(AOP(op),offset,FALSE,FALSE), - offset); - offset++; - } + mov2f(AOP(result), AOP(op), offset); + offset++; + } + + /* toggle the MSB's highest bit */ + pic16_emitpcode(POC_BTG, pic16_popCopyGPR2Bit(pic16_popGet(AOP(result), offset-1), 7)); } /*-----------------------------------------------------------------*/ @@ -2399,56 +2651,56 @@ static void genUminus (iCode *ic) int size, i; sym_link *optype, *rtype; - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - - /* assign asmops */ - pic16_aopOp(IC_LEFT(ic),ic,FALSE); - pic16_aopOp(IC_RESULT(ic),ic,TRUE); - - /* if both in bit space then special case */ - if (AOP_TYPE(IC_RESULT(ic)) == AOP_CRY - && AOP_TYPE(IC_LEFT(ic)) == AOP_CRY ) { + FENTRY; + + /* assign asmops */ + pic16_aopOp(IC_LEFT(ic),ic,FALSE); + pic16_aopOp(IC_RESULT(ic),ic,TRUE); - pic16_emitpcode(POC_BCF, pic16_popGet(AOP(IC_RESULT(ic)),0)); - pic16_emitpcode(POC_BTFSS, pic16_popGet(AOP(IC_LEFT(ic)),0)); - pic16_emitpcode(POC_BSF, pic16_popGet(AOP(IC_RESULT(ic)),0)); - - goto release; - } + /* if both in bit space then special case */ + if (AOP_TYPE(IC_RESULT(ic)) == AOP_CRY + && AOP_TYPE(IC_LEFT(ic)) == AOP_CRY ) { + + pic16_emitpcode(POC_BCF, pic16_popGet(AOP(IC_RESULT(ic)),0)); + pic16_emitpcode(POC_BTFSS, pic16_popGet(AOP(IC_LEFT(ic)),0)); + pic16_emitpcode(POC_BSF, pic16_popGet(AOP(IC_RESULT(ic)),0)); + goto release; + } - optype = operandType(IC_LEFT(ic)); - rtype = operandType(IC_RESULT(ic)); + optype = operandType(IC_LEFT(ic)); + rtype = operandType(IC_RESULT(ic)); - /* if float then do float stuff */ - if (IS_FLOAT(optype)) { - genUminusFloat(IC_LEFT(ic),IC_RESULT(ic)); - goto release; - } + /* if float then do float stuff */ + if (IS_FLOAT(optype)) { + genUminusFloat(IC_LEFT(ic),IC_RESULT(ic)); + goto release; + } - /* otherwise subtract from zero by taking the 2's complement */ - size = AOP_SIZE(IC_LEFT(ic)); + /* otherwise subtract from zero by taking the 2's complement */ + size = AOP_SIZE(IC_LEFT(ic)); - for(i=0; iaopu.pcop->type == PO_DIR)? - - if(AOP(op)->aopu.pcop->type == PO_IMMEDIATE) { - pic16_emitpcode(POC_MOVFW, src); - pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(op), offset)); - } else { - pic16_emitpcode(POC_MOVFF, pic16_popGet2p( - src, pic16_popGet(AOP(op), offset))); - } + if(AOP(op)->aopu.pcop->type == PO_IMMEDIATE) { + pic16_emitpcode(POC_MOVFW, src); + pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(op), offset)); + } else { + pic16_emitpcode(POC_MOVFF, pic16_popGet2p( + src, pic16_popGet(AOP(op), offset))); + } } /*-----------------------------------------------------------------*/ /* assignResultValue - assign results to oper, rescall==1 is */ -/* called from genCall() or genPCall() */ +/* called from genCall() or genPcall() */ /*-----------------------------------------------------------------*/ static void assignResultValue(operand * oper, int rescall) { int size = AOP_SIZE(oper); + int offset=0; + + FENTRY2; +// DEBUGpic16_emitcode ("; ***","%s %d rescall:%d size:%d",__FUNCTION__,__LINE__,rescall,size); // patch 14 + DEBUGpic16_pic16_AopType(__LINE__,oper,NULL,NULL); - DEBUGpic16_emitcode ("; ***","%s %d rescall:%d size:%d",__FUNCTION__,__LINE__,rescall,size); // patch 14 - DEBUGpic16_pic16_AopType(__LINE__,oper,NULL,NULL); - - if(rescall) { - /* assign result from a call/pcall function() */ + if(rescall) { + /* assign result from a call/pcall function() */ - /* function results are stored in a special order, - * see top of file with Function return policy, or manual */ + /* function results are stored in a special order, + * see top of file with Function return policy, or manual */ - if(size <= 4) { - /* 8-bits, result in WREG */ - pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(oper), 0)); + if(size <= 4) { + /* 8-bits, result in WREG */ + pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(oper), 0)); - if(size>1) { - /* 16-bits, result in PRODL:WREG */ - pic16_loadFromReturn(oper, 1, pic16_popCopyReg(&pic16_pc_prodl)); - } + if(size>1) { + /* 16-bits, result in PRODL:WREG */ + pic16_loadFromReturn(oper, 1, pic16_popCopyReg(&pic16_pc_prodl)); + } - if(size>2) { - /* 24-bits, result in PRODH:PRODL:WREG */ - pic16_loadFromReturn(oper, 2, pic16_popCopyReg(&pic16_pc_prodh)); // patch 14 - } + if(size>2) { + /* 24-bits, result in PRODH:PRODL:WREG */ + pic16_loadFromReturn(oper, 2, pic16_popCopyReg(&pic16_pc_prodh)); // patch 14 + } - if(size>3) { - /* 32-bits, result in FSR0L:PRODH:PRODL:WREG */ - pic16_loadFromReturn(oper, 3, pic16_popCopyReg(&pic16_pc_fsr0l)); // patch14 - } - } else { - /* >32-bits, result on stack, and FSR0 points to beginning. - * Fix stack when done */ - /* FIXME FIXME */ - while (size--) { -// DEBUGpic16_emitcode("; ", "POC_MOVLW %d", GpsuedoStkPtr); -// DEBUGpic16_emitcode("; ", "POC_MOVFW PLUSW2"); + if(size>3) { + /* 32-bits, result in FSR0L:PRODH:PRODL:WREG */ + pic16_loadFromReturn(oper, 3, pic16_popCopyReg(&pic16_pc_fsr0l)); // patch14 + } + + } else { + /* >32-bits, result on stack, and FSR0 points to beginning. + * Fix stack when done */ + /* FIXME FIXME */ +// debugf("WARNING: Possible bug when returning more than 4-bytes\n"); + while (size--) { +// DEBUGpic16_emitcode("; ", "POC_MOVLW %d", GpsuedoStkPtr); +// DEBUGpic16_emitcode("; ", "POC_MOVFW PLUSW2"); - popaopidx(AOP(oper), size, GpsuedoStkPtr); - GpsuedoStkPtr++; - } + popaopidx(AOP(oper), size, GpsuedoStkPtr); + GpsuedoStkPtr++; + } - /* fix stack */ - pic16_emitpcode(POC_MOVLW, pic16_popGetLit( AOP_SIZE(oper) )); - pic16_emitpcode(POC_ADDWF, pic16_popCopyReg( &pic16_pc_fsr1l )); - if(STACK_MODEL_LARGE) { - emitSKPNC; - pic16_emitpcode(POC_INCF, pic16_popCopyReg( &pic16_pc_fsr1h )); - } - } - } else { - if(!GpsuedoStkPtr) { -// DEBUGpic16_emitcode("; ", "pop %d", GpsuedoStkPtr); - /* The last byte in the assignment is in W */ - size--; - pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(oper),size)); - GpsuedoStkPtr++; - } - - while (size--) { -// DEBUGpic16_emitcode("; ", "POC_MOVLW %d", GpsuedoStkPtr); -// DEBUGpic16_emitcode("; ", "POC_MOVFW PLUSW2"); - - popaopidx(AOP(oper), size, GpsuedoStkPtr); - GpsuedoStkPtr++; - -#if 0 -#if STACK_SUPPORT - if(!USE_STACK) - pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(oper),size)); -#else - pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(oper),size)); -#endif -#endif - - } + /* fix stack */ + pic16_emitpcode(POC_MOVLW, pic16_popGetLit( AOP_SIZE(oper) )); + pic16_emitpcode(POC_ADDWF, pic16_popCopyReg( &pic16_pc_fsr1l )); + if(STACK_MODEL_LARGE) { + emitSKPNC; + pic16_emitpcode(POC_INCF, pic16_popCopyReg( &pic16_pc_fsr1h )); } - + } + } else { + int areg = 0; /* matching argument register */ + +// debugf("_G.useWreg = %d\tGpsuedoStkPtr = %d\n", _G.useWreg, GpsuedoStkPtr); + areg = SPEC_ARGREG( OP_SYM_ETYPE( oper ) ) - 1; + + + /* its called from genReceive (probably) -- VR */ + /* I hope this code will not be called from somewhere else in the future! + * We manually set the pseudo stack pointer in genReceive. - dw + */ + if(!GpsuedoStkPtr && _G.useWreg) { +// DEBUGpic16_emitcode("; ", "pop %d", GpsuedoStkPtr); + + /* The last byte in the assignment is in W */ + if(areg <= GpsuedoStkPtr) { + size--; + pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(oper), offset /*size*/)); + offset++; +// debugf("receive from WREG\n", 0); + } + GpsuedoStkPtr++; /* otherwise the calculation below fails (-_G.useWreg) */ + } +// GpsuedoStkPtr++; + _G.stack_lat = AOP_SIZE(oper)-1; + + while (size) { + size--; + GpsuedoStkPtr++; + popaopidx(AOP(oper), offset, GpsuedoStkPtr - _G.useWreg); +// debugf("receive from STACK\n", 0); + offset++; + } + } } /*-----------------------------------------------------------------*/ -/* genIpush - genrate code for pushing this gets a little complex */ +/* genIpush - generate code for pushing this gets a little complex */ /*-----------------------------------------------------------------*/ static void genIpush (iCode *ic) { - int size, offset=0; +// int size, offset=0; + FENTRY; DEBUGpic16_emitcode ("; ***","%s %d - WARNING no code generated",__FUNCTION__,__LINE__); + if(ic->parmPush) { + pic16_aopOp(IC_LEFT(ic), ic, FALSE ); - pic16_aopOp(IC_LEFT(ic), ic, FALSE); - + /* send to stack as normal */ + addSet(&_G.sendSet,ic); +// addSetHead(&_G.sendSet,ic); + pic16_freeAsmop(IC_LEFT(ic),NULL,ic,TRUE); + } - size = AOP_SIZE( IC_LEFT(ic) ); - - while(size--) { - mov2w( AOP(IC_LEFT(ic)), offset ); - pushw(); - offset++; - } #if 0 int size, offset = 0 ; @@ -2720,29 +2980,6 @@ static void genIpush (iCode *ic) return ; } - /* this is a paramter push: in this case we call - the routine to find the call and save those - registers that need to be saved */ - saveRegisters(ic); - - /* then do the push */ - pic16_aopOp(IC_LEFT(ic),ic,FALSE); - - - // pushSide(IC_LEFT(ic), AOP_SIZE(IC_LEFT(ic))); - size = AOP_SIZE(IC_LEFT(ic)); - - while (size--) { - l = pic16_aopGet(AOP(IC_LEFT(ic)),offset++,FALSE,TRUE); - if (AOP_TYPE(IC_LEFT(ic)) != AOP_REG && - AOP_TYPE(IC_LEFT(ic)) != AOP_DIR && - strcmp(l,"a") ) { - pic16_emitcode("mov","a,%s",l); - pic16_emitcode("push","acc"); - } else - pic16_emitcode("push","%s",l); - } - pic16_freeAsmop(IC_LEFT(ic),NULL,ic,TRUE); #endif } @@ -2752,6 +2989,7 @@ static void genIpush (iCode *ic) /*-----------------------------------------------------------------*/ static void genIpop (iCode *ic) { + FENTRY; DEBUGpic16_emitcode ("; ***","%s %d - WARNING no code generated",__FUNCTION__,__LINE__); #if 0 int size,offset ; @@ -2772,6 +3010,7 @@ static void genIpop (iCode *ic) #endif } +#if 0 /*-----------------------------------------------------------------*/ /* unsaverbank - restores the resgister bank from stack */ /*-----------------------------------------------------------------*/ @@ -2867,7 +3106,18 @@ static void saverbank (int bank, iCode *ic, bool pushPsw) ic->bankSaved = 1; #endif } +#endif /* 0 */ + + +static int wparamCmp(void *p1, void *p2) +{ + return (!strcmp((char *)p1, (char *)p2)); +} +int inWparamList(char *s) +{ + return isinSetWith(wparamList, s, wparamCmp); +} /*-----------------------------------------------------------------*/ @@ -2875,437 +3125,281 @@ static void saverbank (int bank, iCode *ic, bool pushPsw) /*-----------------------------------------------------------------*/ static void genCall (iCode *ic) { - sym_link *dtype; + sym_link *ftype; int stackParms=0; + int use_wreg=0; + int inwparam=0; + char *fname; - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - - /* if caller saves & we have not saved then */ - if (!ic->regsSaved) - saveRegisters(ic); - - /* if we are calling a function that is not using - * the same register bank then we need to save the - * destination registers on the stack */ - dtype = operandType(IC_LEFT(ic)); - if (currFunc && dtype && - (FUNC_REGBANK(currFunc->type) != FUNC_REGBANK(dtype)) && - IFFUNC_ISISR(currFunc->type) && - !ic->bankSaved) - - saverbank(FUNC_REGBANK(dtype),ic,TRUE); + FENTRY; + ftype = OP_SYM_TYPE(IC_LEFT(ic)); + /* if caller saves & we have not saved then */ +// if (!ic->regsSaved) +// saveRegisters(ic); /* initialise stackParms for IPUSH pushes */ // stackParms = psuedoStkPtr; // fprintf(stderr, "%s:%d ic parmBytes = %d\n", __FILE__, __LINE__, ic->parmBytes); - - /* if send set is not empty the assign */ - if (_G.sendSet) { - iCode *sic; - - /* For the Pic port, there is no data stack. - * So parameters passed to functions are stored - * in registers. (The pCode optimizer will get - * rid of most of these :). */ - - int psuedoStkPtr=-1; - int firstTimeThruLoop = 1; - - _G.sendSet = reverseSet(_G.sendSet); - - /* First figure how many parameters are getting passed */ - for (sic = setFirstItem(_G.sendSet) ; sic ; sic = setNextItem(_G.sendSet)) { - pic16_aopOp(IC_LEFT(sic),sic,FALSE); - psuedoStkPtr += AOP_SIZE(IC_LEFT(sic)); - pic16_freeAsmop (IC_LEFT(sic),NULL,sic,FALSE); - } - - stackParms = psuedoStkPtr; - - for (sic = setFirstItem(_G.sendSet) ; sic ; sic = setNextItem(_G.sendSet)) { - int size, offset = 0; - - pic16_aopOp(IC_LEFT(sic),sic,FALSE); - size = AOP_SIZE(IC_LEFT(sic)); - - while (size--) { - DEBUGpic16_emitcode ("; ","%d left %s",__LINE__, - pic16_AopType(AOP_TYPE(IC_LEFT(sic)))); - DEBUGpic16_emitcode("; ", "push %d", psuedoStkPtr-1); - - if(!firstTimeThruLoop) { - /* If this is not the first time we've been through the loop - * then we need to save the parameter in a temporary - * register. The last byte of the last parameter is - * passed in W. */ - - pushw(); - --psuedoStkPtr; // sanity check - } - - firstTimeThruLoop=0; - - mov2w (AOP(IC_LEFT(sic)), offset); - offset++; - } - pic16_freeAsmop (IC_LEFT(sic),NULL,sic,TRUE); - } - _G.sendSet = NULL; - } - - /* make the call */ - pic16_emitpcode(POC_CALL,pic16_popGetWithString(OP_SYMBOL(IC_LEFT(ic))->rname[0] ? - OP_SYMBOL(IC_LEFT(ic))->rname : - OP_SYMBOL(IC_LEFT(ic))->name)); - - GpsuedoStkPtr=0; - /* if we need assign a result value */ - if ((IS_ITEMP(IC_RESULT(ic)) && - (OP_SYMBOL(IC_RESULT(ic))->nRegs || - OP_SYMBOL(IC_RESULT(ic))->spildir )) || - IS_TRUE_SYMOP(IC_RESULT(ic)) ) { - - _G.accInUse++; - pic16_aopOp(IC_RESULT(ic),ic,FALSE); - _G.accInUse--; - - assignResultValue(IC_RESULT(ic), 1); - - DEBUGpic16_emitcode ("; ","%d left %s",__LINE__, - pic16_AopType(AOP_TYPE(IC_RESULT(ic)))); - - pic16_freeAsmop(IC_RESULT(ic),NULL, ic,TRUE); - } - - if(!stackParms && ic->parmBytes) { - stackParms = ic->parmBytes; - } - - if(stackParms>0) { - pic16_emitpcode(POC_MOVLW, pic16_popGetLit(stackParms)); - pic16_emitpcode(POC_ADDWF, pic16_popCopyReg( &pic16_pc_fsr1l )); - if(STACK_MODEL_LARGE) { - emitSKPNC; - pic16_emitpcode(POC_INCF, pic16_popCopyReg( &pic16_pc_fsr1h )); - } - } - - /* adjust the stack for parameters if required */ -// fprintf(stderr, "%s:%d: %s ic->parmBytes= %d\n", __FILE__, __LINE__, OP_SYMBOL(IC_LEFT(ic))->name, ic->parmBytes); -#if 0 - if (ic->parmBytes) { - int i; - - if (ic->parmBytes > 3) { - pic16_emitcode("mov","a,%s",spname); - pic16_emitcode("add","a,#0x%02x", (- ic->parmBytes) & 0xff); - pic16_emitcode("mov","%s,a",spname); - } else - for ( i = 0 ; i < ic->parmBytes ;i++) - pic16_emitcode("dec","%s",spname); - } -#endif + fname = OP_SYMBOL(IC_LEFT(ic))->rname[0]?OP_SYMBOL(IC_LEFT(ic))->rname:OP_SYMBOL(IC_LEFT(ic))->name; + inwparam = inWparamList(OP_SYMBOL(IC_LEFT(ic))->name); #if 0 - /* if register bank was saved then pop them */ - if (ic->bankSaved) - unsaverbank(FUNC_REGBANK(dtype),ic,TRUE); - - /* if we hade saved some registers then unsave them */ - if (ic->regsSaved && !IFFUNC_CALLEESAVES(dtype)) - unsaveRegisters (ic); + gpsimDebug_StackDump(__FILE__, __LINE__, fname ); #endif -} - - - -/*-----------------------------------------------------------------*/ // patch 14 -/* genPcall - generates a call by pointer statement */ -/*-----------------------------------------------------------------*/ - -// new version, created from genCall - -static void genPcall (iCode *ic) -{ - sym_link *dtype; - int stackParms=0; - symbol *retlbl = newiTempLabel(NULL); - pCodeOp *pcop_lbl = pic16_popGetLabel(retlbl->key); - - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - - /* if caller saves & we have not saved then */ - if (!ic->regsSaved) - saveRegisters(ic); - - /* if we are calling a function that is not using - * the same register bank then we need to save the - * destination registers on the stack */ - dtype = operandType(IC_LEFT(ic)); - if (currFunc && dtype && - (FUNC_REGBANK(currFunc->type) != FUNC_REGBANK(dtype)) && - IFFUNC_ISISR(currFunc->type) && - !ic->bankSaved) - - saverbank(FUNC_REGBANK(dtype),ic,TRUE); - - /* if send set is not empty the assign */ - if (_G.sendSet) { - iCode *sic; - - /* For the Pic port, there is no data stack. - * So parameters passed to functions are stored - * in registers. (The pCode optimizer will get - * rid of most of these :). */ - - int psuedoStkPtr=-1; - int firstTimeThruLoop = 1; - _G.sendSet = reverseSet(_G.sendSet); + /* if send set is not empty the assign */ + if (_G.sendSet) { + iCode *sic; + int psuedoStkPtr=-1; + int firstTimeThruLoop = 1; - /* First figure how many parameters are getting passed */ - for (sic = setFirstItem(_G.sendSet) ; sic ; sic = setNextItem(_G.sendSet)) { - pic16_aopOp(IC_LEFT(sic),sic,FALSE); - psuedoStkPtr += AOP_SIZE(IC_LEFT(sic)); - pic16_freeAsmop (IC_LEFT(sic),NULL,sic,FALSE); - } - stackParms = psuedoStkPtr; + /* reverse sendSet if function is not reentrant */ + if(!IFFUNC_ISREENT(ftype)) + _G.sendSet = reverseSet(_G.sendSet); - for (sic = setFirstItem(_G.sendSet) ; sic ; sic = setNextItem(_G.sendSet)) { - int size, offset = 0; + /* First figure how many parameters are getting passed */ + stackParms = 0; + use_wreg = 0; + + for (sic = setFirstItem(_G.sendSet) ; sic ; sic = setNextItem(_G.sendSet)) { + int size; +// int offset = 0; - pic16_aopOp(IC_LEFT(sic),sic,FALSE); - size = AOP_SIZE(IC_LEFT(sic)); + pic16_aopOp(IC_LEFT(sic),sic,FALSE); + size = AOP_SIZE(IC_LEFT(sic)); - while (size--) { - DEBUGpic16_emitcode ("; ","%d left %s",__LINE__, - pic16_AopType(AOP_TYPE(IC_LEFT(sic)))); - DEBUGpic16_emitcode("; ", "push %d", psuedoStkPtr-1); - - if(!firstTimeThruLoop) { - /* If this is not the first time we've been through the loop - * then we need to save the parameter in a temporary - * register. The last byte of the last parameter is - * passed in W. */ - - pushw(); - --psuedoStkPtr; // sanity check - } - - firstTimeThruLoop=0; + stackParms += size; - mov2w (AOP(IC_LEFT(sic)), offset); - offset++; - } - pic16_freeAsmop (IC_LEFT(sic),NULL,sic,TRUE); - } - _G.sendSet = NULL; - } + /* pass the last byte through WREG */ + if(inwparam) { - pic16_aopOp(IC_LEFT(ic),ic,FALSE); + while (size--) { + DEBUGpic16_emitcode ("; ","%d left %s",__LINE__, + pic16_AopType(AOP_TYPE(IC_LEFT(sic)))); + DEBUGpic16_emitcode("; ", "push %d", psuedoStkPtr-1); - // push return address - // push $ on return stack, then replace with retlbl + if(!firstTimeThruLoop) { + /* If this is not the first time we've been through the loop + * then we need to save the parameter in a temporary + * register. The last byte of the last parameter is + * passed in W. */ - pic16_emitpcodeNULLop(POC_PUSH); + pushw(); +// --psuedoStkPtr; // sanity check + use_wreg = 1; + } + + firstTimeThruLoop=0; - pic16_emitpcode(POC_MOVLW, pic16_popGetImmd(pcop_lbl->name, 0, 0)); - pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_tosl)); - pic16_emitpcode(POC_MOVLW, pic16_popGetImmd(pcop_lbl->name, 1, 0)); - pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_tosh)); - pic16_emitpcode(POC_MOVLW, pic16_popGetImmd(pcop_lbl->name, 2, 0)); - pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_tosu)); + mov2w (AOP(IC_LEFT(sic)), size); - /* make the call by writing the pointer into pc */ - pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(IC_LEFT(ic)),2), pic16_popCopyReg(&pic16_pc_pclatu))); - pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(IC_LEFT(ic)),1), pic16_popCopyReg(&pic16_pc_pclath))); +// offset++; + } + } else { + /* all arguments are passed via stack */ + use_wreg = 0; - // note: MOVFF to PCL not allowed - pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(IC_LEFT(ic)),0)); - pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_pcl)); + while (size--) { + DEBUGpic16_emitcode ("; ","%d left %s",__LINE__, + pic16_AopType(AOP_TYPE(IC_LEFT(sic)))); + DEBUGpic16_emitcode("; ", "push %d", psuedoStkPtr-1); +// pushaop(AOP(IC_LEFT(sic)), size); + mov2w (AOP(IC_LEFT(sic)), size); + pushw(); + } + } -// pic16_emitpcode(POC_GOTO, pic16_popGetLabel(retlbl->key)); -// pic16_emitpcodeNULLop(POC_NOP); -// pic16_emitpcodeNULLop(POC_NOP); + pic16_freeAsmop (IC_LEFT(sic),NULL,sic,TRUE); + } - /* return address is here: (X) */ - pic16_emitpLabelFORCE(retlbl->key); + if(inwparam) { + if(IFFUNC_HASVARARGS(ftype) || IFFUNC_ISREENT(ftype)) { + pushw(); /* save last parameter to stack if functions has varargs */ + use_wreg = 0; + } else + use_wreg = 1; + } else use_wreg = 0; -// pic16_emitpcodeNULLop(POC_NOP); + _G.stackRegSet = _G.sendSet; + _G.sendSet = NULL; + } - pic16_freeAsmop (IC_LEFT(ic),NULL,ic,TRUE); + /* make the call */ + pic16_emitpcode(POC_CALL,pic16_popGetWithString(fname)); - GpsuedoStkPtr=0; - /* if we need assign a result value */ - if ((IS_ITEMP(IC_RESULT(ic)) && - (OP_SYMBOL(IC_RESULT(ic))->nRegs || - OP_SYMBOL(IC_RESULT(ic))->spildir )) || - IS_TRUE_SYMOP(IC_RESULT(ic)) ) { + GpsuedoStkPtr=0; + + /* if we need to assign a result value */ + if ((IS_ITEMP(IC_RESULT(ic)) + && (OP_SYMBOL(IC_RESULT(ic))->nRegs + || OP_SYMBOL(IC_RESULT(ic))->spildir )) + || IS_TRUE_SYMOP(IC_RESULT(ic)) ) { - _G.accInUse++; - pic16_aopOp(IC_RESULT(ic),ic,FALSE); - _G.accInUse--; + _G.accInUse++; + pic16_aopOp(IC_RESULT(ic),ic,FALSE); + _G.accInUse--; - assignResultValue(IC_RESULT(ic), 1); + assignResultValue(IC_RESULT(ic), 1); - DEBUGpic16_emitcode ("; ","%d left %s",__LINE__, - pic16_AopType(AOP_TYPE(IC_RESULT(ic)))); + DEBUGpic16_emitcode ("; ","%d left %s",__LINE__, + pic16_AopType(AOP_TYPE(IC_RESULT(ic)))); - pic16_freeAsmop(IC_RESULT(ic),NULL, ic,TRUE); - } - - if(stackParms>0) { - pic16_emitpcode(POC_MOVLW, pic16_popGetLit(stackParms)); - pic16_emitpcode(POC_ADDWF, pic16_popCopyReg( &pic16_pc_fsr1l )); - if(STACK_MODEL_LARGE) { - emitSKPNC; - pic16_emitpcode(POC_INCF, pic16_popCopyReg( &pic16_pc_fsr1h )); - } - } + pic16_freeAsmop(IC_RESULT(ic),NULL, ic,TRUE); + } - /* adjust the stack for parameters if required */ -// fprintf(stderr, "%s:%d: %s ic->parmBytes= %d\n", __FILE__, __LINE__, OP_SYMBOL(IC_LEFT(ic))->name, ic->parmBytes); + if(!stackParms && ic->parmBytes) { + stackParms = ic->parmBytes; + } + + stackParms -= use_wreg; + + if(stackParms>0) { + if(stackParms == 1) { + pic16_emitpcode(POC_INCF, pic16_popCopyReg(&pic16_pc_fsr1l)); + } else { + pic16_emitpcode(POC_MOVLW, pic16_popGetLit(stackParms)); + pic16_emitpcode(POC_ADDWF, pic16_popCopyReg( &pic16_pc_fsr1l )); + } + if(STACK_MODEL_LARGE) { + emitSKPNC; + pic16_emitpcode(POC_INCF, pic16_popCopyReg( &pic16_pc_fsr1h )); + } + } - if (ic->parmBytes) { - int i; +#if 0 + gpsimDebug_StackDump(__FILE__, __LINE__, fname); +#endif - if (ic->parmBytes > 3) { - pic16_emitcode("mov","a,%s",spname); - pic16_emitcode("add","a,#0x%02x", (- ic->parmBytes) & 0xff); - pic16_emitcode("mov","%s,a",spname); - } else - for ( i = 0 ; i < ic->parmBytes ;i++) - pic16_emitcode("dec","%s",spname); - } + /* adjust the stack for parameters if required */ +// fprintf(stderr, "%s:%d: %s ic->parmBytes= %d\n", __FILE__, __LINE__, OP_SYMBOL(IC_LEFT(ic))->name, ic->parmBytes); - /* if register bank was saved then pop them */ - if (ic->bankSaved) - unsaverbank(FUNC_REGBANK(dtype),ic,TRUE); +#if 0 + /* if register bank was saved then pop them */ + if (ic->bankSaved) + unsaverbank(FUNC_REGBANK(dtype),ic,TRUE); - /* if we hade saved some registers then unsave them */ - if (ic->regsSaved && !IFFUNC_CALLEESAVES(dtype)) - unsaveRegisters (ic); + /* if we hade saved some registers then unsave them */ + if (ic->regsSaved && !IFFUNC_CALLEESAVES(dtype)) + unsaveRegisters (ic); +#endif } -#if 0 // patch 14 -// old version, kept for reference /*-----------------------------------------------------------------*/ /* genPcall - generates a call by pointer statement */ +/* new version, created from genCall - HJD */ /*-----------------------------------------------------------------*/ static void genPcall (iCode *ic) { - sym_link *dtype; - symbol *rlbl = newiTempLabel(NULL); + sym_link *ftype, *fntype; + int stackParms=0; + symbol *retlbl = newiTempLabel(NULL); + pCodeOp *pcop_lbl = pic16_popGetLabel(retlbl->key); + + FENTRY; + ftype = OP_SYM_TYPE(IC_LEFT(ic)); + fntype = operandType( IC_LEFT(ic) )->next; - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - /* if caller saves & we have not saved then */ - if (!ic->regsSaved) - saveRegisters(ic); + /* if send set is not empty the assign */ + if (_G.sendSet) { + iCode *sic; + int psuedoStkPtr=-1; - /* if we are calling a function that is not using - the same register bank then we need to save the - destination registers on the stack */ - dtype = operandType(IC_LEFT(ic)); - if (currFunc && dtype && - IFFUNC_ISISR(currFunc->type) && - (FUNC_REGBANK(currFunc->type) != FUNC_REGBANK(dtype))) - saverbank(FUNC_REGBANK(dtype),ic,TRUE); + /* reverse sendSet if function is not reentrant */ + if(!IFFUNC_ISREENT(fntype)) + _G.sendSet = reverseSet(_G.sendSet); + + stackParms = 0; + + for (sic = setFirstItem(_G.sendSet) ; sic ; sic = setNextItem(_G.sendSet)) { + int size; + + pic16_aopOp(IC_LEFT(sic),sic,FALSE); + size = AOP_SIZE(IC_LEFT(sic)); + stackParms += size; + + /* all parameters are passed via stack, since WREG is clobbered + * by the calling sequence */ + while (size--) { + DEBUGpic16_emitcode ("; ","%d left %s",__LINE__, + pic16_AopType(AOP_TYPE(IC_LEFT(sic)))); + DEBUGpic16_emitcode("; ", "push %d", psuedoStkPtr-1); + + mov2w (AOP(IC_LEFT(sic)), size); + pushw(); + } + pic16_freeAsmop (IC_LEFT(sic),NULL,sic,TRUE); + } - /* push the return address on to the stack */ - pic16_emitcode("mov","a,#%05d_DS_",(rlbl->key+100)); - pic16_emitcode("push","acc"); - pic16_emitcode("mov","a,#(%05d_DS_ >> 8)",(rlbl->key+100)); - pic16_emitcode("push","acc"); - - if (options.model == MODEL_FLAT24) - { - pic16_emitcode("mov","a,#(%05d_DS_ >> 16)",(rlbl->key+100)); - pic16_emitcode("push","acc"); + _G.stackRegSet = _G.sendSet; + _G.sendSet = NULL; } - /* now push the calling address */ pic16_aopOp(IC_LEFT(ic),ic,FALSE); - pushSide(IC_LEFT(ic), FPTRSIZE); + // push return address + // push $ on return stack, then replace with retlbl - pic16_freeAsmop(IC_LEFT(ic),NULL,ic,TRUE); + pic16_emitpcodeNULLop(POC_PUSH); - /* if send set is not empty the assign */ - if (_G.sendSet) { - iCode *sic ; + pic16_emitpcode(POC_MOVLW, pic16_popGetImmd(pcop_lbl->name, 0, 0)); + pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_tosl)); + pic16_emitpcode(POC_MOVLW, pic16_popGetImmd(pcop_lbl->name, 1, 0)); + pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_tosh)); + pic16_emitpcode(POC_MOVLW, pic16_popGetImmd(pcop_lbl->name, 2, 0)); + pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_tosu)); - for (sic = setFirstItem(_G.sendSet) ; sic ; - sic = setNextItem(_G.sendSet)) { - int size, offset = 0; - pic16_aopOp(IC_LEFT(sic),sic,FALSE); - size = AOP_SIZE(IC_LEFT(sic)); - while (size--) { - char *l = pic16_aopGet(AOP(IC_LEFT(sic)),offset, - FALSE,FALSE); - if (strcmp(l,fReturn[offset])) - pic16_emitcode("mov","%s,%s", - fReturn[offset], - l); - offset++; - } - pic16_freeAsmop (IC_LEFT(sic),NULL,sic,TRUE); - } - _G.sendSet = NULL; - } + /* make the call by writing the pointer into pc */ + pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(IC_LEFT(ic)),2), pic16_popCopyReg(&pic16_pc_pclatu))); + pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(IC_LEFT(ic)),1), pic16_popCopyReg(&pic16_pc_pclath))); - pic16_emitcode("ret",""); - pic16_emitcode("","%05d_DS_:",(rlbl->key+100)); + // note: MOVFF to PCL not allowed + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(IC_LEFT(ic)),0)); + pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_pcl)); - /* if we need assign a result value */ - if ((IS_ITEMP(IC_RESULT(ic)) && - (OP_SYMBOL(IC_RESULT(ic))->nRegs || - OP_SYMBOL(IC_RESULT(ic))->spildir)) || - IS_TRUE_SYMOP(IC_RESULT(ic)) ) { - - _G.accInUse++; - pic16_aopOp(IC_RESULT(ic),ic,FALSE); - _G.accInUse--; - - assignResultValue(IC_RESULT(ic), 1); + /* return address is here: (X) */ + pic16_emitpLabelFORCE(retlbl->key); - pic16_freeAsmop(IC_RESULT(ic),NULL,ic,TRUE); - } + pic16_freeAsmop (IC_LEFT(ic),NULL,ic,TRUE); - /* adjust the stack for parameters if - required */ - if (ic->parmBytes) { - int i; - if (ic->parmBytes > 3) { - pic16_emitcode("mov","a,%s",spname); - pic16_emitcode("add","a,#0x%02x", (- ic->parmBytes) & 0xff); - pic16_emitcode("mov","%s,a",spname); - } else - for ( i = 0 ; i < ic->parmBytes ;i++) - pic16_emitcode("dec","%s",spname); + GpsuedoStkPtr=0; + /* if we need assign a result value */ + if ((IS_ITEMP(IC_RESULT(ic)) + && (OP_SYMBOL(IC_RESULT(ic))->nRegs + || OP_SYMBOL(IC_RESULT(ic))->spildir )) + || IS_TRUE_SYMOP(IC_RESULT(ic)) ) { - } + _G.accInUse++; + pic16_aopOp(IC_RESULT(ic),ic,FALSE); + _G.accInUse--; - /* if register bank was saved then unsave them */ - if (currFunc && dtype && - (FUNC_REGBANK(currFunc->type) != FUNC_REGBANK(dtype))) - unsaverbank(FUNC_REGBANK(dtype),ic,TRUE); + assignResultValue(IC_RESULT(ic), 1); - /* if we hade saved some registers then - unsave them */ - if (ic->regsSaved) - unsaveRegisters (ic); + DEBUGpic16_emitcode ("; ","%d left %s",__LINE__, + pic16_AopType(AOP_TYPE(IC_RESULT(ic)))); + + pic16_freeAsmop(IC_RESULT(ic),NULL, ic,TRUE); + } +// stackParms -= use_wreg; + + if(stackParms>0) { + pic16_emitpcode(POC_MOVLW, pic16_popGetLit(stackParms)); + pic16_emitpcode(POC_ADDWF, pic16_popCopyReg( &pic16_pc_fsr1l )); + if(STACK_MODEL_LARGE) { + /* this implies that stackParms < 256 !!! -- VR */ + emitSKPNC; + pic16_emitpcode(POC_INCF, pic16_popCopyReg( &pic16_pc_fsr1h )); + } + } } -#endif // patch 14 - /*-----------------------------------------------------------------*/ /* resultRemat - result is rematerializable */ @@ -3362,266 +3456,200 @@ static void genFunction (iCode *ic) symbol *sym; sym_link *ftype; - DEBUGpic16_emitcode ("; ***","%s %d curr label offset=%dprevious max_key=%d ",__FUNCTION__,__LINE__,pic16_labelOffset,max_key); + FENTRY; + DEBUGpic16_emitcode ("; ***","%s %d curr label offset=%dprevious max_key=%d ",__FUNCTION__,__LINE__,pic16_labelOffset,max_key); - pic16_labelOffset += (max_key+4); - max_key=0; - GpsuedoStkPtr=0; - _G.nRegsSaved = 0; - - ftype = operandType(IC_LEFT(ic)); - sym = OP_SYMBOL(IC_LEFT(ic)); + pic16_labelOffset += (max_key+4); + max_key=0; + GpsuedoStkPtr=0; + _G.nRegsSaved = 0; + + ftype = operandType(IC_LEFT(ic)); + sym = OP_SYMBOL(IC_LEFT(ic)); - if(IFFUNC_ISISR(sym->type /*ftype*/)) { - /* create an absolute section at the interrupt vector: - * that is 0x0008 for interrupt 1 (high), 0x0018 interrupt 2 (low) */ - symbol *asym; - char asymname[128]; - pBlock *apb; + if(IFFUNC_ISISR(sym->type /*ftype*/)) { + /* create an absolute section at the interrupt vector: + * that is 0x0008 for interrupt 1 (high), 0x0018 interrupt 2 (low) */ + symbol *asym; + char asymname[128]; + pBlock *apb; - { - int i, found=-1; - - sym = OP_SYMBOL( IC_LEFT(ic)); - for(i=0;i<=2;i++) { - if(interrupts[i]->name - && !STRCASECMP(interrupts[i]->name, sym->name)) { - found = i; - break; - } - } + { + int i, found=-1; + + sym = OP_SYMBOL( IC_LEFT(ic)); + for(i=0;i<=2;i++) { + if(interrupts[i]->name + && !STRCASECMP(interrupts[i]->name, sym->name)) { + found = i; + break; + } + } - if(found == -1) { - fprintf(stderr, "PIC16 port: %s:%d: interrupt function but cannot locate symbol (%s)\n", - __FILE__, __LINE__, sym->name); - assert( 0 ); - } - _G.interruptvector = found; - } - - sprintf(asymname, "ivec_%d_%s", _G.interruptvector, sym->name); - asym = newSymbol(asymname, 0); - - apb = pic16_newpCodeChain(NULL, 'A', pic16_newpCodeCharP("; Starting pCode block for absolute section")); - pic16_addpBlock( apb ); - - pic16_addpCode2pBlock(apb, - pic16_newpCodeCharP(";-----------------------------------------")); + if(found == -1) { + fprintf(stderr, "PIC16 port: %s:%d: interrupt function but cannot locate symbol (%s)\n", + __FILE__, __LINE__, sym->name); + assert( 0 ); + } + _G.interruptvector = found; + } + sprintf(asymname, "ivec_%d_%s", _G.interruptvector, sym->name); + asym = newSymbol(asymname, 0); - pic16_addpCode2pBlock(apb, pic16_newpCodeFunction(moduleName, asym->name)); + apb = pic16_newpCodeChain(NULL, 'A', pic16_newpCodeCharP("; Starting pCode block for absolute section")); + pic16_addpBlock( apb ); - pic16_addpCode2pBlock(apb, - pic16_newpCode(POC_GOTO, pic16_popGetWithString( sym->rname ))); + pic16_addpCode2pBlock(apb, pic16_newpCodeCharP(";-----------------------------------------")); + pic16_addpCode2pBlock(apb, pic16_newpCodeFunction(moduleName, asym->name)); + pic16_addpCode2pBlock(apb, pic16_newpCode(POC_GOTO, pic16_popGetWithString( sym->rname ))); - /* mark the end of this tiny function */ - pic16_addpCode2pBlock(apb,pic16_newpCodeFunction(NULL,NULL)); - - { - absSym *abSym; - - abSym = Safe_calloc(1, sizeof(absSym)); - abSym->name = Safe_strdup( asymname ); + /* mark the end of this tiny function */ + pic16_addpCode2pBlock(apb,pic16_newpCodeFunction(NULL,NULL)); - switch( _G.interruptvector ) { - case 0: abSym->address = 0x000000; break; - case 1: abSym->address = 0x000008; break; - case 2: abSym->address = 0x000018; break; - } + { + absSym *abSym; - /* relocate interrupt vectors if needed */ - abSym->address += pic16_options.ivt_loc; + abSym = Safe_calloc(1, sizeof(absSym)); + strcpy(abSym->name, asymname); - addSet(&absSymSet, abSym); - } - } + switch( _G.interruptvector ) { + case 0: abSym->address = 0x000000; break; + case 1: abSym->address = 0x000008; break; + case 2: abSym->address = 0x000018; break; + } + /* relocate interrupt vectors if needed */ + abSym->address += pic16_options.ivt_loc; - /* create the function header */ - pic16_emitcode(";","-----------------------------------------"); - pic16_emitcode(";"," function %s",sym->name); - pic16_emitcode(";","-----------------------------------------"); + addSet(&absSymSet, abSym); + } + } - pic16_emitcode("","%s:",sym->rname); - pic16_addpCode2pBlock(pb,pic16_newpCodeFunction(moduleName,sym->rname)); + /* create the function header */ + pic16_emitcode(";","-----------------------------------------"); + pic16_emitcode(";"," function %s",sym->name); + pic16_emitcode(";","-----------------------------------------"); + pic16_emitcode("","%s:",sym->rname); + pic16_addpCode2pBlock(pb,pic16_newpCodeFunction(moduleName,sym->rname)); - { - absSym *ab; - for(ab = setFirstItem(absSymSet); ab; ab = setNextItem(absSymSet)) - if(!strcmp(ab->name, sym->name)) { - pic16_pBlockConvert2Absolute(pb); - break; - } + { + absSym *ab; - } + for(ab = setFirstItem(absSymSet); ab; ab = setNextItem(absSymSet)) { + if(!strcmp(ab->name, sym->rname)) { + pic16_pBlockConvert2Absolute(pb); + break; + } + } + } - if(IFFUNC_ISNAKED(ftype)) { - DEBUGpic16_emitcode("; ***", "_naked function, no prologue"); - return; - } + if(IFFUNC_ISNAKED(ftype)) { + DEBUGpic16_emitcode("; ***", "_naked function, no prologue"); + return; + } - /* if critical function then turn interrupts off */ - if (IFFUNC_ISCRITICAL(ftype)) - pic16_emitcode("clr","ea"); - - /* if this is an interrupt service routine then - * save acc, b, dpl, dph */ - if (IFFUNC_ISISR(sym->type)) { - int i; - - _G.usefastretfie = 1; /* use shadow registers by default */ - /* an ISR should save: WREG, STATUS, BSR, PRODL, PRODH, FSR0L, FSR0H */ - if(!(_G.interruptvector == 1)) { - - /* do not save WREG,STATUS,BSR for high priority interrupts - * because they are stored in the hardware shadow registers already */ - _G.usefastretfie = 0; - pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_wreg )); - pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_status )); - pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_bsr )); - } + /* if critical function then turn interrupts off */ + if (IFFUNC_ISCRITICAL(ftype)) { + //pic16_emitcode("clr","ea"); + } + _G.fregsUsed = sym->regsUsed; - /* these should really be optimized somehow, because not all - * interrupt handlers modify them */ - pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_prodl )); - pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_prodh )); - pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_fsr0l )); - pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_fsr0h )); + /* if this is an interrupt service routine then + * save wreg, status, bsr, prodl, prodh, fsr0l, fsr0h */ + if (IFFUNC_ISISR(sym->type)) { + _G.usefastretfie = 1; /* use shadow registers by default */ + + /* an ISR should save: WREG, STATUS, BSR, PRODL, PRODH, FSR0L, FSR0H */ + if(!(_G.interruptvector == 1)) { + /* do not save WREG,STATUS,BSR for high priority interrupts + * because they are stored in the hardware shadow registers already */ + _G.usefastretfie = 0; + pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_wreg )); + pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_status )); + pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_bsr )); + } -// pic16_pBlockConvert2ISR(pb); + /* these should really be optimized somehow, because not all + * interrupt handlers modify them */ + pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_prodl )); + pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_prodh )); + pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_fsr0l )); + pic16_pushpCodeOp( pic16_popCopyReg( &pic16_pc_fsr0h )); + +// pic16_pBlockConvert2ISR(pb); - /* if any registers used */ - if (sym->regsUsed) { - /* save the registers used */ - DEBUGpic16_emitcode("; **", "Saving used registers in stack"); - for ( i = 0 ; i < sym->regsUsed->size ; i++) { - if (bitVectBitValue(sym->regsUsed,i)) { -#if 0 - fprintf(stderr, "%s:%d function %s uses register %s\n", - __FILE__, __LINE__, OP_SYMBOL(IC_LEFT(ic))->name, - pic16_regWithIdx(i)->name); -#endif - - pic16_pushpCodeOp( pic16_popRegFromIdx(i) ); - _G.nRegsSaved++; - - if(!pic16_regWithIdx(i)->wasUsed) { - fprintf(stderr, "%s:%d register %s is used in function but was wasUsed = 0d\n", - __FILE__, __LINE__, pic16_regWithIdx(i)->name); + } - pic16_regWithIdx(i)->wasUsed = 1; - } - } - } - } - } else { - /* emit code to setup stack frame if user enabled, - * and function is not main() */ + /* emit code to setup stack frame if user enabled, + * and function is not main() */ -// fprintf(stderr, "function name: %s\n", sym->name); - if(strcmp(sym->name, "main")) { - if(/*!options.ommitFramePtr || */sym->regsUsed) { - /* setup the stack frame */ - pic16_emitpcode(POC_MOVFF, pic16_popCombine2( &pic16_pc_fsr2l, &pic16_pc_postdec1, 0)); - pic16_emitpcode(POC_MOVFF, pic16_popCombine2( &pic16_pc_fsr1l, &pic16_pc_fsr2l, 0)); - if(STACK_MODEL_LARGE) - pic16_emitpcode(POC_MOVFF, pic16_popCombine2( &pic16_pc_fsr1h, &pic16_pc_fsr2h, 0)); - } - } - - /* if callee-save to be used for this function - * then save the registers being used in this function */ -// if (IFFUNC_CALLEESAVES(sym->type)) - { - int i; - -// fprintf(stderr, "%s:%d function sym->regsUsed= %d\n", __FILE__, __LINE__, sym->regsUsed->size); - -// pic16_emitpcomment("entry regsUsed: %d\n", sym->regsUsed?sym->regsUsed->size:-1); - - /* if any registers used */ - if (sym->regsUsed) { - /* save the registers used */ - DEBUGpic16_emitcode("; **", "Saving used registers in stack"); - for ( i = 0 ; i < sym->regsUsed->size ; i++) { - if (bitVectBitValue(sym->regsUsed,i)) { - -#if 0 - fprintf(stderr, "%s:%d function %s uses register %s (wasUsed: %d, %p)\n", - __FILE__, __LINE__, OP_SYMBOL(IC_LEFT(ic))->name, - pic16_regWithIdx(i)->name, - pic16_regWithIdx(i)->wasUsed, - pic16_regWithIdx(i)); -#endif - - pic16_pushpCodeOp( pic16_popRegFromIdx(i) ); - -// pic16_emitpcode(POC_MOVFF, pic16_popCombine2( -// PCOR(pic16_popCopyReg( PCOR(pic16_popRegFromIdx(i)))), -// &pic16_pc_postdec1, 0)); + //fprintf(stderr, "function name: %s\n", sym->name); + if(strcmp(sym->name, "main")) { + if(1 /*!options.ommitFramePtr || sym->regsUsed*/) { + /* setup the stack frame */ + if(STACK_MODEL_LARGE) + pic16_pushpCodeOp(pic16_popCopyReg(&pic16_pc_fsr2h)); + pic16_pushpCodeOp(pic16_popCopyReg(&pic16_pc_fsr2l)); +// pic16_emitpcode(POC_MOVFF, pic16_popCombine2( &pic16_pc_fsr2l, &pic16_pc_postdec1, 0)); + pic16_emitpcode(POC_MOVFF, pic16_popCombine2( &pic16_pc_fsr1l, &pic16_pc_fsr2l, 0)); + if(STACK_MODEL_LARGE) + pic16_emitpcode(POC_MOVFF, pic16_popCombine2( &pic16_pc_fsr1h, &pic16_pc_fsr2h, 0)); + } + } - _G.nRegsSaved++; + if ((IFFUNC_ISREENT(sym->type) || options.stackAuto) + && sym->stack) { - if(!pic16_regWithIdx(i)->wasUsed) { - fprintf(stderr, "%s:%d register %s is used in function but was wasUsed = 0d\n", - __FILE__, __LINE__, pic16_regWithIdx(i)->name); + if (sym->stack > 127)werror(W_STACK_OVERFLOW, sym->name); - pic16_regWithIdx(i)->wasUsed = 1; - } - - } - } - } - } - } + pic16_emitpcode(POC_MOVLW, pic16_popGetLit(sym->stack)); + pic16_emitpcode(POC_SUBWF, pic16_popCopyReg(&pic16_pc_fsr1l)); + emitSKPC; + pic16_emitpcode(POC_DECF, pic16_popCopyReg(&pic16_pc_fsr1h)); + } + + if(inWparamList(sym->name)) { + if(IFFUNC_HASVARARGS(sym->type) || IFFUNC_ISREENT(sym->type)) + _G.useWreg = 0; + else + _G.useWreg = 1; + } else + _G.useWreg = 0; + /* if callee-save to be used for this function + * then save the registers being used in this function */ +// if (IFFUNC_CALLEESAVES(sym->type)) + { + int i; + /* if any registers used */ + if (sym->regsUsed) { + /* save the registers used */ + DEBUGpic16_emitcode("; **", "Saving used registers in stack"); + pic16_emitpinfo(INF_LOCALREGS, pic16_newpCodeOpLocalRegs(LR_ENTRY_BEGIN)); + for ( i = 0 ; i < sym->regsUsed->size ; i++) { + if (bitVectBitValue(sym->regsUsed,i)) { + pic16_pushpCodeOp( pic16_popRegFromIdx(i) ); + _G.nRegsSaved++; + + if(!pic16_regWithIdx(i)->wasUsed) { + fprintf(stderr, "%s:%d register %s is used in function but was wasUsed = 0d\n", + __FILE__, __LINE__, pic16_regWithIdx(i)->name); + pic16_regWithIdx(i)->wasUsed = 1; + } + } + } + pic16_emitpinfo(INF_LOCALREGS, pic16_newpCodeOpLocalRegs(LR_ENTRY_END)); + } + } -#if 0 - if (IFFUNC_ISREENT(sym->type) || options.stackAuto) { - - if (options.useXstack) { - pic16_emitcode("mov","r0,%s",spname); - pic16_emitcode("mov","a,_bp"); - pic16_emitcode("movx","@r0,a"); - pic16_emitcode("inc","%s",spname); - } else { - /* set up the stack */ - pic16_emitcode ("push","_bp"); /* save the callers stack */ - } - pic16_emitcode ("mov","_bp,%s",spname); - } -#endif - DEBUGpic16_emitcode("; ", "need to adjust stack = %d", sym->stack); - - /* adjust the stack for the function */ - if (sym->stack) { - int i = sym->stack; - - if (i > 127 ) - werror(W_STACK_OVERFLOW,sym->name); - - if (i > 3 && sym->recvSize < 4) { - pic16_emitcode ("mov","a,sp"); - pic16_emitcode ("add","a,#0x%02x",((char)sym->stack & 0xff)); - pic16_emitcode ("mov","sp,a"); - } else - while(i--) - pic16_emitcode("inc","sp"); - } - - if (sym->xstack) { - DEBUGpic16_emitcode("; ", "%s", __FUNCTION__); - - pic16_emitcode ("mov","a,_spx"); - pic16_emitcode ("add","a,#0x%02x",((char)sym->xstack & 0xff)); - pic16_emitcode ("mov","_spx,a"); - } - + DEBUGpic16_emitcode("; ", "need to adjust stack = %d", sym->stack); +// fprintf(stderr, "Function '%s' uses %d bytes of stack\n", sym->name, sym->stack); } /*-----------------------------------------------------------------*/ @@ -3629,181 +3657,144 @@ static void genFunction (iCode *ic) /*-----------------------------------------------------------------*/ static void genEndFunction (iCode *ic) { - symbol *sym = OP_SYMBOL(IC_LEFT(ic)); + symbol *sym = OP_SYMBOL(IC_LEFT(ic)); - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + FENTRY; if(IFFUNC_ISNAKED(sym->type)) { - DEBUGpic16_emitcode("; ***", "_naked function, no epilogue"); - return; + DEBUGpic16_emitcode("; ***", "_naked function, no epilogue"); + return; } - /* add code for ISCRITICAL */ - -#if 0 - if (IFFUNC_ISREENT(sym->type) || options.stackAuto) - { - pic16_emitcode ("mov","%s,_bp",spname); - } -#endif + _G.stack_lat = 0; - /* if use external stack but some variables were - added to the local stack then decrement the - local stack */ - if (options.useXstack && sym->stack) { - pic16_emitcode("mov","a,sp"); - pic16_emitcode("add","a,#0x%02x",((char)-sym->stack) & 0xff); - pic16_emitcode("mov","sp,a"); + /* add code for ISCRITICAL */ + if(IFFUNC_ISCRITICAL(sym->type)) { + /* if critical function, turn on interrupts */ + + /* TODO: add code here -- VR */ } - - -#if 0 - if ((IFFUNC_ISREENT(sym->type) || options.stackAuto)) { - if (options.useXstack) { - pic16_emitcode("mov","r0,%s",spname); - pic16_emitcode("movx","a,@r0"); - pic16_emitcode("mov","_bp,a"); - pic16_emitcode("dec","%s",spname); - } - else - { - pic16_emitcode ("pop","_bp"); - } + + if ((IFFUNC_ISREENT(sym->type) || options.stackAuto) + && sym->stack) { + if (sym->stack == 1) { + pic16_emitpcode(POC_INFSNZ, pic16_popCopyReg(&pic16_pc_fsr1l)); + pic16_emitpcode(POC_INCF, pic16_popCopyReg(&pic16_pc_fsr1h)); + } else { + // we have to add more than one... + pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_postinc1)); // this holds a return value! + pic16_emitpcode(POC_MOVLW, pic16_popGetLit(sym->stack-1)); + pic16_emitpcode(POC_ADDWF, pic16_popCopyReg(&pic16_pc_fsr1l)); + emitSKPNC; + pic16_emitpcode(POC_INCF, pic16_popCopyReg(&pic16_pc_fsr1h)); + pic16_emitpcode(POC_COMF, pic16_popCopyReg(&pic16_pc_wreg)); // WREG = -(WREG+1)! + pic16_emitpcode(POC_MOVFW, pic16_popCopyReg(&pic16_pc_plusw1)); // this holds a retrun value! + } } -#endif - if (IFFUNC_ISISR(sym->type)) { - /* now we need to restore the registers */ - /* if any registers used */ - if (sym->regsUsed) { - int i; +// sym->regsUsed = _G.fregsUsed; + + /* now we need to restore the registers */ + /* if any registers used */ + if (sym->regsUsed) { + int i; - /* restore registers used */ - DEBUGpic16_emitcode("; **", "Restoring used registers from stack"); - for ( i = sym->regsUsed->size; i >= 0; i--) { - if (bitVectBitValue(sym->regsUsed,i)) { + pic16_emitpinfo(INF_LOCALREGS, pic16_newpCodeOpLocalRegs(LR_EXIT_BEGIN)); + /* restore registers used */ + DEBUGpic16_emitcode("; **", "Restoring used registers from stack"); + for ( i = sym->regsUsed->size; i >= 0; i--) { + if (bitVectBitValue(sym->regsUsed,i)) { + pic16_poppCodeOp( pic16_popRegFromIdx(i) ); + _G.nRegsSaved--; + } + } + pic16_emitpinfo(INF_LOCALREGS, pic16_newpCodeOpLocalRegs(LR_EXIT_END)); -// fprintf(stderr, "%s:%d function %s uses register %s (restoring)\n", -// __FILE__, __LINE__, OP_SYMBOL(IC_LEFT(ic))->name, -// pic16_regWithIdx(i)->name); + } - pic16_poppCodeOp( pic16_popRegFromIdx(i) ); + if(strcmp(sym->name, "main")) { + if(1/*!options.ommitFramePtr ||*/ /*sym->regsUsed*/) { + /* restore stack frame */ + if(STACK_MODEL_LARGE) + pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_fsr2h )); +// pic16_emitpcode(POC_MOVFF, pic16_popCombine2( &pic16_pc_preinc1, &pic16_pc_fsr2h, 0)); + pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_fsr2l )); +// pic16_emitpcode(POC_MOVFF, pic16_popCombine2( &pic16_pc_preinc1, &pic16_pc_fsr2l, 0)); + } + } -// pic16_emitpcode(POC_MOVFF, pic16_popCombine2( -// &pic16_pc_preinc1, -// PCOR(pic16_popCopyReg( PCOR(pic16_popRegFromIdx(i)))), 0)); + _G.useWreg = 0; - } - } - } - - pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_fsr0h )); - pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_fsr0l)); - pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_prodh )); - pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_prodl )); - - if(!(_G.interruptvector == 1)) { - /* do not restore interrupt vector for WREG,STATUS,BSR - * for high priority interrupt, see genFunction */ - - pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_bsr )); - pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_status )); - pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_wreg )); - } - - _G.interruptvector = 0; /* sanity check */ + if (IFFUNC_ISISR(sym->type)) { + pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_fsr0h )); + pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_fsr0l)); + pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_prodh )); + pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_prodl )); -// pic16_pBlockConvert2ISR(pb); + if(!(_G.interruptvector == 1)) { + /* do not restore interrupt vector for WREG,STATUS,BSR + * for high priority interrupt, see genFunction */ + pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_bsr )); + pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_status )); + pic16_poppCodeOp( pic16_popCopyReg( &pic16_pc_wreg )); + } + _G.interruptvector = 0; /* sanity check */ - /* if debug then send end of function */ -/* if (options.debug && currFunc) */ - if (currFunc) { - debugFile->writeEndFunction (currFunc, ic, 1); - } - - if(_G.usefastretfie) - pic16_emitpcode(POC_RETFIE, pic16_newpCodeOpLit(1)); - else - pic16_emitpcodeNULLop(POC_RETFIE); - _G.usefastretfie = 0; - } else { - if (IFFUNC_ISCRITICAL(sym->type)) - pic16_emitcode("setb","ea"); + /* if debug then send end of function */ +/* if (options.debug && currFunc) */ + if (currFunc) { + debugFile->writeEndFunction (currFunc, ic, 1); + } + if(_G.usefastretfie) + pic16_emitpcode(POC_RETFIE, pic16_newpCodeOpLit(1)); + else + pic16_emitpcodeNULLop(POC_RETFIE); -// pic16_emitpcomment("exit regsUsed: %d\n", sym->regsUsed?sym->regsUsed->size:-1); + pic16_addpCode2pBlock(pb,pic16_newpCodeFunction(NULL,NULL)); + + _G.usefastretfie = 0; + return; + } - /* if any registers used */ - if (sym->regsUsed) { - int i; - /* save the registers used */ - DEBUGpic16_emitcode("; **", "Restoring used registers from stack"); - for ( i = sym->regsUsed->size; i >= 0; i--) { - if (bitVectBitValue(sym->regsUsed,i)) { - -// fprintf(stderr, "%s:%d function %s uses register %s (restoring)\n", -// __FILE__, __LINE__, OP_SYMBOL(IC_LEFT(ic))->name, -// pic16_regWithIdx(i)->name); - - pic16_poppCodeOp( pic16_popRegFromIdx(i) ); - -// pic16_emitpcode(POC_MOVFF, pic16_popCombine2( -// &pic16_pc_preinc1, -// PCOR(pic16_popCopyReg( PCOR(pic16_popRegFromIdx(i)))), 0)); + if (IFFUNC_ISCRITICAL(sym->type)) { + pic16_emitcode("setb","ea"); + } - _G.nRegsSaved--; - } - } - } - -// pic16_emitpcomment("%s: _G.nRegsSaved upon exit from function: %d\n", __FUNCTION__, _G.nRegsSaved); - /* if debug then send end of function */ - if (currFunc) { - debugFile->writeEndFunction (currFunc, ic, 1); - } + /* if debug then send end of function */ + if (currFunc) { + debugFile->writeEndFunction (currFunc, ic, 1); + } - /* insert code to restore stack frame, if user enabled it - * and function is not main() */ + /* insert code to restore stack frame, if user enabled it + * and function is not main() */ - if(strcmp(sym->name, "main")) { - if(/*!options.ommitFramePtr ||*/ sym->regsUsed) { - /* restore stack frame */ - if(STACK_MODEL_LARGE) - pic16_emitpcode(POC_MOVFF, - pic16_popCombine2( &pic16_pc_preinc1, &pic16_pc_fsr2h, 0)); - pic16_emitpcode(POC_MOVFF, - pic16_popCombine2( &pic16_pc_preinc1, &pic16_pc_fsr2l, 0)); - } - } - - pic16_emitpcodeNULLop(POC_RETURN); - - /* Mark the end of a function */ - pic16_addpCode2pBlock(pb,pic16_newpCodeFunction(NULL,NULL)); - } + pic16_emitpcodeNULLop(POC_RETURN); + /* Mark the end of a function */ + pic16_addpCode2pBlock(pb,pic16_newpCodeFunction(NULL,NULL)); } void pic16_storeForReturn(operand *op, int offset, pCodeOp *dest) { - - if(is_LitOp(op)) { - pic16_emitpcode(POC_MOVLW, pic16_popGet(AOP(op), offset)); // patch 12 - - if(dest->type != PO_WREG) - pic16_emitpcode(POC_MOVWF, dest); - } else { - if(dest->type == PO_WREG && (offset == 0)) { - pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(op), offset)); - return; - } - - pic16_emitpcode(POC_MOVFF, pic16_popGet2p( - pic16_popGet(AOP(op), offset), dest)); - } + if(is_LitOp(op)) { + unsigned long lit = (unsigned long)floatFromVal(AOP(op)->aopu.aop_lit); + if(lit == 0) { + pic16_emitpcode(POC_CLRF, dest); + } else { + pic16_emitpcode(POC_MOVLW, pic16_popGet(AOP(op), offset)); + if(dest->type != PO_WREG)pic16_emitpcode(POC_MOVWF, dest); + } + } else { + if(dest->type == PO_WREG && (offset == 0)) { + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(op), offset)); + return; + } + pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(op), offset), dest)); + } } /*-----------------------------------------------------------------*/ @@ -3814,7 +3805,7 @@ static void genRet (iCode *ic) int size; operand *left; - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + FENTRY; /* if we have no return value then * just generate the "ret" */ @@ -3934,15 +3925,14 @@ jumpret: /*-----------------------------------------------------------------*/ static void genLabel (iCode *ic) { + FENTRY; + /* special case never generate */ + if (IC_LABEL(ic) == entryLabel) + return ; - /* special case never generate */ - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - if (IC_LABEL(ic) == entryLabel) - return ; - - pic16_emitpLabel(IC_LABEL(ic)->key); - pic16_emitcode("","_%05d_DS_:",(IC_LABEL(ic)->key+100 + pic16_labelOffset)); + pic16_emitpLabel(IC_LABEL(ic)->key); +// pic16_emitcode("","_%05d_DS_:",(IC_LABEL(ic)->key+100 + pic16_labelOffset)); } /*-----------------------------------------------------------------*/ @@ -3951,8 +3941,9 @@ static void genLabel (iCode *ic) //tsd static void genGoto (iCode *ic) { + FENTRY; pic16_emitpcode(POC_GOTO,pic16_popGetLabel(IC_LABEL(ic)->key)); - pic16_emitcode ("goto","_%05d_DS_",(IC_LABEL(ic)->key+100)+pic16_labelOffset); +// pic16_emitcode ("goto","_%05d_DS_",(IC_LABEL(ic)->key+100)+pic16_labelOffset); } @@ -3963,7 +3954,7 @@ static void genMultbits (operand *left, operand *right, operand *result) { - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + FENTRY; if(!pic16_sameRegs(AOP(result),AOP(right))) pic16_emitpcode(POC_BSF, pic16_popGet(AOP(result),0)); @@ -3983,7 +3974,7 @@ static void genMultOneByte (operand *left, operand *result) { - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + FENTRY; DEBUGpic16_pic16_AopType(__LINE__,left,right,result); DEBUGpic16_pic16_AopTypeSign(__LINE__,left,right,result); @@ -4020,35 +4011,34 @@ static void genMultOneWord (operand *left, operand *right, operand *result) { + FENTRY; + DEBUGpic16_pic16_AopType(__LINE__,left,right,result); + DEBUGpic16_pic16_AopTypeSign(__LINE__,left,right,result); - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - DEBUGpic16_pic16_AopType(__LINE__,left,right,result); - DEBUGpic16_pic16_AopTypeSign(__LINE__,left,right,result); - - /* (if two literals, the value is computed before) - * if one literal, literal on the right */ - if (AOP_TYPE(left) == AOP_LIT){ - operand *t = right; - right = left; - left = t; - } + /* (if two literals, the value is computed before) + * if one literal, literal on the right */ + if (AOP_TYPE(left) == AOP_LIT){ + operand *t = right; + right = left; + left = t; + } - /* size is checked already == 2 */ -// size = AOP_SIZE(result); + /* size is checked already == 2 */ +// size = AOP_SIZE(result); - if (AOP_TYPE(right) == AOP_LIT) { - pic16_emitpcomment("multiply lit val:%s by variable %s and store in %s", - pic16_aopGet(AOP(right),0,FALSE,FALSE), - pic16_aopGet(AOP(left),0,FALSE,FALSE), - pic16_aopGet(AOP(result),0,FALSE,FALSE)); - } else { - pic16_emitpcomment("multiply variable :%s by variable %s and store in %s", - pic16_aopGet(AOP(right),0,FALSE,FALSE), - pic16_aopGet(AOP(left),0,FALSE,FALSE), - pic16_aopGet(AOP(result),0,FALSE,FALSE)); - } + if (AOP_TYPE(right) == AOP_LIT) { + pic16_emitpcomment("multiply lit val:%s by variable %s and store in %s", + pic16_aopGet(AOP(right),0,FALSE,FALSE), + pic16_aopGet(AOP(left),0,FALSE,FALSE), + pic16_aopGet(AOP(result),0,FALSE,FALSE)); + } else { + pic16_emitpcomment("multiply variable :%s by variable %s and store in %s", + pic16_aopGet(AOP(right),0,FALSE,FALSE), + pic16_aopGet(AOP(left),0,FALSE,FALSE), + pic16_aopGet(AOP(result),0,FALSE,FALSE)); + } - pic16_genMult16X16_16(left, right,result); + pic16_genMult16X16_16(left, right,result); } /*-----------------------------------------------------------------*/ @@ -4058,35 +4048,34 @@ static void genMultOneLong (operand *left, operand *right, operand *result) { + FENTRY; + DEBUGpic16_pic16_AopType(__LINE__,left,right,result); + DEBUGpic16_pic16_AopTypeSign(__LINE__,left,right,result); - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - DEBUGpic16_pic16_AopType(__LINE__,left,right,result); - DEBUGpic16_pic16_AopTypeSign(__LINE__,left,right,result); - - /* (if two literals, the value is computed before) - * if one literal, literal on the right */ - if (AOP_TYPE(left) == AOP_LIT){ - operand *t = right; - right = left; - left = t; - } + /* (if two literals, the value is computed before) + * if one literal, literal on the right */ + if (AOP_TYPE(left) == AOP_LIT){ + operand *t = right; + right = left; + left = t; + } - /* size is checked already == 4 */ -// size = AOP_SIZE(result); + /* size is checked already == 4 */ +// size = AOP_SIZE(result); - if (AOP_TYPE(right) == AOP_LIT) { - pic16_emitpcomment("multiply lit val:%s by variable %s and store in %s", - pic16_aopGet(AOP(right),0,FALSE,FALSE), - pic16_aopGet(AOP(left),0,FALSE,FALSE), - pic16_aopGet(AOP(result),0,FALSE,FALSE)); - } else { - pic16_emitpcomment("multiply variable :%s by variable %s and store in %s", - pic16_aopGet(AOP(right),0,FALSE,FALSE), - pic16_aopGet(AOP(left),0,FALSE,FALSE), - pic16_aopGet(AOP(result),0,FALSE,FALSE)); - } + if (AOP_TYPE(right) == AOP_LIT) { + pic16_emitpcomment("multiply lit val:%s by variable %s and store in %s", + pic16_aopGet(AOP(right),0,FALSE,FALSE), + pic16_aopGet(AOP(left),0,FALSE,FALSE), + pic16_aopGet(AOP(result),0,FALSE,FALSE)); + } else { + pic16_emitpcomment("multiply variable :%s by variable %s and store in %s", + pic16_aopGet(AOP(right),0,FALSE,FALSE), + pic16_aopGet(AOP(left),0,FALSE,FALSE), + pic16_aopGet(AOP(result),0,FALSE,FALSE)); + } - pic16_genMult32X32_32(left, right,result); + pic16_genMult32X32_32(left, right,result); } @@ -4100,7 +4089,7 @@ static void genMult (iCode *ic) operand *right = IC_RIGHT(ic); operand *result= IC_RESULT(ic); - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + FENTRY; /* assign the amsops */ pic16_aopOp (left,ic,FALSE); pic16_aopOp (right,ic,FALSE); @@ -4157,10 +4146,9 @@ static void genDivbits (operand *left, operand *right, operand *result) { + char *l; - char *l; - - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + FENTRY; /* the result must be bit */ pic16_emitcode("mov","b,%s",pic16_aopGet(AOP(right),0,FALSE,FALSE)); l = pic16_aopGet(AOP(left),0,FALSE,FALSE); @@ -4179,10 +4167,10 @@ static void genDivOneByte (operand *left, operand *right, operand *result) { - sym_link *opetype = operandType(result); - char *l ; - symbol *lbl ; - int size,offset; + sym_link *opetype = operandType(result); + char *l ; + symbol *lbl ; + int size,offset; /* result = divident / divisor * - divident may be a register or a literal, @@ -4192,7 +4180,8 @@ static void genDivOneByte (operand *left, * In addition we must handle signed and unsigned, which * result in 6 final different cases -- VR */ - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + FENTRY; + size = AOP_SIZE(result) - 1; offset = 1; /* signed or unsigned */ @@ -4205,9 +4194,9 @@ static void genDivOneByte (operand *left, /* unsigned is easy */ - pct1 = pic16_popGetTempReg(); - pct2 = pic16_popGetTempReg(); - pct3 = pic16_popGetTempReg(); + pct1 = pic16_popGetTempReg(1); + pct2 = pic16_popGetTempReg(1); + pct3 = pic16_popGetTempReg(1); label1 = newiTempLabel(NULL); label2 = newiTempLabel(NULL); @@ -4257,9 +4246,9 @@ static void genDivOneByte (operand *left, if(AOP_TYPE(result) != AOP_ACC) pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result), 0)); - pic16_popReleaseTempReg( pct3 ); - pic16_popReleaseTempReg( pct2 ); - pic16_popReleaseTempReg( pct1 ); + pic16_popReleaseTempReg( pct3, 1); + pic16_popReleaseTempReg( pct2, 1); + pic16_popReleaseTempReg( pct1, 1); return ; } @@ -4335,7 +4324,8 @@ static void genDiv (iCode *ic) * Division functions written here just in case someone * wants to inline and not use the support libraries -- VR */ - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + FENTRY; + /* assign the amsops */ pic16_aopOp (left,ic,FALSE); pic16_aopOp (right,ic,FALSE); @@ -4371,9 +4361,11 @@ static void genModbits (operand *left, operand *right, operand *result) { + char *l; - char *l; - + FENTRY; + + werror(W_POSSBUG2, __FILE__, __LINE__); /* the result must be bit */ pic16_emitcode("mov","b,%s",pic16_aopGet(AOP(right),0,FALSE,FALSE)); l = pic16_aopGet(AOP(left),0,FALSE,FALSE); @@ -4393,11 +4385,13 @@ static void genModOneByte (operand *left, operand *right, operand *result) { - sym_link *opetype = operandType(result); - char *l ; - symbol *lbl ; + sym_link *opetype = operandType(result); + char *l ; + symbol *lbl ; + + FENTRY; + werror(W_POSSBUG2, __FILE__, __LINE__); - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); /* signed or unsigned */ if (SPEC_USIGN(opetype)) { /* unsigned is easy */ @@ -4465,11 +4459,12 @@ static void genModOneByte (operand *left, /*-----------------------------------------------------------------*/ static void genMod (iCode *ic) { - operand *left = IC_LEFT(ic); - operand *right = IC_RIGHT(ic); - operand *result= IC_RESULT(ic); + operand *left = IC_LEFT(ic); + operand *right = IC_RIGHT(ic); + operand *result= IC_RESULT(ic); - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + FENTRY; + /* assign the amsops */ pic16_aopOp (left,ic,FALSE); pic16_aopOp (right,ic,FALSE); @@ -4507,8 +4502,8 @@ release : */ static void genIfxJump (iCode *ic, char *jval) { - - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + FENTRY; + /* if true label then we jump if condition supplied is true */ if ( IC_TRUE(ic) ) { @@ -4717,6 +4712,9 @@ static int genChkZeroes(operand *op, int lit, int size) /*-----------------------------------------------------------------*/ /* genCmp :- greater or less than comparison */ /*-----------------------------------------------------------------*/ +#if 1 + /* { */ + /* original code */ static void genCmp (operand *left,operand *right, operand *result, iCode *ifx, int sign) { @@ -4733,6 +4731,8 @@ static void genCmp (operand *left,operand *right, } */ + FENTRY; + resolveIfx(&rFalseIfx,ifx); truelbl = newiTempLabel(NULL); size = max(AOP_SIZE(left),AOP_SIZE(right)); @@ -5019,27 +5019,860 @@ static void genCmp (operand *left,operand *right, return; - } -#endif // _swapp + } +#endif // _swapp + + if(AOP_TYPE(left) == AOP_LIT) { + //symbol *lbl = newiTempLabel(NULL); + + //EXPERIMENTAL lit = (unsigned long)(floatFromVal(AOP(left)->aopu.aop_lit)); + + + DEBUGpic16_emitcode(";left lit","lit = 0x%x,sign=%d",lit,sign); + + /* Special cases */ + if((lit == 0) && (sign == 0)){ + + size--; + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),size)); + while(size) + pic16_emitpcode(POC_IORFW, pic16_popGet(AOP(right),--size)); + + genSkipz2(&rFalseIfx,0); + if(ifx) ifx->generated = 1; + return; + } + + if(size==1) { + /* Special cases */ + lit &= 0xff; + if(((lit == 0xff) && !sign) || ((lit==0x7f) && sign)) { + /* degenerate compare can never be true */ + if(rFalseIfx.condition == 0) + pic16_emitpcode(POC_GOTO,pic16_popGetLabel(rFalseIfx.lbl->key)); + + if(ifx) ifx->generated = 1; + return; + } + + if(sign) { + /* signed comparisons to a literal byte */ + + int lp1 = (lit+1) & 0xff; + + DEBUGpic16_emitcode(";left lit","line = %d lit = 0x%x",__LINE__,lit); + switch (lp1) { + case 0: + rFalseIfx.condition ^= 1; + genSkipCond(&rFalseIfx,right,0,7); + break; + case 0x7f: + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),0)); + pic16_emitpcode(POC_XORLW, pic16_popGetLit(0x7f)); + genSkipz2(&rFalseIfx,1); + break; + default: + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),0)); + pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x80)); + pic16_emitpcode(POC_ADDLW, pic16_popGetLit(((-(lit+1)) & 0xff) ^ 0x80)); + rFalseIfx.condition ^= 1; + genSkipc(&rFalseIfx); + break; + } + } else { + /* unsigned comparisons to a literal byte */ + + switch(lit & 0xff ) { + case 0: + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),0)); + genSkipz2(&rFalseIfx,0); + break; + case 0x7f: + rFalseIfx.condition ^= 1; + genSkipCond(&rFalseIfx,right,0,7); + break; + + default: + pic16_emitpcode(POC_MOVLW, pic16_popGetLit((lit+1) & 0xff)); + pic16_emitpcode(POC_SUBFW, pic16_popGet(AOP(right),0)); + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + rFalseIfx.condition ^= 1; + if (AOP_TYPE(result) == AOP_CRY) + genSkipc(&rFalseIfx); + else { + pic16_emitpcode(POC_CLRF, pic16_popGet(AOP(result),0)); + pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),0)); + } + break; + } + } + + if(ifx) ifx->generated = 1; + if ((AOP_TYPE(result) != AOP_CRY) && (AOP_SIZE(result))) + goto check_carry; + return; + + } else { + + /* Size is greater than 1 */ + + if(sign) { + int lp1 = lit+1; + + size--; + + if(lp1 == 0) { + /* this means lit = 0xffffffff, or -1 */ + + + DEBUGpic16_emitcode(";left lit = -1","line = %d ",__LINE__); + rFalseIfx.condition ^= 1; + genSkipCond(&rFalseIfx,right,size,7); + if(ifx) ifx->generated = 1; + + if ((AOP_TYPE(result) != AOP_CRY) && (AOP_SIZE(result))) + goto check_carry; + + return; + } + + if(lit == 0) { + int s = size; + + if(rFalseIfx.condition) { + pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(right),size,FALSE,FALSE),7,0, PO_GPR_REGISTER)); + pic16_emitpcode(POC_GOTO, pic16_popGetLabel(truelbl->key)); + } + + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),size)); + while(size--) + pic16_emitpcode(POC_IORFW, pic16_popGet(AOP(right),size)); + + + emitSKPZ; + if(rFalseIfx.condition) { + pic16_emitpcode(POC_GOTO, pic16_popGetLabel(rFalseIfx.lbl->key)); + pic16_emitpLabel(truelbl->key); + }else { + rFalseIfx.condition ^= 1; + genSkipCond(&rFalseIfx,right,s,7); + } + + if(ifx) ifx->generated = 1; + + if ((AOP_TYPE(result) != AOP_CRY) && (AOP_SIZE(result))) + goto check_carry; + + return; + } + + if((size == 1) && (0 == (lp1&0xff))) { + /* lower byte of signed word is zero */ + DEBUGpic16_emitcode(";left lit","line = %d 0x%x+1 low byte is zero",__LINE__,lit); + i = ((lp1 >> 8) & 0xff) ^0x80; + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),size)); + pic16_emitpcode(POC_ADDLW, pic16_popGetLit( 0x80)); + pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x100-i)); + rFalseIfx.condition ^= 1; + genSkipc(&rFalseIfx); + + + if(ifx) ifx->generated = 1; + + if ((AOP_TYPE(result) != AOP_CRY) && (AOP_SIZE(result))) + goto check_carry; + + return; + } + + if(lit & (0x80 << (size*8))) { + /* Lit is less than zero */ + DEBUGpic16_emitcode(";left lit","line = %d 0x%x is less than 0",__LINE__,lit); + //rFalseIfx.condition ^= 1; + //genSkipCond(&rFalseIfx,left,size,7); + //rFalseIfx.condition ^= 1; + pic16_emitpcode(POC_BTFSS, pic16_newpCodeOpBit(pic16_aopGet(AOP(right),size,FALSE,FALSE),7,0, PO_GPR_REGISTER)); + //pic16_emitpcode(POC_GOTO, pic16_popGetLabel(truelbl->key)); + + if(rFalseIfx.condition) + pic16_emitpcode(POC_GOTO, pic16_popGetLabel(rFalseIfx.lbl->key)); + else + pic16_emitpcode(POC_GOTO, pic16_popGetLabel(truelbl->key)); + + + } else { + /* Lit is greater than or equal to zero */ + DEBUGpic16_emitcode(";left lit","line = %d 0x%x is greater than 0",__LINE__,lit); + //rFalseIfx.condition ^= 1; + //genSkipCond(&rFalseIfx,right,size,7); + //rFalseIfx.condition ^= 1; + + //pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(right),size,FALSE,FALSE),7,0)); + //pic16_emitpcode(POC_GOTO, pic16_popGetLabel(rFalseIfx.lbl->key)); + + pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(right),size,FALSE,FALSE),7,0, PO_GPR_REGISTER)); + if(rFalseIfx.condition) + pic16_emitpcode(POC_GOTO, pic16_popGetLabel(truelbl->key)); + else + pic16_emitpcode(POC_GOTO, pic16_popGetLabel(rFalseIfx.lbl->key)); + + } + + pic16_emitpcode(POC_MOVLW, pic16_popGetLit((lp1 >> (size*8)) & 0xff)); + pic16_emitpcode(POC_SUBFW, pic16_popGet(AOP(right),size)); + + while(size--) { + + pic16_emitpcode(POC_MOVLW, pic16_popGetLit((lp1 >> (size*8)) & 0xff)); + emitSKPNZ; + pic16_emitpcode(POC_SUBFW, pic16_popGet(AOP(right),size)); + } + rFalseIfx.condition ^= 1; + //rFalseIfx.condition = 1; + genSkipc(&rFalseIfx); + + pic16_emitpLabel(truelbl->key); + + if(ifx) ifx->generated = 1; + + + if ((AOP_TYPE(result) != AOP_CRY) && (AOP_SIZE(result))) + goto check_carry; + + return; + // end of if (sign) + } else { + + /* compare word or long to an unsigned literal on the right.*/ + + + size--; + if(lit < 0xff) { + DEBUGpic16_emitcode ("; ***","%s %d lit =0x%x < 0xff",__FUNCTION__,__LINE__,lit); + switch (lit) { + case 0: + break; /* handled above */ +/* + case 0xff: + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),size)); + while(size--) + pic16_emitpcode(POC_IORFW, pic16_popGet(AOP(right),size)); + genSkipz2(&rFalseIfx,0); + break; +*/ + default: + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),size)); + while(--size) + pic16_emitpcode(POC_IORFW, pic16_popGet(AOP(right),size)); + + emitSKPZ; + if(rFalseIfx.condition) + pic16_emitpcode(POC_GOTO, pic16_popGetLabel(rFalseIfx.lbl->key)); + else + pic16_emitpcode(POC_GOTO, pic16_popGetLabel(truelbl->key)); + + + pic16_emitpcode(POC_MOVLW, pic16_popGetLit(lit+1)); + pic16_emitpcode(POC_SUBFW, pic16_popGet(AOP(right),0)); + + rFalseIfx.condition ^= 1; + genSkipc(&rFalseIfx); + } + + pic16_emitpLabel(truelbl->key); + + if(ifx) ifx->generated = 1; + + if ((AOP_TYPE(result) != AOP_CRY) && (AOP_SIZE(result))) + goto check_carry; + + return; + } + + + lit++; + DEBUGpic16_emitcode ("; ***","%s %d lit =0x%x",__FUNCTION__,__LINE__,lit); + i = (lit >> (size*8)) & 0xff; + + pic16_emitpcode(POC_MOVLW, pic16_popGetLit(i)); + pic16_emitpcode(POC_SUBFW, pic16_popGet(AOP(right),size)); + + while(size--) { + i = (lit >> (size*8)) & 0xff; + + if(i) { + pic16_emitpcode(POC_MOVLW, pic16_popGetLit(i)); + emitSKPNZ; + pic16_emitpcode(POC_SUBFW, pic16_popGet(AOP(right),size)); + } else { + /* this byte of the lit is zero, + * if it's not the last then OR in the variable */ + if(size) + pic16_emitpcode(POC_IORFW, pic16_popGet(AOP(right),size)); + } + } + + + pic16_emitpLabel(lbl->key); + + rFalseIfx.condition ^= 1; + + genSkipc(&rFalseIfx); + } + + if(sign) + pic16_emitpLabel(truelbl->key); + if(ifx) ifx->generated = 1; + + if ((AOP_TYPE(result) != AOP_CRY) && (AOP_SIZE(result))) + goto check_carry; + + return; + } + } + /* Compare two variables */ + + DEBUGpic16_emitcode(";sign","%d",sign); + + size--; + if(sign) { + /* Sigh. thus sucks... */ + if(size) { + pCodeOp *pctemp; + + pctemp = pic16_popGetTempReg(1); + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(left),size)); + pic16_emitpcode(POC_MOVWF, pctemp); //pic16_pic16_popRegFromIdx(pic16_Gstack_base_addr)); + pic16_emitpcode(POC_MOVLW, pic16_popGetLit(0x80)); + pic16_emitpcode(POC_XORWF, pctemp); //pic16_popRegFromIdx(pic16_Gstack_base_addr)); + pic16_emitpcode(POC_XORFW, pic16_popGet(AOP(right),size)); + pic16_emitpcode(POC_SUBFW, pctemp); //pic16_popRegFromIdx(pic16_Gstack_base_addr)); + pic16_popReleaseTempReg(pctemp, 1); + } else { + /* Signed char comparison */ + /* Special thanks to Nikolai Golovchenko for this snippet */ + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),0)); + pic16_emitpcode(POC_SUBFW, pic16_popGet(AOP(left),0)); + pic16_emitpcode(POC_RRCFW, pic16_popGet(AOP(left),0)); /* could be any register */ + pic16_emitpcode(POC_XORFW, pic16_popGet(AOP(left),0)); + pic16_emitpcode(POC_XORFW, pic16_popGet(AOP(right),0)); + pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x80)); + + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + genSkipc(&rFalseIfx); + + if(ifx) ifx->generated = 1; + + if ((AOP_TYPE(result) != AOP_CRY) && (AOP_SIZE(result))) + goto check_carry; + + return; + } + + } else { + + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),size)); + pic16_emitpcode(POC_SUBFW, pic16_popGet(AOP(left),size)); + } + + + /* The rest of the bytes of a multi-byte compare */ + while (size) { + + emitSKPZ; + pic16_emitpcode(POC_GOTO, pic16_popGetLabel(lbl->key)); + size--; + + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),size)); + pic16_emitpcode(POC_SUBFW, pic16_popGet(AOP(left),size)); + + + } + + pic16_emitpLabel(lbl->key); + + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + if ((AOP_TYPE(result) == AOP_CRY && AOP_SIZE(result)) || + (AOP_TYPE(result) == AOP_REG)) { + pic16_emitpcode(POC_CLRF, pic16_popGet(AOP(result),0)); + pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),0)); + } else { + genSkipc(&rFalseIfx); + } + //genSkipc(&rFalseIfx); + if(ifx) ifx->generated = 1; + + + if ((AOP_TYPE(result) != AOP_CRY) && (AOP_SIZE(result))) + goto check_carry; + + return; + + } + +check_carry: + if ((AOP_TYPE(result) != AOP_CRY) + && AOP_SIZE(result)) { + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + + if(!ifx)pic16_emitpLabel( rFalseIfx.lbl->key ); + + pic16_outBitC(result); + } else { + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + /* if the result is used in the next + ifx conditional branch then generate + code a little differently */ + if (ifx ) + genIfxJump (ifx,"c"); + else + pic16_outBitC(result); + /* leave the result in acc */ + } + +} + +#else /* old version of genCmp() */ /* } else { */ + +/* check all condition and return appropriate instruction, POC_CPFSGT or POC_CPFFSLT */ +static int selectCompareOp(resolvedIfx *rIfx, iCode *ifx, + operand *result, int offset, int invert_op) +{ + /* add code here */ + + /* check condition, > or < ?? */ + if(rIfx->condition != 0)invert_op ^= 1; + + if(ifx && IC_FALSE(ifx))invert_op ^= 1; + + if(!ifx)invert_op ^= 1; + + DEBUGpic16_emitcode("; +++", "%s:%d %s] rIfx->condition= %d, ifx&&IC_FALSE(ifx)= %d, invert_op = %d", + __FILE__, __LINE__, __FUNCTION__, rIfx->condition, (ifx && IC_FALSE(ifx)), invert_op); + + /* do selection */ + if(!invert_op)return POC_CPFSGT; + else return POC_CPFSLT; +} + +static int compareAopfirstpass=1; + +static void compareAop(resolvedIfx *resIfx, iCode *ifx, symbol *falselbl, + operand *oper, int offset, operand *result, + int sign, int invert_op, pCodeOp *pcop, pCodeOp *pcop2, + symbol *tlbl) +{ + int op; + symbol *truelbl; + + /* invert if there is a result to be loaded, in order to fit, + * SETC/CLRC sequence */ + if(AOP_SIZE(result))invert_op ^= 1; + +// if(sign && !offset)invert_op ^= 1; + +// if(sign)invert_op ^= 1; + + op = selectCompareOp(resIfx, ifx, result, offset, invert_op); + + if(AOP_SIZE(result) && compareAopfirstpass) { + if(!ifx) { + if(pcop2) + pic16_emitpcode(POC_SETF, pcop2); + else + emitSETC; + } else { + if(pcop2) + pic16_emitpcode(POC_CLRF, pcop2); + else + emitCLRC; + } + } + + compareAopfirstpass = 0; + + /* there is a bug when comparing operands with size > 1, + * because higher bytes can be equal and test should be performed + * to the next lower byte, current algorithm, considers operands + * inequal in these cases! -- VR 20041107 */ + + + if(pcop) + pic16_emitpcode(op, pcop); + else + pic16_emitpcode(op, pic16_popGet(AOP(oper), offset)); + + + if((!sign || !offset) && AOP_SIZE(result)) { + if(!ifx) { + if(pcop2) + pic16_emitpcode(POC_CLRF, pcop2); + else + emitCLRC; + } else { + if(pcop2) + pic16_emitpcode(POC_SETF, pcop2); + else + emitSETC; + } + + /* don't emit final branch (offset == 0) */ + if(offset) { + + if(pcop2) + pic16_emitpcode(POC_RRCF, pcop2); + + pic16_emitpcode(POC_BNC, pic16_popGetLabel(falselbl->key)); + } + } else { + if((ifx && (IC_TRUE(ifx)!=NULL)) || (sign && !offset)) { + DEBUGpic16_emitcode ("; +++","%s: %d: ifx = %p, IC_TRUE(ifx) = %d, sign = %d, offset = %d", + __FUNCTION__, __LINE__, ifx, (ifx&&IC_TRUE(ifx)), sign, offset); + + truelbl = newiTempLabel( NULL ); + pic16_emitpcode(POC_BRA, pic16_popGetLabel(truelbl->key)); + if((!ifx || !IC_TRUE(ifx)) && (sign && !offset)) + pic16_emitpcode(POC_GOTO, pic16_popGetLabel(tlbl->key)); + else + pic16_emitpcode(POC_GOTO, pic16_popGetLabel(resIfx->lbl->key)); + pic16_emitpLabel(truelbl->key); + } else { + pic16_emitpcode(POC_GOTO, pic16_popGetLabel(resIfx->lbl->key)); + } + } +} + + + + +#if 1 /* { */ +static void genCmp (operand *left, operand *right, + operand *result, iCode *ifx, int sign) +{ + int size, cmpop=1; + long lit = 0L; + resolvedIfx rFalseIfx; + symbol *falselbl, *tlbl; + + FENTRY; + + DEBUGpic16_pic16_AopType(__LINE__,left,right,result); + + resolveIfx(&rFalseIfx, ifx); + size = max(AOP_SIZE(left), AOP_SIZE(right)); + + /* if left & right are bit variables */ + if(AOP_TYPE(left) == AOP_CRY + && AOP_TYPE(right) == AOP_CRY ) { + + pic16_emitcode("mov","c,%s",AOP(right)->aopu.aop_dir); + pic16_emitcode("anl","c,/%s",AOP(left)->aopu.aop_dir); + + werror(W_POSSBUG2, __FILE__, __LINE__); + exit(-1); + } + + /* if literal is on the right then swap with left */ + if((AOP_TYPE(right) == AOP_LIT)) { + operand *tmp = right ; +// unsigned long mask = (0x100 << (8*(size-1))) - 1; + + lit = /*(unsigned long)*/floatFromVal(AOP(right)->aopu.aop_lit); + +// lit = (lit - 1) & mask; + right = left; + left = tmp; + rFalseIfx.condition ^= 1; /* reverse compare */ + } else + if ((AOP_TYPE(left) == AOP_LIT)) { + /* float compares are handled by support functions */ + lit = /*(unsigned long)*/floatFromVal(AOP(left)->aopu.aop_lit); + } + + /* actual comparing algorithm */ +// size = AOP_SIZE( right ); + + falselbl = newiTempLabel( NULL ); + if(AOP_TYPE(left) == AOP_LIT) { + /* compare to literal */ + DEBUGpic16_emitcode ("; ***","%s: %d: compare to literal", __FUNCTION__, __LINE__); + + if(sign) { + pCodeOp *pct, *pct2; + symbol *tlbl1; + + /* signed compare */ + DEBUGpic16_emitcode ("; ***","%s: %d: signed compare", __FUNCTION__, __LINE__); + + pct = pic16_popCopyReg(&pic16_pc_prodl); + pct2 = pic16_popCopyReg(&pic16_pc_prodh); + tlbl = newiTempLabel( NULL ); + + /* first compare signs: + * a. if both are positive, compare just like unsigned + * b. if both are negative, invert cmpop, compare just like unsigned + * c. if different signs, determine the result directly */ + + size--; + +#if 1 + /* { */ + tlbl1 = newiTempLabel( NULL ); +// pic16_emitpcode(POC_RLCFW, pic16_popGet( AOP(right), size)); /* move sign to carry */ + + if(lit > 0) { + + /* literal is zero or positive: + * a. if carry is zero, too, continue compare, + * b. if carry is set, then continue depending on cmpop ^ condition: + * 1. '<' return false (literal < variable), + * 2. '>' return true (literal > variable) */ +// pic16_emitpcode(POC_BNC, pic16_popGetLabel( tlbl1->key )); + pic16_emitpcode(POC_BTFSC, pic16_popCopyGPR2Bit(pic16_popGet(AOP(right), size), 7)); + + + if(cmpop ^ rFalseIfx.condition)pic16_emitpcode(POC_GOTO, pic16_popGetLabel( tlbl->key )); + else pic16_emitpcode(POC_GOTO, pic16_popGetLabel( rFalseIfx.lbl->key )); + } else + if(lit < 0) { + + /* literal is negative: + * a. if carry is set, too, continue compare, + * b. if carry is zero, then continue depending on cmpop ^ condition: + * 1. '<' return true (literal < variable), + * 2. '>' return false (literal > variable) */ +// pic16_emitpcode(POC_BC, pic16_popGetLabel( tlbl1->key )); + pic16_emitpcode(POC_BTFSS, pic16_popCopyGPR2Bit(pic16_popGet(AOP(right), size), 7)); + + if(!(cmpop ^ rFalseIfx.condition))pic16_emitpcode(POC_GOTO, pic16_popGetLabel( tlbl->key )); + else pic16_emitpcode(POC_GOTO, pic16_popGetLabel( rFalseIfx.lbl->key)); + } +#if 1 + else { + /* lit == 0 */ + pic16_emitpcode(POC_BTFSC, pic16_popCopyGPR2Bit(pic16_popGet(AOP(right), size), 7)); + + if(!(cmpop ^ rFalseIfx.condition))pic16_emitpcode(POC_GOTO, pic16_popGetLabel( tlbl->key )); + else pic16_emitpcode(POC_GOTO, pic16_popGetLabel( rFalseIfx.lbl->key)); + } +#endif + + + pic16_emitpLabel( tlbl1->key ); +#endif /* } */ + + compareAopfirstpass=1; +// pic16_emitpcode(POC_MOVLW, pic16_popGetLit(0x80)); +// pic16_emitpcode(POC_ADDFW, pic16_popGet(AOP(right), size)); +// pic16_emitpcode(POC_MOVWF, pct); + +// pic16_emitpcode(POC_MOVLW, pic16_popGetLit(BYTEofLONG(lit, size) + 0x80)); + pic16_emitpcode(POC_MOVLW, pic16_popGetLit(BYTEofLONG(lit, size))); +// compareAop(&rFalseIfx, ifx, falselbl, right, size, result, sign, 1, pct, pct2, tlbl); + compareAop(&rFalseIfx, ifx, falselbl, right, size, result, sign, 1, NULL, pct2, tlbl); + + /* generic case */ + while( size-- ) { +// pic16_emitpcode(POC_MOVLW, pic16_popGetLit(0x80)); +// pic16_emitpcode(POC_MOVLW, pic16_popGetLit(0x0)); +// pic16_emitpcode(POC_ADDFW, pic16_popGet(AOP(right), size)); +// pic16_emitpcode(POC_MOVWF, pct); + +// pic16_emitpcode(POC_MOVLW, pic16_popGetLit(BYTEofLONG(lit, size) + 0x80)); + pic16_emitpcode(POC_MOVLW, pic16_popGetLit(BYTEofLONG(lit, size) + 0x0)); + compareAop(&rFalseIfx, ifx, falselbl, right, size, result, sign, 1, NULL, pct2, tlbl); +// compareAop(&rFalseIfx, ifx, falselbl, right, size, result, sign, 1, pct, pct2, tlbl); +// compareAop(&rFalseIfx, ifx, falselbl, right, size, result, sign, 0, pct, pct2, tlbl); + } +// } + + if(ifx)ifx->generated = 1; + + if(AOP_SIZE(result)) { + pic16_emitpLabel(tlbl->key); + pic16_emitpLabel(falselbl->key); + pic16_outBitOp( result, pct2 ); + } else { + pic16_emitpLabel(tlbl->key); + } + } else { + + + /* unsigned compare */ + DEBUGpic16_emitcode ("; ***","%s: %d: unsigned compare", __FUNCTION__, __LINE__); + + compareAopfirstpass=1; + while(size--) { + + pic16_emitpcode(POC_MOVLW, pic16_popGetLit(BYTEofLONG(lit, size))); + compareAop(&rFalseIfx, ifx, falselbl, right, size, result, sign, 1, NULL, NULL, NULL); + + } + + if(ifx)ifx->generated = 1; + + + if(AOP_SIZE(result)) { + pic16_emitpLabel(falselbl->key); + pic16_outBitC( result ); + } + + } + } else { + /* compare registers */ + DEBUGpic16_emitcode ("; ***","%s: %d: compare registers", __FUNCTION__, __LINE__); + + + if(sign) { + pCodeOp *pct, *pct2; + + /* signed compare */ + DEBUGpic16_emitcode ("; ***","%s: %d: signed compare", __FUNCTION__, __LINE__); + + pct = pic16_popCopyReg(&pic16_pc_prodl); /* first temporary register */ + pct2 = pic16_popCopyReg(&pic16_pc_prodh); /* second temporary register */ + tlbl = newiTempLabel( NULL ); + + compareAopfirstpass=1; + + size--; + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right), size)); +// pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x80)); + pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x0)); + pic16_emitpcode(POC_MOVWF, pct); + + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(left), size)); +// pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x80)); + pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x0)); + + /* WREG already holds left + 0x80 */ + compareAop(&rFalseIfx, ifx, falselbl, right, size, result, sign, 1, pct, pct2, tlbl); + + while( size-- ) { + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right), size)); +// pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x80)); + pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x0)); + pic16_emitpcode(POC_MOVWF, pct); + + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(left), size)); +// pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x80)); + pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x0)); + + /* WREG already holds left + 0x80 */ + compareAop(&rFalseIfx, ifx, falselbl, right, size, result, sign, 1, pct, pct2, tlbl); +// compareAop(&rFalseIfx, ifx, falselbl, right, size, result, sign, 0, pct, pct2, tlbl); + } + + if(ifx)ifx->generated = 1; + + if(AOP_SIZE(result)) { + pic16_emitpLabel(tlbl->key); + pic16_emitpLabel(falselbl->key); + pic16_outBitOp( result, pct2 ); + } else { + pic16_emitpLabel(tlbl->key); + } + + } else { + /* unsigned compare */ + DEBUGpic16_emitcode ("; ***","%s: %d: unsigned compare", __FUNCTION__, __LINE__); + + compareAopfirstpass=1; + while(size--) { + + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(left), size)); + compareAop(&rFalseIfx, ifx, falselbl, right, size, result, sign, 1, NULL, NULL, NULL); + + } + + if(ifx)ifx->generated = 1; + if(AOP_SIZE(result)) { + + pic16_emitpLabel(falselbl->key); + pic16_outBitC( result ); + } + + } + } +} + +#else /* } else { */ + +/* new version of genCmp -- VR 20041012 */ +static void genCmp (operand *left,operand *right, + operand *result, iCode *ifx, int sign) +{ + int size; //, offset = 0 ; + unsigned long lit = 0L,i = 0; + resolvedIfx rFalseIfx; + int willCheckCarry=0; + // resolvedIfx rTrueIfx; + symbol *truelbl; + + FENTRY; + + /* General concept: + * subtract right from left if at the end the carry flag is set then we + * know that left is greater than right */ + + resolveIfx(&rFalseIfx,ifx); + truelbl = newiTempLabel(NULL); + size = max(AOP_SIZE(left),AOP_SIZE(right)); + + DEBUGpic16_pic16_AopType(__LINE__,left,right,result); + + /* POC_CPFSGT compare f, wreg, skip if f greater than wreg + * POC_CPFSLT compare f, wreg, skip if f less then wreg */ + + + /* if literal is on the right then swap with left */ + if ((AOP_TYPE(right) == AOP_LIT)) { + operand *tmp = right ; + unsigned long mask = (0x100 << (8*(size-1))) - 1; + + lit = (unsigned long)floatFromVal(AOP(right)->aopu.aop_lit); + +// lit = (lit - 1) & mask; + right = left; + left = tmp; + rFalseIfx.condition ^= 1; /* reverse compare */ + } else + if ((AOP_TYPE(left) == AOP_LIT)) { + /* float compares are handled by support functions */ + lit = (unsigned long)floatFromVal(AOP(left)->aopu.aop_lit); + } + - if(AOP_TYPE(left) == AOP_LIT) { - //symbol *lbl = newiTempLabel(NULL); + //if(IC_TRUE(ifx) == NULL) + /* if left & right are bit variables */ + if (AOP_TYPE(left) == AOP_CRY && + AOP_TYPE(right) == AOP_CRY ) { - //EXPERIMENTAL lit = (unsigned long)(floatFromVal(AOP(left)->aopu.aop_lit)); + pic16_emitcode("mov","c,%s",AOP(right)->aopu.aop_dir); + pic16_emitcode("anl","c,/%s",AOP(left)->aopu.aop_dir); + } else { + symbol *lbl = newiTempLabel(NULL); - DEBUGpic16_emitcode(";left lit","lit = 0x%x,sign=%d",lit,sign); + if(AOP_TYPE(left) == AOP_LIT) { + DEBUGpic16_emitcode(";left lit","lit = 0x%x, sign=%d",lit,sign); - /* Special cases */ - if((lit == 0) && (sign == 0)){ + if ((AOP_TYPE(result) != AOP_CRY) && (AOP_SIZE(result))) + willCheckCarry = 1; + else willCheckCarry = 0; + /* Special cases */ + if((lit == 0) && (sign == 0)) { + /* unsigned compare to 0 */ + DEBUGpic16_emitcode("; unsigned compare to 0","lit = 0x%x, sign=%d",lit,sign); + size--; pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),size)); while(size) pic16_emitpcode(POC_IORFW, pic16_popGet(AOP(right),--size)); genSkipz2(&rFalseIfx,0); - if(ifx) ifx->generated = 1; + if(ifx)ifx->generated = 1; return; } @@ -5057,10 +5890,11 @@ static void genCmp (operand *left,operand *right, if(sign) { /* signed comparisons to a literal byte */ + DEBUGpic16_emitcode(";signed compare to literal","%d: lit = 0x%x, sign=%d",__LINE__, lit,sign); int lp1 = (lit+1) & 0xff; - DEBUGpic16_emitcode(";left lit","line = %d lit = 0x%x",__LINE__,lit); + DEBUGpic16_emitcode(";left lit","line = %d lit = 0x%x condition = %d lp1 = %i",__LINE__,lit, rFalseIfx.condition, lp1); switch (lp1) { case 0: rFalseIfx.condition ^= 1; @@ -5072,17 +5906,35 @@ static void genCmp (operand *left,operand *right, genSkipz2(&rFalseIfx,1); break; default: - pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),0)); + pic16_emitpcode(POC_MOVLW, pic16_popGetLit( lit )); + + if(rFalseIfx.condition) + pic16_emitpcode(POC_CPFSLT, pic16_popGet(AOP(right), 0)); + else + pic16_emitpcode(POC_CPFSGT, pic16_popGet(AOP(right), 0)); + + if(willCheckCarry) { + if(!rFalseIfx.condition) { emitCLRC; emitSETC; } + else { emitSETC; emitCLRC; } + + } else { + pic16_emitpcode(POC_GOTO, pic16_popGetLabel(rFalseIfx.lbl->key)); + } + +/* pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),0)); pic16_emitpcode(POC_ADDLW, pic16_popGetLit(0x80)); pic16_emitpcode(POC_ADDLW, pic16_popGetLit(((-(lit+1)) & 0xff) ^ 0x80)); rFalseIfx.condition ^= 1; genSkipc(&rFalseIfx); +*/ break; } } else { /* unsigned comparisons to a literal byte */ + DEBUGpic16_emitcode("; unsigned compare to literal","%d: lit = 0x%x, sign=%d",__LINE__, lit,sign); switch(lit & 0xff ) { + /* special cases */ case 0: pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),0)); genSkipz2(&rFalseIfx,0); @@ -5091,7 +5943,6 @@ static void genCmp (operand *left,operand *right, rFalseIfx.condition ^= 1; genSkipCond(&rFalseIfx,right,0,7); break; - default: pic16_emitpcode(POC_MOVLW, pic16_popGetLit((lit+1) & 0xff)); pic16_emitpcode(POC_SUBFW, pic16_popGet(AOP(right),0)); @@ -5316,14 +6167,14 @@ static void genCmp (operand *left,operand *right, if(size) { pCodeOp *pctemp; - pctemp = pic16_popGetTempReg(); + pctemp = pic16_popGetTempReg(1); pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(left),size)); pic16_emitpcode(POC_MOVWF, pctemp); //pic16_pic16_popRegFromIdx(pic16_Gstack_base_addr)); pic16_emitpcode(POC_MOVLW, pic16_popGetLit(0x80)); pic16_emitpcode(POC_XORWF, pctemp); //pic16_popRegFromIdx(pic16_Gstack_base_addr)); pic16_emitpcode(POC_XORFW, pic16_popGet(AOP(right),size)); pic16_emitpcode(POC_SUBFW, pctemp); //pic16_popRegFromIdx(pic16_Gstack_base_addr)); - pic16_popReleaseTempReg(pctemp); + pic16_popReleaseTempReg(pctemp, 1); } else { /* Signed char comparison */ /* Special thanks to Nikolai Golovchenko for this snippet */ @@ -5399,17 +6250,24 @@ check_carry: } } +#endif /* } */ + + +#endif /* } */ + + /*-----------------------------------------------------------------*/ /* genCmpGt :- greater than comparison */ /*-----------------------------------------------------------------*/ static void genCmpGt (iCode *ic, iCode *ifx) { - operand *left, *right, *result; - sym_link *letype , *retype; - int sign ; + operand *left, *right, *result; + sym_link *letype , *retype; + int sign ; - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + FENTRY; + left = IC_LEFT(ic); right= IC_RIGHT(ic); result = IC_RESULT(ic); @@ -5434,11 +6292,12 @@ static void genCmpGt (iCode *ic, iCode *ifx) /*-----------------------------------------------------------------*/ static void genCmpLt (iCode *ic, iCode *ifx) { - operand *left, *right, *result; - sym_link *letype , *retype; - int sign ; + operand *left, *right, *result; + sym_link *letype , *retype; + int sign ; + + FENTRY; - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); left = IC_LEFT(ic); right= IC_RIGHT(ic); result = IC_RESULT(ic); @@ -5775,10 +6634,10 @@ static void gencjne(operand *left, operand *right, iCode *ifx) /*-----------------------------------------------------------------*/ static bool is_LitOp(operand *op) { - return (AOP_TYPE(op) == AOP_LIT) + return ((AOP_TYPE(op) == AOP_LIT) || ( (AOP_TYPE(op) == AOP_PCODE) && ( (AOP(op)->aopu.pcop->type == PO_LITERAL) - || (AOP(op)->aopu.pcop->type == PO_IMMEDIATE) )); + || (AOP(op)->aopu.pcop->type == PO_IMMEDIATE) ))); } /*-----------------------------------------------------------------*/ @@ -5786,10 +6645,10 @@ static bool is_LitOp(operand *op) /*-----------------------------------------------------------------*/ static bool is_LitAOp(asmop *aop) { - return (aop->type == AOP_LIT) + return ((aop->type == AOP_LIT) || ( (aop->type == AOP_PCODE) && ( (aop->aopu.pcop->type == PO_LITERAL) - || (aop->aopu.pcop->type == PO_IMMEDIATE) )); + || (aop->aopu.pcop->type == PO_IMMEDIATE) ))); } @@ -5807,15 +6666,17 @@ static void genCmpEq (iCode *ic, iCode *ifx) int generate_result = 0; int i=0; + FENTRY; + pic16_aopOp((left=IC_LEFT(ic)),ic,FALSE); pic16_aopOp((right=IC_RIGHT(ic)),ic,FALSE); pic16_aopOp((result=IC_RESULT(ic)),ic,TRUE); - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); DEBUGpic16_pic16_AopType(__LINE__,left,right,result); if( (AOP_TYPE(right) == AOP_CRY) || (AOP_TYPE(left) == AOP_CRY) ) { + werror(W_POSSBUG2, __FILE__, __LINE__); DEBUGpic16_emitcode ("; ***","%s %d -- ERROR",__FUNCTION__,__LINE__); fprintf(stderr, "%s %d error - left/right CRY operands not supported\n",__FUNCTION__,__LINE__); goto release; @@ -6230,8 +7091,9 @@ release: /*-----------------------------------------------------------------*/ static iCode *ifxForOp ( operand *op, iCode *ic ) { + FENTRY2; + /* if true symbol then needs to be assigned */ - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); if (IS_TRUE_SYMOP(op)) return NULL ; @@ -6305,10 +7167,11 @@ static iCode *ifxForOp ( operand *op, iCode *ic ) /*-----------------------------------------------------------------*/ static void genAndOp (iCode *ic) { - operand *left,*right, *result; + operand *left,*right, *result; /* symbol *tlbl; */ - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + FENTRY; + /* note here that && operations that are in an if statement are taken away by backPatchLabels only those used in arthmetic operations remain */ @@ -6353,13 +7216,14 @@ static void genAndOp (iCode *ic) static void genOrOp (iCode *ic) { - operand *left,*right, *result; - symbol *tlbl; + operand *left,*right, *result; + symbol *tlbl; + + FENTRY; - /* note here that || operations that are in an + /* note here that || operations that are in an if statement are taken away by backPatchLabels only those used in arthmetic operations remain */ - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); pic16_aopOp((left=IC_LEFT(ic)),ic,FALSE); pic16_aopOp((right=IC_RIGHT(ic)),ic,FALSE); pic16_aopOp((result=IC_RESULT(ic)),ic,FALSE); @@ -6420,10 +7284,10 @@ static int isLiteralBit(unsigned long lit) /*-----------------------------------------------------------------*/ static void continueIfTrue (iCode *ic) { - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - if(IC_TRUE(ic)) - pic16_emitcode("ljmp","%05d_DS_",IC_TRUE(ic)->key+100); - ic->generated = 1; + FENTRY; + if(IC_TRUE(ic)) + pic16_emitcode("ljmp","%05d_DS_",IC_TRUE(ic)->key+100); + ic->generated = 1; } /*-----------------------------------------------------------------*/ @@ -6431,10 +7295,10 @@ static void continueIfTrue (iCode *ic) /*-----------------------------------------------------------------*/ static void jumpIfTrue (iCode *ic) { - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - if(!IC_TRUE(ic)) - pic16_emitcode("ljmp","%05d_DS_",IC_FALSE(ic)->key+100); - ic->generated = 1; + FENTRY; + if(!IC_TRUE(ic)) + pic16_emitcode("ljmp","%05d_DS_",IC_FALSE(ic)->key+100); + ic->generated = 1; } /*-----------------------------------------------------------------*/ @@ -6442,20 +7306,19 @@ static void jumpIfTrue (iCode *ic) /*-----------------------------------------------------------------*/ static void jmpTrueOrFalse (iCode *ic, symbol *tlbl) { - // ugly but optimized by peephole - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - if(IC_TRUE(ic)){ - symbol *nlbl = newiTempLabel(NULL); - pic16_emitcode("sjmp","%05d_DS_",nlbl->key+100); - pic16_emitcode("","%05d_DS_:",tlbl->key+100); - pic16_emitcode("ljmp","%05d_DS_",IC_TRUE(ic)->key+100); - pic16_emitcode("","%05d_DS_:",nlbl->key+100); - } - else{ - pic16_emitcode("ljmp","%05d_DS_",IC_FALSE(ic)->key+100); - pic16_emitcode("","%05d_DS_:",tlbl->key+100); - } - ic->generated = 1; + // ugly but optimized by peephole + FENTRY; + if(IC_TRUE(ic)){ + symbol *nlbl = newiTempLabel(NULL); + pic16_emitcode("sjmp","%05d_DS_",nlbl->key+100); + pic16_emitcode("","%05d_DS_:",tlbl->key+100); + pic16_emitcode("ljmp","%05d_DS_",IC_TRUE(ic)->key+100); + pic16_emitcode("","%05d_DS_:",nlbl->key+100); + } else { + pic16_emitcode("ljmp","%05d_DS_",IC_FALSE(ic)->key+100); + pic16_emitcode("","%05d_DS_:",tlbl->key+100); + } + ic->generated = 1; } /*-----------------------------------------------------------------*/ @@ -6469,8 +7332,8 @@ static void genAnd (iCode *ic, iCode *ifx) int bytelit = 0; resolvedIfx rIfx; - - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + FENTRY; + pic16_aopOp((left = IC_LEFT(ic)),ic,FALSE); pic16_aopOp((right= IC_RIGHT(ic)),ic,FALSE); pic16_aopOp((result=IC_RESULT(ic)),ic,TRUE); @@ -6618,6 +7481,7 @@ static void genAnd (iCode *ic, iCode *ifx) (posbit-1),0, PO_GPR_REGISTER)); pic16_emitpcode(POC_BRA, pic16_popGetLabel(tlbl->key)); +// pic16_emitpcode(POC_BRA, pic16_popGetLabel(rIfx.lbl->key)); } else { if (bytelit == 0xff) { /* Aaron had a MOVF instruction here, changed to MOVFW cause @@ -6657,9 +7521,9 @@ static void genAnd (iCode *ic, iCode *ifx) else { if(ifx) { pic16_emitpcode(POC_BRA, pic16_popGetLabel(rIfx.lbl->key)); - pic16_emitpLabel(tlbl->key); ifx->generated = 1; } + pic16_emitpLabel(tlbl->key); goto release; } } @@ -6685,7 +7549,7 @@ static void genAnd (iCode *ic, iCode *ifx) default: { - int p = my_powof2( (~lit) & 0xff ); + int p = pic16_my_powof2( (~lit) & 0xff ); if(p>=0) { /* only one bit is set in the literal, so use a bcf instruction */ // pic16_emitcode("bcf","%s,%d",pic16_aopGet(AOP(left),offset,FALSE,TRUE),p); @@ -6989,7 +7853,7 @@ static void genOr (iCode *ic, iCode *ifx) /* or'ing with 0 has no effect */ continue; else { - int p = my_powof2(lit & 0xff); + int p = pic16_my_powof2(lit & 0xff); if(p>=0) { /* only one bit is set in the literal, so use a bsf instruction */ pic16_emitpcode(POC_BSF, @@ -7599,12 +8463,10 @@ static void AccRsh (int shCount, int andmask) return; break; case 1 : pic16_emitpcode(POC_RRNCFW,pic16_popCopyReg(&pic16_pc_wreg)); -// andmask = 0; /* no need */ break; case 2 : pic16_emitpcode(POC_RRNCFW,pic16_popCopyReg(&pic16_pc_wreg)); pic16_emitpcode(POC_RRNCFW,pic16_popCopyReg(&pic16_pc_wreg)); -// andmask = 0; /* no need */ break; case 3 : pic16_emitpcode(POC_SWAPFW,pic16_popCopyReg(&pic16_pc_wreg)); @@ -7663,6 +8525,7 @@ static void AccSRsh (int shCount) } } #endif + /*-----------------------------------------------------------------*/ /* shiftR1Left2Result - shift right one byte from left to result */ /*-----------------------------------------------------------------*/ @@ -7709,8 +8572,8 @@ static void shiftR1Left2ResultSigned (operand *left, int offl, pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offr)); } - pic16_emitpcode(POC_RLCFW, pic16_popGet(AOP(result),offr)); - pic16_emitpcode(POC_RLCFW, pic16_popGet(AOP(result),offr)); + pic16_emitpcode(POC_RLNCFW, pic16_popGet(AOP(result),offr)); + //pic16_emitpcode(POC_RLCFW, pic16_popGet(AOP(result),offr)); pic16_emitpcode(POC_ANDLW, pic16_popGetLit(0x1f)); pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(result),offr,FALSE,FALSE),3,0, PO_GPR_REGISTER)); @@ -7825,8 +8688,8 @@ static void shiftR1Left2Result (operand *left, int offl, pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offr)); } - pic16_emitpcode(POC_RLCFW, pic16_popGet(AOP(result),offr)); - pic16_emitpcode(POC_RLCFW, pic16_popGet(AOP(result),offr)); + pic16_emitpcode(POC_RLNCFW, pic16_popGet(AOP(result),offr)); + //pic16_emitpcode(POC_RLCFW, pic16_popGet(AOP(result),offr)); pic16_emitpcode(POC_ANDLW, pic16_popGetLit(0x1f)); pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offr)); break; @@ -7839,19 +8702,19 @@ static void shiftR1Left2Result (operand *left, int offl, case 5: pic16_emitpcode(POC_SWAPFW, pic16_popGet(AOP(left),offl)); - pic16_emitpcode(POC_ANDLW, pic16_popGetLit(0x0f)); + pic16_emitpcode(POC_ANDLW, pic16_popGetLit(0x0e)); pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offr)); - emitCLRC; - pic16_emitpcode(POC_RRCF, pic16_popGet(AOP(result),offr)); + //emitCLRC; + pic16_emitpcode(POC_RRNCF, pic16_popGet(AOP(result),offr)); break; case 6: - pic16_emitpcode(POC_RLCFW, pic16_popGet(AOP(left),offl)); - pic16_emitpcode(POC_ANDLW, pic16_popGetLit(0x80)); + pic16_emitpcode(POC_RLNCFW, pic16_popGet(AOP(left),offl)); + pic16_emitpcode(POC_ANDLW, pic16_popGetLit(0x81)); pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offr)); - pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr)); - pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr)); + //pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offr)); + pic16_emitpcode(POC_RLNCF, pic16_popGet(AOP(result),offr)); break; case 7: @@ -8065,7 +8928,7 @@ static void shiftL2Left2Result (operand *left, int offl, pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offr+MSB16)); pic16_emitpcode(POC_SWAPFW,pic16_popGet(AOP(left),offl)); pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offr)); - pic16_emitpcode(POC_ANDLW, pic16_popGetLit(0xF0)); + pic16_emitpcode(POC_ANDLW, pic16_popGetLit(0x0F)); pic16_emitpcode(POC_XORWF, pic16_popGet(AOP(result),offr)); pic16_emitpcode(POC_ADDWF, pic16_popGet(AOP(result),offr+MSB16)); @@ -8202,7 +9065,7 @@ static void shiftR2Left2Result (operand *left, int offl, pic16_emitpcode(POC_ANDLW,pic16_popGetLit(0x03)); if(sign) { pic16_emitpcode(POC_BTFSC, - pic16_newpCodeOpBit(pic16_aopGet(AOP(result),offr+MSB16,FALSE,FALSE),1,0, PO_GPR_REGISTER)); + pic16_newpCodeOpBit(pic16_aopGet(AOP(result),offr,FALSE,FALSE),0,0, PO_GPR_REGISTER)); pic16_emitpcode(POC_IORLW,pic16_popGetLit(0xfc)); } pic16_emitpcode(POC_XORFW,pic16_popGet(AOP(result),offr+MSB16)); @@ -8338,7 +9201,7 @@ static void shiftLLong (operand *left, operand *result, int offr ) pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),LSB+offr)); } - if (size >= LSB+offr ){ + if (size > LSB+offr ){ if (same) { pic16_emitpcode(POC_ADDWF,pic16_popGet(AOP(left),LSB+offr)); } else { @@ -8347,7 +9210,7 @@ static void shiftLLong (operand *left, operand *result, int offr ) } } - if(size >= MSB16+offr){ + if(size > MSB16+offr){ if (same) { pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(left),MSB16+offr)); } else { @@ -8356,7 +9219,7 @@ static void shiftLLong (operand *left, operand *result, int offr ) } } - if(size >= MSB24+offr){ + if(size > MSB24+offr){ if (same) { pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(left),MSB24+offr)); } else { @@ -8464,88 +9327,275 @@ static void genlshFour (operand *result, operand *left, int shCount) } } -/*-----------------------------------------------------------------*/ -/* genLeftShiftLiteral - left shifting by known count */ -/*-----------------------------------------------------------------*/ -static void genLeftShiftLiteral (operand *left, - operand *right, - operand *result, - iCode *ic) -{ - int shCount = (int) floatFromVal (AOP(right)->aopu.aop_lit); - int size; +/*-----------------------------------------------------------------*/ +/* genLeftShiftLiteral - left shifting by known count */ +/*-----------------------------------------------------------------*/ +void pic16_genLeftShiftLiteral (operand *left, + operand *right, + operand *result, + iCode *ic) +{ + int shCount = (int) floatFromVal (AOP(right)->aopu.aop_lit); + int size; + + FENTRY; + DEBUGpic16_emitcode ("; ***","shCount:%d", shCount); + pic16_freeAsmop(right,NULL,ic,TRUE); + + pic16_aopOp(left,ic,FALSE); + pic16_aopOp(result,ic,FALSE); + + size = getSize(operandType(result)); + +#if VIEW_SIZE + pic16_emitcode("; shift left ","result %d, left %d",size, + AOP_SIZE(left)); +#endif + + /* I suppose that the left size >= result size */ + if(shCount == 0){ + while(size--){ + movLeft2Result(left, size, result, size); + } + } + + else if(shCount >= (size * 8)) + while(size--) + pic16_aopPut(AOP(result),zero,size); + else{ + switch (size) { + case 1: + genlshOne (result,left,shCount); + break; + + case 2: + case 3: + genlshTwo (result,left,shCount); + break; + + case 4: + genlshFour (result,left,shCount); + break; + } + } + pic16_freeAsmop(left,NULL,ic,TRUE); + pic16_freeAsmop(result,NULL,ic,TRUE); +} + +/*-----------------------------------------------------------------* + * genMultiAsm - repeat assembly instruction for size of register. + * if endian == 1, then the high byte (i.e base address + size of + * register) is used first else the low byte is used first; + *-----------------------------------------------------------------*/ +static void genMultiAsm( PIC_OPCODE poc, operand *reg, int size, int endian) +{ + + int offset = 0; + + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + + if(!reg) + return; + + if(!endian) { + endian = 1; + } else { + endian = -1; + offset = size-1; + } + + while(size--) { + pic16_emitpcode(poc, pic16_popGet(AOP(reg),offset)); + offset += endian; + } + +} +/*-----------------------------------------------------------------*/ +/* genLeftShift - generates code for left shifting */ +/*-----------------------------------------------------------------*/ +static void genLeftShift (iCode *ic) +{ + operand *left,*right, *result; + int size, offset; +// char *l; + symbol *tlbl , *tlbl1; + pCodeOp *pctemp; + + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + + right = IC_RIGHT(ic); + left = IC_LEFT(ic); + result = IC_RESULT(ic); + + pic16_aopOp(right,ic,FALSE); + + /* if the shift count is known then do it + as efficiently as possible */ + if (AOP_TYPE(right) == AOP_LIT) { + pic16_genLeftShiftLiteral (left,right,result,ic); + return ; + } + + /* shift count is unknown then we have to form + * a loop. Get the loop count in WREG : Note: we take + * only the lower order byte since shifting + * more than 32 bits make no sense anyway, ( the + * largest size of an object can be only 32 bits ) */ + + pic16_aopOp(left,ic,FALSE); + pic16_aopOp(result,ic,FALSE); + + /* now move the left to the result if they are not the + * same, and if size > 1, + * and if right is not same to result (!!!) -- VR */ + if (!pic16_sameRegs(AOP(left),AOP(result)) + && (AOP_SIZE(result) > 1)) { + + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + + size = AOP_SIZE(result); + offset=0; + while (size--) { + +#if 0 + l = pic16_aopGet(AOP(left),offset,FALSE,TRUE); + if (*l == '@' && (IS_AOP_PREG(result))) { + + pic16_emitcode("mov","a,%s",l); + pic16_aopPut(AOP(result),"a",offset); + } else +#endif + { + /* we don't know if left is a literal or a register, take care -- VR */ + mov2f(AOP(result), AOP(left), offset); + } + offset++; + } + } + + size = AOP_SIZE(result); + + /* if it is only one byte then */ + if (size == 1) { + if(optimized_for_speed) { + pic16_emitpcode(POC_SWAPFW, pic16_popGet(AOP(left),0)); + pic16_emitpcode(POC_ANDLW, pic16_popGetLit(0xf0)); + pic16_emitpcode(POC_BTFSS, pic16_newpCodeOpBit(pic16_aopGet(AOP(right),0,FALSE,FALSE),2,0, PO_GPR_REGISTER)); + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(left),0)); + pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),0)); + pic16_emitpcode(POC_BTFSS, pic16_newpCodeOpBit(pic16_aopGet(AOP(right),0,FALSE,FALSE),0,0, PO_GPR_REGISTER)); + pic16_emitpcode(POC_ADDWF, pic16_popGet(AOP(result),0)); + pic16_emitpcode(POC_RLCFW, pic16_popGet(AOP(result),0)); + pic16_emitpcode(POC_ANDLW, pic16_popGetLit(0xfe)); + pic16_emitpcode(POC_ADDFW, pic16_popGet(AOP(result),0)); + pic16_emitpcode(POC_BTFSC, pic16_newpCodeOpBit(pic16_aopGet(AOP(right),0,FALSE,FALSE),1,0, PO_GPR_REGISTER)); + pic16_emitpcode(POC_ADDWF, pic16_popGet(AOP(result),0)); + } else { + + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + + tlbl = newiTempLabel(NULL); + +#if 1 + /* this is already done, why change it? */ + if (!pic16_sameRegs(AOP(left),AOP(result))) { + mov2f(AOP(result), AOP(left), 0); + } +#endif + + pic16_emitpcode(POC_COMFW, pic16_popGet(AOP(right),0)); + pic16_emitpcode(POC_RRCF, pic16_popGet(AOP(result),0)); + pic16_emitpLabel(tlbl->key); + pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),0)); + pic16_emitpcode(POC_ADDLW, pic16_popGetLit(1)); + emitSKPC; + pic16_emitpcode(POC_GOTO,pic16_popGetLabel(tlbl->key)); + } + goto release ; + } + + if (pic16_sameRegs(AOP(left),AOP(result))) { + + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + + tlbl = newiTempLabel(NULL); + pic16_emitpcode(POC_COMFW, pic16_popGet(AOP(right),0)); + genMultiAsm(POC_RRCF, result, size,1); + pic16_emitpLabel(tlbl->key); + genMultiAsm(POC_RLCF, result, size,0); + pic16_emitpcode(POC_ADDLW, pic16_popGetLit(1)); + emitSKPC; + pic16_emitpcode(POC_GOTO,pic16_popGetLabel(tlbl->key)); + goto release; + } + + //tlbl = newiTempLabel(NULL); + //offset = 0 ; + //tlbl1 = newiTempLabel(NULL); - DEBUGpic16_emitcode ("; ***","%s %d shCount:%d",__FUNCTION__,__LINE__,shCount); - pic16_freeAsmop(right,NULL,ic,TRUE); + //reAdjustPreg(AOP(result)); + + //pic16_emitcode("sjmp","%05d_DS_",tlbl1->key+100); + //pic16_emitcode("","%05d_DS_:",tlbl->key+100); + //l = pic16_aopGet(AOP(result),offset,FALSE,FALSE); + //MOVA(l); + //pic16_emitcode("add","a,acc"); + //pic16_aopPut(AOP(result),"a",offset++); + //while (--size) { + // l = pic16_aopGet(AOP(result),offset,FALSE,FALSE); + // MOVA(l); + // pic16_emitcode("rlc","a"); + // pic16_aopPut(AOP(result),"a",offset++); + //} + //reAdjustPreg(AOP(result)); - pic16_aopOp(left,ic,FALSE); - pic16_aopOp(result,ic,FALSE); + //pic16_emitcode("","%05d_DS_:",tlbl1->key+100); + //pic16_emitcode("djnz","b,%05d_DS_",tlbl->key+100); - size = getSize(operandType(result)); -#if VIEW_SIZE - pic16_emitcode("; shift left ","result %d, left %d",size, - AOP_SIZE(left)); -#endif + tlbl = newiTempLabel(NULL); + tlbl1= newiTempLabel(NULL); - /* I suppose that the left size >= result size */ - if(shCount == 0){ - while(size--){ - movLeft2Result(left, size, result, size); - } - } + size = AOP_SIZE(result); + offset = 1; - else if(shCount >= (size * 8)) - while(size--) - pic16_aopPut(AOP(result),zero,size); - else{ - switch (size) { - case 1: - genlshOne (result,left,shCount); - break; + pctemp = pic16_popGetTempReg(1); /* grab a temporary working register. */ - case 2: - case 3: - genlshTwo (result,left,shCount); - break; + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),0)); - case 4: - genlshFour (result,left,shCount); - break; - } - } - pic16_freeAsmop(left,NULL,ic,TRUE); - pic16_freeAsmop(result,NULL,ic,TRUE); -} + /* offset should be 0, 1 or 3 */ + + pic16_emitpcode(POC_ANDLW, pic16_popGetLit((size<<3)-1)); + emitSKPNZ; + pic16_emitpcode(POC_GOTO, pic16_popGetLabel(tlbl1->key)); -/*-----------------------------------------------------------------* - * genMultiAsm - repeat assembly instruction for size of register. - * if endian == 1, then the high byte (i.e base address + size of - * register) is used first else the low byte is used first; - *-----------------------------------------------------------------*/ -static void genMultiAsm( PIC_OPCODE poc, operand *reg, int size, int endian) -{ + pic16_emitpcode(POC_MOVWF, pctemp); - int offset = 0; - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + pic16_emitpLabel(tlbl->key); - if(!reg) - return; + emitCLRC; + pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),0)); + while(--size) + pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),offset++)); - if(!endian) { - endian = 1; - } else { - endian = -1; - offset = size-1; - } + pic16_emitpcode(POC_DECFSZ, pctemp); + pic16_emitpcode(POC_GOTO,pic16_popGetLabel(tlbl->key)); + pic16_emitpLabel(tlbl1->key); - while(size--) { - pic16_emitpcode(poc, pic16_popGet(AOP(reg),offset)); - offset += endian; - } + pic16_popReleaseTempReg(pctemp,1); + + release: + pic16_freeAsmop (right,NULL,ic,TRUE); + pic16_freeAsmop(left,NULL,ic,TRUE); + pic16_freeAsmop(result,NULL,ic,TRUE); } + + + +#if 0 +#error old code (left here for reference) /*-----------------------------------------------------------------*/ /* genLeftShift - generates code for left shifting */ /*-----------------------------------------------------------------*/ @@ -8568,7 +9618,7 @@ static void genLeftShift (iCode *ic) /* if the shift count is known then do it as efficiently as possible */ if (AOP_TYPE(right) == AOP_LIT) { - genLeftShiftLiteral (left,right,result,ic); + pic16_genLeftShiftLiteral (left,right,result,ic); return ; } @@ -8587,6 +9637,8 @@ static void genLeftShift (iCode *ic) if (!pic16_sameRegs(AOP(left),AOP(result)) && AOP_SIZE(result) > 1) { + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + size = AOP_SIZE(result); offset=0; while (size--) { @@ -8596,9 +9648,9 @@ static void genLeftShift (iCode *ic) pic16_emitcode("mov","a,%s",l); pic16_aopPut(AOP(result),"a",offset); } else { - pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(left),offset)); - pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offset)); - //pic16_aopPut(AOP(result),l,offset); + + /* we don't know if left is a literal or a register, take care -- VR */ + mov2f(AOP(result), AOP(left), offset); } offset++; } @@ -8623,10 +9675,14 @@ static void genLeftShift (iCode *ic) pic16_emitpcode(POC_ADDWF, pic16_popGet(AOP(result),0)); } else { + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + tlbl = newiTempLabel(NULL); if (!pic16_sameRegs(AOP(left),AOP(result))) { - pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(left),0)); - pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),0)); + mov2f(AOP(result), AOP(left), 0); + +// pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(left),0)); +// pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),0)); } pic16_emitpcode(POC_COMFW, pic16_popGet(AOP(right),0)); @@ -8642,6 +9698,8 @@ static void genLeftShift (iCode *ic) if (pic16_sameRegs(AOP(left),AOP(result))) { + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + tlbl = newiTempLabel(NULL); pic16_emitpcode(POC_COMFW, pic16_popGet(AOP(right),0)); genMultiAsm(POC_RRCF, result, size,1); @@ -8683,7 +9741,7 @@ static void genLeftShift (iCode *ic) size = AOP_SIZE(result); offset = 1; - pctemp = pic16_popGetTempReg(); /* grab a temporary working register. */ + pctemp = pic16_popGetTempReg(1); /* grab a temporary working register. */ pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),0)); @@ -8707,7 +9765,7 @@ static void genLeftShift (iCode *ic) pic16_emitpcode(POC_GOTO,pic16_popGetLabel(tlbl->key)); pic16_emitpLabel(tlbl1->key); - pic16_popReleaseTempReg(pctemp); + pic16_popReleaseTempReg(pctemp,1); release: @@ -8715,6 +9773,7 @@ static void genLeftShift (iCode *ic) pic16_freeAsmop(left,NULL,ic,TRUE); pic16_freeAsmop(result,NULL,ic,TRUE); } +#endif /*-----------------------------------------------------------------*/ /* genrshOne - right shift a one byte quantity by known count */ @@ -9029,7 +10088,7 @@ static void genSignedRightShift (iCode *ic) size = AOP_SIZE(result); offset = size - 1; - pctemp = pic16_popGetTempReg(); /* grab a temporary working register. */ + pctemp = pic16_popGetTempReg(1); /* grab a temporary working register. */ pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),0)); @@ -9054,7 +10113,7 @@ static void genSignedRightShift (iCode *ic) pic16_emitpcode(POC_GOTO,pic16_popGetLabel(tlbl->key)); pic16_emitpLabel(tlbl1->key); - pic16_popReleaseTempReg(pctemp); + pic16_popReleaseTempReg(pctemp,1); #if 0 size = AOP_SIZE(result); offset = size - 1; @@ -9225,13 +10284,47 @@ void pic16_loadFSR0(operand *op) /*-----------------------------------------------------------------*/ static void genUnpackBits (operand *result, operand *left, char *rname, int ptype) { - int shCnt ; - int rlen = 0 ; - sym_link *etype; - int offset = 0 ; + int shCnt ; + int rlen = 0 ; + sym_link *etype, *letype; + int blen=0, bstr=0; + int lbstr; + int offset = 0 ; - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - etype = getSpec(operandType(result)); + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + etype = getSpec(operandType(result)); + letype = getSpec(operandType(left)); + +// if(IS_BITFIELD(etype)) { + blen = SPEC_BLEN(etype); + bstr = SPEC_BSTR(etype); +// } + + lbstr = SPEC_BSTR( letype ); + +#if 1 + if((blen == 1) && (bstr < 8)) { + /* it is a single bit, so use the appropriate bit instructions */ + DEBUGpic16_emitcode (";","%s %d optimize bit read",__FUNCTION__,__LINE__); + + pic16_emitpcode(POC_CLRF, pic16_popCopyReg(&pic16_pc_wreg)); + + if((ptype == POINTER) && (result)) { + /* workaround to reduce the extra lfsr instruction */ + pic16_emitpcode(POC_BTFSC, + pic16_popCopyGPR2Bit(pic16_popGet(AOP(left), 0), bstr)); + } else { + pic16_emitpcode(POC_BTFSC, + pic16_popCopyGPR2Bit(pic16_popCopyReg(&pic16_pc_indf0), bstr)); + } + + pic16_emitpcode(POC_INCF, pic16_popCopyReg(&pic16_pc_wreg)); + + pic16_emitpcode(POC_MOVWF, pic16_popGet( AOP(result), 0 )); + return; + } + +#endif /* the following call to pic16_loadFSR0 is temporary until * optimization to handle single bit assignments is added @@ -9422,92 +10515,92 @@ static void genNearPointerGet (operand *left, operand *result, iCode *ic) { - asmop *aop = NULL; - //regs *preg = NULL ; - sym_link *rtype, *retype; - sym_link *ltype = operandType(left); + asmop *aop = NULL; + //regs *preg = NULL ; + sym_link *rtype, *retype; + sym_link *ltype = operandType(left); - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - rtype = operandType(result); - retype= getSpec(rtype); + FENTRY; - pic16_aopOp(left,ic,FALSE); + rtype = operandType(result); + retype= getSpec(rtype); + + pic16_aopOp(left,ic,FALSE); -// pic16_DumpOp("(left)",left); -// pic16_DumpOp("(result)",result); +// pic16_DumpOp("(left)",left); +// pic16_DumpOp("(result)",result); - /* if left is rematerialisable and - * result is not bit variable type and - * the left is pointer to data space i.e - * lower 128 bytes of space */ - if (AOP_TYPE(left) == AOP_PCODE - && !IS_BITFIELD(retype) - && DCL_TYPE(ltype) == POINTER) { + /* if left is rematerialisable and + * result is not bit variable type and + * the left is pointer to data space i.e + * lower 128 bytes of space */ + + if (AOP_TYPE(left) == AOP_PCODE + && !IS_BITFIELD(retype) + && DCL_TYPE(ltype) == POINTER) { - genDataPointerGet (left,result,ic); - pic16_freeAsmop(left, NULL, ic, TRUE); - return ; - } + genDataPointerGet (left,result,ic); + pic16_freeAsmop(left, NULL, ic, TRUE); + return ; + } - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - /* if the value is already in a pointer register - * then don't need anything more */ - if (!AOP_INPREG(AOP(left))) { - /* otherwise get a free pointer register */ - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + /* if the value is already in a pointer register + * then don't need anything more */ + if (!AOP_INPREG(AOP(left))) { + /* otherwise get a free pointer register */ + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - /* VR -- the whole concept is to load FSR0 with the address of the symbol */ - if( (AOP_TYPE(left) == AOP_PCODE) - && ((AOP(left)->aopu.pcop->type == PO_IMMEDIATE) - || (AOP(left)->aopu.pcop->type == PO_DIR))) // patch 10 - { - if(!IS_BITFIELD(retype)) - pic16_loadFSR0( left ); // patch 10 - } else { - // set up FSR0 with address from left - pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(left),0), pic16_popCopyReg(&pic16_pc_fsr0l))); // patch 10 - pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(left),1), pic16_popCopyReg(&pic16_pc_fsr0h))); // patch 10 - } - } -// else -// rname = pic16_aopGet(AOP(left),0,FALSE,FALSE); + /* VR -- the whole concept is to load FSR0 with the address of the symbol */ + /* bitfields will be handled by genUnpackBits */ + if(!IS_BITFIELD(retype)) { + + if(is_LitAOp( AOP(left) )) { + pic16_loadFSR0( left ); + } else { + // set up FSR0 with address from left + pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(left),0), pic16_popCopyReg(&pic16_pc_fsr0l))); // patch 10 + pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(left),1), pic16_popCopyReg(&pic16_pc_fsr0h))); // patch 10 + } + } + } +// else +// rname = pic16_aopGet(AOP(left),0,FALSE,FALSE); - pic16_aopOp (result,ic,FALSE); + pic16_aopOp (result,ic,FALSE); - /* if bitfield then unpack the bits */ + DEBUGpic16_pic16_AopType(__LINE__, left, NULL, result); + + /* if bitfield then unpack the bits */ if (IS_BITFIELD(retype)) - genUnpackBits (result, left, NULL, POINTER); + genUnpackBits (result, left, NULL, POINTER); else { - /* we have can just get the values */ + /* we have can just get the values */ int size = AOP_SIZE(result); int offset = 0; DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - - /* fsr0 is loaded already -- VR */ -// pic16_loadFSR0( left ); + /* fsr0 is loaded already -- VR */ +// pic16_loadFSR0( left ); // pic16_emitpcode(POC_MOVFW,pic16_popGet(AOP(left),0)); // pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_fsr0)); while(size--) { - - if(size) { - pic16_emitpcode(POC_MOVFF, - pic16_popGet2p(pic16_popCopyReg(&pic16_pc_indf0), - pic16_popGet(AOP(result), offset++))); - } else { - pic16_emitpcode(POC_MOVFF, - pic16_popGet2p(pic16_popCopyReg(&pic16_pc_postinc0), - pic16_popGet(AOP(result), offset++))); - } + if(size) { + pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popCopyReg(&pic16_pc_postinc0), + pic16_popGet(AOP(result), offset++))); + } else { + pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popCopyReg(&pic16_pc_indf0), + pic16_popGet(AOP(result), offset++))); + } } #if 0 -// pic16_emitpcode(POC_MOVFW,pic16_popCopyReg(&pic16_pc_postinc0)); -// pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(result),offset++)); - if(size) - pic16_emitpcode(POC_INCF,pic16_popCopyReg(&pic16_pc_fsr0)); +// pic16_emitpcode(POC_MOVFW,pic16_popCopyReg(&pic16_pc_postinc0)); +// pic16_emitpcode(POC_MOVWF,pic16_popGet(AOP(result),offset++)); + if(size) + pic16_emitpcode(POC_INCF,pic16_popCopyReg(&pic16_pc_fsr0)); #endif /* while (size--) { @@ -9528,30 +10621,29 @@ static void genNearPointerGet (operand *left, /* now some housekeeping stuff */ if (aop) { - /* we had to allocate for this iCode */ - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - pic16_freeAsmop(NULL,aop,ic,TRUE); + /* we had to allocate for this iCode */ + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + pic16_freeAsmop(NULL,aop,ic,TRUE); } else { - /* we did not allocate which means left - already in a pointer register, then - if size > 0 && this could be used again - we have to point it back to where it - belongs */ - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - if (AOP_SIZE(result) > 1 && - !OP_SYMBOL(left)->remat && - ( OP_SYMBOL(left)->liveTo > ic->seq || - ic->depth )) { -// int size = AOP_SIZE(result) - 1; -// while (size--) -// pic16_emitcode("dec","%s",rname); + /* we did not allocate which means left + * already in a pointer register, then + * if size > 0 && this could be used again + * we have to point it back to where it + * belongs */ + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + if (AOP_SIZE(result) > 1 + && !OP_SYMBOL(left)->remat + && ( OP_SYMBOL(left)->liveTo > ic->seq + || ic->depth )) { +// int size = AOP_SIZE(result) - 1; +// while (size--) +// pic16_emitcode("dec","%s",rname); } } /* done */ pic16_freeAsmop(left,NULL,ic,TRUE); pic16_freeAsmop(result,NULL,ic,TRUE); - } /*-----------------------------------------------------------------*/ @@ -9736,6 +10828,7 @@ static void genCodePointerGet (operand *left, pic16_freeAsmop(result,NULL,ic,TRUE); } #endif +#if 0 /*-----------------------------------------------------------------*/ /* genGenPointerGet - gget value from generic pointer space */ /*-----------------------------------------------------------------*/ @@ -9791,13 +10884,95 @@ static void genGenPointerGet (operand *left, } /* if bit then unpack */ - if (IS_BITFIELD(retype)) - genUnpackBits(result,left,"BAD",GPOINTER); - - release: - pic16_freeAsmop(left,NULL,ic,TRUE); - pic16_freeAsmop(result,NULL,ic,TRUE); + if (IS_BITFIELD(retype)) + genUnpackBits(result,left,"BAD",GPOINTER); + + release: + pic16_freeAsmop(left,NULL,ic,TRUE); + pic16_freeAsmop(result,NULL,ic,TRUE); + +} +#endif + + +/*-----------------------------------------------------------------*/ +/* genGenPointerGet - gget value from generic pointer space */ +/*-----------------------------------------------------------------*/ +static void genGenPointerGet (operand *left, + operand *result, iCode *ic) +{ + int size, offset, lit; + sym_link *retype = getSpec(operandType(result)); + char fgptrget[32]; + + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + pic16_aopOp(left,ic,FALSE); + pic16_aopOp(result,ic,FALSE); + size = AOP_SIZE(result); + + DEBUGpic16_pic16_AopType(__LINE__,left,NULL,result); + + if (AOP_TYPE(left) == AOP_IMMD) { // do we ever get here? (untested!) + + lit = (unsigned)floatFromVal(AOP(left)->aopu.aop_lit); + // load FSR0 from immediate + pic16_emitpcode(POC_LFSR,pic16_popGetLit2(0,pic16_popGetLit(lit))); + + werror(W_POSSBUG2, __FILE__, __LINE__); + + offset = 0; + while(size--) { + if(size) { + pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popCopyReg(&pic16_pc_postinc0), pic16_popGet(AOP(result),offset))); + } else { + pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popCopyReg(&pic16_pc_indf0), pic16_popGet(AOP(result),offset))); + } + offset++; + } + + goto release; + + } else { /* we need to get it byte by byte */ + + /* set up WREG:PRODL:FSR0L with address from left */ + pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(left),0), pic16_popCopyReg(&pic16_pc_fsr0l))); + pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(left),1), pic16_popCopyReg(&pic16_pc_prodl))); + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(left), 2)); + + switch( size ) { + case 1: strcpy(fgptrget, "__gptrget1"); break; + case 2: strcpy(fgptrget, "__gptrget2"); break; + case 3: strcpy(fgptrget, "__gptrget3"); break; + case 4: strcpy(fgptrget, "__gptrget4"); break; + default: + werror(W_POSSBUG2, __FILE__, __LINE__); + abort(); + } + + pic16_emitpcode(POC_CALL, pic16_popGetWithString( fgptrget )); + + assignResultValue(result, 1); + + { + symbol *sym; + + sym = newSymbol( fgptrget, 0 ); + strcpy(sym->rname, fgptrget); + checkAddSym(&externs, sym); + +// fprintf(stderr, "%s:%d adding extern symbol %s in externs\n", __FILE__, __LINE__, fgptrget); + } + + goto release; + } + + /* if bit then unpack */ + if (IS_BITFIELD(retype)) + genUnpackBits(result,left,"BAD",GPOINTER); +release: + pic16_freeAsmop(left,NULL,ic,TRUE); + pic16_freeAsmop(result,NULL,ic,TRUE); } /*-----------------------------------------------------------------*/ @@ -9827,7 +11002,7 @@ static void genConstPointerGet (operand *left, pic16_emitpLabel(albl->key); poc = ( (AOP_TYPE(left) == AOP_PCODE) ? POC_MOVLW : POC_MOVFW); - + /* this performs a goto to the specified address -- Why not to use pointer? -- VR */ pic16_emitpcode(poc,pic16_popGet(AOP(left),1)); pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_pclath)); @@ -9882,8 +11057,8 @@ static void genPointerGet (iCode *ic) sym_link *type, *etype; int p_type; - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - + FENTRY; + left = IC_LEFT(ic); result = IC_RESULT(ic) ; @@ -9979,12 +11154,15 @@ static void genPackBits (sym_link *etype , operand *result, int offset = 0 ; int rLen = 0 ; int blen, bstr ; + sym_link *retype; char *l ; DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); blen = SPEC_BLEN(etype); bstr = SPEC_BSTR(etype); + retype = getSpec(operandType(right)); + if(AOP_TYPE(right) == AOP_LIT) { if((blen == 1) && (bstr < 8)) { unsigned long lit; @@ -10004,7 +11182,7 @@ static void genPackBits (sym_link *etype , operand *result, pic16_popCopyGPR2Bit(pic16_popGet(AOP(result), 0), bstr)); } } else { - + pic16_loadFSR0( result ); if(lit) { pic16_emitpcode(POC_BSF, pic16_popCopyGPR2Bit(pic16_popCopyReg(&pic16_pc_indf0), bstr)); @@ -10020,13 +11198,43 @@ static void genPackBits (sym_link *etype , operand *result, pic16_emitpcode(POC_MOVLW, pic16_popGet(AOP(right), 0)); offset++; } else - pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right), offset++)); + if(IS_BITFIELD(retype) + && (AOP_TYPE(right) == AOP_REG || AOP_TYPE(right) == AOP_DIR) + && (blen == 1)) { + int rblen, rbstr; + + rblen = SPEC_BLEN( retype ); + rbstr = SPEC_BSTR( retype ); + + + if(IS_BITFIELD(etype)) { + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(result), 0)); + pic16_emitpcode(POC_BCF, pic16_popCopyGPR2Bit(pic16_popCopyReg(&pic16_pc_wreg), bstr)); + } else { + pic16_emitpcode(POC_CLRF, pic16_popCopyReg(&pic16_pc_wreg)); + } + + pic16_emitpcode(POC_BTFSC, pic16_popCopyGPR2Bit(pic16_popGet(AOP(right), 0), rbstr)); + + if(IS_BITFIELD(etype)) { + pic16_emitpcode(POC_BSF, pic16_popCopyGPR2Bit(pic16_popCopyReg(&pic16_pc_wreg), bstr)); + } else { + pic16_emitpcode(POC_INCF, pic16_popCopyReg(&pic16_pc_wreg)); + } + + pic16_emitpcode(POC_MOVWF, pic16_popGet( AOP(result), 0)); + + return; + } else + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right), offset++)); /* if the bit lenth is less than or */ /* it exactly fits a byte then */ if((shCnt=SPEC_BSTR(etype)) || SPEC_BLEN(etype) <= 8 ) { + pic16_emitpcode(POC_ANDLW, pic16_popGetLit((1U << blen)-1)); + /* shift left acc */ AccLsh(shCnt); @@ -10036,11 +11244,16 @@ static void genPackBits (sym_link *etype , operand *result, switch (p_type) { case FPOINTER: case POINTER: - case GPOINTER: + pic16_loadFSR0( result ); pic16_emitpcode(POC_MOVFW, pic16_popCopyReg(&pic16_pc_indf0)); // pic16_emitcode ("mov","b,a"); // pic16_emitcode("mov","a,@%s",rname); break; + + case GPOINTER: + werror(W_POSSBUG2, __FILE__, __LINE__); + break; + } #if 1 pic16_emitpcode(POC_ANDLW, pic16_popGetLit( @@ -10176,19 +11389,32 @@ static void genDataPointerSet(operand *right, while (size--) { if (AOP_TYPE(right) == AOP_LIT) { - unsigned int lit = (unsigned int) floatFromVal (AOP(IC_RIGHT(ic))->aopu.aop_lit); - - lit = lit >> (8*offset); - if(lit&0xff) { - pic16_emitpcode(POC_MOVLW, pic16_popGetLit(lit&0xff)); - pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offset)); // pstch 8 - } else { - pic16_emitpcode(POC_CLRF, pic16_popGet(AOP(result),offset)); // patch 8 - } - } else { - mov2w(AOP(right), offset); - pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offset)); // patch 8 - } + unsigned int lit; + + if(!IS_FLOAT(operandType( right ))) + lit = (unsigned long)floatFromVal(AOP(IC_RIGHT(ic))->aopu.aop_lit); + else { + union { + unsigned long lit_int; + float lit_float; + } info; + + /* take care if literal is a float */ + info.lit_float = floatFromVal(AOP(IC_RIGHT(ic))->aopu.aop_lit); + lit = info.lit_int; + } + + lit = lit >> (8*offset); + if(lit&0xff) { + pic16_emitpcode(POC_MOVLW, pic16_popGetLit(lit&0xff)); + pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offset)); // pstch 8 + } else { + pic16_emitpcode(POC_CLRF, pic16_popGet(AOP(result),offset)); // patch 8 + } + } else { + mov2w(AOP(right), offset); + pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offset)); // patch 8 + } offset++; resoffset++; } @@ -10242,16 +11468,19 @@ static void genNearPointerSet (operand *right, /* otherwise get a free pointer register */ DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - if( (AOP_TYPE(result) == AOP_PCODE) - && ((AOP(result)->aopu.pcop->type == PO_IMMEDIATE) - || (AOP(result)->aopu.pcop->type == PO_DIR))) // patch 10 +// if( (AOP_TYPE(result) == AOP_PCODE) +// && ((AOP(result)->aopu.pcop->type == PO_IMMEDIATE) +// || (AOP(result)->aopu.pcop->type == PO_DIR))) // patch 10 + if(is_LitAOp( AOP(result) )) { if(!IS_BITFIELD(resetype)) pic16_loadFSR0( result ); // patch 10 } else { + if(!IS_BITFIELD(resetype)) { // set up FSR0 with address of result pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(result),0), pic16_popCopyReg(&pic16_pc_fsr0l))); // patch 10 pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(result),1), pic16_popCopyReg(&pic16_pc_fsr0h))); // patch 10 + } } } @@ -10260,6 +11489,8 @@ static void genNearPointerSet (operand *right, DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); +// pic16_loadFSR0( result ); + /* if bitfield then unpack the bits */ if (IS_BITFIELD(resetype)) { genPackBits (resetype, result, right, NULL, POINTER); @@ -10466,6 +11697,7 @@ static void genFarPointerSet (operand *right, /*-----------------------------------------------------------------*/ /* genGenPointerSet - set value from generic pointer space */ /*-----------------------------------------------------------------*/ +#if 0 static void genGenPointerSet (operand *right, operand *result, iCode *ic) { @@ -10587,17 +11819,93 @@ static void genGenPointerSet (operand *right, pic16_freeAsmop(right,NULL,ic,TRUE); pic16_freeAsmop(result,NULL,ic,TRUE); } +#endif + +static void genGenPointerSet (operand *right, + operand *result, iCode *ic) +{ + int size; + sym_link *retype = getSpec(operandType(right)); + char fgptrput[32]; + + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + + pic16_aopOp(result,ic,FALSE); + pic16_aopOp(right,ic,FALSE); + size = AOP_SIZE(right); + + DEBUGpic16_pic16_AopType(__LINE__,NULL,right,result); + + + /* if bit then unpack */ + if (IS_BITFIELD(retype)) { +// pic16_emitpcode(POC_LFSR,pic16_popGetLit2(0,pic16_popGetLit(lit))); + genPackBits(retype,result,right,"dptr",GPOINTER); + goto release; + } + + size = AOP_SIZE(right); + + DEBUGpic16_emitcode ("; ***","%s %d size=%d",__FUNCTION__,__LINE__,size); + + + + /* load value to write in TBLPTRH:TBLPTRL:PRODH:[stack] */ + + /* value of right+0 is placed on stack, which will be retrieved + * by the support function this restoring the stack. The important + * thing is that there is no need to manually restore stack pointer + * here */ + pushaop(AOP(right), 0); +// mov2fp(pic16_popCopyReg(&pic16_pc_postdec1), AOP(right), 0); + if(size>1)mov2fp(pic16_popCopyReg(&pic16_pc_prodh), AOP(right), 1); + if(size>2)mov2fp(pic16_popCopyReg(&pic16_pc_tblptrl), AOP(right), 2); + if(size>3)mov2fp(pic16_popCopyReg(&pic16_pc_tblptrh), AOP(right), 3); + + /* load address to write to in WREG:FSR0H:FSR0L */ + pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(result), 0), + pic16_popCopyReg(&pic16_pc_fsr0l))); + pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(result), 1), + pic16_popCopyReg(&pic16_pc_prodl))); + pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(result), 2)); + + + /* put code here */ + switch (size) { + case 1: strcpy(fgptrput, "__gptrput1"); break; + case 2: strcpy(fgptrput, "__gptrput2"); break; + case 3: strcpy(fgptrput, "__gptrput3"); break; + case 4: strcpy(fgptrput, "__gptrput4"); break; + default: + werror(W_POSSBUG2, __FILE__, __LINE__); + abort(); + } + + pic16_emitpcode(POC_CALL, pic16_popGetWithString( fgptrput )); + + { + symbol *sym; + + sym = newSymbol( fgptrput, 0 ); + strcpy(sym->rname, fgptrput); + checkAddSym(&externs, sym); + } + +release: + pic16_freeAsmop(right,NULL,ic,TRUE); + pic16_freeAsmop(result,NULL,ic,TRUE); +} /*-----------------------------------------------------------------*/ /* genPointerSet - stores the value into a pointer location */ /*-----------------------------------------------------------------*/ static void genPointerSet (iCode *ic) { - operand *right, *result ; - sym_link *type, *etype; - int p_type; + operand *right, *result ; + sym_link *type, *etype; + int p_type; - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + FENTRY; right = IC_RIGHT(ic); result = IC_RESULT(ic) ; @@ -10665,36 +11973,34 @@ static void genIfx (iCode *ic, iCode *popIc) operand *cond = IC_COND(ic); int isbit =0; - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + FENTRY; - pic16_aopOp(cond,ic,FALSE); + pic16_aopOp(cond,ic,FALSE); - /* get the value into acc */ - if (AOP_TYPE(cond) != AOP_CRY) - pic16_toBoolean(cond); - else - isbit = 1; - /* the result is now in the accumulator */ - pic16_freeAsmop(cond,NULL,ic,TRUE); - - /* if there was something to be popped then do it */ - if (popIc) - genIpop(popIc); - - /* if the condition is a bit variable */ - if (isbit && IS_ITEMP(cond) && - SPIL_LOC(cond)) { - genIfxJump(ic,SPIL_LOC(cond)->rname); - DEBUGpic16_emitcode ("; isbit SPIL_LOC","%s",SPIL_LOC(cond)->rname); - } - else { - if (isbit && !IS_ITEMP(cond)) - genIfxJump(ic,OP_SYMBOL(cond)->rname); + /* get the value into acc */ + if (AOP_TYPE(cond) != AOP_CRY) + pic16_toBoolean(cond); else - genIfxJump(ic,"a"); - } - ic->generated = 1; - + isbit = 1; + /* the result is now in the accumulator */ + pic16_freeAsmop(cond,NULL,ic,TRUE); + + /* if there was something to be popped then do it */ + if (popIc) + genIpop(popIc); + + /* if the condition is a bit variable */ + if (isbit && IS_ITEMP(cond) && + SPIL_LOC(cond)) { + genIfxJump(ic,SPIL_LOC(cond)->rname); + DEBUGpic16_emitcode ("; isbit SPIL_LOC","%s",SPIL_LOC(cond)->rname); + } else { + if (isbit && !IS_ITEMP(cond)) + genIfxJump(ic,OP_SYMBOL(cond)->rname); + else + genIfxJump(ic,"a"); + } + ic->generated = 1; } /*-----------------------------------------------------------------*/ @@ -10707,59 +12013,73 @@ static void genAddrOf (iCode *ic) symbol *sym; // = OP_SYMBOL(IC_LEFT(ic)); pCodeOp *pcop0, *pcop1, *pcop2; - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - - pic16_aopOp((left=IC_LEFT(ic)), ic, FALSE); - pic16_aopOp((result=IC_RESULT(ic)),ic,TRUE); + FENTRY; - sym = OP_SYMBOL( left ); + pic16_aopOp((result=IC_RESULT(ic)),ic,TRUE); - size = AOP_SIZE(IC_RESULT(ic)); + sym = OP_SYMBOL( IC_LEFT(ic) ); + + if(sym->onStack) { + /* get address of symbol on stack */ + DEBUGpic16_emitcode("; ", "%s symbol %s on stack", __FUNCTION__, sym->name); +#if 0 + fprintf(stderr, "%s:%d symbol %s on stack offset %d\n", __FILE__, __LINE__, + OP_SYMBOL(left)->name, OP_SYMBOL(left)->stack); +#endif + pic16_emitpcode(POC_MOVFF, pic16_popGet2p( + pic16_popCopyReg(&pic16_pc_fsr2l), + pic16_popGet(AOP(result), 0))); + pic16_emitpcode(POC_MOVFF, pic16_popGet2p( + pic16_popCopyReg(&pic16_pc_fsr2h), + pic16_popGet(AOP(result), 1))); + + // operands on stack are accessible via "FSR2 - index" with index starting at 0 for the first operand + pic16_emitpcode(POC_MOVLW, pic16_popGetLit( - (OP_SYMBOL( IC_LEFT(ic))->stack + 1 ) /*+ _G.stack_lat*/)); + pic16_emitpcode(POC_SUBWF, pic16_popGet(AOP(result), 0)); + emitSKPC; + pic16_emitpcode(POC_DECF, pic16_popGet(AOP(result), 1)); - if(sym->onStack) { - DEBUGpic16_emitcode("; ", "%s symbol %s on stack", __FUNCTION__, sym->name); - - return; - } + goto release; + } // if(pic16_debug_verbose) { // fprintf(stderr, "%s:%d %s symbol %s , codespace=%d\n", // __FILE__, __LINE__, __FUNCTION__, sym->name, IN_CODESPACE( SPEC_OCLS(sym->etype))); // } - /* Assume that what we want the address of is in data space - * since there is no stack on the PIC, yet! -- VR */ - /* low */ - pcop0 = PCOP(pic16_newpCodeOpImmd(sym->rname, 0, 0, IN_CODESPACE( SPEC_OCLS(sym->etype)))); + pic16_aopOp((left=IC_LEFT(ic)), ic, FALSE); + size = AOP_SIZE(IC_RESULT(ic)); - /* high */ - pcop1 = PCOP(pic16_newpCodeOpImmd(sym->rname, 1, 0, IN_CODESPACE( SPEC_OCLS(sym->etype)))); - - /* upper */ - pcop2 = PCOP(pic16_newpCodeOpImmd(sym->rname, 2, 0, IN_CODESPACE( SPEC_OCLS(sym->etype)))); - - if (size == 3) { - pic16_emitpcode(POC_MOVLW, pcop0); - pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result), 0)); - pic16_emitpcode(POC_MOVLW, pcop1); - pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result), 1)); - pic16_emitpcode(POC_MOVLW, pcop2); - pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result), 2)); - } else - if (size == 2) { - pic16_emitpcode(POC_MOVLW, pcop0); - pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),0)); - pic16_emitpcode(POC_MOVLW, pcop1); - pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),1)); - } else { - pic16_emitpcode(POC_MOVLW, pcop0); - pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),0)); - } + /* Assume that what we want the address of is in data space + * since there is no stack on the PIC, yet! -- VR */ + + pcop0 = PCOP(pic16_newpCodeOpImmd(sym->rname, 0, 0, IN_CODESPACE( SPEC_OCLS(sym->etype)))); + pcop1 = PCOP(pic16_newpCodeOpImmd(sym->rname, 1, 0, IN_CODESPACE( SPEC_OCLS(sym->etype)))); + pcop2 = PCOP(pic16_newpCodeOpImmd(sym->rname, 2, 0, IN_CODESPACE( SPEC_OCLS(sym->etype)))); + + if (size == 3) { + pic16_emitpcode(POC_MOVLW, pcop0); + pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result), 0)); + pic16_emitpcode(POC_MOVLW, pcop1); + pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result), 1)); + pic16_emitpcode(POC_MOVLW, pcop2); + pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result), 2)); + } else + if (size == 2) { + pic16_emitpcode(POC_MOVLW, pcop0); + pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),0)); + pic16_emitpcode(POC_MOVLW, pcop1); + pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),1)); + } else { + pic16_emitpcode(POC_MOVLW, pcop0); + pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),0)); + } - pic16_freeAsmop(result,NULL,ic,TRUE); - pic16_freeAsmop(left, NULL, ic, FALSE); + pic16_freeAsmop(left, NULL, ic, FALSE); +release: + pic16_freeAsmop(result,NULL,ic,TRUE); } @@ -10804,14 +12124,17 @@ static void genAssign (iCode *ic) result = IC_RESULT(ic); right = IC_RIGHT(ic) ; - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + FENTRY; /* if they are the same */ if (operandsEqu (IC_RESULT(ic),IC_RIGHT(ic))) return ; - pic16_aopOp(right,ic,FALSE); + /* reversed order operands are aopOp'ed so that result operand + * is effective in case right is a stack symbol. This maneauver + * allows to use the _G.resDirect flag later */ pic16_aopOp(result,ic,TRUE); + pic16_aopOp(right,ic,FALSE); DEBUGpic16_pic16_AopType(__LINE__,NULL,right,result); @@ -10889,17 +12212,26 @@ static void genAssign (iCode *ic) // fprintf(stderr, "%s:%d: assigning value 0x%04lx (%d:%d)\n", __FUNCTION__, __LINE__, lit, // sizeof(unsigned long int), sizeof(float)); + + if (AOP_TYPE(right) == AOP_REG) { + DEBUGpic16_emitcode("; ", "%s:%d assign from register\n", __FUNCTION__, __LINE__); + while (size--) { + + pic16_emitpcode (POC_MOVFF, pic16_popGet2(AOP(right), AOP(result), offset++)); + } // while + goto release; + } + if(AOP_TYPE(right) != AOP_LIT - && IN_CODESPACE(SPEC_OCLS(OP_SYMBOL(right)->etype))) { + && IN_CODESPACE(SPEC_OCLS(OP_SYMBOL(right)->etype)) + && !IS_FUNC(OP_SYM_TYPE(right)) + ) { DEBUGpic16_emitcode("; ", "%s:%d symbol in code space, take special care\n", __FUNCTION__, __LINE__); fprintf(stderr, "%s:%d symbol %s = [ %s ] is in code space\n", __FILE__, __LINE__, OP_SYMBOL(result)->name, OP_SYMBOL(right)->name); // set up table pointer - if( (AOP_TYPE(right) == AOP_PCODE) - && ((AOP(right)->aopu.pcop->type == PO_IMMEDIATE) - || (AOP(right)->aopu.pcop->type == PO_DIR))) - { - fprintf(stderr, "%s:%d inside block 1\n", __FILE__, __LINE__); + if(is_LitOp(right)) { +// fprintf(stderr, "%s:%d inside block 1\n", __FILE__, __LINE__); pic16_emitpcode(POC_MOVLW,pic16_popGet(AOP(right),0)); pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_tblptrl)); pic16_emitpcode(POC_MOVLW,pic16_popGet(AOP(right),1)); @@ -10907,7 +12239,7 @@ static void genAssign (iCode *ic) pic16_emitpcode(POC_MOVLW,pic16_popGet(AOP(right),2)); pic16_emitpcode(POC_MOVWF,pic16_popCopyReg(&pic16_pc_tblptru)); } else { - fprintf(stderr, "%s:%d inside block 2\n", __FILE__, __LINE__); +// fprintf(stderr, "%s:%d inside block 2\n", __FILE__, __LINE__); pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(right),0), pic16_popCopyReg(&pic16_pc_tblptrl))); pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(right),1), @@ -10956,7 +12288,7 @@ static void genAssign (iCode *ic) know_W=-1; while (size--) { - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + DEBUGpic16_emitcode ("; ***","%s %d size %d",__FUNCTION__,__LINE__, size); if(AOP_TYPE(right) == AOP_LIT) { if(lit&0xff) { if(know_W != (lit&0xff)) @@ -10971,39 +12303,23 @@ static void genAssign (iCode *ic) } else if (AOP_TYPE(right) == AOP_CRY) { pic16_emitpcode(POC_CLRF, pic16_popGet(AOP(result),offset)); if(offset == 0) { - pic16_emitpcode(POC_BTFSS, pic16_popGet(AOP(right),0)); + //debugf("%s: BTFSS offset == 0\n", __FUNCTION__); + pic16_emitpcode(POC_BTFSC, pic16_popGet(AOP(right),0)); pic16_emitpcode(POC_INCF, pic16_popGet(AOP(result),0)); } } else if ( (AOP_TYPE(right) == AOP_PCODE) && (AOP(right)->aopu.pcop->type == PO_IMMEDIATE) ) { pic16_emitpcode(POC_MOVLW, pic16_popGet(AOP(right),offset)); pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offset)); } else { - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); -#if 1 - /* This is a hack to turn MOVFW/MOVWF pairs to MOVFF command. It - normally should work, but mind that the W register live range - is not checked, so if the code generator assumes that the W - is already loaded after such a pair, wrong code will be generated. - - Checking the live range is the next step. - This is experimental code yet and has not been fully tested yet. - USE WITH CARE. Revert to old code by setting 0 to the condition above. - Vangelis Rokas 030603 (vrokas@otenet.gr) */ - - pic16_emitpcode(POC_MOVFF, pic16_popGet2(AOP(right), AOP(result), offset)); -#else - /* This is the old code, which is assumed(?!) that works fine(!?) */ - - pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),offset)); - pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offset)); -#endif + if(!_G.resDirect) /* use this aopForSym feature */ + pic16_emitpcode(POC_MOVFF, pic16_popGet2(AOP(right), AOP(result), offset)); } offset++; } - - + release: pic16_freeAsmop (right,NULL,ic,FALSE); pic16_freeAsmop (result,NULL,ic,TRUE); @@ -11014,10 +12330,13 @@ static void genAssign (iCode *ic) /*-----------------------------------------------------------------*/ static void genJumpTab (iCode *ic) { - symbol *jtab; - char *l; + symbol *jtab; + char *l; + pCodeOp *jt_offs; + pCodeOp *jt_offs_hi; + pCodeOp *jt_label; - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + FENTRY; pic16_aopOp(IC_JTCOND(ic),ic,FALSE); /* get the condition into accumulator */ @@ -11032,6 +12351,7 @@ static void genJumpTab (iCode *ic) pic16_emitcode("jmp","@a+dptr"); pic16_emitcode("","%05d_DS_:",jtab->key+100); +#if 0 pic16_emitpcode(POC_MOVLW, pic16_popGetLabel(jtab->key)); pic16_emitpcode(POC_ADDFW, pic16_popGet(AOP(IC_JTCOND(ic)),0)); emitSKPNC; @@ -11039,15 +12359,58 @@ static void genJumpTab (iCode *ic) pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_pcl)); pic16_emitpLabel(jtab->key); +#else + + jt_offs = pic16_popGetTempReg(0); + jt_offs_hi = pic16_popGetTempReg(1); + jt_label = pic16_popGetLabel (jtab->key); + //fprintf (stderr, "Creating jump table...\n"); + + // calculate offset into jump table (idx * sizeof (GOTO)) + pic16_emitpcode(POC_CLRF , jt_offs_hi); + pic16_emitpcode(POC_RLCFW , pic16_popGet(AOP(IC_JTCOND(ic)),0)); + pic16_emitpcode(POC_RLCF , jt_offs_hi); + pic16_emitpcode(POC_RLCFW , pic16_popCopyReg(&pic16_pc_wreg)); + pic16_emitpcode(POC_RLCF , jt_offs_hi); + pic16_emitpcode(POC_ANDLW , pic16_popGetLit (0xFC)); + pic16_emitpcode(POC_MOVWF , jt_offs); + + // prepare PCLATx (set to first entry in jump table) + pic16_emitpcode(POC_MOVLW , pic16_popGetImmd(jt_label->name, 2, 0)); + pic16_emitpcode(POC_MOVWF , pic16_popCopyReg(&pic16_pc_pclatu)); + pic16_emitpcode(POC_MOVLW , pic16_popGetImmd(jt_label->name, 1, 0)); + pic16_emitpcode(POC_MOVWF , pic16_popCopyReg(&pic16_pc_pclath)); + pic16_emitpcode(POC_MOVLW , pic16_popGetImmd(jt_label->name, 0, 0)); + + // set PCLATx to selected entry (new PCL is stored in jt_offs) + pic16_emitpcode(POC_ADDWF , jt_offs); + pic16_emitpcode(POC_MOVFW , jt_offs_hi); + pic16_emitpcode(POC_ADDWFC, pic16_popCopyReg(&pic16_pc_pclath)); + emitSKPNC; + pic16_emitpcode(POC_INCF , pic16_popCopyReg(&pic16_pc_pclatu)); + + // release temporaries and prepare jump into table (new PCL --> WREG) + pic16_emitpcode(POC_MOVFW , jt_offs); + pic16_popReleaseTempReg (jt_offs_hi, 1); + pic16_popReleaseTempReg (jt_offs, 0); + + // jump into the table + pic16_emitpcode(POC_MOVWF , pic16_popCopyReg(&pic16_pc_pcl)); + + pic16_emitpLabelFORCE(jtab->key); + +#endif pic16_freeAsmop(IC_JTCOND(ic),NULL,ic,TRUE); + pic16_emitpinfo (INF_OPTIMIZATION, pic16_newpCodeOpOpt (OPT_JUMPTABLE_BEGIN, "")); /* now generate the jump labels */ for (jtab = setFirstItem(IC_JTLABELS(ic)) ; jtab; jtab = setNextItem(IC_JTLABELS(ic))) { - pic16_emitcode("ljmp","%05d_DS_",jtab->key+100); +// pic16_emitcode("ljmp","%05d_DS_",jtab->key+100); pic16_emitpcode(POC_GOTO,pic16_popGetLabel(jtab->key)); } + pic16_emitpinfo (INF_OPTIMIZATION, pic16_newpCodeOpOpt (OPT_JUMPTABLE_END, "")); } @@ -11190,19 +12553,23 @@ static void genCast (iCode *ic) operand *result = IC_RESULT(ic); sym_link *ctype = operandType(IC_LEFT(ic)); sym_link *rtype = operandType(IC_RIGHT(ic)); + sym_link *restype = operandType(IC_RESULT(ic)); operand *right = IC_RIGHT(ic); int size, offset ; - DEBUGpic16_emitcode("; ***","%s %d",__FUNCTION__,__LINE__); + + FENTRY; + /* if they are equivalent then do nothing */ - if (operandsEqu(IC_RESULT(ic),IC_RIGHT(ic))) - return ; +// if (operandsEqu(IC_RESULT(ic),IC_RIGHT(ic))) +// return ; pic16_aopOp(right,ic,FALSE) ; pic16_aopOp(result,ic,FALSE); DEBUGpic16_pic16_AopType(__LINE__,NULL,right,result); + /* if the result is a bit */ if (AOP_TYPE(result) == AOP_CRY) { @@ -11266,6 +12633,11 @@ static void genCast (iCode *ic) goto release; } + if(IS_BITFIELD(getSpec(restype)) + && IS_BITFIELD(getSpec(rtype))) { + DEBUGpic16_emitcode("***", "%d casting a bit to another bit", __LINE__); + } + /* if they are the same size : or less */ if (AOP_SIZE(result) <= AOP_SIZE(right)) { @@ -11379,16 +12751,20 @@ static void genCast (iCode *ic) offset = 0 ; while (size--) { if(offset < AOP_SIZE(right)) { - DEBUGpic16_emitcode("; ***","%s %d - pointer cast3",__FUNCTION__,__LINE__); + DEBUGpic16_emitcode("; ***","%s %d - pointer cast3 ptype = 0x%x",__FUNCTION__,__LINE__, p_type); + mov2f(AOP(result), AOP(right), offset); +/* if ((AOP_TYPE(right) == AOP_PCODE) && AOP(right)->aopu.pcop->type == PO_IMMEDIATE) { pic16_emitpcode(POC_MOVLW, pic16_popGet(AOP(right),offset)); pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offset)); } else { + pic16_aopPut(AOP(result), pic16_aopGet(AOP(right),offset,FALSE,FALSE), offset); } +*/ } else pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),offset)); offset++; @@ -11397,21 +12773,28 @@ static void genCast (iCode *ic) switch (p_type) { case IPOINTER: case POINTER: - pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),GPTRSIZE - 1)); + pic16_emitpcode(POC_MOVLW, pic16_popGetLit(0x80)); + pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result), GPTRSIZE - 1)); +// pic16_emitpcode(POC_CLRF,pic16_popGet(AOP(result),GPTRSIZE - 1)); break; + + case CPOINTER: + pic16_emitpcode(POC_MOVFF, pic16_popGet2(AOP(right), AOP(result), GPTRSIZE-1)); + break; + case FPOINTER: pic16_emitcode(";BUG!? ","%d",__LINE__); l = one; break; - case CPOINTER: - pic16_emitcode(";BUG!? ","%d",__LINE__); - l = "#0x02"; - break; case PPOINTER: pic16_emitcode(";BUG!? ","%d",__LINE__); l = "#0x03"; break; - + + case GPOINTER: + pic16_emitpcode(POC_MOVFF, pic16_popGet2(AOP(right), AOP(result), GPTRSIZE-1)); + break; + default: /* this should never happen */ werror(E_INTERNAL_ERROR,__FILE__,__LINE__, @@ -11450,9 +12833,7 @@ static void genCast (iCode *ic) size = AOP_SIZE(right); offset = 0 ; while (size--) { - pic16_emitpcode(POC_MOVFF, pic16_popGet2(AOP(right), AOP(result), offset)); -// pic16_emitpcode(POC_MOVFW, pic16_popGet(AOP(right),offset)); -// pic16_emitpcode(POC_MOVWF, pic16_popGet(AOP(result),offset)); + mov2f(AOP(result), AOP(right), offset); offset++; } @@ -11542,11 +12923,6 @@ static int genDjnz (iCode *ic, iCode *ifx) pic16_emitcode ("goto","_%05d_DS_",IC_TRUE(ifx)->key+100 + pic16_labelOffset); } -/* pic16_emitcode ("sjmp","%05d_DS_",lbl1->key+100); */ -/* pic16_emitcode ("","%05d_DS_:",lbl->key+100); */ -/* pic16_emitcode ("ljmp","%05d_DS_",IC_TRUE(ifx)->key+100); */ -/* pic16_emitcode ("","%05d_DS_:",lbl1->key+100); */ - pic16_freeAsmop(IC_RESULT(ic),NULL,ic,TRUE); ifx->generated = 1; @@ -11558,47 +12934,52 @@ static int genDjnz (iCode *ic, iCode *ifx) /*-----------------------------------------------------------------*/ static void genReceive (iCode *ic) { - DEBUGpic16_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + + FENTRY; #if 0 - fprintf(stderr, "%s:%d %s for symbol %s\tonStack: %d\n", __FILE__, __LINE__, __FUNCTION__, - OP_SYMBOL( IC_RESULT(ic) )->rname, OP_SYMBOL( IC_RESULT(ic) )->onStack); + fprintf(stderr, "%s:%d %s for symbol %s\tonStack: %d\n", __FILE__, __LINE__, __FUNCTION__, + OP_SYMBOL( IC_RESULT(ic) )->rname, OP_SYMBOL( IC_RESULT(ic) )->onStack); #endif -// pic16_DumpOp(__FUNCTION__, IC_RESULT(ic)); +// pic16_DumpOp(__FUNCTION__, IC_RESULT(ic)); - if (isOperandInFarSpace(IC_RESULT(ic)) && - ( OP_SYMBOL(IC_RESULT(ic))->isspilt || - IS_TRUE_SYMOP(IC_RESULT(ic))) ) { + if (isOperandInFarSpace(IC_RESULT(ic)) + && ( OP_SYMBOL(IC_RESULT(ic))->isspilt + || IS_TRUE_SYMOP(IC_RESULT(ic))) ) { - int size = getSize(operandType(IC_RESULT(ic))); - int offset = pic16_fReturnSizePic - size; + int size = getSize(operandType(IC_RESULT(ic))); + int offset = pic16_fReturnSizePic - size; - assert( 0 ); - while (size--) { - pic16_emitcode ("push","%s", (strcmp(fReturn[pic16_fReturnSizePic - offset - 1],"a") ? - fReturn[pic16_fReturnSizePic - offset - 1] : "acc")); - offset++; - } + assert( 0 ); + while (size--) { + pic16_emitcode ("push","%s", (strcmp(fReturn[pic16_fReturnSizePic - offset - 1],"a") ? + fReturn[pic16_fReturnSizePic - offset - 1] : "acc")); + offset++; + } - DEBUGpic16_emitcode ("; ***","1 %s %d",__FUNCTION__,__LINE__); + DEBUGpic16_emitcode ("; ***","1 %s %d",__FUNCTION__,__LINE__); - pic16_aopOp(IC_RESULT(ic),ic,FALSE); - size = AOP_SIZE(IC_RESULT(ic)); - offset = 0; - while (size--) { - pic16_emitcode ("pop","acc"); - pic16_aopPut (AOP(IC_RESULT(ic)),"a",offset++); - } - } else { - DEBUGpic16_emitcode ("; ***","2 %s %d",__FUNCTION__,__LINE__); + pic16_aopOp(IC_RESULT(ic),ic,FALSE); + size = AOP_SIZE(IC_RESULT(ic)); + offset = 0; + while (size--) { + pic16_emitcode ("pop","acc"); + pic16_aopPut (AOP(IC_RESULT(ic)),"a",offset++); + } + } else { + DEBUGpic16_emitcode ("; ***","2 %s %d argreg = %d",__FUNCTION__,__LINE__, SPEC_ARGREG(OP_SYM_ETYPE(IC_RESULT(ic)) )); + _G.accInUse++; + pic16_aopOp(IC_RESULT(ic),ic,FALSE); + _G.accInUse--; - _G.accInUse++; - pic16_aopOp(IC_RESULT(ic),ic,FALSE); - _G.accInUse--; - assignResultValue(IC_RESULT(ic), 0); - } + /* set pseudo stack pointer to where it should be - dw*/ + GpsuedoStkPtr = ic->parmBytes; + + /* setting GpsuedoStkPtr has side effects here: */ + assignResultValue(IC_RESULT(ic), 0); + } - pic16_freeAsmop(IC_RESULT(ic),NULL,ic,TRUE); + pic16_freeAsmop(IC_RESULT(ic),NULL,ic,TRUE); } /*-----------------------------------------------------------------*/ @@ -11627,8 +13008,8 @@ genDummyRead (iCode * ic) void genpic16Code (iCode *lic) { - iCode *ic; - int cln = 0; + iCode *ic; + int cln = 0; lineHead = lineCurr = NULL; @@ -11639,7 +13020,7 @@ void genpic16Code (iCode *lic) /* if debug information required */ if (options.debug && currFunc) { if (currFunc) { - cdbSymbol(currFunc,cdbFile,FALSE,TRUE); + cdbSymbol(currFunc,cdbFile,FALSE,TRUE); } } #endif @@ -11647,234 +13028,233 @@ void genpic16Code (iCode *lic) for (ic = lic ; ic ; ic = ic->next ) { DEBUGpic16_emitcode(";ic ", "\t%c 0x%x",ic->op, ic->op); - if ( cln != ic->lineno ) { - if ( options.debug ) { - debugFile->writeCLine (ic); - } - - if(!options.noCcodeInAsm) { - pic16_addpCode2pBlock(pb, - pic16_newpCodeCSource(ic->lineno, ic->filename, - printCLine(ic->filename, ic->lineno))); - } + if ( cln != ic->lineno ) { + if ( options.debug ) { + debugFile->writeCLine (ic); + } + + if(!options.noCcodeInAsm) { + pic16_addpCode2pBlock(pb, pic16_newpCodeCSource(ic->lineno, ic->filename, + printCLine(ic->filename, ic->lineno))); + } - cln = ic->lineno ; - } + cln = ic->lineno ; + } - if(options.iCodeInAsm) { - char *l; - /* insert here code to print iCode as comment */ - l = Safe_strdup(printILine(ic)); - pic16_emitpcomment("ic:%d: %s", ic->seq, l); - } + if(options.iCodeInAsm) { + char *l; + + /* insert here code to print iCode as comment */ + l = Safe_strdup(printILine(ic)); + pic16_emitpcomment("ic:%d: %s", ic->seq, l); + } - /* if the result is marked as - spilt and rematerializable or code for - this has already been generated then - do nothing */ - if (resultRemat(ic) || ic->generated ) - continue ; + /* if the result is marked as + * spilt and rematerializable or code for + * this has already been generated then + * do nothing */ + if (resultRemat(ic) || ic->generated ) + continue ; - /* depending on the operation */ - switch (ic->op) { - case '!' : - pic16_genNot(ic); - break; - - case '~' : - pic16_genCpl(ic); - break; - - case UNARYMINUS: - genUminus (ic); - break; - - case IPUSH: - genIpush (ic); - break; + /* depending on the operation */ + switch (ic->op) { + case '!' : + pic16_genNot(ic); + break; - case IPOP: - /* IPOP happens only when trying to restore a - spilt live range, if there is an ifx statement - following this pop then the if statement might - be using some of the registers being popped which - would destroy the contents of the register so - we need to check for this condition and handle it */ - if (ic->next && - ic->next->op == IFX && - regsInCommon(IC_LEFT(ic),IC_COND(ic->next))) - genIfx (ic->next,ic); - else - genIpop (ic); - break; + case '~' : + pic16_genCpl(ic); + break; - case CALL: - genCall (ic); - break; + case UNARYMINUS: + genUminus (ic); + break; - case PCALL: - genPcall (ic); - break; + case IPUSH: + genIpush (ic); + break; - case FUNCTION: - genFunction (ic); - break; + case IPOP: + /* IPOP happens only when trying to restore a + * spilt live range, if there is an ifx statement + * following this pop then the if statement might + * be using some of the registers being popped which + * would destroy the contents of the register so + * we need to check for this condition and handle it */ + if (ic->next + && ic->next->op == IFX + && regsInCommon(IC_LEFT(ic),IC_COND(ic->next))) + genIfx (ic->next,ic); + else + genIpop (ic); + break; - case ENDFUNCTION: - genEndFunction (ic); - break; + case CALL: + genCall (ic); + break; - case RETURN: - genRet (ic); - break; + case PCALL: + genPcall (ic); + break; - case LABEL: - genLabel (ic); - break; + case FUNCTION: + genFunction (ic); + break; - case GOTO: - genGoto (ic); - break; + case ENDFUNCTION: + genEndFunction (ic); + break; - case '+' : - pic16_genPlus (ic) ; - break; + case RETURN: + genRet (ic); + break; - case '-' : - if ( ! genDjnz (ic,ifxForOp(IC_RESULT(ic),ic))) - pic16_genMinus (ic); - break; + case LABEL: + genLabel (ic); + break; - case '*' : - genMult (ic); - break; + case GOTO: + genGoto (ic); + break; - case '/' : - genDiv (ic) ; - break; + case '+' : + pic16_genPlus (ic) ; + break; - case '%' : - genMod (ic); - break; + case '-' : + if ( ! genDjnz (ic,ifxForOp(IC_RESULT(ic),ic))) + pic16_genMinus (ic); + break; + + case '*' : + genMult (ic); + break; - case '>' : - genCmpGt (ic,ifxForOp(IC_RESULT(ic),ic)); - break; + case '/' : + genDiv (ic) ; + break; - case '<' : - genCmpLt (ic,ifxForOp(IC_RESULT(ic),ic)); - break; + case '%' : + genMod (ic); + break; - case LE_OP: - case GE_OP: - case NE_OP: + case '>' : + genCmpGt (ic,ifxForOp(IC_RESULT(ic),ic)); + break; - /* note these two are xlated by algebraic equivalence - during parsing SDCC.y */ - werror(E_INTERNAL_ERROR,__FILE__,__LINE__, - "got '>=' or '<=' shouldn't have come here"); - break; + case '<' : + genCmpLt (ic,ifxForOp(IC_RESULT(ic),ic)); + break; - case EQ_OP: - genCmpEq (ic,ifxForOp(IC_RESULT(ic),ic)); - break; + case LE_OP: + case GE_OP: + case NE_OP: + /* note these two are xlated by algebraic equivalence + * during parsing SDCC.y */ + werror(E_INTERNAL_ERROR,__FILE__,__LINE__, + "got '>=' or '<=' shouldn't have come here"); + break; + + case EQ_OP: + genCmpEq (ic,ifxForOp(IC_RESULT(ic),ic)); + break; - case AND_OP: - genAndOp (ic); - break; + case AND_OP: + genAndOp (ic); + break; - case OR_OP: - genOrOp (ic); - break; + case OR_OP: + genOrOp (ic); + break; - case '^' : - genXor (ic,ifxForOp(IC_RESULT(ic),ic)); - break; + case '^' : + genXor (ic,ifxForOp(IC_RESULT(ic),ic)); + break; - case '|' : - genOr (ic,ifxForOp(IC_RESULT(ic),ic)); - break; + case '|' : + genOr (ic,ifxForOp(IC_RESULT(ic),ic)); + break; - case BITWISEAND: - genAnd (ic,ifxForOp(IC_RESULT(ic),ic)); - break; + case BITWISEAND: + genAnd (ic,ifxForOp(IC_RESULT(ic),ic)); + break; - case INLINEASM: - genInline (ic); - break; + case INLINEASM: + genInline (ic); + break; - case RRC: - genRRC (ic); - break; + case RRC: + genRRC (ic); + break; - case RLC: - genRLC (ic); - break; + case RLC: + genRLC (ic); + break; - case GETHBIT: - genGetHbit (ic); - break; + case GETHBIT: + genGetHbit (ic); + break; - case LEFT_OP: - genLeftShift (ic); - break; + case LEFT_OP: + genLeftShift (ic); + break; - case RIGHT_OP: - genRightShift (ic); - break; + case RIGHT_OP: + genRightShift (ic); + break; - case GET_VALUE_AT_ADDRESS: - genPointerGet(ic); - break; + case GET_VALUE_AT_ADDRESS: + genPointerGet(ic); + break; - case '=' : - if (POINTER_SET(ic)) - genPointerSet(ic); - else - genAssign(ic); - break; + case '=' : + if (POINTER_SET(ic)) + genPointerSet(ic); + else + genAssign(ic); + break; - case IFX: - genIfx (ic,NULL); - break; + case IFX: + genIfx (ic,NULL); + break; - case ADDRESS_OF: - genAddrOf (ic); - break; + case ADDRESS_OF: + genAddrOf (ic); + break; - case JUMPTABLE: - genJumpTab (ic); - break; + case JUMPTABLE: + genJumpTab (ic); + break; - case CAST: - genCast (ic); - break; + case CAST: + genCast (ic); + break; - case RECEIVE: - genReceive(ic); - break; + case RECEIVE: + genReceive(ic); + break; - case SEND: - addSet(&_G.sendSet,ic); - break; + case SEND: + addSet(&_G.sendSet,ic); + break; - case DUMMY_READ_VOLATILE: - genDummyRead (ic); - break; + case DUMMY_READ_VOLATILE: + genDummyRead (ic); + break; - default : - ic = ic; - } + default : + ic = ic; + } } /* now we are ready to call the peep hole optimizer */ - if (!options.nopeep) { + if (!options.nopeep) peepHole (&lineHead); - } + /* now do the actual printing */ - printLine (lineHead,codeOutFile); + printLine (lineHead, codeOutFile); #ifdef PCODE_DEBUG DFPRINTF((stderr,"printing pBlock\n\n"));