]> git.proxmox.com Git - rustc.git/blob - src/librustc_driver/target_features.rs
New upstream version 1.12.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] = &[
24 "neon\0",
25 "vfp2\0",
26 "vfp3\0",
27 "vfp4\0",
28 ];
29
30 const X86_WHITELIST: &'static [&'static str] = &[
31 "avx\0",
32 "avx2\0",
33 "bmi\0",
34 "bmi2\0",
35 "sse\0",
36 "sse2\0",
37 "sse3\0",
38 "sse4.1\0",
39 "sse4.2\0",
40 "ssse3\0",
41 "tbm\0",
42 ];
43
44 /// Add `target_feature = "..."` cfgs for a variety of platform
45 /// specific features (SSE, NEON etc.).
46 ///
47 /// This is performed by checking whether a whitelisted set of
48 /// features is available on the target machine, by querying LLVM.
49 pub fn add_configuration(cfg: &mut ast::CrateConfig, sess: &Session) {
50 let target_machine = create_target_machine(sess);
51
52 let whitelist = match &*sess.target.target.arch {
53 "arm" => ARM_WHITELIST,
54 "x86" | "x86_64" => X86_WHITELIST,
55 _ => &[],
56 };
57
58 let tf = InternedString::new("target_feature");
59 for feat in whitelist {
60 assert_eq!(feat.chars().last(), Some('\0'));
61 if unsafe { LLVMRustHasFeature(target_machine, feat.as_ptr() as *const c_char) } {
62 cfg.push(attr::mk_name_value_item_str(tf.clone(), intern(&feat[..feat.len()-1])))
63 }
64 }
65 }