1 /// Module-level assembly support.
3 /// The macro defined here allows you to specify "top-level",
4 /// "file-scoped", or "module-level" assembly. These synonyms
5 /// all correspond to LLVM's module-level inline assembly instruction.
7 /// For example, `global_asm!("some assembly here")` codegens to
8 /// LLVM's `module asm "some assembly here"`. All of LLVM's caveats
11 use errors::DiagnosticBuilder;
14 use syntax::source_map::respan;
15 use syntax::ext::base::{self, *};
16 use syntax::feature_gate;
17 use syntax::parse::token;
19 use syntax::symbol::{Symbol, sym};
21 use syntax::tokenstream;
22 use smallvec::smallvec;
24 pub const MACRO: Symbol = sym::global_asm;
26 pub fn expand_global_asm<'cx>(cx: &'cx mut ExtCtxt<'_>,
28 tts: &[tokenstream::TokenTree]) -> Box<dyn base::MacResult + 'cx> {
29 if !cx.ecfg.enable_global_asm() {
30 feature_gate::emit_feature_err(&cx.parse_sess,
33 feature_gate::GateIssue::Language,
34 feature_gate::EXPLAIN_GLOBAL_ASM);
37 match parse_global_asm(cx, sp, tts) {
38 Ok(Some(global_asm)) => {
39 MacEager::items(smallvec![P(ast::Item {
40 ident: ast::Ident::with_empty_ctxt(Symbol::intern("")),
42 id: ast::DUMMY_NODE_ID,
43 node: ast::ItemKind::GlobalAsm(P(global_asm)),
44 vis: respan(sp.shrink_to_lo(), ast::VisibilityKind::Inherited),
49 Ok(None) => DummyResult::any(sp),
57 fn parse_global_asm<'a>(
60 tts: &[tokenstream::TokenTree]
61 ) -> Result<Option<ast::GlobalAsm>, DiagnosticBuilder<'a>> {
62 let mut p = cx.new_parser_from_tts(tts);
64 if p.token == token::Eof {
65 let mut err = cx.struct_span_err(sp, "macro requires a string literal as an argument");
66 err.span_label(sp, "string literal required");
70 let expr = p.parse_expr()?;
71 let (asm, _) = match expr_to_string(cx, expr, "inline assembly must be a string literal") {
72 Some((s, st)) => (s, st),
73 None => return Ok(None),
76 Ok(Some(ast::GlobalAsm {