+ DEBUGpic14_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);
+
+ /* First figure how many parameters are getting passed */
+ for (sic = setFirstItem(_G.sendSet) ; sic ;
+ sic = setNextItem(_G.sendSet)) {
+
+ aopOp(IC_LEFT(sic),sic,FALSE);
+ psuedoStkPtr += AOP_SIZE(IC_LEFT(sic));
+ freeAsmop (IC_LEFT(sic),NULL,sic,FALSE);
+ }
+
+ for (sic = setFirstItem(_G.sendSet) ; sic ;
+ sic = setNextItem(_G.sendSet)) {
+ int size, offset = 0;
+
+ aopOp(IC_LEFT(sic),sic,FALSE);
+ size = AOP_SIZE(IC_LEFT(sic));
+
+ while (size--) {
+ DEBUGpic14_emitcode ("; ","%d left %s",__LINE__,
+ AopType(AOP_TYPE(IC_LEFT(sic))));
+
+ 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. */
+ emitpcode(POC_MOVWF,popRegFromIdx(Gstack_base_addr - --psuedoStkPtr));
+
+ }
+ firstTimeThruLoop=0;
+
+ mov2w_op (IC_LEFT(sic), offset);
+ offset++;
+ }
+ freeAsmop (IC_LEFT(sic),NULL,sic,TRUE);
+ }
+ _G.sendSet = NULL;
+ }
+ /* make the call */
+ sym = OP_SYMBOL(IC_LEFT(ic));
+ name = sym->rname[0] ? sym->rname : sym->name;
+ /*
+ * As SDCC emits code as soon as it reaches the end of each
+ * function's definition, prototyped functions that are implemented
+ * after the current one are always considered EXTERN, which
+ * introduces many unneccessary PAGESEL instructions.
+ * XXX: Use a post pass to iterate over all `CALL _name' statements
+ * and insert `PAGESEL _name' and `PAGESEL $' around the CALL
+ * only iff there is no definition of the function in the whole
+ * file (might include this in the PAGESEL pass).
+ */
+ isExtern = IS_EXTERN(sym->etype) || pic14_inISR;
+ if (isExtern) {
+ /* Extern functions and ISRs maybe on a different page;
+ * must call pagesel */
+ emitpcode(POC_PAGESEL,popGetWithString(name,1));
+ }
+ emitpcode(POC_CALL,popGetWithString(name,isExtern));
+ if (isExtern) {
+ /* May have returned from a different page;
+ * must use pagesel to restore PCLATH before next
+ * goto or call instruction */
+ emitpcode(POC_PAGESEL,popGetWithString("$",0));
+ }
+ 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++;
+ aopOp(IC_RESULT(ic),ic,FALSE);
+ _G.accInUse--;
+
+ assignResultValue(IC_RESULT(ic));
+
+ DEBUGpic14_emitcode ("; ","%d left %s",__LINE__,
+ AopType(AOP_TYPE(IC_RESULT(ic))));
+
+ freeAsmop(IC_RESULT(ic),NULL, ic,TRUE);
+ }
+
+ /* 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);
+
+
+}
+
+/*-----------------------------------------------------------------*/
+/* genPcall - generates a call by pointer statement */
+/*-----------------------------------------------------------------*/
+static void genPcall (iCode *ic)
+{
+ sym_link *dtype;
+ symbol *albl = newiTempLabel(NULL);
+ symbol *blbl = newiTempLabel(NULL);
+ PIC_OPCODE poc;
+ pCodeOp *pcop;
+ operand *left;
+
+ FENTRY;
+
+ DEBUGpic14_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 &&
+ IFFUNC_ISISR(currFunc->type) &&
+ (FUNC_REGBANK(currFunc->type) != FUNC_REGBANK(dtype)))
+ saverbank(FUNC_REGBANK(dtype),ic,TRUE);
+
+ left = IC_LEFT(ic);
+ aopOp(left,ic,FALSE);
+ DEBUGpic14_AopType(__LINE__,left,NULL,NULL);
+
+ poc = ( op_isLitLike (IC_LEFT(ic)) ? POC_MOVLW : POC_MOVFW );
+
+ pushSide(IC_LEFT(ic), FPTRSIZE);
+
+ /* if send set is not empty, assign parameters */
+ if (_G.sendSet) {
+
+ DEBUGpic14_emitcode ("; ***","%s %d - WARNING arg-passing to indirect call not supported",__FUNCTION__,__LINE__);
+ /* no way to pass args - W always gets used to make the call */
+ }
+ /* first idea - factor out a common helper function and call it.
+ But don't know how to get it generated only once in its own block
+
+ if(AOP_TYPE(IC_LEFT(ic)) == AOP_DIR) {
+ char *rname;
+ char *buffer;
+ rname = IC_LEFT(ic)->aop->aopu.aop_dir;
+ DEBUGpic14_emitcode ("; ***","%s %d AOP_DIR %s",__FUNCTION__,__LINE__,rname);
+ buffer = Safe_calloc(1,strlen(rname)+16);
+ sprintf(buffer, "%s_goto_helper", rname);
+ addpCode2pBlock(pb,newpCode(POC_CALL,newpCodeOp(buffer,PO_STR)));
+ free(buffer);
+ }
+ */
+ emitpcode(POC_CALL,popGetLabel(albl->key));
+ pcop = popGetLabel(blbl->key);
+ emitpcode(POC_PAGESEL,pcop); /* Must restore PCLATH before goto, without destroying W */
+ emitpcode(POC_GOTO,pcop);
+ emitpLabel(albl->key);
+
+ emitpcode(poc,popGetAddr(AOP(left),1,0));
+ emitpcode(POC_MOVWF,popCopyReg(&pc_pclath));
+ emitpcode(poc,popGetAddr(AOP(left),0,0));
+ emitpcode(POC_MOVWF,popCopyReg(&pc_pcl));
+
+ emitpLabel(blbl->key);
+
+ freeAsmop(IC_LEFT(ic),NULL,ic,TRUE);
+
+ /* 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++;
+ aopOp(IC_RESULT(ic),ic,FALSE);
+ _G.accInUse--;
+
+ GpsuedoStkPtr = 0;
+
+ assignResultValue(IC_RESULT(ic));
+
+ freeAsmop(IC_RESULT(ic),NULL,ic,TRUE);
+ }
+
+ /* if register bank was saved then unsave them */
+ if (currFunc && dtype &&
+ (FUNC_REGBANK(currFunc->type) != FUNC_REGBANK(dtype)))
+ unsaverbank(FUNC_REGBANK(dtype),ic,TRUE);
+
+ /* if we hade saved some registers then
+ unsave them */
+ if (ic->regsSaved)
+ unsaveRegisters (ic);
+
+}
+
+/*-----------------------------------------------------------------*/
+/* resultRemat - result is rematerializable */
+/*-----------------------------------------------------------------*/
+static int resultRemat (iCode *ic)
+{
+ // DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__);
+ FENTRY;
+
+ if (SKIP_IC(ic) || ic->op == IFX)
+ return 0;
+
+ if (IC_RESULT(ic) && IS_ITEMP(IC_RESULT(ic))) {
+ symbol *sym = OP_SYMBOL(IC_RESULT(ic));
+ if (sym->remat && !POINTER_SET(ic))
+ return 1;
+ }
+
+ return 0;
+}
+
+#if 0
+/*-----------------------------------------------------------------*/
+/* inExcludeList - return 1 if the string is in exclude Reg list */
+/*-----------------------------------------------------------------*/
+static bool inExcludeList(char *s)
+{
+ DEBUGpic14_emitcode ("; ***","%s %d - WARNING no code generated",__FUNCTION__,__LINE__);
+ int i =0;
+
+ DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__);
+ if (options.excludeRegs[i] &&
+ STRCASECMP(options.excludeRegs[i],"none") == 0)
+ return FALSE ;
+
+ for ( i = 0 ; options.excludeRegs[i]; i++) {
+ if (options.excludeRegs[i] &&
+ STRCASECMP(s,options.excludeRegs[i]) == 0)
+ return TRUE;
+ }
+ return FALSE ;
+}
+#endif
+
+/*-----------------------------------------------------------------*/
+/* genFunction - generated code for function entry */
+/*-----------------------------------------------------------------*/
+static void genFunction (iCode *ic)
+{
+ symbol *sym;
+ sym_link *ftype;
+
+ FENTRY;
+
+ DEBUGpic14_emitcode ("; ***","%s %d curr label offset=%dprevious max_key=%d ",__FUNCTION__,__LINE__,labelOffset,max_key);
+
+ labelOffset += (max_key+4);
+ max_key=0;
+ GpsuedoStkPtr=0;
+ _G.nRegsSaved = 0;
+ /* create the function header */
+ pic14_emitcode(";","-----------------------------------------");
+ pic14_emitcode(";"," function %s",(sym = OP_SYMBOL(IC_LEFT(ic)))->name);
+ pic14_emitcode(";","-----------------------------------------");
+
+ /* prevent this symbol from being emitted as 'extern' */
+ pic14_stringInSet(sym->rname, &pic14_localFunctions, 1);
+
+ pic14_emitcode("","%s:",sym->rname);
+ addpCode2pBlock(pb,newpCodeFunction(NULL,sym->rname,!IS_STATIC (sym->etype)));
+
+ /* mark symbol as NOT extern (even if it was declared so previously) */
+ assert(IS_SPEC(sym->etype));
+ SPEC_EXTR(sym->etype) = 0;
+ sym->cdef = 0;
+ if (!SPEC_OCLS(sym->etype)) SPEC_OCLS(sym->etype) = code;
+ addSetIfnotP(&SPEC_OCLS(sym->etype)->syms, sym);
+
+ ftype = operandType(IC_LEFT(ic));
+
+ /* if critical function then turn interrupts off */
+ if (IFFUNC_ISCRITICAL(ftype))
+ pic14_emitcode("clr","ea");
+
+ /* here we need to generate the equates for the
+ register bank if required */
+#if 0
+ if (FUNC_REGBANK(ftype) != rbank) {
+ int i ;
+
+ rbank = FUNC_REGBANK(ftype);
+ for ( i = 0 ; i < pic14_nRegs ; i++ ) {
+ if (strcmp(regspic14[i].base,"0") == 0)
+ pic14_emitcode("","%s = 0x%02x",
+ regspic14[i].dname,
+ 8*rbank+regspic14[i].offset);
+ else
+ pic14_emitcode ("","%s = %s + 0x%02x",
+ regspic14[i].dname,
+ regspic14[i].base,
+ 8*rbank+regspic14[i].offset);
+ }
+ }
+#endif
+
+ /* if this is an interrupt service routine */
+ pic14_inISR = 0;
+ if (IFFUNC_ISISR(sym->type)) {
+ pic14_inISR = 1;
+ emitpcode(POC_MOVWF, popCopyReg(&pc_wsave));
+ emitpcode(POC_SWAPFW, popCopyReg(&pc_status));
+ /* XXX: Why? Does this assume that ssave and psave reside
+ * in a shared bank or bank0? We cannot guarantee the
+ * latter...
+ */
+ emitpcode(POC_CLRF, popCopyReg(&pc_status));
+ emitpcode(POC_MOVWF, popCopyReg(&pc_ssave));
+ //emitpcode(POC_MOVWF, popGetExternal("___sdcc_saved_status",1 ));
+ emitpcode(POC_MOVFW, popCopyReg(&pc_pclath));
+ /* during an interrupt PCLATH must be cleared before a goto or call statement */
+ emitpcode(POC_CLRF, popCopyReg(&pc_pclath));
+ emitpcode(POC_MOVWF, popCopyReg(&pc_psave));
+ //emitpcode(POC_MOVWF, popGetExternal("___sdcc_saved_pclath", 1));
+ emitpcode(POC_MOVFW, popCopyReg(&pc_fsr));
+ emitpcode(POC_MOVWF, popGetExternal("___sdcc_saved_fsr", 1));
+
+ pBlockConvert2ISR(pb);
+ pic14_hasInterrupt = 1;
+ } else {
+ /* 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 */
+ for ( i = 0 ; i < sym->regsUsed->size ; i++) {
+ if (bitVectBitValue(sym->regsUsed,i)) {
+ //pic14_emitcode("push","%s",pic14_regWithIdx(i)->dname);
+ _G.nRegsSaved++;
+ }
+ }
+ }
+ }
+ }
+
+ /* set the register bank to the desired value */
+ if (FUNC_REGBANK(sym->type) || FUNC_ISISR(sym->type)) {
+ pic14_emitcode("push","psw");
+ pic14_emitcode("mov","psw,#0x%02x",(FUNC_REGBANK(sym->type) << 3)&0x00ff);
+ }
+
+ if (IFFUNC_ISREENT(sym->type) || options.stackAuto) {
+
+ if (options.useXstack) {
+ pic14_emitcode("mov","r0,%s",spname);
+ pic14_emitcode("mov","a,_bp");
+ pic14_emitcode("movx","@r0,a");
+ pic14_emitcode("inc","%s",spname);
+ }
+ else
+ {
+ /* set up the stack */
+ pic14_emitcode ("push","_bp"); /* save the callers stack */
+ }
+ pic14_emitcode ("mov","_bp,%s",spname);
+ }
+
+ /* adjust the stack for the function */
+ if (sym->stack) {
+
+ int i = sym->stack;
+ if (i > 256 )
+ werror(W_STACK_OVERFLOW,sym->name);
+
+ if (i > 3 && sym->recvSize < 4) {
+
+ pic14_emitcode ("mov","a,sp");
+ pic14_emitcode ("add","a,#0x%02x",((char)sym->stack & 0xff));
+ pic14_emitcode ("mov","sp,a");
+
+ }
+ else
+ while(i--)
+ pic14_emitcode("inc","sp");
+ }
+
+ if (sym->xstack) {
+
+ pic14_emitcode ("mov","a,_spx");
+ pic14_emitcode ("add","a,#0x%02x",((char)sym->xstack & 0xff));
+ pic14_emitcode ("mov","_spx,a");
+ }
+
+}
+
+/*-----------------------------------------------------------------*/
+/* genEndFunction - generates epilogue for functions */
+/*-----------------------------------------------------------------*/
+static void genEndFunction (iCode *ic)
+{
+ symbol *sym = OP_SYMBOL(IC_LEFT(ic));
+
+ FENTRY;
+
+ DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__);
+
+ if (IFFUNC_ISREENT(sym->type) || options.stackAuto)
+ {
+ pic14_emitcode ("mov","%s,_bp",spname);
+ }
+
+ /* if use external stack but some variables were
+ added to the local stack then decrement the
+ local stack */
+ if (options.useXstack && sym->stack) {
+ pic14_emitcode("mov","a,sp");
+ pic14_emitcode("add","a,#0x%02x",((char)-sym->stack) & 0xff);
+ pic14_emitcode("mov","sp,a");
+ }
+
+
+ if ((IFFUNC_ISREENT(sym->type) || options.stackAuto)) {
+ if (options.useXstack) {
+ pic14_emitcode("mov","r0,%s",spname);
+ pic14_emitcode("movx","a,@r0");
+ pic14_emitcode("mov","_bp,a");
+ pic14_emitcode("dec","%s",spname);
+ }
+ else
+ {
+ pic14_emitcode ("pop","_bp");
+ }
+ }
+
+ /* restore the register bank */
+ if (FUNC_REGBANK(sym->type) || FUNC_ISISR(sym->type))
+ pic14_emitcode ("pop","psw");
+
+ if (IFFUNC_ISISR(sym->type)) {
+
+ /* now we need to restore the registers */
+ /* if this isr has no bank i.e. is going to
+ run with bank 0 , then we need to save more
+registers :-) */
+ if (!FUNC_REGBANK(sym->type)) {
+
+ /* if this function does not call any other
+ function then we can be economical and
+ save only those registers that are used */
+ if (! IFFUNC_HASFCALL(sym->type)) {
+ int i;
+
+ /* if any registers used */
+ if (sym->regsUsed) {
+ /* save the registers used */
+ for ( i = sym->regsUsed->size ; i >= 0 ; i--) {
+ if (bitVectBitValue(sym->regsUsed,i)) {
+ pic14_emitcode("pop","junk");//"%s",pic14_regWithIdx(i)->dname);
+ }
+ }
+ }
+
+ } else {
+ /* this function has a function call; cannot
+ determines register usage so we will have the
+ entire bank */
+ unsaverbank(0,ic,FALSE);
+ }
+ }
+
+ /* if debug then send end of function */
+ if (options.debug && debugFile && currFunc) {
+ debugFile->writeEndFunction (currFunc, ic, 1);
+ }
+
+ emitpcode(POC_MOVFW, popGetExternal("___sdcc_saved_fsr", 1));
+ emitpcode(POC_MOVWF, popCopyReg(&pc_fsr));
+ //emitpcode(POC_MOVFW, popGetExternal("___sdcc_saved_pclath", 1));
+ emitpcode(POC_MOVFW, popCopyReg(&pc_psave));
+ emitpcode(POC_MOVWF, popCopyReg(&pc_pclath));
+ emitpcode(POC_CLRF, popCopyReg(&pc_status)); // see genFunction
+ //emitpcode(POC_SWAPFW, popGetExternal("___sdcc_saved_status", 1));
+ emitpcode(POC_SWAPFW, popCopyReg(&pc_ssave));
+ emitpcode(POC_MOVWF, popCopyReg(&pc_status));
+ emitpcode(POC_SWAPF, popCopyReg(&pc_wsave));
+ emitpcode(POC_SWAPFW, popCopyReg(&pc_wsave));
+ addpCode2pBlock(pb,newpCodeLabel("END_OF_INTERRUPT",-1));
+ emitpcodeNULLop(POC_RETFIE);
+ }
+ else {
+ if (IFFUNC_ISCRITICAL(sym->type))
+ pic14_emitcode("setb","ea");
+
+ if (IFFUNC_CALLEESAVES(sym->type)) {
+ int i;
+
+ /* if any registers used */
+ if (sym->regsUsed) {
+ /* save the registers used */
+ for ( i = sym->regsUsed->size ; i >= 0 ; i--) {
+ if (bitVectBitValue(sym->regsUsed,i)) {
+ pic14_emitcode("pop","junk");//"%s",pic14_regWithIdx(i)->dname);
+ }
+ }
+ }
+ }
+
+ /* if debug then send end of function */
+ if (options.debug && debugFile && currFunc) {
+ debugFile->writeEndFunction (currFunc, ic, 1);
+ }
+
+ pic14_emitcode ("return","");
+ emitpcodeNULLop(POC_RETURN);
+
+ /* Mark the end of a function */
+ addpCode2pBlock(pb,newpCodeFunction(NULL,NULL,0));
+ }
+
+}
+
+/*-----------------------------------------------------------------*/
+/* genRet - generate code for return statement */
+/*-----------------------------------------------------------------*/
+static void genRet (iCode *ic)
+{
+ int size,offset = 0;
+
+ FENTRY;
+
+ DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__);
+ /* if we have no return value then
+ just generate the "ret" */
+ if (!IC_LEFT(ic))
+ goto jumpret;
+
+ /* we have something to return then
+ move the return value into place */
+ aopOp(IC_LEFT(ic),ic,FALSE);
+ size = AOP_SIZE(IC_LEFT(ic));
+
+ for (offset = 0; offset < size; offset++)
+ {
+ pass_argument (IC_LEFT(ic), offset, size - 1 - offset);
+ }
+
+ freeAsmop (IC_LEFT(ic),NULL,ic,TRUE);
+
+jumpret:
+ /* generate a jump to the return label
+ if the next is not the return statement */
+ if (!(ic->next && ic->next->op == LABEL &&
+ IC_LABEL(ic->next) == returnLabel)) {
+
+ emitpcode(POC_GOTO,popGetLabel(returnLabel->key));
+ }
+
+}
+
+/*-----------------------------------------------------------------*/
+/* genLabel - generates a label */
+/*-----------------------------------------------------------------*/
+static void genLabel (iCode *ic)
+{
+ FENTRY;
+
+ /* special case never generate */
+ DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__);
+ if (IC_LABEL(ic) == entryLabel)
+ return ;
+
+ emitpLabel(IC_LABEL(ic)->key);
+ pic14_emitcode("","_%05d_DS_:",(IC_LABEL(ic)->key+100 + labelOffset));
+}
+
+/*-----------------------------------------------------------------*/
+/* genGoto - generates a goto */
+/*-----------------------------------------------------------------*/
+//tsd
+static void genGoto (iCode *ic)
+{
+ FENTRY;
+
+ emitpcode(POC_GOTO,popGetLabel(IC_LABEL(ic)->key));
+ pic14_emitcode ("goto","_%05d_DS_",(IC_LABEL(ic)->key+100)+labelOffset);
+}
+
+
+/*-----------------------------------------------------------------*/
+/* genMultbits :- multiplication of bits */
+/*-----------------------------------------------------------------*/
+static void genMultbits (operand *left,
+ operand *right,
+ operand *result)
+{
+ FENTRY;
+ DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__);
+
+ if(!pic14_sameRegs(AOP(result),AOP(right)))
+ emitpcode(POC_BSF, popGet(AOP(result),0));
+
+ emitpcode(POC_BTFSC,popGet(AOP(right),0));
+ emitpcode(POC_BTFSS,popGet(AOP(left),0));
+ emitpcode(POC_BCF, popGet(AOP(result),0));
+
+}
+
+
+/*-----------------------------------------------------------------*/
+/* genMultOneByte : 8 bit multiplication & division */