Annotation of OpenXM_contrib2/asir2000/parse/compile.c, Revision 1.4
1.2 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.3 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.2 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
47: *
1.4 ! ohara 48: * $OpenXM: OpenXM_contrib2/asir2000/parse/compile.c,v 1.3 2000/08/22 05:04:25 noro Exp $
1.2 noro 49: */
1.1 noro 50: #include <ctype.h>
51: #include "ca.h"
52: #include "base.h"
53: #include "parse.h"
54: #if !defined(THINK_C)
55: #include <sys/types.h>
56: #include <sys/stat.h>
57: #endif
58:
59: extern jmp_buf timer_env;
60:
61: pointer bcompilef(), compilemapf();
62:
63: pointer compile(f)
64: FNODE f;
65: {
66: LIST t;
67: STRING str;
68: pointer val = 0;
69: pointer a,a1,a2;
70: NODE tn,ind;
71: R s,u;
72: DP dp;
73: int pv,c;
74: FNODE f1;
75: Obj expired;
76: int interval;
77:
78: if ( !f )
79: return ( 0 );
80: switch ( f->id ) {
81: case I_BOP:
82: a1 = compile((FNODE)FA1(f)); a2 = compile((FNODE)FA2(f));
83: val = emit_bop(((ARF)FA0(f)),a1,a2);
84: break;
85: case I_COP:
86: a1 = compile((FNODE)FA1(f)); a2 = compile((FNODE)FA2(f));
87: val = emit_cop((cid)FA0(f),a1,a2);
88: break;
89: case I_AND:
90: a1 = compile((FNODE)FA0(f)); a2 = compile((FNODE)FA1(f));
91: val = emit_and(a1,a2);
92: break;
93: case I_OR:
94: a1 = compile((FNODE)FA0(f)); a2 = compile((FNODE)FA1(f));
95: val = emit_or(a1,a2);
96: break;
97: case I_NOT:
98: a1 = compile((FNODE)FA0(f));
99: val = emit_not(a1);
100: break;
101: case I_CE:
102: a1 = compile((FNODE)FA0(f)); a2 = compile((FNODE)FA1(f));
103: a3 = compile((FNODE)FA0(f));
104: emit_ce(a1,a2,a3);
105: break;
106: case I_FUNC:
107: val = compilef((FUNC)FA0(f),(FNODE)FA1(f)); break;
108: case I_PVAR:
109: pv = (int)FA0(f); ind = (NODE)FA1(f); GETPV(pv,a);
110: if ( !ind )
111: val = a;
112: else {
113: compilenodebody(ind,&tn); getarray(a,tn,&val);
114: }
115: break;
116: case I_ASSPVAR:
117: f1 = (FNODE)FA0(f);
118: if ( ID(f1) == I_PVAR ) {
119: pv = (int)FA0(f1); ind = (NODE)FA1(f1);
120: if ( !ind ) {
121: val = compile((FNODE)FA1(f)); ASSPV(pv,val);
122: } else {
123: GETPV(pv,a);
124: compilenodebody(ind,&tn);
125: putarray(a,tn,val = compile((FNODE)FA1(f)));
126: }
127: } else
128: val = compilepv(ID(f),(FNODE)FA0(f),FA1(f));
129: break;
130: case I_PRESELF:
131: f1 = (FNODE)FA1(f);
132: if ( ID(f1) == I_PVAR ) {
133: pv = (int)FA0(f1); ind = (NODE)FA1(f1); GETPV(pv,a);
134: if ( !ind ) {
135: (*((ARF)FA0(f))->fp)(CO,a,ONE,&val); ASSPV(pv,val);
136: } else if ( a ) {
137: compilenodebody(ind,&tn); getarray(a,tn,(pointer *)&u);
138: (*((ARF)FA0(f))->fp)(CO,u,ONE,&val); putarray(a,tn,val);
139: }
140: } else
141: val = compilepv(f->id,(FNODE)FA1(f),FA0(f));
142: break;
143: case I_POSTSELF:
144: f1 = (FNODE)FA1(f);
145: if ( ID(f1) == I_PVAR ) {
146: pv = (int)FA0(f1); ind = (NODE)FA1(f1); GETPV(pv,val);
147: if ( !ind ) {
148: (*((ARF)FA0(f))->fp)(CO,val,ONE,&u); ASSPV(pv,u);
149: } else if ( val ) {
150: compilenodebody(ind,&tn); getarray(val,tn,&a);
151: (*((ARF)FA0(f))->fp)(CO,a,ONE,&u); putarray(val,tn,(pointer)u);
152: val = a;
153: }
154: } else
155: val = compilepv(f->id,(FNODE)FA1(f),FA0(f));
156: break;
157: case I_STR:
158: MKSTR(str,FA0(f)); val = (pointer)str; break;
159: case I_FORMULA:
160: val = FA0(f); break;
161: case I_LIST:
162: compilenodebody((NODE)FA0(f),&tn); MKLIST(t,tn); val = (pointer)t; break;
163: case I_INDEX:
164: a = compile((FNODE)FA0(f)); ind = (NODE)FA1(f);
165: compilenodebody(ind,&tn); getarray(a,tn,&val);
166: break;
167: default:
168: fprintf(stderr,"compile: unknown id");
169: error("");
170: break;
171: }
172: return ( val );
173: }
174:
175: pointer compilestat(f)
176: SNODE f;
177: {
178: pointer val = 0,t,s,s1,u;
179: NODE tn;
180: int i,ac;
181: V *a;
182: char *buf;
183:
184: if ( !f )
185: return ( 0 );
186: if ( nextbp && nextbplevel <= 0 && f->id != S_CPLX ) {
187: nextbp = 0;
188: bp(f);
189: }
190: compilestatline = f->ln;
191:
192: switch ( f->id ) {
193: case S_BP:
194: if ( !nextbp && (!FA1(f) || compile((FNODE)FA1(f))) ) {
195: if ( (FNODE)FA2(f) ) {
1.4 ! ohara 196: #if defined(PARI)
1.1 noro 197: extern FILE *outfile;
198: outfile = stderr;
199: #endif
200: asir_out = stderr;
201: printexpr(CO,compile((FNODE)FA2(f)));
202: putc('\n',asir_out); fflush(asir_out);
1.4 ! ohara 203: #if defined(PARI)
1.1 noro 204: outfile = stdout;
205: #endif
206: asir_out = stdout;
207: } else {
208: nextbp = 1; nextbplevel = 0;
209: }
210: }
211: val = compilestat((SNODE)FA0(f));
212: break;
213: case S_PFDEF:
214: ac = argc(FA1(f)); a = (V *)MALLOC(ac*sizeof(V));
215: s = compile((FNODE)FA2(f));
216: buf = (char *)ALLOCA(BUFSIZ);
217: for ( i = 0, tn = (NODE)FA1(f); tn; tn = NEXT(tn), i++ ) {
218: t = compile((FNODE)tn->body); sprintf(buf,"_%s",NAME(VR((P)t)));
219: makevar(buf,&u); a[i] = VR((P)u);
220: substr(CO,0,(Obj)s,VR((P)t),(Obj)u,(Obj *)&s1); s = s1;
221: }
222: mkpf((char *)FA0(f),(Obj)s,ac,a,0,0,(PF *)&val); val = 0; break;
223: case S_SINGLE:
224: val = compile((FNODE)FA0(f)); break;
225: case S_CPLX:
226: for ( tn = (NODE)FA0(f); tn; tn = NEXT(tn) ) {
227: if ( BDY(tn) )
228: val = compilestat((SNODE)BDY(tn));
229: if ( f_break || f_return || f_continue )
230: break;
231: }
232: break;
233: case S_BREAK:
234: if ( GPVS != CPVS )
235: f_break = 1;
236: break;
237: case S_CONTINUE:
238: if ( GPVS != CPVS )
239: f_continue = 1;
240: break;
241: case S_RETURN:
242: if ( GPVS != CPVS ) {
243: val = compile((FNODE)FA0(f)); f_return = 1;
244: }
245: break;
246: case S_IFELSE:
247: if ( compilenode((NODE)FA1(f)) )
248: val = compilestat((SNODE)FA2(f));
249: else if ( FA3(f) )
250: val = compilestat((SNODE)FA3(f));
251: break;
252: case S_FOR:
253: compilenode((NODE)FA1(f));
254: while ( 1 ) {
255: if ( !compilenode((NODE)FA2(f)) )
256: break;
257: val = compilestat((SNODE)FA4(f));
258: if ( f_break || f_return )
259: break;
260: f_continue = 0;
261: compilenode((NODE)FA3(f));
262: }
263: f_break = 0; break;
264: case S_DO:
265: while ( 1 ) {
266: val = compilestat((SNODE)FA1(f));
267: if ( f_break || f_return )
268: break;
269: f_continue = 0;
270: if ( !compilenode((NODE)FA2(f)) )
271: break;
272: }
273: f_break = 0; break;
274: default:
275: fprintf(stderr,"compilestat: unknown id");
276: error("");
277: break;
278: }
279: return ( val );
280: }
281:
282: pointer compilenode(node)
283: NODE node;
284: {
285: NODE tn;
286: pointer val;
287:
288: for ( tn = node, val = 0; tn; tn = NEXT(tn) )
289: if ( BDY(tn) )
290: val = compile((FNODE)BDY(tn));
291: return ( val );
292: }
293:
294: extern FUNC cur_binf;
295: extern NODE PVSS;
296:
297: pointer compilef(f,a)
298: FUNC f;
299: FNODE a;
300: {
301: LIST args;
302: pointer val;
303: int i,n,level;
304: NODE tn,sn;
305: VS pvs;
306:
307: if ( f->id == A_UNDEF ) {
308: fprintf(stderr,"%s undefined",NAME(f));
309: error("");
310: }
311: if ( f->id != A_PARI ) {
312: for ( i = 0, tn = a?(NODE)FA0(a):0; tn; i++, tn = NEXT(tn) );
313: if ( ((n = f->argc)>= 0 && i != n) || (n < 0 && i > -n) ) {
314: fprintf(stderr,"argument mismatch in %s()",NAME(f));
315: error("");
316: }
317: }
318: switch ( f->id ) {
319: case A_BIN:
320: if ( !n ) {
321: cur_binf = f;
322: (*f->f.binf)(&val);
323: } else {
324: args = (LIST)compile(a);
325: cur_binf = f;
326: (*f->f.binf)(args?BDY(args):0,&val);
327: }
328: cur_binf = 0;
329: break;
330: case A_PARI:
331: args = (LIST)compile(a);
332: cur_binf = f;
333: val = compileparif(f,args?BDY(args):0);
334: cur_binf = 0;
335: break;
336: case A_USR:
337: args = (LIST)compile(a);
338: pvs = f->f.usrf->pvs;
339: if ( PVSS ) {
340: ((VS)BDY(PVSS))->at = compilestatline;
341: level = ((VS)BDY(PVSS))->level+1;
342: } else
343: level = 1;
344: MKNODE(tn,pvs,PVSS); PVSS = tn;
345: CPVS = (VS)ALLOCA(sizeof(struct oVS)); BDY(PVSS) = (pointer)CPVS;
346: CPVS->usrf = f; CPVS->n = CPVS->asize = pvs->n;
347: CPVS->level = level;
348: if ( CPVS->n ) {
349: CPVS->va = (struct oPV *)ALLOCA(CPVS->n*sizeof(struct oPV));
350: bcopy((char *)pvs->va,(char *)CPVS->va,
351: (int)(pvs->n*sizeof(struct oPV)));
352: }
353: if ( nextbp )
354: nextbplevel++;
355: for ( tn = f->f.usrf->args, sn = BDY(args);
356: sn; tn = NEXT(tn), sn = NEXT(sn) )
357: ASSPV((int)FA0((FNODE)BDY(tn)),BDY(sn));
358: val = compilestat((SNODE)BDY(f->f.usrf));
359: f_return = f_break = f_continue = 0; poppvs();
360: break;
361: case A_PURE:
362: val = compilepf(f->f.puref,a); break;
363: default:
364: fprintf(stderr,"%s undefined",NAME(f));
365: error("");
366: break;
367: }
368: return val;
369: }
370:
371: pointer compilemapf(f,a)
372: FUNC f;
373: FNODE a;
374: {
375: LIST args;
376: NODE node,rest,t,n,l,r,r0;
377: Obj head;
378: VECT v,rv;
379: MAT m,rm;
380: LIST rl;
381: int len,row,col,i,j;
382: pointer val;
383:
384: args = (LIST)compile(a);
385: node = BDY(args); head = (Obj)BDY(node); rest = NEXT(node);
386: switch ( OID(head) ) {
387: case O_VECT:
388: v = (VECT)head; len = v->len; MKVECT(rv,len);
389: for ( i = 0; i < len; i++ ) {
390: MKNODE(t,BDY(v)[i],rest); BDY(rv)[i] = bcompilef(f,t);
391: }
392: val = (pointer)rv;
393: break;
394: case O_MAT:
395: m = (MAT)head; row = m->row; col = m->col; MKMAT(rm,row,col);
396: for ( i = 0; i < row; i++ )
397: for ( j = 0; j < col; j++ ) {
398: MKNODE(t,BDY(m)[i][j],rest); BDY(rm)[i][j] = bcompilef(f,t);
399: }
400: val = (pointer)rm;
401: break;
402: case O_LIST:
403: n = BDY((LIST)head);
404: for ( r0 = r = 0; n; n = NEXT(n) ) {
405: NEXTNODE(r0,r); MKNODE(t,BDY(n),rest); BDY(r) = bcompilef(f,t);
406: }
407: if ( r0 )
408: NEXT(r) = 0;
409: MKLIST(rl,r0);
410: val = (pointer)rl;
411: break;
412: default:
413: val = bcompilef(f,node);
414: break;
415: }
416: return val;
417: }
418:
419: pointer bcompilef(f,a)
420: FUNC f;
421: NODE a;
422: {
423: LIST args;
424: pointer val;
425: int i,n;
426: NODE tn,sn;
427: VS pvs;
428:
429: if ( f->id == A_UNDEF ) {
430: fprintf(stderr,"%s undefined",NAME(f));
431: error("");
432: }
433: if ( f->id != A_PARI ) {
434: for ( i = 0, tn = a; tn; i++, tn = NEXT(tn) );
435: if ( ((n = f->argc)>= 0 && i != n) || (n < 0 && i > -n) ) {
436: fprintf(stderr,"argument mismatch in %s()",NAME(f));
437: error("");
438: }
439: }
440: switch ( f->id ) {
441: case A_BIN:
442: if ( !n ) {
443: cur_binf = f;
444: (*f->f.binf)(&val);
445: } else {
446: cur_binf = f;
447: (*f->f.binf)(a,&val);
448: }
449: cur_binf = 0;
450: break;
451: case A_PARI:
452: cur_binf = f;
453: val = compileparif(f,a);
454: cur_binf = 0;
455: break;
456: case A_USR:
457: pvs = f->f.usrf->pvs;
458: if ( PVSS )
459: ((VS)BDY(PVSS))->at = compilestatline;
460: MKNODE(tn,pvs,PVSS); PVSS = tn;
461: CPVS = (VS)ALLOCA(sizeof(struct oVS)); BDY(PVSS) = (pointer)CPVS;
462: CPVS->usrf = f; CPVS->n = CPVS->asize = pvs->n;
463: if ( CPVS->n ) {
464: CPVS->va = (struct oPV *)ALLOCA(CPVS->n*sizeof(struct oPV));
465: bcopy((char *)pvs->va,(char *)CPVS->va,
466: (int)(pvs->n*sizeof(struct oPV)));
467: }
468: if ( nextbp )
469: nextbplevel++;
470: for ( tn = f->f.usrf->args, sn = a;
471: sn; tn = NEXT(tn), sn = NEXT(sn) )
472: ASSPV((int)FA0((FNODE)BDY(tn)),BDY(sn));
473: val = compilestat((SNODE)BDY(f->f.usrf));
474: f_return = f_break = f_continue = 0; poppvs();
475: break;
476: default:
477: fprintf(stderr,"%s undefined",NAME(f));
478: error("");
479: break;
480: }
481: return val;
482: }
483:
484: pointer compileif(f,a)
485: FNODE f,a;
486: {
487: Obj g;
488:
489: g = (Obj)compile(f);
490: if ( g && (OID(g) == O_P) && (VR((P)g)->attr == (pointer)V_SR) )
491: return compilef((FUNC)VR((P)g)->priv,a);
492: else {
493: fprintf(stderr,"invalid function pointer");
494: error("");
495: }
496: }
497:
498: pointer compilepf(pf,a)
499: PF pf;
500: FNODE a;
501: {
502: LIST args;
503: pointer val;
504: Obj s,s1;
505: int i;
506: NODE node;
507: PFINS ins;
508: PFAD ad;
509:
510: for ( i = 0, node = a?(NODE)FA0(a):0; node; i++, node = NEXT(node) );
511: if ( pf->argc != i ) {
512: fprintf(stderr,"argument mismatch in %s()",NAME(pf));
513: error("");
514: }
515: args = (LIST)compile(a);
516: if ( !pf->body ) {
517: ins = (PFINS)CALLOC(1,sizeof(PF)+pf->argc*sizeof(struct oPFAD));
518: ins->pf = pf;
519: for ( i = 0, node = args->body, ad = ins->ad;
520: node; node = NEXT(node), i++ ) {
521: ad[i].d = 0; ad[i].arg = (Obj)node->body;
522: }
523: simplify_ins(ins,&s); return (pointer)s;
524: } else {
525: for ( i = 0, s = pf->body, node = args->body;
526: node; node = NEXT(node), i++ ) {
527: substr(CO,0,s,pf->args[i],(Obj)node->body,&s1); s = s1;
528: }
529: return (pointer)s;
530: }
531: }
532:
533: void compilenodebody(sn,dnp)
534: NODE sn;
535: NODE *dnp;
536: {
537: NODE n,n0,tn;
538: int line;
539:
540: if ( !sn ) {
541: *dnp = 0;
542: return;
543: }
544: line = compilestatline;
545: for ( tn = sn, n0 = 0; tn; tn = NEXT(tn) ) {
546: NEXTNODE(n0,n);
547: BDY(n) = compile((FNODE)BDY(tn));
548: compilestatline = line;
549: }
550: NEXT(n) = 0; *dnp = n0;
551: }
552:
553: void searchf(fn,name,r)
554: NODE fn;
555: char *name;
556: FUNC *r;
557: {
558: NODE tn;
559:
560: for ( tn = fn;
561: tn && strcmp(NAME((FUNC)BDY(tn)),name); tn = NEXT(tn) );
562: if ( tn ) {
563: *r = (FUNC)BDY(tn);
564: return;
565: }
566: *r = 0;
567: }
568:
569: void appenduf(name,r)
570: char *name;
571: FUNC *r;
572: {
573: NODE tn;
574: FUNC f;
575:
576: f=(FUNC)MALLOC(sizeof(struct oFUNC));
577: f->name = name; f->id = A_UNDEF; f->argc = 0; f->f.binf = 0;
578: MKNODE(tn,f,usrf); usrf = tn;
579: *r = f;
580: }
581:
582: void mkparif(name,r)
583: char *name;
584: FUNC *r;
585: {
586: NODE tn;
587: FUNC f;
588:
589: *r = f =(FUNC)MALLOC(sizeof(struct oFUNC));
590: f->name = name; f->id = A_PARI; f->argc = 0; f->f.binf = 0;
591: }
592:
593: void mkuf(name,fname,args,body,startl,endl,desc)
594: char *name,*fname;
595: NODE args;
596: SNODE body;
597: int startl,endl;
598: char *desc;
599: {
600: FUNC f;
601: USRF t;
602: NODE sn,tn;
603: FNODE fn;
604: int argc;
605:
606: searchf(sysf,name,&f);
607: if ( f ) {
608: fprintf(stderr,"def : builtin function %s() cannot be redefined.\n",name);
609: CPVS = GPVS; return;
610: }
611: for ( argc = 0, sn = args; sn; argc++, sn = NEXT(sn) ) {
612: fn = (FNODE)BDY(sn);
613: if ( !fn || ID(fn) != I_PVAR ) {
614: fprintf(stderr,"illegal argument in %s()\n",name);
615: CPVS = GPVS; return;
616: }
617: }
618: for ( sn = usrf; sn && strcmp(NAME((FUNC)BDY(sn)),name); sn = NEXT(sn) );
619: if ( sn )
620: f = (FUNC)BDY(sn);
621: else {
622: f=(FUNC)MALLOC(sizeof(struct oFUNC));
623: f->name = name;
624: MKNODE(tn,f,usrf); usrf = tn;
625: }
626: if ( Verbose && f->id != A_UNDEF )
627: fprintf(stderr,"Warning : %s() redefined.\n",name);
628: /* else
629: fprintf(stderr,"%s() defined.\n",name); */
630: t=(USRF)MALLOC(sizeof(struct oUSRF));
631: t->args=args; BDY(t)=body; t->pvs = CPVS; t->fname = fname;
632: t->startl = startl; t->endl = endl; t->vol = asir_infile->vol;
633: t->desc = desc;
634: f->id = A_USR; f->argc = argc; f->f.usrf = t;
635: CPVS = GPVS;
636: clearbp(f);
637: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>