Skip to content

Commit e0c34d3

Browse files
committed
Move some code to a new file
1 parent 9b4aca6 commit e0c34d3

File tree

2 files changed

+138
-132
lines changed

2 files changed

+138
-132
lines changed

gen_intrinsics/src/def_visitor.rs

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
use syn::parse::Parser;
2+
use syn::visit::Visit;
3+
use syn::Ident;
4+
5+
pub struct DefVisitor {
6+
pub llvm_intrinsics: Vec<LlvmIntrinsicDef>,
7+
pub structs: Vec<syn::ItemStruct>,
8+
pub aliases: Vec<syn::ItemType>,
9+
}
10+
11+
pub struct LlvmIntrinsicDef {
12+
pub abi: String,
13+
pub link_name: String,
14+
pub sig: syn::Signature,
15+
}
16+
17+
impl<'ast> Visit<'ast> for DefVisitor {
18+
fn visit_item_struct(&mut self, i: &'ast syn::ItemStruct) {
19+
let Some(repr_attr) = i.attrs.iter().find(|attr| attr.path().is_ident("repr")) else {
20+
return;
21+
};
22+
23+
if !repr_attr
24+
.parse_args::<syn::Ident>()
25+
.map_or(false, |repr| repr.to_owned() == "simd" || repr.to_owned() == "C")
26+
{
27+
return;
28+
}
29+
30+
let mut ty = i.clone();
31+
ty.attrs = ty.attrs.into_iter().filter(|attr| attr.path().is_ident("repr")).collect();
32+
33+
self.structs.push(ty);
34+
}
35+
36+
fn visit_item_type(&mut self, i: &'ast syn::ItemType) {
37+
let mut alias = i.clone();
38+
alias.attrs = alias.attrs.into_iter().filter(|attr| attr.path().is_ident("repr")).collect();
39+
40+
self.aliases.push(alias);
41+
}
42+
43+
fn visit_item_foreign_mod(&mut self, i: &'ast syn::ItemForeignMod) {
44+
let abi = i.abi.name.as_ref().unwrap().value();
45+
46+
'items: for item in &i.items {
47+
match &item {
48+
syn::ForeignItem::Fn(i) => {
49+
let link_name_attr =
50+
i.attrs.iter().find(|attr| attr.path().is_ident("link_name")).unwrap();
51+
52+
let link_name =
53+
match link_name_attr.meta.require_name_value().unwrap().value.clone() {
54+
syn::Expr::Lit(syn::ExprLit {
55+
lit: syn::Lit::Str(link_name), ..
56+
}) => link_name.value(),
57+
_ => unreachable!(),
58+
};
59+
60+
assert!(
61+
i.attrs
62+
.iter()
63+
.filter(|attr| !attr.path().is_ident("link_name"))
64+
.collect::<Vec<_>>()
65+
.is_empty()
66+
);
67+
68+
let mut sig = i.sig.clone();
69+
70+
if link_name == "llvm.x86.avx512.mask.cvtss2sd.round" {
71+
match sig.inputs.iter_mut().nth(1).unwrap() {
72+
syn::FnArg::Typed(syn::PatType { ref mut pat, .. }) => match &mut **pat
73+
{
74+
syn::Pat::Ident(name) => {
75+
name.ident = Ident::new("b", name.ident.span());
76+
}
77+
_ => unreachable!(),
78+
},
79+
_ => unreachable!(),
80+
}
81+
}
82+
83+
// FIXME remove this patching
84+
match sig.inputs.iter_mut().nth(0) {
85+
Some(syn::FnArg::Typed(syn::PatType { ref mut pat, .. })) => {
86+
match &mut **pat {
87+
syn::Pat::Wild(_) => {
88+
**pat =
89+
syn::Pat::parse_single.parse2(quote::quote! { a }).unwrap();
90+
}
91+
_ => {}
92+
}
93+
}
94+
_ => {}
95+
}
96+
97+
// FIXME remove this skipping
98+
match &*link_name {
99+
_ if link_name.starts_with("llvm.aarch64.neon.ld") => continue 'items,
100+
_ if link_name.starts_with("llvm.aarch64.neon.st") => continue 'items,
101+
_ if link_name.starts_with("llvm.aarch64.neon.rshrn") => continue 'items,
102+
_ if link_name.starts_with("llvm.aarch64.neon.sq") => continue 'items,
103+
_ if link_name.starts_with("llvm.aarch64.neon.uq") => continue 'items,
104+
_ if link_name.starts_with("llvm.aarch64.neon.vcvt") => continue 'items,
105+
_ if link_name.starts_with("llvm.aarch64.neon.vsli") => continue 'items,
106+
_ if link_name.starts_with("llvm.aarch64.neon.vsri") => continue 'items,
107+
108+
"llvm.prefetch"
109+
| "llvm.aarch64.dmb"
110+
| "llvm.aarch64.dsb"
111+
| "llvm.aarch64.hint"
112+
| "llvm.aarch64.isb"
113+
| "llvm.aarch64.crypto.xar" => continue 'items,
114+
115+
"llvm.aarch64.crypto.sm3tt1a"
116+
| "llvm.aarch64.crypto.sm3tt1b"
117+
| "llvm.aarch64.crypto.sm3tt2a"
118+
| "llvm.aarch64.crypto.sm3tt2b"
119+
| "llvm.aarch64.tcancel" => continue 'items,
120+
_ => {}
121+
}
122+
123+
self.llvm_intrinsics.push(LlvmIntrinsicDef {
124+
abi: abi.clone(),
125+
link_name,
126+
sig,
127+
});
128+
}
129+
_ => {}
130+
}
131+
}
132+
}
133+
}

gen_intrinsics/src/main.rs

Lines changed: 5 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
mod def_visitor;
2+
13
use std::io::Write;
24
use std::process::Stdio;
35

4-
use syn::parse::Parser;
56
use syn::visit::Visit;
67
use syn::Ident;
78

9+
use crate::def_visitor::{DefVisitor, LlvmIntrinsicDef};
10+
811
fn main() {
912
println!("Running rustc -Zunpretty=expanded --edition=2021 core_arch/src/lib.rs ...");
1013
let expanded_file = std::process::Command::new("rustc")
@@ -20,7 +23,7 @@ fn main() {
2023
let file = syn::parse_str::<syn::File>(std::str::from_utf8(&expanded_file).unwrap()).unwrap();
2124

2225
println!("Visting all LLVM intrinsics");
23-
let mut visitor = Visitor { llvm_intrinsics: vec![], structs: vec![], aliases: vec![] };
26+
let mut visitor = DefVisitor { llvm_intrinsics: vec![], structs: vec![], aliases: vec![] };
2427
visitor.visit_file(&file);
2528

2629
println!();
@@ -102,133 +105,3 @@ fn main() {
102105
let status = child.wait().unwrap();
103106
assert!(status.success(), "{status}");
104107
}
105-
106-
struct Visitor {
107-
llvm_intrinsics: Vec<LlvmIntrinsicDef>,
108-
structs: Vec<syn::ItemStruct>,
109-
aliases: Vec<syn::ItemType>,
110-
}
111-
112-
struct LlvmIntrinsicDef {
113-
abi: String,
114-
link_name: String,
115-
sig: syn::Signature,
116-
}
117-
118-
impl<'ast> Visit<'ast> for Visitor {
119-
fn visit_item_struct(&mut self, i: &'ast syn::ItemStruct) {
120-
let Some(repr_attr) = i.attrs.iter().find(|attr| attr.path().is_ident("repr")) else {
121-
return;
122-
};
123-
124-
if !repr_attr
125-
.parse_args::<syn::Ident>()
126-
.map_or(false, |repr| repr.to_owned() == "simd" || repr.to_owned() == "C")
127-
{
128-
return;
129-
}
130-
131-
let mut ty = i.clone();
132-
ty.attrs = ty.attrs.into_iter().filter(|attr| attr.path().is_ident("repr")).collect();
133-
134-
self.structs.push(ty);
135-
}
136-
137-
fn visit_item_type(&mut self, i: &'ast syn::ItemType) {
138-
let mut alias = i.clone();
139-
alias.attrs = alias.attrs.into_iter().filter(|attr| attr.path().is_ident("repr")).collect();
140-
141-
self.aliases.push(alias);
142-
}
143-
144-
fn visit_item_foreign_mod(&mut self, i: &'ast syn::ItemForeignMod) {
145-
let abi = i.abi.name.as_ref().unwrap().value();
146-
147-
'items: for item in &i.items {
148-
match &item {
149-
syn::ForeignItem::Fn(i) => {
150-
let link_name_attr =
151-
i.attrs.iter().find(|attr| attr.path().is_ident("link_name")).unwrap();
152-
153-
let link_name =
154-
match link_name_attr.meta.require_name_value().unwrap().value.clone() {
155-
syn::Expr::Lit(syn::ExprLit {
156-
lit: syn::Lit::Str(link_name), ..
157-
}) => link_name.value(),
158-
_ => unreachable!(),
159-
};
160-
161-
assert!(
162-
i.attrs
163-
.iter()
164-
.filter(|attr| !attr.path().is_ident("link_name"))
165-
.collect::<Vec<_>>()
166-
.is_empty()
167-
);
168-
169-
let mut sig = i.sig.clone();
170-
171-
if link_name == "llvm.x86.avx512.mask.cvtss2sd.round" {
172-
match sig.inputs.iter_mut().nth(1).unwrap() {
173-
syn::FnArg::Typed(syn::PatType { ref mut pat, .. }) => match &mut **pat
174-
{
175-
syn::Pat::Ident(name) => {
176-
name.ident = Ident::new("b", name.ident.span());
177-
}
178-
_ => unreachable!(),
179-
},
180-
_ => unreachable!(),
181-
}
182-
}
183-
184-
// FIXME remove this patching
185-
match sig.inputs.iter_mut().nth(0) {
186-
Some(syn::FnArg::Typed(syn::PatType { ref mut pat, .. })) => {
187-
match &mut **pat {
188-
syn::Pat::Wild(_) => {
189-
**pat =
190-
syn::Pat::parse_single.parse2(quote::quote! { a }).unwrap();
191-
}
192-
_ => {}
193-
}
194-
}
195-
_ => {}
196-
}
197-
198-
// FIXME remove this skipping
199-
match &*link_name {
200-
_ if link_name.starts_with("llvm.aarch64.neon.ld") => continue 'items,
201-
_ if link_name.starts_with("llvm.aarch64.neon.st") => continue 'items,
202-
_ if link_name.starts_with("llvm.aarch64.neon.rshrn") => continue 'items,
203-
_ if link_name.starts_with("llvm.aarch64.neon.sq") => continue 'items,
204-
_ if link_name.starts_with("llvm.aarch64.neon.uq") => continue 'items,
205-
_ if link_name.starts_with("llvm.aarch64.neon.vcvt") => continue 'items,
206-
_ if link_name.starts_with("llvm.aarch64.neon.vsli") => continue 'items,
207-
_ if link_name.starts_with("llvm.aarch64.neon.vsri") => continue 'items,
208-
209-
"llvm.prefetch"
210-
| "llvm.aarch64.dmb"
211-
| "llvm.aarch64.dsb"
212-
| "llvm.aarch64.hint"
213-
| "llvm.aarch64.isb"
214-
| "llvm.aarch64.crypto.xar" => continue 'items,
215-
216-
"llvm.aarch64.crypto.sm3tt1a"
217-
| "llvm.aarch64.crypto.sm3tt1b"
218-
| "llvm.aarch64.crypto.sm3tt2a"
219-
| "llvm.aarch64.crypto.sm3tt2b"
220-
| "llvm.aarch64.tcancel" => continue 'items,
221-
_ => {}
222-
}
223-
224-
self.llvm_intrinsics.push(LlvmIntrinsicDef {
225-
abi: abi.clone(),
226-
link_name,
227-
sig,
228-
});
229-
}
230-
_ => {}
231-
}
232-
}
233-
}
234-
}

0 commit comments

Comments
 (0)