vj 0.2.0

vj: a SystemVerilog (IEEE 1800-2023) simulator built on slang and LLVM.
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
//! DPI-C support for `vj run`: loading the C code a design's `import
//! "DPI-C"` calls reach (shared libraries, or C/C++ sources compiled on the
//! fly against the bundled `include/svdpi.h`), and the `svdpi.h` functions
//! that C code calls back into (IEEE 1800-2023 Annex I), defined here with
//! C linkage and exported from the executable (`build.rs`: `-rdynamic`).
//!
//! Libraries are opened `RTLD_NOW | RTLD_GLOBAL`, so the JIT's DPI binding
//! (`vj_codegen::dpi::resolve_dpi_c_symbols`) finds their functions in
//! the global symbol scope.

use std::cell::Cell;
use std::collections::HashMap;
use std::ffi::{CStr, CString, c_char, c_int, c_void};
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::Mutex;

/// The `svdpi.h` a DPI C source is compiled against.
pub const SVDPI_H: &str = include_str!("../include/svdpi.h");

const RTLD_NOW: c_int = 2;
const RTLD_GLOBAL: c_int = 0x100;

unsafe extern "C" {
    fn dlopen(filename: *const c_char, flags: c_int) -> *mut c_void;
    fn dlerror() -> *mut c_char;
}

/// Opens each shared library `RTLD_NOW | RTLD_GLOBAL`. A library stays
/// loaded for the rest of the process.
pub fn load_libraries(paths: &[PathBuf]) -> Result<(), String> {
    for path in paths {
        let c = CString::new(path.as_os_str().as_encoded_bytes())
            .map_err(|_| format!("library path {} contains a NUL", path.display()))?;
        // SAFETY: `c` is a valid C string; the handle is intentionally never
        // closed (the JIT'd design calls into the library until exit).
        let handle = unsafe { dlopen(c.as_ptr(), RTLD_NOW | RTLD_GLOBAL) };
        if handle.is_null() {
            // SAFETY: `dlerror` returns a C string owned by libc (or null).
            let why = unsafe {
                let e = dlerror();
                if e.is_null() {
                    "unknown error".to_owned()
                } else {
                    CStr::from_ptr(e).to_string_lossy().into_owned()
                }
            };
            return Err(format!("cannot load DPI library {}: {why}", path.display()));
        }
    }
    Ok(())
}

/// A `-sv_lib`/`--sv-lib` argument as a library path: an existing path is
/// used as is; otherwise `NAME` means `NAME.so` (the usual simulator
/// convention of giving the library name without its extension).
pub fn sv_lib_path(arg: &str) -> PathBuf {
    let p = PathBuf::from(arg);
    if p.exists() || p.extension().is_some_and(|e| e == "so") {
        p
    } else {
        PathBuf::from(format!("{arg}.so"))
    }
}

/// Whether `path` is a C/C++ source `vj run` compiles for DPI.
pub fn is_c_source(path: &Path) -> bool {
    path.extension()
        .and_then(|e| e.to_str())
        .is_some_and(|e| matches!(e, "c" | "cc" | "cpp" | "cxx" | "C"))
}

/// Compiles `sources` into one shared library in a fresh temporary
/// directory (with `svdpi.h` on the include path) and returns its path.
/// Uses `$CC` (default `cc`) for C and `$CXX` (default `c++`) when any
/// source is C++.
pub fn compile_sources(sources: &[PathBuf]) -> Result<PathBuf, String> {
    let dir = std::env::temp_dir().join(format!(
        "vj-dpi-{}-{}",
        std::process::id(),
        COMPILE_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
    ));
    std::fs::create_dir_all(&dir).map_err(|e| format!("{}: {e}", dir.display()))?;
    compile_to(sources, &dir, "libsvdpi_user.so")
}

/// Compiles `sources` into `dir/name` (with `svdpi.h` written to `dir`, on
/// the include path).
fn compile_to(sources: &[PathBuf], dir: &Path, name: &str) -> Result<PathBuf, String> {
    std::fs::write(dir.join("svdpi.h"), SVDPI_H).map_err(|e| e.to_string())?;
    // A VPI source (`--vpi x.c`) compiles through here too.
    crate::vpi::write_headers(dir)?;
    let cxx = sources
        .iter()
        .any(|s| s.extension().is_some_and(|e| e != "c"));
    let compiler = if cxx {
        std::env::var("CXX").unwrap_or_else(|_| "c++".to_owned())
    } else {
        std::env::var("CC").unwrap_or_else(|_| "cc".to_owned())
    };
    let out = dir.join(name);
    let status = Command::new(&compiler)
        .args(["-shared", "-fPIC", "-O1", "-g"])
        .arg("-I")
        .arg(dir)
        .args(sources)
        .arg("-o")
        .arg(&out)
        .status()
        .map_err(|e| format!("cannot run the C compiler `{compiler}`: {e}"))?;
    if !status.success() {
        return Err(format!(
            "compiling DPI source(s) {} with `{compiler}` failed",
            sources
                .iter()
                .map(|s| s.display().to_string())
                .collect::<Vec<_>>()
                .join(", ")
        ));
    }
    Ok(out)
}

static COMPILE_COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);

thread_local! {
    /// Libraries `run_design` loads once the design is compiled (after the
    /// export trampolines they may link against).
    static PENDING_LIBRARIES: std::cell::RefCell<Vec<PathBuf>> =
        const { std::cell::RefCell::new(Vec::new()) };
}

/// Sets the DPI libraries the next `run_design` on this thread loads.
pub fn set_pending_libraries(paths: Vec<PathBuf>) {
    PENDING_LIBRARIES.with(|p| *p.borrow_mut() = paths);
}

/// Takes the libraries set by [`set_pending_libraries`].
pub fn take_pending_libraries() -> Vec<PathBuf> {
    PENDING_LIBRARIES.with(|p| std::mem::take(&mut *p.borrow_mut()))
}

// ---------------------------------------------------------------------------
// DPI export trampolines.
//
// A user library calls an exported SV function by its C identifier, but the
// function's thunk is JIT'd code the dynamic linker cannot see (and does not
// exist yet when the library is loaded). For each export, a small shared
// library defines the C identifier as an indirect jump through a pointer
// (signature-agnostic: every argument register passes through untouched);
// it is loaded `RTLD_GLOBAL` before the user libraries, and the pointer is
// set to the thunk once the design is JIT'd.

/// Builds and loads the trampoline library for `c_names`.
pub fn load_export_trampolines(c_names: &[String]) -> Result<(), String> {
    let mut c_names = c_names.to_vec();
    c_names.sort();
    c_names.dedup();
    if c_names.is_empty() {
        return Ok(());
    }
    let mut src = String::from(
        "/* vj DPI export trampolines */\n\
         #if defined(__x86_64__)\n\
         #define VJ_JUMP(p) \"jmp *\" #p \"(%rip)\"\n\
         #elif defined(__aarch64__)\n\
         #define VJ_JUMP(p) \"adrp x16, \" #p \"\\n ldr x16, [x16, :lo12:\" #p \"]\\n br x16\"\n\
         #else\n\
         #error \"DPI exports: unsupported architecture\"\n\
         #endif\n",
    );
    for n in &c_names {
        src.push_str(&format!(
            "void* __vj_p_{n} __attribute__((visibility(\"hidden\")));\n\
             void __vj_set_{n}(void* t) {{ __vj_p_{n} = t; }}\n\
             __attribute__((naked)) void {n}(void) {{ __asm__(VJ_JUMP(__vj_p_{n})); }}\n"
        ));
    }
    let dir = std::env::temp_dir().join(format!(
        "vj-dpi-exports-{}-{}",
        std::process::id(),
        COMPILE_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
    ));
    std::fs::create_dir_all(&dir).map_err(|e| format!("{}: {e}", dir.display()))?;
    let c = dir.join("vj_exports.c");
    std::fs::write(&c, src).map_err(|e| e.to_string())?;
    let lib = compile_to(&[c], &dir, "libvj_exports.so")?;
    load_libraries(&[lib])
}

unsafe extern "C" {
    fn dlsym(handle: *mut c_void, symbol: *const c_char) -> *mut c_void;
}

/// Points each export trampoline at its JIT'd thunk.
pub fn set_export_targets(thunks: &[(String, usize)]) -> Result<(), String> {
    for (name, addr) in thunks {
        let setter = CString::new(format!("__vj_set_{name}")).map_err(|e| e.to_string())?;
        // SAFETY: RTLD_DEFAULT lookup of a C string.
        let f = unsafe { dlsym(std::ptr::null_mut(), setter.as_ptr()) };
        if f.is_null() {
            return Err(format!("DPI export trampoline for `{name}` is not loaded"));
        }
        // SAFETY: `__vj_set_<name>` is `void (void*)`, defined above.
        let set: extern "C" fn(*mut c_void) = unsafe { std::mem::transmute(f) };
        set(*addr as *mut c_void);
    }
    Ok(())
}

/// The table `vj run-aot` hands a design library
/// (`vj_aot_set_dpi_host`): the driver-side half of DPI-C. The layout is
/// `vj_aot::DpiHost`'s (the library is slang-free and cannot depend on
/// this crate, so the two are kept in step by hand and by the DPI tests).
#[repr(C)]
pub struct AotDpiHost {
    pub set_scope: unsafe extern "C" fn(*mut c_void, *const c_char),
    pub export_handle: unsafe extern "C" fn(*const c_char) -> *mut c_void,
    pub export_variant: unsafe extern "C" fn(*const c_char) -> u32,
    pub register_scope: unsafe extern "C" fn(*const c_char, usize),
    pub register_export_variant: unsafe extern "C" fn(*const c_char, *const c_char, u32),
    pub set_export_target: unsafe extern "C" fn(*const c_char, usize) -> i32,
    /// Where the library mirrors the disabled state of a blocked import task
    /// (`svIsDisabledState`, LRM 35.9).
    pub disabled_state: *const std::sync::atomic::AtomicU8,
}

// SAFETY: the raw pointer is to a static atomic.
unsafe impl Sync for AotDpiHost {}

/// The disabled state a `run-aot` design library mirrors for `svIsDisabledState`.
static AOT_DISABLED_STATE: std::sync::atomic::AtomicU8 = std::sync::atomic::AtomicU8::new(0);

/// The driver's DPI table, for the life of the process.
pub static AOT_DPI_HOST: AotDpiHost = AotDpiHost {
    set_scope: sv_rt_dpi_set_scope,
    export_handle: sv_rt_dpi_export_handle,
    export_variant: sv_rt_dpi_export_variant,
    register_scope: aot_register_scope,
    register_export_variant: aot_register_export_variant,
    set_export_target: aot_set_export_target,
    disabled_state: &AOT_DISABLED_STATE,
};

/// # Safety
/// `name` is a NUL-terminated string.
unsafe extern "C" fn aot_register_scope(name: *const c_char, handle: usize) {
    // SAFETY: the caller's contract.
    register_scope(&unsafe { CStr::from_ptr(name) }.to_string_lossy(), handle);
}

/// # Safety
/// Both are NUL-terminated strings.
unsafe extern "C" fn aot_register_export_variant(
    c_name: *const c_char,
    scope: *const c_char,
    variant: u32,
) {
    // SAFETY: the caller's contract.
    let (c_name, scope) = unsafe { (CStr::from_ptr(c_name), CStr::from_ptr(scope)) };
    register_export_variant(&c_name.to_string_lossy(), &scope.to_string_lossy(), variant);
}

/// # Safety
/// `c_name` is a NUL-terminated string.
unsafe extern "C" fn aot_set_export_target(c_name: *const c_char, addr: usize) -> i32 {
    // SAFETY: the caller's contract.
    let name = unsafe { CStr::from_ptr(c_name) }
        .to_string_lossy()
        .into_owned();
    i32::from(set_export_targets(&[(name, addr)]).is_err())
}

/// Records which variant of DPI export `c_name` instance `scope` runs.
pub fn register_export_variant(c_name: &str, scope: &str, variant: u32) {
    scope_named(scope)
        .exports
        .lock()
        .unwrap_or_else(|e| e.into_inner())
        .insert(c_name.to_owned(), variant);
}

/// `sv_rt_dpi_export_variant(c_name)`: the variant of DPI export `c_name`
/// the current scope's instance runs (aborts if it has none).
///
/// # Safety
/// `c_name` is a NUL-terminated string.
pub unsafe extern "C" fn sv_rt_dpi_export_variant(c_name: *const c_char) -> u32 {
    // SAFETY: the caller's contract.
    let name = unsafe { CStr::from_ptr(c_name) }.to_string_lossy();
    let scope = CURRENT_SCOPE.with(|c| c.get());
    // SAFETY: scopes are leaked, so a non-null `scope` is valid.
    let found = (!scope.is_null())
        .then(|| unsafe { &*scope })
        .and_then(|s| {
            s.exports
                .lock()
                .unwrap_or_else(|e| e.into_inner())
                .get(name.as_ref())
                .copied()
        });
    match found {
        Some(k) => k,
        None => {
            let scope_name = if scope.is_null() {
                "(none)".to_owned()
            } else {
                // SAFETY: as above.
                unsafe { &*scope }.name.to_string_lossy().into_owned()
            };
            eprintln!("error: DPI export `{name}` is not exported by scope {scope_name}");
            std::process::abort();
        }
    }
}

/// `sv_rt_dpi_export_handle(c_name)`: the instance an exported
/// subroutine's thunk runs in -- the current DPI scope's.
///
/// # Safety
/// `c_name` is a NUL-terminated string.
pub unsafe extern "C" fn sv_rt_dpi_export_handle(c_name: *const c_char) -> *mut c_void {
    let h = current_scope_handle();
    if h.is_null() {
        // SAFETY: the caller's contract.
        let name = unsafe { CStr::from_ptr(c_name) }.to_string_lossy();
        eprintln!(
            "error: DPI export `{name}` called with no instance scope (call it from a \
             `context` import, or set one with svSetScope)"
        );
        std::process::abort();
    }
    h
}

// ---------------------------------------------------------------------------
// Output ordering around DPI calls.

thread_local! {
    /// The running design's stdout while `run_design` executes it.
    static OUTPUT: Cell<Option<*mut (dyn std::io::Write + 'static)>> = const { Cell::new(None) };
}

unsafe extern "C" {
    fn fflush(stream: *mut c_void) -> c_int;
}

/// Makes `run_design`'s `stdout` reachable from `sv_rt_dpi_sync` for the
/// guard's lifetime. All writes to it go through [`OutputGuard::write`] (or
/// `sv_rt_dpi_sync`) meanwhile, never through the original reference.
pub struct OutputGuard<'a> {
    out: *mut (dyn std::io::Write + 'a),
}

impl<'a> OutputGuard<'a> {
    pub fn set(out: &'a mut dyn std::io::Write) -> Self {
        let out: *mut (dyn std::io::Write + 'a) = out;
        // SAFETY: only the lifetime is erased; the pointer is cleared in
        // `Drop` before `'a` ends.
        let erased: *mut (dyn std::io::Write + 'static) = unsafe { std::mem::transmute(out) };
        OUTPUT.with(|o| o.set(Some(erased)));
        OutputGuard { out }
    }

    /// Writes and flushes `bytes`.
    pub fn write(&self, bytes: &[u8]) -> std::io::Result<()> {
        // SAFETY: `out` is valid for `'a`; no other reference to it is used
        // while the guard lives (single thread, non-overlapping uses).
        let out = unsafe { &mut *self.out };
        out.write_all(bytes)?;
        out.flush()
    }
}

impl Drop for OutputGuard<'_> {
    fn drop(&mut self) {
        OUTPUT.with(|o| o.set(None));
    }
}

/// `sv_rt_dpi_sync()`: around every DPI import call, writes the design's
/// pending `$display` output and flushes C stdio, so the SV and C sides'
/// output appears in execution order.
pub extern "C" fn sv_rt_dpi_sync() {
    if let Some(out) = OUTPUT.with(|o| o.get()) {
        let pending = vj_runtime::display_sink::take_output();
        if !pending.is_empty() {
            // SAFETY: set by a live `OutputGuard` on this thread.
            let out = unsafe { &mut *out };
            let _ = out.write_all(&pending);
            let _ = out.flush();
        }
    }
    // SAFETY: `fflush(NULL)` flushes every open C output stream.
    unsafe {
        fflush(std::ptr::null_mut());
    }
}

// ---------------------------------------------------------------------------
// Scopes (`svScope`): one per instance (and per package), keyed by its
// hierarchical name. A `context` import call sets the current scope to the
// scope of the import's declaration (`sv_rt_dpi_set_scope`).

struct Scope {
    name: CString,
    /// The instance's `SimHandle` (null for a package scope or before the
    /// instance's first context call).
    handle: Mutex<usize>,
    user_data: Mutex<HashMap<usize, usize>>,
    /// C identifier -> the variant of that DPI export this instance runs.
    exports: Mutex<HashMap<String, u32>>,
}

static SCOPES: Mutex<Option<HashMap<String, &'static Scope>>> = Mutex::new(None);

thread_local! {
    static CURRENT_SCOPE: Cell<*const Scope> = const { Cell::new(std::ptr::null()) };
}

fn scope_named(name: &str) -> &'static Scope {
    let mut scopes = SCOPES.lock().unwrap_or_else(|e| e.into_inner());
    let map = scopes.get_or_insert_with(HashMap::new);
    map.entry(name.to_owned()).or_insert_with(|| {
        Box::leak(Box::new(Scope {
            name: CString::new(name).unwrap_or_default(),
            handle: Mutex::new(0),
            user_data: Mutex::new(HashMap::new()),
            exports: Mutex::new(HashMap::new()),
        }))
    })
}

/// Registers an instance scope (so `svGetScopeFromName` finds it before any
/// context call reached it). `run_design` calls this for every instance.
pub fn register_scope(name: &str, handle: usize) {
    let s = scope_named(name);
    *s.handle.lock().unwrap_or_else(|e| e.into_inner()) = handle;
}

/// Clears the current scope at the start of a run. Scope objects (and
/// their user data) persist, as `svScope` handles C code may have cached
/// must stay valid.
pub fn reset_for_new_run() {
    CURRENT_SCOPE.with(|c| c.set(std::ptr::null()));
}

/// `sv_rt_dpi_set_scope(handle, scope_name)`: before a `context` import
/// call, the scope of the import's declaration becomes current.
///
/// # Safety
/// `name` is a NUL-terminated string.
pub unsafe extern "C" fn sv_rt_dpi_set_scope(handle: *mut c_void, name: *const c_char) {
    // SAFETY: the caller's contract.
    let name = unsafe { CStr::from_ptr(name) }.to_string_lossy();
    let s = scope_named(&name);
    if !handle.is_null() {
        *s.handle.lock().unwrap_or_else(|e| e.into_inner()) = handle as usize;
    }
    CURRENT_SCOPE.with(|c| c.set(s));
}

/// The `SimHandle` of the current DPI scope's instance (null if none).
pub fn current_scope_handle() -> *mut c_void {
    CURRENT_SCOPE.with(|c| {
        let s = c.get();
        if s.is_null() {
            std::ptr::null_mut()
        } else {
            // SAFETY: scopes are leaked, so `s` is valid forever.
            let s = unsafe { &*s };
            *s.handle.lock().unwrap_or_else(|e| e.into_inner()) as *mut c_void
        }
    })
}

#[unsafe(no_mangle)]
pub extern "C" fn svDpiVersion() -> *const c_char {
    c"1800-2005".as_ptr()
}

#[unsafe(no_mangle)]
pub extern "C" fn svGetScope() -> *mut c_void {
    CURRENT_SCOPE.with(|c| c.get() as *mut c_void)
}

#[unsafe(no_mangle)]
pub extern "C" fn svSetScope(scope: *mut c_void) -> *mut c_void {
    CURRENT_SCOPE.with(|c| c.replace(scope as *const Scope) as *mut c_void)
}

/// # Safety
/// `scope` is null or an `svScope` from this module.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn svGetNameFromScope(scope: *mut c_void) -> *const c_char {
    if scope.is_null() {
        return std::ptr::null();
    }
    // SAFETY: the caller's contract; scopes are leaked.
    unsafe { &*(scope as *const Scope) }.name.as_ptr()
}

/// # Safety
/// `name` is null or a NUL-terminated string.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn svGetScopeFromName(name: *const c_char) -> *mut c_void {
    if name.is_null() {
        return std::ptr::null_mut();
    }
    // SAFETY: the caller's contract.
    let name = unsafe { CStr::from_ptr(name) }.to_string_lossy();
    let scopes = SCOPES.lock().unwrap_or_else(|e| e.into_inner());
    scopes
        .as_ref()
        .and_then(|m| m.get(name.as_ref()))
        .map_or(std::ptr::null_mut(), |s| *s as *const Scope as *mut c_void)
}

/// # Safety
/// `scope` is null or an `svScope` from this module.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn svPutUserData(
    scope: *mut c_void,
    key: *mut c_void,
    data: *mut c_void,
) -> c_int {
    if scope.is_null() {
        return -1;
    }
    // SAFETY: the caller's contract; scopes are leaked.
    let s = unsafe { &*(scope as *const Scope) };
    s.user_data
        .lock()
        .unwrap_or_else(|e| e.into_inner())
        .insert(key as usize, data as usize);
    0
}

/// # Safety
/// `scope` is null or an `svScope` from this module.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn svGetUserData(scope: *mut c_void, key: *mut c_void) -> *mut c_void {
    if scope.is_null() {
        return std::ptr::null_mut();
    }
    // SAFETY: the caller's contract; scopes are leaked.
    let s = unsafe { &*(scope as *const Scope) };
    s.user_data
        .lock()
        .unwrap_or_else(|e| e.into_inner())
        .get(&(key as usize))
        .map_or(std::ptr::null_mut(), |d| *d as *mut c_void)
}

/// No caller file/line information is recorded: always "not available".
#[unsafe(no_mangle)]
pub extern "C" fn svGetCallerInfo(_file: *mut *const c_char, _line: *mut c_int) -> c_int {
    0
}

/// Whether the import task being called was disabled while blocked in an
/// export task (LRM 35.9; see `vj_sim::dpi_task`). An import function is
/// never in the disabled state.
#[unsafe(no_mangle)]
pub extern "C" fn svIsDisabledState() -> c_int {
    c_int::from(
        vj_sim::dpi_task::active_call_disabled()
            || AOT_DISABLED_STATE.load(std::sync::atomic::Ordering::Relaxed) != 0,
    )
}

#[unsafe(no_mangle)]
pub extern "C" fn svAckDisabledState() {
    vj_sim::dpi_task::ack_disabled_state();
    // The run-aot driver: the design library checks the mirrored state.
    let _ = AOT_DISABLED_STATE.compare_exchange(
        vj_sim::dpi_task::MIRROR_DISABLED,
        vj_sim::dpi_task::MIRROR_ACKED,
        std::sync::atomic::Ordering::Relaxed,
        std::sync::atomic::Ordering::Relaxed,
    );
}

// ---------------------------------------------------------------------------
// Bit and part selects on `svBitVecVal` (32-bit words) and `svLogicVecVal`
// (`{aval, bval}` word pairs), least significant word first.

/// `svLogicVecVal`.
#[repr(C)]
#[derive(Clone, Copy)]
pub struct SvLogicVecVal {
    pub aval: u32,
    pub bval: u32,
}

fn get_bit(words: *const u32, i: usize) -> u32 {
    // SAFETY: callers pass a buffer covering bit `i` (the svdpi contract).
    (unsafe { *words.add(i / 32) } >> (i % 32)) & 1
}

fn put_bit(words: *mut u32, i: usize, b: u32) {
    // SAFETY: as `get_bit`.
    unsafe {
        let w = words.add(i / 32);
        *w = (*w & !(1 << (i % 32))) | ((b & 1) << (i % 32));
    }
}

/// # Safety
/// `s` covers bit `i`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn svGetBitselBit(s: *const u32, i: c_int) -> u8 {
    get_bit(s, i as usize) as u8
}

/// # Safety
/// `s` covers bit `i`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn svGetBitselLogic(s: *const SvLogicVecVal, i: c_int) -> u8 {
    let i = i as usize;
    // SAFETY: the caller's contract.
    let w = unsafe { *s.add(i / 32) };
    let a = (w.aval >> (i % 32)) & 1;
    let b = (w.bval >> (i % 32)) & 1;
    (a | (b << 1)) as u8
}

/// # Safety
/// `d` covers bit `i`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn svPutBitselBit(d: *mut u32, i: c_int, s: u8) {
    put_bit(d, i as usize, u32::from(s & 1));
}

/// # Safety
/// `d` covers bit `i`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn svPutBitselLogic(d: *mut SvLogicVecVal, i: c_int, s: u8) {
    let i = i as usize;
    // SAFETY: the caller's contract.
    let w = unsafe { &mut *d.add(i / 32) };
    let m = 1u32 << (i % 32);
    w.aval = (w.aval & !m) | (u32::from(s & 1) << (i % 32));
    w.bval = (w.bval & !m) | (u32::from((s >> 1) & 1) << (i % 32));
}

/// `d[0 +: w] = s[i +: w]` over one plane (words `stride` apart).
fn copy_bits(d: *mut u32, d_lo: usize, s: *const u32, s_lo: usize, w: usize, stride: usize) {
    for k in 0..w {
        let (si, di) = (s_lo + k, d_lo + k);
        // SAFETY: the svdpi contract: both buffers cover the selected bits.
        let bit = unsafe { (*s.add((si / 32) * stride) >> (si % 32)) & 1 };
        unsafe {
            let p = d.add((di / 32) * stride);
            *p = (*p & !(1 << (di % 32))) | (bit << (di % 32));
        }
    }
}

/// # Safety
/// `s` covers bits `i..i+w`; `d` covers `w` bits.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn svGetPartselBit(d: *mut u32, s: *const u32, i: c_int, w: c_int) {
    copy_bits(d, 0, s, i as usize, w as usize, 1);
}

/// # Safety
/// `s` covers bits `i..i+w`; `d` covers `w` bits.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn svGetPartselLogic(
    d: *mut SvLogicVecVal,
    s: *const SvLogicVecVal,
    i: c_int,
    w: c_int,
) {
    let (d, s) = (d as *mut u32, s as *const u32);
    copy_bits(d, 0, s, i as usize, w as usize, 2);
    // SAFETY: `bval` is the second word of each pair.
    copy_bits(
        unsafe { d.add(1) },
        0,
        unsafe { s.add(1) },
        i as usize,
        w as usize,
        2,
    );
}

/// # Safety
/// `d` covers bits `i..i+w`; `w <= 32`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn svPutPartselBit(d: *mut u32, s: u32, i: c_int, w: c_int) {
    copy_bits(d, i as usize, &s, 0, w as usize, 1);
}

/// # Safety
/// `d` covers bits `i..i+w`; `w <= 32`.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn svPutPartselLogic(
    d: *mut SvLogicVecVal,
    s: SvLogicVecVal,
    i: c_int,
    w: c_int,
) {
    let d = d as *mut u32;
    copy_bits(d, i as usize, &s.aval, 0, w as usize, 2);
    // SAFETY: `bval` is the second word of each pair.
    copy_bits(unsafe { d.add(1) }, i as usize, &s.bval, 0, w as usize, 2);
}

// ---------------------------------------------------------------------------
// Open arrays (`svOpenArrayHandle`): a pointer to an [`OpenArray`] the
// calling code built (`vj_codegen::dpi::build_open_array`). Arrays are
// up to `OPEN_MAX_DIMS` unpacked dimensions (dimensions 1..) of elements
// with one packed dimension (dimension 0), or, for an open packed formal
// (`bit []`), a packed dimension only.

/// The most unpacked dimensions an open array records.
pub const OPEN_MAX_DIMS: usize = 8;

/// The descriptor behind an `svOpenArrayHandle`.
#[repr(C)]
pub struct OpenArray {
    data: *mut u8,
    elem_size: u32,
    /// 0 a native C value, 1 svBitVecVal words, 2 svLogicVecVal words,
    /// 3 an svBit/svLogic byte.
    kind: u32,
    elem_bits: u32,
    pleft: i32,
    pright: i32,
    /// Unpacked dimensions: 0 for an open packed formal.
    udims: i32,
    /// `(left, right)` of unpacked dimension `d + 1`, leftmost first.
    bounds: [[i32; 2]; OPEN_MAX_DIMS],
}

fn oa<'a>(h: *const c_void) -> Option<&'a OpenArray> {
    // SAFETY: an svOpenArrayHandle is an `OpenArray` built by the caller.
    (!h.is_null()).then(|| unsafe { &*(h as *const OpenArray) })
}

/// LRM 1800-2023 H.12.2: dimension 0 is the (linearized) packed part,
/// dimensions 1.. the unpacked ones, leftmost first.
fn dim_bounds(h: *const c_void, d: c_int) -> Option<(i32, i32)> {
    let a = oa(h)?;
    match d {
        0 => Some((a.pleft, a.pright)),
        1.. if d <= a.udims => {
            let [l, r] = a.bounds[d as usize - 1];
            Some((l, r))
        }
        _ => None,
    }
}

#[unsafe(no_mangle)]
pub extern "C" fn svLeft(h: *const c_void, d: c_int) -> c_int {
    dim_bounds(h, d).map_or(0, |(l, _)| l)
}

#[unsafe(no_mangle)]
pub extern "C" fn svRight(h: *const c_void, d: c_int) -> c_int {
    dim_bounds(h, d).map_or(0, |(_, r)| r)
}

#[unsafe(no_mangle)]
pub extern "C" fn svLow(h: *const c_void, d: c_int) -> c_int {
    dim_bounds(h, d).map_or(0, |(l, r)| l.min(r))
}

#[unsafe(no_mangle)]
pub extern "C" fn svHigh(h: *const c_void, d: c_int) -> c_int {
    dim_bounds(h, d).map_or(0, |(l, r)| l.max(r))
}

/// 1 if the left bound is the higher one, else -1 (LRM 1800-2023 H.12.2).
#[unsafe(no_mangle)]
pub extern "C" fn svIncrement(h: *const c_void, d: c_int) -> c_int {
    dim_bounds(h, d).map_or(0, |(l, r)| if l >= r { 1 } else { -1 })
}

#[unsafe(no_mangle)]
pub extern "C" fn svSize(h: *const c_void, d: c_int) -> c_int {
    dim_bounds(h, d).map_or(0, |(l, r)| (l - r).abs() + 1)
}

#[unsafe(no_mangle)]
pub extern "C" fn svDimensions(h: *const c_void) -> c_int {
    oa(h).map_or(0, |a| if a.udims > 0 { a.udims } else { 1 })
}

#[unsafe(no_mangle)]
pub extern "C" fn svGetArrayPtr(h: *const c_void) -> *mut c_void {
    oa(h).map_or(std::ptr::null_mut(), |a| a.data as *mut c_void)
}

#[unsafe(no_mangle)]
pub extern "C" fn svSizeOfArray(h: *const c_void) -> c_int {
    oa(h).map_or(0, |a| {
        let elems: i32 = a.bounds[..a.udims as usize]
            .iter()
            .map(|[l, r]| (l - r).abs() + 1)
            .product();
        elems * a.elem_size as c_int
    })
}

/// The address of the element at SV indices `idx` (one per unpacked
/// dimension, leftmost first), or null when the count is wrong or an index
/// is out of range. Elements are stored row-major, each dimension lowest SV
/// index first (Annex H normalized ranges).
fn elem_ptr_n(h: *const c_void, idx: &[c_int]) -> *mut c_void {
    let Some(a) = oa(h).filter(|a| a.udims >= 1 && a.udims as usize == idx.len()) else {
        return std::ptr::null_mut();
    };
    let mut off = 0i64;
    for (&i, [l, r]) in idx.iter().zip(&a.bounds) {
        let (low, size) = (l.min(r), (l - r).abs() + 1);
        let k = i64::from(i) - i64::from(*low);
        if k < 0 || k >= i64::from(size) {
            return std::ptr::null_mut();
        }
        off = off * i64::from(size) + k;
    }
    // SAFETY: `off` is within the array's `elems * elem_size` bytes.
    unsafe { a.data.add(off as usize * a.elem_size as usize) as *mut c_void }
}

/// The address of the element at SV indices `idx[0..n]`; the C
/// `svGetArrElemPtr(h, i, ...)` family forwards here.
///
/// # Safety
/// `idx` points at `n` ints.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn vj_dpi_elem_ptr(
    h: *const c_void,
    idx: *const c_int,
    n: c_int,
) -> *mut c_void {
    // SAFETY: the caller's contract.
    elem_ptr_n(h, unsafe {
        std::slice::from_raw_parts(idx, n.max(0) as usize)
    })
}

#[unsafe(no_mangle)]
pub extern "C" fn svGetArrElemPtr1(h: *const c_void, i: c_int) -> *mut c_void {
    elem_ptr_n(h, &[i])
}

#[unsafe(no_mangle)]
pub extern "C" fn svGetArrElemPtr2(h: *const c_void, i: c_int, j: c_int) -> *mut c_void {
    elem_ptr_n(h, &[i, j])
}

#[unsafe(no_mangle)]
pub extern "C" fn svGetArrElemPtr3(h: *const c_void, i: c_int, j: c_int, k: c_int) -> *mut c_void {
    elem_ptr_n(h, &[i, j, k])
}

// ---------------------------------------------------------------------------
// Element copy functions (H.12.3-H.12.5) over the descriptor's element kind.

/// Element `i`'s value as `(aval, bval)` bits, word by word.
fn elem_words(a: &OpenArray, p: *const u8) -> Vec<(u32, u32)> {
    let words = a.elem_bits.div_ceil(32).max(1) as usize;
    // SAFETY: `p` is an element of `a` (`elem_size` bytes).
    unsafe {
        match a.kind {
            1 => (0..words)
                .map(|w| ((p as *const u32).add(w).read_unaligned(), 0))
                .collect(),
            2 => (0..words)
                .map(|w| {
                    let q = (p as *const u32).add(2 * w);
                    (q.read_unaligned(), q.add(1).read_unaligned())
                })
                .collect(),
            3 => {
                let b = u32::from(*p);
                vec![(b & 1, (b >> 1) & 1)]
            }
            _ => {
                let mut bytes = [0u8; 8];
                std::ptr::copy_nonoverlapping(p, bytes.as_mut_ptr(), (a.elem_size as usize).min(8));
                let v = u64::from_le_bytes(bytes);
                (0..words).map(|w| ((v >> (32 * w)) as u32, 0)).collect()
            }
        }
    }
}

fn put_elem_words(a: &OpenArray, p: *mut u8, words: &[(u32, u32)]) {
    // SAFETY: `p` is an element of `a` (`elem_size` bytes).
    unsafe {
        match a.kind {
            1 => {
                for (w, (aval, _)) in words.iter().enumerate() {
                    (p as *mut u32).add(w).write_unaligned(*aval);
                }
            }
            2 => {
                for (w, (aval, bval)) in words.iter().enumerate() {
                    let q = (p as *mut u32).add(2 * w);
                    q.write_unaligned(*aval);
                    q.add(1).write_unaligned(*bval);
                }
            }
            3 => *p = ((words[0].0 & 1) | ((words[0].1 & 1) << 1)) as u8,
            _ => {
                let v = words.iter().enumerate().fold(0u64, |acc, (w, (aval, _))| {
                    acc | (u64::from(*aval) << (32 * w))
                });
                let bytes = v.to_le_bytes();
                std::ptr::copy_nonoverlapping(bytes.as_ptr(), p, (a.elem_size as usize).min(8));
            }
        }
    }
}

fn elem(h: *const c_void, idx: &[c_int]) -> Option<(&'static OpenArray, *mut u8)> {
    let p = elem_ptr_n(h, idx);
    (!p.is_null()).then(|| (oa(h).expect("non-null"), p as *mut u8))
}

fn n_words(a: &OpenArray) -> usize {
    a.elem_bits.div_ceil(32).max(1) as usize
}

fn get_bit_vec(d: *mut u32, h: *const c_void, idx: &[c_int]) {
    if let Some((a, p)) = elem(h, idx) {
        for (w, (aval, bval)) in elem_words(a, p).into_iter().enumerate() {
            // SAFETY: the caller's contract. X/Z read as 0.
            unsafe { *d.add(w) = aval & !bval };
        }
    }
}

fn get_logic_vec(d: *mut SvLogicVecVal, h: *const c_void, idx: &[c_int]) {
    if let Some((a, p)) = elem(h, idx) {
        for (w, (aval, bval)) in elem_words(a, p).into_iter().enumerate() {
            // SAFETY: the caller's contract.
            unsafe { *d.add(w) = SvLogicVecVal { aval, bval } };
        }
    }
}

fn put_bit_vec(h: *const c_void, s: *const u32, idx: &[c_int]) {
    if let Some((a, p)) = elem(h, idx) {
        // SAFETY: the caller's contract.
        let words: Vec<(u32, u32)> = (0..n_words(a)).map(|w| (unsafe { *s.add(w) }, 0)).collect();
        put_elem_words(a, p, &words);
    }
}

fn put_logic_vec(h: *const c_void, s: *const SvLogicVecVal, idx: &[c_int]) {
    if let Some((a, p)) = elem(h, idx) {
        let words: Vec<(u32, u32)> = (0..n_words(a))
            .map(|w| {
                // SAFETY: the caller's contract.
                let v = unsafe { *s.add(w) };
                (v.aval, v.bval)
            })
            .collect();
        put_elem_words(a, p, &words);
    }
}

fn oa_get_bit(h: *const c_void, idx: &[c_int]) -> u8 {
    elem(h, idx).map_or(0, |(a, p)| {
        let (aval, bval) = elem_words(a, p)[0];
        (aval & !bval & 1) as u8
    })
}

fn oa_get_logic(h: *const c_void, idx: &[c_int]) -> u8 {
    elem(h, idx).map_or(0, |(a, p)| {
        let (aval, bval) = elem_words(a, p)[0];
        ((aval & 1) | ((bval & 1) << 1)) as u8
    })
}

fn oa_put_bit(h: *const c_void, v: u8, idx: &[c_int]) {
    if let Some((a, p)) = elem(h, idx) {
        let mut words = vec![(0, 0); n_words(a)];
        words[0] = (u32::from(v & 1), 0);
        put_elem_words(a, p, &words);
    }
}

fn oa_put_logic(h: *const c_void, v: u8, idx: &[c_int]) {
    if let Some((a, p)) = elem(h, idx) {
        let mut words = vec![(0, 0); n_words(a)];
        words[0] = (u32::from(v & 1), u32::from((v >> 1) & 1));
        put_elem_words(a, p, &words);
    }
}

/// The one-, two- and three-index forms of every element access function
/// (H.12.4/H.12.5); the variadic forms go through `vj_dpi_elem_op`.
macro_rules! elem_access {
    ($([$($i:ident),+] => $get_bv:ident $get_lv:ident $put_bv:ident $put_lv:ident
       $get_b:ident $get_l:ident $put_b:ident $put_l:ident;)*) => {
        $(
            /// # Safety
            /// `d` holds the element's words.
            #[unsafe(no_mangle)]
            pub unsafe extern "C" fn $get_bv(d: *mut u32, h: *const c_void, $($i: c_int),+) {
                get_bit_vec(d, h, &[$($i),+]);
            }

            /// # Safety
            /// `d` holds the element's words.
            #[unsafe(no_mangle)]
            pub unsafe extern "C" fn $get_lv(d: *mut SvLogicVecVal, h: *const c_void, $($i: c_int),+) {
                get_logic_vec(d, h, &[$($i),+]);
            }

            /// # Safety
            /// `s` holds the element's words.
            #[unsafe(no_mangle)]
            pub unsafe extern "C" fn $put_bv(h: *const c_void, s: *const u32, $($i: c_int),+) {
                put_bit_vec(h, s, &[$($i),+]);
            }

            /// # Safety
            /// `s` holds the element's words.
            #[unsafe(no_mangle)]
            pub unsafe extern "C" fn $put_lv(h: *const c_void, s: *const SvLogicVecVal, $($i: c_int),+) {
                put_logic_vec(h, s, &[$($i),+]);
            }

            #[unsafe(no_mangle)]
            pub extern "C" fn $get_b(h: *const c_void, $($i: c_int),+) -> u8 {
                oa_get_bit(h, &[$($i),+])
            }

            #[unsafe(no_mangle)]
            pub extern "C" fn $get_l(h: *const c_void, $($i: c_int),+) -> u8 {
                oa_get_logic(h, &[$($i),+])
            }

            #[unsafe(no_mangle)]
            pub extern "C" fn $put_b(h: *const c_void, v: u8, $($i: c_int),+) {
                oa_put_bit(h, v, &[$($i),+]);
            }

            #[unsafe(no_mangle)]
            pub extern "C" fn $put_l(h: *const c_void, v: u8, $($i: c_int),+) {
                oa_put_logic(h, v, &[$($i),+]);
            }
        )*
    };
}

elem_access! {
    [i1] => svGetBitArrElem1VecVal svGetLogicArrElem1VecVal svPutBitArrElem1VecVal
        svPutLogicArrElem1VecVal svGetBitArrElem1 svGetLogicArrElem1 svPutBitArrElem1
        svPutLogicArrElem1;
    [i1, i2] => svGetBitArrElem2VecVal svGetLogicArrElem2VecVal svPutBitArrElem2VecVal
        svPutLogicArrElem2VecVal svGetBitArrElem2 svGetLogicArrElem2 svPutBitArrElem2
        svPutLogicArrElem2;
    [i1, i2, i3] => svGetBitArrElem3VecVal svGetLogicArrElem3VecVal svPutBitArrElem3VecVal
        svPutLogicArrElem3VecVal svGetBitArrElem3 svGetLogicArrElem3 svPutBitArrElem3
        svPutLogicArrElem3;
}

/// The variable-index forms behind the C wrappers of `SVDPI_VARIADIC_C`
/// (`idx` holds one index per unpacked dimension).
///
/// # Safety
/// `idx` points at `n` ints; `buf` as for the fixed forms.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn vj_dpi_elem_op(
    op: c_int,
    h: *const c_void,
    buf: *mut c_void,
    value: c_int,
    idx: *const c_int,
    n: c_int,
) -> c_int {
    // SAFETY: the caller's contract.
    let idx = unsafe { std::slice::from_raw_parts(idx, n.max(0) as usize) };
    match op {
        0 => get_bit_vec(buf as *mut u32, h, idx),
        1 => get_logic_vec(buf as *mut SvLogicVecVal, h, idx),
        2 => put_bit_vec(h, buf as *const u32, idx),
        3 => put_logic_vec(h, buf as *const SvLogicVecVal, idx),
        4 => return c_int::from(oa_get_bit(h, idx)),
        5 => return c_int::from(oa_get_logic(h, idx)),
        6 => oa_put_bit(h, value as u8, idx),
        7 => oa_put_logic(h, value as u8, idx),
        _ => {}
    }
    0
}

/// The variadic `svdpi.h` functions (`svGetArrElemPtr(h, i, ...)` and the
/// like), which Rust cannot define: C wrappers that gather one index per
/// unpacked dimension of the handle (`svDimensions`).
const SVDPI_VARIADIC_C: &str = r#"#include <stdarg.h>
#include "svdpi.h"
extern void* vj_dpi_elem_ptr(const void*, const int*, int);
extern int vj_dpi_elem_op(int, const void*, void*, int, const int*, int);
#define GATHER(h, first) int idx[8]; int n = svDimensions(h), k; va_list ap; \
    if (n > 8) n = 8; idx[0] = first; va_start(ap, first); \
    for (k = 1; k < n; k++) idx[k] = va_arg(ap, int); va_end(ap)
void* svGetArrElemPtr(const svOpenArrayHandle h, int i, ...) { GATHER(h, i); return vj_dpi_elem_ptr(h, idx, n); }
void svGetBitArrElemVecVal(svBitVecVal* d, const svOpenArrayHandle s, int i, ...) { GATHER(s, i); vj_dpi_elem_op(0, s, d, 0, idx, n); }
void svGetLogicArrElemVecVal(svLogicVecVal* d, const svOpenArrayHandle s, int i, ...) { GATHER(s, i); vj_dpi_elem_op(1, s, d, 0, idx, n); }
void svPutBitArrElemVecVal(const svOpenArrayHandle d, const svBitVecVal* s, int i, ...) { GATHER(d, i); vj_dpi_elem_op(2, d, (void*)s, 0, idx, n); }
void svPutLogicArrElemVecVal(const svOpenArrayHandle d, const svLogicVecVal* s, int i, ...) { GATHER(d, i); vj_dpi_elem_op(3, d, (void*)s, 0, idx, n); }
svBit svGetBitArrElem(const svOpenArrayHandle s, int i, ...) { GATHER(s, i); return (svBit)vj_dpi_elem_op(4, s, 0, 0, idx, n); }
svLogic svGetLogicArrElem(const svOpenArrayHandle s, int i, ...) { GATHER(s, i); return (svLogic)vj_dpi_elem_op(5, s, 0, 0, idx, n); }
void svPutBitArrElem(const svOpenArrayHandle d, svBit v, int i, ...) { GATHER(d, i); vj_dpi_elem_op(6, d, 0, v, idx, n); }
void svPutLogicArrElem(const svOpenArrayHandle d, svLogic v, int i, ...) { GATHER(d, i); vj_dpi_elem_op(7, d, 0, v, idx, n); }
"#;

/// Builds (once per process) and loads the library of variadic `svdpi.h`
/// functions; `run_design` calls this before loading user libraries.
pub fn load_svdpi_support() -> Result<(), String> {
    static LOADED: std::sync::OnceLock<Result<(), String>> = std::sync::OnceLock::new();
    LOADED
        .get_or_init(|| {
            let dir = std::env::temp_dir().join(format!(
                "vj-svdpi-support-{}-{}",
                std::process::id(),
                COMPILE_COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
            ));
            std::fs::create_dir_all(&dir).map_err(|e| format!("{}: {e}", dir.display()))?;
            let c = dir.join("svdpi_variadic.c");
            std::fs::write(&c, SVDPI_VARIADIC_C).map_err(|e| e.to_string())?;
            let lib = compile_to(&[c], &dir, "libsvdpi_support.so")?;
            load_libraries(&[lib])
        })
        .clone()
}