2016-06-06 14:52:48 +00:00
|
|
|
// Inline assembly support.
|
|
|
|
//
|
2019-02-04 12:49:54 +00:00
|
|
|
use State::*;
|
2013-03-11 05:08:38 +00:00
|
|
|
|
2018-08-05 10:04:56 +00:00
|
|
|
use rustc_data_structures::thin_vec::ThinVec;
|
|
|
|
|
2019-02-07 15:56:05 +00:00
|
|
|
use errors::DiagnosticBuilder;
|
2019-02-04 12:49:54 +00:00
|
|
|
|
2015-12-10 14:23:14 +00:00
|
|
|
use syntax::ast;
|
2019-02-04 12:49:54 +00:00
|
|
|
use syntax::ext::base::{self, *};
|
2015-12-10 14:23:14 +00:00
|
|
|
use syntax::feature_gate;
|
2015-02-01 07:59:46 +00:00
|
|
|
use syntax::parse::{self, token};
|
2015-12-10 14:23:14 +00:00
|
|
|
use syntax::ptr::P;
|
2019-05-08 03:21:18 +00:00
|
|
|
use syntax::symbol::{Symbol, sym};
|
2015-09-21 09:45:04 +00:00
|
|
|
use syntax::ast::AsmDialect;
|
2016-06-21 22:08:13 +00:00
|
|
|
use syntax_pos::Span;
|
2016-06-20 15:49:33 +00:00
|
|
|
use syntax::tokenstream;
|
2019-02-04 12:49:54 +00:00
|
|
|
use syntax::{span_err, struct_span_err};
|
2014-06-12 02:33:52 +00:00
|
|
|
|
2013-03-12 07:01:09 +00:00
|
|
|
enum State {
|
|
|
|
Asm,
|
|
|
|
Outputs,
|
|
|
|
Inputs,
|
|
|
|
Clobbers,
|
2014-03-09 22:41:18 +00:00
|
|
|
Options,
|
2016-06-06 14:52:48 +00:00
|
|
|
StateNone,
|
2013-03-12 07:01:09 +00:00
|
|
|
}
|
|
|
|
|
2014-03-09 22:41:18 +00:00
|
|
|
impl State {
|
|
|
|
fn next(&self) -> State {
|
|
|
|
match *self {
|
2016-06-06 14:52:48 +00:00
|
|
|
Asm => Outputs,
|
|
|
|
Outputs => Inputs,
|
|
|
|
Inputs => Clobbers,
|
|
|
|
Clobbers => Options,
|
|
|
|
Options => StateNone,
|
|
|
|
StateNone => StateNone,
|
2014-03-09 22:41:18 +00:00
|
|
|
}
|
2013-03-12 07:01:09 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-11 05:08:38 +00:00
|
|
|
|
2019-05-07 06:03:44 +00:00
|
|
|
const OPTIONS: &[Symbol] = &[sym::volatile, sym::alignstack, sym::intel];
|
2014-03-09 22:41:18 +00:00
|
|
|
|
2019-02-04 12:49:54 +00:00
|
|
|
pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt<'_>,
|
2016-06-06 14:52:48 +00:00
|
|
|
sp: Span,
|
|
|
|
tts: &[tokenstream::TokenTree])
|
2018-07-12 09:58:16 +00:00
|
|
|
-> Box<dyn base::MacResult + 'cx> {
|
2015-02-15 21:14:03 +00:00
|
|
|
if !cx.ecfg.enable_asm() {
|
2016-09-24 16:42:54 +00:00
|
|
|
feature_gate::emit_feature_err(&cx.parse_sess,
|
2019-05-08 03:21:18 +00:00
|
|
|
sym::asm,
|
2016-06-06 14:52:48 +00:00
|
|
|
sp,
|
|
|
|
feature_gate::GateIssue::Language,
|
|
|
|
feature_gate::EXPLAIN_ASM);
|
2015-02-15 21:14:03 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:10:32 +00:00
|
|
|
let mut inline_asm = match parse_inline_asm(cx, sp, tts) {
|
|
|
|
Ok(Some(inline_asm)) => inline_asm,
|
|
|
|
Ok(None) => return DummyResult::expr(sp),
|
|
|
|
Err(mut err) => {
|
|
|
|
err.emit();
|
|
|
|
return DummyResult::expr(sp);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
node: ast::ExprKind::InlineAsm(P(inline_asm)),
|
|
|
|
span: sp,
|
|
|
|
attrs: ThinVec::new(),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_inline_asm<'a>(
|
|
|
|
cx: &mut ExtCtxt<'a>,
|
|
|
|
sp: Span,
|
|
|
|
tts: &[tokenstream::TokenTree],
|
|
|
|
) -> Result<Option<ast::InlineAsm>, DiagnosticBuilder<'a>> {
|
2015-02-01 07:59:46 +00:00
|
|
|
// Split the tts before the first colon, to avoid `asm!("x": y)` being
|
|
|
|
// parsed as `asm!(z)` with `z = "x": y` which is type ascription.
|
2016-06-06 14:52:48 +00:00
|
|
|
let first_colon = tts.iter()
|
|
|
|
.position(|tt| {
|
|
|
|
match *tt {
|
|
|
|
tokenstream::TokenTree::Token(_, token::Colon) |
|
|
|
|
tokenstream::TokenTree::Token(_, token::ModSep) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unwrap_or(tts.len());
|
2015-02-01 07:59:46 +00:00
|
|
|
let mut p = cx.new_parser_from_tts(&tts[first_colon..]);
|
2016-11-16 10:52:37 +00:00
|
|
|
let mut asm = Symbol::intern("");
|
2013-10-08 00:49:10 +00:00
|
|
|
let mut asm_str_style = None;
|
2014-02-28 21:09:09 +00:00
|
|
|
let mut outputs = Vec::new();
|
|
|
|
let mut inputs = Vec::new();
|
2014-11-30 02:56:31 +00:00
|
|
|
let mut clobs = Vec::new();
|
2013-03-12 07:01:09 +00:00
|
|
|
let mut volatile = false;
|
2013-03-12 08:02:58 +00:00
|
|
|
let mut alignstack = false;
|
2015-09-21 09:45:04 +00:00
|
|
|
let mut dialect = AsmDialect::Att;
|
2013-03-12 07:01:09 +00:00
|
|
|
|
|
|
|
let mut state = Asm;
|
2013-04-26 23:19:26 +00:00
|
|
|
|
2014-03-09 22:41:18 +00:00
|
|
|
'statement: loop {
|
2013-03-12 07:01:09 +00:00
|
|
|
match state {
|
|
|
|
Asm => {
|
2015-01-14 15:02:20 +00: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 19:10:32 +00:00
|
|
|
return Err(struct_span_err!(
|
|
|
|
cx.parse_sess.span_diagnostic,
|
|
|
|
sp,
|
|
|
|
E0660,
|
|
|
|
"malformed inline assembly"
|
|
|
|
));
|
2015-01-14 15:02:20 +00:00
|
|
|
}
|
2015-02-01 07:59:46 +00:00
|
|
|
// Nested parser, stop before the first colon (see above).
|
|
|
|
let mut p2 = cx.new_parser_from_tts(&tts[..first_colon]);
|
2018-12-04 19:10:32 +00: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 07:59:46 +00:00
|
|
|
|
|
|
|
// This is most likely malformed.
|
|
|
|
if p2.token != token::Eof {
|
2018-12-04 19:10:32 +00:00
|
|
|
let mut extra_tts = p2.parse_all_token_trees()?;
|
2015-02-01 07:59:46 +00:00
|
|
|
extra_tts.extend(tts[first_colon..].iter().cloned());
|
2017-02-21 05:05:59 +00:00
|
|
|
p = parse::stream_to_parser(cx.parse_sess, extra_tts.into_iter().collect());
|
2015-02-01 07:59:46 +00:00
|
|
|
}
|
|
|
|
|
2013-10-08 00:49:10 +00:00
|
|
|
asm = s;
|
|
|
|
asm_str_style = Some(style);
|
2013-03-12 07:01:09 +00:00
|
|
|
}
|
|
|
|
Outputs => {
|
2016-06-06 14:52:48 +00:00
|
|
|
while p.token != token::Eof && p.token != token::Colon && p.token != token::ModSep {
|
2015-03-24 23:54:09 +00:00
|
|
|
if !outputs.is_empty() {
|
2015-12-30 23:11:53 +00:00
|
|
|
p.eat(&token::Comma);
|
2013-03-12 07:01:09 +00:00
|
|
|
}
|
2013-03-12 07:09:53 +00:00
|
|
|
|
2018-12-04 19:10:32 +00:00
|
|
|
let (constraint, _) = p.parse_str()?;
|
2013-10-17 18:24:41 +00:00
|
|
|
|
2016-09-21 02:09:22 +00:00
|
|
|
let span = p.prev_span;
|
2013-10-17 18:24:41 +00:00
|
|
|
|
2018-12-04 19:10:32 +00:00
|
|
|
p.expect(&token::OpenDelim(token::Paren))?;
|
|
|
|
let expr = p.parse_expr()?;
|
|
|
|
p.expect(&token::CloseDelim(token::Paren))?;
|
2013-03-12 07:01:09 +00:00
|
|
|
|
2014-03-09 22:41:18 +00: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 10:52:37 +00:00
|
|
|
let constraint_str = constraint.as_str();
|
|
|
|
let mut ch = constraint_str.chars();
|
2016-04-07 17:42:53 +00:00
|
|
|
let output = match ch.next() {
|
|
|
|
Some('=') => None,
|
|
|
|
Some('+') => {
|
2016-11-16 10:52:37 +00:00
|
|
|
Some(Symbol::intern(&format!("={}", ch.as_str())))
|
2014-03-09 22:41:18 +00:00
|
|
|
}
|
|
|
|
_ => {
|
2018-05-10 18:02:19 +00:00
|
|
|
span_err!(cx, span, E0661,
|
2018-02-12 22:21:20 +00:00
|
|
|
"output operand constraint lacks '=' or '+'");
|
2014-03-09 22:41:18 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-19 19:39:26 +00:00
|
|
|
let is_rw = output.is_some();
|
2016-11-16 10:52:37 +00:00
|
|
|
let is_indirect = constraint_str.contains("*");
|
2015-12-05 08:18:24 +00:00
|
|
|
outputs.push(ast::InlineAsmOutput {
|
2016-11-16 10:52:37 +00:00
|
|
|
constraint: output.unwrap_or(constraint),
|
2018-12-04 19:10:32 +00:00
|
|
|
expr,
|
2017-08-07 05:54:09 +00:00
|
|
|
is_rw,
|
|
|
|
is_indirect,
|
2015-12-05 08:18:24 +00:00
|
|
|
});
|
2013-03-12 07:01:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Inputs => {
|
2016-06-06 14:52:48 +00:00
|
|
|
while p.token != token::Eof && p.token != token::Colon && p.token != token::ModSep {
|
2015-03-24 23:54:09 +00:00
|
|
|
if !inputs.is_empty() {
|
2015-12-30 23:11:53 +00:00
|
|
|
p.eat(&token::Comma);
|
2013-03-12 07:01:09 +00:00
|
|
|
}
|
2013-03-12 07:09:53 +00:00
|
|
|
|
2018-12-04 19:10:32 +00:00
|
|
|
let (constraint, _) = p.parse_str()?;
|
2013-10-17 18:24:41 +00:00
|
|
|
|
2016-11-16 10:52:37 +00:00
|
|
|
if constraint.as_str().starts_with("=") {
|
2018-05-10 18:02:19 +00:00
|
|
|
span_err!(cx, p.prev_span, E0662,
|
2018-05-14 18:17:21 +00:00
|
|
|
"input operand constraint contains '='");
|
2016-11-16 10:52:37 +00:00
|
|
|
} else if constraint.as_str().starts_with("+") {
|
2018-05-10 18:02:19 +00:00
|
|
|
span_err!(cx, p.prev_span, E0663,
|
2018-05-14 18:17:21 +00:00
|
|
|
"input operand constraint contains '+'");
|
2013-10-17 18:24:41 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:10:32 +00:00
|
|
|
p.expect(&token::OpenDelim(token::Paren))?;
|
|
|
|
let input = p.parse_expr()?;
|
|
|
|
p.expect(&token::CloseDelim(token::Paren))?;
|
2013-03-12 07:01:09 +00:00
|
|
|
|
2013-07-31 21:59:59 +00:00
|
|
|
inputs.push((constraint, input));
|
2013-03-12 07:01:09 +00:00
|
|
|
}
|
|
|
|
}
|
2014-05-28 16:24:28 +00:00
|
|
|
Clobbers => {
|
2016-06-06 14:52:48 +00:00
|
|
|
while p.token != token::Eof && p.token != token::Colon && p.token != token::ModSep {
|
2015-03-24 23:54:09 +00:00
|
|
|
if !clobs.is_empty() {
|
2015-12-30 23:11:53 +00:00
|
|
|
p.eat(&token::Comma);
|
2014-05-28 16:24:28 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:10:32 +00:00
|
|
|
let (s, _) = p.parse_str()?;
|
2014-05-28 16:24:28 +00:00
|
|
|
|
2014-11-21 01:25:27 +00:00
|
|
|
if OPTIONS.iter().any(|&opt| s == opt) {
|
2016-09-21 02:09:22 +00:00
|
|
|
cx.span_warn(p.prev_span, "expected a clobber, found an option");
|
2016-11-16 10:52:37 +00:00
|
|
|
} else if s.as_str().starts_with("{") || s.as_str().ends_with("}") {
|
2018-05-10 18:02:19 +00:00
|
|
|
span_err!(cx, p.prev_span, E0664,
|
2018-05-14 18:17:21 +00:00
|
|
|
"clobber should not be surrounded by braces");
|
2014-05-28 16:24:28 +00:00
|
|
|
}
|
2016-07-06 12:54:31 +00:00
|
|
|
|
2014-11-30 02:56:31 +00:00
|
|
|
clobs.push(s);
|
2014-05-28 16:24:28 +00:00
|
|
|
}
|
|
|
|
}
|
2013-03-12 07:01:09 +00:00
|
|
|
Options => {
|
2018-12-04 19:10:32 +00:00
|
|
|
let (option, _) = p.parse_str()?;
|
2013-03-12 07:09:53 +00:00
|
|
|
|
2019-05-07 06:03:44 +00:00
|
|
|
if option == sym::volatile {
|
2014-03-09 22:41:18 +00:00
|
|
|
// Indicates that the inline assembly has side effects
|
|
|
|
// and must not be optimized out along with its outputs.
|
2013-03-12 07:01:09 +00:00
|
|
|
volatile = true;
|
2019-05-07 06:03:44 +00:00
|
|
|
} else if option == sym::alignstack {
|
2013-03-12 08:02:58 +00:00
|
|
|
alignstack = true;
|
2019-05-07 06:03:44 +00:00
|
|
|
} else if option == sym::intel {
|
2015-09-21 09:45:04 +00:00
|
|
|
dialect = AsmDialect::Intel;
|
2014-03-09 22:41:18 +00:00
|
|
|
} else {
|
2016-09-21 02:09:22 +00:00
|
|
|
cx.span_warn(p.prev_span, "unrecognized option");
|
2013-03-12 07:01:09 +00:00
|
|
|
}
|
|
|
|
|
2014-10-27 08:22:52 +00:00
|
|
|
if p.token == token::Comma {
|
2015-12-30 23:11:53 +00:00
|
|
|
p.eat(&token::Comma);
|
2013-03-12 07:01:09 +00:00
|
|
|
}
|
|
|
|
}
|
2016-06-06 14:52:48 +00:00
|
|
|
StateNone => (),
|
2013-03-12 07:01:09 +00:00
|
|
|
}
|
|
|
|
|
2014-03-09 22:41:18 +00:00
|
|
|
loop {
|
|
|
|
// MOD_SEP is a double colon '::' without space in between.
|
|
|
|
// When encountered, the state must be advanced twice.
|
|
|
|
match (&p.token, state.next(), state.next().next()) {
|
2016-06-06 14:52:48 +00:00
|
|
|
(&token::Colon, StateNone, _) |
|
2014-10-27 08:22:52 +00:00
|
|
|
(&token::ModSep, _, StateNone) => {
|
2015-12-30 23:11:53 +00:00
|
|
|
p.bump();
|
2014-03-09 22:41:18 +00:00
|
|
|
break 'statement;
|
2013-03-12 07:01:09 +00:00
|
|
|
}
|
2016-06-06 14:52:48 +00:00
|
|
|
(&token::Colon, st, _) |
|
2014-10-27 08:22:52 +00:00
|
|
|
(&token::ModSep, _, st) => {
|
2015-12-30 23:11:53 +00:00
|
|
|
p.bump();
|
2014-03-09 22:41:18 +00:00
|
|
|
state = st;
|
2013-03-12 07:01:09 +00:00
|
|
|
}
|
2016-08-26 16:23:42 +00:00
|
|
|
(&token::Eof, ..) => break 'statement,
|
2016-06-06 14:52:48 +00:00
|
|
|
_ => break,
|
2014-03-09 22:41:18 +00:00
|
|
|
}
|
2013-03-12 07:01:09 +00:00
|
|
|
}
|
2013-03-11 05:08:38 +00:00
|
|
|
}
|
|
|
|
|
2018-12-04 19:10:32 +00:00
|
|
|
Ok(Some(ast::InlineAsm {
|
|
|
|
asm,
|
|
|
|
asm_str_style: asm_str_style.unwrap(),
|
|
|
|
outputs,
|
|
|
|
inputs,
|
|
|
|
clobbers: clobs,
|
|
|
|
volatile,
|
|
|
|
alignstack,
|
|
|
|
dialect,
|
|
|
|
ctxt: cx.backtrace(),
|
2014-09-13 16:06:01 +00:00
|
|
|
}))
|
2013-03-11 05:08:38 +00:00
|
|
|
}
|