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
28 #include "pcodeflow.h"
31 #if defined(__BORLANDC__) || defined(_MSC_VER)
32 #define STRCASECMP stricmp
34 #define STRCASECMP strcasecmp
37 pCodeOp *popCopyGPR2Bit(pCodeOpReg *pc, int bitval);
38 pCodeOp *popRegFromString(char *str, int size, int offset);
40 pCodeOp *newpCodeOpWild(int id, pCodeWildBlock *pcwb, pCodeOp *subtype);
41 pCode *newpCodeWild(int pCodeID, pCodeOp *optional_operand, pCodeOp *optional_label);
42 pCode * findNextInstruction(pCode *pc);
43 int getpCode(char *mnem,int dest);
44 int getpCodePeepCommand(char *cmd);
45 void pBlockMergeLabels(pBlock *pb);
46 char *pCode2str(char *str, int size, pCode *pc);
47 char *get_op( pCodeOp *pcop,char *buf,int buf_size);
49 extern pCodeInstruction *pic14Mnemonics[];
50 extern pCode * findPrevInstruction(pCode *pci);
53 #define IS_PCCOMMENT(x) ( x && (x->type==PC_COMMENT))
55 /****************************************************************/
57 * rootRules - defined in SDCCpeep.c
58 * This is a pointer to the (parsed) peephole rules that are
59 * defined in peep.def.
62 //extern peepRule *rootRules;
67 /****************************************************************/
68 /****************************************************************/
76 typedef struct pCodePeepSnippets
83 /****************************************************************/
87 /****************************************************************/
89 static pCodePeepSnippets *peepSnippets=NULL;
91 /****************************************************************/
95 /****************************************************************/
97 //static pCodePeep *curPeep=NULL;
99 /****************************************************************/
103 /****************************************************************/
105 //static pBlock *curBlock=NULL;
108 /****************************************************************/
110 /* max wild cards in a peep rule */
112 /****************************************************************/
114 //static int sMaxWildVar = 0;
115 //static int sMaxWildMnem = 0;
118 typedef struct pCodeToken
120 int tt; // token type;
129 pCodeToken tokArr[50];
148 typedef struct parsedPattern {
149 struct pcPattern *pcp;
153 #define MAX_PARSEDPATARR 50
154 parsedPattern parsedPatArr[MAX_PARSEDPATARR];
155 unsigned int parsedPatIdx=0;
168 static char pcpat_label[] = {PCT_PERCENT, PCT_NUMBER, PCT_COLON, 0};
169 static char pcpat_number[] = {PCT_NUMBER, 0};
170 static char pcpat_string[] = {PCT_STRING, 0};
171 static char pcpat_wildString[] = {PCT_PERCENT, PCT_STRING, 0};
172 static char pcpat_wildVar[] = {PCT_PERCENT, PCT_NUMBER, 0};
173 static char pcpat_comma[] = {PCT_COMMA, 0};
174 static char pcpat_comment[] = {PCT_COMMENT, 0};
177 typedef struct pcPattern {
178 char pt; // Pattern type
179 char *tokens; // list of tokens that describe the pattern
180 void * (*f) (void *,pCodeWildBlock *);
183 pcPattern pcpArr[] = {
184 {PCP_LABEL, pcpat_label, NULL},
185 {PCP_WILDSTR, pcpat_wildString, NULL},
186 {PCP_STR, pcpat_string, NULL},
187 {PCP_WILDVAR, pcpat_wildVar, NULL},
188 {PCP_COMMA, pcpat_comma, NULL},
189 {PCP_COMMENT, pcpat_comment, NULL},
190 {PCP_NUMBER, pcpat_number, NULL}
193 #define PCPATTERNS (sizeof(pcpArr)/sizeof(pcPattern))
195 // Assembly Line Token
209 static char alt_comment[] = { PCP_COMMENT, 0};
210 static char alt_label[] = { PCP_LABEL, 0};
211 static char alt_mnem0[] = { PCP_STR, 0};
212 static char alt_mnem0a[] = { PCP_WILDVAR, 0};
213 static char alt_mnem1[] = { PCP_STR, PCP_STR, 0};
214 static char alt_mnem1a[] = { PCP_STR, PCP_WILDVAR, 0};
215 static char alt_mnem1b[] = { PCP_STR, PCP_NUMBER, 0};
216 static char alt_mnem2[] = { PCP_STR, PCP_STR, PCP_COMMA, PCP_STR, 0};
217 static char alt_mnem2a[] = { PCP_STR, PCP_WILDVAR, PCP_COMMA, PCP_STR, 0};
218 static char alt_mnem3[] = { PCP_STR, PCP_STR, PCP_COMMA, PCP_NUMBER, 0};
220 static void * cvt_altpat_label(void *pp,pCodeWildBlock *pcwb);
221 static void * cvt_altpat_comment(void *pp,pCodeWildBlock *pcwb);
222 static void * cvt_altpat_mnem0(void *pp,pCodeWildBlock *pcwb);
223 static void * cvt_altpat_mnem0a(void *pp,pCodeWildBlock *pcwb);
224 static void * cvt_altpat_mnem1(void *pp,pCodeWildBlock *pcwb);
225 static void * cvt_altpat_mnem1a(void *pp,pCodeWildBlock *pcwb);
226 static void * cvt_altpat_mnem1b(void *pp,pCodeWildBlock *pcwb);
227 static void * cvt_altpat_mnem2(void *pp,pCodeWildBlock *pcwb);
228 static void * cvt_altpat_mnem2a(void *pp,pCodeWildBlock *pcwb);
229 static void * cvt_altpat_mnem3(void *pp,pCodeWildBlock *pcwb);
231 pcPattern altArr[] = {
232 {ALT_LABEL, alt_label, cvt_altpat_label},
233 {ALT_COMMENT, alt_comment,cvt_altpat_comment},
234 {ALT_MNEM3, alt_mnem3, cvt_altpat_mnem3},
235 {ALT_MNEM2A, alt_mnem2a, cvt_altpat_mnem2a},
236 {ALT_MNEM2, alt_mnem2, cvt_altpat_mnem2},
237 {ALT_MNEM1B, alt_mnem1b, cvt_altpat_mnem1b},
238 {ALT_MNEM1A, alt_mnem1a, cvt_altpat_mnem1a},
239 {ALT_MNEM1, alt_mnem1, cvt_altpat_mnem1},
240 {ALT_MNEM0A, alt_mnem0a, cvt_altpat_mnem0a},
241 {ALT_MNEM0, alt_mnem0, cvt_altpat_mnem0},
245 #define ALTPATTERNS (sizeof(altArr)/sizeof(pcPattern))
247 // forward declarations
248 static void * DLL_append(_DLL *list, _DLL *next);
250 /*-----------------------------------------------------------------*/
251 /* cvt_extract_destination - helper function extracts the register */
252 /* destination from a parsedPattern. */
254 /*-----------------------------------------------------------------*/
255 static int cvt_extract_destination(parsedPattern *pp)
258 if(pp->pct[0].tt == PCT_STRING) {
260 // just check first letter for now
262 if(toupper((unsigned char)*pp->pct[0].tok.s) == 'F')
265 } else if (pp->pct[0].tt == PCT_NUMBER) {
275 /*-----------------------------------------------------------------*/
276 /* pCodeOp *cvt_extract_status(char *reg, char *bit) */
277 /* if *reg is the "status" register and *bit is one of the */
278 /* status bits, then this function will create a new pCode op */
279 /* containing the status register. */
280 /*-----------------------------------------------------------------*/
282 static pCodeOp *cvt_extract_status(char *reg, char *bit)
286 if(STRCASECMP(reg, pc_status.pcop.name))
293 if(toupper((unsigned char)*bit) == 'C')
294 return PCOP(popCopyGPR2Bit(&pc_status,PIC_C_BIT));
295 if(toupper((unsigned char)*bit) == 'Z')
296 return PCOP(popCopyGPR2Bit(&pc_status,PIC_Z_BIT));
300 if(len ==2 && toupper((unsigned char)bit[0]) == 'D' && toupper((unsigned char)bit[1]) == 'C')
301 return PCOP(popCopyGPR2Bit(&pc_status,PIC_DC_BIT));
307 /*-----------------------------------------------------------------*/
308 /* cvt_altpat_label - convert assembly line type to a pCode label */
309 /* INPUT: pointer to the parsedPattern */
313 /* label pattern => '%' number ':' */
314 /* at this point, we wish to extract only the 'number' */
316 /*-----------------------------------------------------------------*/
317 static void * cvt_altpat_label(void *pp,pCodeWildBlock *pcwb)
319 parsedPattern *p = pp;
321 DFPRINTF((stderr,"altpat_label with ID = %d\n",p->pct[1].tok.n));
322 return newpCodeLabel(NULL,-p->pct[1].tok.n);
326 /*-----------------------------------------------------------------*/
327 /* cvt_altpat_comment - convert assembly line type to a comment */
328 /* INPUT: pointer to the parsedPattern */
330 /* pp[0] - comment */
333 /*-----------------------------------------------------------------*/
334 static void * cvt_altpat_comment(void *pp,pCodeWildBlock *pcwb)
336 parsedPattern *p = pp;
338 DFPRINTF((stderr,"altpat_comment = %s\n",p->pct[0].tok.s));
339 return newpCodeCharP(p->pct[0].tok.s);
343 /*-----------------------------------------------------------------*/
344 /* cvt_altpat_mem0 - convert assembly line type to a wild pCode */
349 /*-----------------------------------------------------------------*/
350 static void * cvt_altpat_mnem0(void *pp,pCodeWildBlock *pcwb)
352 parsedPattern *p = pp;
355 pCodeInstruction *pci=NULL;
357 DFPRINTF((stderr,"altpat_mnem0 %s\n", p->pct[0].tok.s));
359 opcode = getpCode(p->pct[0].tok.s,0);
362 /* look for special command strings like _NOTBITSKIP_ */
364 //fprintf(stderr, "Bad mnemonic\n");
366 opcode = getpCodePeepCommand(p->pct[0].tok.s);
368 // fprintf(stderr," but valid peep command: %s, key = %d\n",p->pct[0].tok.s,opcode);
372 pci = PCI(newpCode(opcode, NULL));
375 fprintf(stderr,"couldn't find mnemonic\n");
381 /*-----------------------------------------------------------------*/
382 /* cvt_altpat_mem0a - convert assembly line type to a wild pCode */
385 /* pp[0] - wild var */
387 /*-----------------------------------------------------------------*/
388 static void * cvt_altpat_mnem0a(void *pp, pCodeWildBlock *pcwb)
390 parsedPattern *p = pp;
392 DFPRINTF((stderr,"altpat_mnem0a wild mnem # %d\n", p[0].pct[1].tok.n));
394 /* Save the index of the maximum wildcard mnemonic */
396 //if(p[0].pct[1].tok.n > sMaxWildVar)
397 // sMaxWildMnem = p[0].pct[1].tok.n;
399 if(p[0].pct[1].tok.n > pcwb->nwildpCodes)
400 pcwb->nwildpCodes = p[0].pct[1].tok.n;
402 return newpCodeWild(p[0].pct[1].tok.n,NULL,NULL);
406 /*-----------------------------------------------------------------*/
407 /* cvt_altpat_mem1 - convert assembly line type to a pCode */
408 /* instruction with 1 operand. */
411 /* pp[1] - Operand */
413 /*-----------------------------------------------------------------*/
414 static void * cvt_altpat_mnem1(void *pp,pCodeWildBlock *pcwb)
417 parsedPattern *p = pp;
420 pCodeInstruction *pci=NULL;
423 DFPRINTF((stderr,"altpat_mnem1 %s var %s\n", p->pct[0].tok.s,p[1].pct[0].tok.s));
425 opcode = getpCode(p->pct[0].tok.s,0);
427 //fprintf(stderr, "Bad mnemonic\n");
428 opcode = getpCodePeepCommand(p->pct[0].tok.s);
430 //fprintf(stderr," but valid peep command: %s, key = %d\n",p->pct[0].tok.s,opcode);
435 if(pic14Mnemonics[opcode]->isBitInst)
436 pcosubtype = newpCodeOp(p[1].pct[0].tok.s,PO_BIT);
438 pcosubtype = newpCodeOp(p[1].pct[0].tok.s,PO_GPR_REGISTER);
441 pci = PCI(newpCode(opcode, pcosubtype));
444 fprintf(stderr,"couldn't find mnemonic\n");
450 /*-----------------------------------------------------------------*/
451 /* cvt_altpat_mem1a - convert assembly line type to a pCode */
452 /* instruction with 1 wild operand. */
455 /* pp[1] - wild var */
457 /*-----------------------------------------------------------------*/
458 static void * cvt_altpat_mnem1a(void *pp,pCodeWildBlock *pcwb)
460 parsedPattern *p = pp;
463 pCodeInstruction *pci=NULL;
466 DFPRINTF((stderr,"altpat_mnem1a %s var %d\n", p->pct[0].tok.s,p[1].pct[1].tok.n));
468 opcode = getpCode(p->pct[0].tok.s,0);
470 int cmd_id = getpCodePeepCommand(p->pct[0].tok.s);
474 fprintf(stderr, "Bad mnemonic\n");
478 if(p[0].pct[1].tok.n > pcwb->nwildpCodes)
479 pcwb->nwildpCodes = p[0].pct[1].tok.n;
481 pc = newpCodeWild(p[1].pct[1].tok.n,NULL,NULL);
485 PCW(pc)->mustNotBeBitSkipInst = 1;
488 PCW(pc)->mustBeBitSkipInst = 1;
491 PCW(pc)->invertBitSkipInst = 1;
496 if(pic14Mnemonics[opcode]->isBitInst)
497 pcosubtype = newpCodeOpBit(NULL,-1,0);
499 pcosubtype = newpCodeOp(NULL,PO_GPR_REGISTER);
502 pci = PCI(newpCode(opcode,
503 newpCodeOpWild(p[1].pct[1].tok.n, pcwb, pcosubtype)));
505 /* Save the index of the maximum wildcard variable */
506 //if(p[1].pct[1].tok.n > sMaxWildVar)
507 // sMaxWildVar = p[1].pct[1].tok.n;
509 if(p[1].pct[1].tok.n > pcwb->nvars)
510 pcwb->nvars = p[1].pct[1].tok.n;
513 fprintf(stderr,"couldn't find mnemonic\n");
519 /*-----------------------------------------------------------------*/
520 /*-----------------------------------------------------------------*/
521 static void * cvt_altpat_mnem1b(void *pp,pCodeWildBlock *pcwb)
523 parsedPattern *p = pp;
526 pCodeInstruction *pci=NULL;
528 DFPRINTF((stderr,"altpat_mnem1b %s var %d\n", p->pct[0].tok.s,p[1].pct[0].tok.n));
530 opcode = getpCode(p->pct[0].tok.s,0);
532 fprintf(stderr, "Bad mnemonic\n");
536 pci = PCI(newpCode(opcode, newpCodeOpLit(p[1].pct[0].tok.n) ));
539 fprintf(stderr,"couldn't find mnemonic\n");
545 /*-----------------------------------------------------------------*/
546 /* cvt_altpat_mnem2 */
551 /* pp[3] - destination */
552 /*-----------------------------------------------------------------*/
553 static void * cvt_altpat_mnem2(void *pp,pCodeWildBlock *pcwb)
555 parsedPattern *p = pp;
559 pCodeInstruction *pci=NULL;
562 dest = cvt_extract_destination(&p[3]);
564 DFPRINTF((stderr,"altpat_mnem2 %s var %s destination %s(%d)\n",
571 opcode = getpCode(p->pct[0].tok.s,dest);
573 fprintf(stderr, "Bad mnemonic\n");
577 if(pic14Mnemonics[opcode]->isBitInst) {
578 pcosubtype = cvt_extract_status(p[1].pct[0].tok.s, p[3].pct[0].tok.s);
579 if(pcosubtype == NULL) {
580 fprintf(stderr, "bad operand?\n");
585 pcosubtype = newpCodeOp(p[1].pct[0].tok.s,PO_GPR_REGISTER);
588 pci = PCI(newpCode(opcode,pcosubtype));
591 fprintf(stderr,"couldn't find mnemonic\n");
597 /*-----------------------------------------------------------------*/
598 /* cvt_altpat_mem2a - convert assembly line type to a pCode */
599 /* instruction with 1 wild operand and a */
600 /* destination operand (e.g. w or f) */
603 /* pp[1] - wild var */
605 /* pp[3] - destination */
607 /*-----------------------------------------------------------------*/
608 static void * cvt_altpat_mnem2a(void *pp,pCodeWildBlock *pcwb)
610 parsedPattern *p = pp;
614 pCodeInstruction *pci=NULL;
618 fprintf(stderr,"ERROR %s:%d - can't assemble line\n",__FILE__,__LINE__);
622 dest = cvt_extract_destination(&p[3]);
624 DFPRINTF((stderr,"altpat_mnem2a %s var %d destination %s(%d)\n",
631 opcode = getpCode(p->pct[0].tok.s,dest);
633 fprintf(stderr, "Bad mnemonic\n");
637 if(pic14Mnemonics[opcode]->isBitInst)
638 pcosubtype = newpCodeOp(NULL,PO_BIT);
640 pcosubtype = newpCodeOp(NULL,PO_GPR_REGISTER);
643 pci = PCI(newpCode(opcode,
644 newpCodeOpWild(p[1].pct[1].tok.n, pcwb, pcosubtype)));
646 /* Save the index of the maximum wildcard variable */
647 //if(p[1].pct[1].tok.n > sMaxWildVar)
648 // sMaxWildVar = p[1].pct[1].tok.n;
650 if(p[1].pct[1].tok.n > pcwb->nvars)
651 pcwb->nvars = p[1].pct[1].tok.n;
654 fprintf(stderr,"couldn't find mnemonic\n");
661 /*-----------------------------------------------------------------*/
662 /* cvt_altpat_mem3 - convert assembly line type to a pCode */
663 /* This rule is for bsf/bcf type instructions */
667 /* pp[1] - register */
671 /*-----------------------------------------------------------------*/
672 static void * cvt_altpat_mnem3(void *pp,pCodeWildBlock *pcwb)
674 parsedPattern *p = pp;
676 int dest; // or could be bit position in the register
678 pCodeInstruction *pci=NULL;
679 pCodeOp *pcosubtype=NULL;
681 dest = cvt_extract_destination(&p[3]);
683 DFPRINTF((stderr,"altpat_mnem3 %s var %s bit (%d)\n",
689 opcode = getpCode(p->pct[0].tok.s,0);
691 fprintf(stderr, "Bad mnemonic\n");
696 if(pic14Mnemonics[opcode]->isBitInst) {
697 //pcosubtype = cvt_extract_status(p[1].pct[0].tok.s, p[3].pct[0].tok.s);
699 //if(pcosubtype == NULL) {
700 pcosubtype = newpCodeOpBit(p[1].pct[0].tok.s,p[3].pct[0].tok.n,0);
703 pcosubtype = newpCodeOp(p[1].pct[0].tok.s,PO_GPR_REGISTER);
705 if(pcosubtype == NULL) {
706 fprintf(stderr, "Bad operand\n");
710 pci = PCI(newpCode(opcode, pcosubtype));
713 fprintf(stderr,"couldn't find mnemonic\n");
719 /*-----------------------------------------------------------------*/
720 /* tokenizeLineNode - Convert a string (of char's) that was parsed */
721 /* by SDCCpeeph.c into a string of tokens. */
724 /* The tokenizer is of the classic type. When an item is encounterd*/
725 /* it is converted into a token. The token is a structure that */
726 /* encodes the item's type and it's value (when appropriate). */
728 /* Accepted token types: */
729 /* SPACE NUMBER STRING % : , ; */
733 /*-----------------------------------------------------------------*/
736 static void tokenizeLineNode(char *ln)
739 tokIdx = 0; // Starting off at the beginning
740 tokArr[0].tt = PCT_NULL; // and assume invalid character for first token.
748 if(isspace((unsigned char)*ln)) {
749 // add a SPACE token and eat the extra spaces.
750 tokArr[tokIdx++].tt = PCT_SPACE;
751 while (isspace ((unsigned char)*ln))
756 if(isdigit((unsigned char)*ln)) {
758 tokArr[tokIdx].tt = PCT_NUMBER;
759 tokArr[tokIdx++].tok.n = strtol(ln, &ln, 0);
767 tokArr[tokIdx++].tt = PCT_PERCENT;
770 tokArr[tokIdx++].tt = PCT_LESSTHAN;
773 tokArr[tokIdx++].tt = PCT_GREATERTHAN;
776 tokArr[tokIdx++].tt = PCT_COLON;
779 tokArr[tokIdx].tok.s = Safe_strdup(ln);
780 tokArr[tokIdx++].tt = PCT_COMMENT;
781 tokArr[tokIdx].tt = PCT_NULL;
784 tokArr[tokIdx++].tt = PCT_COMMA;
789 if(isalpha((unsigned char)*ln) || (*ln == '_') ) {
793 while( (isalpha((unsigned char)*ln) || isdigit((unsigned char)*ln) || (*ln == '_')) && i<49)
799 tokArr[tokIdx].tok.s = Safe_strdup(buffer);
800 tokArr[tokIdx++].tt = PCT_STRING;
803 fprintf(stderr, "Error while parsing peep rules (check peeph.def)\n");
804 fprintf(stderr, "Line: %s\n",lnstart);
805 fprintf(stderr, "Token: '%c'\n",*ln);
810 /* Advance to next character in input string .
811 * Note, if none of the tests passed above, then
812 * we effectively ignore the `bad' character.
813 * Since the line has already been parsed by SDCCpeeph,
814 * chance are that there are no invalid characters... */
820 tokArr[tokIdx].tt = 0;
824 /*-----------------------------------------------------------------*/
825 /*-----------------------------------------------------------------*/
829 void dump1Token(pCodeTokens tt)
834 fprintf(stderr, " space ");
837 fprintf(stderr, " pct %%");
840 fprintf(stderr, " pct <");
842 case PCT_GREATERTHAN:
843 fprintf(stderr, " pct >");
846 fprintf(stderr, " col :");
849 fprintf(stderr, " comma , ");
852 fprintf(stderr, " comment ");
853 //fprintf(stderr,"%s",tokArr[i].tok.s);
856 fprintf(stderr, " str ");
857 //fprintf(stderr,"%s",tokArr[i].tok.s);
860 fprintf(stderr, " num ");
861 //fprintf(stderr,"%d",tokArr[i].tok.n);
864 fprintf(stderr, " null ");
871 /*-----------------------------------------------------------------*/
872 /*-----------------------------------------------------------------*/
874 int pcComparePattern(pCodeToken *pct, char *pat, int max_tokens)
878 if(!pct || !pat || !*pat)
881 //DFPRINTF((stderr,"comparing against:\n"));
883 while(i < max_tokens) {
886 //DFPRINTF((stderr,"matched\n"));
890 //dump1Token(*pat); DFPRINTF((stderr,"\n"));
904 /*-----------------------------------------------------------------*/
905 /*-----------------------------------------------------------------*/
907 int altComparePattern( char *pct, parsedPattern *pat, int max_tokens)
911 if(!pct || !pat || !*pct)
915 while(i < max_tokens) {
918 //DFPRINTF((stderr,"matched\n"));
922 //dump1Token(*pat); DFPRINTF((stderr,"\n"));
924 if( !pat || !pat->pcp )
927 if (pat->pcp->pt != *pct)
930 //DFPRINTF((stderr," pct=%d\n",*pct));
939 /*-----------------------------------------------------------------*/
940 /*-----------------------------------------------------------------*/
942 int advTokIdx(int *v, int amt)
945 if((unsigned) (*v + amt) > tokIdx)
953 /*-----------------------------------------------------------------*/
954 /* parseTokens - convert the tokens corresponding to a single line */
955 /* of a peep hole assembly into a pCode object. */
960 /* This is a simple parser that looks for strings of the type */
961 /* allowed in the peep hole definition file. Essentially the format*/
962 /* is the same as a line of assembly: */
964 /* label: mnemonic op1, op2, op3 ; comment */
966 /* Some of these items aren't present. It's the job of the parser */
967 /* to determine which are and convert those into the appropriate */
969 /*-----------------------------------------------------------------*/
971 int parseTokens(pCodeWildBlock *pcwb, pCode **pcret)
982 for(i=0; i<=tokIdx; i++)
983 dump1Token(tokArr[i].tt);
996 char * cPmnem = NULL; // Pointer to non-wild mnemonic (if any)
997 char * cP1stop = NULL;
998 char * cP2ndop = NULL;
1000 //pCodeOp *pcl = NULL; // Storage for a label
1001 //pCodeOp *pco1 = NULL; // 1st operand
1002 //pCodeOp *pco2 = NULL; // 2nd operand
1003 //pCode *pc = NULL; // Mnemonic
1014 ParseStates state = PS_START;
1021 if( ((tokArr[ltokIdx].tt == PCT_SPACE) )
1022 && (advTokIdx(<okIdx, 1)) ) // eat space
1026 j = pcComparePattern(&tokArr[ltokIdx], pcpArr[lpcpIdx].tokens, tokIdx +1);
1029 switch(pcpArr[lpcpIdx].pt) {
1031 if(state == PS_START){
1032 DFPRINTF((stderr," label\n"));
1033 state = PS_HAVE_LABEL;
1035 DFPRINTF((stderr," bad state (%d) for label\n",state));
1039 DFPRINTF((stderr," %s is",tokArr[ltokIdx].tok.s));
1043 DFPRINTF((stderr," mnem\n"));
1044 cPmnem = tokArr[ltokIdx].tok.s;
1045 state = PS_HAVE_MNEM;
1048 DFPRINTF((stderr," 1st operand\n"));
1049 cP1stop = tokArr[ltokIdx].tok.s;
1050 //pco1 = newpCodeOp(NULL,PO_GPR_REGISTER);
1051 state = PS_HAVE_1OPERAND;
1053 case PS_HAVE_1OPERAND:
1054 DFPRINTF((stderr," error expecting comma\n"));
1057 DFPRINTF((stderr," 2 operands\n"));
1058 cP2ndop = tokArr[ltokIdx].tok.s;
1060 case PS_HAVE_2OPERANDS:
1069 DFPRINTF((stderr," wild mnem\n"));
1070 state = PS_HAVE_MNEM;
1073 DFPRINTF((stderr," 1st operand is wild\n"));
1074 state = PS_HAVE_1OPERAND;
1076 case PS_HAVE_1OPERAND:
1077 DFPRINTF((stderr," error expecting comma\n"));
1080 DFPRINTF((stderr," 2nd operand is wild\n"));
1082 case PS_HAVE_2OPERANDS:
1091 fprintf(stderr," ERROR number\n");
1094 DFPRINTF((stderr," 1st operand is a number\n"));
1095 state = PS_HAVE_1OPERAND;
1097 case PS_HAVE_1OPERAND:
1098 fprintf(stderr," error expecting comma\n");
1101 DFPRINTF((stderr," 2nd operand is a number\n"));
1103 case PS_HAVE_2OPERANDS:
1111 if(state == PS_HAVE_1OPERAND){
1112 DFPRINTF((stderr," got a comma\n"));
1113 state = PS_HAVE_COMMA;
1115 fprintf(stderr," unexpected comma\n");
1121 parsedPatArr[lparsedPatIdx].pcp = &pcpArr[lpcpIdx];
1122 parsedPatArr[lparsedPatIdx].pct = &tokArr[ltokIdx];
1125 //dump1Token(tokArr[ltokIdx].tt);
1127 if(advTokIdx(<okIdx, strlen(pcpArr[lpcpIdx].tokens) ) ) {
1128 DFPRINTF((stderr," reached end \n"));
1135 } while ((++lpcpIdx < PCPATTERNS) && !matching);
1139 parsedPatArr[lparsedPatIdx].pcp = NULL;
1140 parsedPatArr[lparsedPatIdx].pct = NULL;
1146 if( (c=altComparePattern( altArr[k].tokens, &parsedPatArr[j],10) ) ) {
1149 pc = altArr[k].f(&parsedPatArr[j],pcwb);
1150 //if(pc && pc->print)
1151 // pc->print(stderr,pc);
1152 //if(pc && pc->destruct) pc->destruct(pc); dumps core?
1154 //if(curBlock && pc)
1155 //addpCode2pBlock(curBlock, pc);
1159 return 0; // Only accept one line for now.
1161 addpCode2pBlock(pcwb->pb, pc);
1169 while(j<=lparsedPatIdx && k<ALTPATTERNS);
1172 DFPRINTF((stderr,"\nConverting parsed line to pCode:\n\n"));
1176 if(parsedPatArr[j].pcp && parsedPatArr[j].pcp->f )
1177 parsedPatArr[j].pcp->f(&parsedPatArr[j]);
1178 DFPRINTF((stderr," %d",parsedPatArr[j].pcp->pt));
1181 while(j<lparsedPatIdx);
1183 DFPRINTF((stderr,"\n"));
1190 /*-----------------------------------------------------------------*/
1192 /*-----------------------------------------------------------------*/
1193 void peepRuleBlock2pCodeBlock( lineNode *ln, pCodeWildBlock *pcwb)
1199 for( ; ln; ln = ln->next) {
1201 //DFPRINTF((stderr,"%s\n",ln->line));
1203 tokenizeLineNode(ln->line);
1205 if(parseTokens(pcwb,NULL)) {
1207 fprintf(stderr,"ERROR assembling line:\n%s\n",ln->line);
1208 fprintf(stderr,"Tokens:\n");
1210 dump1Token(tokArr[i].tt);
1217 /*-----------------------------------------------------------------*/
1219 /*-----------------------------------------------------------------*/
1220 pCode *AssembleLine(char *line)
1224 if(!line || !*line) {
1225 fprintf(stderr,"WARNING returning NULL in AssembleLine\n");
1229 tokenizeLineNode(line);
1231 if(parseTokens(NULL,&pc))
1232 fprintf(stderr, "WARNING: unable to assemble line:\n%s\n",line);
1238 /*-----------------------------------------------------------------*/
1239 /* peepRuleCondition */
1240 /*-----------------------------------------------------------------*/
1241 static void peepRuleCondition(char *cond, pCodePeep *pcp)
1246 //DFPRINTF((stderr,"\nCondition: %s\n",cond));
1247 /* brute force compares for now */
1249 if(STRCASECMP(cond, "NZ") == 0) {
1250 //DFPRINTF((stderr,"found NZ\n"));
1251 pcp->postFalseCond = PCC_Z;
1258 void initpCodeWildBlock(pCodeWildBlock *pcwb)
1261 // pcwb = Safe_calloc(1,sizeof(pCodeWildBlock));
1267 pcwb->wildpCodes = NULL;
1268 pcwb->wildpCodeOps = NULL;
1271 pcwb->nwildpCodes = 0;
1276 void postinit_pCodeWildBlock(pCodeWildBlock *pcwb)
1283 pcwb->nops = pcwb->nvars;
1285 pcwb->vars = Safe_calloc(pcwb->nvars, sizeof(char *));
1286 pcwb->wildpCodeOps = Safe_calloc(pcwb->nvars, sizeof(pCodeOp *));
1288 pcwb->nwildpCodes+=2;
1289 pcwb->wildpCodes = Safe_calloc(pcwb->nwildpCodes, sizeof(pCode *));
1293 void initpCodePeep(pCodePeep *pcp)
1296 // pcwb = Safe_calloc(1,sizeof(pCodeWildBlock));
1301 initpCodeWildBlock(&pcp->target);
1302 pcp->target.pb = newpCodeChain(NULL, 'W', NULL);
1304 initpCodeWildBlock(&pcp->replace);
1305 pcp->replace.pb = newpCodeChain(NULL, 'W', NULL);
1309 /*-----------------------------------------------------------------*/
1310 /* peepRules2pCode - parse the "parsed" peep hole rules to generate*/
1313 /* SDCCpeeph parses the peep rules file and extracts variables, */
1314 /* removes white space, and checks the syntax. This function */
1315 /* extends that processing to produce pCode objects. You can kind */
1316 /* think of this function as an "assembler", though instead of */
1317 /* taking raw text to produce machine code, it produces pCode. */
1319 /*-----------------------------------------------------------------*/
1320 extern void pic14initpCodePeepCommands(void);
1322 void peepRules2pCode(peepRule *rules)
1326 pCodePeep *currentRule;
1327 pCodePeepSnippets *pcps;
1329 pic14initpCodePeepCommands();
1331 /* The rules are in a linked-list. Each rule has two portions */
1332 /* There's the `target' and there's the `replace'. The target */
1333 /* is compared against the SDCC generated code and if it */
1334 /* matches, it gets replaced by the `replace' block of code. */
1336 /* Here we loop through each rule and convert the target's and*/
1337 /* replace's into pCode target and replace blocks */
1339 for (pr = rules; pr; pr = pr->next) {
1341 //DFPRINTF((stderr,"\nRule:\n\n"));
1343 pcps = Safe_calloc(1,sizeof(pCodePeepSnippets));
1344 peepSnippets = DLL_append((_DLL*)peepSnippets,(_DLL*)pcps);
1346 currentRule = pcps->peep = Safe_calloc(1,sizeof(pCodePeep));
1347 initpCodePeep(currentRule);
1349 /* Convert the target block */
1350 peepRuleBlock2pCodeBlock(pr->match, ¤tRule->target);
1352 //DFPRINTF((stderr,"finished target, here it is in pcode form:\n"));
1353 //printpBlock(stderr, currentRule->target.pb);
1355 //DFPRINTF((stderr,"target with labels merged:\n"));
1356 //pBlockMergeLabels(curBlock);
1357 pBlockMergeLabels(currentRule->target.pb);
1358 //printpBlock(stderr, currentRule->replace.pb);
1360 //#ifdef PCODE_DEBUG
1361 // printpBlock(stderr, curBlock);
1363 //DFPRINTF((stderr,"\nReplaced by:\n"));
1366 /* Convert the replace block */
1367 peepRuleBlock2pCodeBlock(pr->replace, ¤tRule->replace);
1369 //DFPRINTF((stderr,"finished replace block, here it is in pcode form:\n"));
1370 //printpBlock(stderr, curBlock);
1372 //DFPRINTF((stderr,"replace with labels merged:\n"));
1374 pBlockMergeLabels(currentRule->replace.pb);
1375 //printpBlock(stderr, currentRule->replace.pb);
1377 peepRuleCondition(pr->cond,currentRule);
1379 /* The rule has been converted to pCode. Now allocate
1380 * space for the wildcards */
1382 postinit_pCodeWildBlock(¤tRule->target);
1383 postinit_pCodeWildBlock(¤tRule->replace);
1385 //return; // debug ... don't want to go through all the rules yet
1389 pCodePeep *peepBlock;
1392 peeprules = (_DLL *)peepSnippets;
1393 //fprintf(stderr,"target rules\n");
1395 //fprintf(stderr," rule:\n");
1396 peepBlock = ((pCodePeepSnippets*)peeprules)->peep;
1397 //printpBlock(stderr, peepBlock->target.pb);
1398 peeprules = peeprules->next;
1400 //fprintf(stderr," ... done\n");
1405 void printpCodeString(FILE *of, pCode *pc, int max)
1409 while(pc && (i++<max)) {
1415 /*-----------------------------------------------------------------*/
1416 /* _DLL * DLL_append */
1418 /* Append a _DLL object to the end of a _DLL (doubly linked list) */
1419 /* If The list to which we want to append is non-existant then one */
1420 /* is created. Other wise, the end of the list is sought out and */
1421 /* a new DLL object is appended to it. In either case, the void */
1422 /* *data is added to the newly created DLL object. */
1423 /*-----------------------------------------------------------------*/
1425 static void * DLL_append(_DLL *list, _DLL *next)
1430 /* If there's no list, then create one: */
1432 next->next = next->prev = NULL;
1437 /* Search for the end of the list. */
1442 /* Now append the new DLL object */
1453 /*-----------------------------------------------------------------
1455 pCode peephole optimization
1458 The pCode "peep hole" optimization is not too unlike the peep hole
1459 optimization in SDCCpeeph.c. The major difference is that here we
1460 use pCode's whereas there we use ASCII strings. The advantage with
1461 pCode's is that we can ascertain flow information in the instructions
1465 <FIX ME> - elaborate...
1467 -----------------------------------------------------------------*/
1471 /*-----------------------------------------------------------------*/
1472 /* pCodeSearchCondition - Search a pCode chain for a 'condition' */
1474 /* return conditions */
1475 /* 1 - The Condition was found for a pCode's input */
1476 /* 0 - No matching condition was found for the whole chain */
1477 /* -1 - The Condition was found for a pCode's output */
1479 /*-----------------------------------------------------------------*/
1480 int pCodeSearchCondition(pCode *pc, unsigned int cond, int contIfSkip)
1484 /* If we reach a function end (presumably an end since we most
1485 probably began the search in the middle of a function), then
1486 the condition was not found. */
1487 if(pc->type == PC_FUNCTION)
1490 if(pc->type == PC_OPCODE) {
1491 if(PCI(pc)->inCond & cond) {
1493 /* If previous instruction is a skip then continue search as condiction is not certain */
1494 pCode *pcp = findPrevInstruction(pc->prev);
1495 if (pcp && !isPCI_SKIP(pcp)) {
1502 if(PCI(pc)->outCond & cond) {
1504 /* If previous instruction is a skip then continue search as condiction is not certain */
1505 pCode *pcp = findPrevInstruction(pc->prev);
1506 if (pcp && !isPCI_SKIP(pcp)) {
1521 /*-----------------------------------------------------------------
1522 * int pCodeOpCompare(pCodeOp *pcops, pCodeOp *pcopd)
1524 * Compare two pCodeOp's and return 1 if they're the same
1525 *-----------------------------------------------------------------*/
1526 int pCodeOpCompare(pCodeOp *pcops, pCodeOp *pcopd)
1530 if(!pcops || !pcopd)
1533 fprintf(stderr," Comparing operands %s",
1534 get_op( pcops,NULL,0));
1536 fprintf(stderr," to %s\n",
1537 get_op( pcopd,NULL,0));
1540 if(pcops->type != pcopd->type) {
1541 //fprintf(stderr," - fail - diff types\n");
1542 return 0; // different types
1545 if(pcops->type == PO_LITERAL) {
1547 if((PCOL(pcops)->lit >= 0) && (PCOL(pcops)->lit == PCOL(pcopd)->lit))
1556 n2 = get_op(pcopd,NULL,0);
1558 if( !n2 || strcmp(b,n2)) {
1559 //fprintf(stderr," - fail - diff names: %s, len=%d, %s, len=%d\n",b,strlen(b), n2, strlen(n2) );
1560 return 0; // different names
1563 switch(pcops->type) {
1565 if( PCOR(pcops)->instance != PCOR(pcopd)->instance) {
1566 //fprintf(stderr, " - fail different instances\n");
1574 //fprintf(stderr," - pass\n");
1579 int pCodePeepMatchLabels(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
1583 /* Check for a label associated with this wild pCode */
1584 // If the wild card has a label, make sure the source code does too.
1585 if(PCI(pcd)->label) {
1586 pCode *pcl = PCI(pcd)->label->pc;
1589 int li = -PCL(pcl)->key;
1591 if(peepBlock->target.vars[li] == NULL) {
1592 if(PCI(pcs)->label) {
1593 DFPRINTF((stderr,"first time for a label: %d %s\n",li,PCL(PCI(pcs)->label->pc)->label));
1596 // DFPRINTF((stderr,"label id = %d \n",PCL(PCI(pcd)->label->pc)->key));
1597 DFPRINTF((stderr," label id: %d %s\n",li,peepBlock->target.vars[li]));
1598 if(PCI(pcs)->label) {
1599 DFPRINTF((stderr," src %s\n",PCL(PCI(pcs)->label->pc)->label));
1605 if(!PCI(pcs)->label)
1608 labindex = -PCL(pcl)->key;
1609 if(peepBlock->target.vars[labindex] == NULL) {
1610 // First time to encounter this label
1611 peepBlock->target.vars[labindex] = PCL(PCI(pcs)->label->pc)->label;
1612 DFPRINTF((stderr,"first time for a label: %d %s\n",labindex,PCL(PCI(pcs)->label->pc)->label));
1615 if(strcmp(peepBlock->target.vars[labindex],PCL(PCI(pcs)->label->pc)->label) != 0) {
1616 DFPRINTF((stderr,"labels don't match dest %s != src %s\n",peepBlock->target.vars[labindex],PCL(PCI(pcs)->label->pc)->label));
1619 DFPRINTF((stderr,"matched a label %d %s -hey\n",labindex,peepBlock->target.vars[labindex]));
1622 //DFPRINTF((stderr,"destination doesn't have a label\n"));
1627 //DFPRINTF((stderr,"neither src nor dest have labels\n"));
1635 /*-----------------------------------------------------------------*/
1636 /* pCodePeepMatchLine - Compare source and destination pCodes to */
1637 /* see they're the same. */
1639 /* In this context, "source" refers to the coded generated by gen.c*/
1640 /* and "destination" refers to a pcode in a peep rule. If the dest-*/
1641 /* ination has no wild cards, then MatchLine will compare the two */
1642 /* pcodes (src and dest) for a one-to-one match. If the destination*/
1643 /* has wildcards, then those get expanded. When a wild card is */
1644 /* encountered for the first time it autmatically is considered a */
1645 /* match and the object that matches it is referenced in the */
1646 /* variables or opcodes array (depending on the type of match). */
1650 /* *peepBlock - A pointer to the peepBlock that contains the */
1651 /* entire rule to which the destination pcode belongs*/
1652 /* *pcs - a pointer to the source pcode */
1653 /* *pcd - a pointer to the destination pcode */
1656 /* 1 - pcodes match */
1657 /* 0 - pcodes don't match */
1660 /*-----------------------------------------------------------------*/
1662 int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
1664 int index; // index into wild card arrays
1666 /* one-for-one match. Here the source and destination opcodes
1667 * are not wild. However, there may be a label or a wild operand */
1670 if(PCI(pcs)->label) {
1671 DFPRINTF((stderr,"Match line source label: %s\n",PCL(PCI(pcs)->label->pc)->label));
1675 if(pcs->type == pcd->type) {
1677 if(pcs->type == PC_OPCODE) {
1679 /* If the opcodes don't match then the line doesn't match */
1680 if(PCI(pcs)->op != PCI(pcd)->op)
1684 DFPRINTF((stderr,"%s comparing\n",__FUNCTION__));
1685 pcs->print(stderr,pcs);
1686 pcd->print(stderr,pcd);
1689 if(!pCodePeepMatchLabels(peepBlock, pcs, pcd))
1692 /* Compare the operands */
1693 if(PCI(pcd)->pcop) {
1694 // Volatile types should not be deleted or modified, these include SFR, externs and publics
1695 // They can be used as a matched, however if a match is found then the optimiser intends
1696 // to change some aspect of a block of code, which is most likely a critcal one. As this
1697 // method of optimisation does not allow a means to distiguishing what may change, it is
1698 // best to just negate any match.
1699 if (PCI(pcs)->pcop) {
1701 pCodeOp *pcop = PCI(pcs)->pcop;
1702 switch(pcop->type) {
1710 case PO_SFR_REGISTER:
1711 return 0; // SFR - do not modify
1713 case PO_GPR_REGISTER:
1716 case PO_GPR_POINTER:
1718 if (r->isPublic||r->isExtern||r->isFixed) // Changes to these types of registers should not be changed as they may be used else where
1724 if (PCI(pcd)->pcop->type == PO_WILD) {
1726 index = PCOW(PCI(pcd)->pcop)->id;
1727 //DFPRINTF((stderr,"destination is wild\n"));
1728 #ifdef DEBUG_PCODEPEEP
1729 if (index > peepBlock->nops) {
1730 DFPRINTF((stderr,"%s - variables exceeded\n",__FUNCTION__));
1734 n = PCI(pcs)->pcop->name;
1735 if(peepBlock->target.vars[index]) {
1736 if ((!n)||(strcmp(peepBlock->target.vars[index],n) != 0))
1737 return 0; // variable is different
1739 DFPRINTF((stderr,"first time for a variable: %d, %s\n",index,n));
1740 peepBlock->target.vars[index] = n;
1743 PCOW(PCI(pcd)->pcop)->matched = PCI(pcs)->pcop;
1744 if(!peepBlock->target.wildpCodeOps[index]) {
1745 peepBlock->target.wildpCodeOps[index] = PCI(pcs)->pcop;
1747 //fprintf(stderr, "first time for wild opcode #%d\n",index);
1752 pcs->print(stderr,pcs);
1753 pcd->print(stderr,pcd);
1754 fprintf(stderr, "comparing operands of these instructions, result %d\n",
1755 pCodeOpCompare(PCI(pcs)->pcop, peepBlock->target.wildpCodeOps[index])
1759 return pCodeOpCompare(PCI(pcs)->pcop, peepBlock->target.wildpCodeOps[index]);
1765 switch(PCI(pcs)->pcop->type) {
1769 //n = PCOR(PCI(pcs)->pcop)->r->name;
1770 n = PCI(pcs)->pcop->name;
1774 n = PCI(pcs)->pcop->name;
1777 if(peepBlock->target.vars[index])
1778 return (strcmp(peepBlock->target.vars[index],n) == 0);
1780 DFPRINTF((stderr,"first time for a variable: %d, %s\n",index,n));
1781 peepBlock->target.vars[index] = n;
1786 } else if (PCI(pcd)->pcop->type == PO_LITERAL) {
1788 pcs->print(stderr,pcs);
1789 pcd->print(stderr,pcd);
1791 fprintf(stderr, "comparing literal operands of these instructions, result %d\n",
1792 pCodeOpCompare(PCI(pcs)->pcop, PCI(pcd)->pcop));
1794 return pCodeOpCompare(PCI(pcs)->pcop, PCI(pcd)->pcop);
1797 /* FIXME - need an else to check the case when the destination
1798 * isn't a wild card */
1800 fprintf(stderr, "Destination is not wild: operand compare =%d\n",
1801 pCodeOpCompare(PCI(pcs)->pcop, PCI(pcd)->pcop));
1803 return pCodeOpCompare(PCI(pcs)->pcop, PCI(pcd)->pcop);
1807 /* The pcd has no operand. Lines match if pcs has no operand either*/
1808 return (PCI(pcs)->pcop == NULL);
1812 /* Compare a wild instruction to a regular one. */
1814 if((pcd->type == PC_WILD) && (pcs->type == PC_OPCODE)) {
1816 index = PCW(pcd)->id;
1818 DFPRINTF((stderr,"%s comparing wild cards\n",__FUNCTION__));
1819 pcs->print(stderr,pcs);
1820 pcd->print(stderr,pcd);
1822 peepBlock->target.wildpCodes[PCW(pcd)->id] = pcs;
1824 if(!pCodePeepMatchLabels(peepBlock, pcs, pcd)) {
1825 DFPRINTF((stderr," Failing because labels don't match\n"));
1829 if(PCW(pcd)->mustBeBitSkipInst & !(PCI(pcs)->isBitInst && PCI(pcs)->isSkip)) {
1830 // doesn't match because the wild pcode must be a bit skip
1831 DFPRINTF((stderr," Failing match because bit skip is req\n"));
1832 //pcd->print(stderr,pcd);
1833 //pcs->print(stderr,pcs);
1837 if(PCW(pcd)->mustNotBeBitSkipInst & (PCI(pcs)->isBitInst && PCI(pcs)->isSkip)) {
1838 // doesn't match because the wild pcode must *not* be a bit skip
1839 DFPRINTF((stderr," Failing match because shouldn't be bit skip\n"));
1840 //pcd->print(stderr,pcd);
1841 //pcs->print(stderr,pcs);
1845 if(PCW(pcd)->operand) {
1846 PCOW(PCI(pcd)->pcop)->matched = PCI(pcs)->pcop;
1847 if(peepBlock->target.vars[index]) {
1848 int i = (strcmp(peepBlock->target.vars[index],PCI(pcs)->pcop->name) == 0);
1852 DFPRINTF((stderr," (matched)\n"));
1854 DFPRINTF((stderr," (no match: wild card operand mismatch\n"));
1855 DFPRINTF((stderr," peepblock= %s, pcodeop= %s\n",
1856 peepBlock->target.vars[index],
1857 PCI(pcs)->pcop->name));
1862 DFPRINTF((stderr," (matched %s\n",PCI(pcs)->pcop->name));
1863 peepBlock->target.vars[index] = PCI(pcs)->pcop->name;
1868 pcs = findNextInstruction(pcs->next);
1870 //DFPRINTF((stderr," (next to match)\n"));
1871 //pcs->print(stderr,pcs);
1872 } else if(pcd->next) {
1873 /* oops, we ran out of code, but there's more to the rule */
1877 return 1; /* wild card matches */
1883 /*-----------------------------------------------------------------*/
1884 /*-----------------------------------------------------------------*/
1885 void pCodePeepClrVars(pCodePeep *pcp)
1892 DFPRINTF((stderr," Clearing peep rule vars\n"));
1893 DFPRINTF((stderr," %d %d %d %d %d %d\n",
1894 pcp->target.nvars,pcp->target.nops,pcp->target.nwildpCodes,
1895 pcp->replace.nvars,pcp->replace.nops,pcp->replace.nwildpCodes));
1897 for(i=0;i<pcp->target.nvars; i++)
1898 pcp->target.vars[i] = NULL;
1899 for(i=0;i<pcp->target.nops; i++)
1900 pcp->target.wildpCodeOps[i] = NULL;
1901 for(i=0;i<pcp->target.nwildpCodes; i++)
1902 pcp->target.wildpCodes[i] = NULL;
1904 for(i=0;i<pcp->replace.nvars; i++)
1905 pcp->replace.vars[i] = NULL;
1906 for(i=0;i<pcp->replace.nops; i++)
1907 pcp->replace.wildpCodeOps[i] = NULL;
1908 for(i=0;i<pcp->replace.nwildpCodes; i++)
1909 pcp->replace.wildpCodes[i] = NULL;
1915 /*-----------------------------------------------------------------*/
1916 /*-----------------------------------------------------------------*/
1917 int pCodePeepMatchRule(pCode *pc)
1919 pCodePeep *peepBlock;
1921 pCodeCSource *pc_cline=NULL;
1925 peeprules = (_DLL *)peepSnippets;
1928 peepBlock = ((pCodePeepSnippets*)peeprules)->peep;
1930 if(!peepBlock || /*!peepBlock->target ||*/ !peepBlock->target.pb->pcHead) {
1931 fprintf(stderr, "skipping rule because target pb is NULL\n");
1935 pCodePeepClrVars(peepBlock);
1938 if(IS_PCCOMMENT(pcin))
1939 pc = pcin = findNextInstruction(pcin->next);
1941 pcin = pc = findNextInstruction(pc);
1943 pct = peepBlock->target.pb->pcHead;
1946 pCode *pcr = peepBlock->replace.pb->pcHead;
1947 if(pcr) pct->print(stderr,pcr);
1951 while(pct && pcin) {
1953 if(! (matched = pCodePeepMatchLine(peepBlock, pcin,pct)))
1956 pcin = findNextInstruction(pcin->next);
1959 //DFPRINTF((stderr," matched\n"));
1962 DFPRINTF((stderr," partial match... no more code\n"));
1966 DFPRINTF((stderr," end of rule\n"));
1970 if(matched && pcin) {
1972 /* So far we matched the rule up to the point of the conditions .
1973 * In other words, all of the opcodes match. Now we need to see
1974 * if the post conditions are satisfied.
1975 * First we check the 'postFalseCond'. This means that we check
1976 * to see if any of the subsequent pCode's in the pCode chain
1977 * following the point just past where we have matched depend on
1978 * the `postFalseCond' as input then we abort the match
1980 DFPRINTF((stderr," matched rule so far, now checking conditions\n"));
1981 //pcin->print(stderr,pcin);
1983 if (pcin && peepBlock->postFalseCond &&
1984 (pCodeSearchCondition(pcin,peepBlock->postFalseCond,0) > 0) )
1987 //fprintf(stderr," condition results = %d\n",pCodeSearchCondition(pcin,peepBlock->postFalseCond));
1990 //if(!matched) fprintf(stderr,"failed on conditions\n");
1999 /* We matched a rule! Now we have to go through and remove the
2000 inefficient code with the optimized version */
2002 DFPRINTF((stderr, "Found a pcode peep match:\nRule:\n"));
2003 printpCodeString(stderr,peepBlock->target.pb->pcHead,10);
2004 DFPRINTF((stderr,"first thing matched\n"));
2005 pc->print(stderr,pc);
2007 DFPRINTF((stderr,"last thing matched\n"));
2008 pcin->print(stderr,pcin);
2013 /* Unlink the original code */
2016 pcprev->next = pcin;
2018 pcin->prev = pc->prev;
2024 /* Converted the deleted pCodes into comments */
2027 pCodeCSource *pc_cline2=NULL;
2032 while(pc && pc!=pcin) {
2034 if(pc->type == PC_OPCODE && PCI(pc)->cline) {
2036 pc_cline2->pc.next = PCODE(PCI(pc)->cline);
2037 pc_cline2 = PCCS(pc_cline2->pc.next);
2039 pc_cline = pc_cline2 = PCI(pc)->cline;
2040 pc_cline->pc.seq = pc->seq;
2044 pCode2str(&buf[2], 254, pc);
2045 pCodeInsertAfter(pcprev, newpCodeCharP(buf));
2046 pcprev = pcprev->next;
2051 pc_cline2->pc.next = NULL;
2056 pCodeDeleteChain(pc,pcin);
2058 /* Generate the replacement code */
2060 pcr = peepBlock->replace.pb->pcHead; // This is the replacement code
2064 /* If the replace pcode is an instruction with an operand, */
2065 /* then duplicate the operand (and expand wild cards in the process). */
2066 if(pcr->type == PC_OPCODE) {
2067 if(PCI(pcr)->pcop) {
2068 /* The replacing instruction has an operand.
2070 if(PCI(pcr)->pcop->type == PO_WILD) {
2071 int index = PCOW(PCI(pcr)->pcop)->id;
2072 //DFPRINTF((stderr,"copying wildopcode\n"));
2073 if(peepBlock->target.wildpCodeOps[index])
2074 pcop = pCodeOpCopy(peepBlock->target.wildpCodeOps[index]);
2076 DFPRINTF((stderr,"error, wildopcode in replace but not source?\n"));
2078 pcop = pCodeOpCopy(PCI(pcr)->pcop);
2080 //DFPRINTF((stderr,"inserting pCode\n"));
2081 pCodeInsertAfter(pc, newpCode(PCI(pcr)->op,pcop));
2082 } else if (pcr->type == PC_WILD) {
2083 if(PCW(pcr)->invertBitSkipInst)
2084 DFPRINTF((stderr,"We need to invert the bit skip instruction\n"));
2085 pCodeInsertAfter(pc,
2086 pCodeInstructionCopy(PCI(peepBlock->target.wildpCodes[PCW(pcr)->id]),
2087 PCW(pcr)->invertBitSkipInst));
2088 } else if (pcr->type == PC_COMMENT) {
2089 pCodeInsertAfter(pc, newpCodeCharP( ((pCodeComment *)(pcr))->comment));
2094 DFPRINTF((stderr," NEW Code:"));
2095 if(pc) pc->print(stderr,pc);
2100 /* We have just replaced the inefficient code with the rule.
2101 * Now, we need to re-add the C-source symbols if there are any */
2103 while(pc && pc_cline ) {
2105 pc = findNextInstruction(pc->next);
2107 PCI(pc)->cline = pc_cline;
2108 pc_cline = PCCS(pc_cline->pc.next);
2112 /* Copy C code comments to new code. */
2115 for (; pc && pcout!=pcin; pcout=pcout->next) {
2116 if (pcout->type==PC_OPCODE && PCI(pcout)->cline) {
2117 while (pc->type!=PC_OPCODE || PCI(pc)->cline) {
2123 PCI(pc)->cline = PCI(pcout)->cline;
2131 peeprules = peeprules->next;
2133 DFPRINTF((stderr," no rule matched\n"));