Skip to content

Commit 5fa1792

Browse files
committed
[WIP]
1 parent 224fd68 commit 5fa1792

File tree

2 files changed

+69
-15
lines changed

2 files changed

+69
-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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
11
//! Emulate AArch64 LLVM intrinsics
22
3+
use cranelift_codegen::isa::CallConv;
34
use rustc_ast::ast::{InlineAsmOptions, InlineAsmTemplatePiece};
45
use rustc_target::asm::*;
56

67
use crate::inline_asm::{CInlineAsmOperand, codegen_inline_asm_inner};
78
use crate::intrinsics::*;
89
use crate::prelude::*;
910

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

478+
// ==== begin autogenerated section ====
479+
"llvm.trunc.v1f64" => {
480+
intrinsic_args!(fx, args => (a); intrinsic);
481+
call_asm(fx, "llvm__trunc__v1f64", &[a], ret, &[0, 192, 101, 30]);
482+
}
483+
"llvm.trunc.v2f32" => {
484+
intrinsic_args!(fx, args => (a); intrinsic);
485+
call_asm(fx, "llvm__trunc__v2f32", &[a], ret, &[0, 152, 161, 14]);
486+
}
487+
"llvm.trunc.v2f64" => {
488+
intrinsic_args!(fx, args => (a); intrinsic);
489+
call_asm(fx, "llvm__trunc__v2f64", &[a], ret, &[0, 152, 225, 78]);
490+
}
491+
"llvm.trunc.v4f32" => {
492+
intrinsic_args!(fx, args => (a); intrinsic);
493+
call_asm(fx, "llvm__trunc__v4f32", &[a], ret, &[0, 152, 161, 78]);
494+
}
495+
// ==== end autogenerated section
496+
455497
/*
456498
_ if intrinsic.starts_with("llvm.aarch64.neon.sshl.v")
457499
|| intrinsic.starts_with("llvm.aarch64.neon.sqshl.v")

0 commit comments

Comments
 (0)