2013-08-07 00:50:23 -04:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2013-03-10 22:08:38 -07:00
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2016-06-06 20:22:48 +05:30
|
|
|
// Inline assembly support.
|
|
|
|
//
|
2014-11-06 00:05:53 -08:00
|
|
|
use self::State::*;
|
2013-03-10 22:08:38 -07:00
|
|
|
|
2015-12-10 23:23:14 +09:00
|
|
|
use syntax::ast;
|
|
|
|
use syntax::ext::base;
|
|
|
|
use syntax::ext::base::*;
|
|
|
|
use syntax::feature_gate;
|
2015-02-01 09:59:46 +02:00
|
|
|
use syntax::parse::{self, token};
|
2015-12-10 23:23:14 +09:00
|
|
|
use syntax::ptr::P;
|
2016-11-16 10:52:37 +00:00
|
|
|
use syntax::symbol::Symbol;
|
2015-09-21 11:45:04 +02:00
|
|
|
use syntax::ast::AsmDialect;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::Span;
|
2016-06-20 08:49:33 -07:00
|
|
|
use syntax::tokenstream;
|
2014-06-11 19:33:52 -07:00
|
|
|
|
2013-03-12 00:01:09 -07:00
|
|
|
enum State {
|
|
|
|
Asm,
|
|
|
|
Outputs,
|
|
|
|
Inputs,
|
|
|
|
Clobbers,
|
2014-03-09 23:41:18 +01:00
|
|
|
Options,
|
2016-06-06 20:22:48 +05:30
|
|
|
StateNone,
|
2013-03-12 00:01:09 -07:00
|
|
|
}
|
|
|
|
|
2014-03-09 23:41:18 +01:00
|
|
|
impl State {
|
|
|
|
fn next(&self) -> State {
|
|
|
|
match *self {
|
2016-06-06 20:22:48 +05:30
|
|
|
Asm => Outputs,
|
|
|
|
Outputs => Inputs,
|
|
|
|
Inputs => Clobbers,
|
|
|
|
Clobbers => Options,
|
|
|
|
Options => StateNone,
|
|
|
|
StateNone => StateNone,
|
2014-03-09 23:41:18 +01:00
|
|
|
}
|
2013-03-12 00:01:09 -07:00
|
|
|
}
|
|
|
|
}
|
2013-03-10 22:08:38 -07:00
|
|
|
|
2018-02-12 23:21:20 +01:00
|
|
|
macro_rules! span_err_if_not_stage0 {
|
|
|
|
($cx:expr, $sp:expr, $code:ident, $text:tt) => {
|
|
|
|
#[cfg(not(stage0))] {
|
|
|
|
span_err!($cx, $sp, $code, $text)
|
|
|
|
}
|
|
|
|
#[cfg(stage0)] {
|
|
|
|
$cx.span_err($sp, $text)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-27 15:36:53 +01:00
|
|
|
const OPTIONS: &'static [&'static str] = &["volatile", "alignstack", "intel"];
|
2014-03-09 23:41:18 +01:00
|
|
|
|
2016-06-06 20:22:48 +05:30
|
|
|
pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt,
|
|
|
|
sp: Span,
|
|
|
|
tts: &[tokenstream::TokenTree])
|
|
|
|
-> Box<base::MacResult + 'cx> {
|
2015-02-15 22:14:03 +01:00
|
|
|
if !cx.ecfg.enable_asm() {
|
2016-09-24 18:42:54 +02:00
|
|
|
feature_gate::emit_feature_err(&cx.parse_sess,
|
2016-06-06 20:22:48 +05:30
|
|
|
"asm",
|
|
|
|
sp,
|
|
|
|
feature_gate::GateIssue::Language,
|
|
|
|
feature_gate::EXPLAIN_ASM);
|
2015-02-15 22:14:03 +01:00
|
|
|
return DummyResult::expr(sp);
|
|
|
|
}
|
|
|
|
|
2015-02-01 09:59:46 +02: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 20:22:48 +05:30
|
|
|
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 09:59:46 +02: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 02:49:10 +02:00
|
|
|
let mut asm_str_style = None;
|
2014-02-28 13:09:09 -08:00
|
|
|
let mut outputs = Vec::new();
|
|
|
|
let mut inputs = Vec::new();
|
2014-11-30 11:56:31 +09:00
|
|
|
let mut clobs = Vec::new();
|
2013-03-12 00:01:09 -07:00
|
|
|
let mut volatile = false;
|
2013-03-12 01:02:58 -07:00
|
|
|
let mut alignstack = false;
|
2015-09-21 11:45:04 +02:00
|
|
|
let mut dialect = AsmDialect::Att;
|
2013-03-12 00:01:09 -07:00
|
|
|
|
|
|
|
let mut state = Asm;
|
2013-04-26 16:19:26 -07:00
|
|
|
|
2014-03-09 23:41:18 +01:00
|
|
|
'statement: loop {
|
2013-03-12 00:01:09 -07:00
|
|
|
match state {
|
|
|
|
Asm => {
|
2015-01-14 17:02:20 +02: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-02-12 23:21:20 +01:00
|
|
|
span_err_if_not_stage0!(cx, sp, E0660, "malformed inline assembly");
|
2015-01-14 17:02:20 +02:00
|
|
|
return DummyResult::expr(sp);
|
|
|
|
}
|
2015-02-01 09:59:46 +02:00
|
|
|
// Nested parser, stop before the first colon (see above).
|
|
|
|
let mut p2 = cx.new_parser_from_tts(&tts[..first_colon]);
|
2016-06-06 20:22:48 +05:30
|
|
|
let (s, style) = match expr_to_string(cx,
|
|
|
|
panictry!(p2.parse_expr()),
|
|
|
|
"inline assembly must be a string literal") {
|
2014-01-18 01:53:10 +11:00
|
|
|
Some((s, st)) => (s, st),
|
|
|
|
// let compilation continue
|
2014-04-15 22:00:14 +10:00
|
|
|
None => return DummyResult::expr(sp),
|
2014-01-18 01:53:10 +11:00
|
|
|
};
|
2015-02-01 09:59:46 +02:00
|
|
|
|
|
|
|
// This is most likely malformed.
|
|
|
|
if p2.token != token::Eof {
|
2015-12-03 05:37:48 +03:00
|
|
|
let mut extra_tts = panictry!(p2.parse_all_token_trees());
|
2015-02-01 09:59:46 +02: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 09:59:46 +02:00
|
|
|
}
|
|
|
|
|
2013-10-08 02:49:10 +02:00
|
|
|
asm = s;
|
|
|
|
asm_str_style = Some(style);
|
2013-03-12 00:01:09 -07:00
|
|
|
}
|
|
|
|
Outputs => {
|
2016-06-06 20:22:48 +05:30
|
|
|
while p.token != token::Eof && p.token != token::Colon && p.token != token::ModSep {
|
2013-03-12 00:09:53 -07:00
|
|
|
|
2015-03-24 16:54:09 -07:00
|
|
|
if !outputs.is_empty() {
|
2015-12-31 12:11:53 +13:00
|
|
|
p.eat(&token::Comma);
|
2013-03-12 00:01:09 -07:00
|
|
|
}
|
2013-03-12 00:09:53 -07:00
|
|
|
|
2015-03-28 21:58:51 +00:00
|
|
|
let (constraint, _str_style) = panictry!(p.parse_str());
|
2013-10-17 21:24:41 +03:00
|
|
|
|
2016-09-21 12:09:22 +10:00
|
|
|
let span = p.prev_span;
|
2013-10-17 21:24:41 +03:00
|
|
|
|
2015-03-28 21:58:51 +00:00
|
|
|
panictry!(p.expect(&token::OpenDelim(token::Paren)));
|
2015-11-10 16:08:26 -08:00
|
|
|
let out = panictry!(p.parse_expr());
|
2015-03-28 21:58:51 +00:00
|
|
|
panictry!(p.expect(&token::CloseDelim(token::Paren)));
|
2013-03-12 00:01:09 -07:00
|
|
|
|
2014-03-09 23:41:18 +01: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 10:42:53 -07: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 23:41:18 +01:00
|
|
|
}
|
|
|
|
_ => {
|
2018-02-12 23:21:20 +01:00
|
|
|
span_err_if_not_stage0!(cx, span, E0661,
|
|
|
|
"output operand constraint lacks '=' or '+'");
|
2014-03-09 23:41:18 +01:00
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-19 20:39:26 +01: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),
|
2015-12-05 08:18:24 +00:00
|
|
|
expr: out,
|
2017-08-06 22:54:09 -07:00
|
|
|
is_rw,
|
|
|
|
is_indirect,
|
2015-12-05 08:18:24 +00:00
|
|
|
});
|
2013-03-12 00:01:09 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Inputs => {
|
2016-06-06 20:22:48 +05:30
|
|
|
while p.token != token::Eof && p.token != token::Colon && p.token != token::ModSep {
|
2013-03-12 00:09:53 -07:00
|
|
|
|
2015-03-24 16:54:09 -07:00
|
|
|
if !inputs.is_empty() {
|
2015-12-31 12:11:53 +13:00
|
|
|
p.eat(&token::Comma);
|
2013-03-12 00:01:09 -07:00
|
|
|
}
|
2013-03-12 00:09:53 -07:00
|
|
|
|
2015-03-28 21:58:51 +00:00
|
|
|
let (constraint, _str_style) = panictry!(p.parse_str());
|
2013-10-17 21:24:41 +03:00
|
|
|
|
2016-11-16 10:52:37 +00:00
|
|
|
if constraint.as_str().starts_with("=") {
|
2018-05-14 20:17:21 +02:00
|
|
|
span_err_if_not_stage0!(cx, p.prev_span, E0662,
|
|
|
|
"input operand constraint contains '='");
|
2016-11-16 10:52:37 +00:00
|
|
|
} else if constraint.as_str().starts_with("+") {
|
2018-05-14 20:17:21 +02:00
|
|
|
span_err_if_not_stage0!(cx, p.prev_span, E0663,
|
|
|
|
"input operand constraint contains '+'");
|
2013-10-17 21:24:41 +03:00
|
|
|
}
|
|
|
|
|
2015-03-28 21:58:51 +00:00
|
|
|
panictry!(p.expect(&token::OpenDelim(token::Paren)));
|
2015-11-10 16:08:26 -08:00
|
|
|
let input = panictry!(p.parse_expr());
|
2015-03-28 21:58:51 +00:00
|
|
|
panictry!(p.expect(&token::CloseDelim(token::Paren)));
|
2013-03-12 00:01:09 -07:00
|
|
|
|
2013-07-31 17:59:59 -04:00
|
|
|
inputs.push((constraint, input));
|
2013-03-12 00:01:09 -07:00
|
|
|
}
|
|
|
|
}
|
2014-05-28 09:24:28 -07:00
|
|
|
Clobbers => {
|
2016-06-06 20:22:48 +05:30
|
|
|
while p.token != token::Eof && p.token != token::Colon && p.token != token::ModSep {
|
2014-05-28 09:24:28 -07:00
|
|
|
|
2015-03-24 16:54:09 -07:00
|
|
|
if !clobs.is_empty() {
|
2015-12-31 12:11:53 +13:00
|
|
|
p.eat(&token::Comma);
|
2014-05-28 09:24:28 -07:00
|
|
|
}
|
|
|
|
|
2015-03-28 21:58:51 +00:00
|
|
|
let (s, _str_style) = panictry!(p.parse_str());
|
2014-05-28 09:24:28 -07:00
|
|
|
|
2014-11-20 20:25:27 -05:00
|
|
|
if OPTIONS.iter().any(|&opt| s == opt) {
|
2016-09-21 12:09:22 +10: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-14 20:17:21 +02:00
|
|
|
span_err_if_not_stage0!(cx, p.prev_span, E0664,
|
|
|
|
"clobber should not be surrounded by braces");
|
2014-05-28 09:24:28 -07:00
|
|
|
}
|
2016-07-06 14:54:31 +02:00
|
|
|
|
2014-11-30 11:56:31 +09:00
|
|
|
clobs.push(s);
|
2014-05-28 09:24:28 -07:00
|
|
|
}
|
|
|
|
}
|
2013-03-12 00:01:09 -07:00
|
|
|
Options => {
|
2015-03-28 21:58:51 +00:00
|
|
|
let (option, _str_style) = panictry!(p.parse_str());
|
2013-03-12 00:09:53 -07:00
|
|
|
|
2014-11-20 20:25:27 -05:00
|
|
|
if option == "volatile" {
|
2014-03-09 23:41:18 +01:00
|
|
|
// Indicates that the inline assembly has side effects
|
|
|
|
// and must not be optimized out along with its outputs.
|
2013-03-12 00:01:09 -07:00
|
|
|
volatile = true;
|
2014-11-20 20:25:27 -05:00
|
|
|
} else if option == "alignstack" {
|
2013-03-12 01:02:58 -07:00
|
|
|
alignstack = true;
|
2014-11-20 20:25:27 -05:00
|
|
|
} else if option == "intel" {
|
2015-09-21 11:45:04 +02:00
|
|
|
dialect = AsmDialect::Intel;
|
2014-03-09 23:41:18 +01:00
|
|
|
} else {
|
2016-09-21 12:09:22 +10:00
|
|
|
cx.span_warn(p.prev_span, "unrecognized option");
|
2013-03-12 00:01:09 -07:00
|
|
|
}
|
|
|
|
|
2014-10-27 19:22:52 +11:00
|
|
|
if p.token == token::Comma {
|
2015-12-31 12:11:53 +13:00
|
|
|
p.eat(&token::Comma);
|
2013-03-12 00:01:09 -07:00
|
|
|
}
|
|
|
|
}
|
2016-06-06 20:22:48 +05:30
|
|
|
StateNone => (),
|
2013-03-12 00:01:09 -07:00
|
|
|
}
|
|
|
|
|
2014-03-09 23:41:18 +01: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 20:22:48 +05:30
|
|
|
(&token::Colon, StateNone, _) |
|
2014-10-27 19:22:52 +11:00
|
|
|
(&token::ModSep, _, StateNone) => {
|
2015-12-31 12:11:53 +13:00
|
|
|
p.bump();
|
2014-03-09 23:41:18 +01:00
|
|
|
break 'statement;
|
2013-03-12 00:01:09 -07:00
|
|
|
}
|
2016-06-06 20:22:48 +05:30
|
|
|
(&token::Colon, st, _) |
|
2014-10-27 19:22:52 +11:00
|
|
|
(&token::ModSep, _, st) => {
|
2015-12-31 12:11:53 +13:00
|
|
|
p.bump();
|
2014-03-09 23:41:18 +01:00
|
|
|
state = st;
|
2013-03-12 00:01:09 -07:00
|
|
|
}
|
2016-08-26 19:23:42 +03:00
|
|
|
(&token::Eof, ..) => break 'statement,
|
2016-06-06 20:22:48 +05:30
|
|
|
_ => break,
|
2014-03-09 23:41:18 +01:00
|
|
|
}
|
2013-03-12 00:01:09 -07:00
|
|
|
}
|
2013-03-10 22:08:38 -07:00
|
|
|
}
|
|
|
|
|
2017-11-16 10:08:19 +01:00
|
|
|
// If there are no outputs, the inline assembly is executed just for its side effects,
|
|
|
|
// so ensure that it is volatile
|
|
|
|
if outputs.is_empty() {
|
|
|
|
volatile = true;
|
|
|
|
}
|
|
|
|
|
2015-02-27 11:14:42 -08:00
|
|
|
MacEager::expr(P(ast::Expr {
|
2013-09-06 22:11:55 -04:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2016-11-03 08:38:17 +00:00
|
|
|
node: ast::ExprKind::InlineAsm(P(ast::InlineAsm {
|
2017-08-06 22:54:09 -07:00
|
|
|
asm,
|
2013-10-08 02:49:10 +02:00
|
|
|
asm_str_style: asm_str_style.unwrap(),
|
2017-08-06 22:54:09 -07:00
|
|
|
outputs,
|
|
|
|
inputs,
|
2014-11-30 11:56:31 +09:00
|
|
|
clobbers: clobs,
|
2017-08-06 22:54:09 -07:00
|
|
|
volatile,
|
|
|
|
alignstack,
|
|
|
|
dialect,
|
2017-03-17 04:04:41 +00:00
|
|
|
ctxt: cx.backtrace(),
|
2016-11-03 08:38:17 +00:00
|
|
|
})),
|
2015-11-03 17:39:51 +01:00
|
|
|
span: sp,
|
2016-06-18 04:01:57 +00:00
|
|
|
attrs: ast::ThinVec::new(),
|
2014-09-13 19:06:01 +03:00
|
|
|
}))
|
2013-03-10 22:08:38 -07:00
|
|
|
}
|