-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add hermit-macro crate with system attribute
Signed-off-by: Martin Kröning <[email protected]>
- Loading branch information
Showing
6 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "hermit-macro" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
proc-macro2 = "1" | ||
quote = "1" | ||
syn = { version = "2", features = ["full"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use proc_macro::TokenStream; | ||
use quote::ToTokens; | ||
use syn::parse::Nothing; | ||
use syn::parse_macro_input; | ||
|
||
macro_rules! bail { | ||
($span:expr, $($tt:tt)*) => { | ||
return Err(syn::Error::new_spanned($span, format!($($tt)*))) | ||
}; | ||
} | ||
|
||
mod system; | ||
|
||
// The structure of this implementation is inspired by Amanieu's excellent naked-function crate. | ||
#[proc_macro_attribute] | ||
pub fn system(attr: TokenStream, item: TokenStream) -> TokenStream { | ||
parse_macro_input!(attr as Nothing); | ||
match system::system_attribute(parse_macro_input!(item)) { | ||
Ok(item) => item.into_token_stream().into(), | ||
Err(e) => e.to_compile_error().into(), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
use proc_macro2::{Ident, Span}; | ||
use quote::quote; | ||
use syn::{Abi, Attribute, Item, ItemFn, Pat, Result, Signature, Visibility}; | ||
|
||
fn validate_vis(vis: &Visibility) -> Result<()> { | ||
if !matches!(vis, Visibility::Public(_)) { | ||
bail!(vis, "#[system] functions must be public"); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
struct ParsedSig { | ||
args: Vec<Ident>, | ||
} | ||
|
||
fn parse_sig(sig: &Signature) -> Result<ParsedSig> { | ||
if let Some(constness) = sig.constness { | ||
bail!(constness, "#[system] is not supported on const functions"); | ||
} | ||
if let Some(asyncness) = sig.asyncness { | ||
bail!(asyncness, "#[system] is not supported on async functions"); | ||
} | ||
match &sig.abi { | ||
Some(Abi { | ||
extern_token: _, | ||
name: Some(name), | ||
}) if matches!(&*name.value(), "C" | "C-unwind") => {} | ||
_ => bail!( | ||
&sig.abi, | ||
"#[system] functions must be `extern \"C\"` or `extern \"C-unwind\"`" | ||
), | ||
} | ||
if !sig.generics.params.is_empty() { | ||
bail!( | ||
&sig.generics, | ||
"#[system] cannot be used with generic functions" | ||
); | ||
} | ||
if !sig.ident.to_string().starts_with("sys_") { | ||
bail!(&sig.ident, "#[system] functions must start with `sys_`"); | ||
} | ||
|
||
let mut args = vec![]; | ||
|
||
for arg in &sig.inputs { | ||
let pat = match arg { | ||
syn::FnArg::Receiver(_) => bail!(arg, "self is not allowed in #[system] functions"), | ||
syn::FnArg::Typed(pat) => pat, | ||
}; | ||
if let Pat::Ident(pat) = &*pat.pat { | ||
args.push(pat.ident.clone()); | ||
} else { | ||
bail!(pat, "unsupported pattern in #[system] function argument"); | ||
} | ||
} | ||
|
||
Ok(ParsedSig { args }) | ||
} | ||
|
||
fn validate_attrs(attrs: &[Attribute]) -> Result<()> { | ||
for attr in attrs { | ||
if attr.path().is_ident("no_mangle") { | ||
bail!(attr, "#[system] functions must not be `#[no_mangle]`"); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn emit_func(mut func: ItemFn, sig: &ParsedSig) -> Result<ItemFn> { | ||
let args = &sig.args; | ||
let vis = func.vis.clone(); | ||
let sig = func.sig.clone(); | ||
|
||
let ident = Ident::new(&format!("__{}", func.sig.ident), Span::call_site()); | ||
func.sig.ident = ident.clone(); | ||
func.vis = Visibility::Inherited; | ||
|
||
let func = syn::parse2(quote! { | ||
#[no_mangle] | ||
#vis #sig { | ||
#func | ||
|
||
kernel_function!(#ident(#(#args),*)) | ||
} | ||
})?; | ||
|
||
Ok(func) | ||
} | ||
|
||
pub fn system_attribute(func: ItemFn) -> Result<Item> { | ||
validate_vis(&func.vis)?; | ||
let sig = parse_sig(&func.sig)?; | ||
validate_attrs(&func.attrs)?; | ||
let func = emit_func(func, &sig)?; | ||
// println!("{}", func.to_token_stream()); | ||
// panic!(); | ||
Ok(Item::Fn(func)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use quote::ToTokens; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_system_attribute() -> Result<()> { | ||
let input = syn::parse2(quote! { | ||
pub extern "C" fn sys_test(a: i8, b: i16) -> i32 { | ||
let c = i16::from(a) + b; | ||
i32::from(c) | ||
} | ||
})?; | ||
|
||
let expected = quote! { | ||
#[no_mangle] | ||
pub extern "C" fn sys_test(a: i8, b: i16) -> i32 { | ||
extern "C" fn __sys_test(a: i8, b: i16) -> i32 { | ||
let c = i16::from(a) + b; | ||
i32::from(c) | ||
} | ||
|
||
kernel_function!(__sys_test(a, b)) | ||
} | ||
}; | ||
|
||
let result = system_attribute(input)?.into_token_stream(); | ||
|
||
assert_eq!(expected.to_string(), result.to_string()); | ||
|
||
Ok(()) | ||
} | ||
} |