]>
Commit | Line | Data |
---|---|---|
e9174d1e SL |
1 | // Copyright 2015 The Rust Project Developers. See the COPYRIGHT |
2 | // file at the top-level directory of this distribution and at | |
3 | // http://rust-lang.org/COPYRIGHT. | |
4 | // | |
5 | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | // option. This file may not be copied, modified, or distributed | |
9 | // except according to those terms. | |
10 | ||
11 | use syntax::{ast, attr}; | |
a7813a04 | 12 | use llvm::LLVMRustHasFeature; |
e9174d1e | 13 | use rustc::session::Session; |
a7813a04 | 14 | use rustc_trans::back::write::create_target_machine; |
e9174d1e SL |
15 | use syntax::parse::token::InternedString; |
16 | use syntax::parse::token::intern_and_get_ident as intern; | |
a7813a04 XL |
17 | use libc::c_char; |
18 | ||
19 | // WARNING: the features must be known to LLVM or the feature | |
20 | // detection code will walk past the end of the feature array, | |
21 | // leading to crashes. | |
22 | ||
c30ab7b3 | 23 | const ARM_WHITELIST: &'static [&'static str] = &["neon\0", "vfp2\0", "vfp3\0", "vfp4\0"]; |
a7813a04 | 24 | |
c30ab7b3 SL |
25 | const X86_WHITELIST: &'static [&'static str] = &["avx\0", "avx2\0", "bmi\0", "bmi2\0", "sse\0", |
26 | "sse2\0", "sse3\0", "sse4.1\0", "sse4.2\0", | |
27 | "ssse3\0", "tbm\0"]; | |
e9174d1e SL |
28 | |
29 | /// Add `target_feature = "..."` cfgs for a variety of platform | |
30 | /// specific features (SSE, NEON etc.). | |
31 | /// | |
a7813a04 XL |
32 | /// This is performed by checking whether a whitelisted set of |
33 | /// features is available on the target machine, by querying LLVM. | |
e9174d1e | 34 | pub fn add_configuration(cfg: &mut ast::CrateConfig, sess: &Session) { |
a7813a04 | 35 | let target_machine = create_target_machine(sess); |
e9174d1e | 36 | |
a7813a04 XL |
37 | let whitelist = match &*sess.target.target.arch { |
38 | "arm" => ARM_WHITELIST, | |
39 | "x86" | "x86_64" => X86_WHITELIST, | |
40 | _ => &[], | |
41 | }; | |
e9174d1e | 42 | |
a7813a04 XL |
43 | let tf = InternedString::new("target_feature"); |
44 | for feat in whitelist { | |
45 | assert_eq!(feat.chars().last(), Some('\0')); | |
46 | if unsafe { LLVMRustHasFeature(target_machine, feat.as_ptr() as *const c_char) } { | |
c30ab7b3 | 47 | cfg.push(attr::mk_name_value_item_str(tf.clone(), intern(&feat[..feat.len() - 1]))) |
a7813a04 XL |
48 | } |
49 | } | |
e9174d1e | 50 | } |