Skip to content

Commit 666e1b4

Browse files
committed
Create asm! syntax extension.
1 parent bcc6c3e commit 666e1b4

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

src/libsyntax/ext/asm.rs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright 2012 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+
12+
13+
/*
14+
* Inline assembly support.
15+
*/
16+
17+
use core::prelude::*;
18+
19+
use ast;
20+
use codemap::span;
21+
use ext::base;
22+
use ext::base::*;
23+
24+
pub fn expand_asm(cx: ext_ctxt, sp: span, tts: &[ast::token_tree])
25+
-> base::MacResult {
26+
let args = get_exprs_from_tts(cx, tts);
27+
if args.len() == 0 {
28+
cx.span_fatal(sp, "ast! takes at least 1 argument.");
29+
}
30+
let asm =
31+
expr_to_str(cx, args[0],
32+
~"inline assembly must be a string literal.");
33+
let cons = if args.len() > 1 {
34+
expr_to_str(cx, args[1],
35+
~"constraints must be a string literal.")
36+
} else { ~"" };
37+
38+
MRExpr(@ast::expr {
39+
id: cx.next_id(),
40+
callee_id: cx.next_id(),
41+
node: ast::expr_inline_asm(@asm, @cons),
42+
span: sp
43+
})
44+
}
45+
46+
//
47+
// Local Variables:
48+
// mode: rust
49+
// fill-column: 78;
50+
// indent-tabs-mode: nil
51+
// c-basic-offset: 4
52+
// buffer-file-coding-system: utf-8-unix
53+
// End:
54+
//

src/libsyntax/ext/base.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ pub fn syntax_expander_table() -> SyntaxEnv {
198198
ext::source_util::expand_mod));
199199
syntax_expanders.insert(@~"proto",
200200
builtin_item_tt(ext::pipes::expand_proto));
201+
syntax_expanders.insert(@~"asm",
202+
builtin_normal_tt(ext::asm::expand_asm));
201203
syntax_expanders.insert(
202204
@~"trace_macros",
203205
builtin_normal_tt(ext::trace_macros::expand_trace_macros));

src/libsyntax/print/pprust.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1402,7 +1402,7 @@ pub fn print_expr(s: @ps, &&expr: @ast::expr) {
14021402
word(s.s, ~"__asm__");
14031403
popen(s);
14041404
print_string(s, *a);
1405-
word_space(s, ~", ");
1405+
word_space(s, ~",");
14061406
print_string(s, *c);
14071407
pclose(s);
14081408
}

src/libsyntax/syntax.rc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub mod print {
6060
}
6161

6262
pub mod ext {
63+
pub mod asm;
6364
pub mod base;
6465
pub mod expand;
6566

0 commit comments

Comments
 (0)