|
| 1 | +#![deny(warnings)] |
| 2 | + |
| 3 | +extern crate proc_macro; |
| 4 | +extern crate rand; |
| 5 | +#[macro_use] |
| 6 | +extern crate quote; |
| 7 | +extern crate core; |
| 8 | +extern crate proc_macro2; |
| 9 | +#[macro_use] |
| 10 | +extern crate syn; |
| 11 | + |
| 12 | +use proc_macro2::Span; |
| 13 | +use rand::Rng; |
| 14 | +use rand::SeedableRng; |
| 15 | +use std::sync::atomic::{AtomicUsize, Ordering}; |
| 16 | +use std::time::{SystemTime, UNIX_EPOCH}; |
| 17 | +use syn::{ |
| 18 | + parse, spanned::Spanned, Ident, ItemFn, ReturnType, Type, Visibility, |
| 19 | +}; |
| 20 | + |
| 21 | +static CALL_COUNT: AtomicUsize = AtomicUsize::new(0); |
| 22 | + |
| 23 | +use proc_macro::TokenStream; |
| 24 | + |
| 25 | +/// Attribute to declare the entry point of the program |
| 26 | +/// |
| 27 | +/// **IMPORTANT**: This attribute must appear exactly *once* in the dependency graph. Also, if you |
| 28 | +/// are using Rust 1.30 the attribute must be used on a reachable item (i.e. there must be no |
| 29 | +/// private modules between the item and the root of the crate); if the item is in the root of the |
| 30 | +/// crate you'll be fine. This reachability restriction doesn't apply to Rust 1.31 and newer releases. |
| 31 | +/// |
| 32 | +/// The specified function will be called by the reset handler *after* RAM has been initialized. In |
| 33 | +/// the case of the `thumbv7em-none-eabihf` target the FPU will also be enabled before the function |
| 34 | +/// is called. |
| 35 | +/// |
| 36 | +/// The type of the specified function must be `[unsafe] fn() -> !` (never ending function) |
| 37 | +/// |
| 38 | +/// # Properties |
| 39 | +/// |
| 40 | +/// The entry point will be called by the reset handler. The program can't reference to the entry |
| 41 | +/// point, much less invoke it. |
| 42 | +/// |
| 43 | +/// # Examples |
| 44 | +/// |
| 45 | +/// - Simple entry point |
| 46 | +/// |
| 47 | +/// ``` no_run |
| 48 | +/// # #![no_main] |
| 49 | +/// # use riscv_rt_macros::entry; |
| 50 | +/// #[entry] |
| 51 | +/// fn main() -> ! { |
| 52 | +/// loop { |
| 53 | +/// /* .. */ |
| 54 | +/// } |
| 55 | +/// } |
| 56 | +/// ``` |
| 57 | +#[proc_macro_attribute] |
| 58 | +pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream { |
| 59 | + let f = parse_macro_input!(input as ItemFn); |
| 60 | + |
| 61 | + // check the function signature |
| 62 | + let valid_signature = f.constness.is_none() |
| 63 | + && f.vis == Visibility::Inherited |
| 64 | + && f.abi.is_none() |
| 65 | + && f.decl.inputs.is_empty() |
| 66 | + && f.decl.generics.params.is_empty() |
| 67 | + && f.decl.generics.where_clause.is_none() |
| 68 | + && f.decl.variadic.is_none() |
| 69 | + && match f.decl.output { |
| 70 | + ReturnType::Default => false, |
| 71 | + ReturnType::Type(_, ref ty) => match **ty { |
| 72 | + Type::Never(_) => true, |
| 73 | + _ => false, |
| 74 | + }, |
| 75 | + }; |
| 76 | + |
| 77 | + if !valid_signature { |
| 78 | + return parse::Error::new( |
| 79 | + f.span(), |
| 80 | + "`#[entry]` function must have signature `[unsafe] fn() -> !`", |
| 81 | + ) |
| 82 | + .to_compile_error() |
| 83 | + .into(); |
| 84 | + } |
| 85 | + |
| 86 | + if !args.is_empty() { |
| 87 | + return parse::Error::new(Span::call_site(), "This attribute accepts no arguments") |
| 88 | + .to_compile_error() |
| 89 | + .into(); |
| 90 | + } |
| 91 | + |
| 92 | + // XXX should we blacklist other attributes? |
| 93 | + let attrs = f.attrs; |
| 94 | + let unsafety = f.unsafety; |
| 95 | + let hash = random_ident(); |
| 96 | + let stmts = f.block.stmts; |
| 97 | + |
| 98 | + quote!( |
| 99 | + #[export_name = "main"] |
| 100 | + #(#attrs)* |
| 101 | + pub #unsafety fn #hash() -> ! { |
| 102 | + #(#stmts)* |
| 103 | + } |
| 104 | + ) |
| 105 | + .into() |
| 106 | +} |
| 107 | + |
| 108 | +/// Attribute to mark which function will be called at the beginning of the reset handler. |
| 109 | +/// |
| 110 | +/// **IMPORTANT**: This attribute can appear at most *once* in the dependency graph. Also, if you |
| 111 | +/// are using Rust 1.30 the attribute must be used on a reachable item (i.e. there must be no |
| 112 | +/// private modules between the item and the root of the crate); if the item is in the root of the |
| 113 | +/// crate you'll be fine. This reachability restriction doesn't apply to Rust 1.31 and newer |
| 114 | +/// releases. |
| 115 | +/// |
| 116 | +/// The function must have the signature of `unsafe fn()`. |
| 117 | +/// |
| 118 | +/// The function passed will be called before static variables are initialized. Any access of static |
| 119 | +/// variables will result in undefined behavior. |
| 120 | +/// |
| 121 | +/// # Examples |
| 122 | +/// |
| 123 | +/// ``` |
| 124 | +/// # use riscv_rt_macros::pre_init; |
| 125 | +/// #[pre_init] |
| 126 | +/// unsafe fn before_main() { |
| 127 | +/// // do something here |
| 128 | +/// } |
| 129 | +/// |
| 130 | +/// # fn main() {} |
| 131 | +/// ``` |
| 132 | +#[proc_macro_attribute] |
| 133 | +pub fn pre_init(args: TokenStream, input: TokenStream) -> TokenStream { |
| 134 | + let f = parse_macro_input!(input as ItemFn); |
| 135 | + |
| 136 | + // check the function signature |
| 137 | + let valid_signature = f.constness.is_none() |
| 138 | + && f.vis == Visibility::Inherited |
| 139 | + && f.unsafety.is_some() |
| 140 | + && f.abi.is_none() |
| 141 | + && f.decl.inputs.is_empty() |
| 142 | + && f.decl.generics.params.is_empty() |
| 143 | + && f.decl.generics.where_clause.is_none() |
| 144 | + && f.decl.variadic.is_none() |
| 145 | + && match f.decl.output { |
| 146 | + ReturnType::Default => true, |
| 147 | + ReturnType::Type(_, ref ty) => match **ty { |
| 148 | + Type::Tuple(ref tuple) => tuple.elems.is_empty(), |
| 149 | + _ => false, |
| 150 | + }, |
| 151 | + }; |
| 152 | + |
| 153 | + if !valid_signature { |
| 154 | + return parse::Error::new( |
| 155 | + f.span(), |
| 156 | + "`#[pre_init]` function must have signature `unsafe fn()`", |
| 157 | + ) |
| 158 | + .to_compile_error() |
| 159 | + .into(); |
| 160 | + } |
| 161 | + |
| 162 | + if !args.is_empty() { |
| 163 | + return parse::Error::new(Span::call_site(), "This attribute accepts no arguments") |
| 164 | + .to_compile_error() |
| 165 | + .into(); |
| 166 | + } |
| 167 | + |
| 168 | + // XXX should we blacklist other attributes? |
| 169 | + let attrs = f.attrs; |
| 170 | + let ident = f.ident; |
| 171 | + let block = f.block; |
| 172 | + |
| 173 | + quote!( |
| 174 | + #[export_name = "__pre_init"] |
| 175 | + #(#attrs)* |
| 176 | + pub unsafe fn #ident() #block |
| 177 | + ) |
| 178 | + .into() |
| 179 | +} |
| 180 | + |
| 181 | +// Creates a random identifier |
| 182 | +fn random_ident() -> Ident { |
| 183 | + let secs = SystemTime::now() |
| 184 | + .duration_since(UNIX_EPOCH) |
| 185 | + .unwrap() |
| 186 | + .as_secs(); |
| 187 | + |
| 188 | + let count: u64 = CALL_COUNT.fetch_add(1, Ordering::SeqCst) as u64; |
| 189 | + let mut seed: [u8; 16] = [0; 16]; |
| 190 | + |
| 191 | + for (i, v) in seed.iter_mut().take(8).enumerate() { |
| 192 | + *v = ((secs >> (i * 8)) & 0xFF) as u8 |
| 193 | + } |
| 194 | + |
| 195 | + for (i, v) in seed.iter_mut().skip(8).enumerate() { |
| 196 | + *v = ((count >> (i * 8)) & 0xFF) as u8 |
| 197 | + } |
| 198 | + |
| 199 | + let mut rng = rand::rngs::SmallRng::from_seed(seed); |
| 200 | + Ident::new( |
| 201 | + &(0..16) |
| 202 | + .map(|i| { |
| 203 | + if i == 0 || rng.gen() { |
| 204 | + ('a' as u8 + rng.gen::<u8>() % 25) as char |
| 205 | + } else { |
| 206 | + ('0' as u8 + rng.gen::<u8>() % 10) as char |
| 207 | + } |
| 208 | + }) |
| 209 | + .collect::<String>(), |
| 210 | + Span::call_site(), |
| 211 | + ) |
| 212 | +} |
0 commit comments