rust/src/libsyntax_ext/global_asm.rs

55 lines
1.9 KiB
Rust
Raw Normal View History

2017-03-16 02:27:40 +00:00
/// Module-level assembly support.
///
/// The macro defined here allows you to specify "top-level",
/// "file-scoped", or "module-level" assembly. These synonyms
/// all correspond to LLVM's module-level inline assembly instruction.
///
2018-05-08 13:10:16 +00:00
/// For example, `global_asm!("some assembly here")` codegens to
2017-03-16 02:27:40 +00:00
/// LLVM's `module asm "some assembly here"`. All of LLVM's caveats
/// therefore apply.
use syntax::ast;
2018-08-18 10:14:03 +00:00
use syntax::source_map::respan;
2017-03-16 02:27:40 +00:00
use syntax::ext::base;
use syntax::ext::base::*;
use syntax::feature_gate;
use syntax::ptr::P;
use syntax::symbol::Symbol;
use syntax_pos::Span;
use syntax::tokenstream;
pub const MACRO: &str = "global_asm";
2017-03-16 02:27:40 +00:00
pub fn expand_global_asm<'cx>(cx: &'cx mut ExtCtxt,
sp: Span,
tts: &[tokenstream::TokenTree]) -> Box<dyn base::MacResult + 'cx> {
2017-03-16 02:27:40 +00:00
if !cx.ecfg.enable_global_asm() {
feature_gate::emit_feature_err(&cx.parse_sess,
MACRO,
sp,
feature_gate::GateIssue::Language,
feature_gate::EXPLAIN_GLOBAL_ASM);
}
let mut p = cx.new_parser_from_tts(tts);
let (asm, _) = match expr_to_string(cx,
panictry!(p.parse_expr()),
"inline assembly must be a string literal") {
Some((s, st)) => (s, st),
None => return DummyResult::any(sp),
};
2018-08-13 19:15:16 +00:00
MacEager::items(smallvec![P(ast::Item {
2017-03-16 02:27:40 +00:00
ident: ast::Ident::with_empty_ctxt(Symbol::intern("")),
attrs: Vec::new(),
id: ast::DUMMY_NODE_ID,
node: ast::ItemKind::GlobalAsm(P(ast::GlobalAsm {
asm,
2017-03-16 02:27:40 +00:00
ctxt: cx.backtrace(),
})),
vis: respan(sp.shrink_to_lo(), ast::VisibilityKind::Inherited),
2017-03-16 02:27:40 +00:00
span: sp,
tokens: None,
2018-08-13 19:15:16 +00:00
})])
2017-03-16 02:27:40 +00:00
}