mir_sys/lib.rs
1#![cfg_attr(docsrs, feature(doc_auto_cfg))]
2//! # [MIR project][mir] bindings for Rust
3//!
4//! This is the low-level binding that only exposes raw C types and functions,
5//! and handles compilation and linking of C code.
6//!
7//! See [`mir-rs`][mir-rs] crate for high-level ergonomic APIs.
8//!
9//! [mir]: https://github.com/vnmakarov/mir
10//! [mir-rs]: https://crates.io/crates/mir-rs
11//!
12//! ## Source
13//!
14//! The source code of mir C library is bundled in this package. Since upstream lacks some
15//! API/features that are necessary for Rust integration, we use a forked version of mir.
16//!
17// NB: Sync with submodule and build.rs.
18//! The fork can be seen at
19//! [this repo](https://github.com/oxalica/mir/tree/1.0.0-rust.x),
20//! and the current revision is based on upstream version
21//! [v1.0.0](https://github.com/vnmakarov/mir/releases/tag/v1.0.0).
22//!
23//! ⚠️ The API/ABI is **NOT** compatible with upstream mir library.
24//!
25//! ## Features
26//!
27//! ⚠️ Warning: Currently, due to [lack of support of `bindgen`][bindgen-fncfg-issue],
28//! extern functions may still be generated even with corresponding feature disabled.
29//! Be careful to avoid using them when disabling features, or you may encounter link errors.
30//!
31//! - `default`: Implies `io`, `scan`, `interp`, `gen`.
32//!
33//! - `io`: De/serialization of MIR memory representation into/from bytes.
34//! If disabled, C macro `MIR_NO_IO` is set for compilation.
35//!
36//! Guarded APIs:
37//! - `MIR_write{,_module}{,_with_func}`
38//! - `MIR_read{,_with_func}`
39//!
40//! - `scan`: Parsing of MIR textual representation.
41//! If disabled, C macro `MIR_NO_SCAN` is set for compilation.
42//!
43//! Guarded API:
44//! - `MIR_scan_string`
45//!
46//! - `interp`: Enables MIR interpreter.
47//! If disabled, C macro `MIR_NO_INTERP` is set for compilation.
48//!
49//! Guarded APIs:
50//! - `MIR_interp*`
51//!
52//! - `gen`: MIR native code generator.
53//! If disabled, `mir-gen.c` will not be compiled.
54//!
55//! Guarded APIs:
56//! - `MIR_gen*`
57//! - `MIR_set_*_gen_interface`
58//!
59//! - `gen-debug`: Debug logging in MIR native code generator. It implies `gen`.
60//! If disabled, C macro `MIR_NO_GEN_DEBUG` is set for compilation.
61//!
62//! Guarded APIs:
63//! - `MIR_gen_set_debug_{file,level}`
64//!
65//! - `assert`: Debug assertions.
66//! If disabled, C macro `NDEBUG` is set for compilation.
67//! It is implicitly enabled when `debug_assertions` is on (eg. in dev profile).
68//!
69//! [bindgen-fncfg-issue]: https://github.com/rust-lang/rust-bindgen/issues/2978
70#![expect(non_upper_case_globals, non_camel_case_types)]
71
72#[rustfmt::skip]
73#[allow(clippy::unreadable_literal)]
74mod bindings;
75
76#[rustfmt::skip]
77#[allow(clippy::wildcard_imports)]
78#[cfg(feature = "gen")]
79mod bindings_gen;
80
81pub use bindings::*;
82
83#[cfg(feature = "gen")]
84pub use bindings_gen::*;
85
86pub type MIR_op_mode_t_u8 = u8;
87
88#[derive(Copy, Clone)]
89#[repr(C)]
90#[cfg(target_endian = "little")]
91pub struct MIR_op_t {
92 pub data: *mut ::std::os::raw::c_void,
93 pub mode: MIR_op_mode_t_u8,
94 pub value_mode: MIR_op_mode_t_u8,
95 pub u: MIR_op_t__u,
96}
97
98#[derive(Copy, Clone)]
99#[repr(C)]
100pub union MIR_op_t__u {
101 pub reg: MIR_reg_t,
102 pub var: MIR_reg_t,
103 pub i: i64,
104 pub u: u64,
105 pub f: f32,
106 pub d: f64,
107 pub ld: f64,
108 pub ref_: MIR_item_t,
109 pub str_: MIR_str_t,
110 pub mem: MIR_mem_t,
111 pub var_mem: MIR_mem_t,
112 pub label: MIR_label_t,
113}
114
115pub type MIR_type_t_u8 = u8;
116
117#[repr(C)]
118#[derive(Debug, Copy, Clone)]
119pub struct MIR_mem_t {
120 pub type_: MIR_type_t_u8,
121 pub scale: MIR_scale_t,
122 pub alias: MIR_alias_t,
123 pub nonalias: MIR_alias_t,
124 pub nloc: u32,
125 pub base: MIR_reg_t,
126 pub index: MIR_reg_t,
127 pub disp: MIR_disp_t,
128}
129
130pub type MIR_insn_code_t_u32 = u32;
131
132#[repr(C)]
133pub struct MIR_insn {
134 pub data: *mut ::std::os::raw::c_void,
135 pub insn_link: DLIST_LINK_MIR_insn_t,
136 pub code: MIR_insn_code_t_u32,
137 pub nops: u32,
138 pub ops: [MIR_op_t; 1usize],
139}
140
141#[expect(non_snake_case)]
142#[expect(clippy::used_underscore_items)]
143#[expect(clippy::float_cmp)]
144#[inline]
145#[must_use]
146pub fn MIR_init() -> MIR_context_t {
147 debug_assert_eq!(MIR_API_VERSION, unsafe { _MIR_get_api_version() });
148 unsafe { _MIR_init() }
149}