Commit 279b60fc authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Twist the compiler logic around a bit.

Concatenate all definitions of the method functions into one
instance of the function:

	sub vcl_pipe {
		foo;
	}

	sub vcl_pipe {
		bar;
	}

is now the same as

	sub vcl_pipe {
		foo;
		bar;
	}

This avoids all the magic related to the default functions and
hopefully makes the newly introduced "include" facility much more
useful.



git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@1284 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent 29347161
......@@ -66,7 +66,7 @@ static TAILQ_HEAD(, vclprog) vclhead = TAILQ_HEAD_INITIALIZER(vclhead);
/* keep this in synch with man/vcl.7 */
static const char *default_vcl =
"sub default_vcl_recv {\n"
"sub vcl_recv {\n"
" if (req.request != \"GET\" && req.request != \"HEAD\") {\n"
" pipe;\n"
" }\n"
......@@ -79,30 +79,30 @@ static const char *default_vcl =
" lookup;\n"
"}\n"
"\n"
"sub default_vcl_pipe {\n"
"sub vcl_pipe {\n"
" pipe;\n"
"}\n"
"\n"
"sub default_vcl_pass {\n"
"sub vcl_pass {\n"
" pass;\n"
"}\n"
"\n"
"sub default_vcl_hash {\n"
"sub vcl_hash {\n"
" hash;\n"
"}\n"
"\n"
"sub default_vcl_hit {\n"
"sub vcl_hit {\n"
" if (!obj.cacheable) {\n"
" pass;\n"
" }\n"
" deliver;\n"
"}\n"
"\n"
"sub default_vcl_miss {\n"
"sub vcl_miss {\n"
" fetch;\n"
"}\n"
"\n"
"sub default_vcl_fetch {\n"
"sub vcl_fetch {\n"
" if (!obj.valid) {\n"
" error;\n"
" }\n"
......@@ -114,7 +114,7 @@ static const char *default_vcl =
" }\n"
" insert;\n"
"}\n"
"sub default_vcl_timeout {\n"
"sub vcl_timeout {\n"
" discard;\n"
"}\n";
......
......@@ -41,3 +41,4 @@ VCL_MET_MAC(hit,HIT,(VCL_RET_ERROR|VCL_RET_PASS|VCL_RET_DELIVER))
VCL_MET_MAC(fetch,FETCH,(VCL_RET_ERROR|VCL_RET_PASS|VCL_RET_INSERT))
VCL_MET_MAC(timeout,TIMEOUT,(VCL_RET_FETCH|VCL_RET_DISCARD))
#endif
#define N_METHODS 8
......@@ -87,7 +87,7 @@
static struct method method_tab[] = {
#define VCL_RET_MAC(l,U,b,n)
#define VCL_MET_MAC(l,U,m) { "vcl_"#l, "default_vcl_"#l, m },
#define VCL_MET_MAC(l,U,m) { "vcl_"#l, m },
#include "vcl_returns.h"
#undef VCL_MET_MAC
#undef VCL_RET_MAC
......@@ -96,7 +96,21 @@ static struct method method_tab[] = {
/*--------------------------------------------------------------------*/
const char *vcc_default_vcl_b, *vcc_default_vcl_e;
static const char *vcc_default_vcl_b, *vcc_default_vcl_e;
/*--------------------------------------------------------------------*/
int
IsMethod(struct token *t)
{
struct method *m;
for(m = method_tab; m->name != NULL; m++) {
if (vcc_IdIs(t, m->name))
return (m - method_tab);
}
return (-1);
}
/*--------------------------------------------------------------------
* Printf output to the two vsbs, possibly indented
......@@ -114,6 +128,19 @@ Fh(struct tokenlist *tl, int indent, const char *fmt, ...)
va_end(ap);
}
void
Fb(struct tokenlist *tl, int indent, const char *fmt, ...)
{
va_list ap;
assert(tl->fb != NULL);
if (indent)
vsb_printf(tl->fb, "%*.*s", tl->indent, tl->indent, "");
va_start(ap, fmt);
vsb_vprintf(tl->fb, fmt, ap);
va_end(ap);
}
void
Fc(struct tokenlist *tl, int indent, const char *fmt, ...)
{
......@@ -152,8 +179,8 @@ Ff(struct tokenlist *tl, int indent, const char *fmt, ...)
/*--------------------------------------------------------------------*/
void
EncString(struct vsb *sb, const char *b, const char *e)
static void
EncString(struct vsb *sb, const char *b, const char *e, int mode)
{
if (e == NULL)
......@@ -166,7 +193,11 @@ EncString(struct vsb *sb, const char *b, const char *e)
case '"':
vsb_printf(sb, "\\%c", *b);
break;
case '\n': vsb_printf(sb, "\\n"); break;
case '\n':
vsb_printf(sb, "\\n");
if (mode)
vsb_printf(sb, "\"\n\t\"");
break;
case '\t': vsb_printf(sb, "\\t"); break;
case '\r': vsb_printf(sb, "\\r"); break;
case ' ': vsb_printf(sb, " "); break;
......@@ -186,7 +217,7 @@ EncToken(struct vsb *sb, struct token *t)
{
assert(t->tok == CSTR);
EncString(sb, t->dec, NULL);
EncString(sb, t->dec, NULL, 0);
}
/*--------------------------------------------------------------------
......@@ -212,20 +243,6 @@ FindRef(struct tokenlist *tl, struct token *t, enum ref_type type)
return (r);
}
static int
FindRefStr(struct tokenlist *tl, const char *s, enum ref_type type)
{
struct ref *r;
TAILQ_FOREACH(r, &tl->refs, list) {
if (r->type != type)
continue;
if (vcc_IdIs(r->name, s))
return (1);
}
return (0);
}
void
AddRef(struct tokenlist *tl, struct token *t, enum ref_type type)
{
......@@ -233,20 +250,6 @@ AddRef(struct tokenlist *tl, struct token *t, enum ref_type type)
FindRef(tl, t, type)->refcnt++;
}
static void
AddRefStr(struct tokenlist *tl, const char *s, enum ref_type type)
{
struct token *t;
t = calloc(sizeof *t, 1);
assert(t != NULL);
t->b = s;
t->e = strchr(s, '\0');
t->tok = METHOD;
AddRef(tl, t, type);
/* XXX: possibly leaking t */
}
void
AddDef(struct tokenlist *tl, struct token *t, enum ref_type type)
{
......@@ -407,8 +410,6 @@ Consistency(struct tokenlist *tl)
TAILQ_FOREACH(p, &tl->procs, list) {
for(m = method_tab; m->name != NULL; m++) {
if (vcc_IdIs(p->name, m->defname))
p->called = 1;
if (vcc_IdIs(p->name, m->name))
break;
}
......@@ -564,15 +565,18 @@ EmitStruct(struct tokenlist *tl)
Fc(tl, 0, "\nconst char *srcname[%u] = {\n", tl->nsources);
TAILQ_FOREACH(sp, &tl->sources, list) {
Fc(tl, 0, "\t");
EncString(tl->fc, sp->name, NULL);
EncString(tl->fc, sp->name, NULL, 0);
Fc(tl, 0, ",\n");
}
Fc(tl, 0, "};\n");
Fc(tl, 0, "\nconst char *srcbody[%u] = {\n", tl->nsources);
TAILQ_FOREACH(sp, &tl->sources, list) {
Fc(tl, 0, " /* ");
EncString(tl->fc, sp->name, NULL, 0);
Fc(tl, 0, "*/\n");
Fc(tl, 0, "\t");
EncString(tl->fc, sp->b, sp->e);
EncString(tl->fc, sp->b, sp->e, 1);
Fc(tl, 0, ",\n");
}
Fc(tl, 0, "};\n");
......@@ -589,13 +593,7 @@ EmitStruct(struct tokenlist *tl)
Fc(tl, 0, "\t.srcbody = srcbody,\n");
#define VCL_RET_MAC(l,u,b,n)
#define VCL_MET_MAC(l,u,b) \
if (FindRefStr(tl, "vcl_" #l, R_FUNC)) { \
Fc(tl, 0, "\t." #l "_func = VGC_function_vcl_" #l ",\n"); \
AddRefStr(tl, "vcl_" #l, R_FUNC); \
} else { \
Fc(tl, 0, "\t." #l "_func = VGC_function_default_vcl_" #l ",\n"); \
} \
AddRefStr(tl, "default_vcl_" #l, R_FUNC);
Fc(tl, 0, "\t." #l "_func = VGC_function_vcl_" #l ",\n");
#include "vcl_returns.h"
#undef VCL_MET_MAC
#undef VCL_RET_MAC
......@@ -744,11 +742,10 @@ vcc_CompileSource(struct vsb *sb, struct source *sp)
assert(tl->ff != NULL);
/* body code of methods */
#define VCL_MET_MAC(l,U,m) \
tl->fm_##l = vsb_new(NULL, NULL, 0, VSB_AUTOEXTEND); \
assert(tl->fm_##l != NULL);
#include "vcl_returns.h"
#undef VCL_MET_MAC
for (i = 0; i < N_METHODS; i++) {
tl->fm[i] = vsb_new(NULL, NULL, 0, VSB_AUTOEXTEND); \
assert(tl->fm[i] != NULL);
}
Fh(tl, 0, "extern struct VCL_conf VCL_conf;\n");
......@@ -779,6 +776,18 @@ vcc_CompileSource(struct vsb *sb, struct source *sp)
Consistency(tl);
if (tl->err)
goto done;
/* Emit method functions */
for (i = 0; i < N_METHODS; i++) {
Fc(tl, 1, "static int\n");
Fc(tl, 1, "VGC_function_%s (struct sess *sp)\n",
method_tab[i].name);
vsb_finish(tl->fm[i]);
Fc(tl, 1, "{\n");
Fc(tl, 1, "%s", vsb_data(tl->fm[i]));
Fc(tl, 1, "}\n\n");
}
LocTable(tl);
Ff(tl, 0, "\tVRT_free_backends(&VCL_conf);\n");
......@@ -824,9 +833,8 @@ vcc_CompileSource(struct vsb *sb, struct source *sp)
}
done:
#define VCL_MET_MAC(l,U,m) vsb_delete(tl->fm_##l);
#include "vcl_returns.h"
#undef VCL_MET_MAC
for (i = 0; i < N_METHODS; i++)
vsb_delete(tl->fm[i]);
/* Free References */
while (!TAILQ_EMPTY(&tl->refs)) {
......
......@@ -62,16 +62,15 @@ struct tokenlist {
struct token *t;
int indent;
unsigned cnt;
struct vsb *fc, *fh, *fi, *ff;
#define VCL_MET_MAC(l,U,m) struct vsb *fm_##l;
#include "vcl_returns.h"
#undef VCL_MET_MAC
struct vsb *fc, *fh, *fi, *ff, *fb;
struct vsb *fm[N_METHODS];
TAILQ_HEAD(, ref) refs;
struct vsb *sb;
int err;
int nbackend;
TAILQ_HEAD(, proc) procs;
struct proc *curproc;
struct proc *mprocs[N_METHODS];
unsigned recnt;
};
......@@ -115,7 +114,6 @@ struct var {
struct method {
const char *name;
const char *defname;
unsigned returns;
};
......@@ -145,19 +143,19 @@ void vcc_Acl(struct tokenlist *tl);
void vcc_Cond_Ip(struct var *vp, struct tokenlist *tl);
/* vcc_compile.c */
extern const char *vcc_default_vcl_b, *vcc_default_vcl_e;
void Fh(struct tokenlist *tl, int indent, const char *fmt, ...);
void Fc(struct tokenlist *tl, int indent, const char *fmt, ...);
void Fb(struct tokenlist *tl, int indent, const char *fmt, ...);
void Fi(struct tokenlist *tl, int indent, const char *fmt, ...);
void Ff(struct tokenlist *tl, int indent, const char *fmt, ...);
unsigned UintVal(struct tokenlist *tl);
void AddDef(struct tokenlist *tl, struct token *t, enum ref_type type);
void AddRef(struct tokenlist *tl, struct token *t, enum ref_type type);
void EncToken(struct vsb *sb, struct token *t);
void EncString(struct vsb *sb, const char *b, const char *e);
struct var *FindVar(struct tokenlist *tl, struct token *t, struct var *vl);
void AddCall(struct tokenlist *tl, struct token *t);
struct proc *AddProc(struct tokenlist *tl, struct token *t, int def);
int IsMethod(struct token *t);
/* vcc_obj.c */
extern struct var vcc_be_vars[];
......
......@@ -185,6 +185,7 @@ puts $for "#define VCL_RET_MAX $i"
puts $for "#endif"
puts $for ""
puts $for "#ifdef VCL_MET_MAC"
set u 0
foreach m $methods {
puts -nonewline $for "VCL_MET_MAC([lindex $m 0]"
puts -nonewline $for ",[string toupper [lindex $m 0]]"
......@@ -195,8 +196,10 @@ foreach m $methods {
}
puts -nonewline $for ")"
puts $for ")"
incr u
}
puts $for "#endif"
puts $for "#define N_METHODS $u"
close $for
#----------------------------------------------------------------------
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment