lamina 0.0.10

High-performance compiler backend for Lamina Intermediate Representation
Documentation
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
//! Dynamic `extern "C"` calls with a runtime-known `i64` argument count.
//!
//! ## C limits
//!
//! ISO C does **not** specify a maximum parameter count. Informative minimums (e.g. 127
//! parameters in a prototype) appear in older annexes; **GCC** and **Clang** accept very
//! large lists in practice. Lamina uses [`MAX_JIT_ARGS`] as a **host policy cap** aligned
//! with “what real toolchains allow,” not a standard ceiling.
//!
//! ## ABI
//!
//! - **AArch64**: AAPCS64 (first eight integer/pointer args in `x0`–`x7`, then stack).
//! - **x86_64** (non-Windows): System V AMD64 (`rdi`…`r9`, then stack).
//! - **Other hosts** (Windows x86_64, non-x86_64, etc.): fixed transmute table for up to **15**
//!   `i64` parameters (same as the previous executor path). [`MAX_JIT_ARGS`] is **15** there.
//!
//! Prefer packing parameters for hot paths; see [`JIT_ARG_SOFT_WARN_THRESHOLD`] for hints.


#[cfg(any(
    target_arch = "aarch64",
    all(target_arch = "x86_64", not(target_os = "windows")),
))]
/// Upper bound for [`call_function_dynamic`] on AArch64 and SysV x86_64 (stack shim).
pub const MAX_JIT_ARGS: usize = crate::mir_codegen::MAX_MIR_CALL_PARAMETERS;

#[cfg(not(any(
    target_arch = "aarch64",
    all(target_arch = "x86_64", not(target_os = "windows")),
)))]
/// Upper bound where only the transmute table is available (no generic stack path).
pub const MAX_JIT_ARGS: usize = 15;

/// Past this count, tooling may warn: extra arguments use the stack under the C ABI.
pub const JIT_ARG_SOFT_WARN_THRESHOLD: usize = 8;

#[cfg(target_arch = "aarch64")]
/// Call an arbitrary C-ABI function at `function_ptr` with up to [`MAX_JIT_ARGS`] `i64` args.
///
/// # Safety
///
/// - `function_ptr` must point to valid, executable code that follows AAPCS64.
/// - `args` must be the exact set of arguments expected by the callee.
/// - The callee must be safe to call from the current thread context.
pub unsafe fn call_function_dynamic(
    function_ptr: *const u8,
    args: &[i64],
    returns_value: bool,
) -> Option<i64> {
    if function_ptr.is_null() {
        return None;
    }
    if args.len() > MAX_JIT_ARGS {
        return None;
    }
    if !(function_ptr as usize).is_multiple_of(4) {
        return None;
    }

    let n = args.len();
    if n == 0 {
        let mut out: i64 = 0;
        unsafe {
            std::arch::asm!(
                "mov x16, {fp}",
                "mov x8, xzr",
                "blr x16",
                fp = in(reg) function_ptr,
                lateout("x0") out,
                clobber_abi("C"),
            );
        }
        return if returns_value { Some(out) } else { None };
    }

    let mut regbuf = [0i64; 8];
    let reg_fill = n.min(8);
    regbuf[..reg_fill].copy_from_slice(&args[..reg_fill]);

    let stack_n = n.saturating_sub(8);
    let rp = regbuf.as_ptr();
    let mut out: i64 = 0;
    if stack_n == 0 {
        unsafe {
            std::arch::asm!(
                "mov x16, {fp}",
                "ldr x0, [{rp}]",
                "ldr x1, [{rp}, #8]",
                "ldr x2, [{rp}, #16]",
                "ldr x3, [{rp}, #24]",
                "ldr x4, [{rp}, #32]",
                "ldr x5, [{rp}, #40]",
                "ldr x6, [{rp}, #48]",
                "ldr x7, [{rp}, #56]",
                "mov x8, xzr",
                "blr x16",
                fp = in(reg) function_ptr,
                rp = in(reg) rp,
                lateout("x0") out,
                clobber_abi("C"),
            );
        }
    } else {
        // Outgoing stack args must not be placed by subtracting sp in asm only: LLVM may use
        // stack slots below the pre-asm sp, so we copy the tail into a heap buffer and point
        // sp at it for the call, then restore the host sp.
        //
        // Keep the saved sp in x20 and declare `lateout("x20") _`: (1) addresses passed via
        // `in(reg)` are in caller-saved registers and the JIT callee may clobber them before we
        // reload; (2) AArch64 Rust reserves x19 for LLVM, so it cannot be an asm operand.
        let byte_len = stack_n * core::mem::size_of::<i64>() + 16;
        let backing = vec![0u8; byte_len];
        let base = backing.as_ptr() as usize;
        let call_sp = (base + 15) & !15;
        let dst = call_sp as *mut i64;
        unsafe {
            core::ptr::copy_nonoverlapping(args.as_ptr().add(8), dst, stack_n);
        }
        unsafe {
            std::arch::asm!(
                "mov x20, sp",
                "mov sp, {csp}",
                "mov x16, {fp}",
                "ldr x0, [{rp}]",
                "ldr x1, [{rp}, #8]",
                "ldr x2, [{rp}, #16]",
                "ldr x3, [{rp}, #24]",
                "ldr x4, [{rp}, #32]",
                "ldr x5, [{rp}, #40]",
                "ldr x6, [{rp}, #48]",
                "ldr x7, [{rp}, #56]",
                "mov x8, xzr",
                "blr x16",
                "mov sp, x20",
                fp = in(reg) function_ptr,
                rp = in(reg) rp,
                csp = in(reg) call_sp,
                lateout("x0") out,
                lateout("x20") _,
                clobber_abi("C"),
            );
        }
    }

    if returns_value { Some(out) } else { None }
}

#[cfg(all(target_arch = "x86_64", not(target_os = "windows")))]
/// Call an arbitrary C-ABI function at `function_ptr` with up to [`MAX_JIT_ARGS`] `i64` args.
///
/// # Safety
///
/// - `function_ptr` must point to valid, executable code that follows the System V AMD64 ABI.
/// - `args` must be the exact set of arguments expected by the callee.
/// - The callee must be safe to call from the current thread context.
pub unsafe fn call_function_dynamic(
    function_ptr: *const u8,
    args: &[i64],
    returns_value: bool,
) -> Option<i64> {
    if function_ptr.is_null() {
        return None;
    }
    if args.len() > MAX_JIT_ARGS {
        return None;
    }
    if args.len() <= 15 {
        return unsafe { transmute_dynamic_call(function_ptr, args, returns_value) };
    }

    let n = args.len();
    if n == 0 {
        let out: i64;
        unsafe {
            std::arch::asm!(
                "mov r11, {fp}",
                "call r11",
                fp = in(reg) function_ptr,
                lateout("rax") out,
                clobber_abi("C"),
            );
        }
        return if returns_value { Some(out) } else { None };
    }

    let mut regbuf = [0i64; 6];
    let reg_fill = n.min(6);
    regbuf[..reg_fill].copy_from_slice(&args[..reg_fill]);

    let stack_n = n.saturating_sub(6);
    let stack_src: *const i64 = if stack_n > 0 {
        args.as_ptr().wrapping_add(6)
    } else {
        args.as_ptr()
    };

    let rp = regbuf.as_ptr();
    let mut out: i64;

    if stack_n == 0 {
        unsafe {
            std::arch::asm!(
                "mov r11, {fp}",
                "mov r10, {rp}",
                "mov rdi, [r10]",
                "mov rsi, [r10 + 8]",
                "mov rdx, [r10 + 16]",
                "mov rcx, [r10 + 24]",
                "mov r8, [r10 + 32]",
                "mov r9, [r10 + 40]",
                "call r11",
                fp = in(reg) function_ptr,
                rp = in(reg) rp,
                lateout("rax") out,
                clobber_abi("C"),
            );
        }
    } else {
        // Copy outgoing stack args into a heap buffer and point rsp at it for the
        // call, then restore the host sp. Same rationale as the AArch64 path:
        // adjusting rsp in asm alone can clobber Rust stack slots below the frame.
        let byte_len = stack_n * core::mem::size_of::<i64>() + 16;
        let backing = vec![0u8; byte_len];
        let call_sp = (backing.as_ptr() as usize + 15) & !15;
        let dst = call_sp as *mut i64;
        unsafe {
            core::ptr::copy_nonoverlapping(stack_src, dst, stack_n);
        }
        let mut saved_sp = 0usize;
        let saved_sp_ptr = &mut saved_sp as *mut usize;
        unsafe {
            std::arch::asm!(
                "mov [{sp}], rsp",
                "mov rsp, {csp}",
                "mov r11, {fp}",
                "mov r10, {rp}",
                "mov rdi, [r10]",
                "mov rsi, [r10 + 8]",
                "mov rdx, [r10 + 16]",
                "mov rcx, [r10 + 24]",
                "mov r8, [r10 + 32]",
                "mov r9, [r10 + 40]",
                "call r11",
                "mov rsp, [{sp}]",
                fp = in(reg) function_ptr,
                rp = in(reg) rp,
                csp = in(reg) call_sp,
                sp = in(reg) saved_sp_ptr,
                lateout("rax") out,
                clobber_abi("C"),
            );
        }
    }

    if returns_value { Some(out) } else { None }
}

#[cfg(not(any(
    target_arch = "aarch64",
    all(target_arch = "x86_64", not(target_os = "windows")),
)))]
pub unsafe fn call_function_dynamic(
    function_ptr: *const u8,
    args: &[i64],
    returns_value: bool,
) -> Option<i64> { unsafe {
    if function_ptr.is_null() {
        return None;
    }
    if args.len() > MAX_JIT_ARGS {
        return None;
    }
    transmute_dynamic_call(function_ptr, args, returns_value)
}}

unsafe fn transmute_dynamic_call(
    function_ptr: *const u8,
    args: &[i64],
    returns_value: bool,
) -> Option<i64> { unsafe {
    match args.len() {
        0 => {
            if returns_value {
                let callee: unsafe extern "C" fn() -> i64 = std::mem::transmute(function_ptr);
                Some(callee())
            } else {
                let callee: unsafe extern "C" fn() = std::mem::transmute(function_ptr);
                callee();
                None
            }
        }
        1 => {
            let a0 = args[0];
            if returns_value {
                let callee: unsafe extern "C" fn(i64) -> i64 = std::mem::transmute(function_ptr);
                Some(callee(a0))
            } else {
                let callee: unsafe extern "C" fn(i64) = std::mem::transmute(function_ptr);
                callee(a0);
                None
            }
        }
        2 => {
            let a0 = args[0];
            let a1 = args[1];
            if returns_value {
                let callee: unsafe extern "C" fn(i64, i64) -> i64 =
                    std::mem::transmute(function_ptr);
                Some(callee(a0, a1))
            } else {
                let callee: unsafe extern "C" fn(i64, i64) = std::mem::transmute(function_ptr);
                callee(a0, a1);
                None
            }
        }
        3 => {
            let a0 = args[0];
            let a1 = args[1];
            let a2 = args[2];
            if returns_value {
                let callee: unsafe extern "C" fn(i64, i64, i64) -> i64 =
                    std::mem::transmute(function_ptr);
                Some(callee(a0, a1, a2))
            } else {
                let callee: unsafe extern "C" fn(i64, i64, i64) = std::mem::transmute(function_ptr);
                callee(a0, a1, a2);
                None
            }
        }
        4 => {
            let a0 = args[0];
            let a1 = args[1];
            let a2 = args[2];
            let a3 = args[3];
            if returns_value {
                let callee: unsafe extern "C" fn(i64, i64, i64, i64) -> i64 =
                    std::mem::transmute(function_ptr);
                Some(callee(a0, a1, a2, a3))
            } else {
                let callee: unsafe extern "C" fn(i64, i64, i64, i64) =
                    std::mem::transmute(function_ptr);
                callee(a0, a1, a2, a3);
                None
            }
        }
        5 => {
            let a0 = args[0];
            let a1 = args[1];
            let a2 = args[2];
            let a3 = args[3];
            let a4 = args[4];
            if returns_value {
                let callee: unsafe extern "C" fn(i64, i64, i64, i64, i64) -> i64 =
                    std::mem::transmute(function_ptr);
                Some(callee(a0, a1, a2, a3, a4))
            } else {
                let callee: unsafe extern "C" fn(i64, i64, i64, i64, i64) =
                    std::mem::transmute(function_ptr);
                callee(a0, a1, a2, a3, a4);
                None
            }
        }
        6 => {
            let a0 = args[0];
            let a1 = args[1];
            let a2 = args[2];
            let a3 = args[3];
            let a4 = args[4];
            let a5 = args[5];
            if returns_value {
                let callee: unsafe extern "C" fn(i64, i64, i64, i64, i64, i64) -> i64 =
                    std::mem::transmute(function_ptr);
                Some(callee(a0, a1, a2, a3, a4, a5))
            } else {
                let callee: unsafe extern "C" fn(i64, i64, i64, i64, i64, i64) =
                    std::mem::transmute(function_ptr);
                callee(a0, a1, a2, a3, a4, a5);
                None
            }
        }
        7 => {
            let a0 = args[0];
            let a1 = args[1];
            let a2 = args[2];
            let a3 = args[3];
            let a4 = args[4];
            let a5 = args[5];
            let a6 = args[6];
            if returns_value {
                let callee: unsafe extern "C" fn(i64, i64, i64, i64, i64, i64, i64) -> i64 =
                    std::mem::transmute(function_ptr);
                Some(callee(a0, a1, a2, a3, a4, a5, a6))
            } else {
                let callee: unsafe extern "C" fn(i64, i64, i64, i64, i64, i64, i64) =
                    std::mem::transmute(function_ptr);
                callee(a0, a1, a2, a3, a4, a5, a6);
                None
            }
        }
        8 => {
            let a0 = args[0];
            let a1 = args[1];
            let a2 = args[2];
            let a3 = args[3];
            let a4 = args[4];
            let a5 = args[5];
            let a6 = args[6];
            let a7 = args[7];
            if returns_value {
                let callee: unsafe extern "C" fn(i64, i64, i64, i64, i64, i64, i64, i64) -> i64 =
                    std::mem::transmute(function_ptr);
                Some(callee(a0, a1, a2, a3, a4, a5, a6, a7))
            } else {
                let callee: unsafe extern "C" fn(i64, i64, i64, i64, i64, i64, i64, i64) =
                    std::mem::transmute(function_ptr);
                callee(a0, a1, a2, a3, a4, a5, a6, a7);
                None
            }
        }
        9 => {
            let a0 = args[0];
            let a1 = args[1];
            let a2 = args[2];
            let a3 = args[3];
            let a4 = args[4];
            let a5 = args[5];
            let a6 = args[6];
            let a7 = args[7];
            let a8 = args[8];
            if returns_value {
                let callee: unsafe extern "C" fn(
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                ) -> i64 = std::mem::transmute(function_ptr);
                Some(callee(a0, a1, a2, a3, a4, a5, a6, a7, a8))
            } else {
                let callee: unsafe extern "C" fn(i64, i64, i64, i64, i64, i64, i64, i64, i64) =
                    std::mem::transmute(function_ptr);
                callee(a0, a1, a2, a3, a4, a5, a6, a7, a8);
                None
            }
        }
        10 => {
            let a0 = args[0];
            let a1 = args[1];
            let a2 = args[2];
            let a3 = args[3];
            let a4 = args[4];
            let a5 = args[5];
            let a6 = args[6];
            let a7 = args[7];
            let a8 = args[8];
            let a9 = args[9];
            if returns_value {
                let callee: unsafe extern "C" fn(
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                ) -> i64 = std::mem::transmute(function_ptr);
                Some(callee(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9))
            } else {
                let callee: unsafe extern "C" fn(i64, i64, i64, i64, i64, i64, i64, i64, i64, i64) =
                    std::mem::transmute(function_ptr);
                callee(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9);
                None
            }
        }
        11 => {
            let a0 = args[0];
            let a1 = args[1];
            let a2 = args[2];
            let a3 = args[3];
            let a4 = args[4];
            let a5 = args[5];
            let a6 = args[6];
            let a7 = args[7];
            let a8 = args[8];
            let a9 = args[9];
            let a10 = args[10];
            if returns_value {
                let callee: unsafe extern "C" fn(
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                ) -> i64 = std::mem::transmute(function_ptr);
                Some(callee(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10))
            } else {
                let callee: unsafe extern "C" fn(
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                ) = std::mem::transmute(function_ptr);
                callee(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
                None
            }
        }
        12 => {
            let a0 = args[0];
            let a1 = args[1];
            let a2 = args[2];
            let a3 = args[3];
            let a4 = args[4];
            let a5 = args[5];
            let a6 = args[6];
            let a7 = args[7];
            let a8 = args[8];
            let a9 = args[9];
            let a10 = args[10];
            let a11 = args[11];
            if returns_value {
                let callee: unsafe extern "C" fn(
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                ) -> i64 = std::mem::transmute(function_ptr);
                Some(callee(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11))
            } else {
                let callee: unsafe extern "C" fn(
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                ) = std::mem::transmute(function_ptr);
                callee(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11);
                None
            }
        }
        13 => {
            let a0 = args[0];
            let a1 = args[1];
            let a2 = args[2];
            let a3 = args[3];
            let a4 = args[4];
            let a5 = args[5];
            let a6 = args[6];
            let a7 = args[7];
            let a8 = args[8];
            let a9 = args[9];
            let a10 = args[10];
            let a11 = args[11];
            let a12 = args[12];
            if returns_value {
                let callee: unsafe extern "C" fn(
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                ) -> i64 = std::mem::transmute(function_ptr);
                Some(callee(
                    a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12,
                ))
            } else {
                let callee: unsafe extern "C" fn(
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                ) = std::mem::transmute(function_ptr);
                callee(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12);
                None
            }
        }
        14 => {
            let a0 = args[0];
            let a1 = args[1];
            let a2 = args[2];
            let a3 = args[3];
            let a4 = args[4];
            let a5 = args[5];
            let a6 = args[6];
            let a7 = args[7];
            let a8 = args[8];
            let a9 = args[9];
            let a10 = args[10];
            let a11 = args[11];
            let a12 = args[12];
            let a13 = args[13];
            if returns_value {
                let callee: unsafe extern "C" fn(
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                ) -> i64 = std::mem::transmute(function_ptr);
                Some(callee(
                    a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13,
                ))
            } else {
                let callee: unsafe extern "C" fn(
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                ) = std::mem::transmute(function_ptr);
                callee(a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13);
                None
            }
        }
        15 => {
            let a0 = args[0];
            let a1 = args[1];
            let a2 = args[2];
            let a3 = args[3];
            let a4 = args[4];
            let a5 = args[5];
            let a6 = args[6];
            let a7 = args[7];
            let a8 = args[8];
            let a9 = args[9];
            let a10 = args[10];
            let a11 = args[11];
            let a12 = args[12];
            let a13 = args[13];
            let a14 = args[14];
            if returns_value {
                let callee: unsafe extern "C" fn(
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                ) -> i64 = std::mem::transmute(function_ptr);
                Some(callee(
                    a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14,
                ))
            } else {
                let callee: unsafe extern "C" fn(
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                    i64,
                ) = std::mem::transmute(function_ptr);
                callee(
                    a0, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14,
                );
                None
            }
        }
        _ => None,
    }
}}

#[cfg(test)]
mod call_dynamic_tests {
    #[cfg(target_arch = "aarch64")]
    extern "C" fn c_abi_dyn_test_ninth(
        _: i64,
        _: i64,
        _: i64,
        _: i64,
        _: i64,
        _: i64,
        _: i64,
        _: i64,
        mark: i64,
    ) -> i64 {
        mark
    }

    #[cfg(target_arch = "aarch64")]
    extern "C" fn c_abi_dyn_test_tenth(
        _: i64,
        _: i64,
        _: i64,
        _: i64,
        _: i64,
        _: i64,
        _: i64,
        _: i64,
        _: i64,
        mark: i64,
    ) -> i64 {
        mark
    }

    #[cfg(target_arch = "aarch64")]
    #[test]
    fn aarch64_call_dynamic_reads_stack_arg8() {
        let args = [0i64, 0, 0, 0, 0, 0, 0, 0, 77];
        let got =
            unsafe { super::call_function_dynamic(c_abi_dyn_test_ninth as *const u8, &args, true) };
        assert_eq!(got, Some(77));
    }

    #[cfg(target_arch = "aarch64")]
    #[test]
    fn aarch64_call_dynamic_reads_stack_arg9_heap_sp() {
        let args = [0i64, 0, 0, 0, 0, 0, 0, 0, 0, 88];
        let got =
            unsafe { super::call_function_dynamic(c_abi_dyn_test_tenth as *const u8, &args, true) };
        assert_eq!(got, Some(88));
    }

    #[cfg(all(target_arch = "x86_64", not(target_os = "windows")))]
    extern "C" fn c_abi_dyn_test_seven_sum(
        a0: i64,
        a1: i64,
        a2: i64,
        a3: i64,
        a4: i64,
        a5: i64,
        a6: i64,
    ) -> i64 {
        a0 + a1 + a2 + a3 + a4 + a5 + a6
    }

    #[cfg(all(target_arch = "x86_64", not(target_os = "windows")))]
    #[test]
    fn x86_64_call_dynamic_seventh_arg_on_stack() {
        let args = [1i64, 2, 3, 4, 5, 6, 7];
        let got = unsafe {
            super::call_function_dynamic(c_abi_dyn_test_seven_sum as *const u8, &args, true)
        };
        assert_eq!(got, Some(28));
    }
}