Skip to content

Commit ecccc0d

Browse files
committed
Parse inline assembly.
1 parent 4e350c7 commit ecccc0d

File tree

19 files changed

+72
-8
lines changed

19 files changed

+72
-8
lines changed

src/etc/vim/syntax/rust.vim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ elseif exists("b:current_syntax")
1111
endif
1212

1313
syn match rustAssert "\<assert\(\w\)*"
14-
syn keyword rustKeyword as break
14+
syn keyword rustKeyword __asm__ as break
1515
syn keyword rustKeyword copy do drop else extern
1616
syn keyword rustKeyword for if impl let log
1717
syn keyword rustKeyword loop match mod once priv pub pure

src/librustc/lib/llvm.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,11 @@ pub mod llvm {
14331433

14341434
/** Enables LLVM debug output. */
14351435
pub unsafe fn LLVMSetDebug(Enabled: c_int);
1436+
1437+
/** Prepares inline assembly. */
1438+
pub unsafe fn LLVMInlineAsm(Ty: TypeRef, AsmString: *c_char,
1439+
Constraints: *c_char, SideEffects: Bool,
1440+
AlignStack: Bool) -> ValueRef;
14361441
}
14371442
}
14381443

src/librustc/middle/liveness.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,8 @@ fn visit_expr(expr: @expr, &&self: @mut IrMaps, vt: vt<@mut IrMaps>) {
620620
expr_do_body(*) | expr_cast(*) | expr_unary(*) | expr_break(_) |
621621
expr_again(_) | expr_lit(_) | expr_ret(*) | expr_block(*) |
622622
expr_assign(*) | expr_swap(*) | expr_assign_op(*) | expr_mac(*) |
623-
expr_struct(*) | expr_repeat(*) | expr_paren(*) => {
623+
expr_struct(*) | expr_repeat(*) | expr_paren(*) |
624+
expr_inline_asm(*) => {
624625
visit::visit_expr(expr, self, vt);
625626
}
626627
}
@@ -1345,6 +1346,7 @@ pub impl Liveness {
13451346
self.propagate_through_expr(e, succ)
13461347
}
13471348
1349+
expr_inline_asm(*) |
13481350
expr_lit(*) => {
13491351
succ
13501352
}
@@ -1618,7 +1620,7 @@ fn check_expr(expr: @expr, &&self: @Liveness, vt: vt<@Liveness>) {
16181620
expr_cast(*) | expr_unary(*) | expr_ret(*) | expr_break(*) |
16191621
expr_again(*) | expr_lit(_) | expr_block(*) | expr_swap(*) |
16201622
expr_mac(*) | expr_addr_of(*) | expr_struct(*) | expr_repeat(*) |
1621-
expr_paren(*) => {
1623+
expr_paren(*) | expr_inline_asm(*) => {
16221624
visit::visit_expr(expr, self, vt);
16231625
}
16241626
}

src/librustc/middle/mem_categorization.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ pub impl mem_categorization_ctxt {
447447
ast::expr_while(*) | ast::expr_block(*) | ast::expr_loop(*) |
448448
ast::expr_match(*) | ast::expr_lit(*) | ast::expr_break(*) |
449449
ast::expr_mac(*) | ast::expr_again(*) | ast::expr_struct(*) |
450-
ast::expr_repeat(*) => {
450+
ast::expr_repeat(*) | ast::expr_inline_asm(*) => {
451451
return self.cat_rvalue(expr, expr_ty);
452452
}
453453
}

src/librustc/middle/moves.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,8 @@ pub impl VisitContext {
560560

561561
expr_break(*) |
562562
expr_again(*) |
563-
expr_lit(*) => {}
563+
expr_lit(*) |
564+
expr_inline_asm(*) => {}
564565

565566
expr_loop(ref blk, _) => {
566567
self.consume_block(blk, visitor);

src/librustc/middle/trans/build.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use syntax::codemap::span;
1818

1919
use core::prelude::*;
2020
use core::cast;
21-
use core::libc::{c_uint, c_int, c_ulonglong};
21+
use core::libc::{c_uint, c_int, c_ulonglong, c_char};
2222
use core::libc;
2323
use core::option::Some;
2424
use core::ptr;
@@ -872,6 +872,17 @@ pub fn add_comment(bcx: block, text: &str) {
872872
}
873873
}
874874
875+
pub fn InlineAsmCall(cx: block, asm: *c_char, cons: *c_char) -> ValueRef {
876+
unsafe {
877+
count_insn(cx, "inlineasm");
878+
879+
let llfty = T_fn(~[], T_void());
880+
let v = llvm::LLVMInlineAsm(llfty, asm, cons, False, False);
881+
882+
Call(cx, v, ~[])
883+
}
884+
}
885+
875886
pub fn Call(cx: block, Fn: ValueRef, Args: &[ValueRef]) -> ValueRef {
876887
if cx.unreachable { return _UndefReturn(cx, Fn); }
877888
unsafe {

src/librustc/middle/trans/expr.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,14 @@ fn trans_rvalue_dps_unadjusted(bcx: block, expr: @ast::expr,
691691
ast::expr_assign_op(op, dst, src) => {
692692
return trans_assign_op(bcx, expr, op, dst, src);
693693
}
694+
ast::expr_inline_asm(asm, cons) => {
695+
do str::as_c_str(*asm) |a| {
696+
do str::as_c_str(*cons) |c| {
697+
InlineAsmCall(bcx, a, c);
698+
}
699+
}
700+
return bcx;
701+
}
694702
_ => {
695703
bcx.tcx().sess.span_bug(
696704
expr.span,

src/librustc/middle/trans/type_use.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ pub fn mark_for_expr(cx: Context, e: @expr) {
353353
expr_match(*) | expr_block(_) | expr_if(*) | expr_while(*) |
354354
expr_break(_) | expr_again(_) | expr_unary(_, _) | expr_lit(_) |
355355
expr_mac(_) | expr_addr_of(_, _) | expr_ret(_) | expr_loop(_, _) |
356-
expr_loop_body(_) | expr_do_body(_) => ()
356+
expr_loop_body(_) | expr_do_body(_) | expr_inline_asm(*) => ()
357357
}
358358
}
359359

src/librustc/middle/ty.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3076,6 +3076,7 @@ pub fn expr_kind(tcx: ctxt,
30763076
ast::expr_block(*) |
30773077
ast::expr_copy(*) |
30783078
ast::expr_repeat(*) |
3079+
ast::expr_inline_asm(*) |
30793080
ast::expr_lit(@codemap::spanned {node: lit_str(_), _}) |
30803081
ast::expr_vstore(_, ast::expr_vstore_slice) |
30813082
ast::expr_vstore(_, ast::expr_vstore_mut_slice) |

src/librustc/middle/typeck/check/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2303,6 +2303,7 @@ pub fn check_expr_with_unifier(fcx: @mut FnCtxt,
23032303
let region_lb = ty::re_scope(expr.id);
23042304
instantiate_path(fcx, pth, tpt, expr.span, expr.id, region_lb);
23052305
}
2306+
ast::expr_inline_asm(*) => { fcx.write_nil(id); }
23062307
ast::expr_mac(_) => tcx.sess.bug(~"unexpanded macro"),
23072308
ast::expr_break(_) => { fcx.write_bot(id); bot = true; }
23082309
ast::expr_again(_) => { fcx.write_bot(id); bot = true; }

src/librustc/middle/typeck/check/regionck.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ pub mod guarantor {
682682

683683
// All of these expressions are rvalues and hence their
684684
// value is not guaranteed by a region pointer.
685+
ast::expr_inline_asm(*) |
685686
ast::expr_mac(*) |
686687
ast::expr_lit(_) |
687688
ast::expr_unary(*) |

src/libsyntax/ast.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,8 @@ pub enum expr_ {
600600
expr_again(Option<ident>),
601601
expr_ret(Option<@expr>),
602602
expr_log(log_level, @expr, @expr),
603+
604+
expr_inline_asm(@~str /* asm */, @~str /* constraints */),
603605
604606
expr_mac(mac),
605607

src/libsyntax/fold.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,7 @@ pub fn noop_fold_expr(e: &expr_, fld: @ast_fold) -> expr_ {
560560
fld.fold_expr(e)
561561
)
562562
}
563+
expr_inline_asm(*) => copy *e,
563564
expr_mac(ref mac) => expr_mac(fold_mac((*mac))),
564565
expr_struct(path, ref fields, maybe_expr) => {
565566
expr_struct(

src/libsyntax/parse/parser.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use ast::{expr_field, expr_fn_block, expr_if, expr_index};
2727
use ast::{expr_lit, expr_log, expr_loop, expr_loop_body, expr_mac};
2828
use ast::{expr_method_call, expr_paren, expr_path, expr_repeat};
2929
use ast::{expr_ret, expr_swap, expr_struct, expr_tup, expr_unary};
30-
use ast::{expr_vec, expr_vstore, expr_vstore_mut_box};
30+
use ast::{expr_vec, expr_vstore, expr_vstore_mut_box, expr_inline_asm};
3131
use ast::{expr_vstore_fixed, expr_vstore_slice, expr_vstore_box};
3232
use ast::{expr_vstore_mut_slice, expr_while, extern_fn, field, fn_decl};
3333
use ast::{expr_vstore_uniq, TyClosure, TyBareFn, Onceness, Once, Many};
@@ -1184,6 +1184,14 @@ pub impl Parser {
11841184
}
11851185
}
11861186
hi = self.span.hi;
1187+
} else if self.eat_keyword(&~"__asm__") {
1188+
self.expect(&token::LPAREN);
1189+
let asm = self.parse_str();
1190+
self.expect(&token::COMMA);
1191+
let cons = self.parse_str();
1192+
ex = expr_inline_asm(asm, cons);
1193+
hi = self.span.hi;
1194+
self.expect(&token::RPAREN);
11871195
} else if self.eat_keyword(&~"log") {
11881196
self.expect(&token::LPAREN);
11891197
let lvl = self.parse_expr();

src/libsyntax/parse/token.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -488,6 +488,7 @@ pub fn temporary_keyword_table() -> HashMap<~str, ()> {
488488
pub fn strict_keyword_table() -> HashMap<~str, ()> {
489489
let words = HashMap();
490490
let keys = ~[
491+
~"__asm__",
491492
~"as", ~"assert",
492493
~"break",
493494
~"const", ~"copy",

src/libsyntax/print/pprust.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,14 @@ pub fn print_expr(s: @ps, &&expr: @ast::expr) {
13981398
}
13991399
}
14001400
}
1401+
ast::expr_inline_asm(a, c) => {
1402+
word(s.s, ~"__asm__");
1403+
popen(s);
1404+
print_string(s, *a);
1405+
word_space(s, ~", ");
1406+
print_string(s, *c);
1407+
pclose(s);
1408+
}
14011409
ast::expr_mac(ref m) => print_mac(s, (*m)),
14021410
ast::expr_paren(e) => {
14031411
popen(s);

src/libsyntax/visit.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,7 @@ pub fn visit_expr<E>(ex: @expr, e: E, v: vt<E>) {
562562
}
563563
expr_mac(ref mac) => visit_mac((*mac), e, v),
564564
expr_paren(x) => (v.visit_expr)(x, e, v),
565+
expr_inline_asm(*) => (),
565566
}
566567
(v.visit_expr_post)(ex, e, v);
567568
}

src/rustllvm/RustWrapper.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//
1616
//===----------------------------------------------------------------------===
1717

18+
#include "llvm/InlineAsm.h"
1819
#include "llvm/LLVMContext.h"
1920
#include "llvm/Linker.h"
2021
#include "llvm/PassManager.h"
@@ -539,3 +540,14 @@ extern "C" void LLVMSetDebug(int Enabled) {
539540
DebugFlag = Enabled;
540541
#endif
541542
}
543+
544+
extern "C" LLVMValueRef LLVMInlineAsm(LLVMTypeRef Ty,
545+
char *AsmString,
546+
char *Constraints,
547+
LLVMBool HasSideEffects,
548+
LLVMBool IsAlignStack) {
549+
return wrap(InlineAsm::get(unwrap<FunctionType>(Ty), AsmString,
550+
Constraints, HasSideEffects,
551+
IsAlignStack));
552+
// IsAlignStack, InlineAsm::AD_Intel));
553+
}

src/rustllvm/rustllvm.def.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -583,3 +583,4 @@ LLVMX86MMXTypeInContext
583583
LLVMConstNamedStruct
584584
LLVMStructCreateNamed
585585
LLVMStructSetBody
586+
LLVMInlineAsm

0 commit comments

Comments
 (0)