From 472654c36656afc69a33032cde71a1037b760568 Mon Sep 17 00:00:00 2001 From: carl Date: Sat, 11 May 2019 01:17:24 +0800 Subject: [PATCH] ANSI C conversion and add procedure declarations. --- util/ncgg/cgg.y | 11 + util/ncgg/coerc.c | 248 +++++---- util/ncgg/coerc.h | 30 ++ util/ncgg/emlookup.c | 84 +-- util/ncgg/error.c | 11 +- util/ncgg/expr.c | 391 ++++++++------ util/ncgg/expr.h | 23 + util/ncgg/extern.h | 7 +- util/ncgg/hall.c | 149 +++--- util/ncgg/hall.h | 17 + util/ncgg/instruct.c | 260 ++++----- util/ncgg/instruct.h | 7 + util/ncgg/iocc.c | 219 ++++---- util/ncgg/iocc.h | 6 + util/ncgg/lookup.c | 91 ++-- util/ncgg/lookup.h | 7 +- util/ncgg/main.c | 71 ++- util/ncgg/output.c | 1168 +++++++++++++++++++++++------------------ util/ncgg/scan.l | 10 +- util/ncgg/set.c | 198 +++---- util/ncgg/set.h | 14 + util/ncgg/strlookup.c | 21 +- util/ncgg/subr.c | 468 +++++++++-------- util/ncgg/subr.h | 42 ++ 24 files changed, 2035 insertions(+), 1518 deletions(-) create mode 100644 util/ncgg/coerc.h create mode 100644 util/ncgg/hall.h create mode 100644 util/ncgg/subr.h diff --git a/util/ncgg/cgg.y b/util/ncgg/cgg.y index c77f566d7..c648d276c 100644 --- a/util/ncgg/cgg.y +++ b/util/ncgg/cgg.y @@ -14,6 +14,9 @@ static char rcsid[]= "$Id$"; #include "iocc.h" #include "instruct.h" #include "expr.h" +#include "coerc.h" +#include "hall.h" +#include "subr.h" #include "extern.h" #include #include @@ -49,6 +52,14 @@ iocc_t subr_iocc(),tokm_iocc(),ident_iocc(),all_iocc(),percident_iocc(), descr_i extern int narexpr; extern expr_t arexp[]; +extern void skipupto(int tok,char *str); +extern void outpatterns(void); +extern void dopattern(int stackcoerc, varinfo *kills, varinfo *allocates, + varinfo *generates, varinfo *yields, varinfo *leaving); +extern int yylex(void); +extern int argtyp(int mn); +extern void yyerror (char const *s); + int niops; iocc_t iops[20]; %} diff --git a/util/ncgg/coerc.c b/util/ncgg/coerc.c index 85148cd43..23b711b22 100644 --- a/util/ncgg/coerc.c +++ b/util/ncgg/coerc.c @@ -3,7 +3,7 @@ * See the copyright notice in the ACK home directory, in the file "Copyright". */ #ifndef NORCSID -static char rcsid[]= "$Id$"; +static char rcsid[] = "$Id$"; #endif #include @@ -14,6 +14,9 @@ static char rcsid[]= "$Id$"; #include "token.h" #include "varinfo.h" #include "iocc.h" +#include "subr.h" +#include "hall.h" +#include "coerc.h" #include #include "pseudo.h" #include "extern.h" @@ -22,165 +25,181 @@ extern set_t l_sets[]; int nmoves; move_t l_moves[MAXMOVES]; -short posmoves[MAXREGS+MAXTOKENS][SETSIZE]; +short posmoves[MAXREGS + MAXTOKENS][SETSIZE]; -void -n_move(s1,e1,s2,e2,vi) struct varinfo *vi; { +void n_split(int , int, struct varinfo *, struct varinfo *, struct varinfo *, int); + +extern void dopattern(int stackcoerc, varinfo *kills, varinfo *allocates, + varinfo *generates, varinfo *yields, varinfo *leaving);; + +void n_move(int s1, int e1, int s2, int e2, struct varinfo *vi) +{ register move_p mp; - register i,j; + register int i, j; - NEXT(nmoves,MAXMOVES,"Moves"); - mp = &l_moves[nmoves-1]; + NEXT(nmoves, MAXMOVES, "Moves"); + mp = &l_moves[nmoves - 1]; mp->m_set1 = s1; mp->m_expr1 = e1; mp->m_set2 = s2; mp->m_expr2 = e2; mp->m_cindex = codeindex; - dopattern(0,VI_NULL,VI_NULL,vi,VI_NULL,VI_NULL); - if (mp->m_expr1!=0 || mp->m_expr2!=0) + dopattern(0, VI_NULL, VI_NULL, vi, VI_NULL, VI_NULL); + if (mp->m_expr1 != 0 || mp->m_expr2 != 0) return; - for (i=0;im_set1].set_val,i)) - for(j=0;jm_set2].set_val[j]; + for (i = 0; i < MAXREGS + MAXTOKENS; i++) + if (BIT(l_sets[mp->m_set1].set_val, i)) + for (j = 0; j < SETSIZE; j++) + posmoves[i][j] |= l_sets[mp->m_set2].set_val[j]; } -existmove(from,sp) iocc_t from; short *sp; { - register i; +int existmove(iocc_t from, short *sp) +{ + register int i; - for (i=0;ivi_int[0] = INSMOVE; vp->vi_int[1] = from.in_index; vp->vi_int[2] = to.in_index; - return(vp); + return (vp); } int ntests; test_t l_tests[MAXTESTS]; short postests[SETSIZE]; -void -n_test(s,e,vi) struct varinfo *vi; { +void n_test(int s, int e, struct varinfo *vi) +{ register test_p tp; - register i; + register int i; - NEXT(ntests,MAXTESTS,"Tests"); - tp = &l_tests[ntests-1]; + NEXT(ntests, MAXTESTS, "Tests"); + tp = &l_tests[ntests - 1]; tp->t_set = s; - tp->t_expr = e; + tp->t_expr = e; tp->t_cindex = codeindex; - dopattern(0,VI_NULL,VI_NULL,vi,VI_NULL,VI_NULL); - if (tp->t_expr!=0) + dopattern(0, VI_NULL, VI_NULL, vi, VI_NULL, VI_NULL); + if (tp->t_expr != 0) return; - for(i=0;it_set].set_val[i]; } -struct varinfo *gen_test(from) iocc_t from; { +struct varinfo *gen_test(iocc_t from) +{ register struct varinfo *vp; - if (!subset(from.in_set,postests,SETSIZE)) { + if (!subset(from.in_set, postests, SETSIZE)) + { error("No such test"); - return(0); + return (0); } - NEW(vp,struct varinfo); + NEW(vp, struct varinfo); vp->vi_int[0] = INSTEST; vp->vi_int[1] = from.in_index; - return(vp); + return (vp); } -struct varinfo *gen_label(arg) int arg; { +struct varinfo *gen_label(int arg) +{ register struct varinfo *vp; - NEW(vp,struct varinfo); + NEW(vp, struct varinfo); vp->vi_int[0] = INSLABDEF; vp->vi_int[1] = arg; - return(vp); + return (vp); } -struct varinfo *gen_preturn() { +struct varinfo *gen_preturn(void) +{ register struct varinfo *vp; - NEW(vp,struct varinfo); + NEW(vp, struct varinfo); vp->vi_int[0] = INSPRETURN; - return(vp); + return (vp); } -struct varinfo *gen_tlab(n) { +struct varinfo *gen_tlab(int n) +{ register struct varinfo *vp; - assert(n>=0 && n<=9); - NEW(vp,struct varinfo); + assert(n >= 0 && n <= 9); + NEW(vp, struct varinfo); vp->vi_int[0] = INSTLAB; vp->vi_int[1] = n; - return(vp); + return (vp); } int nstacks; c1_t l_stacks[MAXSTACKS]; -set_t ustackset,cstackset; +set_t ustackset, cstackset; -n_stack(s,e,p,vi) struct varinfo *vi; { +void n_stack(int s, int e, int p, struct varinfo *vi) +{ register c1_p c1p; register short *sp; - register i; + register int i; - NEXT(nstacks,MAXSTACKS,"Stacks"); - c1p= & l_stacks[nstacks-1]; + NEXT(nstacks, MAXSTACKS, "Stacks"); + c1p = &l_stacks[nstacks - 1]; c1p->c1_texpno = s; c1p->c1_expr = e; c1p->c1_prop = p; c1p->c1_codep = codeindex; - dopattern(0,VI_NULL,VI_NULL,vi,VI_NULL,VI_NULL); + dopattern(0, VI_NULL, VI_NULL, vi, VI_NULL, VI_NULL); - if (e==0 && p== -1) + if (e == 0 && p == -1) sp = ustackset.set_val; else sp = cstackset.set_val; - for(i=0;itk_name); + chkset = ustackset.set_val; + warn = ""; + for (i = 1; i < nregs; i++) + if (BIT(sp,i) && !BIT(chkset, i)) + error("No %sstacking rule for register %s", warn, + l_regs[i].ri_name); + for (; i < nregs + MAXTOKENS; i++) + if (BIT(sp,i) && !BIT(chkset, i)) + error("No %sstacking rule for token %s", warn, + l_tokens[i - nregs]->tk_name); } int ncoercs; @@ -189,78 +208,89 @@ set_t unstackset; /*VARARGS5*/ -void -n_coerc(ti,be,al,ge,rp,in) struct varinfo *al,*ge,*rp; iocc_t in; { +void n_coerc(int ti, int be, struct varinfo *al, struct varinfo *ge, struct varinfo *rp, iocc_t in) +{ register c3_p c3p; - register i; + register int i; register struct varinfo *vi; - if (ti!=0) { - for(i=0,vi=rp;vi!=0;vi=vi->vi_next,i++) + if (ti != 0) + { + for (i = 0, vi = rp; vi != 0; vi = vi->vi_next, i++) ; - if (i>1) { - n_split(ti,be,al,ge,rp,i); + if (i > 1) + { + n_split(ti, be, al, ge, rp, i); return; - } else { - if (i==0) { + } + else + { + if (i == 0) + { error("Coercion should have a result!"); return; } } - } else { - NEW(rp,struct varinfo); + } + else + { + NEW(rp, struct varinfo); rp->vi_next = 0; rp->vi_int[0] = in.in_index; } - if (nallreg>1) + if (nallreg > 1) error("More than 1 register may not be allocated"); - NEXT(ncoercs,MAXCOERCS,"Coercions"); - c3p = & l_coercs[ncoercs-1]; + NEXT(ncoercs, MAXCOERCS, "Coercions"); + c3p = &l_coercs[ncoercs - 1]; c3p->c3_texpno = ti; c3p->c3_expr = be; - c3p->c3_prop = nallreg==0 ? -1 : allreg[0]; + c3p->c3_prop = nallreg == 0 ? -1 : allreg[0]; c3p->c3_repl = rp->vi_int[0]; c3p->c3_codep = codeindex; - dopattern(ti==0,VI_NULL,al,ge,rp,VI_NULL); - if (ti==0) - for(i=0;iMAXSPLIT) { - error("Maximum split factor is %d",MAXSPLIT); + NEXT(nsplit, MAXSPLCOERC, "Splitting coercions"); + c2p = &l_split[nsplit - 1]; + if (n > MAXSPLIT) + { + error("Maximum split factor is %d", MAXSPLIT); n = MAXSPLIT; } - if (n>maxsplit) maxsplit=n; + if (n > maxsplit) + maxsplit = n; c2p->c2_texpno = ti; c2p->c2_expr = be; if (nallreg) error("No register uses allowed in splitting coercion"); c2p->c2_nsplit = n; - for (i=0,vi=rp; ivi_next) + for (i = 0, vi = rp; i < n; i++, vi = vi->vi_next) c2p->c2_repl[i] = vi->vi_int[0]; c2p->c2_codep = codeindex; - dopattern(0,VI_NULL,al,ge,rp,VI_NULL); + dopattern(0, VI_NULL, al, ge, rp, VI_NULL); } diff --git a/util/ncgg/coerc.h b/util/ncgg/coerc.h new file mode 100644 index 000000000..b34338e2f --- /dev/null +++ b/util/ncgg/coerc.h @@ -0,0 +1,30 @@ +/* Copyright (c) 2019 ACK Project. + * See the copyright notice in the ACK home directory, + * in the file "Copyright". + * + * Created on: 2019-05-03 + * + */ +#ifndef COERC_H_ +#define COERC_H_ + +#include "iocc.h" + +struct varinfo; + + +void n_move(int s1, int e1, int s2, int e2, struct varinfo *vi); +int existalmove(iocc_t from, int prpno); +struct varinfo *gen_move(iocc_t from, iocc_t to); +void n_test(int s, int e, struct varinfo *vi); +struct varinfo *gen_test(iocc_t from); +struct varinfo *gen_label(int arg); +struct varinfo *gen_preturn(void); +struct varinfo *gen_tlab(int n); +void n_stack(int s, int e, int p, struct varinfo *vi); +void checkstacking(register short *sp); +/* cgg.y is buggy!!! it has one less parameter.. */ +/*void n_coerc(int ti, int be, struct varinfo *al, struct varinfo *ge, struct varinfo *rp, iocc_t in);*/ +void checkunstacking(int setno); + +#endif /* COERC_H_ */ diff --git a/util/ncgg/emlookup.c b/util/ncgg/emlookup.c index 6927f9259..13f5b892b 100644 --- a/util/ncgg/emlookup.c +++ b/util/ncgg/emlookup.c @@ -3,7 +3,7 @@ * See the copyright notice in the ACK home directory, in the file "Copyright". */ #ifndef NORCSID -static char rcsid[]= "$Id$"; +static char rcsid[] = "$Id$"; #endif #include @@ -18,64 +18,76 @@ extern char em_mnem[][4]; #define HASHSIZE (2*(sp_lmnem-sp_fmnem)) -struct emhashmnem { +struct emhashmnem +{ char h_name[3]; char h_value; } emhashmnem[HASHSIZE]; -initemhash() { - register i; +/* Forward declarations */ +static void enter(char *, int); +static unsigned emhash(register char *); - for(i=0;i<=sp_lmnem-sp_fmnem;i++) - enter(em_mnem[i],i+sp_fmnem); + +void initemhash(void) +{ + register int i; + + for (i = 0; i <= sp_lmnem - sp_fmnem; i++) + enter(em_mnem[i], i + sp_fmnem); enter("lab", op_lab); } -unsigned emhash(name) register char *name; { - register unsigned sum; - register i; +static unsigned emhash(register char *name) +{ + register unsigned int sum; + register int i; - for (sum=i=0;*name;i+=3) - sum ^= (*name++)<<(i&07); - return(sum); + for (sum = i = 0; *name; i += 3) + sum ^= (*name++) << (i & 07); + return (sum); } -enter(name,value) char *name; { +static void enter(char *name, int value) +{ register unsigned h; - h=emhash(name)%HASHSIZE; + h = emhash(name) % HASHSIZE; while (emhashmnem[h].h_name[0] != 0) - h = (h+1)%HASHSIZE; - strncpy(emhashmnem[h].h_name,name,3); + h = (h + 1) % HASHSIZE; + strncpy(emhashmnem[h].h_name, name, 3); emhashmnem[h].h_value = value; } -int mlookup(name) char *name; { +int mlookup(char *name) +{ register unsigned h; - h = emhash(name)%HASHSIZE; - while (strncmp(emhashmnem[h].h_name,name,3) != 0 && - emhashmnem[h].h_name[0] != 0) - h = (h+1)%HASHSIZE; - return(emhashmnem[h].h_value&0xFF); /* 0 if not found */ + h = emhash(name) % HASHSIZE; + while (strncmp(emhashmnem[h].h_name, name, 3) != 0 + && emhashmnem[h].h_name[0] != 0) + h = (h + 1) % HASHSIZE; + return (emhashmnem[h].h_value & 0xFF); /* 0 if not found */ } extern char em_flag[]; -argtyp(mn) { +int argtyp(int mn) +{ - switch(em_flag[mn-sp_fmnem]&EM_PAR) { - case PAR_W: - case PAR_S: - case PAR_Z: - case PAR_O: - case PAR_N: - case PAR_L: - case PAR_F: - case PAR_R: - case PAR_C: - return(TYPINT); - default: - return(TYPADDR); + switch (em_flag[mn - sp_fmnem] & EM_PAR) + { + case PAR_W: + case PAR_S: + case PAR_Z: + case PAR_O: + case PAR_N: + case PAR_L: + case PAR_F: + case PAR_R: + case PAR_C: + return (TYPINT); + default: + return (TYPADDR); } } diff --git a/util/ncgg/error.c b/util/ncgg/error.c index 6eaf59784..0ac822f24 100644 --- a/util/ncgg/error.c +++ b/util/ncgg/error.c @@ -13,12 +13,12 @@ static char rcsid[]= "$Id$"; int nerrors=0; -yyerror(s) char *s; { - +void yyerror(char *s) +{ error("Parser gives %s",s); } -goodbye() { +void goodbye(void) { error("This was fatal, goodbye!"); #ifndef NDEBUG @@ -60,7 +60,8 @@ void error(const char* s, ...) va_end(ap); } -tabovf(string) char *string; { - +int tabovf(char *string) +{ fatal("%s overflow",string); + return 0; } diff --git a/util/ncgg/expr.c b/util/ncgg/expr.c index d1a345961..1a70e7e13 100644 --- a/util/ncgg/expr.c +++ b/util/ncgg/expr.c @@ -3,7 +3,7 @@ * See the copyright notice in the ACK home directory, in the file "Copyright". */ #ifndef NORCSID -static char rcsid[]= "$Id$"; +static char rcsid[] = "$Id$"; #endif #include @@ -24,263 +24,308 @@ static char rcsid[]= "$Id$"; extern set_t l_sets[]; -i_expr(e) expr_t e; { +int i_expr(expr_t e) +{ if (e.ex_typ != TYPINT) error("Expression should be integer"); - return(e.ex_index); + return (e.ex_index); } -b_expr(e) expr_t e; { +int b_expr(expr_t e) +{ if (e.ex_typ != TYPBOOL) error("Expression should be boolean"); - return(e.ex_index); + return (e.ex_index); } -expr_t make_expr(type,operator,op1,op2) { +expr_t make_expr(int type, int operator, int op1, int op2) +{ expr_t result; - result.ex_typ=type; - result.ex_index=ex_lookup(operator,op1,op2); - return(result); + result.ex_typ = type; + result.ex_index = ex_lookup(operator, op1, op2); + return (result); } -expr_t regno_expr(regno) { +expr_t regno_expr(int regno) +{ expr_t result; - register i; + register int i; result.ex_typ = TYPREG; - result.ex_index = ex_lookup(EX_REG,regno,0); - for (i=0;isy_type==symconst) - return(make_expr(TYPINT,EX_CON, - (int) (sy_p->sy_value.syv_cstval&0xFFFF), - (int) (sy_p->sy_value.syv_cstval>>16))); - else if (sy_p->sy_type==symsconst) - return(make_expr(TYPADDR,EX_STRING,sy_p->sy_value.syv_stringno,0)); - else if (sy_p->sy_type!=symreg) - error("Wrong type of identifier %s",name); - return(regno_expr(sy_p->sy_value.syv_regno)); + sy_p = lookup(name, symany, mustexist); + if (sy_p->sy_type == symconst) + return (make_expr(TYPINT, EX_CON, + (int) (sy_p->sy_value.syv_cstval & 0xFFFF), + (int) (sy_p->sy_value.syv_cstval >> 16))); + else if (sy_p->sy_type == symsconst) + return (make_expr(TYPADDR, EX_STRING, sy_p->sy_value.syv_stringno, 0)); + else if (sy_p->sy_type != symreg) + error("Wrong type of identifier %s", name); + return (regno_expr(sy_p->sy_value.syv_regno)); } -expr_t subreg_expr(tokarg,subreg) { +expr_t subreg_expr(int tokarg, int subreg) +{ expr_t result; result.ex_typ = TYPREG; - subregset(l_sets[tokpatset[tokarg > 0 ? tokarg-1 : tokarg]].set_val, - subreg,result.ex_regset); - result.ex_index = ex_lookup(EX_SUBREG,tokarg,subreg); - return(result); + subregset(l_sets[tokpatset[tokarg > 0 ? tokarg - 1 : tokarg]].set_val, + subreg, result.ex_regset); + result.ex_index = ex_lookup(EX_SUBREG, tokarg, subreg); + return (result); } -subregset(sp,subreg,regset) register short *sp; register short *regset; { - register i; +void subregset(register short *sp, int subreg, register short *regset) +{ + register int i; register reginfo *rp; - for (i=0;iri_memb[subreg-1]==0) - error("Register %s in set has no member %d", - rp->ri_name,subreg); - BIS(regset,rp->ri_memb[subreg-1]); - } else - BIS(regset,i); - } - for(;itk_name); + for (i = 0; i < SZOFSET(MAXREGS); i++) + regset[i] = 0; + for (i = 1; i < nregs; i++) + if (BIT(sp, i)) + { + if (subreg) + { + rp = &l_regs[i]; + if (rp->ri_memb[subreg - 1] == 0) + error("Register %s in set has no member %d", rp->ri_name, + subreg); + BIS(regset, rp->ri_memb[subreg - 1]); + } + else + BIS(regset, i); + } + for (; i < nregs + MAXTOKENS; i++) + if (BIT(sp, i)) + error("Set contains %s, which is not a register", + l_tokens[i - nregs]->tk_name); } -membset(setno,name,regset,appearance,restyp,typp) -char *name,*appearance; -short *regset; -int *typp; +int membset(int setno, char *name, short *regset, char *appearance, int restyp, int *typp) { register short *sp; register token_p tp; - register i,j,k; + register int i, j, k; int thistyp; - int typesdiffer=0; - int res_j= -1; + int typesdiffer = 0; + int res_j = -1; - if (setno < 0) return 0; + if (setno < 0) + return 0; sp = l_sets[setno].set_val; - for (i=1;itk_att[j].ta_type == -3 || - strcmp(tp->tk_att[j].ta_name,name));j++) - ; - if (j==MAXATT) - error("Token %s does not contain %s",tp->tk_name,name); - else if (j!=res_j && res_j != -1) - typesdiffer=1; - else { - res_j = j; - thistyp = tp->tk_att[j].ta_type; - if (thistyp== -2) { - if (restyp!=TYPADDR && restyp!=0) - typesdiffer=1; + for (i = nregs; i < nregs + MAXTOKENS; i++) + if (BIT(sp, i)) + { + tp = l_tokens[i - nregs]; + for (j = 0; + j < MAXATT + && (tp->tk_att[j].ta_type == -3 + || strcmp(tp->tk_att[j].ta_name, name)); + j++) + ; + if (j == MAXATT) + error("Token %s does not contain %s", tp->tk_name, name); + else if (j != res_j && res_j != -1) + typesdiffer = 1; + else + { + res_j = j; + thistyp = tp->tk_att[j].ta_type; + if (thistyp == -2) + { + if (restyp != TYPADDR && restyp != 0) + typesdiffer = 1; + else + restyp = TYPADDR; + } + else if (thistyp == -1) + { + if (restyp != TYPINT && restyp != 0) + typesdiffer = 1; + else + restyp = TYPINT; + } else - restyp=TYPADDR; - } else if (thistyp== -1) { - if (restyp!=TYPINT && restyp!=0) - typesdiffer=1; - else - restyp=TYPINT; - } else { - if (restyp!=TYPREG && restyp!=0) - typesdiffer=1; - else { - restyp=TYPREG; - for(k=0;ktk_att[j].ta_type].pr_regset[k]; + { + if (restyp != TYPREG && restyp != 0) + typesdiffer = 1; + else + { + restyp = TYPREG; + for (k = 0; k < SZOFSET(MAXREGS); k++) + regset[k] |= + l_props[tp->tk_att[j].ta_type].pr_regset[k]; + } } } } - } if (typesdiffer) error("%s is not a valid expression; types differ in the set", - appearance); - *typp = restyp==0 ? TYPINT : restyp; - return(res_j == -1 ? 0 : res_j); + appearance); + *typp = restyp == 0 ? TYPINT : restyp; + return (res_j == -1 ? 0 : res_j); } -expr_t memb_expr(setno,name,appearance,tokarg) char *name,*appearance; { +expr_t memb_expr(int setno, char *name, char *appearance, int tokarg) +{ expr_t result; int res_j; - res_j = membset(setno,name,result.ex_regset,appearance,0,&result.ex_typ); - result.ex_index = ex_lookup(EX_TOKFIELD,tokarg,res_j+1); - return(result); + res_j = membset(setno, name, result.ex_regset, appearance, 0, + &result.ex_typ); + result.ex_index = ex_lookup(EX_TOKFIELD, tokarg, res_j + 1); + return (result); } -expr_t tokm_expr(tokarg,name) char *name; { +expr_t tokm_expr(int tokarg, char *name) +{ char app[100]; int tokarg1 = tokarg > 0 ? tokarg : 1; - sprintf(app,"%%%d.%s",tokarg1,name); - return(memb_expr(tokpatset[tokarg1-1],name,app,tokarg)); + sprintf(app, "%%%d.%s", tokarg1, name); + return (memb_expr(tokpatset[tokarg1 - 1], name, app, tokarg)); } -expr_t perc_ident_expr(name) char *name; { +expr_t perc_ident_expr(char *name) +{ char app[100]; - sprintf(app,"%%%s",name); - return(memb_expr(cursetno,name,app,0)); + sprintf(app, "%%%s", name); + return (memb_expr(cursetno, name, app, 0)); } -expr_t all_expr(all_no,subreg) { +expr_t all_expr(int all_no, int subreg) +{ set_t localset; - register i; + register int i; register short *sp; expr_t result; sp = l_props[allreg[all_no]].pr_regset; - for (i=0;iex_operator != operator) continue; if (p->ex_lnode != lnode) continue; if (p->ex_rnode != rnode) continue; - return(p-nodes); + return (p - nodes); } - NEXT(nnodes,MAXNODES,"Node"); + NEXT(nnodes, MAXNODES, "Node"); p->ex_operator = operator; p->ex_lnode = lnode; p->ex_rnode = rnode; - return(p-nodes); + return (p - nodes); } diff --git a/util/ncgg/expr.h b/util/ncgg/expr.h index 177a71f7b..00f080d7a 100644 --- a/util/ncgg/expr.h +++ b/util/ncgg/expr.h @@ -3,6 +3,8 @@ * See the copyright notice in the ACK home directory, in the file "Copyright". */ /* $Id$ */ +#ifndef EXPR_H_ +#define EXPR_H_ typedef struct expr { int ex_typ; @@ -15,6 +17,27 @@ typedef struct expr { #define TYPADDR 3 #define TYPREG 4 +int ex_lookup(int operator, int lnode, int rnode); +void initnodes(void); +expr_t regvar_expr(expr_t e, int regtyp, int regsiz); +expr_t iextoaddr(expr_t e); +expr_t sum_expr(expr_t e1, expr_t e2); +int ne2expr(expr_t e1, expr_t e2); +int eq2expr(expr_t e1, expr_t e2); +expr_t all_expr(int all_no, int subreg); +expr_t perc_ident_expr(char *name); +expr_t tokm_expr(int tokarg, char *name); +int membset(int setno, char *name, short *regset, char *appearance, int restyp, int *typp); +void subregset(register short *sp, int subreg, register short *regset); +expr_t subreg_expr(int tokarg, int subreg); +expr_t ident_expr(char *name); +expr_t make_expr(int type, int operator, int op1, int op2); +int b_expr(expr_t e); +int i_expr(expr_t e); + + /* When the type is register the regset contains the set of possible registers for checking purposes only. */ + +#endif /* EXPR_H_ */ diff --git a/util/ncgg/extern.h b/util/ncgg/extern.h index 561591627..bcff46002 100644 --- a/util/ncgg/extern.h +++ b/util/ncgg/extern.h @@ -38,9 +38,8 @@ extern int maxtokensize; extern int nprocargs, maxprocargs; extern int use_tes; -extern char *mystrcpy(); -extern char *myalloc(); - extern void error(const char* s, ...); extern void fatal(const char* s, ...); - +extern int tabovf(char *string); +extern int strlookup(char *str); +extern void errorexit(void); diff --git a/util/ncgg/hall.c b/util/ncgg/hall.c index f670b24b8..53d695be0 100644 --- a/util/ncgg/hall.c +++ b/util/ncgg/hall.c @@ -3,18 +3,19 @@ * See the copyright notice in the ACK home directory, in the file "Copyright". */ #ifndef NORCSID -static char rcsid[]= "$Id$"; +static char rcsid[] = "$Id$"; #endif #include #include "param.h" #include "set.h" #include "extern.h" +#include "hall.h" #include /* * This file implements the marriage thesis from Hall. - * The thesis says that given a number, say N, of subsets from + * The thesis says that givoid hallverbose(void)ven a number, say N, of subsets from * a finite set, it is possible to create a set with cardinality N, * that contains one member for each of the subsets, * iff for each number, say M, of subsets from 2 to N the union of @@ -29,56 +30,66 @@ static char rcsid[]= "$Id$"; #define MAXHALL (TOKPATMAX+MAXALLREG) short hallsets[MAXHALL][SETSIZE]; -int nhallsets= -1; +int nhallsets = -1; int hallfreq[MAXHALL][2]; -hallverbose() { - register i; - register max; - - fprintf(stderr,"Table of hall frequencies\n # pre post\n"); - for (max=MAXHALL-1;hallfreq[max][0]==0 && hallfreq[max][1]==0;max--) +int recurhall(int, short [][SETSIZE]); +void unite(register short *, short *); + + +void hallverbose(void) +{ + register int i; + register int max; + + fprintf(stderr, "Table of hall frequencies\n # pre post\n"); + for (max = MAXHALL - 1; hallfreq[max][0] == 0 && hallfreq[max][1] == 0; + max--) ; - for (i=0;i<=max;i++) - fprintf(stderr,"%3d%6d%6d\n",i,hallfreq[i][0],hallfreq[i][1]); + for (i = 0; i <= max; i++) + fprintf(stderr, "%3d%6d%6d\n", i, hallfreq[i][0], hallfreq[i][1]); } -inithall() { +void inithall(void) +{ assert(nhallsets == -1); - nhallsets=0; + nhallsets = 0; } -nexthall(sp) register short *sp; { - register i; - - assert(nhallsets>=0); - for(i=0;i= 0); + for (i = 0; i < SETSIZE; i++) hallsets[nhallsets][i] = sp[i]; nhallsets++; } -card(sp) register short *sp; { - register sum,i; - - sum=0; - for(i=0;i<8*sizeof(short)*SETSIZE;i++) - if (BIT(sp,i)) +int card(register short *sp) +{ + register int sum, i; + + sum = 0; + for (i = 0; i < 8 * sizeof(short) * SETSIZE; i++) + if (BIT(sp, i)) sum++; - return(sum); + return (sum); } -checkhall() { - - assert(nhallsets>=0); +void checkhall(void) +{ + assert(nhallsets >= 0); if (!hall()) error("Hall says: \"You can't have those registers\""); } -hall() { - register i,j,k; +int hall(void) +{ + register int i, j, k; int ok; - + hallfreq[nhallsets][0]++; /* * If a set has cardinality >= nhallsets it can never be the cause @@ -86,66 +97,72 @@ hall() { * But then nhallsets is less, so this step can be re-applied. */ - do { + do + { ok = 0; - for(i=0;i=nhallsets) { - for (j=i+1;j= nhallsets) + { + for (j = i + 1; j < nhallsets; j++) + for (k = 0; k < SETSIZE; k++) + hallsets[j - 1][k] = hallsets[j][k]; nhallsets--; ok = 1; break; } } while (ok); - + /* * Now all sets have cardinality < nhallsets */ - + hallfreq[nhallsets][1]++; - ok=recurhall(nhallsets,hallsets); + ok = recurhall(nhallsets, hallsets); nhallsets = -1; - return(ok); + return (ok); } -recurhall(nhallsets,hallsets) short hallsets[][SETSIZE]; { +int recurhall(int nhallsets, short hallsets[][SETSIZE]) +{ short copysets[MAXHALL][SETSIZE]; short setsum[SETSIZE]; - register i,j,k,ncopys; - + register int i, j, k, ncopys; + /* * First check cardinality of union of all */ - for(k=0;k @@ -14,192 +14,198 @@ static char rcsid[]= "$Id$"; #include "set.h" #include "expr.h" #include "iocc.h" +#include "subr.h" #include #include "extern.h" extern int niops; extern iocc_t iops[]; -extern inproc; +extern int inproc; extern set_t l_sets[]; extern inst_t l_instances[]; -extern expr_t subreg_expr(),regno_expr(); +extern expr_t subreg_expr(), regno_expr(); -struct varinfo * setcoco(n) { +static struct varinfo * setcoco(int n) +{ struct varinfo *vi; - - NEW(vi,struct varinfo); + + NEW(vi, struct varinfo); vi->vi_next = VI_NULL; vi->vi_int[0] = INSSETCC; vi->vi_int[1] = n; - return(vi); + return (vi); } -struct varinfo * generase(n) { +static struct varinfo * generase(int n) +{ struct varinfo *vi; - NEW(vi,struct varinfo); + NEW(vi, struct varinfo); vi->vi_next = VI_NULL; vi->vi_int[0] = INSERASE; vi->vi_int[1] = n; - return(vi); + return (vi); } -struct varinfo * genremove(n) { +static struct varinfo * genremove(int n) +{ struct varinfo *vi; - NEW(vi,struct varinfo); + NEW(vi, struct varinfo); vi->vi_next = VI_NULL; vi->vi_int[0] = INSREMOVE; vi->vi_int[1] = n; - return(vi); + return (vi); } -onlyreg(argno) { - register bitno; +static int onlyreg(int argno) +{ + register int bitno; register short *sp; - - if (! argno) argno++; - sp = l_sets[tokpatset[argno-1]].set_val; - for(bitno=nregs;bitnoi_name)) + for (ip = l_instr; ip < l_instr + ninstr; ip++) + { + if (strcmp(ident, ip->i_name)) continue; - if (ip->i_nops!=niops) + if (ip->i_nops != niops) continue; - for(i=0,op=ip->i_oplist;io_next) { - if (!subset(iops[i].in_set,l_sets[op->o_setno].set_val,SETSIZE)) + for (i = 0, op = ip->i_oplist; i < niops; i++, op = op->o_next) + { + if (!subset(iops[i].in_set, l_sets[op->o_setno].set_val, SETSIZE)) goto cont; } - goto found; /* oh well, one more won't hurt */ - cont:; + goto found; + /* oh well, one more won't hurt */ + cont: ; } - error("Such an \"%s\" does not exist",ident); - return(0); -found: - NEW(vi,struct varinfo); - vi->vi_int[0] = ip-l_instr; + error("Such an \"%s\" does not exist", ident); + return (0); + found: + NEW(vi, struct varinfo); + vi->vi_int[0] = ip - l_instr; vi->vi_int[1] = star; - vi->vi_next=0; + vi->vi_next = 0; retval = vi; - for(i=0;ivi_vi,struct varinfo); - vi=vi->vi_vi; + for (i = 0; i < niops; i++) + { + NEW(vi->vi_vi, struct varinfo); + vi = vi->vi_vi; vi->vi_int[0] = iops[i].in_index; } vi->vi_vi = 0; vi = retval; - for(i=0,op=ip->i_oplist;io_next) { - if(op->o_adorn&AD_CC) { - vi->vi_next = setcoco(iops[i].in_index); - vi=vi->vi_next; - } - switch(op->o_adorn&AD_RWMASK) { - default: - /* Nothing possible to do */ - break; - case AD_RO: - /* It might be possible to do something - * here but not now. - */ - break; - case AD_RW: - case AD_WO: - /* Treated the same for now */ - insta = &l_instances[iops[i].in_index]; - switch(insta->in_which) { - case IN_COPY: - if(insta->in_info[1]==0 && !onlyreg(insta->in_info[0])) - break; - makescratch(insta->in_info[0]); - vi->vi_next = generase( - ex_lookup( - EX_SUBREG,insta->in_info[0], - insta->in_info[1] - ) - ); + for (i = 0, op = ip->i_oplist; i < niops; i++, op = op->o_next) + { + if (op->o_adorn & AD_CC) + { + vi->vi_next = setcoco(iops[i].in_index); vi = vi->vi_next; - break; - case IN_MEMB: - vi->vi_next = generase( - ex_lookup( - EX_TOKFIELD,insta->in_info[0], - insta->in_info[1] - ) - ); - vi=vi->vi_next; - break; - case IN_RIDENT: - vi->vi_next = generase( - ex_lookup( - EX_REG,insta->in_info[0],0 - ) - ); - vi = vi->vi_next; - break; - case IN_ALLOC: - vi->vi_next = generase( - ex_lookup( - EX_ALLREG,insta->in_info[0]+1, - insta->in_info[1] - ) - ); - vi = vi->vi_next; - break; - case IN_S_DESCR: - case IN_D_DESCR: - { int temp; - - if (insta->in_which == IN_S_DESCR) - temp = wordsize; - else - temp = 2 * wordsize; - temp=ex_lookup(EX_REGVAR,insta->in_info[1],temp); - vi->vi_next = generase(temp); - vi = vi->vi_next; - vi->vi_next = genremove(temp); - vi = vi->vi_next; - break; - } } - break; - } + switch (op->o_adorn & AD_RWMASK) + { + default: + /* Nothing possible to do */ + break; + case AD_RO: + /* It might be possible to do something + * here but not now. + */ + break; + case AD_RW: + case AD_WO: + /* Treated the same for now */ + insta = &l_instances[iops[i].in_index]; + switch (insta->in_which) + { + case IN_COPY: + if (insta->in_info[1] == 0 + && !onlyreg(insta->in_info[0])) + break; + makescratch(insta->in_info[0]); + vi->vi_next = generase(ex_lookup( + EX_SUBREG, insta->in_info[0], insta->in_info[1])); + vi = vi->vi_next; + break; + case IN_MEMB: + vi->vi_next = generase(ex_lookup( + EX_TOKFIELD, insta->in_info[0], insta->in_info[1])); + vi = vi->vi_next; + break; + case IN_RIDENT: + vi->vi_next = generase(ex_lookup( + EX_REG, insta->in_info[0], 0)); + vi = vi->vi_next; + break; + case IN_ALLOC: + vi->vi_next = generase(ex_lookup( + EX_ALLREG, insta->in_info[0] + 1, insta->in_info[1])); + vi = vi->vi_next; + break; + case IN_S_DESCR: + case IN_D_DESCR: + { + int temp; + + if (insta->in_which == IN_S_DESCR) + temp = wordsize; + else + temp = 2 * wordsize; + temp = ex_lookup(EX_REGVAR, insta->in_info[1], temp); + vi->vi_next = generase(temp); + vi = vi->vi_next; + vi->vi_next = genremove(temp); + vi = vi->vi_next; + break; + } + } + break; + } } - for (eravi=ip->i_erases;eravi != VI_NULL;eravi=eravi->vi_next) { + for (eravi = ip->i_erases; eravi != VI_NULL ; eravi = eravi->vi_next) + { if (eravi->vi_int[0] < 0) vi->vi_next = setcoco(0); - else { + else + { vi->vi_next = generase(eravi->vi_int[0]); - vi=vi->vi_next; + vi = vi->vi_next; vi->vi_next = genremove(eravi->vi_int[0]); } - vi=vi->vi_next; + vi = vi->vi_next; } - return(retval); + return (retval); } diff --git a/util/ncgg/instruct.h b/util/ncgg/instruct.h index 68828f99b..5dea2d28c 100644 --- a/util/ncgg/instruct.h +++ b/util/ncgg/instruct.h @@ -3,6 +3,8 @@ * See the copyright notice in the ACK home directory, in the file "Copyright". */ /* $Id$ */ +#ifndef INSTRUCT_H_ +#define INSTRUCT_H_ #ifndef _COST_ #include "cost.h" @@ -39,3 +41,8 @@ extern instr_t l_instr[]; * if it contains the same information and is allowed as an operand * in this place. Too difficult for now. */ +struct varinfo; + +struct varinfo *gen_inst(char *ident, int star); + +#endif /* INSTRUCT_H_ */ diff --git a/util/ncgg/iocc.c b/util/ncgg/iocc.c index 9519b1cd6..96ad56f6f 100644 --- a/util/ncgg/iocc.c +++ b/util/ncgg/iocc.c @@ -3,7 +3,7 @@ * See the copyright notice in the ACK home directory, in the file "Copyright". */ #ifndef NORCSID -static char rcsid[]= "$Id$"; +static char rcsid[] = "$Id$"; #endif #include @@ -14,6 +14,7 @@ static char rcsid[]= "$Id$"; #include "set.h" #include "expr.h" #include "lookup.h" +#include "subr.h" #include "token.h" #include "property.h" #include "iocc.h" @@ -28,184 +29,204 @@ expr_t arexp[MAXATT]; expr_t iextoaddr(); -iocc_t subr_iocc(tokarg,subreg) { +/* Forward declarations */ +static int instalookup(inst_t, int); + +iocc_t subr_iocc(int tokarg, int subreg) +{ inst_t insta; - iocc_t result; - register i; + iocc_t result; + register int i; insta.in_which = IN_COPY; insta.in_info[0] = tokarg; insta.in_info[1] = subreg; - result.in_index = instalookup(insta,2); - if (tokarg < 1) tokarg = 1; - if (subreg==0) - for (i=0;isy_value.syv_regno; - result.in_index = instalookup(insta,1); - BIS(result.in_set,sy_p->sy_value.syv_regno); - return(result); + result.in_index = instalookup(insta, 1); + BIS(result.in_set, sy_p->sy_value.syv_regno); + return (result); } -iocc_t all_iocc(all_no,subreg) { +iocc_t all_iocc(int all_no, int subreg) +{ iocc_t result; inst_t insta; - register i; + register int i; set_t localset; register short *sp; sp = l_props[allreg[all_no]].pr_regset; - for (i=0;isy_value.syv_tokno]; - BIS(result.in_set,sy_p->sy_value.syv_tokno+nregs); + BIS(result.in_set, sy_p->sy_value.syv_tokno + nregs); insta.in_which = IN_DESCR; - if (rvused&SL_REGVAR && strcmp(ident,"LOCAL")==0) + if (rvused & SL_REGVAR && strcmp(ident, "LOCAL") == 0) insta.in_which = IN_S_DESCR; - else if (rvused&DL_REGVAR && strcmp(ident,"DLOCAL")==0) + else if (rvused & DL_REGVAR && strcmp(ident, "DLOCAL") == 0) insta.in_which = IN_D_DESCR; insta.in_info[0] = sy_p->sy_value.syv_tokno; - for (i=0;itk_att[i].ta_type == -3) { - if (narexpr>i) - error("token %s initialized with too many attributes",ident); + for (i = 0; i < MAXATT; i++) + { + if (tp->tk_att[i].ta_type == -3) + { + if (narexpr > i) + error("token %s initialized with too many attributes", ident); break; } - if (i>= narexpr) { - error("token %s initialized with too few attributes", - ident); + if (i >= narexpr) + { + error("token %s initialized with too few attributes", ident); break; } typerr = 0; - switch(arexp[i].ex_typ) { - default: assert(0); - case TYPINT: - if (tp->tk_att[i].ta_type != -1) - if (tp->tk_att[i].ta_type == -2) - arexp[i] = iextoaddr(arexp[i]); - else + switch (arexp[i].ex_typ) + { + default: + assert(0); + case TYPINT: + if (tp->tk_att[i].ta_type != -1) + if (tp->tk_att[i].ta_type == -2) + arexp[i] = iextoaddr(arexp[i]); + else + typerr++; + break; + case TYPBOOL: + typerr++; + break; + case TYPADDR: + if (tp->tk_att[i].ta_type != -2) typerr++; - break; - case TYPBOOL: - typerr++; break; - case TYPADDR: - if (tp->tk_att[i].ta_type != -2) - typerr++; - break; - case TYPREG: - if (tp->tk_att[i].ta_type<0) - typerr++; - else if (!subset(arexp[i].ex_regset, - l_props[tp->tk_att[i].ta_type].pr_regset, - SZOFSET(MAXREGS))) - typerr++; - break; + break; + case TYPREG: + if (tp->tk_att[i].ta_type < 0) + typerr++; + else if (!subset(arexp[i].ex_regset, + l_props[tp->tk_att[i].ta_type].pr_regset, + SZOFSET(MAXREGS))) + typerr++; + break; } if (typerr) - error("Attribute %s.%s given wrong type of value", - ident,tp->tk_att[i].ta_name); - insta.in_info[i+1] = arexp[i].ex_index; + error("Attribute %s.%s given wrong type of value", ident, + tp->tk_att[i].ta_name); + insta.in_info[i + 1] = arexp[i].ex_index; } - result.in_index = instalookup(insta,i+1); - return(result); + result.in_index = instalookup(insta, i + 1); + return (result); } /* low level instance package */ -int ninstances=1; +int ninstances = 1; inst_t l_instances[MAXINSTANCES]; -instalookup(insta,filled) inst_t insta; { - register i,j; +static int instalookup(inst_t insta, int filled) +{ + register int i, j; - for (j=filled;j<=MAXATT;j++) + for (j = filled; j <= MAXATT; j++) insta.in_info[j] = 0; - for (i=0;i @@ -11,61 +11,66 @@ static char rcsid[]= "$Id$"; #include "param.h" #include "lookup.h" #include "extern.h" +#include "subr.h" -char *myalloc(); -char *mystrcpy(); +; -symbol dumsym; /* dummy to return in case of error */ +symbol dumsym; /* dummy to return in case of error */ -symbol *lookup(name,type,style) -char *name; -symtype type; -lookupstyle style; +/* Forward declarations */ +static int hashvalue(register char *); + +symbol *lookup(char *name, symtype type, lookupstyle style) { - symbol *sy_p,**sy_pp; + symbol *sy_p, **sy_pp; - for (sy_pp = &symhash[hashvalue(name)];(sy_p= *sy_pp) != 0;sy_pp= &sy_p->sy_next) { - if (strcmp(sy_p->sy_name,name)!=0) + for (sy_pp = &symhash[hashvalue(name)]; (sy_p = *sy_pp) != 0; + sy_pp = &sy_p->sy_next) + { + if (strcmp(sy_p->sy_name, name) != 0) continue; - switch(style) { + switch (style) + { + default: + assert(0); + case justlooking: + case mustexist: + case makeexist: + if (type == symany || type == sy_p->sy_type) + return (sy_p); + continue; + case newsymbol: + error("%s already defined", name); + return (&dumsym); + } + } + switch (style) + { default: assert(0); case justlooking: + return ((symbol *) 0); case mustexist: - case makeexist: - if (type==symany || type==sy_p->sy_type) - return(sy_p); - continue; + fatal("%s is unknown symbol", name); + /* NOTREACHED */ case newsymbol: - error("%s already defined",name); - return(&dumsym); - } - } - switch(style) { - default: - assert(0); - case justlooking: - return((symbol *) 0); - case mustexist: - fatal("%s is unknown symbol",name); - /* NOTREACHED */ - case newsymbol: - case makeexist: - NEW(sy_p,symbol); - sy_p->sy_next = 0; - sy_p->sy_name = mystrcpy(name); - assert(type!=symany); - sy_p->sy_type = type; - *sy_pp = sy_p; - return(sy_p); + case makeexist: + NEW(sy_p, symbol); + sy_p->sy_next = 0; + sy_p->sy_name = mystrcpy(name); + assert(type != symany); + sy_p->sy_type = type; + *sy_pp = sy_p; + return (sy_p); } } -hashvalue(s) register char *s; { - register unsigned sum=0; - register i; +static int hashvalue(register char *s) +{ + register unsigned sum = 0; + register int i; - for(i=0;*s;s++,i=(i+3)&07) - sum += *s< #include +#include "param.h" +#include "hall.h" +#include "expr.h" #include "extern.h" char *filename; -main(argc,argv) char **argv; { +extern void enterkeyw(void); +extern void initio(void); +extern void initemhash(void); +extern void finishio(void); +extern void statistics(void); +extern int yyparse(void); + +int main(int argc, char **argv) +{ extern int nerrors; extern int code_in_c; extern int tabledebug; extern int verbose; - while (argc >1 && argv[1][0]=='-') { - switch(argv[1][1]) { - case 'c': - code_in_c = 0; - break; - case 'd': - tabledebug++; - break; - case 'v': - verbose++; - break; - default: - error("Unknown flag -%c",argv[1][1]); + while (argc > 1 && argv[1][0] == '-') + { + switch (argv[1][1]) + { + case 'c': + code_in_c = 0; + break; + case 'd': + tabledebug++; + break; + case 'v': + verbose++; + break; + default: + error("Unknown flag -%c", argv[1][1]); } - argc--; argv++; + argc--; + argv++; } - if (argc==2) { - if (freopen(argv[1],"r",stdin)==NULL) { - error("Can't open %s",argv[1]); + if (argc == 2) + { + if (freopen(argv[1], "r", stdin) == NULL) + { + error("Can't open %s", argv[1]); exit(-1); } filename = argv[1]; } - else if (argc == 1) { + else if (argc == 1) + { filename = ""; - } else - error("Usage: %s [-c] [-d] [-v] [table]",argv[0]); + } + else + error("Usage: %s [-c] [-d] [-v] [table]", argv[0]); initemhash(); enterkeyw(); initnodes(); initio(); yyparse(); - if (nerrors==0) { + if (nerrors == 0) + { finishio(); statistics(); if (verbose) hallverbose(); - } else { + } + else + { errorexit(); } - exit(nerrors==0 ? 0 : -1); + exit(nerrors == 0 ? 0 : -1); } diff --git a/util/ncgg/output.c b/util/ncgg/output.c index 40733bab0..84ce8dd7a 100644 --- a/util/ncgg/output.c +++ b/util/ncgg/output.c @@ -2,34 +2,34 @@ * (c) copyright 1987 by the Vrije Universiteit, Amsterdam, The Netherlands. * See the copyright notice in the ACK home directory, in the file "Copyright". */ -/* #define CODEDEBUG /* print readable code */ +/* #define CODEDEBUG *//* print readable code */ #ifdef CODEDEBUG -int code_in_c=0; /* put readable code in "code" */ -int tabledebug=1; /* generate code for table debugging */ +int code_in_c=0; /* put readable code in "code" */ +int tabledebug=1; /* generate code for table debugging */ #else -int code_in_c=1; /* put code in "tables.c" */ -int tabledebug=0; /* do not generate code for table debugging */ +int code_in_c = 1; /* put code in "tables.c" */ +int tabledebug = 0; /* do not generate code for table debugging */ #endif -int verbose=0; /* print all statistics */ -int use_tes; /* use top element size information */ -char *c_file= "tables.c"; -char *h_file= "tables.H"; -char *cd_file= "code"; +int verbose = 0; /* print all statistics */ +int use_tes; /* use top element size information */ +char *c_file = "tables.c"; +char *h_file = "tables.H"; +char *cd_file = "code"; #ifndef NORCSID -static char rcsid[]= "$Id$"; +static char rcsid[] = "$Id$"; #endif #include #include #include -#include #include "varinfo.h" #include "param.h" #include "reg.h" #include "property.h" #include "token.h" #include "set.h" +#include "subr.h" #include "instruct.h" #include "lookup.h" #include @@ -40,7 +40,7 @@ static char rcsid[]= "$Id$"; #define BMASK 0xFF #define BSHIFT 8 -FILE *ctable,*htable; +FILE *ctable, *htable; FILE *code; short *lineset; int maxline; @@ -59,66 +59,94 @@ extern int nstacks; extern c1_t l_stacks[]; extern int ncoercs; extern c3_t l_coercs[]; -extern int nsplit,maxsplit; +extern int nsplit, maxsplit; extern c2_t l_split[]; extern set_t l_sets[]; -int maxallreg=0; -int maxregvars=0; +int maxallreg = 0; +int maxregvars = 0; int setsize; -opnfile(f,s) FILE **f; char *s; { +int prevind = 0; +int npatbytes = -1; +char pattern[MAXPATBYTES]; +int pathash[256]; - if ((*f=fopen(s,"w"))==NULL) - fatal("Can't create %s",s); +extern int npatterns; +extern int patindex[]; +extern int empatlen; +extern int emmnem[]; +extern int empatexpr; + +extern node_t nodes[]; +extern int nnodes; + +extern char * filename; + +/* Forward declarations */ +static void patbyte(int); +static void patshort(int); +static void pat(int); + +void opnfile(FILE **f, char *s) +{ + + if ((*f = fopen(s, "w")) == NULL) + fatal("Can't create %s", s); } -unlfile(f,s) FILE *f; char *s; { - - if (f) fclose(f); - if (unlink(s)<0) - error("%s incorrect, must be removed!!",s); +void unlfile(FILE *f, char *s) +{ + if (f) + fclose(f); + if (remove(s) < 0) + error("%s incorrect, must be removed!!", s); } -initio() { - extern char *myalloc(); +void initio(void) +{ - opnfile(&ctable,c_file); - opnfile(&htable,h_file); + opnfile(&ctable, c_file); + opnfile(&htable, h_file); if (code_in_c) - fprintf(ctable,"char coderules[] = {"); + fprintf(ctable, "char coderules[] = {"); else - opnfile(&code,cd_file); + opnfile(&code, cd_file); patbyte(0); if (tabledebug) - lineset = (short *) myalloc(SZOFSET(MAXSOURCELINES)*sizeof(short)); + lineset = (short *) myalloc(SZOFSET(MAXSOURCELINES) * sizeof(short)); } -finishcode() { +void finishcode(void) +{ if (code_in_c) - fprintf(ctable,"\n};\n\n"); + fprintf(ctable, "\n};\n\n"); fprintf(ctable, "int allsetno = %d;\n", allsetno); - if (tabledebug) { - int fd; + if (tabledebug) + { + FILE *fd; int sz; - - if ((fd=creat("lineset",0666))>=0) { - sz = SZOFSET(maxline)*2; - write(fd,&sz,sizeof(int)); - write(fd,lineset,sz); - close(fd); - } else + + fd = fopen("lineset","wb"); + if (fd != NULL) + { + sz = SZOFSET(maxline) * 2; + fwrite(&sz,1,sizeof(int),fd); + fwrite(lineset,1,sz,fd); + fclose(fd); + } + else error("Can't create lineset"); } } -errorexit() { - - unlfile(ctable,c_file); - unlfile(htable,h_file); +void errorexit(void) +{ + unlfile(ctable, c_file); + unlfile(htable, h_file); if (!code_in_c) - unlfile(code,cd_file); + unlfile(code, cd_file); } #ifdef CODEDEBUG @@ -131,190 +159,208 @@ errorexit() { #define codenl() #define code8nl(x) code8(x) -code8(x) { +void code8(int x) +{ codeindex++; if (code_in_c) - fprintf(ctable,"%d,",x&0377); + fprintf(ctable, "%d,", x & 0377); else - putc(x,code); + putc(x, code); } -code53(x,y) { - - code8(x+(y<<5)); +void code53(int x, int y) +{ + code8(x + (y << 5)); } -codeint(x) { - - assert(x>=0 && x<=32767); - if (x<128) { +void codeint(int x) +{ + assert(x >= 0 && x <= 32767); + if (x < 128) + { code8(x); - } else { - code8(x/256+128); - code8(x%256); + } + else + { + code8(x / 256 + 128); + code8(x % 256); } } #endif -int prevind=0; -int npatbytes= -1; -char pattern[MAXPATBYTES]; -int pathash[256]; -outpatterns() { - extern int npatterns; - extern int patindex[]; - extern int empatlen; - extern int emmnem[]; - extern int empatexpr; - register i; +void outpatterns(void) +{ + register int i; - if (!inproc) { + if (!inproc) + { patbyte(0); patshort(prevind); - prevind = npatbytes-2; + prevind = npatbytes - 2; patbyte(empatlen); - for(i=0;i=0); - if (n<128) + assert(n >= 0); + if (n < 128) patbyte(n); - else { - patbyte(n/256+128); - patbyte(n%256); + else + { + patbyte(n / 256 + 128); + patbyte(n % 256); } } -patshort(n) { +static void patshort(int n) +{ - patbyte(n%256); - patbyte(n/256); + patbyte(n % 256); + patbyte(n / 256); } -patbyte(n) { +static void patbyte(int n) +{ NEXT(npatbytes, MAXPATBYTES, "Pattern bytes"); - pattern[npatbytes]=n; + pattern[npatbytes] = n; } -hashpatterns() { +void hashpatterns(void) +{ short index; - register char *bp,*tp; + register char *bp, *tp; register short i; unsigned short hashvalue; int patlen; index = prevind; - while (index != 0) { + while (index != 0) + { bp = &pattern[index]; tp = &bp[PO_MATCH]; - i = *tp++&BMASK; - if (i==BMASK) { - i = *tp++&BMASK; - i |= (*tp++&BMASK)<>BSHIFT; + i = index; + index = (bp[PO_NEXT] & BMASK) | (bp[PO_NEXT + 1] << BSHIFT); + bp[PO_HASH] = hashvalue >> BSHIFT; hashvalue &= BMASK; - bp[PO_NEXT] = pathash[hashvalue]&BMASK; - bp[PO_NEXT+1] = pathash[hashvalue]>>BSHIFT; + bp[PO_NEXT] = pathash[hashvalue] & BMASK; + bp[PO_NEXT + 1] = pathash[hashvalue] >> BSHIFT; pathash[hashvalue] = i; } } -outincludes() { +void outincludes(void) +{ - fprintf(ctable,"#include \"param.h\"\n"); - fprintf(ctable,"#include \"tables.h\"\n"); - fprintf(ctable,"#include \"types.h\"\n"); - fprintf(ctable,"#include \n"); - fprintf(ctable,"#include \"data.h\"\n"); + fprintf(ctable, "#include \"param.h\"\n"); + fprintf(ctable, "#include \"tables.h\"\n"); + fprintf(ctable, "#include \"types.h\"\n"); + fprintf(ctable, "#include \n"); + fprintf(ctable, "#include \"data.h\"\n"); } -outregs() { - register i,j,k; +void outregs(void) +{ + register int i, j, k; short rset[SZOFSET(MAXREGS)]; - short clashlist[MAXREGS*MAXREGS]; + short clashlist[MAXREGS * MAXREGS]; int iclashlist = 0; - int t,ready; + int t, ready; - - fprintf(ctable,"char stregclass[] = {\n"); - for (i=0;i=0 && l_regs[i].ri_memb[0]==0); - fprintf(ctable,"},\n"); + fprintf(ctable, ",%d", + l_regs[i].ri_rregvar >= 0 && l_regs[i].ri_memb[0] == 0); + fprintf(ctable, "},\n"); } - fprintf(ctable,"};\n\n short clashlist[] = {\n\t"); - for (i = 0; i < iclashlist; i++) { + fprintf(ctable, "};\n\n short clashlist[] = {\n\t"); + for (i = 0; i < iclashlist; i++) + { fprintf(ctable, "%d, ", clashlist[i]); - if (clashlist[i] == 0) fprintf(ctable, "\n\t"); + if (clashlist[i] == 0) + fprintf(ctable, "\n\t"); } fprintf(ctable, "0};\n\n"); } -outregvars() { - register i,j; +void outregvars(void) +{ + register int i, j; - fprintf(htable,"#define REGVARS\n"); - if (reglap!=0) - fprintf(htable,"#define REGLAP\n"); - fprintf(ctable,"#include \"regvar.h\"\n"); - fprintf(ctable,"int nregvar[4] = { "); - for (i=0;i<4;i++) { - fprintf(ctable,"%d, ",nregvar[i]); - if (nregvar[i]>maxregvars) + fprintf(htable, "#define REGVARS\n"); + if (reglap != 0) + fprintf(htable, "#define REGLAP\n"); + fprintf(ctable, "#include \"regvar.h\"\n"); + fprintf(ctable, "int nregvar[4] = { "); + for (i = 0; i < 4; i++) + { + fprintf(ctable, "%d, ", nregvar[i]); + if (nregvar[i] > maxregvars) maxregvars = nregvar[i]; } - fprintf(ctable,"};\n"); - for (i=0;i<4;i++) - if (nregvar[i]>0) - fprintf(ctable,"struct regassigned ratar%d[%d];\n", - i,nregvar[i]); - for (i=0;i<4;i++) if (nregvar[i]>0) { - fprintf(ctable,"int rvtar%d[] = {",i); - for (j=0;j0) - fprintf(ctable,"\trvtar%d,\n",i); + fprintf(ctable, "};\n"); + for (i = 0; i < 4; i++) + if (nregvar[i] > 0) + fprintf(ctable, "struct regassigned ratar%d[%d];\n", i, nregvar[i]); + for (i = 0; i < 4; i++) + if (nregvar[i] > 0) + { + fprintf(ctable, "int rvtar%d[] = {", i); + for (j = 0; j < nregvar[i]; j++) + fprintf(ctable, "%d,", rvnumbers[i][j]); + fprintf(ctable, "};\n"); + } + fprintf(ctable, "\nint *rvnumbers[] = {\n"); + for (i = 0; i < 4; i++) + if (nregvar[i] > 0) + fprintf(ctable, "\trvtar%d,\n", i); else - fprintf(ctable,"\t0,\n"); - fprintf(ctable,"};\n\nstruct regassigned *regassigned[] = {\n"); - for (i=0;i<4;i++) - if (nregvar[i]>0) - fprintf(ctable,"\tratar%d,\n",i); + fprintf(ctable, "\t0,\n"); + fprintf(ctable, "};\n\nstruct regassigned *regassigned[] = {\n"); + for (i = 0; i < 4; i++) + if (nregvar[i] > 0) + fprintf(ctable, "\tratar%d,\n", i); else - fprintf(ctable,"\t0,\n"); - fprintf(ctable,"};\n"); + fprintf(ctable, "\t0,\n"); + fprintf(ctable, "};\n"); } -typeconv(n) { +int typeconv(int n) +{ - if (n>=0) return(2); - if (n== -1) return(1); - if (n== -2) return(3); - assert (n== -3); - return(0); + if (n >= 0) + return (2); + if (n == -1) + return (1); + if (n == -2) + return (3); + assert(n == -3); + return (0); } -outfmt(p) -register char *p; +static int is_ascii(int c) +{ + if ((c >= 0) && (c <= 127)) + return 1; + return 0; +} + +void outfmt(register char *p) { register int c; - fprintf(ctable,"\""); - while ((c= (*p++&0377))!=0) { - if (! isascii(c) || iscntrl(c)) { - fprintf(ctable,"\\%c%c%c", - ((c&~0300)>>6) + '0', ((c&070)>>3)+'0', - (c&07)+'0'); + fprintf(ctable, "\""); + while ((c = (*p++ & 0377)) != 0) + { + if (!is_ascii(c) || iscntrl(c)) + { + fprintf(ctable, "\\%c%c%c", ((c & ~0300) >> 6) + '0', + ((c & 070) >> 3) + '0', (c & 07) + '0'); } - else fprintf(ctable, "%c",c); + else + fprintf(ctable, "%c", c); } - fprintf(ctable,"\""); + fprintf(ctable, "\""); } -outtokens() { - register tokno,i; +void outtokens(void) +{ + register int tokno, i; register token_p tp; - fprintf(ctable,"tkdef_t tokens[] = {{0},\n"); - for (tokno=1;toknotk_size, tp->tk_cost.ct_space, tp->tk_cost.ct_time); - for(i=0;itk_att[i].ta_type)); - fprintf(ctable,"},%d},\t/* ",tp->tk_format); - if (tp->tk_format >= 0) outfmt(l_strings[tp->tk_format]); - else fprintf(ctable, "(no format)"); - fprintf(ctable," */\n"); + fprintf(ctable, "/* %3d */{%d,{%d,%d},{", tokno, tp->tk_size, + tp->tk_cost.ct_space, tp->tk_cost.ct_time); + for (i = 0; i < maxtokensize; i++) + fprintf(ctable, "%d,", typeconv(tp->tk_att[i].ta_type)); + fprintf(ctable, "},%d},\t/* ", tp->tk_format); + if (tp->tk_format >= 0) + outfmt(l_strings[tp->tk_format]); + else + fprintf(ctable, "(no format)"); + fprintf(ctable, " */\n"); } - fprintf(ctable,"{0}};\n\n"); + fprintf(ctable, "{0}};\n\n"); } -outenodes() { +void outenodes(void) +{ register node_p np; - extern node_t nodes[]; - extern int nnodes; - fprintf(ctable,"node_t enodes[] = {\n"); - for (np=nodes;np<&nodes[nnodes];np++) - fprintf(ctable,"{%d,%d,%d},\n", - np->ex_operator,np->ex_lnode,np->ex_rnode); - fprintf(ctable,"};\n\n"); + fprintf(ctable, "node_t enodes[] = {\n"); + for (np = nodes; np < &nodes[nnodes]; np++) + fprintf(ctable, "{%d,%d,%d},\n", np->ex_operator, np->ex_lnode, + np->ex_rnode); + fprintf(ctable, "};\n\n"); } -outstrings() { - register i; +void outstrings(void) +{ + register int i; +#if 0 register char *p; register int c; - extern char * filename; +#endif if (tabledebug) - fprintf(ctable,"char *tablename = \"%s\";\n",filename); - fprintf(ctable,"string codestrings[] = {\n"); - for(i=0;i>6) + '0', ((c&070)>>3)+'0', - (c&07)+'0'); + ((c&~0300)>>6) + '0', ((c&070)>>3)+'0', + (c&07)+'0'); } else fprintf(ctable, "%c",c); } fprintf(ctable,"\",\n"); #endif - fprintf(ctable,",\n"); + fprintf(ctable, ",\n"); } - fprintf(ctable,"};\n\n"); + fprintf(ctable, "};\n\n"); } extern set_t unstackset; -outsets() { - register i; +void outsets(void) +{ + register int i; register set_p sp; - fprintf(ctable,"set_t machsets[] = {\n"); - for (sp=l_sets;sp< &l_sets[nsets]; sp++) { - fprintf(ctable,"/* %3ld */ {%3d,{",(long)(sp-l_sets),sp->set_size); - for (i=0;iset_val[i]&0xFFFF); - fprintf(ctable,"}},\n"); + fprintf(ctable, "set_t machsets[] = {\n"); + for (sp = l_sets; sp < &l_sets[nsets]; sp++) + { + fprintf(ctable, "/* %3ld */ {%3d,{", (long) (sp - l_sets), + sp->set_size); + for (i = 0; i < setsize; i++) + fprintf(ctable, "0x%x,", sp->set_val[i] & 0xFFFF); + fprintf(ctable, "}},\n"); } - fprintf(ctable,"};\n\n"); + fprintf(ctable, "};\n\n"); fprintf(ctable, "set_t unstackset = { %3d,{\n", unstackset.set_size); - for (i = 0; iin_which); - for(i=0;i<=maxtokensize;i++) - fprintf(ctable,"%d,",ip->in_info[i]); - fprintf(ctable,"}},\n"); + fprintf(ctable, "inst_t tokeninstances[] = {\n"); + for (ip = l_instances; ip < &l_instances[ninstances]; ip++) + { + fprintf(ctable, "{ %d, {", ip->in_which); + for (i = 0; i <= maxtokensize; i++) + fprintf(ctable, "%d,", ip->in_info[i]); + fprintf(ctable, "}},\n"); } - fprintf(ctable,"};\n\n"); + fprintf(ctable, "};\n\n"); } -outmoves() { +void outmoves(void) +{ register move_p mp; - fprintf(ctable,"move_t moves[] = {\n"); - for (mp=l_moves; mp< &l_moves[nmoves]; mp++) - fprintf(ctable,"{%d,%d,%d,%d,%d},\n", - mp->m_set1, mp->m_expr1, - mp->m_set2, mp->m_expr2, - mp->m_cindex); - fprintf(ctable,"{-1}\n};\n\n"); + fprintf(ctable, "move_t moves[] = {\n"); + for (mp = l_moves; mp < &l_moves[nmoves]; mp++) + fprintf(ctable, "{%d,%d,%d,%d,%d},\n", mp->m_set1, mp->m_expr1, + mp->m_set2, mp->m_expr2, mp->m_cindex); + fprintf(ctable, "{-1}\n};\n\n"); } -outtests() { +void outtests(void) +{ register test_p tp; - fprintf(ctable,"test_t tests[] = {\n"); - for (tp=l_tests; tp< &l_tests[ntests]; tp++) - fprintf(ctable,"{%d,%d,%d},\n", - tp->t_set, tp->t_expr, - tp->t_cindex); - fprintf(ctable,"{-1}\n};\n\n"); + fprintf(ctable, "test_t tests[] = {\n"); + for (tp = l_tests; tp < &l_tests[ntests]; tp++) + fprintf(ctable, "{%d,%d,%d},\n", tp->t_set, tp->t_expr, tp->t_cindex); + fprintf(ctable, "{-1}\n};\n\n"); } -outstacks() { +void outstacks(void) +{ register c1_p cp; - fprintf(ctable,"c1_t c1coercs[] = {\n"); - for (cp=l_stacks; cp< &l_stacks[nstacks]; cp++) - fprintf(ctable,"{%d,%d,%d,%d},\n", - cp->c1_texpno, cp->c1_expr, - cp->c1_prop, cp->c1_codep); - fprintf(ctable,"{-1}\n};\n\n"); + fprintf(ctable, "c1_t c1coercs[] = {\n"); + for (cp = l_stacks; cp < &l_stacks[nstacks]; cp++) + fprintf(ctable, "{%d,%d,%d,%d},\n", cp->c1_texpno, cp->c1_expr, + cp->c1_prop, cp->c1_codep); + fprintf(ctable, "{-1}\n};\n\n"); } -outsplits() { +void outsplits(void) +{ register c2_p cp; - register i; + register int i; - fprintf(ctable,"c2_t c2coercs[] = {\n"); - for (cp=l_split; cp< &l_split[nsplit]; cp++) { - fprintf(ctable,"{%d,%d,%d,{", - cp->c2_texpno, cp->c2_expr, cp->c2_nsplit); - for (i=0;ic2_repl[i]); - fprintf(ctable,"},%d},\n",cp->c2_codep); + fprintf(ctable, "c2_t c2coercs[] = {\n"); + for (cp = l_split; cp < &l_split[nsplit]; cp++) + { + fprintf(ctable, "{%d,%d,%d,{", cp->c2_texpno, cp->c2_expr, + cp->c2_nsplit); + for (i = 0; i < maxsplit; i++) + fprintf(ctable, "%d,", cp->c2_repl[i]); + fprintf(ctable, "},%d},\n", cp->c2_codep); } - fprintf(ctable,"{-1}\n};\n\n"); + fprintf(ctable, "{-1}\n};\n\n"); } -outcoercs() { +void outcoercs(void) +{ register c3_p cp; - fprintf(ctable,"c3_t c3coercs[] = {\n"); - for (cp=l_coercs; cp< &l_coercs[ncoercs]; cp++) - fprintf(ctable,"{%d,%d,%d,%d,%d},\n", - cp->c3_texpno, cp->c3_expr, - cp->c3_prop, cp->c3_repl, cp->c3_codep); - fprintf(ctable,"{-1}\n};\n\n"); + fprintf(ctable, "c3_t c3coercs[] = {\n"); + for (cp = l_coercs; cp < &l_coercs[ncoercs]; cp++) + fprintf(ctable, "{%d,%d,%d,%d,%d},\n", cp->c3_texpno, cp->c3_expr, + cp->c3_prop, cp->c3_repl, cp->c3_codep); + fprintf(ctable, "{-1}\n};\n\n"); } -outproplists() { - register propno; - register regno; +void outproplists(void) +{ + register int propno; + register int regno; - for(propno=0;propnosy_value.syv_stringno]; - fprintf(htable,"#define WRD_FMT \"%s\"\n",wrdfmt); + fprintf(htable, "#define WRD_FMT \"%s\"\n", wrdfmt); } - cdef("MAXALLREG",maxallreg); - cdef("SETSIZE",setsize); - cdef("NREGS",nregs); - cdef("REGSETSIZE",SZOFSET(nregs)); - cdef("TOKENSIZE",maxtokensize); - cdef("MAXMEMBERS",maxmembers); - cdef("LONGESTPATTERN",maxempatlen); - cdef("MAXPATLEN",maxtokpatlen); - cdef("MAXREPLLEN",maxtokrepllen); - cdef("MAXEMREPLLEN",maxemrepllen); - cdef("MAXPROCARG",maxprocargs); - cdef("MAXRULE",maxrule<16 ? 16 : maxrule); - if (nsplit>0) { - cdef("MAXSPLIT",maxsplit); + cdef("MAXALLREG", maxallreg); + cdef("SETSIZE", setsize); + cdef("NREGS", nregs); + cdef("REGSETSIZE", SZOFSET(nregs)); + cdef("TOKENSIZE", maxtokensize); + cdef("MAXMEMBERS", maxmembers); + cdef("LONGESTPATTERN", maxempatlen); + cdef("MAXPATLEN", maxtokpatlen); + cdef("MAXREPLLEN", maxtokrepllen); + cdef("MAXEMREPLLEN", maxemrepllen); + cdef("MAXPROCARG", maxprocargs); + cdef("MAXRULE", maxrule < 16 ? 16 : maxrule); + if (nsplit > 0) + { + cdef("MAXSPLIT", maxsplit); } if (tabledebug) - cdef("TABLEDEBUG",1); + cdef("TABLEDEBUG", 1); if (use_tes) - cdef("USE_TES",1); + cdef("USE_TES", 1); } -outars() { - register i; - +void outars(void) +{ + register int i; + if (code_in_c) - fprintf(htable,"#define CODEINC 1\n"); - else { - fprintf(ctable,"char coderules[%d];\n",codeindex); - fprintf(ctable,"int ncodebytes=%d;\n",codeindex); + fprintf(htable, "#define CODEINC 1\n"); + else + { + fprintf(ctable, "char coderules[%d];\n", codeindex); + fprintf(ctable, "int ncodebytes=%d;\n", codeindex); } - fprintf(ctable,"char pattern[%d]={\n",npatbytes+1); - for(i=0;i<=npatbytes;i++) { - fprintf(ctable,"%d,%c",pattern[i]&BMASK,i%16==15 ? '\n' : ' '); + fprintf(ctable, "char pattern[%d]={\n", npatbytes + 1); + for (i = 0; i <= npatbytes; i++) + { + fprintf(ctable, "%d,%c", pattern[i] & BMASK, i % 16 == 15 ? '\n' : ' '); } - fprintf(ctable,"};\n\n"); - fprintf(ctable,"int pathash[256]={\n"); - for(i=0;i<256;i++) { - fprintf(ctable,"%d,%c",pathash[i]&0xFFFF,i%10==9 ? '\n' : ' '); + fprintf(ctable, "};\n\n"); + fprintf(ctable, "int pathash[256]={\n"); + for (i = 0; i < 256; i++) + { + fprintf(ctable, "%d,%c", pathash[i] & 0xFFFF, i % 10 == 9 ? '\n' : ' '); } - fprintf(ctable,"};\n"); + fprintf(ctable, "};\n"); } -finishio() { +void finishio(void) +{ extern int nregs; finishcode(); hashpatterns(); - setsize = SZOFSET(nregs+ntokens); + setsize = SZOFSET(nregs + ntokens); outdefs(); outincludes(); outregs(); @@ -663,7 +752,7 @@ finishio() { outmoves(); outtests(); outstacks(); - if (nsplit>0) + if (nsplit > 0) outsplits(); outcoercs(); outproplists(); @@ -673,31 +762,31 @@ finishio() { outars(); } -void -codecoco(cocono) { +void codecoco(int cocono) +{ - if (cocono== -1) + if (cocono == -1) return; code8(DO_SETCC); codeint(cocono); codenl(); } -dopattern(stackcoerc,kills,allocates,generates,yields,leaving) -varinfo *kills,*allocates,*generates,*yields,*leaving; +void dopattern(int stackcoerc, varinfo *kills, varinfo *allocates, + varinfo *generates, varinfo *yields, varinfo *leaving) { - register i; - int n,nops; - register struct varinfo *vp,*vivp; + register int i; + int n, nops; + register struct varinfo *vp, *vivp; register instr_p instp; - int al,deal; + int al, deal; int vil; - int cocono= -1; + int cocono = -1; cost_t totcost; int nremoves; int removelist[100]; static char tlab[] = "0:"; - extern int optexact,optstack,startline; + extern int optexact, optstack, startline; extern char *filename; extern int lineno; @@ -705,90 +794,114 @@ varinfo *kills,*allocates,*generates,*yields,*leaving; fprintf(code,"Code(%d) at \"%s\", line %d\n",stackcoerc,filename,lineno); #endif if (code_in_c) - fprintf(ctable,"\n/* \"%s\", line %d */ ",filename,lineno); - if (tabledebug) { + fprintf(ctable, "\n/* \"%s\", line %d */ ", filename, lineno); + if (tabledebug) + { code8(DO_DLINE); - codeint(startline); + codeint(startline); codenl(); - if (startlinemaxline) - maxline=startline; - BIS(lineset,startline); - } else { - static int beenhere=0; + if (startline < MAXSOURCELINES) + { + if (startline > maxline) + maxline = startline; + BIS(lineset, startline); + } + else + { + static int beenhere = 0; - if (!beenhere) { + if (!beenhere) + { beenhere++; error("Too many source lines for table debug"); } } } /* MATCH part */ - if (tokpatlen) { + if (tokpatlen) + { if (optexact) if (optstack) - code53(DO_XXMATCH,tokpatlen); + code53(DO_XXMATCH, tokpatlen); else - code53(DO_XMATCH,tokpatlen); + code53(DO_XMATCH, tokpatlen); else - code53(DO_MATCH,tokpatlen); - for (i=0;ivi_next) { - if (vp->vi_int[1] != 0) { - code53(DO_REMOVE,1); + for (vp = kills; vp != 0; vp = vp->vi_next) + { + if (vp->vi_int[1] != 0) + { + code53(DO_REMOVE, 1); codeint(vp->vi_int[0]); codeint(vp->vi_int[1]); - } else if (vp->vi_int[0] >= 0) { - code53(DO_REMOVE,0); + } + else if (vp->vi_int[0] >= 0) + { + code53(DO_REMOVE, 0); codeint(vp->vi_int[0]); - } else { + } + else + { code8(DO_KILLREG); codeint(-vp->vi_int[0] - 1); - } - codenl(); + } codenl(); } - nremoves=0; - for(vp=generates;vp!=0;vp=vp->vi_next) { + nremoves = 0; + for (vp = generates; vp != 0; vp = vp->vi_next) + { if (vp->vi_int[0] != INSREMOVE) continue; - for(i=0;ivi_int[1]==removelist[i]) + for (i = 0; i < nremoves; i++) + if (vp->vi_int[1] == removelist[i]) break; - if (i==nremoves) { - assert(nremoves<(sizeof(removelist)/sizeof(int))); + if (i == nremoves) + { + assert(nremoves < (sizeof(removelist) / sizeof(int))); removelist[nremoves++] = vp->vi_int[1]; } } - for(i=0;ivi_next) { - if (vp->vi_int[0] == -1) { /* Deallocate */ + deal = 0; + al = 0; + for (vp = allocates; vp != 0; vp = vp->vi_next) + { + if (vp->vi_int[0] == -1) + { /* Deallocate */ deal++; code8(DO_DEALLOCATE); codeint(vp->vi_int[1]); codenl(); - } else { - if (vp->vi_int[1]==0) { - code53(DO_ALLOCATE,0); + } + else + { + if (vp->vi_int[1] == 0) + { + code53(DO_ALLOCATE, 0); codeint(vp->vi_int[0]); codenl(); - } else { - code53(DO_ALLOCATE,1); + } + else + { + code53(DO_ALLOCATE, 1); codeint(vp->vi_int[0]); codeint(vp->vi_int[1]); codenl(); @@ -798,132 +911,143 @@ varinfo *kills,*allocates,*generates,*yields,*leaving; } if (deal) code8nl(DO_REALLOCATE); - if (al>maxallreg) - maxallreg=al; + if (al > maxallreg) + maxallreg = al; totcost.ct_space = 0; - totcost.ct_time = 0; - for(vp=generates;vp!=0;vp=vp->vi_next) { - n= vp->vi_int[0]; - switch(n) { - default: - assert(n>=0); - instp = &l_instr[n]; - nops=instp->i_nops; - code53(DO_INSTR,nops); - if (vp->vi_int[1]==0) { - codeint(instp->i_asname); - } else { - codeint(10000+vp->vi_int[1]); - } - vivp=vp->vi_vi; - for(i=0;ivi_int[0]); - vivp = vivp->vi_vi; - } - codenl(); - totcost.ct_space += instp->i_cost.ct_space; - totcost.ct_time += instp->i_cost.ct_time ; - break; - case INSREMOVE: - break; - case INSMOVE: - codecoco(cocono); - code8(DO_MOVE); - codeint(vp->vi_int[1]); - codeint(vp->vi_int[2]); - codenl(); - break; - case INSTEST: - codecoco(cocono); - code8(DO_TEST); - codeint(vp->vi_int[1]); - codenl(); - break; - case INSPRETURN: - code8(DO_PRETURN); - codenl(); - break; - case INSTLAB: - cocono = 0; - tlab[0] = vp->vi_int[1] + '0'; - code53(DO_INSTR,0); - codeint(strlookup(tlab)); - codenl(); - break; - case INSSETCC: - cocono=vp->vi_int[1]; - break; - case INSERASE: - code8(DO_ERASE); - codeint(vp->vi_int[1]); - codenl(); - break; - case INSLABDEF: - cocono = 0; - code8(DO_LABDEF); - codeint(vp->vi_int[1]); - codenl(); - break; + totcost.ct_time = 0; + for (vp = generates; vp != 0; vp = vp->vi_next) + { + n = vp->vi_int[0]; + switch (n) + { + default: + assert(n >= 0); + instp = &l_instr[n]; + nops = instp->i_nops; + code53(DO_INSTR, nops); + if (vp->vi_int[1] == 0) + { + codeint(instp->i_asname); + } + else + { + codeint(10000 + vp->vi_int[1]); + } + vivp = vp->vi_vi; + for (i = 0; i < nops; i++) + { + codeint(vivp->vi_int[0]); + vivp = vivp->vi_vi; + } + codenl(); + totcost.ct_space += instp->i_cost.ct_space; + totcost.ct_time += instp->i_cost.ct_time; + break; + case INSREMOVE: + break; + case INSMOVE: + codecoco(cocono); + code8(DO_MOVE); + codeint(vp->vi_int[1]); + codeint(vp->vi_int[2]); + codenl(); + break; + case INSTEST: + codecoco(cocono); + code8(DO_TEST); + codeint(vp->vi_int[1]); + codenl(); + break; + case INSPRETURN: + code8(DO_PRETURN); + codenl(); + break; + case INSTLAB: + cocono = 0; + tlab[0] = vp->vi_int[1] + '0'; + code53(DO_INSTR, 0); + codeint(strlookup(tlab)); + codenl(); + break; + case INSSETCC: + cocono = vp->vi_int[1]; + break; + case INSERASE: + code8(DO_ERASE); + codeint(vp->vi_int[1]); + codenl(); + break; + case INSLABDEF: + cocono = 0; + code8(DO_LABDEF); + codeint(vp->vi_int[1]); + codenl(); + break; } } codecoco(cocono); vil = vilength(yields); - if (vil!=0 || tokpatlen!=0 || allocates!=0) { - code53(DO_TOKREPLACE,vilength(yields)); - for(vp=yields;vp!=0;vp=vp->vi_next) { + if (vil != 0 || tokpatlen != 0 || allocates != 0) + { + code53(DO_TOKREPLACE, vilength(yields)); + for (vp = yields; vp != 0; vp = vp->vi_next) + { codeint(vp->vi_int[0]); - } - codenl(); + } codenl(); } - if (leaving!=0) { - code53(DO_EMREPLACE,vilength(leaving)); - while (leaving!=0) { + if (leaving != 0) + { + code53(DO_EMREPLACE, vilength(leaving)); + while (leaving != 0) + { codeint(leaving->vi_int[0]); codeint(leaving->vi_int[1]); leaving = leaving->vi_next; - } - codenl(); + } codenl(); } - if (totcost.ct_space!=0 || totcost.ct_time!=0) { + if (totcost.ct_space != 0 || totcost.ct_time != 0) + { code8(DO_COST); codeint(totcost.ct_space); codeint(totcost.ct_time); codenl(); } - if (empatlen==0 && !inproc) + if (empatlen == 0 && !inproc) code8nl(DO_RETURN); else code8nl(DO_NEXTEM); } -used(resource,use,max) char *resource; { +void used(char *resource, int use, int max) +{ - if (verbose || 4*use > 3*max) - fprintf(stderr,"%s %d(%d)\n",resource,use,max); + if (verbose || 4 * use > 3 * max) + fprintf(stderr, "%s %d(%d)\n", resource, use, max); } -statistics() { - extern int nnodes, maxempatlen,maxrule; +void statistics(void) +{ + extern int nnodes, maxempatlen, maxrule; - used("Registers",nregs,MAXREGS); - used("Properties",nprops,MAXPROPS); - used("Tokens",ntokens,MAXTOKENS); - used("Tokensize",maxtokensize,MAXATT); - used("Sets",nsets,MAXSETS); - used("Instructions",ninstr,MAXINSTR); - used("Strings",nstrings,MAXSTRINGS); - used("Exp-nodes",nnodes,MAXNODES); - used("EM-pat length",maxempatlen,EMPATMAX); - used("rules/EM-pattern",maxrule,MAXPATTERNS); - used("Allocates/rule",maxallreg,MAXALLREG); - used("Instances",ninstances,MAXINSTANCES); - used("Moves",nmoves,MAXMOVES); - used("Tests",ntests,MAXTESTS); - used("Stacks",nstacks,MAXSTACKS); - used("1->1 Coercions",ncoercs,MAXCOERCS); - used("Splitting coercions",nsplit,MAXSPLCOERC); - used("Register variables",maxregvars,MAXREGVAR); - used("Pat bytes",npatbytes+1,MAXPATBYTES); + used("Registers", nregs, MAXREGS); + used("Properties", nprops, MAXPROPS); + used("Tokens", ntokens, MAXTOKENS); + used("Tokensize", maxtokensize, MAXATT); + used("Sets", nsets, MAXSETS); + used("Instructions", ninstr, MAXINSTR); + used("Strings", nstrings, MAXSTRINGS); + used("Exp-nodes", nnodes, MAXNODES); + used("EM-pat length", maxempatlen, EMPATMAX); + used("rules/EM-pattern", maxrule, MAXPATTERNS); + used("Allocates/rule", maxallreg, MAXALLREG); + used("Instances", ninstances, MAXINSTANCES); + used("Moves", nmoves, MAXMOVES); + used("Tests", ntests, MAXTESTS); + used("Stacks", nstacks, MAXSTACKS); + used("1->1 Coercions", ncoercs, MAXCOERCS); + used("Splitting coercions", nsplit, MAXSPLCOERC); + used("Register variables", maxregvars, MAXREGVAR); + used("Pat bytes", npatbytes + 1, MAXPATBYTES); if (tabledebug) - used("Source lines",maxline,MAXSOURCELINES); + used("Source lines", maxline, MAXSOURCELINES); } diff --git a/util/ncgg/scan.l b/util/ncgg/scan.l index 46ea6d5e4..f57bc9e97 100644 --- a/util/ncgg/scan.l +++ b/util/ncgg/scan.l @@ -15,14 +15,14 @@ static char rcsid2[]= "$Id$"; #include "instruct.h" #include "expr.h" #include "extern.h" +#include "subr.h" #include #include #include "y.tab.h" extern int emhere; -char *mystrcpy(); -int myatoi(); +extern int mlookup(char *name); int lineno=1; extern char *filename; @@ -101,7 +101,7 @@ extern char *filename; %% int skipping=0; -yywrap() { +int yywrap(void) { if (skipping) fatal("EOF reached during error recovery"); @@ -113,8 +113,8 @@ yywrap() { #define yytext_ptr yytext -skipupto(tok,str) char *str; { - register i; +void skipupto(int tok,char *str) { + register int i; skipping=1; while (yylex()!=tok) diff --git a/util/ncgg/set.c b/util/ncgg/set.c index d2d9b0d8f..dd37590c2 100644 --- a/util/ncgg/set.c +++ b/util/ncgg/set.c @@ -3,7 +3,7 @@ * See the copyright notice in the ACK home directory, in the file "Copyright". */ #ifndef NORCSID -static char rcsid[]= "$Id$"; +static char rcsid[] = "$Id$"; #endif #include "param.h" @@ -12,143 +12,161 @@ static char rcsid[]= "$Id$"; #include "token.h" #include "lookup.h" #include "reg.h" +#include "subr.h" #include #include "extern.h" extern set_t l_sets[]; - -setlookup(s) set_t s; { - register set_p p; - register i; - int setno; - - for(p=l_sets;p<&l_sets[nsets];p++) { - if (p->set_size != s.set_size) - continue; - for (i=0;iset_val[i] != s.set_val[i]) - goto cont; - return(p-l_sets); - cont:; - } - setno = NEXT(nsets,MAXSETS,"Sets"); - l_sets[setno] = s; - return(setno); -} - -make_std_sets() { - set_t s; - register i; - - for(i=0;iset_size != s.set_size) + continue; + for (i = 0; i < SETSIZE; i++) + if (p->set_val[i] != s.set_val[i]) + goto cont; + return (p - l_sets); + cont: ; + } + setno = NEXT(nsets, MAXSETS, "Sets"); + l_sets[setno] = s; + return (setno); +} + +void make_std_sets(void) +{ + set_t s; + register int i; + + for (i = 0; i < SETSIZE; i++) + s.set_val[i] = 0; + for (i = 0; i < nregs + ntokens; i++) + BIS(s.set_val, i); + s.set_size = 0; + allsetno = setlookup(s); + n_set("ALL", allsetno); +} + +set_t ident_to_set(char *name) +{ register symbol *sy_p; - register i; + register int i; register struct propinfo *pp; int bitno; set_t result; - sy_p = lookup(name,symany,mustexist); - switch(sy_p->sy_type) { - default: - error("%s is wrong kind of symbol",name); - return(emptyset); - case symprop: - pp = &l_props[sy_p->sy_value.syv_propno]; - result.set_size = pp->pr_size; - for (i=0;ipr_regset[i]; - BIS(result.set_val,0); - for (;isy_value.syv_tokno+nregs; - for (i=0;isy_value.syv_tokno]->tk_size; - break; - case symset: - return(l_sets[sy_p->sy_value.syv_setno]); + sy_p = lookup(name, symany, mustexist); + switch (sy_p->sy_type) + { + default: + error("%s is wrong kind of symbol", name); + return (emptyset); + case symprop: + pp = &l_props[sy_p->sy_value.syv_propno]; + result.set_size = pp->pr_size; + for (i = 0; i < SZOFSET(MAXREGS); i++) + result.set_val[i] = pp->pr_regset[i]; + BIS(result.set_val, 0); + for (; i < SETSIZE; i++) + result.set_val[i] = 0; + break; + case symtok: + bitno = sy_p->sy_value.syv_tokno + nregs; + for (i = 0; i < SETSIZE; i++) + result.set_val[i] = 0; + BIS(result.set_val, bitno); + result.set_size = l_tokens[sy_p->sy_value.syv_tokno]->tk_size; + break; + case symset: + return (l_sets[sy_p->sy_value.syv_setno]); } - return(result); + return (result); } -static void -checksize(s) - register set_p s; +static void checksize(register set_p s) { register int i; register int size = -1; s->set_size = 0; - for (i = 1; i <= nregs; i++) { - if (BIT(s->set_val, i)) { + for (i = 1; i <= nregs; i++) + { + if (BIT(s->set_val, i)) + { register int sz = l_regs[i].ri_size; - if (size == -1) size = sz; - else if (size != sz) return; + if (size == -1) + size = sz; + else if (size != sz) + return; } } - for (i = 1; i <= ntokens; i++) { - if (BIT(s->set_val, i+nregs)) { + for (i = 1; i <= ntokens; i++) + { + if (BIT(s->set_val, i + nregs)) + { register int sz = l_tokens[i]->tk_size; - if (size == -1) size = sz; - else if (size != sz) return; + if (size == -1) + size = sz; + else if (size != sz) + return; } } - if (size != -1) s->set_size = size; + if (size != -1) + s->set_size = size; } -set_t setproduct(s1,s2) set_t s1,s2; { +set_t setproduct(set_t s1, set_t s2) +{ set_t result; - register i; + register int i; - for(i=0;i #define BIS(sp,n) (sp)[(n)>>4] |= 1<<((n)&0xF) #define BIC(sp,n) (sp)[(n)>>4] &= ~(1<<((n)&0xF)) #define BIT(sp,n) (((sp)[(n)>>4]&(1<<((n)&0xF)))!=0) + +int setlookup(set_t s); +void make_std_sets(void); +set_t ident_to_set(char *name); +set_t setproduct(set_t s1, set_t s2); +set_t setsum(set_t s1, set_t s2); +set_t setdiff(set_t s1, set_t s2); + + +#endif /* SET_H_ */ diff --git a/util/ncgg/strlookup.c b/util/ncgg/strlookup.c index 708dd9b41..52eca7e74 100644 --- a/util/ncgg/strlookup.c +++ b/util/ncgg/strlookup.c @@ -3,24 +3,25 @@ * See the copyright notice in the ACK home directory, in the file "Copyright". */ #ifndef NORCSID -static char rcsid[]= "$Id$"; +static char rcsid[] = "$Id$"; #endif #include #include "param.h" +#include "subr.h" #include "extern.h" -int nstrings=0; +int nstrings = 0; char *l_strings[MAXSTRINGS]; -strlookup(str) char *str; { - register i; - extern char *mystrcpy(); +int strlookup(char *str) +{ + register int i; - for(i=0;i @@ -20,77 +20,86 @@ static char rcsid[]= "$Id$"; #include "regvar.h" #include #include +#include "subr.h" +#include "hall.h" #include "extern.h" -n_proc(name) char *name; { - register symbol *sy_p; - extern int npatbytes; +extern int npatbytes; +extern set_t l_sets[]; - sy_p = lookup(name,symproc,newsymbol); + +void n_proc(char *name) +{ + register symbol *sy_p; + + sy_p = lookup(name, symproc, newsymbol); sy_p->sy_value.syv_procoff = npatbytes + 1; } -struct varinfo * -make_erase(name) char *name; { - expr_t e,ident_expr(); +struct varinfo * make_erase(char *name) +{ + expr_t e; struct varinfo *result; e = ident_expr(name); if (e.ex_typ != TYPREG) error("Register name required here"); - NEW(result,struct varinfo); + NEW(result, struct varinfo); result->vi_next = VI_NULL; result->vi_int[0] = e.ex_index; - return(result); + return (result); } -n_instr(name,asname,oplist,eraselist,cost) -char *name,*asname; -operand *oplist; -struct varinfo *eraselist,*cost; +void n_instr(char *name, char *asname, operand *oplist, struct varinfo *eraselist, + struct varinfo *cost) { - register instrno; - register cc_count; + register int instrno; + register int cc_count; register instr_p ip; - instrno = NEXT(ninstr,MAXINSTR,"Instructions"); + instrno = NEXT(ninstr, MAXINSTR, "Instructions"); ip = &l_instr[instrno]; ip->i_name = name; - ip->i_asname = strlookup(asname!=0 ? asname : name); + ip->i_asname = strlookup(asname != 0 ? asname : name); ip->i_nops = 0; ip->i_oplist = oplist; ip->i_erases = eraselist; - if (cost==0) { + if (cost == 0) + { ip->i_cost.ct_space = 0; ip->i_cost.ct_time = 0; - } else { + } + else + { ip->i_cost.ct_space = cost->vi_int[0]; ip->i_cost.ct_space = cost->vi_int[1]; } - for (cc_count=0; oplist!=0; oplist = oplist->o_next) { + for (cc_count = 0; oplist != 0; oplist = oplist->o_next) + { ip->i_nops++; - if(oplist->o_adorn&AD_CC) + if (oplist->o_adorn & AD_CC) cc_count++; } - while (eraselist!=VI_NULL) { + while (eraselist != VI_NULL ) + { if (eraselist->vi_int[0] == -1 && cc_count) error("Instruction can't both set and break the condition codes"); - eraselist=eraselist->vi_next; + eraselist = eraselist->vi_next; } - if (cc_count>1) + if (cc_count > 1) error("No instruction can set condition codes more than once"); } -n_set(name,number) char *name; { +void n_set(char *name, int number) +{ register symbol *sy_p; - sy_p = lookup(name,symset,newsymbol); + sy_p = lookup(name, symset, newsymbol); sy_p->sy_value.syv_setno = number; } -n_tok(name,atts,size,cost,format) -char *name; -struct varinfo *atts,*cost,*format; +void n_tok(char *name, struct varinfo *atts, int size, + struct varinfo *cost, struct varinfo *format) { register symbol *sy_p; register token_p tp; @@ -98,146 +107,164 @@ struct varinfo *atts,*cost,*format; int i; int tokno; int thistokensize; - char formstr[50],smallstr[2]; + char formstr[50], smallstr[2]; - sy_p = lookup(name,symtok,newsymbol); - NEW(tp,token_t); - tokno = NEXT(ntokens,MAXTOKENS,"Tokens"); + sy_p = lookup(name, symtok, newsymbol); + NEW(tp, token_t); + tokno = NEXT(ntokens, MAXTOKENS, "Tokens"); sy_p->sy_value.syv_tokno = tokno; l_tokens[tokno] = tp; tp->tk_name = sy_p->sy_name; tp->tk_size = size; - if (cost != 0) { + if (cost != 0) + { tp->tk_cost.ct_space = cost->vi_int[0]; - tp->tk_cost.ct_time = cost->vi_int[1]; - } else { - tp->tk_cost.ct_space = 0; - tp->tk_cost.ct_time = 0; + tp->tk_cost.ct_time = cost->vi_int[1]; } - for(i=0,vip=atts;ivi_next) { + else + { + tp->tk_cost.ct_space = 0; + tp->tk_cost.ct_time = 0; + } + for (i = 0, vip = atts; i < MAXATT && vip != 0; i++, vip = vip->vi_next) + { tp->tk_att[i].ta_type = vip->vi_int[0]; tp->tk_att[i].ta_name = vip->vi_str[0]; - vip->vi_str[0]=0; + vip->vi_str[0] = 0; } - thistokensize=i; - if (i>maxtokensize) - maxtokensize=i; - if (vip!=0) - error("More then %d attributes, rest discarded",MAXATT); - for(;itk_att[i].ta_type= -3; - if (format!=0) { + thistokensize = i; + if (i > maxtokensize) + maxtokensize = i; + if (vip != 0) + error("More then %d attributes, rest discarded", MAXATT); + for (; i < MAXATT; i++) + tp->tk_att[i].ta_type = -3; + if (format != 0) + { formstr[0] = 0; - for (vip=format;vip!=0;vip=vip->vi_next) { - if (vip->vi_int[0]==0) - strcat(formstr,vip->vi_str[0]); - else { - for(i=0;ivi_str[0],tp->tk_att[i].ta_name)==0) { - smallstr[0] = i+1; + for (vip = format; vip != 0; vip = vip->vi_next) + { + if (vip->vi_int[0] == 0) + strcat(formstr, vip->vi_str[0]); + else + { + for (i = 0; i < thistokensize; i++) + { + if (strcmp(vip->vi_str[0], tp->tk_att[i].ta_name) == 0) + { + smallstr[0] = i + 1; smallstr[1] = 0; - strcat(formstr,smallstr); + strcat(formstr, smallstr); break; } } - if (i==thistokensize) - error("%s not a known attribute", - vip->vi_str[0]); + if (i == thistokensize) + error("%s not a known attribute", vip->vi_str[0]); } } tp->tk_format = strlookup(formstr); - } else + } + else tp->tk_format = -1; } -checkprintformat(n) { +void checkprintformat(int n) +{ register short *s; - register i; - extern set_t l_sets[]; - - s= l_sets[n].set_val; - for(i=nregs;itk_format<0) + register int i; + + s = l_sets[n].set_val; + for (i = nregs; i < nregs + ntokens; i++) + if (BIT(s,i) && l_tokens[i - nregs]->tk_format < 0) error("Token %s in set does not have printformat", - l_tokens[i-nregs]->tk_name); + l_tokens[i - nregs]->tk_name); } -n_prop(name,size) char *name; int size; { +void n_prop(char *name, int size) +{ int propno; register symbol *sp; - propno = NEXT(nprops,MAXPROPS,"Properties"); - sp = lookup(name,symprop,newsymbol); + propno = NEXT(nprops, MAXPROPS, "Properties"); + sp = lookup(name, symprop, newsymbol); sp->sy_value.syv_propno = propno; - if (size <= 0) { + if (size <= 0) + { error("Size of property must be >0"); size = wordsize; } l_props[propno].pr_size = size; } -void -prophall(n) { - register i; +void prophall(int n) +{ + register int i; short hallset[SETSIZE]; - - if (n < 0) return; - for(i=0;isy_value.syv_regno = regno = NEXT(nregs,MAXREGS,"Number of registers"); + sy_p = lookup(name, symreg, newsymbol); + sy_p->sy_value.syv_regno = regno = NEXT(nregs, MAXREGS, + "Number of registers"); ri_p = &l_regs[regno]; ri_p->ri_name = mystrcpy(name); - ri_p->ri_repr = printstring!=0 ? mystrcpy(printstring) : ri_p->ri_name; + ri_p->ri_repr = printstring != 0 ? mystrcpy(printstring) : ri_p->ri_name; ri_p->ri_memb[0] = member1; ri_p->ri_memb[1] = member2; - if (nmemb>maxmembers) - maxmembers=nmemb; - return(regno); + if (nmemb > maxmembers) + maxmembers = nmemb; + return (regno); } -make_const() { +void make_const(void) +{ wordsize = cmustbeset("EM_WSIZE"); pointersize = cmustbeset("EM_PSIZE"); } -cmustbeset(ident) char *ident; { +int cmustbeset(char *ident) +{ - return(lookup(ident,symconst,mustexist)->sy_value.syv_cstval); + return (lookup(ident, symconst, mustexist)->sy_value.syv_cstval); } -n_const(ident,val) char *ident; { +void n_const(char *ident, int val) +{ register symbol *sy_p; - sy_p = lookup(ident,symconst,newsymbol); + sy_p = lookup(ident, symconst, newsymbol); sy_p->sy_value.syv_cstval = val; } -n_sconst(ident,val) char *ident,*val; { +void n_sconst(char *ident, char *val) +{ register symbol *sy_p; - sy_p = lookup(ident,symsconst,newsymbol); + sy_p = lookup(ident, symsconst, newsymbol); sy_p->sy_value.syv_stringno = strlookup(val); } -static void -add_regvar(int rvnum, reginfo *regp, int rv) +static void add_regvar(int rvnum, reginfo *regp, int rv) { int overlap, wrong; overlap = wrong = 0; - if (regp->ri_memb[0]!=0) { + if (regp->ri_memb[0] != 0) + { /* reglap: float may overlap with one subregister */ - if (rv==reg_float && regp->ri_memb[1]==0) + if (rv == reg_float && regp->ri_memb[1] == 0) overlap = 1; else wrong = 1; @@ -248,28 +275,34 @@ add_regvar(int rvnum, reginfo *regp, int rv) rvused |= ANY_REGVAR; if (regp->ri_size == wordsize) rvused |= SL_REGVAR; - else if (regp->ri_size == 2*wordsize) + else if (regp->ri_size == 2 * wordsize) rvused |= DL_REGVAR; wrong = 0; - if (overlap) { + if (overlap) + { /* reglap = size of overlap float */ - if (reglap==0) { + if (reglap == 0) + { reglap = regp->ri_size; if (reglap == rvsize[reg_float]) error("Two sizes of reg_float can't be same size"); - } else if (reglap!=regp->ri_size) + } + else if (reglap != regp->ri_size) wrong = 1; - } else { - if (nregvar[rv]==0) + } + else + { + if (nregvar[rv] == 0) rvsize[rv] = regp->ri_size; - else if (rvsize[rv]!=regp->ri_size) + else if (rvsize[rv] != regp->ri_size) wrong = 1; } if (wrong) error("All register variables of one type must have the same size"); - if (overlap) { + if (overlap) + { reginfo *member_p = &l_regs[regp->ri_memb[0]]; int i; @@ -278,54 +311,59 @@ add_regvar(int rvnum, reginfo *regp, int rv) * Add reg_p in its place. */ wrong = 1; - for (i = 0; i < nregvar[rv]; i++) { - if (rvnumbers[rv][i] == regp->ri_memb[0]) { + for (i = 0; i < nregvar[rv]; i++) + { + if (rvnumbers[rv][i] == regp->ri_memb[0]) + { rvnumbers[rv][i] = rvnum; wrong = 0; break; } } if (wrong) - error("Register variable %s can't overlap %s", - regp->ri_name, member_p->ri_name); - } else { - NEXT(nregvar[rv],MAXREGVAR,"Register variable"); - rvnumbers[rv][nregvar[rv]-1] = rvnum; + error("Register variable %s can't overlap %s", regp->ri_name, + member_p->ri_name); + } + else + { + NEXT(nregvar[rv], MAXREGVAR, "Register variable"); + rvnumbers[rv][nregvar[rv] - 1] = rvnum; } } -void -regline(rl,pl,rv) varinfo *rl,*pl; { - register varinfo *rrl,*rpl; +void regline(varinfo *rl, varinfo *pl, int rv) +{ + register varinfo *rrl, *rpl; register short *sp; register reginfo *regp; int thissize; int propno; - for(rrl=rl;rrl!=0;rrl=rrl->vi_next) { + for (rrl = rl; rrl != 0; rrl = rrl->vi_next) + { regp = &l_regs[rrl->vi_int[0]]; thissize = 0; - for(rpl=pl;rpl!=0;rpl=rpl->vi_next) { + for (rpl = pl; rpl != 0; rpl = rpl->vi_next) + { propno = rpl->vi_int[0]; - sp= l_props[propno].pr_regset; - BIS(sp,rrl->vi_int[0]); - if (thissize==0) + sp = l_props[propno].pr_regset; + BIS(sp, rrl->vi_int[0]); + if (thissize == 0) thissize = l_props[propno].pr_size; - else if (thissize!=-1 && thissize!=l_props[propno].pr_size) - error("Register %s has no clear size", - regp->ri_name); + else if (thissize != -1 && thissize != l_props[propno].pr_size) + error("Register %s has no clear size", regp->ri_name); } regp->ri_size = thissize; regp->ri_class = regclass; regp->ri_rregvar = rv; - if (rv>=0) + if (rv >= 0) add_regvar(rrl->vi_int[0], regp, rv); } regclass++; } -void -check_reglap() { +void check_reglap(void) +{ reginfo *regp; int i; @@ -333,129 +371,147 @@ check_reglap() { return; /* reglap: Check that every reg_float has size == reglap. */ - for (i = 0; i < nregvar[reg_float]; i++) { + for (i = 0; i < nregvar[reg_float]; i++) + { regp = &l_regs[rvnumbers[reg_float][i]]; if (regp->ri_size != reglap) - error("Missing reg_float of size %d to contain %s", - reglap, regp->ri_name); + error("Missing reg_float of size %d to contain %s", reglap, + regp->ri_name); } } -setallreg(vi) struct varinfo *vi; { +void setallreg(struct varinfo *vi) +{ - nallreg=0; - for(;vi!=0;vi=vi->vi_next) { - if (vi->vi_int[0]<0) + nallreg = 0; + for (; vi != 0; vi = vi->vi_next) + { + if (vi->vi_int[0] < 0) continue; allreg[nallreg++] = vi->vi_int[0]; } } -void -freevi(vip) register struct varinfo *vip; { - register i; +void freevi(register struct varinfo *vip) +{ + register int i; - if (vip==0) + if (vip == 0) return; freevi(vip->vi_next); freevi(vip->vi_vi); - for (i=0;ivi_str[i]); free(vip); } -int myatoi(s) register char *s; { - register int base=10; - register sum=0; +int myatoi(register char *s) +{ + register int base = 10; + register int sum = 0; - if (*s=='0') { + if (*s == '0') + { base = 8; s++; - if (*s=='x') { - base=16; + if (*s == 'x') + { + base = 16; s++; } } - for (;;) { - switch (*s) { - default: return(sum); - case '8': - case '9': - if (base==8) error("Bad digit in octal number"); - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - sum = sum*base + *s++ - '0'; - break; - case 'a': - case 'b': - case 'c': - case 'd': - case 'e': - case 'f': - if (base!=16) error("Hexletter in number not expected"); - sum = sum*base + 10 + *s++ - 'a'; - break; - case 'A': - case 'B': - case 'C': - case 'D': - case 'E': - case 'F': - if (base!=16) error("Hexletter in number not expected"); - sum = sum*base + 10 + *s++ - 'A'; - break; + for (;;) + { + switch (*s) + { + default: + return (sum); + case '8': + case '9': + if (base == 8) + error("Bad digit in octal number"); + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + sum = sum * base + *s++ - '0'; + break; + case 'a': + case 'b': + case 'c': + case 'd': + case 'e': + case 'f': + if (base != 16) + error("Hexletter in number not expected"); + sum = sum * base + 10 + *s++ - 'a'; + break; + case 'A': + case 'B': + case 'C': + case 'D': + case 'E': + case 'F': + if (base != 16) + error("Hexletter in number not expected"); + sum = sum * base + 10 + *s++ - 'A'; + break; } } } -char *mystrcpy(s) char *s; { +char *mystrcpy(char *s) +{ register char *p; - char *myalloc(); - p=myalloc(strlen(s)+1); - strcpy(p,s); - return(p); + p = myalloc(strlen(s) + 1); + strcpy(p, s); + return (p); } -char *myalloc(n) register n; { - register char *p,*result; +char *myalloc(register int n) +{ + register char *p, *result; - result=p=malloc(n); - if (p== (char *) 0) + result = p = malloc(n); + if (p == (char *) 0) fatal("Out of memory"); - do *p++=0; while (--n); - return(result); + do + *p++ = 0; + while (--n); + return (result); } -chkincl(value,lwb,upb) { +int chkincl(int value, int lwb, int upb) +{ - if (valueupb) - error("Number %d should have been between %d and %d", - value,lwb,upb); - return(value); + if (value < lwb || value > upb) + error("Number %d should have been between %d and %d", value, lwb, upb); + return (value); } -subset(sp1,sp2,setsize) short *sp1,*sp2; { - register i; +int subset(short *sp1, short *sp2, int setsize) +{ + register int i; - for(i=0;ivi_next; + while (vip != 0) + { + vip = vip->vi_next; l++; } - return(l); + return (l); } diff --git a/util/ncgg/subr.h b/util/ncgg/subr.h new file mode 100644 index 000000000..dd5fa3ac2 --- /dev/null +++ b/util/ncgg/subr.h @@ -0,0 +1,42 @@ +/* Copyright (c) 2019 ACK Project. + * See the copyright notice in the ACK home directory, + * in the file "Copyright". + * + * Created on: 2019-05-02 + * + */ +#ifndef SUBR_H_ +#define SUBR_H_ + +#include "instruct.h" + +struct varinfo; + + +void n_proc(char *name); +struct varinfo * make_erase(char *name); +void n_instr(char *name, char *asname, operand *oplist, struct varinfo *eraselist, + struct varinfo *cost); +void n_set(char *name, int number); +void n_tok(char *name, struct varinfo *atts, int size, + struct varinfo *cost, struct varinfo *format); +void checkprintformat(int n); +void n_prop(char *name, int size); +void prophall(int n); +int n_reg(char *name, char *printstring, int nmemb, int member1, int member2); +void make_const(void); +int cmustbeset(char *ident); +void n_const(char *ident, int val); +void n_sconst(char *ident, char *val); +void regline(struct varinfo *rl, struct varinfo *pl, int rv); +void check_reglap(void); +void setallreg(struct varinfo *vi); +void freevi(register struct varinfo *vip); +int myatoi(register char *s); +char *mystrcpy(char *s); +char *myalloc(register int n); +int chkincl(int value, int lwb, int upb); +int subset(short *sp1, short *sp2, int setsize); +int vilength(register struct varinfo *vip); + +#endif /* SUBR_H_ */