Debian Codegraph @

texk/web2c/luatexdir/luaffi/call_arm.dasc

raw
1 /* vim: ts=4 sw=4 sts=4 et tw=78
2 * Portions copyright (c) 2015-present, Facebook, Inc. All rights reserved.
3 * Portions copyright (c) 2011 James R. McKaskill.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
8 */
9 |.arch arm
10 |.actionlist build_actionlist
11 |.globalnames globnames
12 |.externnames extnames
13
14 #define JUMP_SIZE 8
15 #define MIN_BRANCH ((INT32_MIN) >> 8)
16 #define MAX_BRANCH ((INT32_MAX) >> 8)
17 #define BRANCH_OFF 4
18
19 static void compile_extern_jump(struct jit* jit, lua_State* L, cfunction func, uint8_t* code)
20 {
21 /* The jump code is the function pointer followed by a stub to call the
22 * function pointer. The stub exists so we can jump to functions with an
23 * offset greater than 32MB.
24 *
25 * Note we have to manually set this up since there are commands buffered
26 * in the jit state.
27 */
28 *(cfunction*) code = func;
29 /* ldr pc, [pc - 12] */
30 *(uint32_t*) &code[4] = 0xE51FF00CU;
31 }
32
33 |.define TOP, r4
34 |.define L_ARG, r5
35 |.define DATA, r6
36 |.define DATA2, r7
37
38 |.macro load32, reg, val
39 | ldr reg, [pc]
40 | b >5
41 |.long val
42 |5:
43 |.endmacro
44
45 |.macro lcall, func
46 | mov r0, L_ARG
47 | bl func
48 |.endmacro
49
50 void compile_globals(struct jit* jit, lua_State* L)
51 {
52 (void) jit;
53 }
54
55 cfunction compile_callback(lua_State* L, int fidx, int ct_usr, const struct ctype* ct)
56 {
57 struct jit* Dst = get_jit(L);;
58 int i, nargs, num_upvals, ref;
59 const struct ctype* mt;
60
61 int top = lua_gettop(L);
62
63 ct_usr = lua_absindex(L, ct_usr);
64 fidx = lua_absindex(L, fidx);
65 nargs = (int) lua_rawlen(L, ct_usr);
66
67 dasm_setup(Dst, build_actionlist);
68
69 lua_newtable(L);
70 lua_pushvalue(L, -1);
71 ref = luaL_ref(L, LUA_REGISTRYINDEX);
72 num_upvals = 0;
73
74 if (ct->has_var_arg) {
75 luaL_error(L, "can't create callbacks with varargs");
76 }
77
78 /* prolog and get the upval table */
79 | mov r12, sp
80 | push {r0, r1, r2, r3} // do this first so that r0-r3 is right before stack bound arguments
81 | push {TOP, L_ARG, DATA, DATA2, r12, lr}
82 | sub DATA, r12, #16 // points to r0 on stack
83 | ldr L_ARG, [pc, #8]
84 | ldr r2, [pc, #8]
85 | ldr r1, [pc, #8]
86 | b >1
87 |.long L, ref, LUA_REGISTRYINDEX
88 |1:
89 | lcall extern lua_rawgeti
90
91 /* get the lua function */
92 lua_pushvalue(L, fidx);
93 lua_rawseti(L, -2, ++num_upvals);
94 | mov r2, #num_upvals
95 | mvn r1, #0 // -1
96 | lcall extern lua_rawgeti
97
98 for (i = 1; i <= nargs; i++) {
99 lua_rawgeti(L, ct_usr, i);
100 mt = (const struct ctype*) lua_touserdata(L, -1);
101
102 if (mt->pointers || mt->is_reference) {
103 lua_getuservalue(L, -1);
104 lua_rawseti(L, -3, ++num_upvals); /* usr value */
105 lua_rawseti(L, -2, ++num_upvals); /* mt */
106
107 | mov r2, #num_upvals-1 // usr value
108 | mvn r1, #i // -i-1, stack is upval table, func, i-1 args
109 | lcall extern lua_rawgeti
110 | load32 r2, mt
111 | mvn r1, #0 // -1
112 | lcall extern push_cdata
113 | ldr r2, [DATA], #4
114 | str r2, [r0]
115 | mvn r1, #1 // -2
116 | lcall extern lua_remove // remove the usr value
117
118 } else {
119 switch (mt->type) {
120 case INT64_TYPE:
121 lua_rawseti(L, -2, ++num_upvals); /* mt */
122 | lcall extern lua_pushnil
123 | load32 r2, mt
124 | mvn r1, #0 // -1
125 | lcall extern push_cdata
126 | ldr r2, [DATA], #4
127 | ldr r3, [DATA], #4
128 | str r2, [r0]
129 | str r3, [r0, #4]
130 | mvn r1, #1 // -2
131 | lcall extern lua_remove // remove the nil usr
132 break;
133
134 case INTPTR_TYPE:
135 lua_rawseti(L, -2, ++num_upvals); /* mt */
136 | lcall extern lua_pushnil
137 | load32 r2, mt
138 | mvn r1, #0 // -1
139 | lcall extern push_cdata
140 | ldr r2, [DATA], #4
141 | str r2, [r0]
142 | mvn r1, #1 // -2
143 | lcall extern lua_remove // remove the nil usr
144 break;
145
146 case BOOL_TYPE:
147 lua_pop(L, 1);
148 | ldr r1, [DATA], #4
149 | lcall extern lua_pushboolean
150 break;
151
152 case INT8_TYPE:
153 lua_pop(L, 1);
154 | ldr r1, [DATA], #4
155 | mov r1, r1, lsl #24
156 if (mt->is_unsigned) {
157 | mov r1, r1, lsr #24
158 } else {
159 | mov r1, r1, asr #24
160 }
161 | lcall extern push_int
162 break;
163
164 case INT16_TYPE:
165 lua_pop(L, 1);
166 | ldr r1, [DATA], #4
167 | mov r1, r1, lsl #16
168 if (mt->is_unsigned) {
169 | mov r1, r1, lsr #16
170 } else {
171 | mov r1, r1, asr #16
172 }
173 | lcall extern push_int
174 break;
175
176 case ENUM_TYPE:
177 case INT32_TYPE:
178 lua_pop(L, 1);
179 | ldr r1, [DATA], #4
180 | lcall extern push_int
181 break;
182
183 case FLOAT_TYPE:
184 lua_pop(L, 1);
185 | ldr r1, [DATA], #4
186 | lcall extern push_float
187 break;
188
189 case DOUBLE_TYPE:
190 lua_pop(L, 1);
191 | ldmia DATA!, {r1, r2}
192 | lcall extern lua_pushnumber
193 break;
194
195 default:
196 luaL_error(L, "NYI: callback arg type");
197 }
198 }
199 }
200
201 lua_rawgeti(L, ct_usr, 0);
202 mt = (const struct ctype*) lua_touserdata(L, -1);
203
204 | mov r3, #0
205 | mov r2, #((mt->pointers || mt->is_reference || mt->type != VOID_TYPE) ? 1 : 0)
206 | mov r1, #nargs
207 | lcall extern lua_callk
208
209 if (mt->pointers || mt->is_reference) {
210 lua_getuservalue(L, -1);
211 lua_rawseti(L, -3, ++num_upvals); /* usr value */
212 lua_rawseti(L, -2, ++num_upvals); /* mt */
213
214 | mov r2, #num_upvals-1 // usr value
215 | mvn r1, #1 // -2 stack is (upval table, ret val)
216 | lcall extern lua_rawgeti
217 | load32 r3, mt
218 | mov r2, #0 // -1 - ct_usr
219 | mvn r1, #1 // -2 - val
220 | lcall extern to_typed_pointer
221 | mov DATA, r0
222 | mvn r1, #3 // -4 - remove 3 (upval table, ret val, usr value)
223 | lcall extern lua_settop
224 | mov r0, DATA
225 } else {
226 switch (mt->type) {
227 case ENUM_TYPE:
228 lua_getuservalue(L, -1);
229 lua_rawseti(L, -3, ++num_upvals); /* usr value */
230 lua_rawseti(L, -2, ++num_upvals); /* mt */
231
232 | mov r2, #num_upvals-1 // usr value
233 | mvn r1, #1 // -2 stack is (upval table, ret val)
234 | lcall extern lua_rawgeti
235 | load32 r3, mt
236 | mvn r2, #0 // -1 - ct_usr
237 | mvn r1, #1 // -2 - val
238 | lcall extern to_enum
239 | mov DATA, r0
240 | mvn r1, #3 // -4 - remove 3 (upval table, ret val, usr value)
241 | lcall extern lua_settop
242 | mov r0, DATA
243 break;
244
245 case VOID_TYPE:
246 | mvn r1, #1 // -2
247 | lcall extern lua_settop
248 lua_pop(L, 1);
249 break;
250
251 case BOOL_TYPE:
252 case INT8_TYPE:
253 case INT16_TYPE:
254 case INT32_TYPE:
255 | mvn r1, #0 // -1
256 if (mt->is_unsigned) {
257 | lcall extern check_uint32
258 } else {
259 | lcall extern check_int32
260 }
261 goto single;
262
263 case INT64_TYPE:
264 | mvn r1, #0 // -1
265 if (mt->is_unsigned) {
266 | lcall extern check_uint64
267 } else {
268 | lcall extern check_int64
269 }
270 goto dual;
271
272 case INTPTR_TYPE:
273 | mvn r1, #0 // -1
274 | lcall extern check_intptr
275 goto single;
276
277 case FLOAT_TYPE:
278 | mvn r1, #0 // -1
279 | lcall extern check_float
280 goto single;
281
282 case DOUBLE_TYPE:
283 | mvn r1, #0 // -1
284 | lcall extern check_double
285 goto dual;
286
287 single:
288 | mov DATA, r0
289 | mvn r1, #2 // -3
290 | lcall extern lua_settop
291 | mov r0, DATA
292 lua_pop(L, 1);
293 break;
294
295 dual:
296 | mov DATA, r0
297 | mov DATA2, r1
298 | mvn r1, #2 // -3
299 | lcall extern lua_settop
300 | mov r0, DATA
301 | mov r1, DATA2
302 lua_pop(L, 1);
303 break;
304
305 default:
306 luaL_error(L, "NYI: callback return type");
307 }
308 }
309
310 | ldmia sp, {TOP, L_ARG, DATA, DATA2, sp, pc}
311
312 lua_pop(L, 1); /* upval table - already in registry */
313 assert(lua_gettop(L) == top);
314
315 {
316 void* p;
317 struct ctype ft;
318 cfunction func;
319
320 func = compile(Dst, L, NULL, ref);
321
322 ft = *ct;
323 ft.is_jitted = 1;
324 p = push_cdata(L, ct_usr, &ft);
325 *(cfunction*) p = func;
326
327 assert(lua_gettop(L) == top + 1);
328
329 return func;
330 }
331 }
332
333 void compile_function(lua_State* L, cfunction func, int ct_usr, const struct ctype* ct)
334 {
335 struct jit* Dst = get_jit(L);;
336 int i, nargs, num_upvals;
337 const struct ctype* mt;
338 void* p;
339
340 int top = lua_gettop(L);
341
342 ct_usr = lua_absindex(L, ct_usr);
343 nargs = (int) lua_rawlen(L, ct_usr);
344
345 p = push_cdata(L, ct_usr, ct);
346 *(cfunction*) p = func;
347 num_upvals = 1;
348
349 dasm_setup(Dst, build_actionlist);
350
351 | mov r12, sp
352 | push {r0}
353 | push {TOP, L_ARG, DATA, DATA2, r11, r12, lr}
354 | sub r11, r12, #4
355 | mov L_ARG, r0
356 | lcall extern lua_gettop
357 | mov TOP, r0
358 | cmp TOP, #nargs
359 | // these should really be in globals - but for some reason dynasm breaks when you do that
360 if (ct->has_var_arg) {
361 | bge >1
362 | load32 r1, "too few arguments"
363 | lcall extern luaL_error
364 |1:
365 } else {
366 | beq >1
367 | load32 r1, "incorrect number of arguments"
368 | lcall extern luaL_error
369 |1:
370 }
371
372 /* reserve enough stack space for all of the arguments (8 bytes per
373 * argument for double and maintains alignment). Add an extra 16 bytes so
374 * that the pop {r0, r1, r2, r3} doesn't clean out our stack frame */
375 | sub sp, sp, TOP, lsl #3
376 | sub sp, sp, #16
377 | mov DATA, sp
378
379 for (i = 1; i <= nargs; i++) {
380 lua_rawgeti(L, ct_usr, i);
381 mt = (const struct ctype*) lua_touserdata(L, -1);
382
383 if (mt->pointers || mt->is_reference || mt->type == FUNCTION_PTR_TYPE || mt->type == ENUM_TYPE) {
384 lua_getuservalue(L, -1);
385 num_upvals += 2;
386
387 | ldr r3, [pc, #4]
388 | ldr r2, [pc, #4]
389 | b >1
390 |.long mt, lua_upvalueindex(num_upvals)
391 |1:
392 | mov r1, #i
393 | mov r0, L_ARG
394
395 if (mt->pointers || mt->is_reference) {
396 | bl extern to_typed_pointer
397 } else if (mt->type == FUNCTION_PTR_TYPE) {
398 | bl extern to_typed_function
399 } else if (mt->type == ENUM_TYPE) {
400 | bl extern to_enum
401 }
402
403 | str r0, [DATA], #4
404
405 } else {
406 lua_pop(L, 1);
407 | mov r1, #i
408
409 switch (mt->type) {
410 case INT8_TYPE:
411 | lcall extern check_int32
412 if (mt->is_unsigned) {
413 | and r0, r0, #0xFF
414 } else {
415 | mov r0, r0, lsl #24
416 | mov r0, r0, asr #24
417 }
418 | str r0, [DATA], #4
419 break;
420
421 case INT16_TYPE:
422 | lcall extern check_int32
423 if (mt->is_unsigned) {
424 | mov r0, r0, lsl #16
425 | mov r0, r0, lsr #16
426 } else {
427 | mov r0, r0, lsl #16
428 | mov r0, r0, asr #16
429 }
430 | str r0, [DATA], #4
431 break;
432
433 case INT32_TYPE:
434 if (mt->is_unsigned) {
435 | lcall extern check_uint32
436 } else {
437 | lcall extern check_int32
438 }
439 | str r0, [DATA], #4
440 break;
441
442 case INT64_TYPE:
443 if (mt->is_unsigned) {
444 | lcall extern check_uint64
445 } else {
446 | lcall extern check_int64
447 }
448 | str r0, [DATA], #4
449 | str r1, [DATA], #4
450 break;
451
452 case DOUBLE_TYPE:
453 | lcall extern check_double
454 | str r0, [DATA], #4
455 | str r1, [DATA], #4
456 break;
457
458 case INTPTR_TYPE:
459 | lcall extern check_intptr
460 | str r0, [DATA], #4
461 break;
462
463 case FLOAT_TYPE:
464 | lcall extern check_float
465 | str r0, [DATA], #4
466 break;
467
468 default:
469 luaL_error(L, "NYI: call arg type");
470 }
471 }
472 }
473
474 if (ct->has_var_arg) {
475 | mov r3, DATA
476 | mov r2, TOP
477 | mov r1, #nargs+1
478 | lcall extern unpack_varargs_stack
479 }
480
481 | load32 r0, &Dst->last_errno
482 | ldr r0, [r0]
483 | bl extern SetLastError
484
485 | pop {r0, r1, r2, r3} // this pop is balanced with the sub sp, #16
486 | bl extern FUNCTION
487
488 |.macro get_errno
489 | bl extern GetLastError
490 | load32 r1, &Dst->last_errno
491 | str r0, [r1]
492 |.endmacro
493
494 |.macro return
495 | ldmdb r11, {TOP, L_ARG, DATA, r11, sp, pc}
496 |.endmacro
497
498 lua_rawgeti(L, ct_usr, 0);
499 mt = (const struct ctype*) lua_touserdata(L, -1);
500
501 if (mt->pointers || mt->is_reference) {
502 lua_getuservalue(L, -1);
503 num_upvals += 2;
504 | mov DATA, r0
505 | get_errno
506 | ldr r2, [pc, #4]
507 | ldr r1, [pc, #4]
508 | b >1
509 |.long mt, lua_upvalueindex(num_upvals)
510 |1:
511 | lcall extern push_cdata
512 | str DATA, [r0]
513 | mov r0, #1
514 | return
515
516 } else {
517 switch (mt->type) {
518 case INT64_TYPE:
519 num_upvals++;
520 | mov DATA, r0
521 | mov DATA2, r1
522 | get_errno
523 | lcall extern lua_pushnil
524 | load32 r2, mt
525 | mvn r1, #0 // -1
526 | lcall extern push_cdata
527 | str DATA, [r0]
528 | str DATA2, [r0, #4]
529 | mov r0, #1
530 | return
531 break;
532
533 case INTPTR_TYPE:
534 num_upvals++;
535 | mov DATA, r0
536 | get_errno
537 | lcall extern lua_pushnil
538 | load32 r2, mt
539 | mvn r1, #0 // -1
540 | lcall extern push_cdata
541 | str DATA, [r0]
542 | mov r0, #1
543 | return
544 break;
545
546 case VOID_TYPE:
547 lua_pop(L, 1);
548 | get_errno
549 | mov r0, #0
550 | return
551 break;
552
553 case BOOL_TYPE:
554 lua_pop(L, 1);
555 | mov DATA, r0
556 | get_errno
557 | mov r1, DATA
558 | lcall extern lua_pushboolean
559 | mov r0, #1
560 | return
561 break;
562
563 case INT8_TYPE:
564 case INT16_TYPE:
565 case INT32_TYPE:
566 case ENUM_TYPE:
567 lua_pop(L, 1);
568 | mov DATA, r0
569 | get_errno
570 | mov r1, DATA
571 if (mt->is_unsigned) {
572 | lcall extern push_uint
573 } else {
574 | lcall extern push_int
575 }
576 | mov r0, #1
577 | return
578 break;
579
580 case FLOAT_TYPE:
581 lua_pop(L, 1);
582 | mov DATA, r0
583 | get_errno
584 | mov r1, DATA
585 | lcall extern push_float
586 | mov r0, #1
587 | return
588 break;
589
590 case DOUBLE_TYPE:
591 lua_pop(L, 1);
592 | mov DATA, r0
593 | mov DATA2, r1
594 | get_errno
595 | mov r2, DATA2
596 | mov r1, DATA
597 | lcall extern lua_pushnumber
598 | mov r0, #1
599 | return
600 break;
601
602 default:
603 luaL_error(L, "NYI: call return type");
604 }
605 }
606
607 assert(lua_gettop(L) == top + num_upvals);
608 lua_pushcclosure(L, (lua_CFunction) compile(Dst, L, func, LUA_NOREF), num_upvals);
609 }
610