-
Notifications
You must be signed in to change notification settings - Fork 1
/
call_arm64.dasc
1645 lines (1495 loc) · 43 KB
/
call_arm64.dasc
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
/* 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 arm64
|.actionlist build_actionlist
|.globalnames globnames
|.externnames extnames
|.define L_ARG, x19
|.define TOP, x20
|.define rbp, x21
|.define EXTRA_REG_1, x22
|.define EXTRA_REG_2, x23
|.define EXTRA_REG_3, x24
#define ZERO_TO_FIFTEEN_OF_64(x) (0x000000000000FFFF & (int64_t)x)
#define SIXTEEN_TO_THIRTYONE_OF_64(x) ((0x00000000FFFF0000 & (int64_t)x) >> 16)
#define THIRTYTWO_TO_FORTYSEVEN_OF_64(x) ((0x0000FFFF00000000 & (int64_t)x) >> 32)
#define FORTYEIGHT_TO_SIXTYTHREE_OF_64(x) ((0xFFFF000000000000 & (int64_t)x) >> 48)
|.macro mov64, reg, imm
| movz reg, #(ZERO_TO_FIFTEEN_OF_64(imm))
| movk reg, #(SIXTEEN_TO_THIRTYONE_OF_64(imm)), lsl #16
| movk reg, #(THIRTYTWO_TO_FORTYSEVEN_OF_64(imm)), lsl #32
| movk reg, #(FORTYEIGHT_TO_SIXTYTHREE_OF_64(imm)), lsl #48
|.endmacro
|.macro mov32, reg, imm
| movz reg, #(ZERO_TO_FIFTEEN_OF_64(imm))
| movk reg, #(SIXTEEN_TO_THIRTYONE_OF_64(imm)), lsl #16
|.endmacro
| // the 5 and 6 arg forms are only used on posix x64
|.macro call_ririir, func, arg0, arg1, arg2, arg3, arg4, arg5
| mov x5, arg5
| mov x4, #arg4
| mov x3, #arg3
| mov x2, arg2
| mov x1, #arg1
| mov x0, arg0
| bl func
|.endmacro
|.macro call_rrrrrr, func, arg0, arg1, arg2, arg3, arg4, arg5
| mov x5, arg5
| mov x4, arg4
| mov x3, arg3
| mov x2, arg2
| mov x1, arg1
| mov x0, arg0
| bl func
|.endmacro
|.macro call_ririr, func, arg0, arg1, arg2, arg3, arg4
| mov x4, arg4
| mov x3, #arg3
| mov x2, arg2
| mov x1, #arg1
| mov x0, arg0
| bl func
|.endmacro
|.macro call_rrrrr, func, arg0, arg1, arg2, arg3, arg4
| mov x4, arg4
| mov x3, arg3
| mov x2, arg2
| mov x1, arg1
| mov x0, arg0
| bl func
|.endmacro
|
|.macro call_riwp, func, arg0, arg1, arg2, arg3
| mov64 x3, arg3
| mov w2, arg2
| mov x1, #arg1
| mov x0, arg0
| bl func
|.endmacro
|.macro call_rwwp, func, arg0, arg1, arg2, arg3
| mov64 x3, (arg3)
| mov w2, arg2
| mov w1, arg1
| mov x0, arg0
| bl func
|.endmacro
|.macro call_riip, func, arg0, arg1, arg2, arg3
| mov64 x3, (arg3)
| mov x2, #(arg2)
| mov x1, #(arg1)
| mov x0, arg0
| bl func
|.endmacro
|.macro call_rrrp, func, arg0, arg1, arg2, arg3
| mov64 x3, arg3
| mov x2, arg2
| mov x1, arg1
| mov x0, arg0
| bl func
|.endmacro
|.macro call_rrrr, func, arg0, arg1, arg2, arg3
| mov x3, arg3
| mov x2, arg2
| mov x1, arg1
| mov x0, arg0
| bl func
|.endmacro
|
|.macro call_rwp, func, arg0, arg1, arg2
| mov64 x2, arg2
| mov w1, arg1
| mov x0, arg0
| bl func
|.endmacro
|.macro call_rip, func, arg0, arg1, arg2
| mov64 x2, arg2
| mov x1, #arg1
| mov x0, arg0
| bl func
|.endmacro
|.macro call_rrp, func, arg0, arg1, arg2
| mov64 x2, arg2
| mov x1, arg1
| mov x0, arg0
| bl func
|.endmacro
|.macro call_rww, func, arg0, arg1, arg2
| mov w2, arg2
| mov w1, arg1
| mov x0, arg0
| bl func
|.endmacro
|.macro call_rwi, func, arg0, arg1, arg2
| mov x2, #(arg2)
| mov w1, arg1
| mov x0, arg0
| bl func
|.endmacro
|.macro call_rii, func, arg0, arg1, arg2
| mov x2, #(arg2)
| mov x1, #(arg1)
| mov x0, arg0
| bl func
|.endmacro
|.macro call_rrr, func, arg0, arg1, arg2
| mov x2, arg2
| mov x1, arg1
| mov x0, arg0
| bl func
|.endmacro
|
|.macro call_rp, func, arg0, arg1
| mov64 x1, arg1
| mov x0, arg0
| bl func
|.endmacro
|.macro call_ri, func, arg0, arg1
| mov x1, #(arg1)
| mov x0, arg0
| bl func
|.endmacro
|.macro call_rw, func, arg0, arg1
| mov w1, arg1
| mov x0, arg0
| bl func
|.endmacro
|.macro call_rr, func, arg0, arg1
| mov x1, arg1
| mov x0, arg0
| bl func
|.endmacro
|
|.macro call_w, func, arg0
| mov w0, arg0
| bl func
|.endmacro
|.macro call_r, func, arg0
| mov x0, arg0
| bl func
|.endmacro
|
|.macro call_debug
| sub sp, sp, #(16*8)
| str x0, [sp]
| str x1, [sp,#8]
| str x2, [sp,#16]
| str x3, [sp,#24]
| str x4, [sp,#32]
| str x5, [sp,#40]
| str x6, [sp,#48]
| str x7, [sp,#56]
| str x8, [sp,#64]
| str x9, [sp,#72]
| str x10, [sp,#80]
| str x11, [sp,#88]
| str x12, [sp,#96]
| str x13, [sp,#104]
| str x14, [sp,#112]
| str x15, [sp,#120]
| bl extern debug_statement
| ldr x0, [sp]
| ldr x1, [sp,#8]
| ldr x2, [sp,#16]
| ldr x3, [sp,#24]
| ldr x4, [sp,#32]
| ldr x5, [sp,#40]
| ldr x6, [sp,#48]
| ldr x7, [sp,#56]
| ldr x8, [sp,#64]
| ldr x9, [sp,#72]
| ldr x10, [sp,#80]
| ldr x11, [sp,#88]
| ldr x12, [sp,#96]
| ldr x13, [sp,#104]
| ldr x14, [sp,#112]
| ldr x15, [sp,#120]
| add sp, sp, #(16*8)
|.endmacro
|.macro call_debug_float, one
| sub sp, sp, #(16*8)
| str d0, [sp]
| str d1, [sp,#16]
| str d2, [sp,#32]
| str d3, [sp,#48]
| str d4, [sp,#64]
| str d5, [sp,#80]
| str d6, [sp,#96]
| str d7, [sp,#112]
| fmov s0, one
| bl extern debug_float
| ldr d0, [sp]
| ldr d1, [sp,#16]
| ldr d2, [sp,#32]
| ldr d3, [sp,#48]
| ldr d4, [sp,#64]
| ldr d5, [sp,#80]
| ldr d6, [sp,#96]
| ldr d7, [sp,#112]
| add sp, sp, #(16*8)
|.endmacro
|.macro call_debug_double, one
| sub sp, sp, #(16*8)
| str d0, [sp]
| str d1, [sp,#16]
| str d2, [sp,#32]
| str d3, [sp,#48]
| str d4, [sp,#64]
| str d5, [sp,#80]
| str d6, [sp,#96]
| str d7, [sp,#112]
| fmov d0, one
| bl extern debug_double
| ldr d0, [sp]
| ldr d1, [sp,#16]
| ldr d2, [sp,#32]
| ldr d3, [sp,#48]
| ldr d4, [sp,#64]
| ldr d5, [sp,#80]
| ldr d6, [sp,#96]
| ldr d7, [sp,#112]
| add sp, sp, #(16*8)
|.endmacro
|.macro call_debug_int, one
| sub sp, sp, #(16*8)
| str x0, [sp]
| str x1, [sp,#8]
| str x2, [sp,#16]
| str x3, [sp,#24]
| str x4, [sp,#32]
| str x5, [sp,#40]
| str x6, [sp,#48]
| str x7, [sp,#56]
| str x8, [sp,#64]
| str x9, [sp,#72]
| str x10, [sp,#80]
| str x11, [sp,#88]
| str x12, [sp,#96]
| str x13, [sp,#104]
| str x14, [sp,#112]
| str x15, [sp,#120]
| mov x0, one
| bl extern debug_int
| ldr x0, [sp]
| ldr x1, [sp,#8]
| ldr x2, [sp,#16]
| ldr x3, [sp,#24]
| ldr x4, [sp,#32]
| ldr x5, [sp,#40]
| ldr x6, [sp,#48]
| ldr x7, [sp,#56]
| ldr x8, [sp,#64]
| ldr x9, [sp,#72]
| ldr x10, [sp,#80]
| ldr x11, [sp,#88]
| ldr x12, [sp,#96]
| ldr x13, [sp,#104]
| ldr x14, [sp,#112]
| ldr x15, [sp,#120]
| add sp, sp, #(16*8)
|.endmacro
|.macro call_debug_1, one
| sub sp, sp, #(16*8)
| str x0, [sp]
| str x1, [sp,#8]
| str x2, [sp,#16]
| str x3, [sp,#24]
| str x4, [sp,#32]
| str x5, [sp,#40]
| str x6, [sp,#48]
| str x7, [sp,#56]
| str x8, [sp,#64]
| str x9, [sp,#72]
| str x10, [sp,#80]
| str x11, [sp,#88]
| str x12, [sp,#96]
| str x13, [sp,#104]
| str x14, [sp,#112]
| str x15, [sp,#120]
| mov x0, one
| bl extern debug_statement_with_arg
| ldr x0, [sp]
| ldr x1, [sp,#8]
| ldr x2, [sp,#16]
| ldr x3, [sp,#24]
| ldr x4, [sp,#32]
| ldr x5, [sp,#40]
| ldr x6, [sp,#48]
| ldr x7, [sp,#56]
| ldr x8, [sp,#64]
| ldr x9, [sp,#72]
| ldr x10, [sp,#80]
| ldr x11, [sp,#88]
| ldr x12, [sp,#96]
| ldr x13, [sp,#104]
| ldr x14, [sp,#112]
| ldr x15, [sp,#120]
| add sp, sp, #(16*8)
|.endmacro
|.macro call_debug_sp
| mov x15, sp
| call_debug_1 x15
|.endmacro
|.macro epilog
| //ldr L_ARG, [rbp, #(-16)]
| //ldr TOP, [rbp, #(-8)]
| ldp L_ARG, TOP, [rbp, #(-16)]
| mov sp, rbp
| ldp rbp, EXTRA_REG_1, [sp]
| add sp, sp, #16
| ldp EXTRA_REG_2, EXTRA_REG_3, [sp]
| add sp, sp, #16
| ldp x29, x30, [sp]
| add sp, sp, #16
| ret
|.endmacro
|.macro get_errno // note trashes registers
| bl extern GetLastError
| mov64 x9, perr
| str w0, [x9]
|.endmacro
#define MESSAGE_1 "Too few arguments"
#define MESSAGE_2 "Too many arguments"
|.macro too_few_arguments
| mov64 x9, MESSAGE_1
| call_rr extern luaL_error, L_ARG, x9
|.endmacro
|.macro too_many_arguments
| mov64 x9, MESSAGE_2
| call_rr extern luaL_error, L_ARG, x9
|.endmacro
|.macro lua_return_arg
| mov x0, #1
| epilog
|.endmacro
|.macro lua_return_void
| get_errno
| mov x0, xzr
| epilog
|.endmacro
|.macro lua_return_double
| str d0, [sp, #32]
| get_errno
| ldr d0, [sp, #32]
| mov x0, L_ARG
| bl extern lua_pushnumber
| lua_return_arg
|.endmacro
|.macro lua_return_bool
| ubfm x0, x0, #0, #7
| str x0, [sp,#32]
| get_errno
| ldr w9, [sp,#32]
| call_rw extern lua_pushboolean, L_ARG, w9
| lua_return_arg
|.endmacro
|.macro lua_return_int
| //mov [sp,#32], eax
| str x0, [sp,#32]
| get_errno
| ldr w9, [sp,#32]
| call_rw extern push_int, L_ARG, w9
| lua_return_arg
|.endmacro
|.macro lua_return_uint
| str x0, [sp,#32]
| get_errno
| ldr w9, [sp,#32]
| call_rw extern push_uint, L_ARG, w9
| lua_return_arg
|.endmacro
|.macro lua_return_long
| str x0, [sp,#32]
| get_errno
| ldr x0, [sp,#32]
| call_rr extern lua_pushinteger, L_ARG, x0
| lua_return_arg
|.endmacro
|.macro lua_return_ulong
| str x0, [sp,#32]
| get_errno
| ldr x0, [sp,#32]
| call_rr extern lua_pushinteger, L_ARG, x0
| lua_return_arg
|.endmacro
|.macro save_registers
| // use rbp relative so we store values in the outer stack frame
| // Floating point registers
| str d0, [rbp, #-32]
| str d1, [rbp, #-48]
| str d2, [rbp, #-64]
| str d3, [rbp, #-80]
| str d4, [rbp, #-88]
| str d5, [rbp, #-96]
| str d6, [rbp, #-112]
| str d7, [rbp, #-128]
| // Integral registers
| str x0, [rbp, #-136]
| str x1, [rbp, #-144]
| str x2, [rbp, #-152]
| str x3, [rbp, #-160]
| str x4, [rbp, #-168]
| str x5, [rbp, #-176]
| str x6, [rbp, #-184]
| str x7, [rbp, #-192]
|.endmacro
#define JUMP_SIZE 24
#define MIN_BRANCH ((INT32_MIN) >> 6)
#define MAX_BRANCH ((INT32_MAX) >> 6)
#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 farther than +/-128MB.
*
* Note we have to manually set this up since there are commands buffered
* in the jit state.
*/
//printf("%s:%d code = [%p]\n", __FILE__, __LINE__, code);
*((cfunction*) (&code[0])) = func;
*((uint32_t*) (&code[8])) = (uint32_t)0x10FFFFC9U; // ADR X9, #-8
*((uint32_t*) (&code[12])) = (uint32_t)0xF940012aU; // LDR X10, [X9]
*((uint32_t*) (&code[16])) = (uint32_t)0xD61F0140U; // BR X10
*((uint32_t*) (&code[20])) = (uint32_t)0x0; // NULL
//printf("%s:%d addr = [%p] sizeof(cfunction) = [%zu] function=[%p]\n",
//__FILE__, __LINE__, &(code[8]), sizeof(cfunction), (*((cfunction*)(&code[0]))));
}
void compile_globals(struct jit* jit, lua_State* L)
{
struct jit* Dst = jit;
int* perr = &jit->last_errno;
dasm_setup(Dst, build_actionlist);
/* Note: since the return code uses EBP to reset the stack pointer, we
* don't have to track the amount of stack space used. It also means we
* can handle stdcall and cdecl with the same code.
*/
/* Note the various call_* functions want 32 bytes of 16 byte aligned
* stack
*/
compile(Dst, L, NULL, LUA_NOREF);
}
int arm64_return_size(lua_State* L, int usr, const struct ctype* ct)
{
int ret = 0;
const struct ctype* mt;
if (ct->calling_convention != C_CALL) {
size_t i;
size_t argn = lua_rawlen(L, usr);
for (i = 1; i <= argn; i++) {
lua_rawgeti(L, usr, (int) i);
mt = (const struct ctype*) lua_touserdata(L, -1);
if (mt->pointers || mt->is_reference) {
ret += sizeof(void*);
} else {
switch (mt->type) {
case DOUBLE_TYPE:
case COMPLEX_FLOAT_TYPE:
case INT64_TYPE:
ret += 8;
break;
case COMPLEX_DOUBLE_TYPE:
ret += 16;
break;
case INTPTR_TYPE:
ret += sizeof(intptr_t);
break;
case FUNCTION_PTR_TYPE:
ret += sizeof(cfunction);
break;
case BOOL_TYPE:
case FLOAT_TYPE:
case INT8_TYPE:
case INT16_TYPE:
case INT32_TYPE:
case ENUM_TYPE:
ret += 4;
break;
default:
return luaL_error(L, "NYI - argument type");
}
}
lua_pop(L, 1);
}
}
return ret;
}
#define MAX_INT_REGISTERS(ct) 8 /* x0-x7 */
#define MAX_FLOAT_REGISTERS(ct) 8 /* q0-q7 */
struct reg_alloc {
int floats;
int ints;
int off;
};
#define REGISTER_STACK_SPACE(ct) ((8*8) + (128*8)) /* 8, 64 bit registers x0-x7 and 8, 128 bit registers v0-v7 */
/* Fastcall:
* Uses ecx, edx as first two int registers
* Everything else on stack (include 64bit ints)
* No overflow stack space
* Pops the stack before returning
* Returns int in eax, float in ST0
* We use the same register allocation logic as posix x64 with 2 int regs and 0 float regs
*/
typedef enum {
int8,
int16,
int32,
int64,
int128
} int_type_enum;
static void get_int(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int_type_enum int_type)
{
/* grab the register from the shadow space */
if (reg->ints < MAX_INT_REGISTERS(ct)) {
int x = -(136 + 8*reg->ints);
| ldr x9, [rbp , #(x)]
reg->ints++;
}
else {
switch (int_type) {
case int8:
#ifdef OS_OSX
| ldrb w9, [rbp , #(reg->off)]
reg->off += 1;
break;
#endif
case int16:
#ifdef OS_OSX
| ldrh w9, [rbp , #(reg->off)]
reg->off += 2;
break;
#endif
case int32:
| ldr w9, [rbp , #(reg->off)]
#ifdef OS_OSX
reg->off += 4;
#else
reg->off += 8;
#endif
break;
case int64:
| ldr x9, [rbp , #(reg->off)] // x9 is a scratch register
reg->off += 8;
break;
case int128:
default:
fprintf(stderr, "Unsupported data type\n");
abort();
break;
}
}
}
static void add_int(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int_type_enum int_type)
{
if (reg->ints < MAX_INT_REGISTERS(ct)) {
| str x0, [sp , #(32 + 8*reg->ints)]
reg->ints++;
}
else {
#ifndef OS_OSX
if (reg->off % 8 != 0) {
reg->off += 8 - (reg->off % 8);
}
#endif
switch (int_type) {
case int8:
#ifdef OS_OSX
| strb w0, [sp , #(reg->off)]
reg->off += 1;
break;
#endif
case int16:
#ifdef OS_OSX
| strh w0, [sp , #(reg->off)]
reg->off += 2;
break;
#endif
case int32:
| str w0, [sp , #(reg->off)]
#ifdef OS_OSX
reg->off += 4;
#else
reg->off += 8;
#endif
break;
case int64:
| str x0, [sp , #(reg->off)]
reg->off += 8;
break;
case int128:
default:
fprintf(stderr, "Unsupported data type\n");
abort();
break;
}
}
}
static void get_float(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_double)
{
int off;
if (reg->floats < MAX_FLOAT_REGISTERS(ct)) {
off = -32 - 16*reg->floats;
reg->floats++;
}
else {
off = reg->off;
if (is_double) {
reg->off += 8;
}
else {
#ifdef OS_OSX
reg->off += 4;
#else
reg->off += 8;
#endif
}
}
if (is_double) {
| ldr d0, [rbp, #off]
} else {
| ldr s16, [rbp, #off] // s16 is a scratch register
| fcvt d0, s16
}
}
static void get_complex_float(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_double)
{
if (reg->floats < (MAX_FLOAT_REGISTERS(ct)-1)) {
int off = 0;
if (is_double) {
off = -32 - 16*reg->floats;
| ldr d0, [rbp, #off]
reg->floats++;
off = -32 - 16*reg->floats;
| ldr d1, [rbp, #off]
reg->floats++;
} else {
off = -32 - 16*reg->floats;
| ldr s0, [rbp, #off] // s16 is a scratch register
reg->floats++;
off = -32 - 16*reg->floats;
| ldr s1, [rbp, #off] // s16 is a scratch register
reg->floats++;
}
}
else {
if (reg->floats == (MAX_FLOAT_REGISTERS(ct)-1)) {
reg->floats = MAX_FLOAT_REGISTERS(ct);
}
if (is_double) {
| ldr d0, [rbp, #(reg->off)]
reg->off += 8;
| ldr d1, [rbp, #(reg->off)]
reg->off += 8;
} else {
| ldr s0, [rbp, #(reg->off)] // s16 is a scratch register
#ifdef OS_OSX
reg->off += 4;
#else
reg->off += 8;
#endif
| ldr s1, [rbp, #(reg->off)] // s16 is a scratch register
#ifdef OS_OSX
reg->off += 4;
#else
reg->off += 8;
#endif
}
}
}
static void add_float(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_double)
{
if (reg->floats < MAX_FLOAT_REGISTERS(ct)) {
if (is_double) {
| str d0, [sp , #(32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats))]
} else {
| fcvt s0, d0 // This is necessary because in lua float is also
// handled as doube
| str s0, [sp, #(32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats))]
}
reg->floats++;
}
else if (is_double) {
| str d0, [sp, #(reg->off)]
reg->off += 8;
} else {
| fcvt s0, d0
| str s0, [sp, #(reg->off)]
#ifdef OS_OSX
reg->off += 4;
#else
reg->off += 8;
#endif
}
}
static void add_complex_float(Dst_DECL, const struct ctype* ct, struct reg_alloc* reg, int is_double)
{
//printf("%s:%d floats = [%d]\n", __FILE__, __LINE__, reg->floats);
if (reg->floats < (MAX_FLOAT_REGISTERS(ct)-1)) {
if (is_double) {
| str d0, [sp , #(32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats))]
reg->floats++;
| str d1, [sp , #(32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats))]
reg->floats++;
} else {
| str s0, [sp, #(32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats))]
reg->floats++;
| str s1, [sp, #(32 + 8*(MAX_INT_REGISTERS(ct) + reg->floats))]
reg->floats++;
}
if (reg->floats > (MAX_FLOAT_REGISTERS(ct)-1)) {
reg->floats = MAX_FLOAT_REGISTERS(ct);
}
}
else {
//printf("%s:%d floats = [%d]\n", __FILE__, __LINE__, reg->floats);
if (reg->floats == (MAX_FLOAT_REGISTERS(ct)-1)) {
reg->floats = MAX_FLOAT_REGISTERS(ct);
}
//printf("%s:%d floats = [%d]\n", __FILE__, __LINE__, reg->floats);
if (is_double) {
| str d0, [sp, #(reg->off)]
reg->off += 8;
| str d1, [sp, #(reg->off)]
reg->off += 8;
} else {
| str s0, [sp, #(reg->off)]
#ifdef OS_OSX
reg->off += 4;
#else
reg->off += 8;
#endif
| str s1, [sp, #(reg->off)]
#ifdef OS_OSX
reg->off += 4;
#else
reg->off += 8;
#endif
}
}
}
#define add_pointer(jit, ct, reg) add_int(jit, ct, reg, int64)
#define get_pointer(jit, ct, reg) get_int(jit, ct, reg, int64)
cfunction compile_callback(lua_State* L, int fidx, int ct_usr, const struct ctype* ct)
{
int i, nargs;
cfunction* pf;
struct ctype ct2 = *ct;
const struct ctype* mt;
struct reg_alloc reg;
int num_upvals = 0;
int top = lua_gettop(L);
struct jit* Dst = get_jit(L);
int ref;
int hidden_arg_off = 0;
ct_usr = lua_absindex(L, ct_usr);
fidx = lua_absindex(L, fidx);
assert(lua_isnil(L, fidx) || lua_isfunction(L, fidx));
memset(®, 0, sizeof(reg));
reg.off = 48;
dasm_setup(Dst, build_actionlist);
// add a table to store ctype and function upvalues
// callback_set assumes the first value is the lua function
nargs = (int) lua_rawlen(L, ct_usr);
lua_newtable(L);
lua_pushvalue(L, -1);
ref = luaL_ref(L, LUA_REGISTRYINDEX);
if (ct->has_var_arg) {
luaL_error(L, "can't create callbacks with varargs");
}
// setup a stack frame to hold args for the call into lua_call
| sub sp, sp, #64 // = -64
| stp x29, x30, [sp, #48] // 16-byte Folded Spill
| stp EXTRA_REG_2, EXTRA_REG_3, [sp, #32] // 16-byte Folded Spill
| stp rbp, EXTRA_REG_1, [sp, #16] // 16-byte Folded Spill
| stp L_ARG, TOP, [sp]
| // -64 -56 -48 -40 -32 -24 -16 -8
| // stack is -64 (mod 16) (L_ARG, TOP, rbp, EXTRA_REG_1, EXTRA_REG_2, EXTRA_REG_3, x29, x30)
| // L_ARG -> -64
| // TOP -> -56
| // rbp -> -48
| // EXTRA_REG_1 -> -40
| // EXTRA_REG_2 -> -32
| // EXTRA_REG_3 -> -24
| // x29 -> -16
| // x30 -> -8
| //
| add rbp, sp, #16
|
| // stack -64 (mod 16) (L_ARG, rbp, rip)
| // 16 for return vars, 32 for local calls, rest to save registers
| sub sp, sp, #(16 + 32 + REGISTER_STACK_SPACE(ct))
| save_registers
| mov64 L_ARG, L
/* get the upval table */
| mov32 w9, LUA_REGISTRYINDEX
| mov32 w10, ref
| call_rww extern lua_rawgeti, L_ARG, w9, w10
/* get the lua function */
lua_pushvalue(L, fidx);
lua_rawseti(L, -2, ++num_upvals);
assert(num_upvals == CALLBACK_FUNC_USR_IDX);
| mov32 w9, -1
| call_rwi extern lua_rawgeti, L_ARG, w9, num_upvals
(void) hidden_arg_off;
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 */
/* on the lua stack in the callback:
* upval tbl, lua func, i-1 args
*/
| mov32 w9, -i-1
| call_rwi extern lua_rawgeti, L_ARG, w9, num_upvals-1
| mov32 w9, -1
| call_rwp extern push_cdata, L_ARG, w9, mt
get_pointer(Dst, ct, ®);
| str x9, [x0]
| mov32 w9, -2
| call_rw, extern lua_remove, L_ARG, w9
} else {
switch (mt->type) {
case INT64_TYPE:
lua_getuservalue(L, -1);
lua_rawseti(L, -3, ++num_upvals); /* mt */
lua_pop(L, 1);
| call_rip extern push_cdata, L_ARG, 0, mt
get_int(Dst, ct, ®, int64);
| str x9, [x0]
break;
case INTPTR_TYPE:
lua_getuservalue(L, -1);
lua_rawseti(L, -3, ++num_upvals); /* mt */
lua_pop(L, 1);
| call_rip extern push_cdata, L_ARG, 0, mt
get_pointer(Dst, ct, ®);
| str x9, [x0]
break;
case COMPLEX_FLOAT_TYPE:
lua_pop(L, 1);
/* complex floats are two floats packed into a double */
| call_rip extern push_cdata, L_ARG, 0, mt
get_complex_float(Dst, ct, ®, 0);
| str s0, [x0]
| str s1, [x0, #4]
break;
case COMPLEX_DOUBLE_TYPE:
lua_pop(L, 1);
| call_rip extern push_cdata, L_ARG, 0, mt
get_complex_float(Dst, ct, ®, 1);
| str d0, [x0]
| str d1, [x0, #8]
break;
case FLOAT_TYPE:
lua_pop(L, 1);
get_float(Dst, ct, ®, 0);
| // for 64bit xmm0 is already set
| mov x0, L_ARG
| bl extern lua_pushnumber
break;
case DOUBLE_TYPE:
lua_pop(L, 1);
get_float(Dst, ct, ®, 1);
| // for 64bit xmm0 is already set
| mov x0, L_ARG
| bl extern lua_pushnumber
break;
case BOOL_TYPE:
lua_pop(L, 1);
get_int(Dst, ct, ®, int8);
| //movzx ecx, cl
| cmp w9, #0
| beq >1
| mov b9, #1
| 1:
| ubfm x9, x9, #0, #7
| call_rr extern lua_pushboolean, L_ARG, x9
break;
case INT8_TYPE:
lua_pop(L, 1);
get_int(Dst, ct, ®, int8);
if (mt->is_unsigned) {
| ubfm x9, x9, #0, #7
} else {
| sbfm x9, x9, #0, #7
}
| call_rr extern push_int, L_ARG, x9