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.
21 use parse::token::InternedString;
35 fn next(&self) -> State {
42 StateNone => StateNone
47 static OPTIONS: &'static [&'static str] = &["volatile", "alignstack", "intel"];
49 pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree])
50 -> Box<base::MacResult+'cx> {
51 let mut p = cx.new_parser_from_tts(tts);
52 let mut asm = InternedString::new("");
53 let mut asm_str_style = None;
54 let mut outputs = Vec::new();
55 let mut inputs = Vec::new();
56 let mut clobs = Vec::new();
57 let mut volatile = false;
58 let mut alignstack = false;
59 let mut dialect = ast::AsmAtt;
66 let (s, style) = match expr_to_string(cx, p.parse_expr(),
67 "inline assembly must be a string literal") {
68 Some((s, st)) => (s, st),
69 // let compilation continue
70 None => return DummyResult::expr(sp),
73 asm_str_style = Some(style);
76 while p.token != token::Eof &&
77 p.token != token::Colon &&
78 p.token != token::ModSep {
80 if outputs.len() != 0 {
84 let (constraint, _str_style) = p.parse_str();
86 let span = p.last_span;
88 p.expect(&token::OpenDelim(token::Paren));
89 let out = p.parse_expr();
90 p.expect(&token::CloseDelim(token::Paren));
92 // Expands a read+write operand into two operands.
94 // Use '+' modifier when you want the same expression
95 // to be both an input and an output at the same time.
96 // It's the opposite of '=&' which means that the memory
97 // cannot be shared with any other operand (usually when
98 // a register is clobbered early.)
99 let output = match constraint.get().slice_shift_char() {
100 Some(('=', _)) => None,
101 Some(('+', operand)) => {
102 Some(token::intern_and_get_ident(&format!(
106 cx.span_err(span, "output operand constraint lacks '=' or '+'");
111 let is_rw = output.is_some();
112 outputs.push((output.unwrap_or(constraint), out, is_rw));
116 while p.token != token::Eof &&
117 p.token != token::Colon &&
118 p.token != token::ModSep {
120 if inputs.len() != 0 {
121 p.eat(&token::Comma);
124 let (constraint, _str_style) = p.parse_str();
126 if constraint.get().starts_with("=") {
127 cx.span_err(p.last_span, "input operand constraint contains '='");
128 } else if constraint.get().starts_with("+") {
129 cx.span_err(p.last_span, "input operand constraint contains '+'");
132 p.expect(&token::OpenDelim(token::Paren));
133 let input = p.parse_expr();
134 p.expect(&token::CloseDelim(token::Paren));
136 inputs.push((constraint, input));
140 while p.token != token::Eof &&
141 p.token != token::Colon &&
142 p.token != token::ModSep {
144 if clobs.len() != 0 {
145 p.eat(&token::Comma);
148 let (s, _str_style) = p.parse_str();
150 if OPTIONS.iter().any(|&opt| s == opt) {
151 cx.span_warn(p.last_span, "expected a clobber, found an option");
157 let (option, _str_style) = p.parse_str();
159 if option == "volatile" {
160 // Indicates that the inline assembly has side effects
161 // and must not be optimized out along with its outputs.
163 } else if option == "alignstack" {
165 } else if option == "intel" {
166 dialect = ast::AsmIntel;
168 cx.span_warn(p.last_span, "unrecognized option");
171 if p.token == token::Comma {
172 p.eat(&token::Comma);
179 // MOD_SEP is a double colon '::' without space in between.
180 // When encountered, the state must be advanced twice.
181 match (&p.token, state.next(), state.next().next()) {
182 (&token::Colon, StateNone, _) |
183 (&token::ModSep, _, StateNone) => {
187 (&token::Colon, st, _) |
188 (&token::ModSep, _, st) => {
192 (&token::Eof, _, _) => break 'statement,
198 let expn_id = cx.codemap().record_expansion(codemap::ExpnInfo {
200 callee: codemap::NameAndSpan {
201 name: "asm".to_string(),
202 format: codemap::MacroBang,
207 MacExpr::new(P(ast::Expr {
208 id: ast::DUMMY_NODE_ID,
209 node: ast::ExprInlineAsm(ast::InlineAsm {
210 asm: token::intern_and_get_ident(asm.get()),
211 asm_str_style: asm_str_style.unwrap(),
216 alignstack: alignstack,