2013-08-06 23:50:23 -05:00
|
|
|
// Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
|
2013-03-11 00:08:38 -05: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.
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Inline assembly support.
|
|
|
|
*/
|
|
|
|
|
|
|
|
use ast;
|
2013-08-31 11:13:04 -05:00
|
|
|
use codemap::Span;
|
2013-03-11 00:08:38 -05:00
|
|
|
use ext::base;
|
|
|
|
use ext::base::*;
|
2013-03-12 02:01:09 -05:00
|
|
|
use parse;
|
2014-01-10 16:02:36 -06:00
|
|
|
use parse::token::InternedString;
|
2013-03-12 02:01:09 -05:00
|
|
|
use parse::token;
|
|
|
|
|
2014-05-05 20:56:44 -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,
|
|
|
|
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 {
|
|
|
|
Asm => Outputs,
|
|
|
|
Outputs => Inputs,
|
|
|
|
Inputs => Clobbers,
|
|
|
|
Clobbers => Options,
|
|
|
|
Options => StateNone,
|
|
|
|
StateNone => StateNone
|
|
|
|
}
|
2013-03-12 02:01:09 -05:00
|
|
|
}
|
|
|
|
}
|
2013-03-11 00:08:38 -05:00
|
|
|
|
2014-03-09 17:41:18 -05:00
|
|
|
static OPTIONS: &'static [&'static str] = &["volatile", "alignstack", "intel"];
|
|
|
|
|
2014-01-09 07:05:33 -06:00
|
|
|
pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
2014-05-05 20:56:44 -05:00
|
|
|
-> Box<base::MacResult> {
|
2013-12-30 16:04:00 -06:00
|
|
|
let mut p = parse::new_parser_from_tts(cx.parse_sess(),
|
|
|
|
cx.cfg(),
|
2014-02-28 14:54:01 -06:00
|
|
|
tts.iter()
|
|
|
|
.map(|x| (*x).clone())
|
|
|
|
.collect());
|
2013-03-12 02:01:09 -05:00
|
|
|
|
2014-01-10 16:02:36 -06:00
|
|
|
let mut asm = InternedString::new("");
|
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-04-15 20:17:48 -05:00
|
|
|
let mut cons = "".to_owned();
|
2013-03-12 02:01:09 -05:00
|
|
|
let mut volatile = false;
|
2013-03-12 03:02:58 -05:00
|
|
|
let mut alignstack = false;
|
2014-01-09 07:05:33 -06:00
|
|
|
let mut dialect = ast::AsmAtt;
|
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
|
|
|
let mut read_write_operands = Vec::new();
|
|
|
|
|
|
|
|
'statement: loop {
|
2013-03-12 02:01:09 -05:00
|
|
|
match state {
|
|
|
|
Asm => {
|
2014-01-17 08:53:10 -06:00
|
|
|
let (s, style) = match expr_to_str(cx, p.parse_expr(),
|
|
|
|
"inline assembly must be a string literal.") {
|
|
|
|
Some((s, st)) => (s, st),
|
|
|
|
// let compilation continue
|
2014-04-15 07:00:14 -05:00
|
|
|
None => return DummyResult::expr(sp),
|
2014-01-17 08:53:10 -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 => {
|
2013-12-30 17:09:41 -06:00
|
|
|
while p.token != token::EOF &&
|
|
|
|
p.token != token::COLON &&
|
|
|
|
p.token != token::MOD_SEP {
|
2013-03-12 02:09:53 -05:00
|
|
|
|
2013-03-12 02:01:09 -05:00
|
|
|
if outputs.len() != 0 {
|
|
|
|
p.eat(&token::COMMA);
|
|
|
|
}
|
2013-03-12 02:09:53 -05:00
|
|
|
|
2013-10-07 19:49:10 -05:00
|
|
|
let (constraint, _str_style) = p.parse_str();
|
2013-10-17 13:24:41 -05:00
|
|
|
|
2014-03-09 17:41:18 -05:00
|
|
|
let span = p.last_span;
|
2013-10-17 13:24:41 -05:00
|
|
|
|
2013-03-12 02:01:09 -05:00
|
|
|
p.expect(&token::LPAREN);
|
|
|
|
let out = p.parse_expr();
|
|
|
|
p.expect(&token::RPAREN);
|
|
|
|
|
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.)
|
|
|
|
let output = match constraint.get().slice_shift_char() {
|
|
|
|
(Some('='), _) => None,
|
|
|
|
(Some('+'), operand) => {
|
|
|
|
// Save a reference to the output
|
|
|
|
read_write_operands.push((outputs.len(), out));
|
|
|
|
Some(token::intern_and_get_ident("=" + operand))
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
cx.span_err(span, "output operand constraint lacks '=' or '+'");
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
outputs.push((output.unwrap_or(constraint), out));
|
2013-03-12 02:01:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Inputs => {
|
2013-12-30 17:09:41 -06:00
|
|
|
while p.token != token::EOF &&
|
|
|
|
p.token != token::COLON &&
|
|
|
|
p.token != token::MOD_SEP {
|
2013-03-12 02:09:53 -05:00
|
|
|
|
2013-03-12 02:01:09 -05:00
|
|
|
if inputs.len() != 0 {
|
|
|
|
p.eat(&token::COMMA);
|
|
|
|
}
|
2013-03-12 02:09:53 -05:00
|
|
|
|
2013-10-07 19:49:10 -05:00
|
|
|
let (constraint, _str_style) = p.parse_str();
|
2013-10-17 13:24:41 -05:00
|
|
|
|
2014-01-15 20:30:40 -06:00
|
|
|
if constraint.get().starts_with("=") {
|
2013-12-30 17:30:14 -06:00
|
|
|
cx.span_err(p.last_span, "input operand constraint contains '='");
|
2014-01-15 20:30:40 -06:00
|
|
|
} else if constraint.get().starts_with("+") {
|
2013-12-30 17:30:14 -06:00
|
|
|
cx.span_err(p.last_span, "input operand constraint contains '+'");
|
2013-10-17 13:24:41 -05:00
|
|
|
}
|
|
|
|
|
2013-03-12 02:01:09 -05:00
|
|
|
p.expect(&token::LPAREN);
|
2013-07-31 16:59:59 -05:00
|
|
|
let input = p.parse_expr();
|
2013-03-12 02:01:09 -05:00
|
|
|
p.expect(&token::RPAREN);
|
|
|
|
|
2013-07-31 16:59:59 -05:00
|
|
|
inputs.push((constraint, input));
|
2013-03-12 02:01:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Clobbers => {
|
2014-02-28 15:09:09 -06:00
|
|
|
let mut clobs = Vec::new();
|
2013-12-30 17:09:41 -06:00
|
|
|
while p.token != token::EOF &&
|
|
|
|
p.token != token::COLON &&
|
|
|
|
p.token != token::MOD_SEP {
|
2013-03-12 02:09:53 -05:00
|
|
|
|
2013-03-12 02:01:09 -05:00
|
|
|
if clobs.len() != 0 {
|
|
|
|
p.eat(&token::COMMA);
|
|
|
|
}
|
2013-03-12 02:09:53 -05:00
|
|
|
|
2013-10-07 19:49:10 -05:00
|
|
|
let (s, _str_style) = p.parse_str();
|
|
|
|
let clob = format!("~\\{{}\\}", s);
|
2013-03-12 02:01:09 -05:00
|
|
|
clobs.push(clob);
|
2014-03-09 17:41:18 -05:00
|
|
|
|
|
|
|
if OPTIONS.iter().any(|opt| s.equiv(opt)) {
|
|
|
|
cx.span_warn(p.last_span, "expected a clobber, but found an option");
|
|
|
|
}
|
2013-03-12 02:01:09 -05:00
|
|
|
}
|
|
|
|
|
2013-06-10 08:25:25 -05:00
|
|
|
cons = clobs.connect(",");
|
2013-03-12 02:01:09 -05:00
|
|
|
}
|
|
|
|
Options => {
|
2013-10-07 19:49:10 -05:00
|
|
|
let (option, _str_style) = p.parse_str();
|
2013-03-12 02:09:53 -05:00
|
|
|
|
2014-01-15 20:30:40 -06:00
|
|
|
if option.equiv(&("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;
|
2014-01-15 20:30:40 -06:00
|
|
|
} else if option.equiv(&("alignstack")) {
|
2013-03-12 03:02:58 -05:00
|
|
|
alignstack = true;
|
2014-01-15 20:30:40 -06:00
|
|
|
} else if option.equiv(&("intel")) {
|
2014-01-09 07:05:33 -06:00
|
|
|
dialect = ast::AsmIntel;
|
2014-03-09 17:41:18 -05:00
|
|
|
} else {
|
|
|
|
cx.span_warn(p.last_span, "unrecognized option");
|
2013-03-12 02:01:09 -05:00
|
|
|
}
|
|
|
|
|
2013-12-30 17:09:41 -06:00
|
|
|
if p.token == token::COMMA {
|
2013-03-12 02:01:09 -05:00
|
|
|
p.eat(&token::COMMA);
|
|
|
|
}
|
|
|
|
}
|
2014-03-09 17:41:18 -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.
|
|
|
|
match (&p.token, state.next(), state.next().next()) {
|
|
|
|
(&token::COLON, StateNone, _) |
|
|
|
|
(&token::MOD_SEP, _, StateNone) => {
|
|
|
|
p.bump();
|
|
|
|
break 'statement;
|
2013-03-12 02:01:09 -05:00
|
|
|
}
|
2014-03-09 17:41:18 -05:00
|
|
|
(&token::COLON, st, _) |
|
|
|
|
(&token::MOD_SEP, _, st) => {
|
|
|
|
p.bump();
|
|
|
|
state = st;
|
2013-03-12 02:01:09 -05:00
|
|
|
}
|
2014-03-09 17:41:18 -05:00
|
|
|
(&token::EOF, _, _) => break 'statement,
|
|
|
|
_ => break
|
|
|
|
}
|
2013-03-12 02:01:09 -05:00
|
|
|
}
|
2013-03-11 00:08:38 -05:00
|
|
|
}
|
|
|
|
|
2014-03-09 17:41:18 -05:00
|
|
|
// Append an input operand, with the form of ("0", expr)
|
|
|
|
// that links to an output operand.
|
|
|
|
for &(i, out) in read_write_operands.iter() {
|
|
|
|
inputs.push((token::intern_and_get_ident(i.to_str()), out));
|
|
|
|
}
|
|
|
|
|
2014-04-15 07:00:14 -05:00
|
|
|
MacExpr::new(@ast::Expr {
|
2013-09-06 21:11:55 -05:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
2014-01-09 07:05:33 -06:00
|
|
|
node: ast::ExprInlineAsm(ast::InlineAsm {
|
2014-01-15 20:30:40 -06:00
|
|
|
asm: token::intern_and_get_ident(asm.get()),
|
2013-10-07 19:49:10 -05:00
|
|
|
asm_str_style: asm_str_style.unwrap(),
|
2014-01-15 20:30:40 -06:00
|
|
|
clobbers: token::intern_and_get_ident(cons),
|
2013-03-27 15:42:21 -05:00
|
|
|
inputs: inputs,
|
|
|
|
outputs: outputs,
|
|
|
|
volatile: volatile,
|
2013-03-27 16:42:19 -05:00
|
|
|
alignstack: alignstack,
|
|
|
|
dialect: dialect
|
2013-03-27 15:42:21 -05:00
|
|
|
}),
|
2013-03-11 00:08:38 -05:00
|
|
|
span: sp
|
|
|
|
})
|
|
|
|
}
|