Skip to content

Commit f0ca5d4

Browse files
committed
Auto merge of #41305 - frewsxcv:rollup, r=frewsxcv
Rollup of 4 pull requests - Successful merges: #40702, #41172, #41249, #41303 - Failed merges:
2 parents bbdaad0 + 13dc855 commit f0ca5d4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+789
-67
lines changed

src/doc/unstable-book/src/SUMMARY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
- [future_atomic_orderings](future-atomic-orderings.md)
8484
- [generic_param_attrs](generic-param-attrs.md)
8585
- [get_type_id](get-type-id.md)
86+
- [global_asm](global_asm.md)
8687
- [heap_api](heap-api.md)
8788
- [i128](i128.md)
8889
- [i128_type](i128-type.md)

src/doc/unstable-book/src/asm.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,5 @@ constraints, etc.
189189

190190
[llvm-docs]: http://llvm.org/docs/LangRef.html#inline-assembler-expressions
191191

192+
If you need more power and don't mind losing some of the niceties of
193+
`asm!`, check out [global_asm](global_asm.html).
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
# `global_asm`
2+
3+
The tracking issue for this feature is: [#35119]
4+
5+
[#35119]: https://github.com/rust-lang/rust/issues/35119
6+
7+
------------------------
8+
9+
The `global_asm!` macro allows the programmer to write arbitrary
10+
assembly outside the scope of a function body, passing it through
11+
`rustc` and `llvm` to the assembler. The macro is a no-frills
12+
interface to LLVM's concept of [module-level inline assembly]. That is,
13+
all caveats applicable to LLVM's module-level inline assembly apply
14+
to `global_asm!`.
15+
16+
[module-level inline assembly]: http://llvm.org/docs/LangRef.html#module-level-inline-assembly
17+
18+
`global_asm!` fills a role not currently satisfied by either `asm!`
19+
or `#[naked]` functions. The programmer has _all_ features of the
20+
assembler at their disposal. The linker will expect to resolve any
21+
symbols defined in the inline assembly, modulo any symbols marked as
22+
external. It also means syntax for directives and assembly follow the
23+
conventions of the assembler in your toolchain.
24+
25+
A simple usage looks like this:
26+
27+
```rust,ignore
28+
# #![feature(global_asm)]
29+
# you also need relevant target_arch cfgs
30+
global_asm!(include_str!("something_neato.s"));
31+
```
32+
33+
And a more complicated usage looks like this:
34+
35+
```rust,ignore
36+
# #![feature(global_asm)]
37+
# #![cfg(any(target_arch = "x86", target_arch = "x86_64"))]
38+
39+
pub mod sally {
40+
global_asm!(r#"
41+
.global foo
42+
foo:
43+
jmp baz
44+
"#);
45+
46+
#[no_mangle]
47+
pub unsafe extern "C" fn baz() {}
48+
}
49+
50+
// the symbols `foo` and `bar` are global, no matter where
51+
// `global_asm!` was used.
52+
extern "C" {
53+
fn foo();
54+
fn bar();
55+
}
56+
57+
pub mod harry {
58+
global_asm!(r#"
59+
.global bar
60+
bar:
61+
jmp quux
62+
"#);
63+
64+
#[no_mangle]
65+
pub unsafe extern "C" fn quux() {}
66+
}
67+
```
68+
69+
You may use `global_asm!` multiple times, anywhere in your crate, in
70+
whatever way suits you. The effect is as if you concatenated all
71+
usages and placed the larger, single usage in the crate root.
72+
73+
------------------------
74+
75+
If you don't need quite as much power and flexibility as
76+
`global_asm!` provides, and you don't mind restricting your inline
77+
assembly to `fn` bodies only, you might try the [asm](asm.html)
78+
feature instead.

src/librustc/hir/def.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ pub enum Def {
5757
// Macro namespace
5858
Macro(DefId, MacroKind),
5959

60+
GlobalAsm(DefId),
61+
6062
// Both namespaces
6163
Err,
6264
}
@@ -144,7 +146,8 @@ impl Def {
144146
Def::Variant(id) | Def::VariantCtor(id, ..) | Def::Enum(id) | Def::TyAlias(id) |
145147
Def::AssociatedTy(id) | Def::TyParam(id) | Def::Struct(id) | Def::StructCtor(id, ..) |
146148
Def::Union(id) | Def::Trait(id) | Def::Method(id) | Def::Const(id) |
147-
Def::AssociatedConst(id) | Def::Local(id) | Def::Upvar(id, ..) | Def::Macro(id, ..) => {
149+
Def::AssociatedConst(id) | Def::Local(id) | Def::Upvar(id, ..) | Def::Macro(id, ..) |
150+
Def::GlobalAsm(id) => {
148151
id
149152
}
150153

@@ -185,6 +188,7 @@ impl Def {
185188
Def::Label(..) => "label",
186189
Def::SelfTy(..) => "self type",
187190
Def::Macro(..) => "macro",
191+
Def::GlobalAsm(..) => "global asm",
188192
Def::Err => "unresolved item",
189193
}
190194
}

src/librustc/hir/intravisit.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,9 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item) {
474474
visitor.visit_id(item.id);
475475
walk_list!(visitor, visit_foreign_item, &foreign_module.items);
476476
}
477+
ItemGlobalAsm(_) => {
478+
visitor.visit_id(item.id);
479+
}
477480
ItemTy(ref typ, ref type_parameters) => {
478481
visitor.visit_id(item.id);
479482
visitor.visit_ty(typ);

src/librustc/hir/lowering.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,13 @@ impl<'a> LoweringContext<'a> {
646646
}
647647
}
648648

649+
fn lower_global_asm(&mut self, ga: &GlobalAsm) -> P<hir::GlobalAsm> {
650+
P(hir::GlobalAsm {
651+
asm: ga.asm,
652+
ctxt: ga.ctxt,
653+
})
654+
}
655+
649656
fn lower_variant(&mut self, v: &Variant) -> hir::Variant {
650657
Spanned {
651658
node: hir::Variant_ {
@@ -1288,6 +1295,7 @@ impl<'a> LoweringContext<'a> {
12881295
}
12891296
ItemKind::Mod(ref m) => hir::ItemMod(self.lower_mod(m)),
12901297
ItemKind::ForeignMod(ref nm) => hir::ItemForeignMod(self.lower_foreign_mod(nm)),
1298+
ItemKind::GlobalAsm(ref ga) => hir::ItemGlobalAsm(self.lower_global_asm(ga)),
12911299
ItemKind::Ty(ref t, ref generics) => {
12921300
hir::ItemTy(self.lower_ty(t), self.lower_generics(generics))
12931301
}

src/librustc/hir/map/def_collector.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ impl<'a> visit::Visitor<'a> for DefCollector<'a> {
109109
DefPathData::ValueNs(i.ident.name.as_str()),
110110
ItemKind::MacroDef(..) => DefPathData::MacroDef(i.ident.name.as_str()),
111111
ItemKind::Mac(..) => return self.visit_macro_invoc(i.id, false),
112+
ItemKind::GlobalAsm(..) => DefPathData::Misc,
112113
ItemKind::Use(ref view_path) => {
113114
match view_path.node {
114115
ViewPathGlob(..) => {}

src/librustc/hir/map/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,6 +1077,7 @@ fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String {
10771077
ItemFn(..) => "fn",
10781078
ItemMod(..) => "mod",
10791079
ItemForeignMod(..) => "foreign mod",
1080+
ItemGlobalAsm(..) => "global asm",
10801081
ItemTy(..) => "ty",
10811082
ItemEnum(..) => "enum",
10821083
ItemStruct(..) => "struct",

src/librustc/hir/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,12 @@ pub struct ForeignMod {
14951495
pub items: HirVec<ForeignItem>,
14961496
}
14971497

1498+
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
1499+
pub struct GlobalAsm {
1500+
pub asm: Symbol,
1501+
pub ctxt: SyntaxContext,
1502+
}
1503+
14981504
#[derive(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
14991505
pub struct EnumDef {
15001506
pub variants: HirVec<Variant>,
@@ -1686,6 +1692,8 @@ pub enum Item_ {
16861692
ItemMod(Mod),
16871693
/// An external module
16881694
ItemForeignMod(ForeignMod),
1695+
/// Module-level inline assembly (from global_asm!)
1696+
ItemGlobalAsm(P<GlobalAsm>),
16891697
/// A type alias, e.g. `type Foo = Bar<u8>`
16901698
ItemTy(P<Ty>, Generics),
16911699
/// An enum definition, e.g. `enum Foo<A, B> {C<A>, D<B>}`
@@ -1720,6 +1728,7 @@ impl Item_ {
17201728
ItemFn(..) => "function",
17211729
ItemMod(..) => "module",
17221730
ItemForeignMod(..) => "foreign module",
1731+
ItemGlobalAsm(..) => "global asm",
17231732
ItemTy(..) => "type alias",
17241733
ItemEnum(..) => "enum",
17251734
ItemStruct(..) => "struct",

src/librustc/hir/print.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,11 @@ impl<'a> State<'a> {
633633
self.print_foreign_mod(nmod, &item.attrs)?;
634634
self.bclose(item.span)?;
635635
}
636+
hir::ItemGlobalAsm(ref ga) => {
637+
self.head(&visibility_qualified(&item.vis, "global asm"))?;
638+
word(&mut self.s, &ga.asm.as_str())?;
639+
self.end()?
640+
}
636641
hir::ItemTy(ref ty, ref params) => {
637642
self.ibox(indent_unit)?;
638643
self.ibox(0)?;

0 commit comments

Comments
 (0)