* src/pic/ralloc.c (newReg): create aliases for registers with
[fw/sdcc] / src / pic / pcode.c
index e2a8452f3051280124499635dc14cff3c4ee9149..c37d6ff85359a323aaba0e2892a1e7fb0bdc0eba 100644 (file)
@@ -1137,7 +1137,7 @@ pCodeInstruction pciTRIS = {
                0,    // literal operand
                POC_NOP,
                PCC_NONE,   // inCond /* FIXME: what's TRIS doing? */
-               PCC_REGISTER // outCond /* FIXME: what's TIS doing */
+               PCC_REGISTER // outCond /* FIXME: what's TRIS doing */
 };
 
 pCodeInstruction pciXORWF = {
@@ -1337,7 +1337,7 @@ void SAFE_snprintf(char **str, size_t *size, const  char  *format, ...)
 
 extern  void initStack(int base_address, int size);
 extern regs *allocProcessorRegister(int rIdx, char * name, short po_type, int alias);
-extern regs *allocInternalRegister(int rIdx, char * name, short po_type, int alias);
+extern regs *allocInternalRegister(int rIdx, char * name, PIC_OPTYPE po_type, int alias);
 extern void init_pic(char *);
 
 void  pCodeInitRegisters(void)
@@ -1350,11 +1350,18 @@ void  pCodeInitRegisters(void)
        initialized = 1;
        
        init_pic(port->processor);
-       shareBankAddress = 0x7f; /* FIXME - some PIC ICs like 16C7X which do not have a shared bank need a different approach. */
-       if ((unsigned)shareBankAddress > getMaxRam()) /* If total RAM is less than 0x7f as with 16f84 then reduce shareBankAddress to fit */
-               shareBankAddress = (int)getMaxRam();
-       stkSize = 15; /* Set pseudo stack size to 15, on multi memory bank ICs this leaves room for WSAVE (used for interrupts) to fit into the shared portion of the memory bank */
-       initStack(shareBankAddress, stkSize); /* Putting the pseudo stack in shared memory so all modules use the same register when passing fn parameters */
+       /* FIXME - some PIC ICs like 16C7X which do not have a shared bank
+        * need a different approach.
+        * The fixed address might not be needed anyway, possibly the
+        * linker will assign udata_shr sections correctly... */
+       shareBankAddress = pic14_getSharebankAddress();
+       /* Set pseudo stack size to SHAREBANKSIZE - 3.
+        * On multi memory bank ICs this leaves room for WSAVE/SSAVE/PSAVE
+        * (used for interrupts) to fit into the shared portion of the
+        * memory bank */
+       stkSize = pic14_getSharebankSize()-3;
+       /* Putting the pseudo stack in shared memory so all modules use the same register when passing fn parameters */
+       initStack(shareBankAddress, stkSize);
        
        pc_status.r = allocProcessorRegister(IDX_STATUS,"STATUS", PO_STATUS, 0x180);
        pc_pcl.r = allocProcessorRegister(IDX_PCL,"PCL", PO_PCL, 0x80);
@@ -1480,7 +1487,7 @@ void pic14initMnemonics(void)
        
        for(i=0; i<MAX_PIC14MNEMONICS; i++)
                if(pic14Mnemonics[i])
-                       hTabAddItem(&pic14MnemonicsHash, mnem2key(pic14Mnemonics[i]->mnemonic), pic14Mnemonics[i]);
+                       hTabAddItem(&pic14MnemonicsHash, mnem2key((unsigned char *)pic14Mnemonics[i]->mnemonic), pic14Mnemonics[i]);
                pci = hTabFirstItem(pic14MnemonicsHash, &key);
                
                while(pci) {
@@ -1497,7 +1504,7 @@ int getpCode(char *mnem,unsigned dest)
 {
        
        pCodeInstruction *pci;
-       int key = mnem2key(mnem);
+       int key = mnem2key((unsigned char *)mnem);
        
        if(!mnemonics_initialized)
                pic14initMnemonics();
@@ -1531,7 +1538,7 @@ void pic14initpCodePeepCommands(void)
        i = 0;
        do {
                hTabAddItem(&pic14pCodePeepCommandsHash, 
-                       mnem2key(peepCommands[i].cmd), &peepCommands[i]);
+                       mnem2key((unsigned char *)peepCommands[i].cmd), &peepCommands[i]);
                i++;
        } while (peepCommands[i].cmd);
        
@@ -1553,7 +1560,7 @@ int getpCodePeepCommand(char *cmd)
 {
        
        peepCommand *pcmd;
-       int key = mnem2key(cmd);
+       int key = mnem2key((unsigned char *)cmd);
        
        
        pcmd = hTabFirstItemWK(pic14pCodePeepCommandsHash, key);
@@ -3969,6 +3976,7 @@ void BuildFlow(pBlock *pb)
                PCI(pc)->pcflow = PCFL(pflow);
                
                //fprintf(stderr," build: ");
+               //pc->print(stderr, pc);
                //pflow->print(stderr,pflow);
                
                if (checkLabel(pc)) { 
@@ -4243,7 +4251,17 @@ static void FillFlow(pCodeFlow *pcflow)
 void LinkFlow_pCode(pCodeInstruction *from, pCodeInstruction *to)
 {
        pCodeFlowLink *fromLink, *toLink;
-       
+#if 0
+       fprintf(stderr, "%s: linking ", __FUNCTION__ );
+       if (from) from->pc.print(stderr, &from->pc);
+       else fprintf(stderr, "(null)");
+       fprintf(stderr, " -(%u)-> with -(%u)-> ",
+               from && from->pcflow ? from->pcflow->pc.seq : 0,
+               to && to->pcflow ? to->pcflow->pc.seq : 0);
+       if (to) to->pc.print(stderr, &to->pc);
+       else fprintf(stderr, "(null)");
+#endif
+
        if(!from || !to || !to->pcflow || !from->pcflow)
                return;
        
@@ -4286,8 +4304,14 @@ void LinkFlow(pBlock *pb)
                
                /* find last instruction in flow */
                pc = findPrevInstruction (PCFL(pcflow)->end);
+               if (!pc) {
+                       fprintf(stderr, "%s: flow without end (%u)?\n",
+                       __FUNCTION__, pcflow->seq );
+                       continue;
+               }
                
                //fprintf(stderr, "LinkFlow - flow block (seq=%d) ", pcflow->seq);
+               //pc->print(stderr, pc);
                if(isPCI_SKIP(pc)) {
                        //fprintf(stderr, "ends with skip\n");
                        //pc->print(stderr,pc);
@@ -4305,20 +4329,23 @@ void LinkFlow(pBlock *pb)
                        //pc->print(stderr,pc);
 
                        if(!(pcol && isPCOLAB(pcol))) {
-                               if((PCI(pc)->op != POC_RETLW) && (PCI(pc)->op != POC_RETURN) && (PCI(pc)->op != POC_CALL) && (PCI(pc)->op != POC_RETFIE) ) {
+                               if((PCI(pc)->op != POC_RETLW)
+                                       && (PCI(pc)->op != POC_RETURN)
+                                       && (PCI(pc)->op != POC_CALL)
+                                       && (PCI(pc)->op != POC_RETFIE) )
+                               {
                                        pc->print(stderr,pc);
                                        fprintf(stderr, "ERROR: %s, branch instruction doesn't have label\n",__FUNCTION__);
                                }
-                               continue;
-                       }
-                       
-                       if( (pct = findLabelinpBlock(pb,pcol)) != NULL)
-                               LinkFlow_pCode(PCI(pc),PCI(pct));
-                       else
-                               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:""));
+                       } else {
                        
+                               if( (pct = findLabelinpBlock(pb,pcol)) != NULL)
+                                       LinkFlow_pCode(PCI(pc),PCI(pct));
+                               else
+                                       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:""));
+                       }
                        /* link CALLs to next instruction */
                        if (PCI(pc)->op != POC_CALL) continue;
                }
@@ -4333,8 +4360,8 @@ void LinkFlow(pBlock *pb)
                }
                
                if(pc) {
-                       fprintf(stderr, "ends with unknown\n");
-                       pc->print(stderr,pc);
+                       //fprintf(stderr, "ends with unknown\n");
+                       //pc->print(stderr,pc);
                        continue;
                }
                
@@ -4514,6 +4541,7 @@ static void insertPCodeInstruction(pCodeInstruction *pci, pCodeInstruction *new_
 
 /*-----------------------------------------------------------------*/
 /*-----------------------------------------------------------------*/
+#if 0
 static void insertBankSwitch(pCodeInstruction *pci, int Set_Clear, int RP_BankBit)
 {
        pCode *new_pc;
@@ -4522,14 +4550,19 @@ static void insertBankSwitch(pCodeInstruction *pci, int Set_Clear, int RP_BankBi
        
        insertPCodeInstruction(pci, PCI(new_pc));
 }
-
+#endif
 /*-----------------------------------------------------------------*/
 /*-----------------------------------------------------------------*/
 static void insertBankSel(pCodeInstruction  *pci, const char *name)
 {
        pCode *new_pc;
        
-       pCodeOp *pcop = popCopyReg(PCOR(pci->pcop));
+       pCodeOp *pcop;
+
+       // This is a NOP for single-banked devices.
+       if (pic14_getMaxRam() < 0x80) return;
+       
+       pcop = popCopyReg(PCOR(pci->pcop));
        pcop->type = PO_GPR_REGISTER; // Sometimes the type is set to legacy 8051 - so override it
        if (pcop->name == 0)
                pcop->name = strdup(name);
@@ -4551,6 +4584,12 @@ static void insertBankSel(pCodeInstruction  *pci, const char *name)
 static int LastRegIdx = -1; /* If the previous register is the same one again then no need to change bank. */
 static int BankSelect(pCodeInstruction *pci, int cur_bank, regs *reg)
 {
+#if 1
+       /* Always insert BANKSELs rather than try to be clever:
+        * Too many bugs in optimized banksels... */
+       insertBankSel(pci, reg->name); // Let linker choose the bank selection
+       return 'L';
+#else
        int bank;
        int a = reg->alias>>7;
        if ((a&3) == 3) {
@@ -4575,6 +4614,8 @@ static int BankSelect(pCodeInstruction *pci, int cur_bank, regs *reg)
        LastRegIdx = reg->rIdx;
 #endif
        
+       /* Optimized code---unfortunately this turns out to be buggy
+        * (at least on devices with more than two banks). */
        if (reg->isFixed) {
                bank = REG_BANK(reg);
        } else if (reg->isExtern) {
@@ -4582,15 +4623,11 @@ static int BankSelect(pCodeInstruction *pci, int cur_bank, regs *reg)
        } else {
                bank = 'L'; // Unfixed local registers are allocated by the linker therefore its bank is unknown
        }
-       if ((cur_bank == 'L')&&(bank == 'L')) { // If current bank and new bank are both allocated locally by the linker, then assume it is in same bank.
-               return 'L'; // Local registers are presumed to be in same linker assigned bank
-       } else if ((bank == 'L')&&(cur_bank != 'L')) { // Reg is now local and linker to assign bank
-               insertBankSel(pci, reg->name); // Let linker choose the bank selection
-       } else if (bank == 'E') { // Reg is now extern and linker to assign bank
+       if (bank == 'E' || bank == 'L') { // Reg is now extern and linker to assign bank
                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);
-               if (getMaxRam()&0x100)
+               if (pic14_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) {
@@ -4604,13 +4641,14 @@ static int BankSelect(pCodeInstruction *pci, int cur_bank, regs *reg)
                        break;
                case 3:
                        insertBankSwitch(pci, bank&1, PIC_RP0_BIT);
-                       if (getMaxRam()&0x100)
+                       if (pic14_getMaxRam()&0x100)
                                insertBankSwitch(pci, bank&2, PIC_RP1_BIT);
                        break;
                }
        }
        
        return bank;
+#endif
 }
 
 /*-----------------------------------------------------------------*/
@@ -4717,6 +4755,11 @@ static int DoBankSelect(pCode *pc, int cur_bank) {
        }
        
        reg = getRegFromInstruction(pc);
+       if (!reg && isPCI(pc) &&
+               ((PCI(pc)->inCond | PCI(pc)->outCond) & PCC_REGISTER))
+       {
+           assert(!"Could not get register from instruction.");
+       }
        if (reg) {
                if (IsBankChange(pc,reg,&cur_bank))
                        return cur_bank;
@@ -5586,7 +5629,8 @@ void AnalyzeBanking(void)
        pBlock  *pb;
 
        if(!picIsInitialized()) {
-               setDefMaxRam(); // Max RAM has not been included, so use default setting
+               werror(E_FILE_OPEN_ERR, "no memory size is known for this processor");
+               exit(1);
        }
        
        if (!the_pFile) return;
@@ -5626,7 +5670,9 @@ DEFSETFUNC (resetrIdx)
 void InitReuseReg(void)
 {
        /* Find end of statically allocated variables for start idx */
-       unsigned maxIdx = 0x20; /* Start from begining of GPR. Note may not be 0x20 on some PICs */
+       /* Start from begining of GPR. Note may not be 0x20 on some PICs */
+       /* XXX: Avoid clashes with fixed registers, start late. */
+       unsigned maxIdx = 0x1000;
        regs *r;
        for (r = setFirstItem(dynDirectRegs); r; r = setNextItem(dynDirectRegs)) {
                if (r->type != REG_SFR) {
@@ -5710,8 +5756,8 @@ static unsigned register_reassign(pBlock *pb, unsigned idx)
 void ReuseReg(void)
 {
        pBlock  *pb;
-       InitReuseReg();
        if (!the_pFile) return;
+       InitReuseReg();
        for(pb = the_pFile->pbHead; pb; pb = pb->next) {
                /* Non static functions can be called from other modules so their registers must reassign */
                if (pb->function_entries&&(PCF(setFirstItem(pb->function_entries))->isPublic||!pb->visited))