Yet again, signed comparisons to literals has been fixed.
[fw/sdcc] / src / pic / pcodepeep.c
index 3a8d52654825b78bde9cf56ea9c0d87bf890074c..115852a54fc6ee11222c496789e130829b85abdc 100644 (file)
@@ -996,8 +996,8 @@ void parseTokens(void)
 
        if( altArr[k].f) {
          pc = altArr[k].f(&parsedPatArr[j]);
-         if(pc && pc->print)
-           pc->print(stderr,pc);
+         //if(pc && pc->print)
+         //  pc->print(stderr,pc);
          //if(pc && pc->destruct) pc->destruct(pc); dumps core?
          if(curBlock && pc)
            addpCode2pBlock(curBlock, pc);
@@ -1038,7 +1038,7 @@ void  peepRuleBlock2pCodeBlock(  lineNode *ln)
 
   for( ; ln; ln = ln->next) {
 
-    fprintf(stderr,"%s\n",ln->line);
+    //fprintf(stderr,"%s\n",ln->line);
 
     tokenizeLineNode(ln->line);
     parseTokens();
@@ -1054,12 +1054,12 @@ static void   peepRuleCondition(char *cond)
   if(!cond)
     return;
 
-  fprintf(stderr,"\nCondition:  %s\n",cond);
+  //fprintf(stderr,"\nCondition:  %s\n",cond);
 
   /* brute force compares for now */
 
   if(STRCASECMP(cond, "NZ") == 0) {
-    fprintf(stderr,"found NZ\n");
+    //fprintf(stderr,"found NZ\n");
     curPeep->postFalseCond = PCC_Z;
 
   }
@@ -1093,7 +1093,7 @@ void  peepRules2pCode(peepRule *rules)
 
   for (pr = rules; pr; pr = pr->next) {
 
-    fprintf(stderr,"\nRule:\n\n");
+    //fprintf(stderr,"\nRule:\n\n");
 
     pcps = Safe_calloc(1,sizeof(pCodePeepSnippets));
     curPeep = pcps->peep  = Safe_calloc(1,sizeof(pCodePeep));
@@ -1112,14 +1112,14 @@ void  peepRules2pCode(peepRule *rules)
     /* Convert the target block */
     peepRuleBlock2pCodeBlock(pr->match);
 
-    fprintf(stderr,"finished target, here it is in pcode form:\n");
-    printpBlock(stderr, curBlock);
+    //fprintf(stderr,"finished target, here it is in pcode form:\n");
+    //printpBlock(stderr, curBlock);
 
-    fprintf(stderr,"target with labels merged:\n");
+    //fprintf(stderr,"target with labels merged:\n");
     pBlockMergeLabels(curBlock);
     printpBlock(stderr, curBlock);
 
-    fprintf(stderr,"\nReplaced by:\n");
+    //fprintf(stderr,"\nReplaced by:\n");
 
 
     curPeep->replace = curBlock = newpCodeChain(NULL, 'W', NULL);
@@ -1127,12 +1127,12 @@ void  peepRules2pCode(peepRule *rules)
     /* Convert the replace block */
     peepRuleBlock2pCodeBlock(pr->replace);
 
-    fprintf(stderr,"finished replace block, here it is in pcode form:\n");
-    printpBlock(stderr, curBlock);
+    //fprintf(stderr,"finished replace block, here it is in pcode form:\n");
+    //printpBlock(stderr, curBlock);
 
-    fprintf(stderr,"replace with labels merged:\n");
+    //fprintf(stderr,"replace with labels merged:\n");
     pBlockMergeLabels(curBlock);
-    printpBlock(stderr, curBlock);
+    //printpBlock(stderr, curBlock);
 
     peepRuleCondition(pr->cond);
 
@@ -1219,227 +1219,7 @@ static void * DLL_append(_DLL *list, _DLL *next)
 
   -----------------------------------------------------------------*/
 
-#if 0
-/*-----------------------------------------------------------------*/
-/* pCodePeep */
-/*-----------------------------------------------------------------*/
-int pCodePeepCompare(pCode *pc, pCodePeep *pcp)
-{
-  pCode *pcfrom,*pcto;
-
-  pcfrom = pc;
-  for( pcto=pcp->target; pcto; pcto=pcto->next) {
 
-    pcfrom = findNextInstruction(pcfrom);
-
-    if( pcfrom &&  
-       (PCI(pcfrom)->op == PCI(pcto)->op || 
-        PCI(pcto)->op == POC_WILD))
-      continue;
-    return 0;
-  }
-  return 0;
-}
-
-/*-----------------------------------------------------------------*/
-/* pCodePeep */
-/*-----------------------------------------------------------------*/
-void pCodePeepSearch(pCodePeep *snippet)
-{
-  pBlock *pb;
-  pCode *pc;
-
-  if(!the_pFile)
-    return;
-
-  /* compare the chain to the pCode that we've 
-     got so far. If a match is found, then replace
-     the pCode chain.
-  */
-  for(pb = the_pFile->pbHead; pb; pb = pb->next) {
-    for(pc = pb->pcHead; pc; pc = pc->next) {
-      pCodePeepCompare(pc,snippet);
-    }
-  }
-
-}
-#endif
-
-#if 0
-pBlock *pBlockAppend(pBlock *pb1, pBlock *pb2)
-{
-  pBlock *pb;
-
-  if(!pb1->tail)
-    return pb2;
-
-  pb = pb1->tail;
-
-  pb2->head = pb1;
-  pb2->tail = NULL;
-  pb1->tail = pb2;
-
-}
-
-#endif
-
-void pCodePeepInit(void)
-{
-#if 0
-  pBlock *pb;
-  //  pCode *pc;
-  pCodePeep *pcp;
-  pCodePeepSnippets *pcps;
-
-  /* Declare a peep code snippet */
-  /* <FIXME> do I really need a separate struct just to DLL the snippets? */
-  /* e.g. I could put the DLL into the pCodePeep structure */
-
-  /* 
-
-     target:
-  
-     movwf %1
-     movf  %1,w
-  
-     replace:
-
-     movwf %1
-
-     Condition:
-     false condition - PCC_Z (Z bit is not used as input to subsequent code)
-     true condition - none
-  */
-  pcps = Safe_calloc(1,sizeof(pCodePeepSnippets));
-  pcp = pcps->peep  = Safe_calloc(1,sizeof(pCodePeep));
-  peepSnippets = DLL_append((_DLL*)peepSnippets,(_DLL*)pcps);
-
-
-  pb = newpCodeChain(NULL, 'W', newpCode(POC_MOVWF, newpCodeOpWild(0,pcp,newpCodeOp(NULL,PO_GPR_REGISTER))) );
-  addpCode2pBlock( pb,     newpCode(POC_MOVFW, newpCodeOpWild(0,pcp,newpCodeOp(NULL,PO_GPR_REGISTER))) );
-
-  pcp->target = pb;
-
-  pcp->replace = newpCodeChain(NULL, 'W',newpCode(POC_MOVWF, newpCodeOpWild(0,pcp,newpCodeOp(NULL,PO_GPR_REGISTER))) );
-
-  /* Allocate space to store pointers to the wildcard variables */
-  pcp->nvars = 1;
-  pcp->vars  = Safe_calloc(pcp->nvars, sizeof(char *));
-  pcp->nwildpCodes = 0;
-  pcp->wildpCodes  = NULL;
-
-  pcp->postFalseCond = PCC_Z;
-  pcp->postTrueCond  = PCC_NONE;
-
-  fprintf(stderr,"Peep rule\nTarget:\n");
-  printpCodeString(stderr,pcp->target->pcHead, 10);
-  fprintf(stderr,"Replaced with:\n");
-  printpCodeString(stderr,pcp->replace->pcHead, 10);
-
-  /* Now for another peep example */
-  pcps = Safe_calloc(1,sizeof(pCodePeepSnippets));
-  pcp = pcps->peep  = Safe_calloc(1,sizeof(pCodePeep));
-  peepSnippets = DLL_append((_DLL*)peepSnippets,(_DLL*)pcps);
-
-  {
-    /*
-      target:
-
-          btfsc  %0
-           goto  %1
-          %3
-      %1: %4
-
-      replace:
-          btfss  %0
-          %3
-      %1:  %4
-
-         The %3 and %4 are wild opcodes. Since the opcodes
-         are stored in a different array than the wild operands,
-         they can have the same indices and not conflict. So
-         below, the %3 is really a %0, %4 is a %1.
-
-     */
-    pCodeOp *pcl;
-    pCodeOp *pcw;
-    pCodeOp *pcwb;
-
-    // Create a new wild operand subtyped as a bit
-    pcwb =  newpCodeOpWild(0,pcp,newpCodeOpBit(NULL,-1,0));
-
-    // Create a 
-    pb = newpCodeChain(NULL, 'W',newpCode(POC_BTFSC,pcwb));
-
-    pcl = newpCodeOpLabel(-1);
-    pcw = newpCodeOpWild(1, pcp, pcl);
-    addpCode2pBlock( pb,     newpCode(POC_GOTO,  pcw));
-    addpCode2pBlock( pb,     newpCodeWild(0,NULL,NULL));
-    addpCode2pBlock( pb,     newpCodeWild(1,NULL,pcw));
-
-
-    pcp->target = pb;
-
-    pb = newpCodeChain(NULL, 'W',newpCode(POC_BTFSS, pcwb));
-    addpCode2pBlock( pb,     newpCodeWild(0,NULL,NULL));
-    addpCode2pBlock( pb,     newpCodeWild(1,NULL,pcw));
-
-    pcp->replace = pb;
-
-    /* Allocate space to store pointers to the wildcard variables */
-    pcp->nvars = 2;
-    pcp->vars = Safe_calloc(pcp->nvars, sizeof(char *));
-    pcp->nwildpCodes = 2;
-    pcp->wildpCodes = Safe_calloc(pcp->nwildpCodes, sizeof(pCode *));
-
-    pcp->postFalseCond = PCC_NONE;
-    pcp->postTrueCond  = PCC_NONE;
-  }
-
-
-
-
-
-
-
-
-
-
-  //-------------
-
-  /* Now for another peep example */
-  pcps = Safe_calloc(1,sizeof(pCodePeepSnippets));
-  pcp = pcps->peep  = Safe_calloc(1,sizeof(pCodePeep));
-  peepSnippets = DLL_append((_DLL*)peepSnippets,(_DLL*)pcps);
-
-  {
-    pCodeOp *pcw;
-
-    pcw = newpCodeOpWild(0,pcp,newpCodeOp(NULL,PO_GPR_REGISTER));
-
-    pb = newpCodeChain(NULL, 'W', newpCode(POC_MOVWF, pcw));
-    addpCode2pBlock( pb,     newpCode(POC_MOVWF, pcw));
-
-    pcp->target = pb;
-
-    pb = newpCodeChain(NULL, 'W',newpCode(POC_MOVWF, pcw));
-
-    pcp->replace = pb;
-
-    /* Allocate space to store pointers to the wildcard variables */
-    pcp->nvars = 1;
-    pcp->vars = Safe_calloc(pcp->nvars, sizeof(char *));
-    pcp->nwildpCodes = 0;
-    pcp->wildpCodes = NULL;
-
-    pcp->postFalseCond = PCC_NONE;
-    pcp->postTrueCond  = PCC_NONE;
-  }
-
-
-
-#endif
-}
 
 /*-----------------------------------------------------------------*/
 /* pCodeSearchCondition - Search a pCode chain for a 'condition'   */
@@ -1487,22 +1267,22 @@ int pCodePeepMatchLabels(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
       return 0;
 
     pcl = pcd->label->pc;
-    //labindex = PCOW(pcl)->id;
+
     labindex = -PCL(pcl)->key;
-    fprintf(stderr,"label id = %d (labindex = %d)\n",PCL(pcl)->key,labindex);
+    //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: %d %s\n",labindex, peepBlock->vars[labindex]);
+      //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");
+       // fprintf(stderr,"labels don't match\n");
        return 0;
       }
-      fprintf(stderr,"matched a label\n");
+      //fprintf(stderr,"matched a label\n");
     }
   } else {
-    fprintf(stderr,"destination doesn't have a label\n");
+    // fprintf(stderr,"destination doesn't have a label\n");
 
     if(pcs->label)
       return 0;
@@ -1551,9 +1331,11 @@ int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
       if(PCI(pcs)->op != PCI(pcd)->op)
        return 0;
 
+      /*
       fprintf(stderr,"%s comparing\n",__FUNCTION__);
       pcs->print(stderr,pcs);
       pcd->print(stderr,pcd);
+      */
 
       if(!pCodePeepMatchLabels(peepBlock, pcs, pcd))
        return 0;
@@ -1563,7 +1345,7 @@ int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
        if (PCI(pcd)->pcop->type == PO_WILD) {
          index = PCOW(PCI(pcd)->pcop)->id;
 
-         fprintf(stderr,"destination is wild\n");
+         //fprintf(stderr,"destination is wild\n");
 #ifdef DEBUG_PCODEPEEP
          if (index > peepBlock->nops) {
            fprintf(stderr,"%s - variables exceeded\n",__FUNCTION__);
@@ -1580,16 +1362,22 @@ int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
          {
            char *n;
 
-           if(PCI(pcs)->pcop->type == PO_GPR_TEMP) 
+           switch(PCI(pcs)->pcop->type) {
+           case PO_GPR_TEMP:
+           case PO_FSR:
+             //case PO_INDF:
              n = PCOR(PCI(pcs)->pcop)->r->name;
-           else
+
+             break;
+           default:
              n = PCI(pcs)->pcop->name;
+           }
 
            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;
+             // fprintf(stderr,"first time for a variable: %d, %s\n",index,n);
+             peepBlock->vars[index] = n;
              return 1;
            }
          }
@@ -1608,9 +1396,9 @@ int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
 
     index = PCW(pcd)->id;
 
-    fprintf(stderr,"%s comparing wild cards\n",__FUNCTION__);
-    pcs->print(stderr,pcs);
-    pcd->print(stderr,pcd);
+    //    fprintf(stderr,"%s comparing wild cards\n",__FUNCTION__);
+    //pcs->print(stderr,pcs);
+    //pcd->print(stderr,pcd);
 
     peepBlock->wildpCodes[PCW(pcd)->id] = pcs;
 
@@ -1621,6 +1409,7 @@ int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
       PCOW(PCI(pcd)->pcop)->matched = PCI(pcs)->pcop;
       if(peepBlock->vars[index]) {
        int i = (strcmp(peepBlock->vars[index],PCI(pcs)->pcop->name) == 0);
+       /*
        if(i)
          fprintf(stderr," (matched)\n");
        else {
@@ -1629,6 +1418,7 @@ int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
                  peepBlock->vars[index],
                  PCI(pcs)->pcop->name);
        }
+       */
        return i;
       } else {
        peepBlock->vars[index] = PCI(pcs)->pcop->name;
@@ -1638,8 +1428,8 @@ int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
 
     pcs = findNextInstruction(pcs->next); 
     if(pcs) {
-      fprintf(stderr," (next to match)\n");
-      pcs->print(stderr,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;
@@ -1699,7 +1489,7 @@ static pCodeOp *pCodeOpCopy(pCodeOp *pcop)
   switch(pcop->type) { 
   case PO_CRY:
   case PO_BIT:
-    fprintf(stderr,"pCodeOpCopy bit\n");
+    //fprintf(stderr,"pCodeOpCopy bit\n");
     pcopnew = Safe_calloc(1,sizeof(pCodeOpBit) );
     PCOB(pcopnew)->bit = PCOB(pcop)->bit;
     PCOB(pcopnew)->inBitSpace = PCOB(pcop)->inBitSpace;
@@ -1709,28 +1499,28 @@ static pCodeOp *pCodeOpCopy(pCodeOp *pcop)
   case PO_WILD:
     /* Here we expand the wild card into the appropriate type: */
     /* By recursively calling pCodeOpCopy */
-    fprintf(stderr,"pCodeOpCopy wild\n");
+    //fprintf(stderr,"pCodeOpCopy wild\n");
     if(PCOW(pcop)->matched)
       pcopnew = pCodeOpCopy(PCOW(pcop)->matched);
     else {
       // Probably a label
       pcopnew = pCodeOpCopy(PCOW(pcop)->subtype);
       pcopnew->name = Safe_strdup(PCOW(pcop)->pcp->vars[PCOW(pcop)->id]);
-      fprintf(stderr,"copied a wild op named %s\n",pcopnew->name);
+      //fprintf(stderr,"copied a wild op named %s\n",pcopnew->name);
     }
 
     return pcopnew;
     break;
 
   case PO_LABEL:
-    fprintf(stderr,"pCodeOpCopy label\n");
+    //fprintf(stderr,"pCodeOpCopy label\n");
     pcopnew = Safe_calloc(1,sizeof(pCodeOpLabel) );
     PCOLAB(pcopnew)->key =  PCOLAB(pcop)->key;
     break;
 
   case PO_LITERAL:
   case PO_IMMEDIATE:
-    fprintf(stderr,"pCodeOpCopy lit\n");
+    //fprintf(stderr,"pCodeOpCopy lit\n");
     pcopnew = Safe_calloc(1,sizeof(pCodeOpLit) );
     PCOL(pcopnew)->lit = PCOL(pcop)->lit;
     break;
@@ -1738,7 +1528,9 @@ static pCodeOp *pCodeOpCopy(pCodeOp *pcop)
   case PO_GPR_REGISTER:
   case PO_GPR_TEMP:
   case PO_GPR_BIT:
-    fprintf(stderr,"pCodeOpCopy GPR register\n");
+  case PO_FSR:
+  case PO_INDF:
+    //fprintf(stderr,"pCodeOpCopy GPR register\n");
     pcopnew = Safe_calloc(1,sizeof(pCodeOpReg) );
     PCOR(pcopnew)->r = PCOR(pcop)->r;
     PCOR(pcopnew)->rIdx = PCOR(pcop)->rIdx;
@@ -1746,24 +1538,25 @@ static pCodeOp *pCodeOpCopy(pCodeOp *pcop)
     break;
 
   case PO_DIR:
-    fprintf(stderr,"pCodeOpCopy PO_DIR\n");
+    //fprintf(stderr,"pCodeOpCopy PO_DIR\n");
   case PO_SFR_REGISTER:
   case PO_STR:
   case PO_NONE:
   case PO_W:
   case PO_STATUS:
-  case PO_FSR:
-  case PO_INDF:
   case PO_PCL:
   case PO_PCLATH:
 
-    fprintf(stderr,"pCodeOpCopy register type %d\n", pcop->type);
+    //fprintf(stderr,"pCodeOpCopy register type %d\n", pcop->type);
     pcopnew = Safe_calloc(1,sizeof(pCodeOp) );
 
   }
 
   pcopnew->type = pcop->type;
-  pcopnew->name = Safe_strdup(pcop->name);
+  if(pcop->name)
+    pcopnew->name = Safe_strdup(pcop->name);
+  else
+    pcopnew->name = NULL;
 
   return pcopnew;
 }
@@ -1827,7 +1620,7 @@ int pCodePeepMatchRule(pCode *pc)
       pcin = findNextInstruction(pcin->next);
       pct = pct->next;
       //debug:
-      fprintf(stderr,"    matched\n");
+      //fprintf(stderr,"    matched\n");
       if(!pcin)
        fprintf(stderr," end of code\n");
       if(!pct)
@@ -1907,7 +1700,7 @@ int pCodePeepMatchRule(pCode *pc)
             * Is it wild? */
            if(PCI(pcr)->pcop->type == PO_WILD) {
              int index = PCOW(PCI(pcr)->pcop)->id;
-             fprintf(stderr,"copying wildopcode\n");
+             //fprintf(stderr,"copying wildopcode\n");
              if(peepBlock->wildpCodeOps[index])
                pcop = pCodeOpCopy(peepBlock->wildpCodeOps[index]);
              else
@@ -1915,7 +1708,7 @@ int pCodePeepMatchRule(pCode *pc)
            } else
              pcop = pCodeOpCopy(PCI(pcr)->pcop);
          }
-         fprintf(stderr,"inserting pCode\n");
+         //fprintf(stderr,"inserting pCode\n");
          pCodeInsertAfter(pc, newpCode(PCI(pcr)->op,pcop));
        } else if (pcr->type == PC_WILD) {
          pCodeInsertAfter(pc,peepBlock->wildpCodes[PCW(pcr)->id]);
@@ -1925,8 +1718,8 @@ int pCodePeepMatchRule(pCode *pc)
 
 
        pc = pc->next;
-       if(pc)
-         pc->print(stderr,pc);
+       //if(pc)
+       //  pc->print(stderr,pc);
        pcr = pcr->next;
       }
 
@@ -1938,84 +1731,3 @@ int pCodePeepMatchRule(pCode *pc)
 
   return 0;
 }
-
-
-
-
-
-
-
-
-
-
-#if 0
-/*******************/
-pCode *parseLineNode(char *ln)
-{
-  char buffer[50], *s;
-  int state=0;          //0 label, 1 mnemonic, 2 operand, 3 operand, 4 comment
-  int var;
-  pCode *pc = NULL;
-  //  pCodeLabel *pcl = NULL;
-
-  if(!ln || !*ln)
-    return pc;
-
-  s = buffer;
-  *s = 0;
-
-  while(*ln) {
-
-    /* skip white space */
-    while (isspace (*ln))
-      ln++;
-
-    switch(state) {
-
-    case 0:   // look for a label
-    case 1:   // look for mnemonic
-
-      if(*ln == '%') {
-
-       // Wild
-
-       ln++;
-       if(!isdigit(*ln) )
-         break;
-         //goto next_state;
-
-       var = strtol(ln, &ln, 10);
-       if(*ln  == ':') {
-         // valid wild card label
-         fprintf(stderr, " wildcard label: %d\n",var);
-         ln++;
-       } else
-         fprintf(stderr, " wild opcode: %d\n",var), state++;
-
-      } else {
-       // not wild
-       // Extract the label/mnemonic from the line
-
-       s = buffer;
-       while(*ln && !(isspace(*ln) || *ln == ':'))
-         *s++ = *ln++;
-
-       *s = 0;
-       if(*ln == ':')
-         fprintf(stderr," regular label: %s\n",buffer), ln++;
-       else
-         fprintf(stderr," regular mnem: %s\n",buffer), state++;
-      }
-      state++;
-      break;
-
-    default:
-      ln++;
-
-    }
-  }
-
-  return pc;
-  
-}
-#endif