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 rustc_errors::DiagnosticBuilder;
12 use rustc_expand::base::{self, *};
13 use rustc_span::source_map::respan;
15 use smallvec::smallvec;
19 use syntax::tokenstream::TokenStream;
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: ast::Ident::invalid(),
30 id: ast::DUMMY_NODE_ID,
31 kind: ast::ItemKind::GlobalAsm(P(global_asm)),
32 vis: respan(sp.shrink_to_lo(), ast::VisibilityKind::Inherited),
33 span: cx.with_def_site_ctxt(sp),
36 Ok(None) => DummyResult::any(sp),
44 fn parse_global_asm<'a>(
48 ) -> Result<Option<ast::GlobalAsm>, DiagnosticBuilder<'a>> {
49 let mut p = cx.new_parser_from_tts(tts);
51 if p.token == token::Eof {
52 let mut err = cx.struct_span_err(sp, "macro requires a string literal as an argument");
53 err.span_label(sp, "string literal required");
57 let expr = p.parse_expr()?;
58 let (asm, _) = match expr_to_string(cx, expr, "inline assembly must be a string literal") {
59 Some((s, st)) => (s, st),
60 None => return Ok(None),
63 Ok(Some(ast::GlobalAsm { asm }))