* src/pic16/pcode.c: declaring variables other than at the start of a
[fw/sdcc] / src / pic16 / pcode.c
index 8ed4d51eb119f505fb2d92e2831833afdab485d0..b85f6987564ed1127861753e26c7cdea97887d80 100644 (file)
@@ -112,6 +112,13 @@ pCodeOpReg pic16_pc_kzero     = {{PO_GPR_REGISTER,  "KZ"}, -1, NULL,0,NULL};
 pCodeOpReg pic16_pc_wsave     = {{PO_GPR_REGISTER,  "WSAVE"}, -1, NULL,0,NULL};
 pCodeOpReg pic16_pc_ssave     = {{PO_GPR_REGISTER,  "SSAVE"}, -1, NULL,0,NULL};
 
+pCodeOpReg pic16_pc_gpsimio   = {{PO_GPR_REGISTER, "GPSIMIO"}, -1, NULL, 0, NULL};
+pCodeOpReg pic16_pc_gpsimio2  = {{PO_GPR_REGISTER, "GPSIMIO2"}, -1, NULL, 0, NULL};
+
+char *OPT_TYPE_STR[] = { "begin", "end" };
+char *LR_TYPE_STR[] = { "entry begin", "entry end", "exit begin", "exit end" };
+
+
 static int mnemonics_initialized = 0;
 
 
@@ -167,9 +174,17 @@ extern void pic16_pCodeInsertAfter(pCode *pc1, pCode *pc2);
 extern pCodeOp *pic16_popCopyReg(pCodeOpReg *pc);
 pCodeOp *pic16_popCopyGPR2Bit(pCodeOp *pc, int bitval);
 void pic16_pCodeRegMapLiveRanges(pBlock *pb);
+void OptimizeLocalRegs(void);
+pCodeOp *pic16_popGet2p(pCodeOp *src, pCodeOp *dst);
 
 char *dumpPicOptype(PIC_OPTYPE type);
 
+pCodeOp *pic16_popGetLit2(int, pCodeOp *);
+pCodeOp *pic16_popGetLit(int);
+pCodeOp *pic16_popGetWithString(char *);
+extern int inWparamList(char *s);
+
+
 /****************************************************************/
 /*                    PIC Instructions                          */
 /****************************************************************/
@@ -181,6 +196,7 @@ pCodeInstruction pic16_pciADDWF = {
    genericPrint},
   POC_ADDWF,
   "ADDWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -208,6 +224,7 @@ pCodeInstruction pic16_pciADDFW = {
    genericPrint},
   POC_ADDFW,
   "ADDWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -235,6 +252,7 @@ pCodeInstruction pic16_pciADDWFC = { // mdubuc - New
    genericPrint},
   POC_ADDWFC,
   "ADDWFC",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -262,6 +280,7 @@ pCodeInstruction pic16_pciADDFWC = {
    genericPrint},
   POC_ADDFWC,
   "ADDWFC",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -289,6 +308,7 @@ pCodeInstruction pic16_pciADDLW = {
    genericPrint},
   POC_ADDLW,
   "ADDLW",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -316,6 +336,7 @@ pCodeInstruction pic16_pciANDLW = {
    genericPrint},
   POC_ANDLW,
   "ANDLW",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -343,6 +364,7 @@ pCodeInstruction pic16_pciANDWF = {
    genericPrint},
   POC_ANDWF,
   "ANDWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -370,6 +392,7 @@ pCodeInstruction pic16_pciANDFW = {
    genericPrint},
   POC_ANDFW,
   "ANDWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -396,6 +419,7 @@ pCodeInstruction pic16_pciBC = { // mdubuc - New
    genericPrint},
   POC_BC,
   "BC",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -423,6 +447,7 @@ pCodeInstruction pic16_pciBCF = {
    genericPrint},
   POC_BCF,
   "BCF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -450,6 +475,7 @@ pCodeInstruction pic16_pciBN = { // mdubuc - New
    genericPrint},
   POC_BN,
   "BN",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -477,6 +503,7 @@ pCodeInstruction pic16_pciBNC = { // mdubuc - New
    genericPrint},
   POC_BNC,
   "BNC",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -504,6 +531,7 @@ pCodeInstruction pic16_pciBNN = { // mdubuc - New
    genericPrint},
   POC_BNN,
   "BNN",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -531,6 +559,7 @@ pCodeInstruction pic16_pciBNOV = { // mdubuc - New
    genericPrint},
   POC_BNOV,
   "BNOV",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -558,6 +587,7 @@ pCodeInstruction pic16_pciBNZ = { // mdubuc - New
    genericPrint},
   POC_BNZ,
   "BNZ",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -585,6 +615,7 @@ pCodeInstruction pic16_pciBOV = { // mdubuc - New
    genericPrint},
   POC_BOV,
   "BOV",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -612,6 +643,7 @@ pCodeInstruction pic16_pciBRA = { // mdubuc - New
    genericPrint},
   POC_BRA,
   "BRA",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -639,6 +671,7 @@ pCodeInstruction pic16_pciBSF = {
    genericPrint},
   POC_BSF,
   "BSF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -666,6 +699,7 @@ pCodeInstruction pic16_pciBTFSC = {
    genericPrint},
   POC_BTFSC,
   "BTFSC",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -693,6 +727,7 @@ pCodeInstruction pic16_pciBTFSS = {
    genericPrint},
   POC_BTFSS,
   "BTFSS",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -720,6 +755,7 @@ pCodeInstruction pic16_pciBTG = { // mdubuc - New
    genericPrint},
   POC_BTG,
   "BTG",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -747,6 +783,7 @@ pCodeInstruction pic16_pciBZ = { // mdubuc - New
    genericPrint},
   POC_BZ,
   "BZ",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -774,6 +811,7 @@ pCodeInstruction pic16_pciCALL = {
    genericPrint},
   POC_CALL,
   "CALL",
+  4,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -790,7 +828,7 @@ pCodeInstruction pic16_pciCALL = {
   0,   // second literal operand
   POC_NOP,
   PCC_NONE, // inCond
-  PCC_NONE , // outCond
+  PCC_NONE, // outCond
   PCI_MAGIC
 };
 
@@ -801,6 +839,7 @@ pCodeInstruction pic16_pciCOMF = {
    genericPrint},
   POC_COMF,
   "COMF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -828,6 +867,7 @@ pCodeInstruction pic16_pciCOMFW = {
    genericPrint},
   POC_COMFW,
   "COMF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -855,6 +895,7 @@ pCodeInstruction pic16_pciCLRF = {
    genericPrint},
   POC_CLRF,
   "CLRF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -882,6 +923,7 @@ pCodeInstruction pic16_pciCLRWDT = {
    genericPrint},
   POC_CLRWDT,
   "CLRWDT",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -909,6 +951,7 @@ pCodeInstruction pic16_pciCPFSEQ = { // mdubuc - New
    genericPrint},
   POC_CPFSEQ,
   "CPFSEQ",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -936,6 +979,7 @@ pCodeInstruction pic16_pciCPFSGT = { // mdubuc - New
    genericPrint},
   POC_CPFSGT,
   "CPFSGT",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -963,6 +1007,7 @@ pCodeInstruction pic16_pciCPFSLT = { // mdubuc - New
    genericPrint},
   POC_CPFSLT,
   "CPFSLT",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -990,6 +1035,7 @@ pCodeInstruction pic16_pciDAW = {
    genericPrint},
   POC_DAW,
   "DAW",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1017,6 +1063,7 @@ pCodeInstruction pic16_pciDCFSNZ = { // mdubuc - New
    genericPrint},
   POC_DCFSNZ,
   "DCFSNZ",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1044,6 +1091,7 @@ pCodeInstruction pic16_pciDCFSNZW = { // mdubuc - New
    genericPrint},
   POC_DCFSNZW,
   "DCFSNZ",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1071,6 +1119,7 @@ pCodeInstruction pic16_pciDECF = {
    genericPrint},
   POC_DECF,
   "DECF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1098,6 +1147,7 @@ pCodeInstruction pic16_pciDECFW = {
    genericPrint},
   POC_DECFW,
   "DECF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1125,6 +1175,7 @@ pCodeInstruction pic16_pciDECFSZ = {
    genericPrint},
   POC_DECFSZ,
   "DECFSZ",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1152,6 +1203,7 @@ pCodeInstruction pic16_pciDECFSZW = {
    genericPrint},
   POC_DECFSZW,
   "DECFSZ",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1179,6 +1231,7 @@ pCodeInstruction pic16_pciGOTO = {
    genericPrint},
   POC_GOTO,
   "GOTO",
+  4,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1206,6 +1259,7 @@ pCodeInstruction pic16_pciINCF = {
    genericPrint},
   POC_INCF,
   "INCF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1233,6 +1287,7 @@ pCodeInstruction pic16_pciINCFW = {
    genericPrint},
   POC_INCFW,
   "INCF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1260,6 +1315,7 @@ pCodeInstruction pic16_pciINCFSZ = {
    genericPrint},
   POC_INCFSZ,
   "INCFSZ",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1274,7 +1330,7 @@ pCodeInstruction pic16_pciINCFSZ = {
   0,    // fast call/return mode select bit
   0,   // second memory operand
   0,   // second literal operand
-  POC_NOP,
+  POC_INFSNZ,
   PCC_REGISTER,   // inCond
   PCC_REGISTER   , // outCond
   PCI_MAGIC
@@ -1287,6 +1343,7 @@ pCodeInstruction pic16_pciINCFSZW = {
    genericPrint},
   POC_INCFSZW,
   "INCFSZ",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1301,7 +1358,7 @@ pCodeInstruction pic16_pciINCFSZW = {
   0,    // fast call/return mode select bit
   0,   // second memory operand
   0,   // second literal operand
-  POC_NOP,
+  POC_INFSNZW,
   PCC_REGISTER,   // inCond
   PCC_W          , // outCond
   PCI_MAGIC
@@ -1313,7 +1370,8 @@ pCodeInstruction pic16_pciINFSNZ = { // mdubuc - New
    genericDestruct,
    genericPrint},
   POC_INFSNZ,
-  "INCFSNZ",
+  "INFSNZ",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1328,12 +1386,40 @@ pCodeInstruction pic16_pciINFSNZ = { // mdubuc - New
   0,    // fast call/return mode select bit
   0,   // second memory operand
   0,   // second literal operand
-  POC_NOP,
+  POC_INCFSZ,
   PCC_REGISTER,   // inCond
   PCC_REGISTER   , // outCond
   PCI_MAGIC
 };
 
+pCodeInstruction pic16_pciINFSNZW = { // vrokas - New
+  {PC_OPCODE, NULL, NULL, 0, NULL, 
+   //   AnalyzeSKIP,
+   genericDestruct,
+   genericPrint},
+  POC_INFSNZW,
+  "INFSNZ",
+  2,
+  NULL, // from branch
+  NULL, // to branch
+  NULL, // label
+  NULL, // operand
+  NULL, // flow block
+  NULL, // C source 
+  3,    // num ops
+  0,0,  // dest, bit instruction
+  1,1,  // branch, skip
+  0,    // literal operand
+  1,    // RAM access bit
+  0,    // fast call/return mode select bit
+  0,   // second memory operand
+  0,   // second literal operand
+  POC_INCFSZW,
+  PCC_REGISTER,   // inCond
+  PCC_W          , // outCond
+  PCI_MAGIC
+};
+
 pCodeInstruction pic16_pciIORWF = {
   {PC_OPCODE, NULL, NULL, 0, NULL, 
    //   genericAnalyze,
@@ -1341,6 +1427,7 @@ pCodeInstruction pic16_pciIORWF = {
    genericPrint},
   POC_IORWF,
   "IORWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1368,6 +1455,7 @@ pCodeInstruction pic16_pciIORFW = {
    genericPrint},
   POC_IORFW,
   "IORWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1395,6 +1483,7 @@ pCodeInstruction pic16_pciIORLW = {
    genericPrint},
   POC_IORLW,
   "IORLW",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1422,6 +1511,7 @@ pCodeInstruction pic16_pciLFSR = { // mdubuc - New
    genericPrint},
   POC_LFSR,
   "LFSR",
+  4,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1449,6 +1539,7 @@ pCodeInstruction pic16_pciMOVF = {
    genericPrint},
   POC_MOVF,
   "MOVF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1476,6 +1567,7 @@ pCodeInstruction pic16_pciMOVFW = {
    genericPrint},
   POC_MOVFW,
   "MOVF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1503,6 +1595,7 @@ pCodeInstruction pic16_pciMOVFF = { // mdubuc - New
    genericPrint},
   POC_MOVFF,
   "MOVFF",
+  4,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1529,6 +1622,7 @@ pCodeInstruction pic16_pciMOVLB = { // mdubuc - New
    genericPrint},
   POC_MOVLB,
   "MOVLB",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1555,6 +1649,7 @@ pCodeInstruction pic16_pciMOVLW = {
    genericPrint},
   POC_MOVLW,
   "MOVLW",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1582,6 +1677,7 @@ pCodeInstruction pic16_pciMOVWF = {
    genericPrint},
   POC_MOVWF,
   "MOVWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1608,6 +1704,7 @@ pCodeInstruction pic16_pciMULLW = { // mdubuc - New
    genericPrint},
   POC_MULLW,
   "MULLW",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1634,6 +1731,7 @@ pCodeInstruction pic16_pciMULWF = { // mdubuc - New
    genericPrint},
   POC_MULWF,
   "MULWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1660,6 +1758,7 @@ pCodeInstruction pic16_pciNEGF = { // mdubuc - New
    genericPrint},
   POC_NEGF,
   "NEGF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1686,6 +1785,7 @@ pCodeInstruction pic16_pciNOP = {
    genericPrint},
   POC_NOP,
   "NOP",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1712,6 +1812,7 @@ pCodeInstruction pic16_pciPOP = { // mdubuc - New
    genericPrint},
   POC_POP,
   "POP",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1738,6 +1839,7 @@ pCodeInstruction pic16_pciPUSH = {
    genericPrint},
   POC_PUSH,
   "PUSH",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1764,6 +1866,7 @@ pCodeInstruction pic16_pciRCALL = { // mdubuc - New
    genericPrint},
   POC_RCALL,
   "RCALL",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1791,6 +1894,7 @@ pCodeInstruction pic16_pciRETFIE = {
    genericPrint},
   POC_RETFIE,
   "RETFIE",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1818,6 +1922,7 @@ pCodeInstruction pic16_pciRETLW = {
    genericPrint},
   POC_RETLW,
   "RETLW",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1845,6 +1950,7 @@ pCodeInstruction pic16_pciRETURN = {
    genericPrint},
   POC_RETURN,
   "RETURN",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1871,6 +1977,7 @@ pCodeInstruction pic16_pciRLCF = { // mdubuc - New
    genericPrint},
   POC_RLCF,
   "RLCF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1898,6 +2005,7 @@ pCodeInstruction pic16_pciRLCFW = { // mdubuc - New
    genericPrint},
   POC_RLCFW,
   "RLCF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1925,6 +2033,7 @@ pCodeInstruction pic16_pciRLNCF = { // mdubuc - New
    genericPrint},
   POC_RLNCF,
   "RLNCF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1951,6 +2060,7 @@ pCodeInstruction pic16_pciRLNCFW = { // mdubuc - New
    genericPrint},
   POC_RLNCFW,
   "RLNCF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1977,6 +2087,7 @@ pCodeInstruction pic16_pciRRCF = { // mdubuc - New
    genericPrint},
   POC_RRCF,
   "RRCF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2003,6 +2114,7 @@ pCodeInstruction pic16_pciRRCFW = { // mdubuc - New
    genericPrint},
   POC_RRCFW,
   "RRCF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2029,6 +2141,7 @@ pCodeInstruction pic16_pciRRNCF = { // mdubuc - New
    genericPrint},
   POC_RRNCF,
   "RRNCF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2056,6 +2169,7 @@ pCodeInstruction pic16_pciRRNCFW = { // mdubuc - New
    genericPrint},
   POC_RRNCFW,
   "RRNCF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2083,6 +2197,7 @@ pCodeInstruction pic16_pciSETF = { // mdubuc - New
    genericPrint},
   POC_SETF,
   "SETF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2110,6 +2225,7 @@ pCodeInstruction pic16_pciSUBLW = {
    genericPrint},
   POC_SUBLW,
   "SUBLW",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2137,6 +2253,7 @@ pCodeInstruction pic16_pciSUBFWB = {
    genericPrint},
   POC_SUBFWB,
   "SUBFWB",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2164,6 +2281,7 @@ pCodeInstruction pic16_pciSUBWF = {
    genericPrint},
   POC_SUBWF,
   "SUBWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2191,6 +2309,7 @@ pCodeInstruction pic16_pciSUBFW = {
    genericPrint},
   POC_SUBFW,
   "SUBWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2218,6 +2337,7 @@ pCodeInstruction pic16_pciSUBFWB_D1 = { // mdubuc - New
    genericPrint},
   POC_SUBFWB_D1,
   "SUBFWB",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2245,6 +2365,7 @@ pCodeInstruction pic16_pciSUBFWB_D0 = { // mdubuc - New
    genericPrint},
   POC_SUBFWB_D0,
   "SUBFWB",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2272,6 +2393,7 @@ pCodeInstruction pic16_pciSUBWFB_D1 = { // mdubuc - New
    genericPrint},
   POC_SUBWFB_D1,
   "SUBWFB",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2299,6 +2421,7 @@ pCodeInstruction pic16_pciSUBWFB_D0 = { // mdubuc - New
    genericPrint},
   POC_SUBWFB_D0,
   "SUBWFB",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2326,6 +2449,7 @@ pCodeInstruction pic16_pciSWAPF = {
    genericPrint},
   POC_SWAPF,
   "SWAPF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2353,6 +2477,7 @@ pCodeInstruction pic16_pciSWAPFW = {
    genericPrint},
   POC_SWAPFW,
   "SWAPF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2379,6 +2504,7 @@ pCodeInstruction pic16_pciTBLRD = {       // patch 15
    genericPrint},
   POC_TBLRD,
   "TBLRD*",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2405,6 +2531,7 @@ pCodeInstruction pic16_pciTBLRD_POSTINC = {       // patch 15
    genericPrint},
   POC_TBLRD_POSTINC,
   "TBLRD*+",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2431,6 +2558,7 @@ pCodeInstruction pic16_pciTBLRD_POSTDEC = {       // patch 15
    genericPrint},
   POC_TBLRD_POSTDEC,
   "TBLRD*-",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2457,6 +2585,7 @@ pCodeInstruction pic16_pciTBLRD_PREINC = {        // patch 15
    genericPrint},
   POC_TBLRD_PREINC,
   "TBLRD+*",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2483,6 +2612,7 @@ pCodeInstruction pic16_pciTBLWT = {       // patch 15
    genericPrint},
   POC_TBLWT,
   "TBLWT*",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2509,6 +2639,7 @@ pCodeInstruction pic16_pciTBLWT_POSTINC = {       // patch 15
    genericPrint},
   POC_TBLWT_POSTINC,
   "TBLWT*+",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2535,6 +2666,7 @@ pCodeInstruction pic16_pciTBLWT_POSTDEC = {       // patch 15
    genericPrint},
   POC_TBLWT_POSTDEC,
   "TBLWT*-",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2561,6 +2693,7 @@ pCodeInstruction pic16_pciTBLWT_PREINC = {        // patch 15
    genericPrint},
   POC_TBLWT_PREINC,
   "TBLWT+*",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2588,6 +2721,7 @@ pCodeInstruction pic16_pciTSTFSZ = { // mdubuc - New
    genericPrint},
   POC_TSTFSZ,
   "TSTFSZ",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2615,6 +2749,7 @@ pCodeInstruction pic16_pciXORWF = {
    genericPrint},
   POC_XORWF,
   "XORWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2642,6 +2777,7 @@ pCodeInstruction pic16_pciXORFW = {
    genericPrint},
   POC_XORFW,
   "XORWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2669,6 +2805,7 @@ pCodeInstruction pic16_pciXORLW = {
    genericPrint},
   POC_XORLW,
   "XORLW",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2696,6 +2833,7 @@ pCodeInstruction pic16_pciBANKSEL = {
    genericPrint},
   POC_BANKSEL,
   "BANKSEL",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2790,6 +2928,7 @@ void SAFE_snprintf(char **str, size_t *size, const  char  *format, ...)
 #endif    //  USE_VSNPRINTF
 #endif
 
+extern set *externs;
 extern  void pic16_initStack(int base_address, int size);
 extern regs *pic16_allocProcessorRegister(int rIdx, char * name, short po_type, int alias);
 extern regs *pic16_allocInternalRegister(int rIdx, char * name, short po_type, int alias);
@@ -2913,6 +3052,12 @@ void  pic16_pCodeInitRegisters(void)
        pic16_pc_eedata.rIdx = IDX_EEDATA;
        pic16_pc_eeadr.rIdx = IDX_EEADR;
        
+       
+       pic16_pc_gpsimio.r = pic16_allocProcessorRegister(IDX_GPSIMIO, "GPSIMIO", PO_GPR_REGISTER, 0x80);
+       pic16_pc_gpsimio2.r = pic16_allocProcessorRegister(IDX_GPSIMIO2, "GPSIMIO2", PO_GPR_REGISTER, 0x80);
+
+       pic16_pc_gpsimio.rIdx = IDX_GPSIMIO;
+       pic16_pc_gpsimio2.rIdx = IDX_GPSIMIO2;
 
        /* probably should put this in a separate initialization routine */
        pb_dead_pcodes = newpBlock();
@@ -3003,6 +3148,7 @@ void pic16initMnemonics(void)
   pic16Mnemonics[POC_INCFSZ] = &pic16_pciINCFSZ;
   pic16Mnemonics[POC_INCFSZW] = &pic16_pciINCFSZW;
   pic16Mnemonics[POC_INFSNZ] = &pic16_pciINFSNZ;
+  pic16Mnemonics[POC_INFSNZW] = &pic16_pciINFSNZW;
   pic16Mnemonics[POC_IORWF] = &pic16_pciIORWF;
   pic16Mnemonics[POC_IORFW] = &pic16_pciIORFW;
   pic16Mnemonics[POC_IORLW] = &pic16_pciIORLW;
@@ -3288,6 +3434,25 @@ void pic16_pcode_test(void)
     }
   }
 }
+
+
+unsigned long pic16_countInstructions(void)
+{
+  pBlock *pb;
+  pCode *pc;
+  unsigned long isize=0;
+
+    if(!the_pFile)return -1;
+    
+    for(pb = the_pFile->pbHead; pb; pb = pb->next) {
+      for(pc = pb->pcHead; pc; pc = pc->next) {
+        if(isPCI(pc) || isPCAD(pc))isize += PCI(pc)->isize;
+      }
+    }
+  return (isize);
+}
+
+
 /*-----------------------------------------------------------------*/
 /* int RegCond(pCodeOp *pcop) - if pcop points to the STATUS reg-  */
 /*      ister, RegCond will return the bit being referenced.       */
@@ -3301,6 +3466,8 @@ static int RegCond(pCodeOp *pcop)
   if(!pcop)
     return 0;
 
+  if(!pcop->name)return 0;
+
   if(pcop->type == PO_GPR_BIT  && !strcmp(pcop->name, pic16_pc_status.pcop.name)) {
     switch(PCORB(pcop)->bit) {
     case PIC_C_BIT:
@@ -3641,7 +3808,7 @@ pCode *pic16_newpCodeAsmDir(char *asdir, char *argfmt, ...)
        pcad->pci.pc.type = PC_ASMDIR;
        pcad->pci.pc.prev = pcad->pci.pc.next = NULL;
        pcad->pci.pc.pb = NULL;
-       
+       pcad->pci.isize = 2;
        pcad->pci.pc.destruct = genericDestruct;
        pcad->pci.pc.print = genericPrint;
 
@@ -3677,8 +3844,8 @@ static void pCodeLabelDestruct(pCode *pc)
   if(!pc)
     return;
 
-  if((pc->type == PC_LABEL) && PCL(pc)->label)
-    free(PCL(pc)->label);
+//  if((pc->type == PC_LABEL) && PCL(pc)->label)
+//    free(PCL(pc)->label);
 
   /* Instead of deleting the memory used by this pCode, mark
    * the object as bad so that if there's a pointer to this pCode
@@ -3739,12 +3906,24 @@ pCode *pic16_newpCodeLabelFORCE(char *name, int key)
   return ( (pCode *)pcl );
 }
 
-#if 0
 pCode *pic16_newpCodeInfo(INFO_TYPE type, pCodeOp *pcop)
 {
+  pCodeInfo *pci;
 
+    pci = Safe_calloc(1, sizeof(pCodeInfo));
+    pci->pci.pc.type = PC_INFO;
+    pci->pci.pc.prev = pci->pci.pc.next = NULL;
+    pci->pci.pc.pb = NULL;
+    pci->pci.label = NULL;
+       
+    pci->pci.pc.destruct = genericDestruct;
+    pci->pci.pc.print = genericPrint;
+    
+    pci->type = type;
+    pci->oper1 = pcop;
+  
+  return ((pCode *)pci);
 }
-#endif
 
 
 /*-----------------------------------------------------------------*/
@@ -3835,11 +4014,13 @@ pCodeOp *pic16_newpCodeOpLit(int lit)
   pcop->type = PO_LITERAL;
 
   pcop->name = NULL;
-  if(lit>=0) {
-    sprintf(s,"0x%02x",lit);
-    if(s)
-      pcop->name = Safe_strdup(s);
-  }
+//  if(lit>=0)
+    sprintf(s,"0x%02hhx",(char)(lit));
+//  else
+//    sprintf(s, "%i", lit);
+  
+  if(s)
+    pcop->name = Safe_strdup(s);
 
   ((pCodeOpLit *)pcop)->lit = lit;
 
@@ -4073,6 +4254,19 @@ pCodeOp *pic16_newpCodeOpOpt(OPT_TYPE type, char *key)
   return (PCOP(pcop));
 }
 
+/*-----------------------------------------------------------------*/
+/*-----------------------------------------------------------------*/
+pCodeOp *pic16_newpCodeOpLocalRegs(LR_TYPE type)
+{
+  pCodeOpLocalReg *pcop;
+
+       pcop = Safe_calloc(1, sizeof(pCodeOpLocalReg));
+       
+       pcop->type = type;
+
+  return (PCOP(pcop));
+}
+
 
 /*-----------------------------------------------------------------*/
 /*-----------------------------------------------------------------*/
@@ -4222,9 +4416,11 @@ void pic16_pCodeConstString(char *name, char *value)
 
   pic16_addpBlock(pb);
 
-  sprintf(buffer,"; %s = %s",name,value);
-  
-  pic16_addpCode2pBlock(pb,pic16_newpCodeCharP(buffer));
+//  sprintf(buffer,"; %s = ", name);
+//  strcat(buffer, value);
+//  fputs(buffer, stderr);
+
+//  pic16_addpCode2pBlock(pb,pic16_newpCodeCharP(buffer));
   pic16_addpCode2pBlock(pb,pic16_newpCodeLabel(name,-1));
 
   do {
@@ -4377,7 +4573,9 @@ void pic16_printpBlock(FILE *of, pBlock *pb)
                        if(pb->dbName == 'A') {
                          absSym *ab;
                                for(ab=setFirstItem(absSymSet); ab; ab=setNextItem(absSymSet)) {
+//                                     fprintf(stderr, "%s:%d testing %s <-> %s\n", __FILE__, __LINE__, PCF(pc)->fname, ab->name);
                                        if(!strcmp(ab->name, PCF(pc)->fname)) {
+//                                             fprintf(stderr, "%s:%d address = %x\n", __FILE__, __LINE__, ab->address);
                                                fprintf(of, "\t0X%06X", ab->address);
                                                break;
                                        }
@@ -4459,7 +4657,7 @@ const char *immdmod[3]={"LOW", "HIGH", "UPPER"};
 char *pic16_get_op(pCodeOp *pcop,char *buffer, size_t size)
 {
   regs *r;
-  static char b[50];
+  static char b[128];
   char *s;
   int use_buffer = 1;    // copy the string to the passed buffer pointer
 
@@ -4530,7 +4728,18 @@ char *pic16_get_op(pCodeOp *pcop,char *buffer, size_t size)
                                        SAFE_snprintf(&s,&size,"%s",pcop->name);
                                }
                                return buffer;
+                       case PO_GPR_BIT:
+                               s = buffer;
+                               if(PCORB(pcop)->subtype == PO_GPR_TEMP) {
+                                       SAFE_snprintf(&s, &size, "%s", pcop->name);
+                               } else {
+                                       if(PCORB(pcop)->pcor.instance)
+                                               SAFE_snprintf(&s, &size, "(%s + %d)", pcop->name, PCORB(pcop)->pcor.instance);
+                                       else
+                                               SAFE_snprintf(&s, &size, "%s", pcop->name);
+                               }
 
+                               return (buffer);
                        default:
                                if(pcop->name) {
                                        if(use_buffer) {
@@ -4679,7 +4888,7 @@ char *pic16_pCode2str(char *str, size_t size, pCode *pc)
        if(isPCI(pc) && (PCI(pc)->pci_magic != PCI_MAGIC)) {
                fprintf(stderr, "%s:%d: pCodeInstruction initialization error in instruction %s, magic is %x (defaut: %x)\n",
                        __FILE__, __LINE__, PCI(pc)->mnemonic, PCI(pc)->pci_magic, PCI_MAGIC);
-               exit(-1);
+//             exit(-1);
        }
 #endif
 
@@ -4703,14 +4912,16 @@ char *pic16_pCode2str(char *str, size_t size, pCode *pc)
        }
 
       if(PCI(pc)->isBitInst) {
-       if(PCI(pc)->pcop->type == PO_GPR_BIT) {
+       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", pic16_get_op_from_instruction(PCI(pc)), 
+           SAFE_snprintf(&s,&size,"%s,%d", pic16_get_op_from_instruction(PCI(pc)),
+//                       (((pCodeOpRegBit *)(PCI(pc)->pcop))->pcor.instance),
                          (((pCodeOpRegBit *)(PCI(pc)->pcop))->bit ));
+                         
        } else if(PCI(pc)->pcop->type == PO_GPR_BIT) {
          SAFE_snprintf(&s,&size,"%s,%d", pic16_get_op_from_instruction(PCI(pc)),PCORB(PCI(pc)->pcop)->bit);
        }else
@@ -4748,6 +4959,17 @@ char *pic16_pCode2str(char *str, size_t size, pCode *pc)
     SAFE_snprintf(&s,&size,";%s", ((pCodeComment *)pc)->comment);
     break;
 
+  case PC_INFO:
+    SAFE_snprintf(&s,&size,"; info ==>");
+    switch(((pCodeInfo *)pc)->type) {
+      case INF_OPTIMIZATION:
+          SAFE_snprintf(&s,&size, " [optimization] %s\n", OPT_TYPE_STR[ PCOO(PCINF(pc)->oper1)->type ]);
+          break;
+      case INF_LOCALREGS:
+          SAFE_snprintf(&s,&size, " [localregs] %s\n", LR_TYPE_STR[ PCOLR(PCINF(pc)->oper1)->type ]);
+          break;
+    }; break;
+
   case PC_INLINE:
     /* assuming that inline code ends with a \n */
     SAFE_snprintf(&s,&size,"%s", ((pCodeComment *)pc)->comment);
@@ -4767,7 +4989,7 @@ char *pic16_pCode2str(char *str, size_t size, pCode *pc)
     break;
   case PC_CSOURCE:
 //    SAFE_snprintf(&s,&size,";#CSRC\t%s %d\t%s\n", PCCS(pc)->file_name, PCCS(pc)->line_number, PCCS(pc)->line);
-      SAFE_snprintf(&s,&size,"%s#LINE\t%d; %s\t%s\n", (pic16_mplab_comp?";":""),
+      SAFE_snprintf(&s,&size,"%s\t.line\t%d; %s\t%s\n", ((pic16_mplab_comp || !options.debug)?";":""),
        PCCS(pc)->line_number, PCCS(pc)->file_name, PCCS(pc)->line);
     break;
   case PC_ASMDIR:
@@ -4783,10 +5005,6 @@ char *pic16_pCode2str(char *str, size_t size, pCode *pc)
   case PC_BAD:
     SAFE_snprintf(&s,&size,";A bad pCode is being used\n");
     break;
-
-  case PC_INFO:
-    SAFE_snprintf(&s,&size,"; PC INFO pcode\n");
-    break;
   }
 
   return str;
@@ -4804,7 +5022,32 @@ static void genericPrint(FILE *of, pCode *pc)
 
   switch(pc->type) {
   case PC_COMMENT:
-    fprintf(of,";%s\n", ((pCodeComment *)pc)->comment);
+//    fputs(((pCodeComment *)pc)->comment, of);
+    fprintf(of,"; %s\n", ((pCodeComment *)pc)->comment);
+    break;
+
+  case PC_INFO:
+    {
+      pBranch *pbl = PCI(pc)->label;
+      while(pbl && pbl->pc) {
+        if(pbl->pc->type == PC_LABEL)
+          pCodePrintLabel(of, pbl->pc);
+        pbl = pbl->next;
+      }
+    }
+          
+    if(pic16_pcode_verbose) {
+      fprintf(of, "; info ==>");
+      switch(((pCodeInfo *)pc)->type) {
+       case INF_OPTIMIZATION:
+             fprintf(of, " [optimization] %s\n", OPT_TYPE_STR[ PCOO(PCINF(pc)->oper1)->type ]);
+              break;
+        case INF_LOCALREGS:
+              fprintf(of, " [localregs] %s\n", LR_TYPE_STR[ PCOLR(PCINF(pc)->oper1)->type ]);
+              break;
+        }
+    };
+    
     break;
 
   case PC_INLINE:
@@ -4864,7 +5107,7 @@ static void genericPrint(FILE *of, pCode *pc)
 
   case PC_CSOURCE:
 //    fprintf(of,";#CSRC\t%s %d\t\t%s\n", PCCS(pc)->file_name, PCCS(pc)->line_number, PCCS(pc)->line);
-    fprintf(of,"%s#LINE\t%d; %s\t%s\n", (pic16_mplab_comp?";":""),
+    fprintf(of,"%s\t.line\t%d; %s\t%s\n", ((pic16_mplab_comp || !options.debug)?";":""),
        PCCS(pc)->line_number, PCCS(pc)->file_name, PCCS(pc)->line);
          
     break;
@@ -4993,11 +5236,11 @@ static void unlinkpCodeFromBranch(pCode *pcl , pCode *pc)
       /* Found a label */
       if(bprev) {
        bprev->next = b->next;  /* Not first pCode in chain */
-       free(b);
+//     free(b);
       } else {
        pc->destruct(pc);
        PCI(pcl)->label = b->next;   /* First pCode in chain */
-       free(b);
+//     free(b);
       }
       return;  /* A label can't occur more than once */
     }
@@ -5824,14 +6067,14 @@ static int isBankInstruction(pCode *pc)
   int bank = -1;
 
   if(!isPCI(pc))
-    return -1;
+    return 0;
 
   if( PCI(pc)->op == POC_MOVLB ||
       (( (reg = pic16_getRegFromInstruction(pc)) != NULL) && isBSR_REG(reg))) {
     bank = PCOL(pc)->lit;
   }
 
-  return bank;
+  return 1;
 }
 
 
@@ -5998,7 +6241,7 @@ int pic16_isPCinFlow(pCode *pc, pCode *pcflow)
   if(!pc || !pcflow)
     return 0;
 
-  if(!isPCI(pc) || !PCI(pc)->pcflow || !isPCFL(pcflow) )
+  if((!isPCI(pc) && !isPCAD(pc)) || !PCI(pc)->pcflow || !isPCFL(pcflow) )
     return 0;
 
   if( PCI(pc)->pcflow->pc.seq == pcflow->seq)
@@ -6022,7 +6265,7 @@ int pic16_isPCinFlow(pCode *pc, pCode *pcflow)
 pCodeOp *pic16_popGetLabel(unsigned int key);
 extern int pic16_labelOffset;
 
-static void insertBankSwitch(int position, pCode *pc)
+static void insertBankSwitch(unsigned char position, pCode *pc)
 {
   pCode *new_pc;
 
@@ -6037,7 +6280,7 @@ static void insertBankSwitch(int position, pCode *pc)
 //     position = 0;           // position is always before (sanity check!)
 
 #if 0
-       fprintf(stderr, "%s:%d: inserting bank switch\n", __FUNCTION__, __LINE__);
+       fprintf(stderr, "%s:%d: inserting bank switch (pos: %d)\n", __FUNCTION__, __LINE__, position);
        pc->print(stderr, pc);
 #endif
 
@@ -6057,8 +6300,10 @@ static void insertBankSwitch(int position, pCode *pc)
 
                case 2: {
                          symbol *tlbl;
-                         pCode *pcnext, *pcprev, *npci;
+                         pCode *pcnext, *pcprev, *npci, *ppc;
                          PIC_OPCODE ipci;
+                         int ofs1=0, ofs2=0, len=0;
+                         
                        /* just like 0, but previous was a skip instruction,
                         * so some care should be taken */
                          
@@ -6070,25 +6315,25 @@ static void insertBankSwitch(int position, pCode *pc)
                                ipci = PCI(pcprev)->inverted_op;
                                npci = pic16_newpCode(ipci, PCI(pcprev)->pcop);
 
-#if 1
-                               PCI(npci)->from = PCI(pcprev)->from;
-                               PCI(npci)->to = PCI(pcprev)->to;
-                               PCI(npci)->label = PCI(pcprev)->label;
-                               PCI(npci)->pcflow = PCI(pcprev)->pcflow;
-                               PCI(npci)->cline = PCI(pcprev)->cline;
-#endif
+//                             fprintf(stderr, "%s:%d old OP: %d\tnew OP: %d\n", __FILE__, __LINE__, PCI(pcprev)->op, ipci);
 
-//                             memmove(PCI(pcprev), PCI(npci), sizeof(pCode) + sizeof(PIC_OPCODE) + sizeof(char const * const));
-
-#if 1
-                               pic16_pCodeInsertAfter(pcprev->prev, npci);
-                               /* unlink the pCode */
-                               pcprev->prev->next = pcprev->next;
-                               pcprev->next->prev = pcprev->prev;
-#endif
+                               /* copy info from old pCode */
+                               ofs1 = ofs2 = sizeof( pCode ) + sizeof(PIC_OPCODE);
+                               len = sizeof(pCodeInstruction) - ofs1 - sizeof( char const * const *);
+                               ofs1 += strlen( PCI(pcprev)->mnemonic) + 1;
+                               ofs2 += strlen( PCI(npci)->mnemonic) + 1;
+                               memcpy(&PCI(npci)->from, &PCI(pcprev)->from, (char *)(&(PCI(npci)->pci_magic)) - (char *)(&(PCI(npci)->from)));
+                               PCI(npci)->op = PCI(pcprev)->inverted_op;
+                               
+                               /* unlink old pCode */
+                               ppc = pcprev->prev;
+                               ppc->next = pcprev->next;
+                               pcprev->next->prev = ppc;
+                               pic16_pCodeInsertAfter(ppc, npci);
                                
-                               pcnext = pic16_newpCode(POC_GOTO, pic16_popGetLabel(tlbl->key));
-                               pic16_pCodeInsertAfter(pc->prev, pcnext);
+                               /* extra instructions to handle invertion */
+                               pcnext = pic16_newpCode(POC_BRA, pic16_popGetLabel(tlbl->key));
+                               pic16_pCodeInsertAfter(npci, pcnext);
                                pic16_pCodeInsertAfter(pc->prev, new_pc);
                                
                                pcnext = pic16_newpCodeLabel(NULL,tlbl->key+100+pic16_labelOffset);
@@ -6289,8 +6534,8 @@ static void exchangeLabels(pCodeLabel *pcl, pCode *pc)
     pCodeOpLabel *pcol = PCOLAB(PCI(pc)->pcop);
 
 //     fprintf(stderr,"changing label key from %d to %d\n",pcol->key, pcl->key);
-    if(pcol->pcop.name)
-      free(pcol->pcop.name);
+//    if(pcol->pcop.name)
+//      free(pcol->pcop.name);
 
     /* If the key is negative, then we (probably) have a label to
      * a function and the name is already defined */
@@ -6501,16 +6746,24 @@ const char *pic16_pCodeOpSubType(pCodeOp *pcop);
 
 pCodeOp *pic16_popCopyGPR2Bit(pCodeOp *pc, int bitval)
 {
-  pCodeOp *pcop;
+  pCodeOp *pcop=NULL;
+
+//  fprintf(stderr, "%s:%d pc type: %s\tname: %s\n", __FILE__, __LINE__, pic16_pCodeOpType(pc), pc->name);
 
-//  fprintf(stderr, "%s:%d pc type: %s\n", __FILE__, __LINE__, pic16_pCodeOpType(pc));
-  pcop = pic16_newpCodeOpBit(pc->name, bitval, 0, pc->type);
+  if(pc->name) {
+       pcop = pic16_newpCodeOpBit(pc->name, bitval, 0, pc->type);
+  } else {
+    if(PCOR(pc)->r)pcop = pic16_newpCodeOpBit(PCOR(pc)->r->name, bitval, 0, pc->type);
+  }
+
+  assert(pcop != NULL);
 
   if( !( (pcop->type == PO_LABEL) ||
         (pcop->type == PO_LITERAL) ||
         (pcop->type == PO_STR) ))
     PCOR(pcop)->r = PCOR(pc)->r;  /* This is dangerous... */
     PCOR(pcop)->r->wasUsed = 1;
+    PCOR(pcop)->instance = PCOR(pc)->instance;
 
   return pcop;
 }
@@ -6534,7 +6787,7 @@ static void pic16_FixRegisterBanking(pBlock *pb)
   pCode *pc=NULL;
   pCode *pcprev=NULL;
   regs *reg, *prevreg;
-  int flag=0;
+  unsigned char flag=0;
   
        if(!pb)
                return;
@@ -6552,9 +6805,18 @@ static void pic16_FixRegisterBanking(pBlock *pb)
                /* for each flow block, determine the register banking 
                 * requirements */
 
+               
+               /* if label, then might come from other point, force banksel */
+               if(isPCL(pc))prevreg = NULL;
+               
                if(!isPCI(pc))goto loop;
 
+               if(PCI(pc)->label)prevreg = NULL;
+
                if(PCI(pc)->is2MemOp)goto loop;
+
+               /* if goto, then force banksel */
+//             if(PCI(pc)->op == POC_GOTO)prevreg = NULL;
        
                reg = pic16_getRegFromInstruction(pc);
 
@@ -6578,6 +6840,7 @@ static void pic16_FixRegisterBanking(pBlock *pb)
                 
                if(isPCI_SKIP(pc)) {
 //                     fprintf(stderr, "instruction is SKIP instruction\n");
+//               prevreg = NULL;
                }
                if(reg && isACCESS_BANK(reg))goto loop;
 
@@ -6591,23 +6854,558 @@ static void pic16_FixRegisterBanking(pBlock *pb)
                 * not a skip type instruction */
                pcprev = findPrevpCode(pc->prev, PC_OPCODE);
 
-               /* FIXME: if previous is SKIP pCode, we should move the BANKSEL
-                * before SKIP, but we have to check if the SKIP uses BANKSEL, etc... */
-               flag = 0;
-               if(pcprev && isPCI_SKIP(pcprev))flag=2; //goto loop;
+               flag = 0;               /* add before this instruction */
+               
+               /* if previous instruction is a skip one, then set flag
+                * to 2 and call insertBankSwitch */
+               if(pcprev && isPCI_SKIP(pcprev)) {
+                 flag=2;       //goto loop
+//               prevreg = NULL;
+               }
                 
+               if(pic16_options.opt_banksel>0) {
+                 char op1[128], op2[128];
+                 
+                   if(prevreg) {
+                     strcpy(op1, pic16_get_op_from_instruction(PCI(pc)));
+                     strcpy(op2, pic16_get_op_from_instruction(PCI(pcprev)));
+                     if(!strcmp(op1, op2))goto loop;
+                   }
+               }
                prevreg = reg;
                insertBankSwitch(flag, pc);
-               pcprev = pc;
 
 //             fprintf(stderr, "BANK SWITCH inserted\n");
                
 loop:
+               pcprev = pc;
                pc = pc->next;
        } while (pc);
 }
 
+/** ADDITIONS BY RAPHAEL NEIDER, 2004-11-16: GOTO OPTIMIZATIONS **/
+
+/* Returns the (maximum of the) number of bytes used by the specified pCode. */
+int instrSize (pCode *pc)
+{
+  if (!pc) return 0;
+
+  if (isPCAD(pc)) {
+    if (!PCAD(pc)->directive || strlen (PCAD(pc)->directive) < 3) return 0;
+    return 4; // assumes only regular instructions using <= 4 bytes
+  }
+
+  if (isPCI(pc)) return PCI(pc)->isize;
+
+  return 0;
+}
+
+/* Returns 1 if pc is referenced by the given label (either
+ * pc is the label itself or is an instruction with an attached
+ * label).
+ * Returns 0 if pc is not preceeded by the specified label.
+ */
+int isLabel (pCode *pc, char *label)
+{
+  if (!pc) return 0;
 
+  // label attached to the pCode?  
+  if (isPCI(pc) || isPCAD(pc) || isPCW(pc) || pc->type == PC_INFO) {
+    pBranch *lab = NULL;
+    lab = PCI(pc)->label;
+
+    while (lab) {
+      if (isPCL(lab->pc) && strcmp(PCL(lab->pc)->label, label) == 0) {
+       return 1;
+      }
+      lab = lab->next;
+    } // while
+  } // if
+
+  // is inline assembly label?
+  if (isPCAD(pc) && PCAD(pc)->directive == NULL && PCAD(pc)->arg) {
+    // do not compare trailing ':'
+    if (strncmp (PCAD(pc)->arg, label, strlen (label)) == 0) {
+      return 1;
+    }
+  } // if
+  
+  // is pCodeLabel?
+  if (isPCL(pc)) {
+      if (strcmp(PCL(pc)->label,label) == 0) {
+      return 1;
+    }
+  } // if
+  
+  // no label/no label attached/wrong label(s)
+  return 0;
+}
+
+/* Returns the distance to the given label in terms of words.
+ * Labels are searched only within -max .. max words from pc.
+ * Returns max if the label could not be found or
+ * its distance from pc in (-max..+max).
+ */
+int findpCodeLabel (pCode *pc, char *label, int max, pCode **target) {
+  int dist = instrSize(pc);
+  pCode *curr = pc;
+
+  // search backwards
+  while (dist < max && curr && !isLabel (curr, label)) {
+    curr = curr->prev;
+    dist += instrSize(curr); // sizeof (instruction)
+  } // while
+  if (curr && dist < max) {
+    if (target != NULL) *target = curr;
+    return -dist;
+  }
+
+  dist = 0;
+  curr = pic16_findNextInstruction (pc->next);
+  //search forwards
+  while (dist < max && curr && !isLabel (curr, label)) {
+    dist += instrSize(curr); // sizeof (instruction)
+    curr = curr->next;
+  } // while
+  if (curr && dist < max) {
+    if (target != NULL) *target = curr;
+    return dist;
+  }
+
+  if (target != NULL) *target = NULL;
+  return max;
+}
+
+/* Returns -1 if pc does NOT denote an instruction like
+ * BTFS[SC] STATUS,i
+ * Otherwise we return 
+ *   (a) 0x10 + i for BTFSS
+ *   (b) 0x00 + i for BTFSC
+ */
+int isSkipOnStatus (pCode *pc)
+{
+  int res = -1;
+  pCodeOp *pcop;
+  if (!pc || !isPCI(pc)) return -1;
+  if (PCI(pc)->op == POC_BTFSS) res = 0x10;
+  else if (PCI(pc)->op == POC_BTFSC) res = 0x00;
+  else return -1;
+
+  pcop = PCI(pc)->pcop;
+
+  if (pcop->type == PO_STATUS || (pcop->type == PO_GPR_BIT && strcmp(pcop->name, "STATUS") == 0)) {
+    return res + ((pCodeOpRegBit *)pcop)->bit;
+  }
+
+  return -1;
+}
+
+/* Returns 1 if pc is one of BC, BZ, BOV, BN, BNC, BNZ, BNOV or BNN,
+ * returns 0 otherwise. */
+int isConditionalBranch (pCode *pc)
+{
+  if (!pc || !isPCI_BRANCH(pc)) return 0;
+
+  switch (PCI(pc)->op) {
+  case POC_BC:
+  case POC_BZ:
+  case POC_BOV:
+  case POC_BN:
+  case POC_BNC:
+  case POC_BNZ:
+  case POC_BNOV:
+  case POC_BNN:
+    return 1;
+
+  default:
+    break;
+  } // switch
+
+  return 0;
+}
+
+/* Returns 1 if pc has a label attached to it.
+ * This can be either a label stored in the pCode itself (.label)
+ * or a label making up its own pCode preceding this pc.
+ * Returns 0 if pc cannot be reached directly via a label.
+ */
+int hasNoLabel (pCode *pc)
+{
+  pCode *prev;
+  if (!pc) return 1;
+
+  // are there any label pCodes between pc and the previous instruction?
+  prev = pic16_findPrevInstruction (pc->prev);
+  while (pc && pc != prev) {
+    // pCode with attached label?
+    if ((isPCI(pc) || isPCAD(pc) || isPCW(pc) || pc->type == PC_INFO)
+       && PCI(pc)->label) {
+      return 0;
+    }
+    // is inline assembly label?
+    if (isPCAD(pc) && PCAD(pc)->directive == NULL) return 0;
+    if (isPCW(pc) && PCW(pc)->label) return 0;
+
+    // pCodeLabel?
+    if (isPCL(pc)) return 0;
+
+    pc = pc->prev;
+  } // if
+
+  // no label found
+  return 1;
+}
+
+/* Replaces the old pCode with the new one, moving the labels,
+ * C source line and probably flow information to the new pCode.
+ */
+void pic16_pCodeReplace (pCode *oldPC, pCode *newPC) {
+  if (!oldPC || !newPC || !isPCI(oldPC) || !isPCI(newPC))
+    return;
+
+  /* first move all labels from old to new */
+  PCI(newPC)->label = pic16_pBranchAppend (PCI(oldPC)->label, PCI(newPC)->label);
+  PCI(oldPC)->label = NULL;
+  
+  /* move C source line (if possible) */
+  if (PCI(oldPC)->cline && !PCI(newPC)->cline)
+    PCI(newPC)->cline = PCI(oldPC)->cline;
+
+  /* insert new pCode into pBlock */
+  pic16_pCodeInsertAfter (oldPC, newPC);
+  pic16_unlinkpCode (oldPC);
+  
+  /* TODO: update flow (newPC->from, newPC->to) */
+  PCI(newPC)->pcflow = PCI(oldPC)->pcflow;
+
+  /* destruct replaced pCode */
+  oldPC->destruct (oldPC);
+}
+
+/* Returns the inverted conditional branch (if any) or NULL.
+ * pcop must be set to the new jump target.
+ */
+pCode *getNegatedBcc (pCode *bcc, pCodeOp *pcop)
+{
+  pCode *newBcc;
+
+  if (!bcc || !isPCI(bcc)) return NULL;
+
+  switch (PCI(bcc)->op) {
+  case POC_BC:   newBcc = pic16_newpCode (POC_BNC , pcop); break;
+  case POC_BZ:   newBcc = pic16_newpCode (POC_BNZ , pcop); break;
+  case POC_BOV:  newBcc = pic16_newpCode (POC_BNOV, pcop); break;
+  case POC_BN:   newBcc = pic16_newpCode (POC_BNN , pcop); break;
+  case POC_BNC:  newBcc = pic16_newpCode (POC_BC  , pcop); break;
+  case POC_BNZ:  newBcc = pic16_newpCode (POC_BZ  , pcop); break;
+  case POC_BNOV: newBcc = pic16_newpCode (POC_BOV , pcop); break;
+  case POC_BNN:  newBcc = pic16_newpCode (POC_BN  , pcop); break;
+  default:
+    newBcc = NULL;
+  }
+  return newBcc;
+}
+
+#define MAX_DIST_GOTO         0x7FFFFFFF
+#define MAX_DIST_BRA                1020       // maximum offset (in bytes) possible with BRA
+#define MAX_DIST_BCC                 120       // maximum offset (in bytes) possible with Bcc
+#define MAX_JUMPCHAIN_DEPTH           16       // number of GOTOs to follow in resolveJumpChain() (to prevent endless loops)
+#define IS_GOTO(arg) ((arg) && isPCI(arg) && (PCI(arg)->op == POC_GOTO || PCI(arg)->op == POC_BRA))
+
+/* Follows GOTO/BRA instructions to their target instructions, stores the
+ * final destination (not a GOTO or BRA instruction) in target and returns
+ * the distance from the original pc to *target.
+ */
+int resolveJumpChain (pCode *pc, pCode **target, pCodeOp **pcop) {
+       pCode *curr = pc;
+       pCode *last = NULL;
+       pCodeOp *lastPCOP = NULL;
+       int dist = 0;
+       int depth = 0;
+
+       //fprintf (stderr, "%s:%d: -=-", __FUNCTION__, __LINE__);
+
+       /* only follow unconditional branches, except for the initial pCode (which may be a conditional branch) */
+       while (curr && (last != curr) && (depth++ < MAX_JUMPCHAIN_DEPTH) && isPCI(curr)
+                       && (PCI(curr)->op == POC_GOTO || PCI(curr)->op == POC_BRA || (curr == pc && isConditionalBranch(curr)))) {
+               last = curr;
+               lastPCOP = PCI(curr)->pcop;
+               dist = findpCodeLabel (pc, PCI(curr)->pcop->name, MAX_DIST_GOTO, &curr);
+               //fprintf (stderr, "last:%p, curr:%p, label:%s\n", last, curr, PCI(last)->pcop->name);
+       } // while
+
+       if (target) *target = last;
+       if (pcop) *pcop = lastPCOP;
+       return dist;
+}
+
+/* Returns pc if it is not a OPT_JUMPTABLE_BEGIN INFO pCode.
+ * Otherwise the first pCode after the jumptable (after
+ * the OPT_JUMPTABLE_END tag) is returned.
+ */
+pCode *skipJumptables (pCode *pc, int *isJumptable)
+{
+  *isJumptable = 0;
+  if (!pc) return NULL;
+  
+  while (pc->type == PC_INFO && PCINF(pc)->type == INF_OPTIMIZATION && PCOO(PCINF(pc)->oper1)->type == OPT_JUMPTABLE_BEGIN) {
+    *isJumptable = 1;
+    //fprintf (stderr, "SKIPPING jumptable\n");
+    do {
+      //pc->print(stderr, pc);
+      pc = pc->next;
+    } while (pc && (pc->type != PC_INFO || PCINF(pc)->type != INF_OPTIMIZATION
+                   || PCOO(PCINF(pc)->oper1)->type != OPT_JUMPTABLE_END));
+    //fprintf (stderr, "<<JUMPTAB:\n");
+    // skip OPT_END as well
+    if (pc) pc = pc->next;
+  } // while
+
+  return pc;
+}
+
+pCode *pic16_findNextInstructionSkipJumptables (pCode *pc, int *isJumptable)
+{
+  int isJumptab;
+  *isJumptable = 0;
+  while (pc && !isPCI(pc) && !isPCAD(pc) && !isPCW(pc)) {
+    // set pc to the first pCode after a jumptable, leave pc untouched otherwise
+    pc = skipJumptables (pc, &isJumptab);
+    if (isJumptab) {
+        // pc is the first pCode after the jumptable
+       *isJumptable = 1;
+    } else {
+        // pc has not been changed by skipJumptables()
+       pc = pc->next;
+    }
+  } // while
+  
+  return pc;
+}
+
+/* Turn GOTOs into BRAs if distance between GOTO and label
+ * is less than 1024 bytes.
+ *
+ * This method is especially useful if GOTOs after BTFS[SC]
+ * can be turned into BRAs as GOTO would cost another NOP
+ * if skipped.
+ */
+void pic16_OptimizeJumps ()
+{
+  pCode *pc;
+  pCode *pc_prev = NULL;
+  pCode *pc_next = NULL;
+  pBlock *pb;
+  pCode *target;
+  int change, iteration, isJumptab;
+  int isHandled = 0;
+  char *label;
+  int opt=0, toofar=0, opt_cond = 0, cond_toofar=0, opt_reorder = 0, opt_gotonext = 0, opt_gotochain = 0;
+  
+  if (!the_pFile) return;
+  
+  //fprintf (stderr, "%s:%d: %s\n", __FILE__, __LINE__, __FUNCTION__);
+  
+  for (pb = the_pFile->pbHead; pb != NULL; pb = pb->next) {
+    int matchedInvertRule = 1;
+    iteration = 1;
+    do {
+      //fprintf (stderr, "%s:%d: iterating over pBlock %p\n", __FUNCTION__, __LINE__, pb);
+      change = 0;
+      pc = pic16_findNextInstruction (pb->pcHead);
+    
+      while (pc) {
+       pc_next = pic16_findNextInstructionSkipJumptables (pc->next, &isJumptab);
+       if (isJumptab) {
+               // skip jumptable, i.e. start over with no pc_prev!     
+               pc_prev = NULL;
+               pc = pc_next;
+               continue;
+       } // if
+
+       /* (1) resolve chained jumps
+        * Do not perform this until pattern (4) is no longer present! Otherwise we will
+        * (a) leave dead code in and
+        * (b) skip over the dead code with an (unneccessary) jump.
+        */
+       if (!matchedInvertRule && (IS_GOTO(pc) || isConditionalBranch(pc))) {
+         pCodeOp *lastTargetOp = NULL;
+         int newDist = resolveJumpChain (pc, &target, &lastTargetOp);
+         int maxDist = MAX_DIST_BCC;
+         if (PCI(pc)->op == POC_BRA) maxDist = MAX_DIST_BRA;
+         if (PCI(pc)->op == POC_GOTO) maxDist = MAX_DIST_GOTO;
+         
+         /* be careful NOT to make the jump instruction longer (might break previously shortened jumps!) */
+         if (lastTargetOp && newDist <= maxDist && lastTargetOp != PCI(pc)->pcop
+             && strcmp (lastTargetOp->name, PCI(pc)->pcop->name) != 0) {
+           //fprintf (stderr, "(1) ");pc->print(stderr, pc); fprintf (stderr, " --> %s\n", lastTargetOp->name);
+           if (pic16_pcode_verbose) { pic16_pCodeInsertAfter (pc->prev, pic16_newpCodeCharP("(1) jump chain resolved")); }
+           PCI(pc)->pcop->name = lastTargetOp->name;
+           change++;
+           opt_gotochain++;
+         } // if
+       } // if
+
+
+       if (IS_GOTO(pc)) {
+         int dist;
+         int condBraType = isSkipOnStatus(pc_prev);
+         label = PCI(pc)->pcop->name;
+         dist = findpCodeLabel(pc, label, MAX_DIST_BRA, &target);
+         if (dist < 0) dist = -dist;
+         //fprintf (stderr, "distance: %d (", dist); pc->print(stderr, pc);fprintf (stderr, ")\n");
+         isHandled = 0;
+         
+         
+         /* (2) remove "GOTO label; label:" */
+         if (isLabel (pc_next, label)) {
+           //fprintf (stderr, "(2) GOTO next instruction: ");pc->print(stderr, pc);fprintf (stderr, " --> ");pc_next->print(stderr, pc_next); fprintf(stderr, "\n");
+           // first remove all preceeding SKIP instructions
+           while (pc_prev && isPCI_SKIP(pc_prev)) {
+             // attach labels on this instruction to pc_next
+             //fprintf (stderr, "(2) preceeding SKIP removed: ");pc_prev->print(stderr, pc_prev);fprintf(stderr, "\n");
+             PCI(pc_next)->label = pic16_pBranchAppend (PCI(pc_prev)->label, PCI(pc_next)->label);
+             PCI(pc_prev)->label = NULL;
+             if (pic16_pcode_verbose) { pic16_pCodeInsertAfter (pc->prev, pic16_newpCodeCharP("(2) SKIP removed")); }
+             pic16_unlinkpCode (pc_prev);
+             pc_prev = pic16_findPrevInstruction (pc);
+           } // while
+           // now remove the redundant goto itself
+           PCI(pc_next)->label = pic16_pBranchAppend (PCI(pc)->label, PCI(pc_next)->label);
+           if (pic16_pcode_verbose) { pic16_pCodeInsertAfter (pc, pic16_newpCodeCharP("(2) GOTO next instruction removed")); }
+           pic16_unlinkpCode (pc);
+           pc = pic16_findPrevInstruction(pc_next->prev);
+           isHandled = 1; // do not perform further optimizations
+           opt_gotonext++;
+           change++;
+         } // if
+         
+         
+         /* (3) turn BTFSx STATUS,i; GOTO label into Bcc label if possible */
+         if (!isHandled && condBraType != -1 && hasNoLabel(pc)) {
+           if (dist < MAX_DIST_BCC) {
+             pCode *bcc = NULL;
+             switch (condBraType) {
+             case 0x00: bcc = pic16_newpCode (POC_BC, PCI(pc)->pcop);break;
+               // no BDC on DIGIT CARRY available
+             case 0x02: bcc = pic16_newpCode (POC_BZ, PCI(pc)->pcop);break;
+             case 0x03: bcc = pic16_newpCode (POC_BOV, PCI(pc)->pcop);break;
+             case 0x04: bcc = pic16_newpCode (POC_BN, PCI(pc)->pcop);break;
+             case 0x10: bcc = pic16_newpCode (POC_BNC, PCI(pc)->pcop);break;
+               // no BNDC on DIGIT CARRY available
+             case 0x12: bcc = pic16_newpCode (POC_BNZ, PCI(pc)->pcop);break;
+             case 0x13: bcc = pic16_newpCode (POC_BNOV, PCI(pc)->pcop);break;
+             case 0x14: bcc = pic16_newpCode (POC_BNN, PCI(pc)->pcop);break;
+             default:
+               // no replacement possible
+               bcc = NULL;
+               break;
+             } // switch
+             if (bcc) {
+               // ATTENTION: keep labels attached to BTFSx!
+               // HINT: GOTO is label free (checked above)
+               //fprintf (stderr, "%s:%d: (3) turning %s %s into %s %s\n", __FUNCTION__, __LINE__, PCI(pc)->mnemonic, label, PCI(bcc)->mnemonic, label);
+               isHandled = 1; // do not perform further optimizations
+               if (pic16_pcode_verbose) { pic16_pCodeInsertAfter(pc_prev->prev, pic16_newpCodeCharP("(3) conditional branch introduced")); }
+               pic16_pCodeReplace (pc_prev, bcc);
+               pc->destruct(pc);
+               pc = bcc;
+               opt_cond++;
+               change++;
+             } // if
+           } else {
+             //fprintf (stderr, "(%d, too far for Bcc)\n", dist);
+             cond_toofar++;
+           } // if
+         } // if
+
+         if (!isHandled) {
+           // (4) eliminate the following (common) tripel:
+           //           <pred.>;
+           //  labels1: Bcc label2;
+           //           GOTO somewhere;    ; <-- instruction referenced by pc
+           //  label2:  <cont.>
+           // and replace it by
+           //  labels1: B#(cc) somewhere;  ; #(cc) is the negated condition cc
+           //  label2:  <cont.>
+           // ATTENTION: all labels pointing to "Bcc label2" must be attached
+           //            to <cont.> instead
+           // ATTENTION: This optimization is only valid if <pred.> is
+           //            not a skip operation!
+           // ATTENTION: somewhere must be within MAX_DIST_BCC bytes!
+           // ATTENTION: no label may be attached to the GOTO instruction!
+           if (isConditionalBranch(pc_prev)
+               && (!isPCI_SKIP(pic16_findPrevInstruction(pc_prev->prev)))
+               && (dist < MAX_DIST_BCC)
+               && isLabel(pc_next,PCI(pc_prev)->pcop->name)
+               && hasNoLabel(pc)) {
+             pCode *newBcc = getNegatedBcc (pc_prev, PCI(pc)->pcop);
+           
+             if (newBcc) {
+               //fprintf (stderr, "%s:%d: (4) turning %s %s into %s %s\n", __FUNCTION__, __LINE__, PCI(pc)->mnemonic, label, PCI(newBcc)->mnemonic, label);
+               isHandled = 1; // do not perform further optimizations
+               if (pic16_pcode_verbose) { pic16_pCodeInsertAfter(pc_prev->prev, pic16_newpCodeCharP("(4) conditional skipping branch inverted")); }
+               pic16_pCodeReplace (pc_prev, newBcc);
+               pc->destruct(pc);
+               pc = newBcc;
+               opt_reorder++;
+               change++;
+               matchedInvertRule++;
+             }
+           }
+         }
+         
+         /* (5) now just turn GOTO into BRA */ 
+         if (!isHandled && (PCI(pc)->op == POC_GOTO)) {
+           if (dist < MAX_DIST_BRA) {
+             pCode *newBra = pic16_newpCode (POC_BRA, PCI(pc)->pcop);
+             //fprintf (stderr, "%s:%d: (5) turning %s %s into %s %s\n", __FUNCTION__, __LINE__, PCI(pc)->mnemonic, label, PCI(newBra)->mnemonic, label);
+             if (pic16_pcode_verbose) { pic16_pCodeInsertAfter(pc->prev, pic16_newpCodeCharP("(5) GOTO replaced by BRA")); }
+             pic16_pCodeReplace (pc, newBra);
+             pc = newBra;
+             opt++;
+             change++;
+           } else {
+             //fprintf (stderr, "(%d, too far for BRA)\n", dist);
+             toofar++;
+           }
+         } // if (!isHandled)
+       } // if
+
+       pc_prev = pc;
+       pc = pc_next;
+      } // while (pc)
+      
+      pBlockRemoveUnusedLabels (pb);
+      
+      // This line enables goto chain resolution!
+      if (matchedInvertRule > 1) matchedInvertRule = 1; else matchedInvertRule = 0;
+
+      iteration++;
+    } while (change); /* fixpoint iteration per pBlock */
+  } // for (pb)
+  
+  // emit some statistics concerning goto-optimization
+  // (maybe this should be moved to the general statistics?)
+  if (pic16_debug_verbose || pic16_pcode_verbose) {
+    fprintf (stderr, "optimize-goto:\n"
+            "\t%5d GOTO->BRA; (%d GOTOs too far)\n"
+            "\t%5d BTFSx, GOTO->Bcc (%d too far)\n"
+            "\t%5d conditional \"skipping\" jumps inverted\n"
+            "\t%5d GOTOs to next instruction removed\n"
+            "\t%5d chained GOTOs resolved\n",
+            opt, toofar, opt_cond, cond_toofar, opt_reorder, opt_gotonext, opt_gotochain);
+  } // if
+  //fprintf (stderr, "%s:%d: %s\n", __FILE__, __LINE__, __FUNCTION__);
+}
+
+#undef IS_GOTO
+#undef MAX_JUMPCHAIN_DEPTH
+#undef MAX_DIST_GOTO
+#undef MAX_DIST_BRA
+#undef MAX_DIST_BCC
+
+/** END OF RAPHAEL NEIDER'S ADDITIONS **/
 
 static void pBlockDestruct(pBlock *pb)
 {
@@ -6616,7 +7414,7 @@ static void pBlockDestruct(pBlock *pb)
     return;
 
 
-  free(pb);
+//  free(pb);
 
 }
 
@@ -6997,9 +7795,183 @@ void pic16_AnalyzepCode(char dbName)
 
   } while(changes && (i++ < MAX_PASSES));
 
+  
   buildCallTree();
 }
 
+
+/* convert a series of movff's of local regs to stack, with a single call to
+ * a support functions which does the same thing via loop */
+static void pic16_convertLocalRegs2Support(pCode *pcstart, pCode *pcend, int count, regs *r, int entry)
+{
+  pBranch *pbr;
+  pCode *pc, *pct;
+  char *fname[]={"__lr_store", "__lr_restore"};
+
+//    pc = pic16_newpCode(POC_CALL, pic16_popGetFromString( (entry?fname[0]:fname[1]) ));
+
+    pct = pic16_findNextInstruction(pcstart->next);
+    do {
+      pc = pct;
+      pct = pc->next;  //pic16_findNextInstruction(pc->next);
+//      pc->print(stderr, pc);
+      if(isPCI(pc) && PCI(pc)->label) {
+        pbr = PCI(pc)->label;
+        while(pbr && pbr->pc) {
+          PCI(pcstart)->label = pic16_pBranchAppend(PCI(pcstart)->label, pbr);
+          pbr = pbr->next;
+        }
+
+//        pc->print(stderr, pc);
+        /* unlink pCode */
+        pc->prev->next = pct;
+        pct->prev = pc->prev;
+//        pc->next = NULL;
+//        pc->prev = NULL;
+      }
+    } while ((pc) && (pc != pcend));
+
+    /* unlink movff instructions */
+    pcstart->next = pcend;
+    pcend->prev = pcstart;
+
+    pc = pcstart;
+//    if(!entry) {
+//      pic16_pCodeInsertAfter(pc, pct = pic16_newpCode(POC_MOVFF, pic16_popGet2p(
+//              pic16_popCopyReg(&pic16_pc_fsr0l), pic16_popCopyReg(&pic16_pc_fsr2l)))); pc = pct;
+//    }
+               
+    pic16_pCodeInsertAfter(pc, pct=pic16_newpCode(POC_LFSR, pic16_popGetLit2(0, pic16_popGetWithString(r->name)))); pc = pct;
+    pic16_pCodeInsertAfter(pc, pct=pic16_newpCode(POC_MOVLW, pic16_popGetLit( count ))); pc = pct;
+    pic16_pCodeInsertAfter(pc, pct=pic16_newpCode(POC_CALL, pic16_popGetWithString( fname[ (entry==1?0:1) ] ))); pc = pct;
+
+//    if(!entry) {
+//      pic16_pCodeInsertAfter(pc, pct = pic16_newpCode(POC_MOVFF, pic16_popGet2p(
+//              pic16_popCopyReg(&pic16_pc_fsr2l), pic16_popCopyReg(&pic16_pc_fsr0l)))); pc = pct;
+//    }
+
+    
+    {
+      symbol *sym;
+
+        sym = newSymbol( fname[ entry?0:1 ], 0 );
+        strcpy(sym->rname, fname[ entry?0:1 ]);
+        checkAddSym(&externs, sym);
+        
+//        fprintf(stderr, "%s:%d adding extern symbol %s in externs\n", __FILE__, __LINE__, fname[ entry?0:1 ]);
+    }
+
+}
+
+/*-----------------------------------------------------------------*/
+/* OptimizeLocalRegs - turn sequence of MOVFF instructions for     */
+/*    local registers to a support function call                   */
+/*-----------------------------------------------------------------*/
+void pic16_OptimizeLocalRegs(void)
+{
+  pBlock  *pb;
+  pCode   *pc;
+  pCodeInfo *pci;
+  pCodeOpLocalReg *pclr;
+  int regCount=0;
+  int inRegCount=0;
+  regs *r, *lastr=NULL, *firstr=NULL;
+  pCode *pcstart=NULL, *pcend=NULL;
+  int inEntry=0;
+  char *curFunc=NULL;
+
+       /* Overview:
+        *   local_regs begin mark
+        *      MOVFF r0x01, POSTDEC1
+        *      MOVFF r0x02, POSTDEC1
+        *      ...
+        *      ...
+        *      MOVFF r0x0n, POSTDEC1
+        *   local_regs end mark
+        *
+        * convert the above to the below:
+        *      MOVLW   starting_register_index
+        *      MOVWF   PRODL
+        *      MOVLW   register_count
+        *      call    __save_registers_in_stack
+        */
+
+    if(!the_pFile)
+      return;
+
+    for(pb = the_pFile->pbHead; pb; pb = pb->next) {
+      inRegCount = regCount = 0;
+      firstr = lastr = NULL;
+      for(pc = pb->pcHead; pc; pc = pc->next) {
+
+       /* hold current function name */
+       if(pc && isPCF(pc))curFunc = PCF(pc)->fname;
+       
+        if(pc && (pc->type == PC_INFO)) {
+          pci = PCINF(pc);
+
+          if(pci->type == INF_LOCALREGS) {
+            pclr = PCOLR(pci->oper1);
+            
+            if((pclr->type == LR_ENTRY_BEGIN)
+              || (pclr->type == LR_ENTRY_END))inEntry = 1;
+            else inEntry = 0;
+            
+            switch(pclr->type) {
+              case LR_ENTRY_BEGIN:
+              case LR_EXIT_BEGIN:
+                       inRegCount = 1; regCount = 0;
+                       pcstart = pc;   //pic16_findNextInstruction(pc->next);
+                       firstr = lastr = NULL;
+                       break;
+              
+              case LR_ENTRY_END:
+              case LR_EXIT_END:
+                       inRegCount = -1;
+                       pcend = pc;     //pic16_findPrevInstruction(pc->prev);
+
+#if 1
+                       if(curFunc && inWparamList(curFunc+1)) {
+                         fprintf(stderr, "sdcc: %s: warning: disabling lr-support for functionn %s\n",
+                                       filename, curFunc);
+                       } else {
+                          if(regCount>2) {
+                           pic16_convertLocalRegs2Support(pcstart, pcend, regCount,
+                             firstr, inEntry);
+                          }
+                        }
+#endif
+                        firstr = lastr = NULL;
+                       break;
+            }
+            
+            if(inRegCount == -1) {
+//              fprintf(stderr, "%s:%d registers used [%s] %d\n", __FILE__, __LINE__, inEntry?"entry":"exit", regCount);
+              regCount = 0;
+              inRegCount = 0;
+            }
+          }
+        } else {
+          if(isPCI(pc) && (PCI(pc)->op == POC_MOVFF) && (inRegCount == 1)) {
+            if(inEntry)
+              r = pic16_getRegFromInstruction(pc);
+            else
+              r = pic16_getRegFromInstruction2(pc);
+            if(r && (r->type == REG_GPR) && (r->pc_type == PO_GPR_TEMP)) {
+              if(!firstr)firstr = r;
+              regCount++;
+//              fprintf(stderr, "%s:%d\t%s\t%i\t%d/%d\n", __FILE__, __LINE__, r->name, r->rIdx);
+            }
+          }
+        }
+      }
+    }
+}
+              
+            
+
+
+
 /*-----------------------------------------------------------------*/
 /* ispCodeFunction - returns true if *pc is the pCode of a         */
 /*                   function                                      */