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[];
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
208 static char alt_comment[] = { PCP_COMMENT, 0};
209 static char alt_label[] = { PCP_LABEL, 0};
210 static char alt_mnem0[] = { PCP_STR, 0};
211 static char alt_mnem0a[] = { PCP_WILDVAR, 0};
212 static char alt_mnem1[] = { PCP_STR, PCP_STR, 0};
213 static char alt_mnem1a[] = { PCP_STR, PCP_WILDVAR, 0};
214 static char alt_mnem1b[] = { PCP_STR, PCP_NUMBER, 0};
215 static char alt_mnem2[] = { PCP_STR, PCP_STR, PCP_COMMA, PCP_STR, 0};
216 static char alt_mnem2a[] = { PCP_STR, PCP_WILDVAR, PCP_COMMA, PCP_STR, 0};
217 static char alt_mnem3[] = { PCP_STR, PCP_STR, PCP_COMMA, PCP_NUMBER, 0};
219 static void * cvt_altpat_label(void *pp,pCodeWildBlock *pcwb);
220 static void * cvt_altpat_comment(void *pp,pCodeWildBlock *pcwb);
221 static void * cvt_altpat_mnem0(void *pp,pCodeWildBlock *pcwb);
222 static void * cvt_altpat_mnem0a(void *pp,pCodeWildBlock *pcwb);
223 static void * cvt_altpat_mnem1(void *pp,pCodeWildBlock *pcwb);
224 static void * cvt_altpat_mnem1a(void *pp,pCodeWildBlock *pcwb);
225 static void * cvt_altpat_mnem1b(void *pp,pCodeWildBlock *pcwb);
226 static void * cvt_altpat_mnem2(void *pp,pCodeWildBlock *pcwb);
227 static void * cvt_altpat_mnem2a(void *pp,pCodeWildBlock *pcwb);
228 static void * cvt_altpat_mnem3(void *pp,pCodeWildBlock *pcwb);
230 pcPattern altArr[] = {
231 {ALT_LABEL, alt_label, cvt_altpat_label},
232 {ALT_COMMENT, alt_comment,cvt_altpat_comment},
233 {ALT_MNEM3, alt_mnem3, cvt_altpat_mnem3},
234 {ALT_MNEM2A, alt_mnem2a, cvt_altpat_mnem2a},
235 {ALT_MNEM2, alt_mnem2, cvt_altpat_mnem2},
236 {ALT_MNEM1B, alt_mnem1b, cvt_altpat_mnem1b},
237 {ALT_MNEM1A, alt_mnem1a, cvt_altpat_mnem1a},
238 {ALT_MNEM1, alt_mnem1, cvt_altpat_mnem1},
239 {ALT_MNEM0A, alt_mnem0a, cvt_altpat_mnem0a},
240 {ALT_MNEM0, alt_mnem0, cvt_altpat_mnem0},
244 #define ALTPATTERNS (sizeof(altArr)/sizeof(pcPattern))
246 // forward declarations
247 static void * DLL_append(_DLL *list, _DLL *next);
249 /*-----------------------------------------------------------------*/
250 /* cvt_extract_destination - helper function extracts the register */
251 /* destination from a parsedPattern. */
253 /*-----------------------------------------------------------------*/
254 static int cvt_extract_destination(parsedPattern *pp)
257 if(pp->pct[0].tt == PCT_STRING) {
259 // just check first letter for now
261 if(toupper(*pp->pct[0].tok.s) == 'F')
264 } else if (pp->pct[0].tt == PCT_NUMBER) {
274 /*-----------------------------------------------------------------*/
275 /* pCodeOp *cvt_extract_status(char *reg, char *bit) */
276 /* if *reg is the "status" register and *bit is one of the */
277 /* status bits, then this function will create a new pCode op */
278 /* containing the status register. */
279 /*-----------------------------------------------------------------*/
281 static pCodeOp *cvt_extract_status(char *reg, char *bit)
285 if(STRCASECMP(reg, pc_status.pcop.name))
292 if(toupper(*bit) == 'C')
293 return PCOP(popCopyGPR2Bit(&pc_status,PIC_C_BIT));
294 if(toupper(*bit) == 'Z')
295 return PCOP(popCopyGPR2Bit(&pc_status,PIC_Z_BIT));
299 if(len ==2 && toupper(bit[0]) == 'D' && toupper(bit[1]) == 'C')
300 return PCOP(popCopyGPR2Bit(&pc_status,PIC_DC_BIT));
306 /*-----------------------------------------------------------------*/
307 /* cvt_altpat_label - convert assembly line type to a pCode label */
308 /* INPUT: pointer to the parsedPattern */
312 /* label pattern => '%' number ':' */
313 /* at this point, we wish to extract only the 'number' */
315 /*-----------------------------------------------------------------*/
316 static void * cvt_altpat_label(void *pp,pCodeWildBlock *pcwb)
318 parsedPattern *p = pp;
320 DFPRINTF((stderr,"altpat_label with ID = %d\n",p->pct[1].tok.n));
321 return newpCodeLabel(NULL,-p->pct[1].tok.n);
325 /*-----------------------------------------------------------------*/
326 /* cvt_altpat_comment - convert assembly line type to a comment */
327 /* INPUT: pointer to the parsedPattern */
329 /* pp[0] - comment */
332 /*-----------------------------------------------------------------*/
333 static void * cvt_altpat_comment(void *pp,pCodeWildBlock *pcwb)
335 parsedPattern *p = pp;
337 DFPRINTF((stderr,"altpat_comment = %s\n",p->pct[0].tok.s));
338 return newpCodeCharP(p->pct[0].tok.s);
342 /*-----------------------------------------------------------------*/
343 /* cvt_altpat_mem0 - convert assembly line type to a wild pCode */
348 /*-----------------------------------------------------------------*/
349 static void * cvt_altpat_mnem0(void *pp,pCodeWildBlock *pcwb)
351 parsedPattern *p = pp;
354 pCodeInstruction *pci=NULL;
356 DFPRINTF((stderr,"altpat_mnem0 %s\n", p->pct[0].tok.s));
358 opcode = getpCode(p->pct[0].tok.s,0);
361 /* look for special command strings like _NOTBITSKIP_ */
363 //fprintf(stderr, "Bad mnemonic\n");
365 opcode = getpCodePeepCommand(p->pct[0].tok.s);
367 // fprintf(stderr," but valid peep command: %s, key = %d\n",p->pct[0].tok.s,opcode);
371 pci = PCI(newpCode(opcode, NULL));
374 fprintf(stderr,"couldn't find mnemonic\n");
380 /*-----------------------------------------------------------------*/
381 /* cvt_altpat_mem0a - convert assembly line type to a wild pCode */
384 /* pp[0] - wild var */
386 /*-----------------------------------------------------------------*/
387 static void * cvt_altpat_mnem0a(void *pp, pCodeWildBlock *pcwb)
389 parsedPattern *p = pp;
391 DFPRINTF((stderr,"altpat_mnem0a wild mnem # %d\n", p[0].pct[1].tok.n));
393 /* Save the index of the maximum wildcard mnemonic */
395 //if(p[0].pct[1].tok.n > sMaxWildVar)
396 // sMaxWildMnem = p[0].pct[1].tok.n;
398 if(p[0].pct[1].tok.n > pcwb->nwildpCodes)
399 pcwb->nwildpCodes = p[0].pct[1].tok.n;
401 return newpCodeWild(p[0].pct[1].tok.n,NULL,NULL);
405 /*-----------------------------------------------------------------*/
406 /* cvt_altpat_mem1 - convert assembly line type to a pCode */
407 /* instruction with 1 operand. */
410 /* pp[1] - Operand */
412 /*-----------------------------------------------------------------*/
413 static void * cvt_altpat_mnem1(void *pp,pCodeWildBlock *pcwb)
416 parsedPattern *p = pp;
419 pCodeInstruction *pci=NULL;
422 DFPRINTF((stderr,"altpat_mnem1 %s var %s\n", p->pct[0].tok.s,p[1].pct[0].tok.s));
424 opcode = getpCode(p->pct[0].tok.s,0);
426 //fprintf(stderr, "Bad mnemonic\n");
427 opcode = getpCodePeepCommand(p->pct[0].tok.s);
429 //fprintf(stderr," but valid peep command: %s, key = %d\n",p->pct[0].tok.s,opcode);
434 if(pic14Mnemonics[opcode]->isBitInst)
435 pcosubtype = newpCodeOp(p[1].pct[0].tok.s,PO_BIT);
437 pcosubtype = newpCodeOp(p[1].pct[0].tok.s,PO_GPR_REGISTER);
440 pci = PCI(newpCode(opcode, pcosubtype));
443 fprintf(stderr,"couldn't find mnemonic\n");
449 /*-----------------------------------------------------------------*/
450 /* cvt_altpat_mem1a - convert assembly line type to a pCode */
451 /* instruction with 1 wild operand. */
454 /* pp[1] - wild var */
456 /*-----------------------------------------------------------------*/
457 static void * cvt_altpat_mnem1a(void *pp,pCodeWildBlock *pcwb)
459 parsedPattern *p = pp;
462 pCodeInstruction *pci=NULL;
465 DFPRINTF((stderr,"altpat_mnem1a %s var %d\n", p->pct[0].tok.s,p[1].pct[1].tok.n));
467 opcode = getpCode(p->pct[0].tok.s,0);
469 int cmd_id = getpCodePeepCommand(p->pct[0].tok.s);
473 fprintf(stderr, "Bad mnemonic\n");
477 if(p[0].pct[1].tok.n > pcwb->nwildpCodes)
478 pcwb->nwildpCodes = p[0].pct[1].tok.n;
480 pc = newpCodeWild(p[1].pct[1].tok.n,NULL,NULL);
484 PCW(pc)->mustNotBeBitSkipInst = 1;
487 PCW(pc)->mustBeBitSkipInst = 1;
490 PCW(pc)->invertBitSkipInst = 1;
495 if(pic14Mnemonics[opcode]->isBitInst)
496 pcosubtype = newpCodeOpBit(NULL,-1,0);
498 pcosubtype = newpCodeOp(NULL,PO_GPR_REGISTER);
501 pci = PCI(newpCode(opcode,
502 newpCodeOpWild(p[1].pct[1].tok.n, pcwb, pcosubtype)));
504 /* Save the index of the maximum wildcard variable */
505 //if(p[1].pct[1].tok.n > sMaxWildVar)
506 // sMaxWildVar = p[1].pct[1].tok.n;
508 if(p[1].pct[1].tok.n > pcwb->nvars)
509 pcwb->nvars = p[1].pct[1].tok.n;
512 fprintf(stderr,"couldn't find mnemonic\n");
518 /*-----------------------------------------------------------------*/
519 /*-----------------------------------------------------------------*/
520 static void * cvt_altpat_mnem1b(void *pp,pCodeWildBlock *pcwb)
522 parsedPattern *p = pp;
525 pCodeInstruction *pci=NULL;
527 DFPRINTF((stderr,"altpat_mnem1b %s var %d\n", p->pct[0].tok.s,p[1].pct[0].tok.n));
529 opcode = getpCode(p->pct[0].tok.s,0);
531 fprintf(stderr, "Bad mnemonic\n");
535 pci = PCI(newpCode(opcode, newpCodeOpLit(p[1].pct[0].tok.n) ));
538 fprintf(stderr,"couldn't find mnemonic\n");
544 /*-----------------------------------------------------------------*/
545 /* cvt_altpat_mnem2 */
550 /* pp[3] - destination */
551 /*-----------------------------------------------------------------*/
552 static void * cvt_altpat_mnem2(void *pp,pCodeWildBlock *pcwb)
554 parsedPattern *p = pp;
558 pCodeInstruction *pci=NULL;
561 dest = cvt_extract_destination(&p[3]);
563 DFPRINTF((stderr,"altpat_mnem2 %s var %s destination %s(%d)\n",
570 opcode = getpCode(p->pct[0].tok.s,dest);
572 fprintf(stderr, "Bad mnemonic\n");
576 if(pic14Mnemonics[opcode]->isBitInst) {
577 pcosubtype = cvt_extract_status(p[1].pct[0].tok.s, p[3].pct[0].tok.s);
578 if(pcosubtype == NULL) {
579 fprintf(stderr, "bad operand?\n");
584 pcosubtype = newpCodeOp(p[1].pct[0].tok.s,PO_GPR_REGISTER);
587 pci = PCI(newpCode(opcode,pcosubtype));
590 fprintf(stderr,"couldn't find mnemonic\n");
596 /*-----------------------------------------------------------------*/
597 /* cvt_altpat_mem2a - convert assembly line type to a pCode */
598 /* instruction with 1 wild operand and a */
599 /* destination operand (e.g. w or f) */
602 /* pp[1] - wild var */
604 /* pp[3] - destination */
606 /*-----------------------------------------------------------------*/
607 static void * cvt_altpat_mnem2a(void *pp,pCodeWildBlock *pcwb)
609 parsedPattern *p = pp;
613 pCodeInstruction *pci=NULL;
617 fprintf(stderr,"ERROR %s:%d - can't assemble line\n",__FILE__,__LINE__);
621 dest = cvt_extract_destination(&p[3]);
623 DFPRINTF((stderr,"altpat_mnem2a %s var %d destination %s(%d)\n",
630 opcode = getpCode(p->pct[0].tok.s,dest);
632 fprintf(stderr, "Bad mnemonic\n");
636 if(pic14Mnemonics[opcode]->isBitInst)
637 pcosubtype = newpCodeOp(NULL,PO_BIT);
639 pcosubtype = newpCodeOp(NULL,PO_GPR_REGISTER);
642 pci = PCI(newpCode(opcode,
643 newpCodeOpWild(p[1].pct[1].tok.n, pcwb, pcosubtype)));
645 /* Save the index of the maximum wildcard variable */
646 //if(p[1].pct[1].tok.n > sMaxWildVar)
647 // sMaxWildVar = p[1].pct[1].tok.n;
649 if(p[1].pct[1].tok.n > pcwb->nvars)
650 pcwb->nvars = p[1].pct[1].tok.n;
653 fprintf(stderr,"couldn't find mnemonic\n");
660 /*-----------------------------------------------------------------*/
661 /* cvt_altpat_mem3 - convert assembly line type to a pCode */
662 /* This rule is for bsf/bcf type instructions */
666 /* pp[1] - register */
670 /*-----------------------------------------------------------------*/
671 static void * cvt_altpat_mnem3(void *pp,pCodeWildBlock *pcwb)
673 parsedPattern *p = pp;
675 int dest; // or could be bit position in the register
677 pCodeInstruction *pci=NULL;
678 pCodeOp *pcosubtype=NULL;
680 dest = cvt_extract_destination(&p[3]);
682 DFPRINTF((stderr,"altpat_mnem3 %s var %s bit (%d)\n",
688 opcode = getpCode(p->pct[0].tok.s,0);
690 fprintf(stderr, "Bad mnemonic\n");
695 if(pic14Mnemonics[opcode]->isBitInst) {
696 //pcosubtype = cvt_extract_status(p[1].pct[0].tok.s, p[3].pct[0].tok.s);
698 //if(pcosubtype == NULL) {
699 pcosubtype = newpCodeOpBit(p[1].pct[0].tok.s,p[3].pct[0].tok.n,0);
702 pcosubtype = newpCodeOp(p[1].pct[0].tok.s,PO_GPR_REGISTER);
704 if(pcosubtype == NULL) {
705 fprintf(stderr, "Bad operand\n");
709 pci = PCI(newpCode(opcode, pcosubtype));
712 fprintf(stderr,"couldn't find mnemonic\n");
718 /*-----------------------------------------------------------------*/
719 /* tokenizeLineNode - Convert a string (of char's) that was parsed */
720 /* by SDCCpeeph.c into a string of tokens. */
723 /* The tokenizer is of the classic type. When an item is encounterd*/
724 /* it is converted into a token. The token is a structure that */
725 /* encodes the item's type and it's value (when appropriate). */
727 /* Accepted token types: */
728 /* SPACE NUMBER STRING % : , ; */
732 /*-----------------------------------------------------------------*/
735 static void tokenizeLineNode(char *ln)
738 tokIdx = 0; // Starting off at the beginning
739 tokArr[0].tt = PCT_NULL; // and assume invalid character for first token.
748 // add a SPACE token and eat the extra spaces.
749 tokArr[tokIdx++].tt = PCT_SPACE;
750 while (isspace (*ln))
757 tokArr[tokIdx].tt = PCT_NUMBER;
758 tokArr[tokIdx++].tok.n = strtol(ln, &ln, 0);
766 tokArr[tokIdx++].tt = PCT_PERCENT;
769 tokArr[tokIdx++].tt = PCT_LESSTHAN;
772 tokArr[tokIdx++].tt = PCT_GREATERTHAN;
775 tokArr[tokIdx++].tt = PCT_COLON;
778 tokArr[tokIdx].tok.s = Safe_strdup(ln);
779 tokArr[tokIdx++].tt = PCT_COMMENT;
780 tokArr[tokIdx].tt = PCT_NULL;
783 tokArr[tokIdx++].tt = PCT_COMMA;
788 if(isalpha(*ln) || (*ln == '_') ) {
792 while( (isalpha(*ln) || isdigit(*ln) || (*ln == '_')) && i<49)
798 tokArr[tokIdx].tok.s = Safe_strdup(buffer);
799 tokArr[tokIdx++].tt = PCT_STRING;
802 fprintf(stderr, "Error while parsing peep rules (check peeph.def)\n");
803 fprintf(stderr, "Line: %s\n",lnstart);
804 fprintf(stderr, "Token: '%c'\n",*ln);
809 /* Advance to next character in input string .
810 * Note, if none of the tests passed above, then
811 * we effectively ignore the `bad' character.
812 * Since the line has already been parsed by SDCCpeeph,
813 * chance are that there are no invalid characters... */
819 tokArr[tokIdx].tt = 0;
823 /*-----------------------------------------------------------------*/
824 /*-----------------------------------------------------------------*/
828 void dump1Token(pCodeTokens tt)
833 fprintf(stderr, " space ");
836 fprintf(stderr, " pct %%");
839 fprintf(stderr, " pct <");
841 case PCT_GREATERTHAN:
842 fprintf(stderr, " pct >");
845 fprintf(stderr, " col :");
848 fprintf(stderr, " comma , ");
851 fprintf(stderr, " comment ");
852 //fprintf(stderr,"%s",tokArr[i].tok.s);
855 fprintf(stderr, " str ");
856 //fprintf(stderr,"%s",tokArr[i].tok.s);
859 fprintf(stderr, " num ");
860 //fprintf(stderr,"%d",tokArr[i].tok.n);
863 fprintf(stderr, " null ");
870 /*-----------------------------------------------------------------*/
871 /*-----------------------------------------------------------------*/
873 int pcComparePattern(pCodeToken *pct, char *pat, int max_tokens)
877 if(!pct || !pat || !*pat)
880 //DFPRINTF((stderr,"comparing against:\n"));
882 while(i < max_tokens) {
885 //DFPRINTF((stderr,"matched\n"));
889 //dump1Token(*pat); DFPRINTF((stderr,"\n"));
903 /*-----------------------------------------------------------------*/
904 /*-----------------------------------------------------------------*/
906 int altComparePattern( char *pct, parsedPattern *pat, int max_tokens)
910 if(!pct || !pat || !*pct)
914 while(i < max_tokens) {
917 //DFPRINTF((stderr,"matched\n"));
921 //dump1Token(*pat); DFPRINTF((stderr,"\n"));
923 if( !pat || !pat->pcp )
926 if (pat->pcp->pt != *pct)
929 //DFPRINTF((stderr," pct=%d\n",*pct));
938 /*-----------------------------------------------------------------*/
939 /*-----------------------------------------------------------------*/
941 int advTokIdx(int *v, int amt)
944 if((unsigned) (*v + amt) > tokIdx)
952 /*-----------------------------------------------------------------*/
953 /* parseTokens - convert the tokens corresponding to a single line */
954 /* of a peep hole assembly into a pCode object. */
959 /* This is a simple parser that looks for strings of the type */
960 /* allowed in the peep hole definition file. Essentially the format*/
961 /* is the same as a line of assembly: */
963 /* label: mnemonic op1, op2, op3 ; comment */
965 /* Some of these items aren't present. It's the job of the parser */
966 /* to determine which are and convert those into the appropriate */
968 /*-----------------------------------------------------------------*/
970 int parseTokens(pCodeWildBlock *pcwb, pCode **pcret)
981 for(i=0; i<=tokIdx; i++)
982 dump1Token(tokArr[i].tt);
995 char * cPmnem = NULL; // Pointer to non-wild mnemonic (if any)
996 char * cP1stop = NULL;
997 char * cP2ndop = NULL;
999 //pCodeOp *pcl = NULL; // Storage for a label
1000 //pCodeOp *pco1 = NULL; // 1st operand
1001 //pCodeOp *pco2 = NULL; // 2nd operand
1002 //pCode *pc = NULL; // Mnemonic
1013 ParseStates state = PS_START;
1020 if( ((tokArr[ltokIdx].tt == PCT_SPACE) )
1021 && (advTokIdx(<okIdx, 1)) ) // eat space
1025 j = pcComparePattern(&tokArr[ltokIdx], pcpArr[lpcpIdx].tokens, tokIdx +1);
1028 switch(pcpArr[lpcpIdx].pt) {
1030 if(state == PS_START){
1031 DFPRINTF((stderr," label\n"));
1032 state = PS_HAVE_LABEL;
1034 DFPRINTF((stderr," bad state (%d) for label\n",state));
1038 DFPRINTF((stderr," %s is",tokArr[ltokIdx].tok.s));
1042 DFPRINTF((stderr," mnem\n"));
1043 cPmnem = tokArr[ltokIdx].tok.s;
1044 state = PS_HAVE_MNEM;
1047 DFPRINTF((stderr," 1st operand\n"));
1048 cP1stop = tokArr[ltokIdx].tok.s;
1049 //pco1 = newpCodeOp(NULL,PO_GPR_REGISTER);
1050 state = PS_HAVE_1OPERAND;
1052 case PS_HAVE_1OPERAND:
1053 DFPRINTF((stderr," error expecting comma\n"));
1056 DFPRINTF((stderr," 2 operands\n"));
1057 cP2ndop = tokArr[ltokIdx].tok.s;
1059 case PS_HAVE_2OPERANDS:
1068 DFPRINTF((stderr," wild mnem\n"));
1069 state = PS_HAVE_MNEM;
1072 DFPRINTF((stderr," 1st operand is wild\n"));
1073 state = PS_HAVE_1OPERAND;
1075 case PS_HAVE_1OPERAND:
1076 DFPRINTF((stderr," error expecting comma\n"));
1079 DFPRINTF((stderr," 2nd operand is wild\n"));
1081 case PS_HAVE_2OPERANDS:
1090 fprintf(stderr," ERROR number\n");
1093 DFPRINTF((stderr," 1st operand is a number\n"));
1094 state = PS_HAVE_1OPERAND;
1096 case PS_HAVE_1OPERAND:
1097 fprintf(stderr," error expecting comma\n");
1100 DFPRINTF((stderr," 2nd operand is a number\n"));
1102 case PS_HAVE_2OPERANDS:
1110 if(state == PS_HAVE_1OPERAND){
1111 DFPRINTF((stderr," got a comma\n"));
1112 state = PS_HAVE_COMMA;
1114 fprintf(stderr," unexpected comma\n");
1120 parsedPatArr[lparsedPatIdx].pcp = &pcpArr[lpcpIdx];
1121 parsedPatArr[lparsedPatIdx].pct = &tokArr[ltokIdx];
1124 //dump1Token(tokArr[ltokIdx].tt);
1126 if(advTokIdx(<okIdx, strlen(pcpArr[lpcpIdx].tokens) ) ) {
1127 DFPRINTF((stderr," reached end \n"));
1134 } while ((++lpcpIdx < PCPATTERNS) && !matching);
1138 parsedPatArr[lparsedPatIdx].pcp = NULL;
1139 parsedPatArr[lparsedPatIdx].pct = NULL;
1145 if( (c=altComparePattern( altArr[k].tokens, &parsedPatArr[j],10) ) ) {
1148 pc = altArr[k].f(&parsedPatArr[j],pcwb);
1149 //if(pc && pc->print)
1150 // pc->print(stderr,pc);
1151 //if(pc && pc->destruct) pc->destruct(pc); dumps core?
1153 //if(curBlock && pc)
1154 //addpCode2pBlock(curBlock, pc);
1158 return 0; // Only accept one line for now.
1160 addpCode2pBlock(pcwb->pb, pc);
1168 while(j<=lparsedPatIdx && k<ALTPATTERNS);
1171 DFPRINTF((stderr,"\nConverting parsed line to pCode:\n\n"));
1175 if(parsedPatArr[j].pcp && parsedPatArr[j].pcp->f )
1176 parsedPatArr[j].pcp->f(&parsedPatArr[j]);
1177 DFPRINTF((stderr," %d",parsedPatArr[j].pcp->pt));
1180 while(j<lparsedPatIdx);
1182 DFPRINTF((stderr,"\n"));
1189 /*-----------------------------------------------------------------*/
1191 /*-----------------------------------------------------------------*/
1192 void peepRuleBlock2pCodeBlock( lineNode *ln, pCodeWildBlock *pcwb)
1198 for( ; ln; ln = ln->next) {
1200 //DFPRINTF((stderr,"%s\n",ln->line));
1202 tokenizeLineNode(ln->line);
1204 if(parseTokens(pcwb,NULL)) {
1206 fprintf(stderr,"ERROR assembling line:\n%s\n",ln->line);
1207 fprintf(stderr,"Tokens:\n");
1209 dump1Token(tokArr[i].tt);
1216 /*-----------------------------------------------------------------*/
1218 /*-----------------------------------------------------------------*/
1219 pCode *AssembleLine(char *line)
1223 if(!line || !*line) {
1224 fprintf(stderr,"WARNING returning NULL in AssembleLine\n");
1228 tokenizeLineNode(line);
1230 if(parseTokens(NULL,&pc))
1231 fprintf(stderr, "WARNING: unable to assemble line:\n%s\n",line);
1237 /*-----------------------------------------------------------------*/
1238 /* peepRuleCondition */
1239 /*-----------------------------------------------------------------*/
1240 static void peepRuleCondition(char *cond, pCodePeep *pcp)
1245 //DFPRINTF((stderr,"\nCondition: %s\n",cond));
1246 /* brute force compares for now */
1248 if(STRCASECMP(cond, "NZ") == 0) {
1249 //DFPRINTF((stderr,"found NZ\n"));
1250 pcp->postFalseCond = PCC_Z;
1257 void initpCodeWildBlock(pCodeWildBlock *pcwb)
1260 // pcwb = Safe_calloc(1,sizeof(pCodeWildBlock));
1266 pcwb->wildpCodes = NULL;
1267 pcwb->wildpCodeOps = NULL;
1270 pcwb->nwildpCodes = 0;
1275 void postinit_pCodeWildBlock(pCodeWildBlock *pcwb)
1282 pcwb->nops = pcwb->nvars;
1284 pcwb->vars = Safe_calloc(pcwb->nvars, sizeof(char *));
1285 pcwb->wildpCodeOps = Safe_calloc(pcwb->nvars, sizeof(pCodeOp *));
1287 pcwb->nwildpCodes+=2;
1288 pcwb->wildpCodes = Safe_calloc(pcwb->nwildpCodes, sizeof(pCode *));
1292 void initpCodePeep(pCodePeep *pcp)
1295 // pcwb = Safe_calloc(1,sizeof(pCodeWildBlock));
1300 initpCodeWildBlock(&pcp->target);
1301 pcp->target.pb = newpCodeChain(NULL, 'W', NULL);
1303 initpCodeWildBlock(&pcp->replace);
1304 pcp->replace.pb = newpCodeChain(NULL, 'W', NULL);
1308 /*-----------------------------------------------------------------*/
1309 /* peepRules2pCode - parse the "parsed" peep hole rules to generate*/
1312 /* SDCCpeeph parses the peep rules file and extracts variables, */
1313 /* removes white space, and checks the syntax. This function */
1314 /* extends that processing to produce pCode objects. You can kind */
1315 /* think of this function as an "assembler", though instead of */
1316 /* taking raw text to produce machine code, it produces pCode. */
1318 /*-----------------------------------------------------------------*/
1319 extern void pic14initpCodePeepCommands(void);
1321 void peepRules2pCode(peepRule *rules)
1325 pCodePeep *currentRule;
1326 pCodePeepSnippets *pcps;
1328 pic14initpCodePeepCommands();
1330 /* The rules are in a linked-list. Each rule has two portions */
1331 /* There's the `target' and there's the `replace'. The target */
1332 /* is compared against the SDCC generated code and if it */
1333 /* matches, it gets replaced by the `replace' block of code. */
1335 /* Here we loop through each rule and convert the target's and*/
1336 /* replace's into pCode target and replace blocks */
1338 for (pr = rules; pr; pr = pr->next) {
1340 //DFPRINTF((stderr,"\nRule:\n\n"));
1342 pcps = Safe_calloc(1,sizeof(pCodePeepSnippets));
1343 peepSnippets = DLL_append((_DLL*)peepSnippets,(_DLL*)pcps);
1345 currentRule = pcps->peep = Safe_calloc(1,sizeof(pCodePeep));
1346 initpCodePeep(currentRule);
1348 /* Convert the target block */
1349 peepRuleBlock2pCodeBlock(pr->match, ¤tRule->target);
1351 //DFPRINTF((stderr,"finished target, here it is in pcode form:\n"));
1352 //printpBlock(stderr, currentRule->target.pb);
1354 //DFPRINTF((stderr,"target with labels merged:\n"));
1355 //pBlockMergeLabels(curBlock);
1356 pBlockMergeLabels(currentRule->target.pb);
1357 //printpBlock(stderr, currentRule->replace.pb);
1359 //#ifdef PCODE_DEBUG
1360 // printpBlock(stderr, curBlock);
1362 //DFPRINTF((stderr,"\nReplaced by:\n"));
1365 /* Convert the replace block */
1366 peepRuleBlock2pCodeBlock(pr->replace, ¤tRule->replace);
1368 //DFPRINTF((stderr,"finished replace block, here it is in pcode form:\n"));
1369 //printpBlock(stderr, curBlock);
1371 //DFPRINTF((stderr,"replace with labels merged:\n"));
1373 pBlockMergeLabels(currentRule->replace.pb);
1374 //printpBlock(stderr, currentRule->replace.pb);
1376 peepRuleCondition(pr->cond,currentRule);
1378 /* The rule has been converted to pCode. Now allocate
1379 * space for the wildcards */
1381 postinit_pCodeWildBlock(¤tRule->target);
1382 postinit_pCodeWildBlock(¤tRule->replace);
1384 //return; // debug ... don't want to go through all the rules yet
1388 pCodePeep *peepBlock;
1391 peeprules = (_DLL *)peepSnippets;
1392 //fprintf(stderr,"target rules\n");
1394 //fprintf(stderr," rule:\n");
1395 peepBlock = ((pCodePeepSnippets*)peeprules)->peep;
1396 //printpBlock(stderr, peepBlock->target.pb);
1397 peeprules = peeprules->next;
1399 //fprintf(stderr," ... done\n");
1404 void printpCodeString(FILE *of, pCode *pc, int max)
1408 while(pc && (i++<max)) {
1414 /*-----------------------------------------------------------------*/
1415 /* _DLL * DLL_append */
1417 /* Append a _DLL object to the end of a _DLL (doubly linked list) */
1418 /* If The list to which we want to append is non-existant then one */
1419 /* is created. Other wise, the end of the list is sought out and */
1420 /* a new DLL object is appended to it. In either case, the void */
1421 /* *data is added to the newly created DLL object. */
1422 /*-----------------------------------------------------------------*/
1424 static void * DLL_append(_DLL *list, _DLL *next)
1429 /* If there's no list, then create one: */
1431 next->next = next->prev = NULL;
1436 /* Search for the end of the list. */
1441 /* Now append the new DLL object */
1452 /*-----------------------------------------------------------------
1454 pCode peephole optimization
1457 The pCode "peep hole" optimization is not too unlike the peep hole
1458 optimization in SDCCpeeph.c. The major difference is that here we
1459 use pCode's whereas there we use ASCII strings. The advantage with
1460 pCode's is that we can ascertain flow information in the instructions
1464 <FIX ME> - elaborate...
1466 -----------------------------------------------------------------*/
1470 /*-----------------------------------------------------------------*/
1471 /* pCodeSearchCondition - Search a pCode chain for a 'condition' */
1473 /* return conditions */
1474 /* 1 - The Condition was found for a pCode's input */
1475 /* 0 - No matching condition was found for the whole chain */
1476 /* -1 - The Condition was found for a pCode's output */
1478 /*-----------------------------------------------------------------*/
1479 int pCodeSearchCondition(pCode *pc, unsigned int cond)
1481 //fprintf(stderr,"Checking conditions %d\n",cond);
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 //fprintf(stderr," checking conditions of: ");
1492 //pc->print(stderr,pc);
1493 //fprintf(stderr,"\t\tinCond=%d\toutCond=%d\n",PCI(pc)->inCond,PCI(pc)->outCond);
1494 if(PCI(pc)->inCond & cond)
1496 if(PCI(pc)->outCond & cond)
1506 /*-----------------------------------------------------------------
1507 * int pCodeOpCompare(pCodeOp *pcops, pCodeOp *pcopd)
1509 * Compare two pCodeOp's and return 1 if they're the same
1510 *-----------------------------------------------------------------*/
1511 int pCodeOpCompare(pCodeOp *pcops, pCodeOp *pcopd)
1515 if(!pcops || !pcopd)
1518 fprintf(stderr," Comparing operands %s",
1519 get_op( pcops,NULL,0));
1521 fprintf(stderr," to %s\n",
1522 get_op( pcopd,NULL,0));
1525 if(pcops->type != pcopd->type) {
1526 //fprintf(stderr," - fail - diff types\n");
1527 return 0; // different types
1530 if(pcops->type == PO_LITERAL) {
1532 if((PCOL(pcops)->lit >= 0) && (PCOL(pcops)->lit == PCOL(pcopd)->lit))
1541 n2 = get_op(pcopd,NULL,0);
1543 if( !n2 || strcmp(b,n2)) {
1544 //fprintf(stderr," - fail - diff names: %s, len=%d, %s, len=%d\n",b,strlen(b), n2, strlen(n2) );
1545 return 0; // different names
1548 switch(pcops->type) {
1550 if( PCOR(pcops)->instance != PCOR(pcopd)->instance) {
1551 //fprintf(stderr, " - fail different instances\n");
1559 //fprintf(stderr," - pass\n");
1564 int pCodePeepMatchLabels(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
1568 /* Check for a label associated with this wild pCode */
1569 // If the wild card has a label, make sure the source code does too.
1570 if(PCI(pcd)->label) {
1571 pCode *pcl = PCI(pcd)->label->pc;
1574 int li = -PCL(pcl)->key;
1576 if(peepBlock->target.vars[li] == NULL) {
1577 if(PCI(pcs)->label) {
1578 DFPRINTF((stderr,"first time for a label: %d %s\n",li,PCL(PCI(pcs)->label->pc)->label));
1581 // DFPRINTF((stderr,"label id = %d \n",PCL(PCI(pcd)->label->pc)->key));
1582 DFPRINTF((stderr," label id: %d %s\n",li,peepBlock->target.vars[li]));
1583 if(PCI(pcs)->label) {
1584 DFPRINTF((stderr," src %s\n",PCL(PCI(pcs)->label->pc)->label));
1590 if(!PCI(pcs)->label)
1593 labindex = -PCL(pcl)->key;
1594 if(peepBlock->target.vars[labindex] == NULL) {
1595 // First time to encounter this label
1596 peepBlock->target.vars[labindex] = PCL(PCI(pcs)->label->pc)->label;
1597 DFPRINTF((stderr,"first time for a label: %d %s\n",labindex,PCL(PCI(pcs)->label->pc)->label));
1600 if(strcmp(peepBlock->target.vars[labindex],PCL(PCI(pcs)->label->pc)->label) != 0) {
1601 DFPRINTF((stderr,"labels don't match dest %s != src %s\n",peepBlock->target.vars[labindex],PCL(PCI(pcs)->label->pc)->label));
1604 DFPRINTF((stderr,"matched a label %d %s -hey\n",labindex,peepBlock->target.vars[labindex]));
1607 //DFPRINTF((stderr,"destination doesn't have a label\n"));
1612 //DFPRINTF((stderr,"neither src nor dest have labels\n"));
1620 /*-----------------------------------------------------------------*/
1621 /* pCodePeepMatchLine - Compare source and destination pCodes to */
1622 /* see they're the same. */
1624 /* In this context, "source" refers to the coded generated by gen.c*/
1625 /* and "destination" refers to a pcode in a peep rule. If the dest-*/
1626 /* ination has no wild cards, then MatchLine will compare the two */
1627 /* pcodes (src and dest) for a one-to-one match. If the destination*/
1628 /* has wildcards, then those get expanded. When a wild card is */
1629 /* encountered for the first time it autmatically is considered a */
1630 /* match and the object that matches it is referenced in the */
1631 /* variables or opcodes array (depending on the type of match). */
1635 /* *peepBlock - A pointer to the peepBlock that contains the */
1636 /* entire rule to which the destination pcode belongs*/
1637 /* *pcs - a pointer to the source pcode */
1638 /* *pcd - a pointer to the destination pcode */
1641 /* 1 - pcodes match */
1642 /* 0 - pcodes don't match */
1645 /*-----------------------------------------------------------------*/
1647 int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
1649 int index; // index into wild card arrays
1651 /* one-for-one match. Here the source and destination opcodes
1652 * are not wild. However, there may be a label or a wild operand */
1655 if(PCI(pcs)->label) {
1656 DFPRINTF((stderr,"Match line source label: %s\n",PCL(PCI(pcs)->label->pc)->label));
1660 if(pcs->type == pcd->type) {
1662 if(pcs->type == PC_OPCODE) {
1664 /* If the opcodes don't match then the line doesn't match */
1665 if(PCI(pcs)->op != PCI(pcd)->op)
1669 DFPRINTF((stderr,"%s comparing\n",__FUNCTION__));
1670 pcs->print(stderr,pcs);
1671 pcd->print(stderr,pcd);
1674 if(!pCodePeepMatchLabels(peepBlock, pcs, pcd))
1677 /* Compare the operands */
1678 if(PCI(pcd)->pcop) {
1679 if (PCI(pcd)->pcop->type == PO_WILD) {
1680 index = PCOW(PCI(pcd)->pcop)->id;
1681 //DFPRINTF((stderr,"destination is wild\n"));
1682 #ifdef DEBUG_PCODEPEEP
1683 if (index > peepBlock->nops) {
1684 DFPRINTF((stderr,"%s - variables exceeded\n",__FUNCTION__));
1689 PCOW(PCI(pcd)->pcop)->matched = PCI(pcs)->pcop;
1690 if(!peepBlock->target.wildpCodeOps[index]) {
1691 peepBlock->target.wildpCodeOps[index] = PCI(pcs)->pcop;
1693 //if(PCI(pcs)->pcop->type == PO_GPR_TEMP)
1697 pcs->print(stderr,pcs);
1698 pcd->print(stderr,pcd);
1700 fprintf(stderr, "comparing operands of these instructions, result %d\n",
1701 pCodeOpCompare(PCI(pcs)->pcop, peepBlock->target.wildpCodeOps[index])
1705 return pCodeOpCompare(PCI(pcs)->pcop, peepBlock->target.wildpCodeOps[index]);
1711 switch(PCI(pcs)->pcop->type) {
1715 //n = PCOR(PCI(pcs)->pcop)->r->name;
1716 n = PCI(pcs)->pcop->name;
1720 n = PCI(pcs)->pcop->name;
1723 if(peepBlock->target.vars[index])
1724 return (strcmp(peepBlock->target.vars[index],n) == 0);
1726 DFPRINTF((stderr,"first time for a variable: %d, %s\n",index,n));
1727 peepBlock->target.vars[index] = n;
1732 } else if (PCI(pcd)->pcop->type == PO_LITERAL) {
1733 return pCodeOpCompare(PCI(pcs)->pcop, PCI(pcd)->pcop);
1736 /* FIXME - need an else to check the case when the destination
1737 * isn't a wild card */
1739 /* The pcd has no operand. Lines match if pcs has no operand either*/
1740 return (PCI(pcs)->pcop == NULL);
1744 /* Compare a wild instruction to a regular one. */
1746 if((pcd->type == PC_WILD) && (pcs->type == PC_OPCODE)) {
1748 index = PCW(pcd)->id;
1750 DFPRINTF((stderr,"%s comparing wild cards\n",__FUNCTION__));
1751 pcs->print(stderr,pcs);
1752 pcd->print(stderr,pcd);
1754 peepBlock->target.wildpCodes[PCW(pcd)->id] = pcs;
1756 if(!pCodePeepMatchLabels(peepBlock, pcs, pcd)) {
1757 DFPRINTF((stderr," Failing because labels don't match\n"));
1761 if(PCW(pcd)->mustBeBitSkipInst & !(PCI(pcs)->isBitInst && PCI(pcs)->isSkip)) {
1762 // doesn't match because the wild pcode must be a bit skip
1763 DFPRINTF((stderr," Failing match because bit skip is req\n"));
1764 //pcd->print(stderr,pcd);
1765 //pcs->print(stderr,pcs);
1769 if(PCW(pcd)->mustNotBeBitSkipInst & (PCI(pcs)->isBitInst && PCI(pcs)->isSkip)) {
1770 // doesn't match because the wild pcode must *not* be a bit skip
1771 DFPRINTF((stderr," Failing match because shouldn't be bit skip\n"));
1772 //pcd->print(stderr,pcd);
1773 //pcs->print(stderr,pcs);
1777 if(PCW(pcd)->operand) {
1778 PCOW(PCI(pcd)->pcop)->matched = PCI(pcs)->pcop;
1779 if(peepBlock->target.vars[index]) {
1780 int i = (strcmp(peepBlock->target.vars[index],PCI(pcs)->pcop->name) == 0);
1784 DFPRINTF((stderr," (matched)\n"));
1786 DFPRINTF((stderr," (no match: wild card operand mismatch\n"));
1787 DFPRINTF((stderr," peepblock= %s, pcodeop= %s\n",
1788 peepBlock->target.vars[index],
1789 PCI(pcs)->pcop->name));
1794 DFPRINTF((stderr," (matched %s\n",PCI(pcs)->pcop->name));
1795 peepBlock->target.vars[index] = PCI(pcs)->pcop->name;
1800 pcs = findNextInstruction(pcs->next);
1802 //DFPRINTF((stderr," (next to match)\n"));
1803 //pcs->print(stderr,pcs);
1804 } else if(pcd->next) {
1805 /* oops, we ran out of code, but there's more to the rule */
1809 return 1; /* wild card matches */
1815 /*-----------------------------------------------------------------*/
1816 /*-----------------------------------------------------------------*/
1817 void pCodePeepClrVars(pCodePeep *pcp)
1824 DFPRINTF((stderr," Clearing peep rule vars\n"));
1825 DFPRINTF((stderr," %d %d %d %d %d %d\n",
1826 pcp->target.nvars,pcp->target.nops,pcp->target.nwildpCodes,
1827 pcp->replace.nvars,pcp->replace.nops,pcp->replace.nwildpCodes));
1829 for(i=0;i<pcp->target.nvars; i++)
1830 pcp->target.vars[i] = NULL;
1831 for(i=0;i<pcp->target.nops; i++)
1832 pcp->target.wildpCodeOps[i] = NULL;
1833 for(i=0;i<pcp->target.nwildpCodes; i++)
1834 pcp->target.wildpCodes[i] = NULL;
1836 for(i=0;i<pcp->replace.nvars; i++)
1837 pcp->replace.vars[i] = NULL;
1838 for(i=0;i<pcp->replace.nops; i++)
1839 pcp->replace.wildpCodeOps[i] = NULL;
1840 for(i=0;i<pcp->replace.nwildpCodes; i++)
1841 pcp->replace.wildpCodes[i] = NULL;
1847 /*-----------------------------------------------------------------*/
1848 /* pCodeInsertAfter - splice in the pCode chain starting with pc2 */
1849 /* into the pCode chain containing pc1 */
1850 /*-----------------------------------------------------------------*/
1851 void pCodeInsertAfter(pCode *pc1, pCode *pc2)
1857 pc2->next = pc1->next;
1859 pc1->next->prev = pc2;
1867 /*------------------------------------------------------------------*/
1868 /* pCodeInsertBefore - splice in the pCode chain starting with pc2 */
1869 /* into the pCode chain containing pc1 */
1870 /*------------------------------------------------------------------*/
1871 void pCodeInsertBefore(pCode *pc1, pCode *pc2)
1877 pc2->prev = pc1->prev;
1879 pc1->prev->next = pc2;
1887 /*-----------------------------------------------------------------*/
1888 /* pCodeOpCopy - copy a pcode operator */
1889 /*-----------------------------------------------------------------*/
1890 pCodeOp *pCodeOpCopy(pCodeOp *pcop)
1892 pCodeOp *pcopnew=NULL;
1897 switch(pcop->type) {
1900 //DFPRINTF((stderr,"pCodeOpCopy bit\n"));
1901 pcopnew = Safe_calloc(1,sizeof(pCodeOpRegBit) );
1902 PCORB(pcopnew)->bit = PCORB(pcop)->bit;
1903 PCORB(pcopnew)->inBitSpace = PCORB(pcop)->inBitSpace;
1908 /* Here we expand the wild card into the appropriate type: */
1909 /* By recursively calling pCodeOpCopy */
1910 //DFPRINTF((stderr,"pCodeOpCopy wild\n"));
1911 if(PCOW(pcop)->matched)
1912 pcopnew = pCodeOpCopy(PCOW(pcop)->matched);
1915 pcopnew = pCodeOpCopy(PCOW(pcop)->subtype);
1916 pcopnew->name = Safe_strdup(PCOW(pcop)->pcwb->vars[PCOW(pcop)->id]);
1917 //DFPRINTF((stderr,"copied a wild op named %s\n",pcopnew->name));
1924 //DFPRINTF((stderr,"pCodeOpCopy label\n"));
1925 pcopnew = Safe_calloc(1,sizeof(pCodeOpLabel) );
1926 PCOLAB(pcopnew)->key = PCOLAB(pcop)->key;
1930 pcopnew = Safe_calloc(1,sizeof(pCodeOpImmd) );
1931 PCOI(pcopnew)->index = PCOI(pcop)->index;
1932 PCOI(pcopnew)->offset = PCOI(pcop)->offset;
1933 PCOI(pcopnew)->_const = PCOI(pcop)->_const;
1934 PCOI(pcopnew)->_function = PCOI(pcop)->_function;
1938 //DFPRINTF((stderr,"pCodeOpCopy lit\n"));
1939 pcopnew = Safe_calloc(1,sizeof(pCodeOpLit) );
1940 PCOL(pcopnew)->lit = PCOL(pcop)->lit;
1945 pcopnew = newpCodeOpBit(pcop->name, PCORB(pcop)->bit,PCORB(pcop)->inBitSpace);
1946 PCOR(pcopnew)->r = PCOR(pcop)->r;
1947 PCOR(pcopnew)->rIdx = PCOR(pcop)->rIdx;
1948 DFPRINTF((stderr," pCodeOpCopy Bit -register index\n"));
1952 case PO_GPR_POINTER:
1953 case PO_GPR_REGISTER:
1957 //DFPRINTF((stderr,"pCodeOpCopy GPR register\n"));
1958 pcopnew = Safe_calloc(1,sizeof(pCodeOpReg) );
1959 PCOR(pcopnew)->r = PCOR(pcop)->r;
1960 PCOR(pcopnew)->rIdx = PCOR(pcop)->rIdx;
1961 PCOR(pcopnew)->instance = PCOR(pcop)->instance;
1962 DFPRINTF((stderr," register index %d\n", PCOR(pcop)->r->rIdx));
1966 //fprintf(stderr,"pCodeOpCopy PO_DIR\n");
1967 pcopnew = Safe_calloc(1,sizeof(pCodeOpReg) );
1968 PCOR(pcopnew)->r = PCOR(pcop)->r;
1969 PCOR(pcopnew)->rIdx = PCOR(pcop)->rIdx;
1970 PCOR(pcopnew)->instance = PCOR(pcop)->instance;
1973 DFPRINTF((stderr,"pCodeOpCopy PO_STATUS\n"));
1974 case PO_SFR_REGISTER:
1982 //DFPRINTF((stderr,"pCodeOpCopy register type %d\n", pcop->type));
1983 pcopnew = Safe_calloc(1,sizeof(pCodeOp) );
1987 pcopnew->type = pcop->type;
1989 pcopnew->name = Safe_strdup(pcop->name);
1991 pcopnew->name = NULL;
1997 /*-----------------------------------------------------------------*/
1998 /* pCodeCopy - copy a pcode */
1999 /*-----------------------------------------------------------------*/
2000 static pCode *pCodeInstructionCopy(pCodeInstruction *pci,int invert)
2002 pCodeInstruction *new_pci;
2005 new_pci = PCI(newpCode(pci->inverted_op,pci->pcop));
2007 new_pci = PCI(newpCode(pci->op,pci->pcop));
2009 new_pci->pc.pb = pci->pc.pb;
2010 new_pci->from = pci->from;
2011 new_pci->to = pci->to;
2012 new_pci->label = pci->label;
2013 new_pci->pcflow = pci->pcflow;
2015 return PCODE(new_pci);
2018 /*-----------------------------------------------------------------*/
2019 /*-----------------------------------------------------------------*/
2020 void pCodeDeleteChain(pCode *f,pCode *t)
2026 DFPRINTF((stderr,"delete pCode:\n"));
2028 //f->print(stderr,f);
2029 //f->delete(f); this dumps core...
2036 /*-----------------------------------------------------------------*/
2037 /*-----------------------------------------------------------------*/
2038 int pCodePeepMatchRule(pCode *pc)
2040 pCodePeep *peepBlock;
2042 pCodeCSource *pc_cline=NULL;
2046 peeprules = (_DLL *)peepSnippets;
2049 peepBlock = ((pCodePeepSnippets*)peeprules)->peep;
2051 if(!peepBlock || /*!peepBlock->target ||*/ !peepBlock->target.pb->pcHead) {
2052 fprintf(stderr, "skipping rule because target pb is NULL\n");
2056 pCodePeepClrVars(peepBlock);
2059 if(IS_PCCOMMENT(pcin))
2060 pc = pcin = findNextInstruction(pcin->next);
2062 pcin = pc = findNextInstruction(pc);
2064 pct = peepBlock->target.pb->pcHead;
2067 pCode *pcr = peepBlock->replace.pb->pcHead;
2068 if(pcr) pct->print(stderr,pcr);
2072 while(pct && pcin) {
2074 if(! (matched = pCodePeepMatchLine(peepBlock, pcin,pct)))
2077 pcin = findNextInstruction(pcin->next);
2080 //DFPRINTF((stderr," matched\n"));
2083 DFPRINTF((stderr," partial match... no more code\n"));
2084 fprintf(stderr," partial match... no more code\n");
2088 DFPRINTF((stderr," end of rule\n"));
2092 if(matched && pcin) {
2094 /* So far we matched the rule up to the point of the conditions .
2095 * In other words, all of the opcodes match. Now we need to see
2096 * if the post conditions are satisfied.
2097 * First we check the 'postFalseCond'. This means that we check
2098 * to see if any of the subsequent pCode's in the pCode chain
2099 * following the point just past where we have matched depend on
2100 * the `postFalseCond' as input then we abort the match
2102 DFPRINTF((stderr," matched rule so far, now checking conditions\n"));
2103 //pcin->print(stderr,pcin);
2105 if (pcin && peepBlock->postFalseCond &&
2106 (pCodeSearchCondition(pcin,peepBlock->postFalseCond) > 0) )
2109 //fprintf(stderr," condition results = %d\n",pCodeSearchCondition(pcin,peepBlock->postFalseCond));
2112 //if(!matched) fprintf(stderr,"failed on conditions\n");
2121 /* We matched a rule! Now we have to go through and remove the
2122 inefficient code with the optimized version */
2124 DFPRINTF((stderr, "Found a pcode peep match:\nRule:\n"));
2125 printpCodeString(stderr,peepBlock->target.pb->pcHead,10);
2126 DFPRINTF((stderr,"first thing matched\n"));
2127 pc->print(stderr,pc);
2129 DFPRINTF((stderr,"last thing matched\n"));
2130 pcin->print(stderr,pcin);
2135 /* Unlink the original code */
2137 pcprev->next = pcin;
2139 pcin->prev = pc->prev;
2145 /* Converted the deleted pCodes into comments */
2148 pCodeCSource *pc_cline2=NULL;
2153 while(pc && pc!=pcin) {
2155 if(pc->type == PC_OPCODE && PCI(pc)->cline) {
2157 pc_cline2->pc.next = PCODE(PCI(pc)->cline);
2158 pc_cline2 = PCCS(pc_cline2->pc.next);
2160 pc_cline = pc_cline2 = PCI(pc)->cline;
2161 pc_cline->pc.seq = pc->seq;
2165 pCode2str(&buf[2], 254, pc);
2166 pCodeInsertAfter(pcprev, newpCodeCharP(buf));
2167 pcprev = pcprev->next;
2172 pc_cline2->pc.next = NULL;
2177 pCodeDeleteChain(pc,pcin);
2179 /* Generate the replacement code */
2181 pcr = peepBlock->replace.pb->pcHead; // This is the replacement code
2185 /* If the replace pcode is an instruction with an operand, */
2186 /* then duplicate the operand (and expand wild cards in the process). */
2187 if(pcr->type == PC_OPCODE) {
2188 if(PCI(pcr)->pcop) {
2189 /* The replacing instruction has an operand.
2191 if(PCI(pcr)->pcop->type == PO_WILD) {
2192 int index = PCOW(PCI(pcr)->pcop)->id;
2193 //DFPRINTF((stderr,"copying wildopcode\n"));
2194 if(peepBlock->target.wildpCodeOps[index])
2195 pcop = pCodeOpCopy(peepBlock->target.wildpCodeOps[index]);
2197 DFPRINTF((stderr,"error, wildopcode in replace but not source?\n"));
2199 pcop = pCodeOpCopy(PCI(pcr)->pcop);
2201 //DFPRINTF((stderr,"inserting pCode\n"));
2202 pCodeInsertAfter(pc, newpCode(PCI(pcr)->op,pcop));
2203 } else if (pcr->type == PC_WILD) {
2204 if(PCW(pcr)->invertBitSkipInst)
2205 DFPRINTF((stderr,"We need to invert the bit skip instruction\n"));
2206 pCodeInsertAfter(pc,
2207 pCodeInstructionCopy(PCI(peepBlock->target.wildpCodes[PCW(pcr)->id]),
2208 PCW(pcr)->invertBitSkipInst));
2209 } else if (pcr->type == PC_COMMENT) {
2210 pCodeInsertAfter(pc, newpCodeCharP( ((pCodeComment *)(pcr))->comment));
2216 DFPRINTF((stderr," NEW Code:"));
2217 if(pc) pc->print(stderr,pc);
2222 /* We have just replaced the inefficient code with the rule.
2223 * Now, we need to re-add the C-source symbols if there are any */
2227 pc = findNextInstruction(pc->next);
2228 PCI(pc)->cline = pc_cline;
2229 pc_cline = PCCS(pc_cline->pc.next);
2236 peeprules = peeprules->next;
2238 DFPRINTF((stderr," no rule matched\n"));