Added more regression test to the pic port. Added support for <<, >>, ^
[fw/sdcc] / src / pic / pcodepeep.c
index c51bcb42021145988b9d0852f1bd5e23547ee8eb..e23e667c712b5bc5cc9a53aeb1d25f57ae7f7056 100644 (file)
@@ -443,7 +443,7 @@ static void * cvt_altpat_mnem1a(void *pp)
   }
 
   if(pic14Mnemonics[opcode]->bit_inst)
-    pcosubtype = newpCodeOpBit(NULL,-1);
+    pcosubtype = newpCodeOpBit(NULL,-1,0);
   else
     pcosubtype = newpCodeOp(NULL,PO_GPR_REGISTER);
 
@@ -1367,7 +1367,7 @@ void pCodePeepInit(void)
     pCodeOp *pcwb;
 
     // Create a new wild operand subtyped as a bit
-    pcwb =  newpCodeOpWild(0,pcp,newpCodeOpBit(NULL,-1));
+    pcwb =  newpCodeOpWild(0,pcp,newpCodeOpBit(NULL,-1,0));
 
     // Create a 
     pb = newpCodeChain(NULL, 'W',newpCode(POC_BTFSC,pcwb));
@@ -1477,7 +1477,30 @@ int pCodeSearchCondition(pCode *pc, unsigned int cond)
 /*-----------------------------------------------------------------*/
 /* pCodePeepMatchLine - Compare source and destination pCodes to   */
 /*                      see they're the same.                      */
+/*                                                                 */
+/* In this context, "source" refers to the coded generated by gen.c*/
+/* and "destination" refers to a pcode in a peep rule. If the dest-*/
+/* ination has no wild cards, then MatchLine will compare the two  */
+/* pcodes (src and dest) for a one-to-one match. If the destination*/
+/* has wildcards, then those get expanded. When a wild card is     */
+/* encountered for the first time it autmatically is considered a  */
+/* match and the object that matches it is referenced in the       */
+/* variables or opcodes array (depending on the type of match).    */
+/*                                                                 */
+/*                                                                 */
+/* Inputs:                                                         */
+/*  *peepBlock - A pointer to the peepBlock that contains the      */
+/*               entire rule to which the destination pcode belongs*/
+/*  *pcs - a pointer to the source pcode                           */
+/*  *pcd - a pointer to the destination pcode                      */
+/*                                                                 */
+/* Returns:                                                        */
+/*  1 - pcodes match                                               */
+/*  0 - pcodes don't match                                         */
+/*                                                                 */
+/*                                                                 */
 /*-----------------------------------------------------------------*/
+
 int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
 {
   int index;   // index into wild card arrays
@@ -1524,11 +1547,14 @@ int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
            if(peepBlock->vars[index])
              return  (strcmp(peepBlock->vars[index],n) == 0);
            else {
+             fprintf(stderr,"first time for a variable: %d, %s\n",index,n);
              peepBlock->vars[index] = n; //PCI(pcs)->pcop->name;
              return 1;
            }
          }
        }
+       /* FIXME - need an else to check the case when the destination 
+        * isn't a wild card */
       } else
        /* The pcd has no operand. Lines match if pcs has no operand either*/
        return (PCI(pcs)->pcop == NULL);
@@ -1557,12 +1583,13 @@ int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
        return 0;
 
       pcl = pcd->label->pc;
-
-      labindex = PCOW(pcl)->id;
+      //labindex = PCOW(pcl)->id;
+      labindex = -PCL(pcl)->key;
+      fprintf(stderr,"label id = %d (labindex = %d)\n",PCL(pcl)->key,labindex);
       if(peepBlock->vars[labindex] == NULL) {
        // First time to encounter this label
        peepBlock->vars[labindex] = PCL(pcs->label->pc)->label;
-       fprintf(stderr,"first time for a label\n");
+       fprintf(stderr,"first time for a label: %d %s\n",labindex, peepBlock->vars[labindex]);
       } else {
        if(strcmp(peepBlock->vars[labindex],PCL(pcs->label->pc)->label) != 0) {
          fprintf(stderr,"labels don't match\n");
@@ -1596,6 +1623,9 @@ int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
     if(pcs) {
       fprintf(stderr," (next to match)\n");
       pcs->print(stderr,pcs);
+    } else if(pcd->next) {
+      /* oops, we ran out of code, but there's more to the rule */
+      return 0;
     }
 
     return 1; /*  wild card matches */
@@ -1690,6 +1720,7 @@ static pCodeOp *pCodeOpCopy(pCodeOp *pcop)
 
   case PO_GPR_REGISTER:
   case PO_GPR_TEMP:
+  case PO_GPR_BIT:
     fprintf(stderr,"pCodeOpCopy GPR register\n");
     pcopnew = Safe_calloc(1,sizeof(pCodeOpReg) );
     PCOR(pcopnew)->r = PCOR(pcop)->r;
@@ -1706,6 +1737,8 @@ static pCodeOp *pCodeOpCopy(pCodeOp *pcop)
   case PO_STATUS:
   case PO_FSR:
   case PO_INDF:
+  case PO_PCL:
+  case PO_PCLATH:
 
     fprintf(stderr,"pCodeOpCopy register type %d\n", pcop->type);
     pcopnew = Safe_calloc(1,sizeof(pCodeOp) );
@@ -1784,7 +1817,7 @@ int pCodePeepMatchRule(pCode *pc)
        fprintf(stderr," end of rule\n");
     }
 
-    if(matched && pcin) {
+    if(matched) {
 
       /* 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
@@ -1795,12 +1828,12 @@ int pCodePeepMatchRule(pCode *pc)
        * the `postFalseCond' as input then we abort the match
        */
       fprintf(stderr,"    matched rule so far, now checking conditions\n");
-      if (peepBlock->postFalseCond && 
+      if (pcin && peepBlock->postFalseCond && 
          (pCodeSearchCondition(pcin,peepBlock->postFalseCond) > 0) )
        matched = 0;
     }
 
-    if(matched && pcin) {
+    if(matched) {
 
       pCode *pcprev;
       pCode *pcr;
@@ -1813,13 +1846,16 @@ int pCodePeepMatchRule(pCode *pc)
       printpCodeString(stderr,peepBlock->target->pcHead,10);
       fprintf(stderr,"first thing matched\n");
       pc->print(stderr,pc);
-      fprintf(stderr,"last thing matched\n");
-      pcin->print(stderr,pcin);
+      if(pcin) {
+       fprintf(stderr,"last thing matched\n");
+       pcin->print(stderr,pcin);
+      }
 
       /* Unlink the original code */
       pcprev = pc->prev;
       pcprev->next = pcin;
-      pcin->prev = pc->prev;
+      if(pcin) 
+       pcin->prev = pc->prev;
 
       {
        /*     DEBUG    */
@@ -1838,7 +1874,8 @@ int pCodePeepMatchRule(pCode *pc)
        }
       }
 
-      pCodeDeleteChain(pc,pcin);
+      if(pcin)
+       pCodeDeleteChain(pc,pcin);
 
       /* Generate the replacement code */
       pc = pcprev;
@@ -1871,7 +1908,8 @@ int pCodePeepMatchRule(pCode *pc)
 
 
        pc = pc->next;
-       pc->print(stderr,pc);
+       if(pc)
+         pc->print(stderr,pc);
        pcr = pcr->next;
       }