1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
|
/*
Copyright (C) 2000-2013 The PARI group.
This file is part of the GP2C package.
PARI/GP is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation. It is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY WHATSOEVER.
Check the License for details. You should have received a copy of it, along
with the package; see the file 'COPYING'. If not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.*/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "header.h"
static int newcvar=1;
/*Use getfunc or newuserfunc instead*/
static int newfunc(const char *gpname)
{
int nf=stack_new(&s_func);
gpfunc *f=lfunc+nf;
f->gpname=gpname;
f->proto.cname=gpname;
f->proto.code=NULL;
f->proto.origin=NULL;
f->proto.help=-1;
f->node=newnode(Ffun,nf,-1);
functype(*f)=Gempty;
funcmode(*f)=0;
f->spec=GPpari;
f->iter=NULL;
f->wrap=NULL;
f->dsc=NULL;
f->user=NULL;
return nf;
}
int newuserfunc(const char *gpname)
{
int nf=newfunc(gpname);
gpfunc *f=lfunc+nf;
userfunc *uf;
if (gpname[0]=='_' && gpname[1]=='.')
{
char *s=strdup(gpname);
s[0]='m'; s[1]='_';
f->proto.cname=usercname(s);
if (s!= f->proto.cname) free(s);
}
else
f->proto.cname=usercname(gpname);
f->spec=GPuser;
f->proto.origin=namelib;
uf=f->user=(userfunc*) malloc(sizeof(*f->user));
stack_init(&uf->v,sizeof(*uf->var),(void **)&uf->var);
stack_init(&uf->g,sizeof(*uf->gcvar),(void **)&uf->gcvar);
return nf;
}
int findfunction_len(const char *s, size_t n)
{
int i;
for(i=0; i<s_func.n;i++)
{
const char *f=lfunc[i].gpname;
if (strlen(f)==n && !strncmp(s,f,n))
return i;
}
return -1;
}
int findfunction(const char *s)
{
int i;
for(i=0; i<s_func.n && strcmp(lfunc[i].gpname,s);i++);
return i<s_func.n?i:-1;
}
int findfuncdesc(const char *s)
{
int n=findfunction(s);
if (n<0)
die(err_desc,"Cannot find description of %s",s);
if (!lfunc[n].dsc)
die(err_desc,"Function %s has no description",s);
return n;
}
int findfuncdescopt(const char *s)
{
int n=findfunction(s);
if (n>=0 && !lfunc[n].dsc)
die(err_desc,"Function %s has no description",s);
return n;
}
int findfunctype(const char *s)
{
int nf=findfuncdesc(s);
gpdescarg *da=descfindrules(0,NULL,lfunc+nf);
return da->type;
}
int findfunctypeopt(const char *s)
{
gpdescarg *da;
int nf=findfuncdescopt(s);
if (nf<0) return -1;
da=descfindrules(0,NULL,lfunc+nf);
return da->type;
}
int getfunc(const char *gpname)
{
int r=findfunction(gpname);
if (r>=0)
return r;
return newfunc(strdup(gpname));
}
void genprec(FILE *fout, int level)
{
if (level)
{
char buf[64];
sprintf(buf, "bitprec%d", level);
if (getvarbyname(buf)>=0)
fprintf(fout, "nbits2prec(%s)",buf);
else if (getvarbyname(buf+3)>=0)
fprintf(fout, "%s",buf+3);
else die(-1,"Internal error: prec not found!");
}
else
fprintf(fout, "prec");
}
void genbitprec(FILE *fout, int level)
{
if (level)
{
char buf[64];
sprintf(buf, "bitprec%d", level);
if (getvarbyname(buf)>=0)
fprintf(fout, "%s",buf);
else if (getvarbyname(buf+3)>=0)
fprintf(fout, "prec2nbits(%s)",buf+3);
else die(-1,"Internal error: prec not found!");
}
else
fprintf(fout, "prec2nbits(prec)");
}
void genautoarg(FILE *fout, char c, int nerr)
{
switch(c)
{
case 'p':
genprec(fout, preclevel);
break;
case 'b':
genbitprec(fout, preclevel);
break;
case 'P':
fprintf(fout,"precdl");
break;
default:
die(nerr,"unhandled letter '%c' in prototype",c);
}
}
int genarg(int nerr, FILE *fout, char c, int n, PPproto pr)
{
if (n==-1)
die(n,"missing mandatory argument in function call");
switch(c)
{
case 'I':
case 'E':
case 'G':
gencast(fout,n,Ggen);
return 1;
case 'U':
gencast(fout,n,Gusmall);
return 1;
case 'L':
case 'P':
gencast(fout,n,Gsmall);
return 1;
case '&':
if (tree[n].f!=Frefarg && pr == PPdefault)
die(nerr,"Missing & for reference");
case '*': /* Fall through */
fprintf(fout,"&");
gencode(fout,n);
return 1;
case 'W':
if (tree[n].f==Findarg)
n=tree[n].x;
gencode(fout,n);
return 1;
case 'r':
case 's':
gencast(fout,n,Gstr);
return 1;
case 'n':
gencast(fout,n,Gvar);
return 1;
default:
die(nerr,"Unsupported letter `%c' in prototype.\n"
"This function is not supported by the compiler."
,c);
return 0;
}
}
void gendefarg(int n, FILE *fout, char c, const char *name)
{
switch(c)
{
case 'G':
case '&':
case 'r':
case 's':
case 'E':
case 'I':
case 'V':
fprintf(fout,"NULL");
break;
case 'n':
fprintf(fout,"-1");
break;
case 'P':
fprintf(fout,"precdl");
break;
default:
die(n,"Unknown default in prototype code `%c' for `%s'",c,name);
}
}
void gendefargmulti(FILE *fout, char const *q, char const *p)
{
for(p++;p<q-3;p++)
if (p[0]!='\\' || p[1]!='"')
fputc(*p,fout);
}
int genfuncbycode(FILE *fout, int nb, int *arg, int nf, int nerr)
{
gpproto pro=lfunc[nf].proto;
const char *name=pro.cname;
const char *proto=pro.code;
int i=0;
int firstarg=0;
char const *p=proto,*q=proto;
char c;
PPproto mod;
if (!proto) return 1;
fprintf(fout,"%s(",name);
while((mod=parseproto(&p,&c)))
{
if (firstarg) fprintf(fout,", ");
firstarg=1;
switch(mod)
{
case PPauto:
genautoarg(fout,c,nerr);
break;
case PPstd:
if (i<nb && arg[i]!=GNOARG)
genarg(nerr,fout,c,arg[i],PPstd);
else
die(nerr,"Mandatory argument needed for %s",name);
i++;
break;
case PPdefault:
if (i<nb && arg[i]!=GNOARG)
genarg(nerr,fout,c,arg[i],PPdefault);
else
gendefarg(nerr,fout,c,name);
i++;
break;
case PPdefaultmulti:
if (i<nb && arg[i]!=GNOARG)
genarg(nerr,fout,c,arg[i],PPdefaultmulti);
else
gendefargmulti(fout,p,q);
i++;
break;
case PPstar:
{
int carg[STACKSZ];
int na,j;
for (na=0,j=i; j<nb; j++)
{
if (arg[j]==GNOARG)
i++;
else
na+=genlistcats(arg[j],carg+na,STACKSZ-na);
}
genfuncbydesc(fout,na,carg,FC_tovec,nerr);
i+=na;
break;
}
default:
die(nerr,"internal error: PPproto %d in genfuncbycode",mod);
}
q=p;
}
if(i<nb) die(nerr,"Too many arguments in function call");
fprintf(fout,")");
return 0;
}
int genfuncbycode1(FILE *fout, int arg, int nf, int nerr)
{
return genfuncbycode(fout,1,&arg,nf,nerr);
}
void gencodenoarg(FILE *fout, int t, int n)
{
int arg=newsmall(0);
tree[arg].t=t;
if (genfuncbydesc1(fout, arg, FC_default_marker, n))
die(n,"No implicit default for type %s",GPname(t));
stack_pop_safe(&s_node,arg);
}
void genuserfunc(FILE *fout, int n, int nf)
{
int arg[STACKSZ];
int nb,firstarg=0;
int j;
gpfunc *gp=lfunc+nf;
userfunc *ufunc=gp->user;
context *fc=block+gp->user->bl;
int narg = ufunc->narg;
if (narg && fc->c[ufunc->sarg+narg-1].flag&(1<<Cvararg))
nb=genlistargs(n,arg,0,STACKSZ);
else
nb=genlistargs(n,arg,0,narg);
fprintf(fout,"%s(",gp->proto.cname);
for(j=0;j<narg;j++)
{
ctxvar *v=fc->c+ufunc->sarg+j;
int t=vartype(*v);
if (firstarg)
fprintf(fout,", ");
firstarg=1;
if (v->flag&(1<<Cvararg))
{
int k;
for (k=j;k<nb;k++)
if (arg[k]==GNOARG) arg[k]=GNIL;
genfuncbydesc(fout,nb-j,arg+j,FC_tovec,nf);
}
else if (j<nb && arg[j]>=0 && arg[j]!=GNOARG)
gencast(fout,arg[j],t);
else if (v->flag&(1<<Cdefmarker))
gencodenoarg(fout,t,n);
else if (v->initval<0)
die(n,"a mandatory argument is missing");
else
gencast(fout,v->initval,t);
}
if (funcmode(*gp)&(1<<Mprec))
{
if (firstarg) fprintf(fout,", ");
else firstarg=1;
genprec(fout, preclevel);
}
fprintf(fout,")");
}
int genfunc(FILE *fout, int nb, int *arg, int nf, int nerr)
{
if (lfunc[nf].dsc && genfuncbydesc(fout,nb,arg,nf,nerr)==0)
return 0;
return genfuncbycode(fout,nb,arg,nf,nerr);
}
int genfunc1(FILE *fout, int arg, int nf, int nerr)
{
return genfunc(fout,1,&arg,nf,nerr);
}
void genentryfunc(FILE *fout, int n)
{
int stack[STACKSZ+1];
const char *name=entryname(n);
int i,nb;
int nf=findfunction(name);
int v = getvar(n);
if (nf >= 0)
{
gpfunc *gp=lfunc+nf;
if (gp->spec==0)
{
genuserfunc(fout,n,nf);
return;
}
else if (gp->spec>0)
{
genentryspec(fout,n,gp);
return;
}
nb=genlistargs(n,stack,0,STACKSZ);
if (genfunc(fout,nb,stack,nf,n)==0)
return;
if (gp->dsc)
{
int i;
for(i=0;i<nb;i++)
fprintf(stderr,"%d: %s\n", i, GPname(tree[stack[i]].t));
die(n," %s: arguments do not match descriptions",name);
}
if (gp->proto.cname)
name=gp->proto.cname;
}
else if (FC_call>=0 && v>=0)
{
nb=genlistargs(n,stack+1,0,STACKSZ-1)+1;
stack[0] = ctxstack[v].node;
if (!genfuncbydesc(fout,nb,stack,FC_call,n))
return;
}
/*copy verbatim*/
nb=genlistargs(n,stack,0,STACKSZ);
fprintf(fout,"%s(",name);
for(i=0;i<nb;i++)
{
if (i) fprintf(fout,", ");
gencode(fout,stack[i]);
}
fprintf(fout,")");
}
void genentry(FILE *fout, int n)
{
ctxvar *v=ctxstack+getvarerr(n);
fprintf(fout,"%s",v->cvar);
}
void genvarproto(FILE *fout, int n, int nerr)
{
if (genfuncbydesc1(fout,n,FC_decl_base,nerr))
die(nerr,"type not suitable for a variable");
fprintf(fout," ");
if (genfuncbydesc1(fout,n,FC_decl_ext,nerr))
gencode(fout,n);
}
void genfuncproto(FILE *fout, int nf, const char *sep, int nerr)
{
gpfunc *gp=lfunc+nf;
int n=gp->node;
if (genfuncbydesc1(fout,n,FC_decl_base,nerr))
die(nerr,"type not suitable for a function");
fprintf(fout,"%s",sep);
if (genfuncbydesc1(fout,n,FC_decl_ext,nerr))
gencode(fout,n);
}
static void gendecvarend(FILE *fout, int t)
{
if (t>=0)
{
if (t!=Ggen && t!=Gsmall && t!=Gpari_sp && t!=Gvoid)
fprintf(fout,";\t /* %s */\n",GPname(t));
else
fprintf(fout,";\n");
}
}
void gendecvar(FILE *fout, context *fc, int nerr)
{
int oldt=-1;
int idx;
if (fc->s.n>0)
genlineno(fout, fc->c->node);
for(idx=0;idx<fc->s.n;idx++)
{
ctxvar *v=fc->c+idx;
int t=vartype(*v);
if ((v->flag&(1<<Cconst)) && v->val!=-1)
continue;
if (v->flag&(1<<Carg))
continue;
if (is_subtype(vartype(*v),Gvoid))
continue;
if (t!=oldt)
{
gendecvarend(fout,oldt);
if (idx<fc->s.n-1)
genlineno(fout, (v+1)->node);
genindent(fout);
if (genfuncbydesc1(fout,v->node,FC_decl_base,nerr))
die(nerr,"type `%s' not suitable for a variable",GPname(t));
fprintf(fout, " ");
}
else
fprintf(fout, ", ");
if (genfuncbydesc1(fout,v->node,FC_decl_ext,nerr))
gencode(fout,v->node);
if (v->initval>=0)
{
fprintf(fout," = ");
gencast(fout,v->initval,vartype(*v));
}
else if (ctype[t]==Vgen && autogc)
{
/* We want to protect gerepile from uninitialized values*/
fprintf(fout," = ");
gencast(fout, GNIL, Ggen);
}
oldt=t;
}
gendecvarend(fout,oldt);
}
static void
genprotoargs(FILE *fout, int nf, int firstarg, int nb, int *name)
{
gpfunc *gp=lfunc+nf;
int i = 0;
char const *p=gp->proto.code;
char c;
PPproto mod;
while((mod=parseproto(&p,&c)))
{
if (firstarg) fprintf(fout,", ");
firstarg=1;
switch(c)
{
case 'b':
case 'p':
case 'P':
case 'L':
case 'n':
fprintf(fout,"long");
break;
case 'U':
fprintf(fout,"ulong");
break;
case 'f':
fprintf(fout,"long *");
break;
case 'W':
case 'G':
case 'E':
case 'I':
fprintf(fout,"GEN");
break;
case 'F':
case '*':
case '&':
fprintf(fout,"GEN *");
break;
case 'r':
case 's':
fprintf(fout,"const char *");
break;
default:
die(err_func,"prototype letter `%c' not known",c);
}
if (name)
{
fputs(" ",fout);
if (c=='p')
fprintf(fout,"prec");
else if (c=='b')
fprintf(fout,"bitprec");
else
{
if (i>=nb)
die(err_func,"too few arguments in lambda");
gencode(fout, name[i++]);
}
}
}
if (name && i<nb)
die(err_func,"too many arguments in lambda");
}
void genprotocode(FILE *fout, int nf)
{
fprintf(fout,"extern ");
genfuncproto(fout, nf, " ", -1);
fprintf(fout,"(");
genprotoargs(fout, nf, 0, 0, NULL);
fprintf(fout,")");
}
void genprototype(FILE *fout, int nf, int kb)
{
int firstarg;
gpfunc *gp=lfunc+nf;
int m=funcmode(*gp);
int i;
int nerr=gp->user->defnode;
int savc=s_ctx.n;
context *fc=block+gp->user->bl;
if (gp->user->flag&(1<<UFstatic))
fprintf(fout,"static ");
genfuncproto(fout,nf,kb?"\n":" ",nerr);
fprintf(fout,"(");
firstarg=0;
pushctx(fc);
for (i=0;i<fc->s.n;i++)
{
ctxvar *v=fc->c+i;
if( v->flag&(1<<Carg) )
{
if (firstarg) fprintf(fout,", ");
else firstarg=1;
genvarproto(fout,v->node, nerr);
if (v->initval>=0 && !(v->flag&(1<<Cdefmarker)) && vartype(*v)!=Ggen)
{
fprintf(fout,"/*=");
printnode(fout,v->initval);
fprintf(fout,"*/");
}
}
}
s_ctx.n=savc;
if (m&(1<<Mprec))
{
if (firstarg) fprintf(fout,", ");
else firstarg=1;
fprintf(fout,"long prec");
}
if (!firstarg) fprintf(fout,"void");
fprintf(fout,")");
}
static int
genwrapargs(FILE *fout, int nf, int nb, int *name, long mode)
{
gpfunc *gp=lfunc+nf;
int i = 0;
int firstarg = 1;
int t;
char const *p=gp->proto.code;
char c;
PPproto mod;
while((mod=parseproto(&p,&c)))
{
switch(c)
{
case 'b':
case 'p':
case 'P':
case 'L':
t = Gsmall;
break;
case 'n':
t = Gvar;
break;
case 'W':
case 'G':
case 'E':
case 'I':
t = Ggen;
break;
case 'r':
case 's':
t = Gstr;
break;
default:
die(err_func,"prototype letter `%c' not known",c);
}
if (c=='p' || c=='b')
{
if (mode&(1<<Mprec))
{
if (!firstarg) fprintf(fout,", ");
firstarg=0;
if (c=='p')
genprec(fout, preclevel);
else
genbitprec(fout, preclevel);
}
}
else
{
int ot;
if (i>=nb)
die(err_func,"too few arguments in lambda");
if (!firstarg) fprintf(fout,", ");
firstarg=0;
ot = tree[name[i]].t;
tree[name[i]].t = t;
gencast(fout, name[i], ot);
tree[name[i]].t = ot;
i++;
}
}
if (name && i<nb)
die(err_func,"too many arguments in lambda");
return firstarg;
}
void genwrapper(FILE *fout, int nf, int wrap)
{
gpfunc *gp=lfunc+nf;
gpfunc *wr=lfunc+wrap;
int t=functype(*gp), m=funcmode(*gp);
int has_prec=(m&(1<<Mprec)) && !(funcmode(*wr)&(1<<Mprec));
int i,firstarg=1,d;
int nerr=gp->user->defnode;
int savc=s_ctx.n;
int stack[STACKSZ];
int nb=0, nbc=0, par=0;
int res;
ctxvar *vres;
context *fc=block+gp->user->bl;
gpdescarg *rule;
if (!wr->proto.code)
die(wr->node,"Wrapper not defined");
pushctx(fc);
for (i=0;i<fc->s.n;i++)
{
ctxvar *v=fc->c+i;
if( v->flag&(1<<Carg) )
{
if (v->flag&(1<<Cclosed))
nbc++;
else
stack[nb++]=v->node;
}
}
fprintf(fout,"static ");
genfuncbydesc1(fout,wr->node,FC_decl_base,nerr);
fprintf(fout,"\nwrap_%s(void * _cargs",gp->proto.cname);
genprotoargs(fout, wrap, 1, nb, stack);
fprintf(fout,")\n{\n");
if (nbc || has_prec)
fprintf(fout," GEN _args = (GEN) _cargs;\n ");
else
fprintf(fout," (void) _cargs;\n ");
res = pushvar(newnode(Fentry, newentry("_res"), -1), 0, t, -1);
vres = ctxstack+res;
if (ctype[t]==ctype[Gvoid])
fprintf(fout,"(void) ");
else
{
genfuncbydesc1(fout,vres->node,FC_decl_base,nerr);
fprintf(fout," ");
if (genfuncbydesc1(fout,vres->node,FC_decl_ext,nerr))
gencode(fout,vres->node);
fprintf(fout," = ");
}
fprintf(fout,"%s(",gp->proto.cname);
firstarg=genwrapargs(fout, wrap, nb, stack, m);
for (i=0,d=1;i<fc->s.n;i++)
{
ctxvar *v=fc->c+i;
if (v->flag&(1<<Cclosed))
{
if (!firstarg)
fprintf(fout,", ");
else
firstarg=0;
fprintf(fout,"gel(_args,%d)",d++);
}
}
if (has_prec)
{
if (!firstarg)
fprintf(fout,", ");
fprintf(fout,"gtos(gel(_args,%d))",d++);
}
if (par)
fputc(')',fout);
fprintf(fout,");\n ");
rule = descfindrules(1, &vres->node, wr);
if (rule)
{
if (rule->type==Gvoid)
gencodedesc(fout,1, &vres->node, rule, nerr, nf);
else
{
fprintf(fout,"return ");
gencodedesc(fout,1, &vres->node, rule, nerr, nf);
fprintf(fout,";\n");
}
}
fprintf(fout,"}\n\n");
s_ctx.n=savc;
}
void gendeffunc(FILE *fout, int n)
{
int funcid=tree[n].x;
int seq=tree[n].y;
const char *name=entryname(funcid);
int savcf=currfunc;
gpfunc *gp;
int t;
/*get func number and context*/
currfunc=findfunction(name);
newcvar=1;
if (currfunc==-1)
die(n,"Internal error in gendeffunc : func %s not found",name);
gp=lfunc+currfunc;
gencomment(fout,funcid,0);
genlineno(fout, funcid);
genprototype(fout,currfunc,1);
t=functype(*gp);
if (t!=Ggen && t!=Gsmall)
fprintf(fout,"\t /* %s */",GPname(t));
fprintf(fout,"\n");
gencode(fout,seq);
fprintf(fout,"\n");
if (gp->user->wrapper>=0)
genwrapper(fout,currfunc,gp->user->wrapper);
currfunc=savcf;
}
void gendefblock(FILE *fout, int n)
{
int b=tree[n].x;
int seq=tree[n].y;
int i;
int m;
int savc;
int level = preclevel;
context *fc=block+b;
savc=s_ctx.n;
for(i=0;i<fc->s.n;i++)
{
ctxvar *v=fc->c+i;
int t=vartype(*v);
if ((v->flag&(1<<Cconst)) && v->val!=-1) continue;
if (t!=Gvoid && isdigit(*varstr(*v)))
{
char s[33];
sprintf(s,"%c%d",(ctype[t]==Vgen?'p':'l'),newcvar++);
v->cvar=strdup(s);
}
}
pushctx(fc);
m=tree[n].m;
if(!(m&(1<<Mbrace)))
{
genindentline(fout,"{\n");
indent++;
}
gendecvar(fout,fc,n);
genindentseq(fout,seq);
gencode(fout,seq);
gensemicolon(fout,seq);
if(!(m&(1<<Mbrace)))
{
indent--;
genendbrace(fout,n);
}
s_ctx.n=savc;
preclevel = level;
}
|