1 /*-------------------------------------------------------------------------
2 genarith.c - source file for code generation - arithmetic
4 Written By - Sandeep Dutta . sandeep.dutta@usa.net (1998)
5 and - Jean-Louis VERN.jlvern@writeme.com (1999)
6 Bug Fixes - Wojciech Stryjewski wstryj1@tiger.lsu.edu (1999 v2.1.9a)
7 PIC port - Scott Dattalo scott@dattalo.com (2000)
9 This program is free software; you can redistribute it and/or modify it
10 under the terms of the GNU General Public License as published by the
11 Free Software Foundation; either version 2, or (at your option) any
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 In other words, you are welcome to use, share and improve this program.
24 You are forbidden to forbid anyone else to use, share and improve
25 what you give them. Help stamp out software-hoarding!
28 000123 mlh Moved aopLiteral to SDCCglue.c to help the split
29 Made everything static
30 -------------------------------------------------------------------------*/
36 #include "SDCCglobl.h"
39 #if defined(_MSC_VER) && (_MSC_VER < 1300)
40 #define __FUNCTION__ __FILE__
44 #include "SDCCpeeph.h"
50 #define BYTEofLONG(l,b) ( (l>> (b<<3)) & 0xff)
51 void DEBUGpic14_AopType(int line_no, operand *left, operand *right, operand *result);
53 const char *AopType(short type)
100 void DebugAop(asmop *aop)
104 printf("%s\n",AopType(aop->type));
105 printf(" current offset: %d\n",aop->coff);
106 printf(" size: %d\n",aop->size);
110 printf(" name: %s\n",aop->aopu.aop_lit->name);
113 printf(" name: %s\n",aop->aopu.aop_reg[0]->name);
117 printf(" name: %s\n",aop->aopu.aop_dir);
124 printf("not supported\n");
127 printf(" Stack offset: %d\n",aop->aopu.aop_stk);
130 printf(" immediate: %s\n",aop->aopu.aop_immd);
133 printf(" aop_str[0]: %s\n",aop->aopu.aop_str[0]);
136 //printpCode(stdout,aop->aopu.pcop);
141 const char *pCodeOpType( pCodeOp *pcop)
160 case PO_GPR_REGISTER:
161 return "PO_GPR_REGISTER";
163 return "PO_GPR_POINTER";
167 return "PO_GPR_TEMP";
168 case PO_SFR_REGISTER:
169 return "PO_SFR_REGISTER";
177 return "PO_IMMEDIATE";
193 return "BAD PO_TYPE";
196 /*-----------------------------------------------------------------*/
197 /* genPlusIncr :- does addition with increment if possible */
198 /*-----------------------------------------------------------------*/
199 bool genPlusIncr (iCode *ic)
201 unsigned int icount ;
202 unsigned int size = pic14_getDataSize(IC_RESULT(ic));
205 DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__);
206 DEBUGpic14_emitcode ("; ","result %s, left %s, right %s",
207 AopType(AOP_TYPE(IC_RESULT(ic))),
208 AopType(AOP_TYPE(IC_LEFT(ic))),
209 AopType(AOP_TYPE(IC_RIGHT(ic))));
211 /* will try to generate an increment */
212 /* if the right side is not a literal
214 if (AOP_TYPE(IC_RIGHT(ic)) != AOP_LIT)
217 DEBUGpic14_emitcode ("; ","%s %d",__FUNCTION__,__LINE__);
218 /* if the literal value of the right hand side
219 is greater than 1 then it is faster to add */
220 if ((icount = (unsigned int) floatFromVal (AOP(IC_RIGHT(ic))->aopu.aop_lit)) > 2)
223 /* if increment 16 bits in register */
224 if (pic14_sameRegs(AOP(IC_LEFT(ic)), AOP(IC_RESULT(ic))) &&
229 emitpcode(POC_INCF, popGet(AOP(IC_RESULT(ic)),LSB));
230 //pic14_emitcode("incf","%s,f",aopGet(AOP(IC_RESULT(ic)),LSB,FALSE,FALSE));
234 emitpcode(POC_INCF, popGet(AOP(IC_RESULT(ic)),offset++));
235 //pic14_emitcode(" incf","%s,f",aopGet(AOP(IC_RESULT(ic)),offset++,FALSE,FALSE));
241 DEBUGpic14_emitcode ("; ","%s %d",__FUNCTION__,__LINE__);
242 /* if left is in accumulator - probably a bit operation*/
243 if( strcmp(aopGet(AOP(IC_LEFT(ic)),0,FALSE,FALSE),"a") &&
244 (AOP_TYPE(IC_RESULT(ic)) == AOP_CRY) ) {
246 emitpcode(POC_BCF, popGet(AOP(IC_RESULT(ic)),0));
247 pic14_emitcode("bcf","(%s >> 3), (%s & 7)",
248 AOP(IC_RESULT(ic))->aopu.aop_dir,
249 AOP(IC_RESULT(ic))->aopu.aop_dir);
251 emitpcode(POC_XORLW,popGetLit(1));
252 //pic14_emitcode("xorlw","1");
254 emitpcode(POC_ANDLW,popGetLit(1));
255 //pic14_emitcode("andlw","1");
258 emitpcode(POC_BSF, popGet(AOP(IC_RESULT(ic)),0));
259 pic14_emitcode("bsf","(%s >> 3), (%s & 7)",
260 AOP(IC_RESULT(ic))->aopu.aop_dir,
261 AOP(IC_RESULT(ic))->aopu.aop_dir);
268 /* if the sizes are greater than 1 then we cannot */
269 if (AOP_SIZE(IC_RESULT(ic)) > 1 ||
270 AOP_SIZE(IC_LEFT(ic)) > 1 )
273 /* If we are incrementing the same register by two: */
275 if (pic14_sameRegs(AOP(IC_LEFT(ic)), AOP(IC_RESULT(ic))) ) {
278 emitpcode(POC_INCF, popGet(AOP(IC_RESULT(ic)),0));
279 //pic14_emitcode("incf","%s,f",aopGet(AOP(IC_RESULT(ic)),0,FALSE,FALSE));
284 DEBUGpic14_emitcode ("; ","couldn't increment ");
289 /*-----------------------------------------------------------------*/
290 /* pic14_outBitAcc - output a bit in acc */
291 /*-----------------------------------------------------------------*/
292 void pic14_outBitAcc(operand *result)
294 symbol *tlbl = newiTempLabel(NULL);
295 /* if the result is a bit */
297 DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__);
299 if (AOP_TYPE(result) == AOP_CRY){
300 aopPut(AOP(result),"a",0);
303 pic14_emitcode("jz","%05d_DS_",tlbl->key+100);
304 pic14_emitcode("mov","a,#01");
305 pic14_emitcode("","%05d_DS_:",tlbl->key+100);
306 pic14_outAcc(result);
310 /*-----------------------------------------------------------------*/
311 /* genPlusBits - generates code for addition of two bits */
312 /*-----------------------------------------------------------------*/
313 void genPlusBits (iCode *ic)
317 DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__);
319 DEBUGpic14_emitcode ("; ","result %s, left %s, right %s",
320 AopType(AOP_TYPE(IC_RESULT(ic))),
321 AopType(AOP_TYPE(IC_LEFT(ic))),
322 AopType(AOP_TYPE(IC_RIGHT(ic))));
324 The following block of code will add two bits.
325 Note that it'll even work if the destination is
326 the carry (C in the status register).
327 It won't work if the 'Z' bit is a source or destination.
330 /* If the result is stored in the accumulator (w) */
331 //if(strcmp(aopGet(AOP(IC_RESULT(ic)),0,FALSE,FALSE),"a") == 0 ) {}
332 switch(AOP_TYPE(IC_RESULT(ic))) {
334 emitpcode(POC_CLRW, NULL);
335 emitpcode(POC_BTFSC, popGet(AOP(IC_RIGHT(ic)),0));
336 emitpcode(POC_XORLW, popGetLit(1));
337 emitpcode(POC_BTFSC, popGet(AOP(IC_LEFT(ic)),0));
338 emitpcode(POC_XORLW, popGetLit(1));
340 pic14_emitcode("clrw","");
341 pic14_emitcode("btfsc","(%s >> 3), (%s & 7)",
342 AOP(IC_RIGHT(ic))->aopu.aop_dir,
343 AOP(IC_RIGHT(ic))->aopu.aop_dir);
344 pic14_emitcode("xorlw","1");
345 pic14_emitcode("btfsc","(%s >> 3), (%s & 7)",
346 AOP(IC_LEFT(ic))->aopu.aop_dir,
347 AOP(IC_LEFT(ic))->aopu.aop_dir);
348 pic14_emitcode("xorlw","1");
351 emitpcode(POC_MOVLW, popGetLit(0));
352 emitpcode(POC_BTFSC, popGet(AOP(IC_RIGHT(ic)),0));
353 emitpcode(POC_XORLW, popGetLit(1));
354 emitpcode(POC_BTFSC, popGet(AOP(IC_LEFT(ic)),0));
355 emitpcode(POC_XORLW, popGetLit(1));
356 emitpcode(POC_MOVWF, popGet(AOP(IC_RESULT(ic)),0));
359 if(pic14_sameRegs(AOP(IC_RESULT(ic)),AOP(IC_LEFT(ic)))) {
366 if(pic14_sameRegs(AOP(IC_RESULT(ic)),AOP(IC_LEFT(ic)))) {
367 DebugAop(AOP(IC_LEFT(ic)));
368 emitpcode(POC_MOVLW, popGet(AOP(IC_LEFT(ic)),0));
369 emitpcode(POC_BTFSC, popGet(AOP(IC_RIGHT(ic)),0));
370 emitpcode(POC_XORWF, popGet(AOP(IC_LEFT(ic)),0));
372 emitpcode(POC_MOVLW, popGet(AOP(IC_RESULT(ic)),0));
373 emitpcode(POC_BCF, popGet(AOP(IC_RESULT(ic)),0));
374 emitpcode(POC_BTFSC, popGet(AOP(IC_RIGHT(ic)),0));
375 emitpcode(POC_XORWF, popGet(AOP(IC_RESULT(ic)),0));
376 emitpcode(POC_BTFSC, popGet(AOP(IC_LEFT(ic)),0));
377 emitpcode(POC_XORWF, popGet(AOP(IC_RESULT(ic)),0));
381 pic14_emitcode("movlw","(1 << (%s & 7))",
382 AOP(IC_RESULT(ic))->aopu.aop_dir,
383 AOP(IC_RESULT(ic))->aopu.aop_dir);
384 pic14_emitcode("bcf","(%s >> 3), (%s & 7)",
385 AOP(IC_RESULT(ic))->aopu.aop_dir,
386 AOP(IC_RESULT(ic))->aopu.aop_dir);
387 pic14_emitcode("btfsc","(%s >> 3), (%s & 7)",
388 AOP(IC_RIGHT(ic))->aopu.aop_dir,
389 AOP(IC_RIGHT(ic))->aopu.aop_dir);
390 pic14_emitcode("xorwf","(%s >>3),f",
391 AOP(IC_RESULT(ic))->aopu.aop_dir);
392 pic14_emitcode("btfsc","(%s >> 3), (%s & 7)",
393 AOP(IC_LEFT(ic))->aopu.aop_dir,
394 AOP(IC_LEFT(ic))->aopu.aop_dir);
395 pic14_emitcode("xorwf","(%s>>3),f",
396 AOP(IC_RESULT(ic))->aopu.aop_dir);
403 /* This is the original version of this code.
405 * This is being kept around for reference,
406 * because I am not entirely sure I got it right...
408 static void adjustArithmeticResult(iCode *ic)
410 if (AOP_SIZE(IC_RESULT(ic)) == 3 &&
411 AOP_SIZE(IC_LEFT(ic)) == 3 &&
412 !pic14_sameRegs(AOP(IC_RESULT(ic)),AOP(IC_LEFT(ic))))
413 aopPut(AOP(IC_RESULT(ic)),
414 aopGet(AOP(IC_LEFT(ic)),2,FALSE,FALSE),
417 if (AOP_SIZE(IC_RESULT(ic)) == 3 &&
418 AOP_SIZE(IC_RIGHT(ic)) == 3 &&
419 !pic14_sameRegs(AOP(IC_RESULT(ic)),AOP(IC_RIGHT(ic))))
420 aopPut(AOP(IC_RESULT(ic)),
421 aopGet(AOP(IC_RIGHT(ic)),2,FALSE,FALSE),
424 if (AOP_SIZE(IC_RESULT(ic)) == 3 &&
425 AOP_SIZE(IC_LEFT(ic)) < 3 &&
426 AOP_SIZE(IC_RIGHT(ic)) < 3 &&
427 !pic14_sameRegs(AOP(IC_RESULT(ic)),AOP(IC_LEFT(ic))) &&
428 !pic14_sameRegs(AOP(IC_RESULT(ic)),AOP(IC_RIGHT(ic)))) {
430 sprintf(buffer,"#%d",pointerCode(getSpec(operandType(IC_LEFT(ic)))));
431 aopPut(AOP(IC_RESULT(ic)),buffer,2);
435 /* This is the pure and virtuous version of this code.
436 * I'm pretty certain it's right, but not enough to toss the old
439 static void adjustArithmeticResult(iCode *ic)
441 if (opIsGptr(IC_RESULT(ic)) &&
442 opIsGptr(IC_LEFT(ic)) &&
443 !pic14_sameRegs(AOP(IC_RESULT(ic)),AOP(IC_LEFT(ic))))
445 aopPut(AOP(IC_RESULT(ic)),
446 aopGet(AOP(IC_LEFT(ic)), GPTRSIZE - 1,FALSE,FALSE),
450 if (opIsGptr(IC_RESULT(ic)) &&
451 opIsGptr(IC_RIGHT(ic)) &&
452 !pic14_sameRegs(AOP(IC_RESULT(ic)),AOP(IC_RIGHT(ic))))
454 aopPut(AOP(IC_RESULT(ic)),
455 aopGet(AOP(IC_RIGHT(ic)),GPTRSIZE - 1,FALSE,FALSE),
459 if (opIsGptr(IC_RESULT(ic)) &&
460 AOP_SIZE(IC_LEFT(ic)) < GPTRSIZE &&
461 AOP_SIZE(IC_RIGHT(ic)) < GPTRSIZE &&
462 !pic14_sameRegs(AOP(IC_RESULT(ic)),AOP(IC_LEFT(ic))) &&
463 !pic14_sameRegs(AOP(IC_RESULT(ic)),AOP(IC_RIGHT(ic)))) {
465 sprintf(buffer,"#%d",pointerCode(getSpec(operandType(IC_LEFT(ic)))));
466 aopPut(AOP(IC_RESULT(ic)),buffer,GPTRSIZE - 1);
471 /*-----------------------------------------------------------------*/
472 /* genAddlit - generates code for addition */
473 /*-----------------------------------------------------------------*/
474 static void genAddLit2byte (operand *result, int offr, int lit)
482 emitpcode(POC_INCF, popGet(AOP(result),offr));
485 emitpcode(POC_DECF, popGet(AOP(result),offr));
488 emitpcode(POC_MOVLW,popGetLit(lit&0xff));
489 emitpcode(POC_ADDWF,popGet(AOP(result),offr));
494 static void emitMOVWF(operand *reg, int offset)
500 if (AOP_TYPE(reg) == AOP_ACC) {
501 DEBUGpic14_emitcode ("; ***","%s %d ignoring mov into W",__FUNCTION__,__LINE__);
505 emitpcode(POC_MOVWF, popGet(AOP(reg),offset));
509 static void genAddLit (iCode *ic, int lit)
519 DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__);
523 result = IC_RESULT(ic);
524 same = pic14_sameRegs(AOP(left), AOP(result));
525 size = pic14_getDataSize(result);
529 /* Handle special cases first */
531 genAddLit2byte (result, 0, lit);
534 int hi = 0xff & (lit >> 8);
541 DEBUGpic14_emitcode ("; hi = 0","%s %d",__FUNCTION__,__LINE__);
546 emitpcode(POC_INCF, popGet(AOP(result),0));
548 emitpcode(POC_INCF, popGet(AOP(result),MSB16));
551 emitpcode(POC_DECF, popGet(AOP(result),0));
552 emitpcode(POC_INCFSZW, popGet(AOP(result),0));
553 emitpcode(POC_INCF, popGet(AOP(result),MSB16));
557 emitpcode(POC_MOVLW,popGetLit(lit&0xff));
558 emitpcode(POC_ADDWF,popGet(AOP(result),0));
560 emitpcode(POC_INCF, popGet(AOP(result),MSB16));
568 DEBUGpic14_emitcode ("; hi = 1","%s %d",__FUNCTION__,__LINE__);
571 emitpcode(POC_INCF, popGet(AOP(result),MSB16));
574 emitpcode(POC_INCF, popGet(AOP(result),MSB16));
575 emitpcode(POC_INCF, popGet(AOP(result),0));
577 emitpcode(POC_INCF, popGet(AOP(result),MSB16));
579 case 0xff: /* 0x01ff */
580 emitpcode(POC_DECF, popGet(AOP(result),0));
581 emitpcode(POC_INCFSZW, popGet(AOP(result),0));
582 emitpcode(POC_INCF, popGet(AOP(result),MSB16));
583 emitpcode(POC_INCF, popGet(AOP(result),MSB16));
588 DEBUGpic14_emitcode ("; hi = ff","%s %d",__FUNCTION__,__LINE__);
592 emitpcode(POC_DECF, popGet(AOP(result),MSB16));
595 emitpcode(POC_INCFSZ, popGet(AOP(result),0));
596 emitpcode(POC_DECF, popGet(AOP(result),MSB16));
598 /* case 0xff: * 0xffff *
599 emitpcode(POC_INCFSZW, popGet(AOP(result),0,FALSE,FALSE));
600 emitpcode(POC_INCF, popGet(AOP(result),MSB16,FALSE,FALSE));
601 emitpcode(POC_DECF, popGet(AOP(result),0,FALSE,FALSE));
605 emitpcode(POC_MOVLW,popGetLit(lo));
606 emitpcode(POC_ADDWF,popGet(AOP(result),0));
608 emitpcode(POC_DECF, popGet(AOP(result),MSB16));
615 DEBUGpic14_emitcode ("; hi is generic","%d %s %d",hi,__FUNCTION__,__LINE__);
620 genAddLit2byte (result, MSB16, hi);
623 emitpcode(POC_MOVLW,popGetLit((hi+1)&0xff));
624 emitpcode(POC_INCFSZ, popGet(AOP(result),0));
625 emitpcode(POC_MOVLW,popGetLit(hi));
626 emitpcode(POC_ADDWF,popGet(AOP(result),MSB16));
628 /* case 0xff: * 0xHHff *
629 emitpcode(POC_MOVFW, popGet(AOP(result),0,FALSE,FALSE));
630 emitpcode(POC_DECF, popGet(AOP(result),MSB16,FALSE,FALSE));
631 emitpcode(POC_MOVLW,popGetLit(hi));
632 emitpcode(POC_ADDWF,popGet(AOP(result),MSB16,FALSE,FALSE));
634 */ default: /* 0xHHLL */
635 emitpcode(POC_MOVLW,popGetLit(lo));
636 emitpcode(POC_ADDWF, popGet(AOP(result),0));
637 emitpcode(POC_MOVLW,popGetLit(hi));
639 emitpcode(POC_MOVLW,popGetLit((hi+1) & 0xff));
640 emitpcode(POC_ADDWF,popGet(AOP(result),MSB16));
649 DEBUGpic14_emitcode ("; add lit to long","%s %d",__FUNCTION__,__LINE__);
652 lo = BYTEofLONG(lit,0);
660 emitpcode(POC_INCF, popGet(AOP(result),offset));
663 emitpcode(POC_RLFW, popGet(AOP(result),offset));
664 emitpcode(POC_ANDLW,popGetLit(1));
665 emitpcode(POC_ADDWF, popGet(AOP(result),offset));
667 default: /* carry_info = 3 */
669 emitpcode(POC_INCF, popGet(AOP(result),offset));
675 emitpcode(POC_MOVLW,popGetLit(lo));
680 emitpcode(POC_ADDWF, popGet(AOP(result),offset));
683 emitpcode(POC_MOVLW,popGetLit(lo));
688 emitpcode(POC_MOVLW,popGetLit(lo+1));
689 emitpcode(POC_ADDWF, popGet(AOP(result),offset));
694 /* no carry info from previous step */
695 /* this means this is the first time to add */
700 emitpcode(POC_INCF, popGet(AOP(result),offset));
704 emitpcode(POC_MOVLW,popGetLit(lo));
705 emitpcode(POC_ADDWF, popGet(AOP(result),offset));
707 carry_info = 3; /* Were adding only one byte and propogating the carry */
718 lo = BYTEofLONG(lit,0);
723 emitpcode(POC_INCF, popGet(AOP(result),0,FALSE,FALSE));
726 emitpcode(POC_MOVLW,popGetLit(lo));
727 emitpcode(POC_ADDWF, popGet(AOP(result),0,FALSE,FALSE));
730 emitpcode(POC_INCF, popGet(AOP(result),1,FALSE,FALSE));
732 emitpcode(POC_INCF, popGet(AOP(result),2,FALSE,FALSE));
734 emitpcode(POC_INCF, popGet(AOP(result),3,FALSE,FALSE));
743 DEBUGpic14_emitcode ("; left and result aren't same","%s %d",__FUNCTION__,__LINE__);
747 if(AOP_TYPE(left) == AOP_ACC) {
748 /* left addend is already in accumulator */
751 //emitpcode(POC_MOVWF, popGet(AOP(result),0,FALSE,FALSE));
755 emitpcode(POC_ADDLW, popGetLit(lit & 0xff));
756 //emitpcode(POC_MOVWF, popGet(AOP(result),0,FALSE,FALSE));
760 /* left addend is in a register */
763 emitpcode(POC_MOVFW, popGet(AOP(left),0));
764 emitMOVWF(result, 0);
765 //emitpcode(POC_MOVWF, popGet(AOP(result),0,FALSE,FALSE));
769 emitpcode(POC_INCFW, popGet(AOP(left),0));
770 //emitpcode(POC_MOVWF, popGet(AOP(result),0,FALSE,FALSE));
774 emitpcode(POC_DECFW, popGet(AOP(left),0));
775 //emitpcode(POC_MOVWF, popGet(AOP(result),0,FALSE,FALSE));
779 emitpcode(POC_MOVLW, popGetLit(lit & 0xff));
780 emitpcode(POC_ADDFW, popGet(AOP(left),0));
781 //emitpcode(POC_MOVWF, popGet(AOP(result),0,FALSE,FALSE));
789 /* left is not the accumulator */
791 emitpcode(POC_MOVLW, popGetLit(lit & 0xff));
792 emitpcode(POC_ADDFW, popGet(AOP(left),0));
794 emitpcode(POC_MOVFW, popGet(AOP(left),0));
795 /* We don't know the state of the carry bit at this point */
798 //emitpcode(POC_MOVWF, popGet(AOP(result),0,FALSE,FALSE));
805 /* The ls byte of the lit must've been zero - that
806 means we don't have to deal with carry */
808 emitpcode(POC_MOVLW, popGetLit(lit & 0xff));
809 emitpcode(POC_ADDFW, popGet(AOP(left),offset));
810 emitpcode(POC_MOVWF, popGet(AOP(left),offset));
815 emitpcode(POC_MOVLW, popGetLit(lit & 0xff));
816 //emitpcode(POC_MOVWF, popGet(AOP(result),offset,FALSE,FALSE));
817 emitMOVWF(result,offset);
818 emitpcode(POC_MOVFW, popGet(AOP(left),offset));
820 emitpcode(POC_INCFSZW,popGet(AOP(left),offset));
821 emitpcode(POC_ADDWF, popGet(AOP(result),offset));
825 emitpcode(POC_CLRF, popGet(AOP(result),offset));
826 emitpcode(POC_RLF, popGet(AOP(result),offset));
827 emitpcode(POC_MOVFW, popGet(AOP(left),offset));
828 emitpcode(POC_ADDWF, popGet(AOP(result),offset));
836 /*-----------------------------------------------------------------*/
837 /* genPlus - generates code for addition */
838 /*-----------------------------------------------------------------*/
839 void genPlus (iCode *ic)
841 int size, offset = 0;
843 /* special cases :- */
844 DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__);
847 aopOp (IC_LEFT(ic),ic,FALSE);
848 aopOp (IC_RIGHT(ic),ic,FALSE);
849 aopOp (IC_RESULT(ic),ic,TRUE);
851 DEBUGpic14_AopType(__LINE__,IC_LEFT(ic),IC_RIGHT(ic),IC_RESULT(ic));
853 /* if literal, literal on the right or
854 if left requires ACC or right is already
857 if (AOP_TYPE(IC_LEFT(ic)) == AOP_LIT) {
858 operand *t = IC_RIGHT(ic);
859 IC_RIGHT(ic) = IC_LEFT(ic);
863 /* if both left & right are in bit space */
864 if (AOP_TYPE(IC_LEFT(ic)) == AOP_CRY &&
865 AOP_TYPE(IC_RIGHT(ic)) == AOP_CRY) {
870 /* if left in bit space & right literal */
871 if (AOP_TYPE(IC_LEFT(ic)) == AOP_CRY &&
872 AOP_TYPE(IC_RIGHT(ic)) == AOP_LIT) {
873 /* if result in bit space */
874 if(AOP_TYPE(IC_RESULT(ic)) == AOP_CRY){
875 if((unsigned long)floatFromVal(AOP(IC_RIGHT(ic))->aopu.aop_lit) != 0L) {
876 emitpcode(POC_MOVLW, popGet(AOP(IC_RESULT(ic)),0));
877 if (!pic14_sameRegs(AOP(IC_LEFT(ic)), AOP(IC_RESULT(ic))) )
878 emitpcode(POC_BTFSC, popGet(AOP(IC_LEFT(ic)),0));
879 emitpcode(POC_XORWF, popGet(AOP(IC_RESULT(ic)),0));
882 size = pic14_getDataSize(IC_RESULT(ic));
884 MOVA(aopGet(AOP(IC_RIGHT(ic)),offset,FALSE,FALSE));
885 pic14_emitcode("addc","a,#00 ;%d",__LINE__);
886 aopPut(AOP(IC_RESULT(ic)),"a",offset++);
892 /* if I can do an increment instead
893 of add then GOOD for ME */
894 if (genPlusIncr (ic) == TRUE)
897 size = pic14_getDataSize(IC_RESULT(ic));
899 if(AOP(IC_RIGHT(ic))->type == AOP_LIT) {
900 /* Add a literal to something else */
902 unsigned lit = (unsigned) floatFromVal(AOP(IC_RIGHT(ic))->aopu.aop_lit);
906 DEBUGpic14_emitcode(";","adding lit to something. size %d",size);
911 } else if(AOP_TYPE(IC_RIGHT(ic)) == AOP_CRY) {
913 pic14_emitcode(";bitadd","right is bit: %s",aopGet(AOP(IC_RIGHT(ic)),0,FALSE,FALSE));
914 pic14_emitcode(";bitadd","left is bit: %s",aopGet(AOP(IC_LEFT(ic)),0,FALSE,FALSE));
915 pic14_emitcode(";bitadd","result is bit: %s",aopGet(AOP(IC_RESULT(ic)),0,FALSE,FALSE));
917 /* here we are adding a bit to a char or int */
919 if (pic14_sameRegs(AOP(IC_LEFT(ic)), AOP(IC_RESULT(ic))) ) {
921 emitpcode(POC_BTFSC , popGet(AOP(IC_RIGHT(ic)),0));
922 emitpcode(POC_INCF , popGet(AOP(IC_RESULT(ic)),0));
924 pic14_emitcode("btfsc","(%s >> 3), (%s & 7)",
925 AOP(IC_RIGHT(ic))->aopu.aop_dir,
926 AOP(IC_RIGHT(ic))->aopu.aop_dir);
927 pic14_emitcode(" incf","%s,f", aopGet(AOP(IC_RESULT(ic)),0,FALSE,FALSE));
930 if(AOP_TYPE(IC_LEFT(ic)) == AOP_ACC) {
931 emitpcode(POC_BTFSC , popGet(AOP(IC_RIGHT(ic)),0));
932 emitpcode(POC_XORLW , popGetLit(1));
934 pic14_emitcode("btfsc","(%s >> 3), (%s & 7)",
935 AOP(IC_RIGHT(ic))->aopu.aop_dir,
936 AOP(IC_RIGHT(ic))->aopu.aop_dir);
937 pic14_emitcode(" xorlw","1");
939 emitpcode(POC_MOVFW , popGet(AOP(IC_LEFT(ic)),0));
940 emitpcode(POC_BTFSC , popGet(AOP(IC_RIGHT(ic)),0));
941 emitpcode(POC_INCFW , popGet(AOP(IC_LEFT(ic)),0));
943 pic14_emitcode("movf","%s,w", aopGet(AOP(IC_LEFT(ic)),0,FALSE,FALSE));
944 pic14_emitcode("btfsc","(%s >> 3), (%s & 7)",
945 AOP(IC_RIGHT(ic))->aopu.aop_dir,
946 AOP(IC_RIGHT(ic))->aopu.aop_dir);
947 pic14_emitcode(" incf","%s,w", aopGet(AOP(IC_LEFT(ic)),0,FALSE,FALSE));
950 if(AOP_TYPE(IC_RESULT(ic)) != AOP_ACC) {
952 if(AOP_TYPE(IC_RESULT(ic)) == AOP_CRY) {
953 emitpcode(POC_ANDLW , popGetLit(1));
954 emitpcode(POC_BCF , popGet(AOP(IC_RESULT(ic)),0));
956 emitpcode(POC_BSF , popGet(AOP(IC_RESULT(ic)),0));
958 emitpcode(POC_MOVWF , popGet(AOP(IC_RESULT(ic)),0));
959 pic14_emitcode("movwf","%s", aopGet(AOP(IC_RESULT(ic)),0,FALSE,FALSE));
966 DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__);
967 if (pic14_sameRegs(AOP(IC_LEFT(ic)), AOP(IC_RESULT(ic))) ) {
969 emitpcode(POC_BTFSC, popGet(AOP(IC_RIGHT(ic)),0));
970 emitpcode(POC_INCF, popGet(AOP(IC_RESULT(ic)),0));
972 pic14_emitcode("clrz","");
974 pic14_emitcode("btfsc","(%s >> 3), (%s & 7)",
975 AOP(IC_RIGHT(ic))->aopu.aop_dir,
976 AOP(IC_RIGHT(ic))->aopu.aop_dir);
977 pic14_emitcode(" incf","%s,f", aopGet(AOP(IC_RESULT(ic)),0,FALSE,FALSE));
981 emitpcode(POC_MOVFW, popGet(AOP(IC_LEFT(ic)),0));
982 emitpcode(POC_BTFSC, popGet(AOP(IC_RIGHT(ic)),0));
983 emitpcode(POC_INCFW, popGet(AOP(IC_LEFT(ic)),0));
984 //emitpcode(POC_MOVWF, popGet(AOP(IC_RIGHT(ic)),0,FALSE,FALSE));
985 emitMOVWF(IC_RIGHT(ic),0);
987 pic14_emitcode("movf","%s,w", aopGet(AOP(IC_LEFT(ic)),0,FALSE,FALSE));
988 pic14_emitcode("btfsc","(%s >> 3), (%s & 7)",
989 AOP(IC_RIGHT(ic))->aopu.aop_dir,
990 AOP(IC_RIGHT(ic))->aopu.aop_dir);
991 pic14_emitcode(" incf","%s,w", aopGet(AOP(IC_LEFT(ic)),0,FALSE,FALSE));
992 pic14_emitcode("movwf","%s", aopGet(AOP(IC_RESULT(ic)),0,FALSE,FALSE));
998 emitpcode(POC_INCF, popGet(AOP(IC_RESULT(ic)),offset++));
999 //pic14_emitcode(" incf","%s,f", aopGet(AOP(IC_RIGHT(ic)),offset++,FALSE,FALSE));
1005 DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__);
1007 /* Add the first bytes */
1009 if(strcmp(aopGet(AOP(IC_LEFT(ic)),0,FALSE,FALSE),"a") == 0 ) {
1010 emitpcode(POC_ADDFW, popGet(AOP(IC_RIGHT(ic)),0));
1011 emitpcode(POC_MOVWF,popGet(AOP(IC_RESULT(ic)),0));
1014 if ( AOP_TYPE(IC_LEFT(ic)) == AOP_ACC) {
1015 emitpcode(POC_ADDFW, popGet(AOP(IC_RIGHT(ic)),0));
1016 if ( AOP_TYPE(IC_RESULT(ic)) != AOP_ACC)
1017 emitpcode(POC_MOVWF,popGet(AOP(IC_RESULT(ic)),0));
1020 emitpcode(POC_MOVFW,popGet(AOP(IC_RIGHT(ic)),0));
1022 if (pic14_sameRegs(AOP(IC_LEFT(ic)), AOP(IC_RESULT(ic))) )
1023 emitpcode(POC_ADDWF, popGet(AOP(IC_LEFT(ic)),0));
1025 PIC_OPCODE poc = POC_ADDFW;
1027 if (op_isLitLike (IC_LEFT (ic)))
1029 emitpcode(poc, popGetAddr(AOP(IC_LEFT(ic)),0,0));
1030 if ( AOP_TYPE(IC_RESULT(ic)) != AOP_ACC)
1031 emitpcode(POC_MOVWF,popGet(AOP(IC_RESULT(ic)),0));
1036 size = min( AOP_SIZE(IC_RESULT(ic)), AOP_SIZE(IC_RIGHT(ic))) - 1;
1041 if (pic14_sameRegs(AOP(IC_RIGHT(ic)), AOP(IC_RESULT(ic)))) {
1042 if (op_isLitLike (IC_LEFT(ic)))
1045 emitpcode(POC_MOVFW, popGet(AOP(IC_RIGHT(ic)),offset));
1047 emitpcode(POC_INCFSZW, popGet(AOP(IC_RIGHT(ic)),offset));
1048 emitpcode(POC_ADDLW, popGetAddr(AOP(IC_LEFT(ic)),offset,0));
1049 emitpcode(POC_MOVWF, popGet(AOP(IC_RESULT(ic)),offset));
1054 emitpcode(POC_MOVFW, popGet(AOP(IC_LEFT(ic)),offset));
1056 emitpcode(POC_INCFSZW, popGet(AOP(IC_LEFT(ic)),offset));
1057 emitpcode(POC_ADDWF, popGet(AOP(IC_RESULT(ic)),offset));
1062 PIC_OPCODE poc = POC_MOVFW;
1063 if (op_isLitLike (IC_LEFT(ic)))
1066 if (!pic14_sameRegs(AOP(IC_LEFT(ic)), AOP(IC_RESULT(ic))) ) {
1067 emitpcode(poc, popGetAddr(AOP(IC_LEFT(ic)),offset,0));
1068 emitpcode(POC_MOVWF, popGet(AOP(IC_RESULT(ic)),offset));
1070 emitpcode(POC_MOVFW, popGet(AOP(IC_RIGHT(ic)),offset));
1072 emitpcode(POC_INCFSZW, popGet(AOP(IC_RIGHT(ic)),offset));
1073 emitpcode(POC_ADDWF, popGet(AOP(IC_RESULT(ic)),offset));
1080 if (AOP_SIZE(IC_RESULT(ic)) > AOP_SIZE(IC_RIGHT(ic))) {
1081 int sign = !(SPEC_USIGN(getSpec(operandType(IC_LEFT(ic)))) |
1082 SPEC_USIGN(getSpec(operandType(IC_RIGHT(ic)))) );
1085 /* Need to extend result to higher bytes */
1086 size = AOP_SIZE(IC_RESULT(ic)) - AOP_SIZE(IC_RIGHT(ic)) - 1;
1088 /* First grab the carry from the lower bytes */
1089 if (AOP_SIZE(IC_LEFT(ic)) > AOP_SIZE(IC_RIGHT(ic))) {
1090 int leftsize = AOP_SIZE(IC_LEFT(ic)) - AOP_SIZE(IC_RIGHT(ic));
1091 PIC_OPCODE poc = POC_MOVFW;
1092 if (op_isLitLike (IC_LEFT(ic)))
1094 while(leftsize-- > 0) {
1095 emitpcode(poc, popGetAddr(AOP(IC_LEFT(ic)),offset,0));
1096 emitpcode(POC_MOVWF, popGet(AOP(IC_RESULT(ic)),offset));
1098 emitpcode(POC_INCF, popGet(AOP(IC_RESULT(ic)),offset));
1106 emitpcode(POC_CLRF, popGet(AOP(IC_RESULT(ic)),offset));
1107 emitpcode(POC_RLF, popGet(AOP(IC_RESULT(ic)),offset));
1111 if(sign && offset > 0 && offset < AOP_SIZE(IC_RESULT(ic))) {
1112 /* Now this is really horrid. Gotta check the sign of the addends and propogate
1115 emitpcode(POC_BTFSC, newpCodeOpBit(aopGet(AOP(IC_LEFT(ic)),offset-1,FALSE,FALSE),7,0));
1116 emitpcode(POC_DECF, popGet(AOP(IC_RESULT(ic)),offset));
1117 emitpcode(POC_BTFSC, newpCodeOpBit(aopGet(AOP(IC_RIGHT(ic)),offset-1,FALSE,FALSE),7,0));
1118 emitpcode(POC_DECF, popGet(AOP(IC_RESULT(ic)),offset));
1120 /* if chars or ints or being signed extended to longs: */
1122 emitpcode(POC_MOVLW, popGetLit(0));
1123 emitpcode(POC_BTFSC, newpCodeOpBit(aopGet(AOP(IC_RESULT(ic)),offset,FALSE,FALSE),7,0));
1124 emitpcode(POC_MOVLW, popGetLit(0xff));
1132 emitpcode(POC_MOVWF, popGet(AOP(IC_RESULT(ic)),offset));
1134 emitpcode(POC_CLRF, popGet(AOP(IC_RESULT(ic)),offset));
1141 //adjustArithmeticResult(ic);
1144 freeAsmop(IC_LEFT(ic),NULL,ic,(RESULTONSTACK(ic) ? FALSE : TRUE));
1145 freeAsmop(IC_RIGHT(ic),NULL,ic,(RESULTONSTACK(ic) ? FALSE : TRUE));
1146 freeAsmop(IC_RESULT(ic),NULL,ic,TRUE);
1149 /*-----------------------------------------------------------------*/
1150 /* genMinusDec :- does subtraction with decrement if possible */
1151 /*-----------------------------------------------------------------*/
1152 bool genMinusDec (iCode *ic)
1154 unsigned int icount ;
1155 unsigned int size = pic14_getDataSize(IC_RESULT(ic));
1158 DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__);
1159 /* will try to generate an increment */
1160 /* if the right side is not a literal
1162 if ((AOP_TYPE(IC_RIGHT(ic)) != AOP_LIT) ||
1163 (AOP_TYPE(IC_LEFT(ic)) == AOP_CRY) ||
1164 (AOP_TYPE(IC_RESULT(ic)) == AOP_CRY) )
1167 DEBUGpic14_emitcode ("; lit val","%d",(unsigned int) floatFromVal (AOP(IC_RIGHT(ic))->aopu.aop_lit));
1169 /* if the literal value of the right hand side
1170 is greater than 4 then it is not worth it */
1171 if ((icount = (unsigned int) floatFromVal (AOP(IC_RIGHT(ic))->aopu.aop_lit)) > 2)
1174 /* if decrement 16 bits in register */
1175 if (pic14_sameRegs(AOP(IC_LEFT(ic)), AOP(IC_RESULT(ic))) &&
1180 emitpcode(POC_DECF, popGet(AOP(IC_RESULT(ic)),LSB));
1181 emitpcode(POC_INCFSZW, popGet(AOP(IC_RESULT(ic)),LSB));
1182 emitpcode(POC_INCF, popGet(AOP(IC_RESULT(ic)),MSB16));
1183 emitpcode(POC_DECF, popGet(AOP(IC_RESULT(ic)),MSB16));
1185 pic14_emitcode("decf","%s,f",aopGet(AOP(IC_RESULT(ic)),LSB,FALSE,FALSE));
1186 pic14_emitcode("incfsz","%s,w",aopGet(AOP(IC_RESULT(ic)),LSB,FALSE,FALSE));
1187 pic14_emitcode(" decf","%s,f",aopGet(AOP(IC_RESULT(ic)),MSB16,FALSE,FALSE));
1189 /* size is 3 or 4 */
1190 emitpcode(POC_MOVLW, popGetLit(0xff));
1191 emitpcode(POC_ADDWF, popGet(AOP(IC_RESULT(ic)),LSB));
1193 emitpcode(POC_ADDWF, popGet(AOP(IC_RESULT(ic)),MSB16));
1195 emitpcode(POC_ADDWF, popGet(AOP(IC_RESULT(ic)),MSB24));
1197 pic14_emitcode("movlw","0xff");
1198 pic14_emitcode("addwf","%s,f",aopGet(AOP(IC_RESULT(ic)),LSB,FALSE,FALSE));
1201 pic14_emitcode("addwf","%s,f",aopGet(AOP(IC_RESULT(ic)),MSB16,FALSE,FALSE));
1203 pic14_emitcode("addwf","%s,f",aopGet(AOP(IC_RESULT(ic)),MSB24,FALSE,FALSE));
1207 emitpcode(POC_ADDWF, popGet(AOP(IC_RESULT(ic)),MSB32));
1209 pic14_emitcode("skpnc","");
1211 pic14_emitcode("addwf","%s,f",aopGet(AOP(IC_RESULT(ic)),MSB32,FALSE,FALSE));
1220 /* if the sizes are greater than 1 then we cannot */
1221 if (AOP_SIZE(IC_RESULT(ic)) > 1 ||
1222 AOP_SIZE(IC_LEFT(ic)) > 1 )
1225 /* we can if the aops of the left & result match or
1226 if they are in registers and the registers are the
1228 if (pic14_sameRegs(AOP(IC_LEFT(ic)), AOP(IC_RESULT(ic)))) {
1231 emitpcode(POC_DECF, popGet(AOP(IC_RESULT(ic)),0));
1233 //pic14_emitcode ("decf","%s,f",aopGet(AOP(IC_RESULT(ic)),0,FALSE,FALSE));
1238 DEBUGpic14_emitcode ("; returning"," result=%s, left=%s",
1239 aopGet(AOP(IC_RESULT(ic)),0,FALSE,FALSE),
1240 aopGet(AOP(IC_LEFT(ic)),0,FALSE,FALSE));
1243 pic14_emitcode("decf","%s,w",aopGet(AOP(IC_LEFT(ic)),0,FALSE,FALSE));
1244 pic14_emitcode("movwf","%s",aopGet(AOP(IC_RESULT(ic)),0,FALSE,FALSE));
1246 emitpcode(POC_DECFW, popGet(AOP(IC_LEFT(ic)),0));
1247 emitpcode(POC_MOVWF, popGet(AOP(IC_RESULT(ic)),0));
1255 /*-----------------------------------------------------------------*/
1256 /* addSign - propogate sign bit to higher bytes */
1257 /*-----------------------------------------------------------------*/
1258 void addSign(operand *result, int offset, int sign)
1260 int size = (pic14_getDataSize(result) - offset);
1261 DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__);
1265 if(sign && offset) {
1268 emitpcode(POC_CLRF,popGet(AOP(result),offset));
1269 emitpcode(POC_BTFSC,newpCodeOpBit(aopGet(AOP(result),offset-1,FALSE,FALSE),7,0));
1270 emitpcode(POC_DECF, popGet(AOP(result),offset));
1273 emitpcode(POC_MOVLW, popGetLit(0));
1274 emitpcode(POC_BTFSC, newpCodeOpBit(aopGet(AOP(result),offset-1,FALSE,FALSE),7,0));
1275 emitpcode(POC_MOVLW, popGetLit(0xff));
1277 emitpcode(POC_MOVWF, popGet(AOP(result),offset+size));
1281 emitpcode(POC_CLRF,popGet(AOP(result),offset++));
1285 /*-----------------------------------------------------------------*/
1286 /* genMinusBits - generates code for subtraction of two bits */
1287 /*-----------------------------------------------------------------*/
1288 void genMinusBits (iCode *ic)
1290 symbol *lbl = newiTempLabel(NULL);
1292 DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__);
1293 if (AOP_TYPE(IC_RESULT(ic)) == AOP_CRY){
1294 pic14_emitcode("mov","c,%s",AOP(IC_LEFT(ic))->aopu.aop_dir);
1295 pic14_emitcode("jnb","%s,%05d_DS_",AOP(IC_RIGHT(ic))->aopu.aop_dir,(lbl->key+100));
1296 pic14_emitcode("cpl","c");
1297 pic14_emitcode("","%05d_DS_:",(lbl->key+100));
1298 pic14_outBitC(IC_RESULT(ic));
1301 pic14_emitcode("mov","c,%s",AOP(IC_RIGHT(ic))->aopu.aop_dir);
1302 pic14_emitcode("subb","a,acc");
1303 pic14_emitcode("jnb","%s,%05d_DS_",AOP(IC_LEFT(ic))->aopu.aop_dir,(lbl->key+100));
1304 pic14_emitcode("inc","a");
1305 pic14_emitcode("","%05d_DS_:",(lbl->key+100));
1306 aopPut(AOP(IC_RESULT(ic)),"a",0);
1307 addSign(IC_RESULT(ic), MSB16, SPEC_USIGN(getSpec(operandType(IC_RESULT(ic)))));
1311 /*-----------------------------------------------------------------*/
1312 /* genMinus - generates code for subtraction */
1313 /*-----------------------------------------------------------------*/
1314 void genMinus (iCode *ic)
1316 int size, offset = 0, same=0;
1317 unsigned long lit = 0L;
1320 DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__);
1321 aopOp (IC_LEFT(ic),ic,FALSE);
1322 aopOp (IC_RIGHT(ic),ic,FALSE);
1323 aopOp (IC_RESULT(ic),ic,TRUE);
1325 if (AOP_TYPE(IC_RESULT(ic)) == AOP_CRY &&
1326 AOP_TYPE(IC_RIGHT(ic)) == AOP_LIT) {
1327 operand *t = IC_RIGHT(ic);
1328 IC_RIGHT(ic) = IC_LEFT(ic);
1332 DEBUGpic14_emitcode ("; ","result %s, left %s, right %s",
1333 AopType(AOP_TYPE(IC_RESULT(ic))),
1334 AopType(AOP_TYPE(IC_LEFT(ic))),
1335 AopType(AOP_TYPE(IC_RIGHT(ic))));
1337 /* special cases :- */
1338 /* if both left & right are in bit space */
1339 if (AOP_TYPE(IC_LEFT(ic)) == AOP_CRY &&
1340 AOP_TYPE(IC_RIGHT(ic)) == AOP_CRY) {
1345 /* if I can do an decrement instead
1346 of subtract then GOOD for ME */
1347 // if (genMinusDec (ic) == TRUE)
1350 size = pic14_getDataSize(IC_RESULT(ic));
1351 same = pic14_sameRegs(AOP(IC_RIGHT(ic)), AOP(IC_RESULT(ic)));
1353 if(AOP(IC_RIGHT(ic))->type == AOP_LIT) {
1354 /* Add a literal to something else */
1356 lit = (unsigned long)floatFromVal(AOP(IC_RIGHT(ic))->aopu.aop_lit);
1359 genAddLit ( ic, lit);
1362 /* add the first byte: */
1363 pic14_emitcode("movlw","0x%x", lit & 0xff);
1364 pic14_emitcode("addwf","%s,f", aopGet(AOP(IC_LEFT(ic)),0,FALSE,FALSE));
1365 emitpcode(POC_MOVLW, popGetLit(lit & 0xff));
1366 emitpcode(POC_ADDWF, popGet(AOP(IC_LEFT(ic)),0));
1378 if((lit & 0xff) == 0xff) {
1379 emitpcode(POC_MOVLW, popGetLit(0xff));
1381 emitpcode(POC_ADDWF, popGet(AOP(IC_LEFT(ic)),offset));
1383 emitpcode(POC_MOVLW, popGetLit(lit & 0xff));
1385 emitpcode(POC_MOVLW, popGetLit((lit+1) & 0xff));
1386 emitpcode(POC_ADDWF, popGet(AOP(IC_LEFT(ic)),offset));
1390 /* do the rlf known zero trick here */
1391 emitpcode(POC_MOVLW, popGetLit(1));
1393 emitpcode(POC_ADDWF, popGet(AOP(IC_LEFT(ic)),offset));
1398 } else if(AOP_TYPE(IC_RIGHT(ic)) == AOP_CRY) {
1401 pic14_emitcode(";bitsub","right is bit: %s",aopGet(AOP(IC_RIGHT(ic)),0,FALSE,FALSE));
1402 pic14_emitcode(";bitsub","left is bit: %s",aopGet(AOP(IC_LEFT(ic)),0,FALSE,FALSE));
1403 pic14_emitcode(";bitsub","result is bit: %s",aopGet(AOP(IC_RESULT(ic)),0,FALSE,FALSE));
1405 /* here we are subtracting a bit from a char or int */
1407 if (pic14_sameRegs(AOP(IC_LEFT(ic)), AOP(IC_RESULT(ic))) ) {
1409 emitpcode(POC_BTFSC , popGet(AOP(IC_RIGHT(ic)),0));
1410 emitpcode(POC_DECF , popGet(AOP(IC_RESULT(ic)),0));
1412 pic14_emitcode("btfsc","(%s >> 3), (%s & 7)",
1413 AOP(IC_RIGHT(ic))->aopu.aop_dir,
1414 AOP(IC_RIGHT(ic))->aopu.aop_dir);
1415 pic14_emitcode(" incf","%s,f", aopGet(AOP(IC_RESULT(ic)),0,FALSE,FALSE));
1418 if(AOP_TYPE(IC_LEFT(ic)) == AOP_ACC) {
1419 emitpcode(POC_BTFSC , popGet(AOP(IC_RIGHT(ic)),0));
1420 emitpcode(POC_XORLW , popGetLit(1));
1421 }else if( (AOP_TYPE(IC_LEFT(ic)) == AOP_IMMD) ||
1422 (AOP_TYPE(IC_LEFT(ic)) == AOP_LIT) ) {
1424 lit = (unsigned long)floatFromVal(AOP(IC_LEFT(ic))->aopu.aop_lit);
1426 if(AOP_TYPE(IC_RESULT(ic)) == AOP_CRY) {
1427 if (pic14_sameRegs(AOP(IC_RIGHT(ic)), AOP(IC_RESULT(ic))) ) {
1429 emitpcode(POC_MOVLW , popGet(AOP(IC_RIGHT(ic)),0));
1430 emitpcode(POC_XORWF , popGet(AOP(IC_RIGHT(ic)),0));
1433 emitpcode(POC_BCF , popGet(AOP(IC_RESULT(ic)),0));
1435 emitpcode(POC_BTFSS , popGet(AOP(IC_RIGHT(ic)),0));
1437 emitpcode(POC_BTFSC , popGet(AOP(IC_RIGHT(ic)),0));
1438 emitpcode(POC_BSF , popGet(AOP(IC_RESULT(ic)),0));
1442 emitpcode(POC_MOVLW , popGetLit(lit & 0xff));
1443 emitpcode(POC_BTFSC , popGet(AOP(IC_RIGHT(ic)),0));
1444 emitpcode(POC_MOVLW , popGetLit((lit-1) & 0xff));
1445 emitpcode(POC_MOVWF , popGet(AOP(IC_RESULT(ic)),0));
1450 emitpcode(POC_MOVFW , popGet(AOP(IC_LEFT(ic)),0));
1451 emitpcode(POC_BTFSC , popGet(AOP(IC_RIGHT(ic)),0));
1452 emitpcode(POC_DECFW , popGet(AOP(IC_LEFT(ic)),0));
1455 if(AOP_TYPE(IC_RESULT(ic)) != AOP_ACC) {
1457 emitpcode(POC_MOVWF , popGet(AOP(IC_RESULT(ic)),0));
1460 emitpcode(POC_ANDLW , popGetLit(1));
1462 emitpcode(POC_BCF , popGet(AOP(IC_RESULT(ic)),0,FALSE,FALSE));
1464 emitpcode(POC_BSF , popGet(AOP(IC_RESULT(ic)),0,FALSE,FALSE));
1471 } else if(// (AOP_TYPE(IC_LEFT(ic)) == AOP_IMMD) ||
1472 (AOP(IC_LEFT(ic))->type == AOP_LIT) &&
1473 (AOP_TYPE(IC_RIGHT(ic)) != AOP_ACC)) {
1475 lit = (unsigned long)floatFromVal(AOP(IC_LEFT(ic))->aopu.aop_lit);
1476 DEBUGpic14_emitcode ("; left is lit","line %d result %s, left %s, right %s",__LINE__,
1477 AopType(AOP_TYPE(IC_RESULT(ic))),
1478 AopType(AOP_TYPE(IC_LEFT(ic))),
1479 AopType(AOP_TYPE(IC_RIGHT(ic))));
1482 if( (size == 1) && ((lit & 0xff) == 0) ) {
1483 /* res = 0 - right */
1484 if (pic14_sameRegs(AOP(IC_RIGHT(ic)), AOP(IC_RESULT(ic))) ) {
1485 emitpcode(POC_COMF, popGet(AOP(IC_RIGHT(ic)),0));
1486 emitpcode(POC_INCF, popGet(AOP(IC_RIGHT(ic)),0));
1488 emitpcode(POC_COMFW, popGet(AOP(IC_RIGHT(ic)),0));
1489 emitpcode(POC_MOVWF, popGet(AOP(IC_RESULT(ic)),0));
1490 emitpcode(POC_INCF, popGet(AOP(IC_RESULT(ic)),0));
1495 emitpcode(POC_MOVFW, popGet(AOP(IC_RIGHT(ic)),0));
1496 emitpcode(POC_SUBLW, popGetLit(lit & 0xff));
1497 emitpcode(POC_MOVWF,popGet(AOP(IC_RESULT(ic)),0));
1505 /* This is the last byte in a multibyte subtraction
1506 * There are a couple of tricks we can do by not worrying about
1507 * propogating the carry */
1509 /* 0xff - x == ~x */
1511 emitpcode(POC_COMF, popGet(AOP(IC_RESULT(ic)),offset));
1513 emitpcode(POC_DECF, popGet(AOP(IC_RESULT(ic)),offset));
1515 emitpcode(POC_COMFW, popGet(AOP(IC_RIGHT(ic)),offset));
1516 emitpcode(POC_MOVWF, popGet(AOP(IC_RESULT(ic)),offset));
1518 emitpcode(POC_DECF, popGet(AOP(IC_RESULT(ic)),offset));
1521 emitpcode(POC_MOVFW, popGet(AOP(IC_RIGHT(ic)),offset));
1523 emitpcode(POC_INCFW, popGet(AOP(IC_RIGHT(ic)),offset));
1524 emitpcode(POC_SUBLW, popGetLit(lit & 0xff));
1525 emitpcode(POC_MOVWF, popGet(AOP(IC_RESULT(ic)),offset));
1534 emitpcode(POC_MOVLW, popGetLit(lit & 0xff));
1536 emitpcode(POC_MOVLW, popGetLit((lit & 0xff)-1));
1537 emitpcode(POC_SUBWF, popGet(AOP(IC_RESULT(ic)),offset));
1540 emitpcode(POC_SUBWF, popGet(AOP(IC_RESULT(ic)),offset));
1546 emitpcode(POC_MOVLW, popGetLit(lit & 0xff));
1547 emitpcode(POC_MOVWF, popGet(AOP(IC_RESULT(ic)),offset));
1549 emitpcode(POC_CLRF, popGet(AOP(IC_RESULT(ic)),offset));
1551 emitpcode(POC_MOVFW, popGet(AOP(IC_RIGHT(ic)),offset));
1553 emitpcode(POC_INCFSZW,popGet(AOP(IC_RIGHT(ic)),offset));
1554 emitpcode(POC_SUBWF, popGet(AOP(IC_RESULT(ic)),offset));
1561 DEBUGpic14_emitcode ("; ","line %d result %s, left %s, right %s",__LINE__,
1562 AopType(AOP_TYPE(IC_RESULT(ic))),
1563 AopType(AOP_TYPE(IC_LEFT(ic))),
1564 AopType(AOP_TYPE(IC_RIGHT(ic))));
1566 if(strcmp(aopGet(AOP(IC_LEFT(ic)),0,FALSE,FALSE),"a") == 0 ) {
1567 DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__);
1568 emitpcode(POC_SUBFW, popGet(AOP(IC_RIGHT(ic)),0));
1569 emitpcode(POC_SUBLW, popGetLit(0));
1570 emitpcode(POC_MOVWF,popGet(AOP(IC_RESULT(ic)),0));
1573 if ( AOP_TYPE(IC_LEFT(ic)) == AOP_ACC) {
1574 emitpcode(POC_SUBFW, popGet(AOP(IC_RIGHT(ic)),0));
1575 emitpcode(POC_SUBLW, popGetLit(0));
1576 if ( AOP_TYPE(IC_RESULT(ic)) != AOP_ACC)
1577 emitpcode(POC_MOVWF,popGet(AOP(IC_RESULT(ic)),0));
1580 DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__);
1581 if(AOP_TYPE(IC_RIGHT(ic)) != AOP_ACC)
1582 emitpcode(POC_MOVFW,popGet(AOP(IC_RIGHT(ic)),0));
1584 if (pic14_sameRegs(AOP(IC_LEFT(ic)), AOP(IC_RESULT(ic))) )
1585 emitpcode(POC_SUBWF, popGet(AOP(IC_LEFT(ic)),0));
1587 if( (AOP_TYPE(IC_LEFT(ic)) == AOP_IMMD) ||
1588 (AOP_TYPE(IC_LEFT(ic)) == AOP_LIT) ) {
1589 emitpcode(POC_SUBLW, popGet(AOP(IC_LEFT(ic)),0));
1591 emitpcode(POC_SUBFW, popGet(AOP(IC_LEFT(ic)),0));
1593 if ( AOP_TYPE(IC_RESULT(ic)) != AOP_ACC) {
1594 if ( AOP_TYPE(IC_RESULT(ic)) == AOP_CRY) {
1595 emitpcode(POC_BCF , popGet(AOP(IC_RESULT(ic)),0));
1597 emitpcode(POC_BSF , popGet(AOP(IC_RESULT(ic)),0));
1599 emitpcode(POC_MOVWF,popGet(AOP(IC_RESULT(ic)),0));
1605 size = min( AOP_SIZE(IC_RESULT(ic)), AOP_SIZE(IC_RIGHT(ic))) - 1;
1609 if (pic14_sameRegs(AOP(IC_RIGHT(ic)), AOP(IC_RESULT(ic)))) {
1611 if (op_isLitLike (IC_LEFT(ic)))
1614 emitpcode(POC_MOVFW, popGet(AOP(IC_RIGHT(ic)),offset));
1616 emitpcode(POC_INCFW, popGet(AOP(IC_RIGHT(ic)),offset));
1617 emitpcode(lit?POC_SUBLW:POC_SUBFW, popGetAddr(AOP(IC_LEFT(ic)),offset,0));
1618 emitpcode(POC_MOVWF, popGet(AOP(IC_RESULT(ic)),offset));
1622 PIC_OPCODE poc = POC_MOVFW;
1623 if (op_isLitLike (IC_LEFT(ic)))
1626 if (!pic14_sameRegs(AOP(IC_LEFT(ic)), AOP(IC_RESULT(ic))) ) {
1627 emitpcode(poc, popGetAddr(AOP(IC_LEFT(ic)),offset,0));
1628 emitpcode(POC_MOVWF, popGet(AOP(IC_RESULT(ic)),offset));
1630 emitpcode(POC_MOVFW, popGet(AOP(IC_RIGHT(ic)),offset));
1632 emitpcode(POC_INCFSZW,popGet(AOP(IC_RIGHT(ic)),offset));
1633 emitpcode(POC_SUBWF, popGet(AOP(IC_RESULT(ic)),offset));
1640 // adjustArithmeticResult(ic);
1643 freeAsmop(IC_LEFT(ic),NULL,ic,(RESULTONSTACK(ic) ? FALSE : TRUE));
1644 freeAsmop(IC_RIGHT(ic),NULL,ic,(RESULTONSTACK(ic) ? FALSE : TRUE));
1645 freeAsmop(IC_RESULT(ic),NULL,ic,TRUE);
1647 /*-----------------------------------------------------------------*
1648 * genUMult8XLit_16 - unsigned multiplication of two 8-bit numbers.
1651 *-----------------------------------------------------------------*/
1652 void genUMult8XLit_16 (operand *left,
1655 pCodeOpReg *result_hi)
1660 unsigned int i,have_first_bit;
1665 if (AOP_TYPE(right) != AOP_LIT){
1666 fprintf(stderr,"%s %d - right operand is not a literal\n",__FILE__,__LINE__);
1672 result_hi = PCOR(popGet(AOP(result),1));
1675 lit = (unsigned int)floatFromVal(AOP(right)->aopu.aop_lit);
1677 pic14_emitcode(";","Unrolled 8 X 8 multiplication");
1679 same = pic14_sameRegs(AOP(left), AOP(result));
1684 emitpcode(POC_CLRF, popGet(AOP(left),0));
1687 emitpcode(POC_MOVFW, popGet(AOP(left),0));
1688 emitpcode(POC_ADDWF, popGet(AOP(left),0));
1691 emitpcode(POC_MOVFW, popGet(AOP(left),0));
1692 emitpcode(POC_ADDWF, popGet(AOP(left),0));
1693 emitpcode(POC_ADDWF, popGet(AOP(left),0));
1696 emitpcode(POC_MOVFW, popGet(AOP(left),0));
1697 emitpcode(POC_ADDWF, popGet(AOP(left),0));
1698 emitpcode(POC_ADDWF, popGet(AOP(left),0));
1699 emitpcode(POC_ADDWF, popGet(AOP(left),0));
1702 emitpcode(POC_MOVFW, popGet(AOP(left),0));
1703 emitpcode(POC_ADDFW, popGet(AOP(left),0)); // W = 2*F
1704 emitpcode(POC_ADDWF, popGet(AOP(left),0)); // F = 3*F
1705 emitpcode(POC_ADDWF, popGet(AOP(left),0)); // F = 5*F
1708 emitpcode(POC_MOVFW, popGet(AOP(left),0));
1709 emitpcode(POC_ADDWF, popGet(AOP(left),0));
1710 emitpcode(POC_ADDWF, popGet(AOP(left),0));
1711 emitpcode(POC_MOVFW, popGet(AOP(left),0));
1712 emitpcode(POC_ADDWF, popGet(AOP(left),0));
1715 emitpcode(POC_MOVFW, popGet(AOP(left),0));
1716 emitpcode(POC_ADDFW, popGet(AOP(left),0)); // W = 2*F
1717 emitpcode(POC_ADDWF, popGet(AOP(left),0)); // F = 3*F
1718 emitpcode(POC_ADDWF, popGet(AOP(left),0)); // F = 5*F
1719 emitpcode(POC_ADDWF, popGet(AOP(left),0)); // F = 7*F
1722 emitpcode(POC_MOVFW, popGet(AOP(left),0));
1723 emitpcode(POC_ADDFW, popGet(AOP(left),0)); // W = 2*F
1724 emitpcode(POC_ADDWF, popGet(AOP(left),0)); // F = 3*F
1725 emitpcode(POC_ADDFW, popGet(AOP(left),0)); // W = 5*F
1726 emitpcode(POC_ADDWF, popGet(AOP(left),0)); // F = 8*F
1729 emitpcode(POC_MOVFW, popGet(AOP(left),0));
1730 emitpcode(POC_ADDWF, popGet(AOP(left),0));
1731 emitpcode(POC_ADDWF, popGet(AOP(left),0));
1732 emitpcode(POC_MOVFW, popGet(AOP(left),0));
1733 emitpcode(POC_ADDWF, popGet(AOP(left),0));
1734 emitpcode(POC_ADDWF, popGet(AOP(left),0));
1737 emitpcode(POC_MOVFW, popGet(AOP(left),0));
1738 emitpcode(POC_ADDFW, popGet(AOP(left),0)); // W = 2*F
1739 emitpcode(POC_ADDWF, popGet(AOP(left),0)); // F = 3*F
1740 emitpcode(POC_ADDWF, popGet(AOP(left),0)); // F = 5*F
1741 emitpcode(POC_MOVFW, popGet(AOP(left),0));
1742 emitpcode(POC_ADDWF, popGet(AOP(left),0));
1745 emitpcode(POC_MOVFW, popGet(AOP(left),0));
1746 emitpcode(POC_ADDFW, popGet(AOP(left),0)); // W = 2*F
1747 emitpcode(POC_ADDWF, popGet(AOP(left),0)); // F = 3*F
1748 emitpcode(POC_ADDFW, popGet(AOP(left),0)); // W = 5*F
1749 emitpcode(POC_ADDFW, popGet(AOP(left),0)); // W = 8*F
1750 emitpcode(POC_ADDWF, popGet(AOP(left),0)); // F = 11*F
1753 emitpcode(POC_MOVFW, popGet(AOP(left),0));
1754 emitpcode(POC_ADDWF, popGet(AOP(left),0));
1755 emitpcode(POC_ADDWF, popGet(AOP(left),0));
1756 emitpcode(POC_MOVFW, popGet(AOP(left),0));
1757 emitpcode(POC_ADDWF, popGet(AOP(left),0));
1758 emitpcode(POC_MOVFW, popGet(AOP(left),0));
1759 emitpcode(POC_ADDWF, popGet(AOP(left),0));
1762 emitpcode(POC_MOVFW, popGet(AOP(left),0));
1763 emitpcode(POC_ADDFW, popGet(AOP(left),0)); // W = 2*F
1764 emitpcode(POC_ADDWF, popGet(AOP(left),0)); // F = 3*F
1765 emitpcode(POC_ADDFW, popGet(AOP(left),0)); // W = 5*F
1766 emitpcode(POC_ADDWF, popGet(AOP(left),0)); // F = 8*F
1767 emitpcode(POC_ADDWF, popGet(AOP(left),0)); // F = 13*F
1770 emitpcode(POC_MOVFW, popGet(AOP(left),0));
1771 emitpcode(POC_ADDFW, popGet(AOP(left),0)); // W = 2*F
1772 emitpcode(POC_ADDWF, popGet(AOP(left),0)); // F = 3*F
1773 emitpcode(POC_ADDFW, popGet(AOP(left),0)); // W = 5*F
1774 emitpcode(POC_ADDFW, popGet(AOP(left),0)); // W = 8*F
1775 emitpcode(POC_ADDFW, popGet(AOP(left),0)); // W = 11*F
1776 emitpcode(POC_ADDWF, popGet(AOP(left),0)); // F = 14*F
1779 temp = popGetTempReg();
1781 fprintf(stderr,"ERROR: unable to allocate register. %s:%d\n",__FUNCTION__,__LINE__);
1784 emitpcode(POC_SWAPFW, popGet(AOP(left),0));
1785 emitpcode(POC_MOVWF, temp);
1786 emitpcode(POC_ANDLW, popGetLit(0xf0));
1787 emitpcode(POC_MOVWF, popGet(AOP(left),0));
1788 emitpcode(POC_SWAPFW, temp);
1789 emitpcode(POC_SUBWF, popGet(AOP(left),0));
1790 popReleaseTempReg(temp);
1793 emitpcode(POC_SWAPFW, popGet(AOP(left),0));
1794 emitpcode(POC_ANDLW, popGetLit(0xf0));
1795 emitpcode(POC_MOVWF, popGet(AOP(left),0));
1798 emitpcode(POC_SWAPFW, popGet(AOP(left),0));
1799 emitpcode(POC_ANDLW, popGetLit(0xf0));
1800 emitpcode(POC_ADDWF, popGet(AOP(left),0));
1803 emitpcode(POC_SWAPF, popGet(AOP(left),0));
1804 emitpcode(POC_RLFW, popGet(AOP(left),0));
1805 emitpcode(POC_ANDLW, popGetLit(0xe0));
1806 emitpcode(POC_MOVWF, popGet(AOP(left),0));
1809 emitpcode(POC_SWAPF, popGet(AOP(left),0));
1810 emitpcode(POC_RLF, popGet(AOP(left),0));
1811 emitpcode(POC_RLFW, popGet(AOP(left),0));
1812 emitpcode(POC_ANDLW, popGetLit(0xc0));
1813 emitpcode(POC_MOVWF, popGet(AOP(left),0));
1816 emitpcode(POC_RRFW, popGet(AOP(left),0));
1817 emitpcode(POC_CLRF, popGet(AOP(left),0));
1818 emitpcode(POC_RRF, popGet(AOP(left),0));
1826 emitpcode(POC_CLRF, popGet(AOP(result),0));
1827 emitpcode(POC_CLRF, popCopyReg(result_hi));
1830 if (AOP_TYPE(left) != AOP_ACC) /* Check if w is already loaded */
1831 emitpcode(POC_MOVFW, popGet(AOP(left),0));
1832 emitpcode(POC_MOVWF, popGet(AOP(result),0));
1833 emitpcode(POC_ADDWF, popGet(AOP(result),0));
1834 emitpcode(POC_CLRF, popCopyReg(result_hi));
1835 emitpcode(POC_RLF, popCopyReg(result_hi));
1839 if (AOP_TYPE(left) != AOP_ACC) /* Check if w is already loaded */
1840 emitpcode(POC_MOVFW, popGet(AOP(left),0));
1841 emitpcode(POC_MOVWF, popGet(AOP(result),0));
1842 emitpcode(POC_CLRF, popCopyReg(result_hi));
1843 emitpcode(POC_BCF, popCopyReg(&pc_status));
1844 emitpcode(POC_RLF, popGet(AOP(result),0));
1845 emitpcode(POC_RLF, popCopyReg(result_hi));
1846 emitpcode(POC_RLF, popGet(AOP(result),0));
1847 emitpcode(POC_RLF, popCopyReg(result_hi));
1849 emitpcode(POC_RLF, popGet(AOP(result),0));
1850 emitpcode(POC_RLF, popCopyReg(result_hi));
1857 if (AOP_TYPE(left) != AOP_ACC) /* Check if w is already loaded */
1858 emitpcode(POC_MOVFW, popGet(AOP(left),0));
1859 emitpcode(POC_CLRF, popGet(AOP(result),0));
1860 emitpcode(POC_CLRF, popCopyReg(result_hi));
1863 for(i=0; i<8; i++) {
1866 emitpcode(POC_ADDWF, popCopyReg(result_hi));
1870 if(have_first_bit) {
1871 emitpcode(POC_RRF, popCopyReg(result_hi));
1872 emitpcode(POC_RRF, popGet(AOP(result),0));
1879 /*-----------------------------------------------------------------*
1880 * genUMult8X8_16 - unsigned multiplication of two 8-bit numbers.
1883 *-----------------------------------------------------------------*/
1884 void genUMult8X8_16 (operand *left,
1887 pCodeOpReg *result_hi)
1896 result_hi = PCOR(popGet(AOP(result),1));
1899 if (AOP_TYPE(right) == AOP_LIT) {
1900 genUMult8XLit_16(left,right,result,result_hi);
1905 pic14_emitcode(";","Unrolled 8 X 8 multiplication");
1907 emitpcode(POC_MOVFW, popGet(AOP(right),0));
1908 emitpcode(POC_CLRF, popGet(AOP(result),0));
1909 emitpcode(POC_CLRF, popCopyReg(result_hi));
1912 for(i=0; i<8; i++) {
1913 emitpcode(POC_BTFSC, newpCodeOpBit(aopGet(AOP(left),0,FALSE,FALSE),i,0));
1914 emitpcode(POC_ADDWF, popCopyReg(result_hi));
1915 emitpcode(POC_RRF, popCopyReg(result_hi));
1916 emitpcode(POC_RRF, popGet(AOP(result),0));
1921 Here's another version that does the same thing and takes the
1922 same number of instructions. The one above is slightly better
1923 because the entry instructions have a higher probability of
1924 being optimized out.
1927 emitpcode(POC_CLRF, popCopyReg(result_hi));
1928 emitpcode(POC_RRFW, popGet(AOP(left),0));
1929 emitpcode(POC_MOVWF, popGet(AOP(result),0));
1930 emitpcode(POC_MOVFW, popGet(AOP(right),0));
1932 for(i=0; i<8; i++) {
1934 emitpcode(POC_ADDWF, popCopyReg(result_hi));
1935 emitpcode(POC_RRF, popCopyReg(result_hi));
1936 emitpcode(POC_RRF, popGet(AOP(result),0));
1941 symbol *tlbl = newiTempLabel(NULL);
1945 pic14_emitcode(";","Looped 8 X 8 multiplication");
1947 emitpcode(POC_CLRF, popGet(AOP(result),0));
1948 emitpcode(POC_CLRF, popCopyReg(result_hi));
1950 emitpcode(POC_BSF, newpCodeOpBit(aopGet(AOP(result),0,FALSE,FALSE),7,0));
1952 emitpcode(POC_MOVFW, popGet(AOP(right),0));
1954 temp = popGetTempReg();
1955 emitpcode(POC_MOVWF, popCopyReg(PCOR(temp)));
1957 emitpcode(POC_MOVFW, popGet(AOP(left),0));
1959 emitpLabel(tlbl->key);
1961 emitpcode(POC_RRF, popCopyReg(PCOR(temp)));
1963 emitpcode(POC_ADDWF, popCopyReg(result_hi));
1965 emitpcode(POC_RRF, popCopyReg(result_hi));
1966 emitpcode(POC_RRF, popGet(AOP(result),0));
1969 emitpcode(POC_GOTO, popGetLabel(tlbl->key));
1971 popReleaseTempReg(temp);
1976 /*-----------------------------------------------------------------*
1977 * genSMult8X8_16 - signed multiplication of two 8-bit numbers
1979 * this routine will call the unsigned multiply routine and then
1980 * post-fix the sign bit.
1981 *-----------------------------------------------------------------*/
1982 void genSMult8X8_16 (operand *left,
1985 pCodeOpReg *result_hi)
1990 result_hi = PCOR(popGet(AOP(result),1));
1993 genUMult8X8_16(left,right,result,result_hi);
1995 emitpcode(POC_BTFSC, newpCodeOpBit(aopGet(AOP(left),0,FALSE,FALSE),7,0));
1996 emitpcode(POC_SUBWF, popCopyReg(result_hi));
1997 emitpcode(POC_MOVFW, popGet(AOP(left),0));
1998 emitpcode(POC_BTFSC, newpCodeOpBit(aopGet(AOP(right),0,FALSE,FALSE),7,0));
1999 emitpcode(POC_SUBWF, popGet(AOP(result),1));
2003 /*-----------------------------------------------------------------*
2004 * genMult8X8_8 - multiplication of two 8-bit numbers
2006 * this routine will call the unsigned multiply 8X8=>16 routine and
2007 * then throw away the high byte of the result.
2009 *-----------------------------------------------------------------*/
2010 void genMult8X8_8 (operand *left,
2016 if (result && result->aop && result->aop->type==2 && result->aop->size>=1) {
2017 result->aop->aopu.aop_reg[0]->isFree = 0; /* Sometimes (ie part of last instruction in a blk) the result reg is pre marked as free, which mean on the next line popGetTempReg() will return this reg instead of allocating a new one. */
2019 result_hi = popGetTempReg();
2021 if (AOP_TYPE(right) == AOP_LIT)
2022 genUMult8XLit_16(left,right,result,PCOR(result_hi));
2024 genUMult8X8_16(left,right,result,PCOR(result_hi));
2026 popReleaseTempReg(result_hi);
2029 /*-----------------------------------------------------------------*/
2030 /* constMult - generates code for multiplication by a constant */
2031 /*-----------------------------------------------------------------*/
2032 void genMultConst(unsigned C)
2036 unsigned sr3; // Shift right 3
2042 Convert a string of 3 binary 1's in the lit into
2046 mask = 7 << ( (size*8) - 3);
2050 while(mask < (1<<size*8)) {
2052 if( (mask & lit) == lit) {
2055 /* We found 3 (or more) consecutive 1's */
2057 lsb = mask & ~(mask & (mask-1)); // lsb of mask.
2059 consecutive_bits = ((lit + lsb) & lit) ^ lit;
2061 lit ^= consecutive_bits;
2065 sr3 |= (consecutive + lsb);