|
| 1 | +//! Internal implementation details of `xtensa-lx6-rt`. |
| 2 | +//! |
| 3 | +//! Do not use this crate directly. |
| 4 | +
|
| 5 | +#![deny(warnings)] |
| 6 | +#![feature(proc_macro_diagnostic)] |
| 7 | + |
| 8 | +extern crate proc_macro; |
| 9 | + |
| 10 | +use proc_macro::TokenStream; |
| 11 | +//use proc_macro2::Span; |
| 12 | +use quote::quote; |
| 13 | +//use std::collections::HashSet; |
| 14 | +use syn::spanned::Spanned; |
| 15 | +use syn::Item; |
| 16 | + |
| 17 | +// check if all called function are in ram |
| 18 | +// check if all used data i in ram |
| 19 | +// check that no constants are use in teh function (these cannot be forced to ram) |
| 20 | +fn check_ram_function(func: &syn::ItemFn) { |
| 21 | + eprintln!("{:?}", func); |
| 22 | +} |
| 23 | + |
| 24 | +#[proc_macro_attribute] |
| 25 | +pub fn ram(_args: TokenStream, input: TokenStream) -> TokenStream { |
| 26 | + let item: syn::Item = syn::parse(input).expect("failed to parse input"); |
| 27 | + |
| 28 | + let section: proc_macro2::TokenStream; |
| 29 | + match item { |
| 30 | + Item::Static(ref _struct_item) => section = quote! {#[link_section=".data"]}, |
| 31 | + Item::Const(ref _struct_item) => section = quote! {#[link_section=".data"]}, |
| 32 | + Item::Fn(ref function_item) => { |
| 33 | + check_ram_function(function_item); |
| 34 | + section = quote! {#[link_section=".rwtext"]}; |
| 35 | + } |
| 36 | + _ => { |
| 37 | + section = quote! {}; |
| 38 | + item.span() |
| 39 | + .unstable() |
| 40 | + .error("#[ram] attribute can only be applied to functions, statics and consts") |
| 41 | + .emit(); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + let output = quote! { |
| 46 | + #section |
| 47 | + #item |
| 48 | + }; |
| 49 | + output.into() |
| 50 | + |
| 51 | + // let f = parse_macro_input!(input as ItemFn); |
| 52 | + /* TokenStream::from(quote!( |
| 53 | + #[link_section=".rwtext"] |
| 54 | + #f |
| 55 | + ))*/ |
| 56 | + |
| 57 | + /* |
| 58 | + // check the function signature |
| 59 | + let valid_signature = f.sig.constness.is_none() |
| 60 | + && f.vis == Visibility::Inherited |
| 61 | + && f.sig.abi.is_none() |
| 62 | + && f.sig.inputs.is_empty() |
| 63 | + && f.sig.generics.params.is_empty() |
| 64 | + && f.sig.generics.where_clause.is_none() |
| 65 | + && f.sig.variadic.is_none() |
| 66 | + && match f.sig.output { |
| 67 | + ReturnType::Default => false, |
| 68 | + ReturnType::Type(_, ref ty) => match **ty { |
| 69 | + Type::Never(_) => true, |
| 70 | + _ => false, |
| 71 | + }, |
| 72 | + }; |
| 73 | +
|
| 74 | + if !valid_signature { |
| 75 | + return parse::Error::new( |
| 76 | + f.span(), |
| 77 | + "`#[entry]` function must have signature `[unsafe] fn() -> !`", |
| 78 | + ) |
| 79 | + .to_compile_error() |
| 80 | + .into(); |
| 81 | + } |
| 82 | +
|
| 83 | + if !args.is_empty() { |
| 84 | + return parse::Error::new(Span::call_site(), "This attribute accepts no arguments") |
| 85 | + .to_compile_error() |
| 86 | + .into(); |
| 87 | + } |
| 88 | +
|
| 89 | + // XXX should we blacklist other attributes? |
| 90 | + let (statics, stmts) = match extract_static_muts(f.block.stmts) { |
| 91 | + Err(e) => return e.to_compile_error().into(), |
| 92 | + Ok(x) => x, |
| 93 | + }; |
| 94 | +
|
| 95 | + f.sig.ident = Ident::new( |
| 96 | + &format!("__xtensa_lx6_rt_{}", f.sig.ident), |
| 97 | + Span::call_site(), |
| 98 | + ); |
| 99 | + f.sig.inputs.extend(statics.iter().map(|statik| { |
| 100 | + let ident = &statik.ident; |
| 101 | + let ty = &statik.ty; |
| 102 | + let attrs = &statik.attrs; |
| 103 | +
|
| 104 | + // Note that we use an explicit `'static` lifetime for the entry point arguments. This makes |
| 105 | + // it more flexible, and is sound here, since the entry will not be called again, ever. |
| 106 | + syn::parse::<FnArg>( |
| 107 | + quote!(#[allow(non_snake_case)] #(#attrs)* #ident: &'static mut #ty).into(), |
| 108 | + ) |
| 109 | + .unwrap() |
| 110 | + })); |
| 111 | + f.block.stmts = stmts; |
| 112 | +
|
| 113 | + let tramp_ident = Ident::new(&format!("{}_trampoline", f.sig.ident), Span::call_site()); |
| 114 | + let ident = &f.sig.ident; |
| 115 | +
|
| 116 | + let resource_args = statics |
| 117 | + .iter() |
| 118 | + .map(|statik| { |
| 119 | + let (ref cfgs, ref attrs) = extract_cfgs(statik.attrs.clone()); |
| 120 | + let ident = &statik.ident; |
| 121 | + let ty = &statik.ty; |
| 122 | + let expr = &statik.expr; |
| 123 | + quote! { |
| 124 | + #(#cfgs)* |
| 125 | + { |
| 126 | + #(#attrs)* |
| 127 | + static mut #ident: #ty = #expr; |
| 128 | + &mut #ident |
| 129 | + } |
| 130 | + } |
| 131 | + }) |
| 132 | + .collect::<Vec<_>>(); |
| 133 | +
|
| 134 | + if let Err(error) = check_attr_whitelist(&f.attrs, WhiteListCaller::Entry) { |
| 135 | + return error; |
| 136 | + } |
| 137 | +
|
| 138 | + let (ref cfgs, ref attrs) = extract_cfgs(f.attrs.clone()); |
| 139 | + quote!( |
| 140 | + #(#cfgs)* |
| 141 | + #(#attrs)* |
| 142 | + #[doc(hidden)] |
| 143 | + #[export_name = "main"] |
| 144 | + pub unsafe extern "C" fn #tramp_ident() { |
| 145 | + #ident( |
| 146 | + #(#resource_args),* |
| 147 | + ) |
| 148 | + } |
| 149 | +
|
| 150 | + #f |
| 151 | + ) |
| 152 | + .into() |
| 153 | + */ |
| 154 | +} |
0 commit comments