aboutsummaryrefslogtreecommitdiff
path: root/src/lj_crecord.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/lj_crecord.c391
1 files changed, 391 insertions, 0 deletions
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