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
12 use rustc_ast::ptr::P;
14 use rustc_ast::tokenstream::TokenStream;
15 use rustc_errors::DiagnosticBuilder;
16 use rustc_expand::base::{self, *};
17 use rustc_span::symbol::Ident;
19 use smallvec::smallvec;
21 pub fn expand_global_asm<'cx>(
22 cx: &'cx mut ExtCtxt<'_>,
25 ) -> Box<dyn base::MacResult + 'cx> {
26 match parse_global_asm(cx, sp, tts) {
27 Ok(Some(global_asm)) => MacEager::items(smallvec![P(ast::Item {
28 ident: Ident::invalid(),
30 id: ast::DUMMY_NODE_ID,
31 kind: ast::ItemKind::GlobalAsm(P(global_asm)),
32 vis: ast::Visibility {
33 span: sp.shrink_to_lo(),
34 kind: ast::VisibilityKind::Inherited,
37 span: cx.with_def_site_ctxt(sp),
40 Ok(None) => DummyResult::any(sp),
48 fn parse_global_asm<'a>(
52 ) -> Result<Option<ast::GlobalAsm>, DiagnosticBuilder<'a>> {
53 let mut p = cx.new_parser_from_tts(tts);
55 if p.token == token::Eof {
56 let mut err = cx.struct_span_err(sp, "macro requires a string literal as an argument");
57 err.span_label(sp, "string literal required");
61 let expr = p.parse_expr()?;
62 let (asm, _) = match expr_to_string(cx, expr, "inline assembly must be a string literal") {
63 Some((s, st)) => (s, st),
64 None => return Ok(None),
67 Ok(Some(ast::GlobalAsm { asm }))