X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=src%2Fpic%2Fpcode.c;h=5128c5ad85a8636f87c767f2fe5bb34699f9ac62;hb=195ee3f3ee25ce2c5f2a59fbd2779c4cb80527c3;hp=fcc80b32910b4fc9ae31c5d21898023e7638958d;hpb=63094771a3c40f1f291edb2272d917c1f8ab81e8;p=fw%2Fsdcc diff --git a/src/pic/pcode.c b/src/pic/pcode.c index fcc80b32..5128c5ad 100644 --- a/src/pic/pcode.c +++ b/src/pic/pcode.c @@ -82,6 +82,11 @@ int debug_verbose = 0; /* Set true to inundate .asm file */ // static int GpCodeSequenceNumber = 1; int GpcFlowSeq = 1; +/* statistics (code size estimation) */ +static unsigned int pcode_insns = 0; +static unsigned int pcode_doubles = 0; + + unsigned maxIdx; /* This keeps track of the maximum register index for call tree register reuse */ unsigned peakIdx; /* This keeps track of the peak register index for call tree register reuse */ @@ -119,6 +124,8 @@ pBlock *newpBlock(void); pCodeOp *popCopyGPR2Bit(pCodeOp *pc, int bitval); void pCodeRegMapLiveRanges(pBlock *pb); +pBranch * pBranchAppend(pBranch *h, pBranch *n); + /****************************************************************/ /* PIC Instructions */ @@ -1390,7 +1397,7 @@ void pCodeInitRegisters(void) /* */ /*-----------------------------------------------------------------*/ -int mnem2key(char const *mnem) +int mnem2key(unsigned char const *mnem) { int key = 0; @@ -1639,7 +1646,7 @@ void copypCode(FILE *of, char dbName) if(!of || !the_pFile) return; - + for(pb = the_pFile->pbHead; pb; pb = pb->next) { if(getpBlock_dbName(pb) == dbName) { pBlockStats(of,pb); @@ -1649,6 +1656,21 @@ void copypCode(FILE *of, char dbName) } } + +void resetpCodeStatistics (void) +{ + pcode_insns = pcode_doubles = 0; +} + +void dumppCodeStatistics (FILE *of) +{ + /* dump statistics */ + fprintf (of, "\n"); + fprintf (of, ";\tcode size estimation:\n"); + fprintf (of, ";\t%5u+%5u = %5u instructions (%5u byte)\n", pcode_insns, pcode_doubles, pcode_insns + pcode_doubles, 2*(pcode_insns + 2*pcode_doubles)); + fprintf (of, "\n"); +} + void pcode_test(void) { @@ -1994,7 +2016,7 @@ pCodeFlowLink *newpCodeFlowLink(pCodeFlow *pcflow) /* newpCodeCSource - create a new pCode Source Symbol */ /*-----------------------------------------------------------------*/ -pCode *newpCodeCSource(int ln, char *f, char *l) +pCode *newpCodeCSource(int ln, char *f, const char *l) { pCodeCSource *pccs; @@ -2045,7 +2067,7 @@ pCode *newpCodeAsmDir(char *asdir, char *argfmt, ...) if(asdir && *asdir) { - while(isspace(*asdir))asdir++; // strip any white space from the beginning + while(isspace((unsigned char)*asdir))asdir++; // strip any white space from the beginning pcad->directive = Safe_strdup( asdir ); } @@ -2058,7 +2080,7 @@ pCode *newpCodeAsmDir(char *asdir, char *argfmt, ...) va_end(ap); - while(isspace(*lbp))lbp++; + while(isspace((unsigned char)*lbp))lbp++; if(lbp && *lbp) pcad->arg = Safe_strdup( lbp ); @@ -2610,9 +2632,19 @@ void printpBlock(FILE *of, pBlock *pb) if(!of) of = stderr; - for(pc = pb->pcHead; pc; pc = pc->next) + for(pc = pb->pcHead; pc; pc = pc->next) { printpCode(of,pc); - + + if (isPCI(pc)) + { + if (isPCI(pc) && (PCI(pc)->op == POC_PAGESEL || PCI(pc)->op == POC_BANKSEL)) { + pcode_doubles++; + } else { + pcode_insns++; + } + } + } // for + } /*-----------------------------------------------------------------*/ @@ -2636,7 +2668,20 @@ void unlinkpCode(pCode *pc) pc->prev->next = pc->next; if(pc->next) pc->next->prev = pc->prev; - + +#if 0 + /* RN: I believe this should be right here, but this did not + * cure the bug I was hunting... */ + /* must keep labels -- attach to following instruction */ + if (isPCI(pc) && PCI(pc)->label && pc->next) + { + pCodeInstruction *pcnext = PCI(findNextInstruction (pc->next)); + if (pcnext) + { + pBranchAppend (pcnext->label, PCI(pc)->label); + } + } +#endif pc->prev = pc->next = NULL; } } @@ -2966,41 +3011,44 @@ char *get_op(pCodeOp *pcop,char *buffer, size_t size) case PO_IMMEDIATE: s = buffer; - if(PCOI(pcop)->_const) { - if( PCOI(pcop)->offset && PCOI(pcop)->offset<4) { + if( PCOI(pcop)->offset >= 0 && PCOI(pcop)->offset<4) { switch(PCOI(pcop)->offset) { case 0: - SAFE_snprintf(&s,&size,"low %s",pcop->name); + SAFE_snprintf(&s,&size,"low (%s+%d)",pcop->name, PCOI(pcop)->index); break; case 1: - SAFE_snprintf(&s,&size,"high %s",pcop->name); + SAFE_snprintf(&s,&size,"high (%s+%d)",pcop->name, PCOI(pcop)->index); break; default: + fprintf (stderr, "PO_IMMEDIATE/_const/offset=%d\n", PCOI(pcop)->offset); + assert ( !"offset too large" ); SAFE_snprintf(&s,&size,"(((%s+%d) >> %d)&0xff)", pcop->name, PCOI(pcop)->index, 8 * PCOI(pcop)->offset ); } } else - SAFE_snprintf(&s,&size,"LOW(%s+%d)",pcop->name,PCOI(pcop)->index); + SAFE_snprintf(&s,&size,"LOW (%s+%d)",pcop->name,PCOI(pcop)->index); } else { - - if( PCOI(pcop)->index) { // && PCOI(pcc->pcop)->offset<4) { + if( !PCOI(pcop)->offset) { // && PCOI(pcc->pcop)->offset<4) { SAFE_snprintf(&s,&size,"(%s + %d)", pcop->name, - PCOI(pcop)->index ); + PCOI(pcop)->index); } else { switch(PCOI(pcop)->offset) { case 0: - SAFE_snprintf(&s,&size,"%s",pcop->name); + SAFE_snprintf(&s,&size,"(%s + %d)",pcop->name, PCOI(pcop)->index); break; case 1: - SAFE_snprintf(&s,&size,"high %s",pcop->name); + SAFE_snprintf(&s,&size,"high (%s + %d)",pcop->name, PCOI(pcop)->index); break; default: - SAFE_snprintf(&s,&size,"(%s >> %d)&0xff",pcop->name, 8*PCOI(pcop)->offset); + fprintf (stderr, "PO_IMMEDIATE/mutable/offset=%d\n", PCOI(pcop)->offset); + assert ( !"offset too large" ); + SAFE_snprintf(&s,&size,"((%s + %d) >> %d)&0xff",pcop->name, PCOI(pcop)->index, 8*PCOI(pcop)->offset); + break; } } } @@ -3146,7 +3194,8 @@ char *pCode2str(char *str, size_t size, pCode *pc) SAFE_snprintf(&s,&size,";\t--FLOW change\n"); break; case PC_CSOURCE: - SAFE_snprintf(&s,&size,";#CSRC\t%s %d\n; %s\n", PCCS(pc)->file_name, PCCS(pc)->line_number, PCCS(pc)->line); +// SAFE_snprintf(&s,&size,";#CSRC\t%s %d\n; %s\n", PCCS(pc)->file_name, PCCS(pc)->line_number, PCCS(pc)->line); + SAFE_snprintf(&s,&size,"%s\t.line\t%d; \"%s\"\t%s\n",(options.debug?"":";"),PCCS(pc)->line_number, PCCS(pc)->file_name, PCCS(pc)->line); break; case PC_ASMDIR: if(PCAD(pc)->directive) { @@ -3260,7 +3309,8 @@ static void genericPrint(FILE *of, pCode *pc) break; case PC_CSOURCE: - fprintf(of,";#CSRC\t%s %d\n; %s\n", PCCS(pc)->file_name, PCCS(pc)->line_number, PCCS(pc)->line); +// fprintf(of,";#CSRC\t%s %d\n; %s\n", PCCS(pc)->file_name, PCCS(pc)->line_number, PCCS(pc)->line); + fprintf(of,"%s\t.line\t%d; \"%s\"\t%s\n", (options.debug?"":";"), PCCS(pc)->line_number, PCCS(pc)->file_name, PCCS(pc)->line); break; case PC_ASMDIR: @@ -4482,9 +4532,11 @@ static int BankSelect(pCodeInstruction *pci, int cur_bank, regs *reg) } } +#if 1 if (LastRegIdx == reg->rIdx) // If this is the same register as last time then it is in same bank return cur_bank; LastRegIdx = reg->rIdx; +#endif if (reg->isFixed) { bank = REG_BANK(reg); @@ -4595,6 +4647,7 @@ static int DoBankSelect(pCode *pc, int cur_bank) { if (isCALL(pc)) { pCode *pcf = findFunction(get_op_from_instruction(PCI(pc))); + LastRegIdx = -1; /* do not know which register is touched in the called function... */ if (pcf && isPCF(pcf)) { pCode *pcfr; int rbank = 'U'; // Undetermined @@ -4604,9 +4657,9 @@ static int DoBankSelect(pCode *pc, int cur_bank) { if (isPCI(pcfr)) { if ((PCI(pcfr)->op==POC_RETURN) || (PCI(pcfr)->op==POC_RETLW)) { if (rbank == 'U') - rbank = PCFL(pcfr)->lastBank; + rbank = PCI(pcfr)->pcflow->lastBank; else - if (rbank != PCFL(pcfr)->lastBank) + if (rbank != PCI(pcfr)->pcflow->lastBank) return -1; // Unknown bank - multiple returns with different banks } } @@ -4618,6 +4671,8 @@ static int DoBankSelect(pCode *pc, int cur_bank) { /* Extern functions may use registers in different bank - must call banksel */ return -1; /* Unknown bank */ } + /* play safe... */ + return -1; } if ((isPCI(pc)) && (PCI(pc)->op == POC_BANKSEL)) { @@ -5744,7 +5799,7 @@ void AnalyzepCode(char dbName) pBlockMergeLabels(pb); AnalyzepBlock(pb); } else { - DFPRINTF((stderr," skipping block analysis dbName=%c blockname=%c\n",dbName,getpBlock_dbName)); + DFPRINTF((stderr," skipping block analysis dbName=%c blockname=%c\n",dbName,getpBlock_dbName(pb))); } }