src/stencil_vm.inc

filter: Statementstmt 94.4% 291 / 308 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 94.4% 291 / 308 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/stencil_str.h ↑ Summary src/stencil_buf.c »
1 /* VM dispatch body - included twice by stencil_render.c:
2 * STENCIL_VM_GOTO=1 -> computed-goto threaded dispatch
3 * STENCIL_VM_GOTO=0 -> portable switch dispatch
4 * with STENCIL_VM_NAME set to the function name. The op handlers are
5 * written once; only the dispatch glue differs. Returns 1 on SOP_END,
6 * 0 after rerror(). */
7  
8 105137 static int STENCIL_VM_NAME(pTHX_ stencil_rstate *r)
9 {
10 105137 const uint8_t *code = r->code;
11 105137 uint32_t pc = 0, at = 0;
12  
13 #if STENCIL_VM_GOTO
14 static const void *const jt[] = {
15 #define STENCIL_X_LBL(n) &&L_##n,
16 STENCIL_OPS(STENCIL_X_LBL)
17 #undef STENCIL_X_LBL
18 };
19 # define VM_CASE(n) L_##n:
20 # define VM_NEXT do { at = pc; goto *jt[code[pc++]]; } while (0)
21 105137 VM_NEXT;
22 #else
23 # define VM_CASE(n) case n:
24 # define VM_NEXT break
25 for (;;) {
26 0 at = pc;
27 0 switch (code[pc++]) {
28 0 default:
29 0 return rfail(r, at, "corrupt bytecode (op %u)",
30 0 (unsigned)code[pc - 1]);
31 #endif
32  
33 105035 VM_CASE(SOP_END)
34 {
35 105035 return 1;
36 }
37  
38 16025 VM_CASE(SOP_LITERAL_SHORT)
39 {
40 16025 uint8_t l = code[pc++];
41 16025 stencil_buf_write(&r->buf, (const char *)code + pc, l);
42 16025 pc += l;
43 }
44 16025 VM_NEXT;
45  
46 288 VM_CASE(SOP_LITERAL_LONG)
47 {
48 288 uint32_t off = vm_u32(code, &pc);
49 288 uint32_t len = vm_u32(code, &pc);
50 288 stencil_buf_write(&r->buf, r->pool + off, len);
51 }
52 288 VM_NEXT;
53  
54 117681 VM_CASE(SOP_PRINT_ESC)
55 {
56 117681 stencil_val v = r->stack[--r->sp];
57 235275 if (STENCIL_LIKELY(v.sv && SvOK(v.sv))) {
58 STRLEN n;
59 117594 const char *p = SvPV(v.sv, n);
60 117594 if (SvUTF8(v.sv))
61 20 r->buf.utf8 = 1;
62 117574 else if (STENCIL_UNLIKELY(has_high_byte(p, n))) {
63 4 p = latin1_upgrade(aTHX_ r, p, &n);
64 4 r->buf.utf8 = 1;
65 }
66 117594 if (STENCIL_UNLIKELY((v.vflags & STENCIL_VF_ESCAPED)
67 || (r->flags & STENCIL_RF_NO_ESCAPE)))
68 9 stencil_buf_write(&r->buf, p, n);
69 else
70 117585 stencil_dispatch.escape(&r->buf, p, n);
71 87 } else if (STENCIL_UNLIKELY(r->flags & STENCIL_RF_STRICT)) {
72 51 val_release(aTHX_ &v);
73 51 return rfail_path(r, at, r->last_path);
74 }
75 117630 val_release(aTHX_ &v);
76 }
77 117630 VM_NEXT;
78  
79 14 VM_CASE(SOP_PRINT_RAW)
80 {
81 14 stencil_val v = r->stack[--r->sp];
82 28 if (STENCIL_LIKELY(v.sv && SvOK(v.sv))) {
83 STRLEN n;
84 14 const char *p = SvPV(v.sv, n);
85 14 if (SvUTF8(v.sv))
86 0 r->buf.utf8 = 1;
87 14 else if (STENCIL_UNLIKELY(has_high_byte(p, n))) {
88 0 p = latin1_upgrade(aTHX_ r, p, &n);
89 0 r->buf.utf8 = 1;
90 }
91 14 stencil_buf_write(&r->buf, p, n);
92 0 } else if (STENCIL_UNLIKELY(r->flags & STENCIL_RF_STRICT)) {
93 0 val_release(aTHX_ &v);
94 0 return rfail_path(r, at, r->last_path);
95 }
96 14 val_release(aTHX_ &v);
97 }
98 14 VM_NEXT;
99  
100 167100 VM_CASE(SOP_PUSH_PATH)
101 {
102 167100 uint32_t id = vm_u32(code, &pc);
103 167100 int owned = 0;
104 SV *sv;
105 167100 r->last_path = id;
106 167100 sv = resolve_path(aTHX_ r, id, &owned);
107 167100 if (STENCIL_UNLIKELY(r->failed))
108 1 return rfail_here(r, at);
109 167099 r->stack[r->sp].sv = sv;
110 167099 r->stack[r->sp].owned = owned;
111 167099 r->stack[r->sp].vflags = 0;
112 167099 r->sp++;
113 }
114 167099 VM_NEXT;
115  
116 149 VM_CASE(SOP_PUSH_LIT_STR)
117 {
118 149 uint32_t off = vm_u32(code, &pc);
119 149 uint32_t len = vm_u32(code, &pc);
120 149 SV *sv = newSVpvn(r->pool + off, len);
121 149 if (r->prog->flags & STENCIL_PROG_SRC_UTF8)
122 7 SvUTF8_on(sv);
123 149 r->stack[r->sp].sv = sv;
124 149 r->stack[r->sp].owned = 1;
125 149 r->stack[r->sp].vflags = 0;
126 149 r->sp++;
127 }
128 149 VM_NEXT;
129  
130 20287 VM_CASE(SOP_PUSH_LIT_NUM)
131 {
132 NV d;
133 20287 memcpy(&d, code + pc, sizeof d);
134 20287 pc += sizeof d;
135 20287 r->stack[r->sp].sv = newSVnv(d);
136 20287 r->stack[r->sp].owned = 1;
137 20287 r->stack[r->sp].vflags = 0;
138 20287 r->sp++;
139 }
140 20287 VM_NEXT;
141  
142 3 VM_CASE(SOP_PUSH_UNDEF)
143 {
144 3 r->stack[r->sp].sv = NULL;
145 3 r->stack[r->sp].owned = 0;
146 3 r->stack[r->sp].vflags = 0;
147 3 r->sp++;
148 }
149 3 VM_NEXT;
150  
151 6 VM_CASE(SOP_NOT)
152 {
153 6 stencil_val v = r->stack[--r->sp];
154 6 int t = sv_truthy(aTHX_ v.sv);
155 6 val_release(aTHX_ &v);
156 6 r->stack[r->sp].sv = t ? &PL_sv_no : &PL_sv_yes;
157 6 r->stack[r->sp].owned = 0;
158 6 r->stack[r->sp].vflags = 0;
159 6 r->sp++;
160 }
161 6 VM_NEXT;
162  
163 6 VM_CASE(SOP_DEFINED)
164 {
165 6 stencil_val v = r->stack[--r->sp];
166 6 int d = v.sv && SvOK(v.sv);
167 6 val_release(aTHX_ &v);
168 6 r->stack[r->sp].sv = d ? &PL_sv_yes : &PL_sv_no;
169 6 r->stack[r->sp].owned = 0;
170 6 r->stack[r->sp].vflags = 0;
171 6 r->sp++;
172 }
173 6 VM_NEXT;
174  
175 20035 VM_CASE(SOP_EQ_NUM) VM_CMP_NUM(==)
176 1 VM_CASE(SOP_NE_NUM) VM_CMP_NUM(!=)
177 2 VM_CASE(SOP_LT_NUM) VM_CMP_NUM(<)
178 106 VM_CASE(SOP_GT_NUM) VM_CMP_NUM(>)
179 1 VM_CASE(SOP_LE_NUM) VM_CMP_NUM(<=)
180 4 VM_CASE(SOP_GE_NUM) VM_CMP_NUM(>=)
181  
182 121 VM_CASE(SOP_EQ_STR) VM_CMP_STR(sv_eq_flags(a, b, 0))
183 2 VM_CASE(SOP_NE_STR) VM_CMP_STR(!sv_eq_flags(a, b, 0))
184 2 VM_CASE(SOP_LT_STR) VM_CMP_STR(sv_cmp_flags(a, b, 0) < 0)
185 1 VM_CASE(SOP_GT_STR) VM_CMP_STR(sv_cmp_flags(a, b, 0) > 0)
186 1 VM_CASE(SOP_LE_STR) VM_CMP_STR(sv_cmp_flags(a, b, 0) <= 0)
187 1 VM_CASE(SOP_GE_STR) VM_CMP_STR(sv_cmp_flags(a, b, 0) >= 0)
188  
189 10493 VM_CASE(SOP_TEST_JF)
190 {
191 10493 uint32_t t = vm_u32(code, &pc);
192 10493 stencil_val v = r->stack[--r->sp];
193 10493 if (!sv_truthy(aTHX_ v.sv))
194 10135 pc = t;
195 10493 val_release(aTHX_ &v);
196 }
197 10493 VM_NEXT;
198  
199 20012 VM_CASE(SOP_TEST_JT)
200 {
201 20012 uint32_t t = vm_u32(code, &pc);
202 20012 stencil_val v = r->stack[--r->sp];
203 20012 if (sv_truthy(aTHX_ v.sv))
204 20004 pc = t;
205 20012 val_release(aTHX_ &v);
206 }
207 20012 VM_NEXT;
208  
209 116 VM_CASE(SOP_JF_KEEP)
210 {
211 116 uint32_t t = vm_u32(code, &pc);
212 116 if (!sv_truthy(aTHX_ r->stack[r->sp - 1].sv))
213 1 pc = t;
214 }
215 116 VM_NEXT;
216  
217 12 VM_CASE(SOP_JT_KEEP)
218 {
219 12 uint32_t t = vm_u32(code, &pc);
220 12 if (sv_truthy(aTHX_ r->stack[r->sp - 1].sv))
221 2 pc = t;
222 }
223 12 VM_NEXT;
224  
225 7364 VM_CASE(SOP_POP)
226 {
227 7364 stencil_val v = r->stack[--r->sp];
228 7364 val_release(aTHX_ &v);
229 }
230 7364 VM_NEXT;
231  
232 5667 VM_CASE(SOP_JUMP)
233 {
234 5667 pc = vm_u32(code, &pc);
235 }
236 5667 VM_NEXT;
237  
238 1018 VM_CASE(SOP_FOR_ARY)
239 {
240 1018 uint32_t name_id = vm_u32(code, &pc);
241 1018 uint32_t end_t = vm_u32(code, &pc);
242 1018 stencil_val v = r->stack[--r->sp];
243 SV *rv;
244 AV *av;
245 SSize_t size;
246 1018 if (!v.sv || !SvOK(v.sv)) {
247 8 val_release(aTHX_ &v);
248 8 pc = end_t;
249 8 VM_NEXT;
250 }
251 1010 if (!SvROK(v.sv)
252 1009 || SvTYPE(rv = SvRV(v.sv)) != SVt_PVAV
253 1008 || SvOBJECT(rv)) {
254 2 val_release(aTHX_ &v);
255 2 return rfail(r, at, "'%.*s' is not an array reference",
256 2 (int)r->paths[r->last_path].full_len,
257 2 r->pool + r->paths[r->last_path].full_off);
258 }
259 1008 av = (AV *)rv;
260 1008 size = av_top_index(av) + 1;
261 1008 if (size == 0) {
262 5 val_release(aTHX_ &v);
263 5 pc = end_t;
264 5 VM_NEXT;
265 }
266 {
267 1003 stencil_rframe *f = &r->frames[r->nf++];
268 1003 SV **svp = av_fetch(av, 0, 0);
269 1003 f->is_hash = 0;
270 1003 f->av = av;
271 1003 f->hv = NULL;
272 1003 f->kh = NULL;
273 1003 f->key_sv = NULL;
274 1003 f->i = 0;
275 1003 f->size = size;
276 1003 f->name = sname_of(r, name_id);
277 1003 memset(&f->val, 0, sizeof f->val);
278 1003 f->serial = ++r->serial;
279 1003 f->cur = svp ? *svp : NULL;
280 1003 f->cur_val = NULL;
281 1003 f->agg = v.sv;
282 1003 f->agg_owned = v.owned;
283 }
284 }
285 1003 VM_NEXT;
286  
287 344 VM_CASE(SOP_FOR_HASH)
288 {
289 344 uint32_t key_id = vm_u32(code, &pc);
290 344 uint32_t val_id = vm_u32(code, &pc);
291 344 uint32_t end_t = vm_u32(code, &pc);
292 344 stencil_val v = r->stack[--r->sp];
293 SV *rv;
294 HV *hv;
295 SSize_t size;
296 344 if (!v.sv || !SvOK(v.sv)) {
297 0 val_release(aTHX_ &v);
298 0 pc = end_t;
299 0 VM_NEXT;
300 }
301 344 if (!SvROK(v.sv)
302 344 || SvTYPE(rv = SvRV(v.sv)) != SVt_PVHV
303 343 || SvOBJECT(rv)) {
304 1 val_release(aTHX_ &v);
305 1 return rfail(r, at, "'%.*s' is not a hash reference",
306 1 (int)r->paths[r->last_path].full_len,
307 1 r->pool + r->paths[r->last_path].full_off);
308 }
309 343 hv = (HV *)rv;
310 343 size = (SSize_t)HvKEYS(hv);
311 343 if (size == 0) {
312 1 val_release(aTHX_ &v);
313 1 pc = end_t;
314 1 VM_NEXT;
315 }
316 {
317 342 stencil_rframe *f = &r->frames[r->nf++];
318 342 f->is_hash = 1;
319 342 f->av = NULL;
320 342 f->hv = hv;
321 684 f->kh = kh_collect(aTHX_ hv, size,
322 342 !(r->flags & STENCIL_RF_NO_SORT_KEYS));
323 342 f->key_sv = newSV(16);
324 342 f->i = 0;
325 342 f->size = size;
326 342 f->name = sname_of(r, key_id);
327 342 f->val = sname_of(r, val_id);
328 342 f->serial = ++r->serial;
329 342 f->agg = v.sv;
330 342 f->agg_owned = v.owned;
331 342 kh_bind(aTHX_ f, 0);
332 }
333 }
334 342 VM_NEXT;
335  
336 34696 VM_CASE(SOP_FOR_NEXT)
337 {
338 34696 uint32_t body_t = vm_u32(code, &pc);
339 34696 stencil_rframe *f = &r->frames[r->nf - 1];
340 34696 f->i++;
341 34696 if (f->i < f->size) {
342 33391 if (f->is_hash) {
343 1168 kh_bind(aTHX_ f, f->i);
344 } else {
345 32223 SV **svp = av_fetch(f->av, f->i, 0);
346 32223 f->cur = svp ? *svp : NULL;
347 }
348 33391 pc = body_t;
349 } else {
350 1305 frame_release(aTHX_ f);
351 1305 r->nf--;
352 }
353 }
354 34696 VM_NEXT;
355  
356 10276 VM_CASE(SOP_SET)
357 {
358 10276 uint32_t id = vm_u32(code, &pc);
359 10276 stencil_val v = r->stack[--r->sp];
360 20552 SV *sv = v.owned ? v.sv
361 20393 : v.sv ? newSVsv(v.sv)
362 10117 : newSV(0);
363 10276 r->binds[r->nb].name = sname_of(r, id);
364 10276 r->binds[r->nb].sv = sv;
365 10276 r->binds[r->nb].serial = ++r->serial;
366 10276 r->nb++;
367 }
368 10276 VM_NEXT;
369  
370 5137 VM_CASE(SOP_POP_BINDS)
371 {
372 5137 uint32_t n = vm_u32(code, &pc);
373 15274 while (n--)
374 10137 SvREFCNT_dec(r->binds[--r->nb].sv);
375 }
376 5137 VM_NEXT;
377  
378 150 VM_CASE(SOP_INCLUDE)
379 {
380 150 uint32_t id = vm_u32(code, &pc);
381 int ok2;
382 150 if (STENCIL_UNLIKELY(!r->incs))
383 0 return rfail(r, at, "include requires an engine");
384 {
385 150 stencil_cache_ent *child = r->incs[id];
386 150 stencil_program *sp = r->prog;
387 150 stencil_cache_ent **si = r->incs;
388 150 const char *sn = r->unit_name;
389 150 set_unit(r, child->prog, child->incs,
390 150 child->abs_path ? child->abs_path : "<string>");
391 150 ok2 = STENCIL_VM_NAME(aTHX_ r);
392 150 set_unit(r, sp, si, sn);
393 }
394 150 if (STENCIL_UNLIKELY(!ok2))
395 3 return 0;
396 147 code = r->code;
397 }
398 147 VM_NEXT;
399  
400 145 VM_CASE(SOP_CONTENT)
401 {
402 int ok2;
403 145 if (STENCIL_UNLIKELY(!r->content_prog))
404 7 return rfail(r, at,
405 "no content template for {%% content %%}");
406 138 if (STENCIL_UNLIKELY(r->content_done))
407 0 return rfail(r, at, "{%% content %%} executed twice");
408 138 r->content_done = 1;
409 {
410 138 stencil_program *sp = r->prog;
411 138 stencil_cache_ent **si = r->incs;
412 138 const char *sn = r->unit_name;
413 138 set_unit(r, r->content_prog, r->content_incs,
414 r->content_name);
415 138 ok2 = STENCIL_VM_NAME(aTHX_ r);
416 138 set_unit(r, sp, si, sn);
417 }
418 138 if (STENCIL_UNLIKELY(!ok2))
419 0 return 0;
420 138 code = r->code;
421 }
422 138 VM_NEXT;
423  
424 51499 VM_CASE(SOP_FILTER)
425 {
426 51499 uint32_t idx = vm_u32(code, &pc);
427 51499 if (STENCIL_UNLIKELY(!filter_apply(aTHX_ r, idx)))
428 1 return rfail_here(r, at);
429 }
430 51498 VM_NEXT;
431  
432 3629 VM_CASE(SOP_CASE_TABLE)
433 {
434 3629 uint32_t id = vm_u32(code, &pc);
435 3629 uint32_t miss = vm_u32(code, &pc);
436 3629 uint32_t hit = case_table_lookup(aTHX_ r, id, r->stack[r->sp - 1].sv);
437 3629 pc = hit ? hit : miss;
438 }
439 3629 VM_NEXT;
440  
441 17785 VM_CASE(SOP_CASE)
442 {
443 17785 uint32_t id = vm_u32(code, &pc);
444 17785 uint32_t miss = vm_u32(code, &pc);
445 17785 int m = case_match(aTHX_ r, id, r->stack[r->sp - 1].sv);
446 17785 if (STENCIL_UNLIKELY(m < 0))
447 36 return rfail_here(r, at);
448 17749 if (!m)
449 14473 pc = miss;
450 }
451 17749 VM_NEXT;
452  
453 #if !STENCIL_VM_GOTO
454 } /* switch */
455 } /* for */
456 #endif
457  
458 #undef VM_CASE
459 #undef VM_NEXT
460 }
« include/stencil_str.h ↑ Summary src/stencil_buf.c »