Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add naked attribute #29189

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/doc/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions src/librustc_trans/trans/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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") {
Expand Down
6 changes: 6 additions & 0 deletions src/libsyntax/feature_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ const KNOWN_FEATURES: &'static [(&'static str, &'static str, Option<u32>, 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),

Expand Down Expand Up @@ -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),
Expand Down