]> git.lizzy.rs Git - rust.git/commitdiff
Add a `allow_asm` option so virtual ISA based targets (JS/PNaCl/WAsm) can disallow...
authorRichard Diamond <wichard@vitalitystudios.com>
Thu, 20 Aug 2015 22:47:21 +0000 (17:47 -0500)
committerRichard Diamond <wichard@vitalitystudios.com>
Fri, 21 Aug 2015 22:50:39 +0000 (17:50 -0500)
src/librustc/lib.rs
src/librustc/middle/check_no_asm.rs [new file with mode: 0644]
src/librustc_back/target/mod.rs
src/librustc_driver/driver.rs

index 370405d82abdbfb9a5afb113aa801839b4bb2474..1047f51e95ffc261216f44f1038208825a4efcca 100644 (file)
@@ -112,6 +112,7 @@ pub mod middle {
     pub mod check_static_recursion;
     pub mod check_loop;
     pub mod check_match;
+    pub mod check_no_asm;
     pub mod check_rvalues;
     pub mod const_eval;
     pub mod dataflow;
diff --git a/src/librustc/middle/check_no_asm.rs b/src/librustc/middle/check_no_asm.rs
new file mode 100644 (file)
index 0000000..aa1b745
--- /dev/null
@@ -0,0 +1,41 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+/// Run over the whole crate and check for ExprInlineAsm.
+/// Inline asm isn't allowed on virtual ISA based targets, so we reject it
+/// here.
+
+use session::Session;
+
+use syntax::ast;
+use syntax::visit::Visitor;
+use syntax::visit;
+
+pub fn check_crate(sess: &Session, krate: &ast::Crate) {
+    if sess.target.target.options.allow_asm { return; }
+
+    visit::walk_crate(&mut CheckNoAsm { sess: sess, }, krate);
+}
+
+#[derive(Copy, Clone)]
+struct CheckNoAsm<'a> {
+    sess: &'a Session,
+}
+
+impl<'a, 'v> Visitor<'v> for CheckNoAsm<'a> {
+    fn visit_expr(&mut self, e: &ast::Expr) {
+        match e.node {
+            ast::ExprInlineAsm(_) => self.sess.span_err(e.span,
+                                                        "asm! is unsupported on this target"),
+            _ => {},
+        }
+        visit::walk_expr(self, e)
+    }
+}
index d9cfdaacc905957532d7fa7e4ead7b46648556aa..b603aad481db7c5fa792064f7568660c4b71f788 100644 (file)
@@ -168,6 +168,8 @@ pub struct TargetOptions {
     /// currently only "gnu" is used to fall into LLVM. Unknown strings cause
     /// the system linker to be used.
     pub archive_format: String,
+    /// Is asm!() allowed? Defaults to true.
+    pub allow_asm: bool,
     /// Whether the target uses a custom unwind resumption routine.
     /// By default LLVM lowers `resume` instructions into calls to `_Unwind_Resume`
     /// defined in libgcc.  If this option is enabled, the target must provide
@@ -217,6 +219,7 @@ fn default() -> TargetOptions {
             custom_unwind_resume: false,
             lib_allocation_crate: "alloc_system".to_string(),
             exe_allocation_crate: "alloc_system".to_string(),
+            allow_asm: true,
         }
     }
 }
@@ -310,6 +313,7 @@ macro_rules! key {
         key!(no_compiler_rt, bool);
         key!(pre_link_args, list);
         key!(post_link_args, list);
+        key!(allow_asm, bool);
 
         base
     }
index 346e7a7bf9886284802d5f8ee34ae8411ea4a830..f08c962a1c265ce0489eb756b48d0dc39faf53fe 100644 (file)
@@ -563,6 +563,9 @@ pub fn phase_2_configure_and_expand(sess: &Session,
     time(time_passes, "checking that all macro invocations are gone", ||
          syntax::ext::expand::check_for_macros(&sess.parse_sess, &krate));
 
+    time(time_passes, "checking for inline asm in case the target doesn't support it", ||
+         middle::check_no_asm::check_crate(sess, &krate));
+
     // One final feature gating of the true AST that gets compiled
     // later, to make sure we've got everything (e.g. configuration
     // can insert new attributes via `cfg_attr`)