Expand file tree
/
Copy pathlcode.c
More file actions
Latest commit
1814 lines (1604 loc) · 49.7 KB
/
Copy pathlcode.c
File metadata and controls
1814 lines (1604 loc) · 49.7 KB
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
/*
** $Id: lcode.c $
** Code generator for Lua
** See Copyright Notice in lua.h
*/
#definelcode_c
#defineLUA_CORE
#include"lprefix.h"
#include<limits.h>
#include<math.h>
#include<stdlib.h>
#include"lua.h"
#include"lcode.h"
#include"ldebug.h"
#include"ldo.h"
#include"lgc.h"
#include"llex.h"
#include"lmem.h"
#include"lobject.h"
#include"lopcodes.h"
#include"lparser.h"
#include"lstring.h"
#include"ltable.h"
#include"lvm.h"
/* Maximum number of registers in a Lua function (must fit in 8 bits) */
#defineMAXREGS 255
#definehasjumps(e) ((e)->t != (e)->f)
staticintcodesJ (FuncState*fs, OpCodeo, intsj, intk);
/* semantic error */
l_noretluaK_semerror (LexState*ls, constchar*msg) {
ls->t.token=0; /* remove "near <token>" from final message */
luaX_syntaxerror(ls, msg);
}
/*
** If expression is a numeric constant, fills 'v' with its value
** and returns 1. Otherwise, returns 0.
*/
staticinttonumeral (constexpdesc*e, TValue*v) {
if (hasjumps(e))
return0; /* not a numeral */
switch (e->k) {
caseVKINT:
if (v) setivalue(v, e->u.ival);
return1;
caseVKFLT:
if (v) setfltvalue(v, e->u.nval);
return1;
default: return0;
}
}
/*
** Get the constant value from a constant expression
*/
staticTValue*const2val (FuncState*fs, constexpdesc*e) {
lua_assert(e->k==VCONST);
return&fs->ls->dyd->actvar.arr[e->u.info].k;
}
/*
** If expression is a constant, fills 'v' with its value
** and returns 1. Otherwise, returns 0.
*/
intluaK_exp2const (FuncState*fs, constexpdesc*e, TValue*v) {
if (hasjumps(e))
return0; /* not a constant */
switch (e->k) {
caseVFALSE:
setbfvalue(v);
return1;
caseVTRUE:
setbtvalue(v);
return1;
caseVNIL:
setnilvalue(v);
return1;
caseVKSTR: {
setsvalue(fs->ls->L, v, e->u.strval);
return1;
}
caseVCONST: {
setobj(fs->ls->L, v, const2val(fs, e));
return1;
}
default: returntonumeral(e, v);
}
}
/*
** Return the previous instruction of the current code. If there
** may be a jump target between the current instruction and the
** previous one, return an invalid instruction (to avoid wrong
** optimizations).
*/
staticInstruction*previousinstruction (FuncState*fs) {
staticconstInstructioninvalidinstruction= ~(Instruction)0;
if (fs->pc>fs->lasttarget)
return&fs->f->code[fs->pc-1]; /* previous instruction */
else
returncast(Instruction*, &invalidinstruction);
}
/*
** Create a OP_LOADNIL instruction, but try to optimize: if the previous
** instruction is also OP_LOADNIL and ranges are compatible, adjust
** range of previous instruction instead of emitting a new one. (For
** instance, 'local a; local b' will generate a single opcode.)
*/
voidluaK_nil (FuncState*fs, intfrom, intn) {
intl=from+n-1; /* last register to set nil */
Instruction*previous=previousinstruction(fs);
if (GET_OPCODE(*previous) ==OP_LOADNIL) { /* previous is LOADNIL? */
intpfrom=GETARG_A(*previous); /* get previous range */
intpl=pfrom+GETARG_B(*previous);
if ((pfrom <= from&&from <= pl+1) ||
(from <= pfrom&&pfrom <= l+1)) { /* can connect both? */
if (pfrom<from) from=pfrom; /* from = min(from, pfrom) */
if (pl>l) l=pl; /* l = max(l, pl) */
SETARG_A(*previous, from);
SETARG_B(*previous, l-from);
return;
} /* else go through */
}
luaK_codeABC(fs, OP_LOADNIL, from, n-1, 0); /* else no optimization */
}
/*
** Gets the destination address of a jump instruction. Used to traverse
** a list of jumps.
*/
staticintgetjump (FuncState*fs, intpc) {
intoffset=GETARG_sJ(fs->f->code[pc]);
if (offset==NO_JUMP) /* point to itself represents end of list */
returnNO_JUMP; /* end of list */
else
return (pc+1)+offset; /* turn offset into absolute position */
}
/*
** Fix jump instruction at position 'pc' to jump to 'dest'.
** (Jump addresses are relative in Lua)
*/
staticvoidfixjump (FuncState*fs, intpc, intdest) {
Instruction*jmp=&fs->f->code[pc];
intoffset=dest- (pc+1);
lua_assert(dest!=NO_JUMP);
if (!(-OFFSET_sJ <= offset&&offset <= MAXARG_sJ-OFFSET_sJ))
luaX_syntaxerror(fs->ls, "control structure too long");
lua_assert(GET_OPCODE(*jmp) ==OP_JMP);
SETARG_sJ(*jmp, offset);
}
/*
** Concatenate jump-list 'l2' into jump-list 'l1'
*/
voidluaK_concat (FuncState*fs, int*l1, intl2) {
if (l2==NO_JUMP) return; /* nothing to concatenate? */
elseif (*l1==NO_JUMP) /* no original list? */
*l1=l2; /* 'l1' points to 'l2' */
else {
intlist=*l1;
intnext;
while ((next=getjump(fs, list)) !=NO_JUMP) /* find last element */
list=next;
fixjump(fs, list, l2); /* last element links to 'l2' */
}
}
/*
** Create a jump instruction and return its position, so its destination
** can be fixed later (with 'fixjump').
*/
intluaK_jump (FuncState*fs) {
returncodesJ(fs, OP_JMP, NO_JUMP, 0);
}
/*
** Code a 'return' instruction
*/
voidluaK_ret (FuncState*fs, intfirst, intnret) {
OpCodeop;
switch (nret) {
case0: op=OP_RETURN0; break;
case1: op=OP_RETURN1; break;
default: op=OP_RETURN; break;
}
luaK_codeABC(fs, op, first, nret+1, 0);
}
/*
** Code a "conditional jump", that is, a test or comparison opcode
** followed by a jump. Return jump position.
*/
staticintcondjump (FuncState*fs, OpCodeop, intA, intB, intC, intk) {
luaK_codeABCk(fs, op, A, B, C, k);
returnluaK_jump(fs);
}
/*
** returns current 'pc' and marks it as a jump target (to avoid wrong
** optimizations with consecutive instructions not in the same basic block).
*/
intluaK_getlabel (FuncState*fs) {
fs->lasttarget=fs->pc;
returnfs->pc;
}
/*
** Returns the position of the instruction "controlling" a given
** jump (that is, its condition), or the jump itself if it is
** unconditional.
*/
staticInstruction*getjumpcontrol (FuncState*fs, intpc) {
Instruction*pi=&fs->f->code[pc];
if (pc >= 1&&testTMode(GET_OPCODE(*(pi-1))))
returnpi-1;
else
returnpi;
}
/*
** Patch destination register for a TESTSET instruction.
** If instruction in position 'node' is not a TESTSET, return 0 ("fails").
** Otherwise, if 'reg' is not 'NO_REG', set it as the destination
** register. Otherwise, change instruction to a simple 'TEST' (produces
** no register value)
*/
staticintpatchtestreg (FuncState*fs, intnode, intreg) {
Instruction*i=getjumpcontrol(fs, node);
if (GET_OPCODE(*i) !=OP_TESTSET)
return0; /* cannot patch other instructions */
if (reg!=NO_REG&®!=GETARG_B(*i))
SETARG_A(*i, reg);
else {
/* no register to put value or register already has the value;
change instruction to simple test */
*i=CREATE_ABCk(OP_TEST, GETARG_B(*i), 0, 0, GETARG_k(*i));
}
return1;
}
/*
** Traverse a list of tests ensuring no one produces a value
*/
staticvoidremovevalues (FuncState*fs, intlist) {
for (; list!=NO_JUMP; list=getjump(fs, list))
patchtestreg(fs, list, NO_REG);
}
/*
** Traverse a list of tests, patching their destination address and
** registers: tests producing values jump to 'vtarget' (and put their
** values in 'reg'), other tests jump to 'dtarget'.
*/
staticvoidpatchlistaux (FuncState*fs, intlist, intvtarget, intreg,
intdtarget) {
while (list!=NO_JUMP) {
intnext=getjump(fs, list);
if (patchtestreg(fs, list, reg))
fixjump(fs, list, vtarget);
else
fixjump(fs, list, dtarget); /* jump to default target */
list=next;
}
}
/*
** Path all jumps in 'list' to jump to 'target'.
** (The assert means that we cannot fix a jump to a forward address
** because we only know addresses once code is generated.)
*/
voidluaK_patchlist (FuncState*fs, intlist, inttarget) {
lua_assert(target <= fs->pc);
patchlistaux(fs, list, target, NO_REG, target);
}
voidluaK_patchtohere (FuncState*fs, intlist) {
inthr=luaK_getlabel(fs); /* mark "here" as a jump target */
luaK_patchlist(fs, list, hr);
}
/*
** MAXimum number of successive Instructions WiTHout ABSolute line
** information.
*/
#if !defined(MAXIWTHABS)
#defineMAXIWTHABS 120
#endif
/* limit for difference between lines in relative line info. */
#defineLIMLINEDIFF 0x80
/*
** Save line info for a new instruction. If difference from last line
** does not fit in a byte, of after that many instructions, save a new
** absolute line info; (in that case, the special value 'ABSLINEINFO'
** in 'lineinfo' signals the existence of this absolute information.)
** Otherwise, store the difference from last line in 'lineinfo'.
*/
staticvoidsavelineinfo (FuncState*fs, Proto*f, intline) {
intlinedif=line-fs->previousline;
intpc=fs->pc-1; /* last instruction coded */
if (abs(linedif) >= LIMLINEDIFF||fs->iwthabs++>MAXIWTHABS) {
luaM_growvector(fs->ls->L, f->abslineinfo, fs->nabslineinfo,
f->sizeabslineinfo, AbsLineInfo, MAX_INT, "lines");
f->abslineinfo[fs->nabslineinfo].pc=pc;
f->abslineinfo[fs->nabslineinfo++].line=line;
linedif=ABSLINEINFO; /* signal that there is absolute information */
fs->iwthabs=0; /* restart counter */
}
luaM_growvector(fs->ls->L, f->lineinfo, pc, f->sizelineinfo, ls_byte,
MAX_INT, "opcodes");
f->lineinfo[pc] =linedif;
fs->previousline=line; /* last line saved */
}
/*
** Remove line information from the last instruction.
** If line information for that instruction is absolute, set 'iwthabs'
** above its max to force the new (replacing) instruction to have
** absolute line info, too.
*/
staticvoidremovelastlineinfo (FuncState*fs) {
Proto*f=fs->f;
intpc=fs->pc-1; /* last instruction coded */
if (f->lineinfo[pc] !=ABSLINEINFO) { /* relative line info? */
fs->previousline-=f->lineinfo[pc]; /* correct last line saved */
fs->iwthabs--; /* undo previous increment */
}
else { /* absolute line information */
lua_assert(f->abslineinfo[fs->nabslineinfo-1].pc==pc);
fs->nabslineinfo--; /* remove it */
fs->iwthabs=MAXIWTHABS+1; /* force next line info to be absolute */
}
}
/*
** Remove the last instruction created, correcting line information
** accordingly.
*/
staticvoidremovelastinstruction (FuncState*fs) {
removelastlineinfo(fs);
fs->pc--;
}
/*
** Emit instruction 'i', checking for array sizes and saving also its
** line information. Return 'i' position.
*/
intluaK_code (FuncState*fs, Instructioni) {
Proto*f=fs->f;
/* put new instruction in code array */
luaM_growvector(fs->ls->L, f->code, fs->pc, f->sizecode, Instruction,
MAX_INT, "opcodes");
f->code[fs->pc++] =i;
savelineinfo(fs, f, fs->ls->lastline);
returnfs->pc-1; /* index of new instruction */
}
/*
** Format and emit an 'iABC' instruction. (Assertions check consistency
** of parameters versus opcode.)
*/
intluaK_codeABCk (FuncState*fs, OpCodeo, inta, intb, intc, intk) {
lua_assert(getOpMode(o) ==iABC);
lua_assert(a <= MAXARG_A&&b <= MAXARG_B&&
c <= MAXARG_C&& (k& ~1) ==0);
returnluaK_code(fs, CREATE_ABCk(o, a, b, c, k));
}
/*
** Format and emit an 'iABx' instruction.
*/
intluaK_codeABx (FuncState*fs, OpCodeo, inta, unsigned intbc) {
lua_assert(getOpMode(o) ==iABx);
lua_assert(a <= MAXARG_A&&bc <= MAXARG_Bx);
returnluaK_code(fs, CREATE_ABx(o, a, bc));
}
/*
** Format and emit an 'iAsBx' instruction.
*/
intluaK_codeAsBx (FuncState*fs, OpCodeo, inta, intbc) {
unsigned intb=bc+OFFSET_sBx;
lua_assert(getOpMode(o) ==iAsBx);
lua_assert(a <= MAXARG_A&&b <= MAXARG_Bx);
returnluaK_code(fs, CREATE_ABx(o, a, b));
}
/*
** Format and emit an 'isJ' instruction.
*/
staticintcodesJ (FuncState*fs, OpCodeo, intsj, intk) {
unsigned intj=sj+OFFSET_sJ;
lua_assert(getOpMode(o) ==isJ);
lua_assert(j <= MAXARG_sJ&& (k& ~1) ==0);
returnluaK_code(fs, CREATE_sJ(o, j, k));
}
/*
** Emit an "extra argument" instruction (format 'iAx')
*/
staticintcodeextraarg (FuncState*fs, inta) {
lua_assert(a <= MAXARG_Ax);
returnluaK_code(fs, CREATE_Ax(OP_EXTRAARG, a));
}
/*
** Emit a "load constant" instruction, using either 'OP_LOADK'
** (if constant index 'k' fits in 18 bits) or an 'OP_LOADKX'
** instruction with "extra argument".
*/
staticintluaK_codek (FuncState*fs, intreg, intk) {
if (k <= MAXARG_Bx)
returnluaK_codeABx(fs, OP_LOADK, reg, k);
else {
intp=luaK_codeABx(fs, OP_LOADKX, reg, 0);
codeextraarg(fs, k);
returnp;
}
}
/*
** Check register-stack level, keeping track of its maximum size
** in field 'maxstacksize'
*/
voidluaK_checkstack (FuncState*fs, intn) {
intnewstack=fs->freereg+n;
if (newstack>fs->f->maxstacksize) {
if (newstack >= MAXREGS)
luaX_syntaxerror(fs->ls,
"function or expression needs too many registers");
fs->f->maxstacksize=cast_byte(newstack);
}
}
/*
** Reserve 'n' registers in register stack
*/
voidluaK_reserveregs (FuncState*fs, intn) {
luaK_checkstack(fs, n);
fs->freereg+=n;
}
/*
** Free register 'reg', if it is neither a constant index nor
** a local variable.
)
*/
staticvoidfreereg (FuncState*fs, intreg) {
if (reg >= luaY_nvarstack(fs)) {
fs->freereg--;
lua_assert(reg==fs->freereg);
}
}
/*
** Free two registers in proper order
*/
staticvoidfreeregs (FuncState*fs, intr1, intr2) {
if (r1>r2) {
freereg(fs, r1);
freereg(fs, r2);
}
else {
freereg(fs, r2);
freereg(fs, r1);
}
}
/*
** Free register used by expression 'e' (if any)
*/
staticvoidfreeexp (FuncState*fs, expdesc*e) {
if (e->k==VNONRELOC)
freereg(fs, e->u.info);
}
/*
** Free registers used by expressions 'e1' and 'e2' (if any) in proper
** order.
*/
staticvoidfreeexps (FuncState*fs, expdesc*e1, expdesc*e2) {
intr1= (e1->k==VNONRELOC) ? e1->u.info : -1;
intr2= (e2->k==VNONRELOC) ? e2->u.info : -1;
freeregs(fs, r1, r2);
}
/*
** Add constant 'v' to prototype's list of constants (field 'k').
** Use scanner's table to cache position of constants in constant list
** and try to reuse constants. Because some values should not be used
** as keys (nil cannot be a key, integer keys can collapse with float
** keys), the caller must provide a useful 'key' for indexing the cache.
*/
staticintaddk (FuncState*fs, TValue*key, TValue*v) {
lua_State*L=fs->ls->L;
Proto*f=fs->f;
TValue*idx=luaH_set(L, fs->ls->h, key); /* index scanner table */
intk, oldsize;
if (ttisinteger(idx)) { /* is there an index there? */
k=cast_int(ivalue(idx));
/* correct value? (warning: must distinguish floats from integers!) */
if (k<fs->nk&&ttypetag(&f->k[k]) ==ttypetag(v) &&
luaV_rawequalobj(&f->k[k], v))
returnk; /* reuse index */
}
/* constant not found; create a new entry */
oldsize=f->sizek;
k=fs->nk;
/* numerical value does not need GC barrier;
table has no metatable, so it does not need to invalidate cache */
setivalue(idx, k);
luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
while (oldsize<f->sizek) setnilvalue(&f->k[oldsize++]);
setobj(L, &f->k[k], v);
fs->nk++;
luaC_barrier(L, f, v);
returnk;
}
/*
** Add a string to list of constants and return its index.
*/
staticintstringK (FuncState*fs, TString*s) {
TValueo;
setsvalue(fs->ls->L, &o, s);
returnaddk(fs, &o, &o); /* use string itself as key */
}
/*
** Add an integer to list of constants and return its index.
** Integers use userdata as keys to avoid collision with floats with
** same value; conversion to 'void*' is used only for hashing, so there
** are no "precision" problems.
*/
staticintluaK_intK (FuncState*fs, lua_Integern) {
TValuek, o;
setpvalue(&k, cast_voidp(cast_sizet(n)));
setivalue(&o, n);
returnaddk(fs, &k, &o);
}
/*
** Add a float to list of constants and return its index.
*/
staticintluaK_numberK (FuncState*fs, lua_Numberr) {
TValueo;
setfltvalue(&o, r);
returnaddk(fs, &o, &o); /* use number itself as key */
}
/*
** Add a false to list of constants and return its index.
*/
staticintboolF (FuncState*fs) {
TValueo;
setbfvalue(&o);
returnaddk(fs, &o, &o); /* use boolean itself as key */
}
/*
** Add a true to list of constants and return its index.
*/
staticintboolT (FuncState*fs) {
TValueo;
setbtvalue(&o);
returnaddk(fs, &o, &o); /* use boolean itself as key */
}
/*
** Add nil to list of constants and return its index.
*/
staticintnilK (FuncState*fs) {
TValuek, v;
setnilvalue(&v);
/* cannot use nil as key; instead use table itself to represent nil */
sethvalue(fs->ls->L, &k, fs->ls->h);
returnaddk(fs, &k, &v);
}
/*
** Check whether 'i' can be stored in an 'sC' operand. Equivalent to
** (0 <= int2sC(i) && int2sC(i) <= MAXARG_C) but without risk of
** overflows in the hidden addition inside 'int2sC'.
*/
staticintfitsC (lua_Integeri) {
return (l_castS2U(i) +OFFSET_sC <= cast_uint(MAXARG_C));
}
/*
** Check whether 'i' can be stored in an 'sBx' operand.
*/
staticintfitsBx (lua_Integeri) {
return (-OFFSET_sBx <= i&&i <= MAXARG_Bx-OFFSET_sBx);
}
voidluaK_int (FuncState*fs, intreg, lua_Integeri) {
if (fitsBx(i))
luaK_codeAsBx(fs, OP_LOADI, reg, cast_int(i));
else
luaK_codek(fs, reg, luaK_intK(fs, i));
}
staticvoidluaK_float (FuncState*fs, intreg, lua_Numberf) {
lua_Integerfi;
if (luaV_flttointeger(f, &fi, F2Ieq) &&fitsBx(fi))
luaK_codeAsBx(fs, OP_LOADF, reg, cast_int(fi));
else
luaK_codek(fs, reg, luaK_numberK(fs, f));
}
/*
** Convert a constant in 'v' into an expression description 'e'
*/
staticvoidconst2exp (TValue*v, expdesc*e) {
switch (ttypetag(v)) {
caseLUA_VNUMINT:
e->k=VKINT; e->u.ival=ivalue(v);
break;
caseLUA_VNUMFLT:
e->k=VKFLT; e->u.nval=fltvalue(v);
break;
caseLUA_VFALSE:
e->k=VFALSE;
break;
caseLUA_VTRUE:
e->k=VTRUE;
break;
caseLUA_VNIL:
e->k=VNIL;
break;
caseLUA_VSHRSTR: caseLUA_VLNGSTR:
e->k=VKSTR; e->u.strval=tsvalue(v);
break;
default: lua_assert(0);
}
}
/*
** Fix an expression to return the number of results 'nresults'.
** 'e' must be a multi-ret expression (function call or vararg).
*/
voidluaK_setreturns (FuncState*fs, expdesc*e, intnresults) {
Instruction*pc=&getinstruction(fs, e);
if (e->k==VCALL) /* expression is an open function call? */
SETARG_C(*pc, nresults+1);
else {
lua_assert(e->k==VVARARG);
SETARG_C(*pc, nresults+1);
SETARG_A(*pc, fs->freereg);
luaK_reserveregs(fs, 1);
}
}
/*
** Convert a VKSTR to a VK
*/
staticvoidstr2K (FuncState*fs, expdesc*e) {
lua_assert(e->k==VKSTR);
e->u.info=stringK(fs, e->u.strval);
e->k=VK;
}
/*
** Fix an expression to return one result.
** If expression is not a multi-ret expression (function call or
** vararg), it already returns one result, so nothing needs to be done.
** Function calls become VNONRELOC expressions (as its result comes
** fixed in the base register of the call), while vararg expressions
** become VRELOC (as OP_VARARG puts its results where it wants).
** (Calls are created returning one result, so that does not need
** to be fixed.)
*/
voidluaK_setoneret (FuncState*fs, expdesc*e) {
if (e->k==VCALL) { /* expression is an open function call? */
/* already returns 1 value */
lua_assert(GETARG_C(getinstruction(fs, e)) ==2);
e->k=VNONRELOC; /* result has fixed position */
e->u.info=GETARG_A(getinstruction(fs, e));
}
elseif (e->k==VVARARG) {
SETARG_C(getinstruction(fs, e), 2);
e->k=VRELOC; /* can relocate its simple result */
}
}
/*
** Ensure that expression 'e' is not a variable (nor a constant).
** (Expression still may have jump lists.)
*/
voidluaK_dischargevars (FuncState*fs, expdesc*e) {
switch (e->k) {
caseVCONST: {
const2exp(const2val(fs, e), e);
break;
}
caseVLOCAL: { /* already in a register */
e->u.info=e->u.var.sidx;
e->k=VNONRELOC; /* becomes a non-relocatable value */
break;
}
caseVUPVAL: { /* move value to some (pending) register */
e->u.info=luaK_codeABC(fs, OP_GETUPVAL, 0, e->u.info, 0);
e->k=VRELOC;
break;
}
caseVINDEXUP: {
e->u.info=luaK_codeABC(fs, OP_GETTABUP, 0, e->u.ind.t, e->u.ind.idx);
e->k=VRELOC;
break;
}
caseVINDEXI: {
freereg(fs, e->u.ind.t);
e->u.info=luaK_codeABC(fs, OP_GETI, 0, e->u.ind.t, e->u.ind.idx);
e->k=VRELOC;
break;
}
caseVINDEXSTR: {
freereg(fs, e->u.ind.t);
e->u.info=luaK_codeABC(fs, OP_GETFIELD, 0, e->u.ind.t, e->u.ind.idx);
e->k=VRELOC;
break;
}
caseVINDEXED: {
freeregs(fs, e->u.ind.t, e->u.ind.idx);
e->u.info=luaK_codeABC(fs, OP_GETTABLE, 0, e->u.ind.t, e->u.ind.idx);
e->k=VRELOC;
break;
}
caseVVARARG: caseVCALL: {
luaK_setoneret(fs, e);
break;
}
default: break; /* there is one value available (somewhere) */
}
}
/*
** Ensures expression value is in register 'reg' (and therefore
** 'e' will become a non-relocatable expression).
** (Expression still may have jump lists.)
*/
staticvoiddischarge2reg (FuncState*fs, expdesc*e, intreg) {
luaK_dischargevars(fs, e);
switch (e->k) {
caseVNIL: {
luaK_nil(fs, reg, 1);
break;
}
caseVFALSE: {
luaK_codeABC(fs, OP_LOADFALSE, reg, 0, 0);
break;
}
caseVTRUE: {
luaK_codeABC(fs, OP_LOADTRUE, reg, 0, 0);
break;
}
caseVKSTR: {
str2K(fs, e);
} /* FALLTHROUGH */
caseVK: {
luaK_codek(fs, reg, e->u.info);
break;
}
caseVKFLT: {
luaK_float(fs, reg, e->u.nval);
break;
}
caseVKINT: {
luaK_int(fs, reg, e->u.ival);
break;
}
caseVRELOC: {
Instruction*pc=&getinstruction(fs, e);
SETARG_A(*pc, reg); /* instruction will put result in 'reg' */
break;
}
caseVNONRELOC: {
if (reg!=e->u.info)
luaK_codeABC(fs, OP_MOVE, reg, e->u.info, 0);
break;
}
default: {
lua_assert(e->k==VJMP);
return; /* nothing to do... */
}
}
e->u.info=reg;
e->k=VNONRELOC;
}
/*
** Ensures expression value is in any register.
** (Expression still may have jump lists.)
*/
staticvoiddischarge2anyreg (FuncState*fs, expdesc*e) {
if (e->k!=VNONRELOC) { /* no fixed register yet? */
luaK_reserveregs(fs, 1); /* get a register */
discharge2reg(fs, e, fs->freereg-1); /* put value there */
}
}
staticintcode_loadbool (FuncState*fs, intA, OpCodeop) {
luaK_getlabel(fs); /* those instructions may be jump targets */
returnluaK_codeABC(fs, op, A, 0, 0);
}
/*
** check whether list has any jump that do not produce a value
** or produce an inverted value
*/
staticintneed_value (FuncState*fs, intlist) {
for (; list!=NO_JUMP; list=getjump(fs, list)) {
Instructioni=*getjumpcontrol(fs, list);
if (GET_OPCODE(i) !=OP_TESTSET) return1;
}
return0; /* not found */
}
/*
** Ensures final expression result (which includes results from its
** jump lists) is in register 'reg'.
** If expression has jumps, need to patch these jumps either to
** its final position or to "load" instructions (for those tests
** that do not produce values).
*/
staticvoidexp2reg (FuncState*fs, expdesc*e, intreg) {
discharge2reg(fs, e, reg);
if (e->k==VJMP) /* expression itself is a test? */
luaK_concat(fs, &e->t, e->u.info); /* put this jump in 't' list */
if (hasjumps(e)) {
intfinal; /* position after whole expression */
intp_f=NO_JUMP; /* position of an eventual LOAD false */
intp_t=NO_JUMP; /* position of an eventual LOAD true */
if (need_value(fs, e->t) ||need_value(fs, e->f)) {
intfj= (e->k==VJMP) ? NO_JUMP : luaK_jump(fs);
p_f=code_loadbool(fs, reg, OP_LFALSESKIP); /* skip next inst. */
p_t=code_loadbool(fs, reg, OP_LOADTRUE);
/* jump around these booleans if 'e' is not a test */
luaK_patchtohere(fs, fj);
}
final=luaK_getlabel(fs);
patchlistaux(fs, e->f, final, reg, p_f);
patchlistaux(fs, e->t, final, reg, p_t);
}
e->f=e->t=NO_JUMP;
e->u.info=reg;
e->k=VNONRELOC;
}
/*
** Ensures final expression result is in next available register.
*/
voidluaK_exp2nextreg (FuncState*fs, expdesc*e) {
luaK_dischargevars(fs, e);
freeexp(fs, e);
luaK_reserveregs(fs, 1);
exp2reg(fs, e, fs->freereg-1);
}
/*
** Ensures final expression result is in some (any) register
** and return that register.
*/
intluaK_exp2anyreg (FuncState*fs, expdesc*e) {
luaK_dischargevars(fs, e);
if (e->k==VNONRELOC) { /* expression already has a register? */
if (!hasjumps(e)) /* no jumps? */
returne->u.info; /* result is already in a register */
if (e->u.info >= luaY_nvarstack(fs)) { /* reg. is not a local? */
exp2reg(fs, e, e->u.info); /* put final result in it */
returne->u.info;
}
}
luaK_exp2nextreg(fs, e); /* otherwise, use next available register */
returne->u.info;
}
/*
** Ensures final expression result is either in a register
** or in an upvalue.
*/
voidluaK_exp2anyregup (FuncState*fs, expdesc*e) {
if (e->k!=VUPVAL||hasjumps(e))
luaK_exp2anyreg(fs, e);
}
/*
** Ensures final expression result is either in a register
** or it is a constant.
*/
voidluaK_exp2val (FuncState*fs, expdesc*e) {
if (hasjumps(e))
luaK_exp2anyreg(fs, e);
else
luaK_dischargevars(fs, e);
}
/*
** Try to make 'e' a K expression with an index in the range of R/K
** indices. Return true iff succeeded.
*/
staticintluaK_exp2K (FuncState*fs, expdesc*e) {
if (!hasjumps(e)) {
intinfo;
switch (e->k) { /* move constants to 'k' */
caseVTRUE: info=boolT(fs); break;
caseVFALSE: info=boolF(fs); break;
caseVNIL: info=nilK(fs); break;
caseVKINT: info=luaK_intK(fs, e->u.ival); break;
caseVKFLT: info=luaK_numberK(fs, e->u.nval); break;
caseVKSTR: info=stringK(fs, e->u.strval); break;
caseVK: info=e->u.info; break;
default: return0; /* not a constant */
}
if (info <= MAXINDEXRK) { /* does constant fit in 'argC'? */
e->k=VK; /* make expression a 'K' expression */
e->u.info=info;
return1;
}
}
/* else, expression doesn't fit; leave it unchanged */