* src/port.h (struct PORT),
[fw/sdcc] / src / pic / pcode.c
index f8a573ff44e916db2e8b246691b7a7065ec7dfad..2d249b431da1a17f33af1d5e7f7e21fb1b278ac7 100644 (file)
@@ -57,7 +57,7 @@ peepCommand peepCommands[] = {
 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_intcon    = {{PO_INTCON,  "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};
 
@@ -75,7 +75,7 @@ static pFile *the_pFile = NULL;
 static pBlock *pb_dead_pcodes = NULL;
 
 /* Hardcoded flags to change the behavior of the PIC port */
-static int peepOptimizing = 0;        /* run the peephole optimizer if nonzero */
+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 */
 
@@ -1347,12 +1347,14 @@ void  pCodeInitRegisters(void)
        stkSize = 8; // Set pseudo stack size to 8
        initStack(shareBankAddress, stkSize); // Putting the pseudo stack in shared memory so all modules use the same register when passing fn parameters
        init_pic(port->processor);
+       if ((unsigned)shareBankAddress > getMaxRam())
+               shareBankAddress = (int)getMaxRam();
        
        pc_status.r = allocProcessorRegister(IDX_STATUS,"STATUS", PO_STATUS, 0x180);
        pc_pcl.r = allocProcessorRegister(IDX_PCL,"PCL", PO_PCL, 0x80);
        pc_pclath.r = allocProcessorRegister(IDX_PCLATH,"PCLATH", PO_PCLATH, 0x180);
        pc_fsr.r = allocProcessorRegister(IDX_FSR,"FSR", PO_FSR, 0x180);
-       pc_indf.r = allocProcessorRegister(IDX_INDF,"INDF", PO_INDF, 0x80);
+       pc_indf.r = allocProcessorRegister(IDX_INDF,"INDF", PO_INDF, 0x180);
        pc_intcon.r = allocProcessorRegister(IDX_INTCON,"INTCON", PO_INTCON, 0x180);
        
        pc_status.rIdx = IDX_STATUS;
@@ -2806,10 +2808,14 @@ pCodeOp *popCopyReg(pCodeOpReg *pc)
        } else
                pcor->pcop.name = NULL;
        
-       pcor->r = pc->r;
-       pcor->rIdx = pc->rIdx;
-       pcor->r->wasUsed=1;
-       
+       if (pcor->pcop.type == PO_IMMEDIATE){
+               PCOL(pcor)->lit = PCOL(pc)->lit;
+       } else {
+               pcor->r = pc->r;
+               pcor->rIdx = pc->rIdx;
+               if (pcor->r)
+                       pcor->r->wasUsed=1;
+       }       
        //DEBUGpic14_emitcode ("; ***","%s  , copying %s, rIdx=%d",__FUNCTION__,pc->pcop.name,pc->rIdx);
        
        return PCOP(pcor);
@@ -3045,7 +3051,7 @@ char *pCode2str(char *str, size_t size, pCode *pc)
                                                (((pCodeOpRegBit *)(PCI(pc)->pcop))->bit)&7);
                                } else if(PCI(pc)->pcop->type == PO_GPR_BIT) {
                                        SAFE_snprintf(&s,&size,"%s,%d", get_op_from_instruction(PCI(pc)),PCORB(PCI(pc)->pcop)->bit);
-                               }else
+                               } else
                                        SAFE_snprintf(&s,&size,"%s,0 ; ?bug", get_op_from_instruction(PCI(pc)));
                                //PCI(pc)->pcop->t.bit );
                        } else {
@@ -3056,7 +3062,7 @@ char *pCode2str(char *str, size_t size, pCode *pc)
                                        else
                                                SAFE_snprintf(&s,&size,"(1 << (%s & 7))",get_op_from_instruction(PCI(pc)));
                                        
-                               }else {
+                               } else {
                                        SAFE_snprintf(&s,&size,"%s",get_op_from_instruction(PCI(pc)));
                                        
                                        if( PCI(pc)->num_ops == 2)
@@ -3122,19 +3128,18 @@ static void genericPrint(FILE *of, pCode *pc)
        case PC_OPCODE:
                // If the opcode has a label, print that first
                {
-                       pBranch *pbl = PCI(pc)->label;
+                       char str[256];
+                       pCodeInstruction *pci = PCI(pc);
+                       pBranch *pbl = pci->label;
                        while(pbl && pbl->pc) {
                                if(pbl->pc->type == PC_LABEL)
                                        pCodePrintLabel(of, pbl->pc);
                                pbl = pbl->next;
                        }
-               }
                
-               if(PCI(pc)->cline) 
-                       genericPrint(of,PCODE(PCI(pc)->cline));
+                       if(pci->cline) 
+                               genericPrint(of,PCODE(pci->cline));
                
-               {
-                       char str[256];
                        
                        pCode2str(str, 256, pc);
                        
@@ -3142,9 +3147,12 @@ static void genericPrint(FILE *of, pCode *pc)
                        
                        /* Debug */
                        if(debug_verbose) {
+                               pCodeOpReg *pcor = PCOR(pci->pcop);
                                fprintf(of, "\t;id=%u,key=%03x",pc->id,pc->seq);
-                               if(PCI(pc)->pcflow)
-                                       fprintf(of,",flow seq=%03x",PCI(pc)->pcflow->pc.seq);
+                               if(pci->pcflow)
+                                       fprintf(of,",flow seq=%03x",pci->pcflow->pc.seq);
+                               if (pcor && pcor->pcop.type==PO_GPR_TEMP && !pcor->r->isFixed)
+                                       fprintf(of,",rIdx=r0x%X",pcor->rIdx);
                        }
                }
 #if 0
@@ -3671,7 +3679,7 @@ static void AnalyzeRETURN(pCode *pc)
 /*-----------------------------------------------------------------*/
 regs * getRegFromInstruction(pCode *pc)
 {
-       
+       regs *r;
        if(!pc                   || 
                !isPCI(pc)            ||
                !PCI(pc)->pcop        ||
@@ -3682,36 +3690,33 @@ regs * getRegFromInstruction(pCode *pc)
        case PO_INDF:
        case PO_FSR:
                return PCOR(PCI(pc)->pcop)->r;
-               
-               //    return typeRegWithIdx (PCOR(PCI(pc)->pcop)->rIdx, REG_SFR, 0);
-               
+
        case PO_BIT:
        case PO_GPR_TEMP:
-               //fprintf(stderr, "getRegFromInstruction - bit or temp\n");
                return PCOR(PCI(pc)->pcop)->r;
                
        case PO_IMMEDIATE:
-               if(PCOI(PCI(pc)->pcop)->r)
-                       return (PCOI(PCI(pc)->pcop)->r);
-               
-               //fprintf(stderr, "getRegFromInstruction - immediate\n");
+               r = PCOI(PCI(pc)->pcop)->r;
+               if (r)
+                       return r;
                return dirregWithName(PCI(pc)->pcop->name);
-               //return NULL; // PCOR(PCI(pc)->pcop)->r;
                
        case PO_GPR_BIT:
-               return PCOR(PCI(pc)->pcop)->r;
+               r = PCOR(PCI(pc)->pcop)->r;
+               if (r)
+                       return r;
+               return dirregWithName(PCI(pc)->pcop->name);
                
        case PO_GPR_REGISTER:
        case PO_DIR:
-               //fprintf(stderr, "getRegFromInstruction - dir\n");
-               return PCOR(PCI(pc)->pcop)->r;
+               r = PCOR(PCI(pc)->pcop)->r;
+               if (r)
+                       return r;
+               return dirregWithName(PCI(pc)->pcop->name);
        case PO_LITERAL:
-               //fprintf(stderr, "getRegFromInstruction - literal\n");
                break;
                
        default:
-               //fprintf(stderr, "getRegFromInstruction - unknown reg type %d\n",PCI(pc)->pcop->type);
-               //genericPrint(stderr, pc);
                break;
        }
        
@@ -4310,8 +4315,9 @@ static void BanksUsedFlow(pBlock *pb)
 */
 
 /*-----------------------------------------------------------------*/
+/* Inserts a new pCodeInstruction before an existing one           */
 /*-----------------------------------------------------------------*/
-static void pCodeInstructionInsertAfter(pCodeInstruction *pci, pCodeInstruction *new_pci)
+static void insertPCodeInstruction(pCodeInstruction *pci, pCodeInstruction *new_pci)
 {
        
        pCodeInsertAfter(pci->pc.prev, &new_pci->pc);
@@ -4343,7 +4349,7 @@ static void insertBankSwitch(pCodeInstruction *pci, int Set_Clear, int RP_BankBi
        
        new_pc = newpCode((Set_Clear?POC_BSF:POC_BCF),popCopyGPR2Bit(PCOP(&pc_status),RP_BankBit));
        
-       pCodeInstructionInsertAfter(pci, PCI(new_pc));
+       insertPCodeInstruction(pci, PCI(new_pc));
 }
 
 /*-----------------------------------------------------------------*/
@@ -4358,7 +4364,7 @@ static void insertBankSel(pCodeInstruction  *pci, const char *name)
                pcop->name = strdup(name);
        new_pc = newpCode(POC_BANKSEL, pcop);
        
-       pCodeInstructionInsertAfter(pci, PCI(new_pc));
+       insertPCodeInstruction(pci, PCI(new_pc));
 }
 
 /*-----------------------------------------------------------------*/
@@ -4411,7 +4417,8 @@ static int BankSelect(pCodeInstruction *pci, int cur_bank, regs *reg)
                insertBankSel(pci, reg->name); // Let linker choose the bank selection
        } else if ((cur_bank == -1)||(cur_bank == 'L')||(cur_bank == 'E')) { // Current bank unknown and new register bank is known then can set bank bits
                insertBankSwitch(pci, bank&1, PIC_RP0_BIT);
-               insertBankSwitch(pci, bank&2, PIC_RP1_BIT);
+               if (getMaxRam()&0x100)
+                       insertBankSwitch(pci, bank&2, PIC_RP1_BIT);
        } else { // Current bank and new register banks known - can set bank bits
                switch((cur_bank^bank) & 3) {
                case 0:
@@ -4424,7 +4431,8 @@ static int BankSelect(pCodeInstruction *pci, int cur_bank, regs *reg)
                        break;
                case 3:
                        insertBankSwitch(pci, bank&1, PIC_RP0_BIT);
-                       insertBankSwitch(pci, bank&2, PIC_RP1_BIT);
+                       if (getMaxRam()&0x100)
+                               insertBankSwitch(pci, bank&2, PIC_RP1_BIT);
                        break;
                }
        }
@@ -4482,7 +4490,8 @@ static int IsBankChange(pCode *pc, regs *reg, int *cur_bank) {
                
        } else if (PCI(pc)->op == POC_BANKSEL) {
                int old_bank = *cur_bank;
-               *cur_bank = (PCOR(PCI(pc)->pcop)->r->isExtern) ? 'E' : 'L';
+               regs *r = PCOR(PCI(pc)->pcop)->r;
+               *cur_bank = (!r || r->isExtern) ? 'E' : 'L';
                LastRegIdx = reg->rIdx;
                return old_bank != *cur_bank;
        }
@@ -4533,13 +4542,11 @@ static int DoBankSelect(pCode *pc, int cur_bank) {
        
        reg = getRegFromInstruction(pc);
        if (reg) {
-               
                if (IsBankChange(pc,reg,&cur_bank))
                        return cur_bank;
-               
                if (!isPCI_LIT(pc)) {
                        
-               /* Examine the instruction before this one to make sure it is
+                       /* Examine the instruction before this one to make sure it is
                        * not a skip type instruction */
                        pcprev = findPrevpCode(pc->prev, PC_OPCODE);
                        
@@ -5404,12 +5411,7 @@ void AnalyzeBanking(void)
        pBlock  *pb;
        
        if(!picIsInitialized()) {
-               fprintf(stderr,"Temporary ERROR: at the moment you have to use\n");
-               fprintf(stderr,"an include file create by inc2h.pl. See SDCC source:\n");
-               fprintf(stderr,"support/scripts/inc2h.pl\n");
-               fprintf(stderr,"this is a nuisance bug that will be fixed shortly\n");
-               
-               exit(1);
+               setDefMaxRam(); // Max RAM has not been included, so use default setting
        }
        
        /* Phase x - Flow Analysis - Used Banks
@@ -5432,8 +5434,10 @@ void AnalyzeBanking(void)
 /*-----------------------------------------------------------------*/
 DEFSETFUNC (resetrIdx)
 {
-       if (!((regs *)item)->isFixed)
-               ((regs *)item)->rIdx = 0;
+       regs *r = (regs *)item;
+       if (!r->isFixed) {
+               r->rIdx = 0;
+       }
        
        return 0;
 }