* src/pic/device.{c,h}: added pic14_getPIC()
[fw/sdcc] / src / pic / pcode.c
index 1540ec025695cb368e210ad1a62efd9db9b950d7..4067610bc8d8ade88c00e1173db246cedf7fc96b 100644 (file)
@@ -543,9 +543,9 @@ pCodeInstruction pciDECFSZ = {
                1,0,  // dest, bit instruction
                1,1,  // branch, skip
                0,    // literal operand
-               POC_NOP,
-               PCC_REGISTER,   // inCond
-               PCC_REGISTER    // outCond
+               POC_DECF,               // followed by BTFSC STATUS, Z --> also kills STATUS
+               PCC_REGISTER,           // inCond
+               PCC_REGISTER | PCC_Z    // outCond
 };
 
 pCodeInstruction pciDECFSZW = {
@@ -565,9 +565,9 @@ pCodeInstruction pciDECFSZW = {
                0,0,  // dest, bit instruction
                1,1,  // branch, skip
                0,    // literal operand
-               POC_NOP,
+               POC_DECFW,      // followed by BTFSC STATUS, Z --> also kills STATUS
                PCC_REGISTER,   // inCond
-               PCC_W           // outCond
+               PCC_W | PCC_Z   // outCond
 };
 
 pCodeInstruction pciGOTO = {
@@ -653,9 +653,9 @@ pCodeInstruction pciINCFSZ = {
                1,0,  // dest, bit instruction
                1,1,  // branch, skip
                0,    // literal operand
-               POC_NOP,
-               PCC_REGISTER,   // inCond
-               PCC_REGISTER    // outCond
+               POC_INCF,               // followed by BTFSC STATUS, Z --> also kills STATUS
+               PCC_REGISTER,           // inCond
+               PCC_REGISTER | PCC_Z    // outCond
 };
 
 pCodeInstruction pciINCFSZW = {
@@ -675,9 +675,9 @@ pCodeInstruction pciINCFSZW = {
                0,0,  // dest, bit instruction
                1,1,  // branch, skip
                0,    // literal operand
-               POC_NOP,
+               POC_INCFW,      // followed by BTFSC STATUS, Z --> also kills STATUS
                PCC_REGISTER,   // inCond
-               PCC_W           // outCond
+               PCC_W | PCC_Z   // outCond
 };
 
 pCodeInstruction pciIORWF = {
@@ -1487,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) {
@@ -1504,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();
@@ -1538,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);
        
@@ -1560,7 +1560,7 @@ int getpCodePeepCommand(char *cmd)
 {
        
        peepCommand *pcmd;
-       int key = mnem2key(cmd);
+       int key = mnem2key((unsigned char *)cmd);
        
        
        pcmd = hTabFirstItemWK(pic14pCodePeepCommandsHash, key);
@@ -2339,6 +2339,11 @@ pCodeOp *newpCodeOpBit(char *name, int ibit, int inBitSpace)
                sym = symFindWithName(bit, name);
                if (!sym) sym = symFindWithName(sfrbit, name);
                if (!sym) sym = symFindWithName(sfr, name);
+               if (!sym) sym = symFindWithName(reg, name);
+               // Hack to fix accesses to _INTCON_bits (e.g. GIE=0), see #1579535.
+               // XXX: This ignores nesting levels, but works for globals...
+               if (!sym) sym = findSym(SymbolTab, NULL, name);
+               if (!sym && name && name[0] == '_') sym = findSym(SymbolTab, NULL, &name[1]);
                if (sym) {
                        r = allocNewDirReg(sym->etype,name);
                }
@@ -4533,6 +4538,21 @@ static void insertPCodeInstruction(pCodeInstruction *pci, pCodeInstruction *new_
 
                pCodeInsertAfter (pcprev, jump);
 
+               // Yuck: Cannot simply replace INCFSZ/INCFSZW/DECFSZ/DECFSZW
+               // We replace them with INCF/INCFW/DECF/DECFW followed by 'BTFSS STATUS, Z'
+               switch (PCI(pcprev)->op) {
+               case POC_INCFSZ:
+               case POC_INCFSZW:
+               case POC_DECFSZ:
+               case POC_DECFSZW:
+                   // These are turned into non-skipping instructions, so
+                   // insert 'BTFSC STATUS, Z' after pcprev
+                   pCodeInsertAfter (pcprev, newpCode(POC_BTFSC, popCopyGPR2Bit(PCOP(&pc_status), PIC_Z_BIT)));
+                   break;
+               default:
+                   // no special actions required
+                   break;
+               }
                pCodeReplace (pcprev, pCodeInstructionCopy (PCI(pcprev), 1));
                pcprev = NULL;
                pCodeInsertAfter((pCode*)pci, label);
@@ -4541,6 +4561,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 +4570,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 +4604,19 @@ 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... */
+       static PIC_device *pic;
+       if (!pic) pic = pic14_getPIC();
+
+       // possible optimizations:
+       // * do not emit BANKSELs for SFRs that are present in all banks (bankmsk == regmap for this register)
+       if (reg && pic && ((reg->alias & pic->bankMask) == pic->bankMask)) return 'L';
+       
+       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 +4641,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,15 +4650,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) {
@@ -4631,13 +4668,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
 }
 
 /*-----------------------------------------------------------------*/
@@ -4744,6 +4782,15 @@ static int DoBankSelect(pCode *pc, int cur_bank) {
        }
        
        reg = getRegFromInstruction(pc);
+       if (!reg && isPCI(pc) &&
+               ((PCI(pc)->inCond | PCI(pc)->outCond) & PCC_REGISTER))
+       {
+           if (PCI(pc)->pcop && PCI(pc)->pcop->type == PO_IMMEDIATE) {
+               // fine, this should be low(variable) --> no BANKING required
+           } else {
+               assert(!"Could not get register from instruction.");
+           }
+       }
        if (reg) {
                if (IsBankChange(pc,reg,&cur_bank))
                        return cur_bank;
@@ -5654,7 +5701,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) {