Browse Source

FFI: Record cdata index operations (preliminary, lots of NYI cases).

Mike Pall 14 years ago
parent
commit
d778680098
12 changed files with 449 additions and 19 deletions
  1. 1 1
      src/Makefile
  2. 10 6
      src/Makefile.dep
  3. 2 2
      src/lib_ffi.c
  4. 1 1
      src/lj_cdata.c
  5. 391 0
      src/lj_crecord.c
  6. 19 0
      src/lj_crecord.h
  7. 2 7
      src/lj_ffrecord.c
  8. 7 0
      src/lj_ffrecord.h
  9. 6 2
      src/lj_ir.h
  10. 6 0
      src/lj_iropt.h
  11. 3 0
      src/lj_traceerr.h
  12. 1 0
      src/ljamalg.c

+ 1 - 1
src/Makefile

@@ -326,7 +326,7 @@ LJCORE_O= lj_gc.o lj_err.o lj_char.o lj_bc.o lj_obj.o \
 	  lj_lex.o lj_parse.o \
 	  lj_ir.o lj_opt_mem.o lj_opt_fold.o lj_opt_narrow.o \
 	  lj_opt_dce.o lj_opt_loop.o \
-	  lj_mcode.o lj_snap.o lj_record.o lj_ffrecord.o \
+	  lj_mcode.o lj_snap.o lj_record.o lj_crecord.o lj_ffrecord.o \
 	  lj_asm.o lj_trace.o lj_gdbjit.o \
 	  lj_ctype.o lj_cdata.o lj_cconv.o lj_cparse.o \
 	  lj_lib.o lj_alloc.o lib_aux.o \

+ 10 - 6
src/Makefile.dep

@@ -63,6 +63,10 @@ lj_char.o: lj_char.c lj_char.h lj_def.h lua.h luaconf.h
 lj_cparse.o: lj_cparse.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
  lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_ctype.h lj_cparse.h lj_frame.h \
  lj_bc.h lj_vm.h lj_char.h
+lj_crecord.o: lj_crecord.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
+ lj_err.h lj_errmsg.h lj_str.h lj_ctype.h lj_gc.h lj_cconv.h lj_ir.h \
+ lj_jit.h lj_iropt.h lj_trace.h lj_dispatch.h lj_bc.h lj_traceerr.h \
+ lj_ffrecord.h lj_crecord.h
 lj_ctype.o: lj_ctype.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
  lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_ctype.h
 lj_dispatch.o: lj_dispatch.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
@@ -74,7 +78,7 @@ lj_err.o: lj_err.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_err.h \
 lj_ffrecord.o: lj_ffrecord.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
  lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_frame.h lj_bc.h lj_ff.h \
  lj_ffdef.h lj_ir.h lj_jit.h lj_iropt.h lj_trace.h lj_dispatch.h \
- lj_traceerr.h lj_record.h lj_vm.h lj_recdef.h
+ lj_traceerr.h lj_record.h lj_ffrecord.h lj_crecord.h lj_vm.h lj_recdef.h
 lj_func.o: lj_func.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
  lj_func.h lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h lj_bc.h \
  lj_traceerr.h lj_vm.h
@@ -151,9 +155,9 @@ ljamalg.o: ljamalg.c lua.h luaconf.h lauxlib.h lj_gc.c lj_obj.h lj_def.h \
  lj_cconv.c lj_cparse.c lj_cparse.h lj_lib.c lj_lib.h lj_ir.c lj_iropt.h \
  lj_opt_mem.c lj_opt_fold.c lj_folddef.h lj_opt_narrow.c lj_opt_dce.c \
  lj_opt_loop.c lj_snap.h lj_mcode.c lj_mcode.h lj_snap.c lj_target.h \
- lj_target_*.h lj_record.c lj_record.h lj_ffrecord.h lj_ffrecord.c \
- lj_recdef.h lj_asm.c lj_asm.h lj_trace.c lj_gdbjit.h lj_gdbjit.c \
- lj_alloc.c lib_aux.c lib_base.c lualib.h lj_libdef.h lib_math.c \
- lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c \
- lib_bit.c lib_jit.c lib_ffi.c lib_init.c
+ lj_target_*.h lj_record.c lj_record.h lj_ffrecord.h lj_crecord.c \
+ lj_crecord.h lj_ffrecord.c lj_recdef.h lj_asm.c lj_asm.h lj_trace.c \
+ lj_gdbjit.h lj_gdbjit.c lj_alloc.c lib_aux.c lib_base.c lualib.h \
+ lj_libdef.h lib_math.c lib_string.c lib_table.c lib_io.c lib_os.c \
+ lib_package.c lib_debug.c lib_bit.c lib_jit.c lib_ffi.c lib_init.c
 luajit.o: luajit.c lua.h luaconf.h lauxlib.h lualib.h luajit.h lj_arch.h

+ 2 - 2
src/lib_ffi.c

@@ -78,7 +78,7 @@ static void *ffi_checkptr(lua_State *L, int narg, CTypeID id)
 
 #define LJLIB_MODULE_ffi_meta
 
-LJLIB_CF(ffi_meta___index)
+LJLIB_CF(ffi_meta___index)	LJLIB_REC(cdata_index 0)
 {
   CTState *cts = ctype_cts(L);
   CTInfo qual = 0;
@@ -92,7 +92,7 @@ LJLIB_CF(ffi_meta___index)
   return 1;
 }
 
-LJLIB_CF(ffi_meta___newindex)
+LJLIB_CF(ffi_meta___newindex)	LJLIB_REC(cdata_index 1)
 {
   CTState *cts = ctype_cts(L);
   CTInfo qual = 0;

+ 1 - 1
src/lj_cdata.c

@@ -89,7 +89,7 @@ CType *lj_cdata_index(CTState *cts, GCcdata *cd, cTValue *key, uint8_t **pp,
     ptrdiff_t idx = LJ_64 ? (ptrdiff_t)numV(key) :
 			    (ptrdiff_t)lj_num2int(numV(key));
     if (ctype_ispointer(ct->info)) {
-      CTSize sz = lj_ctype_size(cts, ctype_cid(ct->info));
+      CTSize sz = lj_ctype_size(cts, ctype_cid(ct->info));  /* Element size. */
       if (sz != CTSIZE_INVALID) {
 	if (ctype_isptr(ct->info))
 	  p = (uint8_t *)cdata_getptr(p, ct->size);

+ 391 - 0
src/lj_crecord.c

@@ -0,0 +1,391 @@
+/*
+** Trace recorder for C data operations.
+** Copyright (C) 2005-2010 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_ctype.h"
+#include "lj_cconv.h"
+#include "lj_ir.h"
+#include "lj_jit.h"
+#include "lj_iropt.h"
+#include "lj_trace.h"
+#include "lj_ffrecord.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))
+
+/* -- C type checks ------------------------------------------------------- */
+
+static GCcdata *argv2cdata(jit_State *J, TRef trcd, TValue *o)
+{
+  GCcdata *cd;
+  TRef trtypeid;
+  if (!tviscdata(o))
+    lj_trace_err(J, LJ_TRERR_BADTYPE);
+  cd = cdataV(o);
+  /* Specialize to the CTypeID. */
+  trtypeid = emitir(IRT(IR_FLOAD, IRT_U16), trcd, IRFL_CDATA_TYPEID);
+  emitir(IRTG(IR_EQ, IRT_INT), trtypeid, lj_ir_kint(J, (int32_t)cd->typeid));
+  return cd;
+}
+
+/* -- 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.
+*/
+
+/* Convert CType to IRType. */
+static IRType crec_ct2irt(CType *ct)
+{
+  if (LJ_LIKELY(ctype_isnum(ct->info))) {
+    if ((ct->info & CTF_FP)) {
+      if (ct->size == sizeof(double))  /* NYI: float IRType. */
+	return IRT_NUM;
+    } 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;
+  }
+  return IRT_CDATA;
+}
+
+static void crec_ct_ct(jit_State *J, CType *d, CType *s, TRef dp, TRef sp)
+{
+  CTState *cts = ctype_ctsG(J2G(J));
+  CTSize dsize = d->size, ssize = s->size;
+  CTInfo dinfo = d->info, sinfo = s->info;
+  IRType dt = crec_ct2irt(d);
+
+  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, P):
+  case CCX(B, F):
+  case CCX(B, C):
+  case CCX(B, A):
+    /* NYI: specialize to the result of a comparison against 0. */
+    goto err_nyi;
+
+  /* Destination is an integer. */
+  case CCX(I, B):
+  case CCX(I, I):
+  conv_I_I:
+    lua_assert(ssize >= 4);
+    if (dsize > 8 || ssize > 8) goto err_nyi;
+    if (dsize > ssize)  /* Zero-extend or sign-extend 32 to 64 bit integer. */
+      sp = emitir(IRT(IR_TOI64, dt), sp,
+		  (sinfo&CTF_UNSIGNED) ? IRTOINT_ZEXT64 : IRTOINT_SEXT64);
+  xstore:
+    emitir(IRT(IR_XSTORE, dt), dp, sp);
+    break;
+  case CCX(I, F):
+  conv_I_F:
+    if (dsize > 8 || ssize != sizeof(double)) goto err_nyi;
+    if (dsize == 8) {
+      if (dt == IRT_U64) goto err_nyi;
+      sp = emitir(IRT(IR_TOI64, dt), sp, IRTOINT_TRUNCI64);
+    } else {
+      sp = emitir(IRTI(IR_TOINT), sp, IRTOINT_ANY);  /* NYI: should truncate. */
+    }
+    goto xstore;
+  case CCX(I, C):
+    if (ssize != 2*sizeof(double)) goto err_nyi;
+    sp = emitir(IRT(IR_XLOAD, IRT_NUM), sp, 0);  /* Load re. */
+    s = ctype_child(cts, s);
+    sinfo = s->info;
+    ssize = s->size;
+    goto conv_I_F;  /* Just convert re. */
+  case CCX(I, P):
+  case CCX(I, A):
+    sinfo = CTINFO(CT_NUM, CTF_UNSIGNED);
+    ssize = CTSIZE_PTR;
+    /*
+    ** Note: Overriding the size is also required for pointers, since
+    ** crec_ct_tv passes IRT_P32/IRT_P64 independently of the C type size.
+    ** This avoids unnecessary zero-extensions on x64.
+    */
+    goto conv_I_I;
+
+  /* Destination is a floating-point number. */
+  case CCX(F, B):
+  case CCX(F, I):
+  conv_F_I:
+    if (dsize != sizeof(double) || ssize > 4) goto err_nyi;
+    if (ssize == 4 && (sinfo & CTF_UNSIGNED)) goto err_nyi;
+    sp = emitir(IRTI(IR_TONUM), sp, 0);
+    goto xstore;
+  case CCX(F, F):
+  conv_F_F:
+    if (dsize != sizeof(double) || ssize != sizeof(double)) goto err_nyi;
+    goto xstore;
+  case CCX(F, C):
+    if (ssize != 2*sizeof(double)) goto err_nyi;
+    sp = emitir(IRT(IR_XLOAD, IRT_NUM), sp, 0);  /* Load re. */
+    s = ctype_child(cts, s);
+    sinfo = s->info;
+    ssize = s->size;
+    goto conv_F_F;  /* Ignore im, and convert from re. */
+
+  /* Destination is a complex number. */
+  case CCX(C, I):
+  case CCX(C, F):
+    d = ctype_child(cts, d);
+    dinfo = d->info;
+    dsize = d->size;
+    if (dsize != sizeof(double)) goto err_nyi;
+    {  /* Clear im. */
+      TRef dpim = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, dsize));
+      emitir(IRT(IR_XSTORE, IRT_NUM), dpim, 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):
+    d = ctype_child(cts, d);
+    dinfo = d->info;
+    dsize = d->size;
+    if (dsize != sizeof(double)) goto err_nyi;
+    {
+      TRef spim = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, dsize));
+      TRef re = emitir(IRT(IR_XLOAD, IRT_NUM), sp, 0);
+      TRef im = emitir(IRT(IR_XLOAD, IRT_NUM), spim, 0);
+      TRef dpim = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, dsize));
+      emitir(IRT(IR_XSTORE, IRT_NUM), dp, re);
+      emitir(IRT(IR_XSTORE, IRT_NUM), dpim, 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):
+    /* Note: ok on x64, since all 32 bit ops clear the upper part of the reg. */
+    goto xstore;
+  case CCX(P, A):
+  case CCX(P, S):
+    ssize = CTSIZE_PTR;
+    sinfo = CTINFO(CT_NUM, CTF_UNSIGNED);
+    /* fallthrough */
+  case CCX(P, I):
+    dinfo = CTINFO(CT_NUM, CTF_UNSIGNED);
+    goto conv_I_I;
+  case CCX(P, F):
+    dinfo = CTINFO(CT_NUM, CTF_UNSIGNED);
+    goto conv_I_F;
+
+  /* Destination is an array. */
+  case CCX(A, A):
+    goto err_nyi;
+
+  /* Destination is a struct/union. */
+  case CCX(S, S):
+    goto err_nyi;
+
+  default:
+  err_conv:
+  err_nyi:
+    lj_trace_err(J, LJ_TRERR_NYICONV);
+    break;
+  }
+}
+
+/* -- Convert C type to TValue (load) ------------------------------------- */
+
+static TRef crec_tv_ct(jit_State *J, CType *s, CTypeID sid, TRef sp)
+{
+  CTInfo sinfo = s->info;
+  lua_assert(!ctype_isenum(sinfo));
+  if (ctype_isnum(sinfo)) {
+    IRType t = crec_ct2irt(s);
+    if ((sinfo & CTF_BOOL))
+      lj_trace_err(J, LJ_TRERR_NYICONV);  /* NYI: specialize to the result. */
+    if (t == IRT_CDATA) goto copyval;
+    if (t == IRT_U32) lj_trace_err(J, LJ_TRERR_NYICONV);
+    return emitir(IRT(IR_XLOAD, t), sp, 0);
+  } else if (ctype_isrefarray(sinfo) || ctype_isstruct(sinfo)) {
+    /* Create reference. */
+    UNUSED(sid); lj_trace_err(J, LJ_TRERR_NYICONV);
+    return 0;
+  } else {
+  copyval:  /* Copy value. */
+    lj_trace_err(J, LJ_TRERR_NYICONV);
+    return 0;
+  }
+}
+
+/* -- Convert TValue to C type (store) ------------------------------------ */
+
+static void crec_ct_tv(jit_State *J, CType *d, TRef dp, TRef sp, TValue *sval)
+{
+  CTState *cts = ctype_ctsG(J2G(J));
+  CTypeID sid = CTID_P_VOID;
+  CType *s;
+  if (LJ_LIKELY(tref_isinteger(sp))) {
+    sid = CTID_INT32;
+  } else if (tref_isnum(sp)) {
+    sid = CTID_DOUBLE;
+  } 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_knull(J, IRT_PTR);
+  } else if (tref_isudata(sp)) {
+    sp = emitir(IRT(IR_ADD, IRT_P32), sp, lj_ir_kint(J, sizeof(GCcdata)));
+  } else {  /* NYI: tref_isstr(sp), tref_istab(sp), tref_islightud(sp). */
+    sid = argv2cdata(J, sp, sval)->typeid;
+    s = ctype_raw(cts, sid);
+    if (ctype_isptr(s->info)) {
+      IRType t = (LJ_64 && s->size == 8) ? IRT_P64 : IRT_P32;
+      sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_DATA);
+      if (ctype_isref(s->info))
+	s = ctype_rawchild(cts, s);
+      else
+	goto doconv;  /* The pointer value was loaded, don't load number. */
+    } else {
+      sp = emitir(IRT(IR_ADD, IRT_P32), sp, lj_ir_kint(J, sizeof(GCcdata)));
+    }
+    if (ctype_isenum(s->info)) s = ctype_child(cts, s);
+    if (ctype_isnum(s->info)) {  /* Load number value. */
+      IRType t = crec_ct2irt(s);
+      if (t != IRT_CDATA) sp = emitir(IRT(IR_XLOAD, t), sp, 0);
+    }
+    goto doconv;
+  }
+  s = ctype_get(cts, sid);
+doconv:
+  crec_ct_ct(J, d, s, dp, sp);
+}
+
+/* -- C data metamethods -------------------------------------------------- */
+
+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->typeid);
+  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_DATA);
+    ofs = 0;
+  }
+
+  idx = J->base[1];
+  if (tref_isnumber(idx)) {
+    /* The size of a ptrdiff_t is target-specific. */
+#if LJ_64
+    idx = emitir(IRT(IR_TOI64, IRT_INTP), idx,
+		 tref_isinteger(idx) ? IRTOINT_SEXT64 : IRTOINT_TRUNCI64);
+#else
+    if (!tref_isinteger(idx))
+      idx = emitir(IRT(IR_TOINT, IRT_INTP), idx, IRTOINT_ANY);
+#endif
+    if (ctype_ispointer(ct->info)) {
+      sid = ctype_cid(ct->info);
+      idx = emitir(IRT(IR_MUL, IRT_INTP), idx,
+		   lj_ir_kintp(J, lj_ctype_size(cts, sid)));
+      ptr = emitir(IRT(IR_ADD, IRT_PTR), idx, ptr);
+    }
+  } else if (tref_isstr(idx)) {
+    GCstr *name = strV(&rd->argv[1]);
+    if (ctype_isstruct(ct->info)) {
+      CTSize fofs;
+      CType *fct = lj_ctype_getfield(cts, ct, name, &fofs);
+      if (fct) {
+	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 (strdata(name)[0] == 'i') ofs += (ct->size >> 1);
+      sid = ctype_cid(ct->info);
+    }
+  }
+  if (!sid) lj_trace_err(J, LJ_TRERR_BADTYPE);
+
+  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);
+
+  /* Skip attributes and enums. */
+  while (ctype_isattrib(ct->info) || ctype_isenum(ct->info))
+    ct = ctype_child(cts, ct);
+
+  if (rd->data == 0) {  /* __index metamethod. */
+    J->base[0] = crec_tv_ct(J, ct, sid, ptr);
+  } else {  /* __newindex metamethod. */
+    rd->nres = 0;
+    crec_ct_tv(J, ct, ptr, J->base[2], &rd->argv[2]);
+  }
+}
+
+#undef IR
+#undef emitir
+
+#endif

+ 19 - 0
src/lj_crecord.h

@@ -0,0 +1,19 @@
+/*
+** Trace recorder for C data operations.
+** Copyright (C) 2005-2010 Mike Pall. See Copyright Notice in luajit.h
+*/
+
+#ifndef _LJ_CRECORD_H
+#define _LJ_CRECORD_H
+
+#include "lj_obj.h"
+#include "lj_jit.h"
+#include "lj_ffrecord.h"
+
+#if LJ_HASJIT && LJ_HASFFI
+LJ_FUNC void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd);
+#else
+#define recff_cdata_index	recff_nyi
+#endif
+
+#endif

+ 2 - 7
src/lj_ffrecord.c

@@ -21,6 +21,8 @@
 #include "lj_iropt.h"
 #include "lj_trace.h"
 #include "lj_record.h"
+#include "lj_ffrecord.h"
+#include "lj_crecord.h"
 #include "lj_dispatch.h"
 #include "lj_vm.h"
 
@@ -55,13 +57,6 @@
 ** in a common handler.
 */
 
-/* Data used by handlers to record a fast function. */
-typedef struct RecordFFData {
-  TValue *argv;		/* Runtime argument values. */
-  ptrdiff_t nres;	/* Number of returned results (defaults to 1). */
-  uint32_t data;	/* Per-ffid auxiliary data (opcode, literal etc.). */
-} RecordFFData;
-
 /* Type of handler to record a fast function. */
 typedef void (LJ_FASTCALL *RecordFunc)(jit_State *J, RecordFFData *rd);
 

+ 7 - 0
src/lj_ffrecord.h

@@ -10,6 +10,13 @@
 #include "lj_jit.h"
 
 #if LJ_HASJIT
+/* Data used by handlers to record a fast function. */
+typedef struct RecordFFData {
+  TValue *argv;		/* Runtime argument values. */
+  ptrdiff_t nres;	/* Number of returned results (defaults to 1). */
+  uint32_t data;	/* Per-ffid auxiliary data (opcode, literal etc.). */
+} RecordFFData;
+
 LJ_FUNC int32_t lj_ffrecord_select_mode(jit_State *J, TRef tr, TValue *tv);
 LJ_FUNC void lj_ffrecord_func(jit_State *J);
 #endif

+ 6 - 2
src/lj_ir.h

@@ -184,7 +184,9 @@ IRFPMDEF(FPMENUM)
   _(TAB_NOMM,	offsetof(GCtab, nomm)) \
   _(UDATA_META,	offsetof(GCudata, metatable)) \
   _(UDATA_UDTYPE, offsetof(GCudata, udtype)) \
-  _(UDATA_FILE,	sizeof(GCudata))
+  _(UDATA_FILE,	sizeof(GCudata)) \
+  _(CDATA_TYPEID, offsetof(GCcdata, typeid)) \
+  _(CDATA_DATA, sizeof(GCcdata))
 
 typedef enum {
 #define FLENUM(name, ofs)	IRFL_##name,
@@ -342,8 +344,9 @@ typedef enum {
   IRT_U64,
   /* There is room for 10 more types. */
 
-  /* Native pointer type. */
+  /* Native pointer type and the corresponding integer type. */
   IRT_PTR = LJ_64 ? IRT_P64 : IRT_P32,
+  IRT_INTP = LJ_64 ? IRT_I64 : IRT_INT,
 
   /* Additional flags. */
   IRT_MARK = 0x20,	/* Marker for misc. purposes. */
@@ -485,6 +488,7 @@ typedef uint32_t TRef;
 #define tref_istrue(tr)		(tref_istype((tr), IRT_TRUE))
 #define tref_isstr(tr)		(tref_istype((tr), IRT_STR))
 #define tref_isfunc(tr)		(tref_istype((tr), IRT_FUNC))
+#define tref_iscdata(tr)	(tref_istype((tr), IRT_CDATA))
 #define tref_istab(tr)		(tref_istype((tr), IRT_TAB))
 #define tref_isudata(tr)	(tref_istype((tr), IRT_UDATA))
 #define tref_isnum(tr)		(tref_istype((tr), IRT_NUM))

+ 6 - 0
src/lj_iropt.h

@@ -49,6 +49,12 @@ LJ_FUNC TRef lj_ir_kptr(jit_State *J, void *ptr);
 LJ_FUNC TRef lj_ir_knull(jit_State *J, IRType t);
 LJ_FUNC TRef lj_ir_kslot(jit_State *J, TRef key, IRRef slot);
 
+#if LJ_64
+#define lj_ir_kintp(J, k)	lj_ir_kint64(J, (uint64_t)(k))
+#else
+#define lj_ir_kintp(J, k)	lj_ir_kint(J, (int32_t)(k))
+#endif
+
 static LJ_AINLINE TRef lj_ir_knum(jit_State *J, lua_Number n)
 {
   TValue tv;

+ 3 - 0
src/lj_traceerr.h

@@ -34,6 +34,9 @@ TREDEF(NOMM,	"missing metamethod")
 TREDEF(IDXLOOP,	"looping index lookup")
 TREDEF(NYITMIX,	"NYI: mixed sparse/dense table")
 
+/* Recording C data operations. */
+TREDEF(NYICONV,	"NYI: unsupported C type conversion")
+
 /* Optimizations. */
 TREDEF(GFAIL,	"guard would always fail")
 TREDEF(PHIOV,	"too many PHIs")

+ 1 - 0
src/ljamalg.c

@@ -54,6 +54,7 @@
 #include "lj_mcode.c"
 #include "lj_snap.c"
 #include "lj_record.c"
+#include "lj_crecord.c"
 #include "lj_ffrecord.c"
 #include "lj_asm.c"
 #include "lj_trace.c"