aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Pall <mike>2010-12-08 02:11:18 +0100
committerMike Pall <mike>2010-12-08 02:11:18 +0100
commitd778680098f630f4c74324f2fad27bb088d29d78 (patch)
tree1514d4a4fecb1ca6bc28281b6be713e7bbdb0f34 /src
parent2a917a9d83baa1fade1b5704705759e8840fa237 (diff)
downloadluajit-d778680098f630f4c74324f2fad27bb088d29d78.tar.gz
luajit-d778680098f630f4c74324f2fad27bb088d29d78.tar.bz2
luajit-d778680098f630f4c74324f2fad27bb088d29d78.zip
FFI: Record cdata index operations (preliminary, lots of NYI cases).
Diffstat (limited to 'src')
-rw-r--r--src/Makefile2
-rw-r--r--src/Makefile.dep16
-rw-r--r--src/lib_ffi.c4
-rw-r--r--src/lj_cdata.c2
-rw-r--r--src/lj_crecord.c391
-rw-r--r--src/lj_crecord.h19
-rw-r--r--src/lj_ffrecord.c9
-rw-r--r--src/lj_ffrecord.h7
-rw-r--r--src/lj_ir.h8
-rw-r--r--src/lj_iropt.h6
-rw-r--r--src/lj_traceerr.h3
-rw-r--r--src/ljamalg.c1
12 files changed, 449 insertions, 19 deletions
diff --git a/src/Makefile b/src/Makefile
index 9dcb8a36..ba94f2fa 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -326,7 +326,7 @@ LJCORE_O= lj_gc.o lj_err.o lj_char.o lj_bc.o lj_obj.o \
326 lj_lex.o lj_parse.o \ 326 lj_lex.o lj_parse.o \
327 lj_ir.o lj_opt_mem.o lj_opt_fold.o lj_opt_narrow.o \ 327 lj_ir.o lj_opt_mem.o lj_opt_fold.o lj_opt_narrow.o \
328 lj_opt_dce.o lj_opt_loop.o \ 328 lj_opt_dce.o lj_opt_loop.o \
329 lj_mcode.o lj_snap.o lj_record.o lj_ffrecord.o \ 329 lj_mcode.o lj_snap.o lj_record.o lj_crecord.o lj_ffrecord.o \
330 lj_asm.o lj_trace.o lj_gdbjit.o \ 330 lj_asm.o lj_trace.o lj_gdbjit.o \
331 lj_ctype.o lj_cdata.o lj_cconv.o lj_cparse.o \ 331 lj_ctype.o lj_cdata.o lj_cconv.o lj_cparse.o \
332 lj_lib.o lj_alloc.o lib_aux.o \ 332 lj_lib.o lj_alloc.o lib_aux.o \
diff --git a/src/Makefile.dep b/src/Makefile.dep
index 9cfbd587..fd90ff4a 100644
--- a/src/Makefile.dep
+++ b/src/Makefile.dep
@@ -63,6 +63,10 @@ lj_char.o: lj_char.c lj_char.h lj_def.h lua.h luaconf.h
63lj_cparse.o: lj_cparse.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 63lj_cparse.o: lj_cparse.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
64 lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_ctype.h lj_cparse.h lj_frame.h \ 64 lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_ctype.h lj_cparse.h lj_frame.h \
65 lj_bc.h lj_vm.h lj_char.h 65 lj_bc.h lj_vm.h lj_char.h
66lj_crecord.o: lj_crecord.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
67 lj_err.h lj_errmsg.h lj_str.h lj_ctype.h lj_gc.h lj_cconv.h lj_ir.h \
68 lj_jit.h lj_iropt.h lj_trace.h lj_dispatch.h lj_bc.h lj_traceerr.h \
69 lj_ffrecord.h lj_crecord.h
66lj_ctype.o: lj_ctype.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 70lj_ctype.o: lj_ctype.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
67 lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_ctype.h 71 lj_gc.h lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_ctype.h
68lj_dispatch.o: lj_dispatch.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 72lj_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 \
74lj_ffrecord.o: lj_ffrecord.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 78lj_ffrecord.o: lj_ffrecord.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
75 lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_frame.h lj_bc.h lj_ff.h \ 79 lj_err.h lj_errmsg.h lj_str.h lj_tab.h lj_frame.h lj_bc.h lj_ff.h \
76 lj_ffdef.h lj_ir.h lj_jit.h lj_iropt.h lj_trace.h lj_dispatch.h \ 80 lj_ffdef.h lj_ir.h lj_jit.h lj_iropt.h lj_trace.h lj_dispatch.h \
77 lj_traceerr.h lj_record.h lj_vm.h lj_recdef.h 81 lj_traceerr.h lj_record.h lj_ffrecord.h lj_crecord.h lj_vm.h lj_recdef.h
78lj_func.o: lj_func.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \ 82lj_func.o: lj_func.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h lj_gc.h \
79 lj_func.h lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h lj_bc.h \ 83 lj_func.h lj_trace.h lj_jit.h lj_ir.h lj_dispatch.h lj_bc.h \
80 lj_traceerr.h lj_vm.h 84 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 \
151 lj_cconv.c lj_cparse.c lj_cparse.h lj_lib.c lj_lib.h lj_ir.c lj_iropt.h \ 155 lj_cconv.c lj_cparse.c lj_cparse.h lj_lib.c lj_lib.h lj_ir.c lj_iropt.h \
152 lj_opt_mem.c lj_opt_fold.c lj_folddef.h lj_opt_narrow.c lj_opt_dce.c \ 156 lj_opt_mem.c lj_opt_fold.c lj_folddef.h lj_opt_narrow.c lj_opt_dce.c \
153 lj_opt_loop.c lj_snap.h lj_mcode.c lj_mcode.h lj_snap.c lj_target.h \ 157 lj_opt_loop.c lj_snap.h lj_mcode.c lj_mcode.h lj_snap.c lj_target.h \
154 lj_target_*.h lj_record.c lj_record.h lj_ffrecord.h lj_ffrecord.c \ 158 lj_target_*.h lj_record.c lj_record.h lj_ffrecord.h lj_crecord.c \
155 lj_recdef.h lj_asm.c lj_asm.h lj_trace.c lj_gdbjit.h lj_gdbjit.c \ 159 lj_crecord.h lj_ffrecord.c lj_recdef.h lj_asm.c lj_asm.h lj_trace.c \
156 lj_alloc.c lib_aux.c lib_base.c lualib.h lj_libdef.h lib_math.c \ 160 lj_gdbjit.h lj_gdbjit.c lj_alloc.c lib_aux.c lib_base.c lualib.h \
157 lib_string.c lib_table.c lib_io.c lib_os.c lib_package.c lib_debug.c \ 161 lj_libdef.h lib_math.c lib_string.c lib_table.c lib_io.c lib_os.c \
158 lib_bit.c lib_jit.c lib_ffi.c lib_init.c 162 lib_package.c lib_debug.c lib_bit.c lib_jit.c lib_ffi.c lib_init.c
159luajit.o: luajit.c lua.h luaconf.h lauxlib.h lualib.h luajit.h lj_arch.h 163luajit.o: luajit.c lua.h luaconf.h lauxlib.h lualib.h luajit.h lj_arch.h
diff --git a/src/lib_ffi.c b/src/lib_ffi.c
index 70d4c4a3..c526df40 100644
--- a/src/lib_ffi.c
+++ b/src/lib_ffi.c
@@ -78,7 +78,7 @@ static void *ffi_checkptr(lua_State *L, int narg, CTypeID id)
78 78
79#define LJLIB_MODULE_ffi_meta 79#define LJLIB_MODULE_ffi_meta
80 80
81LJLIB_CF(ffi_meta___index) 81LJLIB_CF(ffi_meta___index) LJLIB_REC(cdata_index 0)
82{ 82{
83 CTState *cts = ctype_cts(L); 83 CTState *cts = ctype_cts(L);
84 CTInfo qual = 0; 84 CTInfo qual = 0;
@@ -92,7 +92,7 @@ LJLIB_CF(ffi_meta___index)
92 return 1; 92 return 1;
93} 93}
94 94
95LJLIB_CF(ffi_meta___newindex) 95LJLIB_CF(ffi_meta___newindex) LJLIB_REC(cdata_index 1)
96{ 96{
97 CTState *cts = ctype_cts(L); 97 CTState *cts = ctype_cts(L);
98 CTInfo qual = 0; 98 CTInfo qual = 0;
diff --git a/src/lj_cdata.c b/src/lj_cdata.c
index 15ba2dde..29380cbc 100644
--- a/src/lj_cdata.c
+++ b/src/lj_cdata.c
@@ -89,7 +89,7 @@ CType *lj_cdata_index(CTState *cts, GCcdata *cd, cTValue *key, uint8_t **pp,
89 ptrdiff_t idx = LJ_64 ? (ptrdiff_t)numV(key) : 89 ptrdiff_t idx = LJ_64 ? (ptrdiff_t)numV(key) :
90 (ptrdiff_t)lj_num2int(numV(key)); 90 (ptrdiff_t)lj_num2int(numV(key));
91 if (ctype_ispointer(ct->info)) { 91 if (ctype_ispointer(ct->info)) {
92 CTSize sz = lj_ctype_size(cts, ctype_cid(ct->info)); 92 CTSize sz = lj_ctype_size(cts, ctype_cid(ct->info)); /* Element size. */
93 if (sz != CTSIZE_INVALID) { 93 if (sz != CTSIZE_INVALID) {
94 if (ctype_isptr(ct->info)) 94 if (ctype_isptr(ct->info))
95 p = (uint8_t *)cdata_getptr(p, ct->size); 95 p = (uint8_t *)cdata_getptr(p, ct->size);
diff --git a/src/lj_crecord.c b/src/lj_crecord.c
new file mode 100644
index 00000000..b197d4b4
--- /dev/null
+++ b/src/lj_crecord.c
@@ -0,0 +1,391 @@
1/*
2** Trace recorder for C data operations.
3** Copyright (C) 2005-2010 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_str.h"
15#include "lj_ctype.h"
16#include "lj_cconv.h"
17#include "lj_ir.h"
18#include "lj_jit.h"
19#include "lj_iropt.h"
20#include "lj_trace.h"
21#include "lj_ffrecord.h"
22#include "lj_crecord.h"
23#include "lj_dispatch.h"
24
25/* Some local macros to save typing. Undef'd at the end. */
26#define IR(ref) (&J->cur.ir[(ref)])
27
28/* Pass IR on to next optimization in chain (FOLD). */
29#define emitir(ot, a, b) (lj_ir_set(J, (ot), (a), (b)), lj_opt_fold(J))
30
31/* -- C type checks ------------------------------------------------------- */
32
33static GCcdata *argv2cdata(jit_State *J, TRef trcd, TValue *o)
34{
35 GCcdata *cd;
36 TRef trtypeid;
37 if (!tviscdata(o))
38 lj_trace_err(J, LJ_TRERR_BADTYPE);
39 cd = cdataV(o);
40 /* Specialize to the CTypeID. */
41 trtypeid = emitir(IRT(IR_FLOAD, IRT_U16), trcd, IRFL_CDATA_TYPEID);
42 emitir(IRTG(IR_EQ, IRT_INT), trtypeid, lj_ir_kint(J, (int32_t)cd->typeid));
43 return cd;
44}
45
46/* -- Convert C type to C type -------------------------------------------- */
47
48/*
49** This code mirrors the code in lj_cconv.c. It performs the same steps
50** for the trace recorder that lj_cconv.c does for the interpreter.
51**
52** One major difference is that we can get away with much fewer checks
53** here. E.g. checks for casts, constness or correct types can often be
54** omitted, even if they might fail. The interpreter subsequently throws
55** an error, which aborts the trace.
56**
57** All operations are specialized to their C types, so the on-trace
58** outcome must be the same as the outcome in the interpreter. If the
59** interpreter doesn't throw an error, then the trace is correct, too.
60** Care must be taken not to generate invalid (temporary) IR or to
61** trigger asserts.
62*/
63
64/* Convert CType to IRType. */
65static IRType crec_ct2irt(CType *ct)
66{
67 if (LJ_LIKELY(ctype_isnum(ct->info))) {
68 if ((ct->info & CTF_FP)) {
69 if (ct->size == sizeof(double)) /* NYI: float IRType. */
70 return IRT_NUM;
71 } else {
72 uint32_t b = lj_fls(ct->size);
73 if (b <= 3)
74 return IRT_I8 + 2*b + ((ct->info & CTF_UNSIGNED) ? 1 : 0);
75 }
76 } else if (ctype_isptr(ct->info)) {
77 return (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32;
78 }
79 return IRT_CDATA;
80}
81
82static void crec_ct_ct(jit_State *J, CType *d, CType *s, TRef dp, TRef sp)
83{
84 CTState *cts = ctype_ctsG(J2G(J));
85 CTSize dsize = d->size, ssize = s->size;
86 CTInfo dinfo = d->info, sinfo = s->info;
87 IRType dt = crec_ct2irt(d);
88
89 if (ctype_type(dinfo) > CT_MAYCONVERT || ctype_type(sinfo) > CT_MAYCONVERT)
90 goto err_conv;
91
92 /*
93 ** Note: Unlike lj_cconv_ct_ct(), sp holds the _value_ of pointers and
94 ** numbers up to 8 bytes. Otherwise sp holds a pointer.
95 */
96
97 switch (cconv_idx2(dinfo, sinfo)) {
98 /* Destination is a bool. */
99 case CCX(B, B):
100 goto xstore; /* Source operand is already normalized. */
101 case CCX(B, I):
102 case CCX(B, P):
103 case CCX(B, F):
104 case CCX(B, C):
105 case CCX(B, A):
106 /* NYI: specialize to the result of a comparison against 0. */
107 goto err_nyi;
108
109 /* Destination is an integer. */
110 case CCX(I, B):
111 case CCX(I, I):
112 conv_I_I:
113 lua_assert(ssize >= 4);
114 if (dsize > 8 || ssize > 8) goto err_nyi;
115 if (dsize > ssize) /* Zero-extend or sign-extend 32 to 64 bit integer. */
116 sp = emitir(IRT(IR_TOI64, dt), sp,
117 (sinfo&CTF_UNSIGNED) ? IRTOINT_ZEXT64 : IRTOINT_SEXT64);
118 xstore:
119 emitir(IRT(IR_XSTORE, dt), dp, sp);
120 break;
121 case CCX(I, F):
122 conv_I_F:
123 if (dsize > 8 || ssize != sizeof(double)) goto err_nyi;
124 if (dsize == 8) {
125 if (dt == IRT_U64) goto err_nyi;
126 sp = emitir(IRT(IR_TOI64, dt), sp, IRTOINT_TRUNCI64);
127 } else {
128 sp = emitir(IRTI(IR_TOINT), sp, IRTOINT_ANY); /* NYI: should truncate. */
129 }
130 goto xstore;
131 case CCX(I, C):
132 if (ssize != 2*sizeof(double)) goto err_nyi;
133 sp = emitir(IRT(IR_XLOAD, IRT_NUM), sp, 0); /* Load re. */
134 s = ctype_child(cts, s);
135 sinfo = s->info;
136 ssize = s->size;
137 goto conv_I_F; /* Just convert re. */
138 case CCX(I, P):
139 case CCX(I, A):
140 sinfo = CTINFO(CT_NUM, CTF_UNSIGNED);
141 ssize = CTSIZE_PTR;
142 /*
143 ** Note: Overriding the size is also required for pointers, since
144 ** crec_ct_tv passes IRT_P32/IRT_P64 independently of the C type size.
145 ** This avoids unnecessary zero-extensions on x64.
146 */
147 goto conv_I_I;
148
149 /* Destination is a floating-point number. */
150 case CCX(F, B):
151 case CCX(F, I):
152 conv_F_I:
153 if (dsize != sizeof(double) || ssize > 4) goto err_nyi;
154 if (ssize == 4 && (sinfo & CTF_UNSIGNED)) goto err_nyi;
155 sp = emitir(IRTI(IR_TONUM), sp, 0);
156 goto xstore;
157 case CCX(F, F):
158 conv_F_F:
159 if (dsize != sizeof(double) || ssize != sizeof(double)) goto err_nyi;
160 goto xstore;
161 case CCX(F, C):
162 if (ssize != 2*sizeof(double)) goto err_nyi;
163 sp = emitir(IRT(IR_XLOAD, IRT_NUM), sp, 0); /* Load re. */
164 s = ctype_child(cts, s);
165 sinfo = s->info;
166 ssize = s->size;
167 goto conv_F_F; /* Ignore im, and convert from re. */
168
169 /* Destination is a complex number. */
170 case CCX(C, I):
171 case CCX(C, F):
172 d = ctype_child(cts, d);
173 dinfo = d->info;
174 dsize = d->size;
175 if (dsize != sizeof(double)) goto err_nyi;
176 { /* Clear im. */
177 TRef dpim = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, dsize));
178 emitir(IRT(IR_XSTORE, IRT_NUM), dpim, lj_ir_knum(J, 0));
179 }
180 /* Convert to re. */
181 if ((sinfo & CTF_FP)) goto conv_F_F; else goto conv_F_I;
182
183 case CCX(C, C):
184 d = ctype_child(cts, d);
185 dinfo = d->info;
186 dsize = d->size;
187 if (dsize != sizeof(double)) goto err_nyi;
188 {
189 TRef spim = emitir(IRT(IR_ADD, IRT_PTR), sp, lj_ir_kintp(J, dsize));
190 TRef re = emitir(IRT(IR_XLOAD, IRT_NUM), sp, 0);
191 TRef im = emitir(IRT(IR_XLOAD, IRT_NUM), spim, 0);
192 TRef dpim = emitir(IRT(IR_ADD, IRT_PTR), dp, lj_ir_kintp(J, dsize));
193 emitir(IRT(IR_XSTORE, IRT_NUM), dp, re);
194 emitir(IRT(IR_XSTORE, IRT_NUM), dpim, im);
195 }
196 break;
197
198 /* Destination is a vector. */
199 case CCX(V, I):
200 case CCX(V, F):
201 case CCX(V, C):
202 case CCX(V, V):
203 goto err_nyi;
204
205 /* Destination is a pointer. */
206 case CCX(P, P):
207 /* Note: ok on x64, since all 32 bit ops clear the upper part of the reg. */
208 goto xstore;
209 case CCX(P, A):
210 case CCX(P, S):
211 ssize = CTSIZE_PTR;
212 sinfo = CTINFO(CT_NUM, CTF_UNSIGNED);
213 /* fallthrough */
214 case CCX(P, I):
215 dinfo = CTINFO(CT_NUM, CTF_UNSIGNED);
216 goto conv_I_I;
217 case CCX(P, F):
218 dinfo = CTINFO(CT_NUM, CTF_UNSIGNED);
219 goto conv_I_F;
220
221 /* Destination is an array. */
222 case CCX(A, A):
223 goto err_nyi;
224
225 /* Destination is a struct/union. */
226 case CCX(S, S):
227 goto err_nyi;
228
229 default:
230 err_conv:
231 err_nyi:
232 lj_trace_err(J, LJ_TRERR_NYICONV);
233 break;
234 }
235}
236
237/* -- Convert C type to TValue (load) ------------------------------------- */
238
239static TRef crec_tv_ct(jit_State *J, CType *s, CTypeID sid, TRef sp)
240{
241 CTInfo sinfo = s->info;
242 lua_assert(!ctype_isenum(sinfo));
243 if (ctype_isnum(sinfo)) {
244 IRType t = crec_ct2irt(s);
245 if ((sinfo & CTF_BOOL))
246 lj_trace_err(J, LJ_TRERR_NYICONV); /* NYI: specialize to the result. */
247 if (t == IRT_CDATA) goto copyval;
248 if (t == IRT_U32) lj_trace_err(J, LJ_TRERR_NYICONV);
249 return emitir(IRT(IR_XLOAD, t), sp, 0);
250 } else if (ctype_isrefarray(sinfo) || ctype_isstruct(sinfo)) {
251 /* Create reference. */
252 UNUSED(sid); lj_trace_err(J, LJ_TRERR_NYICONV);
253 return 0;
254 } else {
255 copyval: /* Copy value. */
256 lj_trace_err(J, LJ_TRERR_NYICONV);
257 return 0;
258 }
259}
260
261/* -- Convert TValue to C type (store) ------------------------------------ */
262
263static void crec_ct_tv(jit_State *J, CType *d, TRef dp, TRef sp, TValue *sval)
264{
265 CTState *cts = ctype_ctsG(J2G(J));
266 CTypeID sid = CTID_P_VOID;
267 CType *s;
268 if (LJ_LIKELY(tref_isinteger(sp))) {
269 sid = CTID_INT32;
270 } else if (tref_isnum(sp)) {
271 sid = CTID_DOUBLE;
272 } else if (tref_isbool(sp)) {
273 sp = lj_ir_kint(J, tref_istrue(sp) ? 1 : 0);
274 sid = CTID_BOOL;
275 } else if (tref_isnil(sp)) {
276 sp = lj_ir_knull(J, IRT_PTR);
277 } else if (tref_isudata(sp)) {
278 sp = emitir(IRT(IR_ADD, IRT_P32), sp, lj_ir_kint(J, sizeof(GCcdata)));
279 } else { /* NYI: tref_isstr(sp), tref_istab(sp), tref_islightud(sp). */
280 sid = argv2cdata(J, sp, sval)->typeid;
281 s = ctype_raw(cts, sid);
282 if (ctype_isptr(s->info)) {
283 IRType t = (LJ_64 && s->size == 8) ? IRT_P64 : IRT_P32;
284 sp = emitir(IRT(IR_FLOAD, t), sp, IRFL_CDATA_DATA);
285 if (ctype_isref(s->info))
286 s = ctype_rawchild(cts, s);
287 else
288 goto doconv; /* The pointer value was loaded, don't load number. */
289 } else {
290 sp = emitir(IRT(IR_ADD, IRT_P32), sp, lj_ir_kint(J, sizeof(GCcdata)));
291 }
292 if (ctype_isenum(s->info)) s = ctype_child(cts, s);
293 if (ctype_isnum(s->info)) { /* Load number value. */
294 IRType t = crec_ct2irt(s);
295 if (t != IRT_CDATA) sp = emitir(IRT(IR_XLOAD, t), sp, 0);
296 }
297 goto doconv;
298 }
299 s = ctype_get(cts, sid);
300doconv:
301 crec_ct_ct(J, d, s, dp, sp);
302}
303
304/* -- C data metamethods -------------------------------------------------- */
305
306void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd)
307{
308 TRef idx, ptr = J->base[0];
309 ptrdiff_t ofs = sizeof(GCcdata);
310 GCcdata *cd = argv2cdata(J, ptr, &rd->argv[0]);
311 CTState *cts = ctype_ctsG(J2G(J));
312 CType *ct = ctype_raw(cts, cd->typeid);
313 CTypeID sid = 0;
314
315 /* Resolve pointer or reference for cdata object. */
316 if (ctype_isptr(ct->info)) {
317 IRType t = (LJ_64 && ct->size == 8) ? IRT_P64 : IRT_P32;
318 if (ctype_isref(ct->info)) ct = ctype_rawchild(cts, ct);
319 ptr = emitir(IRT(IR_FLOAD, t), ptr, IRFL_CDATA_DATA);
320 ofs = 0;
321 }
322
323 idx = J->base[1];
324 if (tref_isnumber(idx)) {
325 /* The size of a ptrdiff_t is target-specific. */
326#if LJ_64
327 idx = emitir(IRT(IR_TOI64, IRT_INTP), idx,
328 tref_isinteger(idx) ? IRTOINT_SEXT64 : IRTOINT_TRUNCI64);
329#else
330 if (!tref_isinteger(idx))
331 idx = emitir(IRT(IR_TOINT, IRT_INTP), idx, IRTOINT_ANY);
332#endif
333 if (ctype_ispointer(ct->info)) {
334 sid = ctype_cid(ct->info);
335 idx = emitir(IRT(IR_MUL, IRT_INTP), idx,
336 lj_ir_kintp(J, lj_ctype_size(cts, sid)));
337 ptr = emitir(IRT(IR_ADD, IRT_PTR), idx, ptr);
338 }
339 } else if (tref_isstr(idx)) {
340 GCstr *name = strV(&rd->argv[1]);
341 if (ctype_isstruct(ct->info)) {
342 CTSize fofs;
343 CType *fct = lj_ctype_getfield(cts, ct, name, &fofs);
344 if (fct) {
345 if (ctype_isconstval(fct->info)) {
346 if (fct->size >= 0x80000000u &&
347 (ctype_child(cts, fct)->info & CTF_UNSIGNED)) {
348 J->base[0] = lj_ir_knum(J, (lua_Number)(uint32_t)fct->size);
349 return;
350 }
351 J->base[0] = lj_ir_kint(J, (int32_t)fct->size);
352 return; /* Interpreter will throw for newindex. */
353 } else if (ctype_isbitfield(fct->info)) {
354 lj_trace_err(J, LJ_TRERR_NYICONV);
355 } else {
356 lua_assert(ctype_isfield(fct->info));
357 sid = ctype_cid(fct->info);
358 }
359 ofs += (ptrdiff_t)fofs;
360 }
361 } else if (ctype_iscomplex(ct->info)) {
362 if (strdata(name)[0] == 'i') ofs += (ct->size >> 1);
363 sid = ctype_cid(ct->info);
364 }
365 }
366 if (!sid) lj_trace_err(J, LJ_TRERR_BADTYPE);
367
368 if (ofs)
369 ptr = emitir(IRT(IR_ADD, IRT_PTR), ptr, lj_ir_kintp(J, ofs));
370
371 /* Resolve reference for field. */
372 ct = ctype_get(cts, sid);
373 if (ctype_isref(ct->info))
374 ptr = emitir(IRT(IR_XLOAD, IRT_PTR), ptr, 0);
375
376 /* Skip attributes and enums. */
377 while (ctype_isattrib(ct->info) || ctype_isenum(ct->info))
378 ct = ctype_child(cts, ct);
379
380 if (rd->data == 0) { /* __index metamethod. */
381 J->base[0] = crec_tv_ct(J, ct, sid, ptr);
382 } else { /* __newindex metamethod. */
383 rd->nres = 0;
384 crec_ct_tv(J, ct, ptr, J->base[2], &rd->argv[2]);
385 }
386}
387
388#undef IR
389#undef emitir
390
391#endif
diff --git a/src/lj_crecord.h b/src/lj_crecord.h
new file mode 100644
index 00000000..eb57cbb0
--- /dev/null
+++ b/src/lj_crecord.h
@@ -0,0 +1,19 @@
1/*
2** Trace recorder for C data operations.
3** Copyright (C) 2005-2010 Mike Pall. See Copyright Notice in luajit.h
4*/
5
6#ifndef _LJ_CRECORD_H
7#define _LJ_CRECORD_H
8
9#include "lj_obj.h"
10#include "lj_jit.h"
11#include "lj_ffrecord.h"
12
13#if LJ_HASJIT && LJ_HASFFI
14LJ_FUNC void LJ_FASTCALL recff_cdata_index(jit_State *J, RecordFFData *rd);
15#else
16#define recff_cdata_index recff_nyi
17#endif
18
19#endif
diff --git a/src/lj_ffrecord.c b/src/lj_ffrecord.c
index 0dccaa65..0fb48504 100644
--- a/src/lj_ffrecord.c
+++ b/src/lj_ffrecord.c
@@ -21,6 +21,8 @@
21#include "lj_iropt.h" 21#include "lj_iropt.h"
22#include "lj_trace.h" 22#include "lj_trace.h"
23#include "lj_record.h" 23#include "lj_record.h"
24#include "lj_ffrecord.h"
25#include "lj_crecord.h"
24#include "lj_dispatch.h" 26#include "lj_dispatch.h"
25#include "lj_vm.h" 27#include "lj_vm.h"
26 28
@@ -55,13 +57,6 @@
55** in a common handler. 57** in a common handler.
56*/ 58*/
57 59
58/* Data used by handlers to record a fast function. */
59typedef struct RecordFFData {
60 TValue *argv; /* Runtime argument values. */
61 ptrdiff_t nres; /* Number of returned results (defaults to 1). */
62 uint32_t data; /* Per-ffid auxiliary data (opcode, literal etc.). */
63} RecordFFData;
64
65/* Type of handler to record a fast function. */ 60/* Type of handler to record a fast function. */
66typedef void (LJ_FASTCALL *RecordFunc)(jit_State *J, RecordFFData *rd); 61typedef void (LJ_FASTCALL *RecordFunc)(jit_State *J, RecordFFData *rd);
67 62
diff --git a/src/lj_ffrecord.h b/src/lj_ffrecord.h
index f13f5c33..2333b528 100644
--- a/src/lj_ffrecord.h
+++ b/src/lj_ffrecord.h
@@ -10,6 +10,13 @@
10#include "lj_jit.h" 10#include "lj_jit.h"
11 11
12#if LJ_HASJIT 12#if LJ_HASJIT
13/* Data used by handlers to record a fast function. */
14typedef struct RecordFFData {
15 TValue *argv; /* Runtime argument values. */
16 ptrdiff_t nres; /* Number of returned results (defaults to 1). */
17 uint32_t data; /* Per-ffid auxiliary data (opcode, literal etc.). */
18} RecordFFData;
19
13LJ_FUNC int32_t lj_ffrecord_select_mode(jit_State *J, TRef tr, TValue *tv); 20LJ_FUNC int32_t lj_ffrecord_select_mode(jit_State *J, TRef tr, TValue *tv);
14LJ_FUNC void lj_ffrecord_func(jit_State *J); 21LJ_FUNC void lj_ffrecord_func(jit_State *J);
15#endif 22#endif
diff --git a/src/lj_ir.h b/src/lj_ir.h
index 72c37343..7814df34 100644
--- a/src/lj_ir.h
+++ b/src/lj_ir.h
@@ -184,7 +184,9 @@ IRFPMDEF(FPMENUM)
184 _(TAB_NOMM, offsetof(GCtab, nomm)) \ 184 _(TAB_NOMM, offsetof(GCtab, nomm)) \
185 _(UDATA_META, offsetof(GCudata, metatable)) \ 185 _(UDATA_META, offsetof(GCudata, metatable)) \
186 _(UDATA_UDTYPE, offsetof(GCudata, udtype)) \ 186 _(UDATA_UDTYPE, offsetof(GCudata, udtype)) \
187 _(UDATA_FILE, sizeof(GCudata)) 187 _(UDATA_FILE, sizeof(GCudata)) \
188 _(CDATA_TYPEID, offsetof(GCcdata, typeid)) \
189 _(CDATA_DATA, sizeof(GCcdata))
188 190
189typedef enum { 191typedef enum {
190#define FLENUM(name, ofs) IRFL_##name, 192#define FLENUM(name, ofs) IRFL_##name,
@@ -342,8 +344,9 @@ typedef enum {
342 IRT_U64, 344 IRT_U64,
343 /* There is room for 10 more types. */ 345 /* There is room for 10 more types. */
344 346
345 /* Native pointer type. */ 347 /* Native pointer type and the corresponding integer type. */
346 IRT_PTR = LJ_64 ? IRT_P64 : IRT_P32, 348 IRT_PTR = LJ_64 ? IRT_P64 : IRT_P32,
349 IRT_INTP = LJ_64 ? IRT_I64 : IRT_INT,
347 350
348 /* Additional flags. */ 351 /* Additional flags. */
349 IRT_MARK = 0x20, /* Marker for misc. purposes. */ 352 IRT_MARK = 0x20, /* Marker for misc. purposes. */
@@ -485,6 +488,7 @@ typedef uint32_t TRef;
485#define tref_istrue(tr) (tref_istype((tr), IRT_TRUE)) 488#define tref_istrue(tr) (tref_istype((tr), IRT_TRUE))
486#define tref_isstr(tr) (tref_istype((tr), IRT_STR)) 489#define tref_isstr(tr) (tref_istype((tr), IRT_STR))
487#define tref_isfunc(tr) (tref_istype((tr), IRT_FUNC)) 490#define tref_isfunc(tr) (tref_istype((tr), IRT_FUNC))
491#define tref_iscdata(tr) (tref_istype((tr), IRT_CDATA))
488#define tref_istab(tr) (tref_istype((tr), IRT_TAB)) 492#define tref_istab(tr) (tref_istype((tr), IRT_TAB))
489#define tref_isudata(tr) (tref_istype((tr), IRT_UDATA)) 493#define tref_isudata(tr) (tref_istype((tr), IRT_UDATA))
490#define tref_isnum(tr) (tref_istype((tr), IRT_NUM)) 494#define tref_isnum(tr) (tref_istype((tr), IRT_NUM))
diff --git a/src/lj_iropt.h b/src/lj_iropt.h
index ce8b564f..f3d243e0 100644
--- a/src/lj_iropt.h
+++ b/src/lj_iropt.h
@@ -49,6 +49,12 @@ LJ_FUNC TRef lj_ir_kptr(jit_State *J, void *ptr);
49LJ_FUNC TRef lj_ir_knull(jit_State *J, IRType t); 49LJ_FUNC TRef lj_ir_knull(jit_State *J, IRType t);
50LJ_FUNC TRef lj_ir_kslot(jit_State *J, TRef key, IRRef slot); 50LJ_FUNC TRef lj_ir_kslot(jit_State *J, TRef key, IRRef slot);
51 51
52#if LJ_64
53#define lj_ir_kintp(J, k) lj_ir_kint64(J, (uint64_t)(k))
54#else
55#define lj_ir_kintp(J, k) lj_ir_kint(J, (int32_t)(k))
56#endif
57
52static LJ_AINLINE TRef lj_ir_knum(jit_State *J, lua_Number n) 58static LJ_AINLINE TRef lj_ir_knum(jit_State *J, lua_Number n)
53{ 59{
54 TValue tv; 60 TValue tv;
diff --git a/src/lj_traceerr.h b/src/lj_traceerr.h
index 9bfdadc6..f1a86fcf 100644
--- a/src/lj_traceerr.h
+++ b/src/lj_traceerr.h
@@ -34,6 +34,9 @@ TREDEF(NOMM, "missing metamethod")
34TREDEF(IDXLOOP, "looping index lookup") 34TREDEF(IDXLOOP, "looping index lookup")
35TREDEF(NYITMIX, "NYI: mixed sparse/dense table") 35TREDEF(NYITMIX, "NYI: mixed sparse/dense table")
36 36
37/* Recording C data operations. */
38TREDEF(NYICONV, "NYI: unsupported C type conversion")
39
37/* Optimizations. */ 40/* Optimizations. */
38TREDEF(GFAIL, "guard would always fail") 41TREDEF(GFAIL, "guard would always fail")
39TREDEF(PHIOV, "too many PHIs") 42TREDEF(PHIOV, "too many PHIs")
diff --git a/src/ljamalg.c b/src/ljamalg.c
index 1d060960..edcae247 100644
--- a/src/ljamalg.c
+++ b/src/ljamalg.c
@@ -54,6 +54,7 @@
54#include "lj_mcode.c" 54#include "lj_mcode.c"
55#include "lj_snap.c" 55#include "lj_snap.c"
56#include "lj_record.c" 56#include "lj_record.c"
57#include "lj_crecord.c"
57#include "lj_ffrecord.c" 58#include "lj_ffrecord.c"
58#include "lj_asm.c" 59#include "lj_asm.c"
59#include "lj_trace.c" 60#include "lj_trace.c"