Skip to content

Commit 8445e7a

Browse files
authored
Merge pull request #4 from termoshtt/init
* Skip creating proxy for initialization macros * LLVM_InitializeAllTargets * get_native_backend * Fill all init
2 parents 41a0329 + 24296e7 commit 8445e7a

File tree

3 files changed

+119
-0
lines changed

3 files changed

+119
-0
lines changed

build.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,19 @@ mod llvm {
5757
"execution_engine.rs",
5858
];
5959

60+
const INIT_MACROS: &[&str] = &[
61+
"LLVM_InitializeAllTargetInfos",
62+
"LLVM_InitializeAllTargets",
63+
"LLVM_InitializeAllTargetMCs",
64+
"LLVM_InitializeAllAsmPrinters",
65+
"LLVM_InitializeAllAsmParsers",
66+
"LLVM_InitializeAllDisassemblers",
67+
"LLVM_InitializeNativeTarget",
68+
"LLVM_InitializeNativeAsmParser",
69+
"LLVM_InitializeNativeAsmPrinter",
70+
"LLVM_InitializeNativeDisassembler",
71+
];
72+
6073
#[derive(Default)]
6174
pub struct Generator {
6275
declarations: Vec<Declaration>,
@@ -80,6 +93,11 @@ mod llvm {
8093
let mut file = File::create(path)?;
8194

8295
for decl in self.declarations {
96+
if INIT_MACROS.contains(&decl.name.as_str()) {
97+
// Skip target initialization wrappers
98+
// (see llvm-sys/wrappers/target.c)
99+
continue;
100+
}
83101
writeln!(
84102
file,
85103
"create_proxy!({}; {}; {});",

src/init.rs

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
use super::SHARED_LIB;
2+
use llvm_sys::prelude::LLVMBool;
3+
4+
use std::io::{BufRead, BufReader, Result};
5+
use std::process::Command;
6+
7+
const POSSIBLE_BACKENDS: &[&str] = &[
8+
"AArch64", "AMDGPU", "ARM", "BPF", "Hexagon", "Lanai", "Mips", "MSP430", "NVPTX", "PowerPC",
9+
"Sparc", "SystemZ", "X86", "XCore",
10+
];
11+
12+
fn get_native_arch() -> Result<String> {
13+
let output = Command::new("rustc").args(&["--print", "cfg"]).output()?;
14+
let buf = BufReader::new(output.stdout.as_slice());
15+
for line in buf.lines() {
16+
let line = line?;
17+
if !line.starts_with("target_arch") {
18+
continue;
19+
}
20+
// line should be like: target_arch="x86_64"
21+
return Ok(line.split('"').nth(1).unwrap().into());
22+
}
23+
unreachable!("`rustc --print cfg` result is wrong");
24+
}
25+
26+
fn arch2backend(arch: &str) -> String {
27+
match arch {
28+
"aarch64" => "AArch64".into(),
29+
"arm" => "ARM".into(),
30+
"mips" | "mips64" => "Mips".into(),
31+
"powerpc" | "powerpc64" => "PowerPC".into(),
32+
"sparc" | "sparc64" => "Sparc".into(),
33+
"x86" | "x86_64" => "X86".into(),
34+
_ => panic!("Unknown backend: {}", arch),
35+
}
36+
}
37+
38+
fn get_native_backend() -> String {
39+
let arch = get_native_arch().expect("Fail to get native arch");
40+
arch2backend(&arch)
41+
}
42+
43+
unsafe fn init_all(postfix: &str) {
44+
for backend in POSSIBLE_BACKENDS {
45+
let name = format!("LLVMInitialize{}{}", backend, postfix);
46+
if let Ok(entrypoint) = SHARED_LIB.get::<unsafe extern "C" fn()>(name.as_bytes()) {
47+
entrypoint();
48+
}
49+
}
50+
}
51+
52+
#[no_mangle]
53+
pub unsafe extern "C" fn LLVM_InitializeAllTargetInfos() {
54+
init_all("TargetInfo");
55+
}
56+
#[no_mangle]
57+
pub unsafe extern "C" fn LLVM_InitializeAllTargets() {
58+
init_all("Target");
59+
}
60+
#[no_mangle]
61+
pub unsafe extern "C" fn LLVM_InitializeAllTargetMCs() {
62+
init_all("TargetMC");
63+
}
64+
#[no_mangle]
65+
pub unsafe extern "C" fn LLVM_InitializeAllAsmParsers() {
66+
init_all("AsmParser");
67+
}
68+
#[no_mangle]
69+
pub unsafe extern "C" fn LLVM_InitializeAllAsmPrinters() {
70+
init_all("AsmPrinter");
71+
}
72+
73+
unsafe fn init_native(postfix: &str) -> LLVMBool {
74+
let backend = get_native_backend();
75+
let name = format!("LLVMInitialize{}{}", backend, postfix);
76+
if let Ok(entrypoint) = SHARED_LIB.get::<unsafe extern "C" fn()>(name.as_bytes()) {
77+
entrypoint();
78+
0
79+
} else {
80+
1
81+
}
82+
}
83+
84+
#[no_mangle]
85+
pub unsafe extern "C" fn LLVM_InitializeNativeTarget() -> LLVMBool {
86+
init_native("Target")
87+
}
88+
#[no_mangle]
89+
pub unsafe extern "C" fn LLVM_InitializeNativeAsmParser() -> LLVMBool {
90+
init_native("AsmParser")
91+
}
92+
#[no_mangle]
93+
pub unsafe extern "C" fn LLVM_InitializeNativeAsmPrinter() -> LLVMBool {
94+
init_native("AsmPrinter")
95+
}
96+
#[no_mangle]
97+
pub unsafe extern "C" fn LLVM_InitializeNativeDisassembler() -> LLVMBool {
98+
init_native("Disassembler")
99+
}

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ use lib::Library;
4545
mod path;
4646
use path::find_lib_path;
4747

48+
pub mod init;
49+
4850
lazy_static! {
4951
static ref SHARED_LIB: Library = {
5052
let lib_path = match find_lib_path() {

0 commit comments

Comments
 (0)