aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2023-04-25 16:08:15 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2023-04-25 16:08:15 -0300
commitdef10e7c009f71f99d6a11171d84fc27568f9b81 (patch)
treedcbfd5eb01e3a9d238c57911f80ac719b1a76547
parent37ff352e833ae3a543acb07b2bee56fee7bd1491 (diff)
downloadlpeg-def10e7c009f71f99d6a11171d84fc27568f9b81.tar.gz
lpeg-def10e7c009f71f99d6a11171d84fc27568f9b81.tar.bz2
lpeg-def10e7c009f71f99d6a11171d84fc27568f9b81.zip
Instruction array grows by factor of 1.5
-rw-r--r--lpcode.c27
1 files changed, 18 insertions, 9 deletions
diff --git a/lpcode.c b/lpcode.c
index 54bf586..ba6942f 100644
--- a/lpcode.c
+++ b/lpcode.c
@@ -496,11 +496,21 @@ void realloccode (lua_State *L, Pattern *p, int nsize) {
496} 496}
497 497
498 498
499static int nextinstruction (CompileState *compst) { 499/*
500** Add space for 'n' more instructions and return the index of
501** the first one.
502*/
503static int nextinstruction (CompileState *compst, int n) {
500 int size = compst->p->codesize; 504 int size = compst->p->codesize;
501 if (compst->ncode >= size) 505 int ncode = compst->ncode;
502 realloccode(compst->L, compst->p, size * 2); 506 if (ncode >= size - n) {
503 return compst->ncode++; 507 unsigned int nsize = size + (size >> 1) + n;
508 if (nsize > INT_MAX)
509 luaL_error(compst->L, "code too large");
510 realloccode(compst->L, compst->p, nsize);
511 }
512 compst->ncode = ncode + n;
513 return ncode;
504} 514}
505 515
506 516
@@ -508,7 +518,7 @@ static int nextinstruction (CompileState *compst) {
508 518
509 519
510static int addinstruction (CompileState *compst, Opcode op, int aux) { 520static int addinstruction (CompileState *compst, Opcode op, int aux) {
511 int i = nextinstruction(compst); 521 int i = nextinstruction(compst, 1);
512 getinstr(compst, i).i.code = op; 522 getinstr(compst, i).i.code = op;
513 getinstr(compst, i).i.aux1 = aux; 523 getinstr(compst, i).i.aux1 = aux;
514 return i; 524 return i;
@@ -598,7 +608,7 @@ static void codechar (CompileState *compst, int c, int tt) {
598*/ 608*/
599static void addcharset (CompileState *compst, int inst, const byte *cs, 609static void addcharset (CompileState *compst, int inst, const byte *cs,
600 const charsetinfo *info) { 610 const charsetinfo *info) {
601 int p = gethere(compst); 611 int p;
602 Instruction *I = &getinstr(compst, inst); 612 Instruction *I = &getinstr(compst, inst);
603 byte *charset; 613 byte *charset;
604 int isize = instsize(info->size); /* size in instructions */ 614 int isize = instsize(info->size); /* size in instructions */
@@ -606,8 +616,7 @@ static void addcharset (CompileState *compst, int inst, const byte *cs,
606 I->i.aux2.set.offset = info->aux1 * 8; /* offset in bits */ 616 I->i.aux2.set.offset = info->aux1 * 8; /* offset in bits */
607 I->i.aux2.set.size = isize; 617 I->i.aux2.set.size = isize;
608 I->i.aux1 = info->deflt; 618 I->i.aux1 = info->deflt;
609 for (i = 0; i < isize; i++) 619 p = nextinstruction(compst, isize); /* space for charset */
610 nextinstruction(compst); /* space for charset */
611 charset = getinstr(compst, p).buff; /* previous loop may reallocate things */ 620 charset = getinstr(compst, p).buff; /* previous loop may reallocate things */
612 for (i = 0; i < info->size; i++) 621 for (i = 0; i < info->size; i++)
613 charset[i] = cs[i + info->aux1]; /* fill buffer with charset */ 622 charset[i] = cs[i + info->aux1]; /* fill buffer with charset */
@@ -1105,7 +1114,7 @@ static void peephole (CompileState *compst) {
1105Instruction *compile (lua_State *L, Pattern *p) { 1114Instruction *compile (lua_State *L, Pattern *p) {
1106 CompileState compst; 1115 CompileState compst;
1107 compst.p = p; compst.ncode = 0; compst.L = L; 1116 compst.p = p; compst.ncode = 0; compst.L = L;
1108 realloccode(L, p, 2); /* minimum initial size */ 1117 realloccode(L, p, 4); /* minimum initial size */
1109 codegen(&compst, p->tree, 0, NOINST, fullset); 1118 codegen(&compst, p->tree, 0, NOINST, fullset);
1110 addinstruction(&compst, IEnd, 0); 1119 addinstruction(&compst, IEnd, 0);
1111 realloccode(L, p, compst.ncode); /* set final size */ 1120 realloccode(L, p, compst.ncode); /* set final size */