diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-04-21 15:29:53 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-04-21 15:29:53 -0300 |
| commit | 9f7183c280f310c0d0b49b7b9c3b8eac297fafa7 (patch) | |
| tree | 503392ee1a94f6446151a0b90babe8235efdb573 | |
| parent | 7b42a7b13f9c6655dfa7c5951de46dcf0642b0a6 (diff) | |
| download | lpeg-9f7183c280f310c0d0b49b7b9c3b8eac297fafa7.tar.gz lpeg-9f7183c280f310c0d0b49b7b9c3b8eac297fafa7.tar.bz2 lpeg-9f7183c280f310c0d0b49b7b9c3b8eac297fafa7.zip | |
Field Instruction.key put inside a union
So that we can get its space for other uses, if needed.
| -rw-r--r-- | lpcode.c | 16 | ||||
| -rw-r--r-- | lpprint.c | 10 | ||||
| -rw-r--r-- | lptypes.h | 4 | ||||
| -rw-r--r-- | lpvm.c | 10 | ||||
| -rw-r--r-- | lpvm.h | 14 |
5 files changed, 28 insertions, 26 deletions
| @@ -499,7 +499,7 @@ static int nextinstruction (CompileState *compst) { | |||
| 499 | static int addinstruction (CompileState *compst, Opcode op, int aux) { | 499 | static int addinstruction (CompileState *compst, Opcode op, int aux) { |
| 500 | int i = nextinstruction(compst); | 500 | int i = nextinstruction(compst); |
| 501 | getinstr(compst, i).i.code = op; | 501 | getinstr(compst, i).i.code = op; |
| 502 | getinstr(compst, i).i.aux = aux; | 502 | getinstr(compst, i).i.aux1 = aux; |
| 503 | return i; | 503 | return i; |
| 504 | } | 504 | } |
| 505 | 505 | ||
| @@ -528,8 +528,8 @@ static void codeutfr (CompileState *compst, TTree *tree) { | |||
| 528 | int to = sib1(tree)->u.n; | 528 | int to = sib1(tree)->u.n; |
| 529 | assert(sib1(tree)->tag == TXInfo); | 529 | assert(sib1(tree)->tag == TXInfo); |
| 530 | getinstr(compst, i + 1).offset = tree->u.n; | 530 | getinstr(compst, i + 1).offset = tree->u.n; |
| 531 | getinstr(compst, i).i.aux = to & 0xff; | 531 | getinstr(compst, i).i.aux1 = to & 0xff; |
| 532 | getinstr(compst, i).i.key = to >> 8; | 532 | getinstr(compst, i).i.aux2.key = to >> 8; |
| 533 | } | 533 | } |
| 534 | 534 | ||
| 535 | 535 | ||
| @@ -542,7 +542,7 @@ static void codeutfr (CompileState *compst, TTree *tree) { | |||
| 542 | static int addinstcap (CompileState *compst, Opcode op, int cap, int key, | 542 | static int addinstcap (CompileState *compst, Opcode op, int cap, int key, |
| 543 | int aux) { | 543 | int aux) { |
| 544 | int i = addinstruction(compst, op, joinkindoff(cap, aux)); | 544 | int i = addinstruction(compst, op, joinkindoff(cap, aux)); |
| 545 | getinstr(compst, i).i.key = key; | 545 | getinstr(compst, i).i.aux2.key = key; |
| 546 | return i; | 546 | return i; |
| 547 | } | 547 | } |
| 548 | 548 | ||
| @@ -575,7 +575,7 @@ static void jumptohere (CompileState *compst, int instruction) { | |||
| 575 | */ | 575 | */ |
| 576 | static void codechar (CompileState *compst, int c, int tt) { | 576 | static void codechar (CompileState *compst, int c, int tt) { |
| 577 | if (tt >= 0 && getinstr(compst, tt).i.code == ITestChar && | 577 | if (tt >= 0 && getinstr(compst, tt).i.code == ITestChar && |
| 578 | getinstr(compst, tt).i.aux == c) | 578 | getinstr(compst, tt).i.aux1 == c) |
| 579 | addinstruction(compst, IAny, 0); | 579 | addinstruction(compst, IAny, 0); |
| 580 | else | 580 | else |
| 581 | addinstruction(compst, IChar, c); | 581 | addinstruction(compst, IChar, c); |
| @@ -636,7 +636,7 @@ static int codetestset (CompileState *compst, Charset *cs, int e) { | |||
| 636 | case IAny: return addoffsetinst(compst, ITestAny); | 636 | case IAny: return addoffsetinst(compst, ITestAny); |
| 637 | case IChar: { | 637 | case IChar: { |
| 638 | int i = addoffsetinst(compst, ITestChar); | 638 | int i = addoffsetinst(compst, ITestChar); |
| 639 | getinstr(compst, i).i.aux = c; | 639 | getinstr(compst, i).i.aux1 = c; |
| 640 | return i; | 640 | return i; |
| 641 | } | 641 | } |
| 642 | case ISet: { | 642 | case ISet: { |
| @@ -862,7 +862,7 @@ static void correctcalls (CompileState *compst, int *positions, | |||
| 862 | Instruction *code = compst->p->code; | 862 | Instruction *code = compst->p->code; |
| 863 | for (i = from; i < to; i += sizei(&code[i])) { | 863 | for (i = from; i < to; i += sizei(&code[i])) { |
| 864 | if (code[i].i.code == IOpenCall) { | 864 | if (code[i].i.code == IOpenCall) { |
| 865 | int n = code[i].i.key; /* rule number */ | 865 | int n = code[i].i.aux2.key; /* rule number */ |
| 866 | int rule = positions[n]; /* rule position */ | 866 | int rule = positions[n]; /* rule position */ |
| 867 | assert(rule == from || code[rule - 1].i.code == IRet); | 867 | assert(rule == from || code[rule - 1].i.code == IRet); |
| 868 | if (code[finaltarget(code, i + 2)].i.code == IRet) /* call; ret ? */ | 868 | if (code[finaltarget(code, i + 2)].i.code == IRet) /* call; ret ? */ |
| @@ -904,7 +904,7 @@ static void codegrammar (CompileState *compst, TTree *grammar) { | |||
| 904 | static void codecall (CompileState *compst, TTree *call) { | 904 | static void codecall (CompileState *compst, TTree *call) { |
| 905 | int c = addoffsetinst(compst, IOpenCall); /* to be corrected later */ | 905 | int c = addoffsetinst(compst, IOpenCall); /* to be corrected later */ |
| 906 | assert(sib1(sib2(call))->tag == TXInfo); | 906 | assert(sib1(sib2(call))->tag == TXInfo); |
| 907 | getinstr(compst, c).i.key = sib1(sib2(call))->u.n; /* rule number */ | 907 | getinstr(compst, c).i.aux2.key = sib1(sib2(call))->u.n; /* rule number */ |
| 908 | } | 908 | } |
| 909 | 909 | ||
| 910 | 910 | ||
| @@ -62,11 +62,11 @@ void printinst (const Instruction *op, const Instruction *p) { | |||
| 62 | printf("%02ld: %s ", (long)(p - op), names[p->i.code]); | 62 | printf("%02ld: %s ", (long)(p - op), names[p->i.code]); |
| 63 | switch ((Opcode)p->i.code) { | 63 | switch ((Opcode)p->i.code) { |
| 64 | case IChar: { | 64 | case IChar: { |
| 65 | printf("'%c' (%02x)", p->i.aux, p->i.aux); | 65 | printf("'%c' (%02x)", p->i.aux1, p->i.aux1); |
| 66 | break; | 66 | break; |
| 67 | } | 67 | } |
| 68 | case ITestChar: { | 68 | case ITestChar: { |
| 69 | printf("'%c' (%02x)", p->i.aux, p->i.aux); printjmp(op, p); | 69 | printf("'%c' (%02x)", p->i.aux1, p->i.aux1); printjmp(op, p); |
| 70 | break; | 70 | break; |
| 71 | } | 71 | } |
| 72 | case IUTFR: { | 72 | case IUTFR: { |
| @@ -75,11 +75,11 @@ void printinst (const Instruction *op, const Instruction *p) { | |||
| 75 | } | 75 | } |
| 76 | case IFullCapture: { | 76 | case IFullCapture: { |
| 77 | printf("%s (size = %d) (idx = %d)", | 77 | printf("%s (size = %d) (idx = %d)", |
| 78 | capkind(getkind(p)), getoff(p), p->i.key); | 78 | capkind(getkind(p)), getoff(p), p->i.aux2.key); |
| 79 | break; | 79 | break; |
| 80 | } | 80 | } |
| 81 | case IOpenCapture: { | 81 | case IOpenCapture: { |
| 82 | printf("%s (idx = %d)", capkind(getkind(p)), p->i.key); | 82 | printf("%s (idx = %d)", capkind(getkind(p)), p->i.aux2.key); |
| 83 | break; | 83 | break; |
| 84 | } | 84 | } |
| 85 | case ISet: { | 85 | case ISet: { |
| @@ -99,7 +99,7 @@ void printinst (const Instruction *op, const Instruction *p) { | |||
| 99 | break; | 99 | break; |
| 100 | } | 100 | } |
| 101 | case IBehind: { | 101 | case IBehind: { |
| 102 | printf("%d", p->i.aux); | 102 | printf("%d", p->i.aux1); |
| 103 | break; | 103 | break; |
| 104 | } | 104 | } |
| 105 | case IJmp: case ICall: case ICommit: case IChoice: | 105 | case IJmp: case ICall: case ICommit: case IChoice: |
| @@ -111,8 +111,8 @@ typedef struct Charset { | |||
| 111 | ** in capture instructions, 'kind' of capture and its offset are | 111 | ** in capture instructions, 'kind' of capture and its offset are |
| 112 | ** packed in field 'aux', 4 bits for each | 112 | ** packed in field 'aux', 4 bits for each |
| 113 | */ | 113 | */ |
| 114 | #define getkind(op) ((op)->i.aux & 0xF) | 114 | #define getkind(op) ((op)->i.aux1 & 0xF) |
| 115 | #define getoff(op) (((op)->i.aux >> 4) & 0xF) | 115 | #define getoff(op) (((op)->i.aux1 >> 4) & 0xF) |
| 116 | #define joinkindoff(k,o) ((k) | ((o) << 4)) | 116 | #define joinkindoff(k,o) ((k) | ((o) << 4)) |
| 117 | 117 | ||
| 118 | #define MAXOFF 0xF | 118 | #define MAXOFF 0xF |
| @@ -20,7 +20,7 @@ | |||
| 20 | 20 | ||
| 21 | #define getoffset(p) (((p) + 1)->offset) | 21 | #define getoffset(p) (((p) + 1)->offset) |
| 22 | 22 | ||
| 23 | static const Instruction giveup = {{IGiveup, 0, 0}}; | 23 | static const Instruction giveup = {{IGiveup, 0, {0}}}; |
| 24 | 24 | ||
| 25 | 25 | ||
| 26 | /* | 26 | /* |
| @@ -249,12 +249,12 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, | |||
| 249 | continue; | 249 | continue; |
| 250 | } | 250 | } |
| 251 | case IChar: { | 251 | case IChar: { |
| 252 | if ((byte)*s == p->i.aux && s < e) { p++; s++; } | 252 | if ((byte)*s == p->i.aux1 && s < e) { p++; s++; } |
| 253 | else goto fail; | 253 | else goto fail; |
| 254 | continue; | 254 | continue; |
| 255 | } | 255 | } |
| 256 | case ITestChar: { | 256 | case ITestChar: { |
| 257 | if ((byte)*s == p->i.aux && s < e) p += 2; | 257 | if ((byte)*s == p->i.aux1 && s < e) p += 2; |
| 258 | else p += getoffset(p); | 258 | else p += getoffset(p); |
| 259 | continue; | 259 | continue; |
| 260 | } | 260 | } |
| @@ -273,7 +273,7 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, | |||
| 273 | continue; | 273 | continue; |
| 274 | } | 274 | } |
| 275 | case IBehind: { | 275 | case IBehind: { |
| 276 | int n = p->i.aux; | 276 | int n = p->i.aux1; |
| 277 | if (n > s - o) goto fail; | 277 | if (n > s - o) goto fail; |
| 278 | s -= n; p++; | 278 | s -= n; p++; |
| 279 | continue; | 279 | continue; |
| @@ -404,7 +404,7 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, | |||
| 404 | capture[captop].s = s - getoff(p); | 404 | capture[captop].s = s - getoff(p); |
| 405 | /* goto pushcapture; */ | 405 | /* goto pushcapture; */ |
| 406 | pushcapture: { | 406 | pushcapture: { |
| 407 | capture[captop].idx = p->i.key; | 407 | capture[captop].idx = p->i.aux2.key; |
| 408 | capture[captop].kind = getkind(p); | 408 | capture[captop].kind = getkind(p); |
| 409 | captop++; | 409 | captop++; |
| 410 | capture = growcap(L, capture, &capsize, captop, 0, ptop); | 410 | capture = growcap(L, capture, &capsize, captop, 0, ptop); |
| @@ -8,14 +8,14 @@ | |||
| 8 | /* Virtual Machine's instructions */ | 8 | /* Virtual Machine's instructions */ |
| 9 | typedef enum Opcode { | 9 | typedef enum Opcode { |
| 10 | IAny, /* if no char, fail */ | 10 | IAny, /* if no char, fail */ |
| 11 | IChar, /* if char != aux, fail */ | 11 | IChar, /* if char != aux1, fail */ |
| 12 | ISet, /* if char not in buff, fail */ | 12 | ISet, /* if char not in buff, fail */ |
| 13 | ITestAny, /* in no char, jump to 'offset' */ | 13 | ITestAny, /* in no char, jump to 'offset' */ |
| 14 | ITestChar, /* if char != aux, jump to 'offset' */ | 14 | ITestChar, /* if char != aux1, jump to 'offset' */ |
| 15 | ITestSet, /* if char not in buff, jump to 'offset' */ | 15 | ITestSet, /* if char not in buff, jump to 'offset' */ |
| 16 | ISpan, /* read a span of chars in buff */ | 16 | ISpan, /* read a span of chars in buff */ |
| 17 | IUTFR, /* if codepoint not in range [offset, utf_to], fail */ | 17 | IUTFR, /* if codepoint not in range [offset, utf_to], fail */ |
| 18 | IBehind, /* walk back 'aux' characters (fail if not possible) */ | 18 | IBehind, /* walk back 'aux1' characters (fail if not possible) */ |
| 19 | IRet, /* return from a rule */ | 19 | IRet, /* return from a rule */ |
| 20 | IEnd, /* end of pattern */ | 20 | IEnd, /* end of pattern */ |
| 21 | IChoice, /* stack a choice; next fail will jump to 'offset' */ | 21 | IChoice, /* stack a choice; next fail will jump to 'offset' */ |
| @@ -40,8 +40,10 @@ typedef enum Opcode { | |||
| 40 | typedef union Instruction { | 40 | typedef union Instruction { |
| 41 | struct Inst { | 41 | struct Inst { |
| 42 | byte code; | 42 | byte code; |
| 43 | byte aux; | 43 | byte aux1; |
| 44 | short key; | 44 | union { |
| 45 | short key; | ||
| 46 | } aux2; | ||
| 45 | } i; | 47 | } i; |
| 46 | int offset; | 48 | int offset; |
| 47 | byte buff[1]; | 49 | byte buff[1]; |
| @@ -49,7 +51,7 @@ typedef union Instruction { | |||
| 49 | 51 | ||
| 50 | 52 | ||
| 51 | /* extract 24-bit value from an instruction */ | 53 | /* extract 24-bit value from an instruction */ |
| 52 | #define utf_to(inst) (((inst)->i.key << 8) | (inst)->i.aux) | 54 | #define utf_to(inst) (((inst)->i.aux2.key << 8) | (inst)->i.aux1) |
| 53 | 55 | ||
| 54 | 56 | ||
| 55 | void printpatt (Instruction *p, int n); | 57 | void printpatt (Instruction *p, int n); |
