2020-01-14 07:40:42 -06:00
|
|
|
// Llvm-style inline assembly support.
|
2016-06-06 09:52:48 -05:00
|
|
|
//
|
2019-02-04 06:49:54 -06:00
|
|
|
use State::*;
|
2013-03-11 00:08:38 -05:00
|
|
|
|
2020-04-27 12:56:11 -05:00
|
|
|
use rustc_ast as ast;
|
2020-02-29 11:37:32 -06:00
|
|
|
use rustc_ast::ptr::P;
|
|
|
|
use rustc_ast::token::{self, Token};
|
|
|
|
use rustc_ast::tokenstream::{self, TokenStream};
|
2020-04-27 12:56:11 -05:00
|
|
|
use rustc_ast::LlvmAsmDialect;
|
2020-01-09 04:18:47 -06:00
|
|
|
use rustc_errors::{struct_span_err, DiagnosticBuilder, PResult};
|
2019-12-29 08:23:55 -06:00
|
|
|
use rustc_expand::base::*;
|
2019-11-10 08:04:12 -06:00
|
|
|
use rustc_parse::parser::Parser;
|
2020-01-01 12:30:57 -06:00
|
|
|
use rustc_span::symbol::{kw, sym, Symbol};
|
2019-12-31 11:15:40 -06:00
|
|
|
use rustc_span::Span;
|
2014-06-11 21:33:52 -05:00
|
|
|
|
2013-03-12 02:01:09 -05:00
|
|
|
enum State {
|
|
|
|
Asm,
|
|
|
|
Outputs,
|
|
|
|
Inputs,
|
|
|
|
Clobbers,
|
2014-03-09 17:41:18 -05:00
|
|
|
Options,
|
2016-06-06 09:52:48 -05:00
|
|
|
StateNone,
|
2013-03-12 02:01:09 -05:00
|
|
|
}
|
|
|
|
|
2014-03-09 17:41:18 -05:00
|
|
|
impl State {
|
|
|
|
fn next(&self) -> State {
|
|
|
|
match *self {
|
2016-06-06 09:52:48 -05:00
|
|
|
Asm => Outputs,
|
|
|
|
Outputs => Inputs,
|
|
|
|
Inputs => Clobbers,
|
|
|
|
Clobbers => Options,
|
|
|
|
Options => StateNone,
|
|
|
|
StateNone => StateNone,
|
2014-03-09 17:41:18 -05:00
|
|
|
}
|
2013-03-12 02:01:09 -05:00
|
|
|
}
|
|
|
|
}
|
2013-03-11 00:08:38 -05:00
|
|
|
|
2019-05-07 01:03:44 -05:00
|
|
|
const OPTIONS: &[Symbol] = &[sym::volatile, sym::alignstack, sym::intel];
|
2014-03-09 17:41:18 -05:00
|
|
|
|
2020-01-14 07:40:42 -06:00
|
|
|
pub fn expand_llvm_asm<'cx>(
|
2019-12-22 16:42:04 -06:00
|
|
|
cx: &'cx mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
|
|
|
) -> Box<dyn MacResult + 'cx> {
|
2018-12-04 13:10:32 -06:00
|
|
|
let mut inline_asm = match parse_inline_asm(cx, sp, tts) {
|
|
|
|
Ok(Some(inline_asm)) => inline_asm,
|
2019-08-13 12:51:54 -05:00
|
|
|
Ok(None) => return DummyResult::any(sp),
|
2018-12-04 13:10:32 -06:00
|
|
|
Err(mut err) => {
|
|
|
|
err.emit();
|
2019-08-13 12:51:54 -05:00
|
|
|
return DummyResult::any(sp);
|
2018-12-04 13:10:32 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// If there are no outputs, the inline assembly is executed just for its side effects,
|
|
|
|
// so ensure that it is volatile
|
|
|
|
if inline_asm.outputs.is_empty() {
|
|
|
|
inline_asm.volatile = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
MacEager::expr(P(ast::Expr {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2020-01-14 07:40:42 -06:00
|
|
|
kind: ast::ExprKind::LlvmInlineAsm(P(inline_asm)),
|
2019-09-14 15:17:11 -05:00
|
|
|
span: cx.with_def_site_ctxt(sp),
|
2019-12-03 09:38:34 -06:00
|
|
|
attrs: ast::AttrVec::new(),
|
2020-05-19 15:56:20 -05:00
|
|
|
tokens: None,
|
2018-12-04 13:10:32 -06:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2019-11-10 08:04:12 -06:00
|
|
|
fn parse_asm_str<'a>(p: &mut Parser<'a>) -> PResult<'a, Symbol> {
|
|
|
|
match p.parse_str_lit() {
|
|
|
|
Ok(str_lit) => Ok(str_lit.symbol_unescaped),
|
|
|
|
Err(opt_lit) => {
|
|
|
|
let span = opt_lit.map_or(p.token.span, |lit| lit.span);
|
2019-11-16 11:11:05 -06:00
|
|
|
let mut err = p.sess.span_diagnostic.struct_span_err(span, "expected string literal");
|
|
|
|
err.span_label(span, "not a string literal");
|
2019-11-10 08:04:12 -06:00
|
|
|
Err(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-04 13:10:32 -06:00
|
|
|
fn parse_inline_asm<'a>(
|
|
|
|
cx: &mut ExtCtxt<'a>,
|
|
|
|
sp: Span,
|
2019-08-31 12:08:06 -05:00
|
|
|
tts: TokenStream,
|
2020-01-14 07:40:42 -06:00
|
|
|
) -> Result<Option<ast::LlvmInlineAsm>, DiagnosticBuilder<'a>> {
|
|
|
|
// Split the tts before the first colon, to avoid `llvm_asm!("x": y)` being
|
|
|
|
// parsed as `llvm_asm!(z)` with `z = "x": y` which is type ascription.
|
2019-12-22 16:42:04 -06:00
|
|
|
let first_colon = tts
|
|
|
|
.trees()
|
|
|
|
.position(|tt| match tt {
|
2020-04-16 19:38:52 -05:00
|
|
|
tokenstream::TokenTree::Token(Token { kind: token::Colon | token::ModSep, .. }) => true,
|
2019-12-22 16:42:04 -06:00
|
|
|
_ => false,
|
2016-06-06 09:52:48 -05:00
|
|
|
})
|
|
|
|
.unwrap_or(tts.len());
|
2019-08-31 12:08:06 -05:00
|
|
|
let mut p = cx.new_parser_from_tts(tts.trees().skip(first_colon).collect());
|
2019-05-21 21:42:23 -05:00
|
|
|
let mut asm = kw::Invalid;
|
2013-10-07 19:49:10 -05:00
|
|
|
let mut asm_str_style = None;
|
2014-02-28 15:09:09 -06:00
|
|
|
let mut outputs = Vec::new();
|
|
|
|
let mut inputs = Vec::new();
|
2014-11-29 20:56:31 -06:00
|
|
|
let mut clobs = Vec::new();
|
2013-03-12 02:01:09 -05:00
|
|
|
let mut volatile = false;
|
2013-03-12 03:02:58 -05:00
|
|
|
let mut alignstack = false;
|
2020-01-14 07:40:42 -06:00
|
|
|
let mut dialect = LlvmAsmDialect::Att;
|
2013-03-12 02:01:09 -05:00
|
|
|
|
|
|
|
let mut state = Asm;
|
2013-04-26 18:19:26 -05:00
|
|
|
|
2014-03-09 17:41:18 -05:00
|
|
|
'statement: loop {
|
2013-03-12 02:01:09 -05:00
|
|
|
match state {
|
|
|
|
Asm => {
|
2015-01-14 09:02:20 -06:00
|
|
|
if asm_str_style.is_some() {
|
|
|
|
// If we already have a string with instructions,
|
|
|
|
// ending up in Asm state again is an error.
|
2018-12-04 13:10:32 -06:00
|
|
|
return Err(struct_span_err!(
|
2020-07-29 20:27:50 -05:00
|
|
|
cx.sess.parse_sess.span_diagnostic,
|
2018-12-04 13:10:32 -06:00
|
|
|
sp,
|
|
|
|
E0660,
|
|
|
|
"malformed inline assembly"
|
|
|
|
));
|
2015-01-14 09:02:20 -06:00
|
|
|
}
|
2015-02-01 01:59:46 -06:00
|
|
|
// Nested parser, stop before the first colon (see above).
|
2019-12-22 16:42:04 -06:00
|
|
|
let mut p2 = cx.new_parser_from_tts(tts.trees().take(first_colon).collect());
|
2018-12-04 13:10:32 -06:00
|
|
|
|
|
|
|
if p2.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 = p2.parse_expr()?;
|
|
|
|
let (s, style) =
|
|
|
|
match expr_to_string(cx, expr, "inline assembly must be a string literal") {
|
|
|
|
Some((s, st)) => (s, st),
|
|
|
|
None => return Ok(None),
|
|
|
|
};
|
2015-02-01 01:59:46 -06:00
|
|
|
|
|
|
|
// This is most likely malformed.
|
|
|
|
if p2.token != token::Eof {
|
2018-12-04 13:10:32 -06:00
|
|
|
let mut extra_tts = p2.parse_all_token_trees()?;
|
2019-08-31 12:08:06 -05:00
|
|
|
extra_tts.extend(tts.trees().skip(first_colon));
|
|
|
|
p = cx.new_parser_from_tts(extra_tts.into_iter().collect());
|
2015-02-01 01:59:46 -06:00
|
|
|
}
|
|
|
|
|
2013-10-07 19:49:10 -05:00
|
|
|
asm = s;
|
|
|
|
asm_str_style = Some(style);
|
2013-03-12 02:01:09 -05:00
|
|
|
}
|
|
|
|
Outputs => {
|
2016-06-06 09:52:48 -05:00
|
|
|
while p.token != token::Eof && p.token != token::Colon && p.token != token::ModSep {
|
2015-03-24 18:54:09 -05:00
|
|
|
if !outputs.is_empty() {
|
2015-12-30 17:11:53 -06:00
|
|
|
p.eat(&token::Comma);
|
2013-03-12 02:01:09 -05:00
|
|
|
}
|
2013-03-12 02:09:53 -05:00
|
|
|
|
2019-11-10 08:04:12 -06:00
|
|
|
let constraint = parse_asm_str(&mut p)?;
|
2013-10-17 13:24:41 -05:00
|
|
|
|
2020-02-29 05:56:15 -06:00
|
|
|
let span = p.prev_token.span;
|
2013-10-17 13:24:41 -05:00
|
|
|
|
2018-12-04 13:10:32 -06:00
|
|
|
p.expect(&token::OpenDelim(token::Paren))?;
|
|
|
|
let expr = p.parse_expr()?;
|
|
|
|
p.expect(&token::CloseDelim(token::Paren))?;
|
2013-03-12 02:01:09 -05:00
|
|
|
|
2014-03-09 17:41:18 -05:00
|
|
|
// Expands a read+write operand into two operands.
|
|
|
|
//
|
|
|
|
// Use '+' modifier when you want the same expression
|
|
|
|
// to be both an input and an output at the same time.
|
|
|
|
// It's the opposite of '=&' which means that the memory
|
|
|
|
// cannot be shared with any other operand (usually when
|
|
|
|
// a register is clobbered early.)
|
2016-11-16 04:52:37 -06:00
|
|
|
let constraint_str = constraint.as_str();
|
|
|
|
let mut ch = constraint_str.chars();
|
2016-04-07 12:42:53 -05:00
|
|
|
let output = match ch.next() {
|
|
|
|
Some('=') => None,
|
2019-12-22 16:42:04 -06:00
|
|
|
Some('+') => Some(Symbol::intern(&format!("={}", ch.as_str()))),
|
2014-03-09 17:41:18 -05:00
|
|
|
_ => {
|
2019-12-31 14:25:16 -06:00
|
|
|
struct_span_err!(
|
2020-07-29 20:27:50 -05:00
|
|
|
cx.sess.parse_sess.span_diagnostic,
|
2019-12-22 16:42:04 -06:00
|
|
|
span,
|
|
|
|
E0661,
|
|
|
|
"output operand constraint lacks '=' or '+'"
|
2019-12-31 14:25:16 -06:00
|
|
|
)
|
|
|
|
.emit();
|
2014-03-09 17:41:18 -05:00
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-19 14:39:26 -05:00
|
|
|
let is_rw = output.is_some();
|
2020-02-26 06:03:46 -06:00
|
|
|
let is_indirect = constraint_str.contains('*');
|
2020-01-14 07:40:42 -06:00
|
|
|
outputs.push(ast::LlvmInlineAsmOutput {
|
2016-11-16 04:52:37 -06:00
|
|
|
constraint: output.unwrap_or(constraint),
|
2018-12-04 13:10:32 -06:00
|
|
|
expr,
|
2017-08-07 00:54:09 -05:00
|
|
|
is_rw,
|
|
|
|
is_indirect,
|
2015-12-05 02:18:24 -06:00
|
|
|
});
|
2013-03-12 02:01:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Inputs => {
|
2016-06-06 09:52:48 -05:00
|
|
|
while p.token != token::Eof && p.token != token::Colon && p.token != token::ModSep {
|
2015-03-24 18:54:09 -05:00
|
|
|
if !inputs.is_empty() {
|
2015-12-30 17:11:53 -06:00
|
|
|
p.eat(&token::Comma);
|
2013-03-12 02:01:09 -05:00
|
|
|
}
|
2013-03-12 02:09:53 -05:00
|
|
|
|
2019-11-10 08:04:12 -06:00
|
|
|
let constraint = parse_asm_str(&mut p)?;
|
2013-10-17 13:24:41 -05:00
|
|
|
|
2020-02-26 06:03:46 -06:00
|
|
|
if constraint.as_str().starts_with('=') {
|
2019-12-31 14:25:16 -06:00
|
|
|
struct_span_err!(
|
2020-07-29 20:27:50 -05:00
|
|
|
cx.sess.parse_sess.span_diagnostic,
|
2020-02-29 05:56:15 -06:00
|
|
|
p.prev_token.span,
|
2019-12-31 14:25:16 -06:00
|
|
|
E0662,
|
|
|
|
"input operand constraint contains '='"
|
|
|
|
)
|
|
|
|
.emit();
|
2020-02-26 06:03:46 -06:00
|
|
|
} else if constraint.as_str().starts_with('+') {
|
2019-12-31 14:25:16 -06:00
|
|
|
struct_span_err!(
|
2020-07-29 20:27:50 -05:00
|
|
|
cx.sess.parse_sess.span_diagnostic,
|
2020-02-29 05:56:15 -06:00
|
|
|
p.prev_token.span,
|
2019-12-31 14:25:16 -06:00
|
|
|
E0663,
|
|
|
|
"input operand constraint contains '+'"
|
|
|
|
)
|
|
|
|
.emit();
|
2013-10-17 13:24:41 -05:00
|
|
|
}
|
|
|
|
|
2018-12-04 13:10:32 -06:00
|
|
|
p.expect(&token::OpenDelim(token::Paren))?;
|
|
|
|
let input = p.parse_expr()?;
|
|
|
|
p.expect(&token::CloseDelim(token::Paren))?;
|
2013-03-12 02:01:09 -05:00
|
|
|
|
2013-07-31 16:59:59 -05:00
|
|
|
inputs.push((constraint, input));
|
2013-03-12 02:01:09 -05:00
|
|
|
}
|
|
|
|
}
|
2014-05-28 11:24:28 -05:00
|
|
|
Clobbers => {
|
2016-06-06 09:52:48 -05:00
|
|
|
while p.token != token::Eof && p.token != token::Colon && p.token != token::ModSep {
|
2015-03-24 18:54:09 -05:00
|
|
|
if !clobs.is_empty() {
|
2015-12-30 17:11:53 -06:00
|
|
|
p.eat(&token::Comma);
|
2014-05-28 11:24:28 -05:00
|
|
|
}
|
|
|
|
|
2019-11-10 08:04:12 -06:00
|
|
|
let s = parse_asm_str(&mut p)?;
|
2014-05-28 11:24:28 -05:00
|
|
|
|
2014-11-20 19:25:27 -06:00
|
|
|
if OPTIONS.iter().any(|&opt| s == opt) {
|
2020-02-29 05:56:15 -06:00
|
|
|
cx.span_warn(p.prev_token.span, "expected a clobber, found an option");
|
2020-02-26 06:03:46 -06:00
|
|
|
} else if s.as_str().starts_with('{') || s.as_str().ends_with('}') {
|
2019-12-31 14:25:16 -06:00
|
|
|
struct_span_err!(
|
2020-07-29 20:27:50 -05:00
|
|
|
cx.sess.parse_sess.span_diagnostic,
|
2020-02-29 05:56:15 -06:00
|
|
|
p.prev_token.span,
|
2019-12-22 16:42:04 -06:00
|
|
|
E0664,
|
|
|
|
"clobber should not be surrounded by braces"
|
2019-12-31 14:25:16 -06:00
|
|
|
)
|
|
|
|
.emit();
|
2014-05-28 11:24:28 -05:00
|
|
|
}
|
2016-07-06 07:54:31 -05:00
|
|
|
|
2014-11-29 20:56:31 -06:00
|
|
|
clobs.push(s);
|
2014-05-28 11:24:28 -05:00
|
|
|
}
|
|
|
|
}
|
2013-03-12 02:01:09 -05:00
|
|
|
Options => {
|
2019-11-10 08:04:12 -06:00
|
|
|
let option = parse_asm_str(&mut p)?;
|
2013-03-12 02:09:53 -05:00
|
|
|
|
2019-05-07 01:03:44 -05:00
|
|
|
if option == sym::volatile {
|
2014-03-09 17:41:18 -05:00
|
|
|
// Indicates that the inline assembly has side effects
|
|
|
|
// and must not be optimized out along with its outputs.
|
2013-03-12 02:01:09 -05:00
|
|
|
volatile = true;
|
2019-05-07 01:03:44 -05:00
|
|
|
} else if option == sym::alignstack {
|
2013-03-12 03:02:58 -05:00
|
|
|
alignstack = true;
|
2019-05-07 01:03:44 -05:00
|
|
|
} else if option == sym::intel {
|
2020-01-14 07:40:42 -06:00
|
|
|
dialect = LlvmAsmDialect::Intel;
|
2014-03-09 17:41:18 -05:00
|
|
|
} else {
|
2020-02-29 05:56:15 -06:00
|
|
|
cx.span_warn(p.prev_token.span, "unrecognized option");
|
2013-03-12 02:01:09 -05:00
|
|
|
}
|
|
|
|
|
2014-10-27 03:22:52 -05:00
|
|
|
if p.token == token::Comma {
|
2015-12-30 17:11:53 -06:00
|
|
|
p.eat(&token::Comma);
|
2013-03-12 02:01:09 -05:00
|
|
|
}
|
|
|
|
}
|
2016-06-06 09:52:48 -05:00
|
|
|
StateNone => (),
|
2013-03-12 02:01:09 -05:00
|
|
|
}
|
|
|
|
|
2014-03-09 17:41:18 -05:00
|
|
|
loop {
|
|
|
|
// MOD_SEP is a double colon '::' without space in between.
|
|
|
|
// When encountered, the state must be advanced twice.
|
2019-06-04 17:17:07 -05:00
|
|
|
match (&p.token.kind, state.next(), state.next().next()) {
|
2019-12-22 16:42:04 -06:00
|
|
|
(&token::Colon, StateNone, _) | (&token::ModSep, _, StateNone) => {
|
2015-12-30 17:11:53 -06:00
|
|
|
p.bump();
|
2014-03-09 17:41:18 -05:00
|
|
|
break 'statement;
|
2013-03-12 02:01:09 -05:00
|
|
|
}
|
2019-12-22 16:42:04 -06:00
|
|
|
(&token::Colon, st, _) | (&token::ModSep, _, st) => {
|
2015-12-30 17:11:53 -06:00
|
|
|
p.bump();
|
2014-03-09 17:41:18 -05:00
|
|
|
state = st;
|
2013-03-12 02:01:09 -05:00
|
|
|
}
|
2016-08-26 11:23:42 -05:00
|
|
|
(&token::Eof, ..) => break 'statement,
|
2016-06-06 09:52:48 -05:00
|
|
|
_ => break,
|
2014-03-09 17:41:18 -05:00
|
|
|
}
|
2013-03-12 02:01:09 -05:00
|
|
|
}
|
2013-03-11 00:08:38 -05:00
|
|
|
}
|
|
|
|
|
2020-01-14 07:40:42 -06:00
|
|
|
Ok(Some(ast::LlvmInlineAsm {
|
2018-12-04 13:10:32 -06:00
|
|
|
asm,
|
|
|
|
asm_str_style: asm_str_style.unwrap(),
|
|
|
|
outputs,
|
|
|
|
inputs,
|
|
|
|
clobbers: clobs,
|
|
|
|
volatile,
|
|
|
|
alignstack,
|
|
|
|
dialect,
|
2014-09-13 11:06:01 -05:00
|
|
|
}))
|
2013-03-11 00:08:38 -05:00
|
|
|
}
|