Introduce a TargetFrontendConfig type. (#570)
* Introduce a `TargetFrontendConfig` type. `TargetFrontendConfig` is information specific to the target which is provided to frontends to allow them to produce Cranelift IR for the target. Currently this includes the pointer size and the default calling convention. The default calling convention is now inferred from the target, rather than being a setting. cranelift-native is now just a provider of target information, rather than also being a provider of settings, which gives it a clearer role. And instead of having cranelift-frontend routines require the whole `TargetIsa`, just require the `TargetFrontendConfig`, and add a way to get the `TargetFrontendConfig` from a `Module`. Fixes #529. Fixes #555.
This commit is contained in:
@@ -37,26 +37,16 @@ extern crate raw_cpuid;
|
||||
extern crate target_lexicon;
|
||||
|
||||
use cranelift_codegen::isa;
|
||||
use cranelift_codegen::settings::{self, Configurable};
|
||||
use cranelift_codegen::settings::Configurable;
|
||||
use target_lexicon::Triple;
|
||||
|
||||
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
||||
use raw_cpuid::CpuId;
|
||||
|
||||
/// Return `settings` and `isa` builders configured for the current host
|
||||
/// Return an `isa` builder configured for the current host
|
||||
/// machine, or `Err(())` if the host machine is not supported
|
||||
/// in the current configuration.
|
||||
pub fn builders() -> Result<(settings::Builder, isa::Builder), &'static str> {
|
||||
let mut flag_builder = settings::builder();
|
||||
|
||||
if cfg!(any(unix, target_os = "nebulet")) {
|
||||
flag_builder.set("call_conv", "system_v").unwrap();
|
||||
} else if cfg!(windows) {
|
||||
flag_builder.set("call_conv", "windows_fastcall").unwrap();
|
||||
} else {
|
||||
return Err("unrecognized environment");
|
||||
}
|
||||
|
||||
pub fn builder() -> Result<isa::Builder, &'static str> {
|
||||
let mut isa_builder = isa::lookup(Triple::host()).map_err(|err| match err {
|
||||
isa::LookupError::SupportDisabled => "support for architecture disabled at compile time",
|
||||
isa::LookupError::Unsupported => "unsupported architecture",
|
||||
@@ -66,7 +56,7 @@ pub fn builders() -> Result<(settings::Builder, isa::Builder), &'static str> {
|
||||
parse_x86_cpuid(&mut isa_builder)?;
|
||||
}
|
||||
|
||||
Ok((flag_builder, isa_builder))
|
||||
Ok(isa_builder)
|
||||
}
|
||||
|
||||
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
|
||||
@@ -108,3 +98,32 @@ fn parse_x86_cpuid(isa_builder: &mut isa::Builder) -> Result<(), &'static str> {
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::builder;
|
||||
use cranelift_codegen::isa::CallConv;
|
||||
use cranelift_codegen::settings;
|
||||
|
||||
#[test]
|
||||
fn test() {
|
||||
if let Ok(isa_builder) = builder() {
|
||||
let flag_builder = settings::builder();
|
||||
let isa = isa_builder.finish(settings::Flags::new(flag_builder));
|
||||
if cfg!(any(unix, target_os = "nebulet")) {
|
||||
assert_eq!(isa.default_call_conv(), CallConv::SystemV);
|
||||
} else if cfg!(windows) {
|
||||
assert_eq!(isa.default_call_conv(), CallConv::WindowsFastcall);
|
||||
}
|
||||
if cfg!(target_pointer_width = "64") {
|
||||
assert_eq!(isa.pointer_bits(), 64);
|
||||
}
|
||||
if cfg!(target_pointer_width = "32") {
|
||||
assert_eq!(isa.pointer_bits(), 32);
|
||||
}
|
||||
if cfg!(target_pointer_width = "16") {
|
||||
assert_eq!(isa.pointer_bits(), 16);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user