* support/valdiag/tests/overflow.c: added shift tests
[fw/sdcc] / src / pic / pcode.c
index 83f9737be2b05bd5f7dd20f590323a846c035307..295ff3367d90f92de6481e86b8219d398235b0b4 100644 (file)
@@ -50,16 +50,17 @@ peepCommand peepCommands[] = {
 
 
 // Eventually this will go into device dependent files:
-pCodeOpReg pc_status    = {{PO_STATUS,  "_STATUS"}, -1, NULL,0,NULL};
+pCodeOpReg pc_status    = {{PO_STATUS,  "STATUS"}, -1, NULL,0,NULL};
 pCodeOpReg pc_indf      = {{PO_INDF,    "INDF"}, -1, NULL,0,NULL};
 pCodeOpReg pc_fsr       = {{PO_FSR,     "FSR"}, -1, NULL,0,NULL};
 pCodeOpReg pc_intcon    = {{PO_INTCON,  ""}, -1, NULL,0,NULL};
 pCodeOpReg pc_pcl       = {{PO_PCL,     "PCL"}, -1, NULL,0,NULL};
-pCodeOpReg pc_pclath    = {{PO_PCLATH,  "_PCLATH"}, -1, NULL,0,NULL};
+pCodeOpReg pc_pclath    = {{PO_PCLATH,  "PCLATH"}, -1, NULL,0,NULL};
 
 pCodeOpReg pc_kzero     = {{PO_GPR_REGISTER,  "KZ"}, -1, NULL,0,NULL};
 pCodeOpReg pc_wsave     = {{PO_GPR_REGISTER,  "WSAVE"}, -1, NULL,0,NULL};
 pCodeOpReg pc_ssave     = {{PO_GPR_REGISTER,  "SSAVE"}, -1, NULL,0,NULL};
+pCodeOpReg pc_psave     = {{PO_GPR_REGISTER,  "PSAVE"}, -1, NULL,0,NULL};
 
 static int mnemonics_initialized = 0;
 
@@ -77,7 +78,7 @@ static int peepOptimizing = 1;        /* run the peephole optimizer if nonzero *
 static int functionInlining = 1;      /* inline functions if nonzero */
 int debug_verbose = 0;                /* Set true to inundate .asm file */
 
-static int GpCodeSequenceNumber = 1;
+// static int GpCodeSequenceNumber = 1;
 int GpcFlowSeq = 1;
 
 extern void RemoveUnusedRegisters(void);
@@ -105,7 +106,7 @@ static void pCodePrintLabel(FILE *of, pCode *pc);
 static void pCodePrintFunction(FILE *of, pCode *pc);
 static void pCodeOpPrint(FILE *of, pCodeOp *pcop);
 static char *get_op_from_instruction( pCodeInstruction *pcc);
-char *get_op( pCodeOp *pcop,char *buff,int buf_size);
+char *get_op( pCodeOp *pcop,char *buff,size_t buf_size);
 int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd);
 int pCodePeepMatchRule(pCode *pc);
 void pBlockStats(FILE *of, pBlock *pb);
@@ -136,6 +137,7 @@ pCodeInstruction pciADDWF = {
   2,    // num ops
   1,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   (PCC_W | PCC_REGISTER),   // inCond
   (PCC_REGISTER | PCC_Z) // outCond
@@ -157,6 +159,7 @@ pCodeInstruction pciADDFW = {
   2,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   (PCC_W | PCC_REGISTER),   // inCond
   (PCC_W | PCC_Z) // outCond
@@ -178,6 +181,7 @@ pCodeInstruction pciADDLW = {
   1,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  1,    // literal operand
   POC_NOP,
   (PCC_W | PCC_LITERAL),   // inCond
   (PCC_W | PCC_Z | PCC_C | PCC_DC) // outCond
@@ -199,6 +203,7 @@ pCodeInstruction pciANDLW = {
   1,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  1,    // literal operand
   POC_NOP,
   (PCC_W | PCC_LITERAL),   // inCond
   (PCC_W | PCC_Z) // outCond
@@ -220,6 +225,7 @@ pCodeInstruction pciANDWF = {
   2,    // num ops
   1,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   (PCC_W | PCC_REGISTER),   // inCond
   (PCC_REGISTER | PCC_Z) // outCond
@@ -241,6 +247,7 @@ pCodeInstruction pciANDFW = {
   2,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   (PCC_W | PCC_REGISTER),   // inCond
   (PCC_W | PCC_Z) // outCond
@@ -262,6 +269,7 @@ pCodeInstruction pciBCF = {
   2,    // num ops
   1,1,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_BSF,
   (PCC_REGISTER | PCC_EXAMINE_PCOP),   // inCond
   PCC_REGISTER // outCond
@@ -283,6 +291,7 @@ pCodeInstruction pciBSF = {
   2,    // num ops
   1,1,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_BCF,
   (PCC_REGISTER | PCC_EXAMINE_PCOP),   // inCond
   (PCC_REGISTER | PCC_EXAMINE_PCOP) // outCond
@@ -304,6 +313,7 @@ pCodeInstruction pciBTFSC = {
   2,    // num ops
   0,1,  // dest, bit instruction
   1,1,  // branch, skip
+  0,    // literal operand
   POC_BTFSS,
   (PCC_REGISTER | PCC_EXAMINE_PCOP),   // inCond
   PCC_EXAMINE_PCOP // outCond
@@ -325,6 +335,7 @@ pCodeInstruction pciBTFSS = {
   2,    // num ops
   0,1,  // dest, bit instruction
   1,1,  // branch, skip
+  0,    // literal operand
   POC_BTFSC,
   (PCC_REGISTER | PCC_EXAMINE_PCOP),   // inCond
   PCC_EXAMINE_PCOP // outCond
@@ -346,6 +357,7 @@ pCodeInstruction pciCALL = {
   1,    // num ops
   0,0,  // dest, bit instruction
   1,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   PCC_NONE, // inCond
   PCC_NONE  // outCond
@@ -367,6 +379,7 @@ pCodeInstruction pciCOMF = {
   2,    // num ops
   1,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   PCC_REGISTER,  // inCond
   PCC_REGISTER   // outCond
@@ -388,6 +401,7 @@ pCodeInstruction pciCOMFW = {
   2,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   PCC_REGISTER,  // inCond
   PCC_W   // outCond
@@ -409,6 +423,7 @@ pCodeInstruction pciCLRF = {
   1,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   PCC_NONE, // inCond
   PCC_REGISTER  // outCond
@@ -430,6 +445,7 @@ pCodeInstruction pciCLRW = {
   0,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   PCC_NONE, // inCond
   PCC_W  // outCond
@@ -451,6 +467,7 @@ pCodeInstruction pciCLRWDT = {
   0,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   PCC_NONE, // inCond
   PCC_NONE  // outCond
@@ -472,6 +489,7 @@ pCodeInstruction pciDECF = {
   2,    // num ops
   1,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   PCC_REGISTER,   // inCond
   PCC_REGISTER    // outCond
@@ -493,6 +511,7 @@ pCodeInstruction pciDECFW = {
   2,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   PCC_REGISTER,   // inCond
   PCC_W    // outCond
@@ -514,6 +533,7 @@ pCodeInstruction pciDECFSZ = {
   2,    // num ops
   1,0,  // dest, bit instruction
   1,1,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   PCC_REGISTER,   // inCond
   PCC_REGISTER    // outCond
@@ -535,6 +555,7 @@ pCodeInstruction pciDECFSZW = {
   2,    // num ops
   0,0,  // dest, bit instruction
   1,1,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   PCC_REGISTER,   // inCond
   PCC_W           // outCond
@@ -556,6 +577,7 @@ pCodeInstruction pciGOTO = {
   1,    // num ops
   0,0,  // dest, bit instruction
   1,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   PCC_NONE,   // inCond
   PCC_NONE    // outCond
@@ -577,6 +599,7 @@ pCodeInstruction pciINCF = {
   2,    // num ops
   1,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   PCC_REGISTER,   // inCond
   PCC_REGISTER    // outCond
@@ -598,6 +621,7 @@ pCodeInstruction pciINCFW = {
   2,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   PCC_REGISTER,   // inCond
   PCC_W    // outCond
@@ -619,6 +643,7 @@ pCodeInstruction pciINCFSZ = {
   2,    // num ops
   1,0,  // dest, bit instruction
   1,1,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   PCC_REGISTER,   // inCond
   PCC_REGISTER    // outCond
@@ -640,6 +665,7 @@ pCodeInstruction pciINCFSZW = {
   2,    // num ops
   0,0,  // dest, bit instruction
   1,1,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   PCC_REGISTER,   // inCond
   PCC_W           // outCond
@@ -661,6 +687,7 @@ pCodeInstruction pciIORWF = {
   2,    // num ops
   1,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   (PCC_W | PCC_REGISTER),   // inCond
   (PCC_REGISTER | PCC_Z) // outCond
@@ -682,6 +709,7 @@ pCodeInstruction pciIORFW = {
   2,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   (PCC_W | PCC_REGISTER),   // inCond
   (PCC_W | PCC_Z) // outCond
@@ -703,6 +731,7 @@ pCodeInstruction pciIORLW = {
   1,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  1,    // literal operand
   POC_NOP,
   (PCC_W | PCC_LITERAL),   // inCond
   (PCC_W | PCC_Z) // outCond
@@ -724,6 +753,7 @@ pCodeInstruction pciMOVF = {
   2,    // num ops
   1,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   PCC_REGISTER,   // inCond
   PCC_Z // outCond
@@ -745,6 +775,7 @@ pCodeInstruction pciMOVFW = {
   2,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   PCC_REGISTER,   // inCond
   (PCC_W | PCC_Z) // outCond
@@ -766,6 +797,7 @@ pCodeInstruction pciMOVWF = {
   1,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   PCC_W,   // inCond
   PCC_REGISTER // outCond
@@ -773,7 +805,6 @@ pCodeInstruction pciMOVWF = {
 
 pCodeInstruction pciMOVLW = {
   {PC_OPCODE, NULL, NULL, 0, NULL, 
-   //   genericAnalyze,
    genericDestruct,
    genericPrint},
   POC_MOVLW,
@@ -787,6 +818,7 @@ pCodeInstruction pciMOVLW = {
   1,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  1,    // literal operand
   POC_NOP,
   (PCC_NONE | PCC_LITERAL),   // inCond
   PCC_W // outCond
@@ -807,6 +839,7 @@ pCodeInstruction pciNOP = {
   0,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   PCC_NONE,   // inCond
   PCC_NONE // outCond
@@ -828,6 +861,7 @@ pCodeInstruction pciRETFIE = {
   0,    // num ops
   0,0,  // dest, bit instruction
   1,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   PCC_NONE,   // inCond
   PCC_NONE // outCond (not true... affects the GIE bit too)
@@ -849,6 +883,7 @@ pCodeInstruction pciRETLW = {
   1,    // num ops
   0,0,  // dest, bit instruction
   1,0,  // branch, skip
+  1,    // literal operand
   POC_NOP,
   PCC_LITERAL,   // inCond
   PCC_W // outCond
@@ -870,6 +905,7 @@ pCodeInstruction pciRETURN = {
   0,    // num ops
   0,0,  // dest, bit instruction
   1,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   PCC_NONE,   // inCond
   PCC_NONE // outCond
@@ -891,6 +927,7 @@ pCodeInstruction pciRLF = {
   2,    // num ops
   1,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   (PCC_C | PCC_REGISTER),   // inCond
   (PCC_REGISTER | PCC_Z | PCC_C | PCC_DC) // outCond
@@ -912,6 +949,7 @@ pCodeInstruction pciRLFW = {
   2,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   (PCC_C | PCC_REGISTER),   // inCond
   (PCC_W | PCC_Z | PCC_C | PCC_DC) // outCond
@@ -933,6 +971,7 @@ pCodeInstruction pciRRF = {
   2,    // num ops
   1,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   (PCC_C | PCC_REGISTER),   // inCond
   (PCC_REGISTER | PCC_Z | PCC_C | PCC_DC) // outCond
@@ -954,6 +993,7 @@ pCodeInstruction pciRRFW = {
   2,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   (PCC_C | PCC_REGISTER),   // inCond
   (PCC_W | PCC_Z | PCC_C | PCC_DC) // outCond
@@ -975,6 +1015,7 @@ pCodeInstruction pciSUBWF = {
   2,    // num ops
   1,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   (PCC_W | PCC_REGISTER),   // inCond
   (PCC_REGISTER | PCC_Z) // outCond
@@ -996,6 +1037,7 @@ pCodeInstruction pciSUBFW = {
   2,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   (PCC_W | PCC_REGISTER),   // inCond
   (PCC_W | PCC_Z) // outCond
@@ -1017,6 +1059,7 @@ pCodeInstruction pciSUBLW = {
   1,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  1,    // literal operand
   POC_NOP,
   (PCC_W | PCC_LITERAL),   // inCond
   (PCC_W | PCC_Z | PCC_C | PCC_DC) // outCond
@@ -1038,6 +1081,7 @@ pCodeInstruction pciSWAPF = {
   2,    // num ops
   1,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   (PCC_REGISTER),   // inCond
   (PCC_REGISTER) // outCond
@@ -1059,6 +1103,7 @@ pCodeInstruction pciSWAPFW = {
   2,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   (PCC_REGISTER),   // inCond
   (PCC_W) // outCond
@@ -1080,6 +1125,7 @@ pCodeInstruction pciTRIS = {
   1,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   PCC_NONE,   // inCond
   PCC_REGISTER // outCond
@@ -1101,6 +1147,7 @@ pCodeInstruction pciXORWF = {
   2,    // num ops
   1,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   (PCC_W | PCC_REGISTER),   // inCond
   (PCC_REGISTER | PCC_Z) // outCond
@@ -1122,6 +1169,7 @@ pCodeInstruction pciXORFW = {
   2,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  0,    // literal operand
   POC_NOP,
   (PCC_W | PCC_REGISTER),   // inCond
   (PCC_W | PCC_Z) // outCond
@@ -1143,6 +1191,7 @@ pCodeInstruction pciXORLW = {
   1,    // num ops
   0,0,  // dest, bit instruction
   0,0,  // branch, skip
+  1,    // literal operand
   POC_NOP,
   (PCC_W | PCC_LITERAL),   // inCond
   (PCC_W | PCC_Z | PCC_C | PCC_DC) // outCond
@@ -1152,10 +1201,8 @@ pCodeInstruction pciXORLW = {
 #define MAX_PIC14MNEMONICS 100
 pCodeInstruction *pic14Mnemonics[MAX_PIC14MNEMONICS];
 
-/* This definition needs to be part of configure.in */
-// #define USE_VSNPRINTF
 
-#ifdef USE_VSNPRINTF
+#ifdef HAVE_VSNPRINTF
   // Alas, vsnprintf is not ANSI standard, and does not exist
   // on Solaris (and probably other non-Gnu flavored Unixes).
 
@@ -1180,9 +1227,9 @@ void SAFE_snprintf(char **str, size_t *size, const  char  *format, ...)
   va_end (val);
 
   len = strlen(*str);
-  if(len > *size) {
+  if((size_t)len > *size) {
     fprintf(stderr,"WARNING, it looks like %s has overflowed\n",__FUNCTION__);
-    fprintf(stderr,"len = %d is > str size %d\n",len,*size);
+    fprintf(stderr,"len = %d is > str size %d\n",len,(int)*size);
   }
 
   *str += len;
@@ -1190,7 +1237,7 @@ void SAFE_snprintf(char **str, size_t *size, const  char  *format, ...)
 
 }
 
-#else  //  USE_VSNPRINTF
+#else  //  HAVE_VSNPRINTF
 
 // This version is *not* safe, despite the name.
 
@@ -1220,7 +1267,7 @@ void SAFE_snprintf(char **str, size_t *size, const  char  *format, ...)
 
 }
 
-#endif    //  USE_VSNPRINTF
+#endif    //  HAVE_VSNPRINTF
     
 
 extern  void initStack(int base_address, int size);
@@ -1231,6 +1278,7 @@ extern void init_pic(char *);
 void  pCodeInitRegisters(void)
 {
   static int initialized=0;
+  int shareBankAddress;
 
   if(initialized)
     return;
@@ -1239,9 +1287,9 @@ void  pCodeInitRegisters(void)
   initStack(0xfff, 8);
   init_pic(port->processor);
 
-  pc_status.r = allocProcessorRegister(IDX_STATUS,"_STATUS", PO_STATUS, 0x80);
+  pc_status.r = allocProcessorRegister(IDX_STATUS,"STATUS", PO_STATUS, 0x80);
   pc_pcl.r = allocProcessorRegister(IDX_PCL,"PCL", PO_PCL, 0x80);
-  pc_pclath.r = allocProcessorRegister(IDX_PCLATH,"_PCLATH", PO_PCLATH, 0x80);
+  pc_pclath.r = allocProcessorRegister(IDX_PCLATH,"PCLATH", PO_PCLATH, 0x80);
   pc_fsr.r = allocProcessorRegister(IDX_FSR,"FSR", PO_FSR, 0x80);
   pc_indf.r = allocProcessorRegister(IDX_INDF,"INDF", PO_INDF, 0x80);
   pc_intcon.r = allocProcessorRegister(IDX_INTCON,"INTCON", PO_INTCON, 0x80);
@@ -1253,13 +1301,19 @@ void  pCodeInitRegisters(void)
   pc_pcl.rIdx = IDX_PCL;
   pc_pclath.rIdx = IDX_PCLATH;
 
-  pc_kzero.r = allocInternalRegister(IDX_KZ,"KZ",PO_GPR_REGISTER,0);
-  pc_ssave.r = allocInternalRegister(IDX_SSAVE,"SSAVE", PO_GPR_REGISTER, 0x80);
-  pc_wsave.r = allocInternalRegister(IDX_WSAVE,"WSAVE", PO_GPR_REGISTER, 0);
+  pc_kzero.r = allocInternalRegister(IDX_KZ,"KZ",PO_GPR_REGISTER,0); /* Known Zero - actually just a general purpose reg. */
+  pc_wsave.r = allocInternalRegister(IDX_WSAVE,"WSAVE", PO_GPR_REGISTER, 0x80); /* Interupt storage for working register - must be same address in all banks ie section SHAREBANK. */
+  pc_ssave.r = allocInternalRegister(IDX_SSAVE,"SSAVE", PO_GPR_REGISTER, 0); /* Interupt storage for status register. */
+  pc_psave.r = allocInternalRegister(IDX_PSAVE,"PSAVE", PO_GPR_REGISTER, 0); /* Interupt storage for pclath register. */
 
-  pc_kzero.rIdx = IDX_KZ;
-  pc_wsave.rIdx = IDX_WSAVE;
-  pc_ssave.rIdx = IDX_SSAVE;
+  pc_kzero.rIdx = pc_kzero.r->rIdx;
+  pc_wsave.rIdx = pc_wsave.r->rIdx;
+  pc_ssave.rIdx = pc_ssave.r->rIdx;
+  pc_psave.rIdx = pc_psave.r->rIdx;
+
+  shareBankAddress = 0x7f; /* FIXME - this is different for some PICs ICs if the sharebank does not exist then this address needs to be reserved across all banks. */
+  pc_wsave.r->isFixed = 1;
+  pc_wsave.r->address = shareBankAddress;
 
   /* probably should put this in a separate initialization routine */
   pb_dead_pcodes = newpBlock();
@@ -1521,6 +1575,7 @@ void copypCode(FILE *of, char dbName)
     if(getpBlock_dbName(pb) == dbName) {
       pBlockStats(of,pb);
       printpBlock(of,pb);
+      fprintf (of, "\n");
     }
   }
 
@@ -1539,7 +1594,7 @@ void pcode_test(void)
     char buffer[100];
 
     /* create the file name */
-    strcpy(buffer,srcFileName);
+    strcpy(buffer,dstFileName);
     strcat(buffer,".p");
 
     if( !(pFile = fopen(buffer, "w" ))) {
@@ -1574,16 +1629,20 @@ static int RegCond(pCodeOp *pcop)
   if(!pcop)
     return 0;
 
-  if(pcop->type == PO_GPR_BIT  && !strcmp(pcop->name, pc_status.pcop.name)) {
-    switch(PCORB(pcop)->bit) {
-    case PIC_C_BIT:
-      return PCC_C;
-    case PIC_DC_BIT:
-       return PCC_DC;
-    case PIC_Z_BIT:
-      return PCC_Z;
-    }
-
+  if (pcop->type == PO_GPR_BIT) {
+    char *name = pcop->name;
+      if (!name) 
+        name = PCOR(pcop)->r->name;
+//      if (strcmp(name, pc_status.pcop.name) != 0) { <<< This breaks the peep 2 optimisation
+      switch(PCORB(pcop)->bit) {
+      case PIC_C_BIT:
+        return PCC_C;
+      case PIC_DC_BIT:
+        return PCC_DC;
+      case PIC_Z_BIT:
+        return PCC_Z;
+         }
+//    }
   }
 
   return 0;
@@ -2002,6 +2061,7 @@ pCodeOp *newpCodeOpLabel(char *name, int key)
   else 
     s = name, key = label_key--;
 
+  PCOLAB(pcop)->offset = 0;
   if(s)
     pcop->name = Safe_strdup(s);
 
@@ -2036,15 +2096,19 @@ pCodeOp *newpCodeOpLit(int lit)
 
 /*-----------------------------------------------------------------*/
 /*-----------------------------------------------------------------*/
-pCodeOp *newpCodeOpImmd(char *name, int offset, int index, int code_space)
+pCodeOp *newpCodeOpImmd(char *name, int offset, int index, int code_space, int is_func)
 {
   pCodeOp *pcop;
 
   pcop = Safe_calloc(1,sizeof(pCodeOpImmd) );
   pcop->type = PO_IMMEDIATE;
   if(name) {
-    regs *r = dirregWithName(name);
+    regs *r = NULL;
     pcop->name = Safe_strdup(name);
+
+    if(!is_func) 
+     r = dirregWithName(name);
+
     PCOI(pcop)->r = r;
     if(r) {
       //fprintf(stderr, " newpCodeOpImmd reg %s exists\n",name);
@@ -2061,6 +2125,7 @@ pCodeOp *newpCodeOpImmd(char *name, int offset, int index, int code_space)
   PCOI(pcop)->index = index;
   PCOI(pcop)->offset = offset;
   PCOI(pcop)->_const = code_space;
+  PCOI(pcop)->_function = is_func;
 
   return pcop;
 }
@@ -2096,20 +2161,31 @@ pCodeOp *newpCodeOpWild(int id, pCodeWildBlock *pcwb, pCodeOp *subtype)
 pCodeOp *newpCodeOpBit(char *s, int bit, int inBitSpace)
 {
   pCodeOp *pcop;
+  struct regs *r = 0;
 
   pcop = Safe_calloc(1,sizeof(pCodeOpRegBit) );
   pcop->type = PO_GPR_BIT;
-  if(s)
-    pcop->name = Safe_strdup(s);   
-  else
-    pcop->name = NULL;
 
   PCORB(pcop)->bit = bit;
   PCORB(pcop)->inBitSpace = inBitSpace;
 
   /* pCodeOpBit is derived from pCodeOpReg. We need to init this too */
-  PCOR(pcop)->r = NULL;
-  PCOR(pcop)->rIdx = 0;
+  if (s && !inBitSpace) {
+    r = dirregWithName(s);
+    if (!r) {
+      unsigned char idx = ((s[3] - (((s[3]>='0')&&(s[3]<='9'))?'0':'A'-10))<<4)|(s[4] - (((s[4]>='0')&&(s[4]<='9'))?'0':'A'-10));
+      r = pic14_regWithIdx(idx);
+    }
+  }
+  if (r) {
+    pcop->name = NULL;
+    PCOR(pcop)->r = r;
+    PCOR(pcop)->rIdx = r->rIdx;
+  } else {
+    pcop->name = Safe_strdup(s);   
+    PCOR(pcop)->r = NULL;
+    PCOR(pcop)->rIdx = 0;
+  }
   return pcop;
 }
 
@@ -2181,6 +2257,7 @@ pCodeOp *newpCodeOp(char *name, PIC_OPTYPE type)
     pcop = newpCodeOpReg(-1);
     break;
 
+  case PO_GPR_POINTER:
   case PO_GPR_REGISTER:
     if(name)
       pcop = newpCodeOpRegFromStr(name);
@@ -2444,7 +2521,7 @@ void pBlockRegs(FILE *of, pBlock *pb)
 
 /*-----------------------------------------------------------------*/
 /*-----------------------------------------------------------------*/
-char *get_op(pCodeOp *pcop,char *buffer, int size)
+char *get_op(pCodeOp *pcop,char *buffer, size_t size)
 {
   regs *r;
   static char b[50];
@@ -2465,7 +2542,8 @@ char *get_op(pCodeOp *pcop,char *buffer, int size)
        SAFE_snprintf(&buffer,&size,"%s",PCOR(pcop)->r->name);
        return buffer;
       }
-      return PCOR(pcop)->r->name;
+      //return PCOR(pcop)->r->name;
+      return pcop->name;
       break;
     case PO_GPR_TEMP:
       r = pic14_regWithIdx(PCOR(pcop)->r->rIdx);
@@ -2484,10 +2562,19 @@ char *get_op(pCodeOp *pcop,char *buffer, int size)
       if(PCOI(pcop)->_const) {
 
        if( PCOI(pcop)->offset && PCOI(pcop)->offset<4) {
-         SAFE_snprintf(&s,&size,"(((%s+%d) >> %d)&0xff)",
-                       pcop->name,
-                       PCOI(pcop)->index,
-                       8 * PCOI(pcop)->offset );
+         switch(PCOI(pcop)->offset) {
+         case 0:
+           SAFE_snprintf(&s,&size,"low %s",pcop->name);
+           break;
+         case 1:
+           SAFE_snprintf(&s,&size,"high %s",pcop->name);
+           break;
+         default:
+           SAFE_snprintf(&s,&size,"(((%s+%d) >> %d)&0xff)",
+                         pcop->name,
+                         PCOI(pcop)->index,
+                         8 * PCOI(pcop)->offset );
+         }
        } else
          SAFE_snprintf(&s,&size,"LOW(%s+%d)",pcop->name,PCOI(pcop)->index);
       } else {
@@ -2496,8 +2583,18 @@ char *get_op(pCodeOp *pcop,char *buffer, int size)
          SAFE_snprintf(&s,&size,"(%s + %d)",
                        pcop->name,
                        PCOI(pcop)->index );
-       } else
-         SAFE_snprintf(&s,&size,"%s",pcop->name);
+       } else {
+         switch(PCOI(pcop)->offset) {
+         case 0:
+           SAFE_snprintf(&s,&size,"%s",pcop->name);
+           break;
+         case 1:
+           SAFE_snprintf(&s,&size,"high %s",pcop->name);
+           break;
+         default:
+           SAFE_snprintf(&s,&size,"(%s >> %d)&0xff",pcop->name, 8*PCOI(pcop)->offset);
+         }
+       }
       }
 
       return buffer;
@@ -2514,6 +2611,16 @@ char *get_op(pCodeOp *pcop,char *buffer, int size)
        SAFE_snprintf(&s,&size,"%s",pcop->name);
       return buffer;
 
+    case PO_LABEL:
+      s = buffer;
+      if  (pcop->name) {
+        if(PCOLAB(pcop)->offset == 1)
+          SAFE_snprintf(&s,&size,"HIGH(%s)",pcop->name);
+           else
+          SAFE_snprintf(&s,&size,"%s",pcop->name);
+      }
+      return buffer;
+
     default:
       if  (pcop->name) {
        if(use_buffer) {
@@ -2537,8 +2644,11 @@ static char *get_op_from_instruction( pCodeInstruction *pcc)
 
   if(pcc )
     return get_op(pcc->pcop,NULL,0);
-  
+
+  /* gcc 3.2:  warning: concatenation of string literals with __FUNCTION__ is deprecated 
   return ("ERROR Null: "__FUNCTION__);
+  */
+  return ("ERROR Null: get_op_from_instruction");
 
 }
 
@@ -2552,7 +2662,7 @@ static void pCodeOpPrint(FILE *of, pCodeOp *pcop)
 
 /*-----------------------------------------------------------------*/
 /*-----------------------------------------------------------------*/
-char *pCode2str(char *str, int size, pCode *pc)
+char *pCode2str(char *str, size_t size, pCode *pc)
 {
   char *s = str;
 
@@ -2566,13 +2676,14 @@ char *pCode2str(char *str, int size, pCode *pc)
 
       if(PCI(pc)->isBitInst) {
        if(PCI(pc)->pcop->type == PO_GPR_BIT) {
-         if( (((pCodeOpRegBit *)(PCI(pc)->pcop))->inBitSpace) )
-           SAFE_snprintf(&s,&size,"(%s >> 3), (%s & 7)", 
-                         PCI(pc)->pcop->name ,
-                         PCI(pc)->pcop->name );
-         else
-           SAFE_snprintf(&s,&size,"%s,%d", get_op_from_instruction(PCI(pc)), 
-                         (((pCodeOpRegBit *)(PCI(pc)->pcop))->bit ));
+      char *name = PCI(pc)->pcop->name;
+      if (!name) 
+        name = PCOR(PCI(pc)->pcop)->r->name;
+      if( (((pCodeOpRegBit *)(PCI(pc)->pcop))->inBitSpace) )
+           SAFE_snprintf(&s,&size,"(%s >> 3), (%s & 7)", name, name);
+      else
+           SAFE_snprintf(&s,&size,"%s,%d", name, 
+                         (((pCodeOpRegBit *)(PCI(pc)->pcop))->bit)&7);
        } else if(PCI(pc)->pcop->type == PO_GPR_BIT) {
          SAFE_snprintf(&s,&size,"%s,%d", get_op_from_instruction(PCI(pc)),PCORB(PCI(pc)->pcop)->bit);
        }else
@@ -3254,7 +3365,8 @@ void AnalyzepBlock(pBlock *pb)
     /* Is this an instruction with operands? */
     if(pc->type == PC_OPCODE && PCI(pc)->pcop) {
 
-      if(PCI(pc)->pcop->type == PO_GPR_TEMP) {
+      if((PCI(pc)->pcop->type == PO_GPR_TEMP) 
+        || ((PCI(pc)->pcop->type == PO_GPR_BIT) && PCOR(PCI(pc)->pcop)->r && (PCOR(PCI(pc)->pcop)->r->pc_type == PO_GPR_TEMP))) {
 
        /* Loop through all of the registers declared so far in
           this block and see if we find this one there */
@@ -3887,8 +3999,9 @@ void FixRegisterBankingInFlow(pCodeFlow *pcfl, int cur_bank)
     }
 #endif
 
-    if((reg && REG_BANK(reg)!=cur_bank) || 
-       ((PCI(pc)->op == POC_CALL) && (cur_bank != 0) ))  {
+    if( ( (reg && REG_BANK(reg)!=cur_bank) || 
+         ((PCI(pc)->op == POC_CALL) && (cur_bank != 0) ) ) &&
+       (!isPCI_LIT(pc)) ){
 
       /* Examine the instruction before this one to make sure it is
        * not a skip type instruction */
@@ -4546,8 +4659,10 @@ void FixRegisterBanking(pBlock *pb)
        }
 #endif
 
-       if((reg && REG_BANK(reg)!=cur_bank) || 
-          ((PCI(pc)->op == POC_CALL) && (cur_bank != 0) ))  {
+       if( ( (reg && REG_BANK(reg)!=cur_bank) || 
+             ((PCI(pc)->op == POC_CALL) && (cur_bank != 0) ) ) &&
+           (!isPCI_LIT(pc)) ){
+
 
          /* Examine the instruction before this one to make sure it is
           * not a skip type instruction */
@@ -4889,23 +5004,122 @@ void AnalyzeBanking(void)
 
 }
 
+// Undefine REUSE_GPR in files pcode.c & device.c to prevent local function registers being reused.
+#define REUSE_GPR
+#ifdef REUSE_GPR
+/*-----------------------------------------------------------------*/
+/*-----------------------------------------------------------------*/
+DEFSETFUNC (resetrIdx)
+{
+  if (!((regs *)item)->isFixed)
+    ((regs *)item)->rIdx = 0;
+
+  return 0;
+}
+
+pCode *findFunction(char *fname);
+
+/*-----------------------------------------------------------------*/
+/*-----------------------------------------------------------------*/
+unsigned register_reassign(pBlock *pb, unsigned idx)
+{
+  pCode *pc;
+
+  /* check recursion */
+  pc = setFirstItem(pb->function_entries);
+  if(!pc)
+    return idx;
+
+  DFPRINTF((stderr," reassigning registers for function \"%s\"\n",PCF(pc)->fname));
+
+  if (pb->tregisters) {
+    regs *r;
+    for (r = setFirstItem(pb->tregisters); r; r = setNextItem(pb->tregisters)) {
+      if (r->type == REG_GPR) {
+        if (!r->isFixed) {
+          if (r->rIdx < (int)idx) {
+            char s[20];
+            r->rIdx = idx++;
+            sprintf(s,"r0x%02X", r->rIdx);
+            DFPRINTF((stderr," reassigning register \"%s\" to \"%s\"\n",r->name,s));
+            free(r->name);
+            r->name = Safe_strdup(s);
+          }
+        }
+      }
+    }
+  }
+  for(pc = setFirstItem(pb->function_calls); pc; pc = setNextItem(pb->function_calls)) {
+
+    if(pc->type == PC_OPCODE && PCI(pc)->op == POC_CALL) {
+      char *dest = get_op_from_instruction(PCI(pc));
+
+      pCode *pcn = findFunction(dest);
+      if(pcn) {
+         register_reassign(pcn->pb,idx);
+      }
+    }
+
+  }
+
+  return idx;
+}
+
+/*-----------------------------------------------------------------*/
+/* Re-allocate the GPR for optimum reuse for a given pblock        */
+/* eg  if a function m() calls function f1() and f2(), where f1    */
+/* allocates a local variable vf1 and f2 allocates a local         */
+/* variable vf2. Then providing f1 and f2 do not call each other   */
+/* they may share the same general purpose registers for vf1 and   */
+/* vf2.                                                            */
+/* This is done by first setting the the regs rIdx to start after  */
+/* all the global variables, then walking through the call tree    */
+/* renaming the registers to match their new idx and incrementng   */
+/* it as it goes. If a function has already been called it will    */ 
+/* only rename the registers if it has already used up those       */
+/* registers ie rIdx of the function's registers is lower than the */
+/* current rIdx. That way the register will not be reused while    */
+/* still being used by an eariler function call.                   */
+/*-----------------------------------------------------------------*/
+void register_reusage(pBlock *mainPb)
+{
+  static int exercised = 0;
+  if (!exercised) { /* Only do this once */
+       /* Find end of statically allocated variables for start idx */
+    unsigned idx = 0x20; /* Start from begining of GPR. Note may not be 0x20 on some PICs */
+    regs *r;
+    for (r = setFirstItem(dynDirectRegs); r; r = setNextItem(dynDirectRegs)) {
+      if (r->type != REG_SFR) {
+        idx += r->size; /* Increment for all statically allocated variables */
+      }
+    }
+
+    applyToSet(dynAllocRegs,resetrIdx); /* Reset all rIdx to zero. */
+
+    if (mainPb)
+      idx = register_reassign(mainPb,idx); /* Do main and all the functions that are called from it. */
+    idx = register_reassign(the_pFile->pbHead,idx); /* Do the other functions such as interrupts. */
+  }
+  exercised++;
+}
+#endif // REUSE_GPR
+
 /*-----------------------------------------------------------------*/
 /* buildCallTree - look at the flow and extract all of the calls   */
 /*                                                                 */
 /*-----------------------------------------------------------------*/
 set *register_usage(pBlock *pb);
 
+
 void buildCallTree(void    )
 {
   pBranch *pbr;
-  pBlock  *pb;
+  pBlock  *pb, *mainPb = 0;
   pCode   *pc;
 
   if(!the_pFile)
     return;
 
-
-
   /* Now build the call tree.
      First we examine all of the pCodes for functions.
      Keep in mind that the function boundaries coincide
@@ -4936,37 +5150,43 @@ void buildCallTree(void    )
     pCode *pc_fstart=NULL;
     for(pc = pb->pcHead; pc; pc = pc->next) {
       if(isPCF(pc)) {
-       if (PCF(pc)->fname) {
+        pCodeFunction *pcf = PCF(pc);
+        if (pcf->fname) {
 
-         if(STRCASECMP(PCF(pc)->fname, "_main") == 0) {
-           //fprintf(stderr," found main \n");
-           pb->cmemmap = NULL;  /* FIXME do we need to free ? */
-           pb->dbName = 'M';
-         }
+          if(STRCASECMP(pcf->fname, "_main") == 0) {
+            //fprintf(stderr," found main \n");
+            pb->cmemmap = NULL;  /* FIXME do we need to free ? */
+            pb->dbName = 'M';
+                       mainPb = pb;
+          }
 
-         pbr = Safe_calloc(1,sizeof(pBranch));
-         pbr->pc = pc_fstart = pc;
-         pbr->next = NULL;
+          pbr = Safe_calloc(1,sizeof(pBranch));
+          pbr->pc = pc_fstart = pc;
+          pbr->next = NULL;
 
-         the_pFile->functions = pBranchAppend(the_pFile->functions,pbr);
+          the_pFile->functions = pBranchAppend(the_pFile->functions,pbr);
 
-         // Here's a better way of doing the same:
-         addSet(&pb->function_entries, pc);
+          // Here's a better way of doing the same:
+          addSet(&pb->function_entries, pc);
 
-       } else {
-         // Found an exit point in a function, e.g. return
-         // (Note, there may be more than one return per function)
-         if(pc_fstart)
-           pBranchLink(PCF(pc_fstart), PCF(pc));
+        } else {
+          // Found an exit point in a function, e.g. return
+          // (Note, there may be more than one return per function)
+          if(pc_fstart)
+            pBranchLink(PCF(pc_fstart), pcf);
 
-         addSet(&pb->function_exits, pc);
-       }
+          addSet(&pb->function_exits, pc);
+        }
       } else if(isCALL(pc)) {
-       addSet(&pb->function_calls,pc);
+        addSet(&pb->function_calls,pc);
       }
     }
   }
 
+#ifdef REUSE_GPR
+  register_reusage(mainPb); /* Comment out this line to prevent local function registers being reused. Note from this point onwards finding a GPR by its rIdx value will no longer work.*/
+#endif // REUSE_GPR
+
   /* Re-allocate the registers so that there are no collisions
    * between local variables when one function call another */