+ 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));
+ }
+
+ pic16_emitpcode(POC_COMFW, pic16_popGet(AOP(right),0));
+ pic16_emitpcode(POC_RLCF, pic16_popGet(AOP(result),0));
+ pic16_emitpLabel(tlbl->key);
+ pic16_emitpcode(POC_RRCF, pic16_popGet(AOP(result),0));
+ pic16_emitpcode(POC_ADDLW, pic16_popGetLit(1));
+ emitSKPC;
+ pic16_emitpcode(POC_GOTO,pic16_popGetLabel(tlbl->key));
+
+ goto release ;
+ }
+
+ reAdjustPreg(AOP(result));
+ pic16_emitcode("sjmp","%05d_DS_",tlbl1->key+100);
+ pic16_emitcode("","%05d_DS_:",tlbl->key+100);
+ CLRC;
+ while (size--) {
+ l = pic16_aopGet(AOP(result),offset,FALSE,FALSE);
+ MOVA(l);
+ pic16_emitcode("rrc","a");
+ pic16_aopPut(AOP(result),"a",offset--);
+ }
+ reAdjustPreg(AOP(result));
+
+ pic16_emitcode("","%05d_DS_:",tlbl1->key+100);
+ pic16_emitcode("djnz","b,%05d_DS_",tlbl->key+100);
+
+release:
+ pic16_freeAsmop(left,NULL,ic,TRUE);
+ pic16_freeAsmop (right,NULL,ic,TRUE);
+ pic16_freeAsmop(result,NULL,ic,TRUE);
+}
+#endif
+
+#if (USE_GENERIC_SIGNED_SHIFT)
+/*-----------------------------------------------------------------*/
+/* genGenericShift - generates code for left or right shifting */
+/*-----------------------------------------------------------------*/
+static void genGenericShift (iCode *ic, int isShiftLeft) {
+ operand *left,*right, *result;
+ int offset;
+ int sign, signedCount;
+ symbol *label_complete, *label_loop_pos, *label_loop_neg, *label_negative;
+ PIC_OPCODE pos_shift, neg_shift;
+
+ FENTRY;
+
+ right = IC_RIGHT(ic);
+ left = IC_LEFT(ic);
+ result = IC_RESULT(ic);
+
+ pic16_aopOp(right,ic,FALSE);
+ pic16_aopOp(left,ic,FALSE);
+ pic16_aopOp(result,ic,TRUE);
+
+ sign = !SPEC_USIGN(operandType (left));
+ signedCount = !SPEC_USIGN(operandType (right));
+
+ /* if the shift count is known then do it
+ as efficiently as possible */
+ if (AOP_TYPE(right) == AOP_LIT) {
+ long lit = (long)floatFromVal(AOP(right)->aopu.aop_lit);
+ if (signedCount && lit < 0) { lit = -lit; isShiftLeft = !isShiftLeft; }
+ // we should modify right->aopu.aop_lit here!
+ // Instead we use abs(shCount) in genXXXShiftLiteral()...
+ // lit > 8*size is handled in pic16_genXXXShiftLiteral()
+ if (isShiftLeft)
+ pic16_genLeftShiftLiteral (left,right,result,ic);
+ else
+ genRightShiftLiteral (left,right,result,ic, sign);
+
+ goto release;
+ } // if (right is literal)
+
+ /* shift count is unknown then we have to form a loop.
+ * 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 )
+ * Note: we perform arithmetic shifts if the left operand is
+ * signed and we do an (effective) right shift, i. e. we
+ * shift in the sign bit from the left. */
+
+ label_complete = newiTempLabel ( NULL );
+ label_loop_pos = newiTempLabel ( NULL );
+ label_loop_neg = NULL;
+ label_negative = NULL;
+ pos_shift = isShiftLeft ? POC_RLCF : POC_RRCF;
+ neg_shift = isShiftLeft ? POC_RRCF : POC_RLCF;
+
+ if (signedCount) {
+ // additional labels needed
+ label_loop_neg = newiTempLabel ( NULL );
+ label_negative = newiTempLabel ( NULL );
+ } // if
+
+ // copy source to result -- this will effectively truncate the left operand to the size of result!
+ // (e.g. char c = 0x100 << -3 will become c = 0x00 >> 3 == 0x00 instad of 0x20)
+ // This is fine, as it only occurs for left shifting with negative count which is not standardized!
+ for (offset=0; offset < min(AOP_SIZE(left), AOP_SIZE(result)); offset++) {
+ pic16_mov2f (AOP(result),AOP(left), offset);
+ } // for
+
+ // if result is longer than left, fill with zeros (or sign)
+ if (AOP_SIZE(left) < AOP_SIZE(result)) {
+ if (sign && AOP_SIZE(left) > 0) {
+ // shift signed operand -- fill with sign
+ pic16_emitpcode (POC_CLRF, pic16_popCopyReg (&pic16_pc_wreg));
+ pic16_emitpcode (POC_BTFSC, pic16_popCopyGPR2Bit(pic16_popGet(AOP(result), AOP_SIZE(left)-1), 7));
+ pic16_emitpcode (POC_MOVLW, pic16_popGetLit (0xFF));
+ for (offset=AOP_SIZE(left); offset < AOP_SIZE(result); offset++) {
+ pic16_emitpcode (POC_MOVWF, pic16_popGet (AOP(result), offset));
+ } // for
+ } else {
+ // shift unsigned operand -- fill result with zeros
+ for (offset=AOP_SIZE(left); offset < AOP_SIZE(result); offset++) {
+ pic16_emitpcode (POC_CLRF, pic16_popGet (AOP(result), offset));
+ } // for
+ }
+ } // if (size mismatch)
+
+ pic16_mov2w (AOP(right), 0);
+ pic16_emitpcode (POC_BZ, pic16_popGetLabel (label_complete->key));
+ if (signedCount) pic16_emitpcode (POC_BN, pic16_popGetLabel (label_negative->key));
+
+#if 0
+ // perform a shift by one (shift count is positive)
+ // cycles used for shifting {unsigned,signed} values on n bytes by [unsigned,signed] shift count c>0:
+ // 2n+[2,3]+({1,3}+n+3)c-2+[0,2]=({4,6}+n)c+2n+[0,3] ({5,7}c+[2,5] / {6,8}c+[4, 7] / {8,10}c+[ 8,11])
+ pic16_emitpLabel (label_loop_pos->key);
+ emitCLRC;
+ if (sign && (pos_shift == POC_RRCF)) {
+ pic16_emitpcode (POC_BTFSC, pic16_popCopyGPR2Bit(pic16_popGet(AOP(result), AOP_SIZE(result)-1), 7));
+ emitSETC;
+ } // if
+ genMultiAsm (pos_shift, result, AOP_SIZE(result), pos_shift == POC_RRCF);
+ pic16_emitpcode (POC_DECFSZ, pic16_popCopyReg (&pic16_pc_wreg));
+ pic16_emitpcode (POC_BRA, pic16_popGetLabel (label_loop_pos->key));
+#else
+ // perform a shift by one (shift count is positive)
+ // cycles used for shifting {unsigned,signed} values on n bytes by [unsigned,signed] shift count c>0:
+ // 2n+[2,3]+2+({0,2}+n+3)c-1+[0,2]=({3,5}+n)c+2n+[3,6] ({4,6}c+[5,8] / {5,7}c+[7,10] / {7, 9}c+[11,14])
+ // This variant is slower for 0<c<3, equally fast for c==3, and faster for 3<c.
+ pic16_emitpcode (POC_NEGF, pic16_popCopyReg (&pic16_pc_wreg));
+ emitCLRC;
+ pic16_emitpLabel (label_loop_pos->key);
+ if (sign && (pos_shift == POC_RRCF)) {
+ pic16_emitpcode (POC_BTFSC, pic16_popCopyGPR2Bit(pic16_popGet(AOP(result), AOP_SIZE(result)-1), 7));
+ emitSETC;
+ } // if
+ genMultiAsm (pos_shift, result, AOP_SIZE(result), pos_shift == POC_RRCF);
+ //pic16_emitpcode (POC_INCF, pic16_popCopyReg (&pic16_pc_wreg)); // gpsim does not like this...
+ pic16_emitpcode (POC_ADDLW, pic16_popGetLit (0x01));
+ pic16_emitpcode (POC_BNC, pic16_popGetLabel (label_loop_pos->key));
+#endif
+
+ if (signedCount) {
+ pic16_emitpcode (POC_BRA, pic16_popGetLabel (label_complete->key));
+
+ pic16_emitpLabel (label_negative->key);
+ // perform a shift by -1 (shift count is negative)
+ // 2n+4+1+({0,2}+n+3)*c-1=({3,5}+n)c+2n+4 ({4,6}c+6 / {5,7}c+8 / {7,9}c+12)
+ emitCLRC;
+ pic16_emitpLabel (label_loop_neg->key);
+ if (sign && (neg_shift == POC_RRCF)) {
+ pic16_emitpcode (POC_BTFSC, pic16_popCopyGPR2Bit(pic16_popGet(AOP(result), AOP_SIZE(result)-1), 7));
+ emitSETC;
+ } // if
+ genMultiAsm (neg_shift, result, AOP_SIZE(result), neg_shift == POC_RRCF);
+ //pic16_emitpcode (POC_INCF, pic16_popCopyReg (&pic16_pc_wreg)); // gpsim does not like this...
+ pic16_emitpcode (POC_ADDLW, pic16_popGetLit (0x01));
+ pic16_emitpcode (POC_BNC, pic16_popGetLabel (label_loop_neg->key));
+ } // if (signedCount)
+
+ pic16_emitpLabel (label_complete->key);
+
+release:
+ pic16_freeAsmop (right,NULL,ic,TRUE);
+ pic16_freeAsmop(left,NULL,ic,TRUE);
+ pic16_freeAsmop(result,NULL,ic,TRUE);
+}
+
+static void genLeftShift (iCode *ic) {
+ genGenericShift (ic, 1);
+}
+
+static void genRightShift (iCode *ic) {
+ genGenericShift (ic, 0);
+}
+#endif
+
+
+/* load FSR0 with address of/from op according to is_LitOp() or if lit is 1 */
+void pic16_loadFSR0(operand *op, int lit)
+{
+ if((IS_SYMOP(op) && OP_SYMBOL(op)->remat) || is_LitOp( op )) {
+ if (AOP_TYPE(op) == AOP_LIT) {
+ /* handle 12 bit integers correctly */
+ unsigned int val = (unsigned int)floatFromVal(AOP(op)->aopu.aop_lit);
+ if ((val & 0x0fff) != val) {
+ fprintf (stderr, "WARNING: Accessing memory at 0x%x truncated to 0x%x.\n",
+ val, (val & 0x0fff) );
+ val &= 0x0fff;