-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlj_parse.c
2752 lines (2569 loc) · 78.5 KB
/
lj_parse.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** Lua parser (source code -> bytecode).
** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h
**
** Major portions taken verbatim or adapted from the Lua interpreter.
** Copyright (C) 1994-2008 Lua.org, PUC-Rio. See Copyright Notice in lua.h
*/
#define lj_parse_c
#define LUA_CORE
#include "lj_obj.h"
#include "lj_gc.h"
#include "lj_err.h"
#include "lj_debug.h"
#include "lj_str.h"
#include "lj_tab.h"
#include "lj_func.h"
#include "lj_state.h"
#include "lj_bc.h"
#if LJ_HASFFI
#include "lj_ctype.h"
#endif
#include "lj_lex.h"
#include "lj_parse.h"
#include "lj_vm.h"
#include "lj_vmevent.h"
/* -- Parser structures and definitions ----------------------------------- */
/* Expression kinds. */
typedef enum {
/* Constant expressions must be first and in this order: */
VKNIL,
VKFALSE,
VKTRUE,
VKSTR, /* sval = string value */
VKNUM, /* nval = number value */
VKLAST = VKNUM,
VKCDATA, /* nval = cdata value, not treated as a constant expression */
/* Non-constant expressions follow: */
VLOCAL, /* info = local register, aux = vstack index */
VUPVAL, /* info = upvalue index, aux = vstack index */
VGLOBAL, /* sval = string value */
VINDEXED, /* info = table register, aux = index reg/byte/string const */
VJMP, /* info = instruction PC */
VRELOCABLE, /* info = instruction PC */
VNONRELOC, /* info = result register */
VCALL, /* info = instruction PC, aux = base */
VVOID
} ExpKind;
/* Expression descriptor. */
typedef struct ExpDesc {
union {
struct {
uint32_t info; /* Primary info. */
uint32_t aux; /* Secondary info. */
} s;
TValue nval; /* Number value. */
GCstr *sval; /* String value. */
} u;
ExpKind k;
BCPos t; /* True condition jump list. */
BCPos f; /* False condition jump list. */
} ExpDesc;
/* Macros for expressions. */
#define expr_hasjump(e) ((e)->t != (e)->f)
#define expr_isk(e) ((e)->k <= VKLAST)
#define expr_isk_nojump(e) (expr_isk(e) && !expr_hasjump(e))
#define expr_isnumk(e) ((e)->k == VKNUM)
#define expr_isnumk_nojump(e) (expr_isnumk(e) && !expr_hasjump(e))
#define expr_isstrk(e) ((e)->k == VKSTR)
#define expr_numtv(e) check_exp(expr_isnumk((e)), &(e)->u.nval)
#define expr_numberV(e) numberVnum(expr_numtv((e)))
/* Initialize expression. */
static LJ_AINLINE void expr_init(ExpDesc *e, ExpKind k, uint32_t info)
{
e->k = k;
e->u.s.info = info;
e->f = e->t = NO_JMP;
}
/* Check number constant for +-0. */
static int expr_numiszero(ExpDesc *e)
{
TValue *o = expr_numtv(e);
return tvisint(o) ? (intV(o) == 0) : tviszero(o);
}
/* Per-function linked list of scope blocks. */
typedef struct FuncScope {
struct FuncScope *prev; /* Link to outer scope. */
MSize vstart; /* Start of block-local variables. */
uint8_t nactvar; /* Number of active vars outside the scope. */
uint8_t flags; /* Scope flags. */
} FuncScope;
#define FSCOPE_LOOP 0x01 /* Scope is a (breakable) loop. */
#define FSCOPE_BREAK 0x02 /* Break used in scope. */
#define FSCOPE_GOLA 0x04 /* Goto or label used in scope. */
#define FSCOPE_UPVAL 0x08 /* Upvalue in scope. */
#define FSCOPE_NOCLOSE 0x10 /* Do not close upvalues. */
#define NAME_BREAK ((GCstr *)(uintptr_t)1)
/* Index into variable stack. */
typedef uint16_t VarIndex;
#define LJ_MAX_VSTACK (65536 - LJ_MAX_UPVAL)
/* Variable/goto/label info. */
#define VSTACK_VAR_RW 0x01 /* R/W variable. */
#define VSTACK_GOTO 0x02 /* Pending goto. */
#define VSTACK_LABEL 0x04 /* Label. */
/* Per-function state. */
typedef struct FuncState {
GCtab *kt; /* Hash table for constants. */
LexState *ls; /* Lexer state. */
lua_State *L; /* Lua state. */
FuncScope *bl; /* Current scope. */
struct FuncState *prev; /* Enclosing function. */
BCPos pc; /* Next bytecode position. */
BCPos lasttarget; /* Bytecode position of last jump target. */
BCPos jpc; /* Pending jump list to next bytecode. */
BCReg freereg; /* First free register. */
BCReg nactvar; /* Number of active local variables. */
BCReg nkn, nkgc; /* Number of lua_Number/GCobj constants */
BCLine linedefined; /* First line of the function definition. */
BCInsLine *bcbase; /* Base of bytecode stack. */
BCPos bclim; /* Limit of bytecode stack. */
MSize vbase; /* Base of variable stack for this function. */
uint8_t flags; /* Prototype flags. */
uint8_t numparams; /* Number of parameters. */
uint8_t framesize; /* Fixed frame size. */
uint8_t nuv; /* Number of upvalues */
VarIndex varmap[LJ_MAX_LOCVAR]; /* Map from register to variable idx. */
VarIndex uvmap[LJ_MAX_UPVAL]; /* Map from upvalue to variable idx. */
VarIndex uvtmp[LJ_MAX_UPVAL]; /* Temporary upvalue map. */
} FuncState;
/* Binary and unary operators. ORDER OPR */
typedef enum BinOpr {
OPR_ADD, OPR_SUB, OPR_MUL, OPR_DIV, OPR_MOD, OPR_POW, /* ORDER ARITH */
OPR_CONCAT,
OPR_NE, OPR_EQ,
OPR_LT, OPR_GE, OPR_LE, OPR_GT,
OPR_AND, OPR_OR,
OPR_NOBINOPR
} BinOpr;
LJ_STATIC_ASSERT((int)BC_ISGE-(int)BC_ISLT == (int)OPR_GE-(int)OPR_LT);
LJ_STATIC_ASSERT((int)BC_ISLE-(int)BC_ISLT == (int)OPR_LE-(int)OPR_LT);
LJ_STATIC_ASSERT((int)BC_ISGT-(int)BC_ISLT == (int)OPR_GT-(int)OPR_LT);
LJ_STATIC_ASSERT((int)BC_SUBVV-(int)BC_ADDVV == (int)OPR_SUB-(int)OPR_ADD);
LJ_STATIC_ASSERT((int)BC_MULVV-(int)BC_ADDVV == (int)OPR_MUL-(int)OPR_ADD);
LJ_STATIC_ASSERT((int)BC_DIVVV-(int)BC_ADDVV == (int)OPR_DIV-(int)OPR_ADD);
LJ_STATIC_ASSERT((int)BC_MODVV-(int)BC_ADDVV == (int)OPR_MOD-(int)OPR_ADD);
/* -- Error handling ------------------------------------------------------ */
LJ_NORET LJ_NOINLINE static void err_syntax(LexState *ls, ErrMsg em)
{
lj_lex_error(ls, ls->token, em);
}
LJ_NORET LJ_NOINLINE static void err_token(LexState *ls, LexToken token)
{
lj_lex_error(ls, ls->token, LJ_ERR_XTOKEN, lj_lex_token2str(ls, token));
}
LJ_NORET static void err_limit(FuncState *fs, uint32_t limit, const char *what)
{
if (fs->linedefined == 0)
lj_lex_error(fs->ls, 0, LJ_ERR_XLIMM, limit, what);
else
lj_lex_error(fs->ls, 0, LJ_ERR_XLIMF, fs->linedefined, limit, what);
}
#define checklimit(fs, v, l, m) if ((v) >= (l)) err_limit(fs, l, m)
#define checklimitgt(fs, v, l, m) if ((v) > (l)) err_limit(fs, l, m)
#define checkcond(ls, c, em) { if (!(c)) err_syntax(ls, em); }
/* -- Management of constants --------------------------------------------- */
/* Return bytecode encoding for primitive constant. */
#define const_pri(e) check_exp((e)->k <= VKTRUE, (e)->k)
#define tvhaskslot(o) ((o)->u32.hi == 0)
#define tvkslot(o) ((o)->u32.lo)
/* Add a number constant. */
static BCReg const_num(FuncState *fs, ExpDesc *e)
{
lua_State *L = fs->L;
TValue *o;
lua_assert(expr_isnumk(e));
o = lj_tab_set(L, fs->kt, &e->u.nval);
if (tvhaskslot(o))
return tvkslot(o);
o->u64 = fs->nkn;
return fs->nkn++;
}
/* Add a GC object constant. */
static BCReg const_gc(FuncState *fs, GCobj *gc, uint32_t itype)
{
lua_State *L = fs->L;
TValue key, *o;
setgcV(L, &key, gc, itype);
/* NOBARRIER: the key is new or kept alive. */
o = lj_tab_set(L, fs->kt, &key);
if (tvhaskslot(o))
return tvkslot(o);
o->u64 = fs->nkgc;
return fs->nkgc++;
}
/* Add a string constant. */
static BCReg const_str(FuncState *fs, ExpDesc *e)
{
lua_assert(expr_isstrk(e) || e->k == VGLOBAL);
return const_gc(fs, obj2gco(e->u.sval), LJ_TSTR);
}
/* Anchor string constant to avoid GC. */
GCstr *lj_parse_keepstr(LexState *ls, const char *str, size_t len)
{
/* NOBARRIER: the key is new or kept alive. */
lua_State *L = ls->L;
GCstr *s = lj_str_new(L, str, len);
TValue *tv = lj_tab_setstr(L, ls->fs->kt, s);
if (tvisnil(tv)) setboolV(tv, 1);
lj_gc_check(L);
return s;
}
#if LJ_HASFFI
/* Anchor cdata to avoid GC. */
void lj_parse_keepcdata(LexState *ls, TValue *tv, GCcdata *cd)
{
/* NOBARRIER: the key is new or kept alive. */
lua_State *L = ls->L;
setcdataV(L, tv, cd);
setboolV(lj_tab_set(L, ls->fs->kt, tv), 1);
}
#endif
/* -- Jump list handling -------------------------------------------------- */
/* Get next element in jump list. */
static BCPos jmp_next(FuncState *fs, BCPos pc)
{
ptrdiff_t delta = bc_j(fs->bcbase[pc].ins);
if ((BCPos)delta == NO_JMP)
return NO_JMP;
else
return (BCPos)(((ptrdiff_t)pc+1)+delta);
}
/* Check if any of the instructions on the jump list produce no value. */
static int jmp_novalue(FuncState *fs, BCPos list)
{
for (; list != NO_JMP; list = jmp_next(fs, list)) {
BCIns p = fs->bcbase[list >= 1 ? list-1 : list].ins;
if (!(bc_op(p) == BC_ISTC || bc_op(p) == BC_ISFC || bc_a(p) == NO_REG))
return 1;
}
return 0;
}
/* Patch register of test instructions. */
static int jmp_patchtestreg(FuncState *fs, BCPos pc, BCReg reg)
{
BCInsLine *ilp = &fs->bcbase[pc >= 1 ? pc-1 : pc];
BCOp op = bc_op(ilp->ins);
if (op == BC_ISTC || op == BC_ISFC) {
if (reg != NO_REG && reg != bc_d(ilp->ins)) {
setbc_a(&ilp->ins, reg);
} else { /* Nothing to store or already in the right register. */
setbc_op(&ilp->ins, op+(BC_IST-BC_ISTC));
setbc_a(&ilp->ins, 0);
}
} else if (bc_a(ilp->ins) == NO_REG) {
if (reg == NO_REG) {
ilp->ins = BCINS_AJ(BC_JMP, bc_a(fs->bcbase[pc].ins), 0);
} else {
setbc_a(&ilp->ins, reg);
if (reg >= bc_a(ilp[1].ins))
setbc_a(&ilp[1].ins, reg+1);
}
} else {
return 0; /* Cannot patch other instructions. */
}
return 1;
}
/* Drop values for all instructions on jump list. */
static void jmp_dropval(FuncState *fs, BCPos list)
{
for (; list != NO_JMP; list = jmp_next(fs, list))
jmp_patchtestreg(fs, list, NO_REG);
}
/* Patch jump instruction to target. */
static void jmp_patchins(FuncState *fs, BCPos pc, BCPos dest)
{
BCIns *jmp = &fs->bcbase[pc].ins;
BCPos offset = dest-(pc+1)+BCBIAS_J;
lua_assert(dest != NO_JMP);
if (offset > BCMAX_D)
err_syntax(fs->ls, LJ_ERR_XJUMP);
setbc_d(jmp, offset);
}
/* Append to jump list. */
static void jmp_append(FuncState *fs, BCPos *l1, BCPos l2)
{
if (l2 == NO_JMP) {
return;
} else if (*l1 == NO_JMP) {
*l1 = l2;
} else {
BCPos list = *l1;
BCPos next;
while ((next = jmp_next(fs, list)) != NO_JMP) /* Find last element. */
list = next;
jmp_patchins(fs, list, l2);
}
}
/* Patch jump list and preserve produced values. */
static void jmp_patchval(FuncState *fs, BCPos list, BCPos vtarget,
BCReg reg, BCPos dtarget)
{
while (list != NO_JMP) {
BCPos next = jmp_next(fs, list);
if (jmp_patchtestreg(fs, list, reg))
jmp_patchins(fs, list, vtarget); /* Jump to target with value. */
else
jmp_patchins(fs, list, dtarget); /* Jump to default target. */
list = next;
}
}
/* Jump to following instruction. Append to list of pending jumps. */
static void jmp_tohere(FuncState *fs, BCPos list)
{
fs->lasttarget = fs->pc;
jmp_append(fs, &fs->jpc, list);
}
/* Patch jump list to target. */
static void jmp_patch(FuncState *fs, BCPos list, BCPos target)
{
if (target == fs->pc) {
jmp_tohere(fs, list);
} else {
lua_assert(target < fs->pc);
jmp_patchval(fs, list, target, NO_REG, target);
}
}
/* -- Bytecode register allocator ----------------------------------------- */
/* Bump frame size. */
static void bcreg_bump(FuncState *fs, BCReg n)
{
BCReg sz = fs->freereg + n;
if (sz > fs->framesize) {
if (sz >= LJ_MAX_SLOTS)
err_syntax(fs->ls, LJ_ERR_XSLOTS);
fs->framesize = (uint8_t)sz;
}
}
/* Reserve registers. */
static void bcreg_reserve(FuncState *fs, BCReg n)
{
bcreg_bump(fs, n);
fs->freereg += n;
}
/* Free register. */
static void bcreg_free(FuncState *fs, BCReg reg)
{
if (reg >= fs->nactvar) {
fs->freereg--;
lua_assert(reg == fs->freereg);
}
}
/* Free register for expression. */
static void expr_free(FuncState *fs, ExpDesc *e)
{
if (e->k == VNONRELOC)
bcreg_free(fs, e->u.s.info);
}
/* -- Bytecode emitter ---------------------------------------------------- */
/* Emit bytecode instruction. */
static BCPos bcemit_INS(FuncState *fs, BCIns ins)
{
BCPos pc = fs->pc;
LexState *ls = fs->ls;
jmp_patchval(fs, fs->jpc, pc, NO_REG, pc);
fs->jpc = NO_JMP;
if (LJ_UNLIKELY(pc >= fs->bclim)) {
ptrdiff_t base = fs->bcbase - ls->bcstack;
checklimit(fs, ls->sizebcstack, LJ_MAX_BCINS, "bytecode instructions");
lj_mem_growvec(fs->L, ls->bcstack, ls->sizebcstack, LJ_MAX_BCINS,BCInsLine);
fs->bclim = (BCPos)(ls->sizebcstack - base);
fs->bcbase = ls->bcstack + base;
}
fs->bcbase[pc].ins = ins;
fs->bcbase[pc].line = ls->lastline;
fs->pc = pc+1;
return pc;
}
#define bcemit_ABC(fs, o, a, b, c) bcemit_INS(fs, BCINS_ABC(o, a, b, c))
#define bcemit_AD(fs, o, a, d) bcemit_INS(fs, BCINS_AD(o, a, d))
#define bcemit_AJ(fs, o, a, j) bcemit_INS(fs, BCINS_AJ(o, a, j))
#define bcptr(fs, e) (&(fs)->bcbase[(e)->u.s.info].ins)
/* -- Bytecode emitter for expressions ------------------------------------ */
/* Discharge non-constant expression to any register. */
static void expr_discharge(FuncState *fs, ExpDesc *e)
{
BCIns ins;
if (e->k == VUPVAL) {
ins = BCINS_AD(BC_UGET, 0, e->u.s.info);
} else if (e->k == VGLOBAL) {
ins = BCINS_AD(BC_GGET, 0, const_str(fs, e));
} else if (e->k == VINDEXED) {
BCReg rc = e->u.s.aux;
if ((int32_t)rc < 0) {
ins = BCINS_ABC(BC_TGETS, 0, e->u.s.info, ~rc);
} else if (rc > BCMAX_C) {
ins = BCINS_ABC(BC_TGETB, 0, e->u.s.info, rc-(BCMAX_C+1));
} else {
bcreg_free(fs, rc);
ins = BCINS_ABC(BC_TGETV, 0, e->u.s.info, rc);
}
bcreg_free(fs, e->u.s.info);
} else if (e->k == VCALL) {
e->u.s.info = e->u.s.aux;
e->k = VNONRELOC;
return;
} else if (e->k == VLOCAL) {
e->k = VNONRELOC;
return;
} else {
return;
}
e->u.s.info = bcemit_INS(fs, ins);
e->k = VRELOCABLE;
}
/* Emit bytecode to set a range of registers to nil. */
static void bcemit_nil(FuncState *fs, BCReg from, BCReg n)
{
if (fs->pc > fs->lasttarget) { /* No jumps to current position? */
BCIns *ip = &fs->bcbase[fs->pc-1].ins;
BCReg pto, pfrom = bc_a(*ip);
switch (bc_op(*ip)) { /* Try to merge with the previous instruction. */
case BC_KPRI:
if (bc_d(*ip) != ~LJ_TNIL) break;
if (from == pfrom) {
if (n == 1) return;
} else if (from == pfrom+1) {
from = pfrom;
n++;
} else {
break;
}
*ip = BCINS_AD(BC_KNIL, from, from+n-1); /* Replace KPRI. */
return;
case BC_KNIL:
pto = bc_d(*ip);
if (pfrom <= from && from <= pto+1) { /* Can we connect both ranges? */
if (from+n-1 > pto)
setbc_d(ip, from+n-1); /* Patch previous instruction range. */
return;
}
break;
default:
break;
}
}
/* Emit new instruction or replace old instruction. */
bcemit_INS(fs, n == 1 ? BCINS_AD(BC_KPRI, from, VKNIL) :
BCINS_AD(BC_KNIL, from, from+n-1));
}
/* Discharge an expression to a specific register. Ignore branches. */
static void expr_toreg_nobranch(FuncState *fs, ExpDesc *e, BCReg reg)
{
BCIns ins;
expr_discharge(fs, e);
if (e->k == VKSTR) {
ins = BCINS_AD(BC_KSTR, reg, const_str(fs, e));
} else if (e->k == VKNUM) {
#if LJ_DUALNUM
cTValue *tv = expr_numtv(e);
if (tvisint(tv) && checki16(intV(tv)))
ins = BCINS_AD(BC_KSHORT, reg, (BCReg)(uint16_t)intV(tv));
else
#else
lua_Number n = expr_numberV(e);
int32_t k = lj_num2int(n);
if (checki16(k) && n == (lua_Number)k)
ins = BCINS_AD(BC_KSHORT, reg, (BCReg)(uint16_t)k);
else
#endif
ins = BCINS_AD(BC_KNUM, reg, const_num(fs, e));
#if LJ_HASFFI
} else if (e->k == VKCDATA) {
fs->flags |= PROTO_FFI;
ins = BCINS_AD(BC_KCDATA, reg,
const_gc(fs, obj2gco(cdataV(&e->u.nval)), LJ_TCDATA));
#endif
} else if (e->k == VRELOCABLE) {
setbc_a(bcptr(fs, e), reg);
goto noins;
} else if (e->k == VNONRELOC) {
if (reg == e->u.s.info)
goto noins;
ins = BCINS_AD(BC_MOV, reg, e->u.s.info);
} else if (e->k == VKNIL) {
bcemit_nil(fs, reg, 1);
goto noins;
} else if (e->k <= VKTRUE) {
ins = BCINS_AD(BC_KPRI, reg, const_pri(e));
} else {
lua_assert(e->k == VVOID || e->k == VJMP);
return;
}
bcemit_INS(fs, ins);
noins:
e->u.s.info = reg;
e->k = VNONRELOC;
}
/* Forward declaration. */
static BCPos bcemit_jmp(FuncState *fs);
/* Discharge an expression to a specific register. */
static void expr_toreg(FuncState *fs, ExpDesc *e, BCReg reg)
{
expr_toreg_nobranch(fs, e, reg);
if (e->k == VJMP)
jmp_append(fs, &e->t, e->u.s.info); /* Add it to the true jump list. */
if (expr_hasjump(e)) { /* Discharge expression with branches. */
BCPos jend, jfalse = NO_JMP, jtrue = NO_JMP;
if (jmp_novalue(fs, e->t) || jmp_novalue(fs, e->f)) {
BCPos jval = (e->k == VJMP) ? NO_JMP : bcemit_jmp(fs);
jfalse = bcemit_AD(fs, BC_KPRI, reg, VKFALSE);
bcemit_AJ(fs, BC_JMP, fs->freereg, 1);
jtrue = bcemit_AD(fs, BC_KPRI, reg, VKTRUE);
jmp_tohere(fs, jval);
}
jend = fs->pc;
fs->lasttarget = jend;
jmp_patchval(fs, e->f, jend, reg, jfalse);
jmp_patchval(fs, e->t, jend, reg, jtrue);
}
e->f = e->t = NO_JMP;
e->u.s.info = reg;
e->k = VNONRELOC;
}
/* Discharge an expression to the next free register. */
static void expr_tonextreg(FuncState *fs, ExpDesc *e)
{
expr_discharge(fs, e);
expr_free(fs, e);
bcreg_reserve(fs, 1);
expr_toreg(fs, e, fs->freereg - 1);
}
/* Discharge an expression to any register. */
static BCReg expr_toanyreg(FuncState *fs, ExpDesc *e)
{
expr_discharge(fs, e);
if (e->k == VNONRELOC) {
if (!expr_hasjump(e)) return e->u.s.info; /* Already in a register. */
if (e->u.s.info >= fs->nactvar) {
expr_toreg(fs, e, e->u.s.info); /* Discharge to temp. register. */
return e->u.s.info;
}
}
expr_tonextreg(fs, e); /* Discharge to next register. */
return e->u.s.info;
}
/* Partially discharge expression to a value. */
static void expr_toval(FuncState *fs, ExpDesc *e)
{
if (expr_hasjump(e))
expr_toanyreg(fs, e);
else
expr_discharge(fs, e);
}
/* Emit store for LHS expression. */
static void bcemit_store(FuncState *fs, ExpDesc *var, ExpDesc *e)
{
BCIns ins;
if (var->k == VLOCAL) {
fs->ls->vstack[var->u.s.aux].info |= VSTACK_VAR_RW;
expr_free(fs, e);
expr_toreg(fs, e, var->u.s.info);
return;
} else if (var->k == VUPVAL) {
fs->ls->vstack[var->u.s.aux].info |= VSTACK_VAR_RW;
expr_toval(fs, e);
if (e->k <= VKTRUE)
ins = BCINS_AD(BC_USETP, var->u.s.info, const_pri(e));
else if (e->k == VKSTR)
ins = BCINS_AD(BC_USETS, var->u.s.info, const_str(fs, e));
else if (e->k == VKNUM)
ins = BCINS_AD(BC_USETN, var->u.s.info, const_num(fs, e));
else
ins = BCINS_AD(BC_USETV, var->u.s.info, expr_toanyreg(fs, e));
} else if (var->k == VGLOBAL) {
BCReg ra = expr_toanyreg(fs, e);
ins = BCINS_AD(BC_GSET, ra, const_str(fs, var));
} else {
BCReg ra, rc;
lua_assert(var->k == VINDEXED);
ra = expr_toanyreg(fs, e);
rc = var->u.s.aux;
if ((int32_t)rc < 0) {
ins = BCINS_ABC(BC_TSETS, ra, var->u.s.info, ~rc);
} else if (rc > BCMAX_C) {
ins = BCINS_ABC(BC_TSETB, ra, var->u.s.info, rc-(BCMAX_C+1));
} else {
/* Free late alloced key reg to avoid assert on free of value reg. */
/* This can only happen when called from expr_table(). */
lua_assert(e->k != VNONRELOC || ra < fs->nactvar ||
rc < ra || (bcreg_free(fs, rc),1));
ins = BCINS_ABC(BC_TSETV, ra, var->u.s.info, rc);
}
}
bcemit_INS(fs, ins);
expr_free(fs, e);
}
/* Emit method lookup expression. */
static void bcemit_method(FuncState *fs, ExpDesc *e, ExpDesc *key)
{
BCReg idx, func, obj = expr_toanyreg(fs, e);
expr_free(fs, e);
func = fs->freereg;
bcemit_AD(fs, BC_MOV, func+1, obj); /* Copy object to first argument. */
lua_assert(expr_isstrk(key));
idx = const_str(fs, key);
if (idx <= BCMAX_C) {
bcreg_reserve(fs, 2);
bcemit_ABC(fs, BC_TGETS, func, obj, idx);
} else {
bcreg_reserve(fs, 3);
bcemit_AD(fs, BC_KSTR, func+2, idx);
bcemit_ABC(fs, BC_TGETV, func, obj, func+2);
fs->freereg--;
}
e->u.s.info = func;
e->k = VNONRELOC;
}
/* -- Bytecode emitter for branches --------------------------------------- */
/* Emit unconditional branch. */
static BCPos bcemit_jmp(FuncState *fs)
{
BCPos jpc = fs->jpc;
BCPos j = fs->pc - 1;
BCIns *ip = &fs->bcbase[j].ins;
fs->jpc = NO_JMP;
if ((int32_t)j >= (int32_t)fs->lasttarget && bc_op(*ip) == BC_UCLO)
setbc_j(ip, NO_JMP);
else
j = bcemit_AJ(fs, BC_JMP, fs->freereg, NO_JMP);
jmp_append(fs, &j, jpc);
return j;
}
/* Invert branch condition of bytecode instruction. */
static void invertcond(FuncState *fs, ExpDesc *e)
{
BCIns *ip = &fs->bcbase[e->u.s.info - 1].ins;
setbc_op(ip, bc_op(*ip)^1);
}
/* Emit conditional branch. */
static BCPos bcemit_branch(FuncState *fs, ExpDesc *e, int cond)
{
BCPos pc;
if (e->k == VRELOCABLE) {
BCIns *ip = bcptr(fs, e);
if (bc_op(*ip) == BC_NOT) {
*ip = BCINS_AD(cond ? BC_ISF : BC_IST, 0, bc_d(*ip));
return bcemit_jmp(fs);
}
}
if (e->k != VNONRELOC) {
bcreg_reserve(fs, 1);
expr_toreg_nobranch(fs, e, fs->freereg-1);
}
bcemit_AD(fs, cond ? BC_ISTC : BC_ISFC, NO_REG, e->u.s.info);
pc = bcemit_jmp(fs);
expr_free(fs, e);
return pc;
}
/* Emit branch on true condition. */
static void bcemit_branch_t(FuncState *fs, ExpDesc *e)
{
BCPos pc;
expr_discharge(fs, e);
if (e->k == VKSTR || e->k == VKNUM || e->k == VKTRUE)
pc = NO_JMP; /* Never jump. */
else if (e->k == VJMP)
invertcond(fs, e), pc = e->u.s.info;
else if (e->k == VKFALSE || e->k == VKNIL)
expr_toreg_nobranch(fs, e, NO_REG), pc = bcemit_jmp(fs);
else
pc = bcemit_branch(fs, e, 0);
jmp_append(fs, &e->f, pc);
jmp_tohere(fs, e->t);
e->t = NO_JMP;
}
/* Emit branch on false condition. */
static void bcemit_branch_f(FuncState *fs, ExpDesc *e)
{
BCPos pc;
expr_discharge(fs, e);
if (e->k == VKNIL || e->k == VKFALSE)
pc = NO_JMP; /* Never jump. */
else if (e->k == VJMP)
pc = e->u.s.info;
else if (e->k == VKSTR || e->k == VKNUM || e->k == VKTRUE)
expr_toreg_nobranch(fs, e, NO_REG), pc = bcemit_jmp(fs);
else
pc = bcemit_branch(fs, e, 1);
jmp_append(fs, &e->t, pc);
jmp_tohere(fs, e->f);
e->f = NO_JMP;
}
/* -- Bytecode emitter for operators -------------------------------------- */
/* Try constant-folding of arithmetic operators. */
static int foldarith(BinOpr opr, ExpDesc *e1, ExpDesc *e2)
{
TValue o;
lua_Number n;
if (!expr_isnumk_nojump(e1) || !expr_isnumk_nojump(e2)) return 0;
n = lj_vm_foldarith(expr_numberV(e1), expr_numberV(e2), (int)opr-OPR_ADD);
setnumV(&o, n);
if (tvisnan(&o) || tvismzero(&o)) return 0; /* Avoid NaN and -0 as consts. */
if (LJ_DUALNUM) {
int32_t k = lj_num2int(n);
if ((lua_Number)k == n) {
setintV(&e1->u.nval, k);
return 1;
}
}
setnumV(&e1->u.nval, n);
return 1;
}
/* Emit arithmetic operator. */
static void bcemit_arith(FuncState *fs, BinOpr opr, ExpDesc *e1, ExpDesc *e2)
{
BCReg rb, rc, t;
uint32_t op;
if (foldarith(opr, e1, e2))
return;
if (opr == OPR_POW) {
op = BC_POW;
rc = expr_toanyreg(fs, e2);
rb = expr_toanyreg(fs, e1);
} else {
op = opr-OPR_ADD+BC_ADDVV;
/* Must discharge 2nd operand first since VINDEXED might free regs. */
expr_toval(fs, e2);
if (expr_isnumk(e2) && (rc = const_num(fs, e2)) <= BCMAX_C)
op -= BC_ADDVV-BC_ADDVN;
else
rc = expr_toanyreg(fs, e2);
/* 1st operand discharged by bcemit_binop_left, but need KNUM/KSHORT. */
lua_assert(expr_isnumk(e1) || e1->k == VNONRELOC);
expr_toval(fs, e1);
/* Avoid two consts to satisfy bytecode constraints. */
if (expr_isnumk(e1) && !expr_isnumk(e2) &&
(t = const_num(fs, e1)) <= BCMAX_B) {
rb = rc; rc = t; op -= BC_ADDVV-BC_ADDNV;
} else {
rb = expr_toanyreg(fs, e1);
}
}
/* Using expr_free might cause asserts if the order is wrong. */
if (e1->k == VNONRELOC && e1->u.s.info >= fs->nactvar) fs->freereg--;
if (e2->k == VNONRELOC && e2->u.s.info >= fs->nactvar) fs->freereg--;
e1->u.s.info = bcemit_ABC(fs, op, 0, rb, rc);
e1->k = VRELOCABLE;
}
/* Emit comparison operator. */
static void bcemit_comp(FuncState *fs, BinOpr opr, ExpDesc *e1, ExpDesc *e2)
{
ExpDesc *eret = e1;
BCIns ins;
expr_toval(fs, e1);
if (opr == OPR_EQ || opr == OPR_NE) {
BCOp op = opr == OPR_EQ ? BC_ISEQV : BC_ISNEV;
BCReg ra;
if (expr_isk(e1)) { e1 = e2; e2 = eret; } /* Need constant in 2nd arg. */
ra = expr_toanyreg(fs, e1); /* First arg must be in a reg. */
expr_toval(fs, e2);
switch (e2->k) {
case VKNIL: case VKFALSE: case VKTRUE:
ins = BCINS_AD(op+(BC_ISEQP-BC_ISEQV), ra, const_pri(e2));
break;
case VKSTR:
ins = BCINS_AD(op+(BC_ISEQS-BC_ISEQV), ra, const_str(fs, e2));
break;
case VKNUM:
ins = BCINS_AD(op+(BC_ISEQN-BC_ISEQV), ra, const_num(fs, e2));
break;
default:
ins = BCINS_AD(op, ra, expr_toanyreg(fs, e2));
break;
}
} else {
uint32_t op = opr-OPR_LT+BC_ISLT;
BCReg ra, rd;
if ((op-BC_ISLT) & 1) { /* GT -> LT, GE -> LE */
e1 = e2; e2 = eret; /* Swap operands. */
op = ((op-BC_ISLT)^3)+BC_ISLT;
expr_toval(fs, e1);
}
rd = expr_toanyreg(fs, e2);
ra = expr_toanyreg(fs, e1);
ins = BCINS_AD(op, ra, rd);
}
/* Using expr_free might cause asserts if the order is wrong. */
if (e1->k == VNONRELOC && e1->u.s.info >= fs->nactvar) fs->freereg--;
if (e2->k == VNONRELOC && e2->u.s.info >= fs->nactvar) fs->freereg--;
bcemit_INS(fs, ins);
eret->u.s.info = bcemit_jmp(fs);
eret->k = VJMP;
}
/* Fixup left side of binary operator. */
static void bcemit_binop_left(FuncState *fs, BinOpr op, ExpDesc *e)
{
if (op == OPR_AND) {
bcemit_branch_t(fs, e);
} else if (op == OPR_OR) {
bcemit_branch_f(fs, e);
} else if (op == OPR_CONCAT) {
expr_tonextreg(fs, e);
} else if (op == OPR_EQ || op == OPR_NE) {
if (!expr_isk_nojump(e)) expr_toanyreg(fs, e);
} else {
if (!expr_isnumk_nojump(e)) expr_toanyreg(fs, e);
}
}
/* Emit binary operator. */
static void bcemit_binop(FuncState *fs, BinOpr op, ExpDesc *e1, ExpDesc *e2)
{
if (op <= OPR_POW) {
bcemit_arith(fs, op, e1, e2);
} else if (op == OPR_AND) {
lua_assert(e1->t == NO_JMP); /* List must be closed. */
expr_discharge(fs, e2);
jmp_append(fs, &e2->f, e1->f);
*e1 = *e2;
} else if (op == OPR_OR) {
lua_assert(e1->f == NO_JMP); /* List must be closed. */
expr_discharge(fs, e2);
jmp_append(fs, &e2->t, e1->t);
*e1 = *e2;
} else if (op == OPR_CONCAT) {
expr_toval(fs, e2);
if (e2->k == VRELOCABLE && bc_op(*bcptr(fs, e2)) == BC_CAT) {
lua_assert(e1->u.s.info == bc_b(*bcptr(fs, e2))-1);
expr_free(fs, e1);
setbc_b(bcptr(fs, e2), e1->u.s.info);
e1->u.s.info = e2->u.s.info;
} else {
expr_tonextreg(fs, e2);
expr_free(fs, e2);
expr_free(fs, e1);
e1->u.s.info = bcemit_ABC(fs, BC_CAT, 0, e1->u.s.info, e2->u.s.info);
}
e1->k = VRELOCABLE;
} else {
lua_assert(op == OPR_NE || op == OPR_EQ ||
op == OPR_LT || op == OPR_GE || op == OPR_LE || op == OPR_GT);
bcemit_comp(fs, op, e1, e2);
}
}
/* Emit unary operator. */
static void bcemit_unop(FuncState *fs, BCOp op, ExpDesc *e)
{
if (op == BC_NOT) {
/* Swap true and false lists. */
{ BCPos temp = e->f; e->f = e->t; e->t = temp; }
jmp_dropval(fs, e->f);
jmp_dropval(fs, e->t);
expr_discharge(fs, e);
if (e->k == VKNIL || e->k == VKFALSE) {
e->k = VKTRUE;
return;
} else if (expr_isk(e) || (LJ_HASFFI && e->k == VKCDATA)) {
e->k = VKFALSE;
return;
} else if (e->k == VJMP) {
invertcond(fs, e);
return;
} else if (e->k == VRELOCABLE) {
bcreg_reserve(fs, 1);
setbc_a(bcptr(fs, e), fs->freereg-1);
e->u.s.info = fs->freereg-1;
e->k = VNONRELOC;
} else {
lua_assert(e->k == VNONRELOC);
}
} else {
lua_assert(op == BC_UNM || op == BC_LEN);
if (op == BC_UNM && !expr_hasjump(e)) { /* Constant-fold negations. */
#if LJ_HASFFI
if (e->k == VKCDATA) { /* Fold in-place since cdata is not interned. */
GCcdata *cd = cdataV(&e->u.nval);
int64_t *p = (int64_t *)cdataptr(cd);
if (cd->ctypeid == CTID_COMPLEX_DOUBLE)
p[1] ^= (int64_t)U64x(80000000,00000000);
else
*p = -*p;
return;
} else
#endif
if (expr_isnumk(e) && !expr_numiszero(e)) { /* Avoid folding to -0. */
TValue *o = expr_numtv(e);
if (tvisint(o)) {
int32_t k = intV(o);
if (k == -k)
setnumV(o, -(lua_Number)k);
else
setintV(o, -k);
return;
} else {
o->u64 ^= U64x(80000000,00000000);
return;
}
}
}
expr_toanyreg(fs, e);
}
expr_free(fs, e);
e->u.s.info = bcemit_AD(fs, op, 0, e->u.s.info);
e->k = VRELOCABLE;
}
/* -- Lexer support ------------------------------------------------------- */
/* Check and consume optional token. */
static int lex_opt(LexState *ls, LexToken tok)
{
if (ls->token == tok) {
lj_lex_next(ls);
return 1;
}
return 0;
}
/* Check and consume token. */
static void lex_check(LexState *ls, LexToken tok)
{
if (ls->token != tok)
err_token(ls, tok);
lj_lex_next(ls);
}
/* Check for matching token. */
static void lex_match(LexState *ls, LexToken what, LexToken who, BCLine line)