]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/asm.rs
Auto merge of #52046 - cramertj:fix-generator-mir, r=eddyb
[rust.git] / src / libsyntax_ext / asm.rs
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.
4 //
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.
10
11 // Inline assembly support.
12 //
13 use self::State::*;
14
15 use syntax::ast;
16 use syntax::ext::base;
17 use syntax::ext::base::*;
18 use syntax::feature_gate;
19 use syntax::parse::{self, token};
20 use syntax::ptr::P;
21 use syntax::symbol::Symbol;
22 use syntax::ast::AsmDialect;
23 use syntax_pos::Span;
24 use syntax::tokenstream;
25
26 enum State {
27     Asm,
28     Outputs,
29     Inputs,
30     Clobbers,
31     Options,
32     StateNone,
33 }
34
35 impl State {
36     fn next(&self) -> State {
37         match *self {
38             Asm => Outputs,
39             Outputs => Inputs,
40             Inputs => Clobbers,
41             Clobbers => Options,
42             Options => StateNone,
43             StateNone => StateNone,
44         }
45     }
46 }
47
48 const OPTIONS: &'static [&'static str] = &["volatile", "alignstack", "intel"];
49
50 pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt,
51                        sp: Span,
52                        tts: &[tokenstream::TokenTree])
53                        -> Box<dyn base::MacResult + 'cx> {
54     if !cx.ecfg.enable_asm() {
55         feature_gate::emit_feature_err(&cx.parse_sess,
56                                        "asm",
57                                        sp,
58                                        feature_gate::GateIssue::Language,
59                                        feature_gate::EXPLAIN_ASM);
60         return DummyResult::expr(sp);
61     }
62
63     // Split the tts before the first colon, to avoid `asm!("x": y)`  being
64     // parsed as `asm!(z)` with `z = "x": y` which is type ascription.
65     let first_colon = tts.iter()
66         .position(|tt| {
67             match *tt {
68                 tokenstream::TokenTree::Token(_, token::Colon) |
69                 tokenstream::TokenTree::Token(_, token::ModSep) => true,
70                 _ => false,
71             }
72         })
73         .unwrap_or(tts.len());
74     let mut p = cx.new_parser_from_tts(&tts[first_colon..]);
75     let mut asm = Symbol::intern("");
76     let mut asm_str_style = None;
77     let mut outputs = Vec::new();
78     let mut inputs = Vec::new();
79     let mut clobs = Vec::new();
80     let mut volatile = false;
81     let mut alignstack = false;
82     let mut dialect = AsmDialect::Att;
83
84     let mut state = Asm;
85
86     'statement: loop {
87         match state {
88             Asm => {
89                 if asm_str_style.is_some() {
90                     // If we already have a string with instructions,
91                     // ending up in Asm state again is an error.
92                     span_err!(cx, sp, E0660, "malformed inline assembly");
93                     return DummyResult::expr(sp);
94                 }
95                 // Nested parser, stop before the first colon (see above).
96                 let mut p2 = cx.new_parser_from_tts(&tts[..first_colon]);
97                 let (s, style) = match expr_to_string(cx,
98                                                       panictry!(p2.parse_expr()),
99                                                       "inline assembly must be a string literal") {
100                     Some((s, st)) => (s, st),
101                     // let compilation continue
102                     None => return DummyResult::expr(sp),
103                 };
104
105                 // This is most likely malformed.
106                 if p2.token != token::Eof {
107                     let mut extra_tts = panictry!(p2.parse_all_token_trees());
108                     extra_tts.extend(tts[first_colon..].iter().cloned());
109                     p = parse::stream_to_parser(cx.parse_sess, extra_tts.into_iter().collect());
110                 }
111
112                 asm = s;
113                 asm_str_style = Some(style);
114             }
115             Outputs => {
116                 while p.token != token::Eof && p.token != token::Colon && p.token != token::ModSep {
117
118                     if !outputs.is_empty() {
119                         p.eat(&token::Comma);
120                     }
121
122                     let (constraint, _str_style) = panictry!(p.parse_str());
123
124                     let span = p.prev_span;
125
126                     panictry!(p.expect(&token::OpenDelim(token::Paren)));
127                     let out = panictry!(p.parse_expr());
128                     panictry!(p.expect(&token::CloseDelim(token::Paren)));
129
130                     // Expands a read+write operand into two operands.
131                     //
132                     // Use '+' modifier when you want the same expression
133                     // to be both an input and an output at the same time.
134                     // It's the opposite of '=&' which means that the memory
135                     // cannot be shared with any other operand (usually when
136                     // a register is clobbered early.)
137                     let constraint_str = constraint.as_str();
138                     let mut ch = constraint_str.chars();
139                     let output = match ch.next() {
140                         Some('=') => None,
141                         Some('+') => {
142                             Some(Symbol::intern(&format!("={}", ch.as_str())))
143                         }
144                         _ => {
145                             span_err!(cx, span, E0661,
146                                                     "output operand constraint lacks '=' or '+'");
147                             None
148                         }
149                     };
150
151                     let is_rw = output.is_some();
152                     let is_indirect = constraint_str.contains("*");
153                     outputs.push(ast::InlineAsmOutput {
154                         constraint: output.unwrap_or(constraint),
155                         expr: out,
156                         is_rw,
157                         is_indirect,
158                     });
159                 }
160             }
161             Inputs => {
162                 while p.token != token::Eof && p.token != token::Colon && p.token != token::ModSep {
163
164                     if !inputs.is_empty() {
165                         p.eat(&token::Comma);
166                     }
167
168                     let (constraint, _str_style) = panictry!(p.parse_str());
169
170                     if constraint.as_str().starts_with("=") {
171                         span_err!(cx, p.prev_span, E0662,
172                                                 "input operand constraint contains '='");
173                     } else if constraint.as_str().starts_with("+") {
174                         span_err!(cx, p.prev_span, E0663,
175                                                 "input operand constraint contains '+'");
176                     }
177
178                     panictry!(p.expect(&token::OpenDelim(token::Paren)));
179                     let input = panictry!(p.parse_expr());
180                     panictry!(p.expect(&token::CloseDelim(token::Paren)));
181
182                     inputs.push((constraint, input));
183                 }
184             }
185             Clobbers => {
186                 while p.token != token::Eof && p.token != token::Colon && p.token != token::ModSep {
187
188                     if !clobs.is_empty() {
189                         p.eat(&token::Comma);
190                     }
191
192                     let (s, _str_style) = panictry!(p.parse_str());
193
194                     if OPTIONS.iter().any(|&opt| s == opt) {
195                         cx.span_warn(p.prev_span, "expected a clobber, found an option");
196                     } else if s.as_str().starts_with("{") || s.as_str().ends_with("}") {
197                         span_err!(cx, p.prev_span, E0664,
198                                                 "clobber should not be surrounded by braces");
199                     }
200
201                     clobs.push(s);
202                 }
203             }
204             Options => {
205                 let (option, _str_style) = panictry!(p.parse_str());
206
207                 if option == "volatile" {
208                     // Indicates that the inline assembly has side effects
209                     // and must not be optimized out along with its outputs.
210                     volatile = true;
211                 } else if option == "alignstack" {
212                     alignstack = true;
213                 } else if option == "intel" {
214                     dialect = AsmDialect::Intel;
215                 } else {
216                     cx.span_warn(p.prev_span, "unrecognized option");
217                 }
218
219                 if p.token == token::Comma {
220                     p.eat(&token::Comma);
221                 }
222             }
223             StateNone => (),
224         }
225
226         loop {
227             // MOD_SEP is a double colon '::' without space in between.
228             // When encountered, the state must be advanced twice.
229             match (&p.token, state.next(), state.next().next()) {
230                 (&token::Colon, StateNone, _) |
231                 (&token::ModSep, _, StateNone) => {
232                     p.bump();
233                     break 'statement;
234                 }
235                 (&token::Colon, st, _) |
236                 (&token::ModSep, _, st) => {
237                     p.bump();
238                     state = st;
239                 }
240                 (&token::Eof, ..) => break 'statement,
241                 _ => break,
242             }
243         }
244     }
245
246     // If there are no outputs, the inline assembly is executed just for its side effects,
247     // so ensure that it is volatile
248     if outputs.is_empty() {
249         volatile = true;
250     }
251
252     MacEager::expr(P(ast::Expr {
253         id: ast::DUMMY_NODE_ID,
254         node: ast::ExprKind::InlineAsm(P(ast::InlineAsm {
255             asm,
256             asm_str_style: asm_str_style.unwrap(),
257             outputs,
258             inputs,
259             clobbers: clobs,
260             volatile,
261             alignstack,
262             dialect,
263             ctxt: cx.backtrace(),
264         })),
265         span: sp,
266         attrs: ast::ThinVec::new(),
267     }))
268 }