* src/pic/ralloc.c (newReg): create aliases for registers with
[fw/sdcc] / src / pic / pcode.c
index b5539144c5aa4675d21dfdcf0ecaab3ec60b2cb5..c37d6ff85359a323aaba0e2892a1e7fb0bdc0eba 100644 (file)
@@ -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);
@@ -4534,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;
@@ -4542,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);
@@ -4571,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) {
@@ -4595,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) {
@@ -4602,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) {
@@ -4624,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
 }
 
 /*-----------------------------------------------------------------*/
@@ -4737,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;
@@ -5606,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;
@@ -5646,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) {