* src/pic/device.c (sanitise_processor_name): only remove p(ic) prefix,
[fw/sdcc] / src / pic / pcode.c
index 4ef15d0f31278e1cfb58ca26dc63f368e495caa6..8af55b1b10c6e8eb5550476fd0d87c554dae5ccc 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,9 +4304,14 @@ void LinkFlow(pBlock *pb)
                
                /* find last instruction in flow */
                pc = findPrevInstruction (PCFL(pcflow)->end);
-               if (!pc) continue;
+               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);
@@ -4306,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;
                }
@@ -4334,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;
                }
                
@@ -4591,7 +4617,7 @@ 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);
-               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) {
@@ -4605,7 +4631,7 @@ 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;
                }
@@ -5587,7 +5613,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;
@@ -5711,8 +5738,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))