* src/pic/ralloc.c (newReg): create aliases for registers with
[fw/sdcc] / src / pic / pcode.c
index d29b62e42b5486f36b3ed652388481308783a66b..c37d6ff85359a323aaba0e2892a1e7fb0bdc0eba 100644 (file)
@@ -4541,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;
@@ -4549,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);
@@ -4578,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) {
@@ -4602,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) {
@@ -4609,11 +4623,7 @@ 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);
@@ -4638,6 +4648,7 @@ static int BankSelect(pCodeInstruction *pci, int cur_bank, regs *reg)
        }
        
        return bank;
+#endif
 }
 
 /*-----------------------------------------------------------------*/
@@ -4744,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;