1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
use proc_macro::TokenStream;

use proc_macro2::Span;
use proc_macro2::TokenStream as TokenStream2;

use quote::{format_ident, quote, quote_spanned, ToTokens};

use syn::{parse_quote, Attribute, AttributeArgs, Error, Item, ItemImpl, ItemMod, Meta, MetaList};

/// Macro for defining a new LLVM plugin.
///
/// This macro must be used at the module level, and must define a `name` and `version`
/// parameters.
///
/// It will look for the `#[pass(name = ..)]` and `#[analysis]` attributes inside the module,
/// to generate code that, once executed by the [opt] tool, will register passes to the LLVM
/// pass manager.
///
/// [opt]: https://llvm.org/docs/NewPassManager.html#invoking-opt
///
/// # Examples
///
/// Using the outer attribute syntax:
///
/// ```
/// #[llvm_plugin::plugin(name = "some_name", version = "0.1")]
/// mod plugin {
///     # use llvm_plugin::{
///     #    LlvmModuleAnalysis, LlvmModulePass, ModuleAnalysisManager, PreservedAnalyses,
///     # };
///     # use llvm_plugin::inkwell::module::Module;
///     #[derive(Default)]
///     struct Pass1;
///
///     #[pass(name = "some_other_name")]
///     impl LlvmModulePass for Pass1 {
///         fn run_pass(
///             &self,
///             module: &mut Module,
///             manager: &ModuleAnalysisManager,
///         ) -> PreservedAnalyses {
///             todo!()
///         }
///     }
///
///     #[derive(Default)]
///     struct Analysis1;
///
///     #[analysis]
///     impl LlvmModuleAnalysis for Analysis1 {
///         fn run_analysis(
///             &self,
///             module: &Module,
///             manager: &ModuleAnalysisManager,
///         ) -> String {
///             todo!()
///         }
///     }
/// }
/// ```
///
/// Using the inner attribute syntax (requires `nightly`):
///
/// ```
/// #![feature(prelude_import)]
/// #![feature(custom_inner_attributes)]
/// #![llvm_plugin::plugin(name = "some_name", version = "0.1")]
///
/// # use llvm_plugin::{
/// #    LlvmModuleAnalysis, LlvmModulePass, ModuleAnalysisManager, PreservedAnalyses,
/// # };
/// # use llvm_plugin::inkwell::module::Module;
/// #[derive(Default)]
/// struct Pass1;
///
/// #[pass(name = "some_other_name")]
/// impl LlvmModulePass for Pass1 {
///     fn run_pass(
///         &self,
///         module: &mut Module,
///         manager: &ModuleAnalysisManager,
///     ) -> PreservedAnalyses {
///         todo!()
///     }
/// }
///
/// #[derive(Default)]
/// struct Analysis1;
///
/// #[analysis]
/// impl LlvmModuleAnalysis for Analysis1 {
///     fn run_analysis(
///         &self,
///         module: &Module,
///         manager: &ModuleAnalysisManager,
///     ) -> String {
///         todo!()
///     }
/// }
/// ```
#[proc_macro_attribute]
pub fn plugin(attrs: TokenStream, input: TokenStream) -> TokenStream {
    match plugin_impl(attrs, input) {
        Ok(ts) => ts.into(),
        Err(e) => {
            let msg = e.to_string();
            quote_spanned! { e.span() => fn error() { std::compile_error!(#msg) } }.into()
        }
    }
}

fn plugin_impl(attrs: TokenStream, input: TokenStream) -> syn::Result<TokenStream2> {
    // parse_macro_input!() should not be used, since it generates code
    // containing compile_error!() instead of std::compile_error!(), which
    // is not compatible with inner attribute proc macros.

    let args = syn::parse_macro_input::parse(attrs)?;
    let (name, version) = match parse_plugin_args(args) {
        Some(parsed) => parsed?,
        None => return Err(Error::new(Span::call_site(), "`plugin` attr missing args")),
    };

    // try to parse the tokens as if we were called as an outer module attribute
    // (e.g. #[..] mod { .. })
    if let Ok(mut parsed_mod) = syn::parse::<ItemMod>(input.clone()) {
        let items = match &mut parsed_mod.content {
            Some((_, items)) => items,
            None => {
                return Err(Error::new_spanned(
                    parsed_mod,
                    "expecting module with items",
                ));
            }
        };
        expand_plugin_items(&name, &version, items)?;
        Ok(parsed_mod.into_token_stream())
    } else {
        // try to parse the tokens as if we were called as an inner custom attribute
        // (e.g. #![..])
        let mut file = syn::parse::<syn::File>(input)?;
        expand_plugin_items(&name, &version, &mut file.items)?;
        Ok(file.into_token_stream())
    }
}

fn expand_plugin_items(name: &str, version: &str, items: &mut Vec<Item>) -> syn::Result<()> {
    const CRATE_NAME: &str = "llvm_plugin";
    const MODULE_PASS_TRAIT: &str = "LlvmModulePass";
    const MODULE_ANALYSIS_TRAIT: &str = "LlvmModuleAnalysis";
    const FUNCTION_PASS_TRAIT: &str = "LlvmFunctionPass";
    const FUNCTION_ANALYSIS_TRAIT: &str = "LlvmFunctionAnalysis";

    let mut register_snippets = Vec::new();
    let mut new_items = Vec::new();

    for item in items.iter_mut() {
        let item_impl = match item {
            Item::Impl(item) => item,
            _ => continue,
        };

        match parse_llvm_plugin_attribute(&mut item_impl.attrs) {
            Some(Ok(name)) => match &item_impl.trait_ {
                Some((_, path, _)) if matches_path(path, &[CRATE_NAME, MODULE_PASS_TRAIT]) => {
                    process_llvm_module_pass_impl(
                        &name,
                        &mut new_items,
                        item_impl,
                        &mut register_snippets,
                    )
                }
                Some((_, path, _)) if matches_path(path, &[CRATE_NAME, FUNCTION_PASS_TRAIT]) => {
                    process_llvm_function_pass_impl(
                        &name,
                        &mut new_items,
                        item_impl,
                        &mut register_snippets,
                    )
                }
                _ => {
                    return Err(Error::new_spanned(
                        item_impl,
                        format!(
                            "expected impl of traits `{}`, or `{}`",
                            MODULE_PASS_TRAIT, FUNCTION_PASS_TRAIT,
                        ),
                    ))
                }
            },
            Some(Err(e)) => return Err(e),
            None => (),
        };

        if parse_llvm_analysis_attribute(&mut item_impl.attrs).is_none() {
            continue;
        }

        let analysis_id = match &item_impl.trait_ {
            Some((_, path, _)) if matches_path(path, &[CRATE_NAME, MODULE_ANALYSIS_TRAIT]) => {
                process_llvm_module_analysis_impl(
                    &mut new_items,
                    item_impl,
                    &mut register_snippets,
                )?
            }
            Some((_, path, _)) if matches_path(path, &[CRATE_NAME, FUNCTION_ANALYSIS_TRAIT]) => {
                process_llvm_function_analysis_impl(
                    &mut new_items,
                    item_impl,
                    &mut register_snippets,
                )?
            }
            _ => {
                return Err(Error::new_spanned(
                    item_impl,
                    format!(
                        "expected impl of traits `{}`, or `{}`",
                        MODULE_ANALYSIS_TRAIT, FUNCTION_ANALYSIS_TRAIT
                    ),
                ))
            }
        };

        new_items.push(parse_quote! {
            static #analysis_id: u8 = 0; // value not relevant
        });
    }

    items.extend(new_items);
    items.push(parse_quote! {
        #[no_mangle]
        extern "C" fn llvmGetPassPluginInfo() -> llvm_plugin::PassPluginLibraryInfo {
            # ( #register_snippets )*

            llvm_plugin::PassPluginLibraryInfo {
                api_version: llvm_plugin::get_llvm_plugin_api_version__(),
                plugin_name: #name.as_ptr(),
                plugin_version: #version.as_ptr(),
                plugin_registrar: llvm_plugin::get_llvm_plugin_registrar__(),
            }
        }
    });

    Ok(())
}

fn parse_plugin_args(args: AttributeArgs) -> Option<syn::Result<(String, String)>> {
    let mut args_iter = args.iter();

    let arg = args_iter.next()?;
    let name = match arg {
        syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue {
            path,
            lit: syn::Lit::Str(s),
            ..
        })) if path.is_ident("name") => s.value(),
        _ => {
            return Some(Err(Error::new_spanned(
                arg,
                "expected arg `name=\"value\"`",
            )))
        }
    };

    let arg = args_iter.next()?;
    let version = match arg {
        syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue {
            path,
            lit: syn::Lit::Str(s),
            ..
        })) if path.is_ident("version") => s.value(),
        _ => {
            return Some(Err(Error::new_spanned(
                arg,
                "expected arg `version=\"value\"`",
            )))
        }
    };

    Some(Ok((name, version)))
}

fn parse_llvm_plugin_attribute(attrs: &mut Vec<Attribute>) -> Option<Result<String, Error>> {
    for (id, attr) in attrs.iter().enumerate() {
        let args = match attr.parse_meta() {
            Ok(Meta::List(MetaList { path, nested, .. })) if path.is_ident("pass") => nested,
            _ => continue,
        };

        let mut args_iter = args.iter();

        let name = match args_iter.next() {
            Some(arg) => match arg {
                syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue {
                    path,
                    lit: syn::Lit::Str(s),
                    ..
                })) if path.is_ident("name") => s.value(),
                _ => {
                    return Some(Err(Error::new_spanned(
                        arg,
                        "expected arg `name=\"value\"`",
                    )))
                }
            },
            None => return Some(Err(Error::new_spanned(&args, "`pass` attr missing args"))),
        };

        attrs.remove(id);
        return Some(Ok(name));
    }

    None
}

fn parse_llvm_analysis_attribute(attrs: &mut Vec<Attribute>) -> Option<()> {
    for (id, attr) in attrs.iter().enumerate() {
        match attr.parse_meta() {
            Ok(Meta::Path(path)) if path.is_ident("analysis") => {
                attrs.remove(id);
                return Some(());
            }
            _ => continue,
        }
    }
    None
}

fn process_llvm_module_pass_impl(
    name: &str,
    module_items: &mut Vec<Item>,
    item_impl: &ItemImpl,
    register_snippets: &mut Vec<TokenStream2>,
) {
    let ItemImpl { self_ty, .. } = item_impl;
    let entrypoint = format_ident!("{}_entrypoint", name);

    module_items.push(parse_quote! {
        extern "C" fn #entrypoint(
            module: *mut std::ffi::c_void,
            manager: *mut std::ffi::c_void,
        ) -> llvm_plugin::PreservedAnalyses {
            let mut module = unsafe { llvm_plugin::inkwell::module::Module::new(module.cast()) };
            let pass = #self_ty::default();
            let manager = unsafe { llvm_plugin::ModuleAnalysisManager::from_raw(manager, None) };
            use llvm_plugin::LlvmModulePass;
            let preserve = pass.run_pass(&mut module, &manager);
            std::mem::forget(module);
            preserve
        }
    });

    register_snippets.push(quote! {
        llvm_plugin::register_module_pass__(#name, #entrypoint);
    });
}

fn process_llvm_function_pass_impl(
    name: &str,
    module_items: &mut Vec<Item>,
    item_impl: &ItemImpl,
    register_snippets: &mut Vec<TokenStream2>,
) {
    let ItemImpl { self_ty, .. } = item_impl;
    let entrypoint = format_ident!("{}_entrypoint", name);

    module_items.push(parse_quote! {
        extern "C" fn #entrypoint(
            function: *mut std::ffi::c_void,
            manager: *mut std::ffi::c_void,
        ) -> llvm_plugin::PreservedAnalyses {
            let mut function = unsafe { llvm_plugin::inkwell::values::FunctionValue::new(function.cast()).unwrap() };
            let pass = #self_ty::default();
            let manager = unsafe { llvm_plugin::FunctionAnalysisManager::from_raw(manager, None) };
            use llvm_plugin::LlvmFunctionPass;
            let preserve = pass.run_pass(&mut function, &manager);
            std::mem::forget(function);
            preserve
        }
    });

    register_snippets.push(quote! {
        llvm_plugin::register_function_pass__(#name, #entrypoint);
    });
}

fn process_llvm_module_analysis_impl(
    module_items: &mut Vec<Item>,
    item_impl: &mut ItemImpl,
    register_snippets: &mut Vec<TokenStream2>,
) -> syn::Result<syn::Ident> {
    let ItemImpl { self_ty, items, .. } = item_impl;

    // get trait implementor's name
    let analysis_ident = match self_ty.as_ref() {
        syn::Type::Path(path) => path
            .path
            .get_ident()
            .ok_or_else(|| Error::new_spanned(&self_ty, "expected single-ident type"))?
            .clone(),
        _ => return Err(Error::new_spanned(&item_impl, "expected path-like type")),
    };

    let mut return_ty = None;
    for item in items.iter() {
        match item {
            syn::ImplItem::Method(syn::ImplItemMethod {
                sig:
                    syn::Signature {
                        ident,
                        output: syn::ReturnType::Type(_, ty),
                        ..
                    },
                ..
            }) if ident == "run_analysis" => return_ty = Some(ty.clone()),
            _ => continue,
        }
    }

    // preprend the crate name to the analysis name, to avoid symbol resolution conflicts
    let analysis_id = analysis_ident.to_string().to_uppercase();
    let crate_name = std::env::var("CARGO_CRATE_NAME").expect("CARGO_CRATE_NAME not set");
    let analysis_id = format_ident!("{}_{}", crate_name.to_uppercase(), analysis_id);

    items.push(parse_quote! {
        type Result = #return_ty;
    });
    items.push(parse_quote! {
        fn id() -> llvm_plugin::AnalysisKey {
            &#analysis_id as *const u8 as *mut _
        }
    });

    let entrypoint = format_ident!("{}_entrypoint", analysis_ident.to_string().to_lowercase());

    module_items.push(parse_quote! {
        extern "C" fn #entrypoint(
            module: *mut std::ffi::c_void,
            manager: *mut std::ffi::c_void,
            result: *mut *mut std::ffi::c_void,
            deleter: *mut extern "C" fn(*mut std::ffi::c_void),
        ) {
            use llvm_plugin::LlvmModuleAnalysis;
            let mut module = unsafe { llvm_plugin::inkwell::module::Module::new(module.cast()) };
            let analysis = #self_ty::default(); // FIXME
            let manager = unsafe { llvm_plugin::ModuleAnalysisManager::from_raw(manager, Some(#self_ty::id())) };
            let data = analysis.run_analysis(&mut module, &manager);
            let data = Box::new(data);
            extern "C" fn free(data: *mut std::ffi::c_void) {
                drop(unsafe { Box::<<#self_ty as LlvmModuleAnalysis>::Result>::from_raw(data.cast()) })
            }
            unsafe {
                *result = Box::<<#self_ty as LlvmModuleAnalysis>::Result>::into_raw(data).cast();
                *deleter = free;
            }
            std::mem::forget(module);
        }
    });

    register_snippets.push(quote! {
        use llvm_plugin::LlvmModuleAnalysis;
        unsafe { llvm_plugin::register_module_analysis__(#self_ty::id(), #entrypoint) };
    });

    Ok(analysis_id)
}

fn process_llvm_function_analysis_impl(
    module_items: &mut Vec<Item>,
    item_impl: &mut ItemImpl,
    register_snippets: &mut Vec<TokenStream2>,
) -> syn::Result<syn::Ident> {
    let ItemImpl { self_ty, items, .. } = item_impl;

    // get trait implementor's name
    let analysis_ident = match self_ty.as_ref() {
        syn::Type::Path(path) => path
            .path
            .get_ident()
            .ok_or_else(|| Error::new_spanned(&self_ty, "expected single-ident type"))?
            .clone(),
        _ => return Err(Error::new_spanned(&item_impl, "expected path-like type")),
    };

    let mut return_ty = None;
    for item in items.iter() {
        match item {
            syn::ImplItem::Method(syn::ImplItemMethod {
                sig:
                    syn::Signature {
                        ident,
                        output: syn::ReturnType::Type(_, ty),
                        ..
                    },
                ..
            }) if ident == "run_analysis" => return_ty = Some(ty.clone()),
            _ => continue,
        }
    }

    // preprend the crate name to the analysis name, to avoid symbol resolution conflicts
    let analysis_id = analysis_ident.to_string().to_uppercase();
    let crate_name = std::env::var("CARGO_CRATE_NAME").expect("CARGO_CRATE_NAME not set");
    let analysis_id = format_ident!("{}_{}", crate_name.to_uppercase(), analysis_id);

    items.push(parse_quote! {
        type Result = #return_ty;
    });
    items.push(parse_quote! {
        fn id() -> llvm_plugin::AnalysisKey {
            &#analysis_id as *const u8 as *mut _
        }
    });

    let entrypoint = format_ident!("{}_entrypoint", analysis_ident.to_string().to_lowercase());

    module_items.push(parse_quote! {
        extern "C" fn #entrypoint(
            function: *mut std::ffi::c_void,
            manager: *mut std::ffi::c_void,
            result: *mut *mut std::ffi::c_void,
            deleter: *mut extern "C" fn(*mut std::ffi::c_void),
        ) {
            use llvm_plugin::LlvmFunctionAnalysis;
            let mut function = unsafe { llvm_plugin::inkwell::values::FunctionValue::new(function.cast()).unwrap() };
            let analysis = #self_ty::default(); // FIXME
            let manager = unsafe { llvm_plugin::FunctionAnalysisManager::from_raw(manager, Some(#self_ty::id())) };
            let data = analysis.run_analysis(&mut function, &manager);
            let data = Box::new(data);
            extern "C" fn free(data: *mut std::ffi::c_void) {
                drop(unsafe { Box::<<#self_ty as LlvmFunctionAnalysis>::Result>::from_raw(data.cast()) })
            }
            unsafe {
                *result = Box::<<#self_ty as LlvmFunctionAnalysis>::Result>::into_raw(data).cast();
                *deleter = free;
            }
            std::mem::forget(function);
        }
    });

    register_snippets.push(quote! {
        use llvm_plugin::LlvmFunctionAnalysis;
        unsafe { llvm_plugin::register_function_analysis__(#self_ty::id(), #entrypoint) };
    });

    Ok(analysis_id)
}

fn matches_path(path: &syn::Path, pattern: &[&str]) -> bool {
    !path
        .segments
        .iter()
        .rev()
        .peekable()
        .zip(pattern.iter().rev())
        .any(|(seg, s)| seg.ident != s)
}