X-Git-Url: https://git.gag.com/?a=blobdiff_plain;ds=sidebyside;f=src%2Fpic%2Fpcode.c;h=3b2b7d91b14917ef41dd58cb28fe7291a35297f3;hb=f92718a10905fc3571ec395c2bf69b4cac7fdcf7;hp=b7d0cd461a9205f3df4c777bd6f2fb65f7eca80a;hpb=b74e476f6ba4e0e9e397c9aac11e92412fb0b71a;p=fw%2Fsdcc diff --git a/src/pic/pcode.c b/src/pic/pcode.c index b7d0cd46..3b2b7d91 100644 --- a/src/pic/pcode.c +++ b/src/pic/pcode.c @@ -50,12 +50,12 @@ peepCommand peepCommands[] = { // Eventually this will go into device dependent files: -pCodeOpReg pc_status = {{PO_STATUS, "_STATUS"}, -1, NULL,0,NULL}; +pCodeOpReg pc_status = {{PO_STATUS, "STATUS"}, -1, NULL,0,NULL}; pCodeOpReg pc_indf = {{PO_INDF, "INDF"}, -1, NULL,0,NULL}; pCodeOpReg pc_fsr = {{PO_FSR, "FSR"}, -1, NULL,0,NULL}; pCodeOpReg pc_intcon = {{PO_INTCON, ""}, -1, NULL,0,NULL}; pCodeOpReg pc_pcl = {{PO_PCL, "PCL"}, -1, NULL,0,NULL}; -pCodeOpReg pc_pclath = {{PO_PCLATH, "_PCLATH"}, -1, NULL,0,NULL}; +pCodeOpReg pc_pclath = {{PO_PCLATH, "PCLATH"}, -1, NULL,0,NULL}; pCodeOpReg pc_kzero = {{PO_GPR_REGISTER, "KZ"}, -1, NULL,0,NULL}; pCodeOpReg pc_wsave = {{PO_GPR_REGISTER, "WSAVE"}, -1, NULL,0,NULL}; @@ -77,7 +77,7 @@ static int peepOptimizing = 1; /* run the peephole optimizer if nonzero * static int functionInlining = 1; /* inline functions if nonzero */ int debug_verbose = 0; /* Set true to inundate .asm file */ -static int GpCodeSequenceNumber = 1; +// static int GpCodeSequenceNumber = 1; int GpcFlowSeq = 1; extern void RemoveUnusedRegisters(void); @@ -105,7 +105,7 @@ static void pCodePrintLabel(FILE *of, pCode *pc); static void pCodePrintFunction(FILE *of, pCode *pc); static void pCodeOpPrint(FILE *of, pCodeOp *pcop); static char *get_op_from_instruction( pCodeInstruction *pcc); -char *get_op( pCodeOp *pcop,char *buff,int buf_size); +char *get_op( pCodeOp *pcop,char *buff,size_t buf_size); int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd); int pCodePeepMatchRule(pCode *pc); void pBlockStats(FILE *of, pBlock *pb); @@ -136,6 +136,7 @@ pCodeInstruction pciADDWF = { 2, // num ops 1,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, (PCC_W | PCC_REGISTER), // inCond (PCC_REGISTER | PCC_Z) // outCond @@ -157,6 +158,7 @@ pCodeInstruction pciADDFW = { 2, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, (PCC_W | PCC_REGISTER), // inCond (PCC_W | PCC_Z) // outCond @@ -178,6 +180,7 @@ pCodeInstruction pciADDLW = { 1, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 1, // literal operand POC_NOP, (PCC_W | PCC_LITERAL), // inCond (PCC_W | PCC_Z | PCC_C | PCC_DC) // outCond @@ -199,6 +202,7 @@ pCodeInstruction pciANDLW = { 1, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 1, // literal operand POC_NOP, (PCC_W | PCC_LITERAL), // inCond (PCC_W | PCC_Z) // outCond @@ -220,6 +224,7 @@ pCodeInstruction pciANDWF = { 2, // num ops 1,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, (PCC_W | PCC_REGISTER), // inCond (PCC_REGISTER | PCC_Z) // outCond @@ -241,6 +246,7 @@ pCodeInstruction pciANDFW = { 2, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, (PCC_W | PCC_REGISTER), // inCond (PCC_W | PCC_Z) // outCond @@ -262,6 +268,7 @@ pCodeInstruction pciBCF = { 2, // num ops 1,1, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_BSF, (PCC_REGISTER | PCC_EXAMINE_PCOP), // inCond PCC_REGISTER // outCond @@ -283,6 +290,7 @@ pCodeInstruction pciBSF = { 2, // num ops 1,1, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_BCF, (PCC_REGISTER | PCC_EXAMINE_PCOP), // inCond (PCC_REGISTER | PCC_EXAMINE_PCOP) // outCond @@ -304,6 +312,7 @@ pCodeInstruction pciBTFSC = { 2, // num ops 0,1, // dest, bit instruction 1,1, // branch, skip + 0, // literal operand POC_BTFSS, (PCC_REGISTER | PCC_EXAMINE_PCOP), // inCond PCC_EXAMINE_PCOP // outCond @@ -325,6 +334,7 @@ pCodeInstruction pciBTFSS = { 2, // num ops 0,1, // dest, bit instruction 1,1, // branch, skip + 0, // literal operand POC_BTFSC, (PCC_REGISTER | PCC_EXAMINE_PCOP), // inCond PCC_EXAMINE_PCOP // outCond @@ -346,6 +356,7 @@ pCodeInstruction pciCALL = { 1, // num ops 0,0, // dest, bit instruction 1,0, // branch, skip + 0, // literal operand POC_NOP, PCC_NONE, // inCond PCC_NONE // outCond @@ -367,6 +378,7 @@ pCodeInstruction pciCOMF = { 2, // num ops 1,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, PCC_REGISTER, // inCond PCC_REGISTER // outCond @@ -388,6 +400,7 @@ pCodeInstruction pciCOMFW = { 2, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, PCC_REGISTER, // inCond PCC_W // outCond @@ -409,6 +422,7 @@ pCodeInstruction pciCLRF = { 1, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, PCC_NONE, // inCond PCC_REGISTER // outCond @@ -430,6 +444,7 @@ pCodeInstruction pciCLRW = { 0, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, PCC_NONE, // inCond PCC_W // outCond @@ -451,6 +466,7 @@ pCodeInstruction pciCLRWDT = { 0, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, PCC_NONE, // inCond PCC_NONE // outCond @@ -472,6 +488,7 @@ pCodeInstruction pciDECF = { 2, // num ops 1,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, PCC_REGISTER, // inCond PCC_REGISTER // outCond @@ -493,6 +510,7 @@ pCodeInstruction pciDECFW = { 2, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, PCC_REGISTER, // inCond PCC_W // outCond @@ -514,6 +532,7 @@ pCodeInstruction pciDECFSZ = { 2, // num ops 1,0, // dest, bit instruction 1,1, // branch, skip + 0, // literal operand POC_NOP, PCC_REGISTER, // inCond PCC_REGISTER // outCond @@ -535,6 +554,7 @@ pCodeInstruction pciDECFSZW = { 2, // num ops 0,0, // dest, bit instruction 1,1, // branch, skip + 0, // literal operand POC_NOP, PCC_REGISTER, // inCond PCC_W // outCond @@ -556,6 +576,7 @@ pCodeInstruction pciGOTO = { 1, // num ops 0,0, // dest, bit instruction 1,0, // branch, skip + 0, // literal operand POC_NOP, PCC_NONE, // inCond PCC_NONE // outCond @@ -577,6 +598,7 @@ pCodeInstruction pciINCF = { 2, // num ops 1,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, PCC_REGISTER, // inCond PCC_REGISTER // outCond @@ -598,6 +620,7 @@ pCodeInstruction pciINCFW = { 2, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, PCC_REGISTER, // inCond PCC_W // outCond @@ -619,6 +642,7 @@ pCodeInstruction pciINCFSZ = { 2, // num ops 1,0, // dest, bit instruction 1,1, // branch, skip + 0, // literal operand POC_NOP, PCC_REGISTER, // inCond PCC_REGISTER // outCond @@ -640,6 +664,7 @@ pCodeInstruction pciINCFSZW = { 2, // num ops 0,0, // dest, bit instruction 1,1, // branch, skip + 0, // literal operand POC_NOP, PCC_REGISTER, // inCond PCC_W // outCond @@ -661,6 +686,7 @@ pCodeInstruction pciIORWF = { 2, // num ops 1,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, (PCC_W | PCC_REGISTER), // inCond (PCC_REGISTER | PCC_Z) // outCond @@ -682,6 +708,7 @@ pCodeInstruction pciIORFW = { 2, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, (PCC_W | PCC_REGISTER), // inCond (PCC_W | PCC_Z) // outCond @@ -703,6 +730,7 @@ pCodeInstruction pciIORLW = { 1, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 1, // literal operand POC_NOP, (PCC_W | PCC_LITERAL), // inCond (PCC_W | PCC_Z) // outCond @@ -724,6 +752,7 @@ pCodeInstruction pciMOVF = { 2, // num ops 1,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, PCC_REGISTER, // inCond PCC_Z // outCond @@ -745,6 +774,7 @@ pCodeInstruction pciMOVFW = { 2, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, PCC_REGISTER, // inCond (PCC_W | PCC_Z) // outCond @@ -766,6 +796,7 @@ pCodeInstruction pciMOVWF = { 1, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, PCC_W, // inCond PCC_REGISTER // outCond @@ -773,7 +804,6 @@ pCodeInstruction pciMOVWF = { pCodeInstruction pciMOVLW = { {PC_OPCODE, NULL, NULL, 0, NULL, - // genericAnalyze, genericDestruct, genericPrint}, POC_MOVLW, @@ -787,6 +817,7 @@ pCodeInstruction pciMOVLW = { 1, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 1, // literal operand POC_NOP, (PCC_NONE | PCC_LITERAL), // inCond PCC_W // outCond @@ -807,6 +838,7 @@ pCodeInstruction pciNOP = { 0, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, PCC_NONE, // inCond PCC_NONE // outCond @@ -828,6 +860,7 @@ pCodeInstruction pciRETFIE = { 0, // num ops 0,0, // dest, bit instruction 1,0, // branch, skip + 0, // literal operand POC_NOP, PCC_NONE, // inCond PCC_NONE // outCond (not true... affects the GIE bit too) @@ -849,6 +882,7 @@ pCodeInstruction pciRETLW = { 1, // num ops 0,0, // dest, bit instruction 1,0, // branch, skip + 1, // literal operand POC_NOP, PCC_LITERAL, // inCond PCC_W // outCond @@ -870,6 +904,7 @@ pCodeInstruction pciRETURN = { 0, // num ops 0,0, // dest, bit instruction 1,0, // branch, skip + 0, // literal operand POC_NOP, PCC_NONE, // inCond PCC_NONE // outCond @@ -891,6 +926,7 @@ pCodeInstruction pciRLF = { 2, // num ops 1,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, (PCC_C | PCC_REGISTER), // inCond (PCC_REGISTER | PCC_Z | PCC_C | PCC_DC) // outCond @@ -912,6 +948,7 @@ pCodeInstruction pciRLFW = { 2, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, (PCC_C | PCC_REGISTER), // inCond (PCC_W | PCC_Z | PCC_C | PCC_DC) // outCond @@ -933,6 +970,7 @@ pCodeInstruction pciRRF = { 2, // num ops 1,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, (PCC_C | PCC_REGISTER), // inCond (PCC_REGISTER | PCC_Z | PCC_C | PCC_DC) // outCond @@ -954,6 +992,7 @@ pCodeInstruction pciRRFW = { 2, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, (PCC_C | PCC_REGISTER), // inCond (PCC_W | PCC_Z | PCC_C | PCC_DC) // outCond @@ -975,6 +1014,7 @@ pCodeInstruction pciSUBWF = { 2, // num ops 1,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, (PCC_W | PCC_REGISTER), // inCond (PCC_REGISTER | PCC_Z) // outCond @@ -996,6 +1036,7 @@ pCodeInstruction pciSUBFW = { 2, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, (PCC_W | PCC_REGISTER), // inCond (PCC_W | PCC_Z) // outCond @@ -1017,6 +1058,7 @@ pCodeInstruction pciSUBLW = { 1, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 1, // literal operand POC_NOP, (PCC_W | PCC_LITERAL), // inCond (PCC_W | PCC_Z | PCC_C | PCC_DC) // outCond @@ -1038,6 +1080,7 @@ pCodeInstruction pciSWAPF = { 2, // num ops 1,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, (PCC_REGISTER), // inCond (PCC_REGISTER) // outCond @@ -1059,6 +1102,7 @@ pCodeInstruction pciSWAPFW = { 2, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, (PCC_REGISTER), // inCond (PCC_W) // outCond @@ -1080,6 +1124,7 @@ pCodeInstruction pciTRIS = { 1, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, PCC_NONE, // inCond PCC_REGISTER // outCond @@ -1101,6 +1146,7 @@ pCodeInstruction pciXORWF = { 2, // num ops 1,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, (PCC_W | PCC_REGISTER), // inCond (PCC_REGISTER | PCC_Z) // outCond @@ -1122,6 +1168,7 @@ pCodeInstruction pciXORFW = { 2, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 0, // literal operand POC_NOP, (PCC_W | PCC_REGISTER), // inCond (PCC_W | PCC_Z) // outCond @@ -1143,6 +1190,7 @@ pCodeInstruction pciXORLW = { 1, // num ops 0,0, // dest, bit instruction 0,0, // branch, skip + 1, // literal operand POC_NOP, (PCC_W | PCC_LITERAL), // inCond (PCC_W | PCC_Z | PCC_C | PCC_DC) // outCond @@ -1152,10 +1200,8 @@ pCodeInstruction pciXORLW = { #define MAX_PIC14MNEMONICS 100 pCodeInstruction *pic14Mnemonics[MAX_PIC14MNEMONICS]; -/* This definition needs to be part of configure.in */ -// #define USE_VSNPRINTF -#ifdef USE_VSNPRINTF +#ifdef HAVE_VSNPRINTF // Alas, vsnprintf is not ANSI standard, and does not exist // on Solaris (and probably other non-Gnu flavored Unixes). @@ -1182,7 +1228,7 @@ void SAFE_snprintf(char **str, size_t *size, const char *format, ...) len = strlen(*str); if(len > *size) { fprintf(stderr,"WARNING, it looks like %s has overflowed\n",__FUNCTION__); - fprintf(stderr,"len = %d is > str size %d\n",len,*size); + fprintf(stderr,"len = %d is > str size %d\n",len,(int)*size); } *str += len; @@ -1190,7 +1236,7 @@ void SAFE_snprintf(char **str, size_t *size, const char *format, ...) } -#else // USE_VSNPRINTF +#else // HAVE_VSNPRINTF // This version is *not* safe, despite the name. @@ -1220,7 +1266,7 @@ void SAFE_snprintf(char **str, size_t *size, const char *format, ...) } -#endif // USE_VSNPRINTF +#endif // HAVE_VSNPRINTF extern void initStack(int base_address, int size); @@ -1239,9 +1285,9 @@ void pCodeInitRegisters(void) initStack(0xfff, 8); init_pic(port->processor); - pc_status.r = allocProcessorRegister(IDX_STATUS,"_STATUS", PO_STATUS, 0x80); + pc_status.r = allocProcessorRegister(IDX_STATUS,"STATUS", PO_STATUS, 0x80); pc_pcl.r = allocProcessorRegister(IDX_PCL,"PCL", PO_PCL, 0x80); - pc_pclath.r = allocProcessorRegister(IDX_PCLATH,"_PCLATH", PO_PCLATH, 0x80); + pc_pclath.r = allocProcessorRegister(IDX_PCLATH,"PCLATH", PO_PCLATH, 0x80); pc_fsr.r = allocProcessorRegister(IDX_FSR,"FSR", PO_FSR, 0x80); pc_indf.r = allocProcessorRegister(IDX_INDF,"INDF", PO_INDF, 0x80); pc_intcon.r = allocProcessorRegister(IDX_INTCON,"INTCON", PO_INTCON, 0x80); @@ -1381,7 +1427,7 @@ int getpCode(char *mnem,unsigned dest) while(pci) { if(STRCASECMP(pci->mnemonic, mnem) == 0) { - if((pci->num_ops <= 1) || (pci->isModReg == dest)) + if((pci->num_ops <= 1) || (pci->isModReg == dest) || (pci->isBitInst)) return(pci->op); } @@ -1521,6 +1567,7 @@ void copypCode(FILE *of, char dbName) if(getpBlock_dbName(pb) == dbName) { pBlockStats(of,pb); printpBlock(of,pb); + fprintf (of, "\n"); } } @@ -1539,7 +1586,7 @@ void pcode_test(void) char buffer[100]; /* create the file name */ - strcpy(buffer,srcFileName); + strcpy(buffer,dstFileName); strcat(buffer,".p"); if( !(pFile = fopen(buffer, "w" ))) { @@ -1931,7 +1978,7 @@ pCode *newpCodeLabel(char *name, int key) if(s) pcl->label = Safe_strdup(s); - + //fprintf(stderr,"newpCodeLabel: key=%d, name=%s\n",key, ((s)?s:"")); return ( (pCode *)pcl); } @@ -2007,6 +2054,7 @@ pCodeOp *newpCodeOpLabel(char *name, int key) ((pCodeOpLabel *)pcop)->key = key; + //fprintf(stderr,"newpCodeOpLabel: key=%d, name=%s\n",key,((s)?s:"")); return pcop; } @@ -2035,15 +2083,19 @@ pCodeOp *newpCodeOpLit(int lit) /*-----------------------------------------------------------------*/ /*-----------------------------------------------------------------*/ -pCodeOp *newpCodeOpImmd(char *name, int offset, int index, int code_space) +pCodeOp *newpCodeOpImmd(char *name, int offset, int index, int code_space, int is_func) { pCodeOp *pcop; pcop = Safe_calloc(1,sizeof(pCodeOpImmd) ); pcop->type = PO_IMMEDIATE; if(name) { - regs *r = dirregWithName(name); + regs *r = NULL; pcop->name = Safe_strdup(name); + + if(!is_func) + r = dirregWithName(name); + PCOI(pcop)->r = r; if(r) { //fprintf(stderr, " newpCodeOpImmd reg %s exists\n",name); @@ -2060,6 +2112,7 @@ pCodeOp *newpCodeOpImmd(char *name, int offset, int index, int code_space) PCOI(pcop)->index = index; PCOI(pcop)->offset = offset; PCOI(pcop)->_const = code_space; + PCOI(pcop)->_function = is_func; return pcop; } @@ -2106,6 +2159,9 @@ pCodeOp *newpCodeOpBit(char *s, int bit, int inBitSpace) PCORB(pcop)->bit = bit; PCORB(pcop)->inBitSpace = inBitSpace; + /* pCodeOpBit is derived from pCodeOpReg. We need to init this too */ + PCOR(pcop)->r = NULL; + PCOR(pcop)->rIdx = 0; return pcop; } @@ -2133,7 +2189,6 @@ pCodeOp *newpCodeOpReg(int rIdx) if(PCOR(pcop)->r) PCOR(pcop)->rIdx = PCOR(pcop)->r->rIdx; - //fprintf(stderr, "newpcodeOpReg - rIdx = %d\n", PCOR(pcop)->r->rIdx); } pcop->type = PCOR(pcop)->r->pc_type; @@ -2178,6 +2233,14 @@ pCodeOp *newpCodeOp(char *name, PIC_OPTYPE type) pcop = newpCodeOpReg(-1); break; + case PO_GPR_POINTER: + case PO_GPR_REGISTER: + if(name) + pcop = newpCodeOpRegFromStr(name); + else + pcop = newpCodeOpReg(-1); + break; + default: pcop = Safe_calloc(1,sizeof(pCodeOp) ); pcop->type = type; @@ -2434,7 +2497,7 @@ void pBlockRegs(FILE *of, pBlock *pb) /*-----------------------------------------------------------------*/ /*-----------------------------------------------------------------*/ -char *get_op(pCodeOp *pcop,char *buffer, int size) +char *get_op(pCodeOp *pcop,char *buffer, size_t size) { regs *r; static char b[50]; @@ -2455,7 +2518,8 @@ char *get_op(pCodeOp *pcop,char *buffer, int size) SAFE_snprintf(&buffer,&size,"%s",PCOR(pcop)->r->name); return buffer; } - return PCOR(pcop)->r->name; + //return PCOR(pcop)->r->name; + return pcop->name; break; case PO_GPR_TEMP: r = pic14_regWithIdx(PCOR(pcop)->r->rIdx); @@ -2474,10 +2538,19 @@ char *get_op(pCodeOp *pcop,char *buffer, int size) if(PCOI(pcop)->_const) { if( PCOI(pcop)->offset && PCOI(pcop)->offset<4) { - SAFE_snprintf(&s,&size,"(((%s+%d) >> %d)&0xff)", - pcop->name, - PCOI(pcop)->index, - 8 * PCOI(pcop)->offset ); + switch(PCOI(pcop)->offset) { + case 0: + SAFE_snprintf(&s,&size,"low %s",pcop->name); + break; + case 1: + SAFE_snprintf(&s,&size,"high %s",pcop->name); + break; + default: + 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); } else { @@ -2486,8 +2559,18 @@ char *get_op(pCodeOp *pcop,char *buffer, int size) SAFE_snprintf(&s,&size,"(%s + %d)", pcop->name, PCOI(pcop)->index ); - } else - SAFE_snprintf(&s,&size,"%s",pcop->name); + } else { + switch(PCOI(pcop)->offset) { + case 0: + SAFE_snprintf(&s,&size,"%s",pcop->name); + break; + case 1: + SAFE_snprintf(&s,&size,"high %s",pcop->name); + break; + default: + SAFE_snprintf(&s,&size,"(%s >> %d)&0xff",pcop->name, 8*PCOI(pcop)->offset); + } + } } return buffer; @@ -2527,8 +2610,11 @@ static char *get_op_from_instruction( pCodeInstruction *pcc) if(pcc ) return get_op(pcc->pcop,NULL,0); - + + /* gcc 3.2: warning: concatenation of string literals with __FUNCTION__ is deprecated return ("ERROR Null: "__FUNCTION__); + */ + return ("ERROR Null: get_op_from_instruction"); } @@ -2542,7 +2628,7 @@ static void pCodeOpPrint(FILE *of, pCodeOp *pcop) /*-----------------------------------------------------------------*/ /*-----------------------------------------------------------------*/ -char *pCode2str(char *str, int size, pCode *pc) +char *pCode2str(char *str, size_t size, pCode *pc) { char *s = str; @@ -3681,7 +3767,9 @@ void LinkFlow(pBlock *pb) if( (pct = findLabelinpBlock(pb,pcol)) != NULL) LinkFlow_pCode(PCI(pc),PCI(pct)); else - fprintf(stderr, "ERROR: %s, couldn't find label\n",__FUNCTION__); + fprintf(stderr, "ERROR: %s, couldn't find label. key=%d,lab=%s\n", + __FUNCTION__,pcol->key,((PCOP(pcol)->name)?PCOP(pcol)->name:"-")); + //fprintf(stderr,"newpCodeOpLabel: key=%d, name=%s\n",key,((s)?s:"")); continue; } @@ -3815,7 +3903,7 @@ void BanksUsedFlow(pBlock *pb) /*-----------------------------------------------------------------*/ /*-----------------------------------------------------------------*/ -void insertBankSwitch(pCode *pc, int Set_Clear, int RP_BankBit) +void insertBankSwitch(int position, pCode *pc, int Set_Clear, int RP_BankBit) { pCode *new_pc; @@ -3828,7 +3916,15 @@ void insertBankSwitch(pCode *pc, int Set_Clear, int RP_BankBit) new_pc = newpCode((Set_Clear ? POC_BSF : POC_BCF), popCopyGPR2Bit(PCOP(&pc_status),RP_BankBit)); - pCodeInsertAfter(pc->prev, new_pc); + if(position) { + /* insert the bank switch after this pc instruction */ + pCode *pcnext = findNextInstruction(pc); + pCodeInsertAfter(pc, new_pc); + if(pcnext) + pc = pcnext; + + } else + pCodeInsertAfter(pc->prev, new_pc); /* Move the label, if there is one */ @@ -3858,7 +3954,6 @@ void FixRegisterBankingInFlow(pCodeFlow *pcfl, int cur_bank) while(isPCinFlow(pc,PCODE(pcfl))) { - reg = getRegFromInstruction(pc); #if 0 if(reg) { @@ -3868,46 +3963,41 @@ void FixRegisterBankingInFlow(pCodeFlow *pcfl, int cur_bank) } #endif - if(reg && REG_BANK(reg)!=cur_bank) { + if( ( (reg && REG_BANK(reg)!=cur_bank) || + ((PCI(pc)->op == POC_CALL) && (cur_bank != 0) ) ) && + (!isPCI_LIT(pc)) ){ + /* Examine the instruction before this one to make sure it is * not a skip type instruction */ pcprev = findPrevpCode(pc->prev, PC_OPCODE); + if(!pcprev || (pcprev && !isPCI_SKIP(pcprev))) { - int b = cur_bank ^ REG_BANK(reg); + int b; + int reg_bank; + + reg_bank = (reg) ? REG_BANK(reg) : 0; + + b = cur_bank ^ reg_bank; //fprintf(stderr, "Cool! can switch banks\n"); - cur_bank = REG_BANK(reg); + cur_bank = reg_bank; switch(b & 3) { case 0: break; case 1: - insertBankSwitch(pc, cur_bank&1, PIC_RP0_BIT); + insertBankSwitch(0, pc, cur_bank&1, PIC_RP0_BIT); break; case 2: - insertBankSwitch(pc, cur_bank&2, PIC_RP1_BIT); - insertBankSwitch(pc, cur_bank&2, PIC_RP1_BIT); + insertBankSwitch(0, pc, cur_bank&2, PIC_RP1_BIT); + insertBankSwitch(0, pc, cur_bank&2, PIC_RP1_BIT); break; case 3: if(cur_bank & 3) { - insertBankSwitch(pc, cur_bank&1, PIC_RP0_BIT); - insertBankSwitch(pc, cur_bank&2, PIC_RP1_BIT); + insertBankSwitch(0, pc, cur_bank&1, PIC_RP0_BIT); + insertBankSwitch(0, pc, cur_bank&2, PIC_RP1_BIT); } else - insertBankSwitch(pc, -1, -1); + insertBankSwitch(0, pc, -1, -1); break; - /* - new_pc = newpCode(((cur_bank&1) ? POC_BSF : POC_BCF), - popCopyGPR2Bit(PCOP(&pc_status),PIC_RP0_BIT)); - pCodeInsertAfter(pc->prev, new_pc); - if(PCI(pc)->label) { - PCI(new_pc)->label = PCI(pc)->label; - PCI(pc)->label = NULL; - } - */ - /* - new_pc = newpCode(((cur_bank&1) ? POC_BCF : POC_BSF), - popCopyGPR2Bit(PCOP(&pc_status),PIC_RP0_BIT)); - pCodeInsertAfter(pc, new_pc); - */ } @@ -4253,6 +4343,8 @@ pCode * findInstructionUsingLabel(pCodeLabel *pcl, pCode *pcs) void exchangeLabels(pCodeLabel *pcl, pCode *pc) { + char *s=NULL; + if(isPCI(pc) && (PCI(pc)->pcop) && (PCI(pc)->pcop->type == PO_LABEL)) { @@ -4263,9 +4355,19 @@ void exchangeLabels(pCodeLabel *pcl, pCode *pc) if(pcol->pcop.name) free(pcol->pcop.name); - sprintf(buffer,"_%05d_DS_",pcl->key); - - pcol->pcop.name = Safe_strdup(buffer); + /* If the key is negative, then we (probably) have a label to + * a function and the name is already defined */ + + if(pcl->key>0) + sprintf(s=buffer,"_%05d_DS_",pcl->key); + else + s = pcl->label; + + //sprintf(buffer,"_%05d_DS_",pcl->key); + if(!s) { + fprintf(stderr, "ERROR %s:%d function label is null\n",__FUNCTION__,__LINE__); + } + pcol->pcop.name = Safe_strdup(s); pcol->key = pcl->key; //pc->print(stderr,pc); @@ -4485,11 +4587,10 @@ void FixRegisterBanking(pBlock *pb) { pCode *pc=NULL; pCode *pcprev=NULL; - pCode *new_pc; int cur_bank; regs *reg; - // return; + if(!pb) return; @@ -4513,63 +4614,151 @@ void FixRegisterBanking(pBlock *pb) //genericPrint(stderr, pc); reg = getRegFromInstruction(pc); - #if 0 +#if 0 if(reg) { fprintf(stderr, " %s ",reg->name); - fprintf(stderr, "addr = 0x%03x, bank = %d\n",reg->address,REG_BANK(reg)); + fprintf(stderr, "addr = 0x%03x, bank = %d, bit=%d\n", + reg->address,REG_BANK(reg),reg->isBitField); } - #endif - if(reg && REG_BANK(reg)!=cur_bank) { +#endif + + if( ( (reg && REG_BANK(reg)!=cur_bank) || + ((PCI(pc)->op == POC_CALL) && (cur_bank != 0) ) ) && + (!isPCI_LIT(pc)) ){ + + /* Examine the instruction before this one to make sure it is * not a skip type instruction */ pcprev = findPrevpCode(pc->prev, PC_OPCODE); - if(!pcprev || (pcprev && !isPCI_SKIP(pcprev))) { - int b = cur_bank ^ REG_BANK(reg); - //fprintf(stderr, "Cool! can switch banks\n"); - cur_bank = REG_BANK(reg); - if(b & 1) { - new_pc = newpCode(((cur_bank&1) ? POC_BSF : POC_BCF), - popCopyGPR2Bit(PCOP(&pc_status),PIC_RP0_BIT)); - pCodeInsertAfter(pc->prev, new_pc); - if(PCI(pc)->label) { - PCI(new_pc)->label = PCI(pc)->label; - PCI(pc)->label = NULL; - } - /* - new_pc = newpCode(((cur_bank&1) ? POC_BCF : POC_BSF), - popCopyGPR2Bit(PCOP(&pc_status),PIC_RP0_BIT)); - pCodeInsertAfter(pc, new_pc); - */ + if(!pcprev || (pcprev && !isPCI_SKIP(pcprev))) { + int b; + int reg_bank; + + reg_bank = (reg) ? REG_BANK(reg) : 0; + + b = cur_bank ^ reg_bank; + + cur_bank = reg_bank; + switch(b & 3) { + case 0: + break; + case 1: + insertBankSwitch(0, pc, cur_bank&1, PIC_RP0_BIT); + break; + case 2: + insertBankSwitch(0, pc, cur_bank&2, PIC_RP1_BIT); + insertBankSwitch(0, pc, cur_bank&2, PIC_RP1_BIT); + break; + case 3: + if(cur_bank & 3) { + insertBankSwitch(0, pc, cur_bank&1, PIC_RP0_BIT); + insertBankSwitch(0, pc, cur_bank&2, PIC_RP1_BIT); + } else + insertBankSwitch(0, pc, -1, -1); + break; } - } else { + }else { //fprintf(stderr, "Bummer can't switch banks\n"); ; } } + + pcprev = pc; + } - pcprev = pc; pc = pc->next; // } while(pc && !(isPCFL(pc))); }while (pc); - if(pcprev && cur_bank) { - /* Brute force - make sure that we point to bank 0 at the - * end of each flow block */ - new_pc = newpCode(POC_BCF, - popCopyGPR2Bit(PCOP(&pc_status),PIC_RP0_BIT)); - pCodeInsertAfter(pcprev, new_pc); - cur_bank = 0; + if(pcprev && cur_bank) { + + int pos = 1; /* Assume that the bank swithc instruction(s) + * are inserted after this instruction */ + + if((PCI(pcprev)->op == POC_RETLW) || + (PCI(pcprev)->op == POC_RETURN) || + (PCI(pcprev)->op == POC_RETFIE)) { + + /* oops, a RETURN - we need to switch banks *before* the RETURN */ + + pos = 0; + + } + + /* Brute force - make sure that we point to bank 0 at the + * end of each flow block */ + + switch(cur_bank & 3) { + case 0: + break; + case 1: + insertBankSwitch(pos, pcprev, 0, PIC_RP0_BIT); + break; + case 2: + insertBankSwitch(pos, pcprev, 0, PIC_RP1_BIT); + insertBankSwitch(pos, pcprev, 0, PIC_RP1_BIT); + break; + case 3: + insertBankSwitch(pos, pcprev, -1, -1); + break; + } +/* + new_pc = newpCode(POC_BCF, + popCopyGPR2Bit(PCOP(&pc_status),PIC_RP0_BIT)); + pCodeInsertAfter(pcprev, new_pc); +*/ + cur_bank = 0; + //fprintf(stderr, "Brute force switch\n"); + } } + + + +#if 0 + if(reg && REG_BANK(reg)!=cur_bank) { + //fprintf(stderr,"need to switch banks\n"); + /* Examine the instruction before this one to make sure it is + * not a skip type instruction */ + pcprev = findPrevpCode(pc->prev, PC_OPCODE); + if(!pcprev || (pcprev && !isPCI_SKIP(pcprev))) { + int b = cur_bank ^ REG_BANK(reg); + + cur_bank = REG_BANK(reg); + + switch(b & 3) { + case 0: + break; + case 1: + insertBankSwitch(0, pc, cur_bank&1, PIC_RP0_BIT); + break; + case 2: + insertBankSwitch(0, pc, cur_bank&2, PIC_RP1_BIT); + insertBankSwitch(0, pc, cur_bank&2, PIC_RP1_BIT); + break; + case 3: + if(cur_bank & 3) { + insertBankSwitch(0, pc, cur_bank&1, PIC_RP0_BIT); + insertBankSwitch(0, pc, cur_bank&2, PIC_RP1_BIT); + } else + insertBankSwitch(0, pc, -1, -1); + break; + + } +#endif + + + + void pBlockDestruct(pBlock *pb) {