📄 pangen1.c
字号:
return k;}voidc_chandump(FILE *fd){ Queue *q; char buf[256]; int i; if (!qtab) { fprintf(fd, "void\nc_chandump(int unused) "); fprintf(fd, "{ unused++; /* avoid complaints */ }\n"); return; } fprintf(fd, "void\nc_chandump(int from)\n"); fprintf(fd, "{ uchar *z; int slot;\n"); fprintf(fd, " from--;\n"); fprintf(fd, " if (from >= (int) now._nr_qs || from < 0)\n"); fprintf(fd, " { printf(\"pan: bad qid %%d\\n\", from+1);\n"); fprintf(fd, " return;\n"); fprintf(fd, " }\n"); fprintf(fd, " z = qptr(from);\n"); fprintf(fd, " switch (((Q0 *)z)->_t) {\n"); for (q = qtab; q; q = q->nxt) { fprintf(fd, " case %d:\n\t\t", q->qid); sprintf(buf, "((Q%d *)z)->", q->qid); fprintf(fd, "for (slot = 0; slot < %sQlen; slot++)\n\t\t", buf); fprintf(fd, "{ printf(\" [\");\n\t\t"); for (i = 0; i < q->nflds; i++) { if (q->fld_width[i] == MTYPE) { fprintf(fd, "\tprintm(%scontents[slot].fld%d);\n\t\t", buf, i); } else fprintf(fd, "\tprintf(\"%%d,\", %scontents[slot].fld%d);\n\t\t", buf, i); } fprintf(fd, " printf(\"],\");\n\t\t"); fprintf(fd, "}\n\t\t"); fprintf(fd, "break;\n"); } fprintf(fd, " }\n"); fprintf(fd, " printf(\"\\n\");\n}\n");}voidc_var(FILE *fd, char *pref, Symbol *sp){ char buf[256]; int i; switch (sp->type) { case STRUCT: /* c_struct(fd, pref, sp); */ fprintf(fd, "\t\tprintf(\"\t(struct %s)\\n\");\n", sp->name); sprintf(buf, "%s%s.", pref, sp->name); c_struct(fd, buf, sp); break; case BIT: case BYTE: case SHORT: case INT: case UNSIGNED: sputtype(buf, sp->type); if (sp->nel == 1) { fprintf(fd, "\tprintf(\"\t%s %s:\t%%d\\n\", %s%s);\n", buf, sp->name, pref, sp->name); } else { fprintf(fd, "\t{\tint l_in;\n"); fprintf(fd, "\t\tfor (l_in = 0; l_in < %d; l_in++)\n", sp->nel); fprintf(fd, "\t\t{\n"); fprintf(fd, "\t\t\tprintf(\"\t%s %s[%%d]:\t%%d\\n\", l_in, %s%s[l_in]);\n", buf, sp->name, pref, sp->name); fprintf(fd, "\t\t}\n"); fprintf(fd, "\t}\n"); } break; case CHAN: if (sp->nel == 1) { fprintf(fd, "\tprintf(\"\tchan %s (=%%d):\tlen %%d:\\t\", ", sp->name); fprintf(fd, "%s%s, q_len(%s%s));\n", pref, sp->name, pref, sp->name); fprintf(fd, "\tc_chandump(%s%s);\n", pref, sp->name); } else for (i = 0; i < sp->nel; i++) { fprintf(fd, "\tprintf(\"\tchan %s[%d] (=%%d):\tlen %%d:\\t\", ", sp->name, i); fprintf(fd, "%s%s[%d], q_len(%s%s[%d]));\n", pref, sp->name, i, pref, sp->name, i); fprintf(fd, "\tc_chandump(%s%s[%d]);\n", pref, sp->name, i); } break; }}intc_splurge_any(ProcList *p){ Ordered *walk; Symbol *sp; if (strcmp(p->n->name, ":never:") != 0 && strcmp(p->n->name, ":trace:") != 0 && strcmp(p->n->name, ":notrace:") != 0) for (walk = all_names; walk; walk = walk->next) { sp = walk->entry; if (!sp->context || sp->type == 0 || strcmp(sp->context->name, p->n->name) != 0 || sp->owner || (sp->hidden&1) || (sp->type == MTYPE && ismtype(sp->name))) continue; return 1; } return 0;}voidc_splurge(FILE *fd, ProcList *p){ Ordered *walk; Symbol *sp; char pref[64]; if (strcmp(p->n->name, ":never:") != 0 && strcmp(p->n->name, ":trace:") != 0 && strcmp(p->n->name, ":notrace:") != 0) for (walk = all_names; walk; walk = walk->next) { sp = walk->entry; if (!sp->context || sp->type == 0 || strcmp(sp->context->name, p->n->name) != 0 || sp->owner || (sp->hidden&1) || (sp->type == MTYPE && ismtype(sp->name))) continue; sprintf(pref, "((P%d *)pptr(pid))->", p->tn); c_var(fd, pref, sp); }}voidc_wrapper(FILE *fd) /* allow pan.c to print out global sv entries */{ Ordered *walk; ProcList *p; Symbol *sp; Lextok *n; extern Lextok *Mtype; int j; fprintf(fd, "void\nc_globals(void)\n{\t/* int i; */\n"); fprintf(fd, " printf(\"global vars:\\n\");\n"); for (walk = all_names; walk; walk = walk->next) { sp = walk->entry; if (sp->context || sp->owner || (sp->hidden&1) || (sp->type == MTYPE && ismtype(sp->name))) continue; c_var(fd, "now.", sp); } fprintf(fd, "}\n"); fprintf(fd, "void\nc_locals(int pid, int tp)\n{\t/* int i; */\n"); fprintf(fd, " switch(tp) {\n"); for (p = rdy; p; p = p->nxt) { fprintf(fd, " case %d:\n", p->tn); if (c_splurge_any(p)) { fprintf(fd, " \tprintf(\"local vars proc %%d (%s):\\n\", pid);\n", p->n->name); c_splurge(fd, p); } else { fprintf(fd, " \t/* none */\n"); } fprintf(fd, " \tbreak;\n"); } fprintf(fd, " }\n}\n"); fprintf(fd, "void\nprintm(int x)\n{\n"); fprintf(fd, " switch (x) {\n"); for (n = Mtype, j = 1; n && j; n = n->rgt, j++) fprintf(fd, "\tcase %d: Printf(\"%s\"); break;\n", j, n->lft->sym->name); fprintf(fd, " default: Printf(\"%%d\", x);\n"); fprintf(fd, " }\n"); fprintf(fd, "}\n");}static intdoglobal(char *pre, int dowhat){ Ordered *walk; Symbol *sp; int j, cnt = 0; for (j = 0; j < 8; j++) for (walk = all_names; walk; walk = walk->next) { sp = walk->entry; if (!sp->context && !sp->owner && sp->type == Types[j]) { if (Types[j] != MTYPE || !ismtype(sp->name)) switch (dowhat) { case LOGV: if (sp->type == CHAN && verbose == 0) break; if (sp->hidden&1) break; do_var(tc, dowhat, "", sp, pre, "\", now.", ");\n"); break; case INIV: checktype(sp, (char *) 0); cnt++; /* fall through */ case PUTV: do_var(tc, dowhat, (sp->hidden&1)?"":"now.", sp, "", " = ", ";\n"); break; } } } return cnt;}static voiddohidden(void){ Ordered *walk; Symbol *sp; int j; for (j = 0; j < 8; j++) for (walk = all_names; walk; walk = walk->next) { sp = walk->entry; if ((sp->hidden&1) && sp->type == Types[j]) { if (sp->context || sp->owner) fatal("cannot hide non-globals (%s)", sp->name); if (sp->type == CHAN) fatal("cannot hide channels (%s)", sp->name); fprintf(th, "/* hidden variable: */"); typ2c(sp); } } fprintf(th, "int _; /* a predefined write-only variable */\n\n");}voiddo_var(FILE *ofd, int dowhat, char *s, Symbol *sp, char *pre, char *sep, char *ter){ int i; switch(dowhat) { case PUTV: if (sp->hidden&1) break; typ2c(sp); break; case LOGV: case INIV: if (sp->type == STRUCT) { /* struct may contain a chan */ walk_struct(ofd, dowhat, s, sp, pre, sep, ter); break; } if (!sp->ini && dowhat != LOGV) /* it defaults to 0 */ break; if (sp->nel == 1) { fprintf(ofd, "\t\t%s%s%s%s", pre, s, sp->name, sep); if (dowhat == LOGV) fprintf(ofd, "%s%s", s, sp->name); else do_init(ofd, sp); fprintf(ofd, "%s", ter); } else { if (sp->ini && sp->ini->ntyp == CHAN) { for (i = 0; i < sp->nel; i++) { fprintf(ofd, "\t\t%s%s%s[%d]%s", pre, s, sp->name, i, sep); if (dowhat == LOGV) fprintf(ofd, "%s%s[%d]", s, sp->name, i); else do_init(ofd, sp); fprintf(ofd, "%s", ter); } } else { fprintf(ofd, "\t{\tint l_in;\n"); fprintf(ofd, "\t\tfor (l_in = 0; l_in < %d; l_in++)\n", sp->nel); fprintf(ofd, "\t\t{\n"); fprintf(ofd, "\t\t\t%s%s%s[l_in]%s", pre, s, sp->name, sep); if (dowhat == LOGV) fprintf(ofd, "%s%s[l_in]", s, sp->name); else putstmnt(ofd, sp->ini, 0); fprintf(ofd, "%s", ter); fprintf(ofd, "\t\t}\n"); fprintf(ofd, "\t}\n"); } } break; }}static voiddo_init(FILE *ofd, Symbol *sp){ int i; if (sp->ini && sp->type == CHAN && ((i = qmake(sp)) > 0)) { if (sp->ini->ntyp == CHAN) fprintf(ofd, "addqueue(%d, %d)", i, ltab[i-1]->nslots == 0); else fprintf(ofd, "%d", i); } else putstmnt(ofd, sp->ini, 0);}static intblog(int n) /* for small log2 without rounding problems */{ int m=1, r=2; while (r < n) { m++; r *= 2; } return 1+m;}static voidput_ptype(char *s, int i, int m0, int m1){ int k; if (strcmp(s, ":init:") == 0) fprintf(th, "#define Pinit ((P%d *)this)\n", i); if (strcmp(s, ":never:") != 0 && strcmp(s, ":trace:") != 0 && strcmp(s, ":notrace:") != 0 && strcmp(s, ":init:") != 0 && strcmp(s, "_:never_template:_") != 0 && strcmp(s, "np_") != 0) fprintf(th, "#define P%s ((P%d *)this)\n", s, i); fprintf(th, "typedef struct P%d { /* %s */\n", i, s); fprintf(th, " unsigned _pid : 8; /* 0..255 */\n"); fprintf(th, " unsigned _t : %d; /* proctype */\n", blog(m1)); fprintf(th, " unsigned _p : %d; /* state */\n", blog(m0)); LstSet = ZS; nBits = 8 + blog(m1) + blog(m0); k = dolocal(tc, "", PUTV, i, s); /* includes pars */ c_add_loc(th, s); fprintf(th, "} P%d;\n", i); if ((!LstSet && k > 0) || has_state) fprintf(th, "#define Air%d 0\n", i); else if (LstSet || k == 0) /* 5.0, added condition */ { fprintf(th, "#define Air%d (sizeof(P%d) - ", i, i); if (k == 0) { fprintf(th, "%d", (nBits+7)/8); goto done; } if ((LstSet->type != BIT && LstSet->type != UNSIGNED) || LstSet->nel != 1) { fprintf(th, "Offsetof(P%d, %s) - %d*sizeof(", i, LstSet->name, LstSet->nel); } switch(LstSet->type) { case UNSIGNED: fprintf(th, "%d", (nBits+7)/8); break; case BIT: if (LstSet->nel == 1) { fprintf(th, "%d", (nBits+7)/8); break; } /* else fall through */ case MTYPE: case BYTE: case CHAN: fprintf(th, "uchar)"); break; case SHORT: fprintf(th, "short)"); break; case INT: fprintf(th, "int)"); break; default: fatal("cannot happen Air %s", LstSet->name); }done: fprintf(th, ")\n"); }}static voidtc_predef_np(void){ int i = nrRdy; /* 1+ highest proctype nr */ fprintf(th, "#define _NP_ %d\n", i);/* if (separate == 2) fprintf(th, "extern "); */ fprintf(th, "uchar reached%d[3]; /* np_ */\n", i); fprintf(th, "uchar *loopstate%d; /* np_ */\n", i); fprintf(th, "#define nstates%d 3 /* np_ */\n", i); fprintf(th, "#define endstate%d 2 /* np_ */\n\n", i); fprintf(th, "#define start%d 0 /* np_ */\n", i); fprintf(tc, "\tcase %d: /* np_ */\n", i); if (separate == 1) { fprintf(tc, "\t\tini_claim(%d, h);\n", i); } else { fprintf(tc, "\t\t((P%d *)pptr(h))->_t = %d;\n", i, i); fprintf(tc, "\t\t((P%d *)pptr(h))->_p = 0;\n", i); fprintf(tc, "\t\treached%d[0] = 1;\n", i); fprintf(tc, "\t\taccpstate[%d][1] = 1;\n", i); } fprintf(tc, "\t\tbreak;\n");}static voidput_pinit(ProcList *P){ Lextok *fp, *fpt, *t; Element *e = P->s->frst; Symbol *s = P->n; Lextok *p = P->p; int i = P->tn; int ini, j, k; if (i == claimnr && separate == 1) { fprintf(tc, "\tcase %d: /* %s */\n", i, s->name); fprintf(tc, "\t\tini_claim(%d, h);\n", i); fprintf(tc, "\t\tbreak;\n"); return; } if (i != claimnr && separate == 2) return;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -