2 static char rcsidp3[] = "$Id: findworst.c,v 2.8 1994/06/24 11:14:04 ceriel Exp $";
7 #define UPDATEWORST(backups) if(backups>mostbackups) mostbackups = backups;
9 PRIVATE int leftmatch();
10 PRIVATE int rightmatch();
13 struct mnems patt,repl;
16 /* Find the pattern that requires the most backup of output queue.
17 /* Let repl be r1 r2 ... rn. All these are already on the output queue.
18 /* Possibilities in order of most backup first are:
19 /* a) pattern of form: p1 .... pb r1 r2 .... rn pc ... pd
20 /* i.e. <repl> completely in pattern.
21 /* requires a backup of b+n instructions
22 /* and a goto to state 0.
23 /* b) pattern of form: p1 .... pb r1 r2 .... ri
24 /* i.e. a prefix of <repl> ends a pattern.
25 /* requires a backup of b+n instructions
26 /* and a goto to state 0.
27 /* c) pattern of form: ri ri+1 ... rn pc ... pd
28 /* i.e. a suffix of <repl> starts a pattern.
29 /* requires a backup of j-i+1 instructions and a goto to state 0.
30 /* d) pattern of the form: ri ri+1 ... rj
31 /* i.e. a substring of <repl> is a complete pattern
32 /* requires a backup of j-i+1 instructions and a goto to state 0.
35 int diff = patt.m_len - repl.m_len;
40 fprintf(ofile,"\t\tOO_mkrepl(0,%d,%d);\n",diff,maxpattern-1);
43 for(s=1;s<=higheststate;s++) {
44 /* only match complete patterns */
45 if(actions[s]==(struct action *)NULL)
48 if(first=rightmatch(patterns[s],repl,1,n)) {
49 UPDATEWORST(first-1+n);
53 if((first=rightmatch(patterns[s],repl,1,i)) &&
54 (first+i-1==patterns[s].m_len)) {
55 UPDATEWORST(first-1+n);
60 if((first=leftmatch(patterns[s],repl,i,n)) &&
68 if((first=leftmatch(patterns[s],repl,i,j)) &&
70 (j-i+1 == patterns[s].m_len)) {
76 fprintf(ofile,"\t\tOO_mkrepl(%d,%d,%d);\n",n,diff,mostbackups);
79 findfail(state,resout,rescpy,resgto)
81 int *resout, *rescpy, *resgto;
84 /* If pattern matching fails in 'state', how many outputs and how many
85 /* push backs are requires. If pattern is of the form p1 p2 .... pn
86 /* look for patterns of the form p2 p3 ... pn; then p3 p4 ... pn; etc.
87 /* The first such match of the form pi pi+1 ... pn requires an output
88 /* of p1 p2 ... pi-1 and a push back of pn pn-1 ... pi.
93 int n = patterns[state].m_len;
95 for(s=1;s<=higheststate;s++) {
96 /* exclude those on transitions from this state */
98 for(p=states[state];p!=(struct state *)NULL;p=p->next)
103 if((leftmatch(patterns[s],patterns[state],i,n)==1)&&
104 patterns[s].m_len==(n-i+1)) {
118 leftmatch(patt,repl,i,j)
119 struct mnems patt,repl;
123 /* Return the first complete match of the mnems <ri,ri+1,..,rj> of
124 /* 'repl' in the mnems of 'patt'. Find the leftmost match.
125 /* Return 0 if fails.
128 int lastpos = patt.m_len - lenrij + 1;
130 for(k=1;k<=lastpos;k++) {
131 for(n=1;n<=lenrij;n++) {
132 if(patt.m_elems[(k+n-1)-1]->op_code != repl.m_elems[(i+n-1)-1]->op_code)
143 rightmatch(patt,repl,i,j)
144 struct mnems patt,repl;
148 /* Return the first complete match of the mnems <ri,ri+1,..,rj> of
149 /* 'repl' in the mnems of 'patt'. Find the rightmost match.
150 /* Return 0 if fails.
153 int lastpos = patt.m_len - lenrij + 1;
155 for(k=lastpos;k>=1;k--) {
156 for(n=1;n<=lenrij;n++) {
157 if(patt.m_elems[(k+n-1)-1]->op_code != repl.m_elems[(i+n-1)-1]->op_code)