-/*-----------------------------------------------------------------*/
-/*-----------------------------------------------------------------*/
-void pCodeDeleteChain(pCode *f,pCode *t)
-{
- pCode *pc;
-
-
- while(f && f!=t) {
- fprintf(stderr,"delete pCode:\n");
- pc = f->next;
- f->print(stderr,f);
- //f->delete(f); this dumps core...
-
- f = pc;
-
- }
-
-}
-/*-----------------------------------------------------------------*/
-/*-----------------------------------------------------------------*/
-int pCodePeepMatchRule(pCode *pc)
-{
- pCodePeep *peepBlock;
- pCode *pct, *pcin;
- _DLL *peeprules;
- int matched;
-
- peeprules = (_DLL *)peepSnippets;
-
- while(peeprules) {
- peepBlock = ((pCodePeepSnippets*)peeprules)->peep;
- if(!peepBlock || !peepBlock->target || !peepBlock->target->pcHead)
- goto next_rule;
-
- pCodePeepClrVars(peepBlock);
-
- pcin = pc;
- pct = peepBlock->target->pcHead;
- matched = 0;
- while(pct && pcin) {
-
- if(! (matched = pCodePeepMatchLine(peepBlock, pcin,pct)))
- break;
-
- pcin = findNextInstruction(pcin->next);
- pct = pct->next;
- //debug:
- //fprintf(stderr," matched\n");
- if(!pcin)
- fprintf(stderr," end of code\n");
- if(!pct)
- fprintf(stderr," end of rule\n");
- }
-
- 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
- * if the post conditions are satisfied.
- * First we check the 'postFalseCond'. This means that we check
- * to see if any of the subsequent pCode's in the pCode chain
- * following the point just past where we have matched depend on
- * the `postFalseCond' as input then we abort the match
- */
- fprintf(stderr," matched rule so far, now checking conditions\n");
- if (pcin && peepBlock->postFalseCond &&
- (pCodeSearchCondition(pcin,peepBlock->postFalseCond) > 0) )
- matched = 0;
- }
-
- if(matched) {
-
- pCode *pcprev;
- pCode *pcr;
-
-
- /* We matched a rule! Now we have to go through and remove the
- inefficient code with the optimized version */
-
- fprintf(stderr, "Found a pcode peep match:\nRule:\n");
- printpCodeString(stderr,peepBlock->target->pcHead,10);
- fprintf(stderr,"first thing matched\n");
- pc->print(stderr,pc);
- if(pcin) {
- fprintf(stderr,"last thing matched\n");
- pcin->print(stderr,pcin);
- }
-
- /* Unlink the original code */
- pcprev = pc->prev;
- pcprev->next = pcin;
- if(pcin)
- pcin->prev = pc->prev;
-
- {
- /* DEBUG */
- /* Converted the deleted pCodes into comments */
-
- char buf[256];
-
- buf[0] = ';';
- buf[1] = '#';
-
- while(pc && pc!=pcin) {
- pCode2str(&buf[2], 254, pc);
- pCodeInsertAfter(pcprev, newpCodeCharP(buf));
- pcprev = pcprev->next;
- pc = pc->next;
- }
- }
-
- if(pcin)
- pCodeDeleteChain(pc,pcin);
-
- /* Generate the replacement code */
- pc = pcprev;
- pcr = peepBlock->replace->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) {
- if(PCI(pcr)->pcop) {
- /* The replacing instruction has an operand.
- * Is it wild? */
- if(PCI(pcr)->pcop->type == PO_WILD) {
- int index = PCOW(PCI(pcr)->pcop)->id;
- //fprintf(stderr,"copying wildopcode\n");
- if(peepBlock->wildpCodeOps[index])
- pcop = pCodeOpCopy(peepBlock->wildpCodeOps[index]);
- else
- fprintf(stderr,"error, wildopcode in replace but not source?\n");
- } else
- pcop = pCodeOpCopy(PCI(pcr)->pcop);
- }
- //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]);
- } else if (pcr->type == PC_COMMENT) {
- pCodeInsertAfter(pc, newpCodeCharP( ((pCodeComment *)(pcr))->comment));
+
+ if(pcin)
+ pCodeDeleteChain(pc,pcin);
+
+ /* Generate the replacement code */
+ pc = pcprev;
+ 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) {
+ if(PCI(pcr)->pcop) {
+ /* The replacing instruction has an operand.
+ * Is it wild? */
+ if(PCI(pcr)->pcop->type == PO_WILD) {
+ int index = PCOW(PCI(pcr)->pcop)->id;
+ //DFPRINTF((stderr,"copying wildopcode\n"));
+ if(peepBlock->target.wildpCodeOps[index])
+ pcop = pCodeOpCopy(peepBlock->target.wildpCodeOps[index]);
+ else
+ DFPRINTF((stderr,"error, wildopcode in replace but not source?\n"));
+ } else
+ pcop = pCodeOpCopy(PCI(pcr)->pcop);
+ }
+ //DFPRINTF((stderr,"inserting pCode\n"));
+ pCodeInsertAfter(pc, newpCode(PCI(pcr)->op,pcop));
+ } else if (pcr->type == PC_WILD) {
+ if(PCW(pcr)->invertBitSkipInst)
+ DFPRINTF((stderr,"We need to invert the bit skip instruction\n"));
+ pCodeInsertAfter(pc,
+ pCodeInstructionCopy(PCI(peepBlock->target.wildpCodes[PCW(pcr)->id]),
+ PCW(pcr)->invertBitSkipInst));
+ } else if (pcr->type == PC_COMMENT) {
+ pCodeInsertAfter(pc, newpCodeCharP( ((pCodeComment *)(pcr))->comment));
+ }
+
+ pc = pc->next;
+#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 && pc_cline ) {
+
+ pc = findNextInstruction(pc->next);
+ if (!pc) break;
+ PCI(pc)->cline = pc_cline;
+ pc_cline = PCCS(pc_cline->pc.next);
+
+ }
+
+ /* Copy C code comments to new code. */
+ pc = pcprev->next;
+ if (pc) {
+ for (; pc && pcout!=pcin; pcout=pcout->next) {
+ if (pcout->type==PC_OPCODE && PCI(pcout)->cline) {
+ while (pc->type!=PC_OPCODE || PCI(pc)->cline) {
+ pc = pc->next;
+ if (!pc)
+ break;
+ }
+ if (!pc) break;
+ PCI(pc)->cline = PCI(pcout)->cline;
+ }
+ }
+ }
+
+ return 1;
+ }
+next_rule:
+ peeprules = peeprules->next;