diff options
| author | bors <bors@rust-lang.org> | 2013-03-12 07:51:54 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-03-12 07:51:54 -0700 |
| commit | 34aaf350c2c38435a3b20d454b17f8fbbd2a1d8a (patch) | |
| tree | 080d38a2c80450e956bffb75267deb1a114fed2e /src/libsyntax | |
| parent | 014620af902c4798ede78462b2d0e3b749fb2fff (diff) | |
| parent | 18b71a78314505b4dd3816f9662709860aafaf4c (diff) | |
| download | rust-34aaf350c2c38435a3b20d454b17f8fbbd2a1d8a.tar.gz rust-34aaf350c2c38435a3b20d454b17f8fbbd2a1d8a.zip | |
auto merge of #5317 : luqmana/rust/inline-asm, r=graydon
```Rust
#[cfg(target_os = "macos")]
fn helloworld() {
unsafe {
asm!(".pushsection __RODATA, __rodata
msg: .asciz \"Hello World!\"
.popsection
movq msg@GOTPCREL(%rip), %rdi
call _puts");
}
}
#[cfg(target_os = "linux")]
fn helloworld() {
unsafe {
asm!(".pushsection .rodata
msg: .asciz \"Hello World!\"
.popsection
movq msg@GOTPCREL(%rip), %rdi
call puts");
}
}
fn main() {
helloworld();
}
```
```
% rustc foo.rs
% ./foo
Hello World!
```
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ast.rs | 3 | ||||
| -rw-r--r-- | src/libsyntax/ext/asm.rs | 174 | ||||
| -rw-r--r-- | src/libsyntax/ext/base.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/fold.rs | 1 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/print/pprust.rs | 12 | ||||
| -rw-r--r-- | src/libsyntax/syntax.rc | 1 | ||||
| -rw-r--r-- | src/libsyntax/visit.rs | 1 |
8 files changed, 195 insertions, 1 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 27dba9c2b5e..b22018c4c76 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -601,6 +601,9 @@ pub enum expr_ { expr_ret(Option<@expr>), expr_log(log_level, @expr, @expr), + /* asm, clobbers + constraints, volatile, align stack */ + expr_inline_asm(@~str, @~str, bool, bool), + expr_mac(mac), // A struct literal expression. diff --git a/src/libsyntax/ext/asm.rs b/src/libsyntax/ext/asm.rs new file mode 100644 index 00000000000..8051a67d8fd --- /dev/null +++ b/src/libsyntax/ext/asm.rs @@ -0,0 +1,174 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// 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 core::prelude::*; + +use ast; +use codemap::span; +use ext::base; +use ext::base::*; +use parse; +use parse::token; + +enum State { + Asm, + Outputs, + Inputs, + Clobbers, + Options +} + +fn next_state(s: State) -> Option<State> { + match s { + Asm => Some(Outputs), + Outputs => Some(Inputs), + Inputs => Some(Clobbers), + Clobbers => Some(Options), + Options => None + } +} + +pub fn expand_asm(cx: ext_ctxt, sp: span, tts: &[ast::token_tree]) + -> base::MacResult { + + let p = parse::new_parser_from_tts(cx.parse_sess(), cx.cfg(), + vec::from_slice(tts)); + + let mut asm = ~""; + let mut outputs = ~[]; + let mut inputs = ~[]; + let mut cons = ~""; + let mut volatile = false; + let mut alignstack = false; + + let mut state = Asm; + loop outer: { + match state { + Asm => { + asm = expr_to_str(cx, p.parse_expr(), + ~"inline assembly must be a string literal."); + } + Outputs => { + while *p.token != token::EOF && + *p.token != token::COLON && + *p.token != token::MOD_SEP { + + if outputs.len() != 0 { + p.eat(&token::COMMA); + } + + let constraint = p.parse_str(); + p.expect(&token::LPAREN); + let out = p.parse_expr(); + p.expect(&token::RPAREN); + + outputs.push((constraint, out)); + } + } + Inputs => { + while *p.token != token::EOF && + *p.token != token::COLON && + *p.token != token::MOD_SEP { + + if inputs.len() != 0 { + p.eat(&token::COMMA); + } + + let constraint = p.parse_str(); + p.expect(&token::LPAREN); + let in = p.parse_expr(); + p.expect(&token::RPAREN); + + inputs.push((constraint, in)); + } + } + Clobbers => { + let mut clobs = ~[]; + while *p.token != token::EOF && + *p.token != token::COLON && + *p.token != token::MOD_SEP { + + if clobs.len() != 0 { + p.eat(&token::COMMA); + } + + let clob = ~"~{" + *p.parse_str() + ~"}"; + clobs.push(clob); + } + + cons = str::connect(clobs, ","); + } + Options => { + let option = *p.parse_str(); + + if option == ~"volatile" { + volatile = true; + } else if option == ~"alignstack" { + alignstack = true; + } + + if *p.token == token::COMMA { + p.eat(&token::COMMA); + } + } + } + + while *p.token == token::COLON || + *p.token == token::MOD_SEP || + *p.token == token::EOF { + state = if *p.token == token::COLON { + p.bump(); + match next_state(state) { + Some(x) => x, + None => break outer + } + } else if *p.token == token::MOD_SEP { + p.bump(); + let s = match next_state(state) { + Some(x) => x, + None => break outer + }; + match next_state(s) { + Some(x) => x, + None => break outer + } + } else if *p.token == token::EOF { + break outer; + } else { + state + }; + } + } + + MRExpr(@ast::expr { + id: cx.next_id(), + callee_id: cx.next_id(), + node: ast::expr_inline_asm(@asm, @cons, volatile, alignstack), + span: sp + }) +} + + + +// +// Local Variables: +// mode: rust +// fill-column: 78; +// indent-tabs-mode: nil +// c-basic-offset: 4 +// buffer-file-coding-system: utf-8-unix +// End: +// diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 23cabc09946..1eae4b84cc9 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -198,6 +198,8 @@ pub fn syntax_expander_table() -> SyntaxEnv { ext::source_util::expand_mod)); syntax_expanders.insert(@~"proto", builtin_item_tt(ext::pipes::expand_proto)); + syntax_expanders.insert(@~"asm", + builtin_normal_tt(ext::asm::expand_asm)); syntax_expanders.insert( @~"trace_macros", builtin_normal_tt(ext::trace_macros::expand_trace_macros)); diff --git a/src/libsyntax/fold.rs b/src/libsyntax/fold.rs index 427760c920f..15097f57b02 100644 --- a/src/libsyntax/fold.rs +++ b/src/libsyntax/fold.rs @@ -560,6 +560,7 @@ pub fn noop_fold_expr(e: &expr_, fld: @ast_fold) -> expr_ { fld.fold_expr(e) ) } + expr_inline_asm(*) => copy *e, expr_mac(ref mac) => expr_mac(fold_mac((*mac))), expr_struct(path, ref fields, maybe_expr) => { expr_struct( diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 99c1c2cb1fe..51e36d9ec02 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -27,7 +27,7 @@ use ast::{expr_field, expr_fn_block, expr_if, expr_index}; use ast::{expr_lit, expr_log, expr_loop, expr_loop_body, expr_mac}; use ast::{expr_method_call, expr_paren, expr_path, expr_repeat}; use ast::{expr_ret, expr_swap, expr_struct, expr_tup, expr_unary}; -use ast::{expr_vec, expr_vstore, expr_vstore_mut_box}; +use ast::{expr_vec, expr_vstore, expr_vstore_mut_box, expr_inline_asm}; use ast::{expr_vstore_fixed, expr_vstore_slice, expr_vstore_box}; use ast::{expr_vstore_mut_slice, expr_while, extern_fn, field, fn_decl}; use ast::{expr_vstore_uniq, TyClosure, TyBareFn, Onceness, Once, Many}; diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index 62f593f15c1..92883123782 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1398,6 +1398,18 @@ pub fn print_expr(s: @ps, &&expr: @ast::expr) { } } } + ast::expr_inline_asm(a, c, v, _) => { + if v { + word(s.s, ~"__volatile__ asm!"); + } else { + word(s.s, ~"asm!"); + } + popen(s); + print_string(s, *a); + word_space(s, ~","); + print_string(s, *c); + pclose(s); + } ast::expr_mac(ref m) => print_mac(s, (*m)), ast::expr_paren(e) => { popen(s); diff --git a/src/libsyntax/syntax.rc b/src/libsyntax/syntax.rc index 7f84d6a3010..e13ef976d97 100644 --- a/src/libsyntax/syntax.rc +++ b/src/libsyntax/syntax.rc @@ -60,6 +60,7 @@ pub mod print { } pub mod ext { + pub mod asm; pub mod base; pub mod expand; diff --git a/src/libsyntax/visit.rs b/src/libsyntax/visit.rs index f04894729bd..95ab603f584 100644 --- a/src/libsyntax/visit.rs +++ b/src/libsyntax/visit.rs @@ -562,6 +562,7 @@ pub fn visit_expr<E>(ex: @expr, e: E, v: vt<E>) { } expr_mac(ref mac) => visit_mac((*mac), e, v), expr_paren(x) => (v.visit_expr)(x, e, v), + expr_inline_asm(*) => (), } (v.visit_expr_post)(ex, e, v); } |
