• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tarantool / luajit / 13944882635

19 Mar 2025 10:52AM UTC coverage: 93.02% (+0.03%) from 92.99%
13944882635

push

github

Buristan
codehealth: fix warnings for the codespell 2.3.0

This patch fixes the typo 'snapshoting' -> 'snapshotting' in the
<lj-736-BC_UCLO-triggers-infinite-loop.test.lua>. The <lint.yml>
workflow is updated to the corresponding version of the codespell.

Also, it adds `tru` and `fal` to the ignore list since they are used in
the LuaJIT JIT dump output.

Reviewed-by: Sergey Bronnikov <sergeyb@tarantool.org>
Signed-off-by: Sergey Kaplun <skaplun@tarantool.org>
(cherry picked from commit 921cc54d3)

5701 of 6038 branches covered (94.42%)

Branch coverage included in aggregate %.

21726 of 23447 relevant lines covered (92.66%)

2961139.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

87.73
/src/lj_crecord.c
1
/*
2
** Trace recorder for C data operations.
3
** Copyright (C) 2005-2017 Mike Pall. See Copyright Notice in luajit.h
4
*/
5

6
#define lj_ffrecord_c
7
#define LUA_CORE
8

9
#include "lj_obj.h"
10

11
#if LJ_HASJIT && LJ_HASFFI
12

13
#include "lj_err.h"
14
#include "lj_tab.h"
15
#include "lj_frame.h"
16
#include "lj_ctype.h"
17
#include "lj_cdata.h"
18
#include "lj_cparse.h"
19
#include "lj_cconv.h"
20
#include "lj_carith.h"
21
#include "lj_clib.h"
22
#include "lj_ccall.h"
23
#include "lj_ff.h"
24
#include "lj_ir.h"
25
#include "lj_jit.h"
26
#include "lj_ircall.h"
27
#include "lj_iropt.h"
28
#include "lj_trace.h"
29
#include "lj_record.h"
30
#include "lj_ffrecord.h"
31
#include "lj_snap.h"
32
#include "lj_crecord.h"
33
#include "lj_dispatch.h"
34
#include "lj_strfmt.h"
35
#include "lj_strscan.h"
36

37
/* Some local macros to save typing. Undef'd at the end. */
38
#define IR(ref)                        (&J->cur.ir[(ref)])
39

40
/* Pass IR on to next optimization in chain (FOLD). */
41
#define emitir(ot, a, b)        (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))
42

43
#define emitconv(a, dt, st, flags) \
44
  emitir(IRT(IR_CONV, (dt)), (a), (st)|((dt) << 5)|(flags))
45

46
/* -- C type checks ------------------------------------------------------- */
47

48
static GCcdata *argv2cdata(jit_State *J, TRef tr, cTValue *o)
49
{
50
  GCcdata *cd;
51
  TRef trtypeid;
52
  if (!tref_iscdata(tr))
53
    lj_trace_err(J, LJ_TRERR_BADTYPE);
54
  cd = cdataV(o);
55
  /* Specialize to the CTypeID. */
56
  trtypeid = emitir(IRT(IR_FLOAD, IRT_U16), tr, IRFL_CDATA_CTYPEID);
57
  emitir(IRTG(IR_EQ, IRT_INT), trtypeid, lj_ir_kint(J, (int32_t)cd->ctypeid));
58
  return cd;
59
}
60

61
/* Specialize to the CTypeID held by a cdata constructor. */
62
static CTypeID crec_constructor(jit_State *J, GCcdata *cd, TRef tr)
106✔
63
{
64
  CTypeID id;
106✔
65
  lj_assertJ(tref_iscdata(tr) && cd->ctypeid == CTID_CTYPEID,
106✔
66
             "expected CTypeID cdata");
67
  id = *(CTypeID *)cdataptr(cd);
106✔
68
  tr = emitir(IRT(IR_FLOAD, IRT_INT), tr, IRFL_CDATA_INT);
106✔
69
  emitir(IRTG(IR_EQ, IRT_INT), tr, lj_ir_kint(J, (int32_t)id));
106✔
70
  return id;
106✔
71
}
72

73
static CTypeID argv2ctype(jit_State *J, TRef tr, cTValue *o)
22,525✔
74
{
75
  if (tref_isstr(tr)) {
22,525✔
76
    GCstr *s = strV(o);
22,468✔
77
    CPState cp;
22,468✔
78
    CTypeID oldtop;
22,468✔
79
    /* Specialize to the string containing the C type declaration. */
80
    emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, s));
22,468✔
81
    cp.L = J->L;
22,468✔
82
    cp.cts = ctype_cts(J->L);
22,468✔
83
    oldtop = cp.cts->top;
22,468✔
84
    cp.srcname = strdata(s);
22,468✔
85
    cp.p = strdata(s);
22,468✔
86
    cp.param = NULL;
22,468✔
87
    cp.mode = CPARSE_MODE_ABSTRACT|CPARSE_MODE_NOIMPLICIT;
22,468✔
88
    if (lj_cparse(&cp) || cp.cts->top > oldtop)  /* Avoid new struct defs. */
22,468✔
89
      lj_trace_err(J, LJ_TRERR_BADTYPE);
×
90
    return cp.val.id;
22,468✔
91
  } else {
92
    GCcdata *cd = argv2cdata(J, tr, o);
57✔
93
    return cd->ctypeid == CTID_CTYPEID ? crec_constructor(J, cd, tr) :
57✔
94
                                        cd->ctypeid;
95
  }
96
}
97

98
/* Convert CType to IRType (if possible). */
99
static IRType crec_ct2irt(CTState *cts, CType *ct)
100
{
101
  if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
102
  if (LJ_LIKELY(ctype_isnum(ct->info))) {
103
    if ((ct->info & CTF_FP)) {
104
      if (ct->size == sizeof(double))
105
        return IRT_NUM;
106
      else if (ct->size == sizeof(float))
107
        return IRT_FLOAT;
108
    } else {
109
      uint32_t b = lj_fls(ct->size);
110
      if (b <= 3)
111
        return IRT_I8 + 2*b + ((ct->info & CTF_UNSIGNED) ? 1 : 0);
112
    }
113
  } else if (ctype_isptr(ct->info)) {
114
    return (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32;
115
  } else if (ctype_iscomplex(ct->info)) {
116
    if (ct->size == 2*sizeof(double))
117
      return IRT_NUM;
118
    else if (ct->size == 2*sizeof(float))
119
      return IRT_FLOAT;
120
  }
121
  return IRT_CDATA;
122
}
123

124
/* -- Optimized memory fill and copy -------------------------------------- */
125

126
/* Maximum length and unroll of inlined copy/fill. */
127
#define CREC_COPY_MAXUNROLL                16
128
#define CREC_COPY_MAXLEN                128
129

130
#define CREC_FILL_MAXUNROLL                16
131

132
/* Number of windowed registers used for optimized memory copy. */
133
#if LJ_TARGET_X86
134
#define CREC_COPY_REGWIN                2
135
#elif LJ_TARGET_PPC || LJ_TARGET_MIPS
136
#define CREC_COPY_REGWIN                8
137
#else
138
#define CREC_COPY_REGWIN                4
139
#endif
140

141
/* List of memory offsets for copy/fill. */
142
typedef struct CRecMemList {
143
  CTSize ofs;                /* Offset in bytes. */
144
  IRType tp;                /* Type of load/store. */
145
  TRef trofs;                /* TRef of interned offset. */
146
  TRef trval;                /* TRef of load value. */
147
} CRecMemList;
148

149
/* Generate copy list for element-wise struct copy. */
150
static MSize crec_copy_struct(CRecMemList *ml, CTState *cts, CType *ct)
151
{
152
  CTypeID fid = ct->sib;
153
  MSize mlp = 0;
154
  while (fid) {
155
    CType *df = ctype_get(cts, fid);
156
    fid = df->sib;
157
    if (ctype_isfield(df->info)) {
158
      CType *cct;
159
      IRType tp;
160
      if (!gcref(df->name)) continue;  /* Ignore unnamed fields. */
161
      cct = ctype_rawchild(cts, df);  /* Field type. */
162
      tp = crec_ct2irt(cts, cct);
163
      if (tp == IRT_CDATA) return 0;  /* NYI: aggregates. */
164
      if (mlp >= CREC_COPY_MAXUNROLL) return 0;
165
      ml[mlp].ofs = df->size;
166
      ml[mlp].tp = tp;
167
      mlp++;
168
      if (ctype_iscomplex(cct->info)) {
169
        if (mlp >= CREC_COPY_MAXUNROLL) return 0;
170
        ml[mlp].ofs = df->size + (cct->size >> 1);
171
        ml[mlp].tp = tp;
172
        mlp++;
173
      }
174
    } else if (!ctype_isconstval(df->info)) {
175
      /* NYI: bitfields and sub-structures. */
176
      return 0;
177
    }
178
  }
179
  return mlp;
180
}
181

182
/* Generate unrolled copy list, from highest to lowest step size/alignment. */
183
static MSize crec_copy_unroll(CRecMemList *ml, CTSize len, CTSize step,
7✔
184
                              IRType tp)
185
{
186
  CTSize ofs = 0;
7✔
187
  MSize mlp = 0;
7✔
188
  if (tp == IRT_CDATA) tp = IRT_U8 + 2*lj_fls(step);
5✔
189
  do {
190
    while (ofs + step <= len) {
38✔
191
      if (mlp >= CREC_COPY_MAXUNROLL) return 0;
25✔
192
      ml[mlp].ofs = ofs;
25✔
193
      ml[mlp].tp = tp;
25✔
194
      mlp++;
25✔
195
      ofs += step;
25✔
196
    }
197
    step >>= 1;
13✔
198
    tp -= 2;
13✔
199
  } while (ofs < len);
13✔
200
  return mlp;
201
}
202

203
/*
204
** Emit copy list with windowed loads/stores.
205
** LJ_TARGET_UNALIGNED: may emit unaligned loads/stores (not marked as such).
206
*/
207
static void crec_copy_emit(jit_State *J, CRecMemList *ml, MSize mlp,
9✔
208
                           TRef trdst, TRef trsrc)
209
{
210
  MSize i, j, rwin = 0;
9✔
211
  for (i = 0, j = 0; i < mlp; ) {
40✔
212
    TRef trofs = lj_ir_kintp(J, ml[i].ofs);
31✔
213
    TRef trsptr = emitir(IRT(IR_ADD, IRT_PTR), trsrc, trofs);
31✔
214
    ml[i].trval = emitir(IRT(IR_XLOAD, ml[i].tp), trsptr, 0);
31✔
215
    ml[i].trofs = trofs;
31✔
216
    i++;
31✔
217
    rwin += (LJ_SOFTFP32 && ml[i].tp == IRT_NUM) ? 2 : 1;
31✔
218
    if (rwin >= CREC_COPY_REGWIN || i >= mlp) {  /* Flush buffered stores. */
31✔
219
      rwin = 0;
44✔
220
      for ( ; j < i; j++) {
44✔
221
        TRef trdptr = emitir(IRT(IR_ADD, IRT_PTR), trdst, ml[j].trofs);
31✔
222
        emitir(IRT(IR_XSTORE, ml[j].tp), trdptr, ml[j].trval);
31✔
223
      }
224
    }
225
  }
226
}
9✔
227

228
/* Optimized memory copy. */
229
static void crec_copy(jit_State *J, TRef trdst, TRef trsrc, TRef trlen,
9✔
230
                      CType *ct)
231
{
232
  if (tref_isk(trlen)) {  /* Length must be constant. */
9✔
233
    CRecMemList ml[CREC_COPY_MAXUNROLL];
9✔
234
    MSize mlp = 0;
9✔
235
    CTSize step = 1, len = (CTSize)IR(tref_ref(trlen))->i;
9✔
236
    IRType tp = IRT_CDATA;
9✔
237
    int needxbar = 0;
9✔
238
    if (len == 0) return;  /* Shortcut. */
18✔
239
    if (len > CREC_COPY_MAXLEN) goto fallback;
9✔
240
    if (ct) {
9✔
241
      CTState *cts = ctype_ctsG(J2G(J));
4✔
242
      lj_assertJ(ctype_isarray(ct->info) || ctype_isstruct(ct->info),
4✔
243
                 "copy of non-aggregate");
244
      if (ctype_isarray(ct->info)) {
4✔
245
        CType *cct = ctype_rawchild(cts, ct);
2✔
246
        tp = crec_ct2irt(cts, cct);
2✔
247
        if (tp == IRT_CDATA) goto rawcopy;
2✔
248
        step = lj_ir_type_size[tp];
2✔
249
        lj_assertJ((len & (step-1)) == 0, "copy of fractional size");
2✔
250
      } else if ((ct->info & CTF_UNION)) {
2✔
251
        step = (1u << ctype_align(ct->info));
×
252
        goto rawcopy;
×
253
      } else {
254
        mlp = crec_copy_struct(ml, cts, ct);
2✔
255
        goto emitcopy;
2✔
256
      }
257
    } else {
258
    rawcopy:
5✔
259
      needxbar = 1;
260
      if (LJ_TARGET_UNALIGNED || step >= CTSIZE_PTR)
261
        step = CTSIZE_PTR;
262
    }
263
    mlp = crec_copy_unroll(ml, len, step, tp);
7✔
264
  emitcopy:
9✔
265
    if (mlp) {
9✔
266
      crec_copy_emit(J, ml, mlp, trdst, trsrc);
9✔
267
      if (needxbar)
9✔
268
        emitir(IRT(IR_XBAR, IRT_NIL), 0, 0);
5✔
269
      return;
9✔
270
    }
271
  }
272
fallback:
×
273
  /* Call memcpy. Always needs a barrier to disable alias analysis. */
274
  lj_ir_call(J, IRCALL_memcpy, trdst, trsrc, trlen);
×
275
  emitir(IRT(IR_XBAR, IRT_NIL), 0, 0);
×
276
}
277

278
/* Generate unrolled fill list, from highest to lowest step size/alignment. */
279
static MSize crec_fill_unroll(CRecMemList *ml, CTSize len, CTSize step)
280
{
281
  CTSize ofs = 0;
282
  MSize mlp = 0;
283
  IRType tp = IRT_U8 + 2*lj_fls(step);
284
  do {
285
    while (ofs + step <= len) {
286
      if (mlp >= CREC_COPY_MAXUNROLL) return 0;
287
      ml[mlp].ofs = ofs;
288
      ml[mlp].tp = tp;
289
      mlp++;
290
      ofs += step;
291
    }
292
    step >>= 1;
293
    tp -= 2;
294
  } while (ofs < len);
295
  return mlp;
296
}
297

298
/*
299
** Emit stores for fill list.
300
** LJ_TARGET_UNALIGNED: may emit unaligned stores (not marked as such).
301
*/
302
static void crec_fill_emit(jit_State *J, CRecMemList *ml, MSize mlp,
4✔
303
                           TRef trdst, TRef trfill)
304
{
305
  MSize i;
4✔
306
  for (i = 0; i < mlp; i++) {
16✔
307
    TRef trofs = lj_ir_kintp(J, ml[i].ofs);
12✔
308
    TRef trdptr = emitir(IRT(IR_ADD, IRT_PTR), trdst, trofs);
12✔
309
    emitir(IRT(IR_XSTORE, ml[i].tp), trdptr, trfill);
12✔
310
  }
311
}
4✔
312

313
/* Optimized memory fill. */
314
static void crec_fill(jit_State *J, TRef trdst, TRef trlen, TRef trfill,
315
                      CTSize step)
316
{
317
  if (tref_isk(trlen)) {  /* Length must be constant. */
318
    CRecMemList ml[CREC_FILL_MAXUNROLL];
319
    MSize mlp;
320
    CTSize len = (CTSize)IR(tref_ref(trlen))->i;
321
    if (len == 0) return;  /* Shortcut. */
322
    if (LJ_TARGET_UNALIGNED || step >= CTSIZE_PTR)
323
      step = CTSIZE_PTR;
324
    if (step * CREC_FILL_MAXUNROLL < len) goto fallback;
325
    mlp = crec_fill_unroll(ml, len, step);
326
    if (!mlp) goto fallback;
327
    if (tref_isk(trfill) || ml[0].tp != IRT_U8)
328
      trfill = emitconv(trfill, IRT_INT, IRT_U8, 0);
329
    if (ml[0].tp != IRT_U8) {  /* Scatter U8 to U16/U32/U64. */
330
      if (CTSIZE_PTR == 8 && ml[0].tp == IRT_U64) {
331
        if (tref_isk(trfill))  /* Pointless on x64 with zero-extended regs. */
332
          trfill = emitconv(trfill, IRT_U64, IRT_U32, 0);
333
        trfill = emitir(IRT(IR_MUL, IRT_U64), trfill,
334
                        lj_ir_kint64(J, U64x(01010101,01010101)));
335
      } else {
336
        trfill = emitir(IRTI(IR_MUL), trfill,
337
                   lj_ir_kint(J, ml[0].tp == IRT_U16 ? 0x0101 : 0x01010101));
338
      }
339
    }
340
    crec_fill_emit(J, ml, mlp, trdst, trfill);
341
  } else {
342
fallback:
343
    /* Call memset. Always needs a barrier to disable alias analysis. */
344
    lj_ir_call(J, IRCALL_memset, trdst, trfill, trlen);  /* Note: arg order! */
345
  }
346
  emitir(IRT(IR_XBAR, IRT_NIL), 0, 0);
347
}
348

349
/* -- Convert C type to C type -------------------------------------------- */
350

351
/*
352
** This code mirrors the code in lj_cconv.c. It performs the same steps
353
** for the trace recorder that lj_cconv.c does for the interpreter.
354
**
355
** One major difference is that we can get away with much fewer checks
356
** here. E.g. checks for casts, constness or correct types can often be
357
** omitted, even if they might fail. The interpreter subsequently throws
358
** an error, which aborts the trace.
359
**
360
** All operations are specialized to their C types, so the on-trace
361
** outcome must be the same as the outcome in the interpreter. If the
362
** interpreter doesn't throw an error, then the trace is correct, too.
363
** Care must be taken not to generate invalid (temporary) IR or to
364
** trigger asserts.
365
*/
366

367
/* Determine whether a passed number or cdata number is non-zero. */
368
static int crec_isnonzero(CType *s, void *p)
369
{
370
  if (p == (void *)0)
371
    return 0;
372
  if (p == (void *)1)
373
    return 1;
374
  if ((s->info & CTF_FP)) {
375
    if (s->size == sizeof(float))
376
      return (*(float *)p != 0);
377
    else
378
      return (*(double *)p != 0);
379
  } else {
380
    if (s->size == 1)
381
      return (*(uint8_t *)p != 0);
382
    else if (s->size == 2)
383
      return (*(uint16_t *)p != 0);
384
    else if (s->size == 4)
385
      return (*(uint32_t *)p != 0);
386
    else
387
      return (*(uint64_t *)p != 0);
388
  }
389
}
390

391
static TRef crec_ct_ct(jit_State *J, CType *d, CType *s, TRef dp, TRef sp,
36,388✔
392
                       void *svisnz)
393
{
394
  IRType dt = crec_ct2irt(ctype_ctsG(J2G(J)), d);
36,388✔
395
  IRType st = crec_ct2irt(ctype_ctsG(J2G(J)), s);
36,388✔
396
  CTSize dsize = d->size, ssize = s->size;
36,388✔
397
  CTInfo dinfo = d->info, sinfo = s->info;
36,388✔
398

399
  if (ctype_type(dinfo) > CT_MAYCONVERT || ctype_type(sinfo) > CT_MAYCONVERT)
36,388✔
400
    goto err_conv;
×
401

402
  /*
403
  ** Note: Unlike lj_cconv_ct_ct(), sp holds the _value_ of pointers and
404
  ** numbers up to 8 bytes. Otherwise sp holds a pointer.
405
  */
406

407
  switch (cconv_idx2(dinfo, sinfo)) {
36,388✔
408
  /* Destination is a bool. */
409
  case CCX(B, B):
×
410
    goto xstore;  /* Source operand is already normalized. */
×
411
  case CCX(B, I):
4✔
412
  case CCX(B, F):
413
    if (st != IRT_CDATA) {
4✔
414
      /* Specialize to the result of a comparison against 0. */
415
      TRef zero = (st == IRT_NUM  || st == IRT_FLOAT) ? lj_ir_knum(J, 0) :
4✔
416
                  (st == IRT_I64 || st == IRT_U64) ? lj_ir_kint64(J, 0) :
3✔
417
                  lj_ir_kint(J, 0);
3✔
418
      int isnz = crec_isnonzero(s, svisnz);
4✔
419
      emitir(IRTG(isnz ? IR_NE : IR_EQ, st), sp, zero);
5✔
420
      sp = lj_ir_kint(J, isnz);
4✔
421
      goto xstore;
4✔
422
    }
423
    goto err_nyi;
×
424

425
  /* Destination is an integer. */
426
  case CCX(I, B):
427
  case CCX(I, I):
428
  conv_I_I:
22,693✔
429
    if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
22,693✔
430
    /* Extend 32 to 64 bit integer. */
431
    if (dsize == 8 && ssize < 8 && !(LJ_64 && (sinfo & CTF_UNSIGNED)))
22,693✔
432
      sp = emitconv(sp, dt, ssize < 4 ? IRT_INT : st,
44,618✔
433
                    (sinfo & CTF_UNSIGNED) ? 0 : IRCONV_SEXT);
434
    else if (dsize < 8 && ssize == 8)  /* Truncate from 64 bit integer. */
384✔
435
      sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, st, 0);
1✔
436
    else if (st == IRT_INT)
383✔
437
      sp = lj_opt_narrow_toint(J, sp);
291✔
438
  xstore:
92✔
439
    if (dt == IRT_I64 || dt == IRT_U64) lj_needsplit(J);
36,379✔
440
    if (dp == 0) return sp;
36,379✔
441
    emitir(IRT(IR_XSTORE, dt), dp, sp);
194✔
442
    break;
194✔
443
  case CCX(I, C):
×
444
    sp = emitir(IRT(IR_XLOAD, st), sp, 0);  /* Load re. */
×
445
    /* fallthrough */
446
  case CCX(I, F):
273✔
447
    if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
273✔
448
    sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, st, IRCONV_ANY);
273✔
449
    goto xstore;
273✔
450
  case CCX(I, P):
19✔
451
  case CCX(I, A):
452
    sinfo = CTINFO(CT_NUM, CTF_UNSIGNED);
19✔
453
    ssize = CTSIZE_PTR;
19✔
454
    st = IRT_UINTP;
19✔
455
    if (((dsize ^ ssize) & 8) == 0) {  /* Must insert no-op type conversion. */
19✔
456
      sp = emitconv(sp, dsize < 4 ? IRT_INT : dt, IRT_PTR, 0);
19✔
457
      goto xstore;
19✔
458
    }
459
    goto conv_I_I;
×
460

461
  /* Destination is a floating-point number. */
462
  case CCX(F, B):
463
  case CCX(F, I):
464
  conv_F_I:
686✔
465
    if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
686✔
466
    sp = emitconv(sp, dt, ssize < 4 ? IRT_INT : st, 0);
1,372✔
467
    goto xstore;
686✔
468
  case CCX(F, C):
1✔
469
    sp = emitir(IRT(IR_XLOAD, st), sp, 0);  /* Load re. */
1✔
470
    /* fallthrough */
471
  case CCX(F, F):
472
  conv_F_F:
26✔
473
    if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
26✔
474
    if (dt != st) sp = emitconv(sp, dt, st, 0);
26✔
475
    goto xstore;
26✔
476

477
  /* Destination is a complex number. */
478
  case CCX(C, I):
2✔
479
  case CCX(C, F):
480
    {  /* Clear im. */
481
      TRef ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, (dsize >> 1)));
2✔
482
      emitir(IRT(IR_XSTORE, dt), ptr, lj_ir_knum(J, 0));
2✔
483
    }
484
    /* Convert to re. */
485
    if ((sinfo & CTF_FP)) goto conv_F_F; else goto conv_F_I;
2✔
486

487
  case CCX(C, C):
5✔
488
    if (dt == IRT_CDATA || st == IRT_CDATA) goto err_nyi;
5✔
489
    {
490
      TRef re, im, ptr;
5✔
491
      re = emitir(IRT(IR_XLOAD, st), sp, 0);
5✔
492
      ptr = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, (ssize >> 1)));
5✔
493
      im = emitir(IRT(IR_XLOAD, st), ptr, 0);
5✔
494
      if (dt != st) {
5✔
495
        re = emitconv(re, dt, st, 0);
2✔
496
        im = emitconv(im, dt, st, 0);
2✔
497
      }
498
      emitir(IRT(IR_XSTORE, dt), dp, re);
5✔
499
      ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, (dsize >> 1)));
5✔
500
      emitir(IRT(IR_XSTORE, dt), ptr, im);
5✔
501
    }
502
    break;
5✔
503

504
  /* Destination is a vector. */
505
  case CCX(V, I):
×
506
  case CCX(V, F):
507
  case CCX(V, C):
508
  case CCX(V, V):
509
    goto err_nyi;
×
510

511
  /* Destination is a pointer. */
512
  case CCX(P, P):
12,674✔
513
  case CCX(P, A):
514
  case CCX(P, S):
515
    /* There are only 32 bit pointers/addresses on 32 bit machines.
516
    ** Also ok on x64, since all 32 bit ops clear the upper part of the reg.
517
    */
518
    goto xstore;
12,674✔
519
  case CCX(P, I):
2✔
520
    if (st == IRT_CDATA) goto err_nyi;
2✔
521
    if (!LJ_64 && ssize == 8)  /* Truncate from 64 bit integer. */
2✔
522
      sp = emitconv(sp, IRT_U32, st, 0);
523
    goto xstore;
2✔
524
  case CCX(P, F):
2✔
525
    if (st == IRT_CDATA) goto err_nyi;
2✔
526
    /* The signed conversion is cheaper. x64 really has 47 bit pointers. */
527
    sp = emitconv(sp, (LJ_64 && dsize == 8) ? IRT_I64 : IRT_U32,
2✔
528
                  st, IRCONV_ANY);
529
    goto xstore;
2✔
530

531
  /* Destination is an array. */
532
  case CCX(A, A):
4✔
533
  /* Destination is a struct/union. */
534
  case CCX(S, S):
535
    if (dp == 0) goto err_conv;
4✔
536
    crec_copy(J, dp, sp, lj_ir_kint(J, dsize), d);
4✔
537
    break;
4✔
538

539
  default:
540
  err_conv:
×
541
  err_nyi:
×
542
    lj_trace_err(J, LJ_TRERR_NYICONV);
×
543
    break;
544
  }
545
  return 0;
546
}
547

548
/* -- Convert C type to TValue (load) ------------------------------------- */
549

550
static TRef crec_tv_ct(jit_State *J, CType *s, CTypeID sid, TRef sp)
22,089✔
551
{
552
  CTState *cts = ctype_ctsG(J2G(J));
22,089✔
553
  IRType t = crec_ct2irt(cts, s);
22,089✔
554
  CTInfo sinfo = s->info;
22,089✔
555
  if (ctype_isnum(sinfo)) {
22,089✔
556
    TRef tr;
22,008✔
557
    if (t == IRT_CDATA)
22,008✔
558
      goto err_nyi;  /* NYI: copyval of >64 bit integers. */
×
559
    tr = emitir(IRT(IR_XLOAD, t), sp, 0);
22,008✔
560
    if (t == IRT_FLOAT || t == IRT_U32) {  /* Keep uint32_t/float as numbers. */
22,008✔
561
      return emitconv(tr, IRT_NUM, t, 0);
18✔
562
    } else if (t == IRT_I64 || t == IRT_U64) {  /* Box 64 bit integer. */
21,990✔
563
      sp = tr;
564
      lj_needsplit(J);
565
    } else if ((sinfo & CTF_BOOL)) {
21,924✔
566
      /* Assume not equal to zero. Fixup and emit pending guard later. */
567
      lj_ir_set(J, IRTGI(IR_NE), tr, lj_ir_kint(J, 0));
4✔
568
      J->postproc = LJ_POST_FIXGUARD;
4✔
569
      return TREF_TRUE;
4✔
570
    } else {
571
      return tr;
572
    }
573
  } else if (ctype_isptr(sinfo) || ctype_isenum(sinfo)) {
81✔
574
    sp = emitir(IRT(IR_XLOAD, t), sp, 0);  /* Box pointers and enums. */
24✔
575
  } else if (ctype_isrefarray(sinfo) || ctype_isstruct(sinfo)) {
57✔
576
    cts->L = J->L;
44✔
577
    sid = lj_ctype_intern(cts, CTINFO_REF(sid), CTSIZE_PTR);  /* Create ref. */
44✔
578
  } else if (ctype_iscomplex(sinfo)) {  /* Unbox/box complex. */
13✔
579
    ptrdiff_t esz = (ptrdiff_t)(s->size >> 1);
13✔
580
    TRef ptr, tr1, tr2, dp;
13✔
581
    dp = emitir(IRTG(IR_CNEW, IRT_CDATA), lj_ir_kint(J, sid), TREF_NIL);
13✔
582
    tr1 = emitir(IRT(IR_XLOAD, t), sp, 0);
13✔
583
    ptr = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, esz));
13✔
584
    tr2 = emitir(IRT(IR_XLOAD, t), ptr, 0);
13✔
585
    ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, sizeof(GCcdata)));
13✔
586
    emitir(IRT(IR_XSTORE, t), ptr, tr1);
13✔
587
    ptr = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, sizeof(GCcdata)+esz));
13✔
588
    emitir(IRT(IR_XSTORE, t), ptr, tr2);
13✔
589
    return dp;
13✔
590
  } else {
591
    /* NYI: copyval of vectors. */
592
  err_nyi:
×
593
    lj_trace_err(J, LJ_TRERR_NYICONV);
×
594
  }
595
  /* Box pointer, ref, enum or 64 bit integer. */
596
  return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, sid), sp);
134✔
597
}
598

599
/* -- Convert TValue to C type (store) ------------------------------------ */
600

601
static TRef crec_ct_tv(jit_State *J, CType *d, TRef dp, TRef sp, cTValue *sval)
36,396✔
602
{
603
  CTState *cts = ctype_ctsG(J2G(J));
36,396✔
604
  CTypeID sid = CTID_P_VOID;
36,396✔
605
  void *svisnz = 0;
36,396✔
606
  CType *s;
36,396✔
607
  if (LJ_LIKELY(tref_isinteger(sp))) {
36,396✔
608
    sid = CTID_INT32;
22,645✔
609
    svisnz = (void *)(intptr_t)(tvisint(sval)?(intV(sval)!=0):!tviszero(sval));
22,645✔
610
  } else if (tref_isnum(sp)) {
13,751✔
611
    sid = CTID_DOUBLE;
299✔
612
    svisnz = (void *)(intptr_t)(tvisint(sval)?(intV(sval)!=0):!tviszero(sval));
299✔
613
  } else if (tref_isbool(sp)) {
13,452✔
614
    sp = lj_ir_kint(J, tref_istrue(sp) ? 1 : 0);
×
615
    sid = CTID_BOOL;
×
616
  } else if (tref_isnil(sp)) {
13,452✔
617
    sp = lj_ir_kptr(J, NULL);
8✔
618
  } else if (tref_isudata(sp)) {
13,444✔
619
    GCudata *ud = udataV(sval);
5✔
620
    if (ud->udtype == UDTYPE_IO_FILE) {
5✔
621
      TRef tr = emitir(IRT(IR_FLOAD, IRT_U8), sp, IRFL_UDATA_UDTYPE);
1✔
622
      emitir(IRTGI(IR_EQ), tr, lj_ir_kint(J, UDTYPE_IO_FILE));
1✔
623
      sp = emitir(IRT(IR_FLOAD, IRT_PTR), sp, IRFL_UDATA_FILE);
1✔
624
    } else {
625
      sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCudata)));
4✔
626
    }
627
  } else if (tref_isstr(sp)) {
13,439✔
628
    if (ctype_isenum(d->info)) {  /* Match string against enum constant. */
17✔
629
      GCstr *str = strV(sval);
9✔
630
      CTSize ofs;
9✔
631
      CType *cct = lj_ctype_getfield(cts, d, str, &ofs);
9✔
632
      /* Specialize to the name of the enum constant. */
633
      emitir(IRTG(IR_EQ, IRT_STR), sp, lj_ir_kstr(J, str));
9✔
634
      if (cct && ctype_isconstval(cct->info)) {
9✔
635
        lj_assertJ(ctype_child(cts, cct)->size == 4,
9✔
636
                   "only 32 bit const supported");  /* NYI */
637
        svisnz = (void *)(intptr_t)(ofs != 0);
9✔
638
        sp = lj_ir_kint(J, (int32_t)ofs);
9✔
639
        sid = ctype_cid(cct->info);
9✔
640
      }  /* else: interpreter will throw. */
641
    } else if (ctype_isrefarray(d->info)) {  /* Copy string to array. */
8✔
642
      lj_trace_err(J, LJ_TRERR_BADTYPE);  /* NYI */
×
643
    } else {  /* Otherwise pass the string data as a const char[]. */
644
      /* Don't use STRREF. It folds with SNEW, which loses the trailing NUL. */
645
      sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCstr)));
8✔
646
      sid = CTID_A_CCHAR;
8✔
647
    }
648
  } else if (tref_islightud(sp)) {
13,422✔
649
#if LJ_64
650
    lj_trace_err(J, LJ_TRERR_NYICONV);
2✔
651
#endif
652
  } else {  /* NYI: tref_istab(sp). */
653
    IRType t;
13,420✔
654
    sid = argv2cdata(J, sp, sval)->ctypeid;
13,420✔
655
    s = ctype_raw(cts, sid);
13,414✔
656
    svisnz = cdataptr(cdataV(sval));
13,414✔
657
    if (ctype_isfunc(s->info)) {
13,414✔
658
      sid = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|sid), CTSIZE_PTR);
×
659
      s = ctype_get(cts, sid);
×
660
      t = IRT_PTR;
×
661
    } else {
662
      t = crec_ct2irt(cts, s);
13,414✔
663
    }
664
    if (ctype_isptr(s->info)) {
13,414✔
665
      sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_PTR);
12,643✔
666
      if (ctype_isref(s->info)) {
12,643✔
667
        svisnz = *(void **)svisnz;
1✔
668
        s = ctype_rawchild(cts, s);
1✔
669
        if (ctype_isenum(s->info)) s = ctype_child(cts, s);
1✔
670
        t = crec_ct2irt(cts, s);
1✔
671
      } else {
672
        goto doconv;
12,642✔
673
      }
674
    } else if (t == IRT_I64 || t == IRT_U64) {
771✔
675
      sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_INT64);
725✔
676
      lj_needsplit(J);
725✔
677
      goto doconv;
725✔
678
    } else if (t == IRT_INT || t == IRT_U32) {
46✔
679
      if (ctype_isenum(s->info)) s = ctype_child(cts, s);
5✔
680
      sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_INT);
5✔
681
      goto doconv;
5✔
682
    } else {
683
      sp = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, sizeof(GCcdata)));
41✔
684
    }
685
    if (ctype_isnum(s->info) && t != IRT_CDATA)
42✔
686
      sp = emitir(IRT(IR_XLOAD, t), sp, 0);  /* Load number value. */
2✔
687
    goto doconv;
42✔
688
  }
689
  s = ctype_get(cts, sid);
22,974✔
690
doconv:
36,388✔
691
  if (ctype_isenum(d->info)) d = ctype_child(cts, d);
36,388✔
692
  return crec_ct_ct(J, d, s, dp, sp, svisnz);
36,388✔
693
}
694

695
/* -- C data metamethods -------------------------------------------------- */
696

697
/* This would be rather difficult in FOLD, so do it here:
698
** (base+k)+(idx*sz)+ofs ==> (base+idx*sz)+(ofs+k)
699
** (base+(idx+k)*sz)+ofs ==> (base+idx*sz)+(ofs+k*sz)
700
*/
701
static TRef crec_reassoc_ofs(jit_State *J, TRef tr, ptrdiff_t *ofsp, MSize sz)
702
{
703
  IRIns *ir = IR(tref_ref(tr));
704
  if (LJ_LIKELY(J->flags & JIT_F_OPT_FOLD) && irref_isk(ir->op2) &&
705
      (ir->o == IR_ADD || ir->o == IR_ADDOV || ir->o == IR_SUBOV)) {
706
    IRIns *irk = IR(ir->op2);
707
    ptrdiff_t k;
708
    if (LJ_64 && irk->o == IR_KINT64)
709
      k = (ptrdiff_t)ir_kint64(irk)->u64 * sz;
710
    else
711
      k = (ptrdiff_t)irk->i * sz;
712
    if (ir->o == IR_SUBOV) *ofsp -= k; else *ofsp += k;
713
    tr = ir->op1;  /* Not a TRef, but the caller doesn't care. */
714
  }
715
  return tr;
716
}
717

718
/* Tailcall to function. */
719
static void crec_tailcall(jit_State *J, RecordFFData *rd, cTValue *tv)
5✔
720
{
721
  TRef kfunc = lj_ir_kfunc(J, funcV(tv));
10✔
722
#if LJ_FR2
723
  J->base[-2] = kfunc;
5✔
724
  J->base[-1] = TREF_FRAME;
5✔
725
#else
726
  J->base[-1] = kfunc | TREF_FRAME;
727
#endif
728
  rd->nres = -1;  /* Pending tailcall. */
5✔
729
}
1✔
730

731
/* Record ctype __index/__newindex metamethods. */
732
static void crec_index_meta(jit_State *J, CTState *cts, CType *ct,
4✔
733
                            RecordFFData *rd)
734
{
735
  CTypeID id = ctype_typeid(cts, ct);
4✔
736
  cTValue *tv = lj_ctype_meta(cts, id, rd->data ? MM_newindex : MM_index);
4✔
737
  if (!tv)
4✔
738
    lj_trace_err(J, LJ_TRERR_BADTYPE);
×
739
  if (tvisfunc(tv)) {
4✔
740
    crec_tailcall(J, rd, tv);
1✔
741
  } else if (rd->data == 0 && tvistab(tv) && tref_isstr(J->base[1])) {
6✔
742
    /* Specialize to result of __index lookup. */
743
    cTValue *o = lj_tab_get(J->L, tabV(tv), &rd->argv[1]);
3✔
744
    J->base[0] = lj_record_constify(J, o);
3✔
745
    if (!J->base[0])
3✔
746
      lj_trace_err(J, LJ_TRERR_BADTYPE);
×
747
    /* Always specialize to the key. */
748
    emitir(IRTG(IR_EQ, IRT_STR), J->base[1], lj_ir_kstr(J, strV(&rd->argv[1])));
3✔
749
  } else {
750
    /* NYI: resolving of non-function metamethods. */
751
    /* NYI: non-string keys for __index table. */
752
    /* NYI: stores to __newindex table. */
753
    lj_trace_err(J, LJ_TRERR_BADTYPE);
×
754
  }
755
}
4✔
756

757
/* Record bitfield load/store. */
758
static void crec_index_bf(jit_State *J, RecordFFData *rd, TRef ptr, CTInfo info)
×
759
{
760
  IRType t = IRT_I8 + 2*lj_fls(ctype_bitcsz(info)) + ((info&CTF_UNSIGNED)?1:0);
×
761
  TRef tr = emitir(IRT(IR_XLOAD, t), ptr, 0);
×
762
  CTSize pos = ctype_bitpos(info), bsz = ctype_bitbsz(info), shift = 32 - bsz;
×
763
  lj_assertJ(t <= IRT_U32, "only 32 bit bitfields supported");  /* NYI */
×
764
  if (rd->data == 0) {  /* __index metamethod. */
×
765
    if ((info & CTF_BOOL)) {
×
766
      tr = emitir(IRTI(IR_BAND), tr, lj_ir_kint(J, (int32_t)((1u << pos))));
×
767
      /* Assume not equal to zero. Fixup and emit pending guard later. */
768
      lj_ir_set(J, IRTGI(IR_NE), tr, lj_ir_kint(J, 0));
×
769
      J->postproc = LJ_POST_FIXGUARD;
×
770
      tr = TREF_TRUE;
×
771
    } else if (!(info & CTF_UNSIGNED)) {
×
772
      tr = emitir(IRTI(IR_BSHL), tr, lj_ir_kint(J, shift - pos));
×
773
      tr = emitir(IRTI(IR_BSAR), tr, lj_ir_kint(J, shift));
×
774
    } else {
775
      lj_assertJ(bsz < 32, "unexpected full bitfield index");
×
776
      tr = emitir(IRTI(IR_BSHR), tr, lj_ir_kint(J, pos));
×
777
      tr = emitir(IRTI(IR_BAND), tr, lj_ir_kint(J, (int32_t)((1u << bsz)-1)));
×
778
      /* We can omit the U32 to NUM conversion, since bsz < 32. */
779
    }
780
    J->base[0] = tr;
×
781
  } else {  /* __newindex metamethod. */
782
    CTState *cts = ctype_ctsG(J2G(J));
×
783
    CType *ct = ctype_get(cts,
×
784
                          (info & CTF_BOOL) ? CTID_BOOL :
×
785
                          (info & CTF_UNSIGNED) ? CTID_UINT32 : CTID_INT32);
×
786
    int32_t mask = (int32_t)(((1u << bsz)-1) << pos);
×
787
    TRef sp = crec_ct_tv(J, ct, 0, J->base[2], &rd->argv[2]);
×
788
    sp = emitir(IRTI(IR_BSHL), sp, lj_ir_kint(J, pos));
×
789
    /* Use of the target type avoids forwarding conversions. */
790
    sp = emitir(IRT(IR_BAND, t), sp, lj_ir_kint(J, mask));
×
791
    tr = emitir(IRT(IR_BAND, t), tr, lj_ir_kint(J, (int32_t)~mask));
×
792
    tr = emitir(IRT(IR_BOR, t), tr, sp);
×
793
    emitir(IRT(IR_XSTORE, t), ptr, tr);
×
794
    rd->nres = 0;
×
795
    J->needsnap = 1;
×
796
  }
797
}
×
798

799
void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd)
22,228✔
800
{
801
  TRef idx, ptr = J->base[0];
22,228✔
802
  ptrdiff_t ofs = sizeof(GCcdata);
22,228✔
803
  GCcdata *cd = argv2cdata(J, ptr, &rd->argv[0]);
22,228✔
804
  CTState *cts = ctype_ctsG(J2G(J));
22,228✔
805
  CType *ct = ctype_raw(cts, cd->ctypeid);
22,228✔
806
  CTypeID sid = 0;
22,228✔
807

808
  /* Resolve pointer or reference for cdata object. */
809
  if (ctype_isptr(ct->info)) {
22,228✔
810
    IRType t = (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32;
95✔
811
    if (ctype_isref(ct->info)) ct = ctype_rawchild(cts, ct);
95✔
812
    ptr = emitir(IRT(IR_FLOAD, t), ptr, IRFL_CDATA_PTR);
95✔
813
    ofs = 0;
95✔
814
    ptr = crec_reassoc_ofs(J, ptr, &ofs, 1);
95✔
815
  }
816

817
again:
22,133✔
818
  idx = J->base[1];
22,239✔
819
  if (tref_isnumber(idx)) {
22,239✔
820
    idx = lj_opt_narrow_cindex(J, idx);
22,013✔
821
    if (ctype_ispointer(ct->info)) {
22,013✔
822
      CTSize sz;
22,016✔
823
  integer_key:
22,013✔
824
      if ((ct->info & CTF_COMPLEX))
22,016✔
825
        idx = emitir(IRT(IR_BAND, IRT_INTP), idx, lj_ir_kintp(J, 1));
3✔
826
      sz = lj_ctype_size(cts, (sid = ctype_cid(ct->info)));
22,016✔
827
      idx = crec_reassoc_ofs(J, idx, &ofs, sz);
22,016✔
828
#if LJ_TARGET_ARM || LJ_TARGET_PPC
829
      /* Hoist base add to allow fusion of index/shift into operands. */
830
      if (LJ_LIKELY(J->flags & JIT_F_OPT_LOOP) && ofs
831
#if LJ_TARGET_ARM
832
          && (sz == 1 || sz == 4)
833
#endif
834
          ) {
835
        ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs));
836
        ofs = 0;
837
      }
838
#endif
839
      idx = emitir(IRT(IR_MUL, IRT_INTP), idx, lj_ir_kintp(J, sz));
22,016✔
840
      ptr = emitir(IRT(IR_ADD, IRT_PTR), idx, ptr);
22,016✔
841
    }
842
  } else if (tref_iscdata(idx)) {
226✔
843
    GCcdata *cdk = cdataV(&rd->argv[1]);
3✔
844
    CType *ctk = ctype_raw(cts, cdk->ctypeid);
3✔
845
    IRType t = crec_ct2irt(cts, ctk);
3✔
846
    if (ctype_ispointer(ct->info) && t >= IRT_I8 && t <= IRT_U64) {
3✔
847
      if (ctk->size == 8) {
3✔
848
        idx = emitir(IRT(IR_FLOAD, t), idx, IRFL_CDATA_INT64);
2✔
849
      } else if (ctk->size == 4) {
1✔
850
        idx = emitir(IRT(IR_FLOAD, t), idx, IRFL_CDATA_INT);
1✔
851
      } else {
852
        idx = emitir(IRT(IR_ADD, IRT_PTR), idx,
×
853
                     lj_ir_kintp(J, sizeof(GCcdata)));
854
        idx = emitir(IRT(IR_XLOAD, t), idx, 0);
×
855
      }
856
      if (LJ_64 && ctk->size < sizeof(intptr_t) && !(ctk->info & CTF_UNSIGNED))
3✔
857
        idx = emitconv(idx, IRT_INTP, IRT_INT, IRCONV_SEXT);
1✔
858
      if (!LJ_64 && ctk->size > sizeof(intptr_t)) {
3✔
859
        idx = emitconv(idx, IRT_INTP, t, 0);
860
        lj_needsplit(J);
3✔
861
      }
862
      goto integer_key;
3✔
863
    }
864
  } else if (tref_isstr(idx)) {
223✔
865
    GCstr *name = strV(&rd->argv[1]);
223✔
866
    if (cd && cd->ctypeid == CTID_CTYPEID)
223✔
867
      ct = ctype_raw(cts, crec_constructor(J, cd, ptr));
×
868
    if (ctype_isstruct(ct->info)) {
223✔
869
      CTSize fofs;
196✔
870
      CType *fct;
196✔
871
      fct = lj_ctype_getfield(cts, ct, name, &fofs);
196✔
872
      if (fct) {
196✔
873
        ofs += (ptrdiff_t)fofs;
193✔
874
        /* Always specialize to the field name. */
875
        emitir(IRTG(IR_EQ, IRT_STR), idx, lj_ir_kstr(J, name));
193✔
876
        if (ctype_isconstval(fct->info)) {
193✔
877
          if (fct->size >= 0x80000000u &&
×
878
              (ctype_child(cts, fct)->info & CTF_UNSIGNED)) {
×
879
            J->base[0] = lj_ir_knum(J, (lua_Number)(uint32_t)fct->size);
×
880
            return;
×
881
          }
882
          J->base[0] = lj_ir_kint(J, (int32_t)fct->size);
×
883
          return;  /* Interpreter will throw for newindex. */
×
884
        } else if (ctype_isbitfield(fct->info)) {
193✔
885
          if (ofs)
×
886
            ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs));
×
887
          crec_index_bf(J, rd, ptr, fct->info);
×
888
          return;
×
889
        } else {
890
          lj_assertJ(ctype_isfield(fct->info), "field expected");
193✔
891
          sid = ctype_cid(fct->info);
193✔
892
        }
893
      }
894
    } else if (ctype_iscomplex(ct->info)) {
27✔
895
      if (name->len == 2 &&
15✔
896
          ((strdata(name)[0] == 'r' && strdata(name)[1] == 'e') ||
15✔
897
           (strdata(name)[0] == 'i' && strdata(name)[1] == 'm'))) {
7✔
898
        /* Always specialize to the field name. */
899
        emitir(IRTG(IR_EQ, IRT_STR), idx, lj_ir_kstr(J, name));
15✔
900
        if (strdata(name)[0] == 'i') ofs += (ct->size >> 1);
15✔
901
        sid = ctype_cid(ct->info);
15✔
902
      }
903
    }
904
  }
905
  if (!sid) {
22,239✔
906
    if (ctype_isptr(ct->info)) {  /* Automatically perform '->'. */
15✔
907
      CType *cct = ctype_rawchild(cts, ct);
12✔
908
      if (ctype_isstruct(cct->info)) {
12✔
909
        ct = cct;
11✔
910
        cd = NULL;
11✔
911
        if (tref_isstr(idx)) goto again;
11✔
912
      }
913
    }
914
    crec_index_meta(J, cts, ct, rd);
4✔
915
    return;
4✔
916
  }
917

918
  if (ofs)
22,224✔
919
    ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs));
22,161✔
920

921
  /* Resolve reference for field. */
922
  ct = ctype_get(cts, sid);
22,224✔
923
  if (ctype_isref(ct->info)) {
22,224✔
924
    ptr = emitir(IRT(IR_XLOAD, IRT_PTR), ptr, 0);
1✔
925
    sid = ctype_cid(ct->info);
1✔
926
    ct = ctype_get(cts, sid);
1✔
927
  }
928

929
  while (ctype_isattrib(ct->info))
22,224✔
930
    ct = ctype_child(cts, ct);  /* Skip attributes. */
×
931

932
  if (rd->data == 0) {  /* __index metamethod. */
22,224✔
933
    J->base[0] = crec_tv_ct(J, ct, sid, ptr);
22,089✔
934
  } else {  /* __newindex metamethod. */
935
    rd->nres = 0;
135✔
936
    J->needsnap = 1;
135✔
937
    crec_ct_tv(J, ct, ptr, J->base[2], &rd->argv[2]);
135✔
938
  }
939
}
940

941
/* Record setting a finalizer. */
942
static void crec_finalizer(jit_State *J, TRef trcd, TRef trfin, cTValue *fin)
943
{
944
  if (tvisgcv(fin)) {
945
    if (!trfin) trfin = lj_ir_kptr(J, gcval(fin));
946
  } else if (tvisnil(fin)) {
947
    trfin = lj_ir_kptr(J, NULL);
948
  } else {
949
    lj_trace_err(J, LJ_TRERR_BADTYPE);
950
  }
951
  lj_ir_call(J, IRCALL_lj_cdata_setfin, trcd,
952
             trfin, lj_ir_kint(J, (int32_t)itype(fin)));
953
  J->needsnap = 1;
954
}
955

956
/* Record cdata allocation. */
957
static void crec_alloc(jit_State *J, RecordFFData *rd, CTypeID id)
958
{
959
  CTState *cts = ctype_ctsG(J2G(J));
960
  CTSize sz;
961
  CTInfo info = lj_ctype_info(cts, id, &sz);
962
  CType *d = ctype_raw(cts, id);
963
  TRef trcd, trid = lj_ir_kint(J, id);
964
  cTValue *fin;
965
  /* Use special instruction to box pointer or 32/64 bit integer. */
966
  if (ctype_isptr(info) || (ctype_isinteger(info) && (sz == 4 || sz == 8))) {
967
    TRef sp = J->base[1] ? crec_ct_tv(J, d, 0, J->base[1], &rd->argv[1]) :
968
              ctype_isptr(info) ? lj_ir_kptr(J, NULL) :
969
              sz == 4 ? lj_ir_kint(J, 0) :
970
              (lj_needsplit(J), lj_ir_kint64(J, 0));
971
    J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), trid, sp);
972
    return;
973
  } else {
974
    TRef trsz = TREF_NIL;
975
    if ((info & CTF_VLA)) {  /* Calculate VLA/VLS size at runtime. */
976
      CTSize sz0, sz1;
977
      if (!J->base[1] || J->base[2])
978
        lj_trace_err(J, LJ_TRERR_NYICONV);  /* NYI: init VLA/VLS. */
979
      trsz = crec_ct_tv(J, ctype_get(cts, CTID_INT32), 0,
980
                        J->base[1], &rd->argv[1]);
981
      sz0 = lj_ctype_vlsize(cts, d, 0);
982
      sz1 = lj_ctype_vlsize(cts, d, 1);
983
      trsz = emitir(IRTGI(IR_MULOV), trsz, lj_ir_kint(J, (int32_t)(sz1-sz0)));
984
      trsz = emitir(IRTGI(IR_ADDOV), trsz, lj_ir_kint(J, (int32_t)sz0));
985
      J->base[1] = 0;  /* Simplify logic below. */
986
    } else if (ctype_align(info) > CT_MEMALIGN) {
987
      trsz = lj_ir_kint(J, sz);
988
    }
989
    trcd = emitir(IRTG(IR_CNEW, IRT_CDATA), trid, trsz);
990
    if (sz > 128 || (info & CTF_VLA)) {
991
      TRef dp;
992
      CTSize align;
993
    special:  /* Only handle bulk zero-fill for large/VLA/VLS types. */
994
      if (J->base[1])
995
        lj_trace_err(J, LJ_TRERR_NYICONV);  /* NYI: init large/VLA/VLS types. */
996
      dp = emitir(IRT(IR_ADD, IRT_PTR), trcd, lj_ir_kintp(J, sizeof(GCcdata)));
997
      if (trsz == TREF_NIL) trsz = lj_ir_kint(J, sz);
998
      align = ctype_align(info);
999
      if (align < CT_MEMALIGN) align = CT_MEMALIGN;
1000
      crec_fill(J, dp, trsz, lj_ir_kint(J, 0), (1u << align));
1001
    } else if (J->base[1] && !J->base[2] &&
1002
        !lj_cconv_multi_init(cts, d, &rd->argv[1])) {
1003
      goto single_init;
1004
    } else if (ctype_isarray(d->info)) {
1005
      CType *dc = ctype_rawchild(cts, d);  /* Array element type. */
1006
      CTSize ofs, esize = dc->size;
1007
      TRef sp = 0;
1008
      TValue tv;
1009
      TValue *sval = &tv;
1010
      MSize i;
1011
      tv.u64 = 0;
1012
      if (!(ctype_isnum(dc->info) || ctype_isptr(dc->info)) ||
1013
          esize * CREC_FILL_MAXUNROLL < sz)
1014
        goto special;
1015
      for (i = 1, ofs = 0; ofs < sz; ofs += esize) {
1016
        TRef dp = emitir(IRT(IR_ADD, IRT_PTR), trcd,
1017
                         lj_ir_kintp(J, ofs + sizeof(GCcdata)));
1018
        if (J->base[i]) {
1019
          sp = J->base[i];
1020
          sval = &rd->argv[i];
1021
          i++;
1022
        } else if (i != 2) {
1023
          sp = ctype_isnum(dc->info) ? lj_ir_kint(J, 0) : TREF_NIL;
1024
        }
1025
        crec_ct_tv(J, dc, dp, sp, sval);
1026
      }
1027
    } else if (ctype_isstruct(d->info)) {
1028
      CTypeID fid;
1029
      MSize i = 1;
1030
      if (!J->base[1]) {  /* Handle zero-fill of struct-of-NYI. */
1031
        fid = d->sib;
1032
        while (fid) {
1033
          CType *df = ctype_get(cts, fid);
1034
          fid = df->sib;
1035
          if (ctype_isfield(df->info)) {
1036
            CType *dc;
1037
            if (!gcref(df->name)) continue;  /* Ignore unnamed fields. */
1038
            dc = ctype_rawchild(cts, df);  /* Field type. */
1039
            if (!(ctype_isnum(dc->info) || ctype_isptr(dc->info) ||
1040
                  ctype_isenum(dc->info)))
1041
              goto special;
1042
          } else if (!ctype_isconstval(df->info)) {
1043
            goto special;
1044
          }
1045
        }
1046
      }
1047
      fid = d->sib;
1048
      while (fid) {
1049
        CType *df = ctype_get(cts, fid);
1050
        fid = df->sib;
1051
        if (ctype_isfield(df->info)) {
1052
          CType *dc;
1053
          TRef sp, dp;
1054
          TValue tv;
1055
          TValue *sval = &tv;
1056
          setintV(&tv, 0);
1057
          if (!gcref(df->name)) continue;  /* Ignore unnamed fields. */
1058
          dc = ctype_rawchild(cts, df);  /* Field type. */
1059
          if (!(ctype_isnum(dc->info) || ctype_isptr(dc->info) ||
1060
                ctype_isenum(dc->info)))
1061
            lj_trace_err(J, LJ_TRERR_NYICONV);  /* NYI: init aggregates. */
1062
          if (J->base[i]) {
1063
            sp = J->base[i];
1064
            sval = &rd->argv[i];
1065
            i++;
1066
          } else {
1067
            sp = ctype_isptr(dc->info) ? TREF_NIL : lj_ir_kint(J, 0);
1068
          }
1069
          dp = emitir(IRT(IR_ADD, IRT_PTR), trcd,
1070
                      lj_ir_kintp(J, df->size + sizeof(GCcdata)));
1071
          crec_ct_tv(J, dc, dp, sp, sval);
1072
          if ((d->info & CTF_UNION)) {
1073
            if (d->size != dc->size)  /* NYI: partial init of union. */
1074
              lj_trace_err(J, LJ_TRERR_NYICONV);
1075
            break;
1076
          }
1077
        } else if (!ctype_isconstval(df->info)) {
1078
          /* NYI: init bitfields and sub-structures. */
1079
          lj_trace_err(J, LJ_TRERR_NYICONV);
1080
        }
1081
      }
1082
    } else {
1083
      TRef dp;
1084
    single_init:
1085
      dp = emitir(IRT(IR_ADD, IRT_PTR), trcd, lj_ir_kintp(J, sizeof(GCcdata)));
1086
      if (J->base[1]) {
1087
        crec_ct_tv(J, d, dp, J->base[1], &rd->argv[1]);
1088
      } else {
1089
        TValue tv;
1090
        tv.u64 = 0;
1091
        crec_ct_tv(J, d, dp, lj_ir_kint(J, 0), &tv);
1092
      }
1093
    }
1094
  }
1095
  J->base[0] = trcd;
1096
  /* Handle __gc metamethod. */
1097
  fin = lj_ctype_meta(cts, id, MM_gc);
1098
  if (fin)
1099
    crec_finalizer(J, trcd, 0, fin);
1100
}
1101

1102
/* Record argument conversions. */
1103
static TRef crec_call_args(jit_State *J, RecordFFData *rd,
1104
                           CTState *cts, CType *ct)
1105
{
1106
  TRef args[CCI_NARGS_MAX];
1107
  CTypeID fid;
1108
  MSize i, n;
1109
  TRef tr, *base;
1110
  cTValue *o;
1111
#if LJ_TARGET_X86
1112
#if LJ_ABI_WIN
1113
  TRef *arg0 = NULL, *arg1 = NULL;
1114
#endif
1115
  int ngpr = 0;
1116
  if (ctype_cconv(ct->info) == CTCC_THISCALL)
1117
    ngpr = 1;
1118
  else if (ctype_cconv(ct->info) == CTCC_FASTCALL)
1119
    ngpr = 2;
1120
#endif
1121

1122
  /* Skip initial attributes. */
1123
  fid = ct->sib;
1124
  while (fid) {
1125
    CType *ctf = ctype_get(cts, fid);
1126
    if (!ctype_isattrib(ctf->info)) break;
1127
    fid = ctf->sib;
1128
  }
1129
  args[0] = TREF_NIL;
1130
  for (n = 0, base = J->base+1, o = rd->argv+1; *base; n++, base++, o++) {
1131
    CTypeID did;
1132
    CType *d;
1133

1134
    if (n >= CCI_NARGS_MAX)
1135
      lj_trace_err(J, LJ_TRERR_NYICALL);
1136

1137
    if (fid) {  /* Get argument type from field. */
1138
      CType *ctf = ctype_get(cts, fid);
1139
      fid = ctf->sib;
1140
      lj_assertJ(ctype_isfield(ctf->info), "field expected");
1141
      did = ctype_cid(ctf->info);
1142
    } else {
1143
      if (!(ct->info & CTF_VARARG))
1144
        lj_trace_err(J, LJ_TRERR_NYICALL);  /* Too many arguments. */
1145
      did = lj_ccall_ctid_vararg(cts, o);  /* Infer vararg type. */
1146
    }
1147
    d = ctype_raw(cts, did);
1148
    if (!(ctype_isnum(d->info) || ctype_isptr(d->info) ||
1149
          ctype_isenum(d->info)))
1150
      lj_trace_err(J, LJ_TRERR_NYICALL);
1151
    tr = crec_ct_tv(J, d, 0, *base, o);
1152
    if (ctype_isinteger_or_bool(d->info)) {
1153
      if (d->size < 4) {
1154
        if ((d->info & CTF_UNSIGNED))
1155
          tr = emitconv(tr, IRT_INT, d->size==1 ? IRT_U8 : IRT_U16, 0);
1156
        else
1157
          tr = emitconv(tr, IRT_INT, d->size==1 ? IRT_I8 : IRT_I16,IRCONV_SEXT);
1158
      }
1159
    } else if (LJ_SOFTFP32 && ctype_isfp(d->info) && d->size > 4) {
1160
      lj_needsplit(J);
1161
    }
1162
#if LJ_TARGET_X86
1163
    /* 64 bit args must not end up in registers for fastcall/thiscall. */
1164
#if LJ_ABI_WIN
1165
    if (!ctype_isfp(d->info)) {
1166
      /* Sigh, the Windows/x86 ABI allows reordering across 64 bit args. */
1167
      if (tref_typerange(tr, IRT_I64, IRT_U64)) {
1168
        if (ngpr) {
1169
          arg0 = &args[n]; args[n++] = TREF_NIL; ngpr--;
1170
          if (ngpr) {
1171
            arg1 = &args[n]; args[n++] = TREF_NIL; ngpr--;
1172
          }
1173
        }
1174
      } else {
1175
        if (arg0) { *arg0 = tr; arg0 = NULL; n--; continue; }
1176
        if (arg1) { *arg1 = tr; arg1 = NULL; n--; continue; }
1177
        if (ngpr) ngpr--;
1178
      }
1179
    }
1180
#else
1181
    if (!ctype_isfp(d->info) && ngpr) {
1182
      if (tref_typerange(tr, IRT_I64, IRT_U64)) {
1183
        /* No reordering for other x86 ABIs. Simply add alignment args. */
1184
        do { args[n++] = TREF_NIL; } while (--ngpr);
1185
      } else {
1186
        ngpr--;
1187
      }
1188
    }
1189
#endif
1190
#endif
1191
    args[n] = tr;
1192
  }
1193
  tr = args[0];
1194
  for (i = 1; i < n; i++)
1195
    tr = emitir(IRT(IR_CARG, IRT_NIL), tr, args[i]);
1196
  return tr;
1197
}
1198

1199
/* Create a snapshot for the caller, simulating a 'false' return value. */
1200
static void crec_snap_caller(jit_State *J)
5✔
1201
{
1202
  lua_State *L = J->L;
5✔
1203
  TValue *base = L->base, *top = L->top;
5✔
1204
  const BCIns *pc = J->pc;
5✔
1205
  TRef ftr = J->base[-1-LJ_FR2];
5✔
1206
  ptrdiff_t delta;
5✔
1207
  if (!frame_islua(base-1) || J->framedepth <= 0)
5✔
1208
    lj_trace_err(J, LJ_TRERR_NYICALL);
×
1209
  J->pc = frame_pc(base-1); delta = 1+LJ_FR2+bc_a(J->pc[-1]);
5✔
1210
  L->top = base; L->base = base - delta;
5✔
1211
  J->base[-1-LJ_FR2] = TREF_FALSE;
5✔
1212
  J->base -= delta; J->baseslot -= (BCReg)delta;
5✔
1213
  J->maxslot = (BCReg)delta-LJ_FR2; J->framedepth--;
5✔
1214
  lj_snap_add(J);
5✔
1215
  L->base = base; L->top = top;
5✔
1216
  J->framedepth++; J->maxslot = 1;
5✔
1217
  J->base += delta; J->baseslot += (BCReg)delta;
5✔
1218
  J->base[-1-LJ_FR2] = ftr; J->pc = pc;
5✔
1219
}
5✔
1220

1221
/* Record function call. */
1222
static int crec_call(jit_State *J, RecordFFData *rd, GCcdata *cd)
12,447✔
1223
{
1224
  CTState *cts = ctype_ctsG(J2G(J));
12,447✔
1225
  CType *ct = ctype_raw(cts, cd->ctypeid);
12,447✔
1226
  IRType tp = IRT_PTR;
12,447✔
1227
  if (ctype_isptr(ct->info)) {
12,447✔
1228
    tp = (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32;
7✔
1229
    ct = ctype_rawchild(cts, ct);
7✔
1230
  }
1231
  if (ctype_isfunc(ct->info)) {
12,447✔
1232
    TRef func = emitir(IRT(IR_FLOAD, tp), J->base[0], IRFL_CDATA_PTR);
12,446✔
1233
    CType *ctr = ctype_rawchild(cts, ct);
12,446✔
1234
    IRType t = crec_ct2irt(cts, ctr);
12,446✔
1235
    TRef tr;
12,446✔
1236
    TValue tv;
12,446✔
1237
    /* Check for blacklisted C functions that might call a callback. */
1238
    tv.u64 = ((uintptr_t)cdata_getptr(cdataptr(cd), (LJ_64 && tp == IRT_P64) ? 8 : 4) >> 2) | U64x(800000000, 00000000);
12,446✔
1239
    if (tvistrue(lj_tab_get(J->L, cts->miscmap, &tv)))
12,446✔
1240
      lj_trace_err(J, LJ_TRERR_BLACKL);
4✔
1241
    if (ctype_isvoid(ctr->info)) {
12,442✔
1242
      t = IRT_NIL;
2✔
1243
      rd->nres = 0;
2✔
1244
    } else if (!(ctype_isnum(ctr->info) || ctype_isptr(ctr->info) ||
12,440✔
1245
                 ctype_isenum(ctr->info)) || t == IRT_CDATA) {
12,440✔
1246
      lj_trace_err(J, LJ_TRERR_NYICALL);
×
1247
    }
1248
    if ((ct->info & CTF_VARARG)
12,442✔
1249
#if LJ_TARGET_X86
1250
        || ctype_cconv(ct->info) != CTCC_CDECL
1251
#endif
1252
        )
1253
      func = emitir(IRT(IR_CARG, IRT_NIL), func,
2✔
1254
                    lj_ir_kint(J, ctype_typeid(cts, ct)));
1255
    tr = emitir(IRT(IR_CALLXS, t), crec_call_args(J, rd, cts, ct), func);
12,442✔
1256
    if (ctype_isbool(ctr->info)) {
12,442✔
1257
      if (frame_islua(J->L->base-1) && bc_b(frame_pc(J->L->base-1)[-1]) == 1) {
5✔
1258
        /* Don't check result if ignored. */
1259
        tr = TREF_NIL;
1260
      } else {
1261
        crec_snap_caller(J);
5✔
1262
#if LJ_TARGET_X86ORX64
1263
        /* Note: only the x86/x64 backend supports U8 and only for EQ(tr, 0). */
1264
        lj_ir_set(J, IRTG(IR_NE, IRT_U8), tr, lj_ir_kint(J, 0));
5✔
1265
#else
1266
        lj_ir_set(J, IRTGI(IR_NE), tr, lj_ir_kint(J, 0));
1267
#endif
1268
        J->postproc = LJ_POST_FIXGUARDSNAP;
5✔
1269
        tr = TREF_TRUE;
5✔
1270
      }
1271
    } else if (t == IRT_PTR || (LJ_64 && t == IRT_P32) ||
12,437✔
1272
               t == IRT_I64 || t == IRT_U64 || ctype_isenum(ctr->info)) {
12,444✔
1273
      TRef trid = lj_ir_kint(J, ctype_cid(ct->info));
9✔
1274
      tr = emitir(IRTG(IR_CNEWI, IRT_CDATA), trid, tr);
9✔
1275
      if (t == IRT_I64 || t == IRT_U64) lj_needsplit(J);
9✔
1276
    } else if (t == IRT_FLOAT || t == IRT_U32) {
12,428✔
1277
      tr = emitconv(tr, IRT_NUM, t, 0);
1✔
1278
    } else if (t == IRT_I8 || t == IRT_I16) {
12,427✔
1279
      tr = emitconv(tr, IRT_INT, t, IRCONV_SEXT);
2✔
1280
    } else if (t == IRT_U8 || t == IRT_U16) {
12,425✔
1281
      tr = emitconv(tr, IRT_INT, t, 0);
2✔
1282
    }
1283
    J->base[0] = tr;
12,442✔
1284
    J->needsnap = 1;
12,442✔
1285
    return 1;
12,442✔
1286
  }
1287
  return 0;
1288
}
1289

1290
void LJ_FASTCALL recff_cdata_call(jit_State *J, RecordFFData *rd)
12,500✔
1291
{
1292
  CTState *cts = ctype_ctsG(J2G(J));
12,500✔
1293
  GCcdata *cd = argv2cdata(J, J->base[0], &rd->argv[0]);
12,500✔
1294
  CTypeID id = cd->ctypeid;
12,500✔
1295
  CType *ct;
12,500✔
1296
  cTValue *tv;
12,500✔
1297
  MMS mm = MM_call;
12,500✔
1298
  if (id == CTID_CTYPEID) {
12,500✔
1299
    id = crec_constructor(J, cd, J->base[0]);
53✔
1300
    mm = MM_new;
53✔
1301
  } else if (crec_call(J, rd, cd)) {
12,447✔
1302
    return;
1303
  }
1304
  /* Record ctype __call/__new metamethod. */
1305
  ct = ctype_raw(cts, id);
54✔
1306
  tv = lj_ctype_meta(cts, ctype_isptr(ct->info) ? ctype_cid(ct->info) : id, mm);
54✔
1307
  if (tv) {
54✔
1308
    if (tvisfunc(tv)) {
1✔
1309
      crec_tailcall(J, rd, tv);
1✔
1310
      return;
1✔
1311
    }
1312
  } else if (mm == MM_new) {
53✔
1313
    crec_alloc(J, rd, id);
53✔
1314
    return;
53✔
1315
  }
1316
  /* No metamethod or NYI: non-function metamethods. */
1317
  lj_trace_err(J, LJ_TRERR_BADTYPE);
×
1318
}
1319

1320
static TRef crec_arith_int64(jit_State *J, TRef *sp, CType **s, MMS mm)
75,055✔
1321
{
1322
  if (sp[0] && sp[1] && ctype_isnum(s[0]->info) && ctype_isnum(s[1]->info)) {
75,055✔
1323
    IRType dt;
74,816✔
1324
    CTypeID id;
74,816✔
1325
    TRef tr;
74,816✔
1326
    MSize i;
74,816✔
1327
    IROp op;
74,816✔
1328
    lj_needsplit(J);
74,816✔
1329
    if (((s[0]->info & CTF_UNSIGNED) && s[0]->size == 8) ||
74,816✔
1330
        ((s[1]->info & CTF_UNSIGNED) && s[1]->size == 8)) {
34,554✔
1331
      dt = IRT_U64; id = CTID_UINT64;
1332
    } else {
1333
      dt = IRT_I64; id = CTID_INT64;
85✔
1334
      if (mm < MM_add &&
85✔
1335
          !((s[0]->info | s[1]->info) & CTF_FP) &&
31✔
1336
          s[0]->size == 4 && s[1]->size == 4) {  /* Try to narrow comparison. */
28✔
1337
        if (!((s[0]->info ^ s[1]->info) & CTF_UNSIGNED) ||
14✔
1338
            (tref_isk(sp[1]) && IR(tref_ref(sp[1]))->i >= 0)) {
5✔
1339
          dt = (s[0]->info & CTF_UNSIGNED) ? IRT_U32 : IRT_INT;
11✔
1340
          goto comp;
11✔
1341
        } else if (tref_isk(sp[0]) && IR(tref_ref(sp[0]))->i >= 0) {
3✔
1342
          dt = (s[1]->info & CTF_UNSIGNED) ? IRT_U32 : IRT_INT;
1✔
1343
          goto comp;
1✔
1344
        }
1345
      }
1346
    }
1347
    for (i = 0; i < 2; i++) {
224,412✔
1348
      IRType st = tref_type(sp[i]);
149,608✔
1349
      if (st == IRT_NUM || st == IRT_FLOAT)
149,608✔
1350
        sp[i] = emitconv(sp[i], dt, st, IRCONV_ANY);
43,863✔
1351
      else if (!(st == IRT_I64 || st == IRT_U64))
105,745✔
1352
        sp[i] = emitconv(sp[i], dt, IRT_INT,
43,586✔
1353
                         (s[i]->info & CTF_UNSIGNED) ? 0 : IRCONV_SEXT);
1354
    }
1355
    if (mm < MM_add) {
74,804✔
1356
    comp:
34,499✔
1357
      /* Assume true comparison. Fixup and emit pending guard later. */
1358
      if (mm == MM_eq) {
34,511✔
1359
        op = IR_EQ;
1360
      } else {
1361
        op = mm == MM_lt ? IR_LT : IR_LE;
22,075✔
1362
        if (dt == IRT_U32 || dt == IRT_U64)
22,075✔
1363
          op += (IR_ULT-IR_LT);
22,068✔
1364
      }
1365
      lj_ir_set(J, IRTG(op, dt), sp[0], sp[1]);
34,511✔
1366
      J->postproc = LJ_POST_FIXGUARD;
34,511✔
1367
      return TREF_TRUE;
34,511✔
1368
    } else {
1369
      tr = emitir(IRT(mm+(int)IR_ADD-(int)MM_add, dt), sp[0], sp[1]);
40,305✔
1370
    }
1371
    return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr);
40,305✔
1372
  }
1373
  return 0;
1374
}
1375

1376
static TRef crec_arith_ptr(jit_State *J, TRef *sp, CType **s, MMS mm)
239✔
1377
{
1378
  CTState *cts = ctype_ctsG(J2G(J));
239✔
1379
  CType *ctp = s[0];
239✔
1380
  if (!(sp[0] && sp[1])) return 0;
239✔
1381
  if (ctype_isptr(ctp->info) || ctype_isrefarray(ctp->info)) {
238✔
1382
    if ((mm == MM_sub || mm == MM_eq || mm == MM_lt || mm == MM_le) &&
234✔
1383
        (ctype_isptr(s[1]->info) || ctype_isrefarray(s[1]->info))) {
12✔
1384
      if (mm == MM_sub) {  /* Pointer difference. */
10✔
1385
        TRef tr;
2✔
1386
        CTSize sz = lj_ctype_size(cts, ctype_cid(ctp->info));
2✔
1387
        if (sz == 0 || (sz & (sz-1)) != 0)
2✔
1388
          return 0;  /* NYI: integer division. */
1389
        tr = emitir(IRT(IR_SUB, IRT_INTP), sp[0], sp[1]);
2✔
1390
        tr = emitir(IRT(IR_BSAR, IRT_INTP), tr, lj_ir_kint(J, lj_fls(sz)));
2✔
1391
#if LJ_64
1392
        tr = emitconv(tr, IRT_NUM, IRT_INTP, 0);
2✔
1393
#endif
1394
        return tr;
2✔
1395
      } else {  /* Pointer comparison (unsigned). */
1396
        /* Assume true comparison. Fixup and emit pending guard later. */
1397
        IROp op = mm == MM_eq ? IR_EQ : mm == MM_lt ? IR_ULT : IR_ULE;
8✔
1398
        lj_ir_set(J, IRTG(op, IRT_PTR), sp[0], sp[1]);
8✔
1399
        J->postproc = LJ_POST_FIXGUARD;
8✔
1400
        return TREF_TRUE;
8✔
1401
      }
1402
    }
1403
    if (!((mm == MM_add || mm == MM_sub) && ctype_isnum(s[1]->info)))
224✔
1404
      return 0;
1405
  } else if (mm == MM_add && ctype_isnum(ctp->info) &&
4✔
1406
             (ctype_isptr(s[1]->info) || ctype_isrefarray(s[1]->info))) {
1✔
1407
    TRef tr = sp[0]; sp[0] = sp[1]; sp[1] = tr;  /* Swap pointer and index. */
1✔
1408
    ctp = s[1];
1✔
1409
  } else {
1410
    return 0;
1411
  }
1412
  {
1413
    TRef tr = sp[1];
225✔
1414
    IRType t = tref_type(tr);
225✔
1415
    CTSize sz = lj_ctype_size(cts, ctype_cid(ctp->info));
225✔
1416
    CTypeID id;
225✔
1417
#if LJ_64
1418
    if (t == IRT_NUM || t == IRT_FLOAT)
225✔
1419
      tr = emitconv(tr, IRT_INTP, t, IRCONV_ANY);
205✔
1420
    else if (!(t == IRT_I64 || t == IRT_U64))
20✔
1421
      tr = emitconv(tr, IRT_INTP, IRT_INT,
40✔
1422
                    ((t - IRT_I8) & 1) ? 0 : IRCONV_SEXT);
1423
#else
1424
    if (!tref_typerange(sp[1], IRT_I8, IRT_U32)) {
1425
      tr = emitconv(tr, IRT_INTP, t,
1426
                    (t == IRT_NUM || t == IRT_FLOAT) ? IRCONV_ANY : 0);
1427
    }
1428
#endif
1429
    tr = emitir(IRT(IR_MUL, IRT_INTP), tr, lj_ir_kintp(J, sz));
225✔
1430
    tr = emitir(IRT(mm+(int)IR_ADD-(int)MM_add, IRT_PTR), sp[0], tr);
225✔
1431
    id = lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|ctype_cid(ctp->info)),
225✔
1432
                         CTSIZE_PTR);
1433
    return emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr);
225✔
1434
  }
1435
}
1436

1437
/* Record ctype arithmetic metamethods. */
1438
static TRef crec_arith_meta(jit_State *J, TRef *sp, CType **s, CTState *cts,
5✔
1439
                            RecordFFData *rd)
1440
{
1441
  cTValue *tv = NULL;
5✔
1442
  if (J->base[0]) {
5✔
1443
    if (tviscdata(&rd->argv[0])) {
5✔
1444
      CTypeID id = argv2cdata(J, J->base[0], &rd->argv[0])->ctypeid;
5✔
1445
      CType *ct = ctype_raw(cts, id);
5✔
1446
      if (ctype_isptr(ct->info)) id = ctype_cid(ct->info);
5✔
1447
      tv = lj_ctype_meta(cts, id, (MMS)rd->data);
5✔
1448
    }
1449
    if (!tv && J->base[1] && tviscdata(&rd->argv[1])) {
5✔
1450
      CTypeID id = argv2cdata(J, J->base[1], &rd->argv[1])->ctypeid;
×
1451
      CType *ct = ctype_raw(cts, id);
×
1452
      if (ctype_isptr(ct->info)) id = ctype_cid(ct->info);
×
1453
      tv = lj_ctype_meta(cts, id, (MMS)rd->data);
×
1454
    }
1455
  }
1456
  if (tv) {
5✔
1457
    if (tvisfunc(tv)) {
3✔
1458
      crec_tailcall(J, rd, tv);
3✔
1459
      return 0;
3✔
1460
    }  /* NYI: non-function metamethods. */
1461
  } else if ((MMS)rd->data == MM_eq) {  /* Fallback cdata pointer comparison. */
2✔
1462
    if (sp[0] && sp[1] && ctype_isnum(s[0]->info) == ctype_isnum(s[1]->info)) {
2✔
1463
      /* Assume true comparison. Fixup and emit pending guard later. */
1464
      lj_ir_set(J, IRTG(IR_EQ, IRT_PTR), sp[0], sp[1]);
×
1465
      J->postproc = LJ_POST_FIXGUARD;
×
1466
      return TREF_TRUE;
×
1467
    } else {
1468
      return TREF_FALSE;
1469
    }
1470
  }
1471
  lj_trace_err(J, LJ_TRERR_BADTYPE);
×
1472
  return 0;
1473
}
1474

1475
void LJ_FASTCALL recff_cdata_arith(jit_State *J, RecordFFData *rd)
75,062✔
1476
{
1477
  CTState *cts = ctype_cts(J->L);
75,062✔
1478
  MMS mm = (MMS)rd->data;
75,062✔
1479
  TRef sp[2];
75,062✔
1480
  CType *s[2];
75,062✔
1481
  MSize i;
75,062✔
1482
  for (i = 0; i < 2; i++) {
225,180✔
1483
    TRef tr = J->base[i];
150,124✔
1484
    CType *ct = ctype_get(cts, CTID_DOUBLE);
150,124✔
1485
    if (!tr) {
150,124✔
1486
      lj_trace_err(J, LJ_TRERR_BADTYPE);
×
1487
    } else if (tref_iscdata(tr)) {
150,124✔
1488
      CTypeID id = argv2cdata(J, tr, &rd->argv[i])->ctypeid;
84,229✔
1489
      IRType t;
84,229✔
1490
      ct = ctype_raw(cts, id);
84,229✔
1491
      t = crec_ct2irt(cts, ct);
84,229✔
1492
      if (ctype_isptr(ct->info)) {  /* Resolve pointer or reference. */
84,229✔
1493
        tr = emitir(IRT(IR_FLOAD, t), tr, IRFL_CDATA_PTR);
22✔
1494
        if (ctype_isref(ct->info)) {
22✔
1495
          ct = ctype_rawchild(cts, ct);
×
1496
          t = crec_ct2irt(cts, ct);
×
1497
        }
1498
      } else if (t == IRT_I64 || t == IRT_U64) {
84,207✔
1499
        tr = emitir(IRT(IR_FLOAD, t), tr, IRFL_CDATA_INT64);
83,957✔
1500
        lj_needsplit(J);
83,957✔
1501
        goto ok;
83,957✔
1502
      } else if (t == IRT_INT || t == IRT_U32) {
250✔
1503
        tr = emitir(IRT(IR_FLOAD, t), tr, IRFL_CDATA_INT);
23✔
1504
        if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
23✔
1505
        goto ok;
23✔
1506
      } else if (ctype_isfunc(ct->info)) {
227✔
1507
        CTypeID id0 = i ? ctype_typeid(cts, s[0]) : 0;
1✔
1508
        tr = emitir(IRT(IR_FLOAD, IRT_PTR), tr, IRFL_CDATA_PTR);
1✔
1509
        ct = ctype_get(cts,
1✔
1510
          lj_ctype_intern(cts, CTINFO(CT_PTR, CTALIGN_PTR|id), CTSIZE_PTR));
1✔
1511
        if (i) {
1✔
1512
          s[0] = ctype_get(cts, id0);  /* cts->tab may have been reallocated. */
1✔
1513
        }
1514
        goto ok;
1✔
1515
      } else {
1516
        tr = emitir(IRT(IR_ADD, IRT_PTR), tr, lj_ir_kintp(J, sizeof(GCcdata)));
226✔
1517
      }
1518
      if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
248✔
1519
      if (ctype_isnum(ct->info)) {
248✔
1520
        if (t == IRT_CDATA) {
1✔
1521
          tr = 0;
1522
        } else {
1523
          if (t == IRT_I64 || t == IRT_U64) lj_needsplit(J);
1✔
1524
          tr = emitir(IRT(IR_XLOAD, t), tr, 0);
1✔
1525
        }
1526
      }
1527
    } else if (tref_isnil(tr)) {
65,895✔
1528
      if (!(mm == MM_len || mm == MM_eq || mm == MM_lt || mm == MM_le))
4✔
1529
        lj_trace_err(J, LJ_TRERR_BADTYPE);
2✔
1530
      tr = lj_ir_kptr(J, NULL);
2✔
1531
      ct = ctype_get(cts, CTID_P_VOID);
2✔
1532
    } else if (tref_isinteger(tr)) {
65,891✔
1533
      ct = ctype_get(cts, CTID_INT32);
21,811✔
1534
    } else if (tref_isstr(tr)) {
44,080✔
1535
      TRef tr2 = J->base[1-i];
12✔
1536
      CTypeID id = argv2cdata(J, tr2, &rd->argv[1-i])->ctypeid;
12✔
1537
      ct = ctype_raw(cts, id);
12✔
1538
      if (ctype_isenum(ct->info)) {  /* Match string against enum constant. */
12✔
1539
        GCstr *str = strV(&rd->argv[i]);
6✔
1540
        CTSize ofs;
6✔
1541
        CType *cct = lj_ctype_getfield(cts, ct, str, &ofs);
6✔
1542
        if (cct && ctype_isconstval(cct->info)) {
6✔
1543
          /* Specialize to the name of the enum constant. */
1544
          emitir(IRTG(IR_EQ, IRT_STR), tr, lj_ir_kstr(J, str));
6✔
1545
          ct = ctype_child(cts, cct);
6✔
1546
          tr = lj_ir_kint(J, (int32_t)ofs);
6✔
1547
        } else {  /* Interpreter will throw or return false. */
1548
          lj_trace_err(J, LJ_TRERR_BADTYPE);
×
1549
        }
1550
      } else if (ctype_isptr(ct->info)) {
6✔
1551
        tr = emitir(IRT(IR_ADD, IRT_PTR), tr, lj_ir_kintp(J, sizeof(GCstr)));
2✔
1552
      } else {
1553
        lj_trace_err(J, LJ_TRERR_BADTYPE);
4✔
1554
      }
1555
    } else if (!tref_isnum(tr)) {
44,068✔
1556
      tr = 0;
1✔
1557
      ct = ctype_get(cts, CTID_P_VOID);
1✔
1558
    }
1559
  ok:
44,067✔
1560
    s[i] = ct;
150,118✔
1561
    sp[i] = tr;
150,118✔
1562
  }
1563
  {
1564
    TRef tr;
75,056✔
1565
    if ((mm == MM_len || mm == MM_concat ||
75,056✔
1566
         (!(tr = crec_arith_int64(J, sp, s, mm)) &&
75,055✔
1567
          !(tr = crec_arith_ptr(J, sp, s, mm)))) &&
239✔
1568
        !(tr = crec_arith_meta(J, sp, s, cts, rd)))
5✔
1569
      return;
3✔
1570
    J->base[0] = tr;
75,053✔
1571
    /* Fixup cdata comparisons, too. Avoids some cdata escapes. */
1572
    if (J->postproc == LJ_POST_FIXGUARD && frame_iscont(J->L->base-1) &&
75,053✔
1573
        !irt_isguard(J->guardemit)) {
34,519✔
1574
      const BCIns *pc = frame_contpc(J->L->base-1) - 1;
22,098✔
1575
      if (bc_op(*pc) <= BC_ISNEP) {
22,098✔
1576
        J2G(J)->tmptv.u64 = (uint64_t)(uintptr_t)pc;
22,098✔
1577
        J->postproc = LJ_POST_FIXCOMP;
22,098✔
1578
      }
1579
    }
1580
  }
1581
}
1582

1583
/* -- C library namespace metamethods ------------------------------------- */
1584

1585
void LJ_FASTCALL recff_clib_index(jit_State *J, RecordFFData *rd)
12,440✔
1586
{
1587
  CTState *cts = ctype_ctsG(J2G(J));
12,440✔
1588
  if (tref_isudata(J->base[0]) && tref_isstr(J->base[1]) &&
12,440✔
1589
      udataV(&rd->argv[0])->udtype == UDTYPE_FFI_CLIB) {
12,440✔
1590
    CLibrary *cl = (CLibrary *)uddata(udataV(&rd->argv[0]));
12,440✔
1591
    GCstr *name = strV(&rd->argv[1]);
12,440✔
1592
    CType *ct;
12,440✔
1593
    CTypeID id = lj_ctype_getname(cts, &ct, name, CLNS_INDEX);
12,440✔
1594
    cTValue *tv = lj_tab_getstr(cl->cache, name);
12,440✔
1595
    rd->nres = rd->data;
12,440✔
1596
    if (id && tv && !tvisnil(tv)) {
12,440✔
1597
      /* Specialize to the symbol name and make the result a constant. */
1598
      emitir(IRTG(IR_EQ, IRT_STR), J->base[1], lj_ir_kstr(J, name));
12,439✔
1599
      if (ctype_isconstval(ct->info)) {
12,439✔
1600
        if (ct->size >= 0x80000000u &&
×
1601
            (ctype_child(cts, ct)->info & CTF_UNSIGNED))
×
1602
          J->base[0] = lj_ir_knum(J, (lua_Number)(uint32_t)ct->size);
×
1603
        else
1604
          J->base[0] = lj_ir_kint(J, (int32_t)ct->size);
×
1605
      } else if (ctype_isextern(ct->info)) {
12,439✔
1606
        CTypeID sid = ctype_cid(ct->info);
×
1607
        void *sp = *(void **)cdataptr(cdataV(tv));
×
1608
        TRef ptr;
×
1609
        ct = ctype_raw(cts, sid);
×
1610
        if (LJ_64 && !checkptr32(sp))
×
1611
          ptr = lj_ir_kintp(J, (uintptr_t)sp);
×
1612
        else
1613
          ptr = lj_ir_kptr(J, sp);
×
1614
        if (rd->data) {
×
1615
          J->base[0] = crec_tv_ct(J, ct, sid, ptr);
×
1616
        } else {
1617
          J->needsnap = 1;
×
1618
          crec_ct_tv(J, ct, ptr, J->base[2], &rd->argv[2]);
×
1619
        }
1620
      } else {
1621
        J->base[0] = lj_ir_kgc(J, obj2gco(cdataV(tv)), IRT_CDATA);
12,439✔
1622
      }
1623
    } else {
1624
      lj_trace_err(J, LJ_TRERR_NOCACHE);
1✔
1625
    }
1626
  }  /* else: interpreter will throw. */
1627
}
12,439✔
1628

1629
/* -- FFI library functions ----------------------------------------------- */
1630

1631
static TRef crec_toint(jit_State *J, CTState *cts, TRef sp, TValue *sval)
217✔
1632
{
1633
  return crec_ct_tv(J, ctype_get(cts, CTID_INT32), 0, sp, sval);
217✔
1634
}
1635

1636
void LJ_FASTCALL recff_ffi_new(jit_State *J, RecordFFData *rd)
22,513✔
1637
{
1638
  crec_alloc(J, rd, argv2ctype(J, J->base[0], &rd->argv[0]));
22,513✔
1639
}
22,511✔
1640

1641
void LJ_FASTCALL recff_ffi_errno(jit_State *J, RecordFFData *rd)
1✔
1642
{
1643
  UNUSED(rd);
1✔
1644
  if (J->base[0])
1✔
1645
    lj_trace_err(J, LJ_TRERR_NYICALL);
×
1646
  J->base[0] = lj_ir_call(J, IRCALL_lj_vm_errno);
1✔
1647
}
1✔
1648

1649
void LJ_FASTCALL recff_ffi_string(jit_State *J, RecordFFData *rd)
208✔
1650
{
1651
  CTState *cts = ctype_ctsG(J2G(J));
208✔
1652
  TRef tr = J->base[0];
208✔
1653
  if (tr) {
208✔
1654
    TRef trlen = J->base[1];
208✔
1655
    if (!tref_isnil(trlen)) {
208✔
1656
      trlen = crec_toint(J, cts, trlen, &rd->argv[1]);
207✔
1657
      tr = crec_ct_tv(J, ctype_get(cts, CTID_P_CVOID), 0, tr, &rd->argv[0]);
207✔
1658
    } else {
1659
      tr = crec_ct_tv(J, ctype_get(cts, CTID_P_CCHAR), 0, tr, &rd->argv[0]);
1✔
1660
      trlen = lj_ir_call(J, IRCALL_strlen, tr);
1✔
1661
    }
1662
    J->base[0] = emitir(IRT(IR_XSNEW, IRT_STR), tr, trlen);
208✔
1663
  }  /* else: interpreter will throw. */
1664
}
208✔
1665

1666
void LJ_FASTCALL recff_ffi_copy(jit_State *J, RecordFFData *rd)
5✔
1667
{
1668
  CTState *cts = ctype_ctsG(J2G(J));
5✔
1669
  TRef trdst = J->base[0], trsrc = J->base[1], trlen = J->base[2];
5✔
1670
  if (trdst && trsrc && (trlen || tref_isstr(trsrc))) {
5✔
1671
    trdst = crec_ct_tv(J, ctype_get(cts, CTID_P_VOID), 0, trdst, &rd->argv[0]);
5✔
1672
    trsrc = crec_ct_tv(J, ctype_get(cts, CTID_P_CVOID), 0, trsrc, &rd->argv[1]);
5✔
1673
    if (trlen) {
5✔
1674
      trlen = crec_toint(J, cts, trlen, &rd->argv[2]);
4✔
1675
    } else {
1676
      trlen = emitir(IRTI(IR_FLOAD), J->base[1], IRFL_STR_LEN);
1✔
1677
      trlen = emitir(IRTI(IR_ADD), trlen, lj_ir_kint(J, 1));
1✔
1678
    }
1679
    rd->nres = 0;
5✔
1680
    crec_copy(J, trdst, trsrc, trlen, NULL);
5✔
1681
  }  /* else: interpreter will throw. */
1682
}
5✔
1683

1684
void LJ_FASTCALL recff_ffi_fill(jit_State *J, RecordFFData *rd)
3✔
1685
{
1686
  CTState *cts = ctype_ctsG(J2G(J));
3✔
1687
  TRef trdst = J->base[0], trlen = J->base[1], trfill = J->base[2];
3✔
1688
  if (trdst && trlen) {
3✔
1689
    CTSize step = 1;
3✔
1690
    if (tviscdata(&rd->argv[0])) {  /* Get alignment of original destination. */
3✔
1691
      CTSize sz;
3✔
1692
      CType *ct = ctype_raw(cts, cdataV(&rd->argv[0])->ctypeid);
3✔
1693
      if (ctype_isptr(ct->info))
3✔
1694
        ct = ctype_rawchild(cts, ct);
2✔
1695
      step = (1u<<ctype_align(lj_ctype_info(cts, ctype_typeid(cts, ct), &sz)));
3✔
1696
    }
1697
    trdst = crec_ct_tv(J, ctype_get(cts, CTID_P_VOID), 0, trdst, &rd->argv[0]);
3✔
1698
    trlen = crec_toint(J, cts, trlen, &rd->argv[1]);
3✔
1699
    if (trfill)
3✔
1700
      trfill = crec_toint(J, cts, trfill, &rd->argv[2]);
3✔
1701
    else
1702
      trfill = lj_ir_kint(J, 0);
×
1703
    rd->nres = 0;
3✔
1704
    crec_fill(J, trdst, trlen, trfill, step);
3✔
1705
  }  /* else: interpreter will throw. */
1706
}
3✔
1707

1708
void LJ_FASTCALL recff_ffi_typeof(jit_State *J, RecordFFData *rd)
18✔
1709
{
1710
  if (tref_iscdata(J->base[0])) {
18✔
1711
    TRef trid = lj_ir_kint(J, argv2ctype(J, J->base[0], &rd->argv[0]));
1✔
1712
    J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA),
1✔
1713
                        lj_ir_kint(J, CTID_CTYPEID), trid);
1714
  } else {
1715
    setfuncV(J->L, &J->errinfo, J->fn);
17✔
1716
    lj_trace_err_info(J, LJ_TRERR_NYIFFU);
17✔
1717
  }
1718
}
1✔
1719

1720
void LJ_FASTCALL recff_ffi_istype(jit_State *J, RecordFFData *rd)
3✔
1721
{
1722
  argv2ctype(J, J->base[0], &rd->argv[0]);
3✔
1723
  if (tref_iscdata(J->base[1])) {
3✔
1724
    argv2ctype(J, J->base[1], &rd->argv[1]);
3✔
1725
    J->postproc = LJ_POST_FIXBOOL;
3✔
1726
    J->base[0] = TREF_TRUE;
3✔
1727
  } else {
1728
    J->base[0] = TREF_FALSE;
×
1729
  }
1730
}
3✔
1731

1732
void LJ_FASTCALL recff_ffi_abi(jit_State *J, RecordFFData *rd)
11✔
1733
{
1734
  if (tref_isstr(J->base[0])) {
11✔
1735
    /* Specialize to the ABI string to make the boolean result a constant. */
1736
    emitir(IRTG(IR_EQ, IRT_STR), J->base[0], lj_ir_kstr(J, strV(&rd->argv[0])));
11✔
1737
    J->postproc = LJ_POST_FIXBOOL;
11✔
1738
    J->base[0] = TREF_TRUE;
11✔
1739
  } else {
1740
    lj_trace_err(J, LJ_TRERR_BADTYPE);
×
1741
  }
1742
}
11✔
1743

1744
/* Record ffi.sizeof(), ffi.alignof(), ffi.offsetof(). */
1745
void LJ_FASTCALL recff_ffi_xof(jit_State *J, RecordFFData *rd)
5✔
1746
{
1747
  CTypeID id = argv2ctype(J, J->base[0], &rd->argv[0]);
5✔
1748
  if (rd->data == FF_ffi_sizeof) {
5✔
1749
    CType *ct = lj_ctype_rawref(ctype_ctsG(J2G(J)), id);
5✔
1750
    if (ctype_isvltype(ct->info))
5✔
1751
      lj_trace_err(J, LJ_TRERR_BADTYPE);
4✔
1752
  } else if (rd->data == FF_ffi_offsetof) {  /* Specialize to the field name. */
×
1753
    if (!tref_isstr(J->base[1]))
×
1754
      lj_trace_err(J, LJ_TRERR_BADTYPE);
×
1755
    emitir(IRTG(IR_EQ, IRT_STR), J->base[1], lj_ir_kstr(J, strV(&rd->argv[1])));
×
1756
    rd->nres = 3;  /* Just in case. */
×
1757
  }
1758
  J->postproc = LJ_POST_FIXCONST;
1✔
1759
  J->base[0] = J->base[1] = J->base[2] = TREF_NIL;
1✔
1760
}
1✔
1761

1762
void LJ_FASTCALL recff_ffi_gc(jit_State *J, RecordFFData *rd)
7✔
1763
{
1764
  argv2cdata(J, J->base[0], &rd->argv[0]);
7✔
1765
  if (!J->base[1])
7✔
1766
    lj_trace_err(J, LJ_TRERR_BADTYPE);
×
1767
  crec_finalizer(J, J->base[0], J->base[1], &rd->argv[1]);
7✔
1768
}
7✔
1769

1770
/* -- 64 bit bit.* library functions -------------------------------------- */
1771

1772
/* Determine bit operation type from argument type. */
1773
static CTypeID crec_bit64_type(CTState *cts, cTValue *tv)
1774
{
1775
  if (tviscdata(tv)) {
1776
    CType *ct = lj_ctype_rawref(cts, cdataV(tv)->ctypeid);
1777
    if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
1778
    if ((ct->info & (CTMASK_NUM|CTF_BOOL|CTF_FP|CTF_UNSIGNED)) ==
1779
        CTINFO(CT_NUM, CTF_UNSIGNED) && ct->size == 8)
1780
      return CTID_UINT64;  /* Use uint64_t, since it has the highest rank. */
1781
    return CTID_INT64;  /* Otherwise use int64_t. */
1782
  }
1783
  return 0;  /* Use regular 32 bit ops. */
1784
}
1785

1786
static TRef crec_bit64_arg(jit_State *J, CType *d, TRef sp, TValue *sval)
81✔
1787
{
1788
  if (LJ_UNLIKELY(tref_isstr(sp))) {
81✔
1789
    if (lj_strscan_num(strV(sval), sval)) {
1✔
1790
      sp = emitir(IRTG(IR_STRTO, IRT_NUM), sp, 0);
1✔
1791
    }  /* else: interpreter will throw. */
1792
  }
1793
  return crec_ct_tv(J, d, 0, sp, sval);
81✔
1794
}
1795

1796
void LJ_FASTCALL recff_bit64_tobit(jit_State *J, RecordFFData *rd)
×
1797
{
1798
  CTState *cts = ctype_ctsG(J2G(J));
×
1799
  TRef tr = crec_bit64_arg(J, ctype_get(cts, CTID_INT64),
×
1800
                           J->base[0], &rd->argv[0]);
×
1801
  if (!tref_isinteger(tr))
×
1802
    tr = emitconv(tr, IRT_INT, tref_type(tr), 0);
×
1803
  J->base[0] = tr;
×
1804
}
×
1805

1806
int LJ_FASTCALL recff_bit64_unary(jit_State *J, RecordFFData *rd)
4✔
1807
{
1808
  CTState *cts = ctype_ctsG(J2G(J));
4✔
1809
  CTypeID id = crec_bit64_type(cts, &rd->argv[0]);
4✔
1810
  if (id) {
4✔
1811
    TRef tr = crec_bit64_arg(J, ctype_get(cts, id), J->base[0], &rd->argv[0]);
2✔
1812
    tr = emitir(IRT(rd->data, id-CTID_INT64+IRT_I64), tr, 0);
2✔
1813
    J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr);
2✔
1814
    return 1;
2✔
1815
  }
1816
  return 0;
1817
}
1818

1819
int LJ_FASTCALL recff_bit64_nary(jit_State *J, RecordFFData *rd)
43,572✔
1820
{
1821
  CTState *cts = ctype_ctsG(J2G(J));
43,572✔
1822
  CTypeID id = 0;
43,572✔
1823
  MSize i;
43,572✔
1824
  for (i = 0; J->base[i] != 0; i++) {
130,716✔
1825
    CTypeID aid = crec_bit64_type(cts, &rd->argv[i]);
87,144✔
1826
    if (id < aid) id = aid;  /* Determine highest type rank of all arguments. */
87,144✔
1827
  }
1828
  if (id) {
43,572✔
1829
    CType *ct = ctype_get(cts, id);
29✔
1830
    uint32_t ot = IRT(rd->data, id-CTID_INT64+IRT_I64);
29✔
1831
    TRef tr = crec_bit64_arg(J, ct, J->base[0], &rd->argv[0]);
29✔
1832
    for (i = 1; J->base[i] != 0; i++) {
87✔
1833
      TRef tr2 = crec_bit64_arg(J, ct, J->base[i], &rd->argv[i]);
29✔
1834
      tr = emitir(ot, tr, tr2);
29✔
1835
    }
1836
    J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr);
29✔
1837
    return 1;
29✔
1838
  }
1839
  return 0;
1840
}
1841

1842
int LJ_FASTCALL recff_bit64_shift(jit_State *J, RecordFFData *rd)
57✔
1843
{
1844
  CTState *cts = ctype_ctsG(J2G(J));
57✔
1845
  CTypeID id;
57✔
1846
  TRef tsh = 0;
57✔
1847
  if (J->base[0] && tref_iscdata(J->base[1])) {
57✔
1848
    tsh = crec_bit64_arg(J, ctype_get(cts, CTID_INT64),
6✔
1849
                         J->base[1], &rd->argv[1]);
3✔
1850
    if (!tref_isinteger(tsh))
3✔
1851
      tsh = emitconv(tsh, IRT_INT, tref_type(tsh), 0);
3✔
1852
    J->base[1] = tsh;
3✔
1853
  }
1854
  id = crec_bit64_type(cts, &rd->argv[0]);
57✔
1855
  if (id) {
57✔
1856
    TRef tr = crec_bit64_arg(J, ctype_get(cts, id), J->base[0], &rd->argv[0]);
18✔
1857
    uint32_t op = rd->data;
18✔
1858
    if (!tsh) tsh = lj_opt_narrow_tobit(J, J->base[1]);
18✔
1859
    if (!(op < IR_BROL ? LJ_TARGET_MASKSHIFT : LJ_TARGET_MASKROT) &&
18✔
1860
        !tref_isk(tsh))
1861
      tsh = emitir(IRTI(IR_BAND), tsh, lj_ir_kint(J, 63));
1862
#ifdef LJ_TARGET_UNIFYROT
1863
      if (op == (LJ_TARGET_UNIFYROT == 1 ? IR_BROR : IR_BROL)) {
1864
        op = LJ_TARGET_UNIFYROT == 1 ? IR_BROL : IR_BROR;
1865
        tsh = emitir(IRTI(IR_NEG), tsh, tsh);
1866
      }
1867
#endif
1868
    tr = emitir(IRT(op, id-CTID_INT64+IRT_I64), tr, tsh);
18✔
1869
    J->base[0] = emitir(IRTG(IR_CNEWI, IRT_CDATA), lj_ir_kint(J, id), tr);
18✔
1870
    return 1;
18✔
1871
  }
1872
  return 0;
1873
}
1874

1875
TRef recff_bit64_tohex(jit_State *J, RecordFFData *rd, TRef hdr)
14✔
1876
{
1877
  CTState *cts = ctype_ctsG(J2G(J));
14✔
1878
  CTypeID id = crec_bit64_type(cts, &rd->argv[0]);
14✔
1879
  TRef tr, trsf = J->base[1];
14✔
1880
  SFormat sf = (STRFMT_UINT|STRFMT_T_HEX);
14✔
1881
  int32_t n;
14✔
1882
  if (trsf) {
14✔
1883
    CTypeID id2 = 0;
14✔
1884
    n = (int32_t)lj_carith_check64(J->L, 2, &id2);
14✔
1885
    if (id2)
14✔
1886
      trsf = crec_bit64_arg(J, ctype_get(cts, CTID_INT32), trsf, &rd->argv[1]);
×
1887
    else
1888
      trsf = lj_opt_narrow_tobit(J, trsf);
14✔
1889
    emitir(IRTGI(IR_EQ), trsf, lj_ir_kint(J, n));  /* Specialize to n. */
14✔
1890
  } else {
1891
    n = id ? 16 : 8;
×
1892
  }
1893
  if (n < 0) { n = (int32_t)(~n+1u); sf |= STRFMT_F_UPPER; }
14✔
1894
  if ((uint32_t)n > 254) n = 254;
14✔
1895
  sf |= ((SFormat)((n+1)&255) << STRFMT_SH_PREC);
14✔
1896
  if (id) {
14✔
1897
    tr = crec_bit64_arg(J, ctype_get(cts, id), J->base[0], &rd->argv[0]);
×
1898
    if (n < 16)
×
1899
      tr = emitir(IRT(IR_BAND, IRT_U64), tr,
×
1900
                  lj_ir_kint64(J, ((uint64_t)1 << 4*n)-1));
1901
  } else {
1902
    tr = lj_opt_narrow_tobit(J, J->base[0]);
14✔
1903
    if (n < 8)
14✔
1904
      tr = emitir(IRTI(IR_BAND), tr, lj_ir_kint(J, (int32_t)((1u << 4*n)-1)));
12✔
1905
    tr = emitconv(tr, IRT_U64, IRT_INT, 0);  /* No sign-extension. */
14✔
1906
    lj_needsplit(J);
14✔
1907
  }
1908
  return lj_ir_call(J, IRCALL_lj_strfmt_putfxint, hdr, lj_ir_kint(J, sf), tr);
14✔
1909
}
1910

1911
/* -- Miscellaneous library functions ------------------------------------- */
1912

1913
void LJ_FASTCALL lj_crecord_tonumber(jit_State *J, RecordFFData *rd)
639✔
1914
{
1915
  CTState *cts = ctype_ctsG(J2G(J));
639✔
1916
  CType *d, *ct = lj_ctype_rawref(cts, cdataV(&rd->argv[0])->ctypeid);
639✔
1917
  if (ctype_isenum(ct->info)) ct = ctype_child(cts, ct);
639✔
1918
  if (ctype_isnum(ct->info) || ctype_iscomplex(ct->info)) {
639✔
1919
    if (ctype_isinteger_or_bool(ct->info) && ct->size <= 4 &&
638✔
1920
        !(ct->size == 4 && (ct->info & CTF_UNSIGNED)))
3✔
1921
      d = ctype_get(cts, CTID_INT32);
2✔
1922
    else
1923
      d = ctype_get(cts, CTID_DOUBLE);
636✔
1924
    J->base[0] = crec_ct_tv(J, d, 0, J->base[0], &rd->argv[0]);
638✔
1925
  } else {
1926
    /* Specialize to the ctype that couldn't be converted. */
1927
    argv2cdata(J, J->base[0], &rd->argv[0]);
1✔
1928
    J->base[0] = TREF_NIL;
1✔
1929
  }
1930
}
639✔
1931

1932
#undef IR
1933
#undef emitir
1934
#undef emitconv
1935

1936
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc