1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
12 * Inline assembly support.
20 use parse::token::InternedString;
33 fn next(&self) -> State {
40 StateNone => StateNone
45 static OPTIONS: &'static [&'static str] = &["volatile", "alignstack", "intel"];
47 pub fn expand_asm(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
49 let mut p = parse::new_parser_from_tts(cx.parse_sess(),
52 .map(|x| (*x).clone())
55 let mut asm = InternedString::new("");
56 let mut asm_str_style = None;
57 let mut outputs = Vec::new();
58 let mut inputs = Vec::new();
59 let mut cons = "".to_owned();
60 let mut volatile = false;
61 let mut alignstack = false;
62 let mut dialect = ast::AsmAtt;
66 let mut read_write_operands = Vec::new();
71 let (s, style) = match expr_to_str(cx, p.parse_expr(),
72 "inline assembly must be a string literal.") {
73 Some((s, st)) => (s, st),
74 // let compilation continue
75 None => return DummyResult::expr(sp),
78 asm_str_style = Some(style);
81 while p.token != token::EOF &&
82 p.token != token::COLON &&
83 p.token != token::MOD_SEP {
85 if outputs.len() != 0 {
89 let (constraint, _str_style) = p.parse_str();
91 let span = p.last_span;
93 p.expect(&token::LPAREN);
94 let out = p.parse_expr();
95 p.expect(&token::RPAREN);
97 // Expands a read+write operand into two operands.
99 // Use '+' modifier when you want the same expression
100 // to be both an input and an output at the same time.
101 // It's the opposite of '=&' which means that the memory
102 // cannot be shared with any other operand (usually when
103 // a register is clobbered early.)
104 let output = match constraint.get().slice_shift_char() {
105 (Some('='), _) => None,
106 (Some('+'), operand) => {
107 // Save a reference to the output
108 read_write_operands.push((outputs.len(), out));
109 Some(token::intern_and_get_ident("=" + operand))
112 cx.span_err(span, "output operand constraint lacks '=' or '+'");
117 outputs.push((output.unwrap_or(constraint), out));
121 while p.token != token::EOF &&
122 p.token != token::COLON &&
123 p.token != token::MOD_SEP {
125 if inputs.len() != 0 {
126 p.eat(&token::COMMA);
129 let (constraint, _str_style) = p.parse_str();
131 if constraint.get().starts_with("=") {
132 cx.span_err(p.last_span, "input operand constraint contains '='");
133 } else if constraint.get().starts_with("+") {
134 cx.span_err(p.last_span, "input operand constraint contains '+'");
137 p.expect(&token::LPAREN);
138 let input = p.parse_expr();
139 p.expect(&token::RPAREN);
141 inputs.push((constraint, input));
145 let mut clobs = Vec::new();
146 while p.token != token::EOF &&
147 p.token != token::COLON &&
148 p.token != token::MOD_SEP {
150 if clobs.len() != 0 {
151 p.eat(&token::COMMA);
154 let (s, _str_style) = p.parse_str();
155 let clob = format!("~\\{{}\\}", s);
158 if OPTIONS.iter().any(|opt| s.equiv(opt)) {
159 cx.span_warn(p.last_span, "expected a clobber, but found an option");
163 cons = clobs.connect(",");
166 let (option, _str_style) = p.parse_str();
168 if option.equiv(&("volatile")) {
169 // Indicates that the inline assembly has side effects
170 // and must not be optimized out along with its outputs.
172 } else if option.equiv(&("alignstack")) {
174 } else if option.equiv(&("intel")) {
175 dialect = ast::AsmIntel;
177 cx.span_warn(p.last_span, "unrecognized option");
180 if p.token == token::COMMA {
181 p.eat(&token::COMMA);
188 // MOD_SEP is a double colon '::' without space in between.
189 // When encountered, the state must be advanced twice.
190 match (&p.token, state.next(), state.next().next()) {
191 (&token::COLON, StateNone, _) |
192 (&token::MOD_SEP, _, StateNone) => {
196 (&token::COLON, st, _) |
197 (&token::MOD_SEP, _, st) => {
201 (&token::EOF, _, _) => break 'statement,
207 // Append an input operand, with the form of ("0", expr)
208 // that links to an output operand.
209 for &(i, out) in read_write_operands.iter() {
210 inputs.push((token::intern_and_get_ident(i.to_str()), out));
213 MacExpr::new(@ast::Expr {
214 id: ast::DUMMY_NODE_ID,
215 node: ast::ExprInlineAsm(ast::InlineAsm {
216 asm: token::intern_and_get_ident(asm.get()),
217 asm_str_style: asm_str_style.unwrap(),
218 clobbers: token::intern_and_get_ident(cons),
222 alignstack: alignstack,