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.
|
|
|
|
|
2019-02-07 15:56:05 +00:00
|
|
|
use errors::DiagnosticBuilder;
|
2019-02-04 12:49:54 +00:00
|
|
|
|
2017-03-16 02:27:40 +00:00
|
|
|
use syntax::ast;
|
2018-08-18 10:14:03 +00:00
|
|
|
use syntax::source_map::respan;
|
2019-10-16 08:59:30 +00:00
|
|
|
use syntax_expand::base::{self, *};
|
2019-10-11 10:46:32 +00:00
|
|
|
use syntax::token;
|
2017-03-16 02:27:40 +00:00
|
|
|
use syntax::ptr::P;
|
|
|
|
use syntax_pos::Span;
|
2019-08-31 17:08:06 +00:00
|
|
|
use syntax::tokenstream::TokenStream;
|
2019-02-04 12:49:54 +00:00
|
|
|
use smallvec::smallvec;
|
2017-03-16 02:27:40 +00:00
|
|
|
|
2019-02-04 12:49:54 +00:00
|
|
|
pub fn expand_global_asm<'cx>(cx: &'cx mut ExtCtxt<'_>,
|
2017-03-16 02:27:40 +00:00
|
|
|
sp: Span,
|
2019-08-31 17:08:06 +00:00
|
|
|
tts: TokenStream) -> Box<dyn base::MacResult + 'cx> {
|
2018-12-04 19:10:32 +00:00
|
|
|
match parse_global_asm(cx, sp, tts) {
|
|
|
|
Ok(Some(global_asm)) => {
|
|
|
|
MacEager::items(smallvec![P(ast::Item {
|
2019-05-11 16:08:09 +00:00
|
|
|
ident: ast::Ident::invalid(),
|
2018-12-04 19:10:32 +00:00
|
|
|
attrs: Vec::new(),
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2019-09-26 16:51:36 +00:00
|
|
|
kind: ast::ItemKind::GlobalAsm(P(global_asm)),
|
2018-12-04 19:10:32 +00:00
|
|
|
vis: respan(sp.shrink_to_lo(), ast::VisibilityKind::Inherited),
|
2019-09-14 20:17:11 +00:00
|
|
|
span: cx.with_def_site_ctxt(sp),
|
2018-12-04 19:10:32 +00:00
|
|
|
tokens: None,
|
|
|
|
})])
|
|
|
|
}
|
|
|
|
Ok(None) => DummyResult::any(sp),
|
|
|
|
Err(mut err) => {
|
|
|
|
err.emit();
|
|
|
|
DummyResult::any(sp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_global_asm<'a>(
|
|
|
|
cx: &mut ExtCtxt<'a>,
|
|
|
|
sp: Span,
|
2019-08-31 17:08:06 +00:00
|
|
|
tts: TokenStream
|
2018-12-04 19:10:32 +00:00
|
|
|
) -> Result<Option<ast::GlobalAsm>, DiagnosticBuilder<'a>> {
|
2017-03-16 02:27:40 +00:00
|
|
|
let mut p = cx.new_parser_from_tts(tts);
|
2018-12-04 19:10:32 +00:00
|
|
|
|
|
|
|
if p.token == token::Eof {
|
|
|
|
let mut err = cx.struct_span_err(sp, "macro requires a string literal as an argument");
|
|
|
|
err.span_label(sp, "string literal required");
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
let expr = p.parse_expr()?;
|
|
|
|
let (asm, _) = match expr_to_string(cx, expr, "inline assembly must be a string literal") {
|
2017-03-16 02:27:40 +00:00
|
|
|
Some((s, st)) => (s, st),
|
2018-12-04 19:10:32 +00:00
|
|
|
None => return Ok(None),
|
2017-03-16 02:27:40 +00:00
|
|
|
};
|
|
|
|
|
2019-08-12 21:12:53 +00:00
|
|
|
Ok(Some(ast::GlobalAsm { asm }))
|
2017-03-16 02:27:40 +00:00
|
|
|
}
|