Mercurial > hg > CbC > old > device
changeset 34:0756caa79167
macro function recursive and copy method
author | kono |
---|---|
date | Mon, 10 Feb 2003 07:38:09 +0900 |
parents | 8500dbb69dd9 |
children | fd18d440f5ee |
files | .gdbinit mc-parse.c mc.h |
diffstat | 3 files changed, 168 insertions(+), 92 deletions(-) [+] |
line wrap: on
line diff
--- a/.gdbinit Sun Feb 09 23:17:23 2003 +0900 +++ b/.gdbinit Mon Feb 10 07:38:09 2003 +0900 @@ -1,6 +1,8 @@ tb main b error b errmsg +b macro_function +b macro_eval define regs call fprintf(stderr,"eax=%08x ebx=%08x ecx=%08x edx=%08x\nesi=%08x edi=%08x ebp=%08x esp=%08x eip=%08x\n",$eax,$ebx,$ecx,$edx,$esi,$edi,$ebp,$esp,$eip) end
--- a/mc-parse.c Sun Feb 09 23:17:23 2003 +0900 +++ b/mc-parse.c Mon Feb 10 07:38:09 2003 +0900 @@ -85,11 +85,12 @@ static int typespec(void); static int cexpr(int e); static void code_decl(NMTBL *n); -static int macro_args(); -static void fmacro(); +static int macro_args(char **pcheapp,char **pchptr); +static int macro_function(int macrop,char **pchptr,NMTBL *nptr,int history); static void local_define(); static void local_undef(); -static int recursive_macro(char *); +static int macro_eval(int macrop,char *body,int history); +static char * append(int lists); extern void display_ntable(NMTBL *n, char *s); extern void closing(void); @@ -234,9 +235,8 @@ if(lineno==0) return; fprintf(stderr,"%s",linebuf); lim=chptr; - while (chptrsave) { - lim = (char *)car(chptrsave); - chptrsave = cadr(chptrsave); + if (chptrsave) { + lim = chptrsave; } for (p=linebuf; p < lim;) fprintf(stderr,(*p++ == '\t') ? "\t" : " "); @@ -297,7 +297,7 @@ reserve("code",CODE); reserve("environment",ENVIRONMENT); - gpc=glineno=mflag=0; + gpc=glineno=0; gfree=ilabel=1; labelno=2; lfree=HEAPSIZE; @@ -1809,10 +1809,6 @@ type=nptr->ty; getsym(); break; - case FMACRO: - fmacro(); - getsym(); - return expr(); case FLABEL: case BLABEL: case FUNCTION: case CODE: e1=list2(FNAME,(int)nptr); @@ -2198,8 +2194,8 @@ getsym(void) { NMTBL *nptr0,*nptr1,*nptrm; - int i,macro_type; - char c,*macro; + int i,slfree,macrop; + char c,*save_cheapp; if (alpha(skipspc())) { i = hash = 0; @@ -2223,34 +2219,29 @@ return (symval=0); } } - if (nptrm->sc!=EMPTY && !recursive_macro((char *)car((nptrm->dsp)))) { + if (nptrm->sc!=EMPTY) { + i = mode; + mode = STAT; + macrop = 0; + save_cheapp = cheapp; + slfree = lfree; + macropp = macro_buf; if (nptrm->sc == FMACRO) { - nptr = nptrm; - sym = IDENT; - return sym; + macrop=macro_function(macrop,&chptr,nptrm,0); + } else { + macrop=macro_eval(macrop,(char *)car(nptrm->dsp),0); } - if ((macro_type = nptrm->ty)!=0) { - macro = (char *)(car(nptrm->dsp)); - while (!(macro_type<=0||cadddr(macro_type)==mflag)) { - macro = (char *)car(caddr(macro_type)); - macro_type = cadr(macro_type); - } - if (macro_type) { -/* fprintf(stderr,"%d: macro %s save: ch=%c chptr=%s\n",lineno,name,ch,chptr); */ - chptrsave = list4((int)chptr,chptrsave,ch, - list2((int)nptrm,(int)macro)); - if (macro_type!=-1) { - mflag--; - } - chptr = macro; - getch(); - sym = getsym(); - if (macro_type!=-1) { - mflag++; - } - return sym; - } - } /* else diffrent depth level localmacro leave it alone */ + macropp = macro_buf; + append(reverse0(macrop)); + cheapp = save_cheapp; + lfree = slfree; +fprintf(stderr,"#macro: %s => %s\n",nptrm->nm,macro_buf); + chptrsave = chptr; + chsave = ch = chptr[-1]; + chptr = macro_buf; + ch = *chptr++; + mode = i; + return getsym(); } nptr0 = gsearch(); @@ -2369,12 +2360,21 @@ return sym=PERIOD; case '/': if(ch!='*') return postequ(DIV,DIV+AS); + if(ch=='/') { + while(ch!='\n') getch(); + getch(); + return getsym(); + } in_comment = 1; getch(); while(ch=='*'?getch()!='/':getch()); in_comment = 0; getch(); return getsym(); + case 0: + case '\n': + getch(); + return getsym(); default: error(CHERR); return getsym(); @@ -2528,26 +2528,12 @@ int getch(void) { - NMTBL *nptr0; - int sargs; if(*chptr) return ch = *chptr++; - - while (chptrsave) { - nptr0=(NMTBL *)car(cadddr(chptrsave)); - if (nptr0->sc==FMACRO) { - sargs = cadr(nptr0->dsp); - while(sargs) { - local_undef(car(sargs)); - sargs = cadr(sargs); - } - mflag--; - } - chptr=(char *)car(chptrsave); - ch=caddr(chptrsave); - chptrsave=cadr(chptrsave); -/* fprintf(stderr,"macro end: ch=%c chptr=%s\n",ch,chptr); */ - if (ch) - return ch; + else if (chptrsave) { + chptr = chptrsave; + ch = chsave; + chptrsave = 0; + return ch; } getline(); return getch(); @@ -2767,40 +2753,49 @@ mode=MDECL; ch= *chptr; getsym(); -/* fprintf(stderr,"macro def: %s\n",name); */ +fprintf(stderr,"macro def: %s =>",name); if (nptr->sc != EMPTY) { /* override exisiting macro */ } args = 0; if (ch=='(') { nptr->sc = FMACRO; - args = macro_args(); + args = macro_args(&cheapp,&chptr); } else { nptr->sc = MACRO; - /* -1 means gloabl macro, 0 means no macro, >0 means local macro */ nptr->ty = -1; } - nptr->dsp = glist2((int)cheapp,args); + nptr->dsp = list2((int)cheapp,args); /* macro body */ while ((*cheapp++ = c = *chptr++) - && c != '\n'); + && c != '\n') { + if (c=='\\' && chptr[1]=='\n') { + cheapp--; + getline(); + } + } *cheapp++ = '\0'; if (cheapp >= cheap+CHEAPSIZE) /* too late? */ error(STRERR); +fprintf(stderr,"%s\n",(char *)car(nptr->dsp)); mode=i; } int -macro_args() +macro_args(char **pcheapp,char **pchptr) { int c; int in_quote = 0; int in_wquote = 0; int plevel = 0; - int args = glist2((int)cheapp,0); + char *cheapp = *pcheapp; + char *chptr = *pchptr; + int args = list2((int)cheapp,0); for(;;) { *cheapp++ = c = *chptr++; if (!c) { chptr--; error(MCERR); + *pchptr = chptr; + *pcheapp = cheapp; return reverse0(args); } if (in_quote) { @@ -2831,7 +2826,7 @@ } if (plevel==0) { if (c==',') { cheapp[-1] = 0; - args = glist2((int)cheapp,args); + args = list2((int)cheapp,args); } else if (c==')') { cheapp[-1] = 0; break; @@ -2847,47 +2842,54 @@ } else if (c=='\n') { cheapp--; getline(); + chptr = *pchptr; } } else if (c==')') { plevel--; } else if (c=='\n') { cheapp--; getline(); + chptr = *pchptr; } } ch = *chptr; if (ch) chptr++; + *pchptr = chptr; + *pcheapp = cheapp; return reverse0(args); } -void -fmacro() +/* output macro expansion result into macrobuf (macropp) */ + +int +macro_function(int macrop,char **pchptr,NMTBL *nptr,int history) { - int args,values; - args = cadr(nptr->dsp); - values = macro_args(); - mflag++; - while(args) { - local_define(car(args),car(values)); - args = cadr(args); + int args,sargs,values,evalues; + char *macro; + + sargs = args = cadr(nptr->dsp); + values = macro_args(¯opp,pchptr); + evalues = 0; + while(values) { + evalues = list2(macro_eval(0,(char *)car(values),history),evalues); values = cadr(values); } -/* fprintf(stderr,"%d: fmacro %s save: ch=%c chptr=%s\n",lineno,nptr->nm,ch,chptr); */ - chptrsave = list4((int)chptr,chptrsave,ch,list2((int)nptr,car(nptr->dsp))); - chptr = (char *)car(nptr->dsp); - getch(); -} - -int -recursive_macro(char *macro) -{ - int save = chptrsave; - while(save) { - if (cadr(cadddr(save))==(int)macro) - return 1; - save = cadr(save); + evalues = reverse0(evalues); + while(args) { + local_define((char *)car(args),append(reverse0(car(evalues)))); +fprintf(stderr,"%s: %s => %s\n",nptr->nm,(char *)car(args),(char *)car(msearch0((char *)car(args))->dsp)); + args = cadr(args); + evalues = cadr(evalues); } - return 0; + macro = (char *)car(nptr->dsp); + macrop = macro_eval(macrop,macro,list2((int)macro,history)); +fprintf(stderr,"%s: result %s => %s\n",nptr->nm,macro,(char *)car(macrop)); + args = sargs; + while(args) { + local_undef((char *)car(args)); + args = cadr(args); + } + return macrop; } void @@ -2895,7 +2897,7 @@ { NMTBL *nptr0; nptr0 = msearch0(macro); - nptr0->ty=list4(nptr0->sc,nptr0->ty,nptr0->dsp,mflag); + nptr0->ty=list3(nptr0->sc,nptr0->ty,nptr0->dsp); nptr0->sc=MACRO; nptr0->dsp=list2((int)value,0); } @@ -2913,6 +2915,60 @@ } int +macro_eval(int macrop,char *body,int history) +{ + int c; + int in_quote = 0; + int in_wquote = 0; + char *macro; + int i; + NMTBL *nptrm; + c = 1; + macrop = list2((int)macropp,macrop); + while(c && (*macropp++ = c = *body++)) { + if (in_quote) { + if (c=='\\') { + *macropp++ = c = *body++; + } else if (c=='\'') { + in_quote = 0; + } + } else if (in_wquote) { + if (c=='\\') { + *macropp++ = c = *body++; + } else if (c=='"') { + in_wquote = 0; + } + } else if (c=='"') { + in_wquote = 1; + } else if (c=='\'') { + in_quote = 1; + } else if (alpha(c)) { + macropp--; + for(i=0;alpha(c)||digit(c);i++) { namebuf[i] = c; c=*body++;} + namebuf[i]=0; + nptrm = msearch0(namebuf); + macro = (char *)car(nptrm->dsp); + if (nptrm->sc==MACRO) { + while((*macropp++ = *macro++)); + macropp[-1]=c; + } else if (nptrm->sc==FMACRO) { + if(c!='(') error(MCERR); + *macropp++=0; + macrop = macro_function(macrop,&body,nptrm, + list2((int)macro,history)); + macrop = list2((int)macropp,macrop); + } else { + macro = namebuf; + while((*macropp++ = *macro++)); + macropp[-1]=c; + } + } + } + *macropp++=0; + return macrop; +} + +int list2(int e1, int e2) { int e; @@ -2955,6 +3011,7 @@ switch (mode) { case GDECL: case GSDECL: case GUDECL: case GTDECL: + case MDECL: e=gfree; gfree+=n; break; @@ -2991,6 +3048,20 @@ return e; } +char * +append(int lists) +{ + char *p; + char *result = macropp; + while(lists) { + p = (char *)car(lists); + while((*macropp++=*p++)); + macropp--; + lists = cadr(lists); + } + macropp++; + return result; +} void display_ntable(NMTBL *n, char *s)
--- a/mc.h Sun Feb 09 23:17:23 2003 +0900 +++ b/mc.h Mon Feb 10 07:38:09 2003 +0900 @@ -175,6 +175,7 @@ /* #define CHEAPSIZE 3000 */ #define CHEAPSIZE 256000 #define LBUFSIZE 4096 +#define MACROSIZE 16000 #define FILES 10 #define OUTPUT_FILE_NAME "mcout.s" @@ -187,7 +188,8 @@ EXTERN unsigned hash; -EXTERN int chptrsave; +EXTERN int chsave; +EXTERN char *chptrsave; EXTERN char linebuf[LBUFSIZE],namebuf[LBUFSIZE],*chptr; EXTERN char *name,*cheapp,**av,/*obuf[320],*/*sptr,escape(void); EXTERN int arg_offset,stat_no,size_of_int,disp_offset,endian,csvalue,csvalue1; @@ -210,6 +212,7 @@ EXTERN struct {int fd,ln;char *name0;FILE *fcb;} *filep,filestack[FILES]; EXTERN char cheap[CHEAPSIZE]; +EXTERN char *macropp,macro_buf[MACROSIZE]; #define car(e) (heap[(int)(e)])