Skip to content

Commit e8192bb

Browse files
committed
transpile: compute num_ty_params from base_name instead of specifying at call sites
This removes a bit of duplication, but also centralizes this logic, which is much easier to follow when it's all together.
1 parent 08741b5 commit e8192bb

2 files changed

Lines changed: 17 additions & 16 deletions

File tree

c2rust-transpile/src/translator/atomics.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,15 @@ impl<'c> Translation<'c> {
4949
fn atomic_intrinsic_expr_edition_2024(
5050
&self,
5151
base_name: &str,
52-
num_ty_params: usize,
5352
orders: &[Ordering],
5453
) -> Box<Expr> {
54+
let num_ty_params = match base_name {
55+
"fence" => 0,
56+
"load" | "store" | "xchg" | "cxchg" | "cxchgweak" => 1,
57+
"xadd" | "xsub" | "or" | "xor" | "nand" | "and" => 2,
58+
_ => unimplemented!("unknown atomic intrinsic: {base_name}"),
59+
};
60+
5561
let args = repeat_n("_".to_owned(), num_ty_params)
5662
.chain(orders.iter().map(|&order| {
5763
let order = order_ty_name(order);
@@ -66,12 +72,7 @@ impl<'c> Translation<'c> {
6672
Box::new(expr)
6773
}
6874

69-
pub fn atomic_intrinsic_expr(
70-
&self,
71-
base_name: &str,
72-
num_ty_params: usize,
73-
orders: &[Ordering],
74-
) -> Box<Expr> {
75+
pub fn atomic_intrinsic_expr(&self, base_name: &str, orders: &[Ordering]) -> Box<Expr> {
7576
assert!(matches!(
7677
orders,
7778
&[_ /* order */] | &[_ /* order_succ */, _ /* order_fail */]
@@ -81,7 +82,7 @@ impl<'c> Translation<'c> {
8182
if self.tcfg.edition < Edition2024 {
8283
self.atomic_intrinsic_expr_edition_2021(base_name, orders)
8384
} else {
84-
self.atomic_intrinsic_expr_edition_2024(base_name, num_ty_params, orders)
85+
self.atomic_intrinsic_expr_edition_2024(base_name, orders)
8586
}
8687
}
8788

@@ -92,7 +93,7 @@ impl<'c> Translation<'c> {
9293
order_fail: Ordering,
9394
) -> Box<Expr> {
9495
let base = if weak { "cxchgweak" } else { "cxchg" };
95-
self.atomic_intrinsic_expr(base, 1, &[order_succ, order_fail])
96+
self.atomic_intrinsic_expr(base, &[order_succ, order_fail])
9697
}
9798

9899
fn convert_constant_bool(&self, expr: CExprId) -> Option<bool> {
@@ -165,7 +166,7 @@ impl<'c> Translation<'c> {
165166
"__atomic_load" | "__atomic_load_n" | "__c11_atomic_load" => ptr.and_then(|ptr| {
166167
let order = static_order(order);
167168

168-
let atomic_load = self.atomic_intrinsic_expr("load", 1, &[order]);
169+
let atomic_load = self.atomic_intrinsic_expr("load", &[order]);
169170
let call = mk().call_expr(atomic_load, vec![ptr]);
170171
if name == "__atomic_load" {
171172
let ret = val1.expect("__atomic_load should have a ret argument");
@@ -194,7 +195,7 @@ impl<'c> Translation<'c> {
194195
let val = val1.expect("__atomic_store must have a val argument");
195196
ptr.and_then(|ptr| {
196197
val.and_then(|val| {
197-
let atomic_store = self.atomic_intrinsic_expr("store", 1, &[order]);
198+
let atomic_store = self.atomic_intrinsic_expr("store", &[order]);
198199
let val = if name == "__atomic_store" {
199200
mk().unary_expr(UnOp::Deref(Default::default()), val)
200201
} else {
@@ -233,7 +234,7 @@ impl<'c> Translation<'c> {
233234
let val = val1.expect("__atomic_store must have a val argument");
234235
ptr.and_then(|ptr| {
235236
val.and_then(|val| {
236-
let fn_path = self.atomic_intrinsic_expr("xchg", 1, &[order]);
237+
let fn_path = self.atomic_intrinsic_expr("xchg", &[order]);
237238
let val = if name == "__atomic_exchange" {
238239
mk().unary_expr(UnOp::Deref(Default::default()), val)
239240
} else {
@@ -425,7 +426,7 @@ impl<'c> Translation<'c> {
425426
fetch_first: bool,
426427
) -> TranslationResult<WithStmts<Box<Expr>>> {
427428
// Emit `atomic_func(a0, a1) (op a1)?`
428-
let atomic_func = self.atomic_intrinsic_expr(base_name, 2, &[order]);
429+
let atomic_func = self.atomic_intrinsic_expr(base_name, &[order]);
429430

430431
if fetch_first {
431432
let call_expr = mk().call_expr(atomic_func, vec![dst, src]);

c2rust-transpile/src/translator/builtins.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ impl<'c> Translation<'c> {
643643
}
644644

645645
"__sync_synchronize" => {
646-
let atomic_func = self.atomic_intrinsic_expr("fence", 0, &[SeqCst]);
646+
let atomic_func = self.atomic_intrinsic_expr("fence", &[SeqCst]);
647647
let call_expr = mk().call_expr(atomic_func, vec![]);
648648
self.convert_side_effects_expr(
649649
ctx,
@@ -658,7 +658,7 @@ impl<'c> Translation<'c> {
658658
| "__sync_lock_test_and_set_8"
659659
| "__sync_lock_test_and_set_16" => {
660660
// Emit `atomic_xchg_acquire(arg0, arg1)`
661-
let atomic_func = self.atomic_intrinsic_expr("xchg", 1, &[Acquire]);
661+
let atomic_func = self.atomic_intrinsic_expr("xchg", &[Acquire]);
662662
let arg0 = self.convert_expr(ctx.used(), args[0], None)?;
663663
let arg1 = self.convert_expr(ctx.used(), args[1], None)?;
664664
arg0.and_then(|arg0| {
@@ -679,7 +679,7 @@ impl<'c> Translation<'c> {
679679
| "__sync_lock_release_8"
680680
| "__sync_lock_release_16" => {
681681
// Emit `atomic_store_release(arg0, 0)`
682-
let atomic_func = self.atomic_intrinsic_expr("store", 1, &[Release]);
682+
let atomic_func = self.atomic_intrinsic_expr("store", &[Release]);
683683
let arg0 = self.convert_expr(ctx.used(), args[0], None)?;
684684
arg0.and_then(|arg0| {
685685
let zero = mk().lit_expr(mk().int_lit(0, ""));

0 commit comments

Comments
 (0)