]> git.proxmox.com Git - rustc.git/blob - src/librustc_driver/target_features.rs
New upstream version 1.14.0+dfsg1
[rustc.git] / src / librustc_driver / target_features.rs
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};
12 use llvm::LLVMRustHasFeature;
13 use rustc::session::Session;
14 use rustc_trans::back::write::create_target_machine;
15 use syntax::parse::token::InternedString;
16 use syntax::parse::token::intern_and_get_ident as intern;
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
23 const ARM_WHITELIST: &'static [&'static str] = &["neon\0", "vfp2\0", "vfp3\0", "vfp4\0"];
24
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"];
28
29 /// Add `target_feature = "..."` cfgs for a variety of platform
30 /// specific features (SSE, NEON etc.).
31 ///
32 /// This is performed by checking whether a whitelisted set of
33 /// features is available on the target machine, by querying LLVM.
34 pub fn add_configuration(cfg: &mut ast::CrateConfig, sess: &Session) {
35 let target_machine = create_target_machine(sess);
36
37 let whitelist = match &*sess.target.target.arch {
38 "arm" => ARM_WHITELIST,
39 "x86" | "x86_64" => X86_WHITELIST,
40 _ => &[],
41 };
42
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) } {
47 cfg.push(attr::mk_name_value_item_str(tf.clone(), intern(&feat[..feat.len() - 1])))
48 }
49 }
50 }