src/stencil_vm.inc

filter: Statementstmt 94.2% 276 / 293 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 94.2% 276 / 293 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/stencil_filters.c ↑ 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 102565 static int STENCIL_VM_NAME(pTHX_ stencil_rstate *r)
9 {
10 102565 const uint8_t *code = r->code;
11 102565 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 102565 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 102523 VM_CASE(SOP_END)
34 {
35 102523 return 1;
36 }
37  
38 8622 VM_CASE(SOP_LITERAL_SHORT)
39 {
40 8622 uint8_t l = code[pc++];
41 8622 stencil_buf_write(&r->buf, (const char *)code + pc, l);
42 8622 pc += l;
43 }
44 8622 VM_NEXT;
45  
46 283 VM_CASE(SOP_LITERAL_LONG)
47 {
48 283 uint32_t off = vm_u32(code, &pc);
49 283 uint32_t len = vm_u32(code, &pc);
50 283 stencil_buf_write(&r->buf, r->pool + off, len);
51 }
52 283 VM_NEXT;
53  
54 117530 VM_CASE(SOP_PRINT_ESC)
55 {
56 117530 stencil_val v = r->stack[--r->sp];
57 235005 if (STENCIL_LIKELY(v.sv && SvOK(v.sv))) {
58 STRLEN n;
59 117475 const char *p = SvPV(v.sv, n);
60 117475 if (SvUTF8(v.sv))
61 20 r->buf.utf8 = 1;
62 117455 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 117475 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 117466 stencil_dispatch.escape(&r->buf, p, n);
71 55 } else if (STENCIL_UNLIKELY(r->flags & STENCIL_RF_STRICT)) {
72 29 val_release(aTHX_ &v);
73 29 return rfail_path(r, at, r->last_path);
74 }
75 117501 val_release(aTHX_ &v);
76 }
77 117501 VM_NEXT;
78  
79 12 VM_CASE(SOP_PRINT_RAW)
80 {
81 12 stencil_val v = r->stack[--r->sp];
82 24 if (STENCIL_LIKELY(v.sv && SvOK(v.sv))) {
83 STRLEN n;
84 12 const char *p = SvPV(v.sv, n);
85 12 if (SvUTF8(v.sv))
86 0 r->buf.utf8 = 1;
87 12 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 12 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 12 val_release(aTHX_ &v);
97 }
98 12 VM_NEXT;
99  
100 139127 VM_CASE(SOP_PUSH_PATH)
101 {
102 139127 uint32_t id = vm_u32(code, &pc);
103 139127 int owned = 0;
104 SV *sv;
105 139127 r->last_path = id;
106 139127 sv = resolve_path(aTHX_ r, id, &owned);
107 139127 if (STENCIL_UNLIKELY(r->failed))
108 1 return rfail_here(r, at);
109 139126 r->stack[r->sp].sv = sv;
110 139126 r->stack[r->sp].owned = owned;
111 139126 r->stack[r->sp].vflags = 0;
112 139126 r->sp++;
113 }
114 139126 VM_NEXT;
115  
116 133 VM_CASE(SOP_PUSH_LIT_STR)
117 {
118 133 uint32_t off = vm_u32(code, &pc);
119 133 uint32_t len = vm_u32(code, &pc);
120 133 SV *sv = newSVpvn(r->pool + off, len);
121 133 if (r->prog->flags & STENCIL_PROG_SRC_UTF8)
122 1 SvUTF8_on(sv);
123 133 r->stack[r->sp].sv = sv;
124 133 r->stack[r->sp].owned = 1;
125 133 r->stack[r->sp].vflags = 0;
126 133 r->sp++;
127 }
128 133 VM_NEXT;
129  
130 197 VM_CASE(SOP_PUSH_LIT_NUM)
131 {
132 double d;
133 197 memcpy(&d, code + pc, 8);
134 197 pc += 8;
135 197 r->stack[r->sp].sv = newSVnv((NV)d);
136 197 r->stack[r->sp].owned = 1;
137 197 r->stack[r->sp].vflags = 0;
138 197 r->sp++;
139 }
140 197 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 15 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 104 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 113 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 10400 VM_CASE(SOP_TEST_JF)
190 {
191 10400 uint32_t t = vm_u32(code, &pc);
192 10400 stencil_val v = r->stack[--r->sp];
193 10400 if (!sv_truthy(aTHX_ v.sv))
194 10135 pc = t;
195 10400 val_release(aTHX_ &v);
196 }
197 10400 VM_NEXT;
198  
199 11 VM_CASE(SOP_TEST_JT)
200 {
201 11 uint32_t t = vm_u32(code, &pc);
202 11 stencil_val v = r->stack[--r->sp];
203 11 if (sv_truthy(aTHX_ v.sv))
204 3 pc = t;
205 11 val_release(aTHX_ &v);
206 }
207 11 VM_NEXT;
208  
209 114 VM_CASE(SOP_JF_KEEP)
210 {
211 114 uint32_t t = vm_u32(code, &pc);
212 114 if (!sv_truthy(aTHX_ r->stack[r->sp - 1].sv))
213 1 pc = t;
214 }
215 114 VM_NEXT;
216  
217 10 VM_CASE(SOP_JT_KEEP)
218 {
219 10 uint32_t t = vm_u32(code, &pc);
220 10 if (sv_truthy(aTHX_ r->stack[r->sp - 1].sv))
221 2 pc = t;
222 }
223 10 VM_NEXT;
224  
225 121 VM_CASE(SOP_POP)
226 {
227 121 stencil_val v = r->stack[--r->sp];
228 121 val_release(aTHX_ &v);
229 }
230 121 VM_NEXT;
231  
232 70 VM_CASE(SOP_JUMP)
233 {
234 70 pc = vm_u32(code, &pc);
235 }
236 70 VM_NEXT;
237  
238 535 VM_CASE(SOP_FOR_ARY)
239 {
240 535 uint32_t name_id = vm_u32(code, &pc);
241 535 uint32_t end_t = vm_u32(code, &pc);
242 535 stencil_val v = r->stack[--r->sp];
243 SV *rv;
244 AV *av;
245 SSize_t size;
246 535 if (!v.sv || !SvOK(v.sv)) {
247 8 val_release(aTHX_ &v);
248 8 pc = end_t;
249 8 VM_NEXT;
250 }
251 527 if (!SvROK(v.sv)
252 526 || SvTYPE(rv = SvRV(v.sv)) != SVt_PVAV
253 525 || 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 525 av = (AV *)rv;
260 525 size = av_top_index(av) + 1;
261 525 if (size == 0) {
262 5 val_release(aTHX_ &v);
263 5 pc = end_t;
264 5 VM_NEXT;
265 }
266 {
267 520 stencil_rframe *f = &r->frames[r->nf++];
268 520 SV **svp = av_fetch(av, 0, 0);
269 520 f->is_hash = 0;
270 520 f->av = av;
271 520 f->hv = NULL;
272 520 f->kh = NULL;
273 520 f->key_sv = NULL;
274 520 f->i = 0;
275 520 f->size = size;
276 520 f->name = sname_of(r, name_id);
277 520 memset(&f->val, 0, sizeof f->val);
278 520 f->serial = ++r->serial;
279 520 f->cur = svp ? *svp : NULL;
280 520 f->cur_val = NULL;
281 520 f->agg = v.sv;
282 520 f->agg_owned = v.owned;
283 }
284 }
285 520 VM_NEXT;
286  
287 341 VM_CASE(SOP_FOR_HASH)
288 {
289 341 uint32_t key_id = vm_u32(code, &pc);
290 341 uint32_t val_id = vm_u32(code, &pc);
291 341 uint32_t end_t = vm_u32(code, &pc);
292 341 stencil_val v = r->stack[--r->sp];
293 SV *rv;
294 HV *hv;
295 SSize_t size;
296 341 if (!v.sv || !SvOK(v.sv)) {
297 0 val_release(aTHX_ &v);
298 0 pc = end_t;
299 0 VM_NEXT;
300 }
301 341 if (!SvROK(v.sv)
302 341 || SvTYPE(rv = SvRV(v.sv)) != SVt_PVHV
303 340 || 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 340 hv = (HV *)rv;
310 340 size = (SSize_t)HvKEYS(hv);
311 340 if (size == 0) {
312 1 val_release(aTHX_ &v);
313 1 pc = end_t;
314 1 VM_NEXT;
315 }
316 {
317 339 stencil_rframe *f = &r->frames[r->nf++];
318 339 f->is_hash = 1;
319 339 f->av = NULL;
320 339 f->hv = hv;
321 678 f->kh = kh_collect(aTHX_ hv, size,
322 339 !(r->flags & STENCIL_RF_NO_SORT_KEYS));
323 339 f->key_sv = newSV(16);
324 339 f->i = 0;
325 339 f->size = size;
326 339 f->name = sname_of(r, key_id);
327 339 f->val = sname_of(r, val_id);
328 339 f->serial = ++r->serial;
329 339 f->agg = v.sv;
330 339 f->agg_owned = v.owned;
331 339 kh_bind(aTHX_ f, 0);
332 }
333 }
334 339 VM_NEXT;
335  
336 29492 VM_CASE(SOP_FOR_NEXT)
337 {
338 29492 uint32_t body_t = vm_u32(code, &pc);
339 29492 stencil_rframe *f = &r->frames[r->nf - 1];
340 29492 f->i++;
341 29492 if (f->i < f->size) {
342 28653 if (f->is_hash) {
343 1164 kh_bind(aTHX_ f, f->i);
344 } else {
345 27489 SV **svp = av_fetch(f->av, f->i, 0);
346 27489 f->cur = svp ? *svp : NULL;
347 }
348 28653 pc = body_t;
349 } else {
350 839 frame_release(aTHX_ f);
351 839 r->nf--;
352 }
353 }
354 29492 VM_NEXT;
355  
356 10261 VM_CASE(SOP_SET)
357 {
358 10261 uint32_t id = vm_u32(code, &pc);
359 10261 stencil_val v = r->stack[--r->sp];
360 20522 SV *sv = v.owned ? v.sv
361 20374 : v.sv ? newSVsv(v.sv)
362 10113 : newSV(0);
363 10261 r->binds[r->nb].name = sname_of(r, id);
364 10261 r->binds[r->nb].sv = sv;
365 10261 r->binds[r->nb].serial = ++r->serial;
366 10261 r->nb++;
367 }
368 10261 VM_NEXT;
369  
370 5135 VM_CASE(SOP_POP_BINDS)
371 {
372 5135 uint32_t n = vm_u32(code, &pc);
373 15270 while (n--)
374 10135 SvREFCNT_dec(r->binds[--r->nb].sv);
375 }
376 5135 VM_NEXT;
377  
378 136 VM_CASE(SOP_INCLUDE)
379 {
380 136 uint32_t id = vm_u32(code, &pc);
381 int ok2;
382 136 if (STENCIL_UNLIKELY(!r->incs))
383 0 return rfail(r, at, "include requires an engine");
384 {
385 136 stencil_cache_ent *child = r->incs[id];
386 136 stencil_program *sp = r->prog;
387 136 stencil_cache_ent **si = r->incs;
388 136 const char *sn = r->unit_name;
389 136 set_unit(r, child->prog, child->incs,
390 136 child->abs_path ? child->abs_path : "<string>");
391 136 ok2 = STENCIL_VM_NAME(aTHX_ r);
392 136 set_unit(r, sp, si, sn);
393 }
394 136 if (STENCIL_UNLIKELY(!ok2))
395 2 return 0;
396 134 code = r->code;
397 }
398 134 VM_NEXT;
399  
400 138 VM_CASE(SOP_CONTENT)
401 {
402 int ok2;
403 138 if (STENCIL_UNLIKELY(!r->content_prog))
404 6 return rfail(r, at,
405 "no content template for {%% content %%}");
406 132 if (STENCIL_UNLIKELY(r->content_done))
407 0 return rfail(r, at, "{%% content %%} executed twice");
408 132 r->content_done = 1;
409 {
410 132 stencil_program *sp = r->prog;
411 132 stencil_cache_ent **si = r->incs;
412 132 const char *sn = r->unit_name;
413 132 set_unit(r, r->content_prog, r->content_incs,
414 r->content_name);
415 132 ok2 = STENCIL_VM_NAME(aTHX_ r);
416 132 set_unit(r, sp, si, sn);
417 }
418 132 if (STENCIL_UNLIKELY(!ok2))
419 0 return 0;
420 132 code = r->code;
421 }
422 132 VM_NEXT;
423  
424 51481 VM_CASE(SOP_FILTER)
425 {
426 51481 uint32_t idx = vm_u32(code, &pc);
427 51481 if (STENCIL_UNLIKELY(!filter_apply(aTHX_ r, idx)))
428 1 return rfail_here(r, at);
429 }
430 51480 VM_NEXT;
431  
432 #if !STENCIL_VM_GOTO
433 } /* switch */
434 } /* for */
435 #endif
436  
437 #undef VM_CASE
438 #undef VM_NEXT
439 }
« src/stencil_filters.c ↑ Summary src/stencil_buf.c »