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
31 #if defined(__BORLANDC__) || defined(_MSC_VER)
32 #define STRCASECMP stricmp
34 #define STRCASECMP strcasecmp
37 pCodeOp *popCopyGPR2Bit(pCodeOpReg *pc, int bitval);
40 pCodeOp *newpCodeOpWild(int id, pCodePeep *pcp, 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 void pBlockMergeLabels(pBlock *pb);
45 char *pCode2str(char *str, int size, pCode *pc);
47 extern pCodeInstruction *pic14Mnemonics[];
50 /****************************************************************/
52 * rootRules - defined in SDCCpeep.c
53 * This is a pointer to the (parsed) peephole rules that are
54 * defined in peep.def.
57 //extern peepRule *rootRules;
62 /****************************************************************/
63 /****************************************************************/
71 typedef struct pCodePeepSnippets
78 /****************************************************************/
82 /****************************************************************/
84 static pCodePeepSnippets *peepSnippets=NULL;
86 /****************************************************************/
90 /****************************************************************/
92 static pCodePeep *curPeep=NULL;
94 /****************************************************************/
98 /****************************************************************/
100 static pBlock *curBlock=NULL;
103 /****************************************************************/
105 /* max wild cards in a peep rule */
107 /****************************************************************/
109 static int sMaxWildVar = 0;
110 static int sMaxWildMnem = 0;
113 typedef struct pCodeToken
115 int tt; // token type;
124 pCodeToken tokArr[50];
141 typedef struct parsedPattern {
142 struct pcPattern *pcp;
146 #define MAX_PARSEDPATARR 50
147 parsedPattern parsedPatArr[MAX_PARSEDPATARR];
148 unsigned int parsedPatIdx=0;
161 static char pcpat_label[] = {PCT_PERCENT, PCT_NUMBER, PCT_COLON, 0};
162 static char pcpat_number[] = {PCT_NUMBER, 0};
163 static char pcpat_string[] = {PCT_STRING, 0};
164 static char pcpat_wildString[] = {PCT_PERCENT, PCT_STRING, 0};
165 static char pcpat_wildVar[] = {PCT_PERCENT, PCT_NUMBER, 0};
166 static char pcpat_comma[] = {PCT_COMMA, 0};
167 static char pcpat_comment[] = {PCT_COMMENT, 0};
170 typedef struct pcPattern {
171 char pt; // Pattern type
172 char *tokens; // list of tokens that describe the pattern
173 void * (*f) (void *);
176 pcPattern pcpArr[] = {
177 {PCP_LABEL, pcpat_label, NULL},
178 {PCP_WILDSTR, pcpat_wildString, NULL},
179 {PCP_STR, pcpat_string, NULL},
180 {PCP_WILDVAR, pcpat_wildVar, NULL},
181 {PCP_COMMA, pcpat_comma, NULL},
182 {PCP_COMMENT, pcpat_comment, NULL},
183 {PCP_NUMBER, pcpat_number, NULL}
186 #define PCPATTERNS (sizeof(pcpArr)/sizeof(pcPattern))
188 // Assembly Line Token
201 static char alt_comment[] = { PCP_COMMENT, 0};
202 static char alt_label[] = { PCP_LABEL, 0};
203 static char alt_mnem0[] = { PCP_STR, 0};
204 static char alt_mnem0a[] = { PCP_WILDVAR, 0};
205 static char alt_mnem1[] = { PCP_STR, PCP_STR, 0};
206 static char alt_mnem1a[] = { PCP_STR, PCP_WILDVAR, 0};
207 static char alt_mnem1b[] = { PCP_STR, PCP_NUMBER, 0};
208 static char alt_mnem2[] = { PCP_STR, PCP_STR, PCP_COMMA, PCP_STR, 0};
209 static char alt_mnem2a[] = { PCP_STR, PCP_WILDVAR, PCP_COMMA, PCP_STR, 0};
211 static void * cvt_altpat_label(void *pp);
212 static void * cvt_altpat_comment(void *pp);
213 static void * cvt_altpat_mnem0(void *pp);
214 static void * cvt_altpat_mnem0a(void *pp);
215 static void * cvt_altpat_mnem1(void *pp);
216 static void * cvt_altpat_mnem1a(void *pp);
217 static void * cvt_altpat_mnem1b(void *pp);
218 static void * cvt_altpat_mnem2(void *pp);
219 static void * cvt_altpat_mnem2a(void *pp);
221 pcPattern altArr[] = {
222 {ALT_LABEL, alt_label, cvt_altpat_label},
223 {ALT_COMMENT, alt_comment,cvt_altpat_comment},
224 {ALT_MNEM2A, alt_mnem2a, cvt_altpat_mnem2a},
225 {ALT_MNEM2, alt_mnem2, cvt_altpat_mnem2},
226 {ALT_MNEM1B, alt_mnem1b, cvt_altpat_mnem1b},
227 {ALT_MNEM1A, alt_mnem1a, cvt_altpat_mnem1a},
228 {ALT_MNEM1, alt_mnem1, cvt_altpat_mnem1},
229 {ALT_MNEM0A, alt_mnem0a, cvt_altpat_mnem0a},
230 {ALT_MNEM0, alt_mnem0, cvt_altpat_mnem0},
234 #define ALTPATTERNS (sizeof(altArr)/sizeof(pcPattern))
236 // forward declarations
237 static void * DLL_append(_DLL *list, _DLL *next);
239 /*-----------------------------------------------------------------*/
240 /* cvt_extract_destination - helper function extracts the register */
241 /* destination from a parsedPattern. */
243 /*-----------------------------------------------------------------*/
244 static int cvt_extract_destination(parsedPattern *pp)
247 if(pp->pct[0].tt == PCT_STRING) {
249 // just check first letter for now
251 if(toupper(*pp->pct[0].tok.s) == 'F')
254 } else if (pp->pct[0].tt == PCT_NUMBER) {
264 /*-----------------------------------------------------------------*/
265 /* pCodeOp *cvt_extract_status(char *reg, char *bit) */
266 /* if *reg is the "status" register and *bit is one of the */
267 /* status bits, then this function will create a new pCode op */
268 /* containing the status register. */
269 /*-----------------------------------------------------------------*/
271 static pCodeOp *cvt_extract_status(char *reg, char *bit)
275 if(STRCASECMP(reg, pc_status.pcop.name))
282 if(toupper(*bit) == 'C')
283 return PCOP(popCopyGPR2Bit(&pc_status,PIC_C_BIT));
284 if(toupper(*bit) == 'Z')
285 return PCOP(popCopyGPR2Bit(&pc_status,PIC_Z_BIT));
289 if(len ==2 && toupper(bit[0]) == 'D' && toupper(bit[1]) == 'C')
290 return PCOP(popCopyGPR2Bit(&pc_status,PIC_DC_BIT));
296 /*-----------------------------------------------------------------*/
297 /* cvt_altpat_label - convert assembly line type to a pCode label */
298 /* INPUT: pointer to the parsedPattern */
302 /* label pattern => '%' number ':' */
303 /* at this point, we wish to extract only the 'number' */
305 /*-----------------------------------------------------------------*/
306 static void * cvt_altpat_label(void *pp)
308 parsedPattern *p = pp;
310 fprintf(stderr,"altpat_label with ID = %d\n",p->pct[1].tok.n);
311 return newpCodeLabel(-p->pct[1].tok.n);
315 /*-----------------------------------------------------------------*/
316 /* cvt_altpat_comment - convert assembly line type to a comment */
317 /* INPUT: pointer to the parsedPattern */
319 /* pp[0] - comment */
322 /*-----------------------------------------------------------------*/
323 static void * cvt_altpat_comment(void *pp)
325 parsedPattern *p = pp;
327 fprintf(stderr,"altpat_comment = %s\n",p->pct[0].tok.s);
328 return newpCodeCharP(p->pct[0].tok.s);
332 /*-----------------------------------------------------------------*/
333 /*-----------------------------------------------------------------*/
334 static void * cvt_altpat_mnem0(void *pp)
336 parsedPattern *p = pp;
339 pCodeInstruction *pci=NULL;
341 fprintf(stderr,"altpat_mnem0 %s\n", p->pct[0].tok.s);
343 opcode = getpCode(p->pct[0].tok.s,0);
345 fprintf(stderr, "Bad mnemonic\n");
349 pci = PCI(newpCode(opcode, NULL));
352 fprintf(stderr,"couldn't find mnemonic\n");
358 /*-----------------------------------------------------------------*/
359 /* cvt_altpat_mem0a - convert assembly line type to a wild pCode */
362 /* pp[0] - wild var */
364 /*-----------------------------------------------------------------*/
365 static void * cvt_altpat_mnem0a(void *pp)
367 parsedPattern *p = pp;
369 fprintf(stderr,"altpat_mnem0a wild mnem # %d\n", p[0].pct[1].tok.n);
371 /* Save the index of the maximum wildcard mnemonic */
373 if(p[0].pct[1].tok.n > sMaxWildVar)
374 sMaxWildMnem = p[0].pct[1].tok.n;
376 return newpCodeWild(p[0].pct[1].tok.n,NULL,NULL);
380 /*-----------------------------------------------------------------*/
381 /* cvt_altpat_mem1 - convert assembly line type to a pCode */
382 /* instruction with 1 operand. */
385 /* pp[1] - Operand */
387 /*-----------------------------------------------------------------*/
388 static void * cvt_altpat_mnem1(void *pp)
391 parsedPattern *p = pp;
394 pCodeInstruction *pci=NULL;
397 fprintf(stderr,"altpat_mnem1 %s var %s\n", p->pct[0].tok.s,p[1].pct[0].tok.s);
399 opcode = getpCode(p->pct[0].tok.s,0);
401 fprintf(stderr, "Bad mnemonic\n");
405 if(pic14Mnemonics[opcode]->bit_inst)
406 pcosubtype = newpCodeOp(p[1].pct[0].tok.s,PO_BIT);
408 pcosubtype = newpCodeOp(p[1].pct[0].tok.s,PO_GPR_REGISTER);
411 pci = PCI(newpCode(opcode, pcosubtype));
414 fprintf(stderr,"couldn't find mnemonic\n");
420 /*-----------------------------------------------------------------*/
421 /* cvt_altpat_mem1a - convert assembly line type to a pCode */
422 /* instruction with 1 wild operand. */
425 /* pp[1] - wild var */
427 /*-----------------------------------------------------------------*/
428 static void * cvt_altpat_mnem1a(void *pp)
430 parsedPattern *p = pp;
433 pCodeInstruction *pci=NULL;
436 fprintf(stderr,"altpat_mnem1a %s var %d\n", p->pct[0].tok.s,p[1].pct[1].tok.n);
438 opcode = getpCode(p->pct[0].tok.s,0);
440 fprintf(stderr, "Bad mnemonic\n");
444 if(pic14Mnemonics[opcode]->bit_inst)
445 pcosubtype = newpCodeOpBit(NULL,-1,0);
447 pcosubtype = newpCodeOp(NULL,PO_GPR_REGISTER);
450 pci = PCI(newpCode(opcode,
451 newpCodeOpWild(p[1].pct[1].tok.n, curPeep, pcosubtype)));
453 /* Save the index of the maximum wildcard variable */
454 if(p[1].pct[1].tok.n > sMaxWildVar)
455 sMaxWildVar = p[1].pct[1].tok.n;
458 fprintf(stderr,"couldn't find mnemonic\n");
464 /*-----------------------------------------------------------------*/
465 /*-----------------------------------------------------------------*/
466 static void * cvt_altpat_mnem1b(void *pp)
468 parsedPattern *p = pp;
471 pCodeInstruction *pci=NULL;
473 fprintf(stderr,"altpat_mnem1b %s var %d\n", p->pct[0].tok.s,p[1].pct[0].tok.n);
475 opcode = getpCode(p->pct[0].tok.s,0);
477 fprintf(stderr, "Bad mnemonic\n");
481 pci = PCI(newpCode(opcode, newpCodeOpLit(p[1].pct[0].tok.n) ));
484 fprintf(stderr,"couldn't find mnemonic\n");
490 /*-----------------------------------------------------------------*/
491 /*-----------------------------------------------------------------*/
492 static void * cvt_altpat_mnem2(void *pp)
494 parsedPattern *p = pp;
498 pCodeInstruction *pci=NULL;
501 dest = cvt_extract_destination(&p[3]);
503 fprintf(stderr,"altpat_mnem2 %s var %s destination %s(%d)\n",
510 opcode = getpCode(p->pct[0].tok.s,dest);
512 fprintf(stderr, "Bad mnemonic\n");
516 if(pic14Mnemonics[opcode]->bit_inst) {
517 pcosubtype = cvt_extract_status(p[1].pct[0].tok.s, p[3].pct[0].tok.s);
518 if(pcosubtype == NULL) {
519 fprintf(stderr, "bad operand?\n");
524 pcosubtype = newpCodeOp(p[1].pct[0].tok.s,PO_GPR_REGISTER);
527 pci = PCI(newpCode(opcode,pcosubtype));
530 fprintf(stderr,"couldn't find mnemonic\n");
536 /*-----------------------------------------------------------------*/
537 /* cvt_altpat_mem2a - convert assembly line type to a pCode */
538 /* instruction with 1 wild operand and a */
539 /* destination operand (e.g. w or f) */
542 /* pp[1] - wild var */
544 /* pp[3] - destination */
546 /*-----------------------------------------------------------------*/
547 static void * cvt_altpat_mnem2a(void *pp)
549 parsedPattern *p = pp;
553 pCodeInstruction *pci=NULL;
556 dest = cvt_extract_destination(&p[3]);
558 fprintf(stderr,"altpat_mnem2a %s var %d destination %s(%d)\n",
565 opcode = getpCode(p->pct[0].tok.s,dest);
567 fprintf(stderr, "Bad mnemonic\n");
571 if(pic14Mnemonics[opcode]->bit_inst)
572 pcosubtype = newpCodeOp(NULL,PO_BIT);
574 pcosubtype = newpCodeOp(NULL,PO_GPR_REGISTER);
577 pci = PCI(newpCode(opcode,
578 newpCodeOpWild(p[1].pct[1].tok.n, curPeep, pcosubtype)));
580 /* Save the index of the maximum wildcard variable */
581 if(p[1].pct[1].tok.n > sMaxWildVar)
582 sMaxWildVar = p[1].pct[1].tok.n;
585 fprintf(stderr,"couldn't find mnemonic\n");
591 /*-----------------------------------------------------------------*/
592 /* tokenizeLineNode - Convert a string (of char's) that was parsed */
593 /* by SDCCpeeph.c into a string of tokens. */
596 /* The tokenizer is of the classic type. When an item is encounterd*/
597 /* it is converted into a token. The token is a structure that */
598 /* encodes the item's type and it's value (when appropriate). */
600 /* Accepted token types: */
601 /* SPACE NUMBER STRING % : , ; */
605 /*-----------------------------------------------------------------*/
608 static void tokenizeLineNode(char *ln)
611 tokIdx = 0; // Starting off at the beginning
612 tokArr[0].tt = PCT_NULL; // and assume invalid character for first token.
620 // add a SPACE token and eat the extra spaces.
621 tokArr[tokIdx++].tt = PCT_SPACE;
622 while (isspace (*ln))
629 tokArr[tokIdx].tt = PCT_NUMBER;
630 tokArr[tokIdx++].tok.n = strtol(ln, &ln, 0);
638 tokArr[tokIdx++].tt = PCT_PERCENT;
641 tokArr[tokIdx++].tt = PCT_COLON;
644 tokArr[tokIdx].tok.s = Safe_strdup(ln);
645 tokArr[tokIdx++].tt = PCT_COMMENT;
646 tokArr[tokIdx].tt = PCT_NULL;
649 tokArr[tokIdx++].tt = PCT_COMMA;
658 while( (isalpha(*ln) || isdigit(*ln)) && i<49)
664 tokArr[tokIdx].tok.s = Safe_strdup(buffer);
665 tokArr[tokIdx++].tt = PCT_STRING;
670 /* Advance to next character in input string .
671 * Note, if none of the tests passed above, then
672 * we effectively ignore the `bad' character.
673 * Since the line has already been parsed by SDCCpeeph,
674 * chance are that there are no invalid characters... */
680 tokArr[tokIdx].tt = 0;
684 /*-----------------------------------------------------------------*/
685 /*-----------------------------------------------------------------*/
689 void dump1Token(pCodeTokens tt)
694 fprintf(stderr, " space ");
697 fprintf(stderr, " pct ");
701 fprintf(stderr, " col ");
705 fprintf(stderr, " comma , ");
708 fprintf(stderr, " comment ");
709 //fprintf(stderr,"%s",tokArr[i].tok.s);
712 fprintf(stderr, " str ");
713 //fprintf(stderr,"%s",tokArr[i].tok.s);
716 fprintf(stderr, " num ");
717 //fprintf(stderr,"%d",tokArr[i].tok.n);
720 fprintf(stderr, " null ");
726 /*-----------------------------------------------------------------*/
727 /*-----------------------------------------------------------------*/
729 int pcComparePattern(pCodeToken *pct, char *pat, int max_tokens)
733 if(!pct || !pat || !*pat)
736 //fprintf(stderr,"comparing against:\n");
738 while(i < max_tokens) {
741 //fprintf(stderr,"matched\n");
745 //dump1Token(*pat); fprintf(stderr,"\n");
759 /*-----------------------------------------------------------------*/
760 /*-----------------------------------------------------------------*/
762 int altComparePattern( char *pct, parsedPattern *pat, int max_tokens)
766 if(!pct || !pat || !*pct)
770 while(i < max_tokens) {
773 //fprintf(stderr,"matched\n");
777 //dump1Token(*pat); fprintf(stderr,"\n");
779 if( !pat || !pat->pcp )
782 if (pat->pcp->pt != *pct)
785 //fprintf(stderr," pct=%d\n",*pct);
794 /*-----------------------------------------------------------------*/
795 /*-----------------------------------------------------------------*/
797 int advTokIdx(int *v, int amt)
800 if((unsigned) (*v + amt) > tokIdx)
808 /*-----------------------------------------------------------------*/
809 /* parseTokens - convert the tokens corresponding to a single line */
810 /* of a peep hole assembly into a pCode object. */
815 /* This is a simple parser that looks for strings of the type */
816 /* allowed in the peep hole definition file. Essentially the format*/
817 /* is the same as a line of assembly: */
819 /* label: mnemonic op1, op2, op3 ; comment */
821 /* Some of these items aren't present. It's the job of the parser */
822 /* to determine which are and convert those into the appropriate */
824 /*-----------------------------------------------------------------*/
826 void parseTokens(void)
834 for(i=0; i<=tokIdx; i++)
835 dump1Token(tokArr[i].tt);
847 char * cPmnem = NULL; // Pointer to non-wild mnemonic (if any)
848 char * cP1stop = NULL;
849 char * cP2ndop = NULL;
851 //pCodeOp *pcl = NULL; // Storage for a label
852 //pCodeOp *pco1 = NULL; // 1st operand
853 //pCodeOp *pco2 = NULL; // 2nd operand
854 //pCode *pc = NULL; // Mnemonic
865 ParseStates state = PS_START;
872 if( ((tokArr[ltokIdx].tt == PCT_SPACE) )
873 && (advTokIdx(<okIdx, 1)) ) // eat space
877 j = pcComparePattern(&tokArr[ltokIdx], pcpArr[lpcpIdx].tokens, tokIdx +1);
880 switch(pcpArr[lpcpIdx].pt) {
882 if(state == PS_START){
883 fprintf(stderr," label\n");
884 state = PS_HAVE_LABEL;
886 fprintf(stderr," bad state (%d) for label\n",state);
890 fprintf(stderr," %s is",tokArr[ltokIdx].tok.s);
894 fprintf(stderr," mnem\n");
895 cPmnem = tokArr[ltokIdx].tok.s;
896 state = PS_HAVE_MNEM;
899 fprintf(stderr," 1st operand\n");
900 cP1stop = tokArr[ltokIdx].tok.s;
901 //pco1 = newpCodeOp(NULL,PO_GPR_REGISTER);
902 state = PS_HAVE_1OPERAND;
904 case PS_HAVE_1OPERAND:
905 fprintf(stderr," error expecting comma\n");
908 fprintf(stderr," 2 operands\n");
909 cP2ndop = tokArr[ltokIdx].tok.s;
911 case PS_HAVE_2OPERANDS:
920 fprintf(stderr," wild mnem\n");
921 state = PS_HAVE_MNEM;
924 fprintf(stderr," 1st operand is wild\n");
925 state = PS_HAVE_1OPERAND;
927 case PS_HAVE_1OPERAND:
928 fprintf(stderr," error expecting comma\n");
931 fprintf(stderr," 2nd operand is wild\n");
933 case PS_HAVE_2OPERANDS:
942 fprintf(stderr," ERROR number\n");
945 fprintf(stderr," 1st operand is a number\n");
946 state = PS_HAVE_1OPERAND;
948 case PS_HAVE_1OPERAND:
949 fprintf(stderr," error expecting comma\n");
952 fprintf(stderr," 2nd operand is a number\n");
954 case PS_HAVE_2OPERANDS:
962 if(state == PS_HAVE_1OPERAND){
963 fprintf(stderr," got a comma\n");
964 state = PS_HAVE_COMMA;
966 fprintf(stderr," unexpected comma\n");
970 parsedPatArr[lparsedPatIdx].pcp = &pcpArr[lpcpIdx];
971 parsedPatArr[lparsedPatIdx].pct = &tokArr[ltokIdx];
974 //dump1Token(tokArr[ltokIdx].tt);
976 if(advTokIdx(<okIdx, strlen(pcpArr[lpcpIdx].tokens) ) ) {
977 fprintf(stderr," reached end \n");
984 } while ((++lpcpIdx < PCPATTERNS) && !matching);
988 parsedPatArr[lparsedPatIdx].pcp = NULL;
989 parsedPatArr[lparsedPatIdx].pct = NULL;
995 if( (c=altComparePattern( altArr[k].tokens, &parsedPatArr[j],10) ) ) {
998 pc = altArr[k].f(&parsedPatArr[j]);
1000 pc->print(stderr,pc);
1001 //if(pc && pc->destruct) pc->destruct(pc); dumps core?
1003 addpCode2pBlock(curBlock, pc);
1009 while(j<=lparsedPatIdx && k<ALTPATTERNS);
1012 fprintf(stderr,"\nConverting parsed line to pCode:\n\n");
1016 if(parsedPatArr[j].pcp && parsedPatArr[j].pcp->f )
1017 parsedPatArr[j].pcp->f(&parsedPatArr[j]);
1018 fprintf(stderr," %d",parsedPatArr[j].pcp->pt);
1021 while(j<lparsedPatIdx);
1023 fprintf(stderr,"\n");
1030 /*-----------------------------------------------------------------*/
1032 /*-----------------------------------------------------------------*/
1033 void peepRuleBlock2pCodeBlock( lineNode *ln)
1039 for( ; ln; ln = ln->next) {
1041 fprintf(stderr,"%s\n",ln->line);
1043 tokenizeLineNode(ln->line);
1049 /*-----------------------------------------------------------------*/
1050 /* peepRuleCondition */
1051 /*-----------------------------------------------------------------*/
1052 static void peepRuleCondition(char *cond)
1057 fprintf(stderr,"\nCondition: %s\n",cond);
1059 /* brute force compares for now */
1061 if(STRCASECMP(cond, "NZ") == 0) {
1062 fprintf(stderr,"found NZ\n");
1063 curPeep->postFalseCond = PCC_Z;
1068 /*-----------------------------------------------------------------*/
1069 /* peepRules2pCode - parse the "parsed" peep hole rules to generate*/
1072 /* SDCCpeeph parses the peep rules file and extracts variables, */
1073 /* removes white space, and checks the syntax. This function */
1074 /* extends that processing to produce pCode objects. You can kind */
1075 /* think of this function as an "assembler", though instead of */
1076 /* taking raw text to produce machine code, it produces pCode. */
1078 /*-----------------------------------------------------------------*/
1080 void peepRules2pCode(peepRule *rules)
1084 pCodePeepSnippets *pcps;
1086 /* The rules are in a linked-list. Each rule has two portions */
1087 /* There's the `target' and there's the `replace'. The target */
1088 /* is compared against the SDCC generated code and if it */
1089 /* matches, it gets replaced by the `replace' block of code. */
1091 /* Here we loop through each rule and convert the target's and*/
1092 /* replace's into pCode target and replace blocks */
1094 for (pr = rules; pr; pr = pr->next) {
1096 fprintf(stderr,"\nRule:\n\n");
1098 pcps = Safe_calloc(1,sizeof(pCodePeepSnippets));
1099 curPeep = pcps->peep = Safe_calloc(1,sizeof(pCodePeep));
1101 curPeep->vars = NULL;
1102 curPeep->wildpCodes = NULL; curPeep->wildpCodeOps = NULL;
1103 curPeep->postFalseCond = PCC_NONE;
1104 curPeep->postTrueCond = PCC_NONE;
1106 peepSnippets = DLL_append((_DLL*)peepSnippets,(_DLL*)pcps);
1108 curPeep->target = curBlock = newpCodeChain(NULL, 'W', NULL);
1112 /* Convert the target block */
1113 peepRuleBlock2pCodeBlock(pr->match);
1115 fprintf(stderr,"finished target, here it is in pcode form:\n");
1116 printpBlock(stderr, curBlock);
1118 fprintf(stderr,"target with labels merged:\n");
1119 pBlockMergeLabels(curBlock);
1120 printpBlock(stderr, curBlock);
1122 fprintf(stderr,"\nReplaced by:\n");
1125 curPeep->replace = curBlock = newpCodeChain(NULL, 'W', NULL);
1127 /* Convert the replace block */
1128 peepRuleBlock2pCodeBlock(pr->replace);
1130 fprintf(stderr,"finished replace block, here it is in pcode form:\n");
1131 printpBlock(stderr, curBlock);
1133 fprintf(stderr,"replace with labels merged:\n");
1134 pBlockMergeLabels(curBlock);
1135 printpBlock(stderr, curBlock);
1137 peepRuleCondition(pr->cond);
1139 /* The rule has been converted to pCode. Now allocate
1140 * space for the wildcards */
1143 curPeep->nvars = sMaxWildVar;
1144 curPeep->vars = Safe_calloc(sMaxWildVar, sizeof(char *));
1146 curPeep->nops = sMaxWildVar;
1147 curPeep->wildpCodeOps = Safe_calloc(sMaxWildVar, sizeof(pCodeOp *));
1149 curPeep->nwildpCodes = ++sMaxWildMnem;
1150 curPeep->wildpCodes = Safe_calloc(sMaxWildMnem, sizeof(char *));
1153 //return; // debug ... don't want to go through all the rules yet
1158 void printpCodeString(FILE *of, pCode *pc, int max)
1162 while(pc && (i++<max)) {
1168 /*-----------------------------------------------------------------*/
1169 /* _DLL * DLL_append */
1171 /* Append a _DLL object to the end of a _DLL (doubly linked list) */
1172 /* If The list to which we want to append is non-existant then one */
1173 /* is created. Other wise, the end of the list is sought out and */
1174 /* a new DLL object is appended to it. In either case, the void */
1175 /* *data is added to the newly created DLL object. */
1176 /*-----------------------------------------------------------------*/
1178 static void * DLL_append(_DLL *list, _DLL *next)
1183 /* If there's no list, then create one: */
1185 next->next = next->prev = NULL;
1190 /* Search for the end of the list. */
1195 /* Now append the new DLL object */
1206 /*-----------------------------------------------------------------
1208 pCode peephole optimization
1211 The pCode "peep hole" optimization is not too unlike the peep hole
1212 optimization in SDCCpeeph.c. The major difference is that here we
1213 use pCode's whereas there we use ASCII strings. The advantage with
1214 pCode's is that we can ascertain flow information in the instructions
1218 <FIX ME> - elaborate...
1220 -----------------------------------------------------------------*/
1224 /*-----------------------------------------------------------------*/
1225 /* pCodeSearchCondition - Search a pCode chain for a 'condition' */
1227 /* return conditions */
1228 /* 1 - The Condition was found for a pCode's input */
1229 /* 0 - No matching condition was found for the whole chain */
1230 /* -1 - The Condition was found for a pCode's output */
1232 /*-----------------------------------------------------------------*/
1233 int pCodeSearchCondition(pCode *pc, unsigned int cond)
1238 /* If we reach a function end (presumably an end since we most
1239 probably began the search in the middle of a function), then
1240 the condition was not found. */
1241 if(pc->type == PC_FUNCTION)
1244 if(pc->type == PC_OPCODE) {
1245 if(PCI(pc)->inCond & cond)
1247 if(PCI(pc)->outCond & cond)
1257 int pCodePeepMatchLabels(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
1261 /* Check for a label associated with this wild pCode */
1262 // If the wild card has a label, make sure the source code does too.
1269 pcl = pcd->label->pc;
1270 //labindex = PCOW(pcl)->id;
1271 labindex = -PCL(pcl)->key;
1272 fprintf(stderr,"label id = %d (labindex = %d)\n",PCL(pcl)->key,labindex);
1273 if(peepBlock->vars[labindex] == NULL) {
1274 // First time to encounter this label
1275 peepBlock->vars[labindex] = PCL(pcs->label->pc)->label;
1276 fprintf(stderr,"first time for a label: %d %s\n",labindex, peepBlock->vars[labindex]);
1278 if(strcmp(peepBlock->vars[labindex],PCL(pcs->label->pc)->label) != 0) {
1279 fprintf(stderr,"labels don't match\n");
1282 fprintf(stderr,"matched a label\n");
1285 fprintf(stderr,"destination doesn't have a label\n");
1295 /*-----------------------------------------------------------------*/
1296 /* pCodePeepMatchLine - Compare source and destination pCodes to */
1297 /* see they're the same. */
1299 /* In this context, "source" refers to the coded generated by gen.c*/
1300 /* and "destination" refers to a pcode in a peep rule. If the dest-*/
1301 /* ination has no wild cards, then MatchLine will compare the two */
1302 /* pcodes (src and dest) for a one-to-one match. If the destination*/
1303 /* has wildcards, then those get expanded. When a wild card is */
1304 /* encountered for the first time it autmatically is considered a */
1305 /* match and the object that matches it is referenced in the */
1306 /* variables or opcodes array (depending on the type of match). */
1310 /* *peepBlock - A pointer to the peepBlock that contains the */
1311 /* entire rule to which the destination pcode belongs*/
1312 /* *pcs - a pointer to the source pcode */
1313 /* *pcd - a pointer to the destination pcode */
1316 /* 1 - pcodes match */
1317 /* 0 - pcodes don't match */
1320 /*-----------------------------------------------------------------*/
1322 int pCodePeepMatchLine(pCodePeep *peepBlock, pCode *pcs, pCode *pcd)
1324 int index; // index into wild card arrays
1326 if(pcs->type == pcd->type) {
1328 if(pcs->type == PC_OPCODE) {
1330 /* If the opcodes don't match then the line doesn't match */
1331 if(PCI(pcs)->op != PCI(pcd)->op)
1334 fprintf(stderr,"%s comparing\n",__FUNCTION__);
1335 pcs->print(stderr,pcs);
1336 pcd->print(stderr,pcd);
1338 if(!pCodePeepMatchLabels(peepBlock, pcs, pcd))
1341 /* Compare the operands */
1342 if(PCI(pcd)->pcop) {
1343 if (PCI(pcd)->pcop->type == PO_WILD) {
1344 index = PCOW(PCI(pcd)->pcop)->id;
1346 fprintf(stderr,"destination is wild\n");
1347 #ifdef DEBUG_PCODEPEEP
1348 if (index > peepBlock->nops) {
1349 fprintf(stderr,"%s - variables exceeded\n",__FUNCTION__);
1353 PCOW(PCI(pcd)->pcop)->matched = PCI(pcs)->pcop;
1354 if(!peepBlock->wildpCodeOps[index]) {
1355 peepBlock->wildpCodeOps[index] = PCI(pcs)->pcop;
1357 //if(PCI(pcs)->pcop->type == PO_GPR_TEMP)
1363 fprintf(stderr,"line %d\n",__LINE__);
1364 switch(PCI(pcs)->pcop->type) {
1368 n = PCOR(PCI(pcs)->pcop)->r->name;
1372 fprintf(stderr,"line %d\n",__LINE__);
1373 n = PCI(pcs)->pcop->name;
1376 if(peepBlock->vars[index])
1377 return (strcmp(peepBlock->vars[index],n) == 0);
1379 fprintf(stderr,"first time for a variable: %d, %s\n",index,n);
1380 peepBlock->vars[index] = n; //PCI(pcs)->pcop->name;
1385 /* FIXME - need an else to check the case when the destination
1386 * isn't a wild card */
1388 /* The pcd has no operand. Lines match if pcs has no operand either*/
1389 return (PCI(pcs)->pcop == NULL);
1394 if((pcd->type == PC_WILD) && (pcs->type == PC_OPCODE)) {
1397 index = PCW(pcd)->id;
1399 fprintf(stderr,"%s comparing wild cards\n",__FUNCTION__);
1400 pcs->print(stderr,pcs);
1401 pcd->print(stderr,pcd);
1403 peepBlock->wildpCodes[PCW(pcd)->id] = pcs;
1405 if(!pCodePeepMatchLabels(peepBlock, pcs, pcd))
1408 if(PCW(pcd)->operand) {
1409 PCOW(PCI(pcd)->pcop)->matched = PCI(pcs)->pcop;
1410 if(peepBlock->vars[index]) {
1411 int i = (strcmp(peepBlock->vars[index],PCI(pcs)->pcop->name) == 0);
1413 fprintf(stderr," (matched)\n");
1415 fprintf(stderr," (no match: wild card operand mismatch\n");
1416 fprintf(stderr," peepblock= %s, pcodeop= %s\n",
1417 peepBlock->vars[index],
1418 PCI(pcs)->pcop->name);
1422 peepBlock->vars[index] = PCI(pcs)->pcop->name;
1427 pcs = findNextInstruction(pcs->next);
1429 fprintf(stderr," (next to match)\n");
1430 pcs->print(stderr,pcs);
1431 } else if(pcd->next) {
1432 /* oops, we ran out of code, but there's more to the rule */
1436 return 1; /* wild card matches */
1442 /*-----------------------------------------------------------------*/
1443 /*-----------------------------------------------------------------*/
1444 void pCodePeepClrVars(pCodePeep *pcp)
1451 for(i=0;i<pcp->nvars; i++) {
1452 pcp->vars[i] = NULL;
1453 pcp->wildpCodeOps[i] = NULL;
1457 /*-----------------------------------------------------------------*/
1458 /* pCodeInsertAfter - splice in the pCode chain starting with pc2 */
1459 /* into the pCode chain containing pc1 */
1460 /*-----------------------------------------------------------------*/
1461 void pCodeInsertAfter(pCode *pc1, pCode *pc2)
1467 pc2->next = pc1->next;
1469 pc1->next->prev = pc2;
1477 /*-----------------------------------------------------------------*/
1478 /* pCodeOpCopy - copy a pcode operator */
1479 /*-----------------------------------------------------------------*/
1480 static pCodeOp *pCodeOpCopy(pCodeOp *pcop)
1482 pCodeOp *pcopnew=NULL;
1487 switch(pcop->type) {
1490 fprintf(stderr,"pCodeOpCopy bit\n");
1491 pcopnew = Safe_calloc(1,sizeof(pCodeOpBit) );
1492 PCOB(pcopnew)->bit = PCOB(pcop)->bit;
1493 PCOB(pcopnew)->inBitSpace = PCOB(pcop)->inBitSpace;
1498 /* Here we expand the wild card into the appropriate type: */
1499 /* By recursively calling pCodeOpCopy */
1500 fprintf(stderr,"pCodeOpCopy wild\n");
1501 if(PCOW(pcop)->matched)
1502 pcopnew = pCodeOpCopy(PCOW(pcop)->matched);
1505 pcopnew = pCodeOpCopy(PCOW(pcop)->subtype);
1506 pcopnew->name = Safe_strdup(PCOW(pcop)->pcp->vars[PCOW(pcop)->id]);
1507 fprintf(stderr,"copied a wild op named %s\n",pcopnew->name);
1514 fprintf(stderr,"pCodeOpCopy label\n");
1515 pcopnew = Safe_calloc(1,sizeof(pCodeOpLabel) );
1516 PCOLAB(pcopnew)->key = PCOLAB(pcop)->key;
1521 fprintf(stderr,"pCodeOpCopy lit\n");
1522 pcopnew = Safe_calloc(1,sizeof(pCodeOpLit) );
1523 PCOL(pcopnew)->lit = PCOL(pcop)->lit;
1526 case PO_GPR_REGISTER:
1531 fprintf(stderr,"pCodeOpCopy GPR register\n");
1532 pcopnew = Safe_calloc(1,sizeof(pCodeOpReg) );
1533 PCOR(pcopnew)->r = PCOR(pcop)->r;
1534 PCOR(pcopnew)->rIdx = PCOR(pcop)->rIdx;
1535 fprintf(stderr," register index %d\n", PCOR(pcop)->r->rIdx);
1539 fprintf(stderr,"pCodeOpCopy PO_DIR\n");
1540 case PO_SFR_REGISTER:
1548 fprintf(stderr,"pCodeOpCopy register type %d\n", pcop->type);
1549 pcopnew = Safe_calloc(1,sizeof(pCodeOp) );
1553 pcopnew->type = pcop->type;
1555 pcopnew->name = Safe_strdup(pcop->name);
1557 pcopnew->name = NULL;
1563 /*-----------------------------------------------------------------*/
1564 /* pCodeCopy - copy a pcode */
1565 /*-----------------------------------------------------------------*/
1566 static pCode *pCodeCopy(pCode *pc)
1571 pcnew = newpCode(pc->type,pc->pcop);
1574 /*-----------------------------------------------------------------*/
1575 /*-----------------------------------------------------------------*/
1576 void pCodeDeleteChain(pCode *f,pCode *t)
1582 fprintf(stderr,"delete pCode:\n");
1585 //f->delete(f); this dumps core...
1592 /*-----------------------------------------------------------------*/
1593 /*-----------------------------------------------------------------*/
1594 int pCodePeepMatchRule(pCode *pc)
1596 pCodePeep *peepBlock;
1601 peeprules = (_DLL *)peepSnippets;
1604 peepBlock = ((pCodePeepSnippets*)peeprules)->peep;
1605 if(!peepBlock || !peepBlock->target || !peepBlock->target->pcHead)
1608 pCodePeepClrVars(peepBlock);
1611 pct = peepBlock->target->pcHead;
1613 while(pct && pcin) {
1615 if(! (matched = pCodePeepMatchLine(peepBlock, pcin,pct)))
1618 pcin = findNextInstruction(pcin->next);
1621 fprintf(stderr," matched\n");
1623 fprintf(stderr," end of code\n");
1625 fprintf(stderr," end of rule\n");
1630 /* So far we matched the rule up to the point of the conditions .
1631 * In other words, all of the opcodes match. Now we need to see
1632 * if the post conditions are satisfied.
1633 * First we check the 'postFalseCond'. This means that we check
1634 * to see if any of the subsequent pCode's in the pCode chain
1635 * following the point just past where we have matched depend on
1636 * the `postFalseCond' as input then we abort the match
1638 fprintf(stderr," matched rule so far, now checking conditions\n");
1639 if (pcin && peepBlock->postFalseCond &&
1640 (pCodeSearchCondition(pcin,peepBlock->postFalseCond) > 0) )
1650 /* We matched a rule! Now we have to go through and remove the
1651 inefficient code with the optimized version */
1653 fprintf(stderr, "Found a pcode peep match:\nRule:\n");
1654 printpCodeString(stderr,peepBlock->target->pcHead,10);
1655 fprintf(stderr,"first thing matched\n");
1656 pc->print(stderr,pc);
1658 fprintf(stderr,"last thing matched\n");
1659 pcin->print(stderr,pcin);
1662 /* Unlink the original code */
1664 pcprev->next = pcin;
1666 pcin->prev = pc->prev;
1670 /* Converted the deleted pCodes into comments */
1677 while(pc && pc!=pcin) {
1678 pCode2str(&buf[2], 254, pc);
1679 pCodeInsertAfter(pcprev, newpCodeCharP(buf));
1680 pcprev = pcprev->next;
1686 pCodeDeleteChain(pc,pcin);
1688 /* Generate the replacement code */
1690 pcr = peepBlock->replace->pcHead; // This is the replacement code
1693 /* If the replace pcode is an instruction with an operand, */
1694 /* then duplicate the operand (and expand wild cards in the process). */
1695 if(pcr->type == PC_OPCODE) {
1696 if(PCI(pcr)->pcop) {
1697 /* The replacing instruction has an operand.
1699 if(PCI(pcr)->pcop->type == PO_WILD) {
1700 int index = PCOW(PCI(pcr)->pcop)->id;
1701 fprintf(stderr,"copying wildopcode\n");
1702 if(peepBlock->wildpCodeOps[index])
1703 pcop = pCodeOpCopy(peepBlock->wildpCodeOps[index]);
1705 fprintf(stderr,"error, wildopcode in replace but not source?\n");
1707 pcop = pCodeOpCopy(PCI(pcr)->pcop);
1709 fprintf(stderr,"inserting pCode\n");
1710 pCodeInsertAfter(pc, newpCode(PCI(pcr)->op,pcop));
1711 } else if (pcr->type == PC_WILD) {
1712 pCodeInsertAfter(pc,peepBlock->wildpCodes[PCW(pcr)->id]);
1713 } else if (pcr->type == PC_COMMENT) {
1714 pCodeInsertAfter(pc, newpCodeCharP( ((pCodeComment *)(pcr))->comment));
1720 pc->print(stderr,pc);
1727 peeprules = peeprules->next;
1743 /*******************/
1744 pCode *parseLineNode(char *ln)
1746 char buffer[50], *s;
1747 int state=0; //0 label, 1 mnemonic, 2 operand, 3 operand, 4 comment
1750 // pCodeLabel *pcl = NULL;
1760 /* skip white space */
1761 while (isspace (*ln))
1766 case 0: // look for a label
1767 case 1: // look for mnemonic
1778 var = strtol(ln, &ln, 10);
1780 // valid wild card label
1781 fprintf(stderr, " wildcard label: %d\n",var);
1784 fprintf(stderr, " wild opcode: %d\n",var), state++;
1788 // Extract the label/mnemonic from the line
1791 while(*ln && !(isspace(*ln) || *ln == ':'))
1796 fprintf(stderr," regular label: %s\n",buffer), ln++;
1798 fprintf(stderr," regular mnem: %s\n",buffer), state++;