diff --git a/src/doc/reference.md b/src/doc/reference.md index a9e12833906db..068a3b151b9d1 100644 --- a/src/doc/reference.md +++ b/src/doc/reference.md @@ -1847,6 +1847,8 @@ type int8_t = i8; - `should_panic` - indicates that this test function should panic, inverting the success condition. - `cold` - The function is unlikely to be executed, so optimize it (and calls to it) differently. +- `naked` - The function utilizes a custom ABI or custom inline ASM that requires + the normal stack frame and return to be left out. ### Static-only attributes diff --git a/src/librustc_trans/trans/attributes.rs b/src/librustc_trans/trans/attributes.rs index af4c2205e659b..f4e642b89b848 100644 --- a/src/librustc_trans/trans/attributes.rs +++ b/src/librustc_trans/trans/attributes.rs @@ -43,6 +43,21 @@ pub fn inline(val: ValueRef, inline: InlineAttr) { }; } +/// Tell LLVM whether the function should be naked. +#[inline] +pub fn naked(val: ValueRef, naked: bool) { + if naked { + llvm::SetFunctionAttribute(val, llvm::Attribute::Naked); + } else { + unsafe { + llvm::LLVMRemoveFunctionAttr( + val, + llvm::Attribute::Naked.bits() as c_ulonglong, + ); + } + } +} + /// Tell LLVM to emit or not emit the information necessary to unwind the stack for the function. #[inline] pub fn emit_uwtable(val: ValueRef, emit: bool) { @@ -116,6 +131,8 @@ pub fn from_fn_attrs(ccx: &CrateContext, attrs: &[ast::Attribute], llfn: ValueRe llvm::FunctionIndex as c_uint, llvm::ColdAttribute as u64) } + } else if attr.check_name("naked") { + naked(llfn, true); } else if attr.check_name("allocator") { llvm::Attribute::NoAlias.apply_llfn(llvm::ReturnIndex as c_uint, llfn); } else if attr.check_name("unwind") { diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index be6ad9311114a..3dca41b61a484 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -193,6 +193,9 @@ const KNOWN_FEATURES: &'static [(&'static str, &'static str, Option, Status // allow `extern "platform-intrinsic" { ... }` ("platform_intrinsics", "1.4.0", Some(27731), Active), + // allow `#[naked]` + ("naked_attributes", "1.5.0", None, Active), + // allow `#[unwind]` ("unwind_attributes", "1.4.0", None, Active), @@ -326,6 +329,9 @@ pub const KNOWN_ATTRIBUTES: &'static [(&'static str, AttributeType, AttributeGat // FIXME: #14406 these are processed in trans, which happens after the // lint pass ("cold", Whitelisted, Ungated), + ("naked", Whitelisted, Gated("naked_attributes", + "the `#[naked]` attribute \ + is an experimental feature")), ("export_name", Whitelisted, Ungated), ("inline", Whitelisted, Ungated), ("link", Whitelisted, Ungated),