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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
#![deny(missing_docs)]
//! Implementations of clang's builtin functions

use super::*;

impl<'c> Translation<'c> {
    /// Convert a call to a builtin function to a Rust expression
    pub fn convert_builtin(
        &self,
        ctx: ExprContext,
        fexp: CExprId,
        args: &[CExprId],
    ) -> Result<WithStmts<P<Expr>>, TranslationError> {
        let expr = &self.ast_context[fexp];
        let decl_id = match expr.kind {
            CExprKind::DeclRef(_, decl_id, _) => decl_id,
            _ => {
                return Err(TranslationError::generic(
                    "Expected declref when processing builtin",
                ))
            }
        };

        let builtin_name: &str = match self.ast_context[decl_id].kind {
            CDeclKind::Function { ref name, .. } => name,
            _ => {
                return Err(TranslationError::generic(
                    "Expected function when processing builtin",
                ))
            }
        };
        let std_or_core = if self.tcfg.emit_no_std { "core" } else { "std" };

        match builtin_name {
            "__builtin_huge_valf" => Ok(WithStmts::new(mk().path_expr(vec![
                "",
                std_or_core,
                "f32",
                "INFINITY",
            ]))),
            "__builtin_huge_val" | "__builtin_huge_vall" => {
                Ok(WithStmts::new(mk().path_expr(vec![
                    "",
                    std_or_core,
                    "f64",
                    "INFINITY",
                ])))
            }
            "__builtin_inff" => Ok(WithStmts::new(mk().path_expr(vec![
                "",
                std_or_core,
                "f32",
                "INFINITY",
            ]))),
            "__builtin_inf" | "__builtin_infl" => Ok(WithStmts::new(mk().path_expr(vec![
                "",
                std_or_core,
                "f64",
                "INFINITY",
            ]))),
            "__builtin_nanf" => Ok(WithStmts::new(mk().path_expr(vec![
                "",
                std_or_core,
                "f32",
                "NAN",
            ]))),
            "__builtin_nan" => Ok(WithStmts::new(mk().path_expr(vec![
                "",
                std_or_core,
                "f64",
                "NAN",
            ]))),
            "__builtin_clz" | "__builtin_clzl" | "__builtin_clzll" => {
                let val = self.convert_expr(ctx.used(), args[0])?;
                Ok(val.map(|x| {
                    let zeros = mk().method_call_expr(x, "leading_zeros", vec![] as Vec<P<Expr>>);
                    mk().cast_expr(zeros, mk().path_ty(vec!["i32"]))
                }))
            }
            "__builtin_ctz" | "__builtin_ctzl" | "__builtin_ctzll" => {
                let val = self.convert_expr(ctx.used(), args[0])?;
                Ok(val.map(|x| {
                    let zeros = mk().method_call_expr(x, "trailing_zeros", vec![] as Vec<P<Expr>>);
                    mk().cast_expr(zeros, mk().path_ty(vec!["i32"]))
                }))
            }
            "__builtin_bswap16" | "__builtin_bswap32" | "__builtin_bswap64" => {
                let val = self.convert_expr(ctx.used(), args[0])?;
                Ok(val.map(|x| mk().method_call_expr(x, "swap_bytes", vec![] as Vec<P<Expr>>)))
            }
            "__builtin_fabs" | "__builtin_fabsf" | "__builtin_fabsl" => {
                let val = self.convert_expr(ctx.used(), args[0])?;
                Ok(val.map(|x| mk().method_call_expr(x, "abs", vec![] as Vec<P<Expr>>)))
            }
            "__builtin_expect" => self.convert_expr(ctx.used(), args[0]),

            "__builtin_popcount" | "__builtin_popcountl" | "__builtin_popcountll" => {
                let val = self.convert_expr(ctx.used(), args[0])?;
                Ok(val.map(|x| {
                    let zeros = mk().method_call_expr(x, "count_ones", vec![] as Vec<P<Expr>>);
                    mk().cast_expr(zeros, mk().path_ty(vec!["i32"]))
                }))
            }
            "__builtin_bzero" => {
                let ptr_stmts = self.convert_expr(ctx.used(), args[0])?;
                let n_stmts = self.convert_expr(ctx.used(), args[1])?;
                let write_bytes = mk().path_expr(vec!["", "std", "ptr", "write_bytes"]);
                let zero = mk().lit_expr(mk().int_lit(0, "u8"));
                Ok(ptr_stmts.and_then(|ptr| {
                    n_stmts.map(|n| mk().call_expr(write_bytes, vec![ptr, zero, n]))
                }))
            }

            // If the target does not support data prefetch, the address expression is evaluated if
            // it includes side effects but no other code is generated and GCC does not issue a warning.
            // void __builtin_prefetch (const void *addr, ...);
            "__builtin_prefetch" => self.convert_expr(ctx.unused(), args[0]),

            "__builtin_memcpy" => self.convert_memcpy(ctx, args),

            "__builtin_add_overflow"
            | "__builtin_sadd_overflow"
            | "__builtin_saddl_overflow"
            | "__builtin_saddll_overflow"
            | "__builtin_uadd_overflow"
            | "__builtin_uaddl_overflow"
            | "__builtin_uaddll_overflow" => {
                self.convert_overflow_arith(ctx, "overflowing_add", args)
            }

            "__builtin_sub_overflow"
            | "__builtin_ssub_overflow"
            | "__builtin_ssubl_overflow"
            | "__builtin_ssubll_overflow"
            | "__builtin_usub_overflow"
            | "__builtin_usubl_overflow"
            | "__builtin_usubll_overflow" => {
                self.convert_overflow_arith(ctx, "overflowing_sub", args)
            }

            "__builtin_mul_overflow"
            | "__builtin_smul_overflow"
            | "__builtin_smull_overflow"
            | "__builtin_smulll_overflow"
            | "__builtin_umul_overflow"
            | "__builtin_umull_overflow"
            | "__builtin_umulll_overflow" => {
                self.convert_overflow_arith(ctx, "overflowing_mul", args)
            }

            // Should be safe to always return 0 here.  "A return of 0 does not indicate that the
            // value is *not* a constant, but merely that GCC cannot prove it is a constant with
            // the specified value of the -O option. "
            "__builtin_constant_p" => Ok(WithStmts::new(mk().lit_expr(mk().int_lit(0, "")))),

            "__builtin_va_start" => {
                if ctx.is_unused() && args.len() == 2 {
                    if let Some(va_id) = self.match_vastart(args[0]) {
                        if self.is_promoted_va_decl(va_id) {
                            // `va_start` is automatically called for the promoted decl.
                            return Ok(WithStmts::new(self.panic_or_err("va_start stub")));
                        }
                    }
                }
                Err(TranslationError::generic("Unsupported va_start"))
            }
            "__builtin_va_copy" => {
                // We are waiting on raw va_copy support to land in rustc:
                // https://github.com/rust-lang/rust/pull/59625
                Err(TranslationError::new(
                    &expr.loc,
                    Context::new(TranslationErrorKind::VaCopyNotImplemented)
                ))
                // if ctx.is_unused() && args.len() == 2 {
                //     if let Some((_dst_va_id, _src_va_id)) = self.match_vacopy(args[0], args[1]) {
                //         let dst = self.convert_expr(ctx.used(), args[0])?;
                //         let src = self.convert_expr(ctx.used(), args[1])?;

                //         let path = {
                //             let std_or_core = if self.tcfg.emit_no_std { "core" } else { "std" };
                //             let path = vec!["", std_or_core, "intrinsics", "va_copy"];
                //             mk().path_expr(path)
                //         };
                //         let mut_ref_src = mk().mutbl().addr_of_expr(src.val);
                //         let call_expr = mk().call_expr(path, vec![mut_ref_src] as Vec<P<Expr>>);
                //         let assign_expr = mk().assign_expr(dst.val, call_expr);
                //         let stmt = mk().semi_stmt(assign_expr);

                //         let mut res = WithStmts::new(self.panic_or_err("va_copy stub"));
                //         res.stmts.push(stmt);
                //         return Ok(res);
                //     }
                // }
                // Err(TranslationError::generic("Unsupported va_copy"))
            }
            "__builtin_va_end" => {
                if ctx.is_unused() && args.len() == 1 {
                    if let Some(va_id) = self.match_vaend(args[0]) {
                        if self.is_promoted_va_decl(va_id) {
                            // no need to call end on `va_end` on `va_list` promoted to arg
                            return Ok(WithStmts::new(self.panic_or_err("va_end stub")));
                        } else if self.is_copied_va_decl(va_id) {
                            // call to `va_end` on non-promoted `va_list`

                            let val = self.convert_expr(ctx.used(), args[0])?;

                            let path = {
                                let std_or_core =
                                    if self.tcfg.emit_no_std { "core" } else { "std" };
                                let path = vec!["", std_or_core, "intrinsics", "va_end"];
                                mk().path_expr(path)
                            };
                            let ref_val = mk().mutbl().addr_of_expr(val.val);
                            let call_expr = mk().call_expr(path, vec![ref_val] as Vec<P<Expr>>);

                            let stmt = mk().semi_stmt(call_expr);

                            let mut res = WithStmts::new(self.panic_or_err("va_end stub"));
                            res.stmts.push(stmt);
                            return Ok(res);

                            // return Ok(WithStmts::new(self.panic("va_end stub")))
                        }
                    }
                }
                Err(TranslationError::generic("Unsupported va_end"))
            }

            "__builtin_alloca" => {
                let count = self.convert_expr(ctx.used(), args[0])?;
                let mut stmts = count.stmts;

                let alloca_name = self.renamer.borrow_mut().fresh();
                let zero_elem = mk().lit_expr(mk().int_lit(0, LitIntType::Unsuffixed));
                stmts.push(mk().local_stmt(P(mk().local(
                    mk().mutbl().ident_pat(&alloca_name),
                    None as Option<P<Ty>>,
                    Some(vec_expr(zero_elem, cast_int(count.val, "usize"))),
                ))));
                Ok(WithStmts {
                    stmts,
                    val: mk().method_call_expr(
                        mk().ident_expr(&alloca_name),
                        "as_mut_ptr",
                        vec![] as Vec<P<Expr>>,
                    ),
                })
            }

            // SIMD builtins:
            "__builtin_ia32_pshufw" => self.convert_simd_builtin(ctx, "_mm_shuffle_pi16", args),
            "__builtin_ia32_shufps" => self.convert_simd_builtin(ctx, "_mm_shuffle_ps", args),
            "__builtin_ia32_shufpd" => self.convert_simd_builtin(ctx, "_mm_shuffle_pd", args),
            "__builtin_ia32_shufps256" => self.convert_simd_builtin(ctx, "_mm256_shuffle_ps", args),
            "__builtin_ia32_shufpd256" => self.convert_simd_builtin(ctx, "_mm256_shuffle_pd", args),
            "__builtin_ia32_pshufd" => self.convert_simd_builtin(ctx, "_mm_shuffle_epi32", args),
            "__builtin_ia32_pshufhw" => self.convert_simd_builtin(ctx, "_mm_shufflehi_epi16", args),
            "__builtin_ia32_pshuflw" => self.convert_simd_builtin(ctx, "_mm_shufflelo_epi16", args),
            "__builtin_ia32_pslldqi128_byteshift" => {
                self.convert_simd_builtin(ctx, "_mm_slli_si128", args)
            }
            "__builtin_ia32_pshufd256" => {
                self.convert_simd_builtin(ctx, "_mm256_shuffle_epi32", args)
            }
            "__builtin_ia32_pshufhw256" => {
                self.convert_simd_builtin(ctx, "_mm256_shufflehi_epi16", args)
            }
            "__builtin_ia32_pshuflw256" => {
                self.convert_simd_builtin(ctx, "_mm256_shufflelo_epi16", args)
            }
            "__builtin_ia32_vec_ext_v4si" => {
                self.convert_simd_builtin(ctx, "_mm_extract_epi32", args)
            }
            "__builtin_ia32_vec_ext_v16qi" => {
                self.convert_simd_builtin(ctx, "_mm_extract_epi8", args)
            }
            "__builtin_ia32_vec_ext_v2di" => {
                self.convert_simd_builtin(ctx, "_mm_extract_epi64", args)
            }
            "__builtin_ia32_roundps" => self.convert_simd_builtin(ctx, "_mm_round_ps", args),
            "__builtin_ia32_roundss" => self.convert_simd_builtin(ctx, "_mm_round_ss", args),
            "__builtin_ia32_roundpd" => self.convert_simd_builtin(ctx, "_mm_round_pd", args),
            "__builtin_ia32_roundsd" => self.convert_simd_builtin(ctx, "_mm_round_sd", args),
            "__builtin_ia32_blendpd" => self.convert_simd_builtin(ctx, "_mm_blend_pd", args),
            "__builtin_ia32_blendps" => self.convert_simd_builtin(ctx, "_mm_blend_ps", args),
            "__builtin_ia32_pblendw128" => self.convert_simd_builtin(ctx, "_mm_blend_epi16", args),
            "__builtin_ia32_dpps" => self.convert_simd_builtin(ctx, "_mm_dp_ps", args),
            "__builtin_ia32_dppd" => self.convert_simd_builtin(ctx, "_mm_dp_pd", args),
            "__builtin_ia32_insertps128" => self.convert_simd_builtin(ctx, "_mm_insert_ps", args),
            "__builtin_ia32_vec_ext_v4sf" => self.convert_simd_builtin(ctx, "_mm_extract_ps", args),
            "__builtin_ia32_vec_set_v16qi" => {
                self.convert_simd_builtin(ctx, "_mm_insert_epi8", args)
            }
            "__builtin_ia32_vec_set_v2di" => {
                self.convert_simd_builtin(ctx, "_mm_insert_epi64", args)
            }
            "__builtin_ia32_mpsadbw128" => self.convert_simd_builtin(ctx, "_mm_mpsadbw_epu8", args),
            "__builtin_ia32_pcmpistrm128" => self.convert_simd_builtin(ctx, "_mm_cmpistrm", args),
            "__builtin_ia32_pcmpistri128" => self.convert_simd_builtin(ctx, "_mm_cmpistri", args),
            "__builtin_ia32_pcmpestrm128" => self.convert_simd_builtin(ctx, "_mm_cmpestrm", args),
            "__builtin_ia32_pcmpistria128" => self.convert_simd_builtin(ctx, "_mm_cmpistra", args),
            "__builtin_ia32_pcmpistric128" => self.convert_simd_builtin(ctx, "_mm_cmpistrc", args),
            "__builtin_ia32_pcmpistrio128" => self.convert_simd_builtin(ctx, "_mm_cmpistro", args),
            "__builtin_ia32_pcmpistris128" => self.convert_simd_builtin(ctx, "_mm_cmpistrs", args),
            "__builtin_ia32_pcmpistriz128" => self.convert_simd_builtin(ctx, "_mm_cmpistrz", args),
            "__builtin_ia32_pcmpestria128" => self.convert_simd_builtin(ctx, "_mm_cmpestra", args),
            "__builtin_ia32_pcmpestric128" => self.convert_simd_builtin(ctx, "_mm_cmpestrc", args),
            "__builtin_ia32_pcmpestrio128" => self.convert_simd_builtin(ctx, "_mm_cmpestro", args),
            "__builtin_ia32_pcmpestris128" => self.convert_simd_builtin(ctx, "_mm_cmpestrs", args),
            "__builtin_ia32_pcmpestriz128" => self.convert_simd_builtin(ctx, "_mm_cmpestrz", args),

            "__sync_val_compare_and_swap_1"
            | "__sync_val_compare_and_swap_2"
            | "__sync_val_compare_and_swap_4"
            | "__sync_val_compare_and_swap_8"
            | "__sync_val_compare_and_swap_16"
            | "__sync_bool_compare_and_swap_1"
            | "__sync_bool_compare_and_swap_2"
            | "__sync_bool_compare_and_swap_4"
            | "__sync_bool_compare_and_swap_8"
            | "__sync_bool_compare_and_swap_16" => {
                self.use_feature("core_intrinsics");

                // Emit `atomic_cxchg(a0, a1, a2).idx`
                let atomic_cxchg =
                    mk().path_expr(vec!["", std_or_core, "intrinsics", "atomic_cxchg"]);
                let arg0 = self.convert_expr(ctx.used(), args[0])?;
                let arg1 = self.convert_expr(ctx.used(), args[1])?;
                let arg2 = self.convert_expr(ctx.used(), args[2])?;
                Ok(arg0.and_then(|arg0| {
                    arg1.and_then(|arg1| {
                        arg2.and_then(|arg2| {
                            let call = mk().call_expr(atomic_cxchg, vec![arg0, arg1, arg2]);
                            let field_idx = if builtin_name.starts_with("__sync_val") {
                                "0"
                            } else {
                                "1"
                            };
                            let call_expr = mk().field_expr(call, field_idx);
                            self.convert_side_effects_expr(
                                ctx,
                                vec![],
                                call_expr,
                                "Builtin is not supposed to be used",
                            )
                        })
                    })
                }))
            }

            "__sync_fetch_and_add_1"
            | "__sync_fetch_and_add_2"
            | "__sync_fetch_and_add_4"
            | "__sync_fetch_and_add_8"
            | "__sync_fetch_and_add_16"
            | "__sync_fetch_and_sub_1"
            | "__sync_fetch_and_sub_2"
            | "__sync_fetch_and_sub_4"
            | "__sync_fetch_and_sub_8"
            | "__sync_fetch_and_sub_16"
            | "__sync_fetch_and_or_1"
            | "__sync_fetch_and_or_2"
            | "__sync_fetch_and_or_4"
            | "__sync_fetch_and_or_8"
            | "__sync_fetch_and_or_16"
            | "__sync_fetch_and_and_1"
            | "__sync_fetch_and_and_2"
            | "__sync_fetch_and_and_4"
            | "__sync_fetch_and_and_8"
            | "__sync_fetch_and_and_16"
            | "__sync_fetch_and_xor_1"
            | "__sync_fetch_and_xor_2"
            | "__sync_fetch_and_xor_4"
            | "__sync_fetch_and_xor_8"
            | "__sync_fetch_and_xor_16"
            | "__sync_fetch_and_nand_1"
            | "__sync_fetch_and_nand_2"
            | "__sync_fetch_and_nand_4"
            | "__sync_fetch_and_nand_8"
            | "__sync_fetch_and_nand_16"
            | "__sync_add_and_fetch_1"
            | "__sync_add_and_fetch_2"
            | "__sync_add_and_fetch_4"
            | "__sync_add_and_fetch_8"
            | "__sync_add_and_fetch_16"
            | "__sync_sub_and_fetch_1"
            | "__sync_sub_and_fetch_2"
            | "__sync_sub_and_fetch_4"
            | "__sync_sub_and_fetch_8"
            | "__sync_sub_and_fetch_16"
            | "__sync_or_and_fetch_1"
            | "__sync_or_and_fetch_2"
            | "__sync_or_and_fetch_4"
            | "__sync_or_and_fetch_8"
            | "__sync_or_and_fetch_16"
            | "__sync_and_and_fetch_1"
            | "__sync_and_and_fetch_2"
            | "__sync_and_and_fetch_4"
            | "__sync_and_and_fetch_8"
            | "__sync_and_and_fetch_16"
            | "__sync_xor_and_fetch_1"
            | "__sync_xor_and_fetch_2"
            | "__sync_xor_and_fetch_4"
            | "__sync_xor_and_fetch_8"
            | "__sync_xor_and_fetch_16"
            | "__sync_nand_and_fetch_1"
            | "__sync_nand_and_fetch_2"
            | "__sync_nand_and_fetch_4"
            | "__sync_nand_and_fetch_8"
            | "__sync_nand_and_fetch_16" => {
                self.use_feature("core_intrinsics");

                let (func_name, binary_op, is_nand) = if builtin_name.contains("_add_") {
                    ("atomic_xadd", BinOpKind::Add, false)
                } else if builtin_name.contains("_sub_") {
                    ("atomic_xsub", BinOpKind::Sub, false)
                } else if builtin_name.contains("_or_") {
                    ("atomic_or", BinOpKind::BitOr, false)
                } else if builtin_name.contains("_xor_") {
                    ("atomic_xor", BinOpKind::BitXor, false)
                } else if builtin_name.contains("_nand_") {
                    ("atomic_nand", BinOpKind::BitAnd, true)
                } else {
                    // We can't explicitly check for "_and_" since they all contain it
                    ("atomic_and", BinOpKind::BitAnd, false)
                };

                // Emit `atomic_func(a0, a1) (op a1)?`
                let atomic_func = mk().path_expr(vec!["", std_or_core, "intrinsics", func_name]);
                let arg0 = self.convert_expr(ctx.used(), args[0])?;
                let arg1 = self.convert_expr(ctx.used(), args[1])?;
                if builtin_name.starts_with("__sync_fetch") {
                    // __sync_fetch_and_XXX
                    Ok(arg0.and_then(|arg0| {
                        arg1.and_then(|arg1| {
                            let call_expr = mk().call_expr(atomic_func, vec![arg0, arg1]);
                            self.convert_side_effects_expr(
                                ctx,
                                vec![],
                                call_expr,
                                "Builtin is not supposed to be used",
                            )
                        })
                    }))
                } else {
                    // __sync_XXX_and_fetch
                    Ok(arg0.and_then(|arg0| {
                        arg1.and_then(|arg1| {
                            // Since the value of `arg1` is used twice, we need to copy
                            // it into a local temporary so we don't duplicate any side-effects
                            // To preserve ordering of side-effects, we also do this for arg0
                            let arg0_name = self.renamer.borrow_mut().fresh();
                            let arg0_let = mk().local_stmt(P(mk().local(
                                mk().ident_pat(&arg0_name),
                                None as Option<P<Ty>>,
                                Some(arg0),
                            )));

                            let arg1_name = self.renamer.borrow_mut().fresh();
                            let arg1_let = mk().local_stmt(P(mk().local(
                                mk().ident_pat(&arg1_name),
                                None as Option<P<Ty>>,
                                Some(arg1),
                            )));

                            let call = mk().call_expr(
                                atomic_func,
                                vec![mk().ident_expr(&arg0_name), mk().ident_expr(&arg1_name)],
                            );
                            let val = mk().binary_expr(binary_op, call, mk().ident_expr(arg1_name));
                            let val = if is_nand {
                                // For nand, return `!(atomic_nand(arg0, arg1) & arg1)`
                                mk().unary_expr(UnOp::Not, val)
                            } else {
                                val
                            };
                            self.convert_side_effects_expr(
                                ctx,
                                vec![arg0_let, arg1_let],
                                val,
                                "Builtin is not supposed to be used",
                            )
                        })
                    }))
                }
            }

            "__sync_synchronize" => {
                self.use_feature("core_intrinsics");

                let atomic_func =
                    mk().path_expr(vec!["", std_or_core, "intrinsics", "atomic_fence"]);
                let call_expr = mk().call_expr(atomic_func, vec![] as Vec<P<Expr>>);
                Ok(self.convert_side_effects_expr(
                    ctx,
                    vec![],
                    call_expr,
                    "Builtin is not supposed to be used",
                ))
            }

            "__sync_lock_test_and_set_1"
            | "__sync_lock_test_and_set_2"
            | "__sync_lock_test_and_set_4"
            | "__sync_lock_test_and_set_8"
            | "__sync_lock_test_and_set_16" => {
                self.use_feature("core_intrinsics");

                // Emit `atomic_xchg_acq(arg0, arg1)`
                let atomic_func =
                    mk().path_expr(vec!["", std_or_core, "intrinsics", "atomic_xchg_acq"]);
                let arg0 = self.convert_expr(ctx.used(), args[0])?;
                let arg1 = self.convert_expr(ctx.used(), args[1])?;
                Ok(arg0.and_then(|arg0| {
                    arg1.and_then(|arg1| {
                        let call_expr = mk().call_expr(atomic_func, vec![arg0, arg1]);
                        self.convert_side_effects_expr(
                            ctx,
                            vec![],
                            call_expr,
                            "Builtin is not supposed to be used",
                        )
                    })
                }))
            }

            "__sync_lock_release_1"
            | "__sync_lock_release_2"
            | "__sync_lock_release_4"
            | "__sync_lock_release_8"
            | "__sync_lock_release_16" => {
                self.use_feature("core_intrinsics");

                // Emit `atomic_store_rel(arg0, 0)`
                let atomic_func =
                    mk().path_expr(vec!["", std_or_core, "intrinsics", "atomic_store_rel"]);
                let arg0 = self.convert_expr(ctx.used(), args[0])?;
                Ok(arg0.and_then(|arg0| {
                    let zero = mk().lit_expr(mk().int_lit(0, ""));
                    let call_expr = mk().call_expr(atomic_func, vec![arg0, zero]);
                    self.convert_side_effects_expr(
                        ctx,
                        vec![],
                        call_expr,
                        "Builtin is not supposed to be used",
                    )
                }))
            }

            _ => Err(format_err!("Unimplemented builtin: {}", builtin_name).into()),
        }
    }

    // This translation logic handles converting code that uses
    // https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html
    fn convert_overflow_arith(
        &self,
        ctx: ExprContext,
        method_name: &str,
        args: &[CExprId],
    ) -> Result<WithStmts<P<Expr>>, TranslationError> {
        let a = self.convert_expr(ctx.used(), args[0])?;
        let mut b = self.convert_expr(ctx.used(), args[1])?;
        let mut c = self.convert_expr(ctx.used(), args[2])?;

        let overflowing = mk().method_call_expr(a.val, method_name, vec![b.val]);
        let sum_name = self.renamer.borrow_mut().fresh();
        let over_name = self.renamer.borrow_mut().fresh();
        let overflow_let = mk().local_stmt(P(mk().local(
            mk().tuple_pat(vec![
                mk().ident_pat(&sum_name),
                mk().ident_pat(over_name.clone()),
            ]),
            None as Option<P<Ty>>,
            Some(overflowing),
        )));

        let out_assign = mk().assign_expr(
            mk().unary_expr(ast::UnOp::Deref, c.val),
            mk().ident_expr(&sum_name),
        );

        let mut stmts = a.stmts;
        stmts.append(&mut b.stmts);
        stmts.append(&mut c.stmts);
        stmts.push(overflow_let);
        stmts.push(mk().expr_stmt(out_assign));

        Ok(WithStmts {
            stmts,
            val: mk().ident_expr(over_name),
        })
    }

    /// Convert a builtin_memcpy use by calling into libc's memcpy directly.
    fn convert_memcpy(
        &self,
        ctx: ExprContext,
        args: &[CExprId],
    ) -> Result<WithStmts<P<Expr>>, TranslationError> {
        let memcpy = mk().path_expr(vec!["", "libc", "memcpy"]);
        let dst = self.convert_expr(ctx.used(), args[0])?;
        let mut src = self.convert_expr(ctx.used(), args[1])?;
        let mut len = self.convert_expr(ctx.used(), args[2])?;
        let size_t = mk().path_ty(vec!["libc", "size_t"]);
        let len1 = mk().cast_expr(len.val, size_t);
        let memcpy_expr = mk().call_expr(memcpy, vec![dst.val, src.val, len1]);

        let mut stmts = dst.stmts;
        stmts.append(&mut src.stmts);
        stmts.append(&mut len.stmts);

        let val = if ctx.is_used() {
            memcpy_expr
        } else {
            stmts.push(mk().semi_stmt(memcpy_expr));
            self.panic_or_err("__builtin_memcpy not used")
        };

        Ok(WithStmts { stmts, val })
    }
}