inauguration 0.2.0

.in language and general compiler CLI (Core IR, hybrid SIL, staging, plugins)
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
use crate::boundary_emit;
use crate::boundary_ir::{BoundaryModule, IN_ABI_VERSION};
use crate::core_ir::UnifiedModule;
use crate::native_emit::NativeLinkage;
use crate::native_emit::coff::{COFF_WINDOWS_TRIPLE, write_exit_process_exe};
use crate::native_emit::elf::{
    AARCH64_LINUX_TRIPLE, ARMV7_LINUX_GNUEABIHF_TRIPLE, ELF_LINUX_TRIPLE, ElfExecutable, ElfObject,
    aarch64_linux_exit_code, aarch64_return_i32_object_code, arm32_linux_exit_code,
    arm32_return_i32_object_code, write_aarch64_executable, write_aarch64_relocatable_object,
    write_arm32_executable, write_arm32_relocatable_object, write_executable,
    write_x86_64_relocatable_object, x86_64_linux_exit_code, x86_64_return_i32_object_code,
};
use crate::native_emit::macho::{ExportSymbol, MachOImage, MachOLinkage, write_image};
use crate::native_emit::wasm::{WASM32_UNKNOWN_TRIPLE, WasmModule, write_scalar_i32_module};
use crate::native_emit::x86_64_lower::{X86_64_TRIPLE, lower_module};

pub const NATIVE_OBJECT_SUBSET: &str = "native-object-subset";

pub struct NativeObjectRequest<'a> {
    pub target_triple: &'a str,
    pub linkage: NativeLinkage,
    pub entry: &'a str,
    pub exit_code: u8,
    pub module: &'a UnifiedModule,
    pub module_id: &'a str,
}

pub struct NativeObjectArtifact {
    pub bytes: Vec<u8>,
    pub artifact_kind: &'static str,
    pub backend_level: &'static str,
    pub runtime_level: &'static str,
    pub reason_code: &'static str,
    pub reason: &'static str,
    pub abi_manifest: Option<String>,
}

pub fn emit_native_object(request: &NativeObjectRequest<'_>) -> Option<NativeObjectArtifact> {
    match (request.target_triple, request.linkage) {
        (ELF_LINUX_TRIPLE, NativeLinkage::StaticLib) => Some(emit_x86_64_elf_object(request)),
        (ELF_LINUX_TRIPLE, NativeLinkage::Executable) => Some(emit_x86_64_elf_executable(request)),
        (COFF_WINDOWS_TRIPLE, NativeLinkage::Executable) => {
            Some(emit_x86_64_pe_executable(request))
        }
        (AARCH64_LINUX_TRIPLE, NativeLinkage::StaticLib) => Some(emit_aarch64_elf_object(request)),
        (AARCH64_LINUX_TRIPLE, NativeLinkage::Executable) => {
            Some(emit_aarch64_elf_executable(request))
        }
        ("aarch64-apple-darwin", NativeLinkage::StaticLib) => {
            Some(emit_aarch64_macho_archive(request))
        }
        (ARMV7_LINUX_GNUEABIHF_TRIPLE, NativeLinkage::StaticLib) => {
            Some(emit_arm32_elf_object(request))
        }
        (ARMV7_LINUX_GNUEABIHF_TRIPLE, NativeLinkage::Executable) => {
            Some(emit_arm32_elf_executable(request))
        }
        (WASM32_UNKNOWN_TRIPLE, NativeLinkage::StaticLib) => Some(emit_wasm32_module(request)),
        (X86_64_TRIPLE, NativeLinkage::StaticLib) => Some(emit_x86_64_freestanding_object(request)),
        _ => None,
    }
}

fn emit_x86_64_pe_executable(request: &NativeObjectRequest<'_>) -> NativeObjectArtifact {
    let mut bytes = Vec::new();
    write_exit_process_exe(request.exit_code, &mut bytes);
    NativeObjectArtifact {
        bytes,
        artifact_kind: "pe-executable",
        backend_level: "owned-native-subset-x86_64",
        runtime_level: "windows-exitprocess",
        reason_code: "native-x86_64-windows-exe-subset",
        reason: "inauguration owns PE32+ executable emission with kernel32 ExitProcess import for const-evaluable scalar entry functions on this target",
        abi_manifest: None,
    }
}

fn emit_x86_64_elf_object(request: &NativeObjectRequest<'_>) -> NativeObjectArtifact {
    let object = ElfObject {
        code: x86_64_return_i32_object_code(request.exit_code),
        export_name: request.entry.to_string(),
    };
    let mut bytes = Vec::new();
    write_x86_64_relocatable_object(&object, &mut bytes);
    NativeObjectArtifact {
        bytes,
        artifact_kind: "elf-relocatable-object",
        backend_level: "owned-object-subset",
        runtime_level: "none",
        reason_code: NATIVE_OBJECT_SUBSET,
        reason: "inauguration owns ELF64 relocatable object emission for const-evaluable scalar entry functions on this target",
        abi_manifest: Some(object_abi_manifest(request)),
    }
}

fn emit_aarch64_elf_object(request: &NativeObjectRequest<'_>) -> NativeObjectArtifact {
    let object = ElfObject {
        code: aarch64_return_i32_object_code(request.exit_code),
        export_name: request.entry.to_string(),
    };
    let mut bytes = Vec::new();
    write_aarch64_relocatable_object(&object, &mut bytes);
    NativeObjectArtifact {
        bytes,
        artifact_kind: "elf-relocatable-object",
        backend_level: "owned-object-subset",
        runtime_level: "none",
        reason_code: NATIVE_OBJECT_SUBSET,
        reason: "inauguration owns ELF64 AArch64 relocatable object emission for const-evaluable scalar entry functions on this target",
        abi_manifest: Some(object_abi_manifest(request)),
    }
}

fn emit_aarch64_elf_executable(request: &NativeObjectRequest<'_>) -> NativeObjectArtifact {
    let exe = ElfExecutable {
        code: aarch64_linux_exit_code(request.exit_code),
        entry_offset: 0,
    };
    let mut bytes = Vec::new();
    write_aarch64_executable(&exe, &mut bytes);
    NativeObjectArtifact {
        bytes,
        artifact_kind: "elf-executable",
        backend_level: "owned-native-subset-aarch64",
        runtime_level: "linux-syscall-exit",
        reason_code: "native-aarch64-linux-exit-subset",
        reason: "inauguration owns ELF64 AArch64 Linux executable emission for const-evaluable scalar entry functions on this target",
        abi_manifest: None,
    }
}

fn emit_arm32_elf_object(request: &NativeObjectRequest<'_>) -> NativeObjectArtifact {
    let object = ElfObject {
        code: arm32_return_i32_object_code(request.exit_code),
        export_name: request.entry.to_string(),
    };
    let mut bytes = Vec::new();
    write_arm32_relocatable_object(&object, &mut bytes);
    NativeObjectArtifact {
        bytes,
        artifact_kind: "elf32-relocatable-object",
        backend_level: "owned-object-subset",
        runtime_level: "none",
        reason_code: NATIVE_OBJECT_SUBSET,
        reason: "inauguration owns ELF32 ARM relocatable object emission for const-evaluable scalar entry functions on this target",
        abi_manifest: Some(object_abi_manifest(request)),
    }
}

fn emit_arm32_elf_executable(request: &NativeObjectRequest<'_>) -> NativeObjectArtifact {
    let exe = ElfExecutable {
        code: arm32_linux_exit_code(request.exit_code),
        entry_offset: 0,
    };
    let mut bytes = Vec::new();
    write_arm32_executable(&exe, &mut bytes);
    NativeObjectArtifact {
        bytes,
        artifact_kind: "elf32-executable",
        backend_level: "owned-native-subset-arm32",
        runtime_level: "linux-syscall-exit",
        reason_code: "native-armv7-linux-exit-subset",
        reason: "inauguration owns ELF32 ARM Linux executable emission for const-evaluable scalar entry functions on this target",
        abi_manifest: None,
    }
}

fn emit_aarch64_macho_archive(request: &NativeObjectRequest<'_>) -> NativeObjectArtifact {
    let image = MachOImage {
        code: aarch64_return_i32_object_code(request.exit_code),
        entry_offset: None,
        exports: vec![ExportSymbol {
            name: request.entry.to_string(),
            offset: 0,
        }],
    };
    let mut bytes = Vec::new();
    write_image(&image, MachOLinkage::StaticLib, request.entry, &mut bytes);
    NativeObjectArtifact {
        bytes,
        artifact_kind: "mach-o-static-archive",
        backend_level: "owned-object-subset",
        runtime_level: "none",
        reason_code: NATIVE_OBJECT_SUBSET,
        reason: "inauguration owns Mach-O static archive emission for const-evaluable scalar entry functions on this target",
        abi_manifest: Some(object_abi_manifest(request)),
    }
}

fn emit_x86_64_elf_executable(request: &NativeObjectRequest<'_>) -> NativeObjectArtifact {
    let exe = ElfExecutable {
        code: x86_64_linux_exit_code(request.exit_code),
        entry_offset: 0,
    };
    let mut bytes = Vec::new();
    write_executable(&exe, &mut bytes);
    NativeObjectArtifact {
        bytes,
        artifact_kind: "elf-executable",
        backend_level: "owned-native-subset-x86_64",
        runtime_level: "linux-syscall-exit",
        reason_code: "native-x86_64-linux-exit-subset",
        reason: "inauguration owns ELF64 Linux executable emission for const-evaluable scalar entry functions on this target",
        abi_manifest: None,
    }
}

fn emit_x86_64_freestanding_object(request: &NativeObjectRequest<'_>) -> NativeObjectArtifact {
    // Use real x86_64 lowering for freestanding targets
    match lower_module(request.module, request.entry) {
        Ok(result) => {
            let mut bytes = Vec::new();
            let object = ElfObject {
                code: result.code,
                export_name: request.entry.to_string(),
            };
            write_x86_64_relocatable_object(&object, &mut bytes);
            NativeObjectArtifact {
                bytes,
                artifact_kind: "elf-relocatable-object",
                backend_level: "owned-native-subset-freestanding",
                runtime_level: "freestanding-none",
                reason_code: "native-x86_64-unknown-none-freestanding",
                reason: "inauguration owns ELF64 relocatable object emission for real x86_64 freestanding function bodies",
                abi_manifest: Some(object_abi_manifest(request)),
            }
        }
        Err(_err) => {
            // Fallback to const-evaluated exit code for trivial functions
            let object = ElfObject {
                code: x86_64_return_i32_object_code(request.exit_code),
                export_name: request.entry.to_string(),
            };
            let mut bytes = Vec::new();
            write_x86_64_relocatable_object(&object, &mut bytes);
            NativeObjectArtifact {
                bytes,
                artifact_kind: "elf-relocatable-object",
                backend_level: "owned-object-subset",
                runtime_level: "freestanding-none",
                reason_code: "native-object-subset",
                reason: "inauguration owns ELF64 relocatable object emission for const-evaluable scalar entry functions on freestanding target",
                abi_manifest: Some(object_abi_manifest(request)),
            }
        }
    }
}

fn emit_wasm32_module(request: &NativeObjectRequest<'_>) -> NativeObjectArtifact {
    let module = WasmModule {
        export_name: request.entry.to_string(),
        value: request.exit_code,
    };
    let mut bytes = Vec::new();
    write_scalar_i32_module(&module, &mut bytes);
    NativeObjectArtifact {
        bytes,
        artifact_kind: "wasm-module",
        backend_level: "owned-object-subset",
        runtime_level: "none",
        reason_code: NATIVE_OBJECT_SUBSET,
        reason: "inauguration owns WebAssembly module emission for const-evaluable scalar entry functions on this target",
        abi_manifest: None,
    }
}

fn object_abi_manifest(request: &NativeObjectRequest<'_>) -> String {
    boundary_emit::emit_abi_manifest_with_package(
        &BoundaryModule {
            abi_version: IN_ABI_VERSION,
            module: request
                .module
                .effective_module_id(request.module_id)
                .to_string(),
            layouts: Vec::new(),
            symbols: Vec::new(),
            allocators: Vec::new(),
            layout_hash: String::new(),
        }
        .with_layout_hash(),
        request.module.identity.package.as_deref(),
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core_ir::UnifiedModule;

    #[test]
    fn dispatches_x86_64_staticlib_object() {
        let module = UnifiedModule::new(Vec::new());
        let request = NativeObjectRequest {
            target_triple: ELF_LINUX_TRIPLE,
            linkage: NativeLinkage::StaticLib,
            entry: "answer",
            exit_code: 42,
            module: &module,
            module_id: "App",
        };
        let artifact = emit_native_object(&request).expect("object artifact");
        assert_eq!(artifact.artifact_kind, "elf-relocatable-object");
        assert_eq!(artifact.reason_code, NATIVE_OBJECT_SUBSET);
        assert!(artifact.bytes.windows(6).any(|window| window == b"answer"));
        assert!(artifact.abi_manifest.is_some());
    }

    #[test]
    fn dispatches_wasm32_staticlib_module() {
        let module = UnifiedModule::new(Vec::new());
        let request = NativeObjectRequest {
            target_triple: WASM32_UNKNOWN_TRIPLE,
            linkage: NativeLinkage::StaticLib,
            entry: "answer",
            exit_code: 42,
            module: &module,
            module_id: "App",
        };
        let artifact = emit_native_object(&request).expect("wasm artifact");
        assert_eq!(artifact.artifact_kind, "wasm-module");
        assert_eq!(artifact.reason_code, NATIVE_OBJECT_SUBSET);
        assert!(artifact.bytes.windows(6).any(|window| window == b"answer"));
        assert!(artifact.abi_manifest.is_none());
    }

    #[test]
    fn ignores_unsupported_target() {
        let module = UnifiedModule::new(Vec::new());
        let request = NativeObjectRequest {
            target_triple: "riscv64gc-unknown-none-elf",
            linkage: NativeLinkage::StaticLib,
            entry: "answer",
            exit_code: 42,
            module: &module,
            module_id: "App",
        };
        assert!(emit_native_object(&request).is_none());
    }

    #[test]
    fn dispatches_aarch64_linux_staticlib_object() {
        let module = UnifiedModule::new(Vec::new());
        let request = NativeObjectRequest {
            target_triple: "aarch64-unknown-linux-gnu",
            linkage: NativeLinkage::StaticLib,
            entry: "answer",
            exit_code: 42,
            module: &module,
            module_id: "App",
        };
        let artifact = emit_native_object(&request).expect("aarch64 object artifact");
        assert_eq!(artifact.artifact_kind, "elf-relocatable-object");
        assert_eq!(artifact.reason_code, NATIVE_OBJECT_SUBSET);
        assert_eq!(
            u16::from_le_bytes([artifact.bytes[18], artifact.bytes[19]]),
            183
        );
    }

    #[test]
    fn dispatches_aarch64_apple_darwin_staticlib_archive() {
        let module = UnifiedModule::new(Vec::new());
        let request = NativeObjectRequest {
            target_triple: "aarch64-apple-darwin",
            linkage: NativeLinkage::StaticLib,
            entry: "answer",
            exit_code: 42,
            module: &module,
            module_id: "App",
        };
        let artifact = emit_native_object(&request).expect("macho archive artifact");
        assert_eq!(artifact.artifact_kind, "mach-o-static-archive");
        assert_eq!(artifact.reason_code, NATIVE_OBJECT_SUBSET);
        assert_eq!(&artifact.bytes[..8], b"!<arch>\n");
        assert!(artifact.bytes.windows(7).any(|window| window == b"_answer"));
    }

    #[test]
    fn dispatches_arm32_staticlib_object() {
        let module = UnifiedModule::new(Vec::new());
        let request = NativeObjectRequest {
            target_triple: "armv7-unknown-linux-gnueabihf",
            linkage: NativeLinkage::StaticLib,
            entry: "answer",
            exit_code: 42,
            module: &module,
            module_id: "App",
        };
        let artifact = emit_native_object(&request).expect("arm32 object artifact");
        assert_eq!(artifact.artifact_kind, "elf32-relocatable-object");
        assert_eq!(artifact.reason_code, NATIVE_OBJECT_SUBSET);
        assert_eq!(artifact.bytes[4], 1);
        assert_eq!(
            u16::from_le_bytes([artifact.bytes[18], artifact.bytes[19]]),
            40
        );
    }

    #[test]
    fn dispatches_x86_64_linux_executable() {
        let module = UnifiedModule::new(Vec::new());
        let request = NativeObjectRequest {
            target_triple: ELF_LINUX_TRIPLE,
            linkage: NativeLinkage::Executable,
            entry: "answer",
            exit_code: 42,
            module: &module,
            module_id: "App",
        };
        let artifact = emit_native_object(&request).expect("x86 executable artifact");
        assert_eq!(artifact.artifact_kind, "elf-executable");
        assert_eq!(artifact.reason_code, "native-x86_64-linux-exit-subset");
        assert_eq!(
            u16::from_le_bytes([artifact.bytes[16], artifact.bytes[17]]),
            2
        );
    }

    #[test]
    fn dispatches_aarch64_linux_executable() {
        let module = UnifiedModule::new(Vec::new());
        let request = NativeObjectRequest {
            target_triple: AARCH64_LINUX_TRIPLE,
            linkage: NativeLinkage::Executable,
            entry: "answer",
            exit_code: 42,
            module: &module,
            module_id: "App",
        };
        let artifact = emit_native_object(&request).expect("aarch64 executable artifact");
        assert_eq!(artifact.artifact_kind, "elf-executable");
        assert_eq!(artifact.reason_code, "native-aarch64-linux-exit-subset");
        assert_eq!(
            u16::from_le_bytes([artifact.bytes[18], artifact.bytes[19]]),
            183
        );
    }

    #[test]
    fn dispatches_arm32_linux_executable() {
        let module = UnifiedModule::new(Vec::new());
        let request = NativeObjectRequest {
            target_triple: ARMV7_LINUX_GNUEABIHF_TRIPLE,
            linkage: NativeLinkage::Executable,
            entry: "answer",
            exit_code: 42,
            module: &module,
            module_id: "App",
        };
        let artifact = emit_native_object(&request).expect("arm32 executable artifact");
        assert_eq!(artifact.artifact_kind, "elf32-executable");
        assert_eq!(artifact.reason_code, "native-armv7-linux-exit-subset");
        assert_eq!(artifact.bytes[4], 1);
        assert_eq!(
            u16::from_le_bytes([artifact.bytes[18], artifact.bytes[19]]),
            40
        );
    }
}