2 static char rcsid2[] = "$Id: nopt.c,v 2.16 1994/06/24 11:14:16 ceriel Exp $";
7 extern struct dfa OO_checknext[]; /* Initialized in dfa.c */
8 extern struct dfa *OO_base[]; /* Initialized in dfa.c */
9 extern struct dodefault OO_default[]; /* Initialized in dfa.c */
10 extern int OO_maxpattern; /* Initialized in dfa.c */
11 extern int OO_maxreplacement; /* Initialized in dfa.c */
12 extern int (*OO_ftrans[])(); /* Initialized in trans.c */
14 extern char em_mnem[][4];
15 extern char em_pseu[][4];
20 p_instr OO_patternqueue;
24 static p_instr OO_replqueue;
26 static char *filename;
27 static char *strqueue;
31 arith OO_WSIZE; /* wordlength */
32 arith OO_DWSIZE; /* 2*wordlength */
33 arith OO_PSIZE; /* pointer length */
36 int OO_wrstats = 1; /* pattern statistics output */
39 #define printstate(s) dumpstate(s)
44 /**** WHICH IS FASTER? ****
45 #define BTSCPY(pp,qq,i,p,q,n) btscpy(p,q,(n)*sizeof(struct e_instr))
46 **************************/
47 #define BTSCPY(pp,qq,i,p,q,n) for(pp=(p),qq=(q),i=(n);i--;*pp++ = *qq++)
49 PRIVATE void allocmem();
67 return(C_open(fname));
86 register struct dfa *b;
87 register struct dodefault *d;
91 if((b=OO_base[OO_state]) && ((b += last)->check==OO_state)) {
92 if(f=OO_ftrans[OO_state = b->next]) (*f)();
95 /* consult default entry */
96 d = &OO_default[OO_state];
97 if(!OO_endbackup) OO_endbackup = OO_nxtpatt;
99 OO_patternqueue += d->numout;
100 if(f=OO_ftrans[OO_state = d->next]) (*f)();
103 if (!OO_endbackup) return;
104 last = (OO_nxtpatt++)->em_opcode;
105 if (OO_nxtpatt >= OO_endbackup)
115 fprint(STDERR, "%s: ", filename ? filename : "standard input");
124 /* Allocate memory for queues on heap */
125 OO_buffer = (p_instr)
126 Malloc((unsigned)(MAXBUFFER*sizeof(struct e_instr)));
127 OO_patternqueue = OO_nxtpatt = OO_buffer;
128 OO_replqueue = (p_instr)
129 Malloc((unsigned)OO_maxreplacement*sizeof(struct e_instr));
130 OO_nxtrepl = OO_replqueue;
132 (char *)Malloc(MAXSTRING*sizeof(char));
133 laststr = strqueue + MAXSTRING - 1;
140 register char *s = str;
144 if ((s-str) > (laststr-nextstr)) {
145 unsigned newsize = (laststr - strqueue + 1)*2;
146 res = Realloc(strqueue,newsize);
147 laststr = res + newsize - 1;
148 nextstr = res + (nextstr - strqueue);
151 fprintf(stderr,"Warning: Reallocated string area.");
152 fprintf(stderr,"New size is %d bytes\n", newsize);
157 for(s=str;*nextstr++ = *s++;);
165 /* Output all instructions waiting in the output queue and free their
166 /* storage including the saved strings.
168 register p_instr p,q;
171 for(p=OO_buffer;p<OO_patternqueue;p++)
173 if(p->em_opcode!=OTHER)
176 n = OO_endbackup-OO_nxtpatt;
177 BTSCPY(p,q,i,OO_buffer,OO_nxtpatt,n);
178 OO_endbackup = OO_buffer + n;
180 else nextstr = strqueue;
181 OO_patternqueue = OO_nxtpatt = OO_buffer;
188 /* Called when buffer full, flush half the buffer and move the
189 /* the pattern pointers to the new positions. Return a pointer
190 /* to the new nxtpatt position and increment it.
191 /* Note that OO_endbackup is always NIL (i.e. there are no
192 /* instructions on the backup queue) when this is invoked.
195 register p_instr p,q;
196 printstate("Half flush");
198 for(p=OO_buffer,i=n;i--;)
200 /* now copy the rest of buffer and pattern back */
201 BTSCPY(p,q,i,OO_buffer,OO_buffer+n,(OO_nxtpatt-OO_buffer)-n);
202 OO_patternqueue -= n;
204 printstate("after Half flush");
205 return (OO_nxtpatt++);
209 OO_mkext(p,opcode,arg,off)
215 switch(arg->em_argtype) {
217 EM_mkcst(p,opcode,off);
220 EM_mksof(p,opcode,arg->em_dnam,off);
223 EM_mknof(p,opcode,arg->em_dlb,off);
226 fatal("Unexpected type %d in outext",arg->em_argtype);
231 OO_mkrepl(lrepl,diff,numbkup)
232 int lrepl,diff,numbkup;
234 /* copy the replacement queue into the buffer queue */
235 /* then move the pattern queue back n places */
236 register p_instr p,q;
238 printstate("Before backup");
240 /* move the region between OO_nxtpatt and OO_endbackup */
242 /* move left by diff */
243 BTSCPY(p,q,i,OO_nxtpatt-diff,OO_nxtpatt,OO_endbackup-OO_nxtpatt);
245 OO_endbackup -= diff;
248 /* move right by diff */
249 /* careful of overflowing buffer!! */
250 if ((OO_nxtpatt-diff)> (OO_buffer+MAXBUFFER) )
252 /* cannot use btscpy as strings may overlap */
253 p = (q=OO_endbackup-1) - diff;
257 OO_endbackup -= diff;
260 /* copy the replacement */
262 BTSCPY(p,q,i,OO_patternqueue,OO_replqueue,lrepl);
263 OO_nxtrepl = OO_replqueue;
264 OO_patternqueue += lrepl;
266 /* now move the position of interest back nunbkup instructions */
267 if ((OO_patternqueue-OO_buffer) < numbkup)
268 numbkup = (OO_patternqueue-OO_buffer);
269 OO_nxtpatt = OO_patternqueue -= numbkup;
270 if(!OO_endbackup && numbkup)
271 OO_endbackup = OO_patternqueue+numbkup;
273 printstate("After backup");
282 fprintf(stderr,"%s - state(%d): ",mess,OO_state);
284 while(p<OO_patternqueue)
286 fprintf(stderr," |==| ");
289 fprintf(stderr," |==| ");
291 while(p<OO_endbackup)
294 fprintf(stderr,"\n");
303 fprintf(stderr,"%s ",em_mnem[p->em_opcode-sp_fmnem]);
306 fprintf(stderr,"%s ",em_pseu[p->em_opcode-sp_fpseu]);
311 fprintf(stderr,"MES ");
314 fprintf(stderr,"%ld ", (long)p->em_ilb);
317 fprintf(stderr,"%ld ", (long)p->em_dlb);
320 fprintf(stderr,"%d ", p->em_dnam);
327 switch(p->em_argtype) {
331 fprintf(stderr,"%d ",p->em_cst);
334 fprintf(stderr,".%d+%d ",p->em_dlb,p->em_off);
337 fprintf(stderr,"%s+%d ",p->em_dnam,p->em_off);
340 fprintf(stderr,"*%d ",p->em_ilb);
343 fprintf(stderr,"$%s ",p->em_pnam);
348 fprintf(stderr,"\"%s\"",p->em_string);
351 fatal(" prtinst - Unregognized arg %d ",p->em_argtype);