aboutsummaryrefslogtreecommitdiff
path: root/lpcode.c
diff options
context:
space:
mode:
authorAlexey Romanoff <ar@logiceditor.com>2021-08-15 15:17:28 +0300
committerAlexey Romanoff <ar@logiceditor.com>2021-08-16 16:35:07 +0300
commit7668d9076da6e2ae9e220eafb5c6ee1e933c088d (patch)
treec2d8e3bdd0d6d189572bd50d8d9a078097fcc3b5 /lpcode.c
parent635a51b5c88e958c27b26e049c639ea774946367 (diff)
downloadlpeglabel-7668d9076da6e2ae9e220eafb5c6ee1e933c088d.tar.gz
lpeglabel-7668d9076da6e2ae9e220eafb5c6ee1e933c088d.tar.bz2
lpeglabel-7668d9076da6e2ae9e220eafb5c6ee1e933c088d.zip
change prefix of source files from 'lp' to 'lpl'
To eliminate conflicts with the original LPeg: > $ lua -e "require 'lpeg'; require 'lpeglabel'; for _ in (require'lxsh').lexers.lua.gmatch('1') do end" lua: lpvm.c:347: match: Assertion `stack > getstackbase(L, ptop) && (stack - 1)->s != NULL' failed. Aborted
Diffstat (limited to 'lpcode.c')
-rw-r--r--lpcode.c1081
1 files changed, 0 insertions, 1081 deletions
diff --git a/lpcode.c b/lpcode.c
deleted file mode 100644
index 78c6566..0000000
--- a/lpcode.c
+++ /dev/null
@@ -1,1081 +0,0 @@
1/*
2** $Id: lpcode.c $
3** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license)
4*/
5
6#include <limits.h>
7
8
9#include "lua.h"
10#include "lauxlib.h"
11
12#include "lptypes.h"
13#include "lpcode.h"
14
15
16/* signals a "no-instruction */
17#define NOINST -1
18
19
20
21static const Charset fullset_ =
22 {{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
23 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
24 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
25 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}};
26
27static const Charset *fullset = &fullset_;
28
29/*
30** {======================================================
31** Analysis and some optimizations
32** =======================================================
33*/
34
35/*
36** Check whether a charset is empty (returns IFail), singleton (IChar),
37** full (IAny), or none of those (ISet). When singleton, '*c' returns
38** which character it is. (When generic set, the set was the input,
39** so there is no need to return it.)
40*/
41static Opcode charsettype (const byte *cs, int *c) {
42 int count = 0; /* number of characters in the set */
43 int i;
44 int candidate = -1; /* candidate position for the singleton char */
45 for (i = 0; i < CHARSETSIZE; i++) { /* for each byte */
46 int b = cs[i];
47 if (b == 0) { /* is byte empty? */
48 if (count > 1) /* was set neither empty nor singleton? */
49 return ISet; /* neither full nor empty nor singleton */
50 /* else set is still empty or singleton */
51 }
52 else if (b == 0xFF) { /* is byte full? */
53 if (count < (i * BITSPERCHAR)) /* was set not full? */
54 return ISet; /* neither full nor empty nor singleton */
55 else count += BITSPERCHAR; /* set is still full */
56 }
57 else if ((b & (b - 1)) == 0) { /* has byte only one bit? */
58 if (count > 0) /* was set not empty? */
59 return ISet; /* neither full nor empty nor singleton */
60 else { /* set has only one char till now; track it */
61 count++;
62 candidate = i;
63 }
64 }
65 else return ISet; /* byte is neither empty, full, nor singleton */
66 }
67 switch (count) {
68 case 0: return IFail; /* empty set */
69 case 1: { /* singleton; find character bit inside byte */
70 int b = cs[candidate];
71 *c = candidate * BITSPERCHAR;
72 if ((b & 0xF0) != 0) { *c += 4; b >>= 4; }
73 if ((b & 0x0C) != 0) { *c += 2; b >>= 2; }
74 if ((b & 0x02) != 0) { *c += 1; }
75 return IChar;
76 }
77 default: {
78 assert(count == CHARSETSIZE * BITSPERCHAR); /* full set */
79 return IAny;
80 }
81 }
82}
83
84
85/*
86** A few basic operations on Charsets
87*/
88static void cs_complement (Charset *cs) {
89 loopset(i, cs->cs[i] = ~cs->cs[i]);
90}
91
92static int cs_equal (const byte *cs1, const byte *cs2) {
93 loopset(i, if (cs1[i] != cs2[i]) return 0);
94 return 1;
95}
96
97static int cs_disjoint (const Charset *cs1, const Charset *cs2) {
98 loopset(i, if ((cs1->cs[i] & cs2->cs[i]) != 0) return 0;)
99 return 1;
100}
101
102
103/*
104** If 'tree' is a 'char' pattern (TSet, TChar, TAny), convert it into a
105** charset and return 1; else return 0.
106*/
107int tocharset (TTree *tree, Charset *cs) {
108 switch (tree->tag) {
109 case TSet: { /* copy set */
110 loopset(i, cs->cs[i] = treebuffer(tree)[i]);
111 return 1;
112 }
113 case TChar: { /* only one char */
114 assert(0 <= tree->u.n && tree->u.n <= UCHAR_MAX);
115 loopset(i, cs->cs[i] = 0); /* erase all chars */
116 setchar(cs->cs, tree->u.n); /* add that one */
117 return 1;
118 }
119 case TAny: {
120 loopset(i, cs->cs[i] = 0xFF); /* add all characters to the set */
121 return 1;
122 }
123 default: return 0;
124 }
125}
126
127
128/*
129** Visit a TCall node taking care to stop recursion. If node not yet
130** visited, return 'f(sib2(tree))', otherwise return 'def' (default
131** value)
132*/
133static int callrecursive (TTree *tree, int f (TTree *t), int def) {
134 int key = tree->key;
135 assert(tree->tag == TCall);
136 assert(sib2(tree)->tag == TRule);
137 if (key == 0) /* node already visited? */
138 return def; /* return default value */
139 else { /* first visit */
140 int result;
141 tree->key = 0; /* mark call as already visited */
142 result = f(sib2(tree)); /* go to called rule */
143 tree->key = key; /* restore tree */
144 return result;
145 }
146}
147
148
149/*
150** Check whether a pattern tree has captures
151*/
152int hascaptures (TTree *tree) {
153 tailcall:
154 switch (tree->tag) {
155 case TCapture: case TRunTime:
156 return 1;
157 case TCall:
158 return callrecursive(tree, hascaptures, 0);
159 case TRule: /* do not follow siblings */
160 tree = sib1(tree); goto tailcall;
161 case TOpenCall: assert(0);
162 default: {
163 switch (numsiblings[tree->tag]) {
164 case 1: /* return hascaptures(sib1(tree)); */
165 tree = sib1(tree); goto tailcall;
166 case 2:
167 if (hascaptures(sib1(tree)))
168 return 1;
169 /* else return hascaptures(sib2(tree)); */
170 tree = sib2(tree); goto tailcall;
171 default: assert(numsiblings[tree->tag] == 0); return 0;
172 }
173 }
174 }
175}
176
177
178/*
179** Checks how a pattern behaves regarding the empty string,
180** in one of two different ways:
181** A pattern is *nullable* if it can match without consuming any character;
182** A pattern is *nofail* if it never fails for any string
183** (including the empty string).
184** The difference is only for predicates and run-time captures;
185** for other patterns, the two properties are equivalent.
186** (With predicates, &'a' is nullable but not nofail. Of course,
187** nofail => nullable.)
188** These functions are all convervative in the following way:
189** p is nullable => nullable(p)
190** nofail(p) => p cannot fail
191** The function assumes that TOpenCall is not nullable;
192** this will be checked again when the grammar is fixed.
193** Run-time captures can do whatever they want, so the result
194** is conservative.
195*/
196int checkaux (TTree *tree, int pred) {
197 tailcall:
198 switch (tree->tag) {
199 case TChar: case TSet: case TAny: case TUTFR:
200 case TFalse: case TOpenCall: case TThrow: /* labeled failure */
201 return 0; /* not nullable */
202 case TRep: case TTrue:
203 return 1; /* no fail */
204 case TNot: case TBehind: /* can match empty, but can fail */
205 if (pred == PEnofail) return 0;
206 else return 1; /* PEnullable */
207 case TAnd: /* can match empty; fail iff body does */
208 if (pred == PEnullable) return 1;
209 /* else return checkaux(sib1(tree), pred); */
210 tree = sib1(tree); goto tailcall;
211 case TRunTime: /* can fail; match empty iff body does */
212 if (pred == PEnofail) return 0;
213 /* else return checkaux(sib1(tree), pred); */
214 tree = sib1(tree); goto tailcall;
215 case TSeq:
216 if (!checkaux(sib1(tree), pred)) return 0;
217 /* else return checkaux(sib2(tree), pred); */
218 tree = sib2(tree); goto tailcall;
219 case TChoice:
220 if (checkaux(sib2(tree), pred)) return 1;
221 /* else return checkaux(sib1(tree), pred); */
222 tree = sib1(tree); goto tailcall;
223 case TCapture: case TGrammar: case TRule: case TXInfo:
224 /* return checkaux(sib1(tree), pred); */
225 tree = sib1(tree); goto tailcall;
226 case TCall: /* return checkaux(sib2(tree), pred); */
227 tree = sib2(tree); goto tailcall;
228 default: assert(0); return 0;
229 }
230}
231
232
233/*
234** number of characters to match a pattern (or -1 if variable)
235*/
236int fixedlen (TTree *tree) {
237 int len = 0; /* to accumulate in tail calls */
238 tailcall:
239 switch (tree->tag) {
240 case TChar: case TSet: case TAny:
241 return len + 1;
242 case TUTFR:
243 return (tree->cap == sib1(tree)->cap) ? len + tree->cap : -1;
244 case TFalse: case TTrue: case TNot: case TAnd: case TBehind:
245 return len;
246 case TRep: case TRunTime: case TOpenCall:
247 case TThrow: /* labeled failure */
248 return -1;
249 case TCapture: case TRule: case TGrammar: case TXInfo:
250 /* return fixedlen(sib1(tree)); */
251 tree = sib1(tree); goto tailcall;
252 case TCall: {
253 int n1 = callrecursive(tree, fixedlen, -1);
254 if (n1 < 0)
255 return -1;
256 else
257 return len + n1;
258 }
259 case TSeq: {
260 int n1 = fixedlen(sib1(tree));
261 if (n1 < 0)
262 return -1;
263 /* else return fixedlen(sib2(tree)) + len; */
264 len += n1; tree = sib2(tree); goto tailcall;
265 }
266 case TChoice: {
267 int n1 = fixedlen(sib1(tree));
268 int n2 = fixedlen(sib2(tree));
269 if (n1 != n2 || n1 < 0)
270 return -1;
271 else
272 return len + n1;
273 }
274 default: assert(0); return 0;
275 };
276}
277
278
279/*
280** Computes the 'first set' of a pattern.
281** The result is a conservative aproximation:
282** match p ax -> x (for some x) ==> a belongs to first(p)
283** or
284** a not in first(p) ==> match p ax -> fail (for all x)
285**
286** The set 'follow' is the first set of what follows the
287** pattern (full set if nothing follows it).
288**
289** The function returns 0 when this resulting set can be used for
290** test instructions that avoid the pattern altogether.
291** A non-zero return can happen for two reasons:
292** 1) match p '' -> '' ==> return has bit 1 set
293** (tests cannot be used because they would always fail for an empty input);
294** 2) there is a match-time capture ==> return has bit 2 set
295** (optimizations should not bypass match-time captures).
296*/
297static int getfirst (TTree *tree, const Charset *follow, Charset *firstset) {
298 tailcall:
299 switch (tree->tag) {
300 case TChar: case TSet: case TAny: {
301 tocharset(tree, firstset);
302 return 0;
303 }
304 case TUTFR: {
305 int c;
306 loopset(i, firstset->cs[i] = 0); /* erase all chars */
307 for (c = tree->key; c <= sib1(tree)->key; c++)
308 setchar(firstset->cs, c);
309 return 0;
310 }
311 case TTrue: {
312 loopset(i, firstset->cs[i] = follow->cs[i]);
313 return 1; /* accepts the empty string */
314 }
315 case TFalse: {
316 loopset(i, firstset->cs[i] = 0);
317 return 0;
318 }
319 case TThrow: { /* labeled failure: must always throw the label */
320 loopset(i, firstset->cs[i] = fullset->cs[i]);
321 return 1;
322 }
323 case TChoice: {
324 Charset csaux;
325 int e1 = getfirst(sib1(tree), follow, firstset);
326 int e2 = getfirst(sib2(tree), follow, &csaux);
327 loopset(i, firstset->cs[i] |= csaux.cs[i]);
328 return e1 | e2;
329 }
330 case TSeq: {
331 if (!nullable(sib1(tree))) {
332 /* when p1 is not nullable, p2 has nothing to contribute;
333 return getfirst(sib1(tree), fullset, firstset); */
334 tree = sib1(tree); follow = fullset; goto tailcall;
335 }
336 else { /* FIRST(p1 p2, fl) = FIRST(p1, FIRST(p2, fl)) */
337 Charset csaux;
338 int e2 = getfirst(sib2(tree), follow, &csaux);
339 int e1 = getfirst(sib1(tree), &csaux, firstset);
340 if (e1 == 0) return 0; /* 'e1' ensures that first can be used */
341 else if ((e1 | e2) & 2) /* one of the children has a matchtime? */
342 return 2; /* pattern has a matchtime capture */
343 else return e2; /* else depends on 'e2' */
344 }
345 }
346 case TRep: {
347 getfirst(sib1(tree), follow, firstset);
348 loopset(i, firstset->cs[i] |= follow->cs[i]);
349 return 1; /* accept the empty string */
350 }
351 case TCapture: case TGrammar: case TRule: case TXInfo: {
352 /* return getfirst(sib1(tree), follow, firstset); */
353 tree = sib1(tree); goto tailcall;
354 }
355 case TRunTime: { /* function invalidates any follow info. */
356 int e = getfirst(sib1(tree), fullset, firstset);
357 if (e) return 2; /* function is not "protected"? */
358 else return 0; /* pattern inside capture ensures first can be used */
359 }
360 case TCall: {
361 /* return getfirst(sib2(tree), follow, firstset); */
362 tree = sib2(tree); goto tailcall;
363 }
364 case TAnd: {
365 int e = getfirst(sib1(tree), follow, firstset);
366 loopset(i, firstset->cs[i] &= follow->cs[i]);
367 return e;
368 }
369 case TNot: {
370 if (tocharset(sib1(tree), firstset)) {
371 cs_complement(firstset);
372 return 1;
373 }
374 /* else go through */
375 }
376 case TBehind: { /* instruction gives no new information */
377 /* call 'getfirst' only to check for math-time captures */
378 int e = getfirst(sib1(tree), follow, firstset);
379 loopset(i, firstset->cs[i] = follow->cs[i]); /* uses follow */
380 return e | 1; /* always can accept the empty string */
381 }
382 default: assert(0); return 0;
383 }
384}
385
386
387/*
388** If 'headfail(tree)' true, then 'tree' can fail only depending on the
389** next character of the subject.
390*/
391static int headfail (TTree *tree) {
392 tailcall:
393 switch (tree->tag) {
394 case TChar: case TSet: case TAny: case TFalse:
395 return 1;
396 case TTrue: case TRep: case TRunTime: case TNot:
397 case TBehind: case TUTFR:
398 case TThrow: /* labeled failure: must always throw the label */
399 return 0;
400 case TCapture: case TGrammar: case TRule: case TXInfo: case TAnd:
401 tree = sib1(tree); goto tailcall; /* return headfail(sib1(tree)); */
402 case TCall:
403 tree = sib2(tree); goto tailcall; /* return headfail(sib2(tree)); */
404 case TSeq:
405 if (!nofail(sib2(tree))) return 0;
406 /* else return headfail(sib1(tree)); */
407 tree = sib1(tree); goto tailcall;
408 case TChoice:
409 if (!headfail(sib1(tree))) return 0;
410 /* else return headfail(sib2(tree)); */
411 tree = sib2(tree); goto tailcall;
412 default: assert(0); return 0;
413 }
414}
415
416
417/*
418** Check whether the code generation for the given tree can benefit
419** from a follow set (to avoid computing the follow set when it is
420** not needed)
421*/
422static int needfollow (TTree *tree) {
423 tailcall:
424 switch (tree->tag) {
425 case TChar: case TSet: case TAny: case TUTFR:
426 case TFalse: case TTrue: case TAnd: case TNot:
427 case TRunTime: case TGrammar: case TCall: case TBehind:
428 case TThrow: /* (?)labeled failure */
429 return 0;
430 case TChoice: case TRep:
431 return 1;
432 case TCapture:
433 tree = sib1(tree); goto tailcall;
434 case TSeq:
435 tree = sib2(tree); goto tailcall;
436 default: assert(0); return 0;
437 }
438}
439
440/* }====================================================== */
441
442
443
444/*
445** {======================================================
446** Code generation
447** =======================================================
448*/
449
450
451/*
452** size of an instruction
453*/
454int sizei (const Instruction *i) {
455 switch((Opcode)i->i.code) {
456 case ISet: case ISpan: return CHARSETINSTSIZE;
457 case ITestSet: return CHARSETINSTSIZE + 1;
458 case ITestChar: case ITestAny: case IChoice: case IJmp: case ICall:
459 case IOpenCall: case ICommit: case IPartialCommit: case IBackCommit:
460 case IUTFR:
461 return 2;
462 case IThrow: case IPredChoice: /* labeled failure */
463 return 2;
464 case IThrowRec: /* labeled failure */
465 return 3;
466 default: return 1;
467 }
468}
469
470
471/*
472** state for the compiler
473*/
474typedef struct CompileState {
475 Pattern *p; /* pattern being compiled */
476 int ncode; /* next position in p->code to be filled */
477 lua_State *L;
478} CompileState;
479
480
481/*
482** code generation is recursive; 'opt' indicates that the code is being
483** generated as the last thing inside an optional pattern (so, if that
484** code is optional too, it can reuse the 'IChoice' already in place for
485** the outer pattern). 'tt' points to a previous test protecting this
486** code (or NOINST). 'fl' is the follow set of the pattern.
487*/
488static void codegen (CompileState *compst, TTree *tree, int opt, int tt,
489 const Charset *fl);
490
491
492void realloccode (lua_State *L, Pattern *p, int nsize) {
493 void *ud;
494 lua_Alloc f = lua_getallocf(L, &ud);
495 void *newblock = f(ud, p->code, p->codesize * sizeof(Instruction),
496 nsize * sizeof(Instruction));
497 if (newblock == NULL && nsize > 0)
498 luaL_error(L, "not enough memory");
499 p->code = (Instruction *)newblock;
500 p->codesize = nsize;
501}
502
503
504static int nextinstruction (CompileState *compst) {
505 int size = compst->p->codesize;
506 if (compst->ncode >= size)
507 realloccode(compst->L, compst->p, size * 2);
508 return compst->ncode++;
509}
510
511
512#define getinstr(cs,i) ((cs)->p->code[i])
513
514
515static int addinstruction (CompileState *compst, Opcode op, int aux) {
516 int i = nextinstruction(compst);
517 getinstr(compst, i).i.code = op;
518 getinstr(compst, i).i.aux = aux;
519 return i;
520}
521
522
523/*
524** Add an instruction followed by space for an offset (to be set later)
525*/
526static int addoffsetinst (CompileState *compst, Opcode op) {
527 int i = addinstruction(compst, op, 0); /* instruction */
528 addinstruction(compst, (Opcode)0, 0); /* open space for offset */
529 assert(op == ITestSet || sizei(&getinstr(compst, i)) == 2 ||
530 op == IThrowRec); /* labeled failure */
531
532 return i;
533}
534
535
536/* labeled failure */
537static void codethrow (CompileState *compst, TTree *throw) {
538 int recov, aux, n;
539 if (throw->u.ps != 0) {
540 recov = addoffsetinst(compst, IThrowRec);
541 assert(sib1(sib2(throw))->tag == TXInfo);
542 n = sib1(sib2(throw))->u.n;
543 } else {
544 recov = addinstruction(compst, IThrow, 0);
545 n = -1;
546 }
547 aux = nextinstruction(compst);
548 getinstr(compst, aux).i.key = throw->key; /* next instruction keeps only rule name */
549 getinstr(compst, recov).i.key = n; /* rule number */
550}
551/* labeled failure */
552
553
554/*
555** Set the offset of an instruction
556*/
557static void setoffset (CompileState *compst, int instruction, int offset) {
558 getinstr(compst, instruction + 1).offset = offset;
559}
560
561
562static void codeutfr (CompileState *compst, TTree *tree) {
563 int i = addoffsetinst(compst, IUTFR);
564 int to = sib1(tree)->u.n;
565 assert(sib1(tree)->tag == TXInfo);
566 getinstr(compst, i + 1).offset = tree->u.n;
567 getinstr(compst, i).i.aux = to & 0xff;
568 getinstr(compst, i).i.key = to >> 8;
569}
570
571
572/*
573** Add a capture instruction:
574** 'op' is the capture instruction; 'cap' the capture kind;
575** 'key' the key into ktable; 'aux' is the optional capture offset
576**
577*/
578static int addinstcap (CompileState *compst, Opcode op, int cap, int key,
579 int aux) {
580 int i = addinstruction(compst, op, joinkindoff(cap, aux));
581 getinstr(compst, i).i.key = key;
582 return i;
583}
584
585
586#define gethere(compst) ((compst)->ncode)
587
588#define target(code,i) ((i) + code[i + 1].offset)
589
590
591/*
592** Patch 'instruction' to jump to 'target'
593*/
594static void jumptothere (CompileState *compst, int instruction, int target) {
595 if (instruction >= 0)
596 setoffset(compst, instruction, target - instruction);
597}
598
599
600/*
601** Patch 'instruction' to jump to current position
602*/
603static void jumptohere (CompileState *compst, int instruction) {
604 jumptothere(compst, instruction, gethere(compst));
605}
606
607
608/*
609** Code an IChar instruction, or IAny if there is an equivalent
610** test dominating it
611*/
612static void codechar (CompileState *compst, int c, int tt) {
613 if (tt >= 0 && getinstr(compst, tt).i.code == ITestChar &&
614 getinstr(compst, tt).i.aux == c)
615 addinstruction(compst, IAny, 0);
616 else
617 addinstruction(compst, IChar, c);
618}
619
620
621/*
622** Add a charset posfix to an instruction
623*/
624static void addcharset (CompileState *compst, const byte *cs) {
625 int p = gethere(compst);
626 int i;
627 for (i = 0; i < (int)CHARSETINSTSIZE - 1; i++)
628 nextinstruction(compst); /* space for buffer */
629 /* fill buffer with charset */
630 loopset(j, getinstr(compst, p).buff[j] = cs[j]);
631}
632
633
634/*
635** code a char set, optimizing unit sets for IChar, "complete"
636** sets for IAny, and empty sets for IFail; also use an IAny
637** when instruction is dominated by an equivalent test.
638*/
639static void codecharset (CompileState *compst, const byte *cs, int tt) {
640 int c = 0; /* (=) to avoid warnings */
641 Opcode op = charsettype(cs, &c);
642 switch (op) {
643 case IChar: codechar(compst, c, tt); break;
644 case ISet: { /* non-trivial set? */
645 if (tt >= 0 && getinstr(compst, tt).i.code == ITestSet &&
646 cs_equal(cs, getinstr(compst, tt + 2).buff))
647 addinstruction(compst, IAny, 0);
648 else {
649 addinstruction(compst, ISet, 0);
650 addcharset(compst, cs);
651 }
652 break;
653 }
654 default: addinstruction(compst, op, c); break;
655 }
656}
657
658
659/*
660** code a test set, optimizing unit sets for ITestChar, "complete"
661** sets for ITestAny, and empty sets for IJmp (always fails).
662** 'e' is true iff test should accept the empty string. (Test
663** instructions in the current VM never accept the empty string.)
664*/
665static int codetestset (CompileState *compst, Charset *cs, int e) {
666 if (e) return NOINST; /* no test */
667 else {
668 int c = 0;
669 Opcode op = charsettype(cs->cs, &c);
670 switch (op) {
671 case IFail: return addoffsetinst(compst, IJmp); /* always jump */
672 case IAny: return addoffsetinst(compst, ITestAny);
673 case IChar: {
674 int i = addoffsetinst(compst, ITestChar);
675 getinstr(compst, i).i.aux = c;
676 return i;
677 }
678 case ISet: {
679 int i = addoffsetinst(compst, ITestSet);
680 addcharset(compst, cs->cs);
681 return i;
682 }
683 default: assert(0); return 0;
684 }
685 }
686}
687
688
689/*
690** Find the final destination of a sequence of jumps
691*/
692static int finaltarget (Instruction *code, int i) {
693 while (code[i].i.code == IJmp)
694 i = target(code, i);
695 return i;
696}
697
698
699/*
700** final label (after traversing any jumps)
701*/
702static int finallabel (Instruction *code, int i) {
703 return finaltarget(code, target(code, i));
704}
705
706
707/*
708** <behind(p)> == behind n; <p> (where n = fixedlen(p))
709*/
710static void codebehind (CompileState *compst, TTree *tree) {
711 if (tree->u.n > 0)
712 addinstruction(compst, IBehind, tree->u.n);
713 codegen(compst, sib1(tree), 0, NOINST, fullset);
714}
715
716
717/*
718** Choice; optimizations:
719** - when p1 is headfail or when first(p1) and first(p2) are disjoint,
720** than a character not in first(p1) cannot go to p1 and a character
721** in first(p1) cannot go to p2, either because p1 will accept
722** (headfail) or because it is not in first(p2) (disjoint).
723** (The second case is not valid if p1 accepts the empty string,
724** as then there is no character at all...)
725** - when p2 is empty and opt is true; a IPartialCommit can reuse
726** the Choice already active in the stack.
727*/
728static void codechoice (CompileState *compst, TTree *p1, TTree *p2, int opt,
729 const Charset *fl) {
730 int emptyp2 = (p2->tag == TTrue);
731 Charset cs1, cs2;
732 int e1 = getfirst(p1, fullset, &cs1);
733 if (headfail(p1) ||
734 (!e1 && (getfirst(p2, fl, &cs2), cs_disjoint(&cs1, &cs2)))) {
735 /* <p1 / p2> == test (fail(p1)) -> L1 ; p1 ; jmp L2; L1: p2; L2: */
736 int test = codetestset(compst, &cs1, 0);
737 int jmp = NOINST;
738 codegen(compst, p1, 0, test, fl);
739 if (!emptyp2)
740 jmp = addoffsetinst(compst, IJmp);
741 jumptohere(compst, test);
742 codegen(compst, p2, opt, NOINST, fl);
743 jumptohere(compst, jmp);
744 }
745 else if (opt && emptyp2) {
746 /* p1? == IPartialCommit; p1 */
747 jumptohere(compst, addoffsetinst(compst, IPartialCommit));
748 codegen(compst, p1, 1, NOINST, fullset);
749 }
750 else {
751 /* <p1 / p2> ==
752 test(first(p1)) -> L1; choice L1; <p1>; commit L2; L1: <p2>; L2: */
753 int pcommit;
754 int test = codetestset(compst, &cs1, e1);
755 int pchoice = addoffsetinst(compst, IChoice);
756 codegen(compst, p1, emptyp2, test, fullset);
757 pcommit = addoffsetinst(compst, ICommit);
758 jumptohere(compst, pchoice);
759 jumptohere(compst, test);
760 codegen(compst, p2, opt, NOINST, fl);
761 jumptohere(compst, pcommit);
762 }
763}
764
765
766/*
767** And predicate
768** optimization: fixedlen(p) = n ==> <&p> == <p>; behind n
769** (valid only when 'p' has no captures)
770*/
771static void codeand (CompileState *compst, TTree *tree, int tt) {
772 /* labeled failure: optimization disabled bacause in case of a failure it
773 does not report the expected error position (the current subject position
774 when begin the matching of <&p>) */
775 /*int n = fixedlen(tree);
776 if (n >= 0 && n <= MAXBEHIND && !hascaptures(tree)) {
777 codegen(compst, tree, 0, tt, fullset);
778 if (n > 0)
779 addinstruction(compst, IBehind, n);
780 }
781 else */{ /* default: Choice L1; p1; BackCommit L2; L1: Fail; L2: */
782 int pcommit;
783 int pchoice = addoffsetinst(compst, IPredChoice); /* labeled failure */
784 codegen(compst, tree, 0, tt, fullset);
785 pcommit = addoffsetinst(compst, IBackCommit);
786 jumptohere(compst, pchoice);
787 addinstruction(compst, IFail, 0);
788 jumptohere(compst, pcommit);
789 }
790}
791
792
793/*
794** Captures: if pattern has fixed (and not too big) length, and it
795** has no nested captures, use a single IFullCapture instruction
796** after the match; otherwise, enclose the pattern with OpenCapture -
797** CloseCapture.
798*/
799static void codecapture (CompileState *compst, TTree *tree, int tt,
800 const Charset *fl) {
801 int len = fixedlen(sib1(tree));
802 if (len >= 0 && len <= MAXOFF && !hascaptures(sib1(tree))) {
803 codegen(compst, sib1(tree), 0, tt, fl);
804 addinstcap(compst, IFullCapture, tree->cap, tree->key, len);
805 }
806 else {
807 addinstcap(compst, IOpenCapture, tree->cap, tree->key, 0);
808 codegen(compst, sib1(tree), 0, tt, fl);
809 addinstcap(compst, ICloseCapture, Cclose, 0, 0);
810 }
811}
812
813
814static void coderuntime (CompileState *compst, TTree *tree, int tt) {
815 addinstcap(compst, IOpenCapture, Cgroup, tree->key, 0);
816 codegen(compst, sib1(tree), 0, tt, fullset);
817 addinstcap(compst, ICloseRunTime, Cclose, 0, 0);
818}
819
820
821/*
822** Repetion; optimizations:
823** When pattern is a charset, can use special instruction ISpan.
824** When pattern is head fail, or if it starts with characters that
825** are disjoint from what follows the repetions, a simple test
826** is enough (a fail inside the repetition would backtrack to fail
827** again in the following pattern, so there is no need for a choice).
828** When 'opt' is true, the repetion can reuse the Choice already
829** active in the stack.
830*/
831static void coderep (CompileState *compst, TTree *tree, int opt,
832 const Charset *fl) {
833 Charset st;
834 if (tocharset(tree, &st)) {
835 addinstruction(compst, ISpan, 0);
836 addcharset(compst, st.cs);
837 }
838 else {
839 int e1 = getfirst(tree, fullset, &st);
840 if (headfail(tree) || (!e1 && cs_disjoint(&st, fl))) {
841 /* L1: test (fail(p1)) -> L2; <p>; jmp L1; L2: */
842 int jmp;
843 int test = codetestset(compst, &st, 0);
844 codegen(compst, tree, 0, test, fullset);
845 jmp = addoffsetinst(compst, IJmp);
846 jumptohere(compst, test);
847 jumptothere(compst, jmp, test);
848 }
849 else {
850 /* test(fail(p1)) -> L2; choice L2; L1: <p>; partialcommit L1; L2: */
851 /* or (if 'opt'): partialcommit L1; L1: <p>; partialcommit L1; */
852 int commit, l2;
853 int test = codetestset(compst, &st, e1);
854 int pchoice = NOINST;
855 if (opt)
856 jumptohere(compst, addoffsetinst(compst, IPartialCommit));
857 else
858 pchoice = addoffsetinst(compst, IChoice);
859 l2 = gethere(compst);
860 codegen(compst, tree, 0, NOINST, fullset);
861 commit = addoffsetinst(compst, IPartialCommit);
862 jumptothere(compst, commit, l2);
863 jumptohere(compst, pchoice);
864 jumptohere(compst, test);
865 }
866 }
867}
868
869
870/*
871** Not predicate; optimizations:
872** In any case, if first test fails, 'not' succeeds, so it can jump to
873** the end. If pattern is headfail, that is all (it cannot fail
874** in other parts); this case includes 'not' of simple sets. Otherwise,
875** use the default code (a choice plus a failtwice).
876*/
877static void codenot (CompileState *compst, TTree *tree) {
878 Charset st;
879 int e = getfirst(tree, fullset, &st);
880 int test = codetestset(compst, &st, e);
881 if (headfail(tree)) /* test (fail(p1)) -> L1; fail; L1: */
882 addinstruction(compst, IFail, 0);
883 else {
884 /* test(fail(p))-> L1; choice L1; <p>; failtwice; L1: */
885 int pchoice = addoffsetinst(compst, IPredChoice); /* labeled failure */
886 codegen(compst, tree, 0, NOINST, fullset);
887 addinstruction(compst, IFailTwice, 0);
888 jumptohere(compst, pchoice);
889 }
890 jumptohere(compst, test);
891}
892
893
894/*
895** change open calls to calls, using list 'positions' to find
896** correct offsets; also optimize tail calls
897*/
898static void correctcalls (CompileState *compst, int *positions,
899 int from, int to) {
900 int i;
901 Instruction *code = compst->p->code;
902 for (i = from; i < to; i += sizei(&code[i])) {
903 if (code[i].i.code == IOpenCall) {
904 int n = code[i].i.key; /* rule number */
905 int rule = positions[n]; /* rule position */
906 assert(rule == from || code[rule - 1].i.code == IRet);
907 if (code[finaltarget(code, i + 2)].i.code == IRet) /* call; ret ? */
908 code[i].i.code = IJmp; /* tail call */
909 else
910 code[i].i.code = ICall;
911 jumptothere(compst, i, rule); /* call jumps to respective rule */
912 } else if (code[i].i.code == IThrowRec) { /* labeled failure */
913 int n = code[i].i.key; /* rule number */
914 int rule = positions[n]; /* rule position */
915 assert(rule == from || code[rule - 1].i.code == IRet);
916 jumptothere(compst, i, rule); /* call jumps to respective rule */
917 }
918
919 }
920 assert(i == to);
921}
922
923
924/*
925** Code for a grammar:
926** call L1; jmp L2; L1: rule 1; ret; rule 2; ret; ...; L2:
927*/
928static void codegrammar (CompileState *compst, TTree *grammar) {
929 int positions[MAXRULES];
930 int rulenumber = 0;
931 TTree *rule;
932 int firstcall = addoffsetinst(compst, ICall); /* call initial rule */
933 int jumptoend = addoffsetinst(compst, IJmp); /* jump to the end */
934 int start = gethere(compst); /* here starts the initial rule */
935 jumptohere(compst, firstcall);
936 for (rule = sib1(grammar); rule->tag == TRule; rule = sib2(rule)) {
937 TTree *r = sib1(rule);
938 assert(r->tag == TXInfo);
939 positions[rulenumber++] = gethere(compst); /* save rule position */
940 codegen(compst, sib1(r), 0, NOINST, fullset); /* code rule */
941 addinstruction(compst, IRet, 0);
942 }
943 assert(rule->tag == TTrue);
944 jumptohere(compst, jumptoend);
945 correctcalls(compst, positions, start, gethere(compst));
946}
947
948
949static void codecall (CompileState *compst, TTree *call) {
950 int c = addoffsetinst(compst, IOpenCall); /* to be corrected later */
951 assert(sib1(sib2(call))->tag == TXInfo);
952 getinstr(compst, c).i.key = sib1(sib2(call))->u.n; /* rule number */
953}
954
955
956/*
957** Code first child of a sequence
958** (second child is called in-place to allow tail call)
959** Return 'tt' for second child
960*/
961static int codeseq1 (CompileState *compst, TTree *p1, TTree *p2,
962 int tt, const Charset *fl) {
963 if (needfollow(p1)) {
964 Charset fl1;
965 getfirst(p2, fl, &fl1); /* p1 follow is p2 first */
966 codegen(compst, p1, 0, tt, &fl1);
967 }
968 else /* use 'fullset' as follow */
969 codegen(compst, p1, 0, tt, fullset);
970 if (fixedlen(p1) != 0) /* can 'p1' consume anything? */
971 return NOINST; /* invalidate test */
972 else return tt; /* else 'tt' still protects sib2 */
973}
974
975
976/*
977** Main code-generation function: dispatch to auxiliar functions
978** according to kind of tree. ('needfollow' should return true
979** only for consructions that use 'fl'.)
980*/
981static void codegen (CompileState *compst, TTree *tree, int opt, int tt,
982 const Charset *fl) {
983 tailcall:
984 switch (tree->tag) {
985 case TChar: codechar(compst, tree->u.n, tt); break;
986 case TAny: addinstruction(compst, IAny, 0); break;
987 case TSet: codecharset(compst, treebuffer(tree), tt); break;
988 case TTrue: break;
989 case TFalse: addinstruction(compst, IFail, 0); break;
990 case TUTFR: codeutfr(compst, tree); break;
991 case TChoice: codechoice(compst, sib1(tree), sib2(tree), opt, fl); break;
992 case TRep: coderep(compst, sib1(tree), opt, fl); break;
993 case TBehind: codebehind(compst, tree); break;
994 case TNot: codenot(compst, sib1(tree)); break;
995 case TAnd: codeand(compst, sib1(tree), tt); break;
996 case TCapture: codecapture(compst, tree, tt, fl); break;
997 case TRunTime: coderuntime(compst, tree, tt); break;
998 case TGrammar: codegrammar(compst, tree); break;
999 case TCall: codecall(compst, tree); break;
1000 case TSeq: {
1001 tt = codeseq1(compst, sib1(tree), sib2(tree), tt, fl); /* code 'p1' */
1002 /* codegen(compst, p2, opt, tt, fl); */
1003 tree = sib2(tree); goto tailcall;
1004 }
1005 case TThrow: { /* labeled failure */
1006 codethrow(compst, tree);
1007 break;
1008 }
1009 default: assert(0);
1010 }
1011}
1012
1013
1014/*
1015** Optimize jumps and other jump-like instructions.
1016** * Update labels of instructions with labels to their final
1017** destinations (e.g., choice L1; ... L1: jmp L2: becomes
1018** choice L2)
1019** * Jumps to other instructions that do jumps become those
1020** instructions (e.g., jump to return becomes a return; jump
1021** to commit becomes a commit)
1022*/
1023static void peephole (CompileState *compst) {
1024 Instruction *code = compst->p->code;
1025 int i;
1026 for (i = 0; i < compst->ncode; i += sizei(&code[i])) {
1027 redo:
1028 switch (code[i].i.code) {
1029 case IChoice: case ICall: case ICommit: case IPartialCommit:
1030 case IBackCommit: case ITestChar: case ITestSet:
1031 case ITestAny: { /* instructions with labels */
1032 jumptothere(compst, i, finallabel(code, i)); /* optimize label */
1033 break;
1034 }
1035 case IJmp: {
1036 int ft = finaltarget(code, i);
1037 switch (code[ft].i.code) { /* jumping to what? */
1038 case IRet: case IFail: case IFailTwice:
1039 case IEnd: { /* instructions with unconditional implicit jumps */
1040 code[i] = code[ft]; /* jump becomes that instruction */
1041 code[i + 1].i.code = IEmpty; /* 'no-op' for target position */
1042 break;
1043 }
1044 case ICommit: case IPartialCommit:
1045 case IBackCommit: { /* inst. with unconditional explicit jumps */
1046 int fft = finallabel(code, ft);
1047 code[i] = code[ft]; /* jump becomes that instruction... */
1048 jumptothere(compst, i, fft); /* but must correct its offset */
1049 goto redo; /* reoptimize its label */
1050 }
1051 default: {
1052 jumptothere(compst, i, ft); /* optimize label */
1053 break;
1054 }
1055 }
1056 break;
1057 }
1058 default: break;
1059 }
1060 }
1061 assert(code[i - 1].i.code == IEnd);
1062}
1063
1064
1065/*
1066** Compile a pattern
1067*/
1068Instruction *compile (lua_State *L, Pattern *p) {
1069 CompileState compst;
1070 compst.p = p; compst.ncode = 0; compst.L = L;
1071 realloccode(L, p, 2); /* minimum initial size */
1072 codegen(&compst, p->tree, 0, NOINST, fullset);
1073 addinstruction(&compst, IEnd, 0);
1074 realloccode(L, p, compst.ncode); /* set final size */
1075 peephole(&compst);
1076 return p->code;
1077}
1078
1079
1080/* }====================================================== */
1081