X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2Fpic%2Fgen.c;h=6ca5e3ecbb338e8929f783732a851e7ebdd3d0d0;hb=4ab9456bc01da32af2aaccf935a9db0e7d5c63f0;hp=6057496d23188ae559de4775c6598427eda982ca;hpb=42ac194ab811f6198733c7fd82b07de8da31831f;p=fw%2Fsdcc diff --git a/src/pic/gen.c b/src/pic/gen.c index 6057496d..6ca5e3ec 100644 --- a/src/pic/gen.c +++ b/src/pic/gen.c @@ -63,9 +63,11 @@ extern void genUMult8X8_16 (operand *, operand *,operand *,pCodeOpReg *); extern void genSMult8X8_16 (operand *, operand *,operand *,pCodeOpReg *); void genMult8X8_8 (operand *, operand *,operand *); +pCode *AssembleLine(char *line); +extern void printpBlock(FILE *of, pBlock *pb); static int labelOffset=0; -static int debug_verbose=1; +extern int debug_verbose; static int optimized_for_speed = 0; /* max_key keeps track of the largest label number used in @@ -399,15 +401,17 @@ static void resolveIfx(resolvedIfx *resIfx, iCode *ifx) if(!resIfx) return; - DEBUGpic14_emitcode("; ***","%s %d",__FUNCTION__,__LINE__); + // DEBUGpic14_emitcode("; ***","%s %d",__FUNCTION__,__LINE__); resIfx->condition = 1; /* assume that the ifx is true */ resIfx->generated = 0; /* indicate that the ifx has not been used */ if(!ifx) { resIfx->lbl = newiTempLabel(NULL); /* oops, there is no ifx. so create a label */ +/* DEBUGpic14_emitcode("; ***","%s %d null ifx creating new label key =%d", __FUNCTION__,__LINE__,resIfx->lbl->key); +*/ } else { if(IC_TRUE(ifx)) { resIfx->lbl = IC_TRUE(ifx); @@ -415,13 +419,15 @@ static void resolveIfx(resolvedIfx *resIfx, iCode *ifx) resIfx->lbl = IC_FALSE(ifx); resIfx->condition = 0; } +/* if(IC_TRUE(ifx)) DEBUGpic14_emitcode("; ***","ifx true is non-null"); if(IC_FALSE(ifx)) DEBUGpic14_emitcode("; ***","ifx false is non-null"); +*/ } - DEBUGpic14_emitcode("; ***","%s lbl->key=%d, (lab offset=%d)",__FUNCTION__,resIfx->lbl->key,labelOffset); + // DEBUGpic14_emitcode("; ***","%s lbl->key=%d, (lab offset=%d)",__FUNCTION__,resIfx->lbl->key,labelOffset); } /*-----------------------------------------------------------------*/ @@ -552,8 +558,8 @@ static asmop *aopForSym (iCode *ic,symbol *sym,bool result) PCOI(aop->aopu.pcop)->_const = IN_CODESPACE(space); PCOI(aop->aopu.pcop)->index = 0; - DEBUGpic14_emitcode(";"," rname %s, val %d, const = %d", - sym->rname, 0, PCOI(aop->aopu.pcop)->_const); + DEBUGpic14_emitcode(";","%d: rname %s, val %d, const = %d", + __LINE__,sym->rname, 0, PCOI(aop->aopu.pcop)->_const); allocDirReg (IC_LEFT(ic)); @@ -607,8 +613,8 @@ static asmop *aopForRemat (operand *op) // x symbol *sym) PCOI(aop->aopu.pcop)->_const = IS_PTR_CONST(operandType(op)); PCOI(aop->aopu.pcop)->index = val; - DEBUGpic14_emitcode(";"," rname %s, val %d, const = %d", - OP_SYMBOL(IC_LEFT(ic))->rname, + DEBUGpic14_emitcode(";","%d: rname %s, val %d, const = %d", + __LINE__,OP_SYMBOL(IC_LEFT(ic))->rname, val, IS_PTR_CONST(operandType(op))); // DEBUGpic14_emitcode(";","aop type %s",AopType(AOP_TYPE(IC_LEFT(ic)))); @@ -1106,11 +1112,11 @@ char *aopGet (asmop *aop, int offset, bool bit16, bool dname) return rs; case AOP_STR: - DEBUGpic14_emitcode(";","%d",__LINE__); aop->coff = offset ; if (strcmp(aop->aopu.aop_str[offset],"a") == 0 && dname) return "acc"; + DEBUGpic14_emitcode(";","%d - %s",__LINE__, aop->aopu.aop_str[offset]); return aop->aopu.aop_str[offset]; @@ -1648,6 +1654,8 @@ void mov2w (asmop *aop, int offset) if(!aop) return; + DEBUGpic14_emitcode ("; ***","%s %d offset=%d",__FUNCTION__,__LINE__,offset); + if ( aop->type == AOP_PCODE || aop->type == AOP_LIT ) emitpcode(POC_MOVLW,popGet(aop,offset)); @@ -2602,17 +2610,17 @@ static void genPcall (iCode *ic) /*-----------------------------------------------------------------*/ static int resultRemat (iCode *ic) { - DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - if (SKIP_IC(ic) || ic->op == IFX) - return 0; + // DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + 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; - } + 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; + return 0; } #if defined(__BORLANDC__) || defined(_MSC_VER) @@ -2945,7 +2953,7 @@ static void genEndFunction (iCode *ic) emitpcode(POC_SWAPFW, popCopyReg(&pc_ssave)); emitpcode(POC_MOVWF, popCopyReg(&pc_status)); emitpcode(POC_SWAPF, popCopyReg(&pc_wsave)); - emitpcode(POC_MOVFW, popCopyReg(&pc_wsave)); + emitpcode(POC_SWAPFW, popCopyReg(&pc_wsave)); addpCode2pBlock(pb,newpCodeLabel("END_OF_INTERRUPT",-1)); emitpcodeNULLop(POC_RETFIE); @@ -3746,16 +3754,19 @@ static void genCmp (operand *left,operand *right, // resolvedIfx rTrueIfx; symbol *truelbl; DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); +/* if(ifx) { DEBUGpic14_emitcode ("; ***","true ifx is %s",((IC_TRUE(ifx) == NULL) ? "false" : "true")); DEBUGpic14_emitcode ("; ***","false ifx is %s",((IC_FALSE(ifx) == NULL) ? "false" : "true")); } - +*/ resolveIfx(&rFalseIfx,ifx); truelbl = newiTempLabel(NULL); size = max(AOP_SIZE(left),AOP_SIZE(right)); + DEBUGpic14_AopType(__LINE__,left,right,result); + #define _swapp /* if literal is on the right then swap with left */ @@ -3764,10 +3775,8 @@ static void genCmp (operand *left,operand *right, unsigned long mask = (0x100 << (8*(size-1))) - 1; lit = (unsigned long)floatFromVal(AOP(right)->aopu.aop_lit); #ifdef _swapp - DEBUGpic14_emitcode ("; ***","%d swapping left&right, lit =0x%x",__LINE__,lit); - lit = (lit - 1) & mask; - DEBUGpic14_emitcode ("; ***","%d swapping left&right, lit =0x%x, mask 0x%x",__LINE__,lit,mask); + lit = (lit - 1) & mask; right = left; left = tmp; rFalseIfx.condition ^= 1; @@ -4035,7 +4044,8 @@ static void genCmp (operand *left,operand *right, } -#endif +#endif // _swapp + if(AOP_TYPE(left) == AOP_LIT) { //symbol *lbl = newiTempLabel(NULL); @@ -4111,12 +4121,18 @@ static void genCmp (operand *left,operand *right, emitpcode(POC_SUBFW, popGet(AOP(right),0)); DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); rFalseIfx.condition ^= 1; - genSkipc(&rFalseIfx); + if (AOP_TYPE(result) == AOP_CRY) + genSkipc(&rFalseIfx); + else { + emitpcode(POC_CLRF, popGet(AOP(result),0)); + emitpcode(POC_RLF, popGet(AOP(result),0)); + } break; } } if(ifx) ifx->generated = 1; + //goto check_carry; return; } else { @@ -4366,15 +4382,26 @@ static void genCmp (operand *left,operand *right, emitpLabel(lbl->key); DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - genSkipc(&rFalseIfx); + if ((AOP_TYPE(result) == AOP_CRY && AOP_SIZE(result)) || + (AOP_TYPE(result) == AOP_REG)) { + emitpcode(POC_CLRF, popGet(AOP(result),0)); + emitpcode(POC_RLF, popGet(AOP(result),0)); + } else { + genSkipc(&rFalseIfx); + } + //genSkipc(&rFalseIfx); if(ifx) ifx->generated = 1; + return; } + // check_carry: if (AOP_TYPE(result) == AOP_CRY && AOP_SIZE(result)) { + DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); pic14_outBitC(result); } else { + DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); /* if the result is used in the next ifx conditional branch then generate code a little differently */ @@ -4504,13 +4531,16 @@ static void gencjne(operand *left, operand *right, operand *result, iCode *ifx) { int size = max(AOP_SIZE(left),AOP_SIZE(right)); int offset = 0; + int res_offset = 0; /* the result may be a different size then left or right */ + int res_size = AOP_SIZE(result); resolvedIfx rIfx; symbol *lbl; unsigned long lit = 0L; DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); - DEBUGpic14_AopType(__LINE__,left,right,NULL); - + DEBUGpic14_AopType(__LINE__,left,right,result); + if(result) + DEBUGpic14_emitcode ("; ***","%s %d result is not null",__FUNCTION__,__LINE__); resolveIfx(&rIfx,ifx); lbl = newiTempLabel(NULL); @@ -4548,6 +4578,8 @@ static void gencjne(operand *left, operand *right, operand *result, iCode *ifx) emitSKPNZ; emitpcode(POC_GOTO,popGetLabel(lbl->key)); offset++; + if(res_offset < res_size-1) + res_offset++; lit >>= 8; } break; @@ -4560,71 +4592,90 @@ static void gencjne(operand *left, operand *right, operand *result, iCode *ifx) AOP_TYPE(right) == AOP_DIR || (AOP_TYPE(left) == AOP_DIR && AOP_TYPE(right) == AOP_LIT) || (IS_AOP_PREG(left) && !IS_AOP_PREG(right))) { - switch(size) { - case 2: - genc16bit2lit(left, lit, 0); - emitSKPNZ; - emitpcode(POC_GOTO,popGetLabel(lbl->key)); - break; - default: - while (size--) { - int emit_skip=1; - if((AOP_TYPE(left) == AOP_DIR) && - ((AOP_TYPE(right) == AOP_REG) || (AOP_TYPE(right) == AOP_DIR))) { + //int lbl_key = (rIfx.condition) ? rIfx.lbl->key : lbl->key; + int lbl_key = lbl->key; + + if(result) { + emitpcode(POC_CLRF,popGet(AOP(result),res_offset)); + //emitpcode(POC_INCF,popGet(AOP(result),res_offset)); + }else { + DEBUGpic14_emitcode ("; ***","%s %d -- ERROR",__FUNCTION__,__LINE__); + fprintf(stderr, "%s %d error - expecting result to be non_null\n", + __FUNCTION__,__LINE__); + return; + } - emitpcode(POC_MOVFW,popGet(AOP(left),offset)); - emitpcode(POC_XORFW,popGet(AOP(right),offset)); +/* switch(size) { */ +/* case 2: */ +/* genc16bit2lit(left, lit, 0); */ +/* emitSKPNZ; */ +/* emitpcode(POC_GOTO,popGetLabel(lbl->key)); */ +/* break; */ +/* default: */ + while (size--) { + int emit_skip=1; + if((AOP_TYPE(left) == AOP_DIR) && + ((AOP_TYPE(right) == AOP_REG) || (AOP_TYPE(right) == AOP_DIR))) { + + emitpcode(POC_MOVFW,popGet(AOP(left),offset)); + emitpcode(POC_XORFW,popGet(AOP(right),offset)); - } else if((AOP_TYPE(left) == AOP_DIR) && (AOP_TYPE(right) == AOP_LIT)){ + } else if((AOP_TYPE(left) == AOP_DIR) && (AOP_TYPE(right) == AOP_LIT)){ - switch (lit & 0xff) { - case 0: - emitpcode(POC_MOVFW,popGet(AOP(left),offset)); - break; - case 1: - emitpcode(POC_DECFSZ,popGet(AOP(left),offset)); - emitpcode(POC_GOTO,popGetLabel(lbl->key)); - emit_skip=0; - break; - case 0xff: - emitpcode(POC_INCFSZ,popGet(AOP(left),offset)); - emitpcode(POC_GOTO,popGetLabel(lbl->key)); - emit_skip=0; - break; - default: - emitpcode(POC_MOVFW,popGet(AOP(left),offset)); - emitpcode(POC_XORLW,popGetLit(lit & 0xff)); - } - lit >>= 8; + switch (lit & 0xff) { + case 0: + emitpcode(POC_MOVFW,popGet(AOP(left),offset)); + break; + case 1: + emitpcode(POC_DECFSZW,popGet(AOP(left),offset)); + emitpcode(POC_INCF,popGet(AOP(result),res_offset)); + //emitpcode(POC_GOTO,popGetLabel(lbl->key)); + emit_skip=0; + break; + case 0xff: + emitpcode(POC_INCFSZW,popGet(AOP(left),offset)); + //emitpcode(POC_INCF,popGet(AOP(result),res_offset)); + //emitpcode(POC_INCFSZW,popGet(AOP(left),offset)); + emitpcode(POC_GOTO,popGetLabel(lbl_key)); + emit_skip=0; + break; + default: + emitpcode(POC_MOVFW,popGet(AOP(left),offset)); + emitpcode(POC_XORLW,popGetLit(lit & 0xff)); + } + lit >>= 8; + } else { + emitpcode(POC_MOVF,popGet(AOP(left),offset)); + } + if(emit_skip) { + if(AOP_TYPE(result) == AOP_CRY) { + pic14_emitcode(";***","%s %d",__FUNCTION__,__LINE__); + if(rIfx.condition) + emitSKPNZ; + else + emitSKPZ; + emitpcode(POC_GOTO,popGetLabel(rIfx.lbl->key)); } else { - emitpcode(POC_MOVF,popGet(AOP(left),offset)); - } - if(emit_skip) { - if(AOP_TYPE(result) == AOP_CRY) { - pic14_emitcode(";***","%s %d",__FUNCTION__,__LINE__); - if(rIfx.condition) - emitSKPNZ; - else - emitSKPZ; - emitpcode(POC_GOTO,popGetLabel(rIfx.lbl->key)); - } else { - /* fix me. probably need to check result size too */ - emitpcode(POC_CLRF,popGet(AOP(result),0)); - if(rIfx.condition) - emitSKPNZ; - else - emitSKPZ; - emitpcode(POC_INCF,popGet(AOP(result),0)); - } - if(ifx) - ifx->generated=1; + /* fix me. probably need to check result size too */ + //emitpcode(POC_CLRF,popGet(AOP(result),0)); + if(rIfx.condition) + emitSKPZ; + else + emitSKPNZ; + emitpcode(POC_GOTO,popGetLabel(lbl_key)); + //emitpcode(POC_INCF,popGet(AOP(result),res_offset)); } - emit_skip++; - offset++; + if(ifx) + ifx->generated=1; } - break; + emit_skip++; + offset++; + if(res_offset < res_size-1) + res_offset++; } +/* break; */ +/* } */ } else if(AOP_TYPE(right) == AOP_REG && AOP_TYPE(left) != AOP_DIR){ @@ -4638,6 +4689,8 @@ static void gencjne(operand *left, operand *right, operand *result, iCode *ifx) emitSKPZ; emitpcode(POC_GOTO,popGetLabel(rIfx.lbl->key)); offset++; + if(res_offset < res_size-1) + res_offset++; } }else{ @@ -4651,8 +4704,15 @@ static void gencjne(operand *left, operand *right, operand *result, iCode *ifx) offset++; } } + + emitpcode(POC_INCF,popGet(AOP(result),res_offset)); + if(!rIfx.condition) + emitpcode(POC_GOTO,popGetLabel(rIfx.lbl->key)); + emitpLabel(lbl->key); + DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); + if(ifx) ifx->generated = 1; } @@ -4939,6 +4999,7 @@ static void genCmpEq (iCode *ic, iCode *ifx) pic14_outBitC(result); } else { + DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); gencjne(left,right,result,ifx); /* if(ifx) @@ -5025,7 +5086,7 @@ static iCode *ifxForOp ( operand *op, iCode *ic ) static void genAndOp (iCode *ic) { operand *left,*right, *result; - symbol *tlbl; +/* symbol *tlbl; */ DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); /* note here that && operations that are in an @@ -5035,20 +5096,26 @@ static void genAndOp (iCode *ic) aopOp((right=IC_RIGHT(ic)),ic,FALSE); aopOp((result=IC_RESULT(ic)),ic,FALSE); + DEBUGpic14_AopType(__LINE__,left,right,result); + + emitpcode(POC_MOVFW,popGet(AOP(left),0)); + emitpcode(POC_ANDFW,popGet(AOP(right),0)); + emitpcode(POC_MOVWF,popGet(AOP(result),0)); + /* if both are bit variables */ - if (AOP_TYPE(left) == AOP_CRY && - AOP_TYPE(right) == AOP_CRY ) { - pic14_emitcode("mov","c,%s",AOP(left)->aopu.aop_dir); - pic14_emitcode("anl","c,%s",AOP(right)->aopu.aop_dir); - pic14_outBitC(result); - } else { - tlbl = newiTempLabel(NULL); - pic14_toBoolean(left); - pic14_emitcode("jz","%05d_DS_",tlbl->key+100); - pic14_toBoolean(right); - pic14_emitcode("","%05d_DS_:",tlbl->key+100); - pic14_outBitAcc(result); - } +/* if (AOP_TYPE(left) == AOP_CRY && */ +/* AOP_TYPE(right) == AOP_CRY ) { */ +/* pic14_emitcode("mov","c,%s",AOP(left)->aopu.aop_dir); */ +/* pic14_emitcode("anl","c,%s",AOP(right)->aopu.aop_dir); */ +/* pic14_outBitC(result); */ +/* } else { */ +/* tlbl = newiTempLabel(NULL); */ +/* pic14_toBoolean(left); */ +/* pic14_emitcode("jz","%05d_DS_",tlbl->key+100); */ +/* pic14_toBoolean(right); */ +/* pic14_emitcode("","%05d_DS_:",tlbl->key+100); */ +/* pic14_outBitAcc(result); */ +/* } */ freeAsmop(left,NULL,ic,(RESULTONSTACK(ic) ? FALSE : TRUE)); freeAsmop(right,NULL,ic,(RESULTONSTACK(ic) ? FALSE : TRUE)); @@ -6020,8 +6087,9 @@ static void genInline (iCode *ic) while (*bp) { if (*bp == '\n') { *bp++ = '\0'; - pic14_emitcode(bp1,""); - addpCode2pBlock(pb,newpCodeInlineP(bp1)); + + if(*bp1) + addpCode2pBlock(pb,AssembleLine(bp1)); bp1 = bp; } else { if (*bp == ':') { @@ -6034,11 +6102,11 @@ static void genInline (iCode *ic) bp++; } } - if (bp1 != bp) { - pic14_emitcode(bp1,""); - addpCode2pBlock(pb,newpCodeInlineP(bp1)); - } - /* pic14_emitcode("",buffer); */ + if ((bp1 != bp) && *bp1) + addpCode2pBlock(pb,AssembleLine(bp1)); + + Safe_free(buffer); + _G.inLine -= (!options.asmpeep); } @@ -6709,10 +6777,12 @@ static void shiftR2Left2Result (operand *left, int offl, /* don't crash result[offr] */ MOVA(aopGet(AOP(left),offl,FALSE,FALSE)); pic14_emitcode("xch","a,%s", aopGet(AOP(left),offl+MSB16,FALSE,FALSE)); - } else { + } +/* else { movLeft2Result(left,offl, result, offr); MOVA(aopGet(AOP(left),offl+MSB16,FALSE,FALSE)); } +*/ /* a:x >> shCount (x = lsb(result))*/ /* if(sign) @@ -6812,15 +6882,17 @@ static void shiftR2Left2Result (operand *left, int offl, emitpcode(POC_MOVWF,popGet(AOP(result),offr+MSB16)); emitpcode(POC_RLFW, popGet(AOP(left),offl+MSB16)); emitpcode(POC_MOVWF,popGet(AOP(result),offr)); - emitpcode(POC_RLFW, popGet(AOP(result),offr+MSB16)); + emitpcode(POC_RLF, popGet(AOP(result),offr+MSB16)); + emitpcode(POC_RLF, popGet(AOP(result),offr)); + emitpcode(POC_RLFW, popGet(AOP(result),offr+MSB16)); emitpcode(POC_ANDLW,popGetLit(0x03)); if(sign) { emitpcode(POC_BTFSC, newpCodeOpBit(aopGet(AOP(result),offr+MSB16,FALSE,FALSE),0,0)); emitpcode(POC_IORLW,popGetLit(0xfc)); } - emitpcode(POC_MOVWF,popGet(AOP(result),offr)); - emitpcode(POC_RLF, popGet(AOP(result),offr)); + emitpcode(POC_MOVWF,popGet(AOP(result),offr+MSB16)); + //emitpcode(POC_RLF, popGet(AOP(result),offr)); } @@ -7885,7 +7957,7 @@ static void genUnpackBits (operand *result, char *rname, int ptype) return ; } - +#if 0 /*-----------------------------------------------------------------*/ /* genDataPointerGet - generates code when ptr offset is known */ /*-----------------------------------------------------------------*/ @@ -7920,7 +7992,7 @@ static void genDataPointerGet (operand *left, freeAsmop(left,NULL,ic,TRUE); freeAsmop(result,NULL,ic,TRUE); } - +#endif /*-----------------------------------------------------------------*/ /* genNearPointerGet - pic14_emitcode for near pointer fetch */ /*-----------------------------------------------------------------*/ @@ -7929,11 +8001,11 @@ static void genNearPointerGet (operand *left, iCode *ic) { asmop *aop = NULL; - regs *preg = NULL ; + //regs *preg = NULL ; char *rname ; sym_link *rtype, *retype; sym_link *ltype = operandType(left); - char buffer[80]; + //char buffer[80]; DEBUGpic14_emitcode ("; ***","%s %d",__FUNCTION__,__LINE__); @@ -8908,9 +8980,17 @@ static void genGenPointerSet (operand *right, /* hack hack! see if this the FSR. If so don't load W */ if(AOP_TYPE(right) != AOP_ACC) { + emitpcode(POC_MOVFW,popGet(AOP(result),0)); emitpcode(POC_MOVWF,popCopyReg(&pc_fsr)); + if(AOP_SIZE(result) > 1) { + emitpcode(POC_BCF, popCopyGPR2Bit(PCOP(&pc_status),PIC_IRP_BIT)); + emitpcode(POC_BTFSC,newpCodeOpBit(aopGet(AOP(result),1,FALSE,FALSE),0,0)); + emitpcode(POC_BSF, popCopyGPR2Bit(PCOP(&pc_status),PIC_IRP_BIT)); + + } + //if(size==2) //emitpcode(POC_DECF,popCopyReg(&pc_fsr)); //if(size==4) {