+ pic16_aopOp(right,ic,FALSE);
+
+ /* if the shift count is known then do it
+ as efficiently as possible */
+ if (AOP_TYPE(right) == AOP_LIT) {
+ genRightShiftLiteral (left,right,result,ic, 0);
+ return ;
+ }
+
+ /* shift count is unknown then we have to form
+ a loop get the loop count in B : Note: we take
+ only the lower order byte since shifting
+ more that 32 bits make no sense anyway, ( the
+ largest size of an object can be only 32 bits ) */
+
+ pic16_emitcode("mov","b,%s",pic16_aopGet(AOP(right),0,FALSE,FALSE));
+ pic16_emitcode("inc","b");
+ pic16_aopOp(left,ic,FALSE);
+ pic16_aopOp(result,ic,FALSE);
+
+ /* now move the left to the result if they are not the
+ same */
+ if (!pic16_sameRegs(AOP(left),AOP(result)) &&
+ AOP_SIZE(result) > 1) {
+
+ size = AOP_SIZE(result);
+ offset=0;
+ while (size--) {
+ 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
+ pic16_aopPut(AOP(result),l,offset);
+ offset++;
+ }
+ }
+
+ tlbl = newiTempLabel(NULL);
+ tlbl1= newiTempLabel(NULL);
+ size = AOP_SIZE(result);
+ offset = size - 1;
+
+ /* if it is only one byte then */
+ if (size == 1) {
+
+ 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) ulFromVal (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) ulFromVal (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;
+ }
+ pic16_emitpcode(POC_LFSR, pic16_popGetLit2(0, pic16_popGetLit12(val)));
+ } else {
+ pic16_emitpcode(POC_LFSR, pic16_popGetLit2(0, pic16_popGet(AOP(op), 0)));
+ }
+ } else {
+ assert (!IS_SYMOP(op) || !OP_SYMBOL(op)->remat);
+ // set up FSR0 with address of result
+ pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(op),0), pic16_popCopyReg(&pic16_pc_fsr0l)));
+ pic16_emitpcode(POC_MOVFF, pic16_popGet2p(pic16_popGet(AOP(op),1), pic16_popCopyReg(&pic16_pc_fsr0h)));
+ }
+}
+
+/*----------------------------------------------------------------*/
+/* pic16_derefPtr - move one byte from the location ptr points to */
+/* to WREG (doWrite == 0) or one byte from WREG */
+/* to the location ptr points to (doWrite != 0) */
+/*----------------------------------------------------------------*/
+static void pic16_derefPtr (operand *ptr, int p_type, int doWrite, int *fsr0_setup)
+{
+ if (!IS_PTR(operandType(ptr)))
+ {
+ if (doWrite) pic16_emitpcode (POC_MOVWF, pic16_popGet (AOP(ptr), 0));
+ else pic16_mov2w (AOP(ptr), 0);
+ return;
+ }
+
+ //assert (IS_DECL(operandType(ptr)) && (p_type == DCL_TYPE(operandType(ptr))));
+ /* We might determine pointer type right here: */
+ p_type = DCL_TYPE(operandType(ptr));
+
+ switch (p_type) {
+ case FPOINTER:
+ case POINTER:
+ if (!fsr0_setup || !*fsr0_setup)
+ {
+ pic16_loadFSR0( ptr, 0 );
+ if (fsr0_setup) *fsr0_setup = 1;
+ }
+ if (doWrite)
+ pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_indf0));
+ else
+ pic16_emitpcode(POC_MOVFW, pic16_popCopyReg(&pic16_pc_indf0));
+ break;
+
+ case GPOINTER:
+ if (AOP(ptr)->aopu.aop_reg[2]) {
+ if (doWrite) pic16_emitpcode (POC_MOVWF, pic16_popCopyReg(pic16_stack_postdec));
+ // prepare call to __gptrget1, this is actually genGenPointerGet(result, WREG, ?ic?)
+ mov2fp(pic16_popCopyReg(&pic16_pc_fsr0l), AOP(ptr), 0);
+ mov2fp(pic16_popCopyReg(&pic16_pc_prodl), AOP(ptr), 1);
+ pic16_mov2w(AOP(ptr), 2);
+ pic16_callGenericPointerRW(doWrite, 1);
+ } else {
+ // data pointer (just 2 byte given)
+ if (!fsr0_setup || !*fsr0_setup)
+ {
+ pic16_loadFSR0( ptr, 0 );
+ if (fsr0_setup) *fsr0_setup = 1;
+ }
+ if (doWrite)
+ pic16_emitpcode(POC_MOVWF, pic16_popCopyReg(&pic16_pc_indf0));
+ else
+ pic16_emitpcode(POC_MOVFW, pic16_popCopyReg(&pic16_pc_indf0));
+ }
+ break;
+
+ default:
+ assert (0 && "invalid pointer type specified");
+ break;
+ }
+}
+
+/*-----------------------------------------------------------------*/
+/* genUnpackBits - generates code for unpacking bits */
+/*-----------------------------------------------------------------*/
+static void genUnpackBits (operand *result, operand *left, char *rname, int ptype)
+{
+ int shCnt ;
+ sym_link *etype, *letype;
+ int blen=0, bstr=0;
+ int lbstr;
+ int same;
+ pCodeOp *op;