varnish-cache/lib/libvcc/vcc_compile.c
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 Varnish Software AS
3
 * All rights reserved.
4
 *
5
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 * 1. Redistributions of source code must retain the above copyright
13
 *    notice, this list of conditions and the following disclaimer.
14
 * 2. Redistributions in binary form must reproduce the above copyright
15
 *    notice, this list of conditions and the following disclaimer in the
16
 *    documentation and/or other materials provided with the distribution.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
22
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28
 * SUCH DAMAGE.
29
 */
30
31
/*
32
 * XXX:
33
 *      Better error messages, throughout.
34
 *      >It also occurred to me that we could link the errors to the error
35
 *      >documentation.
36
 *      >
37
 *      >Unreferenced  function 'request_policy', first mention is
38
 *      >         Line 8 Pos 4
39
 *      >         sub request_policy {
40
 *      >         ----##############--
41
 *      >Read more about this type of error:
42
 *      >http://varnish/doc/error.html#Unreferenced%20function
43
 *      >
44
 *      >
45
 *      >         Unknown variable 'obj.bandwidth'
46
 *      >         At: Line 88 Pos 12
47
 *      >                 if (obj.bandwidth < 1 kb/h) {
48
 *      >         ------------#############------------
49
 *      >Read more about this type of error:
50
 *      >http://varnish/doc/error.html#Unknown%20variable
51
 *
52
 */
53
54
#include "config.h"
55
56
#include <fcntl.h>
57
#include <stdarg.h>
58
#include <stdlib.h>
59
#include <string.h>
60
#include <unistd.h>
61
62
#include "vcc_compile.h"
63
64
#include "libvcc.h"
65
#include "vfil.h"
66
#include "vct.h"
67
68
static const struct method method_tab[] = {
69
        { "none", 0U, 0},
70
#define VCL_MET_MAC(l,U,t,b)    { "vcl_"#l, b, VCL_MET_##U },
71
#include "tbl/vcl_returns.h"
72
        { NULL, 0U, 0}
73
};
74
75
struct vcc *vcc_builtin;
76
77
/*--------------------------------------------------------------------*/
78
79
static void
80 533104
vcc_vcl_met2c(struct vsb *vsb, unsigned method)
81
{
82 533104
        int d = 0;
83
84
        //lint -e{774} Boolean within 'if' always evaluates to False
85
#define VCL_MET_MAC(l,U,t,b)                            \
86
        do {                                            \
87
                if (method & VCL_MET_##U) {             \
88
                        if (d)                          \
89
                                VSB_putc(vsb, '|');     \
90
                        VSB_cat(vsb, "VCL_MET_" #U);    \
91
                        d = 1;                          \
92
                }                                       \
93
        } while (0);
94
#include "tbl/vcl_returns.h"
95 533104
        AN(d);
96 533104
}
97
98
99
/*--------------------------------------------------------------------*/
100
101
void * v_matchproto_(TlAlloc)
102 43480879
TlAlloc(struct vcc *tl, unsigned len)
103
{
104
        void *p;
105
106 43480879
        (void)tl;
107 43480879
        p = calloc(1, len);
108 43480879
        assert(p != NULL);
109 43480879
        return (p);
110
}
111
112 548314
char *
113 2218398
TlDup(struct vcc *tl, const char *s)
114
{
115
        char *p;
116
117 2218398
        p = TlAlloc(tl, strlen(s) + 1);
118 2218398
        AN(p);
119 2766712
        strcpy(p, s);
120 2218398
        return (p);
121
}
122
123
static int
124 30420
TLWriteVSB(struct vcc *tl, const char *fn, const struct vsb *vsb,
125
    const char *what)
126 548314
{
127
        int fo;
128
        int i;
129
130 30420
        fo = open(fn, O_WRONLY|O_TRUNC|O_CREAT, 0600);
131 30420
        if (fo < 0) {
132 0
                VSB_printf(tl->sb,
133
                    "Could not open %s file %s: %s\n",
134 0
                    what, fn, strerror(errno));
135 548314
                return (-1);
136
        }
137 30420
        i = VSB_tofile(vsb, fo);
138 30420
        if (i) {
139 0
                VSB_printf(tl->sb,
140
                    "Could not write %s to %s: %s\n",
141 0
                    what, fn, strerror(errno));
142 548314
        }
143 30420
        closefd(&fo);
144 30420
        return (i);
145 30420
}
146 548314
147
/*--------------------------------------------------------------------*/
148
149
struct proc *
150 1265069
vcc_NewProc(struct vcc *tl, struct symbol *sym)
151 548314
{
152
        struct proc *p;
153
154 1265069
        ALLOC_OBJ(p, PROC_MAGIC);
155 1265069
        AN(p);
156 1265069
        VTAILQ_INIT(&p->calls);
157 1265069
        VTAILQ_INIT(&p->uses);
158 1265069
        VTAILQ_INIT(&p->priv_tasks);
159 1813383
        VTAILQ_INIT(&p->priv_tops);
160 1265069
        VTAILQ_INSERT_TAIL(&tl->procs, p, list);
161 1265069
        p->prologue = VSB_new_auto();
162 1265069
        AN(p->prologue);
163 1265069
        p->body = VSB_new_auto();
164 1813383
        AN(p->body);
165 1265069
        p->cname = VSB_new_auto();
166 1265069
        AN(p->cname);
167 1265069
        p->okmask = VCL_MET_TASK_ALL;
168 1265069
        sym->proc = p;
169 1265069
        p->sym = sym;
170 1265069
        return (p);
171
}
172 548314
173
static void
174 533104
vcc_EmitProc(struct vcc *tl, struct proc *p)
175
{
176
        struct vsb *vsbm;
177
        unsigned mask, nsub;
178
        const char *maskcmp;
179 548314
        const char *cc_adv;
180 533104
        int dyn = (p->sym->nref > p->called);
181
182 533104
        AN(p->okmask);
183 533104
        AZ(VSB_finish(p->cname));
184 533104
        AZ(VSB_finish(p->prologue));
185 1081418
        AZ(VSB_finish(p->body));
186 533104
        AN(p->sym);
187
188 533104
        if (p->method) {
189 212940
                mask = p->method->bitval;
190 212940
                maskcmp = "==";
191 761254
        } else {
192 320164
                mask = p->okmask;
193 320164
                maskcmp = "&";
194
        }
195
196 533104
        if (dyn == 0 && (p->calledfrom & VCL_MET_TASK_H) == p->calledfrom)
197 30576
                cc_adv = "v_dont_optimize ";
198
        else
199 502528
                cc_adv = "";
200
201 533104
        nsub = tl->nsub++;
202 548314
203 533104
        Fh(tl, 1, "vcl_func_f %s;\n", VSB_data(p->cname));
204 1066208
        Fh(tl, 1, "extern const struct vcl_sub sub_%s[1];\n",
205 533104
            VSB_data(p->cname));
206 533104
        Fh(tl, 1, "const struct vcl_sub sub_%s[1] = {{\n", VSB_data(p->cname));
207 533104
        Fh(tl, 1, "\t.magic\t\t= VCL_SUB_MAGIC,\n");
208 533104
        Fh(tl, 1, "\t.methods\t= 0x%x,\n", p->okmask);
209 533104
        Fh(tl, 1, "\t.name\t\t= \"%.*s\",\n", PF(p->name));
210 533104
        Fh(tl, 1, "\t.vcl_conf\t= &VCL_conf,\n");
211 533104
        Fh(tl, 1, "\t.func\t\t= %s,\n", VSB_data(p->cname));
212 533104
        Fh(tl, 1, "\t.n\t\t= %d,\n", nsub);
213 533104
        Fh(tl, 1, "\t.nref\t\t= %d,\n", p->sym->nref);
214 533104
        Fh(tl, 1, "\t.called\t\t= %d\n", p->called);
215 533104
        Fh(tl, 1, "\t// calledfrom\t  0x%x\n", p->calledfrom);
216 533104
        Fh(tl, 1, "}};\n");
217
218 533104
        if (dyn) {
219 364
                Fc(tl, 1, "\nstatic inline void %s\n", cc_adv);
220 364
                Fc(tl, 1, "%s_checked(VRT_CTX)\n{\n", VSB_data(p->cname));
221 364
        } else {
222 532740
                Fc(tl, 1, "\nvoid %sv_matchproto_(vcl_func_f)\n", cc_adv);
223 1065480
                Fc(tl, 1, "%s(VRT_CTX, enum vcl_func_call_e call,\n",
224 532740
                    VSB_data(p->cname));
225 532740
                Fc(tl, 1, "    enum vcl_func_fail_e *failp)\n{\n");
226 532740
                Fc(tl, 1, "  assert(call == VSUB_STATIC);\n");
227 532740
                Fc(tl, 1, "  assert(failp == NULL);\n");
228
        }
229
230 533104
        vsbm = VSB_new_auto();
231 533104
        AN(vsbm);
232 533104
        vcc_vcl_met2c(vsbm, mask);
233 533104
        AZ(VSB_finish(vsbm));
234 533104
        Fc(tl, 1, "  assert(ctx->method %s (%s));\n", maskcmp, VSB_data(vsbm));
235 533104
        VSB_destroy(&vsbm);
236 533104
        Fc(tl, 1, "%s\n%s}\n", VSB_data(p->prologue), VSB_data(p->body));
237 533104
        VSB_destroy(&p->body);
238 533104
        VSB_destroy(&p->prologue);
239
240 533104
        if (! dyn) {
241 532740
                VSB_destroy(&p->cname);
242 532740
                return;
243
        }
244
245
        /* wrapper to call the actual (_checked) function */
246 364
        Fc(tl, 1, "\nvoid v_matchproto_(vcl_func_f)\n");
247 728
        Fc(tl, 1, "%s(VRT_CTX, enum vcl_func_call_e call,\n",
248 364
            VSB_data(p->cname));
249 364
        Fc(tl, 1, "    enum vcl_func_fail_e *failp)\n{\n");
250 364
        Fc(tl, 1, "  enum vcl_func_fail_e fail;\n\n");
251 728
        Fc(tl, 1, "  fail = VPI_Call_Check(ctx, &VCL_conf, 0x%x, %d);\n",
252 364
            mask, nsub);
253 364
        Fc(tl, 1, "  if (failp)\n");
254 364
        Fc(tl, 1, "    *failp = fail;\n");
255 364
        Fc(tl, 1, "  else if (fail == VSUB_E_METHOD)\n");
256 728
        Fc(tl, 1, "    VRT_fail(ctx, \"call to \\\"sub %.*s{}\\\""
257 364
            " not allowed from here\");\n", PF(p->name));
258 364
        Fc(tl, 1, "  else if (fail == VSUB_E_RECURSE)\n");
259 728
        Fc(tl, 1, "    VRT_fail(ctx, \"Recursive call to "
260 364
            "\\\"sub %.*s{}\\\"\");\n", PF(p->name));
261 364
        Fc(tl, 1, "  else\n");
262 364
        Fc(tl, 1, "    assert (fail == VSUB_E_OK);\n");
263 364
        Fc(tl, 1, "  if (fail != VSUB_E_OK || call == VSUB_CHECK)\n");
264 364
        Fc(tl, 1, "    return;\n");
265 364
        Fc(tl, 1, "  VPI_Call_Begin(ctx, %d);\n", nsub);
266 364
        Fc(tl, 1, "  %s_checked(ctx);\n", VSB_data(p->cname));
267 364
        Fc(tl, 1, "  VPI_Call_End(ctx, %d);\n", nsub);
268 364
        Fc(tl, 1, "}\n");
269 364
        VSB_destroy(&p->cname);
270 533104
}
271
272
/*--------------------------------------------------------------------*/
273
274
struct inifin *
275 191555
New_IniFin(struct vcc *tl)
276
{
277
        struct inifin *p;
278
279 191555
        ALLOC_OBJ(p, INIFIN_MAGIC);
280 191555
        AN(p);
281 191555
        p->ini = VSB_new_auto();
282 191555
        AN(p->ini);
283 191555
        p->fin = VSB_new_auto();
284 191555
        AN(p->fin);
285 191555
        p->final = VSB_new_auto();
286 191555
        AN(p->final);
287 191555
        p->event = VSB_new_auto();
288 191555
        AN(p->event);
289 191555
        p->n = ++tl->ninifin;
290 191555
        VTAILQ_INSERT_TAIL(&tl->inifin, p, list);
291 191555
        return (p);
292
}
293
294
/*--------------------------------------------------------------------
295
 * Printf output to the vsbs, possibly indented
296
 */
297
298
void
299 9584055
Fh(const struct vcc *tl, int indent, const char *fmt, ...)
300
{
301
        va_list ap;
302
303 9584055
        if (indent)
304 6945614
                VSB_printf(tl->fh, "%*.*s", tl->hindent, tl->hindent, "");
305 9584055
        va_start(ap, fmt);
306 9584055
        VSB_vprintf(tl->fh, fmt, ap);
307 9584055
        va_end(ap);
308 9584055
}
309
310
void
311 24101142
Fb(const struct vcc *tl, int indent, const char *fmt, ...)
312
{
313
        va_list ap;
314
315 24101142
        assert(tl->fb != NULL);
316 24101142
        if (indent)
317 21185203
                VSB_printf(tl->fb, "%*.*s", tl->indent, tl->indent, "");
318 24101142
        va_start(ap, fmt);
319 24101142
        VSB_vprintf(tl->fb, fmt, ap);
320 24101142
        va_end(ap);
321 24101142
}
322
323
void
324 19116084
Fc(const struct vcc *tl, int indent, const char *fmt, ...)
325
{
326
        va_list ap;
327
328 19116084
        if (indent)
329 3737552
                VSB_printf(tl->fc, "%*.*s", tl->indent, tl->indent, "");
330 19116084
        va_start(ap, fmt);
331 19116084
        VSB_vprintf(tl->fc, fmt, ap);
332 19116084
        va_end(ap);
333 19116084
}
334
335
/*--------------------------------------------------------------------*/
336
337
void
338 1196273
EncToken(struct vsb *sb, const struct token *t)
339
{
340
341 1196273
        assert(t->tok == CSTR);
342 1196273
        VSB_quote(sb, t->dec, -1, VSB_QUOTE_CSTR);
343 1196273
}
344
345
/*--------------------------------------------------------------------
346
 * Output the location/profiling table.  For each counted token, we
347
 * record source+line+charpos for the first character in the token.
348
 */
349
350
static void
351 15210
EmitCoordinates(const struct vcc *tl, struct vsb *vsb)
352
{
353
        struct token *t;
354
        unsigned lin, pos;
355
        const struct source *sp;
356
        const char *p;
357
358 15210
        VSB_cat(vsb, "/* ---===### Source Code ###===---*/\n");
359
360 15210
        VSB_printf(vsb, "\n#define VGC_NSRCS %u\n", tl->nsources);
361
362 15210
        VSB_cat(vsb, "\nstatic const char *srcname[VGC_NSRCS] = {\n");
363 46033
        VTAILQ_FOREACH(sp, &tl->sources, list) {
364 30823
                VSB_cat(vsb, "\t");
365 30823
                VSB_quote(vsb, sp->name, -1, VSB_QUOTE_CSTR);
366 30823
                VSB_cat(vsb, ",\n");
367 30823
        }
368 15210
        VSB_cat(vsb, "};\n");
369
370 15210
        VSB_printf(vsb, "\nstatic const char *srcbody[%u] = {\n", tl->nsources);
371 46033
        VTAILQ_FOREACH(sp, &tl->sources, list) {
372 30823
                VSB_cat(vsb, "    /* ");
373 30823
                VSB_quote(vsb, sp->name, -1, VSB_QUOTE_CSTR);
374 30823
                VSB_cat(vsb, " */\n");
375 30823
                VSB_quote_pfx(vsb, "\t", sp->b, sp->e - sp->b, VSB_QUOTE_CSTR);
376 30823
                VSB_cat(vsb, ",\n");
377 30823
        }
378 15210
        VSB_cat(vsb, "};\n\n");
379
380 15210
        VSB_cat(vsb, "/* ---===### Location Counters ###===---*/\n");
381
382 15210
        VSB_printf(vsb, "\n#define VGC_NREFS %u\n\n", tl->cnt + 1);
383
384 15210
        VSB_cat(vsb, "static const struct vpi_ref VGC_ref[VGC_NREFS] = {\n");
385 15210
        lin = 1;
386 15210
        pos = 0;
387 15210
        sp = 0;
388 15210
        p = NULL;
389 12523329
        VTAILQ_FOREACH(t, &tl->tokens, list) {
390 12508119
                if (t->cnt == 0)
391 11512579
                        continue;
392 995540
                assert(t->src != NULL);
393 995540
                if (t->src != sp) {
394 25727
                        lin = 1;
395 25727
                        pos = 0;
396 25727
                        sp = t->src;
397 25727
                        p = sp->b;
398 25727
                }
399 995540
                assert(sp != NULL);
400 995540
                assert(p != NULL);
401 98141028
                for (;p < t->b; p++) {
402 97145488
                        if (*p == '\n') {
403 4492358
                                lin++;
404 4492358
                                pos = 0;
405 97145488
                        } else if (*p == '\t') {
406 2169804
                                pos &= ~7;
407 2169804
                                pos += 8;
408 2169804
                        } else
409 90483326
                                pos++;
410
411 97145488
                }
412 1991080
                VSB_printf(vsb, "  [%3u] = { VPI_REF_MAGIC, %u, %8tu, %4u, %3u, ",
413 995540
                    t->cnt, sp->idx, t->b - sp->b, lin, pos + 1);
414 995540
                if (t->tok == CSRC)
415 91
                        VSB_cat(vsb, " \"C{\"},\n");
416
                else
417 995449
                        VSB_printf(vsb, " \"%.*s\" },\n", PF(t));
418 995540
        }
419 15210
        VSB_cat(vsb, "};\n\n");
420 15210
}
421
422
/*--------------------------------------------------------------------
423
 * Init/Fini/Event
424
 *
425
 * We call DISCARD and COLD events in the opposite order of LOAD and
426
 * WARM.
427
 */
428
429
static void
430 15210
EmitInitFini(const struct vcc *tl)
431
{
432 15210
        struct inifin *p, *q = NULL;
433 15210
        unsigned has_event = 0;
434
        struct symbol *sy;
435
436 15210
        Fh(tl, 0, "\n");
437 15210
        Fh(tl, 0, "static unsigned vgc_inistep;\n");
438 15210
        Fh(tl, 0, "static unsigned vgc_warmupstep;\n");
439
440
        /*
441
         * LOAD
442
         */
443 15210
        Fc(tl, 0, "\nstatic int\nVGC_Load(VRT_CTX)\n{\n\n");
444 15210
        Fc(tl, 0, "\tvgc_inistep = 0;\n");
445 15210
        Fc(tl, 0, "\tsize_t ndirector = %dUL;\n", tl->ndirector);
446 15210
        Fc(tl, 0, "\n");
447 122057
        VTAILQ_FOREACH(p, &tl->inifin, list) {
448 106847
                AZ(VSB_finish(p->ini));
449 106847
                assert(p->n > 0);
450 106847
                if (VSB_len(p->ini))
451 104039
                        Fc(tl, 0, "\t/* %u */\n%s\n", p->n, VSB_data(p->ini));
452 106847
                if (p->ignore_errors == 0) {
453 91637
                        Fc(tl, 0, "\tif (ctx->vpi->handling == VCL_RET_FAIL)\n");
454 91637
                        Fc(tl, 0, "\t\treturn(1);\n");
455 91637
                }
456 106847
                Fc(tl, 0, "\tvgc_inistep = %u;\n\n", p->n);
457 106847
                VSB_destroy(&p->ini);
458
459 106847
                AZ(VSB_finish(p->event));
460 106847
                if (VSB_len(p->event))
461 1248
                        has_event = 1;
462 106847
        }
463
464
        /* Handle failures from vcl_init */
465 15210
        Fc(tl, 0, "\n");
466 15210
        Fc(tl, 0, "\tif (ctx->vpi->handling != VCL_RET_OK)\n");
467 15210
        Fc(tl, 0, "\t\treturn(1);\n");
468 15210
        Fc(tl, 0, "\tctx->vpi->handling = 0;\n");
469
470 17680
        VTAILQ_FOREACH(sy, &tl->sym_objects, sideways) {
471 2470
                Fc(tl, 0, "\tif (!%s) {\n", sy->rname);
472 4940
                Fc(tl, 0, "\t\tVRT_fail(ctx, "
473 2470
                    "\"Object %s not initialized\");\n" , sy->name);
474 2470
                Fc(tl, 0, "\t\treturn(1);\n");
475 2470
                Fc(tl, 0, "\t}\n");
476 2470
        }
477
478 15210
        Fc(tl, 0, "\treturn(0);\n");
479 15210
        Fc(tl, 0, "}\n");
480
481
        /*
482
         * DISCARD
483
         */
484 15210
        Fc(tl, 0, "\nstatic int\nVGC_Discard(VRT_CTX)\n{\n\n");
485
486 15210
        Fc(tl, 0, "\tswitch (vgc_inistep) {\n");
487 122057
        VTAILQ_FOREACH_REVERSE(p, &tl->inifin, inifinhead, list) {
488 106847
                AZ(VSB_finish(p->fin));
489 106847
                if (q)
490 91637
                        assert(q->n > p->n);
491 106847
                q = p;
492 106847
                Fc(tl, 0, "\t\tcase %u:\n", p->n);
493 106847
                if (VSB_len(p->fin))
494 106821
                        Fc(tl, 0, "\t%s\n", VSB_data(p->fin));
495 106847
                Fc(tl, 0, "\t\t\t/* FALLTHROUGH */\n");
496 106847
                VSB_destroy(&p->fin);
497 106847
        }
498 15210
        Fc(tl, 0, "\t\tdefault:\n\t\t\tbreak;\n");
499 15210
        Fc(tl, 0, "\t}\n\n");
500 15210
        Fc(tl, 0, "\tswitch (vgc_inistep) {\n");
501 122057
        VTAILQ_FOREACH_REVERSE(p, &tl->inifin, inifinhead, list) {
502 106847
                AZ(VSB_finish(p->final));
503 106847
                Fc(tl, 0, "\t\tcase %u:\n", p->n);
504 106847
                if (VSB_len(p->final))
505 6227
                        Fc(tl, 0, "\t%s\n", VSB_data(p->final));
506 106847
                Fc(tl, 0, "\t\t\t/* FALLTHROUGH */\n");
507 106847
                VSB_destroy(&p->final);
508 106847
        }
509 15210
        Fc(tl, 0, "\t\tdefault:\n\t\t\tbreak;\n");
510 15210
        Fc(tl, 0, "\t}\n\n");
511
512 15210
        Fc(tl, 0, "\treturn (0);\n");
513 15210
        Fc(tl, 0, "}\n");
514
515 15210
        if (has_event) {
516
                /*
517
                 * WARM
518
                 */
519 1248
                Fc(tl, 0, "\nstatic int\n");
520 1248
                Fc(tl, 0, "VGC_Warmup(VRT_CTX, enum vcl_event_e ev)\n{\n\n");
521
522 1248
                Fc(tl, 0, "\tvgc_warmupstep = 0;\n\n");
523 12753
                VTAILQ_FOREACH(p, &tl->inifin, list) {
524 11505
                        assert(p->n > 0);
525 11505
                        if (VSB_len(p->event)) {
526 1248
                                Fc(tl, 0, "\t/* %u */\n", p->n);
527 1248
                                Fc(tl, 0, "\tif (%s)\n", VSB_data(p->event));
528 1248
                                Fc(tl, 0, "\t\treturn (1);\n");
529 1248
                                Fc(tl, 0, "\tvgc_warmupstep = %u;\n\n", p->n);
530 1248
                        }
531 11505
                }
532
533 1248
                Fc(tl, 0, "\treturn (0);\n");
534 1248
                Fc(tl, 0, "}\n");
535
536
                /*
537
                 * COLD
538
                 */
539 1248
                Fc(tl, 0, "\nstatic int\n");
540 1248
                Fc(tl, 0, "VGC_Cooldown(VRT_CTX, enum vcl_event_e ev)\n{\n");
541 1248
                Fc(tl, 0, "\tint retval = 0;\n\n");
542
543 12753
                VTAILQ_FOREACH_REVERSE(p, &tl->inifin, inifinhead, list) {
544 11505
                        if (VSB_len(p->event)) {
545 1248
                                Fc(tl, 0, "\t/* %u */\n", p->n);
546 2496
                                Fc(tl, 0,
547 1248
                                    "\tif (vgc_warmupstep >= %u &&\n", p->n);
548 2496
                                Fc(tl, 0,
549 1248
                                    "\t    %s != 0)\n", VSB_data(p->event));
550 1248
                                Fc(tl, 0, "\t\tretval = 1;\n\n");
551 1248
                        }
552 11505
                        VSB_destroy(&p->event);
553 11505
                }
554
555 1248
                Fc(tl, 0, "\treturn (retval);\n");
556 1248
                Fc(tl, 0, "}\n");
557 1248
        }
558
559
        /*
560
         * EVENTS
561
         */
562 15210
        Fc(tl, 0, "\nstatic int\n");
563 15210
        Fc(tl, 0, "VGC_Event(VRT_CTX, enum vcl_event_e ev)\n");
564 15210
        Fc(tl, 0, "{\n");
565 15210
        Fc(tl, 0, "\tif (ev == VCL_EVENT_LOAD)\n");
566 15210
        Fc(tl, 0, "\t\treturn (VGC_Load(ctx));\n");
567 15210
        if (has_event) {
568 1248
                Fc(tl, 0, "\tif (ev == VCL_EVENT_WARM)\n");
569 1248
                Fc(tl, 0, "\t\treturn (VGC_Warmup(ctx, ev));\n");
570 1248
                Fc(tl, 0, "\tif (ev == VCL_EVENT_COLD)\n");
571 1248
                Fc(tl, 0, "\t\treturn (VGC_Cooldown(ctx, ev));\n");
572 1248
        }
573 15210
        Fc(tl, 0, "\tif (ev == VCL_EVENT_DISCARD)\n");
574 15210
        Fc(tl, 0, "\t\treturn (VGC_Discard(ctx));\n");
575 15210
        Fc(tl, 0, "\n");
576 15210
        if (!has_event)
577 13962
                Fc(tl, 0, "\t(void)vgc_warmupstep;\n");
578 15210
        Fc(tl, 0, "\treturn (%d);\n", has_event ? 1 : 0);
579 15210
        Fc(tl, 0, "}\n");
580 15210
}
581
582
/*--------------------------------------------------------------------*/
583
584
static void
585 15210
EmitStruct(const struct vcc *tl)
586
{
587 15210
        Fc(tl, 0, "\nconst struct VCL_conf VCL_conf = {\n");
588 15210
        Fc(tl, 0, "\t.magic = VCL_CONF_MAGIC,\n");
589 15210
        Fc(tl, 0, "\t.syntax = %u,\n", tl->syntax);
590 15210
        Fc(tl, 0, "\t.event_vcl = VGC_Event,\n");
591 15210
        Fc(tl, 0, "\t.default_director = &%s,\n", tl->default_director);
592 15210
        if (tl->default_probe != NULL)
593 104
                Fc(tl, 0, "\t.default_probe = %s,\n", tl->default_probe);
594 15210
        Fc(tl, 0, "\t.ref = VGC_ref,\n");
595 15210
        Fc(tl, 0, "\t.nref = VGC_NREFS,\n");
596 15210
        Fc(tl, 0, "\t.nsrc = VGC_NSRCS,\n");
597 15210
        Fc(tl, 0, "\t.nsub = %d,\n", tl->subref > 0 ? tl->nsub : 0);
598 15210
        Fc(tl, 0, "\t.srcname = srcname,\n");
599 15210
        Fc(tl, 0, "\t.srcbody = srcbody,\n");
600 15210
        Fc(tl, 0, "\t.nvmod = %u,\n", tl->vmod_count);
601
#define VCL_MET_MAC(l,u,t,b) \
602
        Fc(tl, 0, "\t." #l "_func = VGC_function_vcl_" #l ",\n");
603
#include "tbl/vcl_returns.h"
604 15210
        Fc(tl, 0, "\t.instance_info = VGC_instance_info\n");
605 15210
        Fc(tl, 0, "};\n");
606 15210
}
607
608
/*--------------------------------------------------------------------
609
 * Compile the VCL code from the given source and return the C-source
610
 */
611
612
static struct vsb *
613 37921
vcc_CompileSource(struct vcc *tl, struct source *sp, const char *jfile)
614
{
615
        struct proc *p;
616
        struct vsb *vsb;
617
        struct inifin *ifp;
618
619 37921
        Fh(tl, 0, "/* ---===### VCC generated .h code ###===---*/\n");
620 37921
        Fc(tl, 0, "\n/* ---===### VCC generated .c code ###===---*/\n");
621
622 37921
        Fc(tl, 0, "\n#define END_ if (ctx->vpi->handling) return\n");
623
624 37921
        vcc_Parse_Init(tl);
625
626 37921
        vcc_Expr_Init(tl);
627
628 37921
        vcc_Action_Init(tl);
629
630 37921
        vcc_Backend_Init(tl);
631
632 37921
        vcc_Var_Init(tl);
633
634 37921
        vcc_Type_Init(tl);
635
636 37921
        Fh(tl, 0, "\nextern const struct VCL_conf VCL_conf;\n");
637
638
        /* Register and lex the main source */
639 37921
        if (sp != NULL) {
640 18954
                AN(vcc_builtin);
641 18954
                vcc_lex_source(tl, sp, 0);
642 18954
                if (tl->err)
643 416
                        return (NULL);
644 18538
        }
645
646
        /* Register and lex the builtin VCL */
647 37505
        sp = vcc_new_source(tl->builtin_vcl, "builtin", "<builtin>");
648 37505
        assert(sp != NULL);
649 37505
        vcc_lex_source(tl, sp, 1);
650 37505
        if (tl->err)
651 0
                return (NULL);
652
653
        /* Expand and lex any includes in the token string */
654 37505
        if (tl->err)
655 0
                return (NULL);
656
657
        /* Parse the token string */
658 37505
        tl->t = VTAILQ_FIRST(&tl->tokens);
659 37505
        vcc_Parse(tl);
660 37505
        if (tl->err)
661 2600
                return (NULL);
662
663
        /* Check for orphans */
664 34905
        if (vcc_CheckReferences(tl))
665 117
                return (NULL);
666
667
        /* Check that all action returns are legal */
668 34788
        if (vcc_CheckAction(tl) || tl->err)
669 39
                return (NULL);
670
671
        /* Check that all variable uses are legal */
672 34749
        if (vcc_CheckUses(tl) || tl->err)
673 546
                return (NULL);
674
675 34203
        if (vcc_builtin == NULL)
676 18967
                return (NULL);
677
678
        /* Check if we have any backends at all */
679 15236
        if (tl->default_director == NULL) {
680 26
                VSB_cat(tl->sb,
681
                    "No backends or directors found in VCL program, "
682
                    "at least one is necessary.\n");
683 26
                tl->err = 1;
684 26
                return (NULL);
685
        }
686
687
        /* Tie vcl_init/fini in */
688 15210
        ifp = New_IniFin(tl);
689 15210
        VSB_cat(ifp->ini, "\tVGC_function_vcl_init(ctx, VSUB_STATIC, NULL);\n");
690
        /*
691
         * Because the failure could be half way into vcl_init{} so vcl_fini{}
692
         * must always be called, also on failure.
693
         */
694 15210
        ifp->ignore_errors = 1;
695 15210
        VSB_cat(ifp->fin, "\t\tVGC_function_vcl_fini(ctx, VSUB_STATIC, NULL);\n");
696 15210
        VSB_cat(ifp->fin, "\t\t\tVPI_vcl_fini(ctx);");
697
698
        /* Emit method functions */
699 15210
        Fh(tl, 1, "\n");
700 548314
        VTAILQ_FOREACH(p, &tl->procs, list)
701 853268
                if (p->method == NULL)
702 320164
                        vcc_EmitProc(tl, p);
703 548314
        VTAILQ_FOREACH(p, &tl->procs, list)
704 746044
                if (p->method != NULL)
705 212940
                        vcc_EmitProc(tl, p);
706
707 15210
        EmitInitFini(tl);
708
709 15210
        VCC_InstanceInfo(tl);
710
711 15210
        EmitStruct(tl);
712
713 15210
        VCC_XrefTable(tl);
714
715 15210
        VSB_cat(tl->symtab, "\n]\n");
716 15210
        AZ(VSB_finish(tl->symtab));
717 15210
        if (TLWriteVSB(tl, jfile, tl->symtab, "Symbol table"))
718 0
                return (NULL);
719
720
        /* Combine it all */
721
722 15210
        vsb = VSB_new_auto();
723 15210
        AN(vsb);
724
725 15210
        vcl_output_lang_h(vsb);
726
727 15210
        EmitCoordinates(tl, vsb);
728
729 15210
        AZ(VSB_finish(tl->fh));
730 15210
        VSB_cat(vsb, VSB_data(tl->fh));
731
732 15210
        AZ(VSB_finish(tl->fc));
733 15210
        VSB_cat(vsb, VSB_data(tl->fc));
734
735 15210
        AZ(VSB_finish(vsb));
736 15210
        return (vsb);
737 37921
}
738
739
static struct vcc *
740 18967
vcc_ParseBuiltin(struct vcc *tl)
741
{
742
        struct vcc *tl_builtin;
743
744 18967
        CHECK_OBJ_NOTNULL(tl, VCC_MAGIC);
745 18967
        tl_builtin = VCC_New();
746 18967
        AN(tl_builtin);
747 18967
        VCC_Builtin_VCL(tl_builtin, tl->builtin_vcl);
748 18967
        AZ(vcc_CompileSource(tl_builtin, NULL, NULL));
749 18967
        return (tl_builtin);
750
}
751
752
/*--------------------------------------------------------------------
753
 * Report the range of VCL language we support
754
 */
755
void
756 12883
VCC_VCL_Range(unsigned *lo, unsigned *hi)
757
{
758
759 12883
        AN(lo);
760 12883
        *lo = VCL_LOW;
761 12883
        AN(hi);
762 12883
        *hi = VCL_HIGH;
763 12883
}
764
765
/*--------------------------------------------------------------------
766
 * Compile the VCL code in the argument.  Error messages, if any are
767
 * formatted into the vsb.
768
 */
769
770
int
771 18967
VCC_Compile(struct vcc *tl, struct vsb **sb,
772
    const char *vclsrc, const char *vclsrcfile,
773
    const char *ofile, const char *jfile)
774
{
775
        struct source *sp;
776 18967
        struct vsb *r = NULL;
777 18967
        int retval = 0;
778
779 18967
        CHECK_OBJ_NOTNULL(tl, VCC_MAGIC);
780 18967
        AN(sb);
781 18967
        AN(vclsrcfile);
782 18967
        AN(ofile);
783 18967
        AN(jfile);
784
785 18967
        AZ(vcc_builtin);
786 18967
        vcc_builtin = vcc_ParseBuiltin(tl);
787 18967
        AN(vcc_builtin);
788 18967
        if (vcc_builtin->err) {
789 0
                AZ(VSB_finish(vcc_builtin->sb));
790 0
                *sb = vcc_builtin->sb;
791 0
                return (-1);
792
        }
793
794 18967
        if (vclsrc != NULL)
795 18811
                sp = vcc_new_source(vclsrc, "vcl.inline", vclsrcfile);
796
        else
797 156
                sp = vcc_file_source(tl, vclsrcfile);
798
799 18967
        if (sp != NULL)
800 18954
                r = vcc_CompileSource(tl, sp, jfile);
801
802 18967
        if (r != NULL) {
803 15210
                retval = TLWriteVSB(tl, ofile, r, "C-source");
804 15210
                VSB_destroy(&r);
805 15210
        } else {
806 3757
                retval = -1;
807
        }
808 18967
        AZ(VSB_finish(tl->sb));
809 18967
        *sb = tl->sb;
810 18967
        return (retval);
811 18967
}
812
813
/*--------------------------------------------------------------------
814
 * Allocate a compiler instance
815
 */
816
817
struct vcc *
818 37934
VCC_New(void)
819
{
820
        struct vcc *tl;
821
        struct symbol *sym;
822
        struct proc *p;
823
        struct vsb *vsb;
824
        int i;
825
826 37934
        ALLOC_OBJ(tl, VCC_MAGIC);
827 37934
        AN(tl);
828 37934
        VTAILQ_INIT(&tl->inifin);
829 37934
        VTAILQ_INIT(&tl->tokens);
830 37934
        VTAILQ_INIT(&tl->sources);
831 37934
        VTAILQ_INIT(&tl->procs);
832 37934
        VTAILQ_INIT(&tl->sym_objects);
833 37934
        VTAILQ_INIT(&tl->sym_vmods);
834 37934
        VTAILQ_INIT(&tl->vmod_objects);
835
836 37934
        tl->nsources = 0;
837
838 37934
        tl->symtab = VSB_new_auto();
839 37934
        assert(tl->symtab != NULL);
840 37934
        VSB_cat(tl->symtab, "[\n    {\"version\": 0}");
841
842 37934
        tl->fc = VSB_new_auto();
843 37934
        assert(tl->fc != NULL);
844
845 37934
        tl->fh = VSB_new_auto();
846 37934
        assert(tl->fh != NULL);
847
848 569010
        for (i = 1; i < VCL_MET_MAX; i++) {
849 1062152
                sym = VCC_MkSym(tl, method_tab[i].name,
850 531076
                    SYM_MAIN, SYM_SUB, VCL_LOW, VCL_HIGH);
851 531076
                p = vcc_NewProc(tl, sym);
852 531076
                p->method = &method_tab[i];
853
854
                // see also VCC_GlobalSymbol()
855 531076
                vsb = VSB_new_auto();
856 531076
                AN(vsb);
857 531076
                VSB_printf(vsb, "%s_%s", SUB->global_pfx, p->method->name);
858 531076
                AZ(VSB_finish(vsb));
859
860 531076
                AZ(VSB_bcat(p->cname, VSB_data(vsb), VSB_len(vsb)));
861
862 531076
                sym->lname = strdup(VSB_data(vsb));
863 531076
                AN(sym->lname);
864
865 531076
                VSB_clear(vsb);
866 531076
                VSB_printf(vsb, "sub_%s", sym->lname);
867 531076
                AZ(VSB_finish(vsb));
868
869 531076
                sym->rname = strdup(VSB_data(vsb));
870 531076
                AN(sym->rname);
871 531076
                VSB_destroy(&vsb);
872
873 531076
                sym->type = SUB;
874 531076
                sym->kind = VCC_HandleKind(SUB);
875 531076
                AZ(VCT_invalid_name(sym->rname, NULL));
876 531076
                sym->eval = vcc_Eval_Sub;
877 531076
        }
878 37934
        tl->sb = VSB_new_auto();
879 37934
        AN(tl->sb);
880 37934
        return (tl);
881
}
882
883
/*--------------------------------------------------------------------
884
 * Configure builtin VCL source code
885
 */
886
887
void
888 37934
VCC_Builtin_VCL(struct vcc *vcc, const char *str)
889
{
890
891 37934
        CHECK_OBJ_NOTNULL(vcc, VCC_MAGIC);
892 37934
        REPLACE(vcc->builtin_vcl, str);
893 37934
}
894
895
/*--------------------------------------------------------------------
896
 * Configure default VCL source path
897
 */
898
899
void
900 18967
VCC_VCL_path(struct vcc *vcc, const char *str)
901
{
902
903 18967
        CHECK_OBJ_NOTNULL(vcc, VCC_MAGIC);
904 18967
        VFIL_setpath(&vcc->vcl_path, str);
905 18967
}
906
907
/*--------------------------------------------------------------------
908
 * Configure default VMOD path
909
 */
910
911
void
912 18967
VCC_VMOD_path(struct vcc *vcc, const char *str)
913
{
914
915 18967
        CHECK_OBJ_NOTNULL(vcc, VCC_MAGIC);
916 18967
        VFIL_setpath(&vcc->vmod_path, str);
917 18967
}
918
919
/*--------------------------------------------------------------------
920
 * Configure settings
921
 */
922
923
#define VCC_FEATURE_BIT(U, l, d)                                \
924
        void VCC_Opt_ ## l(struct vcc *vcc, unsigned val)       \
925
        {                                                       \
926
                CHECK_OBJ_NOTNULL(vcc, VCC_MAGIC);              \
927
                vcc->l = val;                                   \
928
        }
929
#include "tbl/vcc_feature_bits.h"
930
931
/*--------------------------------------------------------------------
932
 * Configure settings
933
 */
934
935
static void
936 546
vcc_predef_vcl(struct vcc *vcc, const char *name)
937
{
938
        struct symbol *sym;
939
940 546
        sym = VCC_MkSym(vcc, name, SYM_MAIN, SYM_VCL, VCL_LOW, VCL_HIGH);
941 546
        AN(sym);
942 546
        sym->type = VCL;
943 546
        sym->r_methods = VCL_MET_RECV;
944 546
}
945
946
void
947 38545
VCC_Predef(struct vcc *vcc, const char *type, const char *name)
948
{
949
950 38545
        CHECK_OBJ_NOTNULL(vcc, VCC_MAGIC);
951 38545
        if (!strcmp(type, "VCL_STEVEDORE"))
952 37999
                vcc_stevedore(vcc, name);
953 546
        else if (!strcmp(type, "VCL_VCL"))
954 546
                vcc_predef_vcl(vcc, name);
955
        else
956 0
                WRONG("Unknown VCC predef type");
957 38545
}
958
959
void
960 13
VCC_VEXT(struct vcc *vcc, const char *filename)
961
{
962 13
        CHECK_OBJ_NOTNULL(vcc, VCC_MAGIC);
963 13
        vcc_ImportVext(vcc, filename);
964 13
}