1 // Copyright 2012-2013 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.
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.
11 /// Module-level assembly support.
13 /// The macro defined here allows you to specify "top-level",
14 /// "file-scoped", or "module-level" assembly. These synonyms
15 /// all correspond to LLVM's module-level inline assembly instruction.
17 /// For example, `global_asm!("some assembly here")` codegens to
18 /// LLVM's `module asm "some assembly here"`. All of LLVM's caveats
21 use rustc_data_structures::small_vec::OneVector;
24 use syntax::codemap::respan;
25 use syntax::ext::base;
26 use syntax::ext::base::*;
27 use syntax::feature_gate;
29 use syntax::symbol::Symbol;
31 use syntax::tokenstream;
33 pub const MACRO: &'static str = "global_asm";
35 pub fn expand_global_asm<'cx>(cx: &'cx mut ExtCtxt,
37 tts: &[tokenstream::TokenTree]) -> Box<dyn base::MacResult + 'cx> {
38 if !cx.ecfg.enable_global_asm() {
39 feature_gate::emit_feature_err(&cx.parse_sess,
42 feature_gate::GateIssue::Language,
43 feature_gate::EXPLAIN_GLOBAL_ASM);
44 return DummyResult::any(sp);
47 let mut p = cx.new_parser_from_tts(tts);
48 let (asm, _) = match expr_to_string(cx,
49 panictry!(p.parse_expr()),
50 "inline assembly must be a string literal") {
51 Some((s, st)) => (s, st),
52 None => return DummyResult::any(sp),
55 MacEager::items(OneVector::one(P(ast::Item {
56 ident: ast::Ident::with_empty_ctxt(Symbol::intern("")),
58 id: ast::DUMMY_NODE_ID,
59 node: ast::ItemKind::GlobalAsm(P(ast::GlobalAsm {
63 vis: respan(sp.shrink_to_lo(), ast::VisibilityKind::Inherited),