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::*;
|
2014-01-10 16:02:36 -06:00
|
|
|
use parse::token::InternedString;
|
2013-03-12 02:01:09 -05:00
|
|
|
use parse::token;
|
2014-09-13 11:06:01 -05:00
|
|
|
use ptr::P;
|
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,
|
|
|
|
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-08-27 20:46:52 -05:00
|
|
|
pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
|
|
|
|
-> Box<base::MacResult+'cx> {
|
2014-07-03 04:42:24 -05:00
|
|
|
let mut p = cx.new_parser_from_tts(tts);
|
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-05-25 05:10:11 -05:00
|
|
|
let mut cons = "".to_string();
|
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
|
|
|
'statement: loop {
|
2013-03-12 02:01:09 -05:00
|
|
|
match state {
|
|
|
|
Asm => {
|
2014-06-21 05:39:03 -05:00
|
|
|
let (s, style) = match expr_to_string(cx, p.parse_expr(),
|
2014-01-17 08:53:10 -06:00
|
|
|
"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) => {
|
2014-05-20 01:19:56 -05:00
|
|
|
Some(token::intern_and_get_ident(format!(
|
|
|
|
"={}",
|
|
|
|
operand).as_slice()))
|
2014-03-09 17:41:18 -05:00
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
cx.span_err(span, "output operand constraint lacks '=' or '+'");
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-19 14:39:26 -05:00
|
|
|
let is_rw = output.is_some();
|
|
|
|
outputs.push((output.unwrap_or(constraint), out, is_rw));
|
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
|
|
|
}
|
|
|
|
}
|
2014-05-28 11:24:28 -05:00
|
|
|
Clobbers => {
|
|
|
|
let mut clobs = Vec::new();
|
|
|
|
while p.token != token::EOF &&
|
|
|
|
p.token != token::COLON &&
|
|
|
|
p.token != token::MOD_SEP {
|
|
|
|
|
|
|
|
if clobs.len() != 0 {
|
|
|
|
p.eat(&token::COMMA);
|
|
|
|
}
|
|
|
|
|
|
|
|
let (s, _str_style) = p.parse_str();
|
|
|
|
let clob = format!("~{{{}}}", s);
|
|
|
|
clobs.push(clob);
|
|
|
|
|
|
|
|
if OPTIONS.iter().any(|opt| s.equiv(opt)) {
|
2014-08-23 05:41:32 -05:00
|
|
|
cx.span_warn(p.last_span, "expected a clobber, found an option");
|
2014-05-28 11:24:28 -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-09-13 11:06:01 -05:00
|
|
|
MacExpr::new(P(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(),
|
2013-03-27 15:42:21 -05:00
|
|
|
outputs: outputs,
|
2014-08-19 14:39:26 -05:00
|
|
|
inputs: inputs,
|
|
|
|
clobbers: token::intern_and_get_ident(cons.as_slice()),
|
2013-03-27 15:42:21 -05:00
|
|
|
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
|
2014-09-13 11:06:01 -05:00
|
|
|
}))
|
2013-03-11 00:08:38 -05:00
|
|
|
}
|