Skip to content

Commit 4741f64

Browse files
committed
[WIP]
1 parent cf591d0 commit 4741f64

File tree

2 files changed

+70
-15
lines changed

2 files changed

+70
-15
lines changed

gen_intrinsics/src/main.rs

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -99,32 +99,44 @@ fn main() {
9999
let imports = obj.symbols().filter(|sym| sym.is_undefined()).collect::<Vec<_>>();
100100
assert!(imports.is_empty(), "{imports:?}");
101101

102-
for section in obj.sections() {
103-
let section_name = section.name().unwrap();
104-
if !section_name.starts_with(".text") {
105-
continue;
106-
}
102+
for LlvmIntrinsicDef { abi: _, link_name, sig } in &visitor.llvm_intrinsics {
103+
let section_name = format!(".text.__rust_cranelift_{}", link_name.replace('.', "__"));
104+
let section = obj.section_by_name(&section_name).unwrap();
107105

108-
if section_name == ".text" {
109-
assert_eq!(section.size(), 0);
110-
continue;
111-
}
112-
113-
let name = section_name.strip_prefix(".text.__rust_cranelift_").unwrap().replace("__", ".");
106+
assert_ne!(section.size(), 0);
114107

115108
// Sanity checks
116-
assert!(section.relocations().next().is_none(), "function {name} has relocations");
109+
assert!(section.relocations().next().is_none(), "function {link_name} has relocations");
117110
assert!(
118111
section.size() <= 0x14,
119-
"function {name} is too big. it is {} bytes",
112+
"function {link_name} is too big. it is {} bytes",
120113
section.size(),
121114
);
122115

123116
let data = section.data().unwrap();
124117
let (code, ret) = data.split_at(data.len() - 4);
125118
assert_eq!(ret, [0xc0_u8, 0x03, 0x5f, 0xd6]); // arm64 ret instruction
126-
println!(" \"{name}\" => {{");
127-
println!(" {:x?}", code);
119+
120+
let args = sig
121+
.inputs
122+
.iter()
123+
.map(|arg| match arg {
124+
syn::FnArg::Typed(syn::PatType { pat, .. }) => match &**pat {
125+
syn::Pat::Ident(ident) => ident.ident.to_string(),
126+
_ => unreachable!("{pat:?}"),
127+
},
128+
_ => unreachable!(),
129+
})
130+
.collect::<Vec<_>>();
131+
132+
println!(" \"{link_name}\" => {{");
133+
println!(" intrinsic_args!(fx, args => ({}); intrinsic);", args.join(", "));
134+
println!(
135+
" call_asm(fx, \"{}\", &[{}], ret, &{:?});",
136+
link_name.replace('.', "__"),
137+
args.join(", "),
138+
code
139+
);
128140
println!(" }}");
129141
}
130142
}

src/intrinsics/llvm_aarch64.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,32 @@
11
//! Emulate AArch64 LLVM intrinsics
22
3+
use cranelift_codegen::isa::CallConv;
4+
35
use crate::intrinsics::*;
46
use crate::prelude::*;
57

8+
fn call_asm<'tcx>(
9+
fx: &mut FunctionCx<'_, '_, 'tcx>,
10+
name: &str,
11+
args: &[CValue<'tcx>],
12+
ret: CPlace<'tcx>,
13+
code: &[u8],
14+
) {
15+
let name = format!("__rust_cranelift_{name}");
16+
let is_defined = fx.module.declarations().get_name(&name).is_none();
17+
18+
let sig = Signature { params: todo!(), returns: todo!(), call_conv: CallConv::SystemV };
19+
20+
let func = fx.module.declare_function(&name, Linkage::Local, &sig).unwrap();
21+
if !is_defined {
22+
todo!("define");
23+
}
24+
25+
let func_ref = fx.module.declare_func_in_func(func, &mut fx.bcx.func);
26+
let res = fx.bcx.ins().call(func_ref, &args.into_iter().map(|_| todo!()).collect::<Vec<_>>());
27+
todo!("write result")
28+
}
29+
630
pub(crate) fn codegen_aarch64_llvm_intrinsic_call<'tcx>(
731
fx: &mut FunctionCx<'_, '_, 'tcx>,
832
intrinsic: &str,
@@ -309,6 +333,25 @@ pub(crate) fn codegen_aarch64_llvm_intrinsic_call<'tcx>(
309333
}
310334
}
311335

336+
// ==== begin autogenerated section ====
337+
"llvm.trunc.v1f64" => {
338+
intrinsic_args!(fx, args => (a); intrinsic);
339+
call_asm(fx, "llvm__trunc__v1f64", &[a], ret, &[0, 192, 101, 30]);
340+
}
341+
"llvm.trunc.v2f32" => {
342+
intrinsic_args!(fx, args => (a); intrinsic);
343+
call_asm(fx, "llvm__trunc__v2f32", &[a], ret, &[0, 152, 161, 14]);
344+
}
345+
"llvm.trunc.v2f64" => {
346+
intrinsic_args!(fx, args => (a); intrinsic);
347+
call_asm(fx, "llvm__trunc__v2f64", &[a], ret, &[0, 152, 225, 78]);
348+
}
349+
"llvm.trunc.v4f32" => {
350+
intrinsic_args!(fx, args => (a); intrinsic);
351+
call_asm(fx, "llvm__trunc__v4f32", &[a], ret, &[0, 152, 161, 78]);
352+
}
353+
// ==== end autogenerated section
354+
312355
/*
313356
_ if intrinsic.starts_with("llvm.aarch64.neon.sshl.v")
314357
|| intrinsic.starts_with("llvm.aarch64.neon.sqshl.v")

0 commit comments

Comments
 (0)