Debian Codegraph @

texk/web2c/luatexdir/luaffi/call_arm.h

raw
1 /*
2 ** This file has been pre-processed with DynASM.
3 ** https://luajit.org/dynasm.html
4 ** DynASM version 1.5.0, DynASM arm version 1.5.0
5 ** DO NOT EDIT! The original file is in "call_arm.dasc".
6 */
7
8 /* vim: ts=4 sw=4 sts=4 et tw=78
9 * Portions copyright (c) 2015-present, Facebook, Inc. All rights reserved.
10 * Portions copyright (c) 2011 James R. McKaskill.
11 *
12 * This source code is licensed under the BSD-style license found in the
13 * LICENSE file in the root directory of this source tree. An additional grant
14 * of patent rights can be found in the PATENTS file in the same directory.
15 */
16 //|.arch arm
17 #if DASM_VERSION != 10500
18 #error "Version mismatch between DynASM and included encoding engine"
19 #endif
20 //|.actionlist build_actionlist
21 static const unsigned int build_actionlist[546] = {
22 0xE1A0C00D,
23 0xE92D000F,
24 0xE92D50F0,
25 0xE24C6010,
26 0xE59F5008,
27 0xE59F2008,
28 0xE59F1008,
29 0xEA000000,
30 0x00050001,
31 0x00090000,
32 0x00090000,
33 0x00090000,
34 0x0006000B,
35 0xE1A00005,
36 0xEB000000,
37 0x00030000,
38 0x00000000,
39 0xE3A02000,
40 0x000B0000,
41 0xE3E01000,
42 0xE1A00005,
43 0xEB000000,
44 0x00030000,
45 0x00000000,
46 0xE3A02000,
47 0x000B0000,
48 0xE3E01000,
49 0x000B0000,
50 0xE1A00005,
51 0xEB000000,
52 0x00030000,
53 0xE59F2000,
54 0xEA000000,
55 0x00050005,
56 0x00090000,
57 0x0006000F,
58 0xE3E01000,
59 0xE1A00005,
60 0xEB000000,
61 0x00030001,
62 0xE4962004,
63 0xE5802000,
64 0xE3E01001,
65 0xE1A00005,
66 0xEB000000,
67 0x00030002,
68 0x00000000,
69 0xE1A00005,
70 0xEB000000,
71 0x00030003,
72 0xE59F2000,
73 0xEA000000,
74 0x00050005,
75 0x00090000,
76 0x0006000F,
77 0xE3E01000,
78 0xE1A00005,
79 0xEB000000,
80 0x00030001,
81 0xE4962004,
82 0xE4963004,
83 0xE5802000,
84 0xE5803004,
85 0xE3E01001,
86 0xE1A00005,
87 0xEB000000,
88 0x00030002,
89 0x00000000,
90 0xE1A00005,
91 0xEB000000,
92 0x00030003,
93 0xE59F2000,
94 0xEA000000,
95 0x00050005,
96 0x00090000,
97 0x0006000F,
98 0xE3E01000,
99 0xE1A00005,
100 0xEB000000,
101 0x00030001,
102 0xE4962004,
103 0xE5802000,
104 0xE3E01001,
105 0xE1A00005,
106 0xEB000000,
107 0x00030002,
108 0x00000000,
109 0xE4961004,
110 0xE1A00005,
111 0xEB000000,
112 0x00030004,
113 0x00000000,
114 0xE4961004,
115 0xE1A01C01,
116 0x00000000,
117 0xE1A01C21,
118 0x00000000,
119 0xE1A01C41,
120 0x00000000,
121 0xE1A00005,
122 0xEB000000,
123 0x00030005,
124 0x00000000,
125 0xE4961004,
126 0xE1A01801,
127 0x00000000,
128 0xE1A01821,
129 0x00000000,
130 0xE1A01841,
131 0x00000000,
132 0xE1A00005,
133 0xEB000000,
134 0x00030005,
135 0x00000000,
136 0xE4961004,
137 0xE1A00005,
138 0xEB000000,
139 0x00030005,
140 0x00000000,
141 0xE4961004,
142 0xE1A00005,
143 0xEB000000,
144 0x00030006,
145 0x00000000,
146 0xE8B60006,
147 0xE1A00005,
148 0xEB000000,
149 0x00030007,
150 0x00000000,
151 0xE3A03000,
152 0xE3A02000,
153 0x000B0000,
154 0xE3A01000,
155 0x000B0000,
156 0xE1A00005,
157 0xEB000000,
158 0x00030008,
159 0x00000000,
160 0xE3A02000,
161 0x000B0000,
162 0xE3E01001,
163 0xE1A00005,
164 0xEB000000,
165 0x00030000,
166 0xE59F3000,
167 0xEA000000,
168 0x00050005,
169 0x00090000,
170 0x0006000F,
171 0xE3A02000,
172 0xE3E01001,
173 0xE1A00005,
174 0xEB000000,
175 0x00030009,
176 0xE1A06000,
177 0xE3E01003,
178 0xE1A00005,
179 0xEB000000,
180 0x0003000A,
181 0xE1A00006,
182 0x00000000,
183 0xE3A02000,
184 0x000B0000,
185 0xE3E01001,
186 0xE1A00005,
187 0xEB000000,
188 0x00030000,
189 0xE59F3000,
190 0xEA000000,
191 0x00050005,
192 0x00090000,
193 0x0006000F,
194 0xE3E02000,
195 0xE3E01001,
196 0xE1A00005,
197 0xEB000000,
198 0x0003000B,
199 0xE1A06000,
200 0xE3E01003,
201 0xE1A00005,
202 0xEB000000,
203 0x0003000A,
204 0xE1A00006,
205 0x00000000,
206 0xE3E01001,
207 0xE1A00005,
208 0xEB000000,
209 0x0003000A,
210 0x00000000,
211 0xE3E01000,
212 0x00000000,
213 0xE1A00005,
214 0xEB000000,
215 0x0003000C,
216 0x00000000,
217 0xE1A00005,
218 0xEB000000,
219 0x0003000D,
220 0x00000000,
221 0xE3E01000,
222 0x00000000,
223 0xE1A00005,
224 0xEB000000,
225 0x0003000E,
226 0x00000000,
227 0xE1A00005,
228 0xEB000000,
229 0x0003000F,
230 0x00000000,
231 0xE3E01000,
232 0xE1A00005,
233 0xEB000000,
234 0x00030010,
235 0x00000000,
236 0xE3E01000,
237 0xE1A00005,
238 0xEB000000,
239 0x00030011,
240 0x00000000,
241 0xE3E01000,
242 0xE1A00005,
243 0xEB000000,
244 0x00030012,
245 0x00000000,
246 0xE1A06000,
247 0xE3E01002,
248 0xE1A00005,
249 0xEB000000,
250 0x0003000A,
251 0xE1A00006,
252 0x00000000,
253 0xE1A06000,
254 0xE1A07001,
255 0xE3E01002,
256 0xE1A00005,
257 0xEB000000,
258 0x0003000A,
259 0xE1A00006,
260 0xE1A01007,
261 0x00000000,
262 0xE89DA0F0,
263 0x00000000,
264 0xE1A0C00D,
265 0xE92D0001,
266 0xE92D58F0,
267 0xE24CB004,
268 0xE1A05000,
269 0xE1A00005,
270 0xEB000000,
271 0x00030013,
272 0xE1A04000,
273 0xE3540000,
274 0x000B0000,
275 0x00000000,
276 0xAA000000,
277 0x00050001,
278 0xE59F1000,
279 0xEA000000,
280 0x00050005,
281 0x00090000,
282 0x0006000F,
283 0xE1A00005,
284 0xEB000000,
285 0x00030014,
286 0x0006000B,
287 0x00000000,
288 0x0A000000,
289 0x00050001,
290 0xE59F1000,
291 0xEA000000,
292 0x00050005,
293 0x00090000,
294 0x0006000F,
295 0xE1A00005,
296 0xEB000000,
297 0x00030014,
298 0x0006000B,
299 0x00000000,
300 0xE04DD184,
301 0xE24DD010,
302 0xE1A0600D,
303 0x00000000,
304 0xE59F3004,
305 0xE59F2004,
306 0xEA000000,
307 0x00050001,
308 0x00090000,
309 0x00090000,
310 0x0006000B,
311 0xE3A01000,
312 0x000B0000,
313 0xE1A00005,
314 0x00000000,
315 0xEB000000,
316 0x00030009,
317 0x00000000,
318 0xEB000000,
319 0x00030015,
320 0x00000000,
321 0xEB000000,
322 0x0003000B,
323 0x00000000,
324 0xE4860004,
325 0x00000000,
326 0xE3A01000,
327 0x000B0000,
328 0x00000000,
329 0xE1A00005,
330 0xEB000000,
331 0x0003000D,
332 0x00000000,
333 0xE20000FF,
334 0x00000000,
335 0xE1A00C00,
336 0xE1A00C40,
337 0x00000000,
338 0xE4860004,
339 0x00000000,
340 0xE1A00005,
341 0xEB000000,
342 0x0003000D,
343 0x00000000,
344 0xE1A00800,
345 0xE1A00820,
346 0x00000000,
347 0xE1A00800,
348 0xE1A00840,
349 0x00000000,
350 0xE4860004,
351 0x00000000,
352 0xE1A00005,
353 0xEB000000,
354 0x0003000C,
355 0x00000000,
356 0xE1A00005,
357 0xEB000000,
358 0x0003000D,
359 0x00000000,
360 0xE4860004,
361 0x00000000,
362 0xE1A00005,
363 0xEB000000,
364 0x0003000E,
365 0x00000000,
366 0xE1A00005,
367 0xEB000000,
368 0x0003000F,
369 0x00000000,
370 0xE4860004,
371 0xE4861004,
372 0x00000000,
373 0xE1A00005,
374 0xEB000000,
375 0x00030012,
376 0xE4860004,
377 0xE4861004,
378 0x00000000,
379 0xE1A00005,
380 0xEB000000,
381 0x00030010,
382 0xE4860004,
383 0x00000000,
384 0xE1A00005,
385 0xEB000000,
386 0x00030011,
387 0xE4860004,
388 0x00000000,
389 0xE1A03006,
390 0xE1A02004,
391 0xE3A01000,
392 0x000B0000,
393 0xE1A00005,
394 0xEB000000,
395 0x00030016,
396 0x00000000,
397 0xE59F0000,
398 0xEA000000,
399 0x00050005,
400 0x00090000,
401 0x0006000F,
402 0xE5900000,
403 0xEB000000,
404 0x00030017,
405 0x00000000,
406 0xE8BD000F,
407 0xEB000000,
408 0x00030018,
409 0x00000000,
410 0xE1A06000,
411 0xEB000000,
412 0x00030019,
413 0xE59F1000,
414 0xEA000000,
415 0x00050005,
416 0x00090000,
417 0x0006000F,
418 0xE5810000,
419 0xE59F2004,
420 0xE59F1004,
421 0xEA000000,
422 0x00050001,
423 0x00090000,
424 0x00090000,
425 0x0006000B,
426 0xE1A00005,
427 0xEB000000,
428 0x00030001,
429 0xE5806000,
430 0xE3A00001,
431 0xE91BA870,
432 0x00000000,
433 0xE1A06000,
434 0xE1A07001,
435 0xEB000000,
436 0x00030019,
437 0xE59F1000,
438 0xEA000000,
439 0x00050005,
440 0x00090000,
441 0x0006000F,
442 0xE5810000,
443 0xE1A00005,
444 0xEB000000,
445 0x00030003,
446 0xE59F2000,
447 0xEA000000,
448 0x00050005,
449 0x00090000,
450 0x0006000F,
451 0xE3E01000,
452 0xE1A00005,
453 0xEB000000,
454 0x00030001,
455 0xE5806000,
456 0xE5807004,
457 0xE3A00001,
458 0xE91BA870,
459 0x00000000,
460 0xE1A06000,
461 0xEB000000,
462 0x00030019,
463 0xE59F1000,
464 0xEA000000,
465 0x00050005,
466 0x00090000,
467 0x0006000F,
468 0xE5810000,
469 0xE1A00005,
470 0xEB000000,
471 0x00030003,
472 0xE59F2000,
473 0xEA000000,
474 0x00050005,
475 0x00090000,
476 0x0006000F,
477 0xE3E01000,
478 0xE1A00005,
479 0xEB000000,
480 0x00030001,
481 0xE5806000,
482 0xE3A00001,
483 0xE91BA870,
484 0x00000000,
485 0xEB000000,
486 0x00030019,
487 0xE59F1000,
488 0xEA000000,
489 0x00050005,
490 0x00090000,
491 0x0006000F,
492 0xE5810000,
493 0xE3A00000,
494 0xE91BA870,
495 0x00000000,
496 0xE1A06000,
497 0xEB000000,
498 0x00030019,
499 0xE59F1000,
500 0xEA000000,
501 0x00050005,
502 0x00090000,
503 0x0006000F,
504 0xE5810000,
505 0xE1A01006,
506 0xE1A00005,
507 0xEB000000,
508 0x00030004,
509 0xE3A00001,
510 0xE91BA870,
511 0x00000000,
512 0xE1A06000,
513 0xEB000000,
514 0x00030019,
515 0xE59F1000,
516 0xEA000000,
517 0x00050005,
518 0x00090000,
519 0x0006000F,
520 0xE5810000,
521 0xE1A01006,
522 0x00000000,
523 0xE1A00005,
524 0xEB000000,
525 0x0003001A,
526 0x00000000,
527 0xE1A00005,
528 0xEB000000,
529 0x00030005,
530 0x00000000,
531 0xE3A00001,
532 0xE91BA870,
533 0x00000000,
534 0xE1A06000,
535 0xEB000000,
536 0x00030019,
537 0xE59F1000,
538 0xEA000000,
539 0x00050005,
540 0x00090000,
541 0x0006000F,
542 0xE5810000,
543 0xE1A01006,
544 0xE1A00005,
545 0xEB000000,
546 0x00030006,
547 0xE3A00001,
548 0xE91BA870,
549 0x00000000,
550 0xE1A06000,
551 0xE1A07001,
552 0xEB000000,
553 0x00030019,
554 0xE59F1000,
555 0xEA000000,
556 0x00050005,
557 0x00090000,
558 0x0006000F,
559 0xE5810000,
560 0xE1A02007,
561 0xE1A01006,
562 0xE1A00005,
563 0xEB000000,
564 0x00030007,
565 0xE3A00001,
566 0xE91BA870,
567 0x00000000
568 };
569
570 //|.globalnames globnames
571 static const char *const globnames[] = {
572 (const char *)0
573 };
574 //|.externnames extnames
575 static const char *const extnames[] = {
576 "lua_rawgeti",
577 "push_cdata",
578 "lua_remove",
579 "lua_pushnil",
580 "lua_pushboolean",
581 "push_int",
582 "push_float",
583 "lua_pushnumber",
584 "lua_callk",
585 "to_typed_pointer",
586 "lua_settop",
587 "to_enum",
588 "check_uint32",
589 "check_int32",
590 "check_uint64",
591 "check_int64",
592 "check_intptr",
593 "check_float",
594 "check_double",
595 "lua_gettop",
596 "luaL_error",
597 "to_typed_function",
598 "unpack_varargs_stack",
599 "SetLastError",
600 "FUNCTION",
601 "GetLastError",
602 "push_uint",
603 (const char *)0
604 };
605
606 #define JUMP_SIZE 8
607 #define MIN_BRANCH ((INT32_MIN) >> 8)
608 #define MAX_BRANCH ((INT32_MAX) >> 8)
609 #define BRANCH_OFF 4
610
611 static void compile_extern_jump(struct jit* jit, lua_State* L, cfunction func, uint8_t* code)
612 {
613 /* The jump code is the function pointer followed by a stub to call the
614 * function pointer. The stub exists so we can jump to functions with an
615 * offset greater than 32MB.
616 *
617 * Note we have to manually set this up since there are commands buffered
618 * in the jit state.
619 */
620 *(cfunction*) code = func;
621 /* ldr pc, [pc - 12] */
622 *(uint32_t*) &code[4] = 0xE51FF00CU;
623 }
624
625 //|.define TOP, r4
626 //|.define L_ARG, r5
627 //|.define DATA, r6
628 //|.define DATA2, r7
629
630 //|.macro load32, reg, val
631 //| ldr reg, [pc]
632 //| b >5
633 //|.long val
634 //|5:
635 //|.endmacro
636
637 //|.macro lcall, func
638 //| mov r0, L_ARG
639 //| bl func
640 //|.endmacro
641
642 void compile_globals(struct jit* jit, lua_State* L)
643 {
644 (void) jit;
645 }
646
647 cfunction compile_callback(lua_State* L, int fidx, int ct_usr, const struct ctype* ct)
648 {
649 struct jit* Dst = get_jit(L);;
650 int i, nargs, num_upvals, ref;
651 const struct ctype* mt;
652
653 int top = lua_gettop(L);
654
655 ct_usr = lua_absindex(L, ct_usr);
656 fidx = lua_absindex(L, fidx);
657 nargs = (int) lua_rawlen(L, ct_usr);
658
659 dasm_setup(Dst, build_actionlist);
660
661 lua_newtable(L);
662 lua_pushvalue(L, -1);
663 ref = luaL_ref(L, LUA_REGISTRYINDEX);
664 num_upvals = 0;
665
666 if (ct->has_var_arg) {
667 luaL_error(L, "can't create callbacks with varargs");
668 }
669
670 /* prolog and get the upval table */
671 //| mov r12, sp
672 //| push {r0, r1, r2, r3} // do this first so that r0-r3 is right before stack bound arguments
673 //| push {TOP, L_ARG, DATA, DATA2, r12, lr}
674 //| sub DATA, r12, #16 // points to r0 on stack
675 //| ldr L_ARG, [pc, #8]
676 //| ldr r2, [pc, #8]
677 //| ldr r1, [pc, #8]
678 //| b >1
679 //|.long L, ref, LUA_REGISTRYINDEX
680 //|1:
681 //| lcall extern lua_rawgeti
682 dasm_put(Dst, 0, (uintptr_t)(L), (uintptr_t)(ref), (uintptr_t)(LUA_REGISTRYINDEX));
683
684 /* get the lua function */
685 lua_pushvalue(L, fidx);
686 lua_rawseti(L, -2, ++num_upvals);
687 //| mov r2, #num_upvals
688 //| mvn r1, #0 // -1
689 //| lcall extern lua_rawgeti
690 dasm_put(Dst, 17, num_upvals);
691
692 for (i = 1; i <= nargs; i++) {
693 lua_rawgeti(L, ct_usr, i);
694 mt = (const struct ctype*) lua_touserdata(L, -1);
695
696 if (mt->pointers || mt->is_reference) {
697 lua_getuservalue(L, -1);
698 lua_rawseti(L, -3, ++num_upvals); /* usr value */
699 lua_rawseti(L, -2, ++num_upvals); /* mt */
700
701 //| mov r2, #num_upvals-1 // usr value
702 //| mvn r1, #i // -i-1, stack is upval table, func, i-1 args
703 //| lcall extern lua_rawgeti
704 //| load32 r2, mt
705 //| mvn r1, #0 // -1
706 //| lcall extern push_cdata
707 //| ldr r2, [DATA], #4
708 //| str r2, [r0]
709 //| mvn r1, #1 // -2
710 //| lcall extern lua_remove // remove the usr value
711 dasm_put(Dst, 24, num_upvals-1, i, (uintptr_t)(mt));
712
713 } else {
714 switch (mt->type) {
715 case INT64_TYPE:
716 lua_rawseti(L, -2, ++num_upvals); /* mt */
717 //| lcall extern lua_pushnil
718 //| load32 r2, mt
719 //| mvn r1, #0 // -1
720 //| lcall extern push_cdata
721 //| ldr r2, [DATA], #4
722 //| ldr r3, [DATA], #4
723 //| str r2, [r0]
724 //| str r3, [r0, #4]
725 //| mvn r1, #1 // -2
726 //| lcall extern lua_remove // remove the nil usr
727 dasm_put(Dst, 47, (uintptr_t)(mt));
728 break;
729
730 case INTPTR_TYPE:
731 lua_rawseti(L, -2, ++num_upvals); /* mt */
732 //| lcall extern lua_pushnil
733 //| load32 r2, mt
734 //| mvn r1, #0 // -1
735 //| lcall extern push_cdata
736 //| ldr r2, [DATA], #4
737 //| str r2, [r0]
738 //| mvn r1, #1 // -2
739 //| lcall extern lua_remove // remove the nil usr
740 dasm_put(Dst, 68, (uintptr_t)(mt));
741 break;
742
743 case BOOL_TYPE:
744 lua_pop(L, 1);
745 //| ldr r1, [DATA], #4
746 //| lcall extern lua_pushboolean
747 dasm_put(Dst, 87);
748 break;
749
750 case INT8_TYPE:
751 lua_pop(L, 1);
752 //| ldr r1, [DATA], #4
753 //| mov r1, r1, lsl #24
754 dasm_put(Dst, 92);
755 if (mt->is_unsigned) {
756 //| mov r1, r1, lsr #24
757 dasm_put(Dst, 95);
758 } else {
759 //| mov r1, r1, asr #24
760 dasm_put(Dst, 97);
761 }
762 //| lcall extern push_int
763 dasm_put(Dst, 99);
764 break;
765
766 case INT16_TYPE:
767 lua_pop(L, 1);
768 //| ldr r1, [DATA], #4
769 //| mov r1, r1, lsl #16
770 dasm_put(Dst, 103);
771 if (mt->is_unsigned) {
772 //| mov r1, r1, lsr #16
773 dasm_put(Dst, 106);
774 } else {
775 //| mov r1, r1, asr #16
776 dasm_put(Dst, 108);
777 }
778 //| lcall extern push_int
779 dasm_put(Dst, 110);
780 break;
781
782 case ENUM_TYPE:
783 case INT32_TYPE:
784 lua_pop(L, 1);
785 //| ldr r1, [DATA], #4
786 //| lcall extern push_int
787 dasm_put(Dst, 114);
788 break;
789
790 case FLOAT_TYPE:
791 lua_pop(L, 1);
792 //| ldr r1, [DATA], #4
793 //| lcall extern push_float
794 dasm_put(Dst, 119);
795 break;
796
797 case DOUBLE_TYPE:
798 lua_pop(L, 1);
799 //| ldmia DATA!, {r1, r2}
800 //| lcall extern lua_pushnumber
801 dasm_put(Dst, 124);
802 break;
803
804 default:
805 luaL_error(L, "NYI: callback arg type");
806 }
807 }
808 }
809
810 lua_rawgeti(L, ct_usr, 0);
811 mt = (const struct ctype*) lua_touserdata(L, -1);
812
813 //| mov r3, #0
814 //| mov r2, #((mt->pointers || mt->is_reference || mt->type != VOID_TYPE) ? 1 : 0)
815 //| mov r1, #nargs
816 //| lcall extern lua_callk
817 dasm_put(Dst, 129, ((mt->pointers || mt->is_reference || mt->type != VOID_TYPE) ? 1 : 0), nargs);
818
819 if (mt->pointers || mt->is_reference) {
820 lua_getuservalue(L, -1);
821 lua_rawseti(L, -3, ++num_upvals); /* usr value */
822 lua_rawseti(L, -2, ++num_upvals); /* mt */
823
824 //| mov r2, #num_upvals-1 // usr value
825 //| mvn r1, #1 // -2 stack is (upval table, ret val)
826 //| lcall extern lua_rawgeti
827 //| load32 r3, mt
828 //| mov r2, #0 // -1 - ct_usr
829 //| mvn r1, #1 // -2 - val
830 //| lcall extern to_typed_pointer
831 //| mov DATA, r0
832 //| mvn r1, #3 // -4 - remove 3 (upval table, ret val, usr value)
833 //| lcall extern lua_settop
834 //| mov r0, DATA
835 dasm_put(Dst, 138, num_upvals-1, (uintptr_t)(mt));
836 } else {
837 switch (mt->type) {
838 case ENUM_TYPE:
839 lua_getuservalue(L, -1);
840 lua_rawseti(L, -3, ++num_upvals); /* usr value */
841 lua_rawseti(L, -2, ++num_upvals); /* mt */
842
843 //| mov r2, #num_upvals-1 // usr value
844 //| mvn r1, #1 // -2 stack is (upval table, ret val)
845 //| lcall extern lua_rawgeti
846 //| load32 r3, mt
847 //| mvn r2, #0 // -1 - ct_usr
848 //| mvn r1, #1 // -2 - val
849 //| lcall extern to_enum
850 //| mov DATA, r0
851 //| mvn r1, #3 // -4 - remove 3 (upval table, ret val, usr value)
852 //| lcall extern lua_settop
853 //| mov r0, DATA
854 dasm_put(Dst, 161, num_upvals-1, (uintptr_t)(mt));
855 break;
856
857 case VOID_TYPE:
858 //| mvn r1, #1 // -2
859 //| lcall extern lua_settop
860 dasm_put(Dst, 184);
861 lua_pop(L, 1);
862 break;
863
864 case BOOL_TYPE:
865 case INT8_TYPE:
866 case INT16_TYPE:
867 case INT32_TYPE:
868 //| mvn r1, #0 // -1
869 dasm_put(Dst, 189);
870 if (mt->is_unsigned) {
871 //| lcall extern check_uint32
872 dasm_put(Dst, 191);
873 } else {
874 //| lcall extern check_int32
875 dasm_put(Dst, 195);
876 }
877 goto single;
878
879 case INT64_TYPE:
880 //| mvn r1, #0 // -1
881 dasm_put(Dst, 199);
882 if (mt->is_unsigned) {
883 //| lcall extern check_uint64
884 dasm_put(Dst, 201);
885 } else {
886 //| lcall extern check_int64
887 dasm_put(Dst, 205);
888 }
889 goto dual;
890
891 case INTPTR_TYPE:
892 //| mvn r1, #0 // -1
893 //| lcall extern check_intptr
894 dasm_put(Dst, 209);
895 goto single;
896
897 case FLOAT_TYPE:
898 //| mvn r1, #0 // -1
899 //| lcall extern check_float
900 dasm_put(Dst, 214);
901 goto single;
902
903 case DOUBLE_TYPE:
904 //| mvn r1, #0 // -1
905 //| lcall extern check_double
906 dasm_put(Dst, 219);
907 goto dual;
908
909 single:
910 //| mov DATA, r0
911 //| mvn r1, #2 // -3
912 //| lcall extern lua_settop
913 //| mov r0, DATA
914 dasm_put(Dst, 224);
915 lua_pop(L, 1);
916 break;
917
918 dual:
919 //| mov DATA, r0
920 //| mov DATA2, r1
921 //| mvn r1, #2 // -3
922 //| lcall extern lua_settop
923 //| mov r0, DATA
924 //| mov r1, DATA2
925 dasm_put(Dst, 231);
926 lua_pop(L, 1);
927 break;
928
929 default:
930 luaL_error(L, "NYI: callback return type");
931 }
932 }
933
934 //| ldmia sp, {TOP, L_ARG, DATA, DATA2, sp, pc}
935 dasm_put(Dst, 240);
936
937 lua_pop(L, 1); /* upval table - already in registry */
938 assert(lua_gettop(L) == top);
939
940 {
941 void* p;
942 struct ctype ft;
943 cfunction func;
944
945 func = compile(Dst, L, NULL, ref);
946
947 ft = *ct;
948 ft.is_jitted = 1;
949 p = push_cdata(L, ct_usr, &ft);
950 *(cfunction*) p = func;
951
952 assert(lua_gettop(L) == top + 1);
953
954 return func;
955 }
956 }
957
958 void compile_function(lua_State* L, cfunction func, int ct_usr, const struct ctype* ct)
959 {
960 struct jit* Dst = get_jit(L);;
961 int i, nargs, num_upvals;
962 const struct ctype* mt;
963 void* p;
964
965 int top = lua_gettop(L);
966
967 ct_usr = lua_absindex(L, ct_usr);
968 nargs = (int) lua_rawlen(L, ct_usr);
969
970 p = push_cdata(L, ct_usr, ct);
971 *(cfunction*) p = func;
972 num_upvals = 1;
973
974 dasm_setup(Dst, build_actionlist);
975
976 //| mov r12, sp
977 //| push {r0}
978 //| push {TOP, L_ARG, DATA, DATA2, r11, r12, lr}
979 //| sub r11, r12, #4
980 //| mov L_ARG, r0
981 //| lcall extern lua_gettop
982 //| mov TOP, r0
983 //| cmp TOP, #nargs
984 //| // these should really be in globals - but for some reason dynasm breaks when you do that
985 dasm_put(Dst, 242, nargs);
986 if (ct->has_var_arg) {
987 //| bge >1
988 //| load32 r1, "too few arguments"
989 //| lcall extern luaL_error
990 //|1:
991 dasm_put(Dst, 254, (uintptr_t)("too few arguments"));
992 } else {
993 //| beq >1
994 //| load32 r1, "incorrect number of arguments"
995 //| lcall extern luaL_error
996 //|1:
997 dasm_put(Dst, 266, (uintptr_t)("incorrect number of arguments"));
998 }
999
1000 /* reserve enough stack space for all of the arguments (8 bytes per
1001 * argument for double and maintains alignment). Add an extra 16 bytes so
1002 * that the pop {r0, r1, r2, r3} doesn't clean out our stack frame */
1003 //| sub sp, sp, TOP, lsl #3
1004 //| sub sp, sp, #16
1005 //| mov DATA, sp
1006 dasm_put(Dst, 278);
1007
1008 for (i = 1; i <= nargs; i++) {
1009 lua_rawgeti(L, ct_usr, i);
1010 mt = (const struct ctype*) lua_touserdata(L, -1);
1011
1012 if (mt->pointers || mt->is_reference || mt->type == FUNCTION_PTR_TYPE || mt->type == ENUM_TYPE) {
1013 lua_getuservalue(L, -1);
1014 num_upvals += 2;
1015
1016 //| ldr r3, [pc, #4]
1017 //| ldr r2, [pc, #4]
1018 //| b >1
1019 //|.long mt, lua_upvalueindex(num_upvals)
1020 //|1:
1021 //| mov r1, #i
1022 //| mov r0, L_ARG
1023 dasm_put(Dst, 282, (uintptr_t)(mt), (uintptr_t)(lua_upvalueindex(num_upvals)), i);
1024
1025 if (mt->pointers || mt->is_reference) {
1026 //| bl extern to_typed_pointer
1027 dasm_put(Dst, 293);
1028 } else if (mt->type == FUNCTION_PTR_TYPE) {
1029 //| bl extern to_typed_function
1030 dasm_put(Dst, 296);
1031 } else if (mt->type == ENUM_TYPE) {
1032 //| bl extern to_enum
1033 dasm_put(Dst, 299);
1034 }
1035
1036 //| str r0, [DATA], #4
1037 dasm_put(Dst, 302);
1038
1039 } else {
1040 lua_pop(L, 1);
1041 //| mov r1, #i
1042 dasm_put(Dst, 304, i);
1043
1044 switch (mt->type) {
1045 case INT8_TYPE:
1046 //| lcall extern check_int32
1047 dasm_put(Dst, 307);
1048 if (mt->is_unsigned) {
1049 //| and r0, r0, #0xFF
1050 dasm_put(Dst, 311);
1051 } else {
1052 //| mov r0, r0, lsl #24
1053 //| mov r0, r0, asr #24
1054 dasm_put(Dst, 313);
1055 }
1056 //| str r0, [DATA], #4
1057 dasm_put(Dst, 316);
1058 break;
1059
1060 case INT16_TYPE:
1061 //| lcall extern check_int32
1062 dasm_put(Dst, 318);
1063 if (mt->is_unsigned) {
1064 //| mov r0, r0, lsl #16
1065 //| mov r0, r0, lsr #16
1066 dasm_put(Dst, 322);
1067 } else {
1068 //| mov r0, r0, lsl #16
1069 //| mov r0, r0, asr #16
1070 dasm_put(Dst, 325);
1071 }
1072 //| str r0, [DATA], #4
1073 dasm_put(Dst, 328);
1074 break;
1075
1076 case INT32_TYPE:
1077 if (mt->is_unsigned) {
1078 //| lcall extern check_uint32
1079 dasm_put(Dst, 330);
1080 } else {
1081 //| lcall extern check_int32
1082 dasm_put(Dst, 334);
1083 }
1084 //| str r0, [DATA], #4
1085 dasm_put(Dst, 338);
1086 break;
1087
1088 case INT64_TYPE:
1089 if (mt->is_unsigned) {
1090 //| lcall extern check_uint64
1091 dasm_put(Dst, 340);
1092 } else {
1093 //| lcall extern check_int64
1094 dasm_put(Dst, 344);
1095 }
1096 //| str r0, [DATA], #4
1097 //| str r1, [DATA], #4
1098 dasm_put(Dst, 348);
1099 break;
1100
1101 case DOUBLE_TYPE:
1102 //| lcall extern check_double
1103 //| str r0, [DATA], #4
1104 //| str r1, [DATA], #4
1105 dasm_put(Dst, 351);
1106 break;
1107
1108 case INTPTR_TYPE:
1109 //| lcall extern check_intptr
1110 //| str r0, [DATA], #4
1111 dasm_put(Dst, 357);
1112 break;
1113
1114 case FLOAT_TYPE:
1115 //| lcall extern check_float
1116 //| str r0, [DATA], #4
1117 dasm_put(Dst, 362);
1118 break;
1119
1120 default:
1121 luaL_error(L, "NYI: call arg type");
1122 }
1123 }
1124 }
1125
1126 if (ct->has_var_arg) {
1127 //| mov r3, DATA
1128 //| mov r2, TOP
1129 //| mov r1, #nargs+1
1130 //| lcall extern unpack_varargs_stack
1131 dasm_put(Dst, 367, nargs+1);
1132 }
1133
1134 //| load32 r0, &Dst->last_errno
1135 //| ldr r0, [r0]
1136 //| bl extern SetLastError
1137 dasm_put(Dst, 375, (uintptr_t)(&Dst->last_errno));
1138
1139 //| pop {r0, r1, r2, r3} // this pop is balanced with the sub sp, #16
1140 //| bl extern FUNCTION
1141 dasm_put(Dst, 384);
1142
1143 //|.macro get_errno
1144 //| bl extern GetLastError
1145 //| load32 r1, &Dst->last_errno
1146 //| str r0, [r1]
1147 //|.endmacro
1148
1149 //|.macro return
1150 //| ldmdb r11, {TOP, L_ARG, DATA, r11, sp, pc}
1151 //|.endmacro
1152
1153 lua_rawgeti(L, ct_usr, 0);
1154 mt = (const struct ctype*) lua_touserdata(L, -1);
1155
1156 if (mt->pointers || mt->is_reference) {
1157 lua_getuservalue(L, -1);
1158 num_upvals += 2;
1159 //| mov DATA, r0
1160 //| get_errno
1161 //| ldr r2, [pc, #4]
1162 //| ldr r1, [pc, #4]
1163 //| b >1
1164 //|.long mt, lua_upvalueindex(num_upvals)
1165 //|1:
1166 //| lcall extern push_cdata
1167 //| str DATA, [r0]
1168 //| mov r0, #1
1169 //| return
1170 dasm_put(Dst, 388, (uintptr_t)(&Dst->last_errno), (uintptr_t)(mt), (uintptr_t)(lua_upvalueindex(num_upvals)));
1171
1172 } else {
1173 switch (mt->type) {
1174 case INT64_TYPE:
1175 num_upvals++;
1176 //| mov DATA, r0
1177 //| mov DATA2, r1
1178 //| get_errno
1179 //| lcall extern lua_pushnil
1180 //| load32 r2, mt
1181 //| mvn r1, #0 // -1
1182 //| lcall extern push_cdata
1183 //| str DATA, [r0]
1184 //| str DATA2, [r0, #4]
1185 //| mov r0, #1
1186 //| return
1187 dasm_put(Dst, 411, (uintptr_t)(&Dst->last_errno), (uintptr_t)(mt));
1188 break;
1189
1190 case INTPTR_TYPE:
1191 num_upvals++;
1192 //| mov DATA, r0
1193 //| get_errno
1194 //| lcall extern lua_pushnil
1195 //| load32 r2, mt
1196 //| mvn r1, #0 // -1
1197 //| lcall extern push_cdata
1198 //| str DATA, [r0]
1199 //| mov r0, #1
1200 //| return
1201 dasm_put(Dst, 438, (uintptr_t)(&Dst->last_errno), (uintptr_t)(mt));
1202 break;
1203
1204 case VOID_TYPE:
1205 lua_pop(L, 1);
1206 //| get_errno
1207 //| mov r0, #0
1208 //| return
1209 dasm_put(Dst, 463, (uintptr_t)(&Dst->last_errno));
1210 break;
1211
1212 case BOOL_TYPE:
1213 lua_pop(L, 1);
1214 //| mov DATA, r0
1215 //| get_errno
1216 //| mov r1, DATA
1217 //| lcall extern lua_pushboolean
1218 //| mov r0, #1
1219 //| return
1220 dasm_put(Dst, 474, (uintptr_t)(&Dst->last_errno));
1221 break;
1222
1223 case INT8_TYPE:
1224 case INT16_TYPE:
1225 case INT32_TYPE:
1226 case ENUM_TYPE:
1227 lua_pop(L, 1);
1228 //| mov DATA, r0
1229 //| get_errno
1230 //| mov r1, DATA
1231 dasm_put(Dst, 490, (uintptr_t)(&Dst->last_errno));
1232 if (mt->is_unsigned) {
1233 //| lcall extern push_uint
1234 dasm_put(Dst, 501);
1235 } else {
1236 //| lcall extern push_int
1237 dasm_put(Dst, 505);
1238 }
1239 //| mov r0, #1
1240 //| return
1241 dasm_put(Dst, 509);
1242 break;
1243
1244 case FLOAT_TYPE:
1245 lua_pop(L, 1);
1246 //| mov DATA, r0
1247 //| get_errno
1248 //| mov r1, DATA
1249 //| lcall extern push_float
1250 //| mov r0, #1
1251 //| return
1252 dasm_put(Dst, 512, (uintptr_t)(&Dst->last_errno));
1253 break;
1254
1255 case DOUBLE_TYPE:
1256 lua_pop(L, 1);
1257 //| mov DATA, r0
1258 //| mov DATA2, r1
1259 //| get_errno
1260 //| mov r2, DATA2
1261 //| mov r1, DATA
1262 //| lcall extern lua_pushnumber
1263 //| mov r0, #1
1264 //| return
1265 dasm_put(Dst, 528, (uintptr_t)(&Dst->last_errno));
1266 break;
1267
1268 default:
1269 luaL_error(L, "NYI: call return type");
1270 }
1271 }
1272
1273 assert(lua_gettop(L) == top + num_upvals);
1274 lua_pushcclosure(L, (lua_CFunction) compile(Dst, L, func, LUA_NOREF), num_upvals);
1275 }
1276