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); ...@@ -66,7 +66,7 @@ static TAILQ_HEAD(, vclprog) vclhead = TAILQ_HEAD_INITIALIZER(vclhead);
/* keep this in synch with man/vcl.7 */ /* keep this in synch with man/vcl.7 */
static const char *default_vcl = static const char *default_vcl =
"sub default_vcl_recv {\n" "sub vcl_recv {\n"
" if (req.request != \"GET\" && req.request != \"HEAD\") {\n" " if (req.request != \"GET\" && req.request != \"HEAD\") {\n"
" pipe;\n" " pipe;\n"
" }\n" " }\n"
...@@ -79,30 +79,30 @@ static const char *default_vcl = ...@@ -79,30 +79,30 @@ static const char *default_vcl =
" lookup;\n" " lookup;\n"
"}\n" "}\n"
"\n" "\n"
"sub default_vcl_pipe {\n" "sub vcl_pipe {\n"
" pipe;\n" " pipe;\n"
"}\n" "}\n"
"\n" "\n"
"sub default_vcl_pass {\n" "sub vcl_pass {\n"
" pass;\n" " pass;\n"
"}\n" "}\n"
"\n" "\n"
"sub default_vcl_hash {\n" "sub vcl_hash {\n"
" hash;\n" " hash;\n"
"}\n" "}\n"
"\n" "\n"
"sub default_vcl_hit {\n" "sub vcl_hit {\n"
" if (!obj.cacheable) {\n" " if (!obj.cacheable) {\n"
" pass;\n" " pass;\n"
" }\n" " }\n"
" deliver;\n" " deliver;\n"
"}\n" "}\n"
"\n" "\n"
"sub default_vcl_miss {\n" "sub vcl_miss {\n"
" fetch;\n" " fetch;\n"
"}\n" "}\n"
"\n" "\n"
"sub default_vcl_fetch {\n" "sub vcl_fetch {\n"
" if (!obj.valid) {\n" " if (!obj.valid) {\n"
" error;\n" " error;\n"
" }\n" " }\n"
...@@ -114,7 +114,7 @@ static const char *default_vcl = ...@@ -114,7 +114,7 @@ static const char *default_vcl =
" }\n" " }\n"
" insert;\n" " insert;\n"
"}\n" "}\n"
"sub default_vcl_timeout {\n" "sub vcl_timeout {\n"
" discard;\n" " discard;\n"
"}\n"; "}\n";
......
...@@ -41,3 +41,4 @@ VCL_MET_MAC(hit,HIT,(VCL_RET_ERROR|VCL_RET_PASS|VCL_RET_DELIVER)) ...@@ -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(fetch,FETCH,(VCL_RET_ERROR|VCL_RET_PASS|VCL_RET_INSERT))
VCL_MET_MAC(timeout,TIMEOUT,(VCL_RET_FETCH|VCL_RET_DISCARD)) VCL_MET_MAC(timeout,TIMEOUT,(VCL_RET_FETCH|VCL_RET_DISCARD))
#endif #endif
#define N_METHODS 8
...@@ -87,7 +87,7 @@ ...@@ -87,7 +87,7 @@
static struct method method_tab[] = { static struct method method_tab[] = {
#define VCL_RET_MAC(l,U,b,n) #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" #include "vcl_returns.h"
#undef VCL_MET_MAC #undef VCL_MET_MAC
#undef VCL_RET_MAC #undef VCL_RET_MAC
...@@ -96,7 +96,21 @@ static struct method method_tab[] = { ...@@ -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 * Printf output to the two vsbs, possibly indented
...@@ -114,6 +128,19 @@ Fh(struct tokenlist *tl, int indent, const char *fmt, ...) ...@@ -114,6 +128,19 @@ Fh(struct tokenlist *tl, int indent, const char *fmt, ...)
va_end(ap); 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 void
Fc(struct tokenlist *tl, int indent, const char *fmt, ...) Fc(struct tokenlist *tl, int indent, const char *fmt, ...)
{ {
...@@ -152,8 +179,8 @@ Ff(struct tokenlist *tl, int indent, const char *fmt, ...) ...@@ -152,8 +179,8 @@ Ff(struct tokenlist *tl, int indent, const char *fmt, ...)
/*--------------------------------------------------------------------*/ /*--------------------------------------------------------------------*/
void static void
EncString(struct vsb *sb, const char *b, const char *e) EncString(struct vsb *sb, const char *b, const char *e, int mode)
{ {
if (e == NULL) if (e == NULL)
...@@ -166,7 +193,11 @@ EncString(struct vsb *sb, const char *b, const char *e) ...@@ -166,7 +193,11 @@ EncString(struct vsb *sb, const char *b, const char *e)
case '"': case '"':
vsb_printf(sb, "\\%c", *b); vsb_printf(sb, "\\%c", *b);
break; 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 '\t': vsb_printf(sb, "\\t"); break;
case '\r': vsb_printf(sb, "\\r"); break; case '\r': vsb_printf(sb, "\\r"); break;
case ' ': vsb_printf(sb, " "); break; case ' ': vsb_printf(sb, " "); break;
...@@ -186,7 +217,7 @@ EncToken(struct vsb *sb, struct token *t) ...@@ -186,7 +217,7 @@ EncToken(struct vsb *sb, struct token *t)
{ {
assert(t->tok == CSTR); 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) ...@@ -212,20 +243,6 @@ FindRef(struct tokenlist *tl, struct token *t, enum ref_type type)
return (r); 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 void
AddRef(struct tokenlist *tl, struct token *t, enum ref_type type) 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) ...@@ -233,20 +250,6 @@ AddRef(struct tokenlist *tl, struct token *t, enum ref_type type)
FindRef(tl, t, type)->refcnt++; 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 void
AddDef(struct tokenlist *tl, struct token *t, enum ref_type type) AddDef(struct tokenlist *tl, struct token *t, enum ref_type type)
{ {
...@@ -407,8 +410,6 @@ Consistency(struct tokenlist *tl) ...@@ -407,8 +410,6 @@ Consistency(struct tokenlist *tl)
TAILQ_FOREACH(p, &tl->procs, list) { TAILQ_FOREACH(p, &tl->procs, list) {
for(m = method_tab; m->name != NULL; m++) { 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)) if (vcc_IdIs(p->name, m->name))
break; break;
} }
...@@ -564,15 +565,18 @@ EmitStruct(struct tokenlist *tl) ...@@ -564,15 +565,18 @@ EmitStruct(struct tokenlist *tl)
Fc(tl, 0, "\nconst char *srcname[%u] = {\n", tl->nsources); Fc(tl, 0, "\nconst char *srcname[%u] = {\n", tl->nsources);
TAILQ_FOREACH(sp, &tl->sources, list) { TAILQ_FOREACH(sp, &tl->sources, list) {
Fc(tl, 0, "\t"); 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, "};\n"); Fc(tl, 0, "};\n");
Fc(tl, 0, "\nconst char *srcbody[%u] = {\n", tl->nsources); Fc(tl, 0, "\nconst char *srcbody[%u] = {\n", tl->nsources);
TAILQ_FOREACH(sp, &tl->sources, list) { TAILQ_FOREACH(sp, &tl->sources, list) {
Fc(tl, 0, " /* ");
EncString(tl->fc, sp->name, NULL, 0);
Fc(tl, 0, "*/\n");
Fc(tl, 0, "\t"); 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");
} }
Fc(tl, 0, "};\n"); Fc(tl, 0, "};\n");
...@@ -589,13 +593,7 @@ EmitStruct(struct tokenlist *tl) ...@@ -589,13 +593,7 @@ EmitStruct(struct tokenlist *tl)
Fc(tl, 0, "\t.srcbody = srcbody,\n"); Fc(tl, 0, "\t.srcbody = srcbody,\n");
#define VCL_RET_MAC(l,u,b,n) #define VCL_RET_MAC(l,u,b,n)
#define VCL_MET_MAC(l,u,b) \ #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");
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);
#include "vcl_returns.h" #include "vcl_returns.h"
#undef VCL_MET_MAC #undef VCL_MET_MAC
#undef VCL_RET_MAC #undef VCL_RET_MAC
...@@ -744,11 +742,10 @@ vcc_CompileSource(struct vsb *sb, struct source *sp) ...@@ -744,11 +742,10 @@ vcc_CompileSource(struct vsb *sb, struct source *sp)
assert(tl->ff != NULL); assert(tl->ff != NULL);
/* body code of methods */ /* body code of methods */
#define VCL_MET_MAC(l,U,m) \ for (i = 0; i < N_METHODS; i++) {
tl->fm_##l = vsb_new(NULL, NULL, 0, VSB_AUTOEXTEND); \ tl->fm[i] = vsb_new(NULL, NULL, 0, VSB_AUTOEXTEND); \
assert(tl->fm_##l != NULL); assert(tl->fm[i] != NULL);
#include "vcl_returns.h" }
#undef VCL_MET_MAC
Fh(tl, 0, "extern struct VCL_conf VCL_conf;\n"); Fh(tl, 0, "extern struct VCL_conf VCL_conf;\n");
...@@ -779,6 +776,18 @@ vcc_CompileSource(struct vsb *sb, struct source *sp) ...@@ -779,6 +776,18 @@ vcc_CompileSource(struct vsb *sb, struct source *sp)
Consistency(tl); Consistency(tl);
if (tl->err) if (tl->err)
goto done; 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); LocTable(tl);
Ff(tl, 0, "\tVRT_free_backends(&VCL_conf);\n"); Ff(tl, 0, "\tVRT_free_backends(&VCL_conf);\n");
...@@ -824,9 +833,8 @@ vcc_CompileSource(struct vsb *sb, struct source *sp) ...@@ -824,9 +833,8 @@ vcc_CompileSource(struct vsb *sb, struct source *sp)
} }
done: done:
#define VCL_MET_MAC(l,U,m) vsb_delete(tl->fm_##l); for (i = 0; i < N_METHODS; i++)
#include "vcl_returns.h" vsb_delete(tl->fm[i]);
#undef VCL_MET_MAC
/* Free References */ /* Free References */
while (!TAILQ_EMPTY(&tl->refs)) { while (!TAILQ_EMPTY(&tl->refs)) {
......
...@@ -62,16 +62,15 @@ struct tokenlist { ...@@ -62,16 +62,15 @@ struct tokenlist {
struct token *t; struct token *t;
int indent; int indent;
unsigned cnt; unsigned cnt;
struct vsb *fc, *fh, *fi, *ff; struct vsb *fc, *fh, *fi, *ff, *fb;
#define VCL_MET_MAC(l,U,m) struct vsb *fm_##l; struct vsb *fm[N_METHODS];
#include "vcl_returns.h"
#undef VCL_MET_MAC
TAILQ_HEAD(, ref) refs; TAILQ_HEAD(, ref) refs;
struct vsb *sb; struct vsb *sb;
int err; int err;
int nbackend; int nbackend;
TAILQ_HEAD(, proc) procs; TAILQ_HEAD(, proc) procs;
struct proc *curproc; struct proc *curproc;
struct proc *mprocs[N_METHODS];
unsigned recnt; unsigned recnt;
}; };
...@@ -115,7 +114,6 @@ struct var { ...@@ -115,7 +114,6 @@ struct var {
struct method { struct method {
const char *name; const char *name;
const char *defname;
unsigned returns; unsigned returns;
}; };
...@@ -145,19 +143,19 @@ void vcc_Acl(struct tokenlist *tl); ...@@ -145,19 +143,19 @@ void vcc_Acl(struct tokenlist *tl);
void vcc_Cond_Ip(struct var *vp, struct tokenlist *tl); void vcc_Cond_Ip(struct var *vp, struct tokenlist *tl);
/* vcc_compile.c */ /* 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 Fh(struct tokenlist *tl, int indent, const char *fmt, ...);
void Fc(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 Fi(struct tokenlist *tl, int indent, const char *fmt, ...);
void Ff(struct tokenlist *tl, int indent, const char *fmt, ...); void Ff(struct tokenlist *tl, int indent, const char *fmt, ...);
unsigned UintVal(struct tokenlist *tl); unsigned UintVal(struct tokenlist *tl);
void AddDef(struct tokenlist *tl, struct token *t, enum ref_type type); 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 AddRef(struct tokenlist *tl, struct token *t, enum ref_type type);
void EncToken(struct vsb *sb, struct token *t); 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); struct var *FindVar(struct tokenlist *tl, struct token *t, struct var *vl);
void AddCall(struct tokenlist *tl, struct token *t); void AddCall(struct tokenlist *tl, struct token *t);
struct proc *AddProc(struct tokenlist *tl, struct token *t, int def); struct proc *AddProc(struct tokenlist *tl, struct token *t, int def);
int IsMethod(struct token *t);
/* vcc_obj.c */ /* vcc_obj.c */
extern struct var vcc_be_vars[]; extern struct var vcc_be_vars[];
......
...@@ -185,6 +185,7 @@ puts $for "#define VCL_RET_MAX $i" ...@@ -185,6 +185,7 @@ puts $for "#define VCL_RET_MAX $i"
puts $for "#endif" puts $for "#endif"
puts $for "" puts $for ""
puts $for "#ifdef VCL_MET_MAC" puts $for "#ifdef VCL_MET_MAC"
set u 0
foreach m $methods { foreach m $methods {
puts -nonewline $for "VCL_MET_MAC([lindex $m 0]" puts -nonewline $for "VCL_MET_MAC([lindex $m 0]"
puts -nonewline $for ",[string toupper [lindex $m 0]]" puts -nonewline $for ",[string toupper [lindex $m 0]]"
...@@ -195,8 +196,10 @@ foreach m $methods { ...@@ -195,8 +196,10 @@ foreach m $methods {
} }
puts -nonewline $for ")" puts -nonewline $for ")"
puts $for ")" puts $for ")"
incr u
} }
puts $for "#endif" puts $for "#endif"
puts $for "#define N_METHODS $u"
close $for 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