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