1 /*-------------------------------------------------------------------------
3 pcodepeep.c - post code generation
4 Written By - Scott Dattalo scott@dattalo.com
6 This program is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 -------------------------------------------------------------------------*/
24 #include "common.h" // Include everything in the SDCC src directory
29 #include "pcodeflow.h"
32 #if defined(__BORLANDC__) || defined(_MSC_VER)
33 #define STRCASECMP stricmp
35 #define STRCASECMP strcasecmp
38 pCodeOp *popCopyGPR2Bit(pCodeOpReg *pc, int bitval);
41 pCodeOp *newpCodeOpWild(int id, pCodeWildBlock *pcwb, pCodeOp *subtype);
42 pCode *newpCodeWild(int pCodeID, pCodeOp *optional_operand, pCodeOp *optional_label);
43 pCode * findNextInstruction(pCode *pc);
44 int getpCode(char *mnem,int dest);
45 int getpCodePeepCommand(char *cmd);
46 void pBlockMergeLabels(pBlock *pb);
47 char *pCode2str(char *str, int size, pCode *pc);
49 extern pCodeInstruction *pic14Mnemonics[];
52 #define IS_PCCOMMENT(x) ( x && (x->type==PC_COMMENT))
54 /****************************************************************/
56 * rootRules - defined in SDCCpeep.c
57 * This is a pointer to the (parsed) peephole rules that are
58 * defined in peep.def.
61 //extern peepRule *rootRules;
66 /****************************************************************/
67 /****************************************************************/
75 typedef struct pCodePeepSnippets
82 /****************************************************************/
86 /****************************************************************/
88 static pCodePeepSnippets *peepSnippets=NULL;
90 /****************************************************************/
94 /****************************************************************/
96 //static pCodePeep *curPeep=NULL;
98 /****************************************************************/
102 /****************************************************************/
104 //static pBlock *curBlock=NULL;
107 /****************************************************************/
109 /* max wild cards in a peep rule */
111 /****************************************************************/
113 //static int sMaxWildVar = 0;
114 //static int sMaxWildMnem = 0;
117 typedef struct pCodeToken
119 int tt; // token type;
128 pCodeToken tokArr[50];
147 typedef struct parsedPattern {
148 struct pcPattern *pcp;
152 #define MAX_PARSEDPATARR 50
153 parsedPattern parsedPatArr[MAX_PARSEDPATARR];
154 unsigned int parsedPatIdx=0;
167 static char pcpat_label[] = {PCT_PERCENT, PCT_NUMBER, PCT_COLON, 0};
168 static char pcpat_number[] = {PCT_NUMBER, 0};
169 static char pcpat_string[] = {PCT_STRING, 0};
170 static char pcpat_wildString[] = {PCT_PERCENT, PCT_STRING, 0};
171 static char pcpat_wildVar[] = {PCT_PERCENT, PCT_NUMBER, 0};
172 static char pcpat_comma[] = {PCT_COMMA, 0};
173 static char pcpat_comment[] = {PCT_COMMENT, 0};
176 typedef struct pcPattern {
177 char pt; // Pattern type
178 char *tokens; // list of tokens that describe the pattern
179 void * (*f) (void *,pCodeWildBlock *);
182 pcPattern pcpArr[] = {
183 {PCP_LABEL, pcpat_label, NULL},
184 {PCP_WILDSTR, pcpat_wildString, NULL},
185 {PCP_STR, pcpat_string, NULL},
186 {PCP_WILDVAR, pcpat_wildVar, NULL},
187 {PCP_COMMA, pcpat_comma, NULL},
188 {PCP_COMMENT, pcpat_comment, NULL},
189 {PCP_NUMBER, pcpat_number, NULL}
192 #define PCPATTERNS (sizeof(pcpArr)/sizeof(pcPattern))
194 // Assembly Line Token
207 static char alt_comment[] = { PCP_COMMENT, 0};
208 static char alt_label[] = { PCP_LABEL, 0};
209 static char alt_mnem0[] = { PCP_STR, 0};
210 static char alt_mnem0a[] = { PCP_WILDVAR, 0};
211 static char alt_mnem1[] = { PCP_STR, PCP_STR, 0};
212 static char alt_mnem1a[] = { PCP_STR, PCP_WILDVAR, 0};
213 static char alt_mnem1b[] = { PCP_STR, PCP_NUMBER, 0};
214 static char alt_mnem2[] = { PCP_STR, PCP_STR, PCP_COMMA, PCP_STR, 0};
215 static char alt_mnem2a[] = { PCP_STR, PCP_WILDVAR, PCP_COMMA, PCP_STR, 0};
217 static void * cvt_altpat_label(void *pp,pCodeWildBlock *pcwb);
218 static void * cvt_altpat_comment(void *pp,pCodeWildBlock *pcwb);
219 static void * cvt_altpat_mnem0(void *pp,pCodeWildBlock *pcwb);
220 static void * cvt_altpat_mnem0a(void *pp,pCodeWildBlock *pcwb);
221 static void * cvt_altpat_mnem1(void *pp,pCodeWildBlock *pcwb);
222 static void * cvt_altpat_mnem1a(void *pp,pCodeWildBlock *pcwb);
223 static void * cvt_altpat_mnem1b(void *pp,pCodeWildBlock *pcwb);
224 static void * cvt_altpat_mnem2(void *pp,pCodeWildBlock *pcwb);
225 static void * cvt_altpat_mnem2a(void *pp,pCodeWildBlock *pcwb);
227 pcPattern altArr[] = {
228 {ALT_LABEL, alt_label, cvt_altpat_label},
229 {ALT_COMMENT, alt_comment,cvt_altpat_comment},
230 {ALT_MNEM2A, alt_mnem2a, cvt_altpat_mnem2a},
231 {ALT_MNEM2, alt_mnem2, cvt_altpat_mnem2},
232 {ALT_MNEM1B, alt_mnem1b, cvt_altpat_mnem1b},
233 {ALT_MNEM1A, alt_mnem1a, cvt_altpat_mnem1a},
234 {ALT_MNEM1, alt_mnem1, cvt_altpat_mnem1},
235 {ALT_MNEM0A, alt_mnem0a, cvt_altpat_mnem0a},
236 {ALT_MNEM0, alt_mnem0, cvt_altpat_mnem0},
240 #define ALTPATTERNS (sizeof(altArr)/sizeof(pcPattern))
242 // forward declarations
243 static void * DLL_append(_DLL *list, _DLL *next);
245 /*-----------------------------------------------------------------*/
246 /* cvt_extract_destination - helper function extracts the register */
247 /* destination from a parsedPattern. */
249 /*-----------------------------------------------------------------*/
250 static int cvt_extract_destination(parsedPattern *pp)
253 if(pp->pct[0].tt == PCT_STRING) {
255 // just check first letter for now
257 if(toupper(*pp->pct[0].tok.s) == 'F')
260 } else if (pp->pct[0].tt == PCT_NUMBER) {
270 /*-----------------------------------------------------------------*/
271 /* pCodeOp *cvt_extract_status(char *reg, char *bit) */
272 /* if *reg is the "status" register and *bit is one of the */
273 /* status bits, then this function will create a new pCode op */
274 /* containing the status register. */
275 /*-----------------------------------------------------------------*/
277 static pCodeOp *cvt_extract_status(char *reg, char *bit)
281 if(STRCASECMP(reg, pc_status.pcop.name))
288 if(toupper(*bit) == 'C')
289 return PCOP(popCopyGPR2Bit(&pc_status,PIC_C_BIT));
290 if(toupper(*bit) == 'Z')
291 return PCOP(popCopyGPR2Bit(&pc_status,PIC_Z_BIT));
295 if(len ==2 && toupper(bit[0]) == 'D' && toupper(bit[1]) == 'C')
296 return PCOP(popCopyGPR2Bit(&pc_status,PIC_DC_BIT));
302 /*-----------------------------------------------------------------*/
303 /* cvt_altpat_label - convert assembly line type to a pCode label */
304 /* INPUT: pointer to the parsedPattern */
308 /* label pattern => '%' number ':' */
309 /* at this point, we wish to extract only the 'number' */
311 /*-----------------------------------------------------------------*/
312 static void * cvt_altpat_label(void *pp,pCodeWildBlock *pcwb)
314 parsedPattern *p = pp;
316 DFPRINTF((stderr,"altpat_label with ID = %d\n",p->pct[1].tok.n));
317 return newpCodeLabel(NULL,-p->pct[1].tok.n);
321 /*-----------------------------------------------------------------*/
322 /* cvt_altpat_comment - convert assembly line type to a comment */
323 /* INPUT: pointer to the parsedPattern */
325 /* pp[0] - comment */
328 /*-----------------------------------------------------------------*/
329 static void * cvt_altpat_comment(void *pp,pCodeWildBlock *pcwb)
331 parsedPattern *p = pp;
333 DFPRINTF((stderr,"altpat_comment = %s\n",p->pct[0].tok.s));
334 return newpCodeCharP(p->pct[0].tok.s);
338 /*-----------------------------------------------------------------*/
339 /* cvt_altpat_mem0 - convert assembly line type to a wild pCode */
344 /*-----------------------------------------------------------------*/
345 static void * cvt_altpat_mnem0(void *pp,pCodeWildBlock *pcwb)
347 parsedPattern *p = pp;
350 pCodeInstruction *pci=NULL;
352 DFPRINTF((stderr,"altpat_mnem0 %s\n", p->pct[0].tok.s));
354 opcode = getpCode(p->pct[0].tok.s,0);
357 /* look for special command strings like _NOTBITSKIP_ */
359 //fprintf(stderr, "Bad mnemonic\n");
361 opcode = getpCodePeepCommand(p->pct[0].tok.s);
363 // fprintf(stderr," but valid peep command: %s, key = %d\n",p->pct[0].tok.s,opcode);
367 pci = PCI(newpCode(opcode, NULL));
370 fprintf(stderr,"couldn't find mnemonic\n");
376 /*-----------------------------------------------------------------*/
377 /* cvt_altpat_mem0a - convert assembly line type to a wild pCode */
380 /* pp[0] - wild var */
382 /*-----------------------------------------------------------------*/
383 static void * cvt_altpat_mnem0a(void *pp, pCodeWildBlock *pcwb)
385 parsedPattern *p = pp;
387 DFPRINTF((stderr,"altpat_mnem0a wild mnem # %d\n", p[0].pct[1].tok.n));
389 /* Save the index of the maximum wildcard mnemonic */
391 //if(p[0].pct[1].tok.n > sMaxWildVar)
392 // sMaxWildMnem = p[0].pct[1].tok.n;
394 if(p[0].pct[1].tok.n > pcwb->nvars)
395 pcwb->nwildpCodes = p[0].pct[1].tok.n;
397 return newpCodeWild(p[0].pct[1].tok.n,NULL,NULL);
401 /*-----------------------------------------------------------------*/
402 /* cvt_altpat_mem1 - convert assembly line type to a pCode */
403 /* instruction with 1 operand. */
406 /* pp[1] - Operand */
408 /*-----------------------------------------------------------------*/
409 static void * cvt_altpat_mnem1(void *pp,pCodeWildBlock *pcwb)
412 parsedPattern *p = pp;
415 pCodeInstruction *pci=NULL;
418 DFPRINTF((stderr,"altpat_mnem1 %s var %s\n", p->pct[0].tok.s,p[1].pct[0].tok.s));
420 opcode = getpCode(p->pct[0].tok.s,0);
422 //fprintf(stderr, "Bad mnemonic\n");
423 opcode = getpCodePeepCommand(p->pct[0].tok.s);
425 //fprintf(stderr," but valid peep command: %s, key = %d\n",p->pct[0].tok.s,opcode);
430 if(pic14Mnemonics[opcode]->isBitInst)
431 pcosubtype = newpCodeOp(p[1].pct[0].tok.s,PO_BIT);
433 pcosubtype = newpCodeOp(p[1].pct[0].tok.s,PO_GPR_REGISTER);
436 pci = PCI(newpCode(opcode, pcosubtype));
439 fprintf(stderr,"couldn't find mnemonic\n");
445 /*-----------------------------------------------------------------*/
446 /* cvt_altpat_mem1a - convert assembly line type to a pCode */
447 /* instruction with 1 wild operand. */
450 /* pp[1] - wild var */
452 /*-----------------------------------------------------------------*/
453 static void * cvt_altpat_mnem1a(void *pp,pCodeWildBlock *pcwb)
455 parsedPattern *p = pp;
458 pCodeInstruction *pci=NULL;
461 DFPRINTF((stderr,"altpat_mnem1a %s var %d\n", p->pct[0].tok.s,p[1].pct[1].tok.n));
463 opcode = getpCode(p->pct[0].tok.s,0);
465 int cmd_id = getpCodePeepCommand(p->pct[0].tok.s);
469 fprintf(stderr, "Bad mnemonic\n");
473 if(p[0].pct[1].tok.n > pcwb->nvars)
474 pcwb->nwildpCodes = p[0].pct[1].tok.n;
476 pc = newpCodeWild(p[1].pct[1].tok.n,NULL,NULL);
480 PCW(pc)->mustNotBeBitSkipInst = 1;
483 PCW(pc)->mustBeBitSkipInst = 1;
486 PCW(pc)->invertBitSkipInst = 1;
491 if(pic14Mnemonics[opcode]->isBitInst)
492 pcosubtype = newpCodeOpBit(NULL,-1,0);
494 pcosubtype = newpCodeOp(NULL,PO_GPR_REGISTER);
497 pci = PCI(newpCode(opcode,
498 newpCodeOpWild(p[1].pct[1].tok.n, pcwb, pcosubtype)));
500 /* Save the index of the maximum wildcard variable */
501 //if(p[1].pct[1].tok.n > sMaxWildVar)
502 // sMaxWildVar = p[1].pct[1].tok.n;
504 if(p[1].pct[1].tok.n > pcwb->nvars)
505 pcwb->nvars = p[1].pct[1].tok.n;
508 fprintf(stderr,"couldn't find mnemonic\n");
514 /*-----------------------------------------------------------------*/
515 /*-----------------------------------------------------------------*/
516 static void * cvt_altpat_mnem1b(void *pp,pCodeWildBlock *pcwb)
518 parsedPattern *p = pp;
521 pCodeInstruction *pci=NULL;
523 DFPRINTF((stderr,"altpat_mnem1b %s var %d\n", p->pct[0].tok.s,p[1].pct[0].tok.n));
525 opcode = getpCode(p->pct[0].tok.s,0);
527 fprintf(stderr, "Bad mnemonic\n");
531 pci = PCI(newpCode(opcode, newpCodeOpLit(p[1].pct[0].tok.n) ));
534 fprintf(stderr,"couldn't find mnemonic\n");
540 /*-----------------------------------------------------------------*/
541 /*-----------------------------------------------------------------*/
542 static void * cvt_altpat_mnem2(void *pp,pCodeWildBlock *pcwb)
544 parsedPattern *p = pp;
548 pCodeInstruction *pci=NULL;
551 dest = cvt_extract_destination(&p[3]);
553 DFPRINTF((stderr,"altpat_mnem2 %s var %s destination %s(%d)\n",
560 opcode = getpCode(p->pct[0].tok.s,dest);
562 fprintf(stderr, "Bad mnemonic\n");
566 if(pic14Mnemonics[opcode]->isBitInst) {
567 pcosubtype = cvt_extract_status(p[1].pct[0].tok.s, p[3].pct[0].tok.s);
568 if(pcosubtype == NULL) {
569 fprintf(stderr, "bad operand?\n");
574 pcosubtype = newpCodeOp(p[1].pct[0].tok.s,PO_GPR_REGISTER);
577 pci = PCI(newpCode(opcode,pcosubtype));
580 fprintf(stderr,"couldn't find mnemonic\n");
586 /*-----------------------------------------------------------------*/
587 /* cvt_altpat_mem2a - convert assembly line type to a pCode */
588 /* instruction with 1 wild operand and a */
589 /* destination operand (e.g. w or f) */
592 /* pp[1] - wild var */
594 /* pp[3] - destination */
596 /*-----------------------------------------------------------------*/
597 static void * cvt_altpat_mnem2a(void *pp,pCodeWildBlock *pcwb)
599 parsedPattern *p = pp;
603 pCodeInstruction *pci=NULL;
606 dest = cvt_extract_destination(&p[3]);
608 DFPRINTF((stderr,"altpat_mnem2a %s var %d destination %s(%d)\n",
615 opcode = getpCode(p->pct[0].tok.s,dest);
617 fprintf(stderr, "Bad mnemonic\n");
621 if(pic14Mnemonics[opcode]->isBitInst)
622 pcosubtype = newpCodeOp(NULL,PO_BIT);
624 pcosubtype = newpCodeOp(NULL,PO_GPR_REGISTER);
627 pci = PCI(newpCode(opcode,
628 newpCodeOpWild(p[1].pct[1].tok.n, pcwb, pcosubtype)));
630 /* Save the index of the maximum wildcard variable */
631 //if(p[1].pct[1].tok.n > sMaxWildVar)
632 // sMaxWildVar = p[1].pct[1].tok.n;
634 if(p[1].pct[1].tok.n > pcwb->nvars)
635 pcwb->nvars = p[1].pct[1].tok.n;
638 fprintf(stderr,"couldn't find mnemonic\n");
644 /*-----------------------------------------------------------------*/
645 /* tokenizeLineNode - Convert a string (of char's) that was parsed */
646 /* by SDCCpeeph.c into a string of tokens. */
649 /* The tokenizer is of the classic type. When an item is encounterd*/
650 /* it is converted into a token. The token is a structure that */
651 /* encodes the item's type and it's value (when appropriate). */
653 /* Accepted token types: */
654 /* SPACE NUMBER STRING % : , ; */
658 /*-----------------------------------------------------------------*/
661 static void tokenizeLineNode(char *ln)
664 tokIdx = 0; // Starting off at the beginning
665 tokArr[0].tt = PCT_NULL; // and assume invalid character for first token.
673 // add a SPACE token and eat the extra spaces.
674 tokArr[tokIdx++].tt = PCT_SPACE;
675 while (isspace (*ln))
682 tokArr[tokIdx].tt = PCT_NUMBER;
683 tokArr[tokIdx++].tok.n = strtol(ln, &ln, 0);
691 tokArr[tokIdx++].tt = PCT_PERCENT;
694 tokArr[tokIdx++].tt = PCT_LESSTHAN;
697 tokArr[tokIdx++].tt = PCT_GREATERTHAN;
700 tokArr[tokIdx++].tt = PCT_COLON;
703 tokArr[tokIdx].tok.s = Safe_strdup(ln);
704 tokArr[tokIdx++].tt = PCT_COMMENT;
705 tokArr[tokIdx].tt = PCT_NULL;
708 tokArr[tokIdx++].tt = PCT_COMMA;
713 if(isalpha(*ln) || (*ln == '_') ) {
717 while( (isalpha(*ln) || isdigit(*ln) || (*ln == '_')) && i<49)
723 tokArr[tokIdx].tok.s = Safe_strdup(buffer);
724 tokArr[tokIdx++].tt = PCT_STRING;
729 /* Advance to next character in input string .
730 * Note, if none of the tests passed above, then
731 * we effectively ignore the `bad' character.
732 * Since the line has already been parsed by SDCCpeeph,
733 * chance are that there are no invalid characters... */
739 tokArr[tokIdx].tt = 0;
743 /*-----------------------------------------------------------------*/
744 /*-----------------------------------------------------------------*/
748 void dump1Token(pCodeTokens tt)
753 fprintf(stderr, " space ");
756 fprintf(stderr, " pct %%");
759 fprintf(stderr, " pct <");
761 case PCT_GREATERTHAN:
762 fprintf(stderr, " pct >");
765 fprintf(stderr, " col :");
768 fprintf(stderr, " comma , ");
771 fprintf(stderr, " comment ");
772 //fprintf(stderr,"%s",tokArr[i].tok.s);
775 fprintf(stderr, " str ");
776 //fprintf(stderr,"%s",tokArr[i].tok.s);
779 fprintf(stderr, " num ");
780 //fprintf(stderr,"%d",tokArr[i].tok.n);
783 fprintf(stderr, " null ");
790 /*-----------------------------------------------------------------*/
791 /*-----------------------------------------------------------------*/
793 int pcComparePattern(pCodeToken *pct, char *pat, int max_tokens)
797 if(!pct || !pat || !*pat)
800 //DFPRINTF((stderr,"comparing against:\n"));
802 while(i < max_tokens) {
805 //DFPRINTF((stderr,"matched\n"));
809 //dump1Token(*pat); DFPRINTF((stderr,"\n"));
823 /*-----------------------------------------------------------------*/
824 /*-----------------------------------------------------------------*/
826 int altComparePattern( char *pct, parsedPattern *pat, int max_tokens)
830 if(!pct || !pat || !*pct)
834 while(i < max_tokens) {
837 //DFPRINTF((stderr,"matched\n"));
841 //dump1Token(*pat); DFPRINTF((stderr,"\n"));
843 if( !pat || !pat->pcp )
846 if (pat->pcp->pt != *pct)
849 //DFPRINTF((stderr," pct=%d\n",*pct));
858 /*-----------------------------------------------------------------*/
859 /*-----------------------------------------------------------------*/
861 int advTokIdx(int *v, int amt)
864 if((unsigned) (*v + amt) > tokIdx)
872 /*-----------------------------------------------------------------*/
873 /* parseTokens - convert the tokens corresponding to a single line */
874 /* of a peep hole assembly into a pCode object. */
879 /* This is a simple parser that looks for strings of the type */
880 /* allowed in the peep hole definition file. Essentially the format*/
881 /* is the same as a line of assembly: */
883 /* label: mnemonic op1, op2, op3 ; comment */
885 /* Some of these items aren't present. It's the job of the parser */
886 /* to determine which are and convert those into the appropriate */
888 /*-----------------------------------------------------------------*/
890 void parseTokens(pCodeWildBlock *pcwb)
898 for(i=0; i<=tokIdx; i++)
899 dump1Token(tokArr[i].tt);
911 char * cPmnem = NULL; // Pointer to non-wild mnemonic (if any)
912 char * cP1stop = NULL;
913 char * cP2ndop = NULL;
915 //pCodeOp *pcl = NULL; // Storage for a label
916 //pCodeOp *pco1 = NULL; // 1st operand
917 //pCodeOp *pco2 = NULL; // 2nd operand
918 //pCode *pc = NULL; // Mnemonic
929 ParseStates state = PS_START;
936 if( ((tokArr[ltokIdx].tt == PCT_SPACE) )
937 && (advTokIdx(<okIdx, 1)) ) // eat space
941 j = pcComparePattern(&tokArr[ltokIdx], pcpArr[lpcpIdx].tokens, tokIdx +1);
944 switch(pcpArr[lpcpIdx].pt) {
946 if(state == PS_START){
947 DFPRINTF((stderr," label\n"));
948 state = PS_HAVE_LABEL;
950 DFPRINTF((stderr," bad state (%d) for label\n",state));
954 DFPRINTF((stderr," %s is",tokArr[ltokIdx].tok.s));
958 DFPRINTF((stderr," mnem\n"));
959 cPmnem = tokArr[ltokIdx].tok.s;
960 state = PS_HAVE_MNEM;
963 DFPRINTF((stderr," 1st operand\n"));
964 cP1stop = tokArr[ltokIdx].tok.s;
965 //pco1 = newpCodeOp(NULL,PO_GPR_REGISTER);
966 state = PS_HAVE_1OPERAND;
968 case PS_HAVE_1OPERAND:
969 DFPRINTF((stderr," error expecting comma\n"));
972 DFPRINTF((stderr," 2 operands\n"));
973 cP2ndop = tokArr[ltokIdx].tok.s;
975 case PS_HAVE_2OPERANDS:
984 DFPRINTF((stderr," wild mnem\n"));
985 state = PS_HAVE_MNEM;
988 DFPRINTF((stderr," 1st operand is wild\n"));
989 state = PS_HAVE_1OPERAND;
991 case PS_HAVE_1OPERAND:
992 DFPRINTF((stderr," error expecting comma\n"));
995 DFPRINTF((stderr," 2nd operand is wild\n"));
997 case PS_HAVE_2OPERANDS:
1006 fprintf(stderr," ERROR number\n");
1009 DFPRINTF((stderr," 1st operand is a number\n"));
1010 state = PS_HAVE_1OPERAND;
1012 case PS_HAVE_1OPERAND:
1013 fprintf(stderr," error expecting comma\n");
1016 DFPRINTF((stderr," 2nd operand is a number\n"));
1018 case PS_HAVE_2OPERANDS:
1026 if(state == PS_HAVE_1OPERAND){
1027 DFPRINTF((stderr," got a comma\n"));
1028 state = PS_HAVE_COMMA;
1030 fprintf(stderr," unexpected comma\n");
1034 parsedPatArr[lparsedPatIdx].pcp = &pcpArr[lpcpIdx];
1035 parsedPatArr[lparsedPatIdx].pct = &tokArr[ltokIdx];
1038 //dump1Token(tokArr[ltokIdx].tt);
1040 if(advTokIdx(<okIdx, strlen(pcpArr[lpcpIdx].tokens) ) ) {
1041 DFPRINTF((stderr," reached end \n"));
1048 } while ((++lpcpIdx < PCPATTERNS) && !matching);
1052 parsedPatArr[lparsedPatIdx].pcp = NULL;
1053 parsedPatArr[lparsedPatIdx].pct = NULL;
1059 if( (c=altComparePattern( altArr[k].tokens, &parsedPatArr[j],10) ) ) {
1062 pc = altArr[k].f(&parsedPatArr[j],pcwb);
1063 //if(pc && pc->print)
1064 // pc->print(stderr,pc);
1065 //if(pc && pc->destruct) pc->destruct(pc); dumps core?
1067 //if(curBlock && pc)
1068 //addpCode2pBlock(curBlock, pc);
1069 addpCode2pBlock(pcwb->pb, pc);
1075 while(j<=lparsedPatIdx && k<ALTPATTERNS);
1078 DFPRINTF((stderr,"\nConverting parsed line to pCode:\n\n"));
1082 if(parsedPatArr[j].pcp && parsedPatArr[j].pcp->f )
1083 parsedPatArr[j].pcp->f(&parsedPatArr[j]);
1084 DFPRINTF((stderr," %d",parsedPatArr[j].pcp->pt));
1087 while(j<lparsedPatIdx);
1089 DFPRINTF((stderr,"\n"));
1096 /*-----------------------------------------------------------------*/
1098 /*-----------------------------------------------------------------*/
1099 void peepRuleBlock2pCodeBlock( lineNode *ln, pCodeWildBlock *pcwb)
1105 for( ; ln; ln = ln->next) {
1107 //DFPRINTF((stderr,"%s\n",ln->line));
1109 tokenizeLineNode(ln->line);
1115 /*-----------------------------------------------------------------*/
1116 /* peepRuleCondition */
1117 /*-----------------------------------------------------------------*/
1118 static void peepRuleCondition(char *cond, pCodePeep *pcp)
1123 //DFPRINTF((stderr,"\nCondition: %s\n",cond));
1124 /* brute force compares for now */
1126 if(STRCASECMP(cond, "NZ") == 0) {
1127 //DFPRINTF((stderr,"found NZ\n"));
1128 pcp->postFalseCond = PCC_Z;
1135 void initpCodeWildBlock(pCodeWildBlock *pcwb)
1138 // pcwb = Safe_calloc(1,sizeof(pCodeWildBlock));
1144 pcwb->wildpCodes = NULL;
1145 pcwb->wildpCodeOps = NULL;
1148 pcwb->nwildpCodes = 0;
1153 void postinit_pCodeWildBlock(pCodeWildBlock *pcwb)
1160 pcwb->nops = pcwb->nvars;
1162 pcwb->vars = Safe_calloc(pcwb->nvars, sizeof(char *));
1163 pcwb->wildpCodeOps = Safe_calloc(pcwb->nvars, sizeof(pCodeOp *));
1165 pcwb->nwildpCodes++;
1166 pcwb->wildpCodes = Safe_calloc(pcwb->nwildpCodes, sizeof(pCode *));
1170 void initpCodePeep(pCodePeep *pcp)
1173 // pcwb = Safe_calloc(1,sizeof(pCodeWildBlock));
1178 initpCodeWildBlock(&pcp->target);
1179 pcp->target.pb = newpCodeChain(NULL, 'W', NULL);
1181 initpCodeWildBlock(&pcp->replace);
1182 pcp->replace.pb = newpCodeChain(NULL, 'W', NULL);
1186 /*-----------------------------------------------------------------*/
1187 /* peepRules2pCode - parse the "parsed" peep hole rules to generate*/
1190 /* SDCCpeeph parses the peep rules file and extracts variables, */
1191 /* removes white space, and checks the syntax. This function */
1192 /* extends that processing to produce pCode objects. You can kind */
1193 /* think of this function as an "assembler", though instead of */
1194 /* taking raw text to produce machine code, it produces pCode. */
1196 /*-----------------------------------------------------------------*/
1197 extern void pic14initpCodePeepCommands(void);
1199 void peepRules2pCode(peepRule *rules)
1203 pCodePeep *currentRule;
1204 pCodePeepSnippets *pcps;
1206 pic14initpCodePeepCommands();
1208 /* The rules are in a linked-list. Each rule has two portions */
1209 /* There's the `target' and there's the `replace'. The target */
1210 /* is compared against the SDCC generated code and if it */
1211 /* matches, it gets replaced by the `replace' block of code. */
1213 /* Here we loop through each rule and convert the target's and*/
1214 /* replace's into pCode target and replace blocks */
1216 for (pr = rules; pr; pr = pr->next) {
1218 //DFPRINTF((stderr,"\nRule:\n\n"));
1220 pcps = Safe_calloc(1,sizeof(pCodePeepSnippets));
1221 peepSnippets = DLL_append((_DLL*)peepSnippets,(_DLL*)pcps);
1224 curPeep = pcps->peep = Safe_calloc(1,sizeof(pCodePeep));
1226 curPeep->vars = NULL;
1227 curPeep->wildpCodes = NULL; curPeep->wildpCodeOps = NULL;
1228 curPeep->postFalseCond = PCC_NONE;
1229 curPeep->postTrueCond = PCC_NONE;
1232 curPeep->target = curBlock = newpCodeChain(NULL, 'W', NULL);
1236 currentRule = pcps->peep = Safe_calloc(1,sizeof(pCodePeep));
1237 initpCodePeep(currentRule);
1239 /* Convert the target block */
1240 peepRuleBlock2pCodeBlock(pr->match, ¤tRule->target);
1242 //DFPRINTF((stderr,"finished target, here it is in pcode form:\n"));
1243 //printpBlock(stderr, curBlock);
1245 //DFPRINTF((stderr,"target with labels merged:\n"));
1246 //pBlockMergeLabels(curBlock);
1247 pBlockMergeLabels(currentRule->target.pb);
1248 //printpBlock(stderr, currentRule->replace.pb);
1250 //#ifdef PCODE_DEBUG
1251 // printpBlock(stderr, curBlock);
1253 //DFPRINTF((stderr,"\nReplaced by:\n"));
1256 //curPeep->replace = curBlock = newpCodeChain(NULL, 'W', NULL);
1258 /* Convert the replace block */
1259 peepRuleBlock2pCodeBlock(pr->replace, ¤tRule->replace);
1261 //DFPRINTF((stderr,"finished replace block, here it is in pcode form:\n"));
1262 //printpBlock(stderr, curBlock);
1264 //DFPRINTF((stderr,"replace with labels merged:\n"));
1265 //pBlockMergeLabels(curBlock);
1266 pBlockMergeLabels(currentRule->replace.pb);
1267 //printpBlock(stderr, currentRule->replace.pb);
1269 peepRuleCondition(pr->cond,currentRule);
1271 /* The rule has been converted to pCode. Now allocate
1272 * space for the wildcards */
1276 curPeep->nvars = sMaxWildVar;
1277 curPeep->vars = Safe_calloc(sMaxWildVar, sizeof(char *));
1279 curPeep->nops = sMaxWildVar;
1280 curPeep->wildpCodeOps = Safe_calloc(sMaxWildVar, sizeof(pCodeOp *));
1282 curPeep->nwildpCodes = ++sMaxWildMnem;
1283 curPeep->wildpCodes = Safe_calloc(sMaxWildMnem, sizeof(char *));
1285 postinit_pCodeWildBlock(¤tRule->target);
1287 //return; // debug ... don't want to go through all the rules yet
1291 pCodePeep *peepBlock;
1294 peeprules = (_DLL *)peepSnippets;
1295 //fprintf(stderr,"target rules\n");
1297 //fprintf(stderr," rule:\n");
1298 peepBlock = ((pCodePeepSnippets*)peeprules)->peep;
1299 //printpBlock(stderr, peepBlock->target.pb);
1300 peeprules = peeprules->next;
1302 //fprintf(stderr," ... done\n");
1307 void printpCodeString(FILE *of, pCode *pc, int max)
1311 while(pc && (i++<max)) {
1317 /*-----------------------------------------------------------------*/
1318 /* _DLL * DLL_append */
1320 /* Append a _DLL object to the end of a _DLL (doubly linked list) */
1321 /* If The list to which we want to append is non-existant then one */
1322 /* is created. Other wise, the end of the list is sought out and */
1323 /* a new DLL object is appended to it. In either case, the void */
1324 /* *data is added to the newly created DLL object. */
1325 /*-----------------------------------------------------------------*/
1327 static void * DLL_append(_DLL *list, _DLL *next)
1332 /* If there's no list, then create one: */
1334 next->next = next->prev = NULL;
1339 /* Search for the end of the list. */
1344 /* Now append the new DLL object */
1355 /*-----------------------------------------------------------------
1357 pCode peephole optimization
1360 The pCode "peep hole" optimization is not too unlike the peep hole
1361 optimization in SDCCpeeph.c. The major difference is that here we
1362 use pCode's whereas there we use ASCII strings. The advantage with
1363 pCode's is that we can ascertain flow information in the instructions
1367 <FIX ME> - elaborate...
1369 -----------------------------------------------------------------*/
1373 /*-----------------------------------------------------------------*/
1374 /* pCodeSearchCondition - Search a pCode chain for a 'condition' */
1376 /* return conditions */
1377 /* 1 - The Condition was found for a pCode's input */
1378 /* 0 - No matching condition was found for the whole chain */
1379 /* -1 - The Condition was found for a pCode's output */
1381 /*-----------------------------------------------------------------*/
1382 int pCodeSearchCondition(pCode *pc, unsigned int cond)
1387 /* If we reach a function end (presumably an end since we most
1388 probably began the search in the middle of a function), then
1389 the condition was not found. */
1390 if(pc->type == PC_FUNCTION)
1393 if(pc->type == PC_OPCODE) {
1394 if(PCI(pc)->inCond & cond)
1396 if(PCI(pc)->outCond & cond)
1406 int pCodePeepMatchLabels(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
1410 /* Check for a label associated with this wild pCode */
1411 // If the wild card has a label, make sure the source code does too.
1412 if(PCI(pcd)->label) {
1415 if(!PCI(pcs)->label)
1418 pcl = PCI(pcd)->label->pc;
1420 labindex = -PCL(pcl)->key;
1421 //DFPRINTF((stderr,"label id = %d (labindex = %d)\n",PCL(pcl)->key,labindex));
1422 if(peepBlock->target.vars[labindex] == NULL) {
1423 // First time to encounter this label
1424 peepBlock->target.vars[labindex] = PCL(PCI(pcs)->label->pc)->label;
1425 //DFPRINTF((stderr,"first time for a label: %d %s\n",labindex, peepBlock->vars[labindex]));
1427 if(strcmp(peepBlock->target.vars[labindex],PCL(PCI(pcs)->label->pc)->label) != 0) {
1428 // DFPRINTF((stderr,"labels don't match\n"));
1431 //DFPRINTF((stderr,"matched a label\n"));
1434 // DFPRINTF((stderr,"destination doesn't have a label\n"));
1444 /*-----------------------------------------------------------------*/
1445 /* pCodePeepMatchLine - Compare source and destination pCodes to */
1446 /* see they're the same. */
1448 /* In this context, "source" refers to the coded generated by gen.c*/
1449 /* and "destination" refers to a pcode in a peep rule. If the dest-*/
1450 /* ination has no wild cards, then MatchLine will compare the two */
1451 /* pcodes (src and dest) for a one-to-one match. If the destination*/
1452 /* has wildcards, then those get expanded. When a wild card is */
1453 /* encountered for the first time it autmatically is considered a */
1454 /* match and the object that matches it is referenced in the */
1455 /* variables or opcodes array (depending on the type of match). */
1459 /* *peepBlock - A pointer to the peepBlock that contains the */
1460 /* entire rule to which the destination pcode belongs*/
1461 /* *pcs - a pointer to the source pcode */
1462 /* *pcd - a pointer to the destination pcode */
1465 /* 1 - pcodes match */
1466 /* 0 - pcodes don't match */
1469 /*-----------------------------------------------------------------*/
1471 int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
1473 int index; // index into wild card arrays
1475 /* one-for-one match. Here the source and destination opcodes
1476 * are not wild. However, there may be a label or a wild operand */
1478 if(pcs->type == pcd->type) {
1480 if(pcs->type == PC_OPCODE) {
1482 /* If the opcodes don't match then the line doesn't match */
1483 if(PCI(pcs)->op != PCI(pcd)->op)
1487 DFPRINTF((stderr,"%s comparing\n",__FUNCTION__));
1488 pcs->print(stderr,pcs);
1489 pcd->print(stderr,pcd);
1492 if(!pCodePeepMatchLabels(peepBlock, pcs, pcd))
1495 /* Compare the operands */
1496 if(PCI(pcd)->pcop) {
1497 if (PCI(pcd)->pcop->type == PO_WILD) {
1498 index = PCOW(PCI(pcd)->pcop)->id;
1500 //DFPRINTF((stderr,"destination is wild\n"));
1501 #ifdef DEBUG_PCODEPEEP
1502 if (index > peepBlock->nops) {
1503 DFPRINTF((stderr,"%s - variables exceeded\n",__FUNCTION__));
1507 PCOW(PCI(pcd)->pcop)->matched = PCI(pcs)->pcop;
1508 if(!peepBlock->target.wildpCodeOps[index]) {
1509 peepBlock->target.wildpCodeOps[index] = PCI(pcs)->pcop;
1511 //if(PCI(pcs)->pcop->type == PO_GPR_TEMP)
1517 switch(PCI(pcs)->pcop->type) {
1521 n = PCOR(PCI(pcs)->pcop)->r->name;
1525 n = PCI(pcs)->pcop->name;
1528 if(peepBlock->target.vars[index])
1529 return (strcmp(peepBlock->target.vars[index],n) == 0);
1531 // DFPRINTF((stderr,"first time for a variable: %d, %s\n",index,n));
1532 peepBlock->target.vars[index] = n;
1537 /* FIXME - need an else to check the case when the destination
1538 * isn't a wild card */
1540 /* The pcd has no operand. Lines match if pcs has no operand either*/
1541 return (PCI(pcs)->pcop == NULL);
1545 /* Compare a wild instruction to a regular one. */
1547 if((pcd->type == PC_WILD) && (pcs->type == PC_OPCODE)) {
1550 index = PCW(pcd)->id;
1552 // DFPRINTF((stderr,"%s comparing wild cards\n",__FUNCTION__));
1553 //pcs->print(stderr,pcs);
1554 //pcd->print(stderr,pcd);
1556 peepBlock->target.wildpCodes[PCW(pcd)->id] = pcs;
1558 if(!pCodePeepMatchLabels(peepBlock, pcs, pcd))
1561 if(PCW(pcd)->mustBeBitSkipInst & !(PCI(pcs)->isBitInst && PCI(pcs)->isSkip)) {
1562 // doesn't match because the wild pcode must be a bit skip
1563 //fprintf(stderr," Failing match because bit skip is req:\n");
1564 //pcd->print(stderr,pcd);
1565 //pcs->print(stderr,pcs);
1569 if(PCW(pcd)->mustNotBeBitSkipInst & (PCI(pcs)->isBitInst && PCI(pcs)->isSkip)) {
1570 // doesn't match because the wild pcode must *not* be a bit skip
1571 //fprintf(stderr," Failing match because don't want skip :\n");
1572 //pcd->print(stderr,pcd);
1573 //pcs->print(stderr,pcs);
1577 if(PCW(pcd)->operand) {
1578 PCOW(PCI(pcd)->pcop)->matched = PCI(pcs)->pcop;
1579 if(peepBlock->target.vars[index]) {
1580 int i = (strcmp(peepBlock->target.vars[index],PCI(pcs)->pcop->name) == 0);
1583 DFPRINTF((stderr," (matched)\n"));
1585 DFPRINTF((stderr," (no match: wild card operand mismatch\n"));
1586 DFPRINTF((stderr," peepblock= %s, pcodeop= %s\n"),
1587 peepBlock->vars[index],
1588 PCI(pcs)->pcop->name);
1593 peepBlock->target.vars[index] = PCI(pcs)->pcop->name;
1598 pcs = findNextInstruction(pcs->next);
1600 //DFPRINTF((stderr," (next to match)\n"));
1601 //pcs->print(stderr,pcs);
1602 } else if(pcd->next) {
1603 /* oops, we ran out of code, but there's more to the rule */
1607 return 1; /* wild card matches */
1613 /*-----------------------------------------------------------------*/
1614 /*-----------------------------------------------------------------*/
1615 void pCodePeepClrVars(pCodePeep *pcp)
1622 for(i=0;i<pcp->target.nvars; i++) {
1623 pcp->target.vars[i] = NULL;
1624 pcp->target.wildpCodeOps[i] = NULL;
1628 /*-----------------------------------------------------------------*/
1629 /* pCodeInsertAfter - splice in the pCode chain starting with pc2 */
1630 /* into the pCode chain containing pc1 */
1631 /*-----------------------------------------------------------------*/
1632 void pCodeInsertAfter(pCode *pc1, pCode *pc2)
1638 pc2->next = pc1->next;
1640 pc1->next->prev = pc2;
1648 /*-----------------------------------------------------------------*/
1649 /* pCodeOpCopy - copy a pcode operator */
1650 /*-----------------------------------------------------------------*/
1651 pCodeOp *pCodeOpCopy(pCodeOp *pcop)
1653 pCodeOp *pcopnew=NULL;
1658 switch(pcop->type) {
1661 //DFPRINTF((stderr,"pCodeOpCopy bit\n"));
1662 pcopnew = Safe_calloc(1,sizeof(pCodeOpRegBit) );
1663 PCORB(pcopnew)->bit = PCORB(pcop)->bit;
1664 PCORB(pcopnew)->inBitSpace = PCORB(pcop)->inBitSpace;
1669 /* Here we expand the wild card into the appropriate type: */
1670 /* By recursively calling pCodeOpCopy */
1671 //DFPRINTF((stderr,"pCodeOpCopy wild\n"));
1672 if(PCOW(pcop)->matched)
1673 pcopnew = pCodeOpCopy(PCOW(pcop)->matched);
1676 pcopnew = pCodeOpCopy(PCOW(pcop)->subtype);
1677 pcopnew->name = Safe_strdup(PCOW(pcop)->pcwb->vars[PCOW(pcop)->id]);
1678 //DFPRINTF((stderr,"copied a wild op named %s\n",pcopnew->name));
1685 //DFPRINTF((stderr,"pCodeOpCopy label\n"));
1686 pcopnew = Safe_calloc(1,sizeof(pCodeOpLabel) );
1687 PCOLAB(pcopnew)->key = PCOLAB(pcop)->key;
1691 pcopnew = Safe_calloc(1,sizeof(pCodeOpImmd) );
1692 PCOI(pcopnew)->offset = PCOI(pcop)->offset;
1696 //DFPRINTF((stderr,"pCodeOpCopy lit\n"));
1697 pcopnew = Safe_calloc(1,sizeof(pCodeOpLit) );
1698 PCOL(pcopnew)->lit = PCOL(pcop)->lit;
1703 pcopnew = newpCodeOpBit(pcop->name, PCORB(pcop)->bit,PCORB(pcop)->inBitSpace);
1704 PCOR(pcopnew)->r = PCOR(pcop)->r;
1705 PCOR(pcopnew)->rIdx = PCOR(pcop)->rIdx;
1706 DFPRINTF((stderr," pCodeOpCopy Bit -register index\n"));
1710 case PO_GPR_REGISTER:
1714 //DFPRINTF((stderr,"pCodeOpCopy GPR register\n"));
1715 pcopnew = Safe_calloc(1,sizeof(pCodeOpReg) );
1716 PCOR(pcopnew)->r = PCOR(pcop)->r;
1717 PCOR(pcopnew)->rIdx = PCOR(pcop)->rIdx;
1718 DFPRINTF((stderr," register index %d\n", PCOR(pcop)->r->rIdx));
1722 fprintf(stderr,"pCodeOpCopy PO_DIR\n");
1723 pcopnew = Safe_calloc(1,sizeof(pCodeOpReg) );
1726 DFPRINTF((stderr,"pCodeOpCopy PO_STATUS\n"));
1727 case PO_SFR_REGISTER:
1735 //DFPRINTF((stderr,"pCodeOpCopy register type %d\n", pcop->type));
1736 pcopnew = Safe_calloc(1,sizeof(pCodeOp) );
1740 pcopnew->type = pcop->type;
1742 pcopnew->name = Safe_strdup(pcop->name);
1744 pcopnew->name = NULL;
1750 /*-----------------------------------------------------------------*/
1751 /* pCodeCopy - copy a pcode */
1752 /*-----------------------------------------------------------------*/
1753 static pCode *pCodeInstructionCopy(pCodeInstruction *pci,int invert)
1755 pCodeInstruction *new_pci;
1758 new_pci = PCI(newpCode(pci->inverted_op,pci->pcop));
1760 new_pci = PCI(newpCode(pci->op,pci->pcop));
1762 new_pci->pc.pb = pci->pc.pb;
1763 new_pci->from = pci->from;
1764 new_pci->to = pci->to;
1765 new_pci->label = pci->label;
1766 new_pci->pcflow = pci->pcflow;
1768 return PCODE(new_pci);
1771 /*-----------------------------------------------------------------*/
1772 /*-----------------------------------------------------------------*/
1773 void pCodeDeleteChain(pCode *f,pCode *t)
1779 DFPRINTF((stderr,"delete pCode:\n"));
1782 //f->delete(f); this dumps core...
1789 /*-----------------------------------------------------------------*/
1790 /*-----------------------------------------------------------------*/
1791 int pCodePeepMatchRule(pCode *pc)
1793 pCodePeep *peepBlock;
1798 peeprules = (_DLL *)peepSnippets;
1801 peepBlock = ((pCodePeepSnippets*)peeprules)->peep;
1803 if(!peepBlock || /*!peepBlock->target ||*/ !peepBlock->target.pb->pcHead) {
1804 fprintf(stderr, "skipping rule because target pb is NULL\n");
1808 pCodePeepClrVars(peepBlock);
1811 if(IS_PCCOMMENT(pcin))
1812 pc = pcin = findNextInstruction(pcin->next);
1814 pct = peepBlock->target.pb->pcHead;
1816 while(pct && pcin) {
1818 if(! (matched = pCodePeepMatchLine(peepBlock, pcin,pct)))
1821 pcin = findNextInstruction(pcin->next);
1824 //DFPRINTF((stderr," matched\n"));
1826 DFPRINTF((stderr," end of code\n"));
1828 DFPRINTF((stderr," end of rule\n"));
1833 /* So far we matched the rule up to the point of the conditions .
1834 * In other words, all of the opcodes match. Now we need to see
1835 * if the post conditions are satisfied.
1836 * First we check the 'postFalseCond'. This means that we check
1837 * to see if any of the subsequent pCode's in the pCode chain
1838 * following the point just past where we have matched depend on
1839 * the `postFalseCond' as input then we abort the match
1841 DFPRINTF((stderr," matched rule so far, now checking conditions\n"));
1842 if (pcin && peepBlock->postFalseCond &&
1843 (pCodeSearchCondition(pcin,peepBlock->postFalseCond) > 0) )
1853 /* We matched a rule! Now we have to go through and remove the
1854 inefficient code with the optimized version */
1856 DFPRINTF((stderr, "Found a pcode peep match:\nRule:\n"));
1857 printpCodeString(stderr,peepBlock->target.pb->pcHead,10);
1858 DFPRINTF((stderr,"first thing matched\n"));
1859 pc->print(stderr,pc);
1862 DFPRINTF((stderr,"last thing matched\n"));
1863 pcin->print(stderr,pcin);
1866 /* Unlink the original code */
1868 pcprev->next = pcin;
1870 pcin->prev = pc->prev;
1874 /* Converted the deleted pCodes into comments */
1881 while(pc && pc!=pcin) {
1882 pCode2str(&buf[2], 254, pc);
1883 pCodeInsertAfter(pcprev, newpCodeCharP(buf));
1884 pcprev = pcprev->next;
1890 pCodeDeleteChain(pc,pcin);
1892 /* Generate the replacement code */
1894 pcr = peepBlock->replace.pb->pcHead; // This is the replacement code
1897 /* If the replace pcode is an instruction with an operand, */
1898 /* then duplicate the operand (and expand wild cards in the process). */
1899 if(pcr->type == PC_OPCODE) {
1900 if(PCI(pcr)->pcop) {
1901 /* The replacing instruction has an operand.
1903 if(PCI(pcr)->pcop->type == PO_WILD) {
1904 int index = PCOW(PCI(pcr)->pcop)->id;
1905 //DFPRINTF((stderr,"copying wildopcode\n"));
1906 if(peepBlock->target.wildpCodeOps[index])
1907 pcop = pCodeOpCopy(peepBlock->target.wildpCodeOps[index]);
1909 DFPRINTF((stderr,"error, wildopcode in replace but not source?\n"));
1911 pcop = pCodeOpCopy(PCI(pcr)->pcop);
1913 //DFPRINTF((stderr,"inserting pCode\n"));
1914 pCodeInsertAfter(pc, newpCode(PCI(pcr)->op,pcop));
1915 } else if (pcr->type == PC_WILD) {
1916 if(PCW(pcr)->invertBitSkipInst)
1917 DFPRINTF((stderr,"We need to invert the bit skip instruction\n"));
1918 pCodeInsertAfter(pc,
1919 pCodeInstructionCopy(PCI(peepBlock->target.wildpCodes[PCW(pcr)->id]),
1920 PCW(pcr)->invertBitSkipInst));
1921 } else if (pcr->type == PC_COMMENT) {
1922 pCodeInsertAfter(pc, newpCodeCharP( ((pCodeComment *)(pcr))->comment));
1928 // pc->print(stderr,pc);
1935 peeprules = peeprules->next;