-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlj_crecord.c
1653 lines (1569 loc) · 52.3 KB
/
lj_crecord.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
/*
** Trace recorder for C data operations.
** Copyright (C) 2005-2013 Mike Pall. See Copyright Notice in luajit.h
*/
#define lj_ffrecord_c
#define LUA_CORE
#include "lj_obj.h"
#if LJ_HASJIT && LJ_HASFFI
#include "lj_err.h"
#include "lj_str.h"
#include "lj_tab.h"
#include "lj_frame.h"
#include "lj_ctype.h"
#include "lj_cdata.h"
#include "lj_cparse.h"
#include "lj_cconv.h"
#include "lj_clib.h"
#include "lj_ccall.h"
#include "lj_ff.h"
#include "lj_ir.h"
#include "lj_jit.h"
#include "lj_ircall.h"
#include "lj_iropt.h"
#include "lj_trace.h"
#include "lj_record.h"
#include "lj_ffrecord.h"
#include "lj_snap.h"
#include "lj_crecord.h"
#include "lj_dispatch.h"
/* Some local macros to save typing. Undef'd at the end. */
#define IR(ref) (&J->cur.ir[(ref)])
/* Pass IR on to next optimization in chain (FOLD). */
#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))
#define emitconv(a, dt, st, flags) \
emitir(IRT(IR_CONV, (dt)), (a), (st)|((dt) << 5)|(flags))
/* -- C type checks ------------------------------------------------------- */
static GCcdata *argv2cdata(jit_State *J, TRef tr, cTValue *o)
{
GCcdata *cd;
TRef trtypeid;
if (!tref_iscdata(tr))
lj_trace_err(J, LJ_TRERR_BADTYPE);
cd = cdataV(o);
/* Specialize to the CTypeID. */
trtypeid = emitir(IRT(IR_FLOAD, IRT_U16), tr, IRFL_CDATA_CTYPEID);
emitir(IRTG(IR_EQ, IRT_INT), trtypeid, lj_ir_kint(J, (int32_t)cd->ctypeid));
return cd;
}
/* Specialize to the CTypeID held by a cdata constructor. */
static CTypeID crec_constructor(jit_State *J, GCcdata *cd, TRef tr)
{
CTypeID id;
lua_assert(tref_iscdata(tr) && cd->ctypeid == CTID_CTYPEID);
id = *(CTypeID *)cdataptr(cd);
tr = emitir(IRT(IR_FLOAD, IRT_INT), tr, IRFL_CDATA_INT);
emitir(IRTG(IR_EQ, IRT_INT), tr, lj_ir_kint(J, (int32_t)id));
return id;
}
static CTypeID argv2ctype(jit_State *J, TRef tr, cTValue *o)
{
if (tref_isstr(tr)) {
GCstr *s = strV(o);
CPState cp;
CTypeID oldtop;
/* Specialize to the string containing the C type declaration. */
emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, s));
cp.L = J->L;
cp.cts = ctype_ctsG(J2G(J));
oldtop = cp.cts->top;
cp.srcname = strdata(s);
cp.p = strdata(s);
cp.param = NULL;
cp.mode = CPARSE_MODE_ABSTRACT|CPARSE_MODE_NOIMPLICIT;
if (lj_cparse(&cp) || cp.cts->top > oldtop) /* Avoid new struct defs. */
lj_trace_err(J, LJ_TRERR_BADTYPE);
return cp.val.id;
} else {
GCcdata *cd = argv2cdata(J, tr, o);
return cd->ctypeid == CTID_CTYPEID ? crec_constructor(J, cd, tr) :
cd->ctypeid;
}
}
/* Convert CType to IRType (if possible). */
static IRType crec_ct2irt(CTState *cts, CType *ct)
{
if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
if (LJ_LIKELY(ctype_isnum(ct->info))) {
if ((ct->info & CTF_FP)) {
if (ct->size == sizeof(double))
return IRT_NUM;
else if (ct->size == sizeof(float))
return IRT_FLOAT;
} else {
uint32_t b = lj_fls(ct->size);
if (b <= 3)
return IRT_I8 + 2*b + ((ct->info & CTF_UNSIGNED) ? 1 : 0);
}
} else if (ctype_isptr(ct->info)) {
return (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32;
} else if (ctype_iscomplex(ct->info)) {
if (ct->size == 2*sizeof(double))
return IRT_NUM;
else if (ct->size == 2*sizeof(float))
return IRT_FLOAT;
}
return IRT_CDATA;
}
/* -- Optimized memory fill and copy -------------------------------------- */
/* Maximum length and unroll of inlined copy/fill. */
#define CREC_COPY_MAXUNROLL 16
#define CREC_COPY_MAXLEN 128
#define CREC_FILL_MAXUNROLL 16
/* Number of windowed registers used for optimized memory copy. */
#if LJ_TARGET_X86
#define CREC_COPY_REGWIN 2
#elif LJ_TARGET_PPC || LJ_TARGET_MIPS
#define CREC_COPY_REGWIN 8
#else
#define CREC_COPY_REGWIN 4
#endif
/* List of memory offsets for copy/fill. */
typedef struct CRecMemList {
CTSize ofs; /* Offset in bytes. */
IRType tp; /* Type of load/store. */
TRef trofs; /* TRef of interned offset. */
TRef trval; /* TRef of load value. */
} CRecMemList;
/* Generate copy list for element-wise struct copy. */
static MSize crec_copy_struct(CRecMemList *ml, CTState *cts, CType *ct)
{
CTypeID fid = ct->sib;
MSize mlp = 0;
while (fid) {
CType *df = ctype_get(cts, fid);
fid = df->sib;
if (ctype_isfield(df->info)) {
CType *cct;
IRType tp;
if (!gcref(df->name)) continue; /* Ignore unnamed fields. */
cct = ctype_rawchild(cts, df); /* Field type. */
tp = crec_ct2irt(cts, cct);
if (tp == IRT_CDATA) return 0; /* NYI: aggregates. */
if (mlp >= CREC_COPY_MAXUNROLL) return 0;
ml[mlp].ofs = df->size;
ml[mlp].tp = tp;
mlp++;
if (ctype_iscomplex(cct->info)) {
if (mlp >= CREC_COPY_MAXUNROLL) return 0;
ml[mlp].ofs = df->size + (cct->size >> 1);
ml[mlp].tp = tp;
mlp++;
}
} else if (!ctype_isconstval(df->info)) {
/* NYI: bitfields and sub-structures. */
return 0;
}
}
return mlp;
}
/* Generate unrolled copy list, from highest to lowest step size/alignment. */
static MSize crec_copy_unroll(CRecMemList *ml, CTSize len, CTSize step,
IRType tp)
{
CTSize ofs = 0;
MSize mlp = 0;
if (tp == IRT_CDATA) tp = IRT_U8 + 2*lj_fls(step);
do {
while (ofs + step <= len) {
if (mlp >= CREC_COPY_MAXUNROLL) return 0;
ml[mlp].ofs = ofs;
ml[mlp].tp = tp;
mlp++;
ofs += step;
}
step >>= 1;
tp -= 2;
} while (ofs < len);
return mlp;
}
/*
** Emit copy list with windowed loads/stores.
** LJ_TARGET_UNALIGNED: may emit unaligned loads/stores (not marked as such).
*/
static void crec_copy_emit(jit_State *J, CRecMemList *ml, MSize mlp,
TRef trdst, TRef trsrc)
{
MSize i, j, rwin = 0;
for (i = 0, j = 0; i < mlp; ) {
TRef trofs = lj_ir_kintp(J, ml[i].ofs);
TRef trsptr = emitir(IRT(IR_ADD, IRT_PTR), trsrc, trofs);
ml[i].trval = emitir(IRT(IR_XLOAD, ml[i].tp), trsptr, 0);
ml[i].trofs = trofs;
i++;
rwin += (LJ_SOFTFP && ml[i].tp == IRT_NUM) ? 2 : 1;
if (rwin >= CREC_COPY_REGWIN || i >= mlp) { /* Flush buffered stores. */
rwin = 0;
for ( ; j < i; j++) {
TRef trdptr = emitir(IRT(IR_ADD, IRT_PTR), trdst, ml[j].trofs);
emitir(IRT(IR_XSTORE, ml[j].tp), trdptr, ml[j].trval);
}
}
}
}
/* Optimized memory copy. */
static void crec_copy(jit_State *J, TRef trdst, TRef trsrc, TRef trlen,
CType *ct)
{
if (tref_isk(trlen)) { /* Length must be constant. */
CRecMemList ml[CREC_COPY_MAXUNROLL];
MSize mlp = 0;
CTSize step = 1, len = (CTSize)IR(tref_ref(trlen))->i;
IRType tp = IRT_CDATA;
int needxbar = 0;
if (len == 0) return; /* Shortcut. */
if (len > CREC_COPY_MAXLEN) goto fallback;
if (ct) {
CTState *cts = ctype_ctsG(J2G(J));
lua_assert(ctype_isarray(ct->info) || ctype_isstruct(ct->info));
if (ctype_isarray(ct->info)) {
CType *cct = ctype_rawchild(cts, ct);
tp = crec_ct2irt(cts, cct);
if (tp == IRT_CDATA) goto rawcopy;
step = lj_ir_type_size[tp];
lua_assert((len & (step-1)) == 0);
} else if ((ct->info & CTF_UNION)) {
step = (1u << ctype_align(ct->info));
goto rawcopy;
} else {
mlp = crec_copy_struct(ml, cts, ct);
goto emitcopy;
}
} else {
rawcopy:
needxbar = 1;
if (LJ_TARGET_UNALIGNED || step >= CTSIZE_PTR)
step = CTSIZE_PTR;
}
mlp = crec_copy_unroll(ml, len, step, tp);
emitcopy:
if (mlp) {
crec_copy_emit(J, ml, mlp, trdst, trsrc);
if (needxbar)
emitir(IRT(IR_XBAR, IRT_NIL), 0, 0);
return;
}
}
fallback:
/* Call memcpy. Always needs a barrier to disable alias analysis. */
lj_ir_call(J, IRCALL_memcpy, trdst, trsrc, trlen);
emitir(IRT(IR_XBAR, IRT_NIL), 0, 0);
}
/* Generate unrolled fill list, from highest to lowest step size/alignment. */
static MSize crec_fill_unroll(CRecMemList *ml, CTSize len, CTSize step)
{
CTSize ofs = 0;
MSize mlp = 0;
IRType tp = IRT_U8 + 2*lj_fls(step);
do {
while (ofs + step <= len) {
if (mlp >= CREC_COPY_MAXUNROLL) return 0;
ml[mlp].ofs = ofs;
ml[mlp].tp = tp;
mlp++;
ofs += step;
}
step >>= 1;
tp -= 2;
} while (ofs < len);
return mlp;
}
/*
** Emit stores for fill list.
** LJ_TARGET_UNALIGNED: may emit unaligned stores (not marked as such).
*/
static void crec_fill_emit(jit_State *J, CRecMemList *ml, MSize mlp,
TRef trdst, TRef trfill)
{
MSize i;
for (i = 0; i < mlp; i++) {
TRef trofs = lj_ir_kintp(J, ml[i].ofs);
TRef trdptr = emitir(IRT(IR_ADD, IRT_PTR), trdst, trofs);
emitir(IRT(IR_XSTORE, ml[i].tp), trdptr, trfill);
}
}
/* Optimized memory fill. */
static void crec_fill(jit_State *J, TRef trdst, TRef trlen, TRef trfill,
CTSize step)
{
if (tref_isk(trlen)) { /* Length must be constant. */
CRecMemList ml[CREC_FILL_MAXUNROLL];
MSize mlp;
CTSize len = (CTSize)IR(tref_ref(trlen))->i;
if (len == 0) return; /* Shortcut. */
if (LJ_TARGET_UNALIGNED || step >= CTSIZE_PTR)
step = CTSIZE_PTR;
if (step * CREC_FILL_MAXUNROLL < len) goto fallback;
mlp = crec_fill_unroll(ml, len, step);
if (!mlp) goto fallback;
if (tref_isk(trfill) || ml[0].tp != IRT_U8)
trfill = emitconv(trfill, IRT_INT, IRT_U8, 0);
if (ml[0].tp != IRT_U8) { /* Scatter U8 to U16/U32/U64. */
if (CTSIZE_PTR == 8 && ml[0].tp == IRT_U64) {
if (tref_isk(trfill)) /* Pointless on x64 with zero-extended regs. */
trfill = emitconv(trfill, IRT_U64, IRT_U32, 0);
trfill = emitir(IRT(IR_MUL, IRT_U64), trfill,
lj_ir_kint64(J, U64x(01010101,01010101)));
} else {
trfill = emitir(IRTI(IR_MUL), trfill,
lj_ir_kint(J, ml[0].tp == IRT_U16 ? 0x0101 : 0x01010101));
}
}
crec_fill_emit(J, ml, mlp, trdst, trfill);
} else {
fallback:
/* Call memset. Always needs a barrier to disable alias analysis. */
lj_ir_call(J, IRCALL_memset, trdst, trfill, trlen); /* Note: arg order! */
}
emitir(IRT(IR_XBAR, IRT_NIL), 0, 0);
}
/* -- Convert C type to C type -------------------------------------------- */
/*
** This code mirrors the code in lj_cconv.c. It performs the same steps
** for the trace recorder that lj_cconv.c does for the interpreter.
**
** One major difference is that we can get away with much fewer checks
** here. E.g. checks for casts, constness or correct types can often be
** omitted, even if they might fail. The interpreter subsequently throws
** an error, which aborts the trace.
**
** All operations are specialized to their C types, so the on-trace
** outcome must be the same as the outcome in the interpreter. If the
** interpreter doesn't throw an error, then the trace is correct, too.
** Care must be taken not to generate invalid (temporary) IR or to
** trigger asserts.
*/
/* Determine whether a passed number or cdata number is non-zero. */
static int crec_isnonzero(CType *s, void *p)
{
if (p == (void *)0)
return 0;
if (p == (void *)1)
return 1;
if ((s->info & CTF_FP)) {
if (s->size == sizeof(float))
return (*(float *)p != 0);
else
return (*(double *)p != 0);
} else {
if (s->size == 1)
return (*(uint8_t *)p != 0);
else if (s->size == 2)
return (*(uint16_t *)p != 0);
else if (s->size == 4)
return (*(uint32_t *)p != 0);
else
return (*(uint64_t *)p != 0);
}
}
static TRef crec_ct_ct(jit_State *J, CType *d, CType *s, TRef dp, TRef sp,
void *svisnz)
{
IRType dt = crec_ct2irt(ctype_ctsG(J2G(J)), d);
IRType st = crec_ct2irt(ctype_ctsG(J2G(J)), s);
CTSize dsize = d->size, ssize = s->size;
CTInfo dinfo = d->info, sinfo = s->info;
if (ctype_type(dinfo) > CT_MAYCONVERT || ctype_type(sinfo) > CT_MAYCONVERT)
goto err_conv;
/*
** Note: Unlike lj_cconv_ct_ct(), sp holds the _value_ of pointers and
** numbers up to 8 bytes. Otherwise sp holds a pointer.
*/
switch (cconv_idx2(dinfo, sinfo)) {
/* Destination is a bool. */
case CCX(B, B):
goto xstore; /* Source operand is already normalized. */
case CCX(B, I):
case CCX(B, F):
if (st != IRT_CDATA) {
/* Specialize to the result of a comparison against 0. */
TRef zero = (st == IRT_NUM || st == IRT_FLOAT) ? lj_ir_knum(J, 0) :
(st == IRT_I64 || st == IRT_U64) ? lj_ir_kint64(J, 0) :
lj_ir_kint(J, 0);
int isnz = crec_isnonzero(s, svisnz);
emitir(IRTG(isnz ? IR_NE : IR_EQ, st), sp, zero);
sp = lj_ir_kint(J, isnz);
goto xstore;
}
goto err_nyi;
/* Destination is an integer. */
case CCX(I, B):
case CCX(I, I):
conv_I_I:
if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
/* Extend 32 to 64 bit integer. */
if (dsize == 8 && ssize < 8 && !(LJ_64 && (sinfo & CTF_UNSIGNED)))
sp = emitconv(sp, dt, ssize < 4 ? IRT_INT : st,
(sinfo & CTF_UNSIGNED) ? 0 : IRCONV_SEXT);
else if (dsize < 8 && ssize == 8) /* Truncate from 64 bit integer. */
sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, st, 0);
else if (st == IRT_INT)
sp = lj_opt_narrow_toint(J, sp);
xstore:
if (dt == IRT_I64 || dt == IRT_U64) lj_needsplit(J);
if (dp == 0) return sp;
emitir(IRT(IR_XSTORE, dt), dp, sp);
break;
case CCX(I, C):
sp = emitir(IRT(IR_XLOAD, st), sp, 0); /* Load re. */
/* fallthrough */
case CCX(I, F):
if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, st, IRCONV_TRUNC|IRCONV_ANY);
goto xstore;
case CCX(I, P):
case CCX(I, A):
sinfo = CTINFO(CT_NUM, CTF_UNSIGNED);
ssize = CTSIZE_PTR;
st = IRT_UINTP;
if (((dsize ^ ssize) & 8) == 0) { /* Must insert no-op type conversion. */
sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, IRT_PTR, 0);
goto xstore;
}
goto conv_I_I;
/* Destination is a floating-point number. */
case CCX(F, B):
case CCX(F, I):
conv_F_I:
if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
sp = emitconv(sp, dt, ssize < 4 ? IRT_INT : st, 0);
goto xstore;
case CCX(F, C):
sp = emitir(IRT(IR_XLOAD, st), sp, 0); /* Load re. */
/* fallthrough */
case CCX(F, F):
conv_F_F:
if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
if (dt != st) sp = emitconv(sp, dt, st, 0);
goto xstore;
/* Destination is a complex number. */
case CCX(C, I):
case CCX(C, F):
{ /* Clear im. */
TRef ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, (dsize >> 1)));
emitir(IRT(IR_XSTORE, dt), ptr, lj_ir_knum(J, 0));
}
/* Convert to re. */
if ((sinfo & CTF_FP)) goto conv_F_F; else goto conv_F_I;
case CCX(C, C):
if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
{
TRef re, im, ptr;
re = emitir(IRT(IR_XLOAD, st), sp, 0);
ptr = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, (ssize >> 1)));
im = emitir(IRT(IR_XLOAD, st), ptr, 0);
if (dt != st) {
re = emitconv(re, dt, st, 0);
im = emitconv(im, dt, st, 0);
}
emitir(IRT(IR_XSTORE, dt), dp, re);
ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, (dsize >> 1)));
emitir(IRT(IR_XSTORE, dt), ptr, im);
}
break;
/* Destination is a vector. */
case CCX(V, I):
case CCX(V, F):
case CCX(V, C):
case CCX(V, V):
goto err_nyi;
/* Destination is a pointer. */
case CCX(P, P):
case CCX(P, A):
case CCX(P, S):
/* There are only 32 bit pointers/addresses on 32 bit machines.
** Also ok on x64, since all 32 bit ops clear the upper part of the reg.
*/
goto xstore;
case CCX(P, I):
if (st == IRT_CDATA) goto err_nyi;
if (!LJ_64 && ssize == 8) /* Truncate from 64 bit integer. */
sp = emitconv(sp, IRT_U32, st, 0);
goto xstore;
case CCX(P, F):
if (st == IRT_CDATA) goto err_nyi;
/* The signed conversion is cheaper. x64 really has 47 bit pointers. */
sp = emitconv(sp, (LJ_64 && dsize == 8) ? IRT_I64 : IRT_U32,
st, IRCONV_TRUNC|IRCONV_ANY);
goto xstore;
/* Destination is an array. */
case CCX(A, A):
/* Destination is a struct/union. */
case CCX(S, S):
if (dp == 0) goto err_conv;
crec_copy(J, dp, sp, lj_ir_kint(J, dsize), d);
break;
default:
err_conv:
err_nyi:
lj_trace_err(J, LJ_TRERR_NYICONV);
break;
}
return 0;
}
/* -- Convert C type to TValue (load) ------------------------------------- */
static TRef crec_tv_ct(jit_State *J, CType *s, CTypeID sid, TRef sp)
{
CTState *cts = ctype_ctsG(J2G(J));
IRType t = crec_ct2irt(cts, s);
CTInfo sinfo = s->info;
if (ctype_isnum(sinfo)) {
TRef tr;
if (t == IRT_CDATA)
goto err_nyi; /* NYI: copyval of >64 bit integers. */
tr = emitir(IRT(IR_XLOAD, t), sp, 0);
if (t == IRT_FLOAT || t == IRT_U32) { /* Keep uint32_t/float as numbers. */
return emitconv(tr, IRT_NUM, t, 0);
} else if (t == IRT_I64 || t == IRT_U64) { /* Box 64 bit integer. */
sp = tr;
lj_needsplit(J);
} else if ((sinfo & CTF_BOOL)) {
/* Assume not equal to zero. Fixup and emit pending guard later. */
lj_ir_set(J, IRTGI(IR_NE), tr, lj_ir_kint(J, 0));
J->postproc = LJ_POST_FIXGUARD;
return TREF_TRUE;
} else {
return tr;
}
} else if (ctype_isptr(sinfo) || ctype_isenum(sinfo)) {
sp = emitir(IRT(IR_XLOAD, t), sp, 0); /* Box pointers and enums. */
} else if (ctype_isrefarray(sinfo) || ctype_isstruct(sinfo)) {
cts->L = J->L;
sid = lj_ctype_intern(cts, CTINFO_REF(sid), CTSIZE_PTR); /* Create ref. */
} else if (ctype_iscomplex(sinfo)) { /* Unbox/box complex. */
ptrdiff_t esz = (ptrdiff_t)(s->size >> 1);
TRef ptr, tr1, tr2, dp;
dp = emitir(IRTG(IR_CNEW, IRT_CDATA), lj_ir_kint(J, sid), TREF_NIL);
tr1 = emitir(IRT(IR_XLOAD, t), sp, 0);
ptr = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, esz));
tr2 = emitir(IRT(IR_XLOAD, t), ptr, 0);
ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, sizeof(GCcdata)));
emitir(IRT(IR_XSTORE, t), ptr, tr1);
ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, sizeof(GCcdata)+esz));
emitir(IRT(IR_XSTORE, t), ptr, tr2);
return dp;
} else {
/* NYI: copyval of vectors. */
err_nyi:
lj_trace_err(J, LJ_TRERR_NYICONV);
}
/* Box pointer, ref, enum or 64 bit integer. */
return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, sid), sp);
}
/* -- Convert TValue to C type (store) ------------------------------------ */
static TRef crec_ct_tv(jit_State *J, CType *d, TRef dp, TRef sp, cTValue *sval)
{
CTState *cts = ctype_ctsG(J2G(J));
CTypeID sid = CTID_P_VOID;
void *svisnz = 0;
CType *s;
if (LJ_LIKELY(tref_isinteger(sp))) {
sid = CTID_INT32;
svisnz = (void *)(intptr_t)(tvisint(sval)?(intV(sval)!=0):!tviszero(sval));
} else if (tref_isnum(sp)) {
sid = CTID_DOUBLE;
svisnz = (void *)(intptr_t)(tvisint(sval)?(intV(sval)!=0):!tviszero(sval));
} else if (tref_isbool(sp)) {
sp = lj_ir_kint(J, tref_istrue(sp) ? 1 : 0);
sid = CTID_BOOL;
} else if (tref_isnil(sp)) {
sp = lj_ir_kptr(J, NULL);
} else if (tref_isudata(sp)) {
GCudata *ud = udataV(sval);
if (ud->udtype == UDTYPE_IO_FILE) {
TRef tr = emitir(IRT(IR_FLOAD, IRT_U8), sp, IRFL_UDATA_UDTYPE);
emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, UDTYPE_IO_FILE));
sp = emitir(IRT(IR_FLOAD, IRT_PTR), sp, IRFL_UDATA_FILE);
} else {
sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCudata)));
}
} else if (tref_isstr(sp)) {
if (ctype_isenum(d->info)) { /* Match string against enum constant. */
GCstr *str = strV(sval);
CTSize ofs;
CType *cct = lj_ctype_getfield(cts, d, str, &ofs);
/* Specialize to the name of the enum constant. */
emitir(IRTG(IR_EQ, IRT_STR), sp, lj_ir_kstr(J, str));
if (cct && ctype_isconstval(cct->info)) {
lua_assert(ctype_child(cts, cct)->size == 4);
svisnz = (void *)(intptr_t)(ofs != 0);
sp = lj_ir_kint(J, (int32_t)ofs);
sid = ctype_cid(cct->info);
} /* else: interpreter will throw. */
} else if (ctype_isrefarray(d->info)) { /* Copy string to array. */
lj_trace_err(J, LJ_TRERR_BADTYPE); /* NYI */
} else { /* Otherwise pass the string data as a const char[]. */
/* Don't use STRREF. It folds with SNEW, which loses the trailing NUL. */
sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCstr)));
sid = CTID_A_CCHAR;
}
} else { /* NYI: tref_istab(sp), tref_islightud(sp). */
IRType t;
sid = argv2cdata(J, sp, sval)->ctypeid;
s = ctype_raw(cts, sid);
svisnz = cdataptr(cdataV(sval));
t = crec_ct2irt(cts, s);
if (ctype_isptr(s->info)) {
sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_PTR);
if (ctype_isref(s->info)) {
svisnz = *(void **)svisnz;
s = ctype_rawchild(cts, s);
if (ctype_isenum(s->info)) s = ctype_child(cts, s);
t = crec_ct2irt(cts, s);
} else {
goto doconv;
}
} else if (t == IRT_I64 || t == IRT_U64) {
sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_INT64);
lj_needsplit(J);
goto doconv;
} else if (t == IRT_INT || t == IRT_U32) {
if (ctype_isenum(s->info)) s = ctype_child(cts, s);
sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_INT);
goto doconv;
} else {
sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCcdata)));
}
if (ctype_isnum(s->info) && t != IRT_CDATA)
sp = emitir(IRT(IR_XLOAD, t), sp, 0); /* Load number value. */
goto doconv;
}
s = ctype_get(cts, sid);
doconv:
if (ctype_isenum(d->info)) d = ctype_child(cts, d);
return crec_ct_ct(J, d, s, dp, sp, svisnz);
}
/* -- C data metamethods -------------------------------------------------- */
/* This would be rather difficult in FOLD, so do it here:
** (base+k)+(idx*sz)+ofs ==> (base+idx*sz)+(ofs+k)
** (base+(idx+k)*sz)+ofs ==> (base+idx*sz)+(ofs+k*sz)
*/
static TRef crec_reassoc_ofs(jit_State *J, TRef tr, ptrdiff_t *ofsp, MSize sz)
{
IRIns *ir = IR(tref_ref(tr));
if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && irref_isk(ir->op2) &&
(ir->o == IR_ADD || ir->o == IR_ADDOV || ir->o == IR_SUBOV)) {
IRIns *irk = IR(ir->op2);
ptrdiff_t k;
if (LJ_64 && irk->o == IR_KINT64)
k = (ptrdiff_t)ir_kint64(irk)->u64 * sz;
else
k = (ptrdiff_t)irk->i * sz;
if (ir->o == IR_SUBOV) *ofsp -= k; else *ofsp += k;
tr = ir->op1; /* Not a TRef, but the caller doesn't care. */
}
return tr;
}
/* Record ctype __index/__newindex metamethods. */
static void crec_index_meta(jit_State *J, CTState *cts, CType *ct,
RecordFFData *rd)
{
CTypeID id = ctype_typeid(cts, ct);
cTValue *tv = lj_ctype_meta(cts, id, rd->data ? MM_newindex : MM_index);
if (!tv)
lj_trace_err(J, LJ_TRERR_BADTYPE);
if (tvisfunc(tv)) {
J->base[-1] = lj_ir_kfunc(J, funcV(tv)) | TREF_FRAME;
rd->nres = -1; /* Pending tailcall. */
} else if (rd->data == 0 && tvistab(tv) && tref_isstr(J->base[1])) {
/* Specialize to result of __index lookup. */
cTValue *o = lj_tab_get(J->L, tabV(tv), &rd->argv[1]);
J->base[0] = lj_record_constify(J, o);
if (!J->base[0])
lj_trace_err(J, LJ_TRERR_BADTYPE);
/* Always specialize to the key. */
emitir(IRTG(IR_EQ, IRT_STR), J->base[1], lj_ir_kstr(J, strV(&rd->argv[1])));
} else {
/* NYI: resolving of non-function metamethods. */
/* NYI: non-string keys for __index table. */
/* NYI: stores to __newindex table. */
lj_trace_err(J, LJ_TRERR_BADTYPE);
}
}
void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd)
{
TRef idx, ptr = J->base[0];
ptrdiff_t ofs = sizeof(GCcdata);
GCcdata *cd = argv2cdata(J, ptr, &rd->argv[0]);
CTState *cts = ctype_ctsG(J2G(J));
CType *ct = ctype_raw(cts, cd->ctypeid);
CTypeID sid = 0;
/* Resolve pointer or reference for cdata object. */
if (ctype_isptr(ct->info)) {
IRType t = (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32;
if (ctype_isref(ct->info)) ct = ctype_rawchild(cts, ct);
ptr = emitir(IRT(IR_FLOAD, t), ptr, IRFL_CDATA_PTR);
ofs = 0;
ptr = crec_reassoc_ofs(J, ptr, &ofs, 1);
}
again:
idx = J->base[1];
if (tref_isnumber(idx)) {
idx = lj_opt_narrow_cindex(J, idx);
if (ctype_ispointer(ct->info)) {
CTSize sz;
integer_key:
if ((ct->info & CTF_COMPLEX))
idx = emitir(IRT(IR_BAND, IRT_INTP), idx, lj_ir_kintp(J, 1));
sz = lj_ctype_size(cts, (sid = ctype_cid(ct->info)));
idx = crec_reassoc_ofs(J, idx, &ofs, sz);
#if LJ_TARGET_ARM || LJ_TARGET_PPC
/* Hoist base add to allow fusion of index/shift into operands. */
if (LJ_LIKELY(J->flags & JIT_F_OPT_LOOP) && ofs
#if LJ_TARGET_ARM
&& (sz == 1 || sz == 4)
#endif
) {
ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs));
ofs = 0;
}
#endif
idx = emitir(IRT(IR_MUL, IRT_INTP), idx, lj_ir_kintp(J, sz));
ptr = emitir(IRT(IR_ADD, IRT_PTR), idx, ptr);
}
} else if (tref_iscdata(idx)) {
GCcdata *cdk = cdataV(&rd->argv[1]);
CType *ctk = ctype_raw(cts, cdk->ctypeid);
IRType t = crec_ct2irt(cts, ctk);
if (ctype_ispointer(ct->info) && t >= IRT_I8 && t <= IRT_U64) {
if (ctk->size == 8) {
idx = emitir(IRT(IR_FLOAD, t), idx, IRFL_CDATA_INT64);
} else if (ctk->size == 4) {
idx = emitir(IRT(IR_FLOAD, t), idx, IRFL_CDATA_INT);
} else {
idx = emitir(IRT(IR_ADD, IRT_PTR), idx,
lj_ir_kintp(J, sizeof(GCcdata)));
idx = emitir(IRT(IR_XLOAD, t), idx, 0);
}
if (LJ_64 && ctk->size < sizeof(intptr_t) && !(ctk->info & CTF_UNSIGNED))
idx = emitconv(idx, IRT_INTP, IRT_INT, IRCONV_SEXT);
if (!LJ_64 && ctk->size > sizeof(intptr_t)) {
idx = emitconv(idx, IRT_INTP, t, 0);
lj_needsplit(J);
}
goto integer_key;
}
} else if (tref_isstr(idx)) {
GCstr *name = strV(&rd->argv[1]);
if (cd->ctypeid == CTID_CTYPEID)
ct = ctype_raw(cts, crec_constructor(J, cd, ptr));
if (ctype_isstruct(ct->info)) {
CTSize fofs;
CType *fct;
fct = lj_ctype_getfield(cts, ct, name, &fofs);
if (fct) {
/* Always specialize to the field name. */
emitir(IRTG(IR_EQ, IRT_STR), idx, lj_ir_kstr(J, name));
if (ctype_isconstval(fct->info)) {
if (fct->size >= 0x80000000u &&
(ctype_child(cts, fct)->info & CTF_UNSIGNED)) {
J->base[0] = lj_ir_knum(J, (lua_Number)(uint32_t)fct->size);
return;
}
J->base[0] = lj_ir_kint(J, (int32_t)fct->size);
return; /* Interpreter will throw for newindex. */
} else if (ctype_isbitfield(fct->info)) {
lj_trace_err(J, LJ_TRERR_NYICONV);
} else {
lua_assert(ctype_isfield(fct->info));
sid = ctype_cid(fct->info);
}
ofs += (ptrdiff_t)fofs;
}
} else if (ctype_iscomplex(ct->info)) {
if (name->len == 2 &&
((strdata(name)[0] == 'r' && strdata(name)[1] == 'e') ||
(strdata(name)[0] == 'i' && strdata(name)[1] == 'm'))) {
/* Always specialize to the field name. */
emitir(IRTG(IR_EQ, IRT_STR), idx, lj_ir_kstr(J, name));
if (strdata(name)[0] == 'i') ofs += (ct->size >> 1);
sid = ctype_cid(ct->info);
}
}
}
if (!sid) {
if (ctype_isptr(ct->info)) { /* Automatically perform '->'. */
CType *cct = ctype_rawchild(cts, ct);
if (ctype_isstruct(cct->info)) {
ct = cct;
if (tref_isstr(idx)) goto again;
}
}
crec_index_meta(J, cts, ct, rd);
return;
}
if (ofs)
ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs));
/* Resolve reference for field. */
ct = ctype_get(cts, sid);
if (ctype_isref(ct->info))
ptr = emitir(IRT(IR_XLOAD, IRT_PTR), ptr, 0);
while (ctype_isattrib(ct->info))
ct = ctype_child(cts, ct); /* Skip attributes. */
if (rd->data == 0) { /* __index metamethod. */
J->base[0] = crec_tv_ct(J, ct, sid, ptr);
} else { /* __newindex metamethod. */
rd->nres = 0;
J->needsnap = 1;
crec_ct_tv(J, ct, ptr, J->base[2], &rd->argv[2]);
}
}
/* Record setting a finalizer. */
static void crec_finalizer(jit_State *J, TRef trcd, cTValue *fin)
{
TRef trlo = lj_ir_call(J, IRCALL_lj_cdata_setfin, trcd);
TRef trhi = emitir(IRT(IR_ADD, IRT_P32), trlo, lj_ir_kint(J, 4));
if (LJ_BE) { TRef tmp = trlo; trlo = trhi; trhi = tmp; }
if (tvisfunc(fin)) {
emitir(IRT(IR_XSTORE, IRT_P32), trlo, lj_ir_kfunc(J, funcV(fin)));
emitir(IRTI(IR_XSTORE), trhi, lj_ir_kint(J, LJ_TFUNC));
} else if (tviscdata(fin)) {
emitir(IRT(IR_XSTORE, IRT_P32), trlo,
lj_ir_kgc(J, obj2gco(cdataV(fin)), IRT_CDATA));
emitir(IRTI(IR_XSTORE), trhi, lj_ir_kint(J, LJ_TCDATA));
} else {
lj_trace_err(J, LJ_TRERR_BADTYPE);
}
J->needsnap = 1;
}
/* Record cdata allocation. */
static void crec_alloc(jit_State *J, RecordFFData *rd, CTypeID id)
{
CTState *cts = ctype_ctsG(J2G(J));
CTSize sz;
CTInfo info = lj_ctype_info(cts, id, &sz);
CType *d = ctype_raw(cts, id);
TRef trid;
if (!sz || sz > 128 || (info & CTF_VLA) || ctype_align(info) > CT_MEMALIGN)
lj_trace_err(J, LJ_TRERR_NYICONV); /* NYI: large/special allocations. */
trid = lj_ir_kint(J, id);
/* Use special instruction to box pointer or 32/64 bit integer. */
if (ctype_isptr(info) || (ctype_isinteger(info) && (sz == 4 || sz == 8))) {
TRef sp = J->base[1] ? crec_ct_tv(J, d, 0, J->base[1], &rd->argv[1]) :
ctype_isptr(info) ? lj_ir_kptr(J, NULL) :
sz == 4 ? lj_ir_kint(J, 0) :
(lj_needsplit(J), lj_ir_kint64(J, 0));
J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), trid, sp);
} else {
TRef trcd = emitir(IRTG(IR_CNEW, IRT_CDATA), trid, TREF_NIL);
cTValue *fin;
J->base[0] = trcd;
if (J->base[1] && !J->base[2] &&
!lj_cconv_multi_init(cts, d, &rd->argv[1])) {
goto single_init;
} else if (ctype_isarray(d->info)) {
CType *dc = ctype_rawchild(cts, d); /* Array element type. */
CTSize ofs, esize = dc->size;
TRef sp = 0;
TValue tv;
TValue *sval = &tv;
MSize i;
tv.u64 = 0;
if (!(ctype_isnum(dc->info) || ctype_isptr(dc->info)))
lj_trace_err(J, LJ_TRERR_NYICONV); /* NYI: init array of aggregates. */
for (i = 1, ofs = 0; ofs < sz; ofs += esize) {
TRef dp = emitir(IRT(IR_ADD, IRT_PTR), trcd,
lj_ir_kintp(J, ofs + sizeof(GCcdata)));
if (J->base[i]) {
sp = J->base[i];
sval = &rd->argv[i];
i++;
} else if (i != 2) {
sp = ctype_isnum(dc->info) ? lj_ir_kint(J, 0) : TREF_NIL;
}
crec_ct_tv(J, dc, dp, sp, sval);
}
} else if (ctype_isstruct(d->info)) {
CTypeID fid = d->sib;
MSize i = 1;
while (fid) {
CType *df = ctype_get(cts, fid);
fid = df->sib;
if (ctype_isfield(df->info)) {
CType *dc;
TRef sp, dp;
TValue tv;
TValue *sval = &tv;
setintV(&tv, 0);
if (!gcref(df->name)) continue; /* Ignore unnamed fields. */
dc = ctype_rawchild(cts, df); /* Field type. */
if (!(ctype_isnum(dc->info) || ctype_isptr(dc->info) ||
ctype_isenum(dc->info)))
lj_trace_err(J, LJ_TRERR_NYICONV); /* NYI: init aggregates. */
if (J->base[i]) {
sp = J->base[i];
sval = &rd->argv[i];
i++;
} else {
sp = ctype_isptr(dc->info) ? TREF_NIL : lj_ir_kint(J, 0);
}
dp = emitir(IRT(IR_ADD, IRT_PTR), trcd,
lj_ir_kintp(J, df->size + sizeof(GCcdata)));
crec_ct_tv(J, dc, dp, sp, sval);
} else if (!ctype_isconstval(df->info)) {
/* NYI: init bitfields and sub-structures. */
lj_trace_err(J, LJ_TRERR_NYICONV);
}
}
} else {
TRef dp;
single_init:
dp = emitir(IRT(IR_ADD, IRT_PTR), trcd, lj_ir_kintp(J, sizeof(GCcdata)));
if (J->base[1]) {
crec_ct_tv(J, d, dp, J->base[1], &rd->argv[1]);
} else {
TValue tv;
tv.u64 = 0;
crec_ct_tv(J, d, dp, lj_ir_kint(J, 0), &tv);
}
}
/* Handle __gc metamethod. */
fin = lj_ctype_meta(cts, id, MM_gc);
if (fin)
crec_finalizer(J, trcd, fin);
}
}
/* Record argument conversions. */
static TRef crec_call_args(jit_State *J, RecordFFData *rd,
CTState *cts, CType *ct)
{
TRef args[CCI_NARGS_MAX];
CTypeID fid;
MSize i, n;
TRef tr, *base;
cTValue *o;
#if LJ_TARGET_X86
#if LJ_ABI_WIN
TRef *arg0 = NULL, *arg1 = NULL;
#endif
int ngpr = 0;
if (ctype_cconv(ct->info) == CTCC_THISCALL)
ngpr = 1;
else if (ctype_cconv(ct->info) == CTCC_FASTCALL)
ngpr = 2;
#endif