* src/SDCCglobl.h: ensure that PATH_MAX >= 2048 to guarantee
[fw/sdcc] / src / pic16 / pcode.c
index 38abb36008d12a4a4f10793609ed90eda8a02632..7d04d89db199f6267cd9d55ffd2f295da412791b 100644 (file)
@@ -106,12 +106,24 @@ pCodeOpReg pic16_pc_eecon2        = {{PO_SFR_REGISTER, "EECON2"}, -1, NULL, 0, NULL};
 pCodeOpReg pic16_pc_eedata     = {{PO_SFR_REGISTER, "EEDATA"}, -1, NULL, 0, NULL};
 pCodeOpReg pic16_pc_eeadr      = {{PO_SFR_REGISTER, "EEADR"}, -1, NULL, 0, NULL};
 
-
-
 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_stackpnt_lo;
+pCodeOpReg *pic16_stackpnt_hi;
+pCodeOpReg *pic16_stack_postinc;
+pCodeOpReg *pic16_stack_postdec;
+pCodeOpReg *pic16_stack_preinc;
+pCodeOpReg *pic16_stack_plusw;
+
+pCodeOpReg *pic16_framepnt_lo;
+pCodeOpReg *pic16_framepnt_hi;
+pCodeOpReg *pic16_frame_postinc;
+pCodeOpReg *pic16_frame_postdec;
+pCodeOpReg *pic16_frame_preinc;
+pCodeOpReg *pic16_frame_plusw;
+
 pCodeOpReg pic16_pc_gpsimio   = {{PO_GPR_REGISTER, "GPSIMIO"}, -1, NULL, 0, NULL};
 pCodeOpReg pic16_pc_gpsimio2  = {{PO_GPR_REGISTER, "GPSIMIO2"}, -1, NULL, 0, NULL};
 
@@ -175,12 +187,14 @@ 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);
 
 
 /****************************************************************/
@@ -194,6 +208,7 @@ pCodeInstruction pic16_pciADDWF = {
    genericPrint},
   POC_ADDWF,
   "ADDWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -221,6 +236,7 @@ pCodeInstruction pic16_pciADDFW = {
    genericPrint},
   POC_ADDFW,
   "ADDWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -248,6 +264,7 @@ pCodeInstruction pic16_pciADDWFC = { // mdubuc - New
    genericPrint},
   POC_ADDWFC,
   "ADDWFC",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -275,6 +292,7 @@ pCodeInstruction pic16_pciADDFWC = {
    genericPrint},
   POC_ADDFWC,
   "ADDWFC",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -302,6 +320,7 @@ pCodeInstruction pic16_pciADDLW = {
    genericPrint},
   POC_ADDLW,
   "ADDLW",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -329,6 +348,7 @@ pCodeInstruction pic16_pciANDLW = {
    genericPrint},
   POC_ANDLW,
   "ANDLW",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -356,6 +376,7 @@ pCodeInstruction pic16_pciANDWF = {
    genericPrint},
   POC_ANDWF,
   "ANDWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -383,6 +404,7 @@ pCodeInstruction pic16_pciANDFW = {
    genericPrint},
   POC_ANDFW,
   "ANDWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -409,6 +431,7 @@ pCodeInstruction pic16_pciBC = { // mdubuc - New
    genericPrint},
   POC_BC,
   "BC",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -436,6 +459,7 @@ pCodeInstruction pic16_pciBCF = {
    genericPrint},
   POC_BCF,
   "BCF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -463,6 +487,7 @@ pCodeInstruction pic16_pciBN = { // mdubuc - New
    genericPrint},
   POC_BN,
   "BN",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -490,6 +515,7 @@ pCodeInstruction pic16_pciBNC = { // mdubuc - New
    genericPrint},
   POC_BNC,
   "BNC",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -517,6 +543,7 @@ pCodeInstruction pic16_pciBNN = { // mdubuc - New
    genericPrint},
   POC_BNN,
   "BNN",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -544,6 +571,7 @@ pCodeInstruction pic16_pciBNOV = { // mdubuc - New
    genericPrint},
   POC_BNOV,
   "BNOV",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -571,6 +599,7 @@ pCodeInstruction pic16_pciBNZ = { // mdubuc - New
    genericPrint},
   POC_BNZ,
   "BNZ",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -598,6 +627,7 @@ pCodeInstruction pic16_pciBOV = { // mdubuc - New
    genericPrint},
   POC_BOV,
   "BOV",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -625,6 +655,7 @@ pCodeInstruction pic16_pciBRA = { // mdubuc - New
    genericPrint},
   POC_BRA,
   "BRA",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -652,6 +683,7 @@ pCodeInstruction pic16_pciBSF = {
    genericPrint},
   POC_BSF,
   "BSF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -679,6 +711,7 @@ pCodeInstruction pic16_pciBTFSC = {
    genericPrint},
   POC_BTFSC,
   "BTFSC",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -706,6 +739,7 @@ pCodeInstruction pic16_pciBTFSS = {
    genericPrint},
   POC_BTFSS,
   "BTFSS",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -733,6 +767,7 @@ pCodeInstruction pic16_pciBTG = { // mdubuc - New
    genericPrint},
   POC_BTG,
   "BTG",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -760,6 +795,7 @@ pCodeInstruction pic16_pciBZ = { // mdubuc - New
    genericPrint},
   POC_BZ,
   "BZ",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -787,6 +823,7 @@ pCodeInstruction pic16_pciCALL = {
    genericPrint},
   POC_CALL,
   "CALL",
+  4,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -803,7 +840,7 @@ pCodeInstruction pic16_pciCALL = {
   0,   // second literal operand
   POC_NOP,
   PCC_NONE, // inCond
-  PCC_NONE , // outCond
+  PCC_NONE, // outCond
   PCI_MAGIC
 };
 
@@ -814,6 +851,7 @@ pCodeInstruction pic16_pciCOMF = {
    genericPrint},
   POC_COMF,
   "COMF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -841,6 +879,7 @@ pCodeInstruction pic16_pciCOMFW = {
    genericPrint},
   POC_COMFW,
   "COMF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -868,6 +907,7 @@ pCodeInstruction pic16_pciCLRF = {
    genericPrint},
   POC_CLRF,
   "CLRF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -895,6 +935,7 @@ pCodeInstruction pic16_pciCLRWDT = {
    genericPrint},
   POC_CLRWDT,
   "CLRWDT",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -922,6 +963,7 @@ pCodeInstruction pic16_pciCPFSEQ = { // mdubuc - New
    genericPrint},
   POC_CPFSEQ,
   "CPFSEQ",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -949,6 +991,7 @@ pCodeInstruction pic16_pciCPFSGT = { // mdubuc - New
    genericPrint},
   POC_CPFSGT,
   "CPFSGT",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -976,6 +1019,7 @@ pCodeInstruction pic16_pciCPFSLT = { // mdubuc - New
    genericPrint},
   POC_CPFSLT,
   "CPFSLT",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1003,6 +1047,7 @@ pCodeInstruction pic16_pciDAW = {
    genericPrint},
   POC_DAW,
   "DAW",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1030,6 +1075,7 @@ pCodeInstruction pic16_pciDCFSNZ = { // mdubuc - New
    genericPrint},
   POC_DCFSNZ,
   "DCFSNZ",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1057,6 +1103,7 @@ pCodeInstruction pic16_pciDCFSNZW = { // mdubuc - New
    genericPrint},
   POC_DCFSNZW,
   "DCFSNZ",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1084,6 +1131,7 @@ pCodeInstruction pic16_pciDECF = {
    genericPrint},
   POC_DECF,
   "DECF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1111,6 +1159,7 @@ pCodeInstruction pic16_pciDECFW = {
    genericPrint},
   POC_DECFW,
   "DECF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1138,6 +1187,7 @@ pCodeInstruction pic16_pciDECFSZ = {
    genericPrint},
   POC_DECFSZ,
   "DECFSZ",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1165,6 +1215,7 @@ pCodeInstruction pic16_pciDECFSZW = {
    genericPrint},
   POC_DECFSZW,
   "DECFSZ",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1192,6 +1243,7 @@ pCodeInstruction pic16_pciGOTO = {
    genericPrint},
   POC_GOTO,
   "GOTO",
+  4,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1219,6 +1271,7 @@ pCodeInstruction pic16_pciINCF = {
    genericPrint},
   POC_INCF,
   "INCF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1246,6 +1299,7 @@ pCodeInstruction pic16_pciINCFW = {
    genericPrint},
   POC_INCFW,
   "INCF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1273,6 +1327,7 @@ pCodeInstruction pic16_pciINCFSZ = {
    genericPrint},
   POC_INCFSZ,
   "INCFSZ",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1300,6 +1355,7 @@ pCodeInstruction pic16_pciINCFSZW = {
    genericPrint},
   POC_INCFSZW,
   "INCFSZ",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1327,6 +1383,7 @@ pCodeInstruction pic16_pciINFSNZ = { // mdubuc - New
    genericPrint},
   POC_INFSNZ,
   "INFSNZ",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1354,6 +1411,7 @@ pCodeInstruction pic16_pciINFSNZW = { // vrokas - New
    genericPrint},
   POC_INFSNZW,
   "INFSNZ",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1381,6 +1439,7 @@ pCodeInstruction pic16_pciIORWF = {
    genericPrint},
   POC_IORWF,
   "IORWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1408,6 +1467,7 @@ pCodeInstruction pic16_pciIORFW = {
    genericPrint},
   POC_IORFW,
   "IORWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1435,6 +1495,7 @@ pCodeInstruction pic16_pciIORLW = {
    genericPrint},
   POC_IORLW,
   "IORLW",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1462,6 +1523,7 @@ pCodeInstruction pic16_pciLFSR = { // mdubuc - New
    genericPrint},
   POC_LFSR,
   "LFSR",
+  4,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1489,6 +1551,7 @@ pCodeInstruction pic16_pciMOVF = {
    genericPrint},
   POC_MOVF,
   "MOVF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1516,6 +1579,7 @@ pCodeInstruction pic16_pciMOVFW = {
    genericPrint},
   POC_MOVFW,
   "MOVF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1543,6 +1607,7 @@ pCodeInstruction pic16_pciMOVFF = { // mdubuc - New
    genericPrint},
   POC_MOVFF,
   "MOVFF",
+  4,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1569,6 +1634,7 @@ pCodeInstruction pic16_pciMOVLB = { // mdubuc - New
    genericPrint},
   POC_MOVLB,
   "MOVLB",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1595,6 +1661,7 @@ pCodeInstruction pic16_pciMOVLW = {
    genericPrint},
   POC_MOVLW,
   "MOVLW",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1622,6 +1689,7 @@ pCodeInstruction pic16_pciMOVWF = {
    genericPrint},
   POC_MOVWF,
   "MOVWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1648,6 +1716,7 @@ pCodeInstruction pic16_pciMULLW = { // mdubuc - New
    genericPrint},
   POC_MULLW,
   "MULLW",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1674,6 +1743,7 @@ pCodeInstruction pic16_pciMULWF = { // mdubuc - New
    genericPrint},
   POC_MULWF,
   "MULWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1700,6 +1770,7 @@ pCodeInstruction pic16_pciNEGF = { // mdubuc - New
    genericPrint},
   POC_NEGF,
   "NEGF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1726,6 +1797,7 @@ pCodeInstruction pic16_pciNOP = {
    genericPrint},
   POC_NOP,
   "NOP",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1752,6 +1824,7 @@ pCodeInstruction pic16_pciPOP = { // mdubuc - New
    genericPrint},
   POC_POP,
   "POP",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1778,6 +1851,7 @@ pCodeInstruction pic16_pciPUSH = {
    genericPrint},
   POC_PUSH,
   "PUSH",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1804,6 +1878,7 @@ pCodeInstruction pic16_pciRCALL = { // mdubuc - New
    genericPrint},
   POC_RCALL,
   "RCALL",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1831,6 +1906,7 @@ pCodeInstruction pic16_pciRETFIE = {
    genericPrint},
   POC_RETFIE,
   "RETFIE",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1858,6 +1934,7 @@ pCodeInstruction pic16_pciRETLW = {
    genericPrint},
   POC_RETLW,
   "RETLW",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1885,6 +1962,7 @@ pCodeInstruction pic16_pciRETURN = {
    genericPrint},
   POC_RETURN,
   "RETURN",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1911,6 +1989,7 @@ pCodeInstruction pic16_pciRLCF = { // mdubuc - New
    genericPrint},
   POC_RLCF,
   "RLCF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1938,6 +2017,7 @@ pCodeInstruction pic16_pciRLCFW = { // mdubuc - New
    genericPrint},
   POC_RLCFW,
   "RLCF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1965,6 +2045,7 @@ pCodeInstruction pic16_pciRLNCF = { // mdubuc - New
    genericPrint},
   POC_RLNCF,
   "RLNCF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -1991,6 +2072,7 @@ pCodeInstruction pic16_pciRLNCFW = { // mdubuc - New
    genericPrint},
   POC_RLNCFW,
   "RLNCF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2017,6 +2099,7 @@ pCodeInstruction pic16_pciRRCF = { // mdubuc - New
    genericPrint},
   POC_RRCF,
   "RRCF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2043,6 +2126,7 @@ pCodeInstruction pic16_pciRRCFW = { // mdubuc - New
    genericPrint},
   POC_RRCFW,
   "RRCF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2069,6 +2153,7 @@ pCodeInstruction pic16_pciRRNCF = { // mdubuc - New
    genericPrint},
   POC_RRNCF,
   "RRNCF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2096,6 +2181,7 @@ pCodeInstruction pic16_pciRRNCFW = { // mdubuc - New
    genericPrint},
   POC_RRNCFW,
   "RRNCF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2123,6 +2209,7 @@ pCodeInstruction pic16_pciSETF = { // mdubuc - New
    genericPrint},
   POC_SETF,
   "SETF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2150,6 +2237,7 @@ pCodeInstruction pic16_pciSUBLW = {
    genericPrint},
   POC_SUBLW,
   "SUBLW",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2177,6 +2265,7 @@ pCodeInstruction pic16_pciSUBFWB = {
    genericPrint},
   POC_SUBFWB,
   "SUBFWB",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2204,6 +2293,7 @@ pCodeInstruction pic16_pciSUBWF = {
    genericPrint},
   POC_SUBWF,
   "SUBWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2231,6 +2321,7 @@ pCodeInstruction pic16_pciSUBFW = {
    genericPrint},
   POC_SUBFW,
   "SUBWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2258,6 +2349,7 @@ pCodeInstruction pic16_pciSUBFWB_D1 = { // mdubuc - New
    genericPrint},
   POC_SUBFWB_D1,
   "SUBFWB",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2285,6 +2377,7 @@ pCodeInstruction pic16_pciSUBFWB_D0 = { // mdubuc - New
    genericPrint},
   POC_SUBFWB_D0,
   "SUBFWB",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2312,6 +2405,7 @@ pCodeInstruction pic16_pciSUBWFB_D1 = { // mdubuc - New
    genericPrint},
   POC_SUBWFB_D1,
   "SUBWFB",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2339,6 +2433,7 @@ pCodeInstruction pic16_pciSUBWFB_D0 = { // mdubuc - New
    genericPrint},
   POC_SUBWFB_D0,
   "SUBWFB",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2366,6 +2461,7 @@ pCodeInstruction pic16_pciSWAPF = {
    genericPrint},
   POC_SWAPF,
   "SWAPF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2393,6 +2489,7 @@ pCodeInstruction pic16_pciSWAPFW = {
    genericPrint},
   POC_SWAPFW,
   "SWAPF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2419,6 +2516,7 @@ pCodeInstruction pic16_pciTBLRD = {       // patch 15
    genericPrint},
   POC_TBLRD,
   "TBLRD*",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2445,6 +2543,7 @@ pCodeInstruction pic16_pciTBLRD_POSTINC = {       // patch 15
    genericPrint},
   POC_TBLRD_POSTINC,
   "TBLRD*+",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2471,6 +2570,7 @@ pCodeInstruction pic16_pciTBLRD_POSTDEC = {       // patch 15
    genericPrint},
   POC_TBLRD_POSTDEC,
   "TBLRD*-",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2497,6 +2597,7 @@ pCodeInstruction pic16_pciTBLRD_PREINC = {        // patch 15
    genericPrint},
   POC_TBLRD_PREINC,
   "TBLRD+*",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2523,6 +2624,7 @@ pCodeInstruction pic16_pciTBLWT = {       // patch 15
    genericPrint},
   POC_TBLWT,
   "TBLWT*",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2549,6 +2651,7 @@ pCodeInstruction pic16_pciTBLWT_POSTINC = {       // patch 15
    genericPrint},
   POC_TBLWT_POSTINC,
   "TBLWT*+",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2575,6 +2678,7 @@ pCodeInstruction pic16_pciTBLWT_POSTDEC = {       // patch 15
    genericPrint},
   POC_TBLWT_POSTDEC,
   "TBLWT*-",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2601,6 +2705,7 @@ pCodeInstruction pic16_pciTBLWT_PREINC = {        // patch 15
    genericPrint},
   POC_TBLWT_PREINC,
   "TBLWT+*",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2628,6 +2733,7 @@ pCodeInstruction pic16_pciTSTFSZ = { // mdubuc - New
    genericPrint},
   POC_TSTFSZ,
   "TSTFSZ",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2655,6 +2761,7 @@ pCodeInstruction pic16_pciXORWF = {
    genericPrint},
   POC_XORWF,
   "XORWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2682,6 +2789,7 @@ pCodeInstruction pic16_pciXORFW = {
    genericPrint},
   POC_XORFW,
   "XORWF",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2709,6 +2817,7 @@ pCodeInstruction pic16_pciXORLW = {
    genericPrint},
   POC_XORLW,
   "XORLW",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2736,6 +2845,7 @@ pCodeInstruction pic16_pciBANKSEL = {
    genericPrint},
   POC_BANKSEL,
   "BANKSEL",
+  2,
   NULL, // from branch
   NULL, // to branch
   NULL, // label
@@ -2872,6 +2982,20 @@ void  pic16_pCodeInitRegisters(void)
        pic16_pc_fsr2l.r = pic16_allocProcessorRegister(IDX_FSR2L, "FSR2L", PO_FSR0, 0x80);
        pic16_pc_fsr2h.r = pic16_allocProcessorRegister(IDX_FSR2H, "FSR2H", PO_FSR0, 0x80);
 
+       pic16_stackpnt_lo = &pic16_pc_fsr1l;
+       pic16_stackpnt_hi = &pic16_pc_fsr1h;
+       pic16_stack_postdec = &pic16_pc_postdec1;
+       pic16_stack_postinc = &pic16_pc_postinc1;
+       pic16_stack_preinc = &pic16_pc_preinc1;
+       pic16_stack_plusw = &pic16_pc_plusw1;
+       
+       pic16_framepnt_lo = &pic16_pc_fsr2l;
+       pic16_framepnt_hi = &pic16_pc_fsr2h;
+       pic16_frame_postdec = &pic16_pc_postdec2;
+       pic16_frame_postinc = &pic16_pc_postinc2;
+       pic16_frame_preinc = &pic16_pc_preinc2;
+       pic16_frame_plusw = &pic16_pc_plusw2;
+
        pic16_pc_indf0.r = pic16_allocProcessorRegister(IDX_INDF0,"INDF0", PO_INDF0, 0x80);
        pic16_pc_postinc0.r = pic16_allocProcessorRegister(IDX_POSTINC0, "POSTINC0", PO_INDF0, 0x80);
        pic16_pc_postdec0.r = pic16_allocProcessorRegister(IDX_POSTDEC0, "POSTDEC0", PO_INDF0, 0x80);
@@ -3336,6 +3460,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.       */
@@ -3349,6 +3492,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:
@@ -3689,7 +3834,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;
 
@@ -3895,10 +4040,10 @@ pCodeOp *pic16_newpCodeOpLit(int lit)
   pcop->type = PO_LITERAL;
 
   pcop->name = NULL;
-//  if(lit>=0)
-    sprintf(s,"0x%02hhx",(char)(lit));
-//  else
-//    sprintf(s, "%i", lit);
+  //if(lit>=0)
+    sprintf(s,"0x%02hhx", (unsigned char)lit);
+  //else
+  //  sprintf(s, "%i", lit);
   
   if(s)
     pcop->name = Safe_strdup(s);
@@ -3921,11 +4066,11 @@ pCodeOp *pic16_newpCodeOpLit2(int lit, pCodeOp *arg2)
   pcop->type = PO_LITERAL;
 
   pcop->name = NULL;
-  if(lit>=0) {
-    sprintf(s,"0x%02x, %s",lit, tb);
+  //if(lit>=0) {
+    sprintf(s,"0x%02x, %s", (unsigned char)lit, tb);
     if(s)
       pcop->name = Safe_strdup(s);
-  }
+  //}
 
   ((pCodeOpLit2 *)pcop)->lit = lit;
   ((pCodeOpLit2 *)pcop)->arg2 = arg2;
@@ -4538,7 +4683,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
 
@@ -4609,7 +4754,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) {
@@ -4782,14 +4938,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
@@ -4904,15 +5062,19 @@ static void genericPrint(FILE *of, pCode *pc)
       }
     }
           
-    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;
+    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:
     fprintf(of,"%s\n", ((pCodeComment *)pc)->comment);
@@ -5931,14 +6093,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;
 }
 
 
@@ -6105,7 +6267,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)
@@ -6196,7 +6358,7 @@ static void insertBankSwitch(unsigned char position, pCode *pc)
                                pic16_pCodeInsertAfter(ppc, npci);
                                
                                /* extra instructions to handle invertion */
-                               pcnext = pic16_newpCode(POC_GOTO, pic16_popGetLabel(tlbl->key));
+                               pcnext = pic16_newpCode(POC_BRA, pic16_popGetLabel(tlbl->key));
                                pic16_pCodeInsertAfter(npci, pcnext);
                                pic16_pCodeInsertAfter(pc->prev, new_pc);
                                
@@ -6610,16 +6772,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);
+
+  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);
+  }
 
-//  fprintf(stderr, "%s:%d pc type: %s\n", __FILE__, __LINE__, pic16_pCodeOpType(pc));
-  pcop = pic16_newpCodeOpBit(pc->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;
 }
@@ -6661,9 +6831,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);
 
@@ -6687,6 +6866,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;
 
@@ -6704,20 +6884,555 @@ static void pic16_FixRegisterBanking(pBlock *pb)
                
                /* if previous instruction is a skip one, then set flag
                 * to 2 and call insertBankSwitch */
-               if(pcprev && isPCI_SKIP(pcprev))flag=2; //goto loop;
+               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
+#if 0
+  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
+#endif
+  //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)
 {
@@ -7147,9 +7862,21 @@ static void pic16_convertLocalRegs2Support(pCode *pcstart, pCode *pcend, int cou
     pcstart->next = pcend;
     pcend->prev = pcstart;
 
-    pic16_pCodeInsertAfter(pcstart, (pct=pic16_newpCode(POC_LFSR, pic16_popGetLit2(0, pic16_popGetWithString(r->name))))); pc = pct;
+    pc = pcstart;
+//    if(!entry) {
+//      pic16_pCodeInsertAfter(pc, pct = pic16_newpCode(POC_MOVFF, pic16_popGet2p(
+//              pic16_popCopyReg(&pic16_pc_fsr0l), pic16_popCopyReg(pic16_framepnt_lo)))); 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) ] )));
+    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_framepnt_lo), pic16_popCopyReg(&pic16_pc_fsr0l)))); pc = pct;
+//    }
+
     
     {
       symbol *sym;
@@ -7163,7 +7890,6 @@ static void pic16_convertLocalRegs2Support(pCode *pcstart, pCode *pcend, int cou
 
 }
 
-
 /*-----------------------------------------------------------------*/
 /* OptimizeLocalRegs - turn sequence of MOVFF instructions for     */
 /*    local registers to a support function call                   */
@@ -7179,6 +7905,7 @@ void pic16_OptimizeLocalRegs(void)
   regs *r, *lastr=NULL, *firstr=NULL;
   pCode *pcstart=NULL, *pcend=NULL;
   int inEntry=0;
+  char *curFunc=NULL;
 
        /* Overview:
         *   local_regs begin mark
@@ -7203,6 +7930,10 @@ void pic16_OptimizeLocalRegs(void)
       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);
 
@@ -7227,9 +7958,14 @@ void pic16_OptimizeLocalRegs(void)
                        pcend = pc;     //pic16_findPrevInstruction(pc->prev);
 
 #if 1
-                        if(regCount>2) {
-                          pic16_convertLocalRegs2Support(pcstart, pcend, regCount,
-                               firstr, inEntry);
+                       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;
@@ -7796,3 +8532,663 @@ char *dumpPicOptype(PIC_OPTYPE type)
 {
        return (pic_optype_names[ type ]);
 }
+
+
+/*** BEGIN of stuff belonging to the BANKSEL optimization ***/
+#include "graph.h"
+
+#define MAX_COMMON_BANK_SIZE    32
+#define FIRST_PSEUDO_BANK_NR  1000
+
+hTab *sym2bank = NULL; // <OPERAND NAME> --> <PSEUDO BANK NR>
+hTab *bank2sym = NULL; // <PSEUDO BANK NR> --> <OPERAND NAME>
+hTab *coerce = NULL;   // <PSEUDO BANK NR> --> <&PSEUDOBANK>
+Graph *adj = NULL;
+
+typedef enum { INVALID_BANK = -1, UNKNOWN_BANK = -2, FIXED_BANK = -3 } pseudoBankNr;
+
+typedef struct {
+  pseudoBankNr bank;  // number assigned to this pseudoBank
+  unsigned int size;  // number of operands assigned to this bank
+  unsigned int ref;   // number of symbols referring to this pseudoBank (for garbage collection)
+} pseudoBank;
+
+/*----------------------------------------------------------------------*/
+/* hashSymbol - hash function used to map SYMBOLs (or operands) to ints */
+/*----------------------------------------------------------------------*/
+unsigned int hashSymbol (const char *str)
+{
+  unsigned int res = 0;
+  if (!str) return 0;
+
+  while (*str) {
+    res ^= (*str);
+    res = (res << 4) | (res >> (8 * sizeof(unsigned int) - 4));
+    str++;
+  } // while
+
+  return res;
+}
+
+/*-----------------------------------------------------------------------*/
+/* compareSymbol - return 1 iff sym1 equals sym2                         */
+/*-----------------------------------------------------------------------*/
+int compareSymbol (const void *sym1, const void *sym2)
+{
+  char *s1 = (char*) sym1;
+  char *s2 = (char*) sym2;
+  
+  return (strcmp (s1,s2) == 0);
+}
+
+/*-----------------------------------------------------------------------*/
+/* comparePre - return 1 iff p1 == p2                                    */
+/*-----------------------------------------------------------------------*/
+int comparePtr (const void *p1, const void *p2)
+{
+  return (p1 == p2);
+}
+
+/*----------------------------------------------------------*/
+/* getSymbolFromOperand - return a pointer to the symbol in */
+/*                        the given operand and its length  */
+/*----------------------------------------------------------*/
+char *getSymbolFromOperand (char *op, unsigned int *len)
+{
+  char *sym, *curr;
+  *len = 0;
+
+  if (!op) return NULL;
+
+  // we recognize two forms of operands: SYMBOL and (SYMBOL + offset)
+  sym = op;
+  if (*sym == '(') sym++;
+
+  curr = sym;
+  while (((*curr >= 'A') && (*curr <= 'Z'))
+        || ((*curr >= 'a') && (*curr <= 'z'))
+        || ((curr != sym) && (*curr >= '0') && (*curr <= '9'))
+        || (*curr == '_')) {
+    // find end of symbol [A-Za-z_]?[A-Za-z0-9]*
+    curr++;
+    (*len)++;
+  } // while
+
+  return sym;
+}
+
+/*--------------------------------------------------------------------------*/
+/* getSymFromBank - get (one) name of a symbol assigned to the given bank   */
+/*--------------------------------------------------------------------------*/
+char *getSymFromBank (pseudoBankNr bank)
+{
+  assert (bank2sym);
+
+  if (bank < 0) return "<INVALID BANK NR>";
+  return hTabFindByKey (bank2sym, bank % bank2sym->size, (void *) bank, &comparePtr);
+}
+
+/*-----------------------------------------------------------------------*/
+/* getPseudoBsrFromOperand - maps a string to its corresponding pseudo   */
+/*                           bank number (uses hTab sym2bank), if the    */
+/*                           symbol is not yet assigned a pseudo bank it */
+/*                           is assigned one here                        */
+/*-----------------------------------------------------------------------*/
+pseudoBankNr getPseudoBankNrFromOperand (const char *op)
+{
+  static pseudoBankNr next_bank = FIRST_PSEUDO_BANK_NR;
+  pseudoBankNr bank;
+  unsigned int hash;
+
+  assert (sym2bank);
+
+  hash = hashSymbol (op) % sym2bank->size;
+  bank = (pseudoBankNr) hTabFindByKey (sym2bank, hash, op, &compareSymbol);
+  if (bank == (pseudoBankNr)NULL) bank = UNKNOWN_BANK;
+
+  if (bank == UNKNOWN_BANK) {
+    // create a pseudo bank for the operand
+    bank = next_bank++;
+    hTabAddItemLong (&sym2bank, hash, (char *)op, (void *)bank);
+    hTabAddItemLong (&bank2sym, bank, (void *) bank, (void *)op);
+    getOrAddGNode (adj, NULL, bank); // adds the node if it does not exist yet
+    //fprintf (stderr, "%s:%d: adding %s with hash %u in bank %u\n", __FUNCTION__, __LINE__, op, hash, bank);
+  } else {
+    //fprintf (stderr, "%s:%d: found %s with hash %u in bank %u\n", __FUNCTION__, __LINE__, op, hash, bank);
+  } // if
+
+  assert (bank >= 0);
+
+  return bank;
+}
+
+/*--------------------------------------------------------------------*/
+/* isBanksel - check whether the given pCode is a BANKSEL instruction */
+/*--------------------------------------------------------------------*/
+int isBanksel (pCode *pc)
+{
+  if (!pc) return 0;
+
+  if (isPCI(pc) && (PCI(pc)->op == POC_BANKSEL || PCI(pc)->op == POC_MOVLB)) {
+    // BANKSEL <variablename>  or  MOVLB <banknr>
+    //fprintf (stderr, "%s:%d: BANKSEL found: %s %s\n", __FUNCTION__, __LINE__, PCAD(pc)->directive, PCAD(pc)->arg);
+    return 1;
+  }
+
+  // check for inline assembler BANKSELs
+  if (isPCAD(pc) && PCAD(pc)->directive && (STRCASECMP(PCAD(pc)->directive,"BANKSEL") == 0 ||
+                                           STRCASECMP(PCAD(pc)->directive,"MOVLB") == 0)) {
+    //fprintf (stderr, "%s:%d: BANKSEL found: %s %s\n", __FUNCTION__, __LINE__, PCAD(pc)->directive, PCAD(pc)->arg);
+    return 1;
+  }
+
+  // assume pc is no BANKSEL instruction
+  return 0;
+}
+
+/*---------------------------------------------------------------------------------*/
+/* invalidatesBSR - check whether the pCodeInstruction passed in modifies the BSR  */
+/*                  This method can not guarantee to find all modifications of the */
+/*                  BSR (e.g. via INDirection registers) but covers all compiler   */
+/*                  generated plus some cases.                                     */
+/*---------------------------------------------------------------------------------*/
+int invalidatesBSR(pCode *pc)
+{
+  // assembler directives invalidate BSR (well, they might, we don't know)
+  if (isPCAD(pc)) return 1;
+
+  // only ASMDIRs and pCodeInstructions can invalidate BSR
+  if (!isPCI(pc)) return 0;
+
+  // we have a pCodeInstruction
+
+  // check for BSR modifying instructions
+  switch (PCI(pc)->op) {
+  case POC_CALL:
+  case POC_RCALL:
+  case POC_MOVLB:
+  case POC_RETFIE:  // might be used as CALL replacement
+  case POC_RETLW:   // might be used as CALL replacement
+  case POC_RETURN:  // might be used as CALL replacement
+  case POC_BANKSEL:
+    return 1;
+    break;
+
+  default:          // other instruction do not change BSR unless BSR is an explicit operand!
+    // TODO: check for BSR as an explicit operand (e.g. INCF BSR,F), which should be rather unlikely...!
+    break;
+  } // switch
+
+  // no change of BSR possible/probable
+  return 0;
+}
+
+/*------------------------------------------------------------*/
+/* getBankFromBanksel - return the pseudo bank nr assigned to */
+/*                      the symbol referenced in this BANKSEL */
+/*------------------------------------------------------------*/
+pseudoBankNr getBankFromBanksel (pCode *pc)
+{
+  char *sym;
+  int data = (int)NULL;
+
+  if (!pc) return INVALID_BANK;
+  
+  if (isPCAD(pc) && PCAD(pc)->directive) {
+    if (STRCASECMP(PCAD(pc)->directive,"BANKSEL") == 0) {
+      // get symbolname from PCAD(pc)->arg
+      //fprintf (stderr, "%s:%d: BANKSEL found: %s %s\n", __FUNCTION__, __LINE__, PCAD(pc)->directive, PCAD(pc)->arg);
+      sym = PCAD(pc)->arg;
+      data = getPseudoBankNrFromOperand (sym);
+      //fprintf (stderr, "symbol: %s, data=%i\n", sym, data);
+    } else if (STRCASECMP(PCAD(pc)->directive,"MOVLB")) {
+      // get (literal) bank number from PCAD(pc)->arg
+      fprintf (stderr, "%s:%d: MOVLB found: %s %s\n", __FUNCTION__, __LINE__, PCAD(pc)->directive, PCAD(pc)->arg);
+      assert (0 && "not yet implemented - turn off banksel optimization for now");
+    }
+  } else if (isPCI(pc)) {
+    if (PCI(pc)->op == POC_BANKSEL) {
+      // get symbolname from PCI(pc)->pcop->name (?)
+      //fprintf (stderr, "%s:%d: BANKSEL found: %s %s\n", __FUNCTION__, __LINE__, PCI(pc)->mnemonic, PCI(pc)->pcop->name);
+      sym = PCI(pc)->pcop->name;
+      data = getPseudoBankNrFromOperand (sym);
+      //fprintf (stderr, "symbol: %s, data=%i\n", sym, data);
+    } else if (PCI(pc)->op == POC_MOVLB) {
+      // get (literal) bank number from PCI(pc)->pcop->name
+      fprintf (stderr, "%s:%d: MOVLB found: %s %s\n", __FUNCTION__, __LINE__, PCI(pc)->mnemonic, PCI(pc)->pcop->name);
+      assert (0 && "not yet implemented - turn off banksel optimization for now");
+    }
+  }
+  
+  if (data == 0)
+    // no assigned bank could be found
+    return UNKNOWN_BANK;
+  else
+    return data;
+}
+
+/*------------------------------------------------------------------------------*/
+/* getEffectiveBank - resolves the currently assigned effective pseudo bank nr  */
+/*------------------------------------------------------------------------------*/
+pseudoBankNr getEffectiveBank (pseudoBankNr bank)
+{
+  pseudoBank *data;
+
+  if (bank < FIRST_PSEUDO_BANK_NR) return bank;
+
+  do {
+    //fprintf (stderr, "%s:%d: bank=%d\n", __FUNCTION__, __LINE__, bank);
+    data = (pseudoBank *) hTabFindByKey (coerce, bank % coerce->size, (void *) bank, &comparePtr);
+    if (data) {
+      if (data->bank != bank)
+       bank = data->bank;
+      else
+       data = NULL;
+    }
+  } while (data);
+  
+  //fprintf (stderr, "%s:%d: effective bank=%d\n", __FUNCTION__, __LINE__, bank);
+  return bank;
+}
+
+/*------------------------------------------------------------------*/
+/* attachBsrInfo2pBlock - create a look-up table as to which pseudo */
+/*                        bank is selected at a given pCode         */
+/*------------------------------------------------------------------*/
+
+/* Create a graph with pseudo banks as its nodes and switches between
+ * these as edges (with the edge weight representing the absolute
+ * number of BANKSELs from one to the other).
+ * Removes redundand BANKSELs instead iff mod == 1.
+ * BANKSELs update the pseudo BSR, labels invalidate the current BSR
+ * value (setting it to 0=UNNKOWN), (R)CALLs also invalidate the
+ * pseudo BSR.
+ * TODO: check ALL instructions operands if they modify BSR directly...
+ *
+ * pb - the pBlock to annotate
+ * mod  - select either graph creation (0) or BANKSEL removal (1)
+ */
+unsigned int attachBsrInfo2pBlock (pBlock *pb, int mod)
+{
+  pCode *pc, *pc_next;
+  unsigned int prevBSR = UNKNOWN_BANK, pseudoBSR = UNKNOWN_BANK;
+  int isBankselect = 0;
+  unsigned int banksels=0;
+  
+  if (!pb) return 0;
+
+  pc = pic16_findNextInstruction(pb->pcHead);
+  while (pc) {
+    isBankselect = isBanksel (pc);
+    pc_next = pic16_findNextInstruction (pc->next);
+
+    if (!hasNoLabel (pc)) {
+      // we don't know our predecessors -- assume different BSRs
+      prevBSR = UNKNOWN_BANK;
+      pseudoBSR = UNKNOWN_BANK;
+      //fprintf (stderr, "invalidated by label at "); pc->print (stderr, pc);
+    } // if
+
+    // check if this is a BANKSEL instruction
+    if (isBankselect) {
+      pseudoBSR = getEffectiveBank (getBankFromBanksel(pc));
+      //fprintf (stderr, "BANKSEL via "); pc->print (stderr, pc);
+      if (mod) {
+       if (prevBSR == pseudoBSR && pseudoBSR >= 0) {
+         //fprintf (stderr, "removing redundant "); pc->print (stderr, pc);
+         if (1 || pic16_pcode_verbose) pic16_pCodeInsertAfter (pc->prev, pic16_newpCodeCharP("removed redundant BANKSEL"));
+         pic16_unlinkpCode (pc);
+         banksels++;
+       }
+      } else {
+       addGEdge2 (getOrAddGNode (adj, NULL, prevBSR), getOrAddGNode (adj, NULL, pseudoBSR), 1, 0);
+       banksels++;
+      }
+    } // if
+
+    if (!isBankselect && invalidatesBSR(pc)) {
+      // check if this instruction invalidates the pseudoBSR
+      pseudoBSR = UNKNOWN_BANK;
+      //fprintf (stderr, "invalidated via "); pc->print (stderr, pc);
+    } // if
+
+    prevBSR = pseudoBSR;
+    pc = pc_next;
+  } // while
+
+  return banksels;
+}
+
+/*------------------------------------------------------------------------------------*/
+/* assignToSameBank - returns 0 on success or an error code                           */
+/*  1 - common bank would be too large                                                */
+/*  2 - assignment to fixed (absolute) bank not performed                             */
+/*                                                                                    */
+/* This functions assumes that unsplittable operands are already assigned to the same */
+/* bank (e.g. all objects being referenced as (SYMBOL + offset) must be in the same   */
+/* bank so that we can make sure the bytes are laid out sequentially in memory)       */
+/* TODO: Symbols with an abslute address must be handled specially!                   */
+/*------------------------------------------------------------------------------------*/
+int assignToSameBank (int bank0, int bank1, int doAbs)
+{
+  int eff0, eff1, dummy;
+  pseudoBank *pbank0, *pbank1;
+  hashtItem *hitem;
+
+  eff0 = getEffectiveBank (bank0);
+  eff1 = getEffectiveBank (bank1);
+
+  //fprintf (stderr, "%s:%d: bank0=%d/%d, bank1=%d/%d, doAbs=%d\n", __FUNCTION__, __LINE__, bank0, eff0, bank1, eff1, doAbs);
+
+  // nothing to do if already same bank
+  if (eff0 == eff1) return 0;
+
+  if (!doAbs && (eff0 < FIRST_PSEUDO_BANK_NR || eff1 < FIRST_PSEUDO_BANK_NR))
+    return 2;
+
+  // ensure eff0 < eff1
+  if (eff0 > eff1) {
+    // swap eff0 and eff1
+    dummy = eff0;
+    eff0 = eff1;
+    eff1 = dummy;
+    dummy = bank0;
+    bank0 = bank1;
+    bank1 = dummy;
+  } // if
+
+  // now assign bank eff1 to bank eff0
+  pbank0 = (pseudoBank *) hTabFindByKey (coerce, eff0 % coerce->size, (void *) eff0, &comparePtr);
+  if (!pbank0) {
+    pbank0 = Safe_calloc (1, sizeof (pseudoBank));
+    pbank0->bank = eff0;
+    pbank0->size = 1;
+    pbank0->ref = 1;
+    hTabAddItemLong (&coerce, eff0 % coerce->size, (void *) eff0, (void *) pbank0);
+  } // if
+
+  pbank1 = NULL;
+  hitem = hTabSearch (coerce, eff1 % coerce->size);
+  while (hitem && hitem->pkey != (void *)eff1)
+    hitem = hitem->next;
+
+  if (hitem) pbank1 = (pseudoBank *) hitem->item;
+
+#if 0
+  fprintf (stderr, "bank #%d/%d & bank #%d/%d --> bank #%d: %u (%s & %s)\n", bank0, eff0, bank1, eff1,
+          pbank0->bank, pbank0->size,
+          getSymFromBank (eff0), getSymFromBank (eff1));
+#endif
+
+  if (pbank1) {
+    if (pbank0->size + pbank1->size > MAX_COMMON_BANK_SIZE) {
+#if 0
+      fprintf (stderr, "bank #%d: %u, bank #%d: %u --> bank #%d': %u > %u (%s,%s)\n",
+              pbank0->bank, pbank0->size, pbank1->bank, pbank1->size,
+              pbank0->bank, pbank0->size + pbank1->size, MAX_COMMON_BANK_SIZE,
+              getSymFromBank (pbank0->bank), getSymFromBank (pbank1->bank));
+#endif
+      return 1;
+    } // if
+    pbank0->size += pbank1->size;
+    pbank1->ref--;
+    if (pbank1->ref == 0) Safe_free (pbank1);
+  } else {
+    pbank0->size++;
+  } // if
+
+  if (hitem)
+    hitem->item = pbank0;
+  else  
+    hTabAddItemLong (&coerce, eff1 % coerce->size, (void *) eff1, (void *) pbank0);
+  pbank0->ref++;
+
+  //fprintf (stderr, "%s:%d: leaving.\n", __FUNCTION__, __LINE__);
+
+  return 0;
+}
+
+/*----------------------------------------------------------------*/
+/* mergeGraphNodes - combines two nodes into one and modifies all */
+/*                   edges to and from the nodes accordingly      */
+/* This method needs complete backedges, i.e. if (A,B) is an edge */
+/* then also (B,A) must be an edge (possibly with weight 0).      */
+/*----------------------------------------------------------------*/
+void mergeGraphNodes (GraphNode *node1, GraphNode *node2)
+{
+  GraphEdge *edge, *backedge, *nextedge;
+  GraphNode *node;
+  int backweight;
+
+  assert (node1 && node2);
+  assert (node1 != node2);
+  
+  // add all edges starting at node2 to node1
+  edge = node2->edge;
+  while (edge) {
+    nextedge = edge->next;
+    node = edge->node;
+    backedge = getGEdge (node, node2);
+    if (backedge)
+      backweight = backedge->weight;
+    else
+      backweight = 0;
+    // insert edges (node1,node) and (node,node1)
+    addGEdge2 (node1, node, edge->weight, backweight);
+    // remove edges (node, node2) and (node2, node)
+    remGEdge (node2, node);
+    remGEdge (node, node2);
+    edge = nextedge;
+  } // while
+  
+  // now node2 should not be referenced by any other GraphNode...
+  //remGNode (adj, node2->data, node2->hash);
+}
+
+/*----------------------------------------------------------------*/
+/* showGraph - dump the current BANKSEL graph as a node/edge list */
+/*----------------------------------------------------------------*/
+void showGraph (Graph *g)
+{
+  GraphNode *node;
+  GraphEdge *edge;
+  pseudoBankNr bankNr;
+  pseudoBank *pbank;
+  unsigned int size;
+
+  node = g->node;
+  while (node) {
+    edge = node->edge;
+    bankNr = getEffectiveBank (node->hash);
+    assert (bankNr >= 0);
+    pbank = (pseudoBank *) hTabFindByKey (coerce, bankNr % coerce->size, (void *) bankNr, &comparePtr);
+    if (pbank) {
+      bankNr = pbank->bank;
+      size = pbank->size;
+    } else {
+      size = 1;
+    }
+    
+    fprintf (stderr, "edges from %s (bank %u, size %u) to:\n", getSymFromBank (node->hash), bankNr, size);
+
+    while (edge) {
+      if (edge->weight > 0)
+       fprintf (stderr, "  %4u x %s\n", edge->weight, getSymFromBank (edge->node->hash));
+      edge = edge->next;
+    } // while (edge)
+    node = node->next;
+  } // while (node)
+}
+
+/*---------------------------------------------------------------*/
+/* pic16_OptimizeBanksel - remove redundant BANKSEL instructions */
+/*---------------------------------------------------------------*/
+void pic16_OptimizeBanksel ()
+{
+  GraphNode *node, *node1, *node1next;
+
+#if 0
+  // needed for more effective bank assignment (needs adjusted pic16_emit_usection())
+  GraphEdge *edge, *backedge;
+  GraphEdge *max;
+  int maxWeight, weight, mergeMore, absMaxWeight;
+  pseudoBankNr curr0, curr1;
+#endif
+  pseudoBank *pbank;
+  pseudoBankNr bankNr;
+  char *base_symbol0, *base_symbol1;
+  int len0, len1;
+  pBlock *pb;
+  set *set;
+  regs *reg;
+  unsigned int bankselsTotal = 0, bankselsRemoved = 0; 
+
+  //fprintf (stderr, "%s:%s:%d: entered.\n", __FILE__, __FUNCTION__, __LINE__);
+
+  if (!the_pFile || !the_pFile->pbHead) return;
+
+  adj = newGraph (NULL);
+  sym2bank = newHashTable ( 255 );
+  bank2sym = newHashTable ( 255 );
+  coerce = newHashTable ( 255 );
+
+  // create graph of BANKSEL relationships (node = operands, edge (A,B) iff BANKSEL B follows BANKSEL A)
+  for (pb = the_pFile->pbHead; pb; pb = pb->next) {
+    bankselsTotal += attachBsrInfo2pBlock (pb, 0);
+  } // for pb
+
+#if 1
+  // assign symbols with absolute addresses to their respective bank nrs
+  set = pic16_fix_udata;
+  for (reg = setFirstItem (set); reg; reg = setNextItem (set)) {
+    bankNr = reg->address >> 8;
+    node = getOrAddGNode (adj, NULL, bankNr);
+    bankNr = (pseudoBankNr) getEffectiveBank (getPseudoBankNrFromOperand(reg->name));
+    assignToSameBank (node->hash, bankNr, 1);
+    
+    assert (bankNr >= 0);
+    pbank = (pseudoBank *) hTabFindByKey (coerce, bankNr % coerce->size, (void *) bankNr, &comparePtr);
+    if (!pbank) {
+      pbank = Safe_calloc (1, sizeof (pseudoBank));
+      pbank->bank = reg->address >> 8; //FIXED_BANK;
+      pbank->size = 1;
+      pbank->ref = 1;
+      hTabAddItemLong (&coerce, bankNr % coerce->size, (void *) bankNr, pbank);
+    } else {
+      assert (pbank->bank == (reg->address >> 8));
+      pbank->bank = reg->address >> 8; //FIXED_BANK;
+    }
+    //fprintf (stderr, "ABS: %s (%d bytes) at %x in bank %u\n", reg->name, reg->size, reg->address, bankNr);
+  } // for reg
+#endif
+
+#if 1
+  // assign operands referring to the same symbol (which is not given an absolute address) to the same bank
+  //fprintf (stderr, "assign operands with the same symbol to the same bank\n");
+  node = adj->node;
+  while (node) {
+    if (node->hash < 0) { node = node->next; continue; }
+    base_symbol0 = getSymbolFromOperand (getSymFromBank (getEffectiveBank(node->hash)), &len0);
+    node1 = node->next;
+    while (node1) {
+      if (node1->hash < 0) { node1 = node1->next; continue; }
+      node1next = node1->next;
+      base_symbol1 = getSymbolFromOperand (getSymFromBank (getEffectiveBank (node1->hash)), &len1);
+      if (len0 == len1 && len0 > 0 && strncmp (base_symbol0, base_symbol1, len0) == 0) {
+       // TODO: check for symbols with absolute addresses -- these might be placed across bank boundaries!
+       //fprintf (stderr, "merging %s and %s\n", getSymFromBank (getEffectiveBank(node->hash)), getSymFromBank (getEffectiveBank(node1->hash)));
+       if (assignToSameBank (node->hash, node1->hash, 0)) {
+         fprintf (stderr, "%s(%d) == %s(%d)\n", base_symbol0, len0, base_symbol1, len1);
+         assert (0 && "Could not assign a symbol to a bank!");
+       }
+       mergeGraphNodes (node, node1);
+       /*
+       if (node->hash < node1->hash)
+         mergeGraphNodes (node, node1);
+       else
+         mergeGraphNodes (node1, node); // this removes node so node->next will fail...
+       */
+      } // if
+      node1 = node1next;
+    } // while (node1)
+    node = node->next;
+  } // while (node)
+#endif
+
+#if 0
+  // >>> THIS ALSO NEEDS AN UPDATED pic16_emit_usection() TO REFLECT THE BANK ASSIGNMENTS <<<
+  // assign tightly coupled operands to the same (pseudo) bank
+  //fprintf (stderr, "assign tightly coupled operands to the same bank\n");
+  mergeMore = 1;
+  absMaxWeight = 0;
+  while (mergeMore) {
+    node = adj->node;
+    max = NULL;
+    maxWeight = 0;
+    while (node) {
+      curr0 = getEffectiveBank (node->hash);
+      if (curr0 < 0) { node = node->next; continue; }
+      edge = node->edge;
+      while (edge) {
+       assert (edge->src == node);
+       backedge = getGEdge (edge->node, edge->src);
+       weight = edge->weight + (backedge ? backedge->weight : 0);
+       curr1 = getEffectiveBank (edge->node->hash);
+       if (curr1 < 0) { edge = edge->next; continue; }
+
+       // merging is only useful if the items are not assigned to the same bank already...
+       if (curr0 != curr1 && weight > maxWeight) {
+         if (maxWeight > absMaxWeight) absMaxWeight = maxWeight;
+         maxWeight = weight;
+         max = edge;
+       } // if
+       edge = edge->next;
+      } // while
+      node = node->next;
+    } // while
+    
+    if (maxWeight > 0) {
+#if 1
+      fprintf (stderr, "%s:%d: merging (%4u) %d(%s) and %d(%s)\n", __FUNCTION__, __LINE__, maxWeight,
+              max->src->hash, getSymFromBank (max->src->hash),
+              max->node->hash, getSymFromBank (max->node->hash));
+#endif
+
+      node = getGNode (adj, max->src->data, max->src->hash);
+      node1 = getGNode (adj, max->node->data, max->node->hash);
+
+      if (0 == assignToSameBank (max->src->hash, max->node->hash, 0)) {
+       if (max->src->hash < max->node->hash)
+         mergeGraphNodes (node, node1);
+       else
+         mergeGraphNodes (node1, node);
+      } else {
+       remGEdge (node, node1);
+       remGEdge (node1, node);
+       //mergeMore = 0;
+      }
+
+    } else {
+      mergeMore = 0;
+    }
+  } // while
+#endif
+
+#if 1  
+  // remove redundant BANKSELs
+  //fprintf (stderr, "removing redundant BANKSELs\n");
+  for (pb = the_pFile->pbHead; pb; pb = pb->next) {
+    bankselsRemoved += attachBsrInfo2pBlock (pb, 1);
+  } // for pb
+#endif
+
+#if 0
+  fprintf (stderr, "display graph\n");
+  showGraph ();
+#endif
+
+  deleteGraph (adj);
+  fprintf (stderr, "%s:%s:%d: leaving, %u/%u BANKSELs removed...\n", __FILE__, __FUNCTION__, __LINE__, bankselsRemoved, bankselsTotal);
+}
+
+/*** END of stuff belonging to the BANKSEL optimization ***/