-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add factorial & update examples/source, finalize translation validator
- Loading branch information
Showing
15 changed files
with
35 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
/// according to the cpp language specification, overflow of the addition | ||
/// operation of two signed integers is considered undefined behavior (UB). | ||
/// that's why `clang++` will add the `noundef` attribute to the return value | ||
/// and use `add nsw` instead of normal `add` instruction - in a purely syntactic | ||
/// way that strictly follows the cpp language specification. | ||
/// see `examples/ir_fixed/add/` for more details. | ||
int add(int a, int b) { | ||
return a + b; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
/// though rust panics when overflow happens, Alive2 assumes the **scope** | ||
/// of input parameters will not result in such overflow according to the | ||
/// cpp language specification. | ||
/// see `examples/source/add/add.cpp` for more details. | ||
pub fn add(a: i32, b: i32) -> i32 { | ||
a + b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
/// for the addition of two unsigned integers, Alive2 correctly identifies | ||
/// the different behaviors between source C++ (implicitly wrapping) and | ||
/// target Rust (panics when overflow happens). | ||
pub fn add(a: u32, b: u32) -> u32 { | ||
a + b | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
/// incorrect translation | ||
/// a trivial example where the return values from the | ||
/// source C++ and target Rust are different. | ||
int ret() { | ||
return 20170618; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
/// incorrect translation | ||
pub fn ret() -> i32 { | ||
20021030 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
int factorial(int n) { | ||
return n <= 1 ? 1 : n * factorial(n - 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
/// nested function call is not supported by alive2, | ||
/// so that the recursive call to `factorial` will be trivially | ||
/// marked as undefined behavior (UB) and rejected by alive2. | ||
pub fn factorial(n: i32) -> i32 { | ||
if n <= 1 { 1 } else { n.wrapping_mul(factorial(n - 1)) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,12 @@ | ||
//! generated by claude-3.5-sonnet at Nov 20, 2024 | ||
//! validation result: a seemingly correct translation of `simple_enum.cpp` | ||
#[repr(i32)] // Use same representation as C++ enum | ||
#[derive(Clone, Copy)] // Make it work like C++ value type | ||
#[repr(i32)] // use same representation as C++ enum | ||
#[derive(Clone, Copy)] // make it work like C++ value type | ||
pub enum Color { | ||
Red = 0, | ||
Green = 1, | ||
Blue = 2, | ||
} | ||
|
||
pub fn create_color(x: i32) -> Color { | ||
// Directly transmute the integer to enum, matching C++'s behavior | ||
// directly transmute the integer to enum, matching C++'s behavior | ||
unsafe { std::mem::transmute(x) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
//! translated by claude-3.5-sonnet at Nov 21, 2024 | ||
use std::ops::Add; | ||
|
||
// generic add function | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
/// correct translation | ||
/// a trivial example where the return value of the source C++ | ||
/// and target Rust are the same. | ||
int ret() { | ||
return 20021030; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
/// correct translation | ||
pub fn ret() -> i32 { | ||
20021030 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters