varA = (varB > CONSTANT); Was not compiling correctly.
[fw/sdcc] / src / pic / pcodepeep.c
index ba65ceb5ffd5aa32c74e74f72682b6ef4c907889..81aabf13eca49699879a0b1b2db08da6e67a0e00 100644 (file)
@@ -23,8 +23,7 @@
 
 #include "common.h"   // Include everything in the SDCC src directory
 #include "newalloc.h"
-
-
+//#define PCODE_DEBUG
 #include "pcode.h"
 #include "pcodeflow.h"
 #include "ralloc.h"
@@ -45,6 +44,7 @@ int getpCode(char *mnem,int dest);
 int getpCodePeepCommand(char *cmd);
 void pBlockMergeLabels(pBlock *pb);
 char *pCode2str(char *str, int size, pCode *pc);
+char *get_op( pCodeOp *pcop,char *buf,int buf_size);
 
 extern pCodeInstruction *pic14Mnemonics[];
 
@@ -201,7 +201,8 @@ typedef enum {
   ALT_MNEM1A,
   ALT_MNEM1B,
   ALT_MNEM2,
-  ALT_MNEM2A
+  ALT_MNEM2A,
+  ALT_MNEM3
 } altPatterns;
 
 static char alt_comment[]   = { PCP_COMMENT, 0};
@@ -213,6 +214,7 @@ static char alt_mnem1a[]    = { PCP_STR, PCP_WILDVAR, 0};
 static char alt_mnem1b[]    = { PCP_STR, PCP_NUMBER, 0};
 static char alt_mnem2[]     = { PCP_STR, PCP_STR, PCP_COMMA, PCP_STR, 0};
 static char alt_mnem2a[]    = { PCP_STR, PCP_WILDVAR, PCP_COMMA, PCP_STR, 0};
+static char alt_mnem3[]     = { PCP_STR, PCP_STR, PCP_COMMA, PCP_NUMBER, 0};
 
 static void * cvt_altpat_label(void *pp,pCodeWildBlock *pcwb);
 static void * cvt_altpat_comment(void *pp,pCodeWildBlock *pcwb);
@@ -223,10 +225,12 @@ static void * cvt_altpat_mnem1a(void *pp,pCodeWildBlock *pcwb);
 static void * cvt_altpat_mnem1b(void *pp,pCodeWildBlock *pcwb);
 static void * cvt_altpat_mnem2(void *pp,pCodeWildBlock *pcwb);
 static void * cvt_altpat_mnem2a(void *pp,pCodeWildBlock *pcwb);
+static void * cvt_altpat_mnem3(void *pp,pCodeWildBlock *pcwb);
 
 pcPattern altArr[] = {
   {ALT_LABEL,        alt_label,  cvt_altpat_label},
   {ALT_COMMENT,      alt_comment,cvt_altpat_comment},
+  {ALT_MNEM3,        alt_mnem3,  cvt_altpat_mnem3},
   {ALT_MNEM2A,       alt_mnem2a, cvt_altpat_mnem2a},
   {ALT_MNEM2,        alt_mnem2,  cvt_altpat_mnem2},
   {ALT_MNEM1B,       alt_mnem1b, cvt_altpat_mnem1b},
@@ -603,6 +607,11 @@ static void * cvt_altpat_mnem2a(void *pp,pCodeWildBlock *pcwb)
   pCodeInstruction *pci=NULL;
   pCodeOp *pcosubtype;
 
+  if(!pcwb) {
+    fprintf(stderr,"ERROR %s:%d - can't assemble line\n",__FILE__,__LINE__);
+    return NULL;
+  }
+
   dest = cvt_extract_destination(&p[3]);
 
   DFPRINTF((stderr,"altpat_mnem2a %s var %d destination %s(%d)\n",
@@ -641,6 +650,65 @@ static void * cvt_altpat_mnem2a(void *pp,pCodeWildBlock *pcwb)
 
 }
 
+
+/*-----------------------------------------------------------------*/
+/* cvt_altpat_mem3 -  convert assembly line type to a pCode        */
+/*                    This rule is for bsf/bcf type instructions   */
+/*                                                                 */
+/*                                                                 */
+/*  pp[0] - mnem                                                   */
+/*  pp[1] - register                                               */
+/*  pp[2] - comma                                                  */
+/*  pp[3] - number                                                 */
+/*                                                                 */
+/*-----------------------------------------------------------------*/
+static void * cvt_altpat_mnem3(void *pp,pCodeWildBlock *pcwb)
+{
+  parsedPattern *p = pp;
+  int opcode;
+  int dest;  // or could be bit position in the register
+
+  pCodeInstruction *pci=NULL;
+  pCodeOp *pcosubtype=NULL;
+
+  dest = cvt_extract_destination(&p[3]);
+
+  DFPRINTF((stderr,"altpat_mnem3 %s var %s bit (%d)\n",
+         p->pct[0].tok.s,
+         p[1].pct[0].tok.s,
+         p[3].pct[0].tok.n));
+
+
+  opcode = getpCode(p->pct[0].tok.s,0);
+  if(opcode < 0) {
+    fprintf(stderr, "Bad mnemonic\n");
+    return NULL;
+  }
+
+
+  if(pic14Mnemonics[opcode]->isBitInst) {
+    //pcosubtype = cvt_extract_status(p[1].pct[0].tok.s, p[3].pct[0].tok.s);
+
+    //if(pcosubtype == NULL) {
+    pcosubtype = newpCodeOpBit(p[1].pct[0].tok.s,p[3].pct[0].tok.n,0);
+    //}
+  } else
+    pcosubtype = newpCodeOp(p[1].pct[0].tok.s,PO_GPR_REGISTER);
+
+  if(pcosubtype == NULL) {
+    fprintf(stderr, "Bad operand\n");
+    return NULL;
+  }
+
+  pci = PCI(newpCode(opcode, pcosubtype));
+
+  if(!pci)
+    fprintf(stderr,"couldn't find mnemonic\n");
+
+  return pci;
+
+}
+
 /*-----------------------------------------------------------------*/
 /* tokenizeLineNode - Convert a string (of char's) that was parsed */
 /*                    by SDCCpeeph.c into a string of tokens.      */
@@ -660,13 +728,14 @@ static void * cvt_altpat_mnem2a(void *pp,pCodeWildBlock *pcwb)
 
 static void tokenizeLineNode(char *ln)
 {
-
+  char *lnstart=ln;
   tokIdx = 0;               // Starting off at the beginning
   tokArr[0].tt = PCT_NULL;  // and assume invalid character for first token.
 
   if(!ln || !*ln)
     return;
 
+
   while(*ln) {
 
     if(isspace(*ln)) {
@@ -723,6 +792,11 @@ static void tokenizeLineNode(char *ln)
        tokArr[tokIdx].tok.s = Safe_strdup(buffer);
        tokArr[tokIdx++].tt = PCT_STRING;
 
+      } else {
+       fprintf(stderr, "Error while parsing peep rules (check peeph.def)\n");
+       fprintf(stderr, "Line: %s\n",lnstart);
+       fprintf(stderr, "Token: '%c'\n",*ln);
+       exit(1);
       }
     }
 
@@ -747,7 +821,7 @@ static void tokenizeLineNode(char *ln)
 
 void dump1Token(pCodeTokens tt)
 {
-#ifdef PCODE_DEBUG
+
   switch(tt) {
   case PCT_SPACE:
     fprintf(stderr, " space ");
@@ -783,7 +857,7 @@ void dump1Token(pCodeTokens tt)
     fprintf(stderr, " null ");
 
   }
-#endif
+
 }
 
 
@@ -887,19 +961,23 @@ int advTokIdx(int *v, int amt)
 /* pcode.                                                          */
 /*-----------------------------------------------------------------*/
 
-void parseTokens(pCodeWildBlock *pcwb)
+int parseTokens(pCodeWildBlock *pcwb, pCode **pcret)
 {
-  unsigned i;
   pCode *pc;
+  int error = 0;
 
   if(!tokIdx)
-    return;
+    return error;
 
-  for(i=0; i<=tokIdx; i++)
-    dump1Token(tokArr[i].tt);
 #ifdef PCODE_DEBUG
-  fputc('\n',stderr);
+  {
+    unsigned i;
+    for(i=0; i<=tokIdx; i++)
+      dump1Token(tokArr[i].tt);
+    fputc('\n',stderr);
+  }
 #endif
+
   {
     int lparsedPatIdx=0;
     int lpcpIdx;
@@ -1028,6 +1106,8 @@ void parseTokens(pCodeWildBlock *pcwb)
              state = PS_HAVE_COMMA;
            } else
              fprintf(stderr,"  unexpected comma\n");
+           break;
+
          }
 
          matching = 1;
@@ -1066,7 +1146,14 @@ void parseTokens(pCodeWildBlock *pcwb)
 
          //if(curBlock && pc)
          //addpCode2pBlock(curBlock, pc);
-         addpCode2pBlock(pcwb->pb, pc);
+         if(pc) {
+           if (pcret) {
+             *pcret = pc;
+             return 0;       // Only accept one line for now.
+           } else
+             addpCode2pBlock(pcwb->pb, pc);
+         } else
+           error++;
        }
        j += c;
       }
@@ -1090,7 +1177,7 @@ void parseTokens(pCodeWildBlock *pcwb)
 
   }
 
-
+  return error;
 }
 
 /*-----------------------------------------------------------------*/
@@ -1107,9 +1194,38 @@ void  peepRuleBlock2pCodeBlock(  lineNode *ln, pCodeWildBlock *pcwb)
     //DFPRINTF((stderr,"%s\n",ln->line));
 
     tokenizeLineNode(ln->line);
-    parseTokens(pcwb);
+    
+    if(parseTokens(pcwb,NULL)) {
+      int i;
+      fprintf(stderr,"ERROR assembling line:\n%s\n",ln->line);
+      fprintf(stderr,"Tokens:\n");
+      for(i=0; i<5; i++)
+       dump1Token(tokArr[i].tt);
+      fputc('\n',stderr);
+      exit (1);
+    }
+  }
+}
 
+/*-----------------------------------------------------------------*/
+/*                                                                 */
+/*-----------------------------------------------------------------*/
+pCode *AssembleLine(char *line)
+{
+  pCode *pc=NULL;
+
+  if(!line || !*line) {
+    fprintf(stderr,"WARNING returning NULL in AssembleLine\n");
+    return NULL;
   }
+
+  tokenizeLineNode(line);
+    
+  if(parseTokens(NULL,&pc))
+    fprintf(stderr, "WARNING: unable to assemble line:\n%s\n",line);
+
+  return pc;
+
 }
 
 /*-----------------------------------------------------------------*/
@@ -1156,13 +1272,13 @@ void postinit_pCodeWildBlock(pCodeWildBlock *pcwb)
   if(!pcwb)
     return;
 
-  pcwb->nvars++;
+  pcwb->nvars+=2;
   pcwb->nops = pcwb->nvars;
 
   pcwb->vars = Safe_calloc(pcwb->nvars, sizeof(char *));
   pcwb->wildpCodeOps = Safe_calloc(pcwb->nvars, sizeof(pCodeOp *));
 
-  pcwb->nwildpCodes++;
+  pcwb->nwildpCodes+=2;
   pcwb->wildpCodes = Safe_calloc(pcwb->nwildpCodes, sizeof(pCode *));
 
 }
@@ -1220,19 +1336,6 @@ void  peepRules2pCode(peepRule *rules)
     pcps = Safe_calloc(1,sizeof(pCodePeepSnippets));
     peepSnippets = DLL_append((_DLL*)peepSnippets,(_DLL*)pcps);
 
-/*
-    curPeep = pcps->peep  = Safe_calloc(1,sizeof(pCodePeep));
-
-    curPeep->vars = NULL; 
-    curPeep->wildpCodes = NULL; curPeep->wildpCodeOps = NULL;
-    curPeep->postFalseCond = PCC_NONE;
-    curPeep->postTrueCond  = PCC_NONE;
-
-
-    curPeep->target = curBlock = newpCodeChain(NULL, 'W', NULL);
-    sMaxWildVar  = 0;
-    sMaxWildMnem = 0;
-*/
     currentRule = pcps->peep  = Safe_calloc(1,sizeof(pCodePeep));
     initpCodePeep(currentRule);
 
@@ -1240,7 +1343,7 @@ void  peepRules2pCode(peepRule *rules)
     peepRuleBlock2pCodeBlock(pr->match, &currentRule->target);
 
     //DFPRINTF((stderr,"finished target, here it is in pcode form:\n"));
-    //printpBlock(stderr, curBlock);
+    //printpBlock(stderr, currentRule->target.pb);
 
     //DFPRINTF((stderr,"target with labels merged:\n"));
     //pBlockMergeLabels(curBlock);
@@ -1253,8 +1356,6 @@ void  peepRules2pCode(peepRule *rules)
     //DFPRINTF((stderr,"\nReplaced by:\n"));
 
 
-    //curPeep->replace = curBlock = newpCodeChain(NULL, 'W', NULL);
-
     /* Convert the replace block */
     peepRuleBlock2pCodeBlock(pr->replace, &currentRule->replace);
 
@@ -1262,7 +1363,7 @@ void  peepRules2pCode(peepRule *rules)
     //printpBlock(stderr, curBlock);
 
     //DFPRINTF((stderr,"replace with labels merged:\n"));
-    //pBlockMergeLabels(curBlock);
+
     pBlockMergeLabels(currentRule->replace.pb);
     //printpBlock(stderr, currentRule->replace.pb);
 
@@ -1271,18 +1372,8 @@ void  peepRules2pCode(peepRule *rules)
     /* The rule has been converted to pCode. Now allocate
      * space for the wildcards */
     
-/*
-     ++sMaxWildVar;
-    curPeep->nvars = sMaxWildVar;
-    curPeep->vars = Safe_calloc(sMaxWildVar, sizeof(char *));
-
-    curPeep->nops = sMaxWildVar;
-    curPeep->wildpCodeOps = Safe_calloc(sMaxWildVar, sizeof(pCodeOp *));
-
-    curPeep->nwildpCodes = ++sMaxWildMnem;
-    curPeep->wildpCodes = Safe_calloc(sMaxWildMnem, sizeof(char *));
-*/
     postinit_pCodeWildBlock(&currentRule->target);
+    postinit_pCodeWildBlock(&currentRule->replace);
 
     //return; // debug ... don't want to go through all the rules yet
   }
@@ -1381,7 +1472,7 @@ static void * DLL_append(_DLL *list, _DLL *next)
 /*-----------------------------------------------------------------*/
 int pCodeSearchCondition(pCode *pc, unsigned int cond)
 {
-
+  //fprintf(stderr,"Checking conditions %d\n",cond);
   while(pc) {
 
     /* If we reach a function end (presumably an end since we most
@@ -1391,6 +1482,9 @@ int pCodeSearchCondition(pCode *pc, unsigned int cond)
       return 0;
 
     if(pc->type == PC_OPCODE) {
+      //fprintf(stderr," checking conditions of: ");
+      //pc->print(stderr,pc);
+      //fprintf(stderr,"\t\tinCond=%d\toutCond=%d\n",PCI(pc)->inCond,PCI(pc)->outCond);
       if(PCI(pc)->inCond & cond)
        return 1;
       if(PCI(pc)->outCond & cond)
@@ -1403,6 +1497,64 @@ int pCodeSearchCondition(pCode *pc, unsigned int cond)
   return 0;
 }
 
+/*-----------------------------------------------------------------
+ * int pCodeOpCompare(pCodeOp *pcops, pCodeOp *pcopd)
+ *
+ * Compare two pCodeOp's and return 1 if they're the same
+ *-----------------------------------------------------------------*/
+int pCodeOpCompare(pCodeOp *pcops, pCodeOp *pcopd)
+{
+  char b[50], *n2;
+
+  if(!pcops || !pcopd)
+    return 0;
+/*
+  fprintf(stderr," Comparing operands %s",
+         get_op( pcops,NULL,0));
+
+  fprintf(stderr," to %s\n",
+         get_op( pcopd,NULL,0));
+*/
+
+  if(pcops->type != pcopd->type) {
+    //fprintf(stderr,"  - fail - diff types\n");
+    return 0;  // different types
+  }
+
+  if(pcops->type == PO_LITERAL) {
+
+    if((PCOL(pcops)->lit >= 0) && (PCOL(pcops)->lit == PCOL(pcopd)->lit))
+      return 1;
+
+    return 0;
+  }
+
+  b[0]=0;
+  get_op(pcops,b,50);
+
+  n2 = get_op(pcopd,NULL,0);
+
+  if( !n2 || strcmp(b,n2)) {
+    //fprintf(stderr,"  - fail - diff names: %s, len=%d,  %s, len=%d\n",b,strlen(b), n2, strlen(n2) );
+    return 0;  // different names
+  }
+
+  switch(pcops->type) {
+  case PO_DIR:
+    if( PCOR(pcops)->instance != PCOR(pcopd)->instance) {
+      //fprintf(stderr, "  - fail different instances\n");
+      return 0;
+    }
+    break;
+  default:
+    break;
+  }
+
+  //fprintf(stderr,"  - pass\n");
+
+  return 1;
+}
+
 int pCodePeepMatchLabels(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
 {
   int labindex;
@@ -1410,31 +1562,49 @@ int pCodePeepMatchLabels(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
   /* Check for a label associated with this wild pCode */
   // If the wild card has a label, make sure the source code does too.
   if(PCI(pcd)->label) {
-    pCode *pcl;
+    pCode *pcl = PCI(pcd)->label->pc;
+
+#ifdef PCODE_DEBUG
+    int li = -PCL(pcl)->key;
+
+    if(peepBlock->target.vars[li] == NULL) {
+      if(PCI(pcs)->label) {
+       DFPRINTF((stderr,"first time for a label: %d %s\n",li,PCL(PCI(pcs)->label->pc)->label));
+      }
+    } else {
+      // DFPRINTF((stderr,"label id = %d \n",PCL(PCI(pcd)->label->pc)->key));
+      DFPRINTF((stderr," label id: %d %s\n",li,peepBlock->target.vars[li]));
+      if(PCI(pcs)->label) {
+       DFPRINTF((stderr," src %s\n",PCL(PCI(pcs)->label->pc)->label));
+      }
+    }
+#endif
+
 
     if(!PCI(pcs)->label)
       return 0;
 
-    pcl = PCI(pcd)->label->pc;
-
     labindex = -PCL(pcl)->key;
-    //DFPRINTF((stderr,"label id = %d (labindex = %d)\n",PCL(pcl)->key,labindex));
     if(peepBlock->target.vars[labindex] == NULL) {
       // First time to encounter this label
       peepBlock->target.vars[labindex] = PCL(PCI(pcs)->label->pc)->label;
-      //DFPRINTF((stderr,"first time for a label: %d %s\n",labindex, peepBlock->vars[labindex]));
+      DFPRINTF((stderr,"first time for a label: %d %s\n",labindex,PCL(PCI(pcs)->label->pc)->label));
+
     } else {
       if(strcmp(peepBlock->target.vars[labindex],PCL(PCI(pcs)->label->pc)->label) != 0) {
-       // DFPRINTF((stderr,"labels don't match\n"));
+       DFPRINTF((stderr,"labels don't match dest %s != src %s\n",peepBlock->target.vars[labindex],PCL(PCI(pcs)->label->pc)->label));
        return 0;
       }
-      //DFPRINTF((stderr,"matched a label\n"));
+      DFPRINTF((stderr,"matched a label %d %s -hey\n",labindex,peepBlock->target.vars[labindex]));
     }
   } else {
-    // DFPRINTF((stderr,"destination doesn't have a label\n"));
+    //DFPRINTF((stderr,"destination doesn't have a label\n"));
 
     if(PCI(pcs)->label)
       return 0;
+
+    //DFPRINTF((stderr,"neither src nor dest have labels\n"));
+
   }
 
   return 1;
@@ -1475,6 +1645,12 @@ int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
   /* one-for-one match. Here the source and destination opcodes 
    * are not wild. However, there may be a label or a wild operand */
 
+  if(pcs) {
+    if(PCI(pcs)->label) {
+      DFPRINTF((stderr,"Match line source label: %s\n",PCL(PCI(pcs)->label->pc)->label));
+    }
+  }
+
   if(pcs->type == pcd->type) {
 
     if(pcs->type == PC_OPCODE) {
@@ -1483,11 +1659,11 @@ int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
       if(PCI(pcs)->op != PCI(pcd)->op)
        return 0;
 
-      /*
+#ifdef PCODE_DEBUG
       DFPRINTF((stderr,"%s comparing\n",__FUNCTION__));
       pcs->print(stderr,pcs);
       pcd->print(stderr,pcd);
-      */
+#endif
 
       if(!pCodePeepMatchLabels(peepBlock, pcs, pcd))
        return 0;
@@ -1496,7 +1672,6 @@ int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
       if(PCI(pcd)->pcop) {
        if (PCI(pcd)->pcop->type == PO_WILD) {
          index = PCOW(PCI(pcd)->pcop)->id;
-
          //DFPRINTF((stderr,"destination is wild\n"));
 #ifdef DEBUG_PCODEPEEP
          if (index > peepBlock->nops) {
@@ -1504,13 +1679,26 @@ int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
            exit(1);
          }
 #endif
+
          PCOW(PCI(pcd)->pcop)->matched = PCI(pcs)->pcop;
          if(!peepBlock->target.wildpCodeOps[index]) {
            peepBlock->target.wildpCodeOps[index] = PCI(pcs)->pcop;
 
            //if(PCI(pcs)->pcop->type == PO_GPR_TEMP) 
 
+         } else {
+           /*
+             pcs->print(stderr,pcs);
+             pcd->print(stderr,pcd);
+
+             fprintf(stderr, "comparing operands of these instructions, result %d\n",
+             pCodeOpCompare(PCI(pcs)->pcop, peepBlock->target.wildpCodeOps[index])
+             );
+           */
+
+           return pCodeOpCompare(PCI(pcs)->pcop, peepBlock->target.wildpCodeOps[index]);
          }
+
          {
            char *n;
 
@@ -1528,11 +1716,15 @@ int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
            if(peepBlock->target.vars[index])
              return  (strcmp(peepBlock->target.vars[index],n) == 0);
            else {
-             // DFPRINTF((stderr,"first time for a variable: %d, %s\n",index,n));
+             DFPRINTF((stderr,"first time for a variable: %d, %s\n",index,n));
              peepBlock->target.vars[index] = n;
              return 1;
            }
          }
+
+       } else if (PCI(pcd)->pcop->type == PO_LITERAL) {
+         return pCodeOpCompare(PCI(pcs)->pcop, PCI(pcd)->pcop);
+
        }
        /* FIXME - need an else to check the case when the destination 
         * isn't a wild card */
@@ -1546,21 +1738,22 @@ int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
 
   if((pcd->type == PC_WILD) && (pcs->type == PC_OPCODE)) {
 
-
     index = PCW(pcd)->id;
-
-    //    DFPRINTF((stderr,"%s comparing wild cards\n",__FUNCTION__));
-    //pcs->print(stderr,pcs);
-    //pcd->print(stderr,pcd);
-
+#ifdef PCODE_DEBUG
+    DFPRINTF((stderr,"%s comparing wild cards\n",__FUNCTION__));
+    pcs->print(stderr,pcs);
+    pcd->print(stderr,pcd);
+#endif
     peepBlock->target.wildpCodes[PCW(pcd)->id] = pcs;
 
-    if(!pCodePeepMatchLabels(peepBlock, pcs, pcd))
+    if(!pCodePeepMatchLabels(peepBlock, pcs, pcd)) {
+      DFPRINTF((stderr," Failing because labels don't match\n"));
       return 0;
+    }
 
     if(PCW(pcd)->mustBeBitSkipInst & !(PCI(pcs)->isBitInst && PCI(pcs)->isSkip)) {
       // doesn't match because the wild pcode must be a bit skip
-      //fprintf(stderr," Failing match because bit skip is req:\n");
+      DFPRINTF((stderr," Failing match because bit skip is req\n"));
       //pcd->print(stderr,pcd);
       //pcs->print(stderr,pcs);
       return 0;
@@ -1568,7 +1761,7 @@ int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
 
     if(PCW(pcd)->mustNotBeBitSkipInst & (PCI(pcs)->isBitInst && PCI(pcs)->isSkip)) {
       // doesn't match because the wild pcode must *not* be a bit skip
-      //fprintf(stderr," Failing match because don't want skip :\n");
+      DFPRINTF((stderr," Failing match because shouldn't be bit skip\n"));
       //pcd->print(stderr,pcd);
       //pcs->print(stderr,pcs);
       return 0;
@@ -1578,18 +1771,20 @@ int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
       PCOW(PCI(pcd)->pcop)->matched = PCI(pcs)->pcop;
       if(peepBlock->target.vars[index]) {
        int i = (strcmp(peepBlock->target.vars[index],PCI(pcs)->pcop->name) == 0);
-       /*
+#ifdef PCODE_DEBUG
+
        if(i)
          DFPRINTF((stderr," (matched)\n"));
        else {
          DFPRINTF((stderr," (no match: wild card operand mismatch\n"));
-         DFPRINTF((stderr,"  peepblock= %s,  pcodeop= %s\n"),
-                 peepBlock->vars[index],
-                 PCI(pcs)->pcop->name);
+         DFPRINTF((stderr,"  peepblock= %s,  pcodeop= %s\n",
+                 peepBlock->target.vars[index],
+                 PCI(pcs)->pcop->name));
        }
-       */
+#endif
        return i;
       } else {
+       DFPRINTF((stderr," (matched %s\n",PCI(pcs)->pcop->name));
        peepBlock->target.vars[index] = PCI(pcs)->pcop->name;
        return 1;
       }
@@ -1618,11 +1813,28 @@ void pCodePeepClrVars(pCodePeep *pcp)
   int i;
   if(!pcp)
     return;
-
-  for(i=0;i<pcp->target.nvars; i++) {
+/*
+  DFPRINTF((stderr," Clearing peep rule vars\n"));
+  DFPRINTF((stderr," %d %d %d  %d %d %d\n",
+           pcp->target.nvars,pcp->target.nops,pcp->target.nwildpCodes,
+           pcp->replace.nvars,pcp->replace.nops,pcp->replace.nwildpCodes));
+*/
+  for(i=0;i<pcp->target.nvars; i++)
     pcp->target.vars[i] = NULL;
+  for(i=0;i<pcp->target.nops; i++)
     pcp->target.wildpCodeOps[i] = NULL;
-  }
+  for(i=0;i<pcp->target.nwildpCodes; i++)
+    pcp->target.wildpCodes[i] = NULL;
+
+  for(i=0;i<pcp->replace.nvars; i++)
+    pcp->replace.vars[i] = NULL;
+  for(i=0;i<pcp->replace.nops; i++)
+    pcp->replace.wildpCodeOps[i] = NULL;
+  for(i=0;i<pcp->replace.nwildpCodes; i++)
+    pcp->replace.wildpCodes[i] = NULL;
+
+
+
 }
 
 /*-----------------------------------------------------------------*/
@@ -1717,12 +1929,16 @@ pCodeOp *pCodeOpCopy(pCodeOp *pcop)
     pcopnew = Safe_calloc(1,sizeof(pCodeOpReg) );
     PCOR(pcopnew)->r = PCOR(pcop)->r;
     PCOR(pcopnew)->rIdx = PCOR(pcop)->rIdx;
+    PCOR(pcopnew)->instance = PCOR(pcop)->instance;
     DFPRINTF((stderr," register index %d\n", PCOR(pcop)->r->rIdx));
     break;
 
   case PO_DIR:
-    fprintf(stderr,"pCodeOpCopy PO_DIR\n");
+    //fprintf(stderr,"pCodeOpCopy PO_DIR\n");
     pcopnew = Safe_calloc(1,sizeof(pCodeOpReg) );
+    PCOR(pcopnew)->r = PCOR(pcop)->r;
+    PCOR(pcopnew)->rIdx = PCOR(pcop)->rIdx;
+    PCOR(pcopnew)->instance = PCOR(pcop)->instance;
     break;
   case PO_STATUS:
     DFPRINTF((stderr,"pCodeOpCopy PO_STATUS\n"));
@@ -1780,7 +1996,7 @@ void pCodeDeleteChain(pCode *f,pCode *t)
   while(f && f!=t) {
     DFPRINTF((stderr,"delete pCode:\n"));
     pc = f->next;
-    f->print(stderr,f);
+    //f->print(stderr,f);
     //f->delete(f);  this dumps core...
 
     f = pc;
@@ -1794,6 +2010,7 @@ int pCodePeepMatchRule(pCode *pc)
 {
   pCodePeep *peepBlock;
   pCode *pct, *pcin;
+  pCodeCSource *pc_cline=NULL;
   _DLL *peeprules;
   int matched;
 
@@ -1808,12 +2025,20 @@ int pCodePeepMatchRule(pCode *pc)
     }
 
     pCodePeepClrVars(peepBlock);
-
+/*
     pcin = pc;
     if(IS_PCCOMMENT(pcin))
       pc = pcin = findNextInstruction(pcin->next);
+*/
+    pcin = pc = findNextInstruction(pc);
 
     pct = peepBlock->target.pb->pcHead;
+#ifdef PCODE_DEBUG
+    {
+      pCode *pcr = peepBlock->replace.pb->pcHead;
+      if(pcr) pct->print(stderr,pcr);
+    }
+#endif
     matched = 0;
     while(pct && pcin) {
 
@@ -1824,13 +2049,18 @@ int pCodePeepMatchRule(pCode *pc)
       pct = pct->next;
       //debug:
       //DFPRINTF((stderr,"    matched\n"));
-      if(!pcin)
-       DFPRINTF((stderr," end of code\n"));
-      if(!pct)
+
+      if(!pcin && pct) {
+       DFPRINTF((stderr," partial match... no more code\n"));
+       fprintf(stderr," partial match... no more code\n");
+       matched = 0; 
+      }
+      if(!pct) {
        DFPRINTF((stderr," end of rule\n"));
+      }
     }
 
-    if(matched) {
+    if(matched && pcin) {
 
       /* So far we matched the rule up to the point of the conditions .
        * In other words, all of the opcodes match. Now we need to see
@@ -1841,9 +2071,16 @@ int pCodePeepMatchRule(pCode *pc)
        * the `postFalseCond' as input then we abort the match
        */
       DFPRINTF((stderr,"    matched rule so far, now checking conditions\n"));
+      //pcin->print(stderr,pcin);
+      
       if (pcin && peepBlock->postFalseCond && 
          (pCodeSearchCondition(pcin,peepBlock->postFalseCond) > 0) )
        matched = 0;
+
+      //fprintf(stderr," condition results = %d\n",pCodeSearchCondition(pcin,peepBlock->postFalseCond));
+
+
+      //if(!matched) fprintf(stderr,"failed on conditions\n");
     }
 
     if(matched) {
@@ -1859,11 +2096,12 @@ int pCodePeepMatchRule(pCode *pc)
       printpCodeString(stderr,peepBlock->target.pb->pcHead,10);
       DFPRINTF((stderr,"first thing matched\n"));
       pc->print(stderr,pc);
-#endif
       if(pcin) {
        DFPRINTF((stderr,"last thing matched\n"));
        pcin->print(stderr,pcin);
       }
+#endif
+
 
       /* Unlink the original code */
       pcprev = pc->prev;
@@ -1871,22 +2109,40 @@ int pCodePeepMatchRule(pCode *pc)
       if(pcin) 
        pcin->prev = pc->prev;
 
+
+#if 0
       {
        /*     DEBUG    */
        /* Converted the deleted pCodes into comments */
 
        char buf[256];
+       pCodeCSource *pc_cline2=NULL;
 
        buf[0] = ';';
        buf[1] = '#';
 
        while(pc &&  pc!=pcin) {
+
+         if(pc->type == PC_OPCODE && PCI(pc)->cline) {
+           if(pc_cline) {
+             pc_cline2->pc.next = PCODE(PCI(pc)->cline);
+             pc_cline2 = PCCS(pc_cline2->pc.next);
+           } else {
+             pc_cline = pc_cline2 = PCI(pc)->cline;
+             pc_cline->pc.seq = pc->seq;
+           }
+         }
+
          pCode2str(&buf[2], 254, pc);
          pCodeInsertAfter(pcprev, newpCodeCharP(buf));
          pcprev = pcprev->next;
          pc = pc->next;
+
        }
+       if(pc_cline2)
+         pc_cline2->pc.next = NULL;
       }
+#endif
 
       if(pcin)
        pCodeDeleteChain(pc,pcin);
@@ -1896,6 +2152,7 @@ int pCodePeepMatchRule(pCode *pc)
       pcr = peepBlock->replace.pb->pcHead;  // This is the replacement code
       while (pcr) {
        pCodeOp *pcop=NULL;
+       
        /* If the replace pcode is an instruction with an operand, */
        /* then duplicate the operand (and expand wild cards in the process). */
        if(pcr->type == PC_OPCODE) {
@@ -1926,16 +2183,30 @@ int pCodePeepMatchRule(pCode *pc)
 
 
        pc = pc->next;
-       //if(pc)
-       //  pc->print(stderr,pc);
+#ifdef PCODE_DEBUG
+       DFPRINTF((stderr,"  NEW Code:"));
+       if(pc) pc->print(stderr,pc);
+#endif
        pcr = pcr->next;
       }
 
+      /* We have just replaced the inefficient code with the rule.
+       * Now, we need to re-add the C-source symbols if there are any */
+      pc = pcprev;
+      while(pc_cline ) {
+       
+       pc =  findNextInstruction(pc->next);
+       PCI(pc)->cline = pc_cline;
+       pc_cline = PCCS(pc_cline->pc.next);
+       
+      }
+
       return 1;
     }
   next_rule:
     peeprules = peeprules->next;
   }
+  DFPRINTF((stderr," no rule matched\n"));
 
   return 0;
 }