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::source_map::respan;
18 use rustc_span::symbol::Ident;
20 use smallvec::smallvec;
22 pub fn expand_global_asm<'cx>(
23 cx: &'cx mut ExtCtxt<'_>,
26 ) -> Box<dyn base::MacResult + 'cx> {
27 match parse_global_asm(cx, sp, tts) {
28 Ok(Some(global_asm)) => MacEager::items(smallvec![P(ast::Item {
29 ident: Ident::invalid(),
31 id: ast::DUMMY_NODE_ID,
32 kind: ast::ItemKind::GlobalAsm(P(global_asm)),
33 vis: respan(sp.shrink_to_lo(), ast::VisibilityKind::Inherited),
34 span: cx.with_def_site_ctxt(sp),
37 Ok(None) => DummyResult::any(sp),
45 fn parse_global_asm<'a>(
49 ) -> Result<Option<ast::GlobalAsm>, DiagnosticBuilder<'a>> {
50 let mut p = cx.new_parser_from_tts(tts);
52 if p.token == token::Eof {
53 let mut err = cx.struct_span_err(sp, "macro requires a string literal as an argument");
54 err.span_label(sp, "string literal required");
58 let expr = p.parse_expr()?;
59 let (asm, _) = match expr_to_string(cx, expr, "inline assembly must be a string literal") {
60 Some((s, st)) => (s, st),
61 None => return Ok(None),
64 Ok(Some(ast::GlobalAsm { asm }))